diff --git a/server/moodle/mod/homework/amd/src/homeworkchooser.js b/server/moodle/mod/homework/amd/src/homeworkchooser.js index 350c42f34..8b706e6e3 100644 --- a/server/moodle/mod/homework/amd/src/homeworkchooser.js +++ b/server/moodle/mod/homework/amd/src/homeworkchooser.js @@ -4,9 +4,9 @@ import MyModal from 'mod_homework/modal_homework'; import ModalEvents from 'core/modal_events'; /** - * homework/amd/src/homeworkchooser.js + * Homework/amd/src/modal_homework.js * - * @package mod_homework + * @package * @copyright 2024, cs-24-sw-5-01 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @@ -14,11 +14,11 @@ import ModalEvents from 'core/modal_events'; /** * Initializes the Homework Chooser Modal. - * - * @param {int} cmid - Course Module ID - * @param {string} title - Title for the modal + * @param {int} cmid + * @param {string} title + * @returns {Promise} */ -export const init = async (cmid, title) => { +export const init = async(cmid, title) => { $('#open-homework-chooser').on('click', () => { Ajax.call([{ methodname: 'mod_homework_get_homework_chooser', @@ -27,33 +27,32 @@ export const init = async (cmid, title) => { const modal = await MyModal.create({ title: title, body: `${response.html}`, - // footer: 'An example footer content', large: true, removeOnClose: true, }); - // Show the modal - modal.show(); + // Show the modal. + await modal.show(); - // Initialize elements once the modal content is rendered + // Initialize elements once the modal content is rendered. modal.getRoot().on(ModalEvents.shown, () => { - // Initialize the elements after modal is displayed + // Initialize the elements after modal is displayed. const startPageInput = modal.getRoot().find('#startPage')[0]; const endPageInput = modal.getRoot().find('#endPage')[0]; const radioButtons = modal.getRoot().find('input[name="option"]'); const testTextarea = modal.getRoot().find('#page-range-input')[0]; const testLink = modal.getRoot().find('#linkDiv')[0]; - // Attach event listeners for page input validation + // Attach event listeners for page input validation. startPageInput.addEventListener('input', validatePageRange); endPageInput.addEventListener('input', validatePageRange); - // Attach event listeners for radio buttons + // Attach event listeners for radio buttons. radioButtons.each((_, radio) => { radio.addEventListener('change', toggleInputs); }); - // Function to validate page range + // Function to validate page range. /** * */ @@ -72,7 +71,7 @@ export const init = async (cmid, title) => { } } - // Function to toggle between text and link inputs + // Function to toggle between text and link inputs. /** * */ @@ -87,11 +86,6 @@ export const init = async (cmid, title) => { } }); - // Attach an event listener to handle the modal hidden event - modal.getRoot().on(ModalEvents.hidden, () => { - console.log('Modal closed!'); - }); - // Attach event listeners for buttons modal.getRoot().on('click', '[data-action="submit"]', (e) => { e.preventDefault(); @@ -104,7 +98,7 @@ export const init = async (cmid, title) => { }); }, fail: (error) => { - console.error("Failed to load homework chooser content:", error); + throw new Error(`Failed to load homework chooser content: ${error}`); } }]); }); @@ -118,53 +112,51 @@ export const init = async (cmid, title) => { const handleFormSubmit = (modal) => { let inputField = modal.getRoot().find('#inputField').val(); - if (inputField === "") { - alert("Please fill in input field."); - return; + if (inputField.value === "") { + inputField.setCustomValidity("Please fill in the input field."); + inputField.reportValidity(); // Shows the custom message + event.preventDefault(); // Prevents form submission + } else { + inputField.setCustomValidity(""); // Clear the custom message } if (modal.getRoot().find('#option1').is(':checked')) { - let startPage = modal.getRoot().find('#startPage').val(); let endPage = modal.getRoot().find('#endPage').val(); - // AJAX call to send data to the server + // AJAX call to send data to the server. Ajax.call([{ - methodname: 'mod_homework_save_homework_literature', // Your PHP function that will handle the data + methodname: 'mod_homework_save_homework_literature', args: { inputfield: inputField, startpage: startPage, endpage: endPage, }, - done: function(response) { - console.log("Data saved successfully:", response); - // Close the modal after successful submission + done: function() { + // Close the modal after successful submission. modal.hide(); }, fail: function(error) { - console.error("Failed to save data:", error); + throw new Error(`Failed to save data: ${error}`); } }]); - } else if (modal.getRoot().find('#option2').is(':checked')) { - let link = modal.getRoot().find('#link').val(); - // AJAX call to send data to the server + // AJAX call to send data to the server. Ajax.call([{ - methodname: 'mod_homework_save_homework_link', // Your PHP function that will handle the data + methodname: 'mod_homework_save_homework_link', args: { inputfield: inputField, link: link, }, - done: function(response) { - console.log("Data saved successfully:", response); - // Close the modal after successful submission + done: function() { + // Close the modal after successful submission. modal.hide(); }, fail: function(error) { - console.error("Failed to save data:", error); + throw new Error(`Failed to save data: ${error}`); } }]); } -}; \ No newline at end of file +}; diff --git a/server/moodle/mod/homework/amd/src/modal_homework.js b/server/moodle/mod/homework/amd/src/modal_homework.js index d5d104faa..44827e687 100644 --- a/server/moodle/mod/homework/amd/src/modal_homework.js +++ b/server/moodle/mod/homework/amd/src/modal_homework.js @@ -1,9 +1,9 @@ import Modal from 'core/modal'; /** - * homework/amd/src/modal_homework.js + * Homework/amd/src/modal_homework.js * - * @package mod_homework + * @package * @copyright 2024, cs-24-sw-5-01 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * diff --git a/server/moodle/mod/homework/db/upgrade.php b/server/moodle/mod/homework/db/upgrade.php index 7ca3dfb7d..15ac90627 100644 --- a/server/moodle/mod/homework/db/upgrade.php +++ b/server/moodle/mod/homework/db/upgrade.php @@ -21,7 +21,7 @@ * @copyright 2024, cs-24-sw-5-01 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -function xmldb_homework_upgrade($oldversion) { +function xmldb_homework_upgrade($oldversion): bool { global $DB; $dbman = $DB->get_manager(); @@ -32,16 +32,82 @@ function xmldb_homework_upgrade($oldversion) { $table = new xmldb_table('homework'); // Adding fields to table homework. - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, 'Standard Moodle primary key'); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'Name of the homework'); - $table->add_field('intro', XMLDB_TYPE_TEXT, null, null, null, null, null, 'Introduction text for the homework'); - $table->add_field('introformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'Format of the introduction text'); - $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'Timestamp when the record was created'); - $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'Timestamp when the record was last modified'); - $table->add_field('duedate', XMLDB_TYPE_INTEGER, '10', null, null, null, null); + $table->add_field( + 'id', + XMLDB_TYPE_INTEGER, + '10', + null, + XMLDB_NOTNULL, + XMLDB_SEQUENCE, + null, + 'Standard Moodle primary key' + ); + $table->add_field( + 'name', + XMLDB_TYPE_CHAR, + '255', + null, + XMLDB_NOTNULL, + null, + null, + 'Name of the homework' + ); + $table->add_field( + 'intro', + XMLDB_TYPE_TEXT, + null, + null, + null, + null, + null, + 'Introduction text for the homework' + ); + $table->add_field( + 'introformat', + XMLDB_TYPE_INTEGER, + '4', + null, + XMLDB_NOTNULL, + null, + '0', + 'Format of the introduction text' + ); + $table->add_field( + 'timecreated', + XMLDB_TYPE_INTEGER, + '10', + null, + XMLDB_NOTNULL, + null, + null, + 'Timestamp when the record was created' + ); + $table->add_field( + 'timemodified', + XMLDB_TYPE_INTEGER, + '10', + null, + XMLDB_NOTNULL, + null, + null, + 'Timestamp when the record was last modified' + ); + $table->add_field( + 'duedate', + XMLDB_TYPE_INTEGER, + '10', + null, + null, + null, + null + ); // Adding keys to table homework. - $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table->add_key( + 'primary', + XMLDB_KEY_PRIMARY, + ['id'] + ); // Conditionally launch create table for homework. if (!$dbman->table_exists($table)) { @@ -52,16 +118,83 @@ function xmldb_homework_upgrade($oldversion) { $table = new xmldb_table('homework_literature'); // Adding fields to table homework_literature. - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, 'Standard Moodle primary key'); - $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null, 'Description of the homework literature'); - $table->add_field('startpage', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'Start page number'); - $table->add_field('endpage', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'End page number'); - $table->add_field('introformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'Format of the introduction text'); - $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'Timestamp when the record was created'); - $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'Timestamp when the record was last modified'); + $table->add_field( + 'id', + XMLDB_TYPE_INTEGER, + '10', + null, + XMLDB_NOTNULL, + XMLDB_SEQUENCE, + null, + 'Standard Moodle primary key' + ); + $table->add_field( + 'description', + XMLDB_TYPE_TEXT, + null, + null, + null, + null, + null, + 'Description of the homework literature' + ); + $table->add_field( + 'startpage', + XMLDB_TYPE_INTEGER, + '10', + null, + null, + null, + null, + 'Start page number' + ); + $table->add_field( + 'endpage', + XMLDB_TYPE_INTEGER, + '10', + null, + null, + null, + null, + 'End page number' + ); + $table->add_field( + 'introformat', + XMLDB_TYPE_INTEGER, + '4', + null, + XMLDB_NOTNULL, + null, + '0', + 'Format of the introduction text' + ); + $table->add_field( + 'timecreated', + XMLDB_TYPE_INTEGER, + '10', + null, + XMLDB_NOTNULL, + null, + null, + 'Timestamp when the record was created' + ); + $table->add_field( + 'timemodified', + XMLDB_TYPE_INTEGER, + '10', + null, + XMLDB_NOTNULL, + null, + null, + 'Timestamp when the record was last modified' + ); // Adding keys to table homework_literature. - $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table->add_key( + 'primary', + XMLDB_KEY_PRIMARY, + ['id'] + ); // Conditionally launch create table for homework_literature. if (!$dbman->table_exists($table)) { @@ -72,16 +205,74 @@ function xmldb_homework_upgrade($oldversion) { $table = new xmldb_table('homework_links'); // Adding fields to table homework_links. - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); - $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); - $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); - $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null); - $table->add_field('link', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); + $table->add_field( + 'id', + XMLDB_TYPE_INTEGER, + '10', + null, + XMLDB_NOTNULL, + XMLDB_SEQUENCE, + null + ); + $table->add_field( + 'usermodified', + XMLDB_TYPE_INTEGER, + '10', + null, + XMLDB_NOTNULL, + null, + '0' + ); + $table->add_field( + 'timecreated', + XMLDB_TYPE_INTEGER, + '10', + null, + XMLDB_NOTNULL, + null, + '0' + ); + $table->add_field( + 'timemodified', + XMLDB_TYPE_INTEGER, + '10', + null, + XMLDB_NOTNULL, + null, + '0' + ); + $table->add_field( + 'description', + XMLDB_TYPE_TEXT, + null, + null, + null, + null, + null + ); + $table->add_field( + 'link', + XMLDB_TYPE_TEXT, + null, + null, + XMLDB_NOTNULL, + null, + null + ); // Adding keys to table homework_links. - $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); - $table->add_key('usermodified', XMLDB_KEY_FOREIGN, ['usermodified'], 'user', ['id']); + $table->add_key( + 'primary', + XMLDB_KEY_PRIMARY, + ['id'] + ); + $table->add_key( + 'usermodified', + XMLDB_KEY_FOREIGN, + ['usermodified'], + 'user', + ['id'] + ); // Conditionally launch create table for homework_links. if (!$dbman->table_exists($table)) { @@ -89,9 +280,9 @@ function xmldb_homework_upgrade($oldversion) { } - // homework savepoint reached. + // Homework savepoint reached. upgrade_mod_savepoint(true, 2024090500, 'homework'); } return true; -} \ No newline at end of file +} diff --git a/server/moodle/mod/homework/lib.php b/server/moodle/mod/homework/lib.php index faff8b156..880e766cc 100644 --- a/server/moodle/mod/homework/lib.php +++ b/server/moodle/mod/homework/lib.php @@ -13,7 +13,6 @@ // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -// defined('MOODLE_INTERNAL') || die(); /** * lib functions for homework plugin @@ -41,7 +40,7 @@ function homework_add_instance($homeworkdata) { if (!empty($homeworkdata->duedateselector)) { $homeworkdata->duedate = $homeworkdata->duedateselector; // Store the due date as a UNIX timestamp. } else { - $homeworkdata->duedate = 0; // If no due date is set, store 0 in the database. + $homeworkdata->duedate = null; // If no due date is set, store null in the database. } $homeworkdata->id = $DB->insert_record('homework', $homeworkdata); diff --git a/server/moodle/mod/homework/tests/modal_test.php b/server/moodle/mod/homework/tests/modal_test.php index d1b942358..bab89ca9c 100644 --- a/server/moodle/mod/homework/tests/modal_test.php +++ b/server/moodle/mod/homework/tests/modal_test.php @@ -38,6 +38,7 @@ final class modal_test extends advanced_testcase { * @return void * @throws coding_exception * @runInSeparateProcess + * @covers :: \mod_homework\external\get_homework_chooser */ public function test_get_homework_chooser(): void { global $DB; diff --git a/server/moodle/mod/homework/tests/save_homework_test.php b/server/moodle/mod/homework/tests/save_homework_test.php index 8e8f3bc35..521c36e83 100644 --- a/server/moodle/mod/homework/tests/save_homework_test.php +++ b/server/moodle/mod/homework/tests/save_homework_test.php @@ -26,6 +26,7 @@ namespace mod_homework; use advanced_testcase; +use dml_exception; /** * @@ -42,7 +43,8 @@ protected function setUp(): void { /** * Test saving literature homework with page range. * @runInSeparateProcess - * @throws \dml_exception + * @throws dml_exception + * @covers :: \mod_homework\external\save_homework_literature */ public function test_save_homework_literature(): void { global $DB; @@ -73,7 +75,8 @@ public function test_save_homework_literature(): void { /** * Test saving a homework with a link. * @runInSeparateProcess - * @throws \dml_exception + * @throws dml_exception + * @covers :: \mod_homework\external\save_homework_link */ public function test_save_homework_link(): void { global $DB; diff --git a/server/moodle/mod/homework/version.php b/server/moodle/mod/homework/version.php index 4c69c0cfe..f8d6da733 100644 --- a/server/moodle/mod/homework/version.php +++ b/server/moodle/mod/homework/version.php @@ -13,7 +13,6 @@ // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -global $plugin; /** * Version metadata for the mod_homework plugin. @@ -22,14 +21,12 @@ * @copyright 2024, cs-24-sw-5-01 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ + defined('MOODLE_INTERNAL') || die(); +global $plugin; -$plugin->version = 2024101400; // The current module version (Date: YYYYMMDDXX). -$plugin->requires = 2024090300; // Requires this Moodle version. -$plugin->component = 'mod_homework'; // Full name of the plugin (used for diagnostics) -//$plugin->cron = 0; -//$plugin->dependencies = [ - // 'mod_forum' => 2022042100, - // 'mod_data' => 2022042100 -//]; \ No newline at end of file +$plugin->version = 2024101400; // The current module version (Date: YYYYMMDDXX). +$plugin->requires = 2024090300; // Requires this Moodle version. +$plugin->component = 'mod_homework'; // Full name of the plugin (used for diagnostics). +// $plugin->cron = 0; // Cron job setting.