From 3ab6ab7d0e703d540d7caae00b645694dfb9b889 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 15 Sep 2025 13:40:00 +0100 Subject: [PATCH] MDL-86369 mod_scorm: fix attempt deletion behaviour on submit/redirect. Standard after a POST submission is to redirect back to where user came from (in this case), to avoid awkward page reload behaviour. Also fix bug where deleting an attempt for a user, where the attempt didn't exist would delete all attempts for that user. --- mod/scorm/locallib.php | 5 +++++ mod/scorm/report.php | 8 ++++++++ mod/scorm/report/basic/classes/report.php | 7 ------- mod/scorm/report/interactions/classes/report.php | 7 ------- mod/scorm/report/objectives/classes/report.php | 7 ------- 5 files changed, 13 insertions(+), 21 deletions(-) diff --git a/mod/scorm/locallib.php b/mod/scorm/locallib.php index 13e27972490..3bdb5ca5fb2 100644 --- a/mod/scorm/locallib.php +++ b/mod/scorm/locallib.php @@ -1487,6 +1487,11 @@ function scorm_delete_attempt($userid, $scorm, $attemptornumber) { $attempt = $attemptornumber; } else { $attempt = scorm_get_attempt($userid, $scorm->id, $attemptornumber, false); + + // The attempt doesn't exist. + if ($attempt === null) { + return true; + } } scorm_delete_tracks($scorm->id, null, $userid, $attempt->id); diff --git a/mod/scorm/report.php b/mod/scorm/report.php index 527b15cd929..d9675acb8aa 100644 --- a/mod/scorm/report.php +++ b/mod/scorm/report.php @@ -31,6 +31,8 @@ define('SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO', 2); $id = required_param('id', PARAM_INT);// Course Module ID, or ... $download = optional_param('download', '', PARAM_RAW); $mode = optional_param('mode', '', PARAM_ALPHA); // Report mode. +$action = optional_param('action', '', PARAM_ALPHA); +$attemptids = optional_param_array('attemptid', [], PARAM_RAW); $cm = get_coursemodule_from_id('scorm', $id, 0, false, MUST_EXIST); $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); @@ -56,6 +58,12 @@ $PAGE->set_pagelayout('report'); require_capability('mod/scorm:viewreport', $contextmodule); +if ($action == 'delete' && has_capability('mod/scorm:deleteresponses', $contextmodule) && confirm_sesskey()) { + if (scorm_delete_responses($attemptids, $scorm)) { // Delete responses. + redirect($PAGE->url, get_string('scormresponsedeleted', 'scorm'), null, \core\output\notification::NOTIFY_SUCCESS); + } +} + // Activate the secondary nav tab. navigation_node::override_active_url(new moodle_url('/mod/scorm/report.php', ['id' => $id])); diff --git a/mod/scorm/report/basic/classes/report.php b/mod/scorm/report/basic/classes/report.php index 1289a19ee8f..e4161092a49 100644 --- a/mod/scorm/report/basic/classes/report.php +++ b/mod/scorm/report/basic/classes/report.php @@ -38,8 +38,6 @@ class report extends \mod_scorm\report { global $CFG, $DB, $OUTPUT, $PAGE; $contextmodule = \context_module::instance($cm->id); - $action = optional_param('action', '', PARAM_ALPHA); - $attemptids = optional_param_array('attemptid', array(), PARAM_RAW); $attemptsmode = optional_param('attemptsmode', SCORM_REPORT_ATTEMPTS_ALL_STUDENTS, PARAM_INT); $PAGE->set_url(new \moodle_url($PAGE->url, array('attemptsmode' => $attemptsmode))); @@ -50,11 +48,6 @@ class report extends \mod_scorm\report { echo $renderer->report_actionbar($actionbar); } - if ($action == 'delete' && has_capability('mod/scorm:deleteresponses', $contextmodule) && confirm_sesskey()) { - if (scorm_delete_responses($attemptids, $scorm)) { // Delete responses. - echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess'); - } - } // Find out current groups mode. $currentgroup = groups_get_activity_group($cm, true); diff --git a/mod/scorm/report/interactions/classes/report.php b/mod/scorm/report/interactions/classes/report.php index 21402a2e658..1d8a855f951 100644 --- a/mod/scorm/report/interactions/classes/report.php +++ b/mod/scorm/report/interactions/classes/report.php @@ -39,8 +39,6 @@ class report extends \mod_scorm\report { global $CFG, $DB, $OUTPUT, $PAGE; $contextmodule = \context_module::instance($cm->id); - $action = optional_param('action', '', PARAM_ALPHA); - $attemptids = optional_param_array('attemptid', array(), PARAM_RAW); $attemptsmode = optional_param('attemptsmode', SCORM_REPORT_ATTEMPTS_ALL_STUDENTS, PARAM_INT); $PAGE->set_url(new \moodle_url($PAGE->url, array('attemptsmode' => $attemptsmode))); @@ -51,11 +49,6 @@ class report extends \mod_scorm\report { echo $renderer->report_actionbar($actionbar); } - if ($action == 'delete' && has_capability('mod/scorm:deleteresponses', $contextmodule) && confirm_sesskey()) { - if (scorm_delete_responses($attemptids, $scorm)) { // Delete responses. - echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess'); - } - } // Find out current groups mode. $currentgroup = groups_get_activity_group($cm, true); diff --git a/mod/scorm/report/objectives/classes/report.php b/mod/scorm/report/objectives/classes/report.php index 15f58626932..7e17079f683 100644 --- a/mod/scorm/report/objectives/classes/report.php +++ b/mod/scorm/report/objectives/classes/report.php @@ -45,8 +45,6 @@ class report extends \mod_scorm\report { global $CFG, $DB, $OUTPUT, $PAGE; $contextmodule = \context_module::instance($cm->id); - $action = optional_param('action', '', PARAM_ALPHA); - $attemptids = optional_param_array('attemptid', array(), PARAM_RAW); $attemptsmode = optional_param('attemptsmode', SCORM_REPORT_ATTEMPTS_ALL_STUDENTS, PARAM_INT); $PAGE->set_url(new \moodle_url($PAGE->url, array('attemptsmode' => $attemptsmode))); @@ -57,11 +55,6 @@ class report extends \mod_scorm\report { echo $renderer->report_actionbar($actionbar); } - if ($action == 'delete' && has_capability('mod/scorm:deleteresponses', $contextmodule) && confirm_sesskey()) { - if (scorm_delete_responses($attemptids, $scorm)) { // Delete responses. - echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess'); - } - } // Find out current groups mode. $currentgroup = groups_get_activity_group($cm, true);