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');
29 require_once($CFG->dirroot . '/user/filters/lib.php');
32 * Standard HTML output renderer for badges
34 class core_badges_renderer extends plugin_renderer_base {
36 // Outputs badges list.
37 public function print_badges_list($badges, $userid, $profile = false, $external = false) {
39 foreach ($badges as $badge) {
41 $context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid);
42 $bname = $badge->name;
43 $imageurl = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', 'f1', false);
45 $bname = $badge->assertion->badge->name;
46 $imageurl = $badge->imageUrl;
49 $name = html_writer::tag('span', $bname, array('class' => 'badge-name'));
51 $image = html_writer::empty_tag('img', array('src' => $imageurl, 'class' => 'badge-image'));
52 if (!empty($badge->dateexpire) && $badge->dateexpire < time()) {
53 $image .= $this->output->pix_icon('i/expired',
54 get_string('expireddate', 'badges', userdate($badge->dateexpire)),
56 array('class' => 'expireimage'));
57 $name .= '(' . get_string('expired', 'badges') . ')';
60 $download = $status = $push = '';
61 if (($userid == $USER->id) && !$profile) {
62 $url = new moodle_url('mybadges.php', array('download' => $badge->id, 'hash' => $badge->uniquehash, 'sesskey' => sesskey()));
63 $notexpiredbadge = (empty($badge->dateexpire) || $badge->dateexpire > time());
64 $backpackexists = badges_user_has_backpack($USER->id);
65 if (!empty($CFG->badges_allowexternalbackpack) && $notexpiredbadge && $backpackexists) {
66 $assertion = new moodle_url('/badges/assertion.php', array('b' => $badge->uniquehash));
67 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
68 $push = $this->output->action_icon(new moodle_url('#'), new pix_icon('t/backpack', get_string('addtobackpack', 'badges')), $action);
71 $download = $this->output->action_icon($url, new pix_icon('t/download', get_string('download')));
72 if ($badge->visible) {
73 $url = new moodle_url('mybadges.php', array('hide' => $badge->issuedid, 'sesskey' => sesskey()));
74 $status = $this->output->action_icon($url, new pix_icon('t/hide', get_string('makeprivate', 'badges')));
76 $url = new moodle_url('mybadges.php', array('show' => $badge->issuedid, 'sesskey' => sesskey()));
77 $status = $this->output->action_icon($url, new pix_icon('t/show', get_string('makepublic', 'badges')));
82 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash));
85 $url = new moodle_url($CFG->wwwroot . '/badges/badge.php', array('hash' => $badge->uniquehash));
87 $url = new moodle_url($CFG->wwwroot . '/badges/external.php', array('badge' => serialize($badge)));
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)) {
232 // Activate/deactivate badge.
233 if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) {
234 $url = new moodle_url(qualified_me());
235 $url->param('activate', $badge->id);
236 $url->param('sesskey', sesskey());
237 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
239 $url = new moodle_url(qualified_me());
240 $url->param('lock', $badge->id);
241 $url->param('sesskey', sesskey());
242 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
246 // Award badge manually.
247 if ($badge->has_manual_award_criteria() &&
248 has_capability('moodle/badges:awardbadge', $context) &&
249 $badge->is_active()) {
250 $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
251 $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
255 if (has_capability('moodle/badges:configuredetails', $context)) {
256 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'details'));
257 $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
261 if (has_capability('moodle/badges:createbadge', $context)) {
262 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey()));
263 $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
267 if (has_capability('moodle/badges:deletebadge', $context)) {
268 $url = new moodle_url(qualified_me());
269 $url->param('delete', $badge->id);
270 $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
276 // Outputs issued badge with actions available.
277 protected function render_issued_badge(issued_badge $ibadge) {
278 global $USER, $CFG, $DB;
279 $issued = $ibadge->issued;
280 $badge = new badge($ibadge->badgeid);
281 $today_date = date('Y-m-d');
282 $today = strtotime($today_date);
284 $table = new html_table();
286 $imagetable = new html_table();
287 $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
288 $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued['badge']['image'])));
289 if ($USER->id == $ibadge->recipient && !empty($CFG->enablebadges)) {
290 $imagetable->data[] = array($this->output->single_button(
291 new moodle_url('/badges/badge.php', array('hash' => $ibadge->hash, 'bake' => true)),
292 get_string('download'),
294 $expiration = isset($issued['expires']) ? strtotime($issued['expires']) : $today + 1;
295 if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $today) && badges_user_has_backpack($USER->id)) {
296 $assertion = new moodle_url('/badges/assertion.php', array('b' => $ibadge->hash));
299 'value' => get_string('addtobackpack', 'badges'),
300 'onclick' => 'OpenBadges.issue(["' . $assertion->out(false) . '"], function(errors, successes) { })');
301 $tobackpack = html_writer::tag('input', '', $attributes);
302 $imagetable->data[] = array($tobackpack);
305 $datatable = new html_table();
306 $datatable->attributes = array('class' => 'badgeissuedinfo');
307 $datatable->colclasses = array('bfield', 'bvalue');
308 $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
309 $datatable->data[] = array(get_string('issuername', 'badges'), $badge->issuername);
310 if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
311 $datatable->data[] = array(get_string('contact', 'badges'),
312 html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact)));
314 $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
315 $datatable->data[] = array(get_string('name'), $badge->name);
316 $datatable->data[] = array(get_string('description', 'badges'), $badge->description);
318 if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
319 $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid));
320 $datatable->data[] = array(get_string('course'), $coursename);
323 $datatable->data[] = array(get_string('bcriteria', 'badges'), self::print_badge_criteria($badge));
324 $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
325 $datatable->data[] = array(get_string('dateawarded', 'badges'), $issued['issued_on']);
326 if (isset($issued['expires'])) {
327 $expiration = strtotime($issued['expires']);
328 if ($expiration < $today) {
329 $cell = new html_table_cell($issued['expires'] . get_string('warnexpired', 'badges'));
330 $cell->attributes = array('class' => 'notifyproblem warning');
331 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
333 $image = html_writer::start_tag('div', array('class' => 'badge'));
334 $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
335 $image .= $this->output->pix_icon('i/expired',
336 get_string('expireddate', 'badges', $issued['expires']),
338 array('class' => 'expireimage'));
339 $image .= html_writer::end_tag('div');
340 $imagetable->data[0] = array($image);
342 $datatable->data[] = array(get_string('expirydate', 'badges'), $issued['expires']);
347 $agg = $badge->get_aggregation_methods();
348 $evidence = $badge->get_criteria_completions($ibadge->recipient);
349 $eids = array_map(create_function('$o', 'return $o->critid;'), $evidence);
350 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
353 foreach ($badge->criteria as $type => $c) {
354 if (in_array($c->id, $eids)) {
355 if (count($c->params) == 1) {
356 $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
358 $items[] = get_string('criteria_descr_' . $type , 'badges',
359 strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
364 $datatable->data[] = array(get_string('evidence', 'badges'),
365 get_string('completioninfo', 'badges') .
366 html_writer::alist($items, array(), 'ul'));
367 $table->attributes = array('class' => 'generalbox boxaligncenter issuedbadgebox');
368 $table->data[] = array(html_writer::table($imagetable), html_writer::table($datatable));
369 $htmlbadge = html_writer::table($table);
374 // Outputs external badge.
375 protected function render_external_badge(external_badge $ibadge) {
376 $issued = $ibadge->issued;
377 $assertion = $issued->assertion;
378 $issuer = $assertion->badge->issuer;
379 $table = new html_table();
381 $imagetable = new html_table();
382 $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
383 $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued->imageUrl, 'width' => '100px')));
385 $datatable = new html_table();
386 $datatable->attributes = array('class' => 'badgeissuedinfo');
387 $datatable->colclasses = array('bfield', 'bvalue');
388 $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
389 $datatable->data[] = array(get_string('issuername', 'badges'), $issuer->name);
390 $datatable->data[] = array(get_string('issuerurl', 'badges'),
391 html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin)));
392 if (isset($issuer->contact)) {
393 $datatable->data[] = array(get_string('contact', 'badges'),
394 html_writer::tag('a', $issuer->contact, array('href' => 'mailto:' . $issuer->contact)));
396 $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
397 $datatable->data[] = array(get_string('name'), $assertion->badge->name);
398 $datatable->data[] = array(get_string('description', 'badges'), $assertion->badge->description);
399 $datatable->data[] = array(get_string('bcriteria', 'badges'),
400 html_writer::tag('a', $assertion->badge->criteria, array('href' => $assertion->badge->criteria)));
401 $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
402 if (isset($assertion->issued_on)) {
403 $datatable->data[] = array(get_string('dateawarded', 'badges'), $assertion->issued_on);
405 if (isset($assertion->badge->expire)) {
406 $today_date = date('Y-m-d');
407 $today = strtotime($today_date);
408 $expiration = strtotime($assertion->badge->expire);
409 if ($expiration < $today) {
410 $cell = new html_table_cell($assertion->badge->expire . get_string('warnexpired', 'badges'));
411 $cell->attributes = array('class' => 'notifyproblem warning');
412 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
414 $image = html_writer::start_tag('div', array('class' => 'badge'));
415 $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
416 $image .= html_writer::start_tag('span', array('class' => 'expired'))
417 . $this->output->pix_icon('i/expired',
418 get_string('expireddate', 'badges', $assertion->badge->expire),
420 array('class' => 'expireimage'))
421 . html_writer::end_tag('span');
422 $image .= html_writer::end_tag('div');
423 $imagetable->data[0] = array($image);
425 $datatable->data[] = array(get_string('expirydate', 'badges'), $assertion->badge->expire);
428 if (isset($assertion->evidence)) {
429 $datatable->data[] = array(get_string('evidence', 'badges'),
430 html_writer::tag('a', $assertion->evidence, array('href' => $assertion->evidence)));
432 $table->attributes = array('class' => 'generalbox boxaligncenter issuedbadgebox');
433 $table->data[] = array(html_writer::table($imagetable), html_writer::table($datatable));
434 $htmlbadge = html_writer::table($table);
439 // Outputs table of user badges.
440 protected function render_badge_user_collection(badge_user_collection $badges) {
441 global $CFG, $USER, $SITE;
442 $backpack = $badges->backpack;
443 $mybackpack = new moodle_url('/badges/mybackpack.php');
445 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
446 $htmlpagingbar = $this->render($paging);
448 // Set backpack connection string.
449 $backpackconnect = '';
450 if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) {
451 $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
454 $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
456 // Download all button.
457 $downloadall = $this->output->single_button(
458 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
459 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
462 $localhtml = html_writer::start_tag('fieldset', array('class' => 'generalbox'));
463 $localhtml .= html_writer::tag('legend',
464 $this->output->heading_with_help(get_string('localbadges', 'badges', $SITE->fullname), 'localbadgesh', 'badges'));
465 if ($badges->badges) {
466 $table = new html_table();
467 $table->attributes['class'] = 'statustable';
468 $table->data[] = array($this->output->heading(get_string('badgesearned', 'badges', $badges->totalcount), 4, 'activatebadge'), $downloadall);
469 $downloadbutton = html_writer::table($table);
471 $htmllist = $this->print_badges_list($badges->badges, $USER->id);
472 $localhtml .= $backpackconnect . $downloadbutton . $searchform . $htmlpagingbar . $htmllist . $htmlpagingbar;
474 $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
476 $localhtml .= html_writer::end_tag('fieldset');
480 if (!empty($CFG->badges_allowexternalbackpack)) {
481 $externalhtml .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
482 $externalhtml .= html_writer::tag('legend', $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges'));
483 if (!is_null($backpack)) {
484 if ($backpack->totalcollections == 0) {
485 $externalhtml .= get_string('nobackpackcollections', 'badges', $backpack);
487 if ($backpack->totalbadges == 0) {
488 $externalhtml .= get_string('nobackpackbadges', 'badges', $backpack);
490 $externalhtml .= get_string('backpackbadges', 'badges', $backpack);
491 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
495 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
498 $externalhtml .= html_writer::end_tag('fieldset');
501 return $localhtml . $externalhtml;
504 // Outputs table of available badges.
505 protected function render_badge_collection(badge_collection $badges) {
506 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
507 $htmlpagingbar = $this->render($paging);
508 $table = new html_table();
509 $table->attributes['class'] = 'collection';
511 $sortbyname = $this->helper_sortable_heading(get_string('name'),
512 'name', $badges->sort, $badges->dir);
513 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
514 'dateissued', $badges->sort, $badges->dir);
515 $table->head = array(
516 get_string('badgeimage', 'badges'),
518 get_string('description', 'badges'),
519 get_string('bcriteria', 'badges'),
522 $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
524 foreach ($badges->badges as $badge) {
525 $badgeimage = print_badge_image($badge, $this->page->context, 'large');
526 $name = $badge->name;
527 $description = $badge->description;
528 $criteria = self::print_badge_criteria($badge);
529 if ($badge->dateissued) {
530 $icon = new pix_icon('i/valid',
531 get_string('dateearned', 'badges',
532 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
533 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
534 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
538 $row = array($badgeimage, $name, $description, $criteria, $awarded);
539 $table->data[] = $row;
542 $htmltable = html_writer::table($table);
544 return $htmlpagingbar . $htmltable . $htmlpagingbar;
547 // Outputs table of badges with actions available.
548 protected function render_badge_management(badge_management $badges) {
549 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
553 if (has_capability('moodle/badges:createbadge', $this->page->context)) {
554 $n['type'] = $this->page->url->get_param('type');
555 $n['id'] = $this->page->url->get_param('id');
556 $htmlnew = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
559 $htmlpagingbar = $this->render($paging);
560 $table = new html_table();
561 $table->attributes['class'] = 'collection';
563 $sortbyname = $this->helper_sortable_heading(get_string('name'),
564 'name', $badges->sort, $badges->dir);
565 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
566 'status', $badges->sort, $badges->dir);
567 $table->head = array(
570 get_string('bcriteria', 'badges'),
571 get_string('awards', 'badges'),
572 get_string('actions')
574 $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
576 foreach ($badges->badges as $b) {
577 $style = !$b->is_active() ? array('class' => 'dimmed') : array();
578 $forlink = print_badge_image($b, $this->page->context) . ' ' .
579 html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
580 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
581 $status = $b->statstring;
582 $criteria = self::print_badge_criteria($b, 'short');
584 if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
585 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
587 $awards = $b->awards;
590 $actions = self::print_badge_table_actions($b, $this->page->context);
592 $row = array($name, $status, $criteria, $awards, $actions);
593 $table->data[] = $row;
595 $htmltable = html_writer::table($table);
597 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
600 // Prints tabs for badge editing.
601 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
606 $row[] = new tabobject('overview',
607 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
608 get_string('boverview', 'badges')
611 if (has_capability('moodle/badges:configuredetails', $context)) {
612 $row[] = new tabobject('details',
613 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'details')),
614 get_string('bdetails', 'badges')
618 if (has_capability('moodle/badges:configurecriteria', $context)) {
619 $row[] = new tabobject('criteria',
620 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
621 get_string('bcriteria', 'badges')
625 if (has_capability('moodle/badges:configuremessages', $context)) {
626 $row[] = new tabobject('message',
627 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
628 get_string('bmessage', 'badges')
632 if (has_capability('moodle/badges:viewawarded', $context)) {
633 $awarded = $DB->count_records('badge_issued', array('badgeid' => $badgeid));
634 $row[] = new tabobject('awards',
635 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
636 get_string('bawards', 'badges', $awarded)
640 echo $this->tabtree($row, $current);
644 * Prints badge status box.
645 * @return Either the status box html as a string or null
647 public function print_badge_status_box(badge $badge) {
648 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
649 $table = new html_table();
650 $table->attributes['class'] = 'boxaligncenter statustable';
652 if (!$badge->has_criteria()) {
653 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
654 $status = get_string('nocriteria', 'badges');
655 if ($this->page->url != $criteriaurl) {
656 $action = $this->output->single_button(
658 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
662 $row = array($status, $action);
664 $status = get_string('statusmessage_' . $badge->status, 'badges');
665 if ($badge->is_active()) {
666 $action = $this->output->single_button(new moodle_url('/badges/action.php',
667 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
668 'return' => $this->page->url->out_as_local_url(false))),
669 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
671 $action = $this->output->single_button(new moodle_url('/badges/action.php',
672 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
673 'return' => $this->page->url->out_as_local_url(false))),
674 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
676 $row = array($status . $this->output->help_icon('status', 'badges'), $action);
678 $table->data[] = $row;
680 $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
681 return $this->output->box(html_writer::table($table), $style);
687 // Prints badge criteria.
688 public function print_badge_criteria(badge $badge, $short = '') {
690 $agg = $badge->get_aggregation_methods();
691 if (empty($badge->criteria)) {
692 return get_string('nocriteria', 'badges');
693 } else if (count($badge->criteria) == 2) {
695 $output .= get_string('criteria_descr', 'badges');
698 $output .= get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
699 strtoupper($agg[$badge->get_aggregation_method()]));
702 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
703 foreach ($badge->criteria as $type => $c) {
704 if (count($c->params) == 1) {
705 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') . $c->get_details($short);
707 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
708 strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details($short);
711 $output .= html_writer::alist($items, array(), 'ul');
715 // Prints criteria actions for badge editing.
716 public function print_criteria_actions(badge $badge) {
717 $table = new html_table();
718 $table->attributes = array('class' => 'boxaligncenter', 'id' => 'badgeactions');
719 $table->colclasses = array('activatebadge');
722 if (!$badge->is_active() && !$badge->is_locked()) {
723 $accepted = $badge->get_accepted_criteria();
724 $potential = array_diff($accepted, array_keys($badge->criteria));
726 if (!empty($potential)) {
727 foreach ($potential as $p) {
729 $select[$p] = get_string('criteria_' . $p, 'badges');
732 $actions[] = get_string('addbadgecriteria', 'badges');
733 $actions[] = $this->output->single_select(new moodle_url('/badges/criteria_settings.php',
734 array('badgeid' => $badge->id, 'add' => true)), 'type', $select);
736 $actions[] = $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
740 $table->data[] = $actions;
741 return html_writer::table($table);
744 // Renders a table with users who have earned the badge.
745 // Based on stamps collection plugin.
746 protected function render_badge_recipients(badge_recipients $recipients) {
747 $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
748 $htmlpagingbar = $this->render($paging);
749 $table = new html_table();
750 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
752 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
753 'firstname', $recipients->sort, $recipients->dir);
754 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
755 'lastname', $recipients->sort, $recipients->dir);
756 if ($this->helper_fullname_format() == 'lf') {
757 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
759 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
762 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
763 'dateissued', $recipients->sort, $recipients->dir);
765 $table->head = array($sortbyname, $sortbydate, '');
767 foreach ($recipients->userids as $holder) {
768 $fullname = fullname($holder);
769 $fullname = html_writer::link(
770 new moodle_url('/user/profile.php', array('id' => $holder->userid)),
773 $awarded = userdate($holder->dateissued);
774 $badgeurl = html_writer::link(
775 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
776 get_string('viewbadge', 'badges')
779 $row = array($fullname, $awarded, $badgeurl);
780 $table->data[] = $row;
783 $htmltable = html_writer::table($table);
785 return $htmlpagingbar . $htmltable . $htmlpagingbar;
788 ////////////////////////////////////////////////////////////////////////////
790 // Reused from stamps collection plugin
791 ////////////////////////////////////////////////////////////////////////////
794 * Renders a text with icons to sort by the given column
796 * This is intended for table headings.
798 * @param string $text The heading text
799 * @param string $sortid The column id used for sorting
800 * @param string $sortby Currently sorted by (column id)
801 * @param string $sorthow Currently sorted how (ASC|DESC)
805 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
806 $out = html_writer::tag('span', $text, array('class' => 'text'));
808 if (!is_null($sortid)) {
809 if ($sortby !== $sortid || $sorthow !== 'ASC') {
810 $url = new moodle_url($this->page->url);
811 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
812 $out .= $this->output->action_icon($url,
813 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
815 if ($sortby !== $sortid || $sorthow !== 'DESC') {
816 $url = new moodle_url($this->page->url);
817 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
818 $out .= $this->output->action_icon($url,
819 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
825 * Tries to guess the fullname format set at the site
827 * @return string fl|lf
829 protected function helper_fullname_format() {
830 $fake = new stdClass();
831 $fake->lastname = 'LLLL';
832 $fake->firstname = 'FFFF';
833 $fullname = get_string('fullnamedisplay', '', $fake);
834 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
841 * Renders a search form
843 * @param string $search Search string
844 * @return string HTML
846 protected function helper_search_form($search) {
848 require_once($CFG->libdir . '/formslib.php');
850 $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
852 $mform->addElement('hidden', 'sesskey', sesskey());
854 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
855 $mform->setDefault('search', $search);
856 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
857 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
858 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
862 $out = ob_get_clean();
869 * An issued badges for badge.php page
871 class issued_badge implements renderable {
872 /** @var issued badge */
875 /** @var badge recipient */
876 public $recipient = 0;
878 /** @var badge visibility to others */
881 /** @var badge class */
884 /** @var issued badge unique hash */
888 * Initializes the badge to display
890 * @param string $hash Issued badge hash
892 public function __construct($hash) {
894 $this->issued = badges_get_issued_badge_info($hash);
897 $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
899 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
900 array('hash' => $hash), IGNORE_MISSING);
902 $this->recipient = $rec->userid;
903 $this->visible = $rec->visible;
904 $this->badgeid = $rec->badgeid;
910 * An external badges for external.php page
912 class external_badge implements renderable {
913 /** @var issued badge */
917 * Initializes the badge to display
919 * @param string $json External badge information.
921 public function __construct($json) {
922 $this->issued = $json;
927 * Badge recipients rendering class
929 class badge_recipients implements renderable {
930 /** @var string how are the data sorted */
931 public $sort = 'lastname';
933 /** @var string how are the data sorted */
936 /** @var int page number to display */
939 /** @var int number of badge recipients to display per page */
940 public $perpage = 30;
942 /** @var int the total number or badge recipients to display */
943 public $totalcount = null;
945 /** @var array internal list of badge recipients ids */
946 public $userids = array();
948 * Initializes the list of users to display
950 * @param array $holders List of badge holders
952 public function __construct($holders) {
953 $this->userids = $holders;
958 * Collection of all badges for view.php page
960 class badge_collection implements renderable {
962 /** @var string how are the data sorted */
963 public $sort = 'name';
965 /** @var string how are the data sorted */
968 /** @var int page number to display */
971 /** @var int number of badges to display per page */
972 public $perpage = BADGE_PERPAGE;
974 /** @var int the total number of badges to display */
975 public $totalcount = null;
977 /** @var array list of badges */
978 public $badges = array();
981 * Initializes the list of badges to display
983 * @param array $badges Badges to render
985 public function __construct($badges) {
986 $this->badges = $badges;
991 * Collection of badges used at the index.php page
993 class badge_management extends badge_collection implements renderable {
997 * Collection of user badges used at the mybadges.php page
999 class badge_user_collection extends badge_collection implements renderable {
1000 /** @var array backpack settings */
1001 public $backpack = null;
1003 /** @var string search */
1004 public $search = '';
1007 * Initializes user badge collection.
1009 * @param array $badges Badges to render
1010 * @param int $userid Badges owner
1012 public function __construct($badges, $userid) {
1014 parent::__construct($badges);
1016 if (!empty($CFG->badges_allowexternalbackpack)) {
1017 $this->backpack = get_backpack_settings($userid);