Merge branch 'MDL-60850-311' of https://github.com/junpataleta/moodle into MOODLE_311...
[moodle.git] / badges / recipients.php
blob83121cdbb81e65c73ce58815d5faac732b7f70fd
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(__DIR__ . '/../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);
35 require_login();
37 if (empty($CFG->enablebadges)) {
38 print_error('badgesdisabled', 'badges');
41 if (!in_array($sortby, array('firstname', 'lastname', 'dateissued'))) {
42 $sortby = 'dateissued';
45 if ($sorthow != 'ASC' and $sorthow != 'DESC') {
46 $sorthow = 'DESC';
49 if ($page < 0) {
50 $page = 0;
53 $badge = new badge($badgeid);
54 $context = $badge->get_context();
55 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
57 if ($badge->type == BADGE_TYPE_COURSE) {
58 if (empty($CFG->badges_allowcoursebadges)) {
59 print_error('coursebadgesdisabled', 'badges');
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);
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);
76 $output = $PAGE->get_renderer('core', 'badges');
78 echo $output->header();
79 echo $OUTPUT->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name);
81 echo $output->print_badge_status_box($badge);
82 $output->print_badge_tabs($badgeid, $context, 'awards');
84 // Add button for badge manual award.
85 if ($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');
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";
98 $totalcount = $DB->count_records('badge_issued', array('badgeid' => $badge->id));
100 if ($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;
109 echo $output->render($recipients);
110 } else {
111 echo $output->notification(get_string('noawards', 'badges'));
114 echo $output->footer();