1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Badge awards information
19 *
20 * @package    core
21 * @subpackage badges
22 * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
25 */
26
27require_once(__DIR__ . '/../config.php');
28require_once($CFG->libdir . '/badgeslib.php');
29
30$badgeid    = required_param('id', PARAM_INT);
31$sortby     = optional_param('sort', 'dateissued', PARAM_ALPHA);
32$sorthow    = optional_param('dir', 'DESC', PARAM_ALPHA);
33$page       = optional_param('page', 0, PARAM_INT);
34
35require_login();
36
37if (empty($CFG->enablebadges)) {
38    print_error('badgesdisabled', 'badges');
39}
40
41if (!in_array($sortby, array('firstname', 'lastname', 'dateissued'))) {
42    $sortby = 'dateissued';
43}
44
45if ($sorthow != 'ASC' and $sorthow != 'DESC') {
46    $sorthow = 'DESC';
47}
48
49if ($page < 0) {
50    $page = 0;
51}
52
53$badge = new badge($badgeid);
54$context = $badge->get_context();
55$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
56
57if ($badge->type == BADGE_TYPE_COURSE) {
58    if (empty($CFG->badges_allowcoursebadges)) {
59        print_error('coursebadgesdisabled', 'badges');
60    }
61    require_login($badge->courseid);
62    $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
63    $PAGE->set_pagelayout('standard');
64    navigation_node::override_active_url($navurl);
65} else {
66    $PAGE->set_pagelayout('admin');
67    navigation_node::override_active_url($navurl, true);
68}
69
70$PAGE->set_context($context);
71$PAGE->set_url('/badges/recipients.php', array('id' => $badgeid, 'sort' => $sortby, 'dir' => $sorthow));
72$PAGE->set_heading($badge->name);
73$PAGE->set_title($badge->name);
74$PAGE->navbar->add($badge->name);
75
76$output = $PAGE->get_renderer('core', 'badges');
77
78echo $output->header();
79echo $OUTPUT->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name);
80
81echo $output->print_badge_status_box($badge);
82$output->print_badge_tabs($badgeid, $context, 'awards');
83
84// Add button for badge manual award.
85if ($badge->has_manual_award_criteria() && has_capability('moodle/badges:awardbadge', $context) && $badge->is_active()) {
86    $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
87    echo $OUTPUT->box($OUTPUT->single_button($url, get_string('award', 'badges')), 'clearfix mdl-align');
88}
89
90$userfieldsapi = \core_user\fields::for_name();
91$namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
92$sql = "SELECT b.userid, b.dateissued, b.uniquehash, $namefields
93    FROM {badge_issued} b INNER JOIN {user} u
94        ON b.userid = u.id
95    WHERE b.badgeid = :badgeid AND u.deleted = 0
96    ORDER BY $sortby $sorthow";
97
98$totalcount = $DB->count_records('badge_issued', array('badgeid' => $badge->id));
99
100if ($badge->has_awards()) {
101    $users = $DB->get_records_sql($sql, array('badgeid' => $badge->id), $page * BADGE_PERPAGE, BADGE_PERPAGE);
102    $recipients             = new core_badges\output\badge_recipients($users);
103    $recipients->sort       = $sortby;
104    $recipients->dir        = $sorthow;
105    $recipients->page       = $page;
106    $recipients->perpage    = BADGE_PERPAGE;
107    $recipients->totalcount = $totalcount;
108
109    echo $output->render($recipients);
110} else {
111    echo $output->notification(get_string('noawards', 'badges'));
112}
113
114echo $output->footer();
115