diff --git a/server/moodle/blocks/homework/amd/build/filter.min.js.map b/server/moodle/blocks/homework/amd/build/filter.min.js.map index b0ee2b47..919def3c 100644 --- a/server/moodle/blocks/homework/amd/build/filter.min.js.map +++ b/server/moodle/blocks/homework/amd/build/filter.min.js.map @@ -1 +1 @@ -{"version":3,"file":"filter.min.js","sources":["../src/filter.js"],"sourcesContent":["import $ from 'jquery';\r\nimport Ajax from 'core/ajax';\r\n/**\r\n * Homework/amd/src/filter.js\r\n *\r\n * @package\r\n * @copyright 2024, cs-24-sw-5-13 \r\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\r\n *\r\n */\r\n\r\nexport const init = async() => {\r\n let courses;\r\n Ajax.call([{\r\n methodname: 'block_homework_get_courses',\r\n args: {},\r\n done: async function(response) {\r\n courses = JSON.parse(response.courses);\r\n for (const course in courses) {\r\n $('#filter').append('');\r\n }\r\n },\r\n fail: (error) => {\r\n throw new Error(`Failed to find courses: ${error}`);\r\n }\r\n }]);\r\n $('#filter').on('change', () => {\r\n Ajax.call([{\r\n methodname: 'block_homework_filter_homework',\r\n args: {filter: $('#filter').val()},\r\n done: async function(response) {\r\n let homeworks = JSON.parse(response.homework);\r\n document.getElementById(\"outer-box\").innerHTML = \"\";\r\n homeworks.forEach((homework) => {\r\n console.log(homework);\r\n let box = document.createElement(\"div\");\r\n box.classList.add(\"infobox\");\r\n\r\n let h22 = document.createElement(\"h2\");\r\n h22.innerHTML = `${homework.course}`;\r\n box.appendChild(h22);\r\n\r\n let h2 = document.createElement(\"h2\");\r\n h2.innerHTML = `${homework.name}`;\r\n box.appendChild(h2);\r\n\r\n let h3 = document.createElement(\"h3\");\r\n h3.innerHTML = homework.duedate;\r\n box.appendChild(h3);\r\n\r\n let p = document.createElement(\"p\");\r\n p.innerHTML = `${homework.intro}`;\r\n box.appendChild(p);\r\n\r\n let button = document.createElement(\"button\");\r\n button.classList.add(\"Timebutton\");\r\n button.innerHTML = \"Time\";\r\n box.appendChild(button);\r\n\r\n document.getElementById(\"outer-box\").appendChild(box);\r\n });\r\n },\r\n fail: (error) => {\r\n console.log(error);\r\n throw new Error(`Failed to find filtered homework: ${error}`);\r\n }\r\n }]);\r\n });\r\n};\r\n"],"names":["_interopRequireDefault","e","__esModule","default","_jquery","_ajax","_exports","init","async","courses","Ajax","call","methodname","args","done","response","JSON","parse","course","$","append","fullname","fail","error","Error","on","filter","val","homeworks","homework","document","getElementById","innerHTML","forEach","console","log","box","createElement","classList","add","h22","appendChild","h2","name","h3","duedate","p","intro","button"],"mappings":"kGAC6B,SAAAA,uBAAAC,GAAAA,OAAAA,GAAAA,EAAAC,WAAAD,EAAAE,CAAAA,QAAAF,EAAA;;;;;;;;kFAD7BG,QAAAJ,uBAAAI,SACAC,MAAAL,uBAAAK,OAmEEC,SAAAC,KAzDkBC,UAChB,IAAIC,QACJC,MAAIP,QAACQ,KAAK,CAAC,CACPC,WAAY,6BACZC,KAAM,CAAE,EACRC,KAAMN,eAAeO,UACjBN,QAAUO,KAAKC,MAAMF,SAASN,SAC9B,IAAK,MAAMS,UAAUT,SACjB,EAAAU,QAAAA,SAAE,WAAWC,OAAO,kBAAoBX,QAAQS,QAAQG,SAAW,KAAOZ,QAAQS,QAAQG,SAAW,YAE5G,EACDC,KAAOC,QACH,MAAM,IAAIC,MAAM,2BAA2BD,QAAQ,MAG3D,EAAAJ,QAAAA,SAAE,WAAWM,GAAG,UAAU,KACtBf,MAAIP,QAACQ,KAAK,CAAC,CACPC,WAAY,iCACZC,KAAM,CAACa,QAAQ,EAAAP,QAAChB,SAAC,WAAWwB,OAC5Bb,KAAMN,eAAeO,UACjB,IAAIa,UAAYZ,KAAKC,MAAMF,SAASc,UACpCC,SAASC,eAAe,aAAaC,UAAY,GACjDJ,UAAUK,SAASJ,WACfK,QAAQC,IAAIN,UACZ,IAAIO,IAAMN,SAASO,cAAc,OACjCD,IAAIE,UAAUC,IAAI,WAElB,IAAIC,IAAMV,SAASO,cAAc,MACjCG,IAAIR,UAAY,GAAGH,SAASX,SAC5BkB,IAAIK,YAAYD,KAEhB,IAAIE,GAAKZ,SAASO,cAAc,MAChCK,GAAGV,UAAY,GAAGH,SAASc,OAC3BP,IAAIK,YAAYC,IAEhB,IAAIE,GAAKd,SAASO,cAAc,MAChCO,GAAGZ,UAAYH,SAASgB,QACxBT,IAAIK,YAAYG,IAEhB,IAAIE,EAAIhB,SAASO,cAAc,KAC/BS,EAAEd,UAAY,GAAGH,SAASkB,QAC1BX,IAAIK,YAAYK,GAEhB,IAAIE,OAASlB,SAASO,cAAc,UACpCW,OAAOV,UAAUC,IAAI,cACrBS,OAAOhB,UAAY,OACnBI,IAAIK,YAAYO,QAEhBlB,SAASC,eAAe,aAAaU,YAAYL,IAAI,GAE5D,EACDd,KAAOC,QAEH,MADAW,QAAQC,IAAIZ,OACN,IAAIC,MAAM,qCAAqCD,QAAQ,IAElE,GACL,CACJ"} \ No newline at end of file +{"version":3,"file":"filter.min.js","sources":["../src/filter.js"],"sourcesContent":["import $ from 'jquery';\nimport Ajax from 'core/ajax';\n/**\n * Homework/amd/src/filter.js\n *\n * @package\n * @copyright 2024, cs-24-sw-5-13 \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n *\n */\n\nexport const init = async() => {\n let courses;\n Ajax.call([{\n methodname: 'block_homework_get_courses',\n args: {},\n done: async function(response) {\n courses = JSON.parse(response.courses);\n for (const course in courses) {\n $('#filter').append('');\n }\n },\n fail: (error) => {\n throw new Error(`Failed to find courses: ${error}`);\n }\n }]);\n $('#filter').on('change', () => {\n Ajax.call([{\n methodname: 'block_homework_filter_homework',\n args: {filter: $('#filter').val()},\n done: async function(response) {\n let homeworks = JSON.parse(response.homework);\n document.getElementById(\"outer-box\").innerHTML = \"\";\n homeworks.forEach((homework) => {\n console.log(homework);\n let box = document.createElement(\"div\");\n box.classList.add(\"infobox\");\n\n let h22 = document.createElement(\"h2\");\n h22.innerHTML = `${homework.course}`;\n box.appendChild(h22);\n\n let h2 = document.createElement(\"h2\");\n h2.innerHTML = `${homework.name}`;\n box.appendChild(h2);\n\n let h3 = document.createElement(\"h3\");\n h3.innerHTML = homework.duedate;\n box.appendChild(h3);\n\n let p = document.createElement(\"p\");\n p.innerHTML = `${homework.intro}`;\n box.appendChild(p);\n\n let button = document.createElement(\"button\");\n button.classList.add(\"Timebutton\");\n button.innerHTML = \"Time\";\n box.appendChild(button);\n\n document.getElementById(\"outer-box\").appendChild(box);\n });\n },\n fail: (error) => {\n console.log(error);\n throw new Error(`Failed to find filtered homework: ${error}`);\n }\n }]);\n });\n};\n"],"names":["_interopRequireDefault","e","__esModule","default","_jquery","_ajax","_exports","init","async","courses","Ajax","call","methodname","args","done","response","JSON","parse","course","$","append","fullname","fail","error","Error","on","filter","val","homeworks","homework","document","getElementById","innerHTML","forEach","console","log","box","createElement","classList","add","h22","appendChild","h2","name","h3","duedate","p","intro","button"],"mappings":"kGAC6B,SAAAA,uBAAAC,GAAAA,OAAAA,GAAAA,EAAAC,WAAAD,EAAAE,CAAAA,QAAAF,EAAA;;;;;;;;kFAD7BG,QAAAJ,uBAAAI,SACAC,MAAAL,uBAAAK,OAmEEC,SAAAC,KAzDkBC,UAChB,IAAIC,QACJC,MAAIP,QAACQ,KAAK,CAAC,CACPC,WAAY,6BACZC,KAAM,CAAE,EACRC,KAAMN,eAAeO,UACjBN,QAAUO,KAAKC,MAAMF,SAASN,SAC9B,IAAK,MAAMS,UAAUT,SACjB,EAAAU,QAAAA,SAAE,WAAWC,OAAO,kBAAoBX,QAAQS,QAAQG,SAAW,KAAOZ,QAAQS,QAAQG,SAAW,YAE5G,EACDC,KAAOC,QACH,MAAM,IAAIC,MAAM,2BAA2BD,QAAQ,MAG3D,EAAAJ,QAAAA,SAAE,WAAWM,GAAG,UAAU,KACtBf,MAAIP,QAACQ,KAAK,CAAC,CACPC,WAAY,iCACZC,KAAM,CAACa,QAAQ,EAAAP,QAAChB,SAAC,WAAWwB,OAC5Bb,KAAMN,eAAeO,UACjB,IAAIa,UAAYZ,KAAKC,MAAMF,SAASc,UACpCC,SAASC,eAAe,aAAaC,UAAY,GACjDJ,UAAUK,SAASJ,WACfK,QAAQC,IAAIN,UACZ,IAAIO,IAAMN,SAASO,cAAc,OACjCD,IAAIE,UAAUC,IAAI,WAElB,IAAIC,IAAMV,SAASO,cAAc,MACjCG,IAAIR,UAAY,GAAGH,SAASX,SAC5BkB,IAAIK,YAAYD,KAEhB,IAAIE,GAAKZ,SAASO,cAAc,MAChCK,GAAGV,UAAY,GAAGH,SAASc,OAC3BP,IAAIK,YAAYC,IAEhB,IAAIE,GAAKd,SAASO,cAAc,MAChCO,GAAGZ,UAAYH,SAASgB,QACxBT,IAAIK,YAAYG,IAEhB,IAAIE,EAAIhB,SAASO,cAAc,KAC/BS,EAAEd,UAAY,GAAGH,SAASkB,QAC1BX,IAAIK,YAAYK,GAEhB,IAAIE,OAASlB,SAASO,cAAc,UACpCW,OAAOV,UAAUC,IAAI,cACrBS,OAAOhB,UAAY,OACnBI,IAAIK,YAAYO,QAEhBlB,SAASC,eAAe,aAAaU,YAAYL,IAAI,GAE5D,EACDd,KAAOC,QAEH,MADAW,QAAQC,IAAIZ,OACN,IAAIC,MAAM,qCAAqCD,QAAQ,IAElE,GACL,CACJ"} \ No newline at end of file diff --git a/server/moodle/blocks/homework/block_homework.php b/server/moodle/blocks/homework/block_homework.php index 756d1288..0c7ed398 100644 --- a/server/moodle/blocks/homework/block_homework.php +++ b/server/moodle/blocks/homework/block_homework.php @@ -46,6 +46,7 @@ public function get_content() { $homeworks = []; foreach ($usercourses as $course) { // Fetch homeworks using get_records_select. + $tmp = $DB->get_records('homework', ['course_id' => $course->id]); foreach ($tmp as $tm) { $homeworks[] = $tm; @@ -60,13 +61,11 @@ public function get_content() { $this->content = new stdClass(); - // If the current page is a course then remove unrelated homework. if ($this->page->pagetype == 'course-view-topics') { $homeworks = $this->filter_homework_content($this->page->url, $homeworks); } - // Retrieving all of the user's completions. $homeworkcompletionrecords = $DB->get_records('completions', ['usermodified' => $USER->id]); diff --git a/server/moodle/blocks/homework/classes/external/filter_homework.php b/server/moodle/blocks/homework/classes/external/filter_homework.php index 7edc1b73..2492696e 100644 --- a/server/moodle/blocks/homework/classes/external/filter_homework.php +++ b/server/moodle/blocks/homework/classes/external/filter_homework.php @@ -79,6 +79,7 @@ public static function execute($filter) { } /** + * * @return external_single_structure an array of homework */ public static function execute_returns(): external_single_structure { @@ -88,6 +89,7 @@ public static function execute_returns(): external_single_structure { } /** + * * @param $filter - The current modules id * @param $homeworkarray - Array containing all homework to be filtered * @return array - The html to be shown client-side diff --git a/server/moodle/blocks/homework/classes/external/get_courses.php b/server/moodle/blocks/homework/classes/external/get_courses.php index 7eef023e..5f96fde0 100644 --- a/server/moodle/blocks/homework/classes/external/get_courses.php +++ b/server/moodle/blocks/homework/classes/external/get_courses.php @@ -1,70 +1,74 @@ -. - -/** - * homework/classes/external/get_homework_chooser.php - * A class defining an external API function - * - * @package block_homework - * @copyright 2024, cs-24-sw-5-13 - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - */ - -namespace block_homework\external; -defined('MOODLE_INTERNAL') || die(); - -global $CFG; -require_once("$CFG->libdir/externallib.php"); - -use coding_exception; -use core_external\external_api; -use dml_exception; -use external_function_parameters; -use external_value; -use external_single_structure; -use JsonException; - -class get_courses extends \external_api { - - /** - * @return external_function_parameters - */ - public static function execute_parameters(){ - return new external_function_parameters([]); - } - - /** - * @return array - * @throws JsonException - */ - public static function execute(){ - global $USER; - $usercourses = enrol_get_users_courses($USER->id, true); - - return ["courses" => json_encode($usercourses, JSON_THROW_ON_ERROR), JSON_THROW_ON_ERROR]; - } - - /** - * @return external_single_structure - */ - public static function execute_returns(){ - return new external_single_structure([ - 'courses' => new external_value(PARAM_TEXT, 'Data array of courses'), - ]); - } -} \ No newline at end of file +. + +/** + * homework/classes/external/get_homework_chooser.php + * A class defining an external API function + * + * @package block_homework + * @copyright 2024, cs-24-sw-5-13 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + */ + +namespace block_homework\external; +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once("$CFG->libdir/externallib.php"); + +use coding_exception; +use core_external\external_api; +use dml_exception; +use external_function_parameters; +use external_value; +use external_single_structure; +use JsonException; + +/** + * + */ +class get_courses extends \external_api { + /** + * + * @return external_function_parameters + */ + public static function execute_parameters() { + return new external_function_parameters([]); + } + + /** + * + * @return array + * @throws JsonException + */ + public static function execute() { + global $USER; + $usercourses = enrol_get_users_courses($USER->id, true); + + return ["courses" => json_encode($usercourses, JSON_THROW_ON_ERROR), JSON_THROW_ON_ERROR]; + } + + /** + * + * @return external_single_structure + */ + public static function execute_returns() { + return new external_single_structure([ + 'courses' => new external_value(PARAM_TEXT, 'Data array of courses'), + ]); + } +} diff --git a/server/moodle/blocks/homework/db/services.php b/server/moodle/blocks/homework/db/services.php index 96d7660e..3b9406bc 100644 --- a/server/moodle/blocks/homework/db/services.php +++ b/server/moodle/blocks/homework/db/services.php @@ -66,7 +66,7 @@ 'description' => 'Filter the homework', 'type' => 'read', 'ajax' => true, - ] + ], ]; $services = [ @@ -76,7 +76,7 @@ 'block_homework_save_homeworktime', 'block_homework_get_homework', 'block_homework_get_courses', - 'block_homework_filter_homework' + 'block_homework_filter_homework', ], 'restrictedusers' => 0, 'enabled' => 1, diff --git a/server/moodle/blocks/homework/tests/filter_homework_test.php b/server/moodle/blocks/homework/tests/filter_homework_test.php index 30ff51ea..a8f92f9d 100644 --- a/server/moodle/blocks/homework/tests/filter_homework_test.php +++ b/server/moodle/blocks/homework/tests/filter_homework_test.php @@ -1,5 +1,4 @@ assertEquals(1, count($tmparray)); $this->assertEquals($homeworksarray[1]["id"], $tmparray[0]["id"]); - $tmparray = filter_homework::filter("2", $homeworksarray); $this->assertEquals(1, count($tmparray)); $this->assertEquals($homeworksarray[0]["course"], $tmparray[0]["course"]); diff --git a/server/moodle/mod/homework/db/access.php b/server/moodle/mod/homework/db/access.php index c18c811a..1fd9400f 100644 --- a/server/moodle/mod/homework/db/access.php +++ b/server/moodle/mod/homework/db/access.php @@ -41,6 +41,17 @@ 'manager' => CAP_ALLOW, ], ], + // Add and remove materials. + 'mod/homework:edit' => [ + 'riskbitmask' => RISK_SPAM, + 'captype' => 'write', + 'contextlevel' => CONTEXT_MODULE, + 'archetypes' => [ + 'student' => CAP_PREVENT, + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW, + ], + ], // Functionality to add the activity to course. 'mod/homework:addinstance' => [ diff --git a/server/moodle/mod/homework/edit.php b/server/moodle/mod/homework/edit.php index 22ef35ac..b82f8f2f 100644 --- a/server/moodle/mod/homework/edit.php +++ b/server/moodle/mod/homework/edit.php @@ -36,6 +36,7 @@ $PAGE->set_url('/mod/homework/edit.php', ['id' => $id]); $PAGE->set_title(get_string('modulename', 'homework')); $PAGE->set_heading(get_string('modulename', 'homework')); +require_capability('mod/homework:edit', $context); // Adding secondary navigation links. if ($PAGE->has_secondary_navigation()) { diff --git a/server/moodle/mod/homework/version.php b/server/moodle/mod/homework/version.php index 145fa78d..a4d117fb 100644 --- a/server/moodle/mod/homework/version.php +++ b/server/moodle/mod/homework/version.php @@ -26,6 +26,6 @@ $plugin->component = 'mod_homework'; $plugin->release = '0.1.2'; -$plugin->version = 2024111800; +$plugin->version = 2024112100; $plugin->requires = 2022112800; $plugin->maturity = MATURITY_ALPHA; diff --git a/server/moodle/mod/homework/view.php b/server/moodle/mod/homework/view.php index 691a3476..47842536 100644 --- a/server/moodle/mod/homework/view.php +++ b/server/moodle/mod/homework/view.php @@ -61,19 +61,20 @@ // Add the submissions node to the secondary navigation. $PAGE->secondarynav->add_node($submissionsnode); - - // Example: Add another node, e.g., 'Edit Homework'. - try { - $editnode = navigation_node::create( - get_string('edit', 'moodle'), - new moodle_url('/mod/homework/edit.php', ['id' => $cm->id]), - navigation_node::TYPE_CUSTOM, - null, - 'editnav' - ); - $PAGE->secondarynav->add_node($editnode); - } catch (coding_exception | \core\exception\moodle_exception $e) { - debugging($e->getMessage(), DEBUG_DEVELOPER); + if (has_capability('mod/homework:edit', $context)) { + // Example: Add another node, e.g., 'Edit Homework'. + try { + $editnode = navigation_node::create( + get_string('edit', 'moodle'), + new moodle_url('/mod/homework/edit.php', ['id' => $cm->id]), + navigation_node::TYPE_CUSTOM, + null, + 'editnav' + ); + $PAGE->secondarynav->add_node($editnode); + } catch (coding_exception | \core\exception\moodle_exception $e) { + debugging($e->getMessage(), DEBUG_DEVELOPER); + } } } @@ -245,12 +246,12 @@ - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -if ($viewobj->canedit && !$viewobj->hashomework) { + * + * @package mod_homework + * @copyright 2024, cs-24-sw-5-01 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +if ($viewobj->canedit && !$viewobj->hashomework && has_capability('mod/homework:edit', $context)) { // Add the button for opening the homework chooser modal. echo html_writer::tag('button', get_string('openhomeworkchooser', 'mod_homework'), [ 'type' => 'button',