MDL-72791 customfield: correct access checks for course search data.
[moodle.git] / badges / renderer.php
blob36929f6a7656e06abc0a8588e9a480db64795d74
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', ['src' => $imageurl, 'class' => 'badge-image', 'alt' => $badge->imagecaption]);
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 $data = $ibadge->export_for_template($this);
317 return parent::render_from_template('core_badges/issued_badge', $data);
321 * Render an external badge.
323 * @param \core_badges\output\external_badge $ibadge
324 * @return string
326 protected function render_external_badge(\core_badges\output\external_badge $ibadge) {
327 $issued = $ibadge->issued;
328 $assertion = $issued->assertion;
329 $issuer = $assertion->badge->issuer;
330 $userinfo = $ibadge->recipient;
331 $table = new html_table();
332 $today = strtotime(date('Y-m-d'));
334 $output = '';
335 $output .= html_writer::start_tag('div', array('id' => 'badge'));
336 $output .= html_writer::start_tag('div', array('id' => 'badge-image'));
337 if (isset($issued->imageUrl)) {
338 $issued->image = $issued->imageUrl;
340 $output .= html_writer::empty_tag('img', array('src' => $issued->image, 'width' => '100'));
341 if (isset($assertion->expires)) {
342 $expiration = is_numeric($assertion->expires) ? $assertion->expires : strtotime($assertion->expires);
343 if ($expiration < $today) {
344 $output .= $this->output->pix_icon('i/expired',
345 get_string('expireddate', 'badges', userdate($expiration)),
346 'moodle',
347 array('class' => 'expireimage'));
350 $output .= html_writer::end_tag('div');
352 $output .= html_writer::start_tag('div', array('id' => 'badge-details'));
354 // Recipient information.
355 $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3);
356 $dl = array();
357 // Technically, we should alway have a user at this point, but added an extra check just in case.
358 if ($userinfo) {
359 if (!$ibadge->valid) {
360 $notify = $this->output->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice');
361 $dl[get_string('name')] = fullname($userinfo) . $notify;
362 } else {
363 $dl[get_string('name')] = fullname($userinfo);
365 } else {
366 $notify = $this->output->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice');
367 $dl[get_string('name')] = $notify;
369 $output .= $this->definition_list($dl);
371 $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3);
372 $dl = array();
373 $dl[get_string('issuername', 'badges')] = s($issuer->name);
374 if (isset($issuer->origin)) {
375 $dl[get_string('issuerurl', 'badges')] = html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin));
378 if (isset($issuer->contact)) {
379 $dl[get_string('contact', 'badges')] = obfuscate_mailto($issuer->contact);
381 $output .= $this->definition_list($dl);
383 $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3);
384 $dl = array();
385 $dl[get_string('name')] = s($assertion->badge->name);
386 $dl[get_string('description', 'badges')] = s($assertion->badge->description);
387 if (isset($assertion->badge->criteria)) {
388 $dl[get_string('bcriteria', 'badges')] = html_writer::tag(
389 'a',
390 s($assertion->badge->criteria),
391 array('href' => $assertion->badge->criteria)
394 $output .= $this->definition_list($dl);
396 $dl = array();
397 if (isset($assertion->issued_on)) {
398 $issuedate = is_numeric($assertion->issued_on) ? $assertion->issued_on : strtotime($assertion->issued_on);
399 $dl[get_string('dateawarded', 'badges')] = userdate($issuedate);
401 if (isset($assertion->expires)) {
402 if ($expiration < $today) {
403 $dl[get_string('expirydate', 'badges')] = userdate($expiration) . get_string('warnexpired', 'badges');
404 } else {
405 $dl[get_string('expirydate', 'badges')] = userdate($expiration);
408 if (isset($assertion->evidence)) {
409 $dl[get_string('evidence', 'badges')] = html_writer::tag(
410 'a',
411 s($assertion->evidence),
412 array('href' => $assertion->evidence)
415 if (!empty($dl)) {
416 $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3);
417 $output .= $this->definition_list($dl);
419 $output .= html_writer::end_tag('div');
421 return $output;
425 * Render a collection of user badges.
427 * @param \core_badges\output\badge_user_collection $badges
428 * @return string
430 protected function render_badge_user_collection(\core_badges\output\badge_user_collection $badges) {
431 global $CFG, $USER, $SITE;
432 $backpack = $badges->backpack;
433 $mybackpack = new moodle_url('/badges/mybackpack.php');
435 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
436 $htmlpagingbar = $this->render($paging);
438 // Set backpack connection string.
439 $backpackconnect = '';
440 if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) {
441 $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
443 // Search box.
444 $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
446 // Download all button.
447 $actionhtml = $this->output->single_button(
448 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
449 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
450 $downloadall = $this->output->box('', 'col-md-3');
451 $downloadall .= $this->output->box($actionhtml, 'col-md-9');
452 $downloadall = $this->output->box($downloadall, 'row ml-5');
454 // Local badges.
455 $localhtml = html_writer::start_tag('div', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
456 $sitename = format_string($SITE->fullname, true, array('context' => context_system::instance()));
457 $heading = get_string('localbadges', 'badges', $sitename);
458 $localhtml .= $this->output->heading_with_help($heading, 'localbadgesh', 'badges');
459 if ($badges->badges) {
460 $countmessage = $this->output->box(get_string('badgesearned', 'badges', $badges->totalcount));
462 $htmllist = $this->print_badges_list($badges->badges, $USER->id);
463 $localhtml .= $backpackconnect . $countmessage . $searchform;
464 $localhtml .= $htmlpagingbar . $htmllist . $htmlpagingbar . $downloadall;
465 } else {
466 $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
468 $localhtml .= html_writer::end_tag('div');
470 // External badges.
471 $externalhtml = "";
472 if (!empty($CFG->badges_allowexternalbackpack)) {
473 $externalhtml .= html_writer::start_tag('div', array('class' => 'generalbox'));
474 $externalhtml .= $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges');
475 if (!is_null($backpack)) {
476 if ($backpack->totalcollections == 0) {
477 $externalhtml .= get_string('nobackpackcollectionssummary', 'badges', $backpack);
478 } else {
479 if ($backpack->totalbadges == 0) {
480 $externalhtml .= get_string('nobackpackbadgessummary', 'badges', $backpack);
481 } else {
482 $externalhtml .= get_string('backpackbadgessummary', 'badges', $backpack);
483 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
486 } else {
487 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
490 $externalhtml .= html_writer::end_tag('div');
491 $attr = ['class' => 'btn btn-secondary'];
492 $label = get_string('backpackbadgessettings', 'badges');
493 $backpacksettings = html_writer::link(new moodle_url('/badges/mybackpack.php'), $label, $attr);
494 $actionshtml = $this->output->box('', 'col-md-3');
495 $actionshtml .= $this->output->box($backpacksettings, 'col-md-9');
496 $actionshtml = $this->output->box($actionshtml, 'row ml-5');
497 $externalhtml .= $actionshtml;
500 return $localhtml . $externalhtml;
504 * Render a collection of badges.
506 * @param \core_badges\output\badge_collection $badges
507 * @return string
509 protected function render_badge_collection(\core_badges\output\badge_collection $badges) {
510 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
511 $htmlpagingbar = $this->render($paging);
512 $table = new html_table();
513 $table->attributes['class'] = 'table table-bordered table-striped';
515 $sortbyname = $this->helper_sortable_heading(get_string('name'),
516 'name', $badges->sort, $badges->dir);
517 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
518 'dateissued', $badges->sort, $badges->dir);
519 $table->head = array(
520 get_string('badgeimage', 'badges'),
521 $sortbyname,
522 get_string('description', 'badges'),
523 get_string('bcriteria', 'badges'),
524 $sortbyawarded
526 $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
528 foreach ($badges->badges as $badge) {
529 $badgeimage = print_badge_image($badge, $this->page->context, 'large');
530 $name = $badge->name;
531 $description = $badge->description;
532 $criteria = self::print_badge_criteria($badge);
533 if ($badge->dateissued) {
534 $icon = new pix_icon('i/valid',
535 get_string('dateearned', 'badges',
536 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
537 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
538 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
539 } else {
540 $awarded = "";
542 $row = array($badgeimage, $name, $description, $criteria, $awarded);
543 $table->data[] = $row;
546 $htmltable = html_writer::table($table);
548 return $htmlpagingbar . $htmltable . $htmlpagingbar;
552 * Render a table of badges.
554 * @param \core_badges\output\badge_management $badges
555 * @return string
557 protected function render_badge_management(\core_badges\output\badge_management $badges) {
558 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
560 // New badge button.
561 $htmlnew = '';
562 if (has_capability('moodle/badges:createbadge', $this->page->context)) {
563 $n['type'] = $this->page->url->get_param('type');
564 $n['id'] = $this->page->url->get_param('id');
565 $btn = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
566 $htmlnew = $this->output->box($btn);
569 $htmlpagingbar = $this->render($paging);
570 $table = new html_table();
571 $table->attributes['class'] = 'table table-bordered table-striped';
573 $sortbyname = $this->helper_sortable_heading(get_string('name'),
574 'name', $badges->sort, $badges->dir);
575 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
576 'status', $badges->sort, $badges->dir);
577 $table->head = array(
578 $sortbyname,
579 $sortbystatus,
580 get_string('bcriteria', 'badges'),
581 get_string('awards', 'badges'),
582 get_string('actions')
584 $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
586 foreach ($badges->badges as $b) {
587 $style = !$b->is_active() ? array('class' => 'dimmed') : array();
588 $forlink = print_badge_image($b, $this->page->context) . ' ' .
589 html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
590 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
591 $status = $b->statstring;
592 $criteria = self::print_badge_criteria($b, 'short');
594 if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
595 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
596 } else {
597 $awards = $b->awards;
600 $actions = self::print_badge_table_actions($b, $this->page->context);
602 $row = array($name, $status, $criteria, $awards, $actions);
603 $table->data[] = $row;
605 $htmltable = html_writer::table($table);
607 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
611 * Prints tabs for badge editing.
613 * @param integer $badgeid The badgeid to edit.
614 * @param context $context The current context.
615 * @param string $current The currently selected tab.
616 * @return string
618 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
619 global $DB;
621 $badge = new badge($badgeid);
622 $row = array();
624 $row[] = new tabobject('overview',
625 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
626 get_string('boverview', 'badges')
629 if (has_capability('moodle/badges:configuredetails', $context)) {
630 $row[] = new tabobject('badge',
631 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'badge')),
632 get_string('bdetails', 'badges')
636 if (has_capability('moodle/badges:configurecriteria', $context)) {
637 $row[] = new tabobject('criteria',
638 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
639 get_string('bcriteria', 'badges')
643 if (has_capability('moodle/badges:configuremessages', $context)) {
644 $row[] = new tabobject('message',
645 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
646 get_string('bmessage', 'badges')
650 if (has_capability('moodle/badges:viewawarded', $context)) {
651 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
652 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
653 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
654 $row[] = new tabobject('awards',
655 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
656 get_string('bawards', 'badges', $awarded)
660 if (has_capability('moodle/badges:configuredetails', $context)) {
661 $row[] = new tabobject('bendorsement',
662 new moodle_url('/badges/endorsement.php', array('id' => $badgeid)),
663 get_string('bendorsement', 'badges')
667 if (has_capability('moodle/badges:configuredetails', $context)) {
668 $sql = "SELECT COUNT(br.badgeid)
669 FROM {badge_related} br
670 WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
671 $related = $DB->count_records_sql($sql, ['badgeid' => $badgeid, 'badgeid2' => $badgeid]);
672 $row[] = new tabobject('brelated',
673 new moodle_url('/badges/related.php', array('id' => $badgeid)),
674 get_string('brelated', 'badges', $related)
678 if (has_capability('moodle/badges:configuredetails', $context)) {
679 $alignments = $DB->count_records_sql("SELECT COUNT(bc.id)
680 FROM {badge_alignment} bc WHERE bc.badgeid = :badgeid", array('badgeid' => $badgeid));
681 $row[] = new tabobject('alignment',
682 new moodle_url('/badges/alignment.php', array('id' => $badgeid)),
683 get_string('balignment', 'badges', $alignments)
687 echo $this->tabtree($row, $current);
691 * Prints badge status box.
693 * @param badge $badge
694 * @return Either the status box html as a string or null
696 public function print_badge_status_box(badge $badge) {
697 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
699 if (!$badge->has_criteria()) {
700 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
701 $status = get_string('nocriteria', 'badges');
702 if ($this->page->url != $criteriaurl) {
703 $action = $this->output->single_button(
704 $criteriaurl,
705 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
706 } else {
707 $action = '';
710 $message = $status . $action;
711 } else {
712 $status = get_string('statusmessage_' . $badge->status, 'badges');
713 if ($badge->is_active()) {
714 $action = $this->output->single_button(new moodle_url('/badges/action.php',
715 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
716 'return' => $this->page->url->out_as_local_url(false))),
717 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
718 } else {
719 $action = $this->output->single_button(new moodle_url('/badges/action.php',
720 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
721 'return' => $this->page->url->out_as_local_url(false))),
722 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
725 $message = $status . $this->output->help_icon('status', 'badges') . $action;
729 $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
730 return $this->output->box($message, $style);
733 return null;
737 * Returns information about badge criteria in a list form.
739 * @param badge $badge Badge objects
740 * @param string $short Indicates whether to print full info about this badge
741 * @return string $output HTML string to output
743 public function print_badge_criteria(badge $badge, $short = '') {
744 $agg = $badge->get_aggregation_methods();
745 if (empty($badge->criteria)) {
746 return get_string('nocriteria', 'badges');
749 $overalldescr = '';
750 $overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL];
751 if (!$short && !empty($overall->description)) {
752 $overalldescr = $this->output->box(
753 format_text($overall->description, $overall->descriptionformat, array('context' => $badge->get_context())),
754 'criteria-description'
758 // Get the condition string.
759 $condition = '';
760 if (count($badge->criteria) != 2) {
761 $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
762 core_text::strtoupper($agg[$badge->get_aggregation_method()]));
765 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
767 $items = array();
768 // If only one criterion left, make sure its description goe to the top.
769 if (count($badge->criteria) == 1) {
770 $c = reset($badge->criteria);
771 if (!$short && !empty($c->description)) {
772 $overalldescr = $this->output->box(
773 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
774 'criteria-description'
777 if (count($c->params) == 1) {
778 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype , 'badges') .
779 $c->get_details($short);
780 } else {
781 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype, 'badges',
782 core_text::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype)])) .
783 $c->get_details($short);
785 } else {
786 foreach ($badge->criteria as $type => $c) {
787 $criteriadescr = '';
788 if (!$short && !empty($c->description)) {
789 $criteriadescr = $this->output->box(
790 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
791 'criteria-description'
794 if (count($c->params) == 1) {
795 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
796 $c->get_details($short) . $criteriadescr;
797 } else {
798 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
799 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) .
800 $c->get_details($short) .
801 $criteriadescr;
806 return $overalldescr . $condition . html_writer::alist($items, array(), 'ul');;
810 * Prints criteria actions for badge editing.
812 * @param badge $badge
813 * @return string
815 public function print_criteria_actions(badge $badge) {
816 $output = '';
817 if (!$badge->is_active() && !$badge->is_locked()) {
818 $accepted = $badge->get_accepted_criteria();
819 $potential = array_diff($accepted, array_keys($badge->criteria));
821 if (!empty($potential)) {
822 foreach ($potential as $p) {
823 if ($p != 0) {
824 $select[$p] = get_string('criteria_' . $p, 'badges');
827 $output .= $this->output->single_select(
828 new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id, 'add' => true)),
829 'type',
830 $select,
832 array('' => 'choosedots'),
833 null,
834 array('label' => get_string('addbadgecriteria', 'badges'))
836 } else {
837 $output .= $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
841 return $output;
845 * Renders a table with users who have earned the badge.
846 * Based on stamps collection plugin.
848 * @param \core_badges\output\badge_recipients $recipients
849 * @return string
851 protected function render_badge_recipients(\core_badges\output\badge_recipients $recipients) {
852 $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
853 $htmlpagingbar = $this->render($paging);
854 $table = new html_table();
855 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
857 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
858 'firstname', $recipients->sort, $recipients->dir);
859 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
860 'lastname', $recipients->sort, $recipients->dir);
861 if ($this->helper_fullname_format() == 'lf') {
862 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
863 } else {
864 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
867 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
868 'dateissued', $recipients->sort, $recipients->dir);
870 $table->head = array($sortbyname, $sortbydate, '');
872 foreach ($recipients->userids as $holder) {
873 $fullname = fullname($holder);
874 $fullname = html_writer::link(
875 new moodle_url('/user/profile.php', array('id' => $holder->userid)),
876 $fullname
878 $awarded = userdate($holder->dateissued);
879 $badgeurl = html_writer::link(
880 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
881 get_string('viewbadge', 'badges')
884 $row = array($fullname, $awarded, $badgeurl);
885 $table->data[] = $row;
888 $htmltable = html_writer::table($table);
890 return $htmlpagingbar . $htmltable . $htmlpagingbar;
893 ////////////////////////////////////////////////////////////////////////////
894 // Helper methods
895 // Reused from stamps collection plugin
896 ////////////////////////////////////////////////////////////////////////////
899 * Renders a text with icons to sort by the given column
901 * This is intended for table headings.
903 * @param string $text The heading text
904 * @param string $sortid The column id used for sorting
905 * @param string $sortby Currently sorted by (column id)
906 * @param string $sorthow Currently sorted how (ASC|DESC)
908 * @return string
910 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
911 $out = html_writer::tag('span', $text, array('class' => 'text'));
913 if (!is_null($sortid)) {
914 if ($sortby !== $sortid || $sorthow !== 'ASC') {
915 $url = new moodle_url($this->page->url);
916 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
917 $out .= $this->output->action_icon($url,
918 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
920 if ($sortby !== $sortid || $sorthow !== 'DESC') {
921 $url = new moodle_url($this->page->url);
922 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
923 $out .= $this->output->action_icon($url,
924 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
927 return $out;
930 * Tries to guess the fullname format set at the site
932 * @return string fl|lf
934 protected function helper_fullname_format() {
935 $fake = new stdClass();
936 $fake->lastname = 'LLLL';
937 $fake->firstname = 'FFFF';
938 $fullname = get_string('fullnamedisplay', '', $fake);
939 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
940 return 'lf';
941 } else {
942 return 'fl';
946 * Renders a search form
948 * @param string $search Search string
949 * @return string HTML
951 protected function helper_search_form($search) {
952 global $CFG;
953 require_once($CFG->libdir . '/formslib.php');
955 $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
957 $mform->addElement('hidden', 'sesskey', sesskey());
959 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
960 $mform->setDefault('search', $search);
961 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
962 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
963 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
965 ob_start();
966 $mform->display();
967 $out = ob_get_clean();
969 return $out;
973 * Renders a definition list
975 * @param array $items the list of items to define
976 * @param array
978 protected function definition_list(array $items, array $attributes = array()) {
979 $output = html_writer::start_tag('dl', $attributes);
980 foreach ($items as $label => $value) {
981 $output .= html_writer::tag('dt', $label);
982 $output .= html_writer::tag('dd', $value);
984 $output .= html_writer::end_tag('dl');
985 return $output;
989 * Outputs list en badges.
991 * @param badge $badge Badge object.
992 * @return string $output content endorsement to output.
994 protected function print_badge_endorsement(badge $badge) {
995 $output = '';
996 $endorsement = $badge->get_endorsement();
997 $dl = array();
998 $output .= $this->heading(get_string('endorsement', 'badges'), 3);
999 if (!empty($endorsement)) {
1000 $dl[get_string('issuername', 'badges')] = $endorsement->issuername;
1001 $dl[get_string('issueremail', 'badges')] =
1002 html_writer::tag('a', $endorsement->issueremail, array('href' => 'mailto:' . $endorsement->issueremail));
1003 $dl[get_string('issuerurl', 'badges')] = html_writer::link($endorsement->issuerurl, $endorsement->issuerurl,
1004 array('target' => '_blank'));
1005 $dl[get_string('dateawarded', 'badges')] = userdate($endorsement->dateissued);
1006 $dl[get_string('claimid', 'badges')] = html_writer::link($endorsement->claimid, $endorsement->claimid,
1007 array('target' => '_blank'));
1008 $dl[get_string('claimcomment', 'badges')] = $endorsement->claimcomment;
1009 $output .= $this->definition_list($dl);
1010 } else {
1011 $output .= get_string('noendorsement', 'badges');
1013 return $output;
1017 * Print list badges related.
1019 * @param badge $badge Badge objects.
1020 * @return string $output List related badges to output.
1022 protected function print_badge_related(badge $badge) {
1023 $output = '';
1024 $relatedbadges = $badge->get_related_badges();
1025 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
1026 if (!empty($relatedbadges)) {
1027 $items = array();
1028 foreach ($relatedbadges as $related) {
1029 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id));
1030 $items[] = html_writer::link($relatedurl->out(), $related->name, array('target' => '_blank'));
1032 $output .= html_writer::alist($items, array(), 'ul');
1033 } else {
1034 $output .= get_string('norelated', 'badges');
1036 return $output;
1040 * Print list badge alignments.
1042 * @param badge $badge Badge objects.
1043 * @return string $output List alignments to output.
1045 protected function print_badge_alignments(badge $badge) {
1046 $output = '';
1047 $output .= $this->heading(get_string('alignment', 'badges'), 3);
1048 $alignments = $badge->get_alignments();
1049 if (!empty($alignments)) {
1050 $items = array();
1051 foreach ($alignments as $alignment) {
1052 $urlaligment = new moodle_url('alignment.php',
1053 array('id' => $badge->id, 'alignmentid' => $alignment->id)
1055 $items[] = html_writer::link($urlaligment, $alignment->targetname, array('target' => '_blank'));
1057 $output .= html_writer::alist($items, array(), 'ul');
1058 } else {
1059 $output .= get_string('noalignment', 'badges');
1061 return $output;
1065 * Renders a table for related badges.
1067 * @param \core_badges\output\badge_related $related list related badges.
1068 * @return string list related badges to output.
1070 protected function render_badge_related(\core_badges\output\badge_related $related) {
1071 $currentbadge = new badge($related->currentbadgeid);
1072 $languages = get_string_manager()->get_list_of_languages();
1073 $paging = new paging_bar($related->totalcount, $related->page, $related->perpage, $this->page->url, 'page');
1074 $htmlpagingbar = $this->render($paging);
1075 $table = new html_table();
1076 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1077 $table->head = array(
1078 get_string('name'),
1079 get_string('version', 'badges'),
1080 get_string('language', 'badges'),
1081 get_string('type', 'badges')
1083 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1084 array_push($table->head, '');
1087 foreach ($related->badges as $badge) {
1088 $badgeobject = new badge($badge->id);
1089 $style = array('title' => $badgeobject->name);
1090 if (!$badgeobject->is_active()) {
1091 $style['class'] = 'dimmed';
1093 $context = ($badgeobject->type == BADGE_TYPE_SITE) ?
1094 context_system::instance() : context_course::instance($badgeobject->courseid);
1095 $forlink = print_badge_image($badgeobject, $context) . ' ' .
1096 html_writer::start_tag('span') . $badgeobject->name . html_writer::end_tag('span');
1097 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $badgeobject->id)), $forlink, $style);
1099 $row = array(
1100 $name,
1101 $badge->version,
1102 $badge->language ? $languages[$badge->language] : '',
1103 $badge->type == BADGE_TYPE_COURSE ? get_string('badgesview', 'badges') : get_string('sitebadges', 'badges')
1105 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1106 $action = $this->output->action_icon(
1107 new moodle_url('/badges/related_action.php', [
1108 'badgeid' => $related->currentbadgeid,
1109 'relatedid' => $badge->id,
1110 'sesskey' => sesskey(),
1111 'action' => 'remove'
1113 new pix_icon('t/delete', get_string('delete')));
1114 $actions = html_writer::tag('div', $action, array('class' => 'badge-actions'));
1115 array_push($row, $actions);
1117 $table->data[] = $row;
1119 $htmltable = html_writer::table($table);
1121 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1125 * Renders a table with alignment.
1127 * @param core_badges\output\badge_alignments $alignments List alignments.
1128 * @return string List alignment to output.
1130 protected function render_badge_alignments(\core_badges\output\badge_alignments $alignments) {
1131 $currentbadge = new badge($alignments->currentbadgeid);
1132 $paging = new paging_bar($alignments->totalcount, $alignments->page, $alignments->perpage, $this->page->url, 'page');
1133 $htmlpagingbar = $this->render($paging);
1134 $table = new html_table();
1135 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1136 $table->head = array('Name', 'URL', '');
1138 foreach ($alignments->alignments as $item) {
1139 $urlaligment = new moodle_url('alignment.php',
1140 array(
1141 'id' => $currentbadge->id,
1142 'alignmentid' => $item->id,
1145 $row = array(
1146 html_writer::link($urlaligment, $item->targetname),
1147 html_writer::link($item->targeturl, $item->targeturl, array('target' => '_blank'))
1149 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1150 $delete = $this->output->action_icon(
1151 new moodle_url('alignment_action.php',
1152 array(
1153 'id' => $currentbadge->id,
1154 'alignmentid' => $item->id,
1155 'action' => 'remove'
1157 ), new pix_icon('t/delete', get_string('delete')));
1158 $edit = $this->output->action_icon(
1159 new moodle_url('alignment.php',
1160 array(
1161 'id' => $currentbadge->id,
1162 'alignmentid' => $item->id,
1163 'action' => 'edit'
1165 ), new pix_icon('t/edit', get_string('edit')));
1166 $actions = html_writer::tag('div', $edit . $delete, array('class' => 'badge-actions'));
1167 array_push($row, $actions);
1169 $table->data[] = $row;
1171 $htmltable = html_writer::table($table);
1173 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1177 * Defer to template.
1179 * @param \core_badges\output\external_backpacks_page $page
1180 * @return bool|string
1182 public function render_external_backpacks_page(\core_badges\output\external_backpacks_page $page) {
1183 $data = $page->export_for_template($this);
1184 return parent::render_from_template('core_badges/external_backpacks_page', $data);
1188 * Get the result of a backpack validation with its settings. It returns:
1189 * - A informative message if the backpack version is different from OBv2.
1190 * - A warning with the error if it's not possible to connect to this backpack.
1191 * - A successful message if the connection has worked.
1193 * @param int $backpackid The backpack identifier.
1194 * @return string A message with the validation result.
1196 public function render_test_backpack_result(int $backpackid): string {
1197 // Get the backpack.
1198 $backpack = badges_get_site_backpack($backpackid);
1200 // Add the header to the result.
1201 $result = $this->heading(get_string('testbackpack', 'badges', $backpack->backpackweburl));
1203 if ($backpack->apiversion != OPEN_BADGES_V2) {
1204 // Only OBv2 supports this validation.
1205 $result .= get_string('backpackconnectionnottested', 'badges');
1206 } else {
1207 $message = badges_verify_backpack($backpackid);
1208 if (empty($message)) {
1209 $result .= get_string('backpackconnectionok', 'badges');
1210 } else {
1211 $result .= $message;
1215 return $result;