Skip to content

Commit

Permalink
Merge branch 'MDL-53718_30' of git://github.com/aolley/moodle into MO…
Browse files Browse the repository at this point in the history
…ODLE_30_STABLE
  • Loading branch information
David Monllao committed Nov 8, 2016
2 parents b9f81ce + a85576b commit 8791b78
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
41 changes: 41 additions & 0 deletions badges/tests/badgeslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,47 @@ public function test_badges_get_user_badges() {
// 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() {
Expand Down
6 changes: 4 additions & 2 deletions lib/badgeslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,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
Expand Down Expand Up @@ -880,7 +880,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;
}
Expand Down

0 comments on commit 8791b78

Please sign in to comment.