MDL-78324 core: Convert core/tag to esm
[moodle.git] / badges / renderer.php
blob3880a8f24e91b0288da87875c9f1b8f86a55fa8b
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 $tags = \core_tag_tag::get_item_tags('core_badges', 'badge', $badge->id);
196 $dl[get_string('tags', 'badges')] = $this->output->tag_list($tags, '');
197 $display .= $this->definition_list($dl);
199 // Issuer details.
200 $display .= $this->heading(get_string('issuerdetails', 'badges'), 3);
201 $dl = array();
202 $dl[get_string('issuername', 'badges')] = $badge->issuername;
203 $dl[get_string('contact', 'badges')] = html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact));
204 $display .= $this->definition_list($dl);
206 // Issuance details if any.
207 $display .= $this->heading(get_string('issuancedetails', 'badges'), 3);
208 if ($badge->can_expire()) {
209 if ($badge->expiredate) {
210 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate));
211 } else if ($badge->expireperiod) {
212 if ($badge->expireperiod < 60) {
213 $display .= get_string('expireperiods', 'badges', round($badge->expireperiod, 2));
214 } else if ($badge->expireperiod < 60 * 60) {
215 $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod / 60, 2));
216 } else if ($badge->expireperiod < 60 * 60 * 24) {
217 $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod / 60 / 60, 2));
218 } else {
219 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2));
222 } else {
223 $display .= get_string('noexpiry', 'badges');
226 // Criteria details if any.
227 $display .= $this->heading(get_string('bcriteria', 'badges'), 3);
228 if ($badge->has_criteria()) {
229 $display .= self::print_badge_criteria($badge);
230 } else {
231 $display .= get_string('nocriteria', 'badges');
232 if (has_capability('moodle/badges:configurecriteria', $context)) {
233 $display .= $this->output->single_button(
234 new moodle_url('/badges/criteria.php', array('id' => $badge->id)),
235 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
239 // Awards details if any.
240 if (has_capability('moodle/badges:viewawarded', $context)) {
241 $display .= $this->heading(get_string('awards', 'badges'), 3);
242 if ($badge->has_awards()) {
243 $url = new moodle_url('/badges/recipients.php', array('id' => $badge->id));
244 $a = new stdClass();
245 $a->link = $url->out();
246 $a->count = count($badge->get_awards());
247 $display .= get_string('numawards', 'badges', $a);
248 } else {
249 $display .= get_string('noawards', 'badges');
252 if (has_capability('moodle/badges:awardbadge', $context) &&
253 $badge->has_manual_award_criteria() &&
254 $badge->is_active()) {
255 $display .= $this->output->single_button(
256 new moodle_url('/badges/award.php', array('id' => $badge->id)),
257 get_string('award', 'badges'), 'POST', array('class' => 'activatebadge'));
261 $display .= self::print_badge_endorsement($badge);
262 $display .= self::print_badge_related($badge);
263 $display .= self::print_badge_alignments($badge);
265 return html_writer::div($display, null, array('id' => 'badge-overview'));
269 * Prints action icons for the badge.
271 * @deprecated sinde Moodle 4.3
272 * @param \core_badges\badge $badge
273 * @param \context $context
274 * @return string
276 public function print_badge_table_actions($badge, $context) {
277 debugging("print_badge_table_actions() is deprecated.", DEBUG_DEVELOPER);
278 $actions = "";
280 if (has_capability('moodle/badges:configuredetails', $context) && $badge->has_criteria()) {
281 // Activate/deactivate badge.
282 if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) {
283 // "Activate" will go to another page and ask for confirmation.
284 $url = new moodle_url('/badges/action.php');
285 $url->param('id', $badge->id);
286 $url->param('activate', true);
287 $url->param('sesskey', sesskey());
288 $return = new moodle_url(qualified_me());
289 $url->param('return', $return->out_as_local_url(false));
290 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
291 } else {
292 $url = new moodle_url(qualified_me());
293 $url->param('lock', $badge->id);
294 $url->param('sesskey', sesskey());
295 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
299 // Award badge manually.
300 if ($badge->has_manual_award_criteria() &&
301 has_capability('moodle/badges:awardbadge', $context) &&
302 $badge->is_active()) {
303 $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
304 $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
307 // Edit badge.
308 if (has_capability('moodle/badges:configuredetails', $context)) {
309 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'badge'));
310 $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
313 // Duplicate badge.
314 if (has_capability('moodle/badges:createbadge', $context)) {
315 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey()));
316 $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
319 // Delete badge.
320 if (has_capability('moodle/badges:deletebadge', $context)) {
321 $url = new moodle_url(qualified_me());
322 $url->param('delete', $badge->id);
323 $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
326 return $actions;
330 * Render an issued badge.
332 * @param \core_badges\output\issued_badge $ibadge
333 * @return string
335 protected function render_issued_badge(\core_badges\output\issued_badge $ibadge) {
336 $data = $ibadge->export_for_template($this);
337 return parent::render_from_template('core_badges/issued_badge', $data);
341 * Render an issued badge.
343 * @param \core_badges\output\badgeclass $badge
344 * @return string
346 protected function render_badgeclass(\core_badges\output\badgeclass $badge) {
347 $data = $badge->export_for_template($this);
348 return parent::render_from_template('core_badges/issued_badge', $data);
352 * Render an external badge.
354 * @param \core_badges\output\external_badge $ibadge
355 * @return string
357 protected function render_external_badge(\core_badges\output\external_badge $ibadge) {
358 $data = $ibadge->export_for_template($this);
359 return parent::render_from_template('core_badges/issued_badge', $data);
363 * Render a collection of user badges.
365 * @param \core_badges\output\badge_user_collection $badges
366 * @return string
368 protected function render_badge_user_collection(\core_badges\output\badge_user_collection $badges) {
369 global $CFG, $USER, $SITE;
370 $backpack = $badges->backpack;
371 $mybackpack = new moodle_url('/badges/mybackpack.php');
373 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
374 $htmlpagingbar = $this->render($paging);
376 // Set backpack connection string.
377 $backpackconnect = '';
378 if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) {
379 $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
381 // Search box.
382 $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
384 // Download all button.
385 $actionhtml = $this->output->single_button(
386 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
387 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
388 $downloadall = $this->output->box('', 'col-md-3');
389 $downloadall .= $this->output->box($actionhtml, 'col-md-9');
390 $downloadall = $this->output->box($downloadall, 'row ml-5');
392 // Local badges.
393 $localhtml = html_writer::start_tag('div', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
394 $sitename = format_string($SITE->fullname, true, array('context' => context_system::instance()));
395 $heading = get_string('localbadges', 'badges', $sitename);
396 $localhtml .= $this->output->heading_with_help($heading, 'localbadgesh', 'badges');
397 if ($badges->badges) {
398 $countmessage = $this->output->box(get_string('badgesearned', 'badges', $badges->totalcount));
400 $htmllist = $this->print_badges_list($badges->badges, $USER->id);
401 $localhtml .= $backpackconnect . $countmessage . $searchform;
402 $localhtml .= $htmlpagingbar . $htmllist . $htmlpagingbar . $downloadall;
403 } else {
404 $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'), 'info');
406 $localhtml .= html_writer::end_tag('div');
408 // External badges.
409 $externalhtml = "";
410 if (!empty($CFG->badges_allowexternalbackpack)) {
411 $externalhtml .= html_writer::start_tag('div', array('class' => 'generalbox'));
412 $externalhtml .= $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges');
413 if (!is_null($backpack)) {
414 if ($backpack->totalcollections == 0) {
415 $externalhtml .= get_string('nobackpackcollectionssummary', 'badges', $backpack);
416 } else {
417 if ($backpack->totalbadges == 0) {
418 $externalhtml .= get_string('nobackpackbadgessummary', 'badges', $backpack);
419 } else {
420 $externalhtml .= get_string('backpackbadgessummary', 'badges', $backpack);
421 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
424 } else {
425 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
428 $externalhtml .= html_writer::end_tag('div');
429 $attr = ['class' => 'btn btn-secondary'];
430 $label = get_string('backpackbadgessettings', 'badges');
431 $backpacksettings = html_writer::link(new moodle_url('/badges/mybackpack.php'), $label, $attr);
432 $actionshtml = $this->output->box('', 'col-md-3');
433 $actionshtml .= $this->output->box($backpacksettings, 'col-md-9');
434 $actionshtml = $this->output->box($actionshtml, 'row ml-5');
435 $externalhtml .= $actionshtml;
438 return $localhtml . $externalhtml;
442 * Render a collection of badges.
444 * @param \core_badges\output\badge_collection $badges
445 * @return string
447 protected function render_badge_collection(\core_badges\output\badge_collection $badges) {
448 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
449 $htmlpagingbar = $this->render($paging);
450 $table = new html_table();
451 $table->attributes['class'] = 'table table-bordered table-striped';
453 $sortbyname = $this->helper_sortable_heading(get_string('name'),
454 'name', $badges->sort, $badges->dir);
455 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
456 'dateissued', $badges->sort, $badges->dir);
457 $table->head = array(
458 get_string('badgeimage', 'badges'),
459 $sortbyname,
460 get_string('description', 'badges'),
461 get_string('bcriteria', 'badges'),
462 $sortbyawarded
464 $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
466 foreach ($badges->badges as $badge) {
467 $badgeimage = print_badge_image($badge, $this->page->context, 'large');
468 $name = $badge->name;
469 $description = $badge->description;
470 $criteria = self::print_badge_criteria($badge);
471 if ($badge->dateissued) {
472 $icon = new pix_icon('i/valid',
473 get_string('dateearned', 'badges',
474 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
475 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
476 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
477 } else {
478 $awarded = "";
480 $row = array($badgeimage, $name, $description, $criteria, $awarded);
481 $table->data[] = $row;
484 $htmltable = html_writer::table($table);
486 return $htmlpagingbar . $htmltable . $htmlpagingbar;
490 * Render a table of badges.
492 * @deprecated since Moodle 4.3
493 * @param \core_badges\output\badge_management $badges
494 * @return string
496 protected function render_badge_management(\core_badges\output\badge_management $badges) {
497 debugging("render_badge_management() is deprecated.", DEBUG_DEVELOPER);
498 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
500 // New badge button.
501 $htmlnew = '';
502 $htmlpagingbar = $this->render($paging);
503 $table = new html_table();
504 $table->attributes['class'] = 'table table-bordered table-striped';
506 $sortbyname = $this->helper_sortable_heading(get_string('name'),
507 'name', $badges->sort, $badges->dir);
508 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
509 'status', $badges->sort, $badges->dir);
510 $table->head = array(
511 $sortbyname,
512 $sortbystatus,
513 get_string('bcriteria', 'badges'),
514 get_string('awards', 'badges'),
515 get_string('actions')
517 $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
519 foreach ($badges->badges as $b) {
520 $style = !$b->is_active() ? array('class' => 'dimmed') : array();
521 $forlink = print_badge_image($b, $this->page->context) . ' ' .
522 html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
523 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
524 $status = $b->statstring;
525 $criteria = self::print_badge_criteria($b, 'short');
527 if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
528 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
529 } else {
530 $awards = $b->awards;
533 $actions = self::print_badge_table_actions($b, $this->page->context);
535 $row = array($name, $status, $criteria, $awards, $actions);
536 $table->data[] = $row;
538 $htmltable = html_writer::table($table);
540 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
544 * Prints tabs for badge editing.
546 * @deprecated since Moodle 4.0
547 * @todo MDL-73426 Final deprecation.
548 * @param integer $badgeid The badgeid to edit.
549 * @param context $context The current context.
550 * @param string $current The currently selected tab.
551 * @return string
553 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
554 global $DB;
555 debugging("print_badge_tabs() is deprecated. " .
556 "This is replaced with the manage_badge_action_bar tertiary navigation.", DEBUG_DEVELOPER);
558 $badge = new badge($badgeid);
559 $row = array();
561 $row[] = new tabobject('overview',
562 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
563 get_string('boverview', 'badges')
566 if (has_capability('moodle/badges:configuredetails', $context)) {
567 $row[] = new tabobject('badge',
568 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'badge')),
569 get_string('bdetails', 'badges')
573 if (has_capability('moodle/badges:configurecriteria', $context)) {
574 $row[] = new tabobject('criteria',
575 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
576 get_string('bcriteria', 'badges')
580 if (has_capability('moodle/badges:configuremessages', $context)) {
581 $row[] = new tabobject('message',
582 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
583 get_string('bmessage', 'badges')
587 if (has_capability('moodle/badges:viewawarded', $context)) {
588 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
589 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
590 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
591 $row[] = new tabobject('awards',
592 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
593 get_string('bawards', 'badges', $awarded)
597 if (has_capability('moodle/badges:configuredetails', $context)) {
598 $row[] = new tabobject('bendorsement',
599 new moodle_url('/badges/endorsement.php', array('id' => $badgeid)),
600 get_string('bendorsement', 'badges')
604 if (has_capability('moodle/badges:configuredetails', $context)) {
605 $sql = "SELECT COUNT(br.badgeid)
606 FROM {badge_related} br
607 WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
608 $related = $DB->count_records_sql($sql, ['badgeid' => $badgeid, 'badgeid2' => $badgeid]);
609 $row[] = new tabobject('brelated',
610 new moodle_url('/badges/related.php', array('id' => $badgeid)),
611 get_string('brelated', 'badges', $related)
615 if (has_capability('moodle/badges:configuredetails', $context)) {
616 $alignments = $DB->count_records_sql("SELECT COUNT(bc.id)
617 FROM {badge_alignment} bc WHERE bc.badgeid = :badgeid", array('badgeid' => $badgeid));
618 $row[] = new tabobject('alignment',
619 new moodle_url('/badges/alignment.php', array('id' => $badgeid)),
620 get_string('balignment', 'badges', $alignments)
624 echo $this->tabtree($row, $current);
628 * Prints badge status box.
630 * @param badge $badge
631 * @return Either the status box html as a string or null
633 public function print_badge_status_box(badge $badge) {
634 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
636 if (!$badge->has_criteria()) {
637 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
638 $status = get_string('nocriteria', 'badges');
639 if ($this->page->url != $criteriaurl) {
640 $action = $this->output->single_button(
641 $criteriaurl,
642 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
643 } else {
644 $action = '';
647 $message = $status . $action;
648 } else {
649 $status = get_string('statusmessage_' . $badge->status, 'badges');
650 if ($badge->is_active()) {
651 $action = $this->output->single_button(new moodle_url('/badges/action.php',
652 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
653 'return' => $this->page->url->out_as_local_url(false))),
654 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
655 } else {
656 $action = $this->output->single_button(new moodle_url('/badges/action.php',
657 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
658 'return' => $this->page->url->out_as_local_url(false))),
659 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
662 $message = $status . $this->output->help_icon('status', 'badges') . $action;
666 $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
667 return $this->output->box($message, $style);
670 return null;
674 * Returns information about badge criteria in a list form.
676 * @param badge $badge Badge objects
677 * @param string $short Indicates whether to print full info about this badge
678 * @return string $output HTML string to output
680 public function print_badge_criteria(badge $badge, $short = '') {
681 $agg = $badge->get_aggregation_methods();
682 if (empty($badge->criteria)) {
683 return get_string('nocriteria', 'badges');
686 $overalldescr = '';
687 $overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL];
688 if (!$short && !empty($overall->description)) {
689 $overalldescr = $this->output->box(
690 format_text($overall->description, $overall->descriptionformat, array('context' => $badge->get_context())),
691 'criteria-description'
695 // Get the condition string.
696 $condition = '';
697 if (count($badge->criteria) != 2) {
698 $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
699 core_text::strtoupper($agg[$badge->get_aggregation_method()]));
702 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
704 $items = array();
705 // If only one criterion left, make sure its description goe to the top.
706 if (count($badge->criteria) == 1) {
707 $c = reset($badge->criteria);
708 if (!$short && !empty($c->description)) {
709 $overalldescr = $this->output->box(
710 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
711 'criteria-description'
714 if (count($c->params) == 1) {
715 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype , 'badges') .
716 $c->get_details($short);
717 } else {
718 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype, 'badges',
719 core_text::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype)])) .
720 $c->get_details($short);
722 } else {
723 foreach ($badge->criteria as $type => $c) {
724 $criteriadescr = '';
725 if (!$short && !empty($c->description)) {
726 $criteriadescr = $this->output->box(
727 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
728 'criteria-description'
731 if (count($c->params) == 1) {
732 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
733 $c->get_details($short) . $criteriadescr;
734 } else {
735 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
736 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) .
737 $c->get_details($short) .
738 $criteriadescr;
743 return $overalldescr . $condition . html_writer::alist($items, array(), 'ul');;
747 * Prints criteria actions for badge editing.
749 * @param badge $badge
750 * @return string
752 public function print_criteria_actions(badge $badge) {
753 $output = '';
754 if (!$badge->is_active() && !$badge->is_locked()) {
755 $accepted = $badge->get_accepted_criteria();
756 $potential = array_diff($accepted, array_keys($badge->criteria));
758 if (!empty($potential)) {
759 foreach ($potential as $p) {
760 if ($p != 0) {
761 $select[$p] = get_string('criteria_' . $p, 'badges');
764 $output .= $this->output->single_select(
765 new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id, 'add' => true)),
766 'type',
767 $select,
769 array('' => 'choosedots'),
770 null,
771 array('label' => get_string('addbadgecriteria', 'badges'))
773 } else {
774 $output .= $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
778 return $output;
782 * Renders a table with users who have earned the badge.
783 * Based on stamps collection plugin.
785 * @param \core_badges\output\badge_recipients $recipients
786 * @return string
788 protected function render_badge_recipients(\core_badges\output\badge_recipients $recipients) {
789 $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
790 $htmlpagingbar = $this->render($paging);
791 $table = new html_table();
792 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
794 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
795 'firstname', $recipients->sort, $recipients->dir);
796 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
797 'lastname', $recipients->sort, $recipients->dir);
798 if ($this->helper_fullname_format() == 'lf') {
799 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
800 } else {
801 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
804 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
805 'dateissued', $recipients->sort, $recipients->dir);
807 $table->head = array($sortbyname, $sortbydate, '');
809 foreach ($recipients->userids as $holder) {
810 $fullname = fullname($holder);
811 $fullname = html_writer::link(
812 new moodle_url('/user/profile.php', array('id' => $holder->userid)),
813 $fullname
815 $awarded = userdate($holder->dateissued);
816 $badgeurl = html_writer::link(
817 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
818 get_string('viewbadge', 'badges')
821 $row = array($fullname, $awarded, $badgeurl);
822 $table->data[] = $row;
825 $htmltable = html_writer::table($table);
827 return $htmlpagingbar . $htmltable . $htmlpagingbar;
830 ////////////////////////////////////////////////////////////////////////////
831 // Helper methods
832 // Reused from stamps collection plugin
833 ////////////////////////////////////////////////////////////////////////////
836 * Renders a text with icons to sort by the given column
838 * This is intended for table headings.
840 * @param string $text The heading text
841 * @param string $sortid The column id used for sorting
842 * @param string $sortby Currently sorted by (column id)
843 * @param string $sorthow Currently sorted how (ASC|DESC)
845 * @return string
847 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
848 $out = html_writer::tag('span', $text, array('class' => 'text'));
850 if (!is_null($sortid)) {
851 if ($sortby !== $sortid || $sorthow !== 'ASC') {
852 $url = new moodle_url($this->page->url);
853 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
854 $out .= $this->output->action_icon($url,
855 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
857 if ($sortby !== $sortid || $sorthow !== 'DESC') {
858 $url = new moodle_url($this->page->url);
859 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
860 $out .= $this->output->action_icon($url,
861 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
864 return $out;
867 * Tries to guess the fullname format set at the site
869 * @return string fl|lf
871 protected function helper_fullname_format() {
872 $fake = new stdClass();
873 $fake->lastname = 'LLLL';
874 $fake->firstname = 'FFFF';
875 $fullname = get_string('fullnamedisplay', '', $fake);
876 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
877 return 'lf';
878 } else {
879 return 'fl';
883 * Renders a search form
885 * @param string $search Search string
886 * @return string HTML
888 protected function helper_search_form($search) {
889 global $CFG;
890 require_once($CFG->libdir . '/formslib.php');
892 $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
894 $mform->addElement('hidden', 'sesskey', sesskey());
896 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
897 $mform->setDefault('search', $search);
898 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
899 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
900 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
902 ob_start();
903 $mform->display();
904 $out = ob_get_clean();
906 return $out;
910 * Renders a definition list
912 * @param array $items the list of items to define
913 * @param array
915 protected function definition_list(array $items, array $attributes = array()) {
916 $output = html_writer::start_tag('dl', $attributes);
917 foreach ($items as $label => $value) {
918 $output .= html_writer::tag('dt', $label);
919 $output .= html_writer::tag('dd', $value);
921 $output .= html_writer::end_tag('dl');
922 return $output;
926 * Outputs list en badges.
928 * @param badge $badge Badge object.
929 * @return string $output content endorsement to output.
931 protected function print_badge_endorsement(badge $badge) {
932 $output = '';
933 $endorsement = $badge->get_endorsement();
934 $dl = array();
935 $output .= $this->heading(get_string('endorsement', 'badges'), 3);
936 if (!empty($endorsement)) {
937 $dl[get_string('issuername', 'badges')] = $endorsement->issuername;
938 $dl[get_string('issueremail', 'badges')] =
939 html_writer::tag('a', $endorsement->issueremail, array('href' => 'mailto:' . $endorsement->issueremail));
940 $dl[get_string('issuerurl', 'badges')] = html_writer::link($endorsement->issuerurl, $endorsement->issuerurl,
941 array('target' => '_blank'));
942 $dl[get_string('dateawarded', 'badges')] = userdate($endorsement->dateissued);
943 $dl[get_string('claimid', 'badges')] = html_writer::link($endorsement->claimid, $endorsement->claimid,
944 array('target' => '_blank'));
945 $dl[get_string('claimcomment', 'badges')] = $endorsement->claimcomment;
946 $output .= $this->definition_list($dl);
947 } else {
948 $output .= get_string('noendorsement', 'badges');
950 return $output;
954 * Print list badges related.
956 * @param badge $badge Badge objects.
957 * @return string $output List related badges to output.
959 protected function print_badge_related(badge $badge) {
960 $output = '';
961 $relatedbadges = $badge->get_related_badges();
962 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
963 if (!empty($relatedbadges)) {
964 $items = array();
965 foreach ($relatedbadges as $related) {
966 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id));
967 $items[] = html_writer::link($relatedurl->out(), $related->name, array('target' => '_blank'));
969 $output .= html_writer::alist($items, array(), 'ul');
970 } else {
971 $output .= get_string('norelated', 'badges');
973 return $output;
977 * Print list badge alignments.
979 * @param badge $badge Badge objects.
980 * @return string $output List alignments to output.
982 protected function print_badge_alignments(badge $badge) {
983 $output = '';
984 $output .= $this->heading(get_string('alignment', 'badges'), 3);
985 $alignments = $badge->get_alignments();
986 if (!empty($alignments)) {
987 $items = array();
988 foreach ($alignments as $alignment) {
989 $urlaligment = new moodle_url('alignment.php',
990 array('id' => $badge->id, 'alignmentid' => $alignment->id)
992 $items[] = html_writer::link($urlaligment, $alignment->targetname, array('target' => '_blank'));
994 $output .= html_writer::alist($items, array(), 'ul');
995 } else {
996 $output .= get_string('noalignment', 'badges');
998 return $output;
1002 * Renders a table for related badges.
1004 * @param \core_badges\output\badge_related $related list related badges.
1005 * @return string list related badges to output.
1007 protected function render_badge_related(\core_badges\output\badge_related $related) {
1008 $currentbadge = new badge($related->currentbadgeid);
1009 $languages = get_string_manager()->get_list_of_languages();
1010 $paging = new paging_bar($related->totalcount, $related->page, $related->perpage, $this->page->url, 'page');
1011 $htmlpagingbar = $this->render($paging);
1012 $table = new html_table();
1013 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1014 $table->head = array(
1015 get_string('name'),
1016 get_string('version', 'badges'),
1017 get_string('language', 'badges'),
1018 get_string('type', 'badges')
1020 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1021 array_push($table->head, '');
1024 foreach ($related->badges as $badge) {
1025 $badgeobject = new badge($badge->id);
1026 $style = array('title' => $badgeobject->name);
1027 if (!$badgeobject->is_active()) {
1028 $style['class'] = 'dimmed';
1030 $context = ($badgeobject->type == BADGE_TYPE_SITE) ?
1031 context_system::instance() : context_course::instance($badgeobject->courseid);
1032 $forlink = print_badge_image($badgeobject, $context) . ' ' .
1033 html_writer::start_tag('span') . $badgeobject->name . html_writer::end_tag('span');
1034 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $badgeobject->id)), $forlink, $style);
1036 $row = array(
1037 $name,
1038 $badge->version,
1039 $badge->language ? $languages[$badge->language] : '',
1040 $badge->type == BADGE_TYPE_COURSE ? get_string('badgesview', 'badges') : get_string('sitebadges', 'badges')
1042 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1043 $action = $this->output->action_icon(
1044 new moodle_url('/badges/related_action.php', [
1045 'badgeid' => $related->currentbadgeid,
1046 'relatedid' => $badge->id,
1047 'sesskey' => sesskey(),
1048 'action' => 'remove'
1050 new pix_icon('t/delete', get_string('delete')));
1051 $actions = html_writer::tag('div', $action, array('class' => 'badge-actions'));
1052 array_push($row, $actions);
1054 $table->data[] = $row;
1056 $htmltable = html_writer::table($table);
1058 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1062 * Renders a table with alignment.
1064 * @param core_badges\output\badge_alignments $alignments List alignments.
1065 * @return string List alignment to output.
1067 protected function render_badge_alignments(\core_badges\output\badge_alignments $alignments) {
1068 $currentbadge = new badge($alignments->currentbadgeid);
1069 $paging = new paging_bar($alignments->totalcount, $alignments->page, $alignments->perpage, $this->page->url, 'page');
1070 $htmlpagingbar = $this->render($paging);
1071 $table = new html_table();
1072 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1073 $table->head = array('Name', 'URL', '');
1075 foreach ($alignments->alignments as $item) {
1076 $urlaligment = new moodle_url('alignment.php',
1077 array(
1078 'id' => $currentbadge->id,
1079 'alignmentid' => $item->id,
1082 $row = array(
1083 html_writer::link($urlaligment, $item->targetname),
1084 html_writer::link($item->targeturl, $item->targeturl, array('target' => '_blank'))
1086 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1087 $delete = $this->output->action_icon(
1088 new moodle_url('/badges/alignment_action.php', [
1089 'id' => $currentbadge->id,
1090 'alignmentid' => $item->id,
1091 'sesskey' => sesskey(),
1092 'action' => 'remove'
1094 new pix_icon('t/delete', get_string('delete'))
1096 $edit = $this->output->action_icon(
1097 new moodle_url('alignment.php',
1098 array(
1099 'id' => $currentbadge->id,
1100 'alignmentid' => $item->id,
1101 'action' => 'edit'
1103 ), new pix_icon('t/edit', get_string('edit')));
1104 $actions = html_writer::tag('div', $edit . $delete, array('class' => 'badge-actions'));
1105 array_push($row, $actions);
1107 $table->data[] = $row;
1109 $htmltable = html_writer::table($table);
1111 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1115 * Defer to template.
1117 * @param \core_badges\output\external_backpacks_page $page
1118 * @return bool|string
1120 public function render_external_backpacks_page(\core_badges\output\external_backpacks_page $page) {
1121 $data = $page->export_for_template($this);
1122 return parent::render_from_template('core_badges/external_backpacks_page', $data);
1126 * Get the result of a backpack validation with its settings. It returns:
1127 * - A informative message if the backpack version is different from OBv2.
1128 * - A warning with the error if it's not possible to connect to this backpack.
1129 * - A successful message if the connection has worked.
1131 * @param int $backpackid The backpack identifier.
1132 * @return string A message with the validation result.
1134 public function render_test_backpack_result(int $backpackid): string {
1135 // Get the backpack.
1136 $backpack = badges_get_site_backpack($backpackid);
1138 // Add the header to the result.
1139 $result = $this->heading(get_string('testbackpack', 'badges', $backpack->backpackweburl));
1141 if ($backpack->apiversion != OPEN_BADGES_V2) {
1142 // Only OBv2 supports this validation.
1143 $result .= get_string('backpackconnectionnottested', 'badges');
1144 } else {
1145 $message = badges_verify_backpack($backpackid);
1146 if (empty($message)) {
1147 $result .= get_string('backpackconnectionok', 'badges');
1148 } else {
1149 $result .= $message;
1153 return $result;
1157 * Render the tertiary navigation for the page.
1159 * @param \core_badges\output\base_action_bar $actionbar
1160 * @return bool|string
1162 public function render_tertiary_navigation(\core_badges\output\base_action_bar $actionbar) {
1163 return $this->render_from_template($actionbar->get_template(), $actionbar->export_for_template($this));