2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Renderer for use with the badges output
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');
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) {
40 foreach ($badges as $badge) {
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);
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
;
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
)),
77 array('class' => 'expireimage'));
78 $name .= '(' . get_string('expired', 'badges') . ')';
81 $download = $status = $push = '';
82 if (($userid == $USER->id
) && !$profile) {
84 'download' => $badge->id
,
85 'hash' => $badge->uniquehash
,
86 'sesskey' => sesskey()
88 $url = new moodle_url(
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')));
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')));
117 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash
));
120 $url = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash
));
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) {
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(
151 'value' => $this->output
->larrow() . ' ' . get_string('award', 'badges'),
152 'class' => 'actionbutton btn btn-secondary')
154 $actioncell->text
.= html_writer
::empty_tag('input', array(
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');
175 // Prints a badge overview infomation.
176 public function print_badge_overview($badge, $context) {
178 $languages = get_string_manager()->get_list_of_languages();
181 $display .= $this->heading(get_string('badgedetails', 'badges'), 3);
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);
200 $display .= $this->heading(get_string('issuerdetails', 'badges'), 3);
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));
219 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod
/ 60 / 60 / 24, 2));
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);
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
));
245 $a->link
= $url->out();
246 $a->count
= count($badge->get_awards());
247 $display .= get_string('numawards', 'badges', $a);
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
276 public function print_badge_table_actions($badge, $context) {
277 debugging("print_badge_table_actions() is deprecated.", DEBUG_DEVELOPER
);
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'))) . " ";
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'))) . " ";
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'))) . " ";
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'))) . " ";
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'))) . " ";
330 * Render an issued badge.
332 * @param \core_badges\output\issued_badge $ibadge
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
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
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
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');
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');
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;
404 $localhtml .= $searchform . $this->output
->notification(get_string('nobadges', 'badges'), 'info');
406 $localhtml .= html_writer
::end_tag('div');
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);
417 if ($backpack->totalbadges
== 0) {
418 $externalhtml .= get_string('nobackpackbadgessummary', 'badges', $backpack);
420 $externalhtml .= get_string('backpackbadgessummary', 'badges', $backpack);
421 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges
, $USER->id
, true, true);
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
447 * @deprecated since Moodle 4.4
448 * @todo MDL-80455 this will be removed in Moodle 4.8
450 protected function render_badge_collection(\core_badges\output\badge_collection
$badges) {
451 debugging('The method render_badge_collection() has been deprecated', DEBUG_DEVELOPER
);
452 $paging = new paging_bar($badges->totalcount
, $badges->page
, $badges->perpage
, $this->page
->url
, 'page');
453 $htmlpagingbar = $this->render($paging);
454 $table = new html_table();
455 $table->attributes
['class'] = 'table table-bordered table-striped';
457 $sortbyname = $this->helper_sortable_heading(get_string('name'),
458 'name', $badges->sort
, $badges->dir
);
459 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
460 'dateissued', $badges->sort
, $badges->dir
);
461 $table->head
= array(
462 get_string('badgeimage', 'badges'),
464 get_string('description', 'badges'),
465 get_string('bcriteria', 'badges'),
468 $table->colclasses
= array('badgeimage', 'name', 'description', 'criteria', 'awards');
470 foreach ($badges->badges
as $badge) {
471 $badgeimage = print_badge_image($badge, $this->page
->context
, 'large');
472 $name = $badge->name
;
473 $description = $badge->description
;
474 $criteria = self
::print_badge_criteria($badge);
475 if ($badge->dateissued
) {
476 $icon = new pix_icon('i/valid',
477 get_string('dateearned', 'badges',
478 userdate($badge->dateissued
, get_string('strftimedatefullshort', 'core_langconfig'))));
479 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash
));
480 $awarded = $this->output
->action_icon($badgeurl, $icon, null, null, true);
484 $row = array($badgeimage, $name, $description, $criteria, $awarded);
485 $table->data
[] = $row;
488 $htmltable = html_writer
::table($table);
490 return $htmlpagingbar . $htmltable . $htmlpagingbar;
494 * Render a table of badges.
496 * @deprecated since Moodle 4.3
497 * @param \core_badges\output\badge_management $badges
500 protected function render_badge_management(\core_badges\output\badge_management
$badges) {
501 debugging("render_badge_management() is deprecated.", DEBUG_DEVELOPER
);
502 $paging = new paging_bar($badges->totalcount
, $badges->page
, $badges->perpage
, $this->page
->url
, 'page');
506 $htmlpagingbar = $this->render($paging);
507 $table = new html_table();
508 $table->attributes
['class'] = 'table table-bordered table-striped';
510 $sortbyname = $this->helper_sortable_heading(get_string('name'),
511 'name', $badges->sort
, $badges->dir
);
512 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
513 'status', $badges->sort
, $badges->dir
);
514 $table->head
= array(
517 get_string('bcriteria', 'badges'),
518 get_string('awards', 'badges'),
519 get_string('actions')
521 $table->colclasses
= array('name', 'status', 'criteria', 'awards', 'actions');
523 foreach ($badges->badges
as $b) {
524 $style = !$b->is_active() ?
array('class' => 'dimmed') : array();
525 $forlink = print_badge_image($b, $this->page
->context
) . ' ' .
526 html_writer
::start_tag('span') . $b->name
. html_writer
::end_tag('span');
527 $name = html_writer
::link(new moodle_url('/badges/overview.php', array('id' => $b->id
)), $forlink, $style);
528 $status = $b->statstring
;
529 $criteria = self
::print_badge_criteria($b, 'short');
531 if (has_capability('moodle/badges:viewawarded', $this->page
->context
)) {
532 $awards = html_writer
::link(new moodle_url('/badges/recipients.php', array('id' => $b->id
)), $b->awards
);
534 $awards = $b->awards
;
537 $actions = self
::print_badge_table_actions($b, $this->page
->context
);
539 $row = array($name, $status, $criteria, $awards, $actions);
540 $table->data
[] = $row;
542 $htmltable = html_writer
::table($table);
544 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
548 * Prints tabs for badge editing.
550 * @deprecated since Moodle 4.0
551 * @todo MDL-73426 Final deprecation.
552 * @param integer $badgeid The badgeid to edit.
553 * @param context $context The current context.
554 * @param string $current The currently selected tab.
557 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
559 debugging("print_badge_tabs() is deprecated. " .
560 "This is replaced with the manage_badge_action_bar tertiary navigation.", DEBUG_DEVELOPER
);
562 $badge = new badge($badgeid);
565 $row[] = new tabobject('overview',
566 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
567 get_string('boverview', 'badges')
570 if (has_capability('moodle/badges:configuredetails', $context)) {
571 $row[] = new tabobject('badge',
572 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'badge')),
573 get_string('bdetails', 'badges')
577 if (has_capability('moodle/badges:configurecriteria', $context)) {
578 $row[] = new tabobject('criteria',
579 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
580 get_string('bcriteria', 'badges')
584 if (has_capability('moodle/badges:configuremessages', $context)) {
585 $row[] = new tabobject('message',
586 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
587 get_string('bmessage', 'badges')
591 if (has_capability('moodle/badges:viewawarded', $context)) {
592 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
593 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
594 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
595 $row[] = new tabobject('awards',
596 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
597 get_string('bawards', 'badges', $awarded)
601 if (has_capability('moodle/badges:configuredetails', $context)) {
602 $row[] = new tabobject('bendorsement',
603 new moodle_url('/badges/endorsement.php', array('id' => $badgeid)),
604 get_string('bendorsement', 'badges')
608 if (has_capability('moodle/badges:configuredetails', $context)) {
609 $sql = "SELECT COUNT(br.badgeid)
610 FROM {badge_related} br
611 WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
612 $related = $DB->count_records_sql($sql, ['badgeid' => $badgeid, 'badgeid2' => $badgeid]);
613 $row[] = new tabobject('brelated',
614 new moodle_url('/badges/related.php', array('id' => $badgeid)),
615 get_string('brelated', 'badges', $related)
619 if (has_capability('moodle/badges:configuredetails', $context)) {
620 $alignments = $DB->count_records_sql("SELECT COUNT(bc.id)
621 FROM {badge_alignment} bc WHERE bc.badgeid = :badgeid", array('badgeid' => $badgeid));
622 $row[] = new tabobject('alignment',
623 new moodle_url('/badges/alignment.php', array('id' => $badgeid)),
624 get_string('balignment', 'badges', $alignments)
628 echo $this->tabtree($row, $current);
632 * Prints badge status box.
634 * @param badge $badge
635 * @return Either the status box html as a string or null
637 public function print_badge_status_box(badge
$badge) {
638 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
640 if (!$badge->has_criteria()) {
641 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id
));
642 $status = get_string('nocriteria', 'badges');
643 if ($this->page
->url
!= $criteriaurl) {
644 $action = $this->output
->single_button(
646 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
651 $message = $status . $action;
653 $status = get_string('statusmessage_' . $badge->status
, 'badges');
654 if ($badge->is_active()) {
655 $action = $this->output
->single_button(new moodle_url('/badges/action.php',
656 array('id' => $badge->id
, 'lock' => 1, 'sesskey' => sesskey(),
657 'return' => $this->page
->url
->out_as_local_url(false))),
658 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
660 $action = $this->output
->single_button(new moodle_url('/badges/action.php',
661 array('id' => $badge->id
, 'activate' => 1, 'sesskey' => sesskey(),
662 'return' => $this->page
->url
->out_as_local_url(false))),
663 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
666 $message = $status . $this->output
->help_icon('status', 'badges') . $action;
670 $style = $badge->is_active() ?
'generalbox statusbox active' : 'generalbox statusbox inactive';
671 return $this->output
->box($message, $style);
678 * Returns information about badge criteria in a list form.
680 * @param badge $badge Badge objects
681 * @param string $short Indicates whether to print full info about this badge
682 * @return string $output HTML string to output
684 public function print_badge_criteria(badge
$badge, $short = '') {
685 $agg = $badge->get_aggregation_methods();
686 if (empty($badge->criteria
)) {
687 return get_string('nocriteria', 'badges');
691 $overall = $badge->criteria
[BADGE_CRITERIA_TYPE_OVERALL
];
692 if (!$short && !empty($overall->description
)) {
693 $overalldescr = $this->output
->box(
694 format_text($overall->description
, $overall->descriptionformat
, array('context' => $badge->get_context())),
695 'criteria-description'
699 // Get the condition string.
701 if (count($badge->criteria
) != 2) {
702 $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL
, 'badges',
703 core_text
::strtoupper($agg[$badge->get_aggregation_method()]));
706 unset($badge->criteria
[BADGE_CRITERIA_TYPE_OVERALL
]);
709 // If only one criterion left, make sure its description goe to the top.
710 if (count($badge->criteria
) == 1) {
711 $c = reset($badge->criteria
);
712 if (!$short && !empty($c->description
)) {
713 $overalldescr = $this->output
->box(
714 format_text($c->description
, $c->descriptionformat
, array('context' => $badge->get_context())),
715 'criteria-description'
718 if (count($c->params
) == 1) {
719 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype
, 'badges') .
720 $c->get_details($short);
722 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype
, 'badges',
723 core_text
::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype
)])) .
724 $c->get_details($short);
727 foreach ($badge->criteria
as $type => $c) {
729 if (!$short && !empty($c->description
)) {
730 $criteriadescr = $this->output
->box(
731 format_text($c->description
, $c->descriptionformat
, array('context' => $badge->get_context())),
732 'criteria-description'
735 if (count($c->params
) == 1) {
736 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
737 $c->get_details($short) . $criteriadescr;
739 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
740 core_text
::strtoupper($agg[$badge->get_aggregation_method($type)])) .
741 $c->get_details($short) .
747 return $overalldescr . $condition . html_writer
::alist($items, array(), 'ul');;
751 * Prints criteria actions for badge editing.
753 * @param badge $badge
756 public function print_criteria_actions(badge
$badge) {
758 if (!$badge->is_active() && !$badge->is_locked()) {
759 $accepted = $badge->get_accepted_criteria();
760 $potential = array_diff($accepted, array_keys($badge->criteria
));
762 if (!empty($potential)) {
763 foreach ($potential as $p) {
765 $select[$p] = get_string('criteria_' . $p, 'badges');
768 $output .= $this->output
->single_select(
769 new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id
, 'add' => true)),
773 array('' => 'choosedots'),
775 array('label' => get_string('addbadgecriteria', 'badges'))
778 $output .= $this->output
->box(get_string('nothingtoadd', 'badges'), 'clearfix');
786 * Renders a table with users who have earned the badge.
787 * Based on stamps collection plugin.
789 * @param \core_badges\output\badge_recipients $recipients
792 * @deprecated since Moodle 4.4
793 * @todo MDL-80455 this will be removed in Moodle 4.8
795 protected function render_badge_recipients(\core_badges\output\badge_recipients
$recipients) {
796 debugging('The method render_badge_recipients() has been deprecated', DEBUG_DEVELOPER
);
797 $paging = new paging_bar($recipients->totalcount
, $recipients->page
, $recipients->perpage
, $this->page
->url
, 'page');
798 $htmlpagingbar = $this->render($paging);
799 $table = new html_table();
800 $table->attributes
['class'] = 'generaltable boxaligncenter boxwidthwide';
802 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
803 'firstname', $recipients->sort
, $recipients->dir
);
804 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
805 'lastname', $recipients->sort
, $recipients->dir
);
806 if ($this->helper_fullname_format() == 'lf') {
807 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
809 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
812 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
813 'dateissued', $recipients->sort
, $recipients->dir
);
815 $table->head
= array($sortbyname, $sortbydate, '');
817 foreach ($recipients->userids
as $holder) {
818 $fullname = fullname($holder);
819 $fullname = html_writer
::link(
820 new moodle_url('/user/profile.php', array('id' => $holder->userid
)),
823 $awarded = userdate($holder->dateissued
);
824 $badgeurl = html_writer
::link(
825 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash
)),
826 get_string('viewbadge', 'badges')
829 $row = array($fullname, $awarded, $badgeurl);
830 $table->data
[] = $row;
833 $htmltable = html_writer
::table($table);
835 return $htmlpagingbar . $htmltable . $htmlpagingbar;
838 ////////////////////////////////////////////////////////////////////////////
840 // Reused from stamps collection plugin
841 ////////////////////////////////////////////////////////////////////////////
844 * Renders a text with icons to sort by the given column
846 * This is intended for table headings.
848 * @param string $text The heading text
849 * @param string $sortid The column id used for sorting
850 * @param string $sortby Currently sorted by (column id)
851 * @param string $sorthow Currently sorted how (ASC|DESC)
855 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
856 $out = html_writer
::tag('span', $text, array('class' => 'text'));
858 if (!is_null($sortid)) {
859 if ($sortby !== $sortid ||
$sorthow !== 'ASC') {
860 $url = new moodle_url($this->page
->url
);
861 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
862 $out .= $this->output
->action_icon($url,
863 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
865 if ($sortby !== $sortid ||
$sorthow !== 'DESC') {
866 $url = new moodle_url($this->page
->url
);
867 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
868 $out .= $this->output
->action_icon($url,
869 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
875 * Tries to guess the fullname format set at the site
877 * @return string fl|lf
879 protected function helper_fullname_format() {
880 $fake = new stdClass();
881 $fake->lastname
= 'LLLL';
882 $fake->firstname
= 'FFFF';
883 $fullname = get_string('fullnamedisplay', '', $fake);
884 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
891 * Renders a search form
893 * @param string $search Search string
894 * @return string HTML
896 protected function helper_search_form($search) {
898 require_once($CFG->libdir
. '/formslib.php');
900 $mform = new MoodleQuickForm('searchform', 'POST', $this->page
->url
);
902 $mform->addElement('hidden', 'sesskey', sesskey());
904 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
905 $mform->setDefault('search', $search);
906 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
907 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
908 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
912 $out = ob_get_clean();
918 * Renders a definition list
920 * @param array $items the list of items to define
923 protected function definition_list(array $items, array $attributes = array()) {
924 $output = html_writer
::start_tag('dl', $attributes);
925 foreach ($items as $label => $value) {
926 $output .= html_writer
::tag('dt', $label);
927 $output .= html_writer
::tag('dd', $value);
929 $output .= html_writer
::end_tag('dl');
934 * Outputs list en badges.
936 * @param badge $badge Badge object.
937 * @return string $output content endorsement to output.
939 protected function print_badge_endorsement(badge
$badge) {
941 $endorsement = $badge->get_endorsement();
943 $output .= $this->heading(get_string('endorsement', 'badges'), 3);
944 if (!empty($endorsement)) {
945 $dl[get_string('issuername', 'badges')] = $endorsement->issuername
;
946 $dl[get_string('issueremail', 'badges')] =
947 html_writer
::tag('a', $endorsement->issueremail
, array('href' => 'mailto:' . $endorsement->issueremail
));
948 $dl[get_string('issuerurl', 'badges')] = html_writer
::link($endorsement->issuerurl
, $endorsement->issuerurl
,
949 array('target' => '_blank'));
950 $dl[get_string('dateawarded', 'badges')] = userdate($endorsement->dateissued
);
951 $dl[get_string('claimid', 'badges')] = html_writer
::link($endorsement->claimid
, $endorsement->claimid
,
952 array('target' => '_blank'));
953 $dl[get_string('claimcomment', 'badges')] = $endorsement->claimcomment
;
954 $output .= $this->definition_list($dl);
956 $output .= get_string('noendorsement', 'badges');
962 * Print list badges related.
964 * @param badge $badge Badge objects.
965 * @return string $output List related badges to output.
967 protected function print_badge_related(badge
$badge) {
969 $relatedbadges = $badge->get_related_badges();
970 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
971 if (!empty($relatedbadges)) {
973 foreach ($relatedbadges as $related) {
974 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id
));
975 $items[] = html_writer
::link($relatedurl->out(), $related->name
, array('target' => '_blank'));
977 $output .= html_writer
::alist($items, array(), 'ul');
979 $output .= get_string('norelated', 'badges');
985 * Print list badge alignments.
987 * @param badge $badge Badge objects.
988 * @return string $output List alignments to output.
990 protected function print_badge_alignments(badge
$badge) {
992 $output .= $this->heading(get_string('alignment', 'badges'), 3);
993 $alignments = $badge->get_alignments();
994 if (!empty($alignments)) {
996 foreach ($alignments as $alignment) {
997 $urlaligment = new moodle_url('alignment.php',
998 array('id' => $badge->id
, 'alignmentid' => $alignment->id
)
1000 $items[] = html_writer
::link($urlaligment, $alignment->targetname
, array('target' => '_blank'));
1002 $output .= html_writer
::alist($items, array(), 'ul');
1004 $output .= get_string('noalignment', 'badges');
1010 * Renders a table for related badges.
1012 * @param \core_badges\output\badge_related $related list related badges.
1013 * @return string list related badges to output.
1015 protected function render_badge_related(\core_badges\output\badge_related
$related) {
1016 $currentbadge = new badge($related->currentbadgeid
);
1017 $languages = get_string_manager()->get_list_of_languages();
1018 $paging = new paging_bar($related->totalcount
, $related->page
, $related->perpage
, $this->page
->url
, 'page');
1019 $htmlpagingbar = $this->render($paging);
1020 $table = new html_table();
1021 $table->attributes
['class'] = 'generaltable boxaligncenter boxwidthwide';
1022 $table->head
= array(
1024 get_string('version', 'badges'),
1025 get_string('language', 'badges'),
1026 get_string('type', 'badges')
1028 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1029 array_push($table->head
, '');
1032 foreach ($related->badges
as $badge) {
1033 $badgeobject = new badge($badge->id
);
1034 $style = array('title' => $badgeobject->name
);
1035 if (!$badgeobject->is_active()) {
1036 $style['class'] = 'dimmed';
1038 $context = ($badgeobject->type
== BADGE_TYPE_SITE
) ?
1039 context_system
::instance() : context_course
::instance($badgeobject->courseid
);
1040 $forlink = print_badge_image($badgeobject, $context) . ' ' .
1041 html_writer
::start_tag('span') . $badgeobject->name
. html_writer
::end_tag('span');
1042 $name = html_writer
::link(new moodle_url('/badges/overview.php', array('id' => $badgeobject->id
)), $forlink, $style);
1047 $badge->language ?
$languages[$badge->language
] : '',
1048 $badge->type
== BADGE_TYPE_COURSE ?
get_string('badgesview', 'badges') : get_string('sitebadges', 'badges')
1050 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1051 $action = $this->output
->action_icon(
1052 new moodle_url('/badges/related_action.php', [
1053 'badgeid' => $related->currentbadgeid
,
1054 'relatedid' => $badge->id
,
1055 'sesskey' => sesskey(),
1056 'action' => 'remove'
1058 new pix_icon('t/delete', get_string('delete')));
1059 $actions = html_writer
::tag('div', $action, array('class' => 'badge-actions'));
1060 array_push($row, $actions);
1062 $table->data
[] = $row;
1064 $htmltable = html_writer
::table($table);
1066 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1070 * Renders a table with alignment.
1072 * @param core_badges\output\badge_alignments $alignments List alignments.
1073 * @return string List alignment to output.
1075 protected function render_badge_alignments(\core_badges\output\badge_alignments
$alignments) {
1076 $currentbadge = new badge($alignments->currentbadgeid
);
1077 $paging = new paging_bar($alignments->totalcount
, $alignments->page
, $alignments->perpage
, $this->page
->url
, 'page');
1078 $htmlpagingbar = $this->render($paging);
1079 $table = new html_table();
1080 $table->attributes
['class'] = 'generaltable boxaligncenter boxwidthwide';
1081 $table->head
= array('Name', 'URL', '');
1083 foreach ($alignments->alignments
as $item) {
1084 $urlaligment = new moodle_url('alignment.php',
1086 'id' => $currentbadge->id
,
1087 'alignmentid' => $item->id
,
1091 html_writer
::link($urlaligment, $item->targetname
),
1092 html_writer
::link($item->targeturl
, $item->targeturl
, array('target' => '_blank'))
1094 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1095 $delete = $this->output
->action_icon(
1096 new moodle_url('/badges/alignment_action.php', [
1097 'id' => $currentbadge->id
,
1098 'alignmentid' => $item->id
,
1099 'sesskey' => sesskey(),
1100 'action' => 'remove'
1102 new pix_icon('t/delete', get_string('delete'))
1104 $edit = $this->output
->action_icon(
1105 new moodle_url('alignment.php',
1107 'id' => $currentbadge->id
,
1108 'alignmentid' => $item->id
,
1111 ), new pix_icon('t/edit', get_string('edit')));
1112 $actions = html_writer
::tag('div', $edit . $delete, array('class' => 'badge-actions'));
1113 array_push($row, $actions);
1115 $table->data
[] = $row;
1117 $htmltable = html_writer
::table($table);
1119 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1123 * Defer to template.
1125 * @param \core_badges\output\external_backpacks_page $page
1126 * @return bool|string
1128 public function render_external_backpacks_page(\core_badges\output\external_backpacks_page
$page) {
1129 $data = $page->export_for_template($this);
1130 return parent
::render_from_template('core_badges/external_backpacks_page', $data);
1134 * Get the result of a backpack validation with its settings. It returns:
1135 * - A informative message if the backpack version is different from OBv2.
1136 * - A warning with the error if it's not possible to connect to this backpack.
1137 * - A successful message if the connection has worked.
1139 * @param int $backpackid The backpack identifier.
1140 * @return string A message with the validation result.
1142 public function render_test_backpack_result(int $backpackid): string {
1143 // Get the backpack.
1144 $backpack = badges_get_site_backpack($backpackid);
1146 // Add the header to the result.
1147 $result = $this->heading(get_string('testbackpack', 'badges', $backpack->backpackweburl
));
1149 if ($backpack->apiversion
!= OPEN_BADGES_V2
) {
1150 // Only OBv2 supports this validation.
1151 $result .= get_string('backpackconnectionnottested', 'badges');
1153 $message = badges_verify_backpack($backpackid);
1154 if (empty($message)) {
1155 $result .= get_string('backpackconnectionok', 'badges');
1157 $result .= $message;
1165 * Render the tertiary navigation for the page.
1167 * @param \core_badges\output\base_action_bar $actionbar
1168 * @return bool|string
1170 public function render_tertiary_navigation(\core_badges\output\base_action_bar
$actionbar) {
1171 return $this->render_from_template($actionbar->get_template(), $actionbar->export_for_template($this));