Add user id to docs/todos/notes tables

This commit is contained in:
2019-09-07 12:38:37 +02:00
parent 81a17332b4
commit 42c6f8eff7
6 changed files with 97 additions and 55 deletions

View File

@@ -12,7 +12,8 @@ class CATC extends Main
{ {
$asClasses = array( $asClasses = array(
array('name'=>'auth', 'project'=>true), 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); parent::__construct($oClassManagement, $sProcessPage, $asClasses);
@@ -24,6 +25,7 @@ class CATC extends Main
{ {
//Install DB //Install DB
$this->oDb->install(); $this->oDb->install();
$this->oDb->loadFile('db_build.sql');
} }
protected function getSqlOptions() protected function getSqlOptions()
@@ -34,9 +36,10 @@ class CATC extends Main
( (
Auth::USER_TABLE => array(Db::getText(Auth::USER_TABLE), 'nickname', 'pass', 'cookie'), Auth::USER_TABLE => array(Db::getText(Auth::USER_TABLE), 'nickname', 'pass', 'cookie'),
Course::WS_TABLE => array('dates'), Course::WS_TABLE => array('dates'),
Course::COURSE_TABLE=> array(Db::getId('workshops'), 'description', 'timeslot', 'notes'), Course::COURSE_TABLE=> array(Db::getId('workshops'), 'description', 'timeslot'),
'docs' => array(Db::getId('courses'), 'type', 'path'), 'notes' => array(Db::getId(Auth::USER_TABLE), Db::getId(Course::COURSE_TABLE), 'notes'),
'todos' => array(Db::getId('courses'), 'description') '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 'types' => array
( (
@@ -145,19 +148,20 @@ class CATC extends Main
/* Notes*/ /* Notes*/
public function getNote($iCourseId) { public function getNote($iCourseId) {
$oCourse = new Course($this->oDb, $iCourseId); $oNote = new Note($this->oDb, $this->oAuth->getUserId(), $iCourseId);
$asNote = $oCourse->getNote(); $asNote = $oNote->getNote();
$iCourseId = $asNote[Db::getId(Course::COURSE_TABLE)];
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) { public function setNote($iCourseId, $asOps) {
$oCourse = new Course($this->oDb, $iCourseId); $oNote = new Note($this->oDb, $this->oAuth->getUserId(), $iCourseId);
$sError = $oCourse->setNote($asOps); $sError = $oNote->setNote($asOps);
$bSuccess = ($sError==''); $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);
} }
/* /*

View File

@@ -28,26 +28,6 @@ class Course extends PhpObject {
$this->iCourseId = $iCourseId; $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() { public function getWorkshops() {
$asCourses = $this->oDb->selectRows(array( $asCourses = $this->oDb->selectRows(array(
'select'=> array(Db::getId(self::WS_TABLE), 'dates', Db::getId(self::COURSE_TABLE), 'description', 'timeslot'), 'select'=> array(Db::getId(self::WS_TABLE), 'dates', Db::getId(self::COURSE_TABLE), 'description', 'timeslot'),

57
inc/note.php Normal file
View File

@@ -0,0 +1,57 @@
<?php
class Note extends PhpObject {
const NOTE_TABLE = 'notes';
/**
* DB
* @var Db
*/
private $oDb;
private $iNoteId;
private $iUserId;
private $iCourseId;
public function __construct(Db &$oDb, $iUserId, $iCourseId)
{
parent::__construct(__CLASS__, Settings::DEBUG);
$this->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;
}
}

View File

@@ -10,6 +10,7 @@ oCATC.pageInit = function(asHash, bFirstPage) {
self.setPageTitle('Course'); self.setPageTitle('Course');
self.tmp('id_course', asHash.items[0]); self.tmp('id_course', asHash.items[0]);
//Setup Quill
oEditor = new Editor('#notes'); oEditor = new Editor('#notes');
oEditor.onKeyStroke = (e) => { oEditor.onKeyStroke = (e) => {
if(e.which == 83 && e.ctrlKey) { if(e.which == 83 && e.ctrlKey) {
@@ -17,8 +18,19 @@ oCATC.pageInit = function(asHash, bFirstPage) {
save(true); save(true);
} }
else save(); 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);}); //oQuill.keyboard.addBinding({key: 'S', ctrlKey: true}, function(){saveNotes(true);});
}; };
@@ -60,7 +72,7 @@ function save(bForce)
self.tmp('saving', false); self.tmp('saving', false);
}, },
{ {
id: oEditor.id, id: self.tmp('id_course'),
content: sContent content: sContent
}, },
function(sError) { function(sError) {
@@ -75,7 +87,7 @@ function save(bForce)
} }
} }
else { else {
oSaveTimer = setTimeout(function(){save(true);}, 1000*10); oSaveTimer = setTimeout(function(){save(true);}, 1000*5);
} }
return true; return true;
} }

View File

@@ -2,7 +2,7 @@
Prise de notes pour les cours du Collège des Arts Thérapeutiques Chinois Prise de notes pour les cours du Collège des Arts Thérapeutiques Chinois
# Todo # Todo
- [x] Upload docs: audio, video, word, pdf - [ ] Upload docs: audio, video, word, pdf
- [ ] View docs online: audio, video, word, pdf - [ ] View docs online: audio, video, word, pdf
- [ ] Take notes on courses - [X] Take notes on courses
- [ ] Quick view of musckes / nerves schemas - [ ] Quick view of muscles / nerves schemas

View File

@@ -277,12 +277,8 @@ function CATC(asGlobals)
}; };
} }
class Editor { class Editor {
constructor(sEditorId, bReadOnly) { constructor(sEditorId, bReadOnly) {
this.id = 0;
this.keystrokes = 0; this.keystrokes = 0;
this.readOnly = bReadOnly || false; this.readOnly = bReadOnly || false;
this.sCursorPos = ''; this.sCursorPos = '';
@@ -290,7 +286,7 @@ class Editor {
//DOM Elements //DOM Elements
this.$Editor = $(sEditorId); this.$Editor = $(sEditorId);
this.onKeyStroke = function(e){}; this.onKeyStroke = function() {};
this.oQuill = new Quill(sEditorId, { this.oQuill = new Quill(sEditorId, {
theme: 'snow', theme: 'snow',
@@ -319,18 +315,6 @@ class Editor {
if(!this.readOnly) this.oQuill.focus(); 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() { _incKeyStrokes() {
this.keystrokes += 1; this.keystrokes += 1;
} }
@@ -339,6 +323,11 @@ class Editor {
return this.oQuill.getContents().ops; return this.oQuill.getContents().ops;
} }
setContent(asOps) {
this.oQuill.setContents(asOps);
this._postInit();
}
isEmpty() { isEmpty() {
const rEmpty = /^(<p>(<br>|<br\/>|<br\s\/>|\s+|)<\/p>|)$/gm; const rEmpty = /^(<p>(<br>|<br\/>|<br\s\/>|\s+|)<\/p>|)$/gm;
return rEmpty.test(this.oQuill.getText().trim()); return rEmpty.test(this.oQuill.getText().trim());