on-demand release 3.6dev+
[moodle.git] / badges / renderer.php
CommitLineData
27806552
YB
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 * Renderer for use with the badges output
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($CFG->libdir . '/badgeslib.php');
28require_once($CFG->libdir . '/tablelib.php');
27806552
YB
29
30/**
31 * Standard HTML output renderer for badges
32 */
33class core_badges_renderer extends plugin_renderer_base {
34
35 // Outputs badges list.
36 public function print_badges_list($badges, $userid, $profile = false, $external = false) {
37 global $USER, $CFG;
38 foreach ($badges as $badge) {
39 if (!$external) {
40 $context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid);
41 $bname = $badge->name;
42 $imageurl = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', 'f1', false);
43 } else {
ea76b652 44 $bname = s($badge->assertion->badge->name);
27806552
YB
45 $imageurl = $badge->imageUrl;
46 }
47
48 $name = html_writer::tag('span', $bname, array('class' => 'badge-name'));
49
50 $image = html_writer::empty_tag('img', array('src' => $imageurl, 'class' => 'badge-image'));
51 if (!empty($badge->dateexpire) && $badge->dateexpire < time()) {
52 $image .= $this->output->pix_icon('i/expired',
53 get_string('expireddate', 'badges', userdate($badge->dateexpire)),
54 'moodle',
55 array('class' => 'expireimage'));
56 $name .= '(' . get_string('expired', 'badges') . ')';
57 }
58
59 $download = $status = $push = '';
60 if (($userid == $USER->id) && !$profile) {
61 $url = new moodle_url('mybadges.php', array('download' => $badge->id, 'hash' => $badge->uniquehash, 'sesskey' => sesskey()));
e2805314
YB
62 $notexpiredbadge = (empty($badge->dateexpire) || $badge->dateexpire > time());
63 $backpackexists = badges_user_has_backpack($USER->id);
64 if (!empty($CFG->badges_allowexternalbackpack) && $notexpiredbadge && $backpackexists) {
27806552
YB
65 $assertion = new moodle_url('/badges/assertion.php', array('b' => $badge->uniquehash));
66 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
67 $push = $this->output->action_icon(new moodle_url('#'), new pix_icon('t/backpack', get_string('addtobackpack', 'badges')), $action);
68 }
69
70 $download = $this->output->action_icon($url, new pix_icon('t/download', get_string('download')));
71 if ($badge->visible) {
72 $url = new moodle_url('mybadges.php', array('hide' => $badge->issuedid, 'sesskey' => sesskey()));
73 $status = $this->output->action_icon($url, new pix_icon('t/hide', get_string('makeprivate', 'badges')));
74 } else {
75 $url = new moodle_url('mybadges.php', array('show' => $badge->issuedid, 'sesskey' => sesskey()));
76 $status = $this->output->action_icon($url, new pix_icon('t/show', get_string('makepublic', 'badges')));
77 }
78 }
79
80 if (!$profile) {
81 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash));
82 } else {
83 if (!$external) {
2d3c0fae 84 $url = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
27806552 85 } else {
2d3c0fae
YB
86 $hash = hash('md5', $badge->hostedUrl);
87 $url = new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid));
27806552
YB
88 }
89 }
90 $actions = html_writer::tag('div', $push . $download . $status, array('class' => 'badge-actions'));
91 $items[] = html_writer::link($url, $image . $actions . $name, array('title' => $bname));
92 }
93
94 return html_writer::alist($items, array('class' => 'badges'));
95 }
96
97 // Recipients selection form.
98 public function recipients_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
99 $output = '';
100 $formattributes = array();
101 $formattributes['id'] = 'recipientform';
04da1a5a 102 $formattributes['action'] = $this->page->url;
27806552
YB
103 $formattributes['method'] = 'post';
104 $output .= html_writer::start_tag('form', $formattributes);
105 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
106
107 $existingcell = new html_table_cell();
108 $existingcell->text = $existinguc->display(true);
109 $existingcell->attributes['class'] = 'existing';
110 $actioncell = new html_table_cell();
111 $actioncell->text = html_writer::start_tag('div', array());
112 $actioncell->text .= html_writer::empty_tag('input', array(
113 'type' => 'submit',
114 'name' => 'award',
115 'value' => $this->output->larrow() . ' ' . get_string('award', 'badges'),
116 'class' => 'actionbutton')
117 );
b7374fac
MD
118 $actioncell->text .= html_writer::empty_tag('input', array(
119 'type' => 'submit',
120 'name' => 'revoke',
121 'value' => get_string('revoke', 'badges') . ' ' . $this->output->rarrow(),
122 'class' => 'actionbutton')
123 );
27806552
YB
124 $actioncell->text .= html_writer::end_tag('div', array());
125 $actioncell->attributes['class'] = 'actions';
126 $potentialcell = new html_table_cell();
127 $potentialcell->text = $potentialuc->display(true);
128 $potentialcell->attributes['class'] = 'potential';
129
130 $table = new html_table();
131 $table->attributes['class'] = 'recipienttable boxaligncenter';
132 $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
133 $output .= html_writer::table($table);
134
135 $output .= html_writer::end_tag('form');
136 return $output;
137 }
138
139 // Prints a badge overview infomation.
140 public function print_badge_overview($badge, $context) {
141 $display = "";
142
143 // Badge details.
27806552 144
cd243d94
Y
145 $display .= $this->heading(get_string('badgedetails', 'badges'), 3);
146 $dl = array();
147 $dl[get_string('name')] = $badge->name;
148 $dl[get_string('description', 'badges')] = $badge->description;
64d00f4c 149 $dl[get_string('createdon', 'search')] = userdate($badge->timecreated);
cd243d94
Y
150 $dl[get_string('badgeimage', 'badges')] = print_badge_image($badge, $context, 'large');
151 $display .= $this->definition_list($dl);
27806552 152
cd243d94
Y
153 // Issuer details.
154 $display .= $this->heading(get_string('issuerdetails', 'badges'), 3);
155 $dl = array();
156 $dl[get_string('issuername', 'badges')] = $badge->issuername;
157 $dl[get_string('contact', 'badges')] = html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact));
158 $display .= $this->definition_list($dl);
27806552
YB
159
160 // Issuance details if any.
cd243d94 161 $display .= $this->heading(get_string('issuancedetails', 'badges'), 3);
27806552
YB
162 if ($badge->can_expire()) {
163 if ($badge->expiredate) {
164 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate));
165 } else if ($badge->expireperiod) {
166 if ($badge->expireperiod < 60) {
167 $display .= get_string('expireperiods', 'badges', round($badge->expireperiod, 2));
168 } else if ($badge->expireperiod < 60 * 60) {
169 $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod / 60, 2));
170 } else if ($badge->expireperiod < 60 * 60 * 24) {
171 $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod / 60 / 60, 2));
172 } else {
173 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2));
174 }
175 }
176 } else {
177 $display .= get_string('noexpiry', 'badges');
178 }
27806552
YB
179
180 // Criteria details if any.
cd243d94 181 $display .= $this->heading(get_string('bcriteria', 'badges'), 3);
27806552
YB
182 if ($badge->has_criteria()) {
183 $display .= self::print_badge_criteria($badge);
184 } else {
185 $display .= get_string('nocriteria', 'badges');
186 if (has_capability('moodle/badges:configurecriteria', $context)) {
187 $display .= $this->output->single_button(
188 new moodle_url('/badges/criteria.php', array('id' => $badge->id)),
189 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
190 }
191 }
27806552
YB
192
193 // Awards details if any.
194 if (has_capability('moodle/badges:viewawarded', $context)) {
cd243d94 195 $display .= $this->heading(get_string('awards', 'badges'), 3);
27806552
YB
196 if ($badge->has_awards()) {
197 $url = new moodle_url('/badges/recipients.php', array('id' => $badge->id));
198 $a = new stdClass();
199 $a->link = $url->out();
200 $a->count = count($badge->get_awards());
201 $display .= get_string('numawards', 'badges', $a);
202 } else {
203 $display .= get_string('noawards', 'badges');
204 }
205
206 if (has_capability('moodle/badges:awardbadge', $context) &&
207 $badge->has_manual_award_criteria() &&
208 $badge->is_active()) {
209 $display .= $this->output->single_button(
210 new moodle_url('/badges/award.php', array('id' => $badge->id)),
211 get_string('award', 'badges'), 'POST', array('class' => 'activatebadge'));
212 }
27806552
YB
213 }
214
cd243d94 215 return html_writer::div($display, null, array('id' => 'badge-overview'));
27806552
YB
216 }
217
218 // Prints action icons for the badge.
219 public function print_badge_table_actions($badge, $context) {
220 $actions = "";
221
4d7d17dd 222 if (has_capability('moodle/badges:configuredetails', $context) && $badge->has_criteria()) {
27806552
YB
223 // Activate/deactivate badge.
224 if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) {
4d7d17dd
YB
225 // "Activate" will go to another page and ask for confirmation.
226 $url = new moodle_url('/badges/action.php');
227 $url->param('id', $badge->id);
228 $url->param('activate', true);
c5177654 229 $url->param('sesskey', sesskey());
4d7d17dd
YB
230 $return = new moodle_url(qualified_me());
231 $url->param('return', $return->out_as_local_url(false));
27806552
YB
232 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
233 } else {
234 $url = new moodle_url(qualified_me());
235 $url->param('lock', $badge->id);
236 $url->param('sesskey', sesskey());
237 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
238 }
239 }
240
241 // Award badge manually.
242 if ($badge->has_manual_award_criteria() &&
243 has_capability('moodle/badges:awardbadge', $context) &&
244 $badge->is_active()) {
245 $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
246 $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
247 }
248
249 // Edit badge.
250 if (has_capability('moodle/badges:configuredetails', $context)) {
251 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'details'));
252 $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
253 }
254
255 // Duplicate badge.
256 if (has_capability('moodle/badges:createbadge', $context)) {
257 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey()));
258 $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
259 }
260
261 // Delete badge.
262 if (has_capability('moodle/badges:deletebadge', $context)) {
263 $url = new moodle_url(qualified_me());
264 $url->param('delete', $badge->id);
265 $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
266 }
267
268 return $actions;
269 }
270
271 // Outputs issued badge with actions available.
272 protected function render_issued_badge(issued_badge $ibadge) {
762b2a6b 273 global $USER, $CFG, $DB, $SITE;
27806552 274 $issued = $ibadge->issued;
737a352f 275 $userinfo = $ibadge->recipient;
853e506a 276 $badgeclass = $ibadge->badgeclass;
27806552 277 $badge = new badge($ibadge->badgeid);
853e506a 278 $now = time();
cd243d94 279 $expiration = isset($issued['expires']) ? $issued['expires'] : $now + 86400;
27806552 280
cd243d94
Y
281 $output = '';
282 $output .= html_writer::start_tag('div', array('id' => 'badge'));
283 $output .= html_writer::start_tag('div', array('id' => 'badge-image'));
284 $output .= html_writer::empty_tag('img', array('src' => $badgeclass['image'], 'alt' => $badge->name));
285 if ($expiration < $now) {
286 $output .= $this->output->pix_icon('i/expired',
287 get_string('expireddate', 'badges', userdate($issued['expires'])),
288 'moodle',
289 array('class' => 'expireimage'));
290 }
27806552 291
737a352f 292 if ($USER->id == $userinfo->id && !empty($CFG->enablebadges)) {
cd243d94 293 $output .= $this->output->single_button(
853e506a 294 new moodle_url('/badges/badge.php', array('hash' => $issued['uid'], 'bake' => true)),
e2805314 295 get_string('download'),
cd243d94 296 'POST');
853e506a
Y
297 if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $now) && badges_user_has_backpack($USER->id)) {
298 $assertion = new moodle_url('/badges/assertion.php', array('b' => $issued['uid']));
2c910861 299 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
e2805314 300 $attributes = array(
2c910861
YB
301 'type' => 'button',
302 'id' => 'addbutton',
303 'value' => get_string('addtobackpack', 'badges'));
e2805314 304 $tobackpack = html_writer::tag('input', '', $attributes);
2c910861 305 $this->output->add_action_handler($action, 'addbutton');
cd243d94 306 $output .= $tobackpack;
27806552 307 }
e2805314 308 }
cd243d94 309 $output .= html_writer::end_tag('div');
737a352f 310
cd243d94 311 $output .= html_writer::start_tag('div', array('id' => 'badge-details'));
737a352f 312 // Recipient information.
cd243d94
Y
313 $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3);
314 $dl = array();
762b2a6b
YB
315 if ($userinfo->deleted) {
316 $strdata = new stdClass();
317 $strdata->user = fullname($userinfo);
318 $strdata->site = format_string($SITE->fullname, true, array('context' => context_system::instance()));
cd243d94
Y
319
320 $dl[get_string('name')] = get_string('error:userdeleted', 'badges', $strdata);
737a352f 321 } else {
cd243d94 322 $dl[get_string('name')] = fullname($userinfo);
737a352f 323 }
cd243d94 324 $output .= $this->definition_list($dl);
737a352f 325
cd243d94
Y
326 $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3);
327 $dl = array();
328 $dl[get_string('issuername', 'badges')] = $badge->issuername;
e2805314 329 if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
cd243d94 330 $dl[get_string('contact', 'badges')] = obfuscate_mailto($badge->issuercontact);
e2805314 331 }
cd243d94
Y
332 $output .= $this->definition_list($dl);
333
334 $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3);
335 $dl = array();
336 $dl[get_string('name')] = $badge->name;
337 $dl[get_string('description', 'badges')] = $badge->description;
27806552 338
e2805314
YB
339 if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
340 $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid));
cd243d94 341 $dl[get_string('course')] = $coursename;
e2805314 342 }
cd243d94
Y
343 $dl[get_string('bcriteria', 'badges')] = self::print_badge_criteria($badge);
344 $output .= $this->definition_list($dl);
27806552 345
cd243d94
Y
346 $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3);
347 $dl = array();
348 $dl[get_string('dateawarded', 'badges')] = userdate($issued['issuedOn']);
e2805314 349 if (isset($issued['expires'])) {
853e506a 350 if ($issued['expires'] < $now) {
cd243d94 351 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']) . get_string('warnexpired', 'badges');
e2805314 352
e2805314 353 } else {
cd243d94 354 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']);
27806552 355 }
e2805314 356 }
27806552 357
e2805314
YB
358 // Print evidence.
359 $agg = $badge->get_aggregation_methods();
737a352f 360 $evidence = $badge->get_criteria_completions($userinfo->id);
484b43f4
MG
361 $eids = array_map(function($o) {
362 return $o->critid;
363 }, $evidence);
e2805314
YB
364 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
365
366 $items = array();
367 foreach ($badge->criteria as $type => $c) {
368 if (in_array($c->id, $eids)) {
369 if (count($c->params) == 1) {
370 $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
371 } else {
372 $items[] = get_string('criteria_descr_' . $type , 'badges',
ae17e383 373 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
27806552
YB
374 }
375 }
e2805314 376 }
27806552 377
cd243d94
Y
378 $dl[get_string('evidence', 'badges')] = get_string('completioninfo', 'badges') . html_writer::alist($items, array(), 'ul');
379 $output .= $this->definition_list($dl);
380 $output .= html_writer::end_tag('div');
27806552 381
cd243d94 382 return $output;
27806552
YB
383 }
384
385 // Outputs external badge.
386 protected function render_external_badge(external_badge $ibadge) {
387 $issued = $ibadge->issued;
388 $assertion = $issued->assertion;
389 $issuer = $assertion->badge->issuer;
737a352f 390 $userinfo = $ibadge->recipient;
27806552 391 $table = new html_table();
ea76b652 392 $today = strtotime(date('Y-m-d'));
27806552 393
cd243d94
Y
394 $output = '';
395 $output .= html_writer::start_tag('div', array('id' => 'badge'));
396 $output .= html_writer::start_tag('div', array('id' => 'badge-image'));
ea76b652
YB
397 $output .= html_writer::empty_tag('img', array('src' => $issued->imageUrl));
398 if (isset($assertion->expires)) {
399 $expiration = !strtotime($assertion->expires) ? s($assertion->expires) : strtotime($assertion->expires);
400 if ($expiration < $today) {
401 $output .= $this->output->pix_icon('i/expired',
402 get_string('expireddate', 'badges', userdate($expiration)),
403 'moodle',
404 array('class' => 'expireimage'));
405 }
cd243d94
Y
406 }
407 $output .= html_writer::end_tag('div');
27806552 408
cd243d94 409 $output .= html_writer::start_tag('div', array('id' => 'badge-details'));
737a352f
YB
410
411 // Recipient information.
cd243d94
Y
412 $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3);
413 $dl = array();
737a352f
YB
414 // Technically, we should alway have a user at this point, but added an extra check just in case.
415 if ($userinfo) {
737a352f
YB
416 if (!$ibadge->valid) {
417 $notify = $this->output->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice');
cd243d94
Y
418 $dl[get_string('name')] = fullname($userinfo) . $notify;
419 } else {
420 $dl[get_string('name')] = fullname($userinfo);
737a352f
YB
421 }
422 } else {
423 $notify = $this->output->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice');
cd243d94 424 $dl[get_string('name')] = $notify;
737a352f 425 }
cd243d94
Y
426 $output .= $this->definition_list($dl);
427
428 $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3);
429 $dl = array();
ea76b652 430 $dl[get_string('issuername', 'badges')] = s($issuer->name);
cd243d94 431 $dl[get_string('issuerurl', 'badges')] = html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin));
737a352f 432
27806552 433 if (isset($issuer->contact)) {
cd243d94
Y
434 $dl[get_string('contact', 'badges')] = obfuscate_mailto($issuer->contact);
435 }
436 $output .= $this->definition_list($dl);
437
438 $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3);
439 $dl = array();
ea76b652
YB
440 $dl[get_string('name')] = s($assertion->badge->name);
441 $dl[get_string('description', 'badges')] = s($assertion->badge->description);
442 $dl[get_string('bcriteria', 'badges')] = html_writer::tag('a', s($assertion->badge->criteria), array('href' => $assertion->badge->criteria));
cd243d94
Y
443 $output .= $this->definition_list($dl);
444
445 $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3);
446 $dl = array();
27806552 447 if (isset($assertion->issued_on)) {
ea76b652
YB
448 $issuedate = !strtotime($assertion->issued_on) ? s($assertion->issued_on) : strtotime($assertion->issued_on);
449 $dl[get_string('dateawarded', 'badges')] = userdate($issuedate);
27806552 450 }
ea76b652 451 if (isset($assertion->expires)) {
27806552 452 if ($expiration < $today) {
ea76b652 453 $dl[get_string('expirydate', 'badges')] = userdate($expiration) . get_string('warnexpired', 'badges');
27806552 454 } else {
ea76b652 455 $dl[get_string('expirydate', 'badges')] = userdate($expiration);
27806552
YB
456 }
457 }
458 if (isset($assertion->evidence)) {
ea76b652 459 $dl[get_string('evidence', 'badges')] = html_writer::tag('a', s($assertion->evidence), array('href' => $assertion->evidence));
27806552 460 }
cd243d94
Y
461 $output .= $this->definition_list($dl);
462 $output .= html_writer::end_tag('div');
27806552 463
cd243d94 464 return $output;
27806552
YB
465 }
466
cd243d94 467 // Displays the user badges.
27806552
YB
468 protected function render_badge_user_collection(badge_user_collection $badges) {
469 global $CFG, $USER, $SITE;
e2805314
YB
470 $backpack = $badges->backpack;
471 $mybackpack = new moodle_url('/badges/mybackpack.php');
472
27806552
YB
473 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
474 $htmlpagingbar = $this->render($paging);
475
e2805314
YB
476 // Set backpack connection string.
477 $backpackconnect = '';
478 if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) {
479 $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
480 }
27806552
YB
481 // Search box.
482 $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
483
484 // Download all button.
485 $downloadall = $this->output->single_button(
486 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
487 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
488
489 // Local badges.
508e756e 490 $localhtml = html_writer::start_tag('div', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
5dcda2f8 491 $heading = get_string('localbadges', 'badges', format_string($SITE->fullname, true, array('context' => context_system::instance())));
508e756e 492 $localhtml .= $this->output->heading_with_help($heading, 'localbadgesh', 'badges');
27806552 493 if ($badges->badges) {
cd243d94
Y
494 $downloadbutton = $this->output->heading(get_string('badgesearned', 'badges', $badges->totalcount), 4, 'activatebadge');
495 $downloadbutton .= $downloadall;
27806552
YB
496
497 $htmllist = $this->print_badges_list($badges->badges, $USER->id);
e2805314 498 $localhtml .= $backpackconnect . $downloadbutton . $searchform . $htmlpagingbar . $htmllist . $htmlpagingbar;
27806552
YB
499 } else {
500 $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
501 }
508e756e 502 $localhtml .= html_writer::end_tag('div');
27806552
YB
503
504 // External badges.
27806552 505 $externalhtml = "";
60d72efb 506 if (!empty($CFG->badges_allowexternalbackpack)) {
508e756e
BB
507 $externalhtml .= html_writer::start_tag('div', array('class' => 'generalbox'));
508 $externalhtml .= $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges');
27806552 509 if (!is_null($backpack)) {
e2805314
YB
510 if ($backpack->totalcollections == 0) {
511 $externalhtml .= get_string('nobackpackcollections', 'badges', $backpack);
27806552 512 } else {
e2805314
YB
513 if ($backpack->totalbadges == 0) {
514 $externalhtml .= get_string('nobackpackbadges', 'badges', $backpack);
515 } else {
516 $externalhtml .= get_string('backpackbadges', 'badges', $backpack);
517 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
518 }
27806552 519 }
27806552 520 } else {
e2805314 521 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
27806552 522 }
27806552 523
508e756e 524 $externalhtml .= html_writer::end_tag('div');
27806552
YB
525 }
526
527 return $localhtml . $externalhtml;
528 }
529
cd243d94 530 // Displays the available badges.
27806552
YB
531 protected function render_badge_collection(badge_collection $badges) {
532 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
533 $htmlpagingbar = $this->render($paging);
534 $table = new html_table();
f8ba185a 535 $table->attributes['class'] = 'collection';
27806552
YB
536
537 $sortbyname = $this->helper_sortable_heading(get_string('name'),
538 'name', $badges->sort, $badges->dir);
539 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
540 'dateissued', $badges->sort, $badges->dir);
541 $table->head = array(
542 get_string('badgeimage', 'badges'),
543 $sortbyname,
544 get_string('description', 'badges'),
545 get_string('bcriteria', 'badges'),
546 $sortbyawarded
547 );
548 $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
549
550 foreach ($badges->badges as $badge) {
551 $badgeimage = print_badge_image($badge, $this->page->context, 'large');
552 $name = $badge->name;
553 $description = $badge->description;
554 $criteria = self::print_badge_criteria($badge);
555 if ($badge->dateissued) {
a8dd22a7 556 $icon = new pix_icon('i/valid',
27806552
YB
557 get_string('dateearned', 'badges',
558 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
559 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
560 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
561 } else {
562 $awarded = "";
563 }
564 $row = array($badgeimage, $name, $description, $criteria, $awarded);
565 $table->data[] = $row;
566 }
567
568 $htmltable = html_writer::table($table);
569
570 return $htmlpagingbar . $htmltable . $htmlpagingbar;
571 }
572
573 // Outputs table of badges with actions available.
574 protected function render_badge_management(badge_management $badges) {
575 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
576
577 // New badge button.
19a9f2ea
YB
578 $htmlnew = '';
579 if (has_capability('moodle/badges:createbadge', $this->page->context)) {
580 $n['type'] = $this->page->url->get_param('type');
581 $n['id'] = $this->page->url->get_param('id');
582 $htmlnew = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
583 }
27806552
YB
584
585 $htmlpagingbar = $this->render($paging);
586 $table = new html_table();
587 $table->attributes['class'] = 'collection';
588
589 $sortbyname = $this->helper_sortable_heading(get_string('name'),
590 'name', $badges->sort, $badges->dir);
591 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
592 'status', $badges->sort, $badges->dir);
593 $table->head = array(
594 $sortbyname,
595 $sortbystatus,
596 get_string('bcriteria', 'badges'),
597 get_string('awards', 'badges'),
598 get_string('actions')
599 );
600 $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
601
602 foreach ($badges->badges as $b) {
603 $style = !$b->is_active() ? array('class' => 'dimmed') : array();
604 $forlink = print_badge_image($b, $this->page->context) . ' ' .
605 html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
606 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
607 $status = $b->statstring;
608 $criteria = self::print_badge_criteria($b, 'short');
609
610 if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
611 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
612 } else {
613 $awards = $b->awards;
614 }
615
616 $actions = self::print_badge_table_actions($b, $this->page->context);
617
618 $row = array($name, $status, $criteria, $awards, $actions);
619 $table->data[] = $row;
620 }
621 $htmltable = html_writer::table($table);
622
623 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
624 }
625
626 // Prints tabs for badge editing.
627 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
628 global $DB;
629
a5f82c5b 630 $row = array();
27806552
YB
631
632 $row[] = new tabobject('overview',
633 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
634 get_string('boverview', 'badges')
635 );
636
637 if (has_capability('moodle/badges:configuredetails', $context)) {
638 $row[] = new tabobject('details',
639 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'details')),
640 get_string('bdetails', 'badges')
641 );
642 }
643
644 if (has_capability('moodle/badges:configurecriteria', $context)) {
645 $row[] = new tabobject('criteria',
646 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
647 get_string('bcriteria', 'badges')
648 );
649 }
650
651 if (has_capability('moodle/badges:configuremessages', $context)) {
652 $row[] = new tabobject('message',
653 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
654 get_string('bmessage', 'badges')
655 );
656 }
657
658 if (has_capability('moodle/badges:viewawarded', $context)) {
7f964cfd
YB
659 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
660 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
661 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
27806552
YB
662 $row[] = new tabobject('awards',
663 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
664 get_string('bawards', 'badges', $awarded)
665 );
666 }
667
a5f82c5b 668 echo $this->tabtree($row, $current);
27806552
YB
669 }
670
36388ba8
AD
671 /**
672 * Prints badge status box.
673 * @return Either the status box html as a string or null
674 */
27806552 675 public function print_badge_status_box(badge $badge) {
27806552 676 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
36388ba8 677
27806552
YB
678 if (!$badge->has_criteria()) {
679 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
680 $status = get_string('nocriteria', 'badges');
681 if ($this->page->url != $criteriaurl) {
682 $action = $this->output->single_button(
683 $criteriaurl,
684 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
685 } else {
686 $action = '';
687 }
cd243d94
Y
688
689 $message = $status . $action;
27806552
YB
690 } else {
691 $status = get_string('statusmessage_' . $badge->status, 'badges');
692 if ($badge->is_active()) {
693 $action = $this->output->single_button(new moodle_url('/badges/action.php',
694 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
695 'return' => $this->page->url->out_as_local_url(false))),
696 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
697 } else {
698 $action = $this->output->single_button(new moodle_url('/badges/action.php',
699 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
700 'return' => $this->page->url->out_as_local_url(false))),
701 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
702 }
cd243d94
Y
703
704 $message = $status . $this->output->help_icon('status', 'badges') . $action;
705
27806552 706 }
27806552 707
36388ba8 708 $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
cd243d94 709 return $this->output->box($message, $style);
36388ba8 710 }
27806552 711
36388ba8 712 return null;
27806552
YB
713 }
714
3784d3be
YB
715 /**
716 * Returns information about badge criteria in a list form.
717 *
718 * @param badge $badge Badge objects
719 * @param string $short Indicates whether to print full info about this badge
720 * @return string $output HTML string to output
721 */
27806552 722 public function print_badge_criteria(badge $badge, $short = '') {
27806552
YB
723 $agg = $badge->get_aggregation_methods();
724 if (empty($badge->criteria)) {
725 return get_string('nocriteria', 'badges');
3784d3be
YB
726 }
727
728 $overalldescr = '';
0b6ba27a
Y
729 $overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL];
730 if (!$short && !empty($overall->description)) {
731 $overalldescr = $this->output->box(
fffeb03f 732 format_text($overall->description, $overall->descriptionformat, array('context' => $badge->get_context())),
0b6ba27a
Y
733 'criteria-description'
734 );
3784d3be 735 }
0b6ba27a
Y
736
737 // Get the condition string.
3784d3be 738 if (count($badge->criteria) == 2) {
0b6ba27a 739 $condition = '';
27806552 740 if (!$short) {
0b6ba27a 741 $condition = get_string('criteria_descr', 'badges');
27806552
YB
742 }
743 } else {
0b6ba27a 744 $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
3784d3be 745 core_text::strtoupper($agg[$badge->get_aggregation_method()]));
27806552 746 }
0b6ba27a 747
27806552 748 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
0b6ba27a
Y
749
750 $items = array();
751 // If only one criterion left, make sure its description goe to the top.
752 if (count($badge->criteria) == 1) {
753 $c = reset($badge->criteria);
754 if (!$short && !empty($c->description)) {
755 $overalldescr = $this->output->box(
fffeb03f 756 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
0b6ba27a
Y
757 'criteria-description'
758 );
3784d3be 759 }
27806552 760 if (count($c->params) == 1) {
0b6ba27a 761 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype , 'badges') .
3784d3be 762 $c->get_details($short);
27806552 763 } else {
0b6ba27a
Y
764 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype, 'badges',
765 core_text::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype)])) .
766 $c->get_details($short);
767 }
768 } else {
769 foreach ($badge->criteria as $type => $c) {
770 $criteriadescr = '';
771 if (!$short && !empty($c->description)) {
772 $criteriadescr = $this->output->box(
fffeb03f 773 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
0b6ba27a
Y
774 'criteria-description'
775 );
776 }
777 if (count($c->params) == 1) {
778 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
779 $c->get_details($short) . $criteriadescr;
780 } else {
781 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
782 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) .
783 $c->get_details($short) .
784 $criteriadescr;
785 }
27806552
YB
786 }
787 }
0b6ba27a
Y
788
789 return $overalldescr . $condition . html_writer::alist($items, array(), 'ul');;
27806552
YB
790 }
791
792 // Prints criteria actions for badge editing.
793 public function print_criteria_actions(badge $badge) {
32bd11cb 794 $output = '';
27806552
YB
795 if (!$badge->is_active() && !$badge->is_locked()) {
796 $accepted = $badge->get_accepted_criteria();
797 $potential = array_diff($accepted, array_keys($badge->criteria));
798
799 if (!empty($potential)) {
800 foreach ($potential as $p) {
801 if ($p != 0) {
802 $select[$p] = get_string('criteria_' . $p, 'badges');
803 }
804 }
32bd11cb
BB
805 $output .= $this->output->single_select(
806 new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id, 'add' => true)),
807 'type',
808 $select,
809 '',
810 array('' => 'choosedots'),
811 null,
812 array('label' => get_string('addbadgecriteria', 'badges'))
813 );
27806552 814 } else {
32bd11cb 815 $output .= $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
27806552
YB
816 }
817 }
818
32bd11cb 819 return $output;
27806552
YB
820 }
821
822 // Renders a table with users who have earned the badge.
823 // Based on stamps collection plugin.
824 protected function render_badge_recipients(badge_recipients $recipients) {
825 $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
826 $htmlpagingbar = $this->render($paging);
827 $table = new html_table();
f8ba185a 828 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
27806552
YB
829
830 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
831 'firstname', $recipients->sort, $recipients->dir);
832 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
833 'lastname', $recipients->sort, $recipients->dir);
834 if ($this->helper_fullname_format() == 'lf') {
835 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
836 } else {
837 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
838 }
839
840 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
841 'dateissued', $recipients->sort, $recipients->dir);
842
843 $table->head = array($sortbyname, $sortbydate, '');
844
845 foreach ($recipients->userids as $holder) {
846 $fullname = fullname($holder);
847 $fullname = html_writer::link(
848 new moodle_url('/user/profile.php', array('id' => $holder->userid)),
849 $fullname
850 );
851 $awarded = userdate($holder->dateissued);
852 $badgeurl = html_writer::link(
853 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
854 get_string('viewbadge', 'badges')
855 );
856
857 $row = array($fullname, $awarded, $badgeurl);
858 $table->data[] = $row;
859 }
860
861 $htmltable = html_writer::table($table);
862
863 return $htmlpagingbar . $htmltable . $htmlpagingbar;
864 }
865
866 ////////////////////////////////////////////////////////////////////////////
867 // Helper methods
868 // Reused from stamps collection plugin
869 ////////////////////////////////////////////////////////////////////////////
870
871 /**
872 * Renders a text with icons to sort by the given column
873 *
874 * This is intended for table headings.
875 *
876 * @param string $text The heading text
877 * @param string $sortid The column id used for sorting
878 * @param string $sortby Currently sorted by (column id)
879 * @param string $sorthow Currently sorted how (ASC|DESC)
880 *
881 * @return string
882 */
883 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
884 $out = html_writer::tag('span', $text, array('class' => 'text'));
885
886 if (!is_null($sortid)) {
887 if ($sortby !== $sortid || $sorthow !== 'ASC') {
888 $url = new moodle_url($this->page->url);
889 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
890 $out .= $this->output->action_icon($url,
891 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
892 }
893 if ($sortby !== $sortid || $sorthow !== 'DESC') {
894 $url = new moodle_url($this->page->url);
895 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
896 $out .= $this->output->action_icon($url,
897 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
898 }
899 }
900 return $out;
901 }
902 /**
903 * Tries to guess the fullname format set at the site
904 *
905 * @return string fl|lf
906 */
907 protected function helper_fullname_format() {
908 $fake = new stdClass();
909 $fake->lastname = 'LLLL';
910 $fake->firstname = 'FFFF';
911 $fullname = get_string('fullnamedisplay', '', $fake);
912 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
913 return 'lf';
914 } else {
915 return 'fl';
916 }
917 }
918 /**
919 * Renders a search form
920 *
921 * @param string $search Search string
922 * @return string HTML
923 */
924 protected function helper_search_form($search) {
925 global $CFG;
926 require_once($CFG->libdir . '/formslib.php');
927
928 $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
929
930 $mform->addElement('hidden', 'sesskey', sesskey());
931
932 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
933 $mform->setDefault('search', $search);
934 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
935 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
936 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
937
938 ob_start();
939 $mform->display();
940 $out = ob_get_clean();
941
942 return $out;
943 }
cd243d94
Y
944
945 /**
946 * Renders a definition list
947 *
948 * @param array $items the list of items to define
949 * @param array
950 */
951 protected function definition_list(array $items, array $attributes = array()) {
952 $output = html_writer::start_tag('dl', $attributes);
953 foreach ($items as $label => $value) {
954 $output .= html_writer::tag('dt', $label);
955 $output .= html_writer::tag('dd', $value);
956 }
957 $output .= html_writer::end_tag('dl');
958 return $output;
959 }
27806552
YB
960}
961
962/**
963 * An issued badges for badge.php page
964 */
965class issued_badge implements renderable {
966 /** @var issued badge */
967 public $issued;
968
969 /** @var badge recipient */
737a352f 970 public $recipient;
27806552 971
853e506a
Y
972 /** @var badge class */
973 public $badgeclass;
974
27806552
YB
975 /** @var badge visibility to others */
976 public $visible = 0;
977
978 /** @var badge class */
979 public $badgeid = 0;
980
27806552
YB
981 /**
982 * Initializes the badge to display
983 *
984 * @param string $hash Issued badge hash
985 */
986 public function __construct($hash) {
987 global $DB;
853e506a
Y
988
989 $assertion = new core_badges_assertion($hash);
990 $this->issued = $assertion->get_badge_assertion();
991 $this->badgeclass = $assertion->get_badge_class();
27806552
YB
992
993 $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
994 FROM {badge_issued}
995 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
996 array('hash' => $hash), IGNORE_MISSING);
997 if ($rec) {
737a352f 998 // Get a recipient from database.
5241526d 999 $namefields = get_all_user_name_fields(true, 'u');
774ead81
YB
1000 $user = $DB->get_record_sql("SELECT u.id, $namefields, u.deleted, u.email
1001 FROM {user} u WHERE u.id = :userid", array('userid' => $rec->userid));
737a352f 1002 $this->recipient = $user;
27806552
YB
1003 $this->visible = $rec->visible;
1004 $this->badgeid = $rec->badgeid;
1005 }
1006 }
1007}
1008
1009/**
1010 * An external badges for external.php page
1011 */
1012class external_badge implements renderable {
1013 /** @var issued badge */
1014 public $issued;
1015
737a352f
YB
1016 /** @var User ID */
1017 public $recipient;
1018
1019 /** @var validation of external badge */
1020 public $valid = true;
1021
27806552
YB
1022 /**
1023 * Initializes the badge to display
1024 *
2d3c0fae 1025 * @param object $badge External badge information.
737a352f 1026 * @param int $recipient User id.
27806552 1027 */
737a352f
YB
1028 public function __construct($badge, $recipient) {
1029 global $DB;
1030 // At this point a user has connected a backpack. So, we are going to get
1031 // their backpack email rather than their account email.
cd243d94
Y
1032 $namefields = get_all_user_name_fields(true, 'u');
1033 $user = $DB->get_record_sql("SELECT {$namefields}, b.email
737a352f 1034 FROM {user} u INNER JOIN {badge_backpack} b ON u.id = b.userid
cd243d94 1035 WHERE userid = :userid", array('userid' => $recipient), IGNORE_MISSING);
737a352f 1036
2d3c0fae 1037 $this->issued = $badge;
737a352f
YB
1038 $this->recipient = $user;
1039
1040 // Check if recipient is valid.
1041 // There is no way to be 100% sure that a badge belongs to a user.
1042 // Backpack does not return any recipient information.
1043 // All we can do is compare that backpack email hashed using salt
1044 // provided in the assertion matches a badge recipient from the assertion.
1045 if ($user) {
1046 if (validate_email($badge->assertion->recipient) && $badge->assertion->recipient == $user->email) {
1047 // If we have email, compare emails.
1048 $this->valid = true;
1049 } else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email)) {
1050 // If recipient is hashed, but no salt, compare hashes without salt.
1051 $this->valid = true;
1052 } else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email . $badge->assertion->salt)) {
1053 // If recipient is hashed, compare hashes.
1054 $this->valid = true;
1055 } else {
1056 // Otherwise, we cannot be sure that this user is a recipient.
1057 $this->valid = false;
1058 }
1059 } else {
1060 $this->valid = false;
1061 }
27806552
YB
1062 }
1063}
1064
1065/**
1066 * Badge recipients rendering class
1067 */
1068class badge_recipients implements renderable {
1069 /** @var string how are the data sorted */
1070 public $sort = 'lastname';
1071
1072 /** @var string how are the data sorted */
1073 public $dir = 'ASC';
1074
1075 /** @var int page number to display */
1076 public $page = 0;
1077
1078 /** @var int number of badge recipients to display per page */
1079 public $perpage = 30;
1080
1081 /** @var int the total number or badge recipients to display */
1082 public $totalcount = null;
1083
1084 /** @var array internal list of badge recipients ids */
1085 public $userids = array();
1086 /**
1087 * Initializes the list of users to display
1088 *
1089 * @param array $holders List of badge holders
1090 */
1091 public function __construct($holders) {
1092 $this->userids = $holders;
1093 }
1094}
1095
1096/**
1097 * Collection of all badges for view.php page
1098 */
1099class badge_collection implements renderable {
1100
1101 /** @var string how are the data sorted */
1102 public $sort = 'name';
1103
1104 /** @var string how are the data sorted */
1105 public $dir = 'ASC';
1106
1107 /** @var int page number to display */
1108 public $page = 0;
1109
1110 /** @var int number of badges to display per page */
1111 public $perpage = BADGE_PERPAGE;
1112
1113 /** @var int the total number of badges to display */
1114 public $totalcount = null;
1115
1116 /** @var array list of badges */
1117 public $badges = array();
1118
1119 /**
1120 * Initializes the list of badges to display
1121 *
1122 * @param array $badges Badges to render
1123 */
1124 public function __construct($badges) {
1125 $this->badges = $badges;
1126 }
1127}
1128
1129/**
1130 * Collection of badges used at the index.php page
1131 */
1132class badge_management extends badge_collection implements renderable {
1133}
1134
1135/**
1136 * Collection of user badges used at the mybadges.php page
1137 */
1138class badge_user_collection extends badge_collection implements renderable {
1139 /** @var array backpack settings */
e2805314 1140 public $backpack = null;
27806552
YB
1141
1142 /** @var string search */
1143 public $search = '';
1144
1145 /**
1146 * Initializes user badge collection.
1147 *
1148 * @param array $badges Badges to render
1149 * @param int $userid Badges owner
1150 */
1151 public function __construct($badges, $userid) {
e2805314 1152 global $CFG;
27806552 1153 parent::__construct($badges);
e2805314
YB
1154
1155 if (!empty($CFG->badges_allowexternalbackpack)) {
2d3c0fae 1156 $this->backpack = get_backpack_settings($userid, true);
e2805314 1157 }
27806552
YB
1158 }
1159}