Merge branch 'openbadges-fixes' of https://github.com/danpoltawski/moodle
[moodle.git] / badges / renderer.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Renderer for use with the badges output
19  *
20  * @package    core
21  * @subpackage badges
22  * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
25  */
27 require_once($CFG->libdir . '/badgeslib.php');
28 require_once($CFG->libdir . '/tablelib.php');
29 require_once($CFG->dirroot . '/user/filters/lib.php');
31 /**
32  * Standard HTML output renderer for badges
33  */
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) {
38         global $USER, $CFG;
39         foreach ($badges as $badge) {
40             if (!$external) {
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);
44             } else {
45                 $bname = $badge->assertion->badge->name;
46                 $imageurl = $badge->imageUrl;
47             }
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)),
55                         'moodle',
56                         array('class' => 'expireimage'));
57                 $name .= '(' . get_string('expired', 'badges') . ')';
58             }
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 ($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);
67                 }
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')));
73                 } else {
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')));
76                 }
77             }
79             if (!$profile) {
80                 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash));
81             } else {
82                 if (!$external) {
83                     $url = new moodle_url($CFG->wwwroot . '/badges/badge.php', array('hash' => $badge->uniquehash));
84                 } else {
85                     $url = new moodle_url($CFG->wwwroot . '/badges/external.php', array('badge' => serialize($badge)));
86                 }
87             }
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));
90         }
92         return html_writer::alist($items, array('class' => 'badges'));
93     }
95     // Recipients selection form.
96     public function recipients_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
97         $output = '';
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(
111                     'type' => 'submit',
112                     'name' => 'award',
113                     'value' => $this->output->larrow() . ' ' . get_string('award', 'badges'),
114                     'class' => 'actionbutton')
115                 );
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');
128         return $output;
129     }
131     // Prints a badge overview infomation.
132     public function print_badge_overview($badge, $context) {
133         $display = "";
135         // Badge details.
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');
149         // Issuer details.
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));
174                 } else {
175                     $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2));
176                 }
177             }
178         } else {
179             $display .= get_string('noexpiry', 'badges');
180         }
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);
188         } else {
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'));
194             }
195         }
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));
204                 $a = new stdClass();
205                 $a->link = $url->out();
206                 $a->count = count($badge->get_awards());
207                 $display .= get_string('numawards', 'badges', $a);
208             } else {
209                 $display .= get_string('noawards', 'badges');
210             }
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'));
218             }
219             $display .= html_writer::end_tag('fieldset');
220         }
222         return $display;
223     }
225     // Prints action icons for the badge.
226     public function print_badge_table_actions($badge, $context) {
227         $actions = "";
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                 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
235             } else {
236                 $url = new moodle_url(qualified_me());
237                 $url->param('lock', $badge->id);
238                 $url->param('sesskey', sesskey());
239                 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
240             }
241         }
243         // Award badge manually.
244         if ($badge->has_manual_award_criteria() &&
245                 has_capability('moodle/badges:awardbadge', $context) &&
246                 $badge->is_active()) {
247             $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
248             $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
249         }
251         // Edit badge.
252         if (has_capability('moodle/badges:configuredetails', $context)) {
253             $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'details'));
254             $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
255         }
257         // Duplicate badge.
258         if (has_capability('moodle/badges:createbadge', $context)) {
259             $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey()));
260             $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
261         }
263         // Delete badge.
264         if (has_capability('moodle/badges:deletebadge', $context)) {
265             $url = new moodle_url(qualified_me());
266             $url->param('delete', $badge->id);
267             $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
268         }
270         return $actions;
271     }
273     // Outputs issued badge with actions available.
274     protected function render_issued_badge(issued_badge $ibadge) {
275         global $USER, $CFG, $DB;
276         $issued = $ibadge->issued;
277         $badge = new badge($ibadge->badgeid);
278         $today_date = date('Y-m-d');
279         $today = strtotime($today_date);
281         if ($ibadge->visible
282             || ($USER->id == $ibadge->recipient)
283             || has_capability('moodle/badges:viewawarded', context_system::instance())) {
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'),
293                             'POST'));
294                 $expiration = isset($issued['expires']) ? strtotime($issued['expires']) : $today + 1;
295                 if ($CFG->badges_allowexternalbackpack && ($expiration > $today)) {
296                     $assertion = new moodle_url('/badges/assertion.php', array('b' => $ibadge->hash));
297                     $attributes = array(
298                             'type' => 'button',
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);
303                 }
304             }
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)));
313             }
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);
321             }
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']),
337                                     'moodle',
338                                     array('class' => 'expireimage'));
339                     $image .= html_writer::end_tag('div');
340                     $imagetable->data[0] = array($image);
341                 } else {
342                     $datatable->data[] = array(get_string('expirydate', 'badges'), $issued['expires']);
343                 }
344             }
346             // Print evidence.
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]);
352             $items = array();
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();
357                     } else {
358                         $items[] = get_string('criteria_descr_' . $type , 'badges',
359                                 strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
360                     }
361                 }
362             }
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);
371             return $htmlbadge;
372         } else {
373             return get_string('hiddenbadge', 'badges');
374         }
375     }
377     // Outputs external badge.
378     protected function render_external_badge(external_badge $ibadge) {
379         $issued = $ibadge->issued;
380         $assertion = $issued->assertion;
381         $issuer = $assertion->badge->issuer;
382         $table = new html_table();
384         $imagetable = new html_table();
385         $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
386         $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued->imageUrl, 'width' => '100px')));
388         $datatable = new html_table();
389         $datatable->attributes = array('class' => 'badgeissuedinfo');
390         $datatable->colclasses = array('bfield', 'bvalue');
391         $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
392         $datatable->data[] = array(get_string('issuername', 'badges'), $issuer->name);
393         $datatable->data[] = array(get_string('issuerurl', 'badges'),
394                 html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin)));
395         if (isset($issuer->contact)) {
396             $datatable->data[] = array(get_string('contact', 'badges'),
397                 html_writer::tag('a', $issuer->contact, array('href' => 'mailto:' . $issuer->contact)));
398         }
399         $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
400         $datatable->data[] = array(get_string('name'), $assertion->badge->name);
401         $datatable->data[] = array(get_string('description', 'badges'), $assertion->badge->description);
402         $datatable->data[] = array(get_string('bcriteria', 'badges'),
403                 html_writer::tag('a', $assertion->badge->criteria, array('href' => $assertion->badge->criteria)));
404         $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
405         if (isset($assertion->issued_on)) {
406             $datatable->data[] = array(get_string('dateawarded', 'badges'), $assertion->issued_on);
407         }
408         if (isset($assertion->badge->expire)) {
409             $today_date = date('Y-m-d');
410             $today = strtotime($today_date);
411             $expiration = strtotime($assertion->badge->expire);
412             if ($expiration < $today) {
413                 $cell = new html_table_cell($assertion->badge->expire . get_string('warnexpired', 'badges'));
414                 $cell->attributes = array('class' => 'notifyproblem warning');
415                 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
417                 $image = html_writer::start_tag('div', array('class' => 'badge'));
418                 $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
419                 $image .= html_writer::start_tag('span', array('class' => 'expired'))
420                             . $this->output->pix_icon('i/expired',
421                                 get_string('expireddate', 'badges', $assertion->badge->expire),
422                                 'moodle',
423                                 array('class' => 'expireimage'))
424                             . html_writer::end_tag('span');
425                 $image .= html_writer::end_tag('div');
426                 $imagetable->data[0] = array($image);
427             } else {
428                 $datatable->data[] = array(get_string('expirydate', 'badges'), $assertion->badge->expire);
429             }
430         }
431         if (isset($assertion->evidence)) {
432             $datatable->data[] = array(get_string('evidence', 'badges'),
433                 html_writer::tag('a', $assertion->evidence, array('href' => $assertion->evidence)));
434         }
435         $table->attributes = array('class' => 'generalbox boxaligncenter issuedbadgebox');
436         $table->data[] = array(html_writer::table($imagetable), html_writer::table($datatable));
437         $htmlbadge = html_writer::table($table);
439         return $htmlbadge;
440     }
442     // Outputs table of user badges.
443     protected function render_badge_user_collection(badge_user_collection $badges) {
444         global $CFG, $USER, $SITE;
445         $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
446         $htmlpagingbar = $this->render($paging);
448         // Search box.
449         $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
451         // Download all button.
452         $downloadall = $this->output->single_button(
453                     new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
454                     get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
456         // Local badges.
457         $localhtml = html_writer::start_tag('fieldset', array('class' => 'generalbox'));
458         $localhtml .= html_writer::tag('legend',
459                     $this->output->heading_with_help(get_string('localbadges', 'badges', $SITE->fullname), 'localbadgesh', 'badges'));
460         if ($badges->badges) {
461             $table = new html_table();
462             $table->attributes['class'] = 'statustable';
463             $table->data[] = array($this->output->heading(get_string('badgesearned', 'badges', $badges->totalcount), 4, 'activatebadge'), $downloadall);
464             $downloadbutton = html_writer::table($table);
466             $htmllist = $this->print_badges_list($badges->badges, $USER->id);
467             $localhtml .= $downloadbutton . $searchform . $htmlpagingbar . $htmllist . $htmlpagingbar;
468         } else {
469             $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
470         }
471         $localhtml .= html_writer::end_tag('fieldset');
473         // External badges.
474         $backpack = $badges->backpack;
475         $externalhtml = "";
476         if ($CFG->badges_allowexternalbackpack) {
477             $externalhtml .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
478             $externalhtml .= html_writer::tag('legend', $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges'));
479             if (!is_null($backpack)) {
480                 if ($backpack->totalbadges > 0) {
481                     $externalhtml .= get_string('backpackbadges', 'badges', $backpack);
482                 } else {
483                     $externalhtml .= get_string('nobackpackbadges', 'badges', $backpack);
484                 }
485                 $label = get_string('editsettings', 'badges');
486                 $externalhtml .= $this->output->single_button(
487                         new moodle_url('mybackpack.php', array('clear' => true)),
488                         get_string('clearsettings', 'badges'),
489                         'POST',
490                         array('class' => 'backpackform'));
491             } else {
492                 $externalhtml .= get_string('nobackpack', 'badges');
493                 $label = get_string('setup', 'badges');
494             }
495             $externalhtml .= $this->output->single_button('mybackpack.php', $label, 'POST', array('class' => 'backpackform'));
497             if (isset($backpack->totalbadges) && $backpack->totalbadges !== 0) {
498                 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
499             }
500             $externalhtml .= html_writer::end_tag('fieldset');
501         }
503         return $localhtml . $externalhtml;
504     }
506     // Outputs table of available badges.
507     protected function render_badge_collection(badge_collection $badges) {
508         $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
509         $htmlpagingbar = $this->render($paging);
510         $table = new html_table();
511         $table->attributes['class'] = 'collection boxaligncenter boxwidthwide';
513         $sortbyname = $this->helper_sortable_heading(get_string('name'),
514                 'name', $badges->sort, $badges->dir);
515         $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
516                 'dateissued', $badges->sort, $badges->dir);
517         $table->head = array(
518                     get_string('badgeimage', 'badges'),
519                     $sortbyname,
520                     get_string('description', 'badges'),
521                     get_string('bcriteria', 'badges'),
522                     $sortbyawarded
523                 );
524         $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
526         foreach ($badges->badges as $badge) {
527             $badgeimage = print_badge_image($badge, $this->page->context, 'large');
528             $name = $badge->name;
529             $description = $badge->description;
530             $criteria = self::print_badge_criteria($badge);
531             if ($badge->dateissued) {
532                 $icon = new pix_icon('i/tick_green_big',
533                             get_string('dateearned', 'badges',
534                                 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
535                 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
536                 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
537             } else {
538                 $awarded = "";
539             }
540             $row = array($badgeimage, $name, $description, $criteria, $awarded);
541             $table->data[] = $row;
542         }
544         $htmltable = html_writer::table($table);
546         return $htmlpagingbar . $htmltable . $htmlpagingbar;
547     }
549     // Outputs table of badges with actions available.
550     protected function render_badge_management(badge_management $badges) {
551         $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
553         // New badge button.
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'));
558         $htmlpagingbar = $this->render($paging);
559         $table = new html_table();
560         $table->attributes['class'] = 'collection';
562         $sortbyname = $this->helper_sortable_heading(get_string('name'),
563                 'name', $badges->sort, $badges->dir);
564         $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
565                 'status', $badges->sort, $badges->dir);
566         $table->head = array(
567                 $sortbyname,
568                 $sortbystatus,
569                 get_string('bcriteria', 'badges'),
570                 get_string('awards', 'badges'),
571                 get_string('actions')
572             );
573         $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
575         foreach ($badges->badges as $b) {
576             $style = !$b->is_active() ? array('class' => 'dimmed') : array();
577             $forlink =  print_badge_image($b, $this->page->context) . ' ' .
578                         html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
579             $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
580             $status = $b->statstring;
581             $criteria = self::print_badge_criteria($b, 'short');
583             if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
584                 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
585             } else {
586                 $awards = $b->awards;
587             }
589             $actions = self::print_badge_table_actions($b, $this->page->context);
591             $row = array($name, $status, $criteria, $awards, $actions);
592             $table->data[] = $row;
593         }
594         $htmltable = html_writer::table($table);
596         return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
597     }
599     // Prints tabs for badge editing.
600     public function print_badge_tabs($badgeid, $context, $current = 'overview') {
601         global $DB;
603         $tabs = $row = array();
605         $row[] = new tabobject('overview',
606                     new moodle_url('/badges/overview.php', array('id' => $badgeid)),
607                     get_string('boverview', 'badges')
608                 );
610         if (has_capability('moodle/badges:configuredetails', $context)) {
611             $row[] = new tabobject('details',
612                         new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'details')),
613                         get_string('bdetails', 'badges')
614                     );
615         }
617         if (has_capability('moodle/badges:configurecriteria', $context)) {
618             $row[] = new tabobject('criteria',
619                         new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
620                         get_string('bcriteria', 'badges')
621                     );
622         }
624         if (has_capability('moodle/badges:configuremessages', $context)) {
625             $row[] = new tabobject('message',
626                         new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
627                         get_string('bmessage', 'badges')
628                     );
629         }
631         if (has_capability('moodle/badges:viewawarded', $context)) {
632             $awarded = $DB->count_records('badge_issued', array('badgeid' => $badgeid));
633             $row[] = new tabobject('awards',
634                         new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
635                         get_string('bawards', 'badges', $awarded)
636                     );
637         }
639         $tabs[] = $row;
641         print_tabs($tabs, $current);
642     }
644     // Prints badge status box.
645     public function print_badge_status_box(badge $badge) {
646         $table = new html_table();
647         $table->attributes['class'] = 'boxaligncenter statustable';
649         if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
650             if (!$badge->has_criteria()) {
651                 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
652                 $status = get_string('nocriteria', 'badges');
653                 if ($this->page->url != $criteriaurl) {
654                     $action = $this->output->single_button(
655                         $criteriaurl,
656                         get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
657                 } else {
658                     $action = '';
659                 }
660                 $row = array($status, $action);
661             } else {
662                 $status = get_string('statusmessage_' . $badge->status, 'badges');
663                 if ($badge->is_active()) {
664                     $action = $this->output->single_button(new moodle_url('/badges/action.php',
665                                 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
666                                       'return' => $this->page->url->out_as_local_url(false))),
667                             get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
668                 } else {
669                     $action = $this->output->single_button(new moodle_url('/badges/action.php',
670                                 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
671                                       'return' => $this->page->url->out_as_local_url(false))),
672                             get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
673                 }
674                 $row = array($status . $this->output->help_icon('status', 'badges'), $action);
675             }
676         }
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);
682     }
684     // Prints badge criteria.
685     public function print_badge_criteria(badge $badge, $short = '') {
686         $output = "";
687         $agg = $badge->get_aggregation_methods();
688         if (empty($badge->criteria)) {
689             return get_string('nocriteria', 'badges');
690         } else if (count($badge->criteria) == 2) {
691             if (!$short) {
692                 $output .= get_string('criteria_descr', 'badges');
693             }
694         } else {
695             $output .= get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
696                                     strtoupper($agg[$badge->get_aggregation_method()]));
697         }
698         $items = array();
699         unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
700         foreach ($badge->criteria as $type => $c) {
701             if (count($c->params) == 1) {
702                 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') . $c->get_details($short);
703             } else {
704                 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
705                         strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details($short);
706             }
707         }
708         $output .= html_writer::alist($items, array(), 'ul');
709         return $output;
710     }
712     // Prints criteria actions for badge editing.
713     public function print_criteria_actions(badge $badge) {
714         $table = new html_table();
715         $table->attributes = array('class' => 'boxaligncenter', 'id' => 'badgeactions');
716         $table->colclasses = array('activatebadge');
718         $actions = array();
719         if (!$badge->is_active() && !$badge->is_locked()) {
720             $accepted = $badge->get_accepted_criteria();
721             $potential = array_diff($accepted, array_keys($badge->criteria));
723             if (!empty($potential)) {
724                 foreach ($potential as $p) {
725                     if ($p != 0) {
726                         $select[$p] = get_string('criteria_' . $p, 'badges');
727                     }
728                 }
729                 $actions[] = get_string('addbadgecriteria', 'badges');
730                 $actions[] = $this->output->single_select(new moodle_url('/badges/criteria_settings.php',
731                         array('badgeid' => $badge->id, 'add' => true)), 'type', $select);
732             } else {
733                 $actions[] = $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
734             }
735         }
737         $table->data[] = $actions;
738         return html_writer::table($table);
739     }
741     // Renders a table with users who have earned the badge.
742     // Based on stamps collection plugin.
743     protected function render_badge_recipients(badge_recipients $recipients) {
744         $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
745         $htmlpagingbar = $this->render($paging);
746         $table = new html_table();
747         $table->attributes['class'] = 'generaltable generalbox boxaligncenter boxwidthwide';
749         $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
750                 'firstname', $recipients->sort, $recipients->dir);
751         $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
752                 'lastname', $recipients->sort, $recipients->dir);
753         if ($this->helper_fullname_format() == 'lf') {
754             $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
755         } else {
756             $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
757         }
759         $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
760                 'dateissued', $recipients->sort, $recipients->dir);
762         $table->head = array($sortbyname, $sortbydate, '');
764         foreach ($recipients->userids as $holder) {
765             $fullname = fullname($holder);
766             $fullname = html_writer::link(
767                             new moodle_url('/user/profile.php', array('id' => $holder->userid)),
768                             $fullname
769                         );
770             $awarded  = userdate($holder->dateissued);
771             $badgeurl = html_writer::link(
772                             new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
773                             get_string('viewbadge', 'badges')
774                         );
776             $row = array($fullname, $awarded, $badgeurl);
777             $table->data[] = $row;
778         }
780         $htmltable = html_writer::table($table);
782         return $htmlpagingbar . $htmltable . $htmlpagingbar;
783     }
785     ////////////////////////////////////////////////////////////////////////////
786     // Helper methods
787     // Reused from stamps collection plugin
788     ////////////////////////////////////////////////////////////////////////////
790     /**
791      * Renders a text with icons to sort by the given column
792      *
793      * This is intended for table headings.
794      *
795      * @param string $text    The heading text
796      * @param string $sortid  The column id used for sorting
797      * @param string $sortby  Currently sorted by (column id)
798      * @param string $sorthow Currently sorted how (ASC|DESC)
799      *
800      * @return string
801      */
802     protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
803         $out = html_writer::tag('span', $text, array('class' => 'text'));
805         if (!is_null($sortid)) {
806             if ($sortby !== $sortid || $sorthow !== 'ASC') {
807                 $url = new moodle_url($this->page->url);
808                 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
809                 $out .= $this->output->action_icon($url,
810                         new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
811             }
812             if ($sortby !== $sortid || $sorthow !== 'DESC') {
813                 $url = new moodle_url($this->page->url);
814                 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
815                 $out .= $this->output->action_icon($url,
816                         new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
817             }
818         }
819         return $out;
820     }
821     /**
822      * Tries to guess the fullname format set at the site
823      *
824      * @return string fl|lf
825      */
826     protected function helper_fullname_format() {
827         $fake = new stdClass();
828         $fake->lastname = 'LLLL';
829         $fake->firstname = 'FFFF';
830         $fullname = get_string('fullnamedisplay', '', $fake);
831         if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
832             return 'lf';
833         } else {
834             return 'fl';
835         }
836     }
837     /**
838      * Renders a search form
839      *
840      * @param string $search Search string
841      * @return string HTML
842      */
843     protected function helper_search_form($search) {
844         global $CFG;
845         require_once($CFG->libdir . '/formslib.php');
847         $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
849         $mform->addElement('hidden', 'sesskey', sesskey());
851         $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
852         $mform->setDefault('search', $search);
853         $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
854         $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
855         $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
857         ob_start();
858         $mform->display();
859         $out = ob_get_clean();
861         return $out;
862     }
865 /**
866  * An issued badges for badge.php page
867  */
868 class issued_badge implements renderable {
869     /** @var issued badge */
870     public $issued;
872     /** @var badge recipient */
873     public $recipient = 0;
875     /** @var badge visibility to others */
876     public $visible = 0;
878     /** @var badge class */
879     public $badgeid = 0;
881     /** @var issued badge unique hash */
882     public $hash = "";
884     /**
885      * Initializes the badge to display
886      *
887      * @param string $hash Issued badge hash
888      */
889     public function __construct($hash) {
890         global $DB;
891         $this->issued = badges_get_issued_badge_info($hash);
892         $this->hash = $hash;
894         $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
895                 FROM {badge_issued}
896                 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
897                 array('hash' => $hash), IGNORE_MISSING);
898         if ($rec) {
899             $this->recipient = $rec->userid;
900             $this->visible = $rec->visible;
901             $this->badgeid = $rec->badgeid;
902         }
903     }
906 /**
907  * An external badges for external.php page
908  */
909 class external_badge implements renderable {
910     /** @var issued badge */
911     public $issued;
913     /**
914      * Initializes the badge to display
915      *
916      * @param string $json External badge information.
917      */
918     public function __construct($json) {
919         $this->issued = $json;
920     }
923 /**
924  * Badge recipients rendering class
925  */
926 class badge_recipients implements renderable {
927     /** @var string how are the data sorted */
928     public $sort = 'lastname';
930     /** @var string how are the data sorted */
931     public $dir = 'ASC';
933     /** @var int page number to display */
934     public $page = 0;
936     /** @var int number of badge recipients to display per page */
937     public $perpage = 30;
939     /** @var int the total number or badge recipients to display */
940     public $totalcount = null;
942     /** @var array internal list of  badge recipients ids */
943     public $userids = array();
944     /**
945      * Initializes the list of users to display
946      *
947      * @param array $holders List of badge holders
948      */
949     public function __construct($holders) {
950         $this->userids = $holders;
951     }
954 /**
955  * Collection of all badges for view.php page
956  */
957 class badge_collection implements renderable {
959     /** @var string how are the data sorted */
960     public $sort = 'name';
962     /** @var string how are the data sorted */
963     public $dir = 'ASC';
965     /** @var int page number to display */
966     public $page = 0;
968     /** @var int number of badges to display per page */
969     public $perpage = BADGE_PERPAGE;
971     /** @var int the total number of badges to display */
972     public $totalcount = null;
974     /** @var array list of badges */
975     public $badges = array();
977     /**
978      * Initializes the list of badges to display
979      *
980      * @param array $badges Badges to render
981      */
982     public function __construct($badges) {
983         $this->badges = $badges;
984     }
987 /**
988  * Collection of badges used at the index.php page
989  */
990 class badge_management extends badge_collection implements renderable {
993 /**
994  * Collection of user badges used at the mybadges.php page
995  */
996 class badge_user_collection extends badge_collection implements renderable {
997     /** @var array backpack settings */
998     public $backpack;
1000     /** @var string search */
1001     public $search = '';
1003     /**
1004      * Initializes user badge collection.
1005      *
1006      * @param array $badges Badges to render
1007      * @param int $userid Badges owner
1008      */
1009     public function __construct($badges, $userid) {
1010         parent::__construct($badges);
1011         $this->backpack = get_backpack_settings($userid);
1012     }