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