MDL-71161 mod_scorm: Display activity completion information
[moodle.git] / badges / renderer.php
blob1afadf478eb27b9db47ab3111d81962a77e6de93
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.
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/>.
17 /**
18 * Renderer for use with the badges output
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>
27 require_once($CFG->libdir . '/badgeslib.php');
28 require_once($CFG->libdir . '/tablelib.php');
30 /**
31 * Standard HTML output renderer for badges
33 class core_badges_renderer extends plugin_renderer_base {
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, '/', 'f3', false);
43 } else {
44 $bname = '';
45 $imageurl = '';
46 if (!empty($badge->name)) {
47 $bname = s($badge->name);
49 if (!empty($badge->image)) {
50 $imageurl = $badge->image;
52 if (isset($badge->assertion->badge->name)) {
53 $bname = s($badge->assertion->badge->name);
55 if (isset($badge->imageUrl)) {
56 $imageurl = $badge->imageUrl;
60 $name = html_writer::tag('span', $bname, array('class' => 'badge-name'));
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') . ')';
71 $download = $status = $push = '';
72 if (($userid == $USER->id) && !$profile) {
73 $params = array(
74 'download' => $badge->id,
75 'hash' => $badge->uniquehash,
76 'sesskey' => sesskey()
78 $url = new moodle_url(
79 'mybadges.php',
80 $params
82 $notexpiredbadge = (empty($badge->dateexpire) || $badge->dateexpire > time());
83 $userbackpack = badges_get_user_backpack();
84 if (!empty($CFG->badges_allowexternalbackpack) && $notexpiredbadge && $userbackpack) {
85 $assertion = new moodle_url('/badges/assertion.php', array('b' => $badge->uniquehash));
86 $icon = new pix_icon('t/backpack', get_string('addtobackpack', 'badges'));
87 if (badges_open_badges_backpack_api($userbackpack->id) == OPEN_BADGES_V2) {
88 $addurl = new moodle_url('/badges/backpack-add.php', array('hash' => $badge->uniquehash));
89 $push = $this->output->action_icon($addurl, $icon);
90 } else if (badges_open_badges_backpack_api($userbackpack->id) == OPEN_BADGES_V2P1) {
91 $addurl = new moodle_url('/badges/backpack-export.php', array('hash' => $badge->uniquehash));
92 $push = $this->output->action_icon($addurl, $icon);
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')));
106 if (!$profile) {
107 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash));
108 } else {
109 if (!$external) {
110 $url = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
111 } else {
112 $hash = hash('md5', $badge->hostedUrl);
113 $url = new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid));
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));
120 return html_writer::alist($items, array('class' => 'badges'));
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';
128 $formattributes['action'] = $this->page->url;
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()));
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'),
142 'class' => 'actionbutton btn btn-secondary')
144 $actioncell->text .= html_writer::empty_tag('input', array(
145 'type' => 'submit',
146 'name' => 'revoke',
147 'value' => get_string('revoke', 'badges') . ' ' . $this->output->rarrow(),
148 'class' => 'actionbutton btn btn-secondary')
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';
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);
161 $output .= html_writer::end_tag('form');
162 return $output;
165 // Prints a badge overview infomation.
166 public function print_badge_overview($badge, $context) {
167 $display = "";
168 $languages = get_string_manager()->get_list_of_languages();
170 // Badge details.
171 $display .= $this->heading(get_string('badgedetails', 'badges'), 3);
172 $dl = array();
173 $dl[get_string('name')] = $badge->name;
174 $dl[get_string('version', 'badges')] = $badge->version;
175 $dl[get_string('language')] = $languages[$badge->language];
176 $dl[get_string('description', 'badges')] = $badge->description;
177 $dl[get_string('createdon', 'search')] = userdate($badge->timecreated);
178 $dl[get_string('badgeimage', 'badges')] = print_badge_image($badge, $context, 'large');
179 $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname;
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;
185 $display .= $this->definition_list($dl);
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);
194 // Issuance details if any.
195 $display .= $this->heading(get_string('issuancedetails', 'badges'), 3);
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));
210 } else {
211 $display .= get_string('noexpiry', 'badges');
214 // Criteria details if any.
215 $display .= $this->heading(get_string('bcriteria', 'badges'), 3);
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'));
227 // Awards details if any.
228 if (has_capability('moodle/badges:viewawarded', $context)) {
229 $display .= $this->heading(get_string('awards', 'badges'), 3);
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');
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'));
249 $display .= self::print_badge_endorsement($badge);
250 $display .= self::print_badge_related($badge);
251 $display .= self::print_badge_alignments($badge);
253 return html_writer::div($display, null, array('id' => 'badge-overview'));
256 // Prints action icons for the badge.
257 public function print_badge_table_actions($badge, $context) {
258 $actions = "";
260 if (has_capability('moodle/badges:configuredetails', $context) && $badge->has_criteria()) {
261 // Activate/deactivate badge.
262 if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) {
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);
267 $url->param('sesskey', sesskey());
268 $return = new moodle_url(qualified_me());
269 $url->param('return', $return->out_as_local_url(false));
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'))) . " ";
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'))) . " ";
287 // Edit badge.
288 if (has_capability('moodle/badges:configuredetails', $context)) {
289 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'badge'));
290 $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
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'))) . " ";
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'))) . " ";
306 return $actions;
310 * Render an issued badge.
312 * @param \core_badges\output\issued_badge $ibadge
313 * @return string
315 protected function render_issued_badge(\core_badges\output\issued_badge $ibadge) {
316 global $USER, $CFG, $DB, $SITE;
317 $issued = $ibadge->issued;
318 $userinfo = $ibadge->recipient;
319 $badgeclass = $ibadge->badgeclass;
320 $badge = new badge($ibadge->badgeid);
321 $now = time();
322 if (isset($issued['expires'])) {
323 if (!is_numeric($issued['expires'])) {
324 $issued['expires'] = strtotime($issued['expires']);
326 $expiration = $issued['expires'];
327 } else {
328 $expiration = $now + 86400;
331 $badgeimage = is_array($badgeclass['image']) ? $badgeclass['image']['id'] : $badgeclass['image'];
332 $languages = get_string_manager()->get_list_of_languages();
334 $output = '';
335 $output .= html_writer::start_tag('div', array('id' => 'badge'));
336 $output .= html_writer::start_tag('div', array('id' => 'badge-image'));
337 $output .= html_writer::empty_tag('img', array('src' => $badgeimage, 'alt' => $badge->name, 'width' => '100'));
338 if ($expiration < $now) {
339 $output .= $this->output->pix_icon('i/expired',
340 get_string('expireddate', 'badges', userdate($issued['expires'])),
341 'moodle',
342 array('class' => 'expireimage'));
345 if ($USER->id == $userinfo->id && !empty($CFG->enablebadges)) {
346 $output .= $this->output->single_button(
347 new moodle_url('/badges/badge.php', array('hash' => $ibadge->hash, 'bake' => true)),
348 get_string('download'),
349 'POST');
350 if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $now)
351 && $userbackpack = badges_get_user_backpack($USER->id)) {
353 $assertion = null;
354 if (badges_open_badges_backpack_api($userbackpack->id) == OPEN_BADGES_V2P1) {
355 $assertion = new moodle_url('/badges/backpack-export.php', array('hash' => $ibadge->hash));
356 } else {
357 $assertion = new moodle_url('/badges/backpack-add.php', array('hash' => $ibadge->hash));
360 if (!is_null(assertion)) {
361 $attributes = ['class' => 'btn btn-secondary m-1', 'role' => 'button'];
362 $tobackpack = html_writer::link($assertion, get_string('addtobackpack', 'badges'), $attributes);
363 $output .= $tobackpack;
367 $output .= html_writer::end_tag('div');
369 $output .= html_writer::start_tag('div', array('id' => 'badge-details'));
370 // Recipient information.
371 $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3);
372 $dl = array();
373 if ($userinfo->deleted) {
374 $strdata = new stdClass();
375 $strdata->user = fullname($userinfo);
376 $strdata->site = format_string($SITE->fullname, true, array('context' => context_system::instance()));
378 $dl[get_string('name')] = get_string('error:userdeleted', 'badges', $strdata);
379 } else {
380 $dl[get_string('name')] = fullname($userinfo);
382 $output .= $this->definition_list($dl);
384 $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3);
385 $dl = array();
386 $dl[get_string('issuername', 'badges')] = $badge->issuername;
387 if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
388 $dl[get_string('contact', 'badges')] = obfuscate_mailto($badge->issuercontact);
390 $output .= $this->definition_list($dl);
392 $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3);
393 $dl = array();
394 $dl[get_string('name')] = $badge->name;
395 if (!empty($badge->version)) {
396 $dl[get_string('version', 'badges')] = $badge->version;
398 if (!empty($badge->language)) {
399 $dl[get_string('language')] = $languages[$badge->language];
401 $dl[get_string('description', 'badges')] = $badge->description;
402 if (!empty($badge->imageauthorname)) {
403 $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname;
405 if (!empty($badge->imageauthoremail)) {
406 $dl[get_string('imageauthoremail', 'badges')] =
407 html_writer::tag('a', $badge->imageauthoremail, array('href' => 'mailto:' . $badge->imageauthoremail));
409 if (!empty($badge->imageauthorurl)) {
410 $dl[get_string('imageauthorurl', 'badges')] =
411 html_writer::link($badge->imageauthorurl, $badge->imageauthorurl, array('target' => '_blank'));
413 if (!empty($badge->imagecaption)) {
414 $dl[get_string('imagecaption', 'badges')] = $badge->imagecaption;
417 if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
418 $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid));
419 $dl[get_string('course')] = $coursename;
421 $dl[get_string('bcriteria', 'badges')] = self::print_badge_criteria($badge);
422 $output .= $this->definition_list($dl);
424 $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3);
425 $dl = array();
426 if (!is_numeric($issued['issuedOn'])) {
427 $issued['issuedOn'] = strtotime($issued['issuedOn']);
429 $dl[get_string('dateawarded', 'badges')] = userdate($issued['issuedOn']);
430 if (isset($issued['expires'])) {
431 if ($issued['expires'] < $now) {
432 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']) . get_string('warnexpired', 'badges');
434 } else {
435 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']);
439 // Print evidence.
440 $agg = $badge->get_aggregation_methods();
441 $evidence = $badge->get_criteria_completions($userinfo->id);
442 $eids = array_map(function($o) {
443 return $o->critid;
444 }, $evidence);
445 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
447 $items = array();
448 foreach ($badge->criteria as $type => $c) {
449 if (in_array($c->id, $eids)) {
450 if (count($c->params) == 1) {
451 $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
452 } else {
453 $items[] = get_string('criteria_descr_' . $type , 'badges',
454 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
459 $dl[get_string('evidence', 'badges')] = get_string('completioninfo', 'badges') . html_writer::alist($items, array(), 'ul');
460 $output .= $this->definition_list($dl);
461 $endorsement = $badge->get_endorsement();
462 if (!empty($endorsement)) {
463 $output .= self::print_badge_endorsement($badge);
466 $relatedbadges = $badge->get_related_badges(true);
467 $items = array();
468 foreach ($relatedbadges as $related) {
469 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id));
470 $items[] = html_writer::link($relatedurl->out(), $related->name, array('target' => '_blank'));
472 if (!empty($items)) {
473 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
474 $output .= html_writer::alist($items, array(), 'ul');
477 $alignments = $badge->get_alignments();
478 if (!empty($alignments)) {
479 $output .= $this->heading(get_string('alignment', 'badges'), 3);
480 $items = array();
481 foreach ($alignments as $alignment) {
482 $items[] = html_writer::link($alignment->targeturl, $alignment->targetname, array('target' => '_blank'));
484 $output .= html_writer::alist($items, array(), 'ul');
486 $output .= html_writer::end_tag('div');
488 return $output;
492 * Render an external badge.
494 * @param \core_badges\output\external_badge $ibadge
495 * @return string
497 protected function render_external_badge(\core_badges\output\external_badge $ibadge) {
498 $issued = $ibadge->issued;
499 $assertion = $issued->assertion;
500 $issuer = $assertion->badge->issuer;
501 $userinfo = $ibadge->recipient;
502 $table = new html_table();
503 $today = strtotime(date('Y-m-d'));
505 $output = '';
506 $output .= html_writer::start_tag('div', array('id' => 'badge'));
507 $output .= html_writer::start_tag('div', array('id' => 'badge-image'));
508 if (isset($issued->imageUrl)) {
509 $issued->image = $issued->imageUrl;
511 $output .= html_writer::empty_tag('img', array('src' => $issued->image, 'width' => '100'));
512 if (isset($assertion->expires)) {
513 $expiration = is_numeric($assertion->expires) ? $assertion->expires : strtotime($assertion->expires);
514 if ($expiration < $today) {
515 $output .= $this->output->pix_icon('i/expired',
516 get_string('expireddate', 'badges', userdate($expiration)),
517 'moodle',
518 array('class' => 'expireimage'));
521 $output .= html_writer::end_tag('div');
523 $output .= html_writer::start_tag('div', array('id' => 'badge-details'));
525 // Recipient information.
526 $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3);
527 $dl = array();
528 // Technically, we should alway have a user at this point, but added an extra check just in case.
529 if ($userinfo) {
530 if (!$ibadge->valid) {
531 $notify = $this->output->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice');
532 $dl[get_string('name')] = fullname($userinfo) . $notify;
533 } else {
534 $dl[get_string('name')] = fullname($userinfo);
536 } else {
537 $notify = $this->output->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice');
538 $dl[get_string('name')] = $notify;
540 $output .= $this->definition_list($dl);
542 $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3);
543 $dl = array();
544 $dl[get_string('issuername', 'badges')] = s($issuer->name);
545 if (isset($issuer->origin)) {
546 $dl[get_string('issuerurl', 'badges')] = html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin));
549 if (isset($issuer->contact)) {
550 $dl[get_string('contact', 'badges')] = obfuscate_mailto($issuer->contact);
552 $output .= $this->definition_list($dl);
554 $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3);
555 $dl = array();
556 $dl[get_string('name')] = s($assertion->badge->name);
557 $dl[get_string('description', 'badges')] = s($assertion->badge->description);
558 if (isset($assertion->badge->criteria)) {
559 $dl[get_string('bcriteria', 'badges')] = html_writer::tag(
560 'a',
561 s($assertion->badge->criteria),
562 array('href' => $assertion->badge->criteria)
565 $output .= $this->definition_list($dl);
567 $dl = array();
568 if (isset($assertion->issued_on)) {
569 $issuedate = is_numeric($assertion->issued_on) ? $assertion->issued_on : strtotime($assertion->issued_on);
570 $dl[get_string('dateawarded', 'badges')] = userdate($issuedate);
572 if (isset($assertion->expires)) {
573 if ($expiration < $today) {
574 $dl[get_string('expirydate', 'badges')] = userdate($expiration) . get_string('warnexpired', 'badges');
575 } else {
576 $dl[get_string('expirydate', 'badges')] = userdate($expiration);
579 if (isset($assertion->evidence)) {
580 $dl[get_string('evidence', 'badges')] = html_writer::tag(
581 'a',
582 s($assertion->evidence),
583 array('href' => $assertion->evidence)
586 if (!empty($dl)) {
587 $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3);
588 $output .= $this->definition_list($dl);
590 $output .= html_writer::end_tag('div');
592 return $output;
596 * Render a collection of user badges.
598 * @param \core_badges\output\badge_user_collection $badges
599 * @return string
601 protected function render_badge_user_collection(\core_badges\output\badge_user_collection $badges) {
602 global $CFG, $USER, $SITE;
603 $backpack = $badges->backpack;
604 $mybackpack = new moodle_url('/badges/mybackpack.php');
606 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
607 $htmlpagingbar = $this->render($paging);
609 // Set backpack connection string.
610 $backpackconnect = '';
611 if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) {
612 $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
614 // Search box.
615 $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
617 // Download all button.
618 $actionhtml = $this->output->single_button(
619 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
620 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
621 $downloadall = $this->output->box('', 'col-md-3');
622 $downloadall .= $this->output->box($actionhtml, 'col-md-9');
623 $downloadall = $this->output->box($downloadall, 'row ml-5');
625 // Local badges.
626 $localhtml = html_writer::start_tag('div', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
627 $sitename = format_string($SITE->fullname, true, array('context' => context_system::instance()));
628 $heading = get_string('localbadges', 'badges', $sitename);
629 $localhtml .= $this->output->heading_with_help($heading, 'localbadgesh', 'badges');
630 if ($badges->badges) {
631 $countmessage = $this->output->box(get_string('badgesearned', 'badges', $badges->totalcount));
633 $htmllist = $this->print_badges_list($badges->badges, $USER->id);
634 $localhtml .= $backpackconnect . $countmessage . $searchform;
635 $localhtml .= $htmlpagingbar . $htmllist . $htmlpagingbar . $downloadall;
636 } else {
637 $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
639 $localhtml .= html_writer::end_tag('div');
641 // External badges.
642 $externalhtml = "";
643 if (!empty($CFG->badges_allowexternalbackpack)) {
644 $externalhtml .= html_writer::start_tag('div', array('class' => 'generalbox'));
645 $externalhtml .= $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges');
646 if (!is_null($backpack)) {
647 if ($backpack->totalcollections == 0) {
648 $externalhtml .= get_string('nobackpackcollectionssummary', 'badges', $backpack);
649 } else {
650 if ($backpack->totalbadges == 0) {
651 $externalhtml .= get_string('nobackpackbadgessummary', 'badges', $backpack);
652 } else {
653 $externalhtml .= get_string('backpackbadgessummary', 'badges', $backpack);
654 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
657 } else {
658 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
661 $externalhtml .= html_writer::end_tag('div');
662 $attr = ['class' => 'btn btn-secondary'];
663 $label = get_string('backpackbadgessettings', 'badges');
664 $backpacksettings = html_writer::link(new moodle_url('/badges/mybackpack.php'), $label, $attr);
665 $actionshtml = $this->output->box('', 'col-md-3');
666 $actionshtml .= $this->output->box($backpacksettings, 'col-md-9');
667 $actionshtml = $this->output->box($actionshtml, 'row ml-5');
668 $externalhtml .= $actionshtml;
671 return $localhtml . $externalhtml;
675 * Render a collection of badges.
677 * @param \core_badges\output\badge_collection $badges
678 * @return string
680 protected function render_badge_collection(\core_badges\output\badge_collection $badges) {
681 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
682 $htmlpagingbar = $this->render($paging);
683 $table = new html_table();
684 $table->attributes['class'] = 'table table-bordered table-striped';
686 $sortbyname = $this->helper_sortable_heading(get_string('name'),
687 'name', $badges->sort, $badges->dir);
688 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
689 'dateissued', $badges->sort, $badges->dir);
690 $table->head = array(
691 get_string('badgeimage', 'badges'),
692 $sortbyname,
693 get_string('description', 'badges'),
694 get_string('bcriteria', 'badges'),
695 $sortbyawarded
697 $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
699 foreach ($badges->badges as $badge) {
700 $badgeimage = print_badge_image($badge, $this->page->context, 'large');
701 $name = $badge->name;
702 $description = $badge->description;
703 $criteria = self::print_badge_criteria($badge);
704 if ($badge->dateissued) {
705 $icon = new pix_icon('i/valid',
706 get_string('dateearned', 'badges',
707 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
708 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
709 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
710 } else {
711 $awarded = "";
713 $row = array($badgeimage, $name, $description, $criteria, $awarded);
714 $table->data[] = $row;
717 $htmltable = html_writer::table($table);
719 return $htmlpagingbar . $htmltable . $htmlpagingbar;
723 * Render a table of badges.
725 * @param \core_badges\output\badge_management $badges
726 * @return string
728 protected function render_badge_management(\core_badges\output\badge_management $badges) {
729 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
731 // New badge button.
732 $htmlnew = '';
733 if (has_capability('moodle/badges:createbadge', $this->page->context)) {
734 $n['type'] = $this->page->url->get_param('type');
735 $n['id'] = $this->page->url->get_param('id');
736 $btn = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
737 $htmlnew = $this->output->box($btn);
740 $htmlpagingbar = $this->render($paging);
741 $table = new html_table();
742 $table->attributes['class'] = 'table table-bordered table-striped';
744 $sortbyname = $this->helper_sortable_heading(get_string('name'),
745 'name', $badges->sort, $badges->dir);
746 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
747 'status', $badges->sort, $badges->dir);
748 $table->head = array(
749 $sortbyname,
750 $sortbystatus,
751 get_string('bcriteria', 'badges'),
752 get_string('awards', 'badges'),
753 get_string('actions')
755 $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
757 foreach ($badges->badges as $b) {
758 $style = !$b->is_active() ? array('class' => 'dimmed') : array();
759 $forlink = print_badge_image($b, $this->page->context) . ' ' .
760 html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
761 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
762 $status = $b->statstring;
763 $criteria = self::print_badge_criteria($b, 'short');
765 if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
766 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
767 } else {
768 $awards = $b->awards;
771 $actions = self::print_badge_table_actions($b, $this->page->context);
773 $row = array($name, $status, $criteria, $awards, $actions);
774 $table->data[] = $row;
776 $htmltable = html_writer::table($table);
778 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
782 * Prints tabs for badge editing.
784 * @param integer $badgeid The badgeid to edit.
785 * @param context $context The current context.
786 * @param string $current The currently selected tab.
787 * @return string
789 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
790 global $DB;
792 $badge = new badge($badgeid);
793 $row = array();
795 $row[] = new tabobject('overview',
796 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
797 get_string('boverview', 'badges')
800 if (has_capability('moodle/badges:configuredetails', $context)) {
801 $row[] = new tabobject('badge',
802 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'badge')),
803 get_string('bdetails', 'badges')
807 if (has_capability('moodle/badges:configurecriteria', $context)) {
808 $row[] = new tabobject('criteria',
809 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
810 get_string('bcriteria', 'badges')
814 if (has_capability('moodle/badges:configuremessages', $context)) {
815 $row[] = new tabobject('message',
816 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
817 get_string('bmessage', 'badges')
821 if (has_capability('moodle/badges:viewawarded', $context)) {
822 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
823 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
824 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
825 $row[] = new tabobject('awards',
826 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
827 get_string('bawards', 'badges', $awarded)
831 if (has_capability('moodle/badges:configuredetails', $context)) {
832 $row[] = new tabobject('bendorsement',
833 new moodle_url('/badges/endorsement.php', array('id' => $badgeid)),
834 get_string('bendorsement', 'badges')
838 if (has_capability('moodle/badges:configuredetails', $context)) {
839 $sql = "SELECT COUNT(br.badgeid)
840 FROM {badge_related} br
841 WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
842 $related = $DB->count_records_sql($sql, ['badgeid' => $badgeid, 'badgeid2' => $badgeid]);
843 $row[] = new tabobject('brelated',
844 new moodle_url('/badges/related.php', array('id' => $badgeid)),
845 get_string('brelated', 'badges', $related)
849 if (has_capability('moodle/badges:configuredetails', $context)) {
850 $alignments = $DB->count_records_sql("SELECT COUNT(bc.id)
851 FROM {badge_alignment} bc WHERE bc.badgeid = :badgeid", array('badgeid' => $badgeid));
852 $row[] = new tabobject('alignment',
853 new moodle_url('/badges/alignment.php', array('id' => $badgeid)),
854 get_string('balignment', 'badges', $alignments)
858 echo $this->tabtree($row, $current);
862 * Prints badge status box.
864 * @param badge $badge
865 * @return Either the status box html as a string or null
867 public function print_badge_status_box(badge $badge) {
868 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
870 if (!$badge->has_criteria()) {
871 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
872 $status = get_string('nocriteria', 'badges');
873 if ($this->page->url != $criteriaurl) {
874 $action = $this->output->single_button(
875 $criteriaurl,
876 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
877 } else {
878 $action = '';
881 $message = $status . $action;
882 } else {
883 $status = get_string('statusmessage_' . $badge->status, 'badges');
884 if ($badge->is_active()) {
885 $action = $this->output->single_button(new moodle_url('/badges/action.php',
886 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
887 'return' => $this->page->url->out_as_local_url(false))),
888 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
889 } else {
890 $action = $this->output->single_button(new moodle_url('/badges/action.php',
891 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
892 'return' => $this->page->url->out_as_local_url(false))),
893 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
896 $message = $status . $this->output->help_icon('status', 'badges') . $action;
900 $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
901 return $this->output->box($message, $style);
904 return null;
908 * Returns information about badge criteria in a list form.
910 * @param badge $badge Badge objects
911 * @param string $short Indicates whether to print full info about this badge
912 * @return string $output HTML string to output
914 public function print_badge_criteria(badge $badge, $short = '') {
915 $agg = $badge->get_aggregation_methods();
916 if (empty($badge->criteria)) {
917 return get_string('nocriteria', 'badges');
920 $overalldescr = '';
921 $overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL];
922 if (!$short && !empty($overall->description)) {
923 $overalldescr = $this->output->box(
924 format_text($overall->description, $overall->descriptionformat, array('context' => $badge->get_context())),
925 'criteria-description'
929 // Get the condition string.
930 if (count($badge->criteria) == 2) {
931 $condition = '';
932 if (!$short) {
933 $condition = get_string('criteria_descr', 'badges');
935 } else {
936 $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
937 core_text::strtoupper($agg[$badge->get_aggregation_method()]));
940 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
942 $items = array();
943 // If only one criterion left, make sure its description goe to the top.
944 if (count($badge->criteria) == 1) {
945 $c = reset($badge->criteria);
946 if (!$short && !empty($c->description)) {
947 $overalldescr = $this->output->box(
948 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
949 'criteria-description'
952 if (count($c->params) == 1) {
953 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype , 'badges') .
954 $c->get_details($short);
955 } else {
956 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype, 'badges',
957 core_text::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype)])) .
958 $c->get_details($short);
960 } else {
961 foreach ($badge->criteria as $type => $c) {
962 $criteriadescr = '';
963 if (!$short && !empty($c->description)) {
964 $criteriadescr = $this->output->box(
965 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
966 'criteria-description'
969 if (count($c->params) == 1) {
970 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
971 $c->get_details($short) . $criteriadescr;
972 } else {
973 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
974 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) .
975 $c->get_details($short) .
976 $criteriadescr;
981 return $overalldescr . $condition . html_writer::alist($items, array(), 'ul');;
985 * Prints criteria actions for badge editing.
987 * @param badge $badge
988 * @return string
990 public function print_criteria_actions(badge $badge) {
991 $output = '';
992 if (!$badge->is_active() && !$badge->is_locked()) {
993 $accepted = $badge->get_accepted_criteria();
994 $potential = array_diff($accepted, array_keys($badge->criteria));
996 if (!empty($potential)) {
997 foreach ($potential as $p) {
998 if ($p != 0) {
999 $select[$p] = get_string('criteria_' . $p, 'badges');
1002 $output .= $this->output->single_select(
1003 new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id, 'add' => true)),
1004 'type',
1005 $select,
1007 array('' => 'choosedots'),
1008 null,
1009 array('label' => get_string('addbadgecriteria', 'badges'))
1011 } else {
1012 $output .= $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
1016 return $output;
1020 * Renders a table with users who have earned the badge.
1021 * Based on stamps collection plugin.
1023 * @param \core_badges\output\badge_recipients $recipients
1024 * @return string
1026 protected function render_badge_recipients(\core_badges\output\badge_recipients $recipients) {
1027 $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
1028 $htmlpagingbar = $this->render($paging);
1029 $table = new html_table();
1030 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1032 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
1033 'firstname', $recipients->sort, $recipients->dir);
1034 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
1035 'lastname', $recipients->sort, $recipients->dir);
1036 if ($this->helper_fullname_format() == 'lf') {
1037 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
1038 } else {
1039 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
1042 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
1043 'dateissued', $recipients->sort, $recipients->dir);
1045 $table->head = array($sortbyname, $sortbydate, '');
1047 foreach ($recipients->userids as $holder) {
1048 $fullname = fullname($holder);
1049 $fullname = html_writer::link(
1050 new moodle_url('/user/profile.php', array('id' => $holder->userid)),
1051 $fullname
1053 $awarded = userdate($holder->dateissued);
1054 $badgeurl = html_writer::link(
1055 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
1056 get_string('viewbadge', 'badges')
1059 $row = array($fullname, $awarded, $badgeurl);
1060 $table->data[] = $row;
1063 $htmltable = html_writer::table($table);
1065 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1068 ////////////////////////////////////////////////////////////////////////////
1069 // Helper methods
1070 // Reused from stamps collection plugin
1071 ////////////////////////////////////////////////////////////////////////////
1074 * Renders a text with icons to sort by the given column
1076 * This is intended for table headings.
1078 * @param string $text The heading text
1079 * @param string $sortid The column id used for sorting
1080 * @param string $sortby Currently sorted by (column id)
1081 * @param string $sorthow Currently sorted how (ASC|DESC)
1083 * @return string
1085 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
1086 $out = html_writer::tag('span', $text, array('class' => 'text'));
1088 if (!is_null($sortid)) {
1089 if ($sortby !== $sortid || $sorthow !== 'ASC') {
1090 $url = new moodle_url($this->page->url);
1091 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
1092 $out .= $this->output->action_icon($url,
1093 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
1095 if ($sortby !== $sortid || $sorthow !== 'DESC') {
1096 $url = new moodle_url($this->page->url);
1097 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
1098 $out .= $this->output->action_icon($url,
1099 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
1102 return $out;
1105 * Tries to guess the fullname format set at the site
1107 * @return string fl|lf
1109 protected function helper_fullname_format() {
1110 $fake = new stdClass();
1111 $fake->lastname = 'LLLL';
1112 $fake->firstname = 'FFFF';
1113 $fullname = get_string('fullnamedisplay', '', $fake);
1114 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
1115 return 'lf';
1116 } else {
1117 return 'fl';
1121 * Renders a search form
1123 * @param string $search Search string
1124 * @return string HTML
1126 protected function helper_search_form($search) {
1127 global $CFG;
1128 require_once($CFG->libdir . '/formslib.php');
1130 $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
1132 $mform->addElement('hidden', 'sesskey', sesskey());
1134 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
1135 $mform->setDefault('search', $search);
1136 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
1137 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
1138 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
1140 ob_start();
1141 $mform->display();
1142 $out = ob_get_clean();
1144 return $out;
1148 * Renders a definition list
1150 * @param array $items the list of items to define
1151 * @param array
1153 protected function definition_list(array $items, array $attributes = array()) {
1154 $output = html_writer::start_tag('dl', $attributes);
1155 foreach ($items as $label => $value) {
1156 $output .= html_writer::tag('dt', $label);
1157 $output .= html_writer::tag('dd', $value);
1159 $output .= html_writer::end_tag('dl');
1160 return $output;
1164 * Outputs list en badges.
1166 * @param badge $badge Badge object.
1167 * @return string $output content endorsement to output.
1169 protected function print_badge_endorsement(badge $badge) {
1170 $output = '';
1171 $endorsement = $badge->get_endorsement();
1172 $dl = array();
1173 $output .= $this->heading(get_string('endorsement', 'badges'), 3);
1174 if (!empty($endorsement)) {
1175 $dl[get_string('issuername', 'badges')] = $endorsement->issuername;
1176 $dl[get_string('issueremail', 'badges')] =
1177 html_writer::tag('a', $endorsement->issueremail, array('href' => 'mailto:' . $endorsement->issueremail));
1178 $dl[get_string('issuerurl', 'badges')] = html_writer::link($endorsement->issuerurl, $endorsement->issuerurl,
1179 array('target' => '_blank'));
1180 $dl[get_string('dateawarded', 'badges')] = userdate($endorsement->dateissued);
1181 $dl[get_string('claimid', 'badges')] = html_writer::link($endorsement->claimid, $endorsement->claimid,
1182 array('target' => '_blank'));
1183 $dl[get_string('claimcomment', 'badges')] = $endorsement->claimcomment;
1184 $output .= $this->definition_list($dl);
1185 } else {
1186 $output .= get_string('noendorsement', 'badges');
1188 return $output;
1192 * Print list badges related.
1194 * @param badge $badge Badge objects.
1195 * @return string $output List related badges to output.
1197 protected function print_badge_related(badge $badge) {
1198 $output = '';
1199 $relatedbadges = $badge->get_related_badges();
1200 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
1201 if (!empty($relatedbadges)) {
1202 $items = array();
1203 foreach ($relatedbadges as $related) {
1204 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id));
1205 $items[] = html_writer::link($relatedurl->out(), $related->name, array('target' => '_blank'));
1207 $output .= html_writer::alist($items, array(), 'ul');
1208 } else {
1209 $output .= get_string('norelated', 'badges');
1211 return $output;
1215 * Print list badge alignments.
1217 * @param badge $badge Badge objects.
1218 * @return string $output List alignments to output.
1220 protected function print_badge_alignments(badge $badge) {
1221 $output = '';
1222 $output .= $this->heading(get_string('alignment', 'badges'), 3);
1223 $alignments = $badge->get_alignments();
1224 if (!empty($alignments)) {
1225 $items = array();
1226 foreach ($alignments as $alignment) {
1227 $urlaligment = new moodle_url('alignment.php',
1228 array('id' => $badge->id, 'alignmentid' => $alignment->id)
1230 $items[] = html_writer::link($urlaligment, $alignment->targetname, array('target' => '_blank'));
1232 $output .= html_writer::alist($items, array(), 'ul');
1233 } else {
1234 $output .= get_string('noalignment', 'badges');
1236 return $output;
1240 * Renders a table for related badges.
1242 * @param \core_badges\output\badge_related $related list related badges.
1243 * @return string list related badges to output.
1245 protected function render_badge_related(\core_badges\output\badge_related $related) {
1246 $currentbadge = new badge($related->currentbadgeid);
1247 $languages = get_string_manager()->get_list_of_languages();
1248 $paging = new paging_bar($related->totalcount, $related->page, $related->perpage, $this->page->url, 'page');
1249 $htmlpagingbar = $this->render($paging);
1250 $table = new html_table();
1251 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1252 $table->head = array(
1253 get_string('name'),
1254 get_string('version', 'badges'),
1255 get_string('language', 'badges'),
1256 get_string('type', 'badges')
1258 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1259 array_push($table->head, '');
1262 foreach ($related->badges as $badge) {
1263 $badgeobject = new badge($badge->id);
1264 $style = array('title' => $badgeobject->name);
1265 if (!$badgeobject->is_active()) {
1266 $style['class'] = 'dimmed';
1268 $context = ($badgeobject->type == BADGE_TYPE_SITE) ?
1269 context_system::instance() : context_course::instance($badgeobject->courseid);
1270 $forlink = print_badge_image($badgeobject, $context) . ' ' .
1271 html_writer::start_tag('span') . $badgeobject->name . html_writer::end_tag('span');
1272 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $badgeobject->id)), $forlink, $style);
1274 $row = array(
1275 $name,
1276 $badge->version,
1277 $badge->language ? $languages[$badge->language] : '',
1278 $badge->type == BADGE_TYPE_COURSE ? get_string('badgesview', 'badges') : get_string('sitebadges', 'badges')
1280 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1281 $action = $this->output->action_icon(
1282 new moodle_url('related_action.php',
1283 array(
1284 'badgeid' => $related->currentbadgeid,
1285 'relatedid' => $badge->id,
1286 'action' => 'remove'
1288 ), new pix_icon('t/delete', get_string('delete')));
1289 $actions = html_writer::tag('div', $action, array('class' => 'badge-actions'));
1290 array_push($row, $actions);
1292 $table->data[] = $row;
1294 $htmltable = html_writer::table($table);
1296 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1300 * Renders a table with alignment.
1302 * @param core_badges\output\badge_alignments $alignments List alignments.
1303 * @return string List alignment to output.
1305 protected function render_badge_alignments(\core_badges\output\badge_alignments $alignments) {
1306 $currentbadge = new badge($alignments->currentbadgeid);
1307 $paging = new paging_bar($alignments->totalcount, $alignments->page, $alignments->perpage, $this->page->url, 'page');
1308 $htmlpagingbar = $this->render($paging);
1309 $table = new html_table();
1310 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1311 $table->head = array('Name', 'URL', '');
1313 foreach ($alignments->alignments as $item) {
1314 $urlaligment = new moodle_url('alignment.php',
1315 array(
1316 'id' => $currentbadge->id,
1317 'alignmentid' => $item->id,
1320 $row = array(
1321 html_writer::link($urlaligment, $item->targetname),
1322 html_writer::link($item->targeturl, $item->targeturl, array('target' => '_blank'))
1324 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1325 $delete = $this->output->action_icon(
1326 new moodle_url('alignment_action.php',
1327 array(
1328 'id' => $currentbadge->id,
1329 'alignmentid' => $item->id,
1330 'action' => 'remove'
1332 ), new pix_icon('t/delete', get_string('delete')));
1333 $edit = $this->output->action_icon(
1334 new moodle_url('alignment.php',
1335 array(
1336 'id' => $currentbadge->id,
1337 'alignmentid' => $item->id,
1338 'action' => 'edit'
1340 ), new pix_icon('t/edit', get_string('edit')));
1341 $actions = html_writer::tag('div', $edit . $delete, array('class' => 'badge-actions'));
1342 array_push($row, $actions);
1344 $table->data[] = $row;
1346 $htmltable = html_writer::table($table);
1348 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1352 * Defer to template.
1354 * @param \core_badges\output\external_backpacks_page $page
1355 * @return bool|string
1357 public function render_external_backpacks_page(\core_badges\output\external_backpacks_page $page) {
1358 $data = $page->export_for_template($this);
1359 return parent::render_from_template('core_badges/external_backpacks_page', $data);
1363 * Get the result of a backpack validation with its settings. It returns:
1364 * - A informative message if the backpack version is different from OBv2.
1365 * - A warning with the error if it's not possible to connect to this backpack.
1366 * - A successful message if the connection has worked.
1368 * @param int $backpackid The backpack identifier.
1369 * @return string A message with the validation result.
1371 public function render_test_backpack_result(int $backpackid): string {
1372 // Get the backpack.
1373 $backpack = badges_get_site_backpack($backpackid);
1375 // Add the header to the result.
1376 $result = $this->heading(get_string('testbackpack', 'badges', $backpack->backpackweburl));
1378 if ($backpack->apiversion != OPEN_BADGES_V2) {
1379 // Only OBv2 supports this validation.
1380 $result .= get_string('backpackconnectionnottested', 'badges');
1381 } else {
1382 $message = badges_verify_backpack($backpackid);
1383 if (empty($message)) {
1384 $result .= get_string('backpackconnectionok', 'badges');
1385 } else {
1386 $result .= $message;
1390 return $result;