diff --git a/amd/build/coursenotes.min.js b/amd/build/coursenotes.min.js index 182b138..d539ecb 100644 --- a/amd/build/coursenotes.min.js +++ b/amd/build/coursenotes.min.js @@ -1,3 +1,3 @@ -define("block_coursenotes/coursenotes",["exports","core/ajax","core/log"],(function(_exports,_ajax,_log){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_ajax=_interopRequireDefault(_ajax),_log=_interopRequireDefault(_log);_exports.init=()=>{let noteHistory=[];const courseNoteExists=""!==document.getElementById("coursenote-display").textContent.trim(),textarea=document.getElementById("coursenote-textarea");let timeoutId;courseNoteExists&&(document.getElementById("coursenote-form").style.display="none"),document.getElementById("edit-icon").addEventListener("click",(function(){document.getElementById("coursenote-display").style.display="none",this.style.display="none",document.getElementById("coursenote-form").style.display="block",fetchNoteHistory()})),document.getElementById("cancel-button").addEventListener("click",(function(){document.getElementById("coursenote-display").style.display="block",document.getElementById("edit-icon").style.display="block",document.getElementById("coursenote-form").style.display="none"})),document.getElementById("undo-button").addEventListener("click",(function(){if(noteHistory.length>1){noteHistory.pop();const lastNote=noteHistory[noteHistory.length-1];document.getElementById("coursenote-textarea").value=lastNote,noteHistory.length<=1&&(this.style.display="none")}}));const courseId=textarea.dataset.courseid;textarea.addEventListener("input",(()=>{clearTimeout(timeoutId),textarea.value.length>10&&(timeoutId=setTimeout((()=>{saveNoteAJAX(textarea.value)}),1e4))}));const saveNoteAJAX=note=>{let formattedNote;formattedNote=stripHtmlTags(note),_ajax.default.call([{methodname:"block_coursenotes_save_note",args:{coursenote:formattedNote,courseid:courseId},done:response=>{response.status?(noteHistory.push(note),noteHistory.shift(),_log.default.log("Note saved successfully")):_log.default.log("Error saving note:",response.message)},fail:error=>{_log.default.error("AJAX error: "+JSON.stringify(error))}}])},stripHtmlTags=str=>str.replace(/<\/?[^>]+(>|$)/g,""),fetchNoteHistory=()=>{_ajax.default.call([{methodname:"block_coursenotes_fetch_notes",args:{courseid:courseId},done:response=>{response.status?(noteHistory=response.notes,noteHistory.length>0&&(document.getElementById("undo-button").style.display="block"),_log.default.log(response),_log.default.log("Note history fetched successfully")):_log.default.log("Error fetching note history:",response.message)},fail:error=>{_log.default.error("AJAX error: "+JSON.stringify(error))}}])}}})); +define("block_coursenotes/coursenotes",["exports","core/ajax","core/log"],(function(_exports,_ajax,_log){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_ajax=_interopRequireDefault(_ajax),_log=_interopRequireDefault(_log);_exports.init=()=>{let noteHistory=[];const courseNoteExists=""!==document.getElementById("coursenote-display").textContent.trim(),textarea=document.getElementById("coursenote-textarea");let timeoutId;courseNoteExists&&(document.getElementById("coursenote-form").style.display="none"),document.getElementById("edit-icon").addEventListener("click",(function(){document.getElementById("coursenote-display").style.display="none",this.style.display="none",document.getElementById("coursenote-form").style.display="block",fetchNoteHistory()})),document.getElementById("cancel-button").addEventListener("click",(function(){document.getElementById("coursenote-display").style.display="block",document.getElementById("edit-icon").style.display="block",document.getElementById("coursenote-form").style.display="none"})),document.getElementById("undo-button").addEventListener("click",(function(){if(noteHistory.length>1){noteHistory.pop();const lastNote=noteHistory[noteHistory.length-1];document.getElementById("coursenote-textarea").value=lastNote,noteHistory.length<=1&&(this.style.display="none")}}));const courseId=textarea.dataset.courseid;textarea.addEventListener("input",(()=>{clearTimeout(timeoutId),textarea.value.length>10&&(timeoutId=setTimeout((()=>{saveNoteAJAX(textarea.value)}),1e4))}));const saveNoteAJAX=note=>{let formattedNote;formattedNote=stripHtmlTags(note),_ajax.default.call([{methodname:"block_coursenotes_save_note",args:{coursenote:formattedNote,courseid:courseId},done:response=>{response.status?(noteHistory.push(note),noteHistory.shift(),_log.default.log("Note saved successfully")):_log.default.log("Error saving note:",response.message)},fail:error=>{_log.default.error("AJAX error: "+JSON.stringify(error))}}])},stripHtmlTags=str=>str.replace(/<\/?[^>]+(>|$)/g,""),fetchNoteHistory=()=>{_ajax.default.call([{methodname:"block_coursenotes_fetch_notes",args:{courseid:courseId},done:response=>{response.status?(noteHistory=response.notes,response.note_count>1?document.getElementById("undo-button").style.display="block":document.getElementById("undo-button").style.display="none",_log.default.log("Note history fetched successfully")):_log.default.log("Error fetching note history:",response.message)},fail:error=>{_log.default.error("AJAX error: "+JSON.stringify(error))}}])}}})); //# sourceMappingURL=coursenotes.min.js.map \ No newline at end of file diff --git a/amd/build/coursenotes.min.js.map b/amd/build/coursenotes.min.js.map index 1c696b8..e810d1c 100644 --- a/amd/build/coursenotes.min.js.map +++ b/amd/build/coursenotes.min.js.map @@ -1 +1 @@ -{"version":3,"file":"coursenotes.min.js","sources":["../src/coursenotes.js"],"sourcesContent":["import Ajax from 'core/ajax';\nimport Log from 'core/log';\n\nexport const init = () => {\n let noteHistory = [];\n const courseNoteExists = document.getElementById('coursenote-display').textContent.trim() !== '';\n const textarea = document.getElementById('coursenote-textarea');\n\n // Hide form if course note exists.\n if (courseNoteExists) {\n document.getElementById('coursenote-form').style.display = 'none';\n }\n\n document.getElementById('edit-icon').addEventListener('click', function () {\n document.getElementById('coursenote-display').style.display = 'none';\n this.style.display = 'none'; // Hide the edit icon itself.\n document.getElementById('coursenote-form').style.display = 'block';\n\n fetchNoteHistory();\n });\n\n document.getElementById('cancel-button').addEventListener('click', function () {\n document.getElementById('coursenote-display').style.display = 'block';\n document.getElementById('edit-icon').style.display = 'block'; // Show the edit icon again.\n document.getElementById('coursenote-form').style.display = 'none';\n });\n\n document.getElementById('undo-button').addEventListener('click', function () {\n if (noteHistory.length > 1) {\n noteHistory.pop(); // Remove current note.\n const lastNote = noteHistory[noteHistory.length - 1];\n document.getElementById('coursenote-textarea').value = lastNote;\n if (noteHistory.length <= 1) {\n this.style.display = 'none'; // Hide undo button if only one note left.\n }\n }\n });\n\n let timeoutId;\n const courseId = textarea.dataset.courseid;\n\n textarea.addEventListener('input', () => {\n clearTimeout(timeoutId);\n\n if (textarea.value.length > 10) {\n timeoutId = setTimeout(() => {\n saveNoteAJAX(textarea.value);\n }, 10000);\n }\n });\n const saveNoteAJAX = (note) => {\n let formattedNote;\n formattedNote = stripHtmlTags(note);\n Ajax.call([{\n methodname: 'block_coursenotes_save_note',\n args: {\n coursenote: formattedNote,\n courseid: courseId\n },\n done: (response) => {\n if (response.status) {\n noteHistory.push(note);\n noteHistory.shift();\n Log.log('Note saved successfully');\n } else {\n Log.log('Error saving note:', response.message);\n }\n },\n fail: (error) => {\n Log.error('AJAX error: ' + JSON.stringify(error));\n }\n }]);\n };\n\n const stripHtmlTags = (str) => {\n return str.replace(/<\\/?[^>]+(>|$)/g, \"\");\n };\n\n const fetchNoteHistory = () => {\n Ajax.call([{\n methodname: 'block_coursenotes_fetch_notes',\n args: {\n courseid: courseId\n },\n done: (response) => {\n if (response.status) {\n noteHistory = response.notes;\n if (noteHistory.length > 0) {\n document.getElementById('undo-button').style.display = 'block';\n }\n Log.log(response);\n Log.log('Note history fetched successfully');\n } else {\n Log.log('Error fetching note history:', response.message);\n }\n },\n fail: (error) => {\n Log.error('AJAX error: ' + JSON.stringify(error));\n }\n }]);\n };\n};\n"],"names":["noteHistory","courseNoteExists","document","getElementById","textContent","trim","textarea","timeoutId","style","display","addEventListener","fetchNoteHistory","length","pop","lastNote","value","courseId","dataset","courseid","clearTimeout","setTimeout","saveNoteAJAX","note","formattedNote","stripHtmlTags","call","methodname","args","coursenote","done","response","status","push","shift","log","message","fail","error","JSON","stringify","str","replace","notes"],"mappings":"4VAGoB,SACZA,YAAc,SACZC,iBAAwF,KAArEC,SAASC,eAAe,sBAAsBC,YAAYC,OAC7EC,SAAWJ,SAASC,eAAe,2BAgCrCI,UA7BAN,mBACAC,SAASC,eAAe,mBAAmBK,MAAMC,QAAU,QAG/DP,SAASC,eAAe,aAAaO,iBAAiB,SAAS,WAC3DR,SAASC,eAAe,sBAAsBK,MAAMC,QAAU,YACzDD,MAAMC,QAAU,OACrBP,SAASC,eAAe,mBAAmBK,MAAMC,QAAU,QAE3DE,sBAGJT,SAASC,eAAe,iBAAiBO,iBAAiB,SAAS,WAC/DR,SAASC,eAAe,sBAAsBK,MAAMC,QAAU,QAC9DP,SAASC,eAAe,aAAaK,MAAMC,QAAU,QACrDP,SAASC,eAAe,mBAAmBK,MAAMC,QAAU,UAG/DP,SAASC,eAAe,eAAeO,iBAAiB,SAAS,cACzDV,YAAYY,OAAS,EAAG,CACxBZ,YAAYa,YACNC,SAAWd,YAAYA,YAAYY,OAAS,GAClDV,SAASC,eAAe,uBAAuBY,MAAQD,SACnDd,YAAYY,QAAU,SACjBJ,MAAMC,QAAU,kBAM3BO,SAAWV,SAASW,QAAQC,SAElCZ,SAASI,iBAAiB,SAAS,KAC/BS,aAAaZ,WAETD,SAASS,MAAMH,OAAS,KACxBL,UAAYa,YAAW,KACnBC,aAAaf,SAASS,SACvB,eAGLM,aAAgBC,WACdC,cACJA,cAAgBC,cAAcF,oBACzBG,KAAK,CAAC,CACPC,WAAY,8BACZC,KAAM,CACFC,WAAYL,cACZL,SAAUF,UAEda,KAAOC,WACCA,SAASC,QACT/B,YAAYgC,KAAKV,MACjBtB,YAAYiC,qBACRC,IAAI,yCAEJA,IAAI,qBAAsBJ,SAASK,UAG/CC,KAAOC,qBACCA,MAAM,eAAiBC,KAAKC,UAAUF,aAKhDb,cAAiBgB,KACZA,IAAIC,QAAQ,kBAAmB,IAGpC9B,iBAAmB,mBAChBc,KAAK,CAAC,CACPC,WAAY,gCACZC,KAAM,CACFT,SAAUF,UAEda,KAAOC,WACCA,SAASC,QACT/B,YAAc8B,SAASY,MACnB1C,YAAYY,OAAS,IACrBV,SAASC,eAAe,eAAeK,MAAMC,QAAU,sBAEvDyB,IAAIJ,uBACJI,IAAI,mDAEJA,IAAI,+BAAgCJ,SAASK,UAGzDC,KAAOC,qBACCA,MAAM,eAAiBC,KAAKC,UAAUF"} \ No newline at end of file +{"version":3,"file":"coursenotes.min.js","sources":["../src/coursenotes.js"],"sourcesContent":["import Ajax from 'core/ajax';\nimport Log from 'core/log';\n\nexport const init = () => {\n let noteHistory = [];\n const courseNoteExists = document.getElementById('coursenote-display').textContent.trim() !== '';\n const textarea = document.getElementById('coursenote-textarea');\n\n // Hide form if course note exists.\n if (courseNoteExists) {\n document.getElementById('coursenote-form').style.display = 'none';\n }\n\n document.getElementById('edit-icon').addEventListener('click', function () {\n document.getElementById('coursenote-display').style.display = 'none';\n this.style.display = 'none'; // Hide the edit icon itself.\n document.getElementById('coursenote-form').style.display = 'block';\n\n fetchNoteHistory();\n });\n\n document.getElementById('cancel-button').addEventListener('click', function () {\n document.getElementById('coursenote-display').style.display = 'block';\n document.getElementById('edit-icon').style.display = 'block'; // Show the edit icon again.\n document.getElementById('coursenote-form').style.display = 'none';\n });\n\n document.getElementById('undo-button').addEventListener('click', function () {\n if (noteHistory.length > 1) {\n noteHistory.pop(); // Remove current note.\n const lastNote = noteHistory[noteHistory.length - 1];\n document.getElementById('coursenote-textarea').value = lastNote;\n if (noteHistory.length <= 1) {\n this.style.display = 'none'; // Hide undo button if only one note left.\n }\n }\n });\n\n let timeoutId;\n const courseId = textarea.dataset.courseid;\n\n textarea.addEventListener('input', () => {\n clearTimeout(timeoutId);\n\n if (textarea.value.length > 10) {\n timeoutId = setTimeout(() => {\n saveNoteAJAX(textarea.value);\n }, 10000);\n }\n });\n const saveNoteAJAX = (note) => {\n let formattedNote;\n formattedNote = stripHtmlTags(note);\n Ajax.call([{\n methodname: 'block_coursenotes_save_note',\n args: {\n coursenote: formattedNote,\n courseid: courseId\n },\n done: (response) => {\n if (response.status) {\n noteHistory.push(note);\n noteHistory.shift();\n Log.log('Note saved successfully');\n } else {\n Log.log('Error saving note:', response.message);\n }\n },\n fail: (error) => {\n Log.error('AJAX error: ' + JSON.stringify(error));\n }\n }]);\n };\n\n const stripHtmlTags = (str) => {\n return str.replace(/<\\/?[^>]+(>|$)/g, \"\");\n };\n\n const fetchNoteHistory = () => {\n Ajax.call([{\n methodname: 'block_coursenotes_fetch_notes',\n args: {\n courseid: courseId\n },\n done: (response) => {\n if (response.status) {\n noteHistory = response.notes;\n if (response.note_count > 1) {\n document.getElementById('undo-button').style.display = 'block';\n } else {\n document.getElementById('undo-button').style.display = 'none';\n }\n Log.log('Note history fetched successfully');\n } else {\n Log.log('Error fetching note history:', response.message);\n }\n },\n fail: (error) => {\n Log.error('AJAX error: ' + JSON.stringify(error));\n }\n }]);\n };\n};\n"],"names":["noteHistory","courseNoteExists","document","getElementById","textContent","trim","textarea","timeoutId","style","display","addEventListener","fetchNoteHistory","length","pop","lastNote","value","courseId","dataset","courseid","clearTimeout","setTimeout","saveNoteAJAX","note","formattedNote","stripHtmlTags","call","methodname","args","coursenote","done","response","status","push","shift","log","message","fail","error","JSON","stringify","str","replace","notes","note_count"],"mappings":"4VAGoB,SACZA,YAAc,SACZC,iBAAwF,KAArEC,SAASC,eAAe,sBAAsBC,YAAYC,OAC7EC,SAAWJ,SAASC,eAAe,2BAgCrCI,UA7BAN,mBACAC,SAASC,eAAe,mBAAmBK,MAAMC,QAAU,QAG/DP,SAASC,eAAe,aAAaO,iBAAiB,SAAS,WAC3DR,SAASC,eAAe,sBAAsBK,MAAMC,QAAU,YACzDD,MAAMC,QAAU,OACrBP,SAASC,eAAe,mBAAmBK,MAAMC,QAAU,QAE3DE,sBAGJT,SAASC,eAAe,iBAAiBO,iBAAiB,SAAS,WAC/DR,SAASC,eAAe,sBAAsBK,MAAMC,QAAU,QAC9DP,SAASC,eAAe,aAAaK,MAAMC,QAAU,QACrDP,SAASC,eAAe,mBAAmBK,MAAMC,QAAU,UAG/DP,SAASC,eAAe,eAAeO,iBAAiB,SAAS,cACzDV,YAAYY,OAAS,EAAG,CACxBZ,YAAYa,YACNC,SAAWd,YAAYA,YAAYY,OAAS,GAClDV,SAASC,eAAe,uBAAuBY,MAAQD,SACnDd,YAAYY,QAAU,SACjBJ,MAAMC,QAAU,kBAM3BO,SAAWV,SAASW,QAAQC,SAElCZ,SAASI,iBAAiB,SAAS,KAC/BS,aAAaZ,WAETD,SAASS,MAAMH,OAAS,KACxBL,UAAYa,YAAW,KACnBC,aAAaf,SAASS,SACvB,eAGLM,aAAgBC,WACdC,cACJA,cAAgBC,cAAcF,oBACzBG,KAAK,CAAC,CACPC,WAAY,8BACZC,KAAM,CACFC,WAAYL,cACZL,SAAUF,UAEda,KAAOC,WACCA,SAASC,QACT/B,YAAYgC,KAAKV,MACjBtB,YAAYiC,qBACRC,IAAI,yCAEJA,IAAI,qBAAsBJ,SAASK,UAG/CC,KAAOC,qBACCA,MAAM,eAAiBC,KAAKC,UAAUF,aAKhDb,cAAiBgB,KACZA,IAAIC,QAAQ,kBAAmB,IAGpC9B,iBAAmB,mBAChBc,KAAK,CAAC,CACPC,WAAY,gCACZC,KAAM,CACFT,SAAUF,UAEda,KAAOC,WACCA,SAASC,QACT/B,YAAc8B,SAASY,MACnBZ,SAASa,WAAa,EACtBzC,SAASC,eAAe,eAAeK,MAAMC,QAAU,QAEvDP,SAASC,eAAe,eAAeK,MAAMC,QAAU,oBAEvDyB,IAAI,mDAEJA,IAAI,+BAAgCJ,SAASK,UAGzDC,KAAOC,qBACCA,MAAM,eAAiBC,KAAKC,UAAUF"} \ No newline at end of file diff --git a/amd/src/coursenotes.js b/amd/src/coursenotes.js index edf7592..ff58db0 100644 --- a/amd/src/coursenotes.js +++ b/amd/src/coursenotes.js @@ -85,10 +85,11 @@ export const init = () => { done: (response) => { if (response.status) { noteHistory = response.notes; - if (noteHistory.length > 0) { + if (response.note_count > 1) { document.getElementById('undo-button').style.display = 'block'; + } else { + document.getElementById('undo-button').style.display = 'none'; } - Log.log(response); Log.log('Note history fetched successfully'); } else { Log.log('Error fetching note history:', response.message); diff --git a/externallib.php b/externallib.php index 44e6b70..8d4b20a 100644 --- a/externallib.php +++ b/externallib.php @@ -147,7 +147,7 @@ public static function fetch_notes($courseid): array { $notelist[] = $note->coursenote; } - return ['status' => true, 'notes' => $notelist]; + return ['status' => true, 'notes' => $notelist, 'note_count' => count($notelist)]; } /** @@ -162,6 +162,7 @@ public static function fetch_notes_returns(): external_single_structure { 'notes' => new external_multiple_structure( new external_value(PARAM_TEXT, 'Course note') ), + 'note_count' => new external_value(PARAM_INT, 'Number of course notes'), ] ); } diff --git a/templates/block.mustache b/templates/block.mustache index 37fa0eb..fc52569 100644 --- a/templates/block.mustache +++ b/templates/block.mustache @@ -11,6 +11,6 @@ - +