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
::end_tag('div', array());
119 $actioncell->attributes
['class'] = 'actions';
120 $potentialcell = new html_table_cell();
121 $potentialcell->text
= $potentialuc->display(true);
122 $potentialcell->attributes
['class'] = 'potential';
124 $table = new html_table();
125 $table->attributes
['class'] = 'recipienttable boxaligncenter';
126 $table->data
= array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
127 $output .= html_writer
::table($table);
129 $output .= html_writer
::end_tag('form');
133 // Prints a badge overview infomation.
134 public function print_badge_overview($badge, $context) {
138 $display .= html_writer
::start_tag('fieldset', array('class' => 'generalbox'));
139 $display .= html_writer
::tag('legend', get_string('badgedetails', 'badges'), array('class' => 'bold'));
141 $detailstable = new html_table();
142 $detailstable->attributes
= array('class' => 'clearfix', 'id' => 'badgedetails');
143 $detailstable->data
[] = array(get_string('name') . ":", $badge->name
);
144 $detailstable->data
[] = array(get_string('description', 'badges') . ":", $badge->description
);
145 $detailstable->data
[] = array(get_string('createdon', 'search') . ":", userdate($badge->timecreated
));
146 $detailstable->data
[] = array(get_string('badgeimage', 'badges') . ":",
147 print_badge_image($badge, $context, 'large'));
148 $display .= html_writer
::table($detailstable);
149 $display .= html_writer
::end_tag('fieldset');
152 $display .= html_writer
::start_tag('fieldset', array('class' => 'generalbox'));
153 $display .= html_writer
::tag('legend', get_string('issuerdetails', 'badges'), array('class' => 'bold'));
155 $issuertable = new html_table();
156 $issuertable->attributes
= array('class' => 'clearfix', 'id' => 'badgeissuer');
157 $issuertable->data
[] = array(get_string('issuername', 'badges') . ":", $badge->issuername
);
158 $issuertable->data
[] = array(get_string('contact', 'badges') . ":",
159 html_writer
::tag('a', $badge->issuercontact
, array('href' => 'mailto:' . $badge->issuercontact
)));
160 $display .= html_writer
::table($issuertable);
161 $display .= html_writer
::end_tag('fieldset');
163 // Issuance details if any.
164 $display .= html_writer
::start_tag('fieldset', array('class' => 'generalbox'));
165 $display .= html_writer
::tag('legend', get_string('issuancedetails', 'badges'), array('class' => 'bold'));
166 if ($badge->can_expire()) {
167 if ($badge->expiredate
) {
168 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate
));
169 } else if ($badge->expireperiod
) {
170 if ($badge->expireperiod
< 60) {
171 $display .= get_string('expireperiods', 'badges', round($badge->expireperiod
, 2));
172 } else if ($badge->expireperiod
< 60 * 60) {
173 $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod
/ 60, 2));
174 } else if ($badge->expireperiod
< 60 * 60 * 24) {
175 $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod
/ 60 / 60, 2));
177 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod
/ 60 / 60 / 24, 2));
181 $display .= get_string('noexpiry', 'badges');
183 $display .= html_writer
::end_tag('fieldset');
185 // Criteria details if any.
186 $display .= html_writer
::start_tag('fieldset', array('class' => 'generalbox'));
187 $display .= html_writer
::tag('legend', get_string('bcriteria', 'badges'), array('class' => 'bold'));
188 if ($badge->has_criteria()) {
189 $display .= self
::print_badge_criteria($badge);
191 $display .= get_string('nocriteria', 'badges');
192 if (has_capability('moodle/badges:configurecriteria', $context)) {
193 $display .= $this->output
->single_button(
194 new moodle_url('/badges/criteria.php', array('id' => $badge->id
)),
195 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
198 $display .= html_writer
::end_tag('fieldset');
200 // Awards details if any.
201 if (has_capability('moodle/badges:viewawarded', $context)) {
202 $display .= html_writer
::start_tag('fieldset', array('class' => 'generalbox'));
203 $display .= html_writer
::tag('legend', get_string('awards', 'badges'), array('class' => 'bold'));
204 if ($badge->has_awards()) {
205 $url = new moodle_url('/badges/recipients.php', array('id' => $badge->id
));
207 $a->link
= $url->out();
208 $a->count
= count($badge->get_awards());
209 $display .= get_string('numawards', 'badges', $a);
211 $display .= get_string('noawards', 'badges');
214 if (has_capability('moodle/badges:awardbadge', $context) &&
215 $badge->has_manual_award_criteria() &&
216 $badge->is_active()) {
217 $display .= $this->output
->single_button(
218 new moodle_url('/badges/award.php', array('id' => $badge->id
)),
219 get_string('award', 'badges'), 'POST', array('class' => 'activatebadge'));
221 $display .= html_writer
::end_tag('fieldset');
227 // Prints action icons for the badge.
228 public function print_badge_table_actions($badge, $context) {
231 if (has_capability('moodle/badges:configuredetails', $context) && $badge->has_criteria()) {
232 // Activate/deactivate badge.
233 if ($badge->status
== BADGE_STATUS_INACTIVE ||
$badge->status
== BADGE_STATUS_INACTIVE_LOCKED
) {
234 // "Activate" will go to another page and ask for confirmation.
235 $url = new moodle_url('/badges/action.php');
236 $url->param('id', $badge->id
);
237 $url->param('activate', true);
238 $url->param('sesskey', sesskey());
239 $return = new moodle_url(qualified_me());
240 $url->param('return', $return->out_as_local_url(false));
241 $actions .= $this->output
->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
243 $url = new moodle_url(qualified_me());
244 $url->param('lock', $badge->id
);
245 $url->param('sesskey', sesskey());
246 $actions .= $this->output
->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
250 // Award badge manually.
251 if ($badge->has_manual_award_criteria() &&
252 has_capability('moodle/badges:awardbadge', $context) &&
253 $badge->is_active()) {
254 $url = new moodle_url('/badges/award.php', array('id' => $badge->id
));
255 $actions .= $this->output
->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
259 if (has_capability('moodle/badges:configuredetails', $context)) {
260 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id
, 'action' => 'details'));
261 $actions .= $this->output
->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
265 if (has_capability('moodle/badges:createbadge', $context)) {
266 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id
, 'sesskey' => sesskey()));
267 $actions .= $this->output
->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
271 if (has_capability('moodle/badges:deletebadge', $context)) {
272 $url = new moodle_url(qualified_me());
273 $url->param('delete', $badge->id
);
274 $actions .= $this->output
->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
280 // Outputs issued badge with actions available.
281 protected function render_issued_badge(issued_badge
$ibadge) {
282 global $USER, $CFG, $DB, $SITE;
283 $issued = $ibadge->issued
;
284 $userinfo = $ibadge->recipient
;
285 $badgeclass = $ibadge->badgeclass
;
286 $badge = new badge($ibadge->badgeid
);
289 $table = new html_table();
290 $table->id
= 'issued-badge-table';
292 $imagetable = new html_table();
293 $imagetable->attributes
= array('class' => 'clearfix badgeissuedimage');
294 $imagetable->data
[] = array(html_writer
::empty_tag('img', array('src' => $badgeclass['image'])));
295 if ($USER->id
== $userinfo->id
&& !empty($CFG->enablebadges
)) {
296 $imagetable->data
[] = array($this->output
->single_button(
297 new moodle_url('/badges/badge.php', array('hash' => $issued['uid'], 'bake' => true)),
298 get_string('download'),
300 $expiration = isset($issued['expires']) ?
$issued['expires'] : $now +
86400;
301 if (!empty($CFG->badges_allowexternalbackpack
) && ($expiration > $now) && badges_user_has_backpack($USER->id
)) {
302 $assertion = new moodle_url('/badges/assertion.php', array('b' => $issued['uid']));
303 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
307 'value' => get_string('addtobackpack', 'badges'));
308 $tobackpack = html_writer
::tag('input', '', $attributes);
309 $this->output
->add_action_handler($action, 'addbutton');
310 $imagetable->data
[] = array($tobackpack);
313 $datatable = new html_table();
314 $datatable->attributes
= array('class' => 'badgeissuedinfo');
315 $datatable->colclasses
= array('bfield', 'bvalue');
317 // Recipient information.
318 $datatable->data
[] = array($this->output
->heading(get_string('recipientdetails', 'badges'), 3), '');
319 if ($userinfo->deleted
) {
320 $strdata = new stdClass();
321 $strdata->user
= fullname($userinfo);
322 $strdata->site
= format_string($SITE->fullname
, true, array('context' => context_system
::instance()));
323 $datatable->data
[] = array(get_string('name'), get_string('error:userdeleted', 'badges', $strdata));
325 $datatable->data
[] = array(get_string('name'), fullname($userinfo));
328 $datatable->data
[] = array($this->output
->heading(get_string('issuerdetails', 'badges'), 3), '');
329 $datatable->data
[] = array(get_string('issuername', 'badges'), $badge->issuername
);
330 if (isset($badge->issuercontact
) && !empty($badge->issuercontact
)) {
331 $datatable->data
[] = array(get_string('contact', 'badges'), obfuscate_mailto($badge->issuercontact
));
333 $datatable->data
[] = array($this->output
->heading(get_string('badgedetails', 'badges'), 3), '');
334 $datatable->data
[] = array(get_string('name'), $badge->name
);
335 $datatable->data
[] = array(get_string('description', 'badges'), $badge->description
);
337 if ($badge->type
== BADGE_TYPE_COURSE
&& isset($badge->courseid
)) {
338 $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid
));
339 $datatable->data
[] = array(get_string('course'), $coursename);
342 $datatable->data
[] = array(get_string('bcriteria', 'badges'), self
::print_badge_criteria($badge));
343 $datatable->data
[] = array($this->output
->heading(get_string('issuancedetails', 'badges'), 3), '');
344 $datatable->data
[] = array(get_string('dateawarded', 'badges'), userdate($issued['issuedOn']));
345 if (isset($issued['expires'])) {
346 if ($issued['expires'] < $now) {
347 $cell = new html_table_cell(userdate($issued['expires']) . get_string('warnexpired', 'badges'));
348 $cell->attributes
= array('class' => 'notifyproblem warning');
349 $datatable->data
[] = array(get_string('expirydate', 'badges'), $cell);
351 $image = html_writer
::start_tag('div', array('class' => 'badge'));
352 $image .= html_writer
::empty_tag('img', array('src' => $badgeclass['image']));
353 $image .= $this->output
->pix_icon('i/expired',
354 get_string('expireddate', 'badges', userdate($issued['expires'])),
356 array('class' => 'expireimage'));
357 $image .= html_writer
::end_tag('div');
358 $imagetable->data
[0] = array($image);
360 $datatable->data
[] = array(get_string('expirydate', 'badges'), userdate($issued['expires']));
365 $agg = $badge->get_aggregation_methods();
366 $evidence = $badge->get_criteria_completions($userinfo->id
);
367 $eids = array_map(create_function('$o', 'return $o->critid;'), $evidence);
368 unset($badge->criteria
[BADGE_CRITERIA_TYPE_OVERALL
]);
371 foreach ($badge->criteria
as $type => $c) {
372 if (in_array($c->id
, $eids)) {
373 if (count($c->params
) == 1) {
374 $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
376 $items[] = get_string('criteria_descr_' . $type , 'badges',
377 core_text
::strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
382 $datatable->data
[] = array(get_string('evidence', 'badges'),
383 get_string('completioninfo', 'badges') .
384 html_writer
::alist($items, array(), 'ul'));
385 $table->attributes
= array('class' => 'generalbox boxaligncenter issuedbadgebox');
386 $table->data
[] = array(html_writer
::table($imagetable), html_writer
::table($datatable));
387 $htmlbadge = html_writer
::table($table);
392 // Outputs external badge.
393 protected function render_external_badge(external_badge
$ibadge) {
394 $issued = $ibadge->issued
;
395 $assertion = $issued->assertion
;
396 $issuer = $assertion->badge
->issuer
;
397 $userinfo = $ibadge->recipient
;
398 $table = new html_table();
400 $imagetable = new html_table();
401 $imagetable->attributes
= array('class' => 'clearfix badgeissuedimage');
402 $imagetable->data
[] = array(html_writer
::empty_tag('img', array('src' => $issued->imageUrl
, 'width' => '100px')));
404 $datatable = new html_table();
405 $datatable->attributes
= array('class' => 'badgeissuedinfo');
406 $datatable->colclasses
= array('bfield', 'bvalue');
408 // Recipient information.
409 $datatable->data
[] = array($this->output
->heading(get_string('recipientdetails', 'badges'), 3), '');
410 // Technically, we should alway have a user at this point, but added an extra check just in case.
413 if (!$ibadge->valid
) {
414 $notify = $this->output
->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice');
416 $datatable->data
[] = array(get_string('name'), fullname($userinfo). $notify);
418 $notify = $this->output
->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice');
419 $datatable->data
[] = array(get_string('name'), $notify);
422 $datatable->data
[] = array($this->output
->heading(get_string('issuerdetails', 'badges'), 3), '');
423 $datatable->data
[] = array(get_string('issuername', 'badges'), s($issuer->name
));
424 $datatable->data
[] = array(get_string('issuerurl', 'badges'),
425 html_writer
::tag('a', s($issuer->origin
), array('href' => $issuer->origin
)));
426 if (isset($issuer->contact
)) {
427 $datatable->data
[] = array(get_string('contact', 'badges'), obfuscate_mailto($issuer->contact
));
429 $datatable->data
[] = array($this->output
->heading(get_string('badgedetails', 'badges'), 3), '');
430 $datatable->data
[] = array(get_string('name'), s($assertion->badge
->name
));
431 $datatable->data
[] = array(get_string('description', 'badges'), s($assertion->badge
->description
));
432 $datatable->data
[] = array(get_string('bcriteria', 'badges'),
433 html_writer
::tag('a', s($assertion->badge
->criteria
), array('href' => $assertion->badge
->criteria
)));
434 $datatable->data
[] = array($this->output
->heading(get_string('issuancedetails', 'badges'), 3), '');
435 if (isset($assertion->issued_on
)) {
436 $issuedate = !strtotime($assertion->issued_on
) ?
s($assertion->issued_on
) : strtotime($assertion->issued_on
);
437 $datatable->data
[] = array(get_string('dateawarded', 'badges'), userdate($issuedate));
439 if (isset($assertion->expires
)) {
440 $today_date = date('Y-m-d');
441 $today = strtotime($today_date);
442 $expiration = !strtotime($assertion->expires
) ?
s($assertion->expires
) : strtotime($assertion->expires
);
443 if ($expiration < $today) {
444 $cell = new html_table_cell(userdate($expiration) . get_string('warnexpired', 'badges'));
445 $cell->attributes
= array('class' => 'notifyproblem warning');
446 $datatable->data
[] = array(get_string('expirydate', 'badges'), $cell);
448 $image = html_writer
::start_tag('div', array('class' => 'badge'));
449 $image .= html_writer
::empty_tag('img', array('src' => $issued->imageUrl
));
450 $image .= html_writer
::start_tag('span', array('class' => 'expired'))
451 . $this->output
->pix_icon('i/expired',
452 get_string('expireddate', 'badges', userdate($expiration)),
454 array('class' => 'expireimage'))
455 . html_writer
::end_tag('span');
456 $image .= html_writer
::end_tag('div');
457 $imagetable->data
[0] = array($image);
459 $datatable->data
[] = array(get_string('expirydate', 'badges'), userdate($expiration));
462 if (isset($assertion->evidence
)) {
463 $datatable->data
[] = array(get_string('evidence', 'badges'),
464 html_writer
::tag('a', s($assertion->evidence
), array('href' => $assertion->evidence
)));
466 $table->attributes
= array('class' => 'generalbox boxaligncenter issuedbadgebox');
467 $table->data
[] = array(html_writer
::table($imagetable), html_writer
::table($datatable));
468 $htmlbadge = html_writer
::table($table);
473 // Outputs table of user badges.
474 protected function render_badge_user_collection(badge_user_collection
$badges) {
475 global $CFG, $USER, $SITE;
476 $backpack = $badges->backpack
;
477 $mybackpack = new moodle_url('/badges/mybackpack.php');
479 $paging = new paging_bar($badges->totalcount
, $badges->page
, $badges->perpage
, $this->page
->url
, 'page');
480 $htmlpagingbar = $this->render($paging);
482 // Set backpack connection string.
483 $backpackconnect = '';
484 if (!empty($CFG->badges_allowexternalbackpack
) && is_null($backpack)) {
485 $backpackconnect = $this->output
->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
488 $searchform = $this->output
->box($this->helper_search_form($badges->search
), 'boxwidthwide boxaligncenter');
490 // Download all button.
491 $downloadall = $this->output
->single_button(
492 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
493 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
496 $localhtml = html_writer
::start_tag('fieldset', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
497 $heading = get_string('localbadges', 'badges', format_string($SITE->fullname
, true, array('context' => context_system
::instance())));
498 $localhtml .= html_writer
::tag('legend', $this->output
->heading_with_help($heading, 'localbadgesh', 'badges'));
499 if ($badges->badges
) {
500 $table = new html_table();
501 $table->attributes
['class'] = 'statustable';
502 $table->data
[] = array($this->output
->heading(get_string('badgesearned', 'badges', $badges->totalcount
), 4, 'activatebadge'), $downloadall);
503 $downloadbutton = html_writer
::table($table);
505 $htmllist = $this->print_badges_list($badges->badges
, $USER->id
);
506 $localhtml .= $backpackconnect . $downloadbutton . $searchform . $htmlpagingbar . $htmllist . $htmlpagingbar;
508 $localhtml .= $searchform . $this->output
->notification(get_string('nobadges', 'badges'));
510 $localhtml .= html_writer
::end_tag('fieldset');
514 if (!empty($CFG->badges_allowexternalbackpack
)) {
515 $externalhtml .= html_writer
::start_tag('fieldset', array('class' => 'generalbox'));
516 $externalhtml .= html_writer
::tag('legend', $this->output
->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges'));
517 if (!is_null($backpack)) {
518 if ($backpack->totalcollections
== 0) {
519 $externalhtml .= get_string('nobackpackcollections', 'badges', $backpack);
521 if ($backpack->totalbadges
== 0) {
522 $externalhtml .= get_string('nobackpackbadges', 'badges', $backpack);
524 $externalhtml .= get_string('backpackbadges', 'badges', $backpack);
525 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges
, $USER->id
, true, true);
529 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
532 $externalhtml .= html_writer
::end_tag('fieldset');
535 return $localhtml . $externalhtml;
538 // Outputs table of available badges.
539 protected function render_badge_collection(badge_collection
$badges) {
540 $paging = new paging_bar($badges->totalcount
, $badges->page
, $badges->perpage
, $this->page
->url
, 'page');
541 $htmlpagingbar = $this->render($paging);
542 $table = new html_table();
543 $table->attributes
['class'] = 'collection';
545 $sortbyname = $this->helper_sortable_heading(get_string('name'),
546 'name', $badges->sort
, $badges->dir
);
547 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
548 'dateissued', $badges->sort
, $badges->dir
);
549 $table->head
= array(
550 get_string('badgeimage', 'badges'),
552 get_string('description', 'badges'),
553 get_string('bcriteria', 'badges'),
556 $table->colclasses
= array('badgeimage', 'name', 'description', 'criteria', 'awards');
558 foreach ($badges->badges
as $badge) {
559 $badgeimage = print_badge_image($badge, $this->page
->context
, 'large');
560 $name = $badge->name
;
561 $description = $badge->description
;
562 $criteria = self
::print_badge_criteria($badge);
563 if ($badge->dateissued
) {
564 $icon = new pix_icon('i/valid',
565 get_string('dateearned', 'badges',
566 userdate($badge->dateissued
, get_string('strftimedatefullshort', 'core_langconfig'))));
567 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash
));
568 $awarded = $this->output
->action_icon($badgeurl, $icon, null, null, true);
572 $row = array($badgeimage, $name, $description, $criteria, $awarded);
573 $table->data
[] = $row;
576 $htmltable = html_writer
::table($table);
578 return $htmlpagingbar . $htmltable . $htmlpagingbar;
581 // Outputs table of badges with actions available.
582 protected function render_badge_management(badge_management
$badges) {
583 $paging = new paging_bar($badges->totalcount
, $badges->page
, $badges->perpage
, $this->page
->url
, 'page');
587 if (has_capability('moodle/badges:createbadge', $this->page
->context
)) {
588 $n['type'] = $this->page
->url
->get_param('type');
589 $n['id'] = $this->page
->url
->get_param('id');
590 $htmlnew = $this->output
->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
593 $htmlpagingbar = $this->render($paging);
594 $table = new html_table();
595 $table->attributes
['class'] = 'collection';
597 $sortbyname = $this->helper_sortable_heading(get_string('name'),
598 'name', $badges->sort
, $badges->dir
);
599 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
600 'status', $badges->sort
, $badges->dir
);
601 $table->head
= array(
604 get_string('bcriteria', 'badges'),
605 get_string('awards', 'badges'),
606 get_string('actions')
608 $table->colclasses
= array('name', 'status', 'criteria', 'awards', 'actions');
610 foreach ($badges->badges
as $b) {
611 $style = !$b->is_active() ?
array('class' => 'dimmed') : array();
612 $forlink = print_badge_image($b, $this->page
->context
) . ' ' .
613 html_writer
::start_tag('span') . $b->name
. html_writer
::end_tag('span');
614 $name = html_writer
::link(new moodle_url('/badges/overview.php', array('id' => $b->id
)), $forlink, $style);
615 $status = $b->statstring
;
616 $criteria = self
::print_badge_criteria($b, 'short');
618 if (has_capability('moodle/badges:viewawarded', $this->page
->context
)) {
619 $awards = html_writer
::link(new moodle_url('/badges/recipients.php', array('id' => $b->id
)), $b->awards
);
621 $awards = $b->awards
;
624 $actions = self
::print_badge_table_actions($b, $this->page
->context
);
626 $row = array($name, $status, $criteria, $awards, $actions);
627 $table->data
[] = $row;
629 $htmltable = html_writer
::table($table);
631 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
634 // Prints tabs for badge editing.
635 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
640 $row[] = new tabobject('overview',
641 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
642 get_string('boverview', 'badges')
645 if (has_capability('moodle/badges:configuredetails', $context)) {
646 $row[] = new tabobject('details',
647 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'details')),
648 get_string('bdetails', 'badges')
652 if (has_capability('moodle/badges:configurecriteria', $context)) {
653 $row[] = new tabobject('criteria',
654 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
655 get_string('bcriteria', 'badges')
659 if (has_capability('moodle/badges:configuremessages', $context)) {
660 $row[] = new tabobject('message',
661 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
662 get_string('bmessage', 'badges')
666 if (has_capability('moodle/badges:viewawarded', $context)) {
667 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
668 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
669 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
670 $row[] = new tabobject('awards',
671 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
672 get_string('bawards', 'badges', $awarded)
676 echo $this->tabtree($row, $current);
680 * Prints badge status box.
681 * @return Either the status box html as a string or null
683 public function print_badge_status_box(badge
$badge) {
684 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
685 $table = new html_table();
686 $table->attributes
['class'] = 'boxaligncenter statustable';
688 if (!$badge->has_criteria()) {
689 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id
));
690 $status = get_string('nocriteria', 'badges');
691 if ($this->page
->url
!= $criteriaurl) {
692 $action = $this->output
->single_button(
694 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
698 $row = array($status, $action);
700 $status = get_string('statusmessage_' . $badge->status
, 'badges');
701 if ($badge->is_active()) {
702 $action = $this->output
->single_button(new moodle_url('/badges/action.php',
703 array('id' => $badge->id
, 'lock' => 1, 'sesskey' => sesskey(),
704 'return' => $this->page
->url
->out_as_local_url(false))),
705 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
707 $action = $this->output
->single_button(new moodle_url('/badges/action.php',
708 array('id' => $badge->id
, 'activate' => 1, 'sesskey' => sesskey(),
709 'return' => $this->page
->url
->out_as_local_url(false))),
710 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
712 $row = array($status . $this->output
->help_icon('status', 'badges'), $action);
714 $table->data
[] = $row;
716 $style = $badge->is_active() ?
'generalbox statusbox active' : 'generalbox statusbox inactive';
717 return $this->output
->box(html_writer
::table($table), $style);
723 // Prints badge criteria.
724 public function print_badge_criteria(badge
$badge, $short = '') {
726 $agg = $badge->get_aggregation_methods();
727 if (empty($badge->criteria
)) {
728 return get_string('nocriteria', 'badges');
729 } else if (count($badge->criteria
) == 2) {
731 $output .= get_string('criteria_descr', 'badges');
734 $output .= get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL
, 'badges',
735 core_text
::strtoupper($agg[$badge->get_aggregation_method()]));
738 unset($badge->criteria
[BADGE_CRITERIA_TYPE_OVERALL
]);
739 foreach ($badge->criteria
as $type => $c) {
740 if (count($c->params
) == 1) {
741 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') . $c->get_details($short);
743 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
744 core_text
::strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details($short);
747 $output .= html_writer
::alist($items, array(), 'ul');
751 // Prints criteria actions for badge editing.
752 public function print_criteria_actions(badge
$badge) {
753 $table = new html_table();
754 $table->attributes
= array('class' => 'boxaligncenter', 'id' => 'badgeactions');
755 $table->colclasses
= array('activatebadge');
758 if (!$badge->is_active() && !$badge->is_locked()) {
759 $accepted = $badge->get_accepted_criteria();
760 $potential = array_diff($accepted, array_keys($badge->criteria
));
762 if (!empty($potential)) {
763 foreach ($potential as $p) {
765 $select[$p] = get_string('criteria_' . $p, 'badges');
768 $actions[] = get_string('addbadgecriteria', 'badges');
769 $actions[] = $this->output
->single_select(new moodle_url('/badges/criteria_settings.php',
770 array('badgeid' => $badge->id
, 'add' => true)), 'type', $select);
772 $actions[] = $this->output
->box(get_string('nothingtoadd', 'badges'), 'clearfix');
776 $table->data
[] = $actions;
777 return html_writer
::table($table);
780 // Renders a table with users who have earned the badge.
781 // Based on stamps collection plugin.
782 protected function render_badge_recipients(badge_recipients
$recipients) {
783 $paging = new paging_bar($recipients->totalcount
, $recipients->page
, $recipients->perpage
, $this->page
->url
, 'page');
784 $htmlpagingbar = $this->render($paging);
785 $table = new html_table();
786 $table->attributes
['class'] = 'generaltable boxaligncenter boxwidthwide';
788 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
789 'firstname', $recipients->sort
, $recipients->dir
);
790 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
791 'lastname', $recipients->sort
, $recipients->dir
);
792 if ($this->helper_fullname_format() == 'lf') {
793 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
795 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
798 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
799 'dateissued', $recipients->sort
, $recipients->dir
);
801 $table->head
= array($sortbyname, $sortbydate, '');
803 foreach ($recipients->userids
as $holder) {
804 $fullname = fullname($holder);
805 $fullname = html_writer
::link(
806 new moodle_url('/user/profile.php', array('id' => $holder->userid
)),
809 $awarded = userdate($holder->dateissued
);
810 $badgeurl = html_writer
::link(
811 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash
)),
812 get_string('viewbadge', 'badges')
815 $row = array($fullname, $awarded, $badgeurl);
816 $table->data
[] = $row;
819 $htmltable = html_writer
::table($table);
821 return $htmlpagingbar . $htmltable . $htmlpagingbar;
824 ////////////////////////////////////////////////////////////////////////////
826 // Reused from stamps collection plugin
827 ////////////////////////////////////////////////////////////////////////////
830 * Renders a text with icons to sort by the given column
832 * This is intended for table headings.
834 * @param string $text The heading text
835 * @param string $sortid The column id used for sorting
836 * @param string $sortby Currently sorted by (column id)
837 * @param string $sorthow Currently sorted how (ASC|DESC)
841 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
842 $out = html_writer
::tag('span', $text, array('class' => 'text'));
844 if (!is_null($sortid)) {
845 if ($sortby !== $sortid ||
$sorthow !== 'ASC') {
846 $url = new moodle_url($this->page
->url
);
847 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
848 $out .= $this->output
->action_icon($url,
849 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
851 if ($sortby !== $sortid ||
$sorthow !== 'DESC') {
852 $url = new moodle_url($this->page
->url
);
853 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
854 $out .= $this->output
->action_icon($url,
855 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
861 * Tries to guess the fullname format set at the site
863 * @return string fl|lf
865 protected function helper_fullname_format() {
866 $fake = new stdClass();
867 $fake->lastname
= 'LLLL';
868 $fake->firstname
= 'FFFF';
869 $fullname = get_string('fullnamedisplay', '', $fake);
870 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
877 * Renders a search form
879 * @param string $search Search string
880 * @return string HTML
882 protected function helper_search_form($search) {
884 require_once($CFG->libdir
. '/formslib.php');
886 $mform = new MoodleQuickForm('searchform', 'POST', $this->page
->url
);
888 $mform->addElement('hidden', 'sesskey', sesskey());
890 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
891 $mform->setDefault('search', $search);
892 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
893 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
894 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
898 $out = ob_get_clean();
905 * An issued badges for badge.php page
907 class issued_badge
implements renderable
{
908 /** @var issued badge */
911 /** @var badge recipient */
914 /** @var badge class */
917 /** @var badge visibility to others */
920 /** @var badge class */
924 * Initializes the badge to display
926 * @param string $hash Issued badge hash
928 public function __construct($hash) {
931 $assertion = new core_badges_assertion($hash);
932 $this->issued
= $assertion->get_badge_assertion();
933 $this->badgeclass
= $assertion->get_badge_class();
935 $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
937 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
938 array('hash' => $hash), IGNORE_MISSING
);
940 // Get a recipient from database.
941 $namefields = get_all_user_name_fields(true, 'u');
942 $user = $DB->get_record_sql("SELECT u.id, $namefields, u.deleted, u.email
943 FROM {user} u WHERE u.id = :userid", array('userid' => $rec->userid
));
944 $this->recipient
= $user;
945 $this->visible
= $rec->visible
;
946 $this->badgeid
= $rec->badgeid
;
952 * An external badges for external.php page
954 class external_badge
implements renderable
{
955 /** @var issued badge */
961 /** @var validation of external badge */
962 public $valid = true;
965 * Initializes the badge to display
967 * @param object $badge External badge information.
968 * @param int $recipient User id.
970 public function __construct($badge, $recipient) {
972 // At this point a user has connected a backpack. So, we are going to get
973 // their backpack email rather than their account email.
974 $namefields = get_all_user_name_fields(true, 'u');
975 $user = $DB->get_record_sql("SELECT {$namefields}, b.email
976 FROM {user} u INNER JOIN {badge_backpack} b ON u.id = b.userid
977 WHERE userid = :userid", array('userid' => $recipient), IGNORE_MISSING
);
979 $this->issued
= $badge;
980 $this->recipient
= $user;
982 // Check if recipient is valid.
983 // There is no way to be 100% sure that a badge belongs to a user.
984 // Backpack does not return any recipient information.
985 // All we can do is compare that backpack email hashed using salt
986 // provided in the assertion matches a badge recipient from the assertion.
988 if (validate_email($badge->assertion
->recipient
) && $badge->assertion
->recipient
== $user->email
) {
989 // If we have email, compare emails.
991 } else if ($badge->assertion
->recipient
== 'sha256$' . hash('sha256', $user->email
)) {
992 // If recipient is hashed, but no salt, compare hashes without salt.
994 } else if ($badge->assertion
->recipient
== 'sha256$' . hash('sha256', $user->email
. $badge->assertion
->salt
)) {
995 // If recipient is hashed, compare hashes.
998 // Otherwise, we cannot be sure that this user is a recipient.
999 $this->valid
= false;
1002 $this->valid
= false;
1008 * Badge recipients rendering class
1010 class badge_recipients
implements renderable
{
1011 /** @var string how are the data sorted */
1012 public $sort = 'lastname';
1014 /** @var string how are the data sorted */
1015 public $dir = 'ASC';
1017 /** @var int page number to display */
1020 /** @var int number of badge recipients to display per page */
1021 public $perpage = 30;
1023 /** @var int the total number or badge recipients to display */
1024 public $totalcount = null;
1026 /** @var array internal list of badge recipients ids */
1027 public $userids = array();
1029 * Initializes the list of users to display
1031 * @param array $holders List of badge holders
1033 public function __construct($holders) {
1034 $this->userids
= $holders;
1039 * Collection of all badges for view.php page
1041 class badge_collection
implements renderable
{
1043 /** @var string how are the data sorted */
1044 public $sort = 'name';
1046 /** @var string how are the data sorted */
1047 public $dir = 'ASC';
1049 /** @var int page number to display */
1052 /** @var int number of badges to display per page */
1053 public $perpage = BADGE_PERPAGE
;
1055 /** @var int the total number of badges to display */
1056 public $totalcount = null;
1058 /** @var array list of badges */
1059 public $badges = array();
1062 * Initializes the list of badges to display
1064 * @param array $badges Badges to render
1066 public function __construct($badges) {
1067 $this->badges
= $badges;
1072 * Collection of badges used at the index.php page
1074 class badge_management
extends badge_collection
implements renderable
{
1078 * Collection of user badges used at the mybadges.php page
1080 class badge_user_collection
extends badge_collection
implements renderable
{
1081 /** @var array backpack settings */
1082 public $backpack = null;
1084 /** @var string search */
1085 public $search = '';
1088 * Initializes user badge collection.
1090 * @param array $badges Badges to render
1091 * @param int $userid Badges owner
1093 public function __construct($badges, $userid) {
1095 parent
::__construct($badges);
1097 if (!empty($CFG->badges_allowexternalbackpack
)) {
1098 $this->backpack
= get_backpack_settings($userid, true);