From 0b14abf3c02a383d2341049e791475dedb1564ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolaj=20Skj=C3=B8dt?= Date: Fri, 15 Nov 2024 12:45:04 +0100 Subject: [PATCH] Fixed CodeSniffer --- .../homework/classes/external/delete_file.php | 174 ++++++++++-------- .../external/edit_homework_material.php | 12 +- .../external/save_homework_material.php | 11 +- server/moodle/mod/homework/db/services.php | 4 - server/moodle/mod/homework/edit.php | 53 ++++-- server/moodle/mod/homework/lib.php | 20 +- .../tests/behat/behat_mod_homework.php | 161 ---------------- .../tests/behat/homework_add_homework.feature | 61 ------ .../mod/homework/tests/edit_homework_test.php | 2 +- .../mod/homework/tests/save_homework_test.php | 2 +- server/moodle/mod/homework/view.php | 55 ++++-- 11 files changed, 209 insertions(+), 346 deletions(-) delete mode 100644 server/moodle/mod/homework/tests/behat/behat_mod_homework.php delete mode 100644 server/moodle/mod/homework/tests/behat/homework_add_homework.feature diff --git a/server/moodle/mod/homework/classes/external/delete_file.php b/server/moodle/mod/homework/classes/external/delete_file.php index 7d0cffe6..26aa4aa7 100644 --- a/server/moodle/mod/homework/classes/external/delete_file.php +++ b/server/moodle/mod/homework/classes/external/delete_file.php @@ -1,76 +1,98 @@ -libdir/externallib.php"); - -use core\exception\moodle_exception; -use external_api; -use external_function_parameters; -use external_value; -use context_system; - -/** - * Class to handle file deletion for mod_homework. - */ -class delete_file extends external_api { - - /** - * Define the parameters for delete_file. - * - * @return external_function_parameters - */ - public static function execute_parameters() { - return new external_function_parameters( - array( - 'id' => new external_value(PARAM_INT, 'The ID of the homework to update'), - 'file_id' => new external_value(PARAM_INT, 'The ID of the file to delete'), - ) - ); - } - - /** - * Deletes the specified file. - * - * @param int $id The ID of the homework to update. - * @param int $file_id The ID of the file to delete. - * @return bool True if the file was successfully deleted, false otherwise. - */ - public static function execute($id, $file_id) { - global $DB; - - // Validate parameters. - $params = self::validate_parameters(self::execute_parameters(), array( - 'id' => $id, - 'file_id' => $file_id, - )); - - // Ensure the user is logged in and validate context. - require_login(); - $context = context_system::instance(); - self::validate_context($context); - - // Include the library function to delete the file. - require_once(__DIR__ . '/../../lib.php'); - $success = mod_homework_delete_file( - $params['id'], - $params['file_id'] - ); - - if (!$success) { - throw new moodle_exception('deleteerror', 'mod_homework', '', null, 'File deletion failed.'); - } - - return true; - } - - /** - * Define the return type for delete_file. - * - * @return external_value - */ - public static function execute_returns() { - return new external_value(PARAM_BOOL, 'True if file was deleted successfully'); - } -} +. + +/** + * homework/classes/external/delete_file.php + * + * @package mod_homework + * @copyright 2024, cs-24-sw-5-01 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_homework\external; + +defined('MOODLE_INTERNAL') || die(); + +require_once("$CFG->libdir/externallib.php"); + +use core\exception\moodle_exception; +use external_api; +use external_function_parameters; +use external_value; +use context_system; + +/** + * Class to handle file deletion for mod_homework. + */ +class delete_file extends external_api { + /** + * Define the parameters for delete_file. + * + * @return external_function_parameters + */ + public static function execute_parameters() { + return new external_function_parameters( + [ + 'id' => new external_value(PARAM_INT, 'The ID of the homework to update'), + 'fileid' => new external_value(PARAM_INT, 'The ID of the file to delete'), + ] + ); + } + + /** + * Deletes the specified file. + * + * @param int $id The ID of the homework to update. + * @param int $fileid The ID of the file to delete. + * @return bool True if the file was successfully deleted, false otherwise. + */ + public static function execute($id, $fileid) { + global $DB; + + // Validate parameters. + $params = self::validate_parameters(self::execute_parameters(), [ + 'id' => $id, + 'file_id' => $fileid, + ]); + + // Ensure the user is logged in and validate context. + require_login(); + $context = context_system::instance(); + self::validate_context($context); + + // Include the library function to delete the file. + require_once(__DIR__ . '/../../lib.php'); + $success = mod_homework_delete_file( + $params['id'], + $params['file_id'] + ); + + if (!$success) { + throw new moodle_exception('deleteerror', 'mod_homework', '', null, 'File deletion failed.'); + } + + return true; + } + + /** + * Define the return type for delete_file. + * + * @return external_value + */ + public static function execute_returns() { + return new external_value(PARAM_BOOL, 'True if file was deleted successfully'); + } +} diff --git a/server/moodle/mod/homework/classes/external/edit_homework_material.php b/server/moodle/mod/homework/classes/external/edit_homework_material.php index 416ed0e1..1b2b1965 100644 --- a/server/moodle/mod/homework/classes/external/edit_homework_material.php +++ b/server/moodle/mod/homework/classes/external/edit_homework_material.php @@ -72,7 +72,17 @@ public static function execute_parameters() { * @return string[] * @throws \dml_exception */ - public static function execute($id, $inputfield, $homeworkid, $link = null, $startpage = null, $endpage = null, $starttime = null, $endtime = null, $fileid = null) { + public static function execute( + $id, + $inputfield, + $homeworkid, + $link = null, + $startpage = null, + $endpage = null, + $starttime = null, + $endtime = null, + $fileid = null + ) { global $DB, $USER; $record = new \stdClass(); diff --git a/server/moodle/mod/homework/classes/external/save_homework_material.php b/server/moodle/mod/homework/classes/external/save_homework_material.php index d52ff9b0..d10a48fb 100644 --- a/server/moodle/mod/homework/classes/external/save_homework_material.php +++ b/server/moodle/mod/homework/classes/external/save_homework_material.php @@ -70,7 +70,16 @@ public static function execute_parameters() { * @return string[] * @throws \dml_exception */ - public static function execute($inputfield, $homeworkid, $link = null, $startpage = null, $endpage = null, $starttime = null, $endtime = null, $fileid = null) { + public static function execute( + $inputfield, + $homeworkid, + $link = null, + $startpage = null, + $endpage = null, + $starttime = null, + $endtime = null, + $fileid = null + ) { global $DB, $USER; $record = new \stdClass(); diff --git a/server/moodle/mod/homework/db/services.php b/server/moodle/mod/homework/db/services.php index 96a77309..82ccfdf4 100644 --- a/server/moodle/mod/homework/db/services.php +++ b/server/moodle/mod/homework/db/services.php @@ -80,7 +80,3 @@ 'enabled' => 1, ], ]; - - - - diff --git a/server/moodle/mod/homework/edit.php b/server/moodle/mod/homework/edit.php index 0001b3cd..22ef35ac 100644 --- a/server/moodle/mod/homework/edit.php +++ b/server/moodle/mod/homework/edit.php @@ -85,12 +85,12 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ foreach ($homeworkmaterials as $materials) : - // Generate the preview URL for the file if it exists + // Generate the preview URL for the file if it exists. if ($materials->file_id !== null) { - // Retrieve additional metadata for generating the URL + // Retrieve additional metadata for generating the URL. $file = $DB->get_record('files', ['id' => $materials->file_id]); if ($file) { - // Generate the preview URL using Moodle's pluginfile.php + // Generate the preview URL using Moodle's pluginfile.php. $previewurl = moodle_url::make_pluginfile_url( $file->contextid, $file->component, @@ -103,16 +103,35 @@ } ?> -
+

