diff --git a/inc/catc.php b/inc/catc.php index 7821993..0ddabc5 100644 --- a/inc/catc.php +++ b/inc/catc.php @@ -12,7 +12,8 @@ class CATC extends Main { $asClasses = array( array('name'=>'auth', 'project'=>true), - array('name'=>'course', 'project'=>true) + array('name'=>'course', 'project'=>true), + array('name'=>'note', 'project'=>true) ); parent::__construct($oClassManagement, $sProcessPage, $asClasses); @@ -24,6 +25,7 @@ class CATC extends Main { //Install DB $this->oDb->install(); + $this->oDb->loadFile('db_build.sql'); } protected function getSqlOptions() @@ -34,9 +36,10 @@ class CATC extends Main ( Auth::USER_TABLE => array(Db::getText(Auth::USER_TABLE), 'nickname', 'pass', 'cookie'), Course::WS_TABLE => array('dates'), - Course::COURSE_TABLE=> array(Db::getId('workshops'), 'description', 'timeslot', 'notes'), - 'docs' => array(Db::getId('courses'), 'type', 'path'), - 'todos' => array(Db::getId('courses'), 'description') + Course::COURSE_TABLE=> array(Db::getId('workshops'), 'description', 'timeslot'), + 'notes' => array(Db::getId(Auth::USER_TABLE), Db::getId(Course::COURSE_TABLE), 'notes'), + 'docs' => array(Db::getId(Auth::USER_TABLE), Db::getId(Course::COURSE_TABLE), 'type', 'path'), + 'todos' => array(Db::getId(Auth::USER_TABLE), Db::getId(Course::COURSE_TABLE), 'description') ), 'types' => array ( @@ -145,19 +148,20 @@ class CATC extends Main /* Notes*/ public function getNote($iCourseId) { - $oCourse = new Course($this->oDb, $iCourseId); - $asNote = $oCourse->getNote(); - $iCourseId = $asNote[Db::getId(Course::COURSE_TABLE)]; + $oNote = new Note($this->oDb, $this->oAuth->getUserId(), $iCourseId); + $asNote = $oNote->getNote(); - return self::getJsonResult(($iCourseId > 0), '', array('id'=>$iCourseId, 'ops'=>$asNote['notes'], 'led'=>$asNote['led'])); + return self::getJsonResult(!empty($asNote), '', $asNote); } public function setNote($iCourseId, $asOps) { - $oCourse = new Course($this->oDb, $iCourseId); - $sError = $oCourse->setNote($asOps); + $oNote = new Note($this->oDb, $this->oAuth->getUserId(), $iCourseId); + $sError = $oNote->setNote($asOps); $bSuccess = ($sError==''); - return self::getJsonResult(($sError==''), $sError, array('led' => $oCourse->getNote()['led'])); + $asData = ($bSuccess)?array('led' => $oNote->getNote()['led']):array(); + + return self::getJsonResult($bSuccess, $sError, $asData); } /* diff --git a/inc/course.php b/inc/course.php index e43b148..8a16b0b 100644 --- a/inc/course.php +++ b/inc/course.php @@ -28,26 +28,6 @@ class Course extends PhpObject { $this->iCourseId = $iCourseId; } - public function getNote() { - $asCourse = $this->oDb->selectRow(self::COURSE_TABLE, $this->getCourseId(), array(Db::getId(self::COURSE_TABLE), 'notes', 'led')); - $asCourse['notes'] = json_decode($asCourse['notes'], true); - - return $asCourse; - } - - public function setNote($asOps) { - $sError = ''; - $sIdCol = Db::getId(self::COURSE_TABLE); - - if($this->getCourseId() > 0) { - $iCourseId = $this->oDb->insertUpdateRow(self::COURSE_TABLE, array($sIdCol=>$this->getCourseId(), 'notes'=>json_encode($asOps)), array($sIdCol)); - if(!$iCourseId) $sError = $this->oDb->getLastError(); - } - else $sError = 'Course ID not set'; - - return $sError; - } - public function getWorkshops() { $asCourses = $this->oDb->selectRows(array( 'select'=> array(Db::getId(self::WS_TABLE), 'dates', Db::getId(self::COURSE_TABLE), 'description', 'timeslot'), diff --git a/inc/note.php b/inc/note.php new file mode 100644 index 0000000..596dffa --- /dev/null +++ b/inc/note.php @@ -0,0 +1,57 @@ +oDb = &$oDb; + $this->setNoteId(0); + $this->setUserCourseId($iUserId, $iCourseId); + } + + public function setNoteId($iNoteId) { + $this->iNoteId = $iNoteId; + } + + public function setUserCourseId($iUserId, $iCourseId) { + $this->iUserId = $iUserId; + $this->iCourseId = $iCourseId; + } + + private function getNoteKeys() { + return array(Db::getId(Auth::USER_TABLE) => $this->iUserId, Db::getId(Course::COURSE_TABLE) => $this->iCourseId); + } + + public function getNote() { + $asCourse = $this->oDb->selectRow(self::NOTE_TABLE, $this->getNoteKeys()); + if(!empty($asCourse)) $asCourse['notes'] = json_decode($asCourse['notes'], true); + + return $asCourse; + } + + public function setNote($asOps) { + $sError = ''; + + if($this->iUserId > 0 && $this->iCourseId > 0) { + $asData = array_merge($this->getNoteKeys(), array('notes'=>json_encode($asOps))); + $iNoteId = $this->oDb->insertUpdateRow(self::NOTE_TABLE, $asData, array_keys($this->getNoteKeys())); + if(!$iNoteId) $sError = $this->oDb->getLastError(); + } + else $sError = 'Course ID not set'; + + return $sError; + } +} \ No newline at end of file diff --git a/masks/course.html b/masks/course.html index 3ee9717..fd3f89b 100644 --- a/masks/course.html +++ b/masks/course.html @@ -10,6 +10,7 @@ oCATC.pageInit = function(asHash, bFirstPage) { self.setPageTitle('Course'); self.tmp('id_course', asHash.items[0]); + //Setup Quill oEditor = new Editor('#notes'); oEditor.onKeyStroke = (e) => { if(e.which == 83 && e.ctrlKey) { @@ -17,8 +18,19 @@ oCATC.pageInit = function(asHash, bFirstPage) { save(true); } else save(); - } - oEditor.open(self.tmp('id_course')); + }; + + //Load notes + Tools.ajax( + 'get_note', + (asData) => { + oEditor.setContent(asData.ops); + oCATC.feedback('notice', 'Last update on ('+asData.led.substr(11, 5)+')'); + }, + {id: self.tmp('id_course')}, + () => {console.log('Note not found for course ID = '+self.tmp('id_course'))} + ); + //oQuill.keyboard.addBinding({key: 'S', ctrlKey: true}, function(){saveNotes(true);}); }; @@ -60,7 +72,7 @@ function save(bForce) self.tmp('saving', false); }, { - id: oEditor.id, + id: self.tmp('id_course'), content: sContent }, function(sError) { @@ -75,7 +87,7 @@ function save(bForce) } } else { - oSaveTimer = setTimeout(function(){save(true);}, 1000*10); + oSaveTimer = setTimeout(function(){save(true);}, 1000*5); } return true; } diff --git a/readme.md b/readme.md index a056f90..51c06c4 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ Prise de notes pour les cours du Collège des Arts Thérapeutiques Chinois # Todo -- [x] Upload docs: audio, video, word, pdf +- [ ] Upload docs: audio, video, word, pdf - [ ] View docs online: audio, video, word, pdf -- [ ] Take notes on courses -- [ ] Quick view of musckes / nerves schemas \ No newline at end of file +- [X] Take notes on courses +- [ ] Quick view of muscles / nerves schemas \ No newline at end of file diff --git a/scripts/catc.js b/scripts/catc.js index 8889c1b..a22d31d 100644 --- a/scripts/catc.js +++ b/scripts/catc.js @@ -277,12 +277,8 @@ function CATC(asGlobals) }; } - - - class Editor { constructor(sEditorId, bReadOnly) { - this.id = 0; this.keystrokes = 0; this.readOnly = bReadOnly || false; this.sCursorPos = ''; @@ -290,7 +286,7 @@ class Editor { //DOM Elements this.$Editor = $(sEditorId); - this.onKeyStroke = function(e){}; + this.onKeyStroke = function() {}; this.oQuill = new Quill(sEditorId, { theme: 'snow', @@ -319,18 +315,6 @@ class Editor { if(!this.readOnly) this.oQuill.focus(); } - open(iCourseId) { - Tools.ajax( - 'get_note', - (asData) => { - this.id = asData.id; - this.oQuill.setContents(asData.ops); - this._postInit(); - }, - {id: iCourseId} - ); - } - _incKeyStrokes() { this.keystrokes += 1; } @@ -339,6 +323,11 @@ class Editor { return this.oQuill.getContents().ops; } + setContent(asOps) { + this.oQuill.setContents(asOps); + this._postInit(); + } + isEmpty() { const rEmpty = /^(
(
|
|
|\s+|)<\/p>|)$/gm;
return rEmpty.test(this.oQuill.getText().trim());