MDL-64441 badges: Remove notice when language is empty
[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'),
2e477f03 116 'class' => 'actionbutton btn btn-secondary')
27806552 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(),
2e477f03 122 'class' => 'actionbutton btn btn-secondary')
b7374fac 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 = "";
d363a5c2 142 $languages = get_string_manager()->get_list_of_languages();
27806552
YB
143
144 // Badge details.
cd243d94
Y
145 $display .= $this->heading(get_string('badgedetails', 'badges'), 3);
146 $dl = array();
147 $dl[get_string('name')] = $badge->name;
d363a5c2
TT
148 $dl[get_string('version', 'badges')] = $badge->version;
149 $dl[get_string('language')] = $languages[$badge->language];
cd243d94 150 $dl[get_string('description', 'badges')] = $badge->description;
64d00f4c 151 $dl[get_string('createdon', 'search')] = userdate($badge->timecreated);
cd243d94 152 $dl[get_string('badgeimage', 'badges')] = print_badge_image($badge, $context, 'large');
65ffd0a3 153 $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname;
d363a5c2
TT
154 $dl[get_string('imageauthoremail', 'badges')] =
155 html_writer::tag('a', $badge->imageauthoremail, array('href' => 'mailto:' . $badge->imageauthoremail));
156 $dl[get_string('imageauthorurl', 'badges')] =
157 html_writer::link($badge->imageauthorurl, $badge->imageauthorurl, array('target' => '_blank'));
158 $dl[get_string('imagecaption', 'badges')] = $badge->imagecaption;
cd243d94 159 $display .= $this->definition_list($dl);
27806552 160
cd243d94
Y
161 // Issuer details.
162 $display .= $this->heading(get_string('issuerdetails', 'badges'), 3);
163 $dl = array();
164 $dl[get_string('issuername', 'badges')] = $badge->issuername;
165 $dl[get_string('contact', 'badges')] = html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact));
166 $display .= $this->definition_list($dl);
27806552
YB
167
168 // Issuance details if any.
cd243d94 169 $display .= $this->heading(get_string('issuancedetails', 'badges'), 3);
27806552
YB
170 if ($badge->can_expire()) {
171 if ($badge->expiredate) {
172 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate));
173 } else if ($badge->expireperiod) {
174 if ($badge->expireperiod < 60) {
175 $display .= get_string('expireperiods', 'badges', round($badge->expireperiod, 2));
176 } else if ($badge->expireperiod < 60 * 60) {
177 $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod / 60, 2));
178 } else if ($badge->expireperiod < 60 * 60 * 24) {
179 $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod / 60 / 60, 2));
180 } else {
181 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2));
182 }
183 }
184 } else {
185 $display .= get_string('noexpiry', 'badges');
186 }
27806552
YB
187
188 // Criteria details if any.
cd243d94 189 $display .= $this->heading(get_string('bcriteria', 'badges'), 3);
27806552
YB
190 if ($badge->has_criteria()) {
191 $display .= self::print_badge_criteria($badge);
192 } else {
193 $display .= get_string('nocriteria', 'badges');
194 if (has_capability('moodle/badges:configurecriteria', $context)) {
195 $display .= $this->output->single_button(
196 new moodle_url('/badges/criteria.php', array('id' => $badge->id)),
197 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
198 }
199 }
27806552
YB
200
201 // Awards details if any.
202 if (has_capability('moodle/badges:viewawarded', $context)) {
cd243d94 203 $display .= $this->heading(get_string('awards', 'badges'), 3);
27806552
YB
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 }
27806552
YB
221 }
222
d363a5c2
TT
223 $display .= self::print_badge_endorsement($badge);
224 $display .= self::print_badge_related($badge);
225 $display .= self::print_badge_competencies($badge);
226
cd243d94 227 return html_writer::div($display, null, array('id' => 'badge-overview'));
27806552
YB
228 }
229
230 // Prints action icons for the badge.
231 public function print_badge_table_actions($badge, $context) {
232 $actions = "";
233
4d7d17dd 234 if (has_capability('moodle/badges:configuredetails', $context) && $badge->has_criteria()) {
27806552
YB
235 // Activate/deactivate badge.
236 if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) {
4d7d17dd
YB
237 // "Activate" will go to another page and ask for confirmation.
238 $url = new moodle_url('/badges/action.php');
239 $url->param('id', $badge->id);
240 $url->param('activate', true);
c5177654 241 $url->param('sesskey', sesskey());
4d7d17dd
YB
242 $return = new moodle_url(qualified_me());
243 $url->param('return', $return->out_as_local_url(false));
27806552
YB
244 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
245 } else {
246 $url = new moodle_url(qualified_me());
247 $url->param('lock', $badge->id);
248 $url->param('sesskey', sesskey());
249 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
250 }
251 }
252
253 // Award badge manually.
254 if ($badge->has_manual_award_criteria() &&
255 has_capability('moodle/badges:awardbadge', $context) &&
256 $badge->is_active()) {
257 $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
258 $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
259 }
260
261 // Edit badge.
262 if (has_capability('moodle/badges:configuredetails', $context)) {
263 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'details'));
264 $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
265 }
266
267 // Duplicate badge.
268 if (has_capability('moodle/badges:createbadge', $context)) {
269 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey()));
270 $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
271 }
272
273 // Delete badge.
274 if (has_capability('moodle/badges:deletebadge', $context)) {
275 $url = new moodle_url(qualified_me());
276 $url->param('delete', $badge->id);
277 $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
278 }
279
280 return $actions;
281 }
282
283 // Outputs issued badge with actions available.
284 protected function render_issued_badge(issued_badge $ibadge) {
762b2a6b 285 global $USER, $CFG, $DB, $SITE;
27806552 286 $issued = $ibadge->issued;
737a352f 287 $userinfo = $ibadge->recipient;
853e506a 288 $badgeclass = $ibadge->badgeclass;
27806552 289 $badge = new badge($ibadge->badgeid);
853e506a 290 $now = time();
cd243d94 291 $expiration = isset($issued['expires']) ? $issued['expires'] : $now + 86400;
d363a5c2
TT
292 $badgeimage = is_array($badgeclass['image']) ? $badgeclass['image']['id'] : $badgeclass['image'];
293 $languages = get_string_manager()->get_list_of_languages();
27806552 294
cd243d94
Y
295 $output = '';
296 $output .= html_writer::start_tag('div', array('id' => 'badge'));
297 $output .= html_writer::start_tag('div', array('id' => 'badge-image'));
d363a5c2 298 $output .= html_writer::empty_tag('img', array('src' => $badgeimage, 'alt' => $badge->name));
cd243d94
Y
299 if ($expiration < $now) {
300 $output .= $this->output->pix_icon('i/expired',
301 get_string('expireddate', 'badges', userdate($issued['expires'])),
302 'moodle',
303 array('class' => 'expireimage'));
304 }
27806552 305
737a352f 306 if ($USER->id == $userinfo->id && !empty($CFG->enablebadges)) {
cd243d94 307 $output .= $this->output->single_button(
853e506a 308 new moodle_url('/badges/badge.php', array('hash' => $issued['uid'], 'bake' => true)),
e2805314 309 get_string('download'),
cd243d94 310 'POST');
853e506a
Y
311 if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $now) && badges_user_has_backpack($USER->id)) {
312 $assertion = new moodle_url('/badges/assertion.php', array('b' => $issued['uid']));
2c910861 313 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
e2805314 314 $attributes = array(
2c910861
YB
315 'type' => 'button',
316 'id' => 'addbutton',
317 'value' => get_string('addtobackpack', 'badges'));
e2805314 318 $tobackpack = html_writer::tag('input', '', $attributes);
2c910861 319 $this->output->add_action_handler($action, 'addbutton');
cd243d94 320 $output .= $tobackpack;
27806552 321 }
e2805314 322 }
cd243d94 323 $output .= html_writer::end_tag('div');
737a352f 324
cd243d94 325 $output .= html_writer::start_tag('div', array('id' => 'badge-details'));
737a352f 326 // Recipient information.
cd243d94
Y
327 $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3);
328 $dl = array();
762b2a6b
YB
329 if ($userinfo->deleted) {
330 $strdata = new stdClass();
331 $strdata->user = fullname($userinfo);
332 $strdata->site = format_string($SITE->fullname, true, array('context' => context_system::instance()));
cd243d94
Y
333
334 $dl[get_string('name')] = get_string('error:userdeleted', 'badges', $strdata);
737a352f 335 } else {
cd243d94 336 $dl[get_string('name')] = fullname($userinfo);
737a352f 337 }
cd243d94 338 $output .= $this->definition_list($dl);
737a352f 339
cd243d94
Y
340 $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3);
341 $dl = array();
342 $dl[get_string('issuername', 'badges')] = $badge->issuername;
e2805314 343 if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
cd243d94 344 $dl[get_string('contact', 'badges')] = obfuscate_mailto($badge->issuercontact);
e2805314 345 }
cd243d94
Y
346 $output .= $this->definition_list($dl);
347
348 $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3);
349 $dl = array();
350 $dl[get_string('name')] = $badge->name;
674c00ed
AG
351 if (!empty($badge->version)) {
352 $dl[get_string('version', 'badges')] = $badge->version;
353 }
706c8d19
SA
354 if (!empty($badge->language)) {
355 $dl[get_string('language')] = $languages[$badge->language];
356 }
cd243d94 357 $dl[get_string('description', 'badges')] = $badge->description;
674c00ed
AG
358 if (!empty($badge->imageauthorname)) {
359 $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname;
360 }
361 if (!empty($badge->imageauthoremail)) {
362 $dl[get_string('imageauthoremail', 'badges')] =
363 html_writer::tag('a', $badge->imageauthoremail, array('href' => 'mailto:' . $badge->imageauthoremail));
364 }
365 if (!empty($badge->imageauthorurl)) {
366 $dl[get_string('imageauthorurl', 'badges')] =
367 html_writer::link($badge->imageauthorurl, $badge->imageauthorurl, array('target' => '_blank'));
368 }
369 if (!empty($badge->imagecaption)) {
370 $dl[get_string('imagecaption', 'badges')] = $badge->imagecaption;
371 }
27806552 372
e2805314
YB
373 if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
374 $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid));
cd243d94 375 $dl[get_string('course')] = $coursename;
e2805314 376 }
cd243d94
Y
377 $dl[get_string('bcriteria', 'badges')] = self::print_badge_criteria($badge);
378 $output .= $this->definition_list($dl);
27806552 379
cd243d94
Y
380 $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3);
381 $dl = array();
d363a5c2
TT
382 $issued['issuedOn'] = !preg_match( '~^[1-9][0-9]*$~', $issued['issuedOn'] ) ?
383 strtotime($issued['issuedOn']) : $issued['issuedOn'];
cd243d94 384 $dl[get_string('dateawarded', 'badges')] = userdate($issued['issuedOn']);
e2805314 385 if (isset($issued['expires'])) {
d363a5c2
TT
386 $issued['expires'] = !preg_match( '~^[1-9][0-9]*$~', $issued['expires'] ) ?
387 strtotime($issued['expires']) : $issued['expires'];
853e506a 388 if ($issued['expires'] < $now) {
cd243d94 389 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']) . get_string('warnexpired', 'badges');
e2805314 390
e2805314 391 } else {
cd243d94 392 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']);
27806552 393 }
e2805314 394 }
27806552 395
e2805314
YB
396 // Print evidence.
397 $agg = $badge->get_aggregation_methods();
737a352f 398 $evidence = $badge->get_criteria_completions($userinfo->id);
484b43f4
MG
399 $eids = array_map(function($o) {
400 return $o->critid;
401 }, $evidence);
e2805314
YB
402 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
403
404 $items = array();
405 foreach ($badge->criteria as $type => $c) {
406 if (in_array($c->id, $eids)) {
407 if (count($c->params) == 1) {
408 $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
409 } else {
410 $items[] = get_string('criteria_descr_' . $type , 'badges',
ae17e383 411 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
27806552
YB
412 }
413 }
e2805314 414 }
27806552 415
cd243d94
Y
416 $dl[get_string('evidence', 'badges')] = get_string('completioninfo', 'badges') . html_writer::alist($items, array(), 'ul');
417 $output .= $this->definition_list($dl);
c693beb3
AG
418 $endorsement = $badge->get_endorsement();
419 if (!empty($endorsement)) {
420 $output .= self::print_badge_endorsement($badge);
421 }
d363a5c2 422 $relatedbadges = $badge->get_related_badges();
d363a5c2 423 if (!empty($relatedbadges)) {
c693beb3 424 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
d363a5c2
TT
425 $items = array();
426 foreach ($relatedbadges as $related) {
427 $items[] = $related->name;
428 }
429 $output .= html_writer::alist($items, array(), 'ul');
d363a5c2 430 }
d363a5c2
TT
431 $competencies = $badge->get_alignment();
432 if (!empty($competencies)) {
c693beb3 433 $output .= $this->heading(get_string('alignment', 'badges'), 3);
d363a5c2
TT
434 $items = array();
435 foreach ($competencies as $competency) {
436 $items[] = html_writer::link($competency->targeturl, $competency->targetname, array('target' => '_blank'));
437 }
438 $output .= html_writer::alist($items, array(), 'ul');
d363a5c2 439 }
cd243d94 440 $output .= html_writer::end_tag('div');
27806552 441
cd243d94 442 return $output;
27806552
YB
443 }
444
445 // Outputs external badge.
446 protected function render_external_badge(external_badge $ibadge) {
447 $issued = $ibadge->issued;
448 $assertion = $issued->assertion;
449 $issuer = $assertion->badge->issuer;
737a352f 450 $userinfo = $ibadge->recipient;
27806552 451 $table = new html_table();
ea76b652 452 $today = strtotime(date('Y-m-d'));
27806552 453
cd243d94
Y
454 $output = '';
455 $output .= html_writer::start_tag('div', array('id' => 'badge'));
456 $output .= html_writer::start_tag('div', array('id' => 'badge-image'));
ea76b652
YB
457 $output .= html_writer::empty_tag('img', array('src' => $issued->imageUrl));
458 if (isset($assertion->expires)) {
459 $expiration = !strtotime($assertion->expires) ? s($assertion->expires) : strtotime($assertion->expires);
460 if ($expiration < $today) {
461 $output .= $this->output->pix_icon('i/expired',
462 get_string('expireddate', 'badges', userdate($expiration)),
463 'moodle',
464 array('class' => 'expireimage'));
465 }
cd243d94
Y
466 }
467 $output .= html_writer::end_tag('div');
27806552 468
cd243d94 469 $output .= html_writer::start_tag('div', array('id' => 'badge-details'));
737a352f
YB
470
471 // Recipient information.
cd243d94
Y
472 $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3);
473 $dl = array();
737a352f
YB
474 // Technically, we should alway have a user at this point, but added an extra check just in case.
475 if ($userinfo) {
737a352f
YB
476 if (!$ibadge->valid) {
477 $notify = $this->output->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice');
cd243d94
Y
478 $dl[get_string('name')] = fullname($userinfo) . $notify;
479 } else {
480 $dl[get_string('name')] = fullname($userinfo);
737a352f
YB
481 }
482 } else {
483 $notify = $this->output->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice');
cd243d94 484 $dl[get_string('name')] = $notify;
737a352f 485 }
cd243d94
Y
486 $output .= $this->definition_list($dl);
487
488 $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3);
489 $dl = array();
ea76b652 490 $dl[get_string('issuername', 'badges')] = s($issuer->name);
cd243d94 491 $dl[get_string('issuerurl', 'badges')] = html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin));
737a352f 492
27806552 493 if (isset($issuer->contact)) {
cd243d94
Y
494 $dl[get_string('contact', 'badges')] = obfuscate_mailto($issuer->contact);
495 }
496 $output .= $this->definition_list($dl);
497
498 $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3);
499 $dl = array();
ea76b652
YB
500 $dl[get_string('name')] = s($assertion->badge->name);
501 $dl[get_string('description', 'badges')] = s($assertion->badge->description);
502 $dl[get_string('bcriteria', 'badges')] = html_writer::tag('a', s($assertion->badge->criteria), array('href' => $assertion->badge->criteria));
cd243d94
Y
503 $output .= $this->definition_list($dl);
504
505 $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3);
506 $dl = array();
27806552 507 if (isset($assertion->issued_on)) {
ea76b652
YB
508 $issuedate = !strtotime($assertion->issued_on) ? s($assertion->issued_on) : strtotime($assertion->issued_on);
509 $dl[get_string('dateawarded', 'badges')] = userdate($issuedate);
27806552 510 }
ea76b652 511 if (isset($assertion->expires)) {
27806552 512 if ($expiration < $today) {
ea76b652 513 $dl[get_string('expirydate', 'badges')] = userdate($expiration) . get_string('warnexpired', 'badges');
27806552 514 } else {
ea76b652 515 $dl[get_string('expirydate', 'badges')] = userdate($expiration);
27806552
YB
516 }
517 }
518 if (isset($assertion->evidence)) {
ea76b652 519 $dl[get_string('evidence', 'badges')] = html_writer::tag('a', s($assertion->evidence), array('href' => $assertion->evidence));
27806552 520 }
cd243d94
Y
521 $output .= $this->definition_list($dl);
522 $output .= html_writer::end_tag('div');
27806552 523
cd243d94 524 return $output;
27806552
YB
525 }
526
cd243d94 527 // Displays the user badges.
27806552
YB
528 protected function render_badge_user_collection(badge_user_collection $badges) {
529 global $CFG, $USER, $SITE;
e2805314
YB
530 $backpack = $badges->backpack;
531 $mybackpack = new moodle_url('/badges/mybackpack.php');
532
27806552
YB
533 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
534 $htmlpagingbar = $this->render($paging);
535
e2805314
YB
536 // Set backpack connection string.
537 $backpackconnect = '';
538 if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) {
539 $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
540 }
27806552
YB
541 // Search box.
542 $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
543
544 // Download all button.
545 $downloadall = $this->output->single_button(
546 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
547 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
548
549 // Local badges.
508e756e 550 $localhtml = html_writer::start_tag('div', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
5dcda2f8 551 $heading = get_string('localbadges', 'badges', format_string($SITE->fullname, true, array('context' => context_system::instance())));
508e756e 552 $localhtml .= $this->output->heading_with_help($heading, 'localbadgesh', 'badges');
27806552 553 if ($badges->badges) {
cd243d94
Y
554 $downloadbutton = $this->output->heading(get_string('badgesearned', 'badges', $badges->totalcount), 4, 'activatebadge');
555 $downloadbutton .= $downloadall;
27806552
YB
556
557 $htmllist = $this->print_badges_list($badges->badges, $USER->id);
e2805314 558 $localhtml .= $backpackconnect . $downloadbutton . $searchform . $htmlpagingbar . $htmllist . $htmlpagingbar;
27806552
YB
559 } else {
560 $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
561 }
508e756e 562 $localhtml .= html_writer::end_tag('div');
27806552
YB
563
564 // External badges.
27806552 565 $externalhtml = "";
60d72efb 566 if (!empty($CFG->badges_allowexternalbackpack)) {
508e756e
BB
567 $externalhtml .= html_writer::start_tag('div', array('class' => 'generalbox'));
568 $externalhtml .= $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges');
27806552 569 if (!is_null($backpack)) {
e2805314
YB
570 if ($backpack->totalcollections == 0) {
571 $externalhtml .= get_string('nobackpackcollections', 'badges', $backpack);
27806552 572 } else {
e2805314
YB
573 if ($backpack->totalbadges == 0) {
574 $externalhtml .= get_string('nobackpackbadges', 'badges', $backpack);
575 } else {
576 $externalhtml .= get_string('backpackbadges', 'badges', $backpack);
577 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
578 }
27806552 579 }
27806552 580 } else {
e2805314 581 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
27806552 582 }
27806552 583
508e756e 584 $externalhtml .= html_writer::end_tag('div');
27806552
YB
585 }
586
587 return $localhtml . $externalhtml;
588 }
589
cd243d94 590 // Displays the available badges.
27806552
YB
591 protected function render_badge_collection(badge_collection $badges) {
592 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
593 $htmlpagingbar = $this->render($paging);
594 $table = new html_table();
f8ba185a 595 $table->attributes['class'] = 'collection';
27806552
YB
596
597 $sortbyname = $this->helper_sortable_heading(get_string('name'),
598 'name', $badges->sort, $badges->dir);
599 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
600 'dateissued', $badges->sort, $badges->dir);
601 $table->head = array(
602 get_string('badgeimage', 'badges'),
603 $sortbyname,
604 get_string('description', 'badges'),
605 get_string('bcriteria', 'badges'),
606 $sortbyawarded
607 );
608 $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
609
610 foreach ($badges->badges as $badge) {
611 $badgeimage = print_badge_image($badge, $this->page->context, 'large');
612 $name = $badge->name;
613 $description = $badge->description;
614 $criteria = self::print_badge_criteria($badge);
615 if ($badge->dateissued) {
a8dd22a7 616 $icon = new pix_icon('i/valid',
27806552
YB
617 get_string('dateearned', 'badges',
618 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
619 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
620 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
621 } else {
622 $awarded = "";
623 }
624 $row = array($badgeimage, $name, $description, $criteria, $awarded);
625 $table->data[] = $row;
626 }
627
628 $htmltable = html_writer::table($table);
629
630 return $htmlpagingbar . $htmltable . $htmlpagingbar;
631 }
632
633 // Outputs table of badges with actions available.
634 protected function render_badge_management(badge_management $badges) {
635 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
636
637 // New badge button.
19a9f2ea
YB
638 $htmlnew = '';
639 if (has_capability('moodle/badges:createbadge', $this->page->context)) {
640 $n['type'] = $this->page->url->get_param('type');
641 $n['id'] = $this->page->url->get_param('id');
642 $htmlnew = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
643 }
27806552
YB
644
645 $htmlpagingbar = $this->render($paging);
646 $table = new html_table();
647 $table->attributes['class'] = 'collection';
648
649 $sortbyname = $this->helper_sortable_heading(get_string('name'),
650 'name', $badges->sort, $badges->dir);
651 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
652 'status', $badges->sort, $badges->dir);
653 $table->head = array(
654 $sortbyname,
655 $sortbystatus,
656 get_string('bcriteria', 'badges'),
657 get_string('awards', 'badges'),
658 get_string('actions')
659 );
660 $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
661
662 foreach ($badges->badges as $b) {
663 $style = !$b->is_active() ? array('class' => 'dimmed') : array();
664 $forlink = print_badge_image($b, $this->page->context) . ' ' .
665 html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
666 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
667 $status = $b->statstring;
668 $criteria = self::print_badge_criteria($b, 'short');
669
670 if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
671 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
672 } else {
673 $awards = $b->awards;
674 }
675
676 $actions = self::print_badge_table_actions($b, $this->page->context);
677
678 $row = array($name, $status, $criteria, $awards, $actions);
679 $table->data[] = $row;
680 }
681 $htmltable = html_writer::table($table);
682
683 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
684 }
685
686 // Prints tabs for badge editing.
687 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
688 global $DB;
689
d363a5c2 690 $badge = new badge($badgeid);
a5f82c5b 691 $row = array();
27806552
YB
692
693 $row[] = new tabobject('overview',
694 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
695 get_string('boverview', 'badges')
696 );
697
698 if (has_capability('moodle/badges:configuredetails', $context)) {
699 $row[] = new tabobject('details',
700 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'details')),
701 get_string('bdetails', 'badges')
702 );
703 }
704
705 if (has_capability('moodle/badges:configurecriteria', $context)) {
706 $row[] = new tabobject('criteria',
707 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
708 get_string('bcriteria', 'badges')
709 );
710 }
711
712 if (has_capability('moodle/badges:configuremessages', $context)) {
713 $row[] = new tabobject('message',
714 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
715 get_string('bmessage', 'badges')
716 );
717 }
718
719 if (has_capability('moodle/badges:viewawarded', $context)) {
7f964cfd
YB
720 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
721 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
722 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
27806552
YB
723 $row[] = new tabobject('awards',
724 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
725 get_string('bawards', 'badges', $awarded)
726 );
727 }
728
d363a5c2
TT
729 if (has_capability('moodle/badges:configuredetails', $context)) {
730 $row[] = new tabobject('bendorsement',
731 new moodle_url('/badges/endorsement.php', array('id' => $badgeid)),
732 get_string('bendorsement', 'badges')
733 );
734 }
735
736 if (has_capability('moodle/badges:configuredetails', $context)) {
c6607726
JD
737 $sql = "SELECT COUNT(br.badgeid)
738 FROM {badge_related} br
739 WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
740 $related = $DB->count_records_sql($sql, ['badgeid' => $badgeid, 'badgeid2' => $badgeid]);
d363a5c2
TT
741 $row[] = new tabobject('brelated',
742 new moodle_url('/badges/related.php', array('id' => $badgeid)),
743 get_string('brelated', 'badges', $related)
744 );
745 }
746
747 if (has_capability('moodle/badges:configuredetails', $context)) {
748 $competencies = $DB->count_records_sql("SELECT COUNT(bc.id)
749 FROM {badge_competencies} bc WHERE bc.badgeid = :badgeid", array('badgeid' => $badgeid));
750 $row[] = new tabobject('balignment',
751 new moodle_url('/badges/competency.php', array('id' => $badgeid)),
752 get_string('balignment', 'badges', $competencies)
753 );
754 }
755
a5f82c5b 756 echo $this->tabtree($row, $current);
27806552
YB
757 }
758
36388ba8
AD
759 /**
760 * Prints badge status box.
761 * @return Either the status box html as a string or null
762 */
27806552 763 public function print_badge_status_box(badge $badge) {
27806552 764 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
36388ba8 765
27806552
YB
766 if (!$badge->has_criteria()) {
767 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
768 $status = get_string('nocriteria', 'badges');
769 if ($this->page->url != $criteriaurl) {
770 $action = $this->output->single_button(
771 $criteriaurl,
772 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
773 } else {
774 $action = '';
775 }
cd243d94
Y
776
777 $message = $status . $action;
27806552
YB
778 } else {
779 $status = get_string('statusmessage_' . $badge->status, 'badges');
780 if ($badge->is_active()) {
781 $action = $this->output->single_button(new moodle_url('/badges/action.php',
782 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
783 'return' => $this->page->url->out_as_local_url(false))),
784 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
785 } else {
786 $action = $this->output->single_button(new moodle_url('/badges/action.php',
787 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
788 'return' => $this->page->url->out_as_local_url(false))),
789 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
790 }
cd243d94
Y
791
792 $message = $status . $this->output->help_icon('status', 'badges') . $action;
793
27806552 794 }
27806552 795
36388ba8 796 $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
cd243d94 797 return $this->output->box($message, $style);
36388ba8 798 }
27806552 799
36388ba8 800 return null;
27806552
YB
801 }
802
3784d3be
YB
803 /**
804 * Returns information about badge criteria in a list form.
805 *
806 * @param badge $badge Badge objects
807 * @param string $short Indicates whether to print full info about this badge
808 * @return string $output HTML string to output
809 */
27806552 810 public function print_badge_criteria(badge $badge, $short = '') {
27806552
YB
811 $agg = $badge->get_aggregation_methods();
812 if (empty($badge->criteria)) {
813 return get_string('nocriteria', 'badges');
3784d3be
YB
814 }
815
816 $overalldescr = '';
0b6ba27a
Y
817 $overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL];
818 if (!$short && !empty($overall->description)) {
819 $overalldescr = $this->output->box(
fffeb03f 820 format_text($overall->description, $overall->descriptionformat, array('context' => $badge->get_context())),
0b6ba27a
Y
821 'criteria-description'
822 );
3784d3be 823 }
0b6ba27a
Y
824
825 // Get the condition string.
3784d3be 826 if (count($badge->criteria) == 2) {
0b6ba27a 827 $condition = '';
27806552 828 if (!$short) {
0b6ba27a 829 $condition = get_string('criteria_descr', 'badges');
27806552
YB
830 }
831 } else {
0b6ba27a 832 $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
3784d3be 833 core_text::strtoupper($agg[$badge->get_aggregation_method()]));
27806552 834 }
0b6ba27a 835
27806552 836 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
0b6ba27a
Y
837
838 $items = array();
839 // If only one criterion left, make sure its description goe to the top.
840 if (count($badge->criteria) == 1) {
841 $c = reset($badge->criteria);
842 if (!$short && !empty($c->description)) {
843 $overalldescr = $this->output->box(
fffeb03f 844 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
0b6ba27a
Y
845 'criteria-description'
846 );
3784d3be 847 }
27806552 848 if (count($c->params) == 1) {
0b6ba27a 849 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype , 'badges') .
3784d3be 850 $c->get_details($short);
27806552 851 } else {
0b6ba27a
Y
852 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype, 'badges',
853 core_text::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype)])) .
854 $c->get_details($short);
855 }
856 } else {
857 foreach ($badge->criteria as $type => $c) {
858 $criteriadescr = '';
859 if (!$short && !empty($c->description)) {
860 $criteriadescr = $this->output->box(
fffeb03f 861 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
0b6ba27a
Y
862 'criteria-description'
863 );
864 }
865 if (count($c->params) == 1) {
866 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
867 $c->get_details($short) . $criteriadescr;
868 } else {
869 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
870 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) .
871 $c->get_details($short) .
872 $criteriadescr;
873 }
27806552
YB
874 }
875 }
0b6ba27a
Y
876
877 return $overalldescr . $condition . html_writer::alist($items, array(), 'ul');;
27806552
YB
878 }
879
880 // Prints criteria actions for badge editing.
881 public function print_criteria_actions(badge $badge) {
32bd11cb 882 $output = '';
27806552
YB
883 if (!$badge->is_active() && !$badge->is_locked()) {
884 $accepted = $badge->get_accepted_criteria();
885 $potential = array_diff($accepted, array_keys($badge->criteria));
886
887 if (!empty($potential)) {
888 foreach ($potential as $p) {
889 if ($p != 0) {
890 $select[$p] = get_string('criteria_' . $p, 'badges');
891 }
892 }
32bd11cb
BB
893 $output .= $this->output->single_select(
894 new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id, 'add' => true)),
895 'type',
896 $select,
897 '',
898 array('' => 'choosedots'),
899 null,
900 array('label' => get_string('addbadgecriteria', 'badges'))
901 );
27806552 902 } else {
32bd11cb 903 $output .= $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
27806552
YB
904 }
905 }
906
32bd11cb 907 return $output;
27806552
YB
908 }
909
910 // Renders a table with users who have earned the badge.
911 // Based on stamps collection plugin.
912 protected function render_badge_recipients(badge_recipients $recipients) {
913 $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
914 $htmlpagingbar = $this->render($paging);
915 $table = new html_table();
f8ba185a 916 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
27806552
YB
917
918 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
919 'firstname', $recipients->sort, $recipients->dir);
920 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
921 'lastname', $recipients->sort, $recipients->dir);
922 if ($this->helper_fullname_format() == 'lf') {
923 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
924 } else {
925 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
926 }
927
928 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
929 'dateissued', $recipients->sort, $recipients->dir);
930
931 $table->head = array($sortbyname, $sortbydate, '');
932
933 foreach ($recipients->userids as $holder) {
934 $fullname = fullname($holder);
935 $fullname = html_writer::link(
936 new moodle_url('/user/profile.php', array('id' => $holder->userid)),
937 $fullname
938 );
939 $awarded = userdate($holder->dateissued);
940 $badgeurl = html_writer::link(
941 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
942 get_string('viewbadge', 'badges')
943 );
944
945 $row = array($fullname, $awarded, $badgeurl);
946 $table->data[] = $row;
947 }
948
949 $htmltable = html_writer::table($table);
950
951 return $htmlpagingbar . $htmltable . $htmlpagingbar;
952 }
953
954 ////////////////////////////////////////////////////////////////////////////
955 // Helper methods
956 // Reused from stamps collection plugin
957 ////////////////////////////////////////////////////////////////////////////
958
959 /**
960 * Renders a text with icons to sort by the given column
961 *
962 * This is intended for table headings.
963 *
964 * @param string $text The heading text
965 * @param string $sortid The column id used for sorting
966 * @param string $sortby Currently sorted by (column id)
967 * @param string $sorthow Currently sorted how (ASC|DESC)
968 *
969 * @return string
970 */
971 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
972 $out = html_writer::tag('span', $text, array('class' => 'text'));
973
974 if (!is_null($sortid)) {
975 if ($sortby !== $sortid || $sorthow !== 'ASC') {
976 $url = new moodle_url($this->page->url);
977 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
978 $out .= $this->output->action_icon($url,
979 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
980 }
981 if ($sortby !== $sortid || $sorthow !== 'DESC') {
982 $url = new moodle_url($this->page->url);
983 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
984 $out .= $this->output->action_icon($url,
985 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
986 }
987 }
988 return $out;
989 }
990 /**
991 * Tries to guess the fullname format set at the site
992 *
993 * @return string fl|lf
994 */
995 protected function helper_fullname_format() {
996 $fake = new stdClass();
997 $fake->lastname = 'LLLL';
998 $fake->firstname = 'FFFF';
999 $fullname = get_string('fullnamedisplay', '', $fake);
1000 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
1001 return 'lf';
1002 } else {
1003 return 'fl';
1004 }
1005 }
1006 /**
1007 * Renders a search form
1008 *
1009 * @param string $search Search string
1010 * @return string HTML
1011 */
1012 protected function helper_search_form($search) {
1013 global $CFG;
1014 require_once($CFG->libdir . '/formslib.php');
1015
1016 $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
1017
1018 $mform->addElement('hidden', 'sesskey', sesskey());
1019
1020 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
1021 $mform->setDefault('search', $search);
1022 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
1023 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
1024 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
1025
1026 ob_start();
1027 $mform->display();
1028 $out = ob_get_clean();
1029
1030 return $out;
1031 }
cd243d94
Y
1032
1033 /**
1034 * Renders a definition list
1035 *
1036 * @param array $items the list of items to define
1037 * @param array
1038 */
1039 protected function definition_list(array $items, array $attributes = array()) {
1040 $output = html_writer::start_tag('dl', $attributes);
1041 foreach ($items as $label => $value) {
1042 $output .= html_writer::tag('dt', $label);
1043 $output .= html_writer::tag('dd', $value);
1044 }
1045 $output .= html_writer::end_tag('dl');
1046 return $output;
1047 }
d363a5c2
TT
1048
1049 /**
1050 * Outputs list en badges.
1051 *
1052 * @param badge $badge Badge object.
1053 * @return string $output content endorsement to output.
1054 */
1055 protected function print_badge_endorsement(badge $badge) {
1056 $output = '';
1057 $endorsement = $badge->get_endorsement();
1058 $dl = array();
1059 $output .= $this->heading(get_string('endorsement', 'badges'), 3);
1060 if (!empty($endorsement)) {
1061 $dl[get_string('issuername', 'badges')] = $endorsement->issuername;
65ffd0a3
PFO
1062 $dl[get_string('issueremail', 'badges')] =
1063 html_writer::tag('a', $endorsement->issueremail, array('href' => 'mailto:' . $endorsement->issueremail));
d363a5c2
TT
1064 $dl[get_string('issuerurl', 'badges')] = html_writer::link($endorsement->issuerurl, $endorsement->issuerurl,
1065 array('target' => '_blank'));
750f1588 1066 $dl[get_string('dateawarded', 'badges')] = userdate($endorsement->dateissued);
d363a5c2
TT
1067 $dl[get_string('claimid', 'badges')] = html_writer::link($endorsement->claimid, $endorsement->claimid,
1068 array('target' => '_blank'));
1069 $dl[get_string('claimcomment', 'badges')] = $endorsement->claimcomment;
1070 $output .= $this->definition_list($dl);
1071 } else {
1072 $output .= get_string('noendorsement', 'badges');
1073 }
1074 return $output;
1075 }
1076
1077 /**
1078 * Print list badges related.
1079 *
1080 * @param badge $badge Badge objects.
1081 * @return string $output List related badges to output.
1082 */
1083 protected function print_badge_related(badge $badge) {
1084 $output = '';
1085 $relatedbadges = $badge->get_related_badges();
1086 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
1087 if (!empty($relatedbadges)) {
1088 $items = array();
1089 foreach ($relatedbadges as $related) {
1090 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id));
1091 $items[] = html_writer::link($relatedurl->out(), $related->name, array('target' => '_blank'));
1092 }
1093 $output .= html_writer::alist($items, array(), 'ul');
1094 } else {
1095 $output .= get_string('norelated', 'badges');
1096 }
1097 return $output;
1098 }
1099
1100 /**
1101 * Print list badge competencies.
1102 *
1103 * @param badge $badge Badge objects.
1104 * @return string $output List competencies to output.
1105 */
1106 protected function print_badge_competencies(badge $badge) {
1107 $output = '';
1108 $output .= $this->heading(get_string('alignment', 'badges'), 3);
1109 $competencies = $badge->get_alignment();
1110 if (!empty($competencies)) {
1111 $items = array();
1112 foreach ($competencies as $competency) {
1113 $urlaligment = new moodle_url('competency.php',
1114 array('id' => $badge->id, 'alignmentid' => $competency->id)
1115 );
1116 $items[] = html_writer::link($urlaligment, $competency->targetname, array('target' => '_blank'));
1117 }
1118 $output .= html_writer::alist($items, array(), 'ul');
1119 } else {
1120 $output .= get_string('noalignment', 'badges');
1121 }
1122 return $output;
1123 }
1124
1125 /**
1126 * Renders a table for related badges.
1127 *
1128 * @param badge_related $related list related badges.
1129 * @return string list related badges to output.
1130 */
1131 protected function render_badge_related(badge_related $related) {
1132 $currentbadge = new badge($related->currentbadgeid);
1133 $languages = get_string_manager()->get_list_of_languages();
1134 $paging = new paging_bar($related->totalcount, $related->page, $related->perpage, $this->page->url, 'page');
1135 $htmlpagingbar = $this->render($paging);
1136 $table = new html_table();
1137 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1138 $table->head = array(
1139 get_string('name'),
1140 get_string('version', 'badges'),
1141 get_string('language', 'badges'),
1142 get_string('type', 'badges')
1143 );
1144 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1145 array_push($table->head, '');
1146 }
1147
1148 foreach ($related->badges as $badge) {
1149 $badgeobject = new badge($badge->id);
1150 $style = array('title' => $badgeobject->name);
1151 if (!$badgeobject->is_active()) {
1152 $style['class'] = 'dimmed';
1153 }
1154 $context = ($badgeobject->type == BADGE_TYPE_SITE) ?
1155 context_system::instance() : context_course::instance($badgeobject->courseid);
1156 $forlink = print_badge_image($badgeobject, $context) . ' ' .
1157 html_writer::start_tag('span') . $badgeobject->name . html_writer::end_tag('span');
1158 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $badgeobject->id)), $forlink, $style);
1159
1160 $row = array(
1161 $name,
1162 $badge->version,
1163 $badge->language ? $languages[$badge->language] : '',
1164 $badge->type == BADGE_TYPE_COURSE ? get_string('badgesview', 'badges') : get_string('sitebadges', 'badges')
1165 );
1166 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1167 $action = $this->output->action_icon(
1168 new moodle_url('related_action.php',
1169 array(
1170 'badgeid' => $related->currentbadgeid,
1171 'relatedid' => $badge->id,
1172 'action' => 'remove'
1173 )
1174 ), new pix_icon('t/delete', get_string('delete')));
1175 $actions = html_writer::tag('div', $action, array('class' => 'badge-actions'));
1176 array_push($row, $actions);
1177 }
1178 $table->data[] = $row;
1179 }
1180 $htmltable = html_writer::table($table);
1181
1182 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1183 }
1184
1185 /**
1186 * Renders a table with competencies alignment.
1187 *
1188 * @param badge_competencies_alignment $alignment List competencies alignment.
1189 * @return string List competencies aligment to output.
1190 */
1191 protected function render_badge_competencies_alignment(badge_competencies_alignment $alignment) {
1192 $currentbadge = new badge($alignment->currentbadgeid);
1193 $paging = new paging_bar($alignment->totalcount, $alignment->page, $alignment->perpage, $this->page->url, 'page');
1194 $htmlpagingbar = $this->render($paging);
1195 $table = new html_table();
1196 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1197 $table->head = array('Name', 'URL', '');
1198
1199 foreach ($alignment->alignments as $item) {
1200 $urlaligment = new moodle_url('competency.php',
1201 array(
1202 'id' => $currentbadge->id,
1203 'alignmentid' => $item->id,
1204 )
1205 );
1206 $row = array(
1207 html_writer::link($urlaligment, $item->targetname),
1208 html_writer::link($item->targeturl, $item->targeturl, array('target' => '_blank'))
1209 );
1210 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1211 $delete = $this->output->action_icon(
1212 new moodle_url('competency_action.php',
1213 array(
1214 'id' => $currentbadge->id,
1215 'alignmentid' => $item->id,
1216 'action' => 'remove'
1217 )
1218 ), new pix_icon('t/delete', get_string('delete')));
1219 $edit = $this->output->action_icon(
1220 new moodle_url('competency.php',
1221 array(
1222 'id' => $currentbadge->id,
1223 'alignmentid' => $item->id,
1224 'action' => 'edit'
1225 )
1226 ), new pix_icon('t/edit', get_string('edit')));
1227 $actions = html_writer::tag('div', $edit . $delete, array('class' => 'badge-actions'));
1228 array_push($row, $actions);
1229 }
1230 $table->data[] = $row;
1231 }
1232 $htmltable = html_writer::table($table);
1233
1234 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1235 }
27806552
YB
1236}
1237
1238/**
1239 * An issued badges for badge.php page
1240 */
1241class issued_badge implements renderable {
1242 /** @var issued badge */
1243 public $issued;
1244
1245 /** @var badge recipient */
737a352f 1246 public $recipient;
27806552 1247
853e506a
Y
1248 /** @var badge class */
1249 public $badgeclass;
1250
27806552
YB
1251 /** @var badge visibility to others */
1252 public $visible = 0;
1253
1254 /** @var badge class */
1255 public $badgeid = 0;
1256
27806552
YB
1257 /**
1258 * Initializes the badge to display
1259 *
1260 * @param string $hash Issued badge hash
1261 */
1262 public function __construct($hash) {
1263 global $DB;
853e506a
Y
1264
1265 $assertion = new core_badges_assertion($hash);
1266 $this->issued = $assertion->get_badge_assertion();
1267 $this->badgeclass = $assertion->get_badge_class();
27806552
YB
1268
1269 $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
1270 FROM {badge_issued}
1271 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
1272 array('hash' => $hash), IGNORE_MISSING);
1273 if ($rec) {
737a352f 1274 // Get a recipient from database.
5241526d 1275 $namefields = get_all_user_name_fields(true, 'u');
774ead81
YB
1276 $user = $DB->get_record_sql("SELECT u.id, $namefields, u.deleted, u.email
1277 FROM {user} u WHERE u.id = :userid", array('userid' => $rec->userid));
737a352f 1278 $this->recipient = $user;
27806552
YB
1279 $this->visible = $rec->visible;
1280 $this->badgeid = $rec->badgeid;
1281 }
1282 }
1283}
1284
1285/**
1286 * An external badges for external.php page
1287 */
1288class external_badge implements renderable {
1289 /** @var issued badge */
1290 public $issued;
1291
737a352f
YB
1292 /** @var User ID */
1293 public $recipient;
1294
1295 /** @var validation of external badge */
1296 public $valid = true;
1297
27806552
YB
1298 /**
1299 * Initializes the badge to display
1300 *
2d3c0fae 1301 * @param object $badge External badge information.
737a352f 1302 * @param int $recipient User id.
27806552 1303 */
737a352f
YB
1304 public function __construct($badge, $recipient) {
1305 global $DB;
1306 // At this point a user has connected a backpack. So, we are going to get
1307 // their backpack email rather than their account email.
cd243d94
Y
1308 $namefields = get_all_user_name_fields(true, 'u');
1309 $user = $DB->get_record_sql("SELECT {$namefields}, b.email
737a352f 1310 FROM {user} u INNER JOIN {badge_backpack} b ON u.id = b.userid
cd243d94 1311 WHERE userid = :userid", array('userid' => $recipient), IGNORE_MISSING);
737a352f 1312
2d3c0fae 1313 $this->issued = $badge;
737a352f
YB
1314 $this->recipient = $user;
1315
1316 // Check if recipient is valid.
1317 // There is no way to be 100% sure that a badge belongs to a user.
1318 // Backpack does not return any recipient information.
1319 // All we can do is compare that backpack email hashed using salt
1320 // provided in the assertion matches a badge recipient from the assertion.
1321 if ($user) {
1322 if (validate_email($badge->assertion->recipient) && $badge->assertion->recipient == $user->email) {
1323 // If we have email, compare emails.
1324 $this->valid = true;
1325 } else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email)) {
1326 // If recipient is hashed, but no salt, compare hashes without salt.
1327 $this->valid = true;
1328 } else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email . $badge->assertion->salt)) {
1329 // If recipient is hashed, compare hashes.
1330 $this->valid = true;
1331 } else {
1332 // Otherwise, we cannot be sure that this user is a recipient.
1333 $this->valid = false;
1334 }
1335 } else {
1336 $this->valid = false;
1337 }
27806552
YB
1338 }
1339}
1340
1341/**
1342 * Badge recipients rendering class
1343 */
1344class badge_recipients implements renderable {
1345 /** @var string how are the data sorted */
1346 public $sort = 'lastname';
1347
1348 /** @var string how are the data sorted */
1349 public $dir = 'ASC';
1350
1351 /** @var int page number to display */
1352 public $page = 0;
1353
1354 /** @var int number of badge recipients to display per page */
1355 public $perpage = 30;
1356
1357 /** @var int the total number or badge recipients to display */
1358 public $totalcount = null;
1359
1360 /** @var array internal list of badge recipients ids */
1361 public $userids = array();
1362 /**
1363 * Initializes the list of users to display
1364 *
1365 * @param array $holders List of badge holders
1366 */
1367 public function __construct($holders) {
1368 $this->userids = $holders;
1369 }
1370}
1371
1372/**
1373 * Collection of all badges for view.php page
1374 */
1375class badge_collection implements renderable {
1376
1377 /** @var string how are the data sorted */
1378 public $sort = 'name';
1379
1380 /** @var string how are the data sorted */
1381 public $dir = 'ASC';
1382
1383 /** @var int page number to display */
1384 public $page = 0;
1385
1386 /** @var int number of badges to display per page */
1387 public $perpage = BADGE_PERPAGE;
1388
1389 /** @var int the total number of badges to display */
1390 public $totalcount = null;
1391
1392 /** @var array list of badges */
1393 public $badges = array();
1394
1395 /**
1396 * Initializes the list of badges to display
1397 *
1398 * @param array $badges Badges to render
1399 */
1400 public function __construct($badges) {
1401 $this->badges = $badges;
1402 }
1403}
1404
1405/**
1406 * Collection of badges used at the index.php page
1407 */
1408class badge_management extends badge_collection implements renderable {
1409}
1410
1411/**
1412 * Collection of user badges used at the mybadges.php page
1413 */
1414class badge_user_collection extends badge_collection implements renderable {
1415 /** @var array backpack settings */
e2805314 1416 public $backpack = null;
27806552
YB
1417
1418 /** @var string search */
1419 public $search = '';
1420
1421 /**
1422 * Initializes user badge collection.
1423 *
1424 * @param array $badges Badges to render
1425 * @param int $userid Badges owner
1426 */
1427 public function __construct($badges, $userid) {
e2805314 1428 global $CFG;
27806552 1429 parent::__construct($badges);
e2805314
YB
1430
1431 if (!empty($CFG->badges_allowexternalbackpack)) {
2d3c0fae 1432 $this->backpack = get_backpack_settings($userid, true);
e2805314 1433 }
27806552
YB
1434 }
1435}
d363a5c2
TT
1436
1437/**
1438 * Collection of all related badges.
1439 *
1440 * @copyright 2018 Tung Thai
1441 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1442 * @author Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
1443 */
1444class badge_related implements renderable {
1445
1446 /** @var string how are the data sorted. */
1447 public $sort = 'name';
1448
1449 /** @var string how are the data sorted. */
1450 public $dir = 'ASC';
1451
1452 /** @var int page number to display. */
1453 public $page = 0;
1454
1455 /** @var int number of badges to display per page. */
1456 public $perpage = BADGE_PERPAGE;
1457
1458 /** @var int the total number of badges to display. */
1459 public $totalcount = null;
1460
1461 /** @var int the current badge. */
1462 public $currentbadgeid = 0;
1463
1464 /** @var array list of badges. */
1465 public $badges = array();
1466
1467 /**
1468 * Initializes the list of badges to display.
1469 *
1470 * @param array $badges related badges to render.
1471 * @param int $currentbadgeid ID current badge.
1472 */
1473 public function __construct($badges, $currentbadgeid) {
1474 $this->badges = $badges;
1475 $this->currentbadgeid = $currentbadgeid;
1476 }
1477}
1478
1479/**
1480 * Collection of all competencies.
1481 *
1482 * @copyright 2018 Tung Thai
1483 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1484 * @author Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
1485 */
1486class badge_competencies_alignment implements renderable
1487{
1488 /** @var string how are the data sorted. */
1489 public $sort = 'name';
1490
1491 /** @var string how are the data sorted. */
1492 public $dir = 'ASC';
1493
1494 /** @var int page number to display. */
1495 public $page = 0;
1496
1497 /** @var int number of badges to display per page. */
1498 public $perpage = BADGE_PERPAGE;
1499
1500 /** @var int the total number of badges to display. */
1501 public $totalcount = null;
1502
1503 /** @var array list of badges. */
1504 public $alignments = array();
1505
1506 /** @var array list of badges. */
1507 public $currentbadgeid = 0;
1508
1509 /**
1510 * Initializes the list of competencies to display.
1511 *
1512 * @param array $alignments List competencies alignment to render.
1513 * @param int $currentbadgeid ID current badge.
1514 */
1515 public function __construct($alignments, $currentbadgeid) {
1516 $this->alignments = $alignments;
1517 $this->currentbadgeid = $currentbadgeid;
1518 }
c6607726 1519}