From 45efaaaf13a0d72f76ec8395b1727bf9d5982566 Mon Sep 17 00:00:00 2001 From: Paola Maneggia Date: Thu, 18 Apr 2024 18:33:09 +0000 Subject: [PATCH 1/2] MBS-9026 implement reset userdata Also delete entries in hvp_content_user_data when deleting instance --- editor | 2 +- lang/en/hvp.php | 2 ++ lib.php | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/editor b/editor index 53dc7bdc5..1137fdbbe 160000 --- a/editor +++ b/editor @@ -1 +1 @@ -Subproject commit 53dc7bdc57b17f5a0d55a8871a36d1b1bd631e70 +Subproject commit 1137fdbbec44e7727e117ebf45c20f61d7f7267e diff --git a/lang/en/hvp.php b/lang/en/hvp.php index 8e922bfab..569afb9a0 100644 --- a/lang/en/hvp.php +++ b/lang/en/hvp.php @@ -63,6 +63,8 @@ $string['removeoldlogentries'] = 'Remove old H5P log entries'; $string['removeoldmobileauthentries'] = 'Remove old H5P mobile auth entries'; +$string['reset_hvp_attempts'] = 'Reset user attempts'; + // Admin settings. $string['displayoptiondownloadnever'] = 'Never'; $string['displayoptiondownloadalways'] = 'Always'; diff --git a/lib.php b/lib.php index c16286dbe..1d2c24ffd 100644 --- a/lib.php +++ b/lib.php @@ -205,6 +205,11 @@ function hvp_delete_instance($id) { 'content_id' => $hvp->id )); + // Delete entries in the hvp_content_user_data table. + $DB->delete_records('hvp_content_user_data', [ + 'hvp_id' => $hvp->id + ]); + // Get library details. $library = $DB->get_record_sql( "SELECT machine_name AS name, major_version, minor_version @@ -516,3 +521,73 @@ function mod_hvp_core_calendar_provide_event_action(calendar_event $event, actio ); } +/** + * Implementation of the function for printing the form elements that control + * whether the course reset functionality affects the hvp activity. + * + * @param object $mform form passed by reference + */ +function hvp_reset_course_form_definition(&$mform): void { + $mform->addElement('header', 'hvpactivityheader', get_string('modulenameplural', 'mod_hvp')); + $mform->addElement('advcheckbox', 'reset_hvp_attempts', get_string('reset_hvp_attempts', 'mod_hvp')); +} + +/** + * Course reset form defaults. + * + * @param stdClass $course the course object + * @return array + */ +function hvp_reset_course_form_defaults(stdClass $course): array { + return [ + 'reset_hvp_attempts' => 1, + ]; +} + +/** + * This function is used by the reset_course_userdata function in moodlelib. + * + * @param object $data the data submitted from the reset course. + * @return array status array + */ +function hvp_reset_userdata($data) { + global $DB; + $status = []; + $hvpinstances = $DB->get_records('hvp', ['course' => $data->courseid]); + + foreach ($hvpinstances as $hvpinstance) { + if (!empty($data->reset_hvp_userdata)) { + $DB->delete_records('hvp_content_user_data', ['hvp_id' => $hvpinstance->id]); + $DB->delete_records('hvp_xapi_results', ['content_id' => $hvpinstance->id]); + // Remove all grades from gradebook. + if (empty($data->reset_gradebook_grades)) { + hvp_reset_gradebook($data->courseid); + } + $status[] = [ + 'component' => get_string('modulenameplural', 'mod_hvp'), + 'item' => get_string('reset_hvp_attempts', 'mod_hvp'), + 'error' => false, + ]; + } + } + return $status; +} + +/** + * Removes all grades from gradebook + * @param int $courseid + * @param string optional type + */ +function hvp_reset_gradebook($courseid, $type='') { + global $DB; + + $sql = "SELECT h.*, cm.idnumber as cmidnumber, h.course as courseid + FROM {hvp} h, {course_modules} cm, {modules} m + WHERE m.name = 'hvp' AND m.id = cm.module AND cm.instance = h.id AND h.course=?"; + + if ($activities = $DB->get_records_sql($sql, [$courseid])) { + foreach ($activities as $activity) { + hvp_grade_item_update($activity, 'reset'); + } + } +} From 5dcc7570cbd4b92b0aa9eade341bcfcbe72f08bb Mon Sep 17 00:00:00 2001 From: Paola Maneggia Date: Tue, 21 May 2024 10:05:54 +0000 Subject: [PATCH 2/2] MBS-9026 Fix name of data field --- lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib.php b/lib.php index 1d2c24ffd..46daaa866 100644 --- a/lib.php +++ b/lib.php @@ -556,7 +556,7 @@ function hvp_reset_userdata($data) { $hvpinstances = $DB->get_records('hvp', ['course' => $data->courseid]); foreach ($hvpinstances as $hvpinstance) { - if (!empty($data->reset_hvp_userdata)) { + if (!empty($data->reset_hvp_attempts)) { $DB->delete_records('hvp_content_user_data', ['hvp_id' => $hvpinstance->id]); $DB->delete_records('hvp_xapi_results', ['content_id' => $hvpinstance->id]); // Remove all grades from gradebook.