From b4846b88d9cb90b6600a2868d6ad92ad80d0b060 Mon Sep 17 00:00:00 2001 From: Adam Olley Date: Fri, 4 Nov 2016 10:18:25 +1030 Subject: [PATCH] MDL-53718 badges: Dont display course badges when disabled --- badges/tests/badgeslib_test.php | 41 +++++++++++++++++++++++++++++++++ lib/badgeslib.php | 6 +++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/badges/tests/badgeslib_test.php b/badges/tests/badgeslib_test.php index a8d43821d9e..0f1f6caeb9c 100644 --- a/badges/tests/badgeslib_test.php +++ b/badges/tests/badgeslib_test.php @@ -288,6 +288,47 @@ class core_badges_badgeslib_testcase extends advanced_testcase { // The term Totara doesn't appear anywhere in the badges. $result = badges_get_user_badges($user2->id, 0, 0, 0, 'Totara'); $this->assertCount(0, $result); + + // Issue a user with a course badge and verify its returned based on if + // coursebadges are enabled or disabled. + $sitebadgeid = key($badges); + $badges[$sitebadgeid]->issue($this->user->id, true); + + $badge = new stdClass(); + $badge->id = null; + $badge->name = "Test course badge"; + $badge->description = "Testing course badge"; + $badge->timecreated = $now; + $badge->timemodified = $now; + $badge->usercreated = $user1->id; + $badge->usermodified = $user1->id; + $badge->issuername = "Test issuer"; + $badge->issuerurl = "http://issuer-url.domain.co.nz"; + $badge->issuercontact = "issuer@example.com"; + $badge->expiredate = null; + $badge->expireperiod = null; + $badge->type = BADGE_TYPE_COURSE; + $badge->courseid = $this->course->id; + $badge->messagesubject = "Test message subject for course badge"; + $badge->message = "Test message body for course badge"; + $badge->attachment = 1; + $badge->notification = 0; + $badge->status = BADGE_STATUS_ACTIVE; + + $badgeid = $DB->insert_record('badge', $badge, true); + $badges[$badgeid] = new badge($badgeid); + $badges[$badgeid]->issue($this->user->id, true); + + // With coursebadges off, we should only get the site badge. + set_config('badges_allowcoursebadges', false); + $result = badges_get_user_badges($this->user->id); + $this->assertCount(1, $result); + + // With it on, we should get both. + set_config('badges_allowcoursebadges', true); + $result = badges_get_user_badges($this->user->id); + $this->assertCount(2, $result); + } public function data_for_message_from_template() { diff --git a/lib/badgeslib.php b/lib/badgeslib.php index 70ba3ba7cac..8c42e62845c 100644 --- a/lib/badgeslib.php +++ b/lib/badgeslib.php @@ -886,7 +886,7 @@ function badges_get_badges($type, $courseid = 0, $sort = '', $dir = '', $page = * @return array of badges ordered by decreasing date of issue */ function badges_get_user_badges($userid, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false) { - global $DB; + global $CFG, $DB; $params = array( 'userid' => $userid @@ -915,7 +915,9 @@ function badges_get_user_badges($userid, $courseid = 0, $page = 0, $perpage = 0, $sql .= ' AND (bi.visible = 1) '; } - if ($courseid != 0) { + if (empty($CFG->badges_allowcoursebadges)) { + $sql .= ' AND b.courseid IS NULL'; + } else if ($courseid != 0) { $sql .= ' AND (b.courseid = :courseid) '; $params['courseid'] = $courseid; }