description) ?>

startpage !== null && $materials->endpage !== null) : ?> -

startpage) . " - " . htmlspecialchars($materials->endpage) ?>

+

startpage) . " - " . + htmlspecialchars($materials->endpage) ?> +

link !== null) : ?> -

link) ?>

+

+ link) ?> + +

starttime !== null && $materials->endtime !== null) : ?> -

starttime) . " - " . htmlspecialchars($materials->endtime) ?>

+

starttime) . " - " . + htmlspecialchars($materials->endtime) ?> +

file_id !== null && isset($previewurl)) : ?> @@ -124,7 +143,13 @@ -

filename) ?> (Preview)

+

+ + filename) ?> + (Preview) +

@@ -168,10 +193,16 @@ ]; }, $homeworkmaterials); -$PAGE->requires->js_call_amd('mod_homework/homeworkchooseredit', 'init', [$cm->id, - get_string('homeworkchooser', 'mod_homework'), $instance->id, $homeworkmaterialids]); +$PAGE->requires->js_call_amd('mod_homework/homeworkchooseredit', 'init', [ + $cm->id, + get_string('homeworkchooser', 'mod_homework'), + $instance->id, $homeworkmaterialids, +]); -$PAGE->requires->js_call_amd('mod_homework/homeworkchooserdelete', 'init', [$cm->id, $homeworkmaterialids]); +$PAGE->requires->js_call_amd('mod_homework/homeworkchooserdelete', 'init', [ + $cm->id, + $homeworkmaterialids, +]); // Output the footer - REQUIRED. echo $OUTPUT->footer(); diff --git a/server/moodle/mod/homework/lib.php b/server/moodle/mod/homework/lib.php index 708d9531..2cef7ca6 100644 --- a/server/moodle/mod/homework/lib.php +++ b/server/moodle/mod/homework/lib.php @@ -153,18 +153,18 @@ function mod_homework_pluginfile( * @param int $file_id The file ID to delete. * @return bool True if deletion was successful, false otherwise. */ -function mod_homework_delete_file($id, $file_id) { +function mod_homework_delete_file($id, $fileid) { global $DB; $fs = get_file_storage(); - // Get the file record - $file = $DB->get_record('files', ['id' => $file_id]); + // Get the file record. + $file = $DB->get_record('files', ['id' => $fileid]); if (!$file) { return false; } - // Load the file from file storage and delete it - $stored_file = $fs->get_file( + // Load the file from file storage and delete it. + $storedfile = $fs->get_file( $file->contextid, $file->component, $file->filearea, @@ -173,20 +173,18 @@ function mod_homework_delete_file($id, $file_id) { $file->filename ); - if ($stored_file) { - // Delete the file record from the database - $stored_file->delete(); + if ($storedfile) { + // Delete the file record from the database. + $storedfile->delete(); $record = new \stdClass(); $record->id = $id; $record->file_id = null; - // Update the record from the database + // Update the record from the database. $DB->update_record('homework_materials', $record); } return true; } - - diff --git a/server/moodle/mod/homework/tests/behat/behat_mod_homework.php b/server/moodle/mod/homework/tests/behat/behat_mod_homework.php deleted file mode 100644 index 5a86e70d..00000000 --- a/server/moodle/mod/homework/tests/behat/behat_mod_homework.php +++ /dev/null @@ -1,161 +0,0 @@ - $this->get_cm_by_homework_name($page)->id]); - default: - throw new Exception('Unrecognised homework page type "' . $page . '."'); - } - } - - /** - * Get a course module ID by homework name. - * - * @param string $name Homework name. - * @return stdClass The corresponding course module. - */ - protected function get_cm_by_homework_name(string $name): stdClass { - global $DB; - $homework = $DB->get_record('homework', ['name' => $name], '*', MUST_EXIST); - return get_coursemodule_from_instance('homework', $homework->id, $homework->course); - } - - /** - * Add a literature item to the homework activity. - * - * @When /^I add a "(?P(?:[^"]|\\")*)" item to the "(?P(?:[^"]|\\")*)" homework with:$/ - * @param string $item_type The type of item to add (e.g., Literature). - * @param string $homework_name The name of the homework activity. - * @param TableNode $data The data table with item details. - */ - public function i_add_item_to_homework_with($item_type, $homework_name, TableNode $data) { - $this->execute("behat_general::i_click_on", ["{$homework_name}", "link"]); - - // Assuming fields like "Name", "Description", and "Link" in the TableNode. - $item_data = $data->getRowsHash(); - $this->execute("behat_general::i_set_the_field", ["Name", $item_data['Literature name']]); - $this->execute("behat_general::i_set_the_field", ["Description", $item_data['Description']]); - $this->execute("behat_general::i_set_the_field", ["Link", $item_data['Link']]); - - $this->execute("behat_general::i_click_on", ["Save", "button"]); - } - - /** - * Go to a specific page of the homework activity as a specific user - * @When /^I am on the "(?P(?:[^"]|\\")*)" homework activity page logged in as "(?P(?:[^"]|\\")*)"$/ - * @param string $activity_name The name of the homework activity. - * @param string $username The username to log in as. - */ - public function i_am_on_homework_activity_page_logged_in_as($activity_name, $username) { - global $DB; - - // Log in as the specified user. - $user = $DB->get_record('user', ['username' => $username], '*', MUST_EXIST); - // $this->set_user($user); - - // Get the course module ID for the homework activity. - $cm = $DB->get_record_sql(" - SELECT cm.id - FROM {course_modules} cm - JOIN {modules} m ON m.id = cm.module - JOIN {homework} h ON h.id = cm.instance - WHERE m.name = 'homework' AND h.name = :activity_name - ", ['activity_name' => $activity_name], MUST_EXIST); - - // Navigate to the homework activity page. - $url = new moodle_url('/mod/homework/view.php', ['id' => $cm->id]); - $this->getSession()->visit($url->out()); - } - - - /** - * Assert that specific text is visible in the homework activity. - * - * @Then /^I should see "(?P(?:[^"]|\\")*)" in the homework activity$/ - * @param string $text The text to check for. - * @throws ExpectationException If the text is not found. - */ - public function i_should_see_text_in_homework($text) { - $this->assert_page_contains_text($text); - } - - /** - * Submit a homework as a student. - * - * @Given /^user "([^"]*)" has submitted homework "([^"]*)" with:$/ - * @param string $username The username of the student. - * @param string $homeworkname The name of the homework activity. - * @param TableNode $submissiondata The data for the homework submission. - */ - public function user_has_submitted_homework($username, $homeworkname, TableNode $submissiondata) { - global $DB; - - // Get the homework and user records. - $homework = $DB->get_record('homework', ['name' => $homeworkname], '*', MUST_EXIST); - $user = $DB->get_record('user', ['username' => $username], '*', MUST_EXIST); - // $this->set_user($user); - - // Submission logic goes here, depending on the module's structure. - $submission_data = $submissiondata->getRowsHash(); - $this->execute("behat_general::i_set_the_field", ["Submission Text", $submission_data['submission_text']]); - $this->execute("behat_general::i_click_on", ["Submit", "button"]); - - // $this->set_user(); // Reset the user after the action. - } - - /** - * Check that specific feedback text appears after submission. - * - * @Then /^I should see feedback "(?P(?:[^"]|\\")*)" in the homework activity$/ - * @param string $text The feedback text to check for. - * @throws ExpectationException If the feedback is not found. - */ - public function i_should_see_feedback_in_homework($text) { - $this->assert_page_contains_text($text); - } - - /** - * Ensure a student sees the grade after submission. - * - * @Then /^user "([^"]*)" should see grade "([^"]*)" for homework "([^"]*)"$/ - * @param string $username The username of the student. - * @param string $grade The expected grade. - * @param string $homeworkname The name of the homework activity. - * @throws ExpectationException If the grade is not found. - */ - public function user_should_see_grade_for_homework($username, $grade, $homeworkname) { - global $DB; - - $user = $DB->get_record('user', ['username' => $username], '*', MUST_EXIST); - // $this->set_user($user); - - $this->execute("behat_navigation::i_am_on_page_instance", [$homeworkname, "mod_homework > view"]); - $this->assert_page_contains_text("Grade: $grade"); - - // $this->set_user(); // Reset the user after checking. - } -} diff --git a/server/moodle/mod/homework/tests/behat/homework_add_homework.feature b/server/moodle/mod/homework/tests/behat/homework_add_homework.feature deleted file mode 100644 index 12011e0c..00000000 --- a/server/moodle/mod/homework/tests/behat/homework_add_homework.feature +++ /dev/null @@ -1,61 +0,0 @@ -@mod @mod_homework -Feature: Add a homework assignment - In order to assign tasks to students - As a teacher - I need to create a homework assignment - - - # Background: - # Given the following "users" exist: - # | username | firstname | lastname | email | - # | teacher1 | Terry1 | Teacher1 | teacher1@example.com | - # | student1 | Sam1 | Student1 | student1@example.com | - # And the following "courses" exist: - # | fullname | shortname | category | - # | Course 1 | C1 | 0 | - # And the following "course enrolments" exist: - # | user | course | role | - # | teacher1 | C1 | editingteacher | - # | student1 | C1 | student | - # And the following "activity" exists: - # | activity | homework | - # | course | C1 | - # | idnumber | 00001 | - # | name | Test homework name | - # | intro | Test homework description | - # | section | 1 | - # | grade | 10 | - - @javascript - Scenario: Login and add homework - When I log in as "admin" - And I am on "Course 1" course homepage with editing mode on - And I add a "Homework" to section "1" using the activity chooser - # And I add a "Homework" item to the "Test homework name" homework with: - # | Literature name | First literature item | - # | Description | Read the first chapter | - # | Link | https://example.com/resource | - And I log out - - # And I am on the "Test homework name" homework activity page logged in as "student1" - # And I press "Submit homework" - # Then I should see "First literature item" - # And I should see "Read the first chapter" - # And I set the field "Link" to "https://example.com/resource" - # And I press "Finish submission" - # And I should see "Submission saved" - # And I press "Submit all and finish" - - @javascript @skip_chrome_zerosize - Scenario: Add and configure small homework and perform a submission as a student with Javascript enabled - Then I click on "Submit all and finish" "button" in the "Submit all your answers and finish?" "dialogue" - And I should see "Thank you for your submission" - And I should see "Please check back for feedback" - And I follow "Finish review" - And I should see "Grade: 0.00 / 10.00." - - Scenario: Add and configure small homework and perform a submission as a student with Javascript disabled - Then I should see "Thank you for your submission" - And I should see "Please check back for feedback" - And I follow "Finish review" - And I should see "Grade: 0.00 / 10.00." diff --git a/server/moodle/mod/homework/tests/edit_homework_test.php b/server/moodle/mod/homework/tests/edit_homework_test.php index 228fbd24..6a3fc7a6 100644 --- a/server/moodle/mod/homework/tests/edit_homework_test.php +++ b/server/moodle/mod/homework/tests/edit_homework_test.php @@ -305,4 +305,4 @@ public function test_edit_homework_video(): void { $this->assertEquals($newendtime, $updatedrecord->endtime); $this->assertEquals($homeworkid, $updatedrecord->homework_id); } -} \ No newline at end of file +} diff --git a/server/moodle/mod/homework/tests/save_homework_test.php b/server/moodle/mod/homework/tests/save_homework_test.php index 35e414bb..aa3a0d67 100644 --- a/server/moodle/mod/homework/tests/save_homework_test.php +++ b/server/moodle/mod/homework/tests/save_homework_test.php @@ -244,4 +244,4 @@ public function test_file_upload(): void { // Assert that the output contains a success message. $this->assertStringContainsString('"status":"success","message":"File uploaded successfully"', $output); } -} \ No newline at end of file +} diff --git a/server/moodle/mod/homework/view.php b/server/moodle/mod/homework/view.php index c109620e..a27b4a48 100644 --- a/server/moodle/mod/homework/view.php +++ b/server/moodle/mod/homework/view.php @@ -92,11 +92,11 @@ echo $record->description . '
'; $homeworkmaterials = $DB->get_records_sql( - "SELECT hm.*, f.filename - FROM {homework_materials} hm - LEFT JOIN {files} f ON hm.file_id = f.id - WHERE hm.homework_id = :homework_id", - ['homework_id' => $cm->instance] + "SELECT hm.*, f.filename + FROM {homework_materials} hm + LEFT JOIN {files} f ON hm.file_id = f.id + WHERE hm.homework_id = :homework_id", + ['homework_id' => $cm->instance] ); ?> - -
+

description) ?>

- startpage !== null && $materials->endpage !== null): ?> -

startpage) . " - " . htmlspecialchars($materials->endpage) ?>

+ startpage !== null && $materials->endpage !== null) : ?> +

startpage) . " - " . + htmlspecialchars($materials->endpage) ?> +

- link !== null): ?> -

link) ?>

+ link !== null) : ?> +

link) ?> + +

- starttime !== null && $materials->endtime !== null): ?> -

starttime) . " - " . htmlspecialchars($materials->endtime) ?>

+ starttime !== null && $materials->endtime !== null) : ?> +

starttime) . " - " . + htmlspecialchars($materials->endtime) + ?> +

- file_id !== null): ?> -

filename) ?>

+ file_id !== null) : ?> +

filename) + ?> +

@@ -141,9 +163,6 @@ 'class' => 'btn btn-primary', ]); - // Add a container for the modal if needed. - // echo html_writer::tag('div', '', ['id' => 'homework-chooser-container']); - // Include the AMD module. $PAGE->requires->js_call_amd('mod_homework/homeworkchooser', 'init', [$cm->id, get_string('homeworkchooser', 'mod_homework'), $instance->id]);