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
, '/', 'f1', false);
44 $bname = s($badge->assertion
->badge
->name
);
45 $imageurl = $badge->imageUrl
;
48 $name = html_writer
::tag('span', $bname, array('class' => 'badge-name'));
50 $image = html_writer
::empty_tag('img', array('src' => $imageurl, 'class' => 'badge-image'));
51 if (!empty($badge->dateexpire
) && $badge->dateexpire
< time()) {
52 $image .= $this->output
->pix_icon('i/expired',
53 get_string('expireddate', 'badges', userdate($badge->dateexpire
)),
55 array('class' => 'expireimage'));
56 $name .= '(' . get_string('expired', 'badges') . ')';
59 $download = $status = $push = '';
60 if (($userid == $USER->id
) && !$profile) {
61 $url = new moodle_url('mybadges.php', array('download' => $badge->id
, 'hash' => $badge->uniquehash
, 'sesskey' => sesskey()));
62 $notexpiredbadge = (empty($badge->dateexpire
) ||
$badge->dateexpire
> time());
63 $backpackexists = badges_user_has_backpack($USER->id
);
64 if (!empty($CFG->badges_allowexternalbackpack
) && $notexpiredbadge && $backpackexists) {
65 $assertion = new moodle_url('/badges/assertion.php', array('b' => $badge->uniquehash
));
66 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
67 $push = $this->output
->action_icon(new moodle_url('#'), new pix_icon('t/backpack', get_string('addtobackpack', 'badges')), $action);
70 $download = $this->output
->action_icon($url, new pix_icon('t/download', get_string('download')));
71 if ($badge->visible
) {
72 $url = new moodle_url('mybadges.php', array('hide' => $badge->issuedid
, 'sesskey' => sesskey()));
73 $status = $this->output
->action_icon($url, new pix_icon('t/hide', get_string('makeprivate', 'badges')));
75 $url = new moodle_url('mybadges.php', array('show' => $badge->issuedid
, 'sesskey' => sesskey()));
76 $status = $this->output
->action_icon($url, new pix_icon('t/show', get_string('makepublic', 'badges')));
81 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash
));
84 $url = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash
));
86 $hash = hash('md5', $badge->hostedUrl
);
87 $url = new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid));
90 $actions = html_writer
::tag('div', $push . $download . $status, array('class' => 'badge-actions'));
91 $items[] = html_writer
::link($url, $image . $actions . $name, array('title' => $bname));
94 return html_writer
::alist($items, array('class' => 'badges'));
97 // Recipients selection form.
98 public function recipients_selection_form(user_selector_base
$existinguc, user_selector_base
$potentialuc) {
100 $formattributes = array();
101 $formattributes['id'] = 'recipientform';
102 $formattributes['action'] = $this->page
->url
;
103 $formattributes['method'] = 'post';
104 $output .= html_writer
::start_tag('form', $formattributes);
105 $output .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
107 $existingcell = new html_table_cell();
108 $existingcell->text
= $existinguc->display(true);
109 $existingcell->attributes
['class'] = 'existing';
110 $actioncell = new html_table_cell();
111 $actioncell->text
= html_writer
::start_tag('div', array());
112 $actioncell->text
.= html_writer
::empty_tag('input', array(
115 'value' => $this->output
->larrow() . ' ' . get_string('award', 'badges'),
116 'class' => 'actionbutton')
118 $actioncell->text
.= html_writer
::empty_tag('input', array(
121 'value' => get_string('revoke', 'badges') . ' ' . $this->output
->rarrow(),
122 'class' => 'actionbutton')
124 $actioncell->text
.= html_writer
::end_tag('div', array());
125 $actioncell->attributes
['class'] = 'actions';
126 $potentialcell = new html_table_cell();
127 $potentialcell->text
= $potentialuc->display(true);
128 $potentialcell->attributes
['class'] = 'potential';
130 $table = new html_table();
131 $table->attributes
['class'] = 'recipienttable boxaligncenter';
132 $table->data
= array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
133 $output .= html_writer
::table($table);
135 $output .= html_writer
::end_tag('form');
139 // Prints a badge overview infomation.
140 public function print_badge_overview($badge, $context) {
145 $display .= $this->heading(get_string('badgedetails', 'badges'), 3);
147 $dl[get_string('name')] = $badge->name
;
148 $dl[get_string('description', 'badges')] = $badge->description
;
149 $dl[get_string('createdon', 'search')] = userdate($badge->timecreated
);
150 $dl[get_string('badgeimage', 'badges')] = print_badge_image($badge, $context, 'large');
151 $display .= $this->definition_list($dl);
154 $display .= $this->heading(get_string('issuerdetails', 'badges'), 3);
156 $dl[get_string('issuername', 'badges')] = $badge->issuername
;
157 $dl[get_string('contact', 'badges')] = html_writer
::tag('a', $badge->issuercontact
, array('href' => 'mailto:' . $badge->issuercontact
));
158 $display .= $this->definition_list($dl);
160 // Issuance details if any.
161 $display .= $this->heading(get_string('issuancedetails', 'badges'), 3);
162 if ($badge->can_expire()) {
163 if ($badge->expiredate
) {
164 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate
));
165 } else if ($badge->expireperiod
) {
166 if ($badge->expireperiod
< 60) {
167 $display .= get_string('expireperiods', 'badges', round($badge->expireperiod
, 2));
168 } else if ($badge->expireperiod
< 60 * 60) {
169 $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod
/ 60, 2));
170 } else if ($badge->expireperiod
< 60 * 60 * 24) {
171 $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod
/ 60 / 60, 2));
173 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod
/ 60 / 60 / 24, 2));
177 $display .= get_string('noexpiry', 'badges');
180 // Criteria details if any.
181 $display .= $this->heading(get_string('bcriteria', 'badges'), 3);
182 if ($badge->has_criteria()) {
183 $display .= self
::print_badge_criteria($badge);
185 $display .= get_string('nocriteria', 'badges');
186 if (has_capability('moodle/badges:configurecriteria', $context)) {
187 $display .= $this->output
->single_button(
188 new moodle_url('/badges/criteria.php', array('id' => $badge->id
)),
189 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
193 // Awards details if any.
194 if (has_capability('moodle/badges:viewawarded', $context)) {
195 $display .= $this->heading(get_string('awards', 'badges'), 3);
196 if ($badge->has_awards()) {
197 $url = new moodle_url('/badges/recipients.php', array('id' => $badge->id
));
199 $a->link
= $url->out();
200 $a->count
= count($badge->get_awards());
201 $display .= get_string('numawards', 'badges', $a);
203 $display .= get_string('noawards', 'badges');
206 if (has_capability('moodle/badges:awardbadge', $context) &&
207 $badge->has_manual_award_criteria() &&
208 $badge->is_active()) {
209 $display .= $this->output
->single_button(
210 new moodle_url('/badges/award.php', array('id' => $badge->id
)),
211 get_string('award', 'badges'), 'POST', array('class' => 'activatebadge'));
215 return html_writer
::div($display, null, array('id' => 'badge-overview'));
218 // Prints action icons for the badge.
219 public function print_badge_table_actions($badge, $context) {
222 if (has_capability('moodle/badges:configuredetails', $context) && $badge->has_criteria()) {
223 // Activate/deactivate badge.
224 if ($badge->status
== BADGE_STATUS_INACTIVE ||
$badge->status
== BADGE_STATUS_INACTIVE_LOCKED
) {
225 // "Activate" will go to another page and ask for confirmation.
226 $url = new moodle_url('/badges/action.php');
227 $url->param('id', $badge->id
);
228 $url->param('activate', true);
229 $url->param('sesskey', sesskey());
230 $return = new moodle_url(qualified_me());
231 $url->param('return', $return->out_as_local_url(false));
232 $actions .= $this->output
->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
234 $url = new moodle_url(qualified_me());
235 $url->param('lock', $badge->id
);
236 $url->param('sesskey', sesskey());
237 $actions .= $this->output
->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
241 // Award badge manually.
242 if ($badge->has_manual_award_criteria() &&
243 has_capability('moodle/badges:awardbadge', $context) &&
244 $badge->is_active()) {
245 $url = new moodle_url('/badges/award.php', array('id' => $badge->id
));
246 $actions .= $this->output
->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
250 if (has_capability('moodle/badges:configuredetails', $context)) {
251 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id
, 'action' => 'details'));
252 $actions .= $this->output
->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
256 if (has_capability('moodle/badges:createbadge', $context)) {
257 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id
, 'sesskey' => sesskey()));
258 $actions .= $this->output
->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
262 if (has_capability('moodle/badges:deletebadge', $context)) {
263 $url = new moodle_url(qualified_me());
264 $url->param('delete', $badge->id
);
265 $actions .= $this->output
->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
271 // Outputs issued badge with actions available.
272 protected function render_issued_badge(issued_badge
$ibadge) {
273 global $USER, $CFG, $DB, $SITE;
274 $issued = $ibadge->issued
;
275 $userinfo = $ibadge->recipient
;
276 $badgeclass = $ibadge->badgeclass
;
277 $badge = new badge($ibadge->badgeid
);
279 $expiration = isset($issued['expires']) ?
$issued['expires'] : $now +
86400;
282 $output .= html_writer
::start_tag('div', array('id' => 'badge'));
283 $output .= html_writer
::start_tag('div', array('id' => 'badge-image'));
284 $output .= html_writer
::empty_tag('img', array('src' => $badgeclass['image'], 'alt' => $badge->name
));
285 if ($expiration < $now) {
286 $output .= $this->output
->pix_icon('i/expired',
287 get_string('expireddate', 'badges', userdate($issued['expires'])),
289 array('class' => 'expireimage'));
292 if ($USER->id
== $userinfo->id
&& !empty($CFG->enablebadges
)) {
293 $output .= $this->output
->single_button(
294 new moodle_url('/badges/badge.php', array('hash' => $issued['uid'], 'bake' => true)),
295 get_string('download'),
297 if (!empty($CFG->badges_allowexternalbackpack
) && ($expiration > $now) && badges_user_has_backpack($USER->id
)) {
298 $assertion = new moodle_url('/badges/assertion.php', array('b' => $issued['uid']));
299 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
303 'value' => get_string('addtobackpack', 'badges'));
304 $tobackpack = html_writer
::tag('input', '', $attributes);
305 $this->output
->add_action_handler($action, 'addbutton');
306 $output .= $tobackpack;
309 $output .= html_writer
::end_tag('div');
311 $output .= html_writer
::start_tag('div', array('id' => 'badge-details'));
312 // Recipient information.
313 $output .= $this->output
->heading(get_string('recipientdetails', 'badges'), 3);
315 if ($userinfo->deleted
) {
316 $strdata = new stdClass();
317 $strdata->user
= fullname($userinfo);
318 $strdata->site
= format_string($SITE->fullname
, true, array('context' => context_system
::instance()));
320 $dl[get_string('name')] = get_string('error:userdeleted', 'badges', $strdata);
322 $dl[get_string('name')] = fullname($userinfo);
324 $output .= $this->definition_list($dl);
326 $output .= $this->output
->heading(get_string('issuerdetails', 'badges'), 3);
328 $dl[get_string('issuername', 'badges')] = $badge->issuername
;
329 if (isset($badge->issuercontact
) && !empty($badge->issuercontact
)) {
330 $dl[get_string('contact', 'badges')] = obfuscate_mailto($badge->issuercontact
);
332 $output .= $this->definition_list($dl);
334 $output .= $this->output
->heading(get_string('badgedetails', 'badges'), 3);
336 $dl[get_string('name')] = $badge->name
;
337 $dl[get_string('description', 'badges')] = $badge->description
;
339 if ($badge->type
== BADGE_TYPE_COURSE
&& isset($badge->courseid
)) {
340 $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid
));
341 $dl[get_string('course')] = $coursename;
343 $dl[get_string('bcriteria', 'badges')] = self
::print_badge_criteria($badge);
344 $output .= $this->definition_list($dl);
346 $output .= $this->output
->heading(get_string('issuancedetails', 'badges'), 3);
348 $dl[get_string('dateawarded', 'badges')] = userdate($issued['issuedOn']);
349 if (isset($issued['expires'])) {
350 if ($issued['expires'] < $now) {
351 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']) . get_string('warnexpired', 'badges');
354 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']);
359 $agg = $badge->get_aggregation_methods();
360 $evidence = $badge->get_criteria_completions($userinfo->id
);
361 $eids = array_map(function($o) {
364 unset($badge->criteria
[BADGE_CRITERIA_TYPE_OVERALL
]);
367 foreach ($badge->criteria
as $type => $c) {
368 if (in_array($c->id
, $eids)) {
369 if (count($c->params
) == 1) {
370 $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
372 $items[] = get_string('criteria_descr_' . $type , 'badges',
373 core_text
::strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
378 $dl[get_string('evidence', 'badges')] = get_string('completioninfo', 'badges') . html_writer
::alist($items, array(), 'ul');
379 $output .= $this->definition_list($dl);
380 $output .= html_writer
::end_tag('div');
385 // Outputs external badge.
386 protected function render_external_badge(external_badge
$ibadge) {
387 $issued = $ibadge->issued
;
388 $assertion = $issued->assertion
;
389 $issuer = $assertion->badge
->issuer
;
390 $userinfo = $ibadge->recipient
;
391 $table = new html_table();
392 $today = strtotime(date('Y-m-d'));
395 $output .= html_writer
::start_tag('div', array('id' => 'badge'));
396 $output .= html_writer
::start_tag('div', array('id' => 'badge-image'));
397 $output .= html_writer
::empty_tag('img', array('src' => $issued->imageUrl
));
398 if (isset($assertion->expires
)) {
399 $expiration = !strtotime($assertion->expires
) ?
s($assertion->expires
) : strtotime($assertion->expires
);
400 if ($expiration < $today) {
401 $output .= $this->output
->pix_icon('i/expired',
402 get_string('expireddate', 'badges', userdate($expiration)),
404 array('class' => 'expireimage'));
407 $output .= html_writer
::end_tag('div');
409 $output .= html_writer
::start_tag('div', array('id' => 'badge-details'));
411 // Recipient information.
412 $output .= $this->output
->heading(get_string('recipientdetails', 'badges'), 3);
414 // Technically, we should alway have a user at this point, but added an extra check just in case.
416 if (!$ibadge->valid
) {
417 $notify = $this->output
->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice');
418 $dl[get_string('name')] = fullname($userinfo) . $notify;
420 $dl[get_string('name')] = fullname($userinfo);
423 $notify = $this->output
->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice');
424 $dl[get_string('name')] = $notify;
426 $output .= $this->definition_list($dl);
428 $output .= $this->output
->heading(get_string('issuerdetails', 'badges'), 3);
430 $dl[get_string('issuername', 'badges')] = s($issuer->name
);
431 $dl[get_string('issuerurl', 'badges')] = html_writer
::tag('a', $issuer->origin
, array('href' => $issuer->origin
));
433 if (isset($issuer->contact
)) {
434 $dl[get_string('contact', 'badges')] = obfuscate_mailto($issuer->contact
);
436 $output .= $this->definition_list($dl);
438 $output .= $this->output
->heading(get_string('badgedetails', 'badges'), 3);
440 $dl[get_string('name')] = s($assertion->badge
->name
);
441 $dl[get_string('description', 'badges')] = s($assertion->badge
->description
);
442 $dl[get_string('bcriteria', 'badges')] = html_writer
::tag('a', s($assertion->badge
->criteria
), array('href' => $assertion->badge
->criteria
));
443 $output .= $this->definition_list($dl);
445 $output .= $this->output
->heading(get_string('issuancedetails', 'badges'), 3);
447 if (isset($assertion->issued_on
)) {
448 $issuedate = !strtotime($assertion->issued_on
) ?
s($assertion->issued_on
) : strtotime($assertion->issued_on
);
449 $dl[get_string('dateawarded', 'badges')] = userdate($issuedate);
451 if (isset($assertion->expires
)) {
452 if ($expiration < $today) {
453 $dl[get_string('expirydate', 'badges')] = userdate($expiration) . get_string('warnexpired', 'badges');
455 $dl[get_string('expirydate', 'badges')] = userdate($expiration);
458 if (isset($assertion->evidence
)) {
459 $dl[get_string('evidence', 'badges')] = html_writer
::tag('a', s($assertion->evidence
), array('href' => $assertion->evidence
));
461 $output .= $this->definition_list($dl);
462 $output .= html_writer
::end_tag('div');
467 // Displays the user badges.
468 protected function render_badge_user_collection(badge_user_collection
$badges) {
469 global $CFG, $USER, $SITE;
470 $backpack = $badges->backpack
;
471 $mybackpack = new moodle_url('/badges/mybackpack.php');
473 $paging = new paging_bar($badges->totalcount
, $badges->page
, $badges->perpage
, $this->page
->url
, 'page');
474 $htmlpagingbar = $this->render($paging);
476 // Set backpack connection string.
477 $backpackconnect = '';
478 if (!empty($CFG->badges_allowexternalbackpack
) && is_null($backpack)) {
479 $backpackconnect = $this->output
->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
482 $searchform = $this->output
->box($this->helper_search_form($badges->search
), 'boxwidthwide boxaligncenter');
484 // Download all button.
485 $downloadall = $this->output
->single_button(
486 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
487 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
490 $localhtml = html_writer
::start_tag('div', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
491 $heading = get_string('localbadges', 'badges', format_string($SITE->fullname
, true, array('context' => context_system
::instance())));
492 $localhtml .= $this->output
->heading_with_help($heading, 'localbadgesh', 'badges');
493 if ($badges->badges
) {
494 $downloadbutton = $this->output
->heading(get_string('badgesearned', 'badges', $badges->totalcount
), 4, 'activatebadge');
495 $downloadbutton .= $downloadall;
497 $htmllist = $this->print_badges_list($badges->badges
, $USER->id
);
498 $localhtml .= $backpackconnect . $downloadbutton . $searchform . $htmlpagingbar . $htmllist . $htmlpagingbar;
500 $localhtml .= $searchform . $this->output
->notification(get_string('nobadges', 'badges'));
502 $localhtml .= html_writer
::end_tag('div');
506 if (!empty($CFG->badges_allowexternalbackpack
)) {
507 $externalhtml .= html_writer
::start_tag('div', array('class' => 'generalbox'));
508 $externalhtml .= $this->output
->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges');
509 if (!is_null($backpack)) {
510 if ($backpack->totalcollections
== 0) {
511 $externalhtml .= get_string('nobackpackcollections', 'badges', $backpack);
513 if ($backpack->totalbadges
== 0) {
514 $externalhtml .= get_string('nobackpackbadges', 'badges', $backpack);
516 $externalhtml .= get_string('backpackbadges', 'badges', $backpack);
517 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges
, $USER->id
, true, true);
521 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
524 $externalhtml .= html_writer
::end_tag('div');
527 return $localhtml . $externalhtml;
530 // Displays the available badges.
531 protected function render_badge_collection(badge_collection
$badges) {
532 $paging = new paging_bar($badges->totalcount
, $badges->page
, $badges->perpage
, $this->page
->url
, 'page');
533 $htmlpagingbar = $this->render($paging);
534 $table = new html_table();
535 $table->attributes
['class'] = 'collection';
537 $sortbyname = $this->helper_sortable_heading(get_string('name'),
538 'name', $badges->sort
, $badges->dir
);
539 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
540 'dateissued', $badges->sort
, $badges->dir
);
541 $table->head
= array(
542 get_string('badgeimage', 'badges'),
544 get_string('description', 'badges'),
545 get_string('bcriteria', 'badges'),
548 $table->colclasses
= array('badgeimage', 'name', 'description', 'criteria', 'awards');
550 foreach ($badges->badges
as $badge) {
551 $badgeimage = print_badge_image($badge, $this->page
->context
, 'large');
552 $name = $badge->name
;
553 $description = $badge->description
;
554 $criteria = self
::print_badge_criteria($badge);
555 if ($badge->dateissued
) {
556 $icon = new pix_icon('i/valid',
557 get_string('dateearned', 'badges',
558 userdate($badge->dateissued
, get_string('strftimedatefullshort', 'core_langconfig'))));
559 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash
));
560 $awarded = $this->output
->action_icon($badgeurl, $icon, null, null, true);
564 $row = array($badgeimage, $name, $description, $criteria, $awarded);
565 $table->data
[] = $row;
568 $htmltable = html_writer
::table($table);
570 return $htmlpagingbar . $htmltable . $htmlpagingbar;
573 // Outputs table of badges with actions available.
574 protected function render_badge_management(badge_management
$badges) {
575 $paging = new paging_bar($badges->totalcount
, $badges->page
, $badges->perpage
, $this->page
->url
, 'page');
579 if (has_capability('moodle/badges:createbadge', $this->page
->context
)) {
580 $n['type'] = $this->page
->url
->get_param('type');
581 $n['id'] = $this->page
->url
->get_param('id');
582 $htmlnew = $this->output
->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
585 $htmlpagingbar = $this->render($paging);
586 $table = new html_table();
587 $table->attributes
['class'] = 'collection';
589 $sortbyname = $this->helper_sortable_heading(get_string('name'),
590 'name', $badges->sort
, $badges->dir
);
591 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
592 'status', $badges->sort
, $badges->dir
);
593 $table->head
= array(
596 get_string('bcriteria', 'badges'),
597 get_string('awards', 'badges'),
598 get_string('actions')
600 $table->colclasses
= array('name', 'status', 'criteria', 'awards', 'actions');
602 foreach ($badges->badges
as $b) {
603 $style = !$b->is_active() ?
array('class' => 'dimmed') : array();
604 $forlink = print_badge_image($b, $this->page
->context
) . ' ' .
605 html_writer
::start_tag('span') . $b->name
. html_writer
::end_tag('span');
606 $name = html_writer
::link(new moodle_url('/badges/overview.php', array('id' => $b->id
)), $forlink, $style);
607 $status = $b->statstring
;
608 $criteria = self
::print_badge_criteria($b, 'short');
610 if (has_capability('moodle/badges:viewawarded', $this->page
->context
)) {
611 $awards = html_writer
::link(new moodle_url('/badges/recipients.php', array('id' => $b->id
)), $b->awards
);
613 $awards = $b->awards
;
616 $actions = self
::print_badge_table_actions($b, $this->page
->context
);
618 $row = array($name, $status, $criteria, $awards, $actions);
619 $table->data
[] = $row;
621 $htmltable = html_writer
::table($table);
623 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
626 // Prints tabs for badge editing.
627 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
632 $row[] = new tabobject('overview',
633 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
634 get_string('boverview', 'badges')
637 if (has_capability('moodle/badges:configuredetails', $context)) {
638 $row[] = new tabobject('details',
639 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'details')),
640 get_string('bdetails', 'badges')
644 if (has_capability('moodle/badges:configurecriteria', $context)) {
645 $row[] = new tabobject('criteria',
646 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
647 get_string('bcriteria', 'badges')
651 if (has_capability('moodle/badges:configuremessages', $context)) {
652 $row[] = new tabobject('message',
653 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
654 get_string('bmessage', 'badges')
658 if (has_capability('moodle/badges:viewawarded', $context)) {
659 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
660 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
661 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
662 $row[] = new tabobject('awards',
663 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
664 get_string('bawards', 'badges', $awarded)
668 echo $this->tabtree($row, $current);
672 * Prints badge status box.
673 * @return Either the status box html as a string or null
675 public function print_badge_status_box(badge
$badge) {
676 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
678 if (!$badge->has_criteria()) {
679 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id
));
680 $status = get_string('nocriteria', 'badges');
681 if ($this->page
->url
!= $criteriaurl) {
682 $action = $this->output
->single_button(
684 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
689 $message = $status . $action;
691 $status = get_string('statusmessage_' . $badge->status
, 'badges');
692 if ($badge->is_active()) {
693 $action = $this->output
->single_button(new moodle_url('/badges/action.php',
694 array('id' => $badge->id
, 'lock' => 1, 'sesskey' => sesskey(),
695 'return' => $this->page
->url
->out_as_local_url(false))),
696 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
698 $action = $this->output
->single_button(new moodle_url('/badges/action.php',
699 array('id' => $badge->id
, 'activate' => 1, 'sesskey' => sesskey(),
700 'return' => $this->page
->url
->out_as_local_url(false))),
701 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
704 $message = $status . $this->output
->help_icon('status', 'badges') . $action;
708 $style = $badge->is_active() ?
'generalbox statusbox active' : 'generalbox statusbox inactive';
709 return $this->output
->box($message, $style);
716 * Returns information about badge criteria in a list form.
718 * @param badge $badge Badge objects
719 * @param string $short Indicates whether to print full info about this badge
720 * @return string $output HTML string to output
722 public function print_badge_criteria(badge
$badge, $short = '') {
723 $agg = $badge->get_aggregation_methods();
724 if (empty($badge->criteria
)) {
725 return get_string('nocriteria', 'badges');
729 $overall = $badge->criteria
[BADGE_CRITERIA_TYPE_OVERALL
];
730 if (!$short && !empty($overall->description
)) {
731 $overalldescr = $this->output
->box(
732 format_text($overall->description
, $overall->descriptionformat
, array('context' => $badge->get_context())),
733 'criteria-description'
737 // Get the condition string.
738 if (count($badge->criteria
) == 2) {
741 $condition = get_string('criteria_descr', 'badges');
744 $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL
, 'badges',
745 core_text
::strtoupper($agg[$badge->get_aggregation_method()]));
748 unset($badge->criteria
[BADGE_CRITERIA_TYPE_OVERALL
]);
751 // If only one criterion left, make sure its description goe to the top.
752 if (count($badge->criteria
) == 1) {
753 $c = reset($badge->criteria
);
754 if (!$short && !empty($c->description
)) {
755 $overalldescr = $this->output
->box(
756 format_text($c->description
, $c->descriptionformat
, array('context' => $badge->get_context())),
757 'criteria-description'
760 if (count($c->params
) == 1) {
761 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype
, 'badges') .
762 $c->get_details($short);
764 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype
, 'badges',
765 core_text
::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype
)])) .
766 $c->get_details($short);
769 foreach ($badge->criteria
as $type => $c) {
771 if (!$short && !empty($c->description
)) {
772 $criteriadescr = $this->output
->box(
773 format_text($c->description
, $c->descriptionformat
, array('context' => $badge->get_context())),
774 'criteria-description'
777 if (count($c->params
) == 1) {
778 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
779 $c->get_details($short) . $criteriadescr;
781 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
782 core_text
::strtoupper($agg[$badge->get_aggregation_method($type)])) .
783 $c->get_details($short) .
789 return $overalldescr . $condition . html_writer
::alist($items, array(), 'ul');;
792 // Prints criteria actions for badge editing.
793 public function print_criteria_actions(badge
$badge) {
795 if (!$badge->is_active() && !$badge->is_locked()) {
796 $accepted = $badge->get_accepted_criteria();
797 $potential = array_diff($accepted, array_keys($badge->criteria
));
799 if (!empty($potential)) {
800 foreach ($potential as $p) {
802 $select[$p] = get_string('criteria_' . $p, 'badges');
805 $output .= $this->output
->single_select(
806 new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id
, 'add' => true)),
810 array('' => 'choosedots'),
812 array('label' => get_string('addbadgecriteria', 'badges'))
815 $output .= $this->output
->box(get_string('nothingtoadd', 'badges'), 'clearfix');
822 // Renders a table with users who have earned the badge.
823 // Based on stamps collection plugin.
824 protected function render_badge_recipients(badge_recipients
$recipients) {
825 $paging = new paging_bar($recipients->totalcount
, $recipients->page
, $recipients->perpage
, $this->page
->url
, 'page');
826 $htmlpagingbar = $this->render($paging);
827 $table = new html_table();
828 $table->attributes
['class'] = 'generaltable boxaligncenter boxwidthwide';
830 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
831 'firstname', $recipients->sort
, $recipients->dir
);
832 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
833 'lastname', $recipients->sort
, $recipients->dir
);
834 if ($this->helper_fullname_format() == 'lf') {
835 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
837 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
840 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
841 'dateissued', $recipients->sort
, $recipients->dir
);
843 $table->head
= array($sortbyname, $sortbydate, '');
845 foreach ($recipients->userids
as $holder) {
846 $fullname = fullname($holder);
847 $fullname = html_writer
::link(
848 new moodle_url('/user/profile.php', array('id' => $holder->userid
)),
851 $awarded = userdate($holder->dateissued
);
852 $badgeurl = html_writer
::link(
853 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash
)),
854 get_string('viewbadge', 'badges')
857 $row = array($fullname, $awarded, $badgeurl);
858 $table->data
[] = $row;
861 $htmltable = html_writer
::table($table);
863 return $htmlpagingbar . $htmltable . $htmlpagingbar;
866 ////////////////////////////////////////////////////////////////////////////
868 // Reused from stamps collection plugin
869 ////////////////////////////////////////////////////////////////////////////
872 * Renders a text with icons to sort by the given column
874 * This is intended for table headings.
876 * @param string $text The heading text
877 * @param string $sortid The column id used for sorting
878 * @param string $sortby Currently sorted by (column id)
879 * @param string $sorthow Currently sorted how (ASC|DESC)
883 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
884 $out = html_writer
::tag('span', $text, array('class' => 'text'));
886 if (!is_null($sortid)) {
887 if ($sortby !== $sortid ||
$sorthow !== 'ASC') {
888 $url = new moodle_url($this->page
->url
);
889 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
890 $out .= $this->output
->action_icon($url,
891 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
893 if ($sortby !== $sortid ||
$sorthow !== 'DESC') {
894 $url = new moodle_url($this->page
->url
);
895 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
896 $out .= $this->output
->action_icon($url,
897 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
903 * Tries to guess the fullname format set at the site
905 * @return string fl|lf
907 protected function helper_fullname_format() {
908 $fake = new stdClass();
909 $fake->lastname
= 'LLLL';
910 $fake->firstname
= 'FFFF';
911 $fullname = get_string('fullnamedisplay', '', $fake);
912 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
919 * Renders a search form
921 * @param string $search Search string
922 * @return string HTML
924 protected function helper_search_form($search) {
926 require_once($CFG->libdir
. '/formslib.php');
928 $mform = new MoodleQuickForm('searchform', 'POST', $this->page
->url
);
930 $mform->addElement('hidden', 'sesskey', sesskey());
932 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
933 $mform->setDefault('search', $search);
934 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
935 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
936 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
940 $out = ob_get_clean();
946 * Renders a definition list
948 * @param array $items the list of items to define
951 protected function definition_list(array $items, array $attributes = array()) {
952 $output = html_writer
::start_tag('dl', $attributes);
953 foreach ($items as $label => $value) {
954 $output .= html_writer
::tag('dt', $label);
955 $output .= html_writer
::tag('dd', $value);
957 $output .= html_writer
::end_tag('dl');
963 * An issued badges for badge.php page
965 class issued_badge
implements renderable
{
966 /** @var issued badge */
969 /** @var badge recipient */
972 /** @var badge class */
975 /** @var badge visibility to others */
978 /** @var badge class */
982 * Initializes the badge to display
984 * @param string $hash Issued badge hash
986 public function __construct($hash) {
989 $assertion = new core_badges_assertion($hash);
990 $this->issued
= $assertion->get_badge_assertion();
991 $this->badgeclass
= $assertion->get_badge_class();
993 $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
995 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
996 array('hash' => $hash), IGNORE_MISSING
);
998 // Get a recipient from database.
999 $namefields = get_all_user_name_fields(true, 'u');
1000 $user = $DB->get_record_sql("SELECT u.id, $namefields, u.deleted, u.email
1001 FROM {user} u WHERE u.id = :userid", array('userid' => $rec->userid
));
1002 $this->recipient
= $user;
1003 $this->visible
= $rec->visible
;
1004 $this->badgeid
= $rec->badgeid
;
1010 * An external badges for external.php page
1012 class external_badge
implements renderable
{
1013 /** @var issued badge */
1019 /** @var validation of external badge */
1020 public $valid = true;
1023 * Initializes the badge to display
1025 * @param object $badge External badge information.
1026 * @param int $recipient User id.
1028 public function __construct($badge, $recipient) {
1030 // At this point a user has connected a backpack. So, we are going to get
1031 // their backpack email rather than their account email.
1032 $namefields = get_all_user_name_fields(true, 'u');
1033 $user = $DB->get_record_sql("SELECT {$namefields}, b.email
1034 FROM {user} u INNER JOIN {badge_backpack} b ON u.id = b.userid
1035 WHERE userid = :userid", array('userid' => $recipient), IGNORE_MISSING
);
1037 $this->issued
= $badge;
1038 $this->recipient
= $user;
1040 // Check if recipient is valid.
1041 // There is no way to be 100% sure that a badge belongs to a user.
1042 // Backpack does not return any recipient information.
1043 // All we can do is compare that backpack email hashed using salt
1044 // provided in the assertion matches a badge recipient from the assertion.
1046 if (validate_email($badge->assertion
->recipient
) && $badge->assertion
->recipient
== $user->email
) {
1047 // If we have email, compare emails.
1048 $this->valid
= true;
1049 } else if ($badge->assertion
->recipient
== 'sha256$' . hash('sha256', $user->email
)) {
1050 // If recipient is hashed, but no salt, compare hashes without salt.
1051 $this->valid
= true;
1052 } else if ($badge->assertion
->recipient
== 'sha256$' . hash('sha256', $user->email
. $badge->assertion
->salt
)) {
1053 // If recipient is hashed, compare hashes.
1054 $this->valid
= true;
1056 // Otherwise, we cannot be sure that this user is a recipient.
1057 $this->valid
= false;
1060 $this->valid
= false;
1066 * Badge recipients rendering class
1068 class badge_recipients
implements renderable
{
1069 /** @var string how are the data sorted */
1070 public $sort = 'lastname';
1072 /** @var string how are the data sorted */
1073 public $dir = 'ASC';
1075 /** @var int page number to display */
1078 /** @var int number of badge recipients to display per page */
1079 public $perpage = 30;
1081 /** @var int the total number or badge recipients to display */
1082 public $totalcount = null;
1084 /** @var array internal list of badge recipients ids */
1085 public $userids = array();
1087 * Initializes the list of users to display
1089 * @param array $holders List of badge holders
1091 public function __construct($holders) {
1092 $this->userids
= $holders;
1097 * Collection of all badges for view.php page
1099 class badge_collection
implements renderable
{
1101 /** @var string how are the data sorted */
1102 public $sort = 'name';
1104 /** @var string how are the data sorted */
1105 public $dir = 'ASC';
1107 /** @var int page number to display */
1110 /** @var int number of badges to display per page */
1111 public $perpage = BADGE_PERPAGE
;
1113 /** @var int the total number of badges to display */
1114 public $totalcount = null;
1116 /** @var array list of badges */
1117 public $badges = array();
1120 * Initializes the list of badges to display
1122 * @param array $badges Badges to render
1124 public function __construct($badges) {
1125 $this->badges
= $badges;
1130 * Collection of badges used at the index.php page
1132 class badge_management
extends badge_collection
implements renderable
{
1136 * Collection of user badges used at the mybadges.php page
1138 class badge_user_collection
extends badge_collection
implements renderable
{
1139 /** @var array backpack settings */
1140 public $backpack = null;
1142 /** @var string search */
1143 public $search = '';
1146 * Initializes user badge collection.
1148 * @param array $badges Badges to render
1149 * @param int $userid Badges owner
1151 public function __construct($badges, $userid) {
1153 parent
::__construct($badges);
1155 if (!empty($CFG->badges_allowexternalbackpack
)) {
1156 $this->backpack
= get_backpack_settings($userid, true);