MDL-65959 core_badges: Allow ability to upload badges cross domain.
[moodle.git] / badges / renderer.php
CommitLineData
27806552
YB
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
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/>.
16
17/**
18 * Renderer for use with the badges output
19 *
20 * @package core
21 * @subpackage badges
22 * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
25 */
26
27require_once($CFG->libdir . '/badgeslib.php');
28require_once($CFG->libdir . '/tablelib.php');
27806552
YB
29
30/**
31 * Standard HTML output renderer for badges
32 */
33class core_badges_renderer extends plugin_renderer_base {
34
35 // Outputs badges list.
36 public function print_badges_list($badges, $userid, $profile = false, $external = false) {
37 global $USER, $CFG;
38 foreach ($badges as $badge) {
39 if (!$external) {
40 $context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid);
41 $bname = $badge->name;
42 $imageurl = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', 'f1', false);
43 } else {
aae219ac
DW
44 $bname = '';
45 $imageurl = '';
46 if (!empty($badge->name)) {
47 $bname = s($badge->name);
48 }
49 if (!empty($badge->image)) {
50 $imageurl = $badge->image;
51 }
52 if (isset($badge->assertion->badge->name)) {
53 $bname = s($badge->assertion->badge->name);
54 }
55 if (isset($badge->imageUrl)) {
56 $imageurl = $badge->imageUrl;
57 }
27806552
YB
58 }
59
60 $name = html_writer::tag('span', $bname, array('class' => 'badge-name'));
61
62 $image = html_writer::empty_tag('img', array('src' => $imageurl, 'class' => 'badge-image'));
63 if (!empty($badge->dateexpire) && $badge->dateexpire < time()) {
64 $image .= $this->output->pix_icon('i/expired',
65 get_string('expireddate', 'badges', userdate($badge->dateexpire)),
66 'moodle',
67 array('class' => 'expireimage'));
68 $name .= '(' . get_string('expired', 'badges') . ')';
69 }
70
71 $download = $status = $push = '';
72 if (($userid == $USER->id) && !$profile) {
aae219ac
DW
73 $params = array(
74 'download' => $badge->id,
75 'hash' => $badge->uniquehash,
76 'sesskey' => sesskey()
77 );
78 $url = new moodle_url(
79 'mybadges.php',
80 $params
81 );
e2805314
YB
82 $notexpiredbadge = (empty($badge->dateexpire) || $badge->dateexpire > time());
83 $backpackexists = badges_user_has_backpack($USER->id);
84 if (!empty($CFG->badges_allowexternalbackpack) && $notexpiredbadge && $backpackexists) {
27806552 85 $assertion = new moodle_url('/badges/assertion.php', array('b' => $badge->uniquehash));
aae219ac
DW
86 $action = null;
87 if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
88 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
1837b1d5 89 $addurl = new moodle_url('#');
15a00bea
TT
90 } else if (badges_open_badges_backpack_api() == OPEN_BADGES_V2P1) {
91 $addurl = new moodle_url('/badges/backpack-export.php', array('hash' => $badge->uniquehash));
aae219ac 92 } else {
1837b1d5 93 $addurl = new moodle_url('/badges/backpack-add.php', array('hash' => $badge->uniquehash));
aae219ac 94 }
1837b1d5
DW
95 $icon = new pix_icon('t/backpack', get_string('addtobackpack', 'badges'));
96 $push = $this->output->action_icon($addurl, $icon, $action);
27806552
YB
97 }
98
99 $download = $this->output->action_icon($url, new pix_icon('t/download', get_string('download')));
100 if ($badge->visible) {
101 $url = new moodle_url('mybadges.php', array('hide' => $badge->issuedid, 'sesskey' => sesskey()));
102 $status = $this->output->action_icon($url, new pix_icon('t/hide', get_string('makeprivate', 'badges')));
103 } else {
104 $url = new moodle_url('mybadges.php', array('show' => $badge->issuedid, 'sesskey' => sesskey()));
105 $status = $this->output->action_icon($url, new pix_icon('t/show', get_string('makepublic', 'badges')));
106 }
107 }
108
109 if (!$profile) {
110 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash));
111 } else {
112 if (!$external) {
2d3c0fae 113 $url = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
27806552 114 } else {
2d3c0fae
YB
115 $hash = hash('md5', $badge->hostedUrl);
116 $url = new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid));
27806552
YB
117 }
118 }
119 $actions = html_writer::tag('div', $push . $download . $status, array('class' => 'badge-actions'));
120 $items[] = html_writer::link($url, $image . $actions . $name, array('title' => $bname));
121 }
122
123 return html_writer::alist($items, array('class' => 'badges'));
124 }
125
126 // Recipients selection form.
127 public function recipients_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
128 $output = '';
129 $formattributes = array();
130 $formattributes['id'] = 'recipientform';
04da1a5a 131 $formattributes['action'] = $this->page->url;
27806552
YB
132 $formattributes['method'] = 'post';
133 $output .= html_writer::start_tag('form', $formattributes);
134 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
135
136 $existingcell = new html_table_cell();
137 $existingcell->text = $existinguc->display(true);
138 $existingcell->attributes['class'] = 'existing';
139 $actioncell = new html_table_cell();
140 $actioncell->text = html_writer::start_tag('div', array());
141 $actioncell->text .= html_writer::empty_tag('input', array(
142 'type' => 'submit',
143 'name' => 'award',
144 'value' => $this->output->larrow() . ' ' . get_string('award', 'badges'),
2e477f03 145 'class' => 'actionbutton btn btn-secondary')
27806552 146 );
b7374fac
MD
147 $actioncell->text .= html_writer::empty_tag('input', array(
148 'type' => 'submit',
149 'name' => 'revoke',
150 'value' => get_string('revoke', 'badges') . ' ' . $this->output->rarrow(),
2e477f03 151 'class' => 'actionbutton btn btn-secondary')
b7374fac 152 );
27806552
YB
153 $actioncell->text .= html_writer::end_tag('div', array());
154 $actioncell->attributes['class'] = 'actions';
155 $potentialcell = new html_table_cell();
156 $potentialcell->text = $potentialuc->display(true);
157 $potentialcell->attributes['class'] = 'potential';
158
159 $table = new html_table();
160 $table->attributes['class'] = 'recipienttable boxaligncenter';
161 $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
162 $output .= html_writer::table($table);
163
164 $output .= html_writer::end_tag('form');
165 return $output;
166 }
167
168 // Prints a badge overview infomation.
169 public function print_badge_overview($badge, $context) {
170 $display = "";
d363a5c2 171 $languages = get_string_manager()->get_list_of_languages();
27806552
YB
172
173 // Badge details.
cd243d94
Y
174 $display .= $this->heading(get_string('badgedetails', 'badges'), 3);
175 $dl = array();
176 $dl[get_string('name')] = $badge->name;
d363a5c2
TT
177 $dl[get_string('version', 'badges')] = $badge->version;
178 $dl[get_string('language')] = $languages[$badge->language];
cd243d94 179 $dl[get_string('description', 'badges')] = $badge->description;
64d00f4c 180 $dl[get_string('createdon', 'search')] = userdate($badge->timecreated);
cd243d94 181 $dl[get_string('badgeimage', 'badges')] = print_badge_image($badge, $context, 'large');
65ffd0a3 182 $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname;
d363a5c2
TT
183 $dl[get_string('imageauthoremail', 'badges')] =
184 html_writer::tag('a', $badge->imageauthoremail, array('href' => 'mailto:' . $badge->imageauthoremail));
185 $dl[get_string('imageauthorurl', 'badges')] =
186 html_writer::link($badge->imageauthorurl, $badge->imageauthorurl, array('target' => '_blank'));
187 $dl[get_string('imagecaption', 'badges')] = $badge->imagecaption;
cd243d94 188 $display .= $this->definition_list($dl);
27806552 189
cd243d94
Y
190 // Issuer details.
191 $display .= $this->heading(get_string('issuerdetails', 'badges'), 3);
192 $dl = array();
193 $dl[get_string('issuername', 'badges')] = $badge->issuername;
194 $dl[get_string('contact', 'badges')] = html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact));
195 $display .= $this->definition_list($dl);
27806552
YB
196
197 // Issuance details if any.
cd243d94 198 $display .= $this->heading(get_string('issuancedetails', 'badges'), 3);
27806552
YB
199 if ($badge->can_expire()) {
200 if ($badge->expiredate) {
201 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate));
202 } else if ($badge->expireperiod) {
203 if ($badge->expireperiod < 60) {
204 $display .= get_string('expireperiods', 'badges', round($badge->expireperiod, 2));
205 } else if ($badge->expireperiod < 60 * 60) {
206 $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod / 60, 2));
207 } else if ($badge->expireperiod < 60 * 60 * 24) {
208 $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod / 60 / 60, 2));
209 } else {
210 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2));
211 }
212 }
213 } else {
214 $display .= get_string('noexpiry', 'badges');
215 }
27806552
YB
216
217 // Criteria details if any.
cd243d94 218 $display .= $this->heading(get_string('bcriteria', 'badges'), 3);
27806552
YB
219 if ($badge->has_criteria()) {
220 $display .= self::print_badge_criteria($badge);
221 } else {
222 $display .= get_string('nocriteria', 'badges');
223 if (has_capability('moodle/badges:configurecriteria', $context)) {
224 $display .= $this->output->single_button(
225 new moodle_url('/badges/criteria.php', array('id' => $badge->id)),
226 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
227 }
228 }
27806552
YB
229
230 // Awards details if any.
231 if (has_capability('moodle/badges:viewawarded', $context)) {
cd243d94 232 $display .= $this->heading(get_string('awards', 'badges'), 3);
27806552
YB
233 if ($badge->has_awards()) {
234 $url = new moodle_url('/badges/recipients.php', array('id' => $badge->id));
235 $a = new stdClass();
236 $a->link = $url->out();
237 $a->count = count($badge->get_awards());
238 $display .= get_string('numawards', 'badges', $a);
239 } else {
240 $display .= get_string('noawards', 'badges');
241 }
242
243 if (has_capability('moodle/badges:awardbadge', $context) &&
244 $badge->has_manual_award_criteria() &&
245 $badge->is_active()) {
246 $display .= $this->output->single_button(
247 new moodle_url('/badges/award.php', array('id' => $badge->id)),
248 get_string('award', 'badges'), 'POST', array('class' => 'activatebadge'));
249 }
27806552
YB
250 }
251
d363a5c2
TT
252 $display .= self::print_badge_endorsement($badge);
253 $display .= self::print_badge_related($badge);
e8bfd9b4 254 $display .= self::print_badge_alignments($badge);
d363a5c2 255
cd243d94 256 return html_writer::div($display, null, array('id' => 'badge-overview'));
27806552
YB
257 }
258
259 // Prints action icons for the badge.
260 public function print_badge_table_actions($badge, $context) {
261 $actions = "";
262
4d7d17dd 263 if (has_capability('moodle/badges:configuredetails', $context) && $badge->has_criteria()) {
27806552
YB
264 // Activate/deactivate badge.
265 if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) {
4d7d17dd
YB
266 // "Activate" will go to another page and ask for confirmation.
267 $url = new moodle_url('/badges/action.php');
268 $url->param('id', $badge->id);
269 $url->param('activate', true);
c5177654 270 $url->param('sesskey', sesskey());
4d7d17dd
YB
271 $return = new moodle_url(qualified_me());
272 $url->param('return', $return->out_as_local_url(false));
27806552
YB
273 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
274 } else {
275 $url = new moodle_url(qualified_me());
276 $url->param('lock', $badge->id);
277 $url->param('sesskey', sesskey());
278 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
279 }
280 }
281
282 // Award badge manually.
283 if ($badge->has_manual_award_criteria() &&
284 has_capability('moodle/badges:awardbadge', $context) &&
285 $badge->is_active()) {
286 $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
287 $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
288 }
289
290 // Edit badge.
291 if (has_capability('moodle/badges:configuredetails', $context)) {
aae219ac 292 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'badge'));
27806552
YB
293 $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
294 }
295
296 // Duplicate badge.
297 if (has_capability('moodle/badges:createbadge', $context)) {
298 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey()));
299 $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
300 }
301
302 // Delete badge.
303 if (has_capability('moodle/badges:deletebadge', $context)) {
304 $url = new moodle_url(qualified_me());
305 $url->param('delete', $badge->id);
306 $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
307 }
308
309 return $actions;
310 }
311
1837b1d5
DW
312 /**
313 * Render an issued badge.
314 *
315 * @param \core_badges\output\issued_badge $ibadge
316 * @return string
317 */
aae219ac 318 protected function render_issued_badge(\core_badges\output\issued_badge $ibadge) {
762b2a6b 319 global $USER, $CFG, $DB, $SITE;
27806552 320 $issued = $ibadge->issued;
737a352f 321 $userinfo = $ibadge->recipient;
853e506a 322 $badgeclass = $ibadge->badgeclass;
27806552 323 $badge = new badge($ibadge->badgeid);
853e506a 324 $now = time();
cd243d94 325 $expiration = isset($issued['expires']) ? $issued['expires'] : $now + 86400;
d363a5c2
TT
326 $badgeimage = is_array($badgeclass['image']) ? $badgeclass['image']['id'] : $badgeclass['image'];
327 $languages = get_string_manager()->get_list_of_languages();
27806552 328
cd243d94
Y
329 $output = '';
330 $output .= html_writer::start_tag('div', array('id' => 'badge'));
331 $output .= html_writer::start_tag('div', array('id' => 'badge-image'));
7444ba74 332 $output .= html_writer::empty_tag('img', array('src' => $badgeimage, 'alt' => $badge->name, 'width' => '100'));
cd243d94
Y
333 if ($expiration < $now) {
334 $output .= $this->output->pix_icon('i/expired',
335 get_string('expireddate', 'badges', userdate($issued['expires'])),
336 'moodle',
337 array('class' => 'expireimage'));
338 }
27806552 339
737a352f 340 if ($USER->id == $userinfo->id && !empty($CFG->enablebadges)) {
cd243d94 341 $output .= $this->output->single_button(
d03d1109 342 new moodle_url('/badges/badge.php', array('hash' => $ibadge->hash, 'bake' => true)),
e2805314 343 get_string('download'),
cd243d94 344 'POST');
853e506a 345 if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $now) && badges_user_has_backpack($USER->id)) {
aae219ac
DW
346
347 if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
d03d1109 348 $assertion = new moodle_url('/badges/assertion.php', array('b' => $ibadge->hash));
aae219ac
DW
349 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
350 $attributes = array(
351 'type' => 'button',
352 'class' => 'btn btn-secondary m-1',
353 'id' => 'addbutton',
354 'value' => get_string('addtobackpack', 'badges'));
355 $tobackpack = html_writer::tag('input', '', $attributes);
356 $this->output->add_action_handler($action, 'addbutton');
357 $output .= $tobackpack;
358 } else {
15a00bea
TT
359 if (badges_open_badges_backpack_api() == OPEN_BADGES_V2P1) {
360 $assertion = new moodle_url('/badges/backpack-export.php', array('hash' => $ibadge->hash));
361 } else {
362 $assertion = new moodle_url('/badges/backpack-add.php', array('hash' => $ibadge->hash));
363 }
aae219ac
DW
364 $attributes = ['class' => 'btn btn-secondary m-1', 'role' => 'button'];
365 $tobackpack = html_writer::link($assertion, get_string('addtobackpack', 'badges'), $attributes);
366 $output .= $tobackpack;
367 }
27806552 368 }
e2805314 369 }
cd243d94 370 $output .= html_writer::end_tag('div');
737a352f 371
cd243d94 372 $output .= html_writer::start_tag('div', array('id' => 'badge-details'));
737a352f 373 // Recipient information.
cd243d94
Y
374 $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3);
375 $dl = array();
762b2a6b
YB
376 if ($userinfo->deleted) {
377 $strdata = new stdClass();
378 $strdata->user = fullname($userinfo);
379 $strdata->site = format_string($SITE->fullname, true, array('context' => context_system::instance()));
cd243d94
Y
380
381 $dl[get_string('name')] = get_string('error:userdeleted', 'badges', $strdata);
737a352f 382 } else {
cd243d94 383 $dl[get_string('name')] = fullname($userinfo);
737a352f 384 }
cd243d94 385 $output .= $this->definition_list($dl);
737a352f 386
cd243d94
Y
387 $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3);
388 $dl = array();
389 $dl[get_string('issuername', 'badges')] = $badge->issuername;
e2805314 390 if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
cd243d94 391 $dl[get_string('contact', 'badges')] = obfuscate_mailto($badge->issuercontact);
e2805314 392 }
cd243d94
Y
393 $output .= $this->definition_list($dl);
394
395 $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3);
396 $dl = array();
397 $dl[get_string('name')] = $badge->name;
674c00ed
AG
398 if (!empty($badge->version)) {
399 $dl[get_string('version', 'badges')] = $badge->version;
400 }
706c8d19
SA
401 if (!empty($badge->language)) {
402 $dl[get_string('language')] = $languages[$badge->language];
403 }
cd243d94 404 $dl[get_string('description', 'badges')] = $badge->description;
674c00ed
AG
405 if (!empty($badge->imageauthorname)) {
406 $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname;
407 }
408 if (!empty($badge->imageauthoremail)) {
409 $dl[get_string('imageauthoremail', 'badges')] =
410 html_writer::tag('a', $badge->imageauthoremail, array('href' => 'mailto:' . $badge->imageauthoremail));
411 }
412 if (!empty($badge->imageauthorurl)) {
413 $dl[get_string('imageauthorurl', 'badges')] =
414 html_writer::link($badge->imageauthorurl, $badge->imageauthorurl, array('target' => '_blank'));
415 }
416 if (!empty($badge->imagecaption)) {
417 $dl[get_string('imagecaption', 'badges')] = $badge->imagecaption;
418 }
27806552 419
e2805314
YB
420 if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
421 $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid));
cd243d94 422 $dl[get_string('course')] = $coursename;
e2805314 423 }
cd243d94
Y
424 $dl[get_string('bcriteria', 'badges')] = self::print_badge_criteria($badge);
425 $output .= $this->definition_list($dl);
27806552 426
cd243d94
Y
427 $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3);
428 $dl = array();
39f4caf3
SA
429 if (!is_numeric($issued['issuedOn'])) {
430 $issued['issuedOn'] = strtotime($issued['issuedOn']);
431 }
cd243d94 432 $dl[get_string('dateawarded', 'badges')] = userdate($issued['issuedOn']);
e2805314 433 if (isset($issued['expires'])) {
39f4caf3
SA
434 if (!is_numeric($issued['expires'])) {
435 $issued['expires'] = strtotime($issued['expires']);
436 }
853e506a 437 if ($issued['expires'] < $now) {
cd243d94 438 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']) . get_string('warnexpired', 'badges');
e2805314 439
e2805314 440 } else {
cd243d94 441 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']);
27806552 442 }
e2805314 443 }
27806552 444
e2805314
YB
445 // Print evidence.
446 $agg = $badge->get_aggregation_methods();
737a352f 447 $evidence = $badge->get_criteria_completions($userinfo->id);
484b43f4
MG
448 $eids = array_map(function($o) {
449 return $o->critid;
450 }, $evidence);
e2805314
YB
451 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
452
453 $items = array();
454 foreach ($badge->criteria as $type => $c) {
455 if (in_array($c->id, $eids)) {
456 if (count($c->params) == 1) {
457 $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
458 } else {
459 $items[] = get_string('criteria_descr_' . $type , 'badges',
ae17e383 460 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
27806552
YB
461 }
462 }
e2805314 463 }
27806552 464
cd243d94
Y
465 $dl[get_string('evidence', 'badges')] = get_string('completioninfo', 'badges') . html_writer::alist($items, array(), 'ul');
466 $output .= $this->definition_list($dl);
c693beb3
AG
467 $endorsement = $badge->get_endorsement();
468 if (!empty($endorsement)) {
469 $output .= self::print_badge_endorsement($badge);
470 }
263a5833
DW
471
472 $relatedbadges = $badge->get_related_badges(true);
473 $items = array();
474 foreach ($relatedbadges as $related) {
475 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id));
476 $items[] = html_writer::link($relatedurl->out(), $related->name, array('target' => '_blank'));
477 }
478 if (!empty($items)) {
c693beb3 479 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
d363a5c2 480 $output .= html_writer::alist($items, array(), 'ul');
d363a5c2 481 }
263a5833 482
e8bfd9b4
DW
483 $alignments = $badge->get_alignments();
484 if (!empty($alignments)) {
c693beb3 485 $output .= $this->heading(get_string('alignment', 'badges'), 3);
d363a5c2 486 $items = array();
e8bfd9b4
DW
487 foreach ($alignments as $alignment) {
488 $items[] = html_writer::link($alignment->targeturl, $alignment->targetname, array('target' => '_blank'));
d363a5c2
TT
489 }
490 $output .= html_writer::alist($items, array(), 'ul');
d363a5c2 491 }
cd243d94 492 $output .= html_writer::end_tag('div');
27806552 493
cd243d94 494 return $output;
27806552
YB
495 }
496
1837b1d5
DW
497 /**
498 * Render an external badge.
499 *
500 * @param \core_badges\output\external_badge $ibadge
501 * @return string
502 */
aae219ac 503 protected function render_external_badge(\core_badges\output\external_badge $ibadge) {
27806552
YB
504 $issued = $ibadge->issued;
505 $assertion = $issued->assertion;
506 $issuer = $assertion->badge->issuer;
737a352f 507 $userinfo = $ibadge->recipient;
27806552 508 $table = new html_table();
ea76b652 509 $today = strtotime(date('Y-m-d'));
27806552 510
cd243d94
Y
511 $output = '';
512 $output .= html_writer::start_tag('div', array('id' => 'badge'));
513 $output .= html_writer::start_tag('div', array('id' => 'badge-image'));
aae219ac
DW
514 if (isset($issued->imageUrl)) {
515 $issued->image = $issued->imageUrl;
516 }
7444ba74 517 $output .= html_writer::empty_tag('img', array('src' => $issued->image, 'width' => '100'));
ea76b652 518 if (isset($assertion->expires)) {
39f4caf3 519 $expiration = is_numeric($assertion->expires) ? $assertion->expires : strtotime($assertion->expires);
ea76b652
YB
520 if ($expiration < $today) {
521 $output .= $this->output->pix_icon('i/expired',
522 get_string('expireddate', 'badges', userdate($expiration)),
523 'moodle',
524 array('class' => 'expireimage'));
525 }
cd243d94
Y
526 }
527 $output .= html_writer::end_tag('div');
27806552 528
cd243d94 529 $output .= html_writer::start_tag('div', array('id' => 'badge-details'));
737a352f
YB
530
531 // Recipient information.
cd243d94
Y
532 $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3);
533 $dl = array();
737a352f
YB
534 // Technically, we should alway have a user at this point, but added an extra check just in case.
535 if ($userinfo) {
737a352f
YB
536 if (!$ibadge->valid) {
537 $notify = $this->output->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice');
cd243d94
Y
538 $dl[get_string('name')] = fullname($userinfo) . $notify;
539 } else {
540 $dl[get_string('name')] = fullname($userinfo);
737a352f
YB
541 }
542 } else {
543 $notify = $this->output->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice');
cd243d94 544 $dl[get_string('name')] = $notify;
737a352f 545 }
cd243d94
Y
546 $output .= $this->definition_list($dl);
547
548 $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3);
549 $dl = array();
ea76b652 550 $dl[get_string('issuername', 'badges')] = s($issuer->name);
aae219ac
DW
551 if (isset($issuer->origin)) {
552 $dl[get_string('issuerurl', 'badges')] = html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin));
553 }
737a352f 554
27806552 555 if (isset($issuer->contact)) {
cd243d94
Y
556 $dl[get_string('contact', 'badges')] = obfuscate_mailto($issuer->contact);
557 }
558 $output .= $this->definition_list($dl);
559
560 $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3);
561 $dl = array();
ea76b652
YB
562 $dl[get_string('name')] = s($assertion->badge->name);
563 $dl[get_string('description', 'badges')] = s($assertion->badge->description);
aae219ac
DW
564 if (isset($assertion->badge->criteria)) {
565 $dl[get_string('bcriteria', 'badges')] = html_writer::tag(
566 'a',
567 s($assertion->badge->criteria),
568 array('href' => $assertion->badge->criteria)
569 );
570 }
cd243d94
Y
571 $output .= $this->definition_list($dl);
572
cd243d94 573 $dl = array();
27806552 574 if (isset($assertion->issued_on)) {
39f4caf3 575 $issuedate = is_numeric($assertion->issued_on) ? $assertion->issued_on : strtotime($assertion->issued_on);
ea76b652 576 $dl[get_string('dateawarded', 'badges')] = userdate($issuedate);
27806552 577 }
ea76b652 578 if (isset($assertion->expires)) {
27806552 579 if ($expiration < $today) {
ea76b652 580 $dl[get_string('expirydate', 'badges')] = userdate($expiration) . get_string('warnexpired', 'badges');
27806552 581 } else {
ea76b652 582 $dl[get_string('expirydate', 'badges')] = userdate($expiration);
27806552
YB
583 }
584 }
585 if (isset($assertion->evidence)) {
aae219ac
DW
586 $dl[get_string('evidence', 'badges')] = html_writer::tag(
587 'a',
588 s($assertion->evidence),
589 array('href' => $assertion->evidence)
590 );
591 }
592 if (!empty($dl)) {
593 $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3);
594 $output .= $this->definition_list($dl);
27806552 595 }
cd243d94 596 $output .= html_writer::end_tag('div');
27806552 597
cd243d94 598 return $output;
27806552
YB
599 }
600
1837b1d5
DW
601 /**
602 * Render a collection of user badges.
603 *
604 * @param \core_badges\output\badge_user_collection $badges
605 * @return string
606 */
aae219ac 607 protected function render_badge_user_collection(\core_badges\output\badge_user_collection $badges) {
1dcd0d34 608 global $CFG, $USER, $SITE;
e2805314
YB
609 $backpack = $badges->backpack;
610 $mybackpack = new moodle_url('/badges/mybackpack.php');
611
27806552
YB
612 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
613 $htmlpagingbar = $this->render($paging);
614
e2805314
YB
615 // Set backpack connection string.
616 $backpackconnect = '';
617 if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) {
618 $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
619 }
27806552
YB
620 // Search box.
621 $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
622
623 // Download all button.
aae219ac 624 $actionhtml = $this->output->single_button(
27806552
YB
625 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
626 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
aae219ac
DW
627 $downloadall = $this->output->box('', 'col-md-3');
628 $downloadall .= $this->output->box($actionhtml, 'col-md-9');
4394f9e3 629 $downloadall = $this->output->box($downloadall, 'row ml-5');
27806552
YB
630
631 // Local badges.
508e756e 632 $localhtml = html_writer::start_tag('div', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
aae219ac
DW
633 $sitename = format_string($SITE->fullname, true, array('context' => context_system::instance()));
634 $heading = get_string('localbadges', 'badges', $sitename);
508e756e 635 $localhtml .= $this->output->heading_with_help($heading, 'localbadgesh', 'badges');
27806552 636 if ($badges->badges) {
aae219ac 637 $countmessage = $this->output->box(get_string('badgesearned', 'badges', $badges->totalcount));
27806552
YB
638
639 $htmllist = $this->print_badges_list($badges->badges, $USER->id);
1837b1d5
DW
640 $localhtml .= $backpackconnect . $countmessage . $searchform;
641 $localhtml .= $htmlpagingbar . $htmllist . $htmlpagingbar . $downloadall;
27806552
YB
642 } else {
643 $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
644 }
508e756e 645 $localhtml .= html_writer::end_tag('div');
27806552
YB
646
647 // External badges.
27806552 648 $externalhtml = "";
60d72efb 649 if (!empty($CFG->badges_allowexternalbackpack)) {
508e756e
BB
650 $externalhtml .= html_writer::start_tag('div', array('class' => 'generalbox'));
651 $externalhtml .= $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges');
27806552 652 if (!is_null($backpack)) {
e2805314 653 if ($backpack->totalcollections == 0) {
aae219ac 654 $externalhtml .= get_string('nobackpackcollectionssummary', 'badges', $backpack);
27806552 655 } else {
e2805314 656 if ($backpack->totalbadges == 0) {
aae219ac 657 $externalhtml .= get_string('nobackpackbadgessummary', 'badges', $backpack);
e2805314 658 } else {
aae219ac 659 $externalhtml .= get_string('backpackbadgessummary', 'badges', $backpack);
e2805314
YB
660 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
661 }
27806552 662 }
27806552 663 } else {
e2805314 664 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
27806552 665 }
27806552 666
508e756e 667 $externalhtml .= html_writer::end_tag('div');
aae219ac
DW
668 $attr = ['class' => 'btn btn-secondary'];
669 $label = get_string('backpackbadgessettings', 'badges');
670 $backpacksettings = html_writer::link(new moodle_url('/badges/mybackpack.php'), $label, $attr);
671 $actionshtml = $this->output->box('', 'col-md-3');
672 $actionshtml .= $this->output->box($backpacksettings, 'col-md-9');
4394f9e3 673 $actionshtml = $this->output->box($actionshtml, 'row ml-5');
aae219ac 674 $externalhtml .= $actionshtml;
27806552
YB
675 }
676
677 return $localhtml . $externalhtml;
678 }
679
1837b1d5
DW
680 /**
681 * Render a collection of badges.
682 *
683 * @param \core_badges\output\badge_collection $badges
684 * @return string
685 */
aae219ac 686 protected function render_badge_collection(\core_badges\output\badge_collection $badges) {
27806552
YB
687 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
688 $htmlpagingbar = $this->render($paging);
689 $table = new html_table();
6c4dbbf0 690 $table->attributes['class'] = 'table table-bordered table-striped';
27806552
YB
691
692 $sortbyname = $this->helper_sortable_heading(get_string('name'),
693 'name', $badges->sort, $badges->dir);
694 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
695 'dateissued', $badges->sort, $badges->dir);
696 $table->head = array(
697 get_string('badgeimage', 'badges'),
698 $sortbyname,
699 get_string('description', 'badges'),
700 get_string('bcriteria', 'badges'),
701 $sortbyawarded
702 );
703 $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
704
705 foreach ($badges->badges as $badge) {
706 $badgeimage = print_badge_image($badge, $this->page->context, 'large');
707 $name = $badge->name;
708 $description = $badge->description;
709 $criteria = self::print_badge_criteria($badge);
710 if ($badge->dateissued) {
a8dd22a7 711 $icon = new pix_icon('i/valid',
27806552
YB
712 get_string('dateearned', 'badges',
713 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
714 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
715 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
716 } else {
717 $awarded = "";
718 }
719 $row = array($badgeimage, $name, $description, $criteria, $awarded);
720 $table->data[] = $row;
721 }
722
723 $htmltable = html_writer::table($table);
724
725 return $htmlpagingbar . $htmltable . $htmlpagingbar;
726 }
727
1837b1d5
DW
728 /**
729 * Render a table of badges.
730 *
731 * @param \core_badges\output\badge_management $badges
732 * @return string
733 */
aae219ac 734 protected function render_badge_management(\core_badges\output\badge_management $badges) {
27806552
YB
735 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
736
737 // New badge button.
19a9f2ea
YB
738 $htmlnew = '';
739 if (has_capability('moodle/badges:createbadge', $this->page->context)) {
740 $n['type'] = $this->page->url->get_param('type');
741 $n['id'] = $this->page->url->get_param('id');
1837b1d5 742 $btn = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
aae219ac 743 $htmlnew = $this->output->box($btn);
19a9f2ea 744 }
27806552
YB
745
746 $htmlpagingbar = $this->render($paging);
747 $table = new html_table();
6c4dbbf0 748 $table->attributes['class'] = 'table table-bordered table-striped';
27806552
YB
749
750 $sortbyname = $this->helper_sortable_heading(get_string('name'),
751 'name', $badges->sort, $badges->dir);
752 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
753 'status', $badges->sort, $badges->dir);
754 $table->head = array(
755 $sortbyname,
756 $sortbystatus,
757 get_string('bcriteria', 'badges'),
758 get_string('awards', 'badges'),
759 get_string('actions')
760 );
761 $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
762
763 foreach ($badges->badges as $b) {
764 $style = !$b->is_active() ? array('class' => 'dimmed') : array();
765 $forlink = print_badge_image($b, $this->page->context) . ' ' .
766 html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
767 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
768 $status = $b->statstring;
769 $criteria = self::print_badge_criteria($b, 'short');
770
771 if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
772 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
773 } else {
774 $awards = $b->awards;
775 }
776
777 $actions = self::print_badge_table_actions($b, $this->page->context);
778
779 $row = array($name, $status, $criteria, $awards, $actions);
780 $table->data[] = $row;
781 }
782 $htmltable = html_writer::table($table);
783
784 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
785 }
786
1837b1d5
DW
787 /**
788 * Prints tabs for badge editing.
789 *
790 * @param integer $badgeid The badgeid to edit.
791 * @param context $context The current context.
792 * @param string $current The currently selected tab.
793 * @return string
794 */
27806552
YB
795 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
796 global $DB;
797
d363a5c2 798 $badge = new badge($badgeid);
a5f82c5b 799 $row = array();
27806552
YB
800
801 $row[] = new tabobject('overview',
802 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
803 get_string('boverview', 'badges')
804 );
805
806 if (has_capability('moodle/badges:configuredetails', $context)) {
807 $row[] = new tabobject('details',
aae219ac 808 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'badge')),
27806552
YB
809 get_string('bdetails', 'badges')
810 );
811 }
812
813 if (has_capability('moodle/badges:configurecriteria', $context)) {
814 $row[] = new tabobject('criteria',
815 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
816 get_string('bcriteria', 'badges')
817 );
818 }
819
820 if (has_capability('moodle/badges:configuremessages', $context)) {
821 $row[] = new tabobject('message',
822 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
823 get_string('bmessage', 'badges')
824 );
825 }
826
827 if (has_capability('moodle/badges:viewawarded', $context)) {
7f964cfd
YB
828 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
829 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
830 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
27806552
YB
831 $row[] = new tabobject('awards',
832 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
833 get_string('bawards', 'badges', $awarded)
834 );
835 }
836
d363a5c2
TT
837 if (has_capability('moodle/badges:configuredetails', $context)) {
838 $row[] = new tabobject('bendorsement',
839 new moodle_url('/badges/endorsement.php', array('id' => $badgeid)),
840 get_string('bendorsement', 'badges')
841 );
842 }
843
844 if (has_capability('moodle/badges:configuredetails', $context)) {
c6607726
JD
845 $sql = "SELECT COUNT(br.badgeid)
846 FROM {badge_related} br
847 WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
848 $related = $DB->count_records_sql($sql, ['badgeid' => $badgeid, 'badgeid2' => $badgeid]);
d363a5c2
TT
849 $row[] = new tabobject('brelated',
850 new moodle_url('/badges/related.php', array('id' => $badgeid)),
851 get_string('brelated', 'badges', $related)
852 );
853 }
854
855 if (has_capability('moodle/badges:configuredetails', $context)) {
e8bfd9b4
DW
856 $alignments = $DB->count_records_sql("SELECT COUNT(bc.id)
857 FROM {badge_alignment} bc WHERE bc.badgeid = :badgeid", array('badgeid' => $badgeid));
d363a5c2 858 $row[] = new tabobject('balignment',
e8bfd9b4
DW
859 new moodle_url('/badges/alignment.php', array('id' => $badgeid)),
860 get_string('balignment', 'badges', $alignments)
d363a5c2
TT
861 );
862 }
863
a5f82c5b 864 echo $this->tabtree($row, $current);
27806552
YB
865 }
866
36388ba8
AD
867 /**
868 * Prints badge status box.
1837b1d5
DW
869 *
870 * @param badge $badge
36388ba8
AD
871 * @return Either the status box html as a string or null
872 */
27806552 873 public function print_badge_status_box(badge $badge) {
27806552 874 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
36388ba8 875
27806552
YB
876 if (!$badge->has_criteria()) {
877 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
878 $status = get_string('nocriteria', 'badges');
879 if ($this->page->url != $criteriaurl) {
880 $action = $this->output->single_button(
881 $criteriaurl,
882 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
883 } else {
884 $action = '';
885 }
cd243d94
Y
886
887 $message = $status . $action;
27806552
YB
888 } else {
889 $status = get_string('statusmessage_' . $badge->status, 'badges');
890 if ($badge->is_active()) {
891 $action = $this->output->single_button(new moodle_url('/badges/action.php',
892 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
893 'return' => $this->page->url->out_as_local_url(false))),
894 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
895 } else {
896 $action = $this->output->single_button(new moodle_url('/badges/action.php',
897 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
898 'return' => $this->page->url->out_as_local_url(false))),
899 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
900 }
cd243d94
Y
901
902 $message = $status . $this->output->help_icon('status', 'badges') . $action;
903
27806552 904 }
27806552 905
36388ba8 906 $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
cd243d94 907 return $this->output->box($message, $style);
36388ba8 908 }
27806552 909
36388ba8 910 return null;
27806552
YB
911 }
912
3784d3be
YB
913 /**
914 * Returns information about badge criteria in a list form.
915 *
916 * @param badge $badge Badge objects
917 * @param string $short Indicates whether to print full info about this badge
918 * @return string $output HTML string to output
919 */
27806552 920 public function print_badge_criteria(badge $badge, $short = '') {
27806552
YB
921 $agg = $badge->get_aggregation_methods();
922 if (empty($badge->criteria)) {
923 return get_string('nocriteria', 'badges');
3784d3be
YB
924 }
925
926 $overalldescr = '';
0b6ba27a
Y
927 $overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL];
928 if (!$short && !empty($overall->description)) {
929 $overalldescr = $this->output->box(
fffeb03f 930 format_text($overall->description, $overall->descriptionformat, array('context' => $badge->get_context())),
0b6ba27a
Y
931 'criteria-description'
932 );
3784d3be 933 }
0b6ba27a
Y
934
935 // Get the condition string.
3784d3be 936 if (count($badge->criteria) == 2) {
0b6ba27a 937 $condition = '';
27806552 938 if (!$short) {
0b6ba27a 939 $condition = get_string('criteria_descr', 'badges');
27806552
YB
940 }
941 } else {
0b6ba27a 942 $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
3784d3be 943 core_text::strtoupper($agg[$badge->get_aggregation_method()]));
27806552 944 }
0b6ba27a 945
27806552 946 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
0b6ba27a
Y
947
948 $items = array();
949 // If only one criterion left, make sure its description goe to the top.
950 if (count($badge->criteria) == 1) {
951 $c = reset($badge->criteria);
952 if (!$short && !empty($c->description)) {
953 $overalldescr = $this->output->box(
fffeb03f 954 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
0b6ba27a
Y
955 'criteria-description'
956 );
3784d3be 957 }
27806552 958 if (count($c->params) == 1) {
0b6ba27a 959 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype , 'badges') .
3784d3be 960 $c->get_details($short);
27806552 961 } else {
0b6ba27a
Y
962 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype, 'badges',
963 core_text::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype)])) .
964 $c->get_details($short);
965 }
966 } else {
967 foreach ($badge->criteria as $type => $c) {
968 $criteriadescr = '';
969 if (!$short && !empty($c->description)) {
970 $criteriadescr = $this->output->box(
fffeb03f 971 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
0b6ba27a
Y
972 'criteria-description'
973 );
974 }
975 if (count($c->params) == 1) {
976 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
977 $c->get_details($short) . $criteriadescr;
978 } else {
979 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
980 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) .
981 $c->get_details($short) .
982 $criteriadescr;
983 }
27806552
YB
984 }
985 }
0b6ba27a
Y
986
987 return $overalldescr . $condition . html_writer::alist($items, array(), 'ul');;
27806552
YB
988 }
989
1837b1d5
DW
990 /**
991 * Prints criteria actions for badge editing.
992 *
993 * @param badge $badge
994 * @return string
995 */
27806552 996 public function print_criteria_actions(badge $badge) {
32bd11cb 997 $output = '';
27806552
YB
998 if (!$badge->is_active() && !$badge->is_locked()) {
999 $accepted = $badge->get_accepted_criteria();
1000 $potential = array_diff($accepted, array_keys($badge->criteria));
1001
1002 if (!empty($potential)) {
1003 foreach ($potential as $p) {
1004 if ($p != 0) {
1005 $select[$p] = get_string('criteria_' . $p, 'badges');
1006 }
1007 }
32bd11cb
BB
1008 $output .= $this->output->single_select(
1009 new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id, 'add' => true)),
1010 'type',
1011 $select,
1012 '',
1013 array('' => 'choosedots'),
1014 null,
1015 array('label' => get_string('addbadgecriteria', 'badges'))
1016 );
27806552 1017 } else {
32bd11cb 1018 $output .= $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
27806552
YB
1019 }
1020 }
1021
32bd11cb 1022 return $output;
27806552
YB
1023 }
1024
1837b1d5
DW
1025 /**
1026 * Renders a table with users who have earned the badge.
1027 * Based on stamps collection plugin.
1028 *
1029 * @param \core_badges\output\badge_recipients $recipients
1030 * @return string
1031 */
aae219ac 1032 protected function render_badge_recipients(\core_badges\output\badge_recipients $recipients) {
27806552
YB
1033 $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
1034 $htmlpagingbar = $this->render($paging);
1035 $table = new html_table();
f8ba185a 1036 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
27806552
YB
1037
1038 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
1039 'firstname', $recipients->sort, $recipients->dir);
1040 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
1041 'lastname', $recipients->sort, $recipients->dir);
1042 if ($this->helper_fullname_format() == 'lf') {
1043 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
1044 } else {
1045 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
1046 }
1047
1048 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
1049 'dateissued', $recipients->sort, $recipients->dir);
1050
1051 $table->head = array($sortbyname, $sortbydate, '');
1052
1053 foreach ($recipients->userids as $holder) {
1054 $fullname = fullname($holder);
1055 $fullname = html_writer::link(
1056 new moodle_url('/user/profile.php', array('id' => $holder->userid)),
1057 $fullname
1058 );
1059 $awarded = userdate($holder->dateissued);
1060 $badgeurl = html_writer::link(
1061 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
1062 get_string('viewbadge', 'badges')
1063 );
1064
1065 $row = array($fullname, $awarded, $badgeurl);
1066 $table->data[] = $row;
1067 }
1068
1069 $htmltable = html_writer::table($table);
1070
1071 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1072 }
1073
1074 ////////////////////////////////////////////////////////////////////////////
1075 // Helper methods
1076 // Reused from stamps collection plugin
1077 ////////////////////////////////////////////////////////////////////////////
1078
1079 /**
1080 * Renders a text with icons to sort by the given column
1081 *
1082 * This is intended for table headings.
1083 *
1084 * @param string $text The heading text
1085 * @param string $sortid The column id used for sorting
1086 * @param string $sortby Currently sorted by (column id)
1087 * @param string $sorthow Currently sorted how (ASC|DESC)
1088 *
1089 * @return string
1090 */
1091 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
1092 $out = html_writer::tag('span', $text, array('class' => 'text'));
1093
1094 if (!is_null($sortid)) {
1095 if ($sortby !== $sortid || $sorthow !== 'ASC') {
1096 $url = new moodle_url($this->page->url);
1097 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
1098 $out .= $this->output->action_icon($url,
1099 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
1100 }
1101 if ($sortby !== $sortid || $sorthow !== 'DESC') {
1102 $url = new moodle_url($this->page->url);
1103 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
1104 $out .= $this->output->action_icon($url,
1105 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
1106 }
1107 }
1108 return $out;
1109 }
1110 /**
1111 * Tries to guess the fullname format set at the site
1112 *
1113 * @return string fl|lf
1114 */
1115 protected function helper_fullname_format() {
1116 $fake = new stdClass();
1117 $fake->lastname = 'LLLL';
1118 $fake->firstname = 'FFFF';
1119 $fullname = get_string('fullnamedisplay', '', $fake);
1120 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
1121 return 'lf';
1122 } else {
1123 return 'fl';
1124 }
1125 }
1126 /**
1127 * Renders a search form
1128 *
1129 * @param string $search Search string
1130 * @return string HTML
1131 */
1132 protected function helper_search_form($search) {
1133 global $CFG;
1134 require_once($CFG->libdir . '/formslib.php');
1135
1136 $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
1137
1138 $mform->addElement('hidden', 'sesskey', sesskey());
1139
1140 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
1141 $mform->setDefault('search', $search);
1142 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
1143 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
1144 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
1145
1146 ob_start();
1147 $mform->display();
1148 $out = ob_get_clean();
1149
1150 return $out;
1151 }
cd243d94
Y
1152
1153 /**
1154 * Renders a definition list
1155 *
1156 * @param array $items the list of items to define
1157 * @param array
1158 */
1159 protected function definition_list(array $items, array $attributes = array()) {
1160 $output = html_writer::start_tag('dl', $attributes);
1161 foreach ($items as $label => $value) {
1162 $output .= html_writer::tag('dt', $label);
1163 $output .= html_writer::tag('dd', $value);
1164 }
1165 $output .= html_writer::end_tag('dl');
1166 return $output;
1167 }
d363a5c2
TT
1168
1169 /**
1170 * Outputs list en badges.
1171 *
1172 * @param badge $badge Badge object.
1173 * @return string $output content endorsement to output.
1174 */
1175 protected function print_badge_endorsement(badge $badge) {
1176 $output = '';
1177 $endorsement = $badge->get_endorsement();
1178 $dl = array();
1179 $output .= $this->heading(get_string('endorsement', 'badges'), 3);
1180 if (!empty($endorsement)) {
1181 $dl[get_string('issuername', 'badges')] = $endorsement->issuername;
65ffd0a3
PFO
1182 $dl[get_string('issueremail', 'badges')] =
1183 html_writer::tag('a', $endorsement->issueremail, array('href' => 'mailto:' . $endorsement->issueremail));
d363a5c2
TT
1184 $dl[get_string('issuerurl', 'badges')] = html_writer::link($endorsement->issuerurl, $endorsement->issuerurl,
1185 array('target' => '_blank'));
750f1588 1186 $dl[get_string('dateawarded', 'badges')] = userdate($endorsement->dateissued);
d363a5c2
TT
1187 $dl[get_string('claimid', 'badges')] = html_writer::link($endorsement->claimid, $endorsement->claimid,
1188 array('target' => '_blank'));
1189 $dl[get_string('claimcomment', 'badges')] = $endorsement->claimcomment;
1190 $output .= $this->definition_list($dl);
1191 } else {
1192 $output .= get_string('noendorsement', 'badges');
1193 }
1194 return $output;
1195 }
1196
1197 /**
1198 * Print list badges related.
1199 *
1200 * @param badge $badge Badge objects.
1201 * @return string $output List related badges to output.
1202 */
1203 protected function print_badge_related(badge $badge) {
1204 $output = '';
1205 $relatedbadges = $badge->get_related_badges();
1206 $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
1207 if (!empty($relatedbadges)) {
1208 $items = array();
1209 foreach ($relatedbadges as $related) {
1210 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id));
1211 $items[] = html_writer::link($relatedurl->out(), $related->name, array('target' => '_blank'));
1212 }
1213 $output .= html_writer::alist($items, array(), 'ul');
1214 } else {
1215 $output .= get_string('norelated', 'badges');
1216 }
1217 return $output;
1218 }
1219
1220 /**
e8bfd9b4 1221 * Print list badge alignments.
d363a5c2
TT
1222 *
1223 * @param badge $badge Badge objects.
e8bfd9b4 1224 * @return string $output List alignments to output.
d363a5c2 1225 */
e8bfd9b4 1226 protected function print_badge_alignments(badge $badge) {
d363a5c2
TT
1227 $output = '';
1228 $output .= $this->heading(get_string('alignment', 'badges'), 3);
e8bfd9b4
DW
1229 $alignments = $badge->get_alignments();
1230 if (!empty($alignments)) {
d363a5c2 1231 $items = array();
e8bfd9b4
DW
1232 foreach ($alignments as $alignment) {
1233 $urlaligment = new moodle_url('alignment.php',
1234 array('id' => $badge->id, 'alignmentid' => $alignment->id)
d363a5c2 1235 );
e8bfd9b4 1236 $items[] = html_writer::link($urlaligment, $alignment->targetname, array('target' => '_blank'));
d363a5c2
TT
1237 }
1238 $output .= html_writer::alist($items, array(), 'ul');
1239 } else {
1240 $output .= get_string('noalignment', 'badges');
1241 }
1242 return $output;
1243 }
1244
1245 /**
1246 * Renders a table for related badges.
1247 *
aae219ac 1248 * @param \core_badges\output\badge_related $related list related badges.
d363a5c2
TT
1249 * @return string list related badges to output.
1250 */
aae219ac 1251 protected function render_badge_related(\core_badges\output\badge_related $related) {
d363a5c2
TT
1252 $currentbadge = new badge($related->currentbadgeid);
1253 $languages = get_string_manager()->get_list_of_languages();
1254 $paging = new paging_bar($related->totalcount, $related->page, $related->perpage, $this->page->url, 'page');
1255 $htmlpagingbar = $this->render($paging);
1256 $table = new html_table();
1257 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1258 $table->head = array(
1259 get_string('name'),
1260 get_string('version', 'badges'),
1261 get_string('language', 'badges'),
1262 get_string('type', 'badges')
1263 );
1264 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1265 array_push($table->head, '');
1266 }
1267
1268 foreach ($related->badges as $badge) {
1269 $badgeobject = new badge($badge->id);
1270 $style = array('title' => $badgeobject->name);
1271 if (!$badgeobject->is_active()) {
1272 $style['class'] = 'dimmed';
1273 }
1274 $context = ($badgeobject->type == BADGE_TYPE_SITE) ?
1275 context_system::instance() : context_course::instance($badgeobject->courseid);
1276 $forlink = print_badge_image($badgeobject, $context) . ' ' .
1277 html_writer::start_tag('span') . $badgeobject->name . html_writer::end_tag('span');
1278 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $badgeobject->id)), $forlink, $style);
1279
1280 $row = array(
1281 $name,
1282 $badge->version,
1283 $badge->language ? $languages[$badge->language] : '',
1284 $badge->type == BADGE_TYPE_COURSE ? get_string('badgesview', 'badges') : get_string('sitebadges', 'badges')
1285 );
1286 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1287 $action = $this->output->action_icon(
1288 new moodle_url('related_action.php',
1289 array(
1290 'badgeid' => $related->currentbadgeid,
1291 'relatedid' => $badge->id,
1292 'action' => 'remove'
1293 )
1294 ), new pix_icon('t/delete', get_string('delete')));
1295 $actions = html_writer::tag('div', $action, array('class' => 'badge-actions'));
1296 array_push($row, $actions);
1297 }
1298 $table->data[] = $row;
1299 }
1300 $htmltable = html_writer::table($table);
1301
1302 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1303 }
1304
1305 /**
e8bfd9b4 1306 * Renders a table with alignment.
d363a5c2 1307 *
aae219ac 1308 * @param core_badges\output\badge_alignments $alignments List alignments.
e8bfd9b4 1309 * @return string List alignment to output.
d363a5c2 1310 */
aae219ac 1311 protected function render_badge_alignments(\core_badges\output\badge_alignments $alignments) {
e8bfd9b4
DW
1312 $currentbadge = new badge($alignments->currentbadgeid);
1313 $paging = new paging_bar($alignments->totalcount, $alignments->page, $alignments->perpage, $this->page->url, 'page');
d363a5c2
TT
1314 $htmlpagingbar = $this->render($paging);
1315 $table = new html_table();
1316 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1317 $table->head = array('Name', 'URL', '');
1318
e8bfd9b4
DW
1319 foreach ($alignments->alignments as $item) {
1320 $urlaligment = new moodle_url('alignment.php',
d363a5c2
TT
1321 array(
1322 'id' => $currentbadge->id,
1323 'alignmentid' => $item->id,
1324 )
1325 );
1326 $row = array(
1327 html_writer::link($urlaligment, $item->targetname),
1328 html_writer::link($item->targeturl, $item->targeturl, array('target' => '_blank'))
1329 );
1330 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1331 $delete = $this->output->action_icon(
e8bfd9b4 1332 new moodle_url('alignment_action.php',
d363a5c2
TT
1333 array(
1334 'id' => $currentbadge->id,
1335 'alignmentid' => $item->id,
1336 'action' => 'remove'
1337 )
1338 ), new pix_icon('t/delete', get_string('delete')));
1339 $edit = $this->output->action_icon(
e8bfd9b4 1340 new moodle_url('alignment.php',
d363a5c2
TT
1341 array(
1342 'id' => $currentbadge->id,
1343 'alignmentid' => $item->id,
1344 'action' => 'edit'
1345 )
1346 ), new pix_icon('t/edit', get_string('edit')));
1347 $actions = html_writer::tag('div', $edit . $delete, array('class' => 'badge-actions'));
1348 array_push($row, $actions);
1349 }
1350 $table->data[] = $row;
1351 }
1352 $htmltable = html_writer::table($table);
1353
1354 return $htmlpagingbar . $htmltable . $htmlpagingbar;
1355 }
737a352f 1356
27806552 1357 /**
aae219ac 1358 * Defer to template.
27806552 1359 *
aae219ac
DW
1360 * @param \core_badges\output\external_backpacks_page $page
1361 * @return bool|string
27806552 1362 */
aae219ac
DW
1363 public function render_external_backpacks_page(\core_badges\output\external_backpacks_page $page) {
1364 $data = $page->export_for_template($this);
1365 return parent::render_from_template('core_badges/external_backpacks_page', $data);
27806552 1366 }
3e567085
SA
1367
1368 /**
1369 * Get the result of a backpack validation with its settings. It returns:
1370 * - A informative message if the backpack version is different from OBv2.
1371 * - A warning with the error if it's not possible to connect to this backpack.
1372 * - A successful message if the connection has worked.
1373 *
1374 * @param int $backpackid The backpack identifier.
1375 * @return string A message with the validation result.
1376 */
1377 public function render_test_backpack_result(int $backpackid): string {
1378 // Get the backpack.
1379 $backpack = badges_get_site_backpack($backpackid);
1380
1381 // Add the header to the result.
1382 $result = $this->heading(get_string('testbackpack', 'badges', $backpack->backpackweburl));
1383
1384 if ($backpack->apiversion != OPEN_BADGES_V2) {
1385 // Only OBv2 supports this validation.
1386 $result .= get_string('backpackconnectionnottested', 'badges');
1387 } else {
1388 $message = badges_verify_backpack($backpackid);
1389 if (empty($message)) {
1390 $result .= get_string('backpackconnectionok', 'badges');
1391 } else {
1392 $result .= $message;
1393 }
1394 }
1395
1396 return $result;
1397 }
27806552 1398}