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 require_once($CFG->libdir
. '/badgeslib.php');
28 require_once($CFG->libdir
. '/tablelib.php');
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) {
38 foreach ($badges as $badge) {
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);
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 $imagecaption = $badge->imagecaption ??
'';
63 $image = html_writer
::empty_tag('img', ['src' => $imageurl, 'class' => 'badge-image', 'alt' => $imagecaption]);
64 if (!empty($badge->dateexpire
) && $badge->dateexpire
< time()) {
65 $image .= $this->output
->pix_icon('i/expired',
66 get_string('expireddate', 'badges', userdate($badge->dateexpire
)),
68 array('class' => 'expireimage'));
69 $name .= '(' . get_string('expired', 'badges') . ')';
72 $download = $status = $push = '';
73 if (($userid == $USER->id
) && !$profile) {
75 'download' => $badge->id
,
76 'hash' => $badge->uniquehash
,
77 'sesskey' => sesskey()
79 $url = new moodle_url(
83 $notexpiredbadge = (empty($badge->dateexpire
) ||
$badge->dateexpire
> time());
84 $userbackpack = badges_get_user_backpack();
85 if (!empty($CFG->badges_allowexternalbackpack
) && $notexpiredbadge && $userbackpack) {
86 $assertion = new moodle_url('/badges/assertion.php', array('b' => $badge->uniquehash
));
87 $icon = new pix_icon('t/backpack', get_string('addtobackpack', 'badges'));
88 if (badges_open_badges_backpack_api($userbackpack->id
) == OPEN_BADGES_V2
) {
89 $addurl = new moodle_url('/badges/backpack-add.php', array('hash' => $badge->uniquehash
));
90 $push = $this->output
->action_icon($addurl, $icon);
91 } else if (badges_open_badges_backpack_api($userbackpack->id
) == OPEN_BADGES_V2P1
) {
92 $addurl = new moodle_url('/badges/backpack-export.php', array('hash' => $badge->uniquehash
));
93 $push = $this->output
->action_icon($addurl, $icon);
97 $download = $this->output
->action_icon($url, new pix_icon('t/download', get_string('download')));
98 if ($badge->visible
) {
99 $url = new moodle_url('mybadges.php', array('hide' => $badge->issuedid
, 'sesskey' => sesskey()));
100 $status = $this->output
->action_icon($url, new pix_icon('t/hide', get_string('makeprivate', 'badges')));
102 $url = new moodle_url('mybadges.php', array('show' => $badge->issuedid
, 'sesskey' => sesskey()));
103 $status = $this->output
->action_icon($url, new pix_icon('t/show', get_string('makepublic', 'badges')));
108 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash
));
111 $url = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash
));
113 $hash = hash('md5', $badge->hostedUrl
);
114 $url = new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid));
117 $actions = html_writer
::tag('div', $push . $download . $status, array('class' => 'badge-actions'));
118 $items[] = html_writer
::link($url, $image . $actions . $name, array('title' => $bname));
121 return html_writer
::alist($items, array('class' => 'badges'));
124 // Recipients selection form.
125 public function recipients_selection_form(user_selector_base
$existinguc, user_selector_base
$potentialuc) {
127 $formattributes = array();
128 $formattributes['id'] = 'recipientform';
129 $formattributes['action'] = $this->page
->url
;
130 $formattributes['method'] = 'post';
131 $output .= html_writer
::start_tag('form', $formattributes);
132 $output .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
134 $existingcell = new html_table_cell();
135 $existingcell->text
= $existinguc->display(true);
136 $existingcell->attributes
['class'] = 'existing';
137 $actioncell = new html_table_cell();
138 $actioncell->text
= html_writer
::start_tag('div', array());
139 $actioncell->text
.= html_writer
::empty_tag('input', array(
142 'value' => $this->output
->larrow() . ' ' . get_string('award', 'badges'),
143 'class' => 'actionbutton btn btn-secondary')
145 $actioncell->text
.= html_writer
::empty_tag('input', array(
148 'value' => get_string('revoke', 'badges') . ' ' . $this->output
->rarrow(),
149 'class' => 'actionbutton btn btn-secondary')
151 $actioncell->text
.= html_writer
::end_tag('div', array());
152 $actioncell->attributes
['class'] = 'actions';
153 $potentialcell = new html_table_cell();
154 $potentialcell->text
= $potentialuc->display(true);
155 $potentialcell->attributes
['class'] = 'potential';
157 $table = new html_table();
158 $table->attributes
['class'] = 'recipienttable boxaligncenter';
159 $table->data
= array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
160 $output .= html_writer
::table($table);
162 $output .= html_writer
::end_tag('form');
166 // Prints a badge overview infomation.
167 public function print_badge_overview($badge, $context) {
169 $languages = get_string_manager()->get_list_of_languages();
172 $display .= $this->heading(get_string('badgedetails', 'badges'), 3);
174 $dl[get_string('name')] = $badge->name
;
175 $dl[get_string('version', 'badges')] = $badge->version
;
176 $dl[get_string('language')] = $languages[$badge->language
];
177 $dl[get_string('description', 'badges')] = $badge->description
;
178 $dl[get_string('createdon', 'search')] = userdate($badge->timecreated
);
179 $dl[get_string('badgeimage', 'badges')] = print_badge_image($badge, $context, 'large');
180 $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname
;
181 $dl[get_string('imageauthoremail', 'badges')] =
182 html_writer
::tag('a', $badge->imageauthoremail
, array('href' => 'mailto:' . $badge->imageauthoremail
));
183 $dl[get_string('imageauthorurl', 'badges')] =
184 html_writer
::link($badge->imageauthorurl
, $badge->imageauthorurl
, array('target' => '_blank'));
185 $dl[get_string('imagecaption', 'badges')] = $badge->imagecaption
;
186 $display .= $this->definition_list($dl);
189 $display .= $this->heading(get_string('issuerdetails', 'badges'), 3);
191 $dl[get_string('issuername', 'badges')] = $badge->issuername
;
192 $dl[get_string('contact', 'badges')] = html_writer
::tag('a', $badge->issuercontact
, array('href' => 'mailto:' . $badge->issuercontact
));
193 $display .= $this->definition_list($dl);
195 // Issuance details if any.
196 $display .= $this->heading(get_string('issuancedetails', 'badges'), 3);
197 if ($badge->can_expire()) {
198 if ($badge->expiredate
) {
199 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate
));
200 } else if ($badge->expireperiod
) {
201 if ($badge->expireperiod
< 60) {
202 $display .= get_string('expireperiods', 'badges', round($badge->expireperiod
, 2));
203 } else if ($badge->expireperiod
< 60 * 60) {
204 $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod
/ 60, 2));
205 } else if ($badge->expireperiod
< 60 * 60 * 24) {
206 $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod
/ 60 / 60, 2));
208 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod
/ 60 / 60 / 24, 2));
212 $display .= get_string('noexpiry', 'badges');
215 // Criteria details if any.
216 $display .= $this->heading(get_string('bcriteria', 'badges'), 3);
217 if ($badge->has_criteria()) {
218 $display .= self
::print_badge_criteria($badge);
220 $display .= get_string('nocriteria', 'badges');
221 if (has_capability('moodle/badges:configurecriteria', $context)) {
222 $display .= $this->output
->single_button(
223 new moodle_url('/badges/criteria.php', array('id' => $badge->id
)),
224 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
228 // Awards details if any.
229 if (has_capability('moodle/badges:viewawarded', $context)) {
230 $display .= $this->heading(get_string('awards', 'badges'), 3);
231 if ($badge->has_awards()) {
232 $url = new moodle_url('/badges/recipients.php', array('id' => $badge->id
));
234 $a->link
= $url->out();
235 $a->count
= count($badge->get_awards());
236 $display .= get_string('numawards', 'badges', $a);
238 $display .= get_string('noawards', 'badges');
241 if (has_capability('moodle/badges:awardbadge', $context) &&
242 $badge->has_manual_award_criteria() &&
243 $badge->is_active()) {
244 $display .= $this->output
->single_button(
245 new moodle_url('/badges/award.php', array('id' => $badge->id
)),
246 get_string('award', 'badges'), 'POST', array('class' => 'activatebadge'));
250 $display .= self
::print_badge_endorsement($badge);
251 $display .= self
::print_badge_related($badge);
252 $display .= self
::print_badge_alignments($badge);
254 return html_writer
::div($display, null, array('id' => 'badge-overview'));
257 // Prints action icons for the badge.
258 public function print_badge_table_actions($badge, $context) {
261 if (has_capability('moodle/badges:configuredetails', $context) && $badge->has_criteria()) {
262 // Activate/deactivate badge.
263 if ($badge->status
== BADGE_STATUS_INACTIVE ||
$badge->status
== BADGE_STATUS_INACTIVE_LOCKED
) {
264 // "Activate" will go to another page and ask for confirmation.
265 $url = new moodle_url('/badges/action.php');
266 $url->param('id', $badge->id
);
267 $url->param('activate', true);
268 $url->param('sesskey', sesskey());
269 $return = new moodle_url(qualified_me());
270 $url->param('return', $return->out_as_local_url(false));
271 $actions .= $this->output
->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
273 $url = new moodle_url(qualified_me());
274 $url->param('lock', $badge->id
);
275 $url->param('sesskey', sesskey());
276 $actions .= $this->output
->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
280 // Award badge manually.
281 if ($badge->has_manual_award_criteria() &&
282 has_capability('moodle/badges:awardbadge', $context) &&
283 $badge->is_active()) {
284 $url = new moodle_url('/badges/award.php', array('id' => $badge->id
));
285 $actions .= $this->output
->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
289 if (has_capability('moodle/badges:configuredetails', $context)) {
290 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id
, 'action' => 'badge'));
291 $actions .= $this->output
->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
295 if (has_capability('moodle/badges:createbadge', $context)) {
296 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id
, 'sesskey' => sesskey()));
297 $actions .= $this->output
->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
301 if (has_capability('moodle/badges:deletebadge', $context)) {
302 $url = new moodle_url(qualified_me());
303 $url->param('delete', $badge->id
);
304 $actions .= $this->output
->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
311 * Render an issued badge.
313 * @param \core_badges\output\issued_badge $ibadge
316 protected function render_issued_badge(\core_badges\output\issued_badge
$ibadge) {
317 global $USER, $CFG, $DB, $SITE;
318 $issued = $ibadge->issued
;
319 $userinfo = $ibadge->recipient
;
320 $badgeclass = $ibadge->badgeclass
;
321 $badge = new badge($ibadge->badgeid
);
323 if (isset($issued['expires'])) {
324 if (!is_numeric($issued['expires'])) {
325 $issued['expires'] = strtotime($issued['expires']);
327 $expiration = $issued['expires'];
329 $expiration = $now +
86400;
332 $badgeimage = is_array($badgeclass['image']) ?
$badgeclass['image']['id'] : $badgeclass['image'];
333 $languages = get_string_manager()->get_list_of_languages();
336 $output .= html_writer
::start_tag('div', array('id' => 'badge'));
337 $output .= html_writer
::start_tag('div', array('id' => 'badge-image'));
338 $output .= html_writer
::empty_tag('img', array('src' => $badgeimage, 'alt' => $badge->imagecaption
, 'width' => '100'));
339 if ($expiration < $now) {
340 $output .= $this->output
->pix_icon('i/expired',
341 get_string('expireddate', 'badges', userdate($issued['expires'])),
343 array('class' => 'expireimage'));
346 if ($USER->id
== $userinfo->id
&& !empty($CFG->enablebadges
)) {
347 $output .= $this->output
->single_button(
348 new moodle_url('/badges/badge.php', array('hash' => $ibadge->hash
, 'bake' => true)),
349 get_string('download'),
351 if (!empty($CFG->badges_allowexternalbackpack
) && ($expiration > $now)
352 && $userbackpack = badges_get_user_backpack($USER->id
)) {
354 if (badges_open_badges_backpack_api($userbackpack->id
) == OPEN_BADGES_V2P1
) {
355 $assertion = new moodle_url('/badges/backpack-export.php', array('hash' => $ibadge->hash
));
357 $assertion = new moodle_url('/badges/backpack-add.php', array('hash' => $ibadge->hash
));
360 $attributes = ['class' => 'btn btn-secondary m-1', 'role' => 'button'];
361 $tobackpack = html_writer
::link($assertion, get_string('addtobackpack', 'badges'), $attributes);
362 $output .= $tobackpack;
365 $output .= html_writer
::end_tag('div');
367 $output .= html_writer
::start_tag('div', array('id' => 'badge-details'));
368 // Recipient information.
369 $output .= $this->output
->heading(get_string('recipientdetails', 'badges'), 3);
371 if ($userinfo->deleted
) {
372 $strdata = new stdClass();
373 $strdata->user
= fullname($userinfo);
374 $strdata->site
= format_string($SITE->fullname
, true, array('context' => context_system
::instance()));
376 $dl[get_string('name')] = get_string('error:userdeleted', 'badges', $strdata);
378 $dl[get_string('name')] = fullname($userinfo);
380 $output .= $this->definition_list($dl);
382 $output .= $this->output
->heading(get_string('issuerdetails', 'badges'), 3);
384 $dl[get_string('issuername', 'badges')] = format_string($badge->issuername
, true,
385 ['context' => context_system
::instance()]);
387 if (isset($badge->issuercontact
) && !empty($badge->issuercontact
)) {
388 $dl[get_string('contact', 'badges')] = obfuscate_mailto($badge->issuercontact
);
390 $output .= $this->definition_list($dl);
392 $output .= $this->output
->heading(get_string('badgedetails', 'badges'), 3);
394 $dl[get_string('name')] = $badge->name
;
395 if (!empty($badge->version
)) {
396 $dl[get_string('version', 'badges')] = $badge->version
;
398 if (!empty($badge->language
)) {
399 $dl[get_string('language')] = $languages[$badge->language
];
401 $dl[get_string('description', 'badges')] = $badge->description
;
402 if (!empty($badge->imageauthorname
)) {
403 $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname
;
405 if (!empty($badge->imageauthoremail
)) {
406 $dl[get_string('imageauthoremail', 'badges')] =
407 html_writer
::tag('a', $badge->imageauthoremail
, array('href' => 'mailto:' . $badge->imageauthoremail
));
409 if (!empty($badge->imageauthorurl
)) {
410 $dl[get_string('imageauthorurl', 'badges')] =
411 html_writer
::link($badge->imageauthorurl
, $badge->imageauthorurl
, array('target' => '_blank'));
413 if (!empty($badge->imagecaption
)) {
414 $dl[get_string('imagecaption', 'badges')] = $badge->imagecaption
;
417 if ($badge->type
== BADGE_TYPE_COURSE
&& isset($badge->courseid
)) {
418 $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid
));
419 $dl[get_string('course')] = format_string($coursename, true, ['context' => context_course
::instance($badge->courseid
)]);
421 $dl[get_string('bcriteria', 'badges')] = self
::print_badge_criteria($badge);
422 $output .= $this->definition_list($dl);
424 $output .= $this->output
->heading(get_string('issuancedetails', 'badges'), 3);
426 if (!is_numeric($issued['issuedOn'])) {
427 $issued['issuedOn'] = strtotime($issued['issuedOn']);
429 $dl[get_string('dateawarded', 'badges')] = userdate($issued['issuedOn']);
430 if (isset($issued['expires'])) {
431 if ($issued['expires'] < $now) {
432 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']) . get_string('warnexpired', 'badges');
435 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']);
440 $agg = $badge->get_aggregation_methods();
441 $evidence = $badge->get_criteria_completions($userinfo->id
);
442 $eids = array_map(function($o) {
445 unset($badge->criteria
[BADGE_CRITERIA_TYPE_OVERALL
]);
448 foreach ($badge->criteria
as $type => $c) {
449 if (in_array($c->id
, $eids)) {
450 if (count($c->params
) == 1) {
451 $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
453 $items[] = get_string('criteria_descr_' . $type , 'badges',
454 core_text
::strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
459 $dl[get_string('evidence', 'badges')] = get_string('completioninfo', 'badges') . html_writer
::alist($items, array(), 'ul');
460 $output .= $this->definition_list($dl);
461 $endorsement = $badge->get_endorsement();
462 if (!empty($endorsement)) {
463 $output .= self
::print_badge_endorsement($badge);
466 $relatedbadges = $badge->get_related_badges(true);
468 foreach ($relatedbadges as $related) {
469 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id
));
470 $items[] = html_writer
::link($relatedurl->out(), $related->name
, array('target' => '_blank'));
472 if (!empty($items)) {
473 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
474 $output .= html_writer
::alist($items, array(), 'ul');
477 $alignments = $badge->get_alignments();
478 if (!empty($alignments)) {
479 $output .= $this->heading(get_string('alignment', 'badges'), 3);
481 foreach ($alignments as $alignment) {
482 $items[] = html_writer
::link($alignment->targeturl
, $alignment->targetname
, array('target' => '_blank'));
484 $output .= html_writer
::alist($items, array(), 'ul');
486 $output .= html_writer
::end_tag('div');
492 * Render an external badge.
494 * @param \core_badges\output\external_badge $ibadge
497 protected function render_external_badge(\core_badges\output\external_badge
$ibadge) {
498 $issued = $ibadge->issued
;
499 $assertion = $issued->assertion
;
500 $issuer = $assertion->badge
->issuer
;
501 $userinfo = $ibadge->recipient
;
502 $table = new html_table();
503 $today = strtotime(date('Y-m-d'));
506 $output .= html_writer
::start_tag('div', array('id' => 'badge'));
507 $output .= html_writer
::start_tag('div', array('id' => 'badge-image'));
508 if (isset($issued->imageUrl
)) {
509 $issued->image
= $issued->imageUrl
;
511 $output .= html_writer
::empty_tag('img', array('src' => $issued->image
, 'width' => '100'));
512 if (isset($assertion->expires
)) {
513 $expiration = is_numeric($assertion->expires
) ?
$assertion->expires
: strtotime($assertion->expires
);
514 if ($expiration < $today) {
515 $output .= $this->output
->pix_icon('i/expired',
516 get_string('expireddate', 'badges', userdate($expiration)),
518 array('class' => 'expireimage'));
521 $output .= html_writer
::end_tag('div');
523 $output .= html_writer
::start_tag('div', array('id' => 'badge-details'));
525 // Recipient information.
526 $output .= $this->output
->heading(get_string('recipientdetails', 'badges'), 3);
528 // Technically, we should alway have a user at this point, but added an extra check just in case.
530 if (!$ibadge->valid
) {
531 $notify = $this->output
->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice');
532 $dl[get_string('name')] = fullname($userinfo) . $notify;
534 $dl[get_string('name')] = fullname($userinfo);
537 $notify = $this->output
->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice');
538 $dl[get_string('name')] = $notify;
540 $output .= $this->definition_list($dl);
542 $output .= $this->output
->heading(get_string('issuerdetails', 'badges'), 3);
544 $dl[get_string('issuername', 'badges')] = s($issuer->name
);
545 if (isset($issuer->origin
)) {
546 $dl[get_string('issuerurl', 'badges')] = html_writer
::tag('a', $issuer->origin
, array('href' => $issuer->origin
));
549 if (isset($issuer->contact
)) {
550 $dl[get_string('contact', 'badges')] = obfuscate_mailto($issuer->contact
);
552 $output .= $this->definition_list($dl);
554 $output .= $this->output
->heading(get_string('badgedetails', 'badges'), 3);
556 $dl[get_string('name')] = s($assertion->badge
->name
);
557 $dl[get_string('description', 'badges')] = s($assertion->badge
->description
);
558 if (isset($assertion->badge
->criteria
)) {
559 $dl[get_string('bcriteria', 'badges')] = html_writer
::tag(
561 s($assertion->badge
->criteria
),
562 array('href' => $assertion->badge
->criteria
)
565 $output .= $this->definition_list($dl);
568 if (isset($assertion->issued_on
)) {
569 $issuedate = is_numeric($assertion->issued_on
) ?
$assertion->issued_on
: strtotime($assertion->issued_on
);
570 $dl[get_string('dateawarded', 'badges')] = userdate($issuedate);
572 if (isset($assertion->expires
)) {
573 if ($expiration < $today) {
574 $dl[get_string('expirydate', 'badges')] = userdate($expiration) . get_string('warnexpired', 'badges');
576 $dl[get_string('expirydate', 'badges')] = userdate($expiration);
579 if (isset($assertion->evidence
)) {
580 $dl[get_string('evidence', 'badges')] = html_writer
::tag(
582 s($assertion->evidence
),
583 array('href' => $assertion->evidence
)
587 $output .= $this->output
->heading(get_string('issuancedetails', 'badges'), 3);
588 $output .= $this->definition_list($dl);
590 $output .= html_writer
::end_tag('div');
596 * Render a collection of user badges.
598 * @param \core_badges\output\badge_user_collection $badges
601 protected function render_badge_user_collection(\core_badges\output\badge_user_collection
$badges) {
602 global $CFG, $USER, $SITE;
603 $backpack = $badges->backpack
;
604 $mybackpack = new moodle_url('/badges/mybackpack.php');
606 $paging = new paging_bar($badges->totalcount
, $badges->page
, $badges->perpage
, $this->page
->url
, 'page');
607 $htmlpagingbar = $this->render($paging);
609 // Set backpack connection string.
610 $backpackconnect = '';
611 if (!empty($CFG->badges_allowexternalbackpack
) && is_null($backpack)) {
612 $backpackconnect = $this->output
->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
615 $searchform = $this->output
->box($this->helper_search_form($badges->search
), 'boxwidthwide boxaligncenter');
617 // Download all button.
618 $actionhtml = $this->output
->single_button(
619 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
620 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
621 $downloadall = $this->output
->box('', 'col-md-3');
622 $downloadall .= $this->output
->box($actionhtml, 'col-md-9');
623 $downloadall = $this->output
->box($downloadall, 'row ml-5');
626 $localhtml = html_writer
::start_tag('div', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
627 $sitename = format_string($SITE->fullname
, true, array('context' => context_system
::instance()));
628 $heading = get_string('localbadges', 'badges', $sitename);
629 $localhtml .= $this->output
->heading_with_help($heading, 'localbadgesh', 'badges');
630 if ($badges->badges
) {
631 $countmessage = $this->output
->box(get_string('badgesearned', 'badges', $badges->totalcount
));
633 $htmllist = $this->print_badges_list($badges->badges
, $USER->id
);
634 $localhtml .= $backpackconnect . $countmessage . $searchform;
635 $localhtml .= $htmlpagingbar . $htmllist . $htmlpagingbar . $downloadall;
637 $localhtml .= $searchform . $this->output
->notification(get_string('nobadges', 'badges'));
639 $localhtml .= html_writer
::end_tag('div');
643 if (!empty($CFG->badges_allowexternalbackpack
)) {
644 $externalhtml .= html_writer
::start_tag('div', array('class' => 'generalbox'));
645 $externalhtml .= $this->output
->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges');
646 if (!is_null($backpack)) {
647 if ($backpack->totalcollections
== 0) {
648 $externalhtml .= get_string('nobackpackcollectionssummary', 'badges', $backpack);
650 if ($backpack->totalbadges
== 0) {
651 $externalhtml .= get_string('nobackpackbadgessummary', 'badges', $backpack);
653 $externalhtml .= get_string('backpackbadgessummary', 'badges', $backpack);
654 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges
, $USER->id
, true, true);
658 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
661 $externalhtml .= html_writer
::end_tag('div');
662 $attr = ['class' => 'btn btn-secondary'];
663 $label = get_string('backpackbadgessettings', 'badges');
664 $backpacksettings = html_writer
::link(new moodle_url('/badges/mybackpack.php'), $label, $attr);
665 $actionshtml = $this->output
->box('', 'col-md-3');
666 $actionshtml .= $this->output
->box($backpacksettings, 'col-md-9');
667 $actionshtml = $this->output
->box($actionshtml, 'row ml-5');
668 $externalhtml .= $actionshtml;
671 return $localhtml . $externalhtml;
675 * Render a collection of badges.
677 * @param \core_badges\output\badge_collection $badges
680 protected function render_badge_collection(\core_badges\output\badge_collection
$badges) {
681 $paging = new paging_bar($badges->totalcount
, $badges->page
, $badges->perpage
, $this->page
->url
, 'page');
682 $htmlpagingbar = $this->render($paging);
683 $table = new html_table();
684 $table->attributes
['class'] = 'table table-bordered table-striped';
686 $sortbyname = $this->helper_sortable_heading(get_string('name'),
687 'name', $badges->sort
, $badges->dir
);
688 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
689 'dateissued', $badges->sort
, $badges->dir
);
690 $table->head
= array(
691 get_string('badgeimage', 'badges'),
693 get_string('description', 'badges'),
694 get_string('bcriteria', 'badges'),
697 $table->colclasses
= array('badgeimage', 'name', 'description', 'criteria', 'awards');
699 foreach ($badges->badges
as $badge) {
700 $badgeimage = print_badge_image($badge, $this->page
->context
, 'large');
701 $name = $badge->name
;
702 $description = $badge->description
;
703 $criteria = self
::print_badge_criteria($badge);
704 if ($badge->dateissued
) {
705 $icon = new pix_icon('i/valid',
706 get_string('dateearned', 'badges',
707 userdate($badge->dateissued
, get_string('strftimedatefullshort', 'core_langconfig'))));
708 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash
));
709 $awarded = $this->output
->action_icon($badgeurl, $icon, null, null, true);
713 $row = array($badgeimage, $name, $description, $criteria, $awarded);
714 $table->data
[] = $row;
717 $htmltable = html_writer
::table($table);
719 return $htmlpagingbar . $htmltable . $htmlpagingbar;
723 * Render a table of badges.
725 * @param \core_badges\output\badge_management $badges
728 protected function render_badge_management(\core_badges\output\badge_management
$badges) {
729 $paging = new paging_bar($badges->totalcount
, $badges->page
, $badges->perpage
, $this->page
->url
, 'page');
733 if (has_capability('moodle/badges:createbadge', $this->page
->context
)) {
734 $n['type'] = $this->page
->url
->get_param('type');
735 $n['id'] = $this->page
->url
->get_param('id');
736 $btn = $this->output
->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
737 $htmlnew = $this->output
->box($btn);
740 $htmlpagingbar = $this->render($paging);
741 $table = new html_table();
742 $table->attributes
['class'] = 'table table-bordered table-striped';
744 $sortbyname = $this->helper_sortable_heading(get_string('name'),
745 'name', $badges->sort
, $badges->dir
);
746 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
747 'status', $badges->sort
, $badges->dir
);
748 $table->head
= array(
751 get_string('bcriteria', 'badges'),
752 get_string('awards', 'badges'),
753 get_string('actions')
755 $table->colclasses
= array('name', 'status', 'criteria', 'awards', 'actions');
757 foreach ($badges->badges
as $b) {
758 $style = !$b->is_active() ?
array('class' => 'dimmed') : array();
759 $forlink = print_badge_image($b, $this->page
->context
) . ' ' .
760 html_writer
::start_tag('span') . $b->name
. html_writer
::end_tag('span');
761 $name = html_writer
::link(new moodle_url('/badges/overview.php', array('id' => $b->id
)), $forlink, $style);
762 $status = $b->statstring
;
763 $criteria = self
::print_badge_criteria($b, 'short');
765 if (has_capability('moodle/badges:viewawarded', $this->page
->context
)) {
766 $awards = html_writer
::link(new moodle_url('/badges/recipients.php', array('id' => $b->id
)), $b->awards
);
768 $awards = $b->awards
;
771 $actions = self
::print_badge_table_actions($b, $this->page
->context
);
773 $row = array($name, $status, $criteria, $awards, $actions);
774 $table->data
[] = $row;
776 $htmltable = html_writer
::table($table);
778 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
782 * Prints tabs for badge editing.
784 * @param integer $badgeid The badgeid to edit.
785 * @param context $context The current context.
786 * @param string $current The currently selected tab.
789 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
792 $badge = new badge($badgeid);
795 $row[] = new tabobject('overview',
796 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
797 get_string('boverview', 'badges')
800 if (has_capability('moodle/badges:configuredetails', $context)) {
801 $row[] = new tabobject('badge',
802 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'badge')),
803 get_string('bdetails', 'badges')
807 if (has_capability('moodle/badges:configurecriteria', $context)) {
808 $row[] = new tabobject('criteria',
809 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
810 get_string('bcriteria', 'badges')
814 if (has_capability('moodle/badges:configuremessages', $context)) {
815 $row[] = new tabobject('message',
816 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
817 get_string('bmessage', 'badges')
821 if (has_capability('moodle/badges:viewawarded', $context)) {
822 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
823 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
824 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
825 $row[] = new tabobject('awards',
826 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
827 get_string('bawards', 'badges', $awarded)
831 if (has_capability('moodle/badges:configuredetails', $context)) {
832 $row[] = new tabobject('bendorsement',
833 new moodle_url('/badges/endorsement.php', array('id' => $badgeid)),
834 get_string('bendorsement', 'badges')
838 if (has_capability('moodle/badges:configuredetails', $context)) {
839 $sql = "SELECT COUNT(br.badgeid)
840 FROM {badge_related} br
841 WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
842 $related = $DB->count_records_sql($sql, ['badgeid' => $badgeid, 'badgeid2' => $badgeid]);
843 $row[] = new tabobject('brelated',
844 new moodle_url('/badges/related.php', array('id' => $badgeid)),
845 get_string('brelated', 'badges', $related)
849 if (has_capability('moodle/badges:configuredetails', $context)) {
850 $alignments = $DB->count_records_sql("SELECT COUNT(bc.id)
851 FROM {badge_alignment} bc WHERE bc.badgeid = :badgeid", array('badgeid' => $badgeid));
852 $row[] = new tabobject('alignment',
853 new moodle_url('/badges/alignment.php', array('id' => $badgeid)),
854 get_string('balignment', 'badges', $alignments)
858 echo $this->tabtree($row, $current);
862 * Prints badge status box.
864 * @param badge $badge
865 * @return Either the status box html as a string or null
867 public function print_badge_status_box(badge
$badge) {
868 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
870 if (!$badge->has_criteria()) {
871 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id
));
872 $status = get_string('nocriteria', 'badges');
873 if ($this->page
->url
!= $criteriaurl) {
874 $action = $this->output
->single_button(
876 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
881 $message = $status . $action;
883 $status = get_string('statusmessage_' . $badge->status
, 'badges');
884 if ($badge->is_active()) {
885 $action = $this->output
->single_button(new moodle_url('/badges/action.php',
886 array('id' => $badge->id
, 'lock' => 1, 'sesskey' => sesskey(),
887 'return' => $this->page
->url
->out_as_local_url(false))),
888 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
890 $action = $this->output
->single_button(new moodle_url('/badges/action.php',
891 array('id' => $badge->id
, 'activate' => 1, 'sesskey' => sesskey(),
892 'return' => $this->page
->url
->out_as_local_url(false))),
893 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
896 $message = $status . $this->output
->help_icon('status', 'badges') . $action;
900 $style = $badge->is_active() ?
'generalbox statusbox active' : 'generalbox statusbox inactive';
901 return $this->output
->box($message, $style);
908 * Returns information about badge criteria in a list form.
910 * @param badge $badge Badge objects
911 * @param string $short Indicates whether to print full info about this badge
912 * @return string $output HTML string to output
914 public function print_badge_criteria(badge
$badge, $short = '') {
915 $agg = $badge->get_aggregation_methods();
916 if (empty($badge->criteria
)) {
917 return get_string('nocriteria', 'badges');
921 $overall = $badge->criteria
[BADGE_CRITERIA_TYPE_OVERALL
];
922 if (!$short && !empty($overall->description
)) {
923 $overalldescr = $this->output
->box(
924 format_text($overall->description
, $overall->descriptionformat
, array('context' => $badge->get_context())),
925 'criteria-description'
929 // Get the condition string.
930 if (count($badge->criteria
) == 2) {
933 $condition = get_string('criteria_descr', 'badges');
936 $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL
, 'badges',
937 core_text
::strtoupper($agg[$badge->get_aggregation_method()]));
940 unset($badge->criteria
[BADGE_CRITERIA_TYPE_OVERALL
]);
943 // If only one criterion left, make sure its description goe to the top.
944 if (count($badge->criteria
) == 1) {
945 $c = reset($badge->criteria
);
946 if (!$short && !empty($c->description
)) {
947 $overalldescr = $this->output
->box(
948 format_text($c->description
, $c->descriptionformat
, array('context' => $badge->get_context())),
949 'criteria-description'
952 if (count($c->params
) == 1) {
953 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype
, 'badges') .
954 $c->get_details($short);
956 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype
, 'badges',
957 core_text
::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype
)])) .
958 $c->get_details($short);
961 foreach ($badge->criteria
as $type => $c) {
963 if (!$short && !empty($c->description
)) {
964 $criteriadescr = $this->output
->box(
965 format_text($c->description
, $c->descriptionformat
, array('context' => $badge->get_context())),
966 'criteria-description'
969 if (count($c->params
) == 1) {
970 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
971 $c->get_details($short) . $criteriadescr;
973 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
974 core_text
::strtoupper($agg[$badge->get_aggregation_method($type)])) .
975 $c->get_details($short) .
981 return $overalldescr . $condition . html_writer
::alist($items, array(), 'ul');;
985 * Prints criteria actions for badge editing.
987 * @param badge $badge
990 public function print_criteria_actions(badge
$badge) {
992 if (!$badge->is_active() && !$badge->is_locked()) {
993 $accepted = $badge->get_accepted_criteria();
994 $potential = array_diff($accepted, array_keys($badge->criteria
));
996 if (!empty($potential)) {
997 foreach ($potential as $p) {
999 $select[$p] = get_string('criteria_' . $p, 'badges');
1002 $output .= $this->output
->single_select(
1003 new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id
, 'add' => true)),
1007 array('' => 'choosedots'),
1009 array('label' => get_string('addbadgecriteria', 'badges'))
1012 $output .= $this->output
->box(get_string('nothingtoadd', 'badges'), 'clearfix');
1020 * Renders a table with users who have earned the badge.
1021 * Based on stamps collection plugin.
1023 * @param \core_badges\output\badge_recipients $recipients
1026 protected function render_badge_recipients(\core_badges\output\badge_recipients
$recipients) {
1027 $paging = new paging_bar($recipients->totalcount
, $recipients->page
, $recipients->perpage
, $this->page
->url
, 'page');
1028 $htmlpagingbar = $this->render($paging);
1029 $table = new html_table();
1030 $table->attributes
['class'] = 'generaltable boxaligncenter boxwidthwide';
1032 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
1033 'firstname', $recipients->sort
, $recipients->dir
);
1034 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
1035 'lastname', $recipients->sort
, $recipients->dir
);
1036 if ($this->helper_fullname_format() == 'lf') {
1037 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
1039 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
1042 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
1043 'dateissued', $recipients->sort
, $recipients->dir
);
1045 $table->head
= array($sortbyname, $sortbydate, '');
1047 foreach ($recipients->userids
as $holder) {
1048 $fullname = fullname($holder);
1049 $fullname = html_writer
::link(
1050 new moodle_url('/user/profile.php', array('id' => $holder->userid
)),
1053 $awarded = userdate($holder->dateissued
);
1054 $badgeurl = html_writer
::link(
1055 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash
)),
1056 get_string('viewbadge', 'badges')
1059 $row = array($fullname, $awarded, $badgeurl);
1060 $table->data
[] = $row;
1063 $htmltable = html_writer
::table($table);
1065 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1068 ////////////////////////////////////////////////////////////////////////////
1070 // Reused from stamps collection plugin
1071 ////////////////////////////////////////////////////////////////////////////
1074 * Renders a text with icons to sort by the given column
1076 * This is intended for table headings.
1078 * @param string $text The heading text
1079 * @param string $sortid The column id used for sorting
1080 * @param string $sortby Currently sorted by (column id)
1081 * @param string $sorthow Currently sorted how (ASC|DESC)
1085 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
1086 $out = html_writer
::tag('span', $text, array('class' => 'text'));
1088 if (!is_null($sortid)) {
1089 if ($sortby !== $sortid ||
$sorthow !== 'ASC') {
1090 $url = new moodle_url($this->page
->url
);
1091 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
1092 $out .= $this->output
->action_icon($url,
1093 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
1095 if ($sortby !== $sortid ||
$sorthow !== 'DESC') {
1096 $url = new moodle_url($this->page
->url
);
1097 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
1098 $out .= $this->output
->action_icon($url,
1099 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
1105 * Tries to guess the fullname format set at the site
1107 * @return string fl|lf
1109 protected function helper_fullname_format() {
1110 $fake = new stdClass();
1111 $fake->lastname
= 'LLLL';
1112 $fake->firstname
= 'FFFF';
1113 $fullname = get_string('fullnamedisplay', '', $fake);
1114 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
1121 * Renders a search form
1123 * @param string $search Search string
1124 * @return string HTML
1126 protected function helper_search_form($search) {
1128 require_once($CFG->libdir
. '/formslib.php');
1130 $mform = new MoodleQuickForm('searchform', 'POST', $this->page
->url
);
1132 $mform->addElement('hidden', 'sesskey', sesskey());
1134 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
1135 $mform->setDefault('search', $search);
1136 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
1137 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
1138 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
1142 $out = ob_get_clean();
1148 * Renders a definition list
1150 * @param array $items the list of items to define
1153 protected function definition_list(array $items, array $attributes = array()) {
1154 $output = html_writer
::start_tag('dl', $attributes);
1155 foreach ($items as $label => $value) {
1156 $output .= html_writer
::tag('dt', $label);
1157 $output .= html_writer
::tag('dd', $value);
1159 $output .= html_writer
::end_tag('dl');
1164 * Outputs list en badges.
1166 * @param badge $badge Badge object.
1167 * @return string $output content endorsement to output.
1169 protected function print_badge_endorsement(badge
$badge) {
1171 $endorsement = $badge->get_endorsement();
1173 $output .= $this->heading(get_string('endorsement', 'badges'), 3);
1174 if (!empty($endorsement)) {
1175 $dl[get_string('issuername', 'badges')] = $endorsement->issuername
;
1176 $dl[get_string('issueremail', 'badges')] =
1177 html_writer
::tag('a', $endorsement->issueremail
, array('href' => 'mailto:' . $endorsement->issueremail
));
1178 $dl[get_string('issuerurl', 'badges')] = html_writer
::link($endorsement->issuerurl
, $endorsement->issuerurl
,
1179 array('target' => '_blank'));
1180 $dl[get_string('dateawarded', 'badges')] = userdate($endorsement->dateissued
);
1181 $dl[get_string('claimid', 'badges')] = html_writer
::link($endorsement->claimid
, $endorsement->claimid
,
1182 array('target' => '_blank'));
1183 $dl[get_string('claimcomment', 'badges')] = $endorsement->claimcomment
;
1184 $output .= $this->definition_list($dl);
1186 $output .= get_string('noendorsement', 'badges');
1192 * Print list badges related.
1194 * @param badge $badge Badge objects.
1195 * @return string $output List related badges to output.
1197 protected function print_badge_related(badge
$badge) {
1199 $relatedbadges = $badge->get_related_badges();
1200 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
1201 if (!empty($relatedbadges)) {
1203 foreach ($relatedbadges as $related) {
1204 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id
));
1205 $items[] = html_writer
::link($relatedurl->out(), $related->name
, array('target' => '_blank'));
1207 $output .= html_writer
::alist($items, array(), 'ul');
1209 $output .= get_string('norelated', 'badges');
1215 * Print list badge alignments.
1217 * @param badge $badge Badge objects.
1218 * @return string $output List alignments to output.
1220 protected function print_badge_alignments(badge
$badge) {
1222 $output .= $this->heading(get_string('alignment', 'badges'), 3);
1223 $alignments = $badge->get_alignments();
1224 if (!empty($alignments)) {
1226 foreach ($alignments as $alignment) {
1227 $urlaligment = new moodle_url('alignment.php',
1228 array('id' => $badge->id
, 'alignmentid' => $alignment->id
)
1230 $items[] = html_writer
::link($urlaligment, $alignment->targetname
, array('target' => '_blank'));
1232 $output .= html_writer
::alist($items, array(), 'ul');
1234 $output .= get_string('noalignment', 'badges');
1240 * Renders a table for related badges.
1242 * @param \core_badges\output\badge_related $related list related badges.
1243 * @return string list related badges to output.
1245 protected function render_badge_related(\core_badges\output\badge_related
$related) {
1246 $currentbadge = new badge($related->currentbadgeid
);
1247 $languages = get_string_manager()->get_list_of_languages();
1248 $paging = new paging_bar($related->totalcount
, $related->page
, $related->perpage
, $this->page
->url
, 'page');
1249 $htmlpagingbar = $this->render($paging);
1250 $table = new html_table();
1251 $table->attributes
['class'] = 'generaltable boxaligncenter boxwidthwide';
1252 $table->head
= array(
1254 get_string('version', 'badges'),
1255 get_string('language', 'badges'),
1256 get_string('type', 'badges')
1258 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1259 array_push($table->head
, '');
1262 foreach ($related->badges
as $badge) {
1263 $badgeobject = new badge($badge->id
);
1264 $style = array('title' => $badgeobject->name
);
1265 if (!$badgeobject->is_active()) {
1266 $style['class'] = 'dimmed';
1268 $context = ($badgeobject->type
== BADGE_TYPE_SITE
) ?
1269 context_system
::instance() : context_course
::instance($badgeobject->courseid
);
1270 $forlink = print_badge_image($badgeobject, $context) . ' ' .
1271 html_writer
::start_tag('span') . $badgeobject->name
. html_writer
::end_tag('span');
1272 $name = html_writer
::link(new moodle_url('/badges/overview.php', array('id' => $badgeobject->id
)), $forlink, $style);
1277 $badge->language ?
$languages[$badge->language
] : '',
1278 $badge->type
== BADGE_TYPE_COURSE ?
get_string('badgesview', 'badges') : get_string('sitebadges', 'badges')
1280 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1281 $action = $this->output
->action_icon(
1282 new moodle_url('/badges/related_action.php', [
1283 'badgeid' => $related->currentbadgeid
,
1284 'relatedid' => $badge->id
,
1285 'sesskey' => sesskey(),
1286 'action' => 'remove'
1288 new pix_icon('t/delete', get_string('delete')));
1289 $actions = html_writer
::tag('div', $action, array('class' => 'badge-actions'));
1290 array_push($row, $actions);
1292 $table->data
[] = $row;
1294 $htmltable = html_writer
::table($table);
1296 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1300 * Renders a table with alignment.
1302 * @param core_badges\output\badge_alignments $alignments List alignments.
1303 * @return string List alignment to output.
1305 protected function render_badge_alignments(\core_badges\output\badge_alignments
$alignments) {
1306 $currentbadge = new badge($alignments->currentbadgeid
);
1307 $paging = new paging_bar($alignments->totalcount
, $alignments->page
, $alignments->perpage
, $this->page
->url
, 'page');
1308 $htmlpagingbar = $this->render($paging);
1309 $table = new html_table();
1310 $table->attributes
['class'] = 'generaltable boxaligncenter boxwidthwide';
1311 $table->head
= array('Name', 'URL', '');
1313 foreach ($alignments->alignments
as $item) {
1314 $urlaligment = new moodle_url('alignment.php',
1316 'id' => $currentbadge->id
,
1317 'alignmentid' => $item->id
,
1321 html_writer
::link($urlaligment, $item->targetname
),
1322 html_writer
::link($item->targeturl
, $item->targeturl
, array('target' => '_blank'))
1324 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1325 $delete = $this->output
->action_icon(
1326 new moodle_url('alignment_action.php',
1328 'id' => $currentbadge->id
,
1329 'alignmentid' => $item->id
,
1330 'action' => 'remove'
1332 ), new pix_icon('t/delete', get_string('delete')));
1333 $edit = $this->output
->action_icon(
1334 new moodle_url('alignment.php',
1336 'id' => $currentbadge->id
,
1337 'alignmentid' => $item->id
,
1340 ), new pix_icon('t/edit', get_string('edit')));
1341 $actions = html_writer
::tag('div', $edit . $delete, array('class' => 'badge-actions'));
1342 array_push($row, $actions);
1344 $table->data
[] = $row;
1346 $htmltable = html_writer
::table($table);
1348 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1352 * Defer to template.
1354 * @param \core_badges\output\external_backpacks_page $page
1355 * @return bool|string
1357 public function render_external_backpacks_page(\core_badges\output\external_backpacks_page
$page) {
1358 $data = $page->export_for_template($this);
1359 return parent
::render_from_template('core_badges/external_backpacks_page', $data);
1363 * Get the result of a backpack validation with its settings. It returns:
1364 * - A informative message if the backpack version is different from OBv2.
1365 * - A warning with the error if it's not possible to connect to this backpack.
1366 * - A successful message if the connection has worked.
1368 * @param int $backpackid The backpack identifier.
1369 * @return string A message with the validation result.
1371 public function render_test_backpack_result(int $backpackid): string {
1372 // Get the backpack.
1373 $backpack = badges_get_site_backpack($backpackid);
1375 // Add the header to the result.
1376 $result = $this->heading(get_string('testbackpack', 'badges', $backpack->backpackweburl
));
1378 if ($backpack->apiversion
!= OPEN_BADGES_V2
) {
1379 // Only OBv2 supports this validation.
1380 $result .= get_string('backpackconnectionnottested', 'badges');
1382 $message = badges_verify_backpack($backpackid);
1383 if (empty($message)) {
1384 $result .= get_string('backpackconnectionok', 'badges');
1386 $result .= $message;