Merge branch 'MDL-74802-400-2' of https://github.com/junpataleta/moodle into MOODLE_4...
[moodle.git] / badges / renderer.php
blob57cc70a7312e3b55b2c7dc10dea1149269066d12
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 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/badgeslib.php');
30 require_once($CFG->libdir . '/tablelib.php');
32 /**
33 * Standard HTML output renderer for badges
35 class core_badges_renderer extends plugin_renderer_base {
37 // Outputs badges list.
38 public function print_badges_list($badges, $userid, $profile = false, $external = false) {
39 global $USER, $CFG;
40 foreach ($badges as $badge) {
41 if (!$external) {
42 $context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid);
43 $bname = $badge->name;
44 $imageurl = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', 'f3', false);
45 } else {
46 $bname = '';
47 $imageurl = '';
48 if (!empty($badge->name)) {
49 $bname = s($badge->name);
51 if (!empty($badge->image)) {
52 if (is_object($badge->image)) {
53 if (!empty($badge->image->caption)) {
54 $badge->imagecaption = $badge->image->caption;
56 $imageurl = $badge->image->id;
57 } else {
58 $imageurl = $badge->image;
61 if (isset($badge->assertion->badge->name)) {
62 $bname = s($badge->assertion->badge->name);
64 if (isset($badge->imageUrl)) {
65 $imageurl = $badge->imageUrl;
69 $name = html_writer::tag('span', $bname, array('class' => 'badge-name'));
71 $imagecaption = $badge->imagecaption ?? '';
72 $image = html_writer::empty_tag('img', ['src' => $imageurl, 'class' => 'badge-image', 'alt' => $imagecaption]);
73 if (!empty($badge->dateexpire) && $badge->dateexpire < time()) {
74 $image .= $this->output->pix_icon('i/expired',
75 get_string('expireddate', 'badges', userdate($badge->dateexpire)),
76 'moodle',
77 array('class' => 'expireimage'));
78 $name .= '(' . get_string('expired', 'badges') . ')';
81 $download = $status = $push = '';
82 if (($userid == $USER->id) && !$profile) {
83 $params = array(
84 'download' => $badge->id,
85 'hash' => $badge->uniquehash,
86 'sesskey' => sesskey()
88 $url = new moodle_url(
89 'mybadges.php',
90 $params
92 $notexpiredbadge = (empty($badge->dateexpire) || $badge->dateexpire > time());
93 $userbackpack = badges_get_user_backpack();
94 if (!empty($CFG->badges_allowexternalbackpack) && $notexpiredbadge && $userbackpack) {
95 $assertion = new moodle_url('/badges/assertion.php', array('b' => $badge->uniquehash));
96 $icon = new pix_icon('t/backpack', get_string('addtobackpack', 'badges'));
97 if (badges_open_badges_backpack_api($userbackpack->id) == OPEN_BADGES_V2) {
98 $addurl = new moodle_url('/badges/backpack-add.php', array('hash' => $badge->uniquehash));
99 $push = $this->output->action_icon($addurl, $icon);
100 } else if (badges_open_badges_backpack_api($userbackpack->id) == OPEN_BADGES_V2P1) {
101 $addurl = new moodle_url('/badges/backpack-export.php', array('hash' => $badge->uniquehash));
102 $push = $this->output->action_icon($addurl, $icon);
106 $download = $this->output->action_icon($url, new pix_icon('t/download', get_string('download')));
107 if ($badge->visible) {
108 $url = new moodle_url('mybadges.php', array('hide' => $badge->issuedid, 'sesskey' => sesskey()));
109 $status = $this->output->action_icon($url, new pix_icon('t/hide', get_string('makeprivate', 'badges')));
110 } else {
111 $url = new moodle_url('mybadges.php', array('show' => $badge->issuedid, 'sesskey' => sesskey()));
112 $status = $this->output->action_icon($url, new pix_icon('t/show', get_string('makepublic', 'badges')));
116 if (!$profile) {
117 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash));
118 } else {
119 if (!$external) {
120 $url = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
121 } else {
122 $hash = hash('md5', $badge->hostedUrl);
123 $url = new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid));
126 $actions = html_writer::tag('div', $push . $download . $status, array('class' => 'badge-actions'));
127 $items[] = html_writer::link($url, $image . $actions . $name, array('title' => $bname));
130 return html_writer::alist($items, array('class' => 'badges'));
133 // Recipients selection form.
134 public function recipients_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
135 $output = '';
136 $formattributes = array();
137 $formattributes['id'] = 'recipientform';
138 $formattributes['action'] = $this->page->url;
139 $formattributes['method'] = 'post';
140 $output .= html_writer::start_tag('form', $formattributes);
141 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
143 $existingcell = new html_table_cell();
144 $existingcell->text = $existinguc->display(true);
145 $existingcell->attributes['class'] = 'existing';
146 $actioncell = new html_table_cell();
147 $actioncell->text = html_writer::start_tag('div', array());
148 $actioncell->text .= html_writer::empty_tag('input', array(
149 'type' => 'submit',
150 'name' => 'award',
151 'value' => $this->output->larrow() . ' ' . get_string('award', 'badges'),
152 'class' => 'actionbutton btn btn-secondary')
154 $actioncell->text .= html_writer::empty_tag('input', array(
155 'type' => 'submit',
156 'name' => 'revoke',
157 'value' => get_string('revoke', 'badges') . ' ' . $this->output->rarrow(),
158 'class' => 'actionbutton btn btn-secondary')
160 $actioncell->text .= html_writer::end_tag('div', array());
161 $actioncell->attributes['class'] = 'actions';
162 $potentialcell = new html_table_cell();
163 $potentialcell->text = $potentialuc->display(true);
164 $potentialcell->attributes['class'] = 'potential';
166 $table = new html_table();
167 $table->attributes['class'] = 'recipienttable boxaligncenter';
168 $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
169 $output .= html_writer::table($table);
171 $output .= html_writer::end_tag('form');
172 return $output;
175 // Prints a badge overview infomation.
176 public function print_badge_overview($badge, $context) {
177 $display = "";
178 $languages = get_string_manager()->get_list_of_languages();
180 // Badge details.
181 $display .= $this->heading(get_string('badgedetails', 'badges'), 3);
182 $dl = array();
183 $dl[get_string('name')] = $badge->name;
184 $dl[get_string('version', 'badges')] = $badge->version;
185 $dl[get_string('language')] = $languages[$badge->language];
186 $dl[get_string('description', 'badges')] = $badge->description;
187 $dl[get_string('createdon', 'search')] = userdate($badge->timecreated);
188 $dl[get_string('badgeimage', 'badges')] = print_badge_image($badge, $context, 'large');
189 $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname;
190 $dl[get_string('imageauthoremail', 'badges')] =
191 html_writer::tag('a', $badge->imageauthoremail, array('href' => 'mailto:' . $badge->imageauthoremail));
192 $dl[get_string('imageauthorurl', 'badges')] =
193 html_writer::link($badge->imageauthorurl, $badge->imageauthorurl, array('target' => '_blank'));
194 $dl[get_string('imagecaption', 'badges')] = $badge->imagecaption;
195 $display .= $this->definition_list($dl);
197 // Issuer details.
198 $display .= $this->heading(get_string('issuerdetails', 'badges'), 3);
199 $dl = array();
200 $dl[get_string('issuername', 'badges')] = $badge->issuername;
201 $dl[get_string('contact', 'badges')] = html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact));
202 $display .= $this->definition_list($dl);
204 // Issuance details if any.
205 $display .= $this->heading(get_string('issuancedetails', 'badges'), 3);
206 if ($badge->can_expire()) {
207 if ($badge->expiredate) {
208 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate));
209 } else if ($badge->expireperiod) {
210 if ($badge->expireperiod < 60) {
211 $display .= get_string('expireperiods', 'badges', round($badge->expireperiod, 2));
212 } else if ($badge->expireperiod < 60 * 60) {
213 $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod / 60, 2));
214 } else if ($badge->expireperiod < 60 * 60 * 24) {
215 $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod / 60 / 60, 2));
216 } else {
217 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2));
220 } else {
221 $display .= get_string('noexpiry', 'badges');
224 // Criteria details if any.
225 $display .= $this->heading(get_string('bcriteria', 'badges'), 3);
226 if ($badge->has_criteria()) {
227 $display .= self::print_badge_criteria($badge);
228 } else {
229 $display .= get_string('nocriteria', 'badges');
230 if (has_capability('moodle/badges:configurecriteria', $context)) {
231 $display .= $this->output->single_button(
232 new moodle_url('/badges/criteria.php', array('id' => $badge->id)),
233 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
237 // Awards details if any.
238 if (has_capability('moodle/badges:viewawarded', $context)) {
239 $display .= $this->heading(get_string('awards', 'badges'), 3);
240 if ($badge->has_awards()) {
241 $url = new moodle_url('/badges/recipients.php', array('id' => $badge->id));
242 $a = new stdClass();
243 $a->link = $url->out();
244 $a->count = count($badge->get_awards());
245 $display .= get_string('numawards', 'badges', $a);
246 } else {
247 $display .= get_string('noawards', 'badges');
250 if (has_capability('moodle/badges:awardbadge', $context) &&
251 $badge->has_manual_award_criteria() &&
252 $badge->is_active()) {
253 $display .= $this->output->single_button(
254 new moodle_url('/badges/award.php', array('id' => $badge->id)),
255 get_string('award', 'badges'), 'POST', array('class' => 'activatebadge'));
259 $display .= self::print_badge_endorsement($badge);
260 $display .= self::print_badge_related($badge);
261 $display .= self::print_badge_alignments($badge);
263 return html_writer::div($display, null, array('id' => 'badge-overview'));
266 // Prints action icons for the badge.
267 public function print_badge_table_actions($badge, $context) {
268 $actions = "";
270 if (has_capability('moodle/badges:configuredetails', $context) && $badge->has_criteria()) {
271 // Activate/deactivate badge.
272 if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) {
273 // "Activate" will go to another page and ask for confirmation.
274 $url = new moodle_url('/badges/action.php');
275 $url->param('id', $badge->id);
276 $url->param('activate', true);
277 $url->param('sesskey', sesskey());
278 $return = new moodle_url(qualified_me());
279 $url->param('return', $return->out_as_local_url(false));
280 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
281 } else {
282 $url = new moodle_url(qualified_me());
283 $url->param('lock', $badge->id);
284 $url->param('sesskey', sesskey());
285 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
289 // Award badge manually.
290 if ($badge->has_manual_award_criteria() &&
291 has_capability('moodle/badges:awardbadge', $context) &&
292 $badge->is_active()) {
293 $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
294 $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
297 // Edit badge.
298 if (has_capability('moodle/badges:configuredetails', $context)) {
299 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'badge'));
300 $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
303 // Duplicate badge.
304 if (has_capability('moodle/badges:createbadge', $context)) {
305 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey()));
306 $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
309 // Delete badge.
310 if (has_capability('moodle/badges:deletebadge', $context)) {
311 $url = new moodle_url(qualified_me());
312 $url->param('delete', $badge->id);
313 $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
316 return $actions;
320 * Render an issued badge.
322 * @param \core_badges\output\issued_badge $ibadge
323 * @return string
325 protected function render_issued_badge(\core_badges\output\issued_badge $ibadge) {
326 $data = $ibadge->export_for_template($this);
327 return parent::render_from_template('core_badges/issued_badge', $data);
331 * Render an issued badge.
333 * @param \core_badges\output\badgeclass $badge
334 * @return string
336 protected function render_badgeclass(\core_badges\output\badgeclass $badge) {
337 $data = $badge->export_for_template($this);
338 return parent::render_from_template('core_badges/issued_badge', $data);
342 * Render an external badge.
344 * @param \core_badges\output\external_badge $ibadge
345 * @return string
347 protected function render_external_badge(\core_badges\output\external_badge $ibadge) {
348 $data = $ibadge->export_for_template($this);
349 return parent::render_from_template('core_badges/issued_badge', $data);
353 * Render a collection of user badges.
355 * @param \core_badges\output\badge_user_collection $badges
356 * @return string
358 protected function render_badge_user_collection(\core_badges\output\badge_user_collection $badges) {
359 global $CFG, $USER, $SITE;
360 $backpack = $badges->backpack;
361 $mybackpack = new moodle_url('/badges/mybackpack.php');
363 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
364 $htmlpagingbar = $this->render($paging);
366 // Set backpack connection string.
367 $backpackconnect = '';
368 if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) {
369 $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
371 // Search box.
372 $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
374 // Download all button.
375 $actionhtml = $this->output->single_button(
376 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
377 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
378 $downloadall = $this->output->box('', 'col-md-3');
379 $downloadall .= $this->output->box($actionhtml, 'col-md-9');
380 $downloadall = $this->output->box($downloadall, 'row ml-5');
382 // Local badges.
383 $localhtml = html_writer::start_tag('div', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
384 $sitename = format_string($SITE->fullname, true, array('context' => context_system::instance()));
385 $heading = get_string('localbadges', 'badges', $sitename);
386 $localhtml .= $this->output->heading_with_help($heading, 'localbadgesh', 'badges');
387 if ($badges->badges) {
388 $countmessage = $this->output->box(get_string('badgesearned', 'badges', $badges->totalcount));
390 $htmllist = $this->print_badges_list($badges->badges, $USER->id);
391 $localhtml .= $backpackconnect . $countmessage . $searchform;
392 $localhtml .= $htmlpagingbar . $htmllist . $htmlpagingbar . $downloadall;
393 } else {
394 $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'), 'info');
396 $localhtml .= html_writer::end_tag('div');
398 // External badges.
399 $externalhtml = "";
400 if (!empty($CFG->badges_allowexternalbackpack)) {
401 $externalhtml .= html_writer::start_tag('div', array('class' => 'generalbox'));
402 $externalhtml .= $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges');
403 if (!is_null($backpack)) {
404 if ($backpack->totalcollections == 0) {
405 $externalhtml .= get_string('nobackpackcollectionssummary', 'badges', $backpack);
406 } else {
407 if ($backpack->totalbadges == 0) {
408 $externalhtml .= get_string('nobackpackbadgessummary', 'badges', $backpack);
409 } else {
410 $externalhtml .= get_string('backpackbadgessummary', 'badges', $backpack);
411 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
414 } else {
415 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
418 $externalhtml .= html_writer::end_tag('div');
419 $attr = ['class' => 'btn btn-secondary'];
420 $label = get_string('backpackbadgessettings', 'badges');
421 $backpacksettings = html_writer::link(new moodle_url('/badges/mybackpack.php'), $label, $attr);
422 $actionshtml = $this->output->box('', 'col-md-3');
423 $actionshtml .= $this->output->box($backpacksettings, 'col-md-9');
424 $actionshtml = $this->output->box($actionshtml, 'row ml-5');
425 $externalhtml .= $actionshtml;
428 return $localhtml . $externalhtml;
432 * Render a collection of badges.
434 * @param \core_badges\output\badge_collection $badges
435 * @return string
437 protected function render_badge_collection(\core_badges\output\badge_collection $badges) {
438 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
439 $htmlpagingbar = $this->render($paging);
440 $table = new html_table();
441 $table->attributes['class'] = 'table table-bordered table-striped';
443 $sortbyname = $this->helper_sortable_heading(get_string('name'),
444 'name', $badges->sort, $badges->dir);
445 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
446 'dateissued', $badges->sort, $badges->dir);
447 $table->head = array(
448 get_string('badgeimage', 'badges'),
449 $sortbyname,
450 get_string('description', 'badges'),
451 get_string('bcriteria', 'badges'),
452 $sortbyawarded
454 $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
456 foreach ($badges->badges as $badge) {
457 $badgeimage = print_badge_image($badge, $this->page->context, 'large');
458 $name = $badge->name;
459 $description = $badge->description;
460 $criteria = self::print_badge_criteria($badge);
461 if ($badge->dateissued) {
462 $icon = new pix_icon('i/valid',
463 get_string('dateearned', 'badges',
464 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
465 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
466 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
467 } else {
468 $awarded = "";
470 $row = array($badgeimage, $name, $description, $criteria, $awarded);
471 $table->data[] = $row;
474 $htmltable = html_writer::table($table);
476 return $htmlpagingbar . $htmltable . $htmlpagingbar;
480 * Render a table of badges.
482 * @param \core_badges\output\badge_management $badges
483 * @return string
485 protected function render_badge_management(\core_badges\output\badge_management $badges) {
486 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
488 // New badge button.
489 $htmlnew = '';
490 $htmlpagingbar = $this->render($paging);
491 $table = new html_table();
492 $table->attributes['class'] = 'table table-bordered table-striped';
494 $sortbyname = $this->helper_sortable_heading(get_string('name'),
495 'name', $badges->sort, $badges->dir);
496 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
497 'status', $badges->sort, $badges->dir);
498 $table->head = array(
499 $sortbyname,
500 $sortbystatus,
501 get_string('bcriteria', 'badges'),
502 get_string('awards', 'badges'),
503 get_string('actions')
505 $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
507 foreach ($badges->badges as $b) {
508 $style = !$b->is_active() ? array('class' => 'dimmed') : array();
509 $forlink = print_badge_image($b, $this->page->context) . ' ' .
510 html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
511 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
512 $status = $b->statstring;
513 $criteria = self::print_badge_criteria($b, 'short');
515 if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
516 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
517 } else {
518 $awards = $b->awards;
521 $actions = self::print_badge_table_actions($b, $this->page->context);
523 $row = array($name, $status, $criteria, $awards, $actions);
524 $table->data[] = $row;
526 $htmltable = html_writer::table($table);
528 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
532 * Prints tabs for badge editing.
534 * @deprecated since Moodle 4.0
535 * @todo MDL-73426 Final deprecation.
536 * @param integer $badgeid The badgeid to edit.
537 * @param context $context The current context.
538 * @param string $current The currently selected tab.
539 * @return string
541 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
542 global $DB;
543 debugging("print_badge_tabs() is deprecated. " .
544 "This is replaced with the manage_badge_action_bar tertiary navigation.", DEBUG_DEVELOPER);
546 $badge = new badge($badgeid);
547 $row = array();
549 $row[] = new tabobject('overview',
550 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
551 get_string('boverview', 'badges')
554 if (has_capability('moodle/badges:configuredetails', $context)) {
555 $row[] = new tabobject('badge',
556 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'badge')),
557 get_string('bdetails', 'badges')
561 if (has_capability('moodle/badges:configurecriteria', $context)) {
562 $row[] = new tabobject('criteria',
563 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
564 get_string('bcriteria', 'badges')
568 if (has_capability('moodle/badges:configuremessages', $context)) {
569 $row[] = new tabobject('message',
570 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
571 get_string('bmessage', 'badges')
575 if (has_capability('moodle/badges:viewawarded', $context)) {
576 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
577 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
578 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
579 $row[] = new tabobject('awards',
580 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
581 get_string('bawards', 'badges', $awarded)
585 if (has_capability('moodle/badges:configuredetails', $context)) {
586 $row[] = new tabobject('bendorsement',
587 new moodle_url('/badges/endorsement.php', array('id' => $badgeid)),
588 get_string('bendorsement', 'badges')
592 if (has_capability('moodle/badges:configuredetails', $context)) {
593 $sql = "SELECT COUNT(br.badgeid)
594 FROM {badge_related} br
595 WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
596 $related = $DB->count_records_sql($sql, ['badgeid' => $badgeid, 'badgeid2' => $badgeid]);
597 $row[] = new tabobject('brelated',
598 new moodle_url('/badges/related.php', array('id' => $badgeid)),
599 get_string('brelated', 'badges', $related)
603 if (has_capability('moodle/badges:configuredetails', $context)) {
604 $alignments = $DB->count_records_sql("SELECT COUNT(bc.id)
605 FROM {badge_alignment} bc WHERE bc.badgeid = :badgeid", array('badgeid' => $badgeid));
606 $row[] = new tabobject('alignment',
607 new moodle_url('/badges/alignment.php', array('id' => $badgeid)),
608 get_string('balignment', 'badges', $alignments)
612 echo $this->tabtree($row, $current);
616 * Prints badge status box.
618 * @param badge $badge
619 * @return Either the status box html as a string or null
621 public function print_badge_status_box(badge $badge) {
622 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
624 if (!$badge->has_criteria()) {
625 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
626 $status = get_string('nocriteria', 'badges');
627 if ($this->page->url != $criteriaurl) {
628 $action = $this->output->single_button(
629 $criteriaurl,
630 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
631 } else {
632 $action = '';
635 $message = $status . $action;
636 } else {
637 $status = get_string('statusmessage_' . $badge->status, 'badges');
638 if ($badge->is_active()) {
639 $action = $this->output->single_button(new moodle_url('/badges/action.php',
640 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
641 'return' => $this->page->url->out_as_local_url(false))),
642 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
643 } else {
644 $action = $this->output->single_button(new moodle_url('/badges/action.php',
645 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
646 'return' => $this->page->url->out_as_local_url(false))),
647 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
650 $message = $status . $this->output->help_icon('status', 'badges') . $action;
654 $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
655 return $this->output->box($message, $style);
658 return null;
662 * Returns information about badge criteria in a list form.
664 * @param badge $badge Badge objects
665 * @param string $short Indicates whether to print full info about this badge
666 * @return string $output HTML string to output
668 public function print_badge_criteria(badge $badge, $short = '') {
669 $agg = $badge->get_aggregation_methods();
670 if (empty($badge->criteria)) {
671 return get_string('nocriteria', 'badges');
674 $overalldescr = '';
675 $overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL];
676 if (!$short && !empty($overall->description)) {
677 $overalldescr = $this->output->box(
678 format_text($overall->description, $overall->descriptionformat, array('context' => $badge->get_context())),
679 'criteria-description'
683 // Get the condition string.
684 $condition = '';
685 if (count($badge->criteria) != 2) {
686 $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
687 core_text::strtoupper($agg[$badge->get_aggregation_method()]));
690 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
692 $items = array();
693 // If only one criterion left, make sure its description goe to the top.
694 if (count($badge->criteria) == 1) {
695 $c = reset($badge->criteria);
696 if (!$short && !empty($c->description)) {
697 $overalldescr = $this->output->box(
698 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
699 'criteria-description'
702 if (count($c->params) == 1) {
703 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype , 'badges') .
704 $c->get_details($short);
705 } else {
706 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype, 'badges',
707 core_text::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype)])) .
708 $c->get_details($short);
710 } else {
711 foreach ($badge->criteria as $type => $c) {
712 $criteriadescr = '';
713 if (!$short && !empty($c->description)) {
714 $criteriadescr = $this->output->box(
715 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
716 'criteria-description'
719 if (count($c->params) == 1) {
720 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
721 $c->get_details($short) . $criteriadescr;
722 } else {
723 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
724 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) .
725 $c->get_details($short) .
726 $criteriadescr;
731 return $overalldescr . $condition . html_writer::alist($items, array(), 'ul');;
735 * Prints criteria actions for badge editing.
737 * @param badge $badge
738 * @return string
740 public function print_criteria_actions(badge $badge) {
741 $output = '';
742 if (!$badge->is_active() && !$badge->is_locked()) {
743 $accepted = $badge->get_accepted_criteria();
744 $potential = array_diff($accepted, array_keys($badge->criteria));
746 if (!empty($potential)) {
747 foreach ($potential as $p) {
748 if ($p != 0) {
749 $select[$p] = get_string('criteria_' . $p, 'badges');
752 $output .= $this->output->single_select(
753 new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id, 'add' => true)),
754 'type',
755 $select,
757 array('' => 'choosedots'),
758 null,
759 array('label' => get_string('addbadgecriteria', 'badges'))
761 } else {
762 $output .= $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
766 return $output;
770 * Renders a table with users who have earned the badge.
771 * Based on stamps collection plugin.
773 * @param \core_badges\output\badge_recipients $recipients
774 * @return string
776 protected function render_badge_recipients(\core_badges\output\badge_recipients $recipients) {
777 $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
778 $htmlpagingbar = $this->render($paging);
779 $table = new html_table();
780 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
782 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
783 'firstname', $recipients->sort, $recipients->dir);
784 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
785 'lastname', $recipients->sort, $recipients->dir);
786 if ($this->helper_fullname_format() == 'lf') {
787 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
788 } else {
789 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
792 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
793 'dateissued', $recipients->sort, $recipients->dir);
795 $table->head = array($sortbyname, $sortbydate, '');
797 foreach ($recipients->userids as $holder) {
798 $fullname = fullname($holder);
799 $fullname = html_writer::link(
800 new moodle_url('/user/profile.php', array('id' => $holder->userid)),
801 $fullname
803 $awarded = userdate($holder->dateissued);
804 $badgeurl = html_writer::link(
805 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
806 get_string('viewbadge', 'badges')
809 $row = array($fullname, $awarded, $badgeurl);
810 $table->data[] = $row;
813 $htmltable = html_writer::table($table);
815 return $htmlpagingbar . $htmltable . $htmlpagingbar;
818 ////////////////////////////////////////////////////////////////////////////
819 // Helper methods
820 // Reused from stamps collection plugin
821 ////////////////////////////////////////////////////////////////////////////
824 * Renders a text with icons to sort by the given column
826 * This is intended for table headings.
828 * @param string $text The heading text
829 * @param string $sortid The column id used for sorting
830 * @param string $sortby Currently sorted by (column id)
831 * @param string $sorthow Currently sorted how (ASC|DESC)
833 * @return string
835 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
836 $out = html_writer::tag('span', $text, array('class' => 'text'));
838 if (!is_null($sortid)) {
839 if ($sortby !== $sortid || $sorthow !== 'ASC') {
840 $url = new moodle_url($this->page->url);
841 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
842 $out .= $this->output->action_icon($url,
843 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
845 if ($sortby !== $sortid || $sorthow !== 'DESC') {
846 $url = new moodle_url($this->page->url);
847 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
848 $out .= $this->output->action_icon($url,
849 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
852 return $out;
855 * Tries to guess the fullname format set at the site
857 * @return string fl|lf
859 protected function helper_fullname_format() {
860 $fake = new stdClass();
861 $fake->lastname = 'LLLL';
862 $fake->firstname = 'FFFF';
863 $fullname = get_string('fullnamedisplay', '', $fake);
864 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
865 return 'lf';
866 } else {
867 return 'fl';
871 * Renders a search form
873 * @param string $search Search string
874 * @return string HTML
876 protected function helper_search_form($search) {
877 global $CFG;
878 require_once($CFG->libdir . '/formslib.php');
880 $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
882 $mform->addElement('hidden', 'sesskey', sesskey());
884 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
885 $mform->setDefault('search', $search);
886 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
887 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
888 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
890 ob_start();
891 $mform->display();
892 $out = ob_get_clean();
894 return $out;
898 * Renders a definition list
900 * @param array $items the list of items to define
901 * @param array
903 protected function definition_list(array $items, array $attributes = array()) {
904 $output = html_writer::start_tag('dl', $attributes);
905 foreach ($items as $label => $value) {
906 $output .= html_writer::tag('dt', $label);
907 $output .= html_writer::tag('dd', $value);
909 $output .= html_writer::end_tag('dl');
910 return $output;
914 * Outputs list en badges.
916 * @param badge $badge Badge object.
917 * @return string $output content endorsement to output.
919 protected function print_badge_endorsement(badge $badge) {
920 $output = '';
921 $endorsement = $badge->get_endorsement();
922 $dl = array();
923 $output .= $this->heading(get_string('endorsement', 'badges'), 3);
924 if (!empty($endorsement)) {
925 $dl[get_string('issuername', 'badges')] = $endorsement->issuername;
926 $dl[get_string('issueremail', 'badges')] =
927 html_writer::tag('a', $endorsement->issueremail, array('href' => 'mailto:' . $endorsement->issueremail));
928 $dl[get_string('issuerurl', 'badges')] = html_writer::link($endorsement->issuerurl, $endorsement->issuerurl,
929 array('target' => '_blank'));
930 $dl[get_string('dateawarded', 'badges')] = userdate($endorsement->dateissued);
931 $dl[get_string('claimid', 'badges')] = html_writer::link($endorsement->claimid, $endorsement->claimid,
932 array('target' => '_blank'));
933 $dl[get_string('claimcomment', 'badges')] = $endorsement->claimcomment;
934 $output .= $this->definition_list($dl);
935 } else {
936 $output .= get_string('noendorsement', 'badges');
938 return $output;
942 * Print list badges related.
944 * @param badge $badge Badge objects.
945 * @return string $output List related badges to output.
947 protected function print_badge_related(badge $badge) {
948 $output = '';
949 $relatedbadges = $badge->get_related_badges();
950 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
951 if (!empty($relatedbadges)) {
952 $items = array();
953 foreach ($relatedbadges as $related) {
954 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id));
955 $items[] = html_writer::link($relatedurl->out(), $related->name, array('target' => '_blank'));
957 $output .= html_writer::alist($items, array(), 'ul');
958 } else {
959 $output .= get_string('norelated', 'badges');
961 return $output;
965 * Print list badge alignments.
967 * @param badge $badge Badge objects.
968 * @return string $output List alignments to output.
970 protected function print_badge_alignments(badge $badge) {
971 $output = '';
972 $output .= $this->heading(get_string('alignment', 'badges'), 3);
973 $alignments = $badge->get_alignments();
974 if (!empty($alignments)) {
975 $items = array();
976 foreach ($alignments as $alignment) {
977 $urlaligment = new moodle_url('alignment.php',
978 array('id' => $badge->id, 'alignmentid' => $alignment->id)
980 $items[] = html_writer::link($urlaligment, $alignment->targetname, array('target' => '_blank'));
982 $output .= html_writer::alist($items, array(), 'ul');
983 } else {
984 $output .= get_string('noalignment', 'badges');
986 return $output;
990 * Renders a table for related badges.
992 * @param \core_badges\output\badge_related $related list related badges.
993 * @return string list related badges to output.
995 protected function render_badge_related(\core_badges\output\badge_related $related) {
996 $currentbadge = new badge($related->currentbadgeid);
997 $languages = get_string_manager()->get_list_of_languages();
998 $paging = new paging_bar($related->totalcount, $related->page, $related->perpage, $this->page->url, 'page');
999 $htmlpagingbar = $this->render($paging);
1000 $table = new html_table();
1001 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1002 $table->head = array(
1003 get_string('name'),
1004 get_string('version', 'badges'),
1005 get_string('language', 'badges'),
1006 get_string('type', 'badges')
1008 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1009 array_push($table->head, '');
1012 foreach ($related->badges as $badge) {
1013 $badgeobject = new badge($badge->id);
1014 $style = array('title' => $badgeobject->name);
1015 if (!$badgeobject->is_active()) {
1016 $style['class'] = 'dimmed';
1018 $context = ($badgeobject->type == BADGE_TYPE_SITE) ?
1019 context_system::instance() : context_course::instance($badgeobject->courseid);
1020 $forlink = print_badge_image($badgeobject, $context) . ' ' .
1021 html_writer::start_tag('span') . $badgeobject->name . html_writer::end_tag('span');
1022 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $badgeobject->id)), $forlink, $style);
1024 $row = array(
1025 $name,
1026 $badge->version,
1027 $badge->language ? $languages[$badge->language] : '',
1028 $badge->type == BADGE_TYPE_COURSE ? get_string('badgesview', 'badges') : get_string('sitebadges', 'badges')
1030 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1031 $action = $this->output->action_icon(
1032 new moodle_url('/badges/related_action.php', [
1033 'badgeid' => $related->currentbadgeid,
1034 'relatedid' => $badge->id,
1035 'sesskey' => sesskey(),
1036 'action' => 'remove'
1038 new pix_icon('t/delete', get_string('delete')));
1039 $actions = html_writer::tag('div', $action, array('class' => 'badge-actions'));
1040 array_push($row, $actions);
1042 $table->data[] = $row;
1044 $htmltable = html_writer::table($table);
1046 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1050 * Renders a table with alignment.
1052 * @param core_badges\output\badge_alignments $alignments List alignments.
1053 * @return string List alignment to output.
1055 protected function render_badge_alignments(\core_badges\output\badge_alignments $alignments) {
1056 $currentbadge = new badge($alignments->currentbadgeid);
1057 $paging = new paging_bar($alignments->totalcount, $alignments->page, $alignments->perpage, $this->page->url, 'page');
1058 $htmlpagingbar = $this->render($paging);
1059 $table = new html_table();
1060 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1061 $table->head = array('Name', 'URL', '');
1063 foreach ($alignments->alignments as $item) {
1064 $urlaligment = new moodle_url('alignment.php',
1065 array(
1066 'id' => $currentbadge->id,
1067 'alignmentid' => $item->id,
1070 $row = array(
1071 html_writer::link($urlaligment, $item->targetname),
1072 html_writer::link($item->targeturl, $item->targeturl, array('target' => '_blank'))
1074 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1075 $delete = $this->output->action_icon(
1076 new moodle_url('/badges/alignment_action.php', [
1077 'id' => $currentbadge->id,
1078 'alignmentid' => $item->id,
1079 'sesskey' => sesskey(),
1080 'action' => 'remove'
1082 new pix_icon('t/delete', get_string('delete'))
1084 $edit = $this->output->action_icon(
1085 new moodle_url('alignment.php',
1086 array(
1087 'id' => $currentbadge->id,
1088 'alignmentid' => $item->id,
1089 'action' => 'edit'
1091 ), new pix_icon('t/edit', get_string('edit')));
1092 $actions = html_writer::tag('div', $edit . $delete, array('class' => 'badge-actions'));
1093 array_push($row, $actions);
1095 $table->data[] = $row;
1097 $htmltable = html_writer::table($table);
1099 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1103 * Defer to template.
1105 * @param \core_badges\output\external_backpacks_page $page
1106 * @return bool|string
1108 public function render_external_backpacks_page(\core_badges\output\external_backpacks_page $page) {
1109 $data = $page->export_for_template($this);
1110 return parent::render_from_template('core_badges/external_backpacks_page', $data);
1114 * Get the result of a backpack validation with its settings. It returns:
1115 * - A informative message if the backpack version is different from OBv2.
1116 * - A warning with the error if it's not possible to connect to this backpack.
1117 * - A successful message if the connection has worked.
1119 * @param int $backpackid The backpack identifier.
1120 * @return string A message with the validation result.
1122 public function render_test_backpack_result(int $backpackid): string {
1123 // Get the backpack.
1124 $backpack = badges_get_site_backpack($backpackid);
1126 // Add the header to the result.
1127 $result = $this->heading(get_string('testbackpack', 'badges', $backpack->backpackweburl));
1129 if ($backpack->apiversion != OPEN_BADGES_V2) {
1130 // Only OBv2 supports this validation.
1131 $result .= get_string('backpackconnectionnottested', 'badges');
1132 } else {
1133 $message = badges_verify_backpack($backpackid);
1134 if (empty($message)) {
1135 $result .= get_string('backpackconnectionok', 'badges');
1136 } else {
1137 $result .= $message;
1141 return $result;
1145 * Render the tertiary navigation for the page.
1147 * @param \core_badges\output\base_action_bar $actionbar
1148 * @return bool|string
1150 public function render_tertiary_navigation(\core_badges\output\base_action_bar $actionbar) {
1151 return $this->render_from_template($actionbar->get_template(), $actionbar->export_for_template($this));