Merge branch 'MDL-40255_M25' of git://github.com/lazydaisy/moodle into MOODLE_25_STABLE
[moodle.git] / badges / recipients.php
blob539696b6ea7c8d14cc65c6fbad1718e6306a5a7a
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.
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/>.
17 /**
18 * Badge awards information
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>
27 require_once(dirname(dirname(__FILE__)) . '/config.php');
28 require_once($CFG->libdir . '/badgeslib.php');
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 $updatepref = optional_param('updatepref', false, PARAM_BOOL);
36 require_login();
38 if (empty($CFG->enablebadges)) {
39 print_error('badgesdisabled', 'badges');
42 if (!in_array($sortby, array('firstname', 'lastname', 'dateissued'))) {
43 $sortby = 'dateissued';
46 if ($sorthow != 'ASC' and $sorthow != 'DESC') {
47 $sorthow = 'DESC';
50 if ($page < 0) {
51 $page = 0;
54 $badge = new badge($badgeid);
55 $context = $badge->get_context();
56 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
58 if ($badge->type == BADGE_TYPE_COURSE) {
59 if (empty($CFG->badges_allowcoursebadges)) {
60 print_error('coursebadgesdisabled', 'badges');
62 require_login($badge->courseid);
63 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
66 $PAGE->set_context($context);
67 $PAGE->set_url('/badges/recipients.php', array('id' => $badgeid, 'sort' => $sortby, 'dir' => $sorthow));
68 $PAGE->set_pagelayout('standard');
69 $PAGE->set_heading($badge->name);
70 $PAGE->set_title($badge->name);
71 $PAGE->navbar->add($badge->name);
72 navigation_node::override_active_url($navurl);
74 $output = $PAGE->get_renderer('core', 'badges');
76 echo $output->header();
77 echo $OUTPUT->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name);
79 echo $output->print_badge_status_box($badge);
80 $output->print_badge_tabs($badgeid, $context, 'awards');
82 // Add button for badge manual award.
83 if ($badge->has_manual_award_criteria() && has_capability('moodle/badges:awardbadge', $context) && $badge->is_active()) {
84 $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
85 echo $OUTPUT->box($OUTPUT->single_button($url, get_string('award', 'badges')), 'clearfix mdl-align');
88 $sql = "SELECT b.userid, b.dateissued, b.uniquehash, u.firstname, u.lastname
89 FROM {badge_issued} b INNER JOIN {user} u
90 ON b.userid = u.id
91 WHERE b.badgeid = :badgeid
92 ORDER BY $sortby $sorthow";
94 $totalcount = $DB->count_records('badge_issued', array('badgeid' => $badge->id));
96 if ($badge->has_awards()) {
97 $users = $DB->get_records_sql($sql, array('badgeid' => $badge->id), $page * BADGE_PERPAGE, BADGE_PERPAGE);
98 $recipients = new badge_recipients($users);
99 $recipients->sort = $sortby;
100 $recipients->dir = $sorthow;
101 $recipients->page = $page;
102 $recipients->perpage = BADGE_PERPAGE;
103 $recipients->totalcount = $totalcount;
105 echo $output->render($recipients);
106 } else {
107 echo $output->notification(get_string('noawards', 'badges'));
110 echo $output->footer();