Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBS-9026 implement reset userdata #548

Open
wants to merge 2 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion editor
2 changes: 2 additions & 0 deletions lang/en/hvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
75 changes: 75 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_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.
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');
}
}
}