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 if (!empty($CFG->badges_allowexternalbackpack) && (empty($badge->dateexpire) || $badge->dateexpire > time())) {
64 $assertion = new moodle_url('/badges/assertion.php', array('b' => $badge->uniquehash));
65 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
66 $push = $this->output->action_icon(new moodle_url('#'), new pix_icon('t/backpack', get_string('addtobackpack', 'badges')), $action);
69 $download = $this->output->action_icon($url, new pix_icon('t/download', get_string('download')));
70 if ($badge->visible) {
71 $url = new moodle_url('mybadges.php', array('hide' => $badge->issuedid, 'sesskey' => sesskey()));
72 $status = $this->output->action_icon($url, new pix_icon('t/hide', get_string('makeprivate', 'badges')));
74 $url = new moodle_url('mybadges.php', array('show' => $badge->issuedid, 'sesskey' => sesskey()));
75 $status = $this->output->action_icon($url, new pix_icon('t/show', get_string('makepublic', 'badges')));
80 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash));
83 $url = new moodle_url($CFG->wwwroot . '/badges/badge.php', array('hash' => $badge->uniquehash));
85 $url = new moodle_url($CFG->wwwroot . '/badges/external.php', array('badge' => serialize($badge)));
88 $actions = html_writer::tag('div', $push . $download . $status, array('class' => 'badge-actions'));
89 $items[] = html_writer::link($url, $image . $actions . $name, array('title' => $bname));
92 return html_writer::alist($items, array('class' => 'badges'));
95 // Recipients selection form.
96 public function recipients_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
98 $formattributes = array();
99 $formattributes['id'] = 'recipientform';
100 $formattributes['action'] = '';
101 $formattributes['method'] = 'post';
102 $output .= html_writer::start_tag('form', $formattributes);
103 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
105 $existingcell = new html_table_cell();
106 $existingcell->text = $existinguc->display(true);
107 $existingcell->attributes['class'] = 'existing';
108 $actioncell = new html_table_cell();
109 $actioncell->text = html_writer::start_tag('div', array());
110 $actioncell->text .= html_writer::empty_tag('input', array(
113 'value' => $this->output->larrow() . ' ' . get_string('award', 'badges'),
114 'class' => 'actionbutton')
116 $actioncell->text .= html_writer::end_tag('div', array());
117 $actioncell->attributes['class'] = 'actions';
118 $potentialcell = new html_table_cell();
119 $potentialcell->text = $potentialuc->display(true);
120 $potentialcell->attributes['class'] = 'potential';
122 $table = new html_table();
123 $table->attributes['class'] = 'recipienttable boxaligncenter';
124 $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
125 $output .= html_writer::table($table);
127 $output .= html_writer::end_tag('form');
131 // Prints a badge overview infomation.
132 public function print_badge_overview($badge, $context) {
136 $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
137 $display .= html_writer::tag('legend', get_string('badgedetails', 'badges'), array('class' => 'bold'));
139 $detailstable = new html_table();
140 $detailstable->attributes = array('class' => 'clearfix', 'id' => 'badgedetails');
141 $detailstable->data[] = array(get_string('name') . ":", $badge->name);
142 $detailstable->data[] = array(get_string('description', 'badges') . ":", $badge->description);
143 $detailstable->data[] = array(get_string('createdon', 'search') . ":", userdate($badge->timecreated));
144 $detailstable->data[] = array(get_string('badgeimage', 'badges') . ":",
145 print_badge_image($badge, $context, 'large'));
146 $display .= html_writer::table($detailstable);
147 $display .= html_writer::end_tag('fieldset');
150 $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
151 $display .= html_writer::tag('legend', get_string('issuerdetails', 'badges'), array('class' => 'bold'));
153 $issuertable = new html_table();
154 $issuertable->attributes = array('class' => 'clearfix', 'id' => 'badgeissuer');
155 $issuertable->data[] = array(get_string('issuername', 'badges') . ":", $badge->issuername);
156 $issuertable->data[] = array(get_string('contact', 'badges') . ":",
157 html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact)));
158 $display .= html_writer::table($issuertable);
159 $display .= html_writer::end_tag('fieldset');
161 // Issuance details if any.
162 $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
163 $display .= html_writer::tag('legend', get_string('issuancedetails', 'badges'), array('class' => 'bold'));
164 if ($badge->can_expire()) {
165 if ($badge->expiredate) {
166 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate));
167 } else if ($badge->expireperiod) {
168 if ($badge->expireperiod < 60) {
169 $display .= get_string('expireperiods', 'badges', round($badge->expireperiod, 2));
170 } else if ($badge->expireperiod < 60 * 60) {
171 $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod / 60, 2));
172 } else if ($badge->expireperiod < 60 * 60 * 24) {
173 $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod / 60 / 60, 2));
175 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2));
179 $display .= get_string('noexpiry', 'badges');
181 $display .= html_writer::end_tag('fieldset');
183 // Criteria details if any.
184 $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
185 $display .= html_writer::tag('legend', get_string('bcriteria', 'badges'), array('class' => 'bold'));
186 if ($badge->has_criteria()) {
187 $display .= self::print_badge_criteria($badge);
189 $display .= get_string('nocriteria', 'badges');
190 if (has_capability('moodle/badges:configurecriteria', $context)) {
191 $display .= $this->output->single_button(
192 new moodle_url('/badges/criteria.php', array('id' => $badge->id)),
193 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
196 $display .= html_writer::end_tag('fieldset');
198 // Awards details if any.
199 if (has_capability('moodle/badges:viewawarded', $context)) {
200 $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
201 $display .= html_writer::tag('legend', get_string('awards', 'badges'), array('class' => 'bold'));
202 if ($badge->has_awards()) {
203 $url = new moodle_url('/badges/recipients.php', array('id' => $badge->id));
205 $a->link = $url->out();
206 $a->count = count($badge->get_awards());
207 $display .= get_string('numawards', 'badges', $a);
209 $display .= get_string('noawards', 'badges');
212 if (has_capability('moodle/badges:awardbadge', $context) &&
213 $badge->has_manual_award_criteria() &&
214 $badge->is_active()) {
215 $display .= $this->output->single_button(
216 new moodle_url('/badges/award.php', array('id' => $badge->id)),
217 get_string('award', 'badges'), 'POST', array('class' => 'activatebadge'));
219 $display .= html_writer::end_tag('fieldset');
225 // Prints action icons for the badge.
226 public function print_badge_table_actions($badge, $context) {
229 if (has_capability('moodle/badges:configuredetails', $context)) {
230 // Activate/deactivate badge.
231 if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) {
232 $url = new moodle_url(qualified_me());
233 $url->param('activate', $badge->id);
234 $url->param('sesskey', sesskey());
235 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
237 $url = new moodle_url(qualified_me());
238 $url->param('lock', $badge->id);
239 $url->param('sesskey', sesskey());
240 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
244 // Award badge manually.
245 if ($badge->has_manual_award_criteria() &&
246 has_capability('moodle/badges:awardbadge', $context) &&
247 $badge->is_active()) {
248 $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
249 $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
253 if (has_capability('moodle/badges:configuredetails', $context)) {
254 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'details'));
255 $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
259 if (has_capability('moodle/badges:createbadge', $context)) {
260 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey()));
261 $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
265 if (has_capability('moodle/badges:deletebadge', $context)) {
266 $url = new moodle_url(qualified_me());
267 $url->param('delete', $badge->id);
268 $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
274 // Outputs issued badge with actions available.
275 protected function render_issued_badge(issued_badge $ibadge) {
276 global $USER, $CFG, $DB;
277 $issued = $ibadge->issued;
278 $badge = new badge($ibadge->badgeid);
279 $today_date = date('Y-m-d');
280 $today = strtotime($today_date);
283 || ($USER->id == $ibadge->recipient)
284 || has_capability('moodle/badges:viewawarded', context_system::instance())) {
285 $table = new html_table();
287 $imagetable = new html_table();
288 $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
289 $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued['badge']['image'])));
290 if ($USER->id == $ibadge->recipient && !empty($CFG->enablebadges)) {
291 $imagetable->data[] = array($this->output->single_button(
292 new moodle_url('/badges/badge.php', array('hash' => $ibadge->hash, 'bake' => true)),
293 get_string('download'),
295 $expiration = isset($issued['expires']) ? strtotime($issued['expires']) : $today + 1;
296 if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $today)) {
297 $assertion = new moodle_url('/badges/assertion.php', array('b' => $ibadge->hash));
300 'value' => get_string('addtobackpack', 'badges'),
301 'onclick' => 'OpenBadges.issue(["' . $assertion->out(false) . '"], function(errors, successes) { })');
302 $tobackpack = html_writer::tag('input', '', $attributes);
303 $imagetable->data[] = array($tobackpack);
306 $datatable = new html_table();
307 $datatable->attributes = array('class' => 'badgeissuedinfo');
308 $datatable->colclasses = array('bfield', 'bvalue');
309 $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
310 $datatable->data[] = array(get_string('issuername', 'badges'), $badge->issuername);
311 if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
312 $datatable->data[] = array(get_string('contact', 'badges'),
313 html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact)));
315 $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
316 $datatable->data[] = array(get_string('name'), $badge->name);
317 $datatable->data[] = array(get_string('description', 'badges'), $badge->description);
319 if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
320 $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid));
321 $datatable->data[] = array(get_string('course'), $coursename);
324 $datatable->data[] = array(get_string('bcriteria', 'badges'), self::print_badge_criteria($badge));
325 $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
326 $datatable->data[] = array(get_string('dateawarded', 'badges'), $issued['issued_on']);
327 if (isset($issued['expires'])) {
328 $expiration = strtotime($issued['expires']);
329 if ($expiration < $today) {
330 $cell = new html_table_cell($issued['expires'] . get_string('warnexpired', 'badges'));
331 $cell->attributes = array('class' => 'notifyproblem warning');
332 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
334 $image = html_writer::start_tag('div', array('class' => 'badge'));
335 $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
336 $image .= $this->output->pix_icon('i/expired',
337 get_string('expireddate', 'badges', $issued['expires']),
339 array('class' => 'expireimage'));
340 $image .= html_writer::end_tag('div');
341 $imagetable->data[0] = array($image);
343 $datatable->data[] = array(get_string('expirydate', 'badges'), $issued['expires']);
348 $agg = $badge->get_aggregation_methods();
349 $evidence = $badge->get_criteria_completions($ibadge->recipient);
350 $eids = array_map(create_function('$o', 'return $o->critid;'), $evidence);
351 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
354 foreach ($badge->criteria as $type => $c) {
355 if (in_array($c->id, $eids)) {
356 if (count($c->params) == 1) {
357 $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
359 $items[] = get_string('criteria_descr_' . $type , 'badges',
360 strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
365 $datatable->data[] = array(get_string('evidence', 'badges'),
366 get_string('completioninfo', 'badges') .
367 html_writer::alist($items, array(), 'ul'));
368 $table->attributes = array('class' => 'generalbox boxaligncenter issuedbadgebox');
369 $table->data[] = array(html_writer::table($imagetable), html_writer::table($datatable));
370 $htmlbadge = html_writer::table($table);
374 return get_string('hiddenbadge', 'badges');
378 // Outputs external badge.
379 protected function render_external_badge(external_badge $ibadge) {
380 $issued = $ibadge->issued;
381 $assertion = $issued->assertion;
382 $issuer = $assertion->badge->issuer;
383 $table = new html_table();
385 $imagetable = new html_table();
386 $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
387 $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued->imageUrl, 'width' => '100px')));
389 $datatable = new html_table();
390 $datatable->attributes = array('class' => 'badgeissuedinfo');
391 $datatable->colclasses = array('bfield', 'bvalue');
392 $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
393 $datatable->data[] = array(get_string('issuername', 'badges'), $issuer->name);
394 $datatable->data[] = array(get_string('issuerurl', 'badges'),
395 html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin)));
396 if (isset($issuer->contact)) {
397 $datatable->data[] = array(get_string('contact', 'badges'),
398 html_writer::tag('a', $issuer->contact, array('href' => 'mailto:' . $issuer->contact)));
400 $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
401 $datatable->data[] = array(get_string('name'), $assertion->badge->name);
402 $datatable->data[] = array(get_string('description', 'badges'), $assertion->badge->description);
403 $datatable->data[] = array(get_string('bcriteria', 'badges'),
404 html_writer::tag('a', $assertion->badge->criteria, array('href' => $assertion->badge->criteria)));
405 $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
406 if (isset($assertion->issued_on)) {
407 $datatable->data[] = array(get_string('dateawarded', 'badges'), $assertion->issued_on);
409 if (isset($assertion->badge->expire)) {
410 $today_date = date('Y-m-d');
411 $today = strtotime($today_date);
412 $expiration = strtotime($assertion->badge->expire);
413 if ($expiration < $today) {
414 $cell = new html_table_cell($assertion->badge->expire . get_string('warnexpired', 'badges'));
415 $cell->attributes = array('class' => 'notifyproblem warning');
416 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
418 $image = html_writer::start_tag('div', array('class' => 'badge'));
419 $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
420 $image .= html_writer::start_tag('span', array('class' => 'expired'))
421 . $this->output->pix_icon('i/expired',
422 get_string('expireddate', 'badges', $assertion->badge->expire),
424 array('class' => 'expireimage'))
425 . html_writer::end_tag('span');
426 $image .= html_writer::end_tag('div');
427 $imagetable->data[0] = array($image);
429 $datatable->data[] = array(get_string('expirydate', 'badges'), $assertion->badge->expire);
432 if (isset($assertion->evidence)) {
433 $datatable->data[] = array(get_string('evidence', 'badges'),
434 html_writer::tag('a', $assertion->evidence, array('href' => $assertion->evidence)));
436 $table->attributes = array('class' => 'generalbox boxaligncenter issuedbadgebox');
437 $table->data[] = array(html_writer::table($imagetable), html_writer::table($datatable));
438 $htmlbadge = html_writer::table($table);
443 // Outputs table of user badges.
444 protected function render_badge_user_collection(badge_user_collection $badges) {
445 global $CFG, $USER, $SITE;
446 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
447 $htmlpagingbar = $this->render($paging);
450 $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
452 // Download all button.
453 $downloadall = $this->output->single_button(
454 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
455 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
458 $localhtml = html_writer::start_tag('fieldset', array('class' => 'generalbox'));
459 $localhtml .= html_writer::tag('legend',
460 $this->output->heading_with_help(get_string('localbadges', 'badges', $SITE->fullname), 'localbadgesh', 'badges'));
461 if ($badges->badges) {
462 $table = new html_table();
463 $table->attributes['class'] = 'statustable';
464 $table->data[] = array($this->output->heading(get_string('badgesearned', 'badges', $badges->totalcount), 4, 'activatebadge'), $downloadall);
465 $downloadbutton = html_writer::table($table);
467 $htmllist = $this->print_badges_list($badges->badges, $USER->id);
468 $localhtml .= $downloadbutton . $searchform . $htmlpagingbar . $htmllist . $htmlpagingbar;
470 $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
472 $localhtml .= html_writer::end_tag('fieldset');
475 $backpack = $badges->backpack;
477 if (!empty($CFG->badges_allowexternalbackpack)) {
478 $externalhtml .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
479 $externalhtml .= html_writer::tag('legend', $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges'));
480 if (!is_null($backpack)) {
481 if ($backpack->totalbadges > 0) {
482 $externalhtml .= get_string('backpackbadges', 'badges', $backpack);
484 $externalhtml .= get_string('nobackpackbadges', 'badges', $backpack);
486 $label = get_string('editsettings', 'badges');
487 $externalhtml .= $this->output->single_button(
488 new moodle_url('mybackpack.php', array('clear' => true)),
489 get_string('clearsettings', 'badges'),
491 array('class' => 'backpackform'));
493 $externalhtml .= get_string('nobackpack', 'badges');
494 $label = get_string('setup', 'badges');
496 $externalhtml .= $this->output->single_button('mybackpack.php', $label, 'POST', array('class' => 'backpackform'));
498 if (isset($backpack->totalbadges) && $backpack->totalbadges !== 0) {
499 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
501 $externalhtml .= html_writer::end_tag('fieldset');
504 return $localhtml . $externalhtml;
507 // Outputs table of available badges.
508 protected function render_badge_collection(badge_collection $badges) {
509 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
510 $htmlpagingbar = $this->render($paging);
511 $table = new html_table();
512 $table->attributes['class'] = 'collection boxaligncenter boxwidthwide';
514 $sortbyname = $this->helper_sortable_heading(get_string('name'),
515 'name', $badges->sort, $badges->dir);
516 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
517 'dateissued', $badges->sort, $badges->dir);
518 $table->head = array(
519 get_string('badgeimage', 'badges'),
521 get_string('description', 'badges'),
522 get_string('bcriteria', 'badges'),
525 $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
527 foreach ($badges->badges as $badge) {
528 $badgeimage = print_badge_image($badge, $this->page->context, 'large');
529 $name = $badge->name;
530 $description = $badge->description;
531 $criteria = self::print_badge_criteria($badge);
532 if ($badge->dateissued) {
533 $icon = new pix_icon('i/tick_green_big',
534 get_string('dateearned', 'badges',
535 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
536 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
537 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
541 $row = array($badgeimage, $name, $description, $criteria, $awarded);
542 $table->data[] = $row;
545 $htmltable = html_writer::table($table);
547 return $htmlpagingbar . $htmltable . $htmlpagingbar;
550 // Outputs table of badges with actions available.
551 protected function render_badge_management(badge_management $badges) {
552 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
555 $n['type'] = $this->page->url->get_param('type');
556 $n['id'] = $this->page->url->get_param('id');
557 $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') {
604 $tabs = $row = array();
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)
642 print_tabs($tabs, $current);
645 // Prints badge status box.
646 public function print_badge_status_box(badge $badge) {
647 $table = new html_table();
648 $table->attributes['class'] = 'boxaligncenter statustable';
650 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
651 if (!$badge->has_criteria()) {
652 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
653 $status = get_string('nocriteria', 'badges');
654 if ($this->page->url != $criteriaurl) {
655 $action = $this->output->single_button(
657 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
661 $row = array($status, $action);
663 $status = get_string('statusmessage_' . $badge->status, 'badges');
664 if ($badge->is_active()) {
665 $action = $this->output->single_button(new moodle_url('/badges/action.php',
666 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
667 'return' => $this->page->url->out_as_local_url(false))),
668 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
670 $action = $this->output->single_button(new moodle_url('/badges/action.php',
671 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
672 'return' => $this->page->url->out_as_local_url(false))),
673 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
675 $row = array($status . $this->output->help_icon('status', 'badges'), $action);
679 $table->data[] = $row;
681 $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
682 return $this->output->box(html_writer::table($table), $style);
685 // Prints badge criteria.
686 public function print_badge_criteria(badge $badge, $short = '') {
688 $agg = $badge->get_aggregation_methods();
689 if (empty($badge->criteria)) {
690 return get_string('nocriteria', 'badges');
691 } else if (count($badge->criteria) == 2) {
693 $output .= get_string('criteria_descr', 'badges');
696 $output .= get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
697 strtoupper($agg[$badge->get_aggregation_method()]));
700 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
701 foreach ($badge->criteria as $type => $c) {
702 if (count($c->params) == 1) {
703 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') . $c->get_details($short);
705 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
706 strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details($short);
709 $output .= html_writer::alist($items, array(), 'ul');
713 // Prints criteria actions for badge editing.
714 public function print_criteria_actions(badge $badge) {
715 $table = new html_table();
716 $table->attributes = array('class' => 'boxaligncenter', 'id' => 'badgeactions');
717 $table->colclasses = array('activatebadge');
720 if (!$badge->is_active() && !$badge->is_locked()) {
721 $accepted = $badge->get_accepted_criteria();
722 $potential = array_diff($accepted, array_keys($badge->criteria));
724 if (!empty($potential)) {
725 foreach ($potential as $p) {
727 $select[$p] = get_string('criteria_' . $p, 'badges');
730 $actions[] = get_string('addbadgecriteria', 'badges');
731 $actions[] = $this->output->single_select(new moodle_url('/badges/criteria_settings.php',
732 array('badgeid' => $badge->id, 'add' => true)), 'type', $select);
734 $actions[] = $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
738 $table->data[] = $actions;
739 return html_writer::table($table);
742 // Renders a table with users who have earned the badge.
743 // Based on stamps collection plugin.
744 protected function render_badge_recipients(badge_recipients $recipients) {
745 $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
746 $htmlpagingbar = $this->render($paging);
747 $table = new html_table();
748 $table->attributes['class'] = 'generaltable generalbox boxaligncenter boxwidthwide';
750 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
751 'firstname', $recipients->sort, $recipients->dir);
752 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
753 'lastname', $recipients->sort, $recipients->dir);
754 if ($this->helper_fullname_format() == 'lf') {
755 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
757 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
760 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
761 'dateissued', $recipients->sort, $recipients->dir);
763 $table->head = array($sortbyname, $sortbydate, '');
765 foreach ($recipients->userids as $holder) {
766 $fullname = fullname($holder);
767 $fullname = html_writer::link(
768 new moodle_url('/user/profile.php', array('id' => $holder->userid)),
771 $awarded = userdate($holder->dateissued);
772 $badgeurl = html_writer::link(
773 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
774 get_string('viewbadge', 'badges')
777 $row = array($fullname, $awarded, $badgeurl);
778 $table->data[] = $row;
781 $htmltable = html_writer::table($table);
783 return $htmlpagingbar . $htmltable . $htmlpagingbar;
786 ////////////////////////////////////////////////////////////////////////////
788 // Reused from stamps collection plugin
789 ////////////////////////////////////////////////////////////////////////////
792 * Renders a text with icons to sort by the given column
794 * This is intended for table headings.
796 * @param string $text The heading text
797 * @param string $sortid The column id used for sorting
798 * @param string $sortby Currently sorted by (column id)
799 * @param string $sorthow Currently sorted how (ASC|DESC)
803 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
804 $out = html_writer::tag('span', $text, array('class' => 'text'));
806 if (!is_null($sortid)) {
807 if ($sortby !== $sortid || $sorthow !== 'ASC') {
808 $url = new moodle_url($this->page->url);
809 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
810 $out .= $this->output->action_icon($url,
811 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
813 if ($sortby !== $sortid || $sorthow !== 'DESC') {
814 $url = new moodle_url($this->page->url);
815 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
816 $out .= $this->output->action_icon($url,
817 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
823 * Tries to guess the fullname format set at the site
825 * @return string fl|lf
827 protected function helper_fullname_format() {
828 $fake = new stdClass();
829 $fake->lastname = 'LLLL';
830 $fake->firstname = 'FFFF';
831 $fullname = get_string('fullnamedisplay', '', $fake);
832 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
839 * Renders a search form
841 * @param string $search Search string
842 * @return string HTML
844 protected function helper_search_form($search) {
846 require_once($CFG->libdir . '/formslib.php');
848 $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
850 $mform->addElement('hidden', 'sesskey', sesskey());
852 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
853 $mform->setDefault('search', $search);
854 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
855 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
856 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
860 $out = ob_get_clean();
867 * An issued badges for badge.php page
869 class issued_badge implements renderable {
870 /** @var issued badge */
873 /** @var badge recipient */
874 public $recipient = 0;
876 /** @var badge visibility to others */
879 /** @var badge class */
882 /** @var issued badge unique hash */
886 * Initializes the badge to display
888 * @param string $hash Issued badge hash
890 public function __construct($hash) {
892 $this->issued = badges_get_issued_badge_info($hash);
895 $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
897 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
898 array('hash' => $hash), IGNORE_MISSING);
900 $this->recipient = $rec->userid;
901 $this->visible = $rec->visible;
902 $this->badgeid = $rec->badgeid;
908 * An external badges for external.php page
910 class external_badge implements renderable {
911 /** @var issued badge */
915 * Initializes the badge to display
917 * @param string $json External badge information.
919 public function __construct($json) {
920 $this->issued = $json;
925 * Badge recipients rendering class
927 class badge_recipients implements renderable {
928 /** @var string how are the data sorted */
929 public $sort = 'lastname';
931 /** @var string how are the data sorted */
934 /** @var int page number to display */
937 /** @var int number of badge recipients to display per page */
938 public $perpage = 30;
940 /** @var int the total number or badge recipients to display */
941 public $totalcount = null;
943 /** @var array internal list of badge recipients ids */
944 public $userids = array();
946 * Initializes the list of users to display
948 * @param array $holders List of badge holders
950 public function __construct($holders) {
951 $this->userids = $holders;
956 * Collection of all badges for view.php page
958 class badge_collection implements renderable {
960 /** @var string how are the data sorted */
961 public $sort = 'name';
963 /** @var string how are the data sorted */
966 /** @var int page number to display */
969 /** @var int number of badges to display per page */
970 public $perpage = BADGE_PERPAGE;
972 /** @var int the total number of badges to display */
973 public $totalcount = null;
975 /** @var array list of badges */
976 public $badges = array();
979 * Initializes the list of badges to display
981 * @param array $badges Badges to render
983 public function __construct($badges) {
984 $this->badges = $badges;
989 * Collection of badges used at the index.php page
991 class badge_management extends badge_collection implements renderable {
995 * Collection of user badges used at the mybadges.php page
997 class badge_user_collection extends badge_collection implements renderable {
998 /** @var array backpack settings */
1001 /** @var string search */
1002 public $search = '';
1005 * Initializes user badge collection.
1007 * @param array $badges Badges to render
1008 * @param int $userid Badges owner
1010 public function __construct($badges, $userid) {
1011 parent::__construct($badges);
1012 $this->backpack = get_backpack_settings($userid);