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