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