Merge branch 'MDL-73169-integration-master' of git://github.com/mihailges/moodle
[moodle.git] / badges / recipients.php
blob623d8e64cf4647a1b83f3d9bc17a50870ef101b0
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 $course = get_course($badge->courseid);
63 $heading = format_string($course->fullname, true, ['context' => $context]);
64 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
65 $PAGE->set_pagelayout('standard');
66 navigation_node::override_active_url($navurl);
67 } else {
68 $PAGE->set_pagelayout('admin');
69 $heading = get_string('administrationsite');
70 navigation_node::override_active_url($navurl, true);
73 $PAGE->set_context($context);
74 $PAGE->set_url('/badges/recipients.php', array('id' => $badgeid, 'sort' => $sortby, 'dir' => $sorthow));
75 $PAGE->set_heading($heading);
76 $PAGE->set_title($badge->name);
77 $PAGE->navbar->add($badge->name);
79 $output = $PAGE->get_renderer('core', 'badges');
81 echo $output->header();
83 $actionbar = new \core_badges\output\recipients_action_bar($badge, $PAGE);
84 echo $output->render_tertiary_navigation($actionbar);
86 echo $OUTPUT->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name);
87 echo $output->print_badge_status_box($badge);
89 $userfieldsapi = \core_user\fields::for_name();
90 $namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
91 $sql = "SELECT b.userid, b.dateissued, b.uniquehash, $namefields
92 FROM {badge_issued} b INNER JOIN {user} u
93 ON b.userid = u.id
94 WHERE b.badgeid = :badgeid AND u.deleted = 0
95 ORDER BY $sortby $sorthow";
97 $totalcount = $DB->count_records('badge_issued', array('badgeid' => $badge->id));
99 if ($badge->has_awards()) {
100 $users = $DB->get_records_sql($sql, array('badgeid' => $badge->id), $page * BADGE_PERPAGE, BADGE_PERPAGE);
101 $recipients = new core_badges\output\badge_recipients($users);
102 $recipients->sort = $sortby;
103 $recipients->dir = $sorthow;
104 $recipients->page = $page;
105 $recipients->perpage = BADGE_PERPAGE;
106 $recipients->totalcount = $totalcount;
108 echo $output->render($recipients);
109 } else {
110 echo $output->notification(get_string('noawards', 'badges'));
113 echo $output->footer();