From c09541fd280cc46bde7475adb8a135e070200bbb Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Thu, 3 Aug 2017 13:58:27 +0800 Subject: [PATCH] MDL-59049 badges: allows users to view badges from deleted courses --- badges/criteria/award_criteria_activity.php | 7 ++++++- badges/criteria/award_criteria_course.php | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/badges/criteria/award_criteria_activity.php b/badges/criteria/award_criteria_activity.php index 171361b062e..8e9ed2efe97 100644 --- a/badges/criteria/award_criteria_activity.php +++ b/badges/criteria/award_criteria_activity.php @@ -47,8 +47,13 @@ class award_criteria_activity extends award_criteria { parent::__construct($record); $this->course = $DB->get_record_sql('SELECT c.id, c.enablecompletion, c.cacherev, c.startdate - FROM {badge} b INNER JOIN {course} c ON b.courseid = c.id + FROM {badge} b LEFT JOIN {course} c ON b.courseid = c.id WHERE b.id = :badgeid ', array('badgeid' => $this->badgeid), MUST_EXIST); + + // If the course doesn't exist but we're sure the badge does (thanks to the LEFT JOIN), then use the site as the course. + if (empty($this->course->id)) { + $this->course = get_course(SITEID); + } $this->courseid = $this->course->id; } diff --git a/badges/criteria/award_criteria_course.php b/badges/criteria/award_criteria_course.php index c8ae554a2a6..9f5b41a0022 100644 --- a/badges/criteria/award_criteria_course.php +++ b/badges/criteria/award_criteria_course.php @@ -49,8 +49,13 @@ class award_criteria_course extends award_criteria { parent::__construct($record); $this->course = $DB->get_record_sql('SELECT c.id, c.enablecompletion, c.cacherev, c.startdate - FROM {badge} b INNER JOIN {course} c ON b.courseid = c.id + FROM {badge} b LEFT JOIN {course} c ON b.courseid = c.id WHERE b.id = :badgeid ', array('badgeid' => $this->badgeid), MUST_EXIST); + + // If the course doesn't exist but we're sure the badge does (thanks to the LEFT JOIN), then use the site as the course. + if (empty($this->course->id)) { + $this->course = get_course(SITEID); + } $this->courseid = $this->course->id; }