From b6e85266dae6eae4333aad731a0cf9617355639d Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 12 Sep 2023 14:55:04 +0100 Subject: [PATCH] MDL-79334 badges: fix return URL of activate action in system report. We cannot rely on `qualified_me()` to work reliably upon report reload events (paging, sorting, filtering, etc. are all performed via AJAX). Small cleanup after f0770658. --- .../local/systemreports/badges.php | 19 ++++++------ badges/index.php | 31 +++++++------------ 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/badges/classes/reportbuilder/local/systemreports/badges.php b/badges/classes/reportbuilder/local/systemreports/badges.php index 099ae683c79..aaec07927b7 100644 --- a/badges/classes/reportbuilder/local/systemreports/badges.php +++ b/badges/classes/reportbuilder/local/systemreports/badges.php @@ -163,9 +163,8 @@ class badges extends system_report { $this->add_action((new action( new moodle_url('/badges/action.php', [ 'id' => ':id', - 'sesskey' => sesskey(), 'activate' => true, - 'return' => (new moodle_url(qualified_me()))->out_as_local_url(false), + 'return' => ':return', ]), new pix_icon('t/show', '', 'core'), [], @@ -173,8 +172,12 @@ class badges extends system_report { new lang_string('activate', 'badges') ))->add_callback(static function(stdclass $row): bool { $badge = new \core_badges\badge($row->id); - $context = self::get_badge_context((int)$row->type, (int)$row->courseid); - return has_capability('moodle/badges:configuredetails', $context) && + + // Populate the return URL. + $row->return = (new moodle_url('/badges/index.php', + ['type' => $badge->type, 'id' => (int) $badge->courseid]))->out_as_local_url(false); + + return has_capability('moodle/badges:configuredetails', $badge->get_context()) && $badge->has_criteria() && ($row->status == BADGE_STATUS_INACTIVE || $row->status == BADGE_STATUS_INACTIVE_LOCKED); @@ -194,8 +197,7 @@ class badges extends system_report { new lang_string('deactivate', 'badges') ))->add_callback(static function(stdclass $row): bool { $badge = new \core_badges\badge($row->id); - $context = self::get_badge_context((int)$row->type, (int)$row->courseid); - return has_capability('moodle/badges:configuredetails', $context) && + return has_capability('moodle/badges:configuredetails', $badge->get_context()) && $badge->has_criteria() && $row->status != BADGE_STATUS_INACTIVE && $row->status != BADGE_STATUS_INACTIVE_LOCKED; })); @@ -211,9 +213,8 @@ class badges extends system_report { new lang_string('award', 'badges') ))->add_callback(static function(stdclass $row): bool { $badge = new \core_badges\badge($row->id); - $context = self::get_badge_context((int)$row->type, (int)$row->courseid); - return $badge->has_manual_award_criteria() && - has_capability('moodle/badges:awardbadge', $context) && + return has_capability('moodle/badges:awardbadge', $badge->get_context()) && + $badge->has_manual_award_criteria() && $badge->is_active(); })); diff --git a/badges/index.php b/badges/index.php index b75a10e8e51..df043ec35e8 100644 --- a/badges/index.php +++ b/badges/index.php @@ -48,7 +48,6 @@ if (empty($CFG->badges_allowcoursebadges) && ($type == BADGE_TYPE_COURSE)) { throw new \moodle_exception('coursebadgesdisabled', 'badges'); } -$err = ''; $urlparams = ['type' => $type]; if ($course = $DB->get_record('course', ['id' => $courseid])) { @@ -150,25 +149,17 @@ if ($type == BADGE_TYPE_SITE) { echo $OUTPUT->box('', 'notifyproblem hide', 'check_connection'); -$totalcount = count(badges_get_badges($type, $courseid, '', '' , 0, 0)); - -if ($totalcount) { - if ($course && $course->startdate > time()) { - echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem'); - } - - if ($err !== '') { - echo $OUTPUT->notification($err, 'notifyproblem'); - } - - if ($msg !== '') { - echo $OUTPUT->notification(get_string($msg, 'badges'), 'notifysuccess'); - } - - $report = system_report_factory::create(badges::class, $PAGE->context); - echo $report->output(); -} else { - echo $output->notification(get_string('nobadges', 'badges'), 'info'); +if ($course && $course->startdate > time()) { + echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem'); } +if ($msg !== '') { + echo $OUTPUT->notification(get_string($msg, 'badges'), 'notifysuccess'); +} + +$report = system_report_factory::create(badges::class, $PAGE->context); +$report->set_default_no_results_notice(new lang_string('nobadges', 'badges')); + +echo $report->output(); + echo $OUTPUT->footer();