MDL-40712 badges: Display recipient information
[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');
30 /**
31  * Standard HTML output renderer for badges
32  */
33 class core_badges_renderer extends plugin_renderer_base {
35     // Outputs badges list.
36     public function print_badges_list($badges, $userid, $profile = false, $external = false) {
37         global $USER, $CFG;
38         foreach ($badges as $badge) {
39             if (!$external) {
40                 $context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid);
41                 $bname = $badge->name;
42                 $imageurl = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', 'f1', false);
43             } else {
44                 $bname = $badge->assertion->badge->name;
45                 $imageurl = $badge->imageUrl;
46             }
48             $name = html_writer::tag('span', $bname, array('class' => 'badge-name'));
50             $image = html_writer::empty_tag('img', array('src' => $imageurl, 'class' => 'badge-image'));
51             if (!empty($badge->dateexpire) && $badge->dateexpire < time()) {
52                 $image .= $this->output->pix_icon('i/expired',
53                         get_string('expireddate', 'badges', userdate($badge->dateexpire)),
54                         'moodle',
55                         array('class' => 'expireimage'));
56                 $name .= '(' . get_string('expired', 'badges') . ')';
57             }
59             $download = $status = $push = '';
60             if (($userid == $USER->id) && !$profile) {
61                 $url = new moodle_url('mybadges.php', array('download' => $badge->id, 'hash' => $badge->uniquehash, 'sesskey' => sesskey()));
62                 $notexpiredbadge = (empty($badge->dateexpire) || $badge->dateexpire > time());
63                 $backpackexists = badges_user_has_backpack($USER->id);
64                 if (!empty($CFG->badges_allowexternalbackpack) && $notexpiredbadge && $backpackexists) {
65                     $assertion = new moodle_url('/badges/assertion.php', array('b' => $badge->uniquehash));
66                     $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
67                     $push = $this->output->action_icon(new moodle_url('#'), new pix_icon('t/backpack', get_string('addtobackpack', 'badges')), $action);
68                 }
70                 $download = $this->output->action_icon($url, new pix_icon('t/download', get_string('download')));
71                 if ($badge->visible) {
72                     $url = new moodle_url('mybadges.php', array('hide' => $badge->issuedid, 'sesskey' => sesskey()));
73                     $status = $this->output->action_icon($url, new pix_icon('t/hide', get_string('makeprivate', 'badges')));
74                 } else {
75                     $url = new moodle_url('mybadges.php', array('show' => $badge->issuedid, 'sesskey' => sesskey()));
76                     $status = $this->output->action_icon($url, new pix_icon('t/show', get_string('makepublic', 'badges')));
77                 }
78             }
80             if (!$profile) {
81                 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash));
82             } else {
83                 if (!$external) {
84                     $url = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
85                 } else {
86                     $hash = hash('md5', $badge->hostedUrl);
87                     $url = new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid));
88                 }
89             }
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));
92         }
94         return html_writer::alist($items, array('class' => 'badges'));
95     }
97     // Recipients selection form.
98     public function recipients_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
99         $output = '';
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(
113                     'type' => 'submit',
114                     'name' => 'award',
115                     'value' => $this->output->larrow() . ' ' . get_string('award', 'badges'),
116                     'class' => 'actionbutton')
117                 );
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');
130         return $output;
131     }
133     // Prints a badge overview infomation.
134     public function print_badge_overview($badge, $context) {
135         $display = "";
137         // Badge details.
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');
151         // Issuer details.
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));
176                 } else {
177                     $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2));
178                 }
179             }
180         } else {
181             $display .= get_string('noexpiry', 'badges');
182         }
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);
190         } else {
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'));
196             }
197         }
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));
206                 $a = new stdClass();
207                 $a->link = $url->out();
208                 $a->count = count($badge->get_awards());
209                 $display .= get_string('numawards', 'badges', $a);
210             } else {
211                 $display .= get_string('noawards', 'badges');
212             }
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'));
220             }
221             $display .= html_writer::end_tag('fieldset');
222         }
224         return $display;
225     }
227     // Prints action icons for the badge.
228     public function print_badge_table_actions($badge, $context) {
229         $actions = "";
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'))) . " ";
238             } else {
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'))) . " ";
243             }
244         }
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'))) . " ";
252         }
254         // Edit badge.
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'))) . " ";
258         }
260         // Duplicate badge.
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'))) . " ";
264         }
266         // Delete badge.
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'))) . " ";
271         }
273         return $actions;
274     }
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         $userinfo = $ibadge->recipient;
281         $badge = new badge($ibadge->badgeid);
282         $today_date = date('Y-m-d');
283         $today = strtotime($today_date);
285         $table = new html_table();
286         $table->id = 'issued-badge-table';
288         $imagetable = new html_table();
289         $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
290         $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued['badge']['image'])));
291         if ($USER->id == $userinfo->id && !empty($CFG->enablebadges)) {
292             $imagetable->data[] = array($this->output->single_button(
293                         new moodle_url('/badges/badge.php', array('hash' => $ibadge->hash, 'bake' => true)),
294                         get_string('download'),
295                         'POST'));
296             $expiration = isset($issued['expires']) ? strtotime($issued['expires']) : $today + 1;
297             if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $today) && badges_user_has_backpack($USER->id)) {
298                 $assertion = new moodle_url('/badges/assertion.php', array('b' => $ibadge->hash));
299                 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
300                 $attributes = array(
301                         'type'  => 'button',
302                         'id'    => 'addbutton',
303                         'value' => get_string('addtobackpack', 'badges'));
304                 $tobackpack = html_writer::tag('input', '', $attributes);
305                 $this->output->add_action_handler($action, 'addbutton');
306                 $imagetable->data[] = array($tobackpack);
307             }
308         }
309         $datatable = new html_table();
310         $datatable->attributes = array('class' => 'badgeissuedinfo');
311         $datatable->colclasses = array('bfield', 'bvalue');
313         // Recipient information.
314         $datatable->data[] = array($this->output->heading(get_string('recipientdetails', 'badges'), 3), '');
315         $datatable->data[] = array(get_string('name'), fullname($userinfo));
316         if (empty($userinfo->backpackemail)) {
317             $datatable->data[] = array(get_string('email'), obfuscate_mailto($userinfo->accountemail));
318         } else {
319             $datatable->data[] = array(get_string('email'), obfuscate_mailto($userinfo->backpackemail));
320         }
322         $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
323         $datatable->data[] = array(get_string('issuername', 'badges'), $badge->issuername);
324         if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
325             $datatable->data[] = array(get_string('contact', 'badges'), obfuscate_mailto($badge->issuercontact));
326         }
327         $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
328         $datatable->data[] = array(get_string('name'), $badge->name);
329         $datatable->data[] = array(get_string('description', 'badges'), $badge->description);
331         if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
332             $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid));
333             $datatable->data[] = array(get_string('course'), $coursename);
334         }
336         $datatable->data[] = array(get_string('bcriteria', 'badges'), self::print_badge_criteria($badge));
337         $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
338         $datatable->data[] = array(get_string('dateawarded', 'badges'), $issued['issued_on']);
339         if (isset($issued['expires'])) {
340             $expiration = strtotime($issued['expires']);
341             if ($expiration < $today) {
342                 $cell = new html_table_cell($issued['expires'] . get_string('warnexpired', 'badges'));
343                 $cell->attributes = array('class' => 'notifyproblem warning');
344                 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
346                 $image = html_writer::start_tag('div', array('class' => 'badge'));
347                 $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
348                 $image .= $this->output->pix_icon('i/expired',
349                                 get_string('expireddate', 'badges', $issued['expires']),
350                                 'moodle',
351                                 array('class' => 'expireimage'));
352                 $image .= html_writer::end_tag('div');
353                 $imagetable->data[0] = array($image);
354             } else {
355                 $datatable->data[] = array(get_string('expirydate', 'badges'), $issued['expires']);
356             }
357         }
359         // Print evidence.
360         $agg = $badge->get_aggregation_methods();
361         $evidence = $badge->get_criteria_completions($userinfo->id);
362         $eids = array_map(create_function('$o', 'return $o->critid;'), $evidence);
363         unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
365         $items = array();
366         foreach ($badge->criteria as $type => $c) {
367             if (in_array($c->id, $eids)) {
368                 if (count($c->params) == 1) {
369                     $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
370                 } else {
371                     $items[] = get_string('criteria_descr_' . $type , 'badges',
372                             strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
373                 }
374             }
375         }
377         $datatable->data[] = array(get_string('evidence', 'badges'),
378                 get_string('completioninfo', 'badges') .
379                 html_writer::alist($items, array(), 'ul'));
380         $table->attributes = array('class' => 'generalbox boxaligncenter issuedbadgebox');
381         $table->data[] = array(html_writer::table($imagetable), html_writer::table($datatable));
382         $htmlbadge = html_writer::table($table);
384         return $htmlbadge;
385     }
387     // Outputs external badge.
388     protected function render_external_badge(external_badge $ibadge) {
389         $issued = $ibadge->issued;
390         $assertion = $issued->assertion;
391         $issuer = $assertion->badge->issuer;
392         $userinfo = $ibadge->recipient;
393         $table = new html_table();
395         $imagetable = new html_table();
396         $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
397         $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued->imageUrl, 'width' => '100px')));
399         $datatable = new html_table();
400         $datatable->attributes = array('class' => 'badgeissuedinfo');
401         $datatable->colclasses = array('bfield', 'bvalue');
403         // Recipient information.
404         $datatable->data[] = array($this->output->heading(get_string('recipientdetails', 'badges'), 3), '');
405         // Technically, we should alway have a user at this point, but added an extra check just in case.
406         if ($userinfo) {
407             $datatable->data[] = array(get_string('name'), fullname($userinfo));
408             if (!$ibadge->valid) {
409                 $notify = $this->output->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice');
410                 $datatable->data[] = array(get_string('email'), obfuscate_mailto($userinfo->email) . $notify);
411             } else {
412                 $datatable->data[] = array(get_string('email'), obfuscate_mailto($userinfo->email));
413             }
414         } else {
415             $notify = $this->output->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice');
416             $datatable->data[] = array(get_string('name'), $notify);
417         }
419         $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
420         $datatable->data[] = array(get_string('issuername', 'badges'), $issuer->name);
421         $datatable->data[] = array(get_string('issuerurl', 'badges'),
422                 html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin)));
423         if (isset($issuer->contact)) {
424             $datatable->data[] = array(get_string('contact', 'badges'), obfuscate_mailto($issuer->contact));
425         }
426         $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
427         $datatable->data[] = array(get_string('name'), $assertion->badge->name);
428         $datatable->data[] = array(get_string('description', 'badges'), $assertion->badge->description);
429         $datatable->data[] = array(get_string('bcriteria', 'badges'),
430                 html_writer::tag('a', $assertion->badge->criteria, array('href' => $assertion->badge->criteria)));
431         $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
432         if (isset($assertion->issued_on)) {
433             $datatable->data[] = array(get_string('dateawarded', 'badges'), $assertion->issued_on);
434         }
435         if (isset($assertion->badge->expire)) {
436             $today_date = date('Y-m-d');
437             $today = strtotime($today_date);
438             $expiration = strtotime($assertion->badge->expire);
439             if ($expiration < $today) {
440                 $cell = new html_table_cell($assertion->badge->expire . get_string('warnexpired', 'badges'));
441                 $cell->attributes = array('class' => 'notifyproblem warning');
442                 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
444                 $image = html_writer::start_tag('div', array('class' => 'badge'));
445                 $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
446                 $image .= html_writer::start_tag('span', array('class' => 'expired'))
447                             . $this->output->pix_icon('i/expired',
448                                 get_string('expireddate', 'badges', $assertion->badge->expire),
449                                 'moodle',
450                                 array('class' => 'expireimage'))
451                             . html_writer::end_tag('span');
452                 $image .= html_writer::end_tag('div');
453                 $imagetable->data[0] = array($image);
454             } else {
455                 $datatable->data[] = array(get_string('expirydate', 'badges'), $assertion->badge->expire);
456             }
457         }
458         if (isset($assertion->evidence)) {
459             $datatable->data[] = array(get_string('evidence', 'badges'),
460                 html_writer::tag('a', $assertion->evidence, array('href' => $assertion->evidence)));
461         }
462         $table->attributes = array('class' => 'generalbox boxaligncenter issuedbadgebox');
463         $table->data[] = array(html_writer::table($imagetable), html_writer::table($datatable));
464         $htmlbadge = html_writer::table($table);
466         return $htmlbadge;
467     }
469     // Outputs table of user badges.
470     protected function render_badge_user_collection(badge_user_collection $badges) {
471         global $CFG, $USER, $SITE;
472         $backpack = $badges->backpack;
473         $mybackpack = new moodle_url('/badges/mybackpack.php');
475         $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
476         $htmlpagingbar = $this->render($paging);
478         // Set backpack connection string.
479         $backpackconnect = '';
480         if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) {
481             $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
482         }
483         // Search box.
484         $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
486         // Download all button.
487         $downloadall = $this->output->single_button(
488                     new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
489                     get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
491         // Local badges.
492         $localhtml = html_writer::start_tag('fieldset', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
493         $localhtml .= html_writer::tag('legend',
494                     $this->output->heading_with_help(get_string('localbadges', 'badges', $SITE->fullname), 'localbadgesh', 'badges'));
495         if ($badges->badges) {
496             $table = new html_table();
497             $table->attributes['class'] = 'statustable';
498             $table->data[] = array($this->output->heading(get_string('badgesearned', 'badges', $badges->totalcount), 4, 'activatebadge'), $downloadall);
499             $downloadbutton = html_writer::table($table);
501             $htmllist = $this->print_badges_list($badges->badges, $USER->id);
502             $localhtml .= $backpackconnect . $downloadbutton . $searchform . $htmlpagingbar . $htmllist . $htmlpagingbar;
503         } else {
504             $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
505         }
506         $localhtml .= html_writer::end_tag('fieldset');
508         // External badges.
509         $externalhtml = "";
510         if (!empty($CFG->badges_allowexternalbackpack)) {
511             $externalhtml .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
512             $externalhtml .= html_writer::tag('legend', $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges'));
513             if (!is_null($backpack)) {
514                 if ($backpack->totalcollections == 0) {
515                     $externalhtml .= get_string('nobackpackcollections', 'badges', $backpack);
516                 } else {
517                     if ($backpack->totalbadges == 0) {
518                         $externalhtml .= get_string('nobackpackbadges', 'badges', $backpack);
519                     } else {
520                         $externalhtml .= get_string('backpackbadges', 'badges', $backpack);
521                         $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
522                     }
523                 }
524             } else {
525                 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
526             }
528             $externalhtml .= html_writer::end_tag('fieldset');
529         }
531         return $localhtml . $externalhtml;
532     }
534     // Outputs table of available badges.
535     protected function render_badge_collection(badge_collection $badges) {
536         $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
537         $htmlpagingbar = $this->render($paging);
538         $table = new html_table();
539         $table->attributes['class'] = 'collection';
541         $sortbyname = $this->helper_sortable_heading(get_string('name'),
542                 'name', $badges->sort, $badges->dir);
543         $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
544                 'dateissued', $badges->sort, $badges->dir);
545         $table->head = array(
546                     get_string('badgeimage', 'badges'),
547                     $sortbyname,
548                     get_string('description', 'badges'),
549                     get_string('bcriteria', 'badges'),
550                     $sortbyawarded
551                 );
552         $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
554         foreach ($badges->badges as $badge) {
555             $badgeimage = print_badge_image($badge, $this->page->context, 'large');
556             $name = $badge->name;
557             $description = $badge->description;
558             $criteria = self::print_badge_criteria($badge);
559             if ($badge->dateissued) {
560                 $icon = new pix_icon('i/valid',
561                             get_string('dateearned', 'badges',
562                                 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
563                 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
564                 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
565             } else {
566                 $awarded = "";
567             }
568             $row = array($badgeimage, $name, $description, $criteria, $awarded);
569             $table->data[] = $row;
570         }
572         $htmltable = html_writer::table($table);
574         return $htmlpagingbar . $htmltable . $htmlpagingbar;
575     }
577     // Outputs table of badges with actions available.
578     protected function render_badge_management(badge_management $badges) {
579         $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
581         // New badge button.
582         $htmlnew = '';
583         if (has_capability('moodle/badges:createbadge', $this->page->context)) {
584             $n['type'] = $this->page->url->get_param('type');
585             $n['id'] = $this->page->url->get_param('id');
586             $htmlnew = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
587         }
589         $htmlpagingbar = $this->render($paging);
590         $table = new html_table();
591         $table->attributes['class'] = 'collection';
593         $sortbyname = $this->helper_sortable_heading(get_string('name'),
594                 'name', $badges->sort, $badges->dir);
595         $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
596                 'status', $badges->sort, $badges->dir);
597         $table->head = array(
598                 $sortbyname,
599                 $sortbystatus,
600                 get_string('bcriteria', 'badges'),
601                 get_string('awards', 'badges'),
602                 get_string('actions')
603             );
604         $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
606         foreach ($badges->badges as $b) {
607             $style = !$b->is_active() ? array('class' => 'dimmed') : array();
608             $forlink =  print_badge_image($b, $this->page->context) . ' ' .
609                         html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
610             $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
611             $status = $b->statstring;
612             $criteria = self::print_badge_criteria($b, 'short');
614             if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
615                 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
616             } else {
617                 $awards = $b->awards;
618             }
620             $actions = self::print_badge_table_actions($b, $this->page->context);
622             $row = array($name, $status, $criteria, $awards, $actions);
623             $table->data[] = $row;
624         }
625         $htmltable = html_writer::table($table);
627         return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
628     }
630     // Prints tabs for badge editing.
631     public function print_badge_tabs($badgeid, $context, $current = 'overview') {
632         global $DB;
634         $row = array();
636         $row[] = new tabobject('overview',
637                     new moodle_url('/badges/overview.php', array('id' => $badgeid)),
638                     get_string('boverview', 'badges')
639                 );
641         if (has_capability('moodle/badges:configuredetails', $context)) {
642             $row[] = new tabobject('details',
643                         new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'details')),
644                         get_string('bdetails', 'badges')
645                     );
646         }
648         if (has_capability('moodle/badges:configurecriteria', $context)) {
649             $row[] = new tabobject('criteria',
650                         new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
651                         get_string('bcriteria', 'badges')
652                     );
653         }
655         if (has_capability('moodle/badges:configuremessages', $context)) {
656             $row[] = new tabobject('message',
657                         new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
658                         get_string('bmessage', 'badges')
659                     );
660         }
662         if (has_capability('moodle/badges:viewawarded', $context)) {
663             $awarded = $DB->count_records('badge_issued', array('badgeid' => $badgeid));
664             $row[] = new tabobject('awards',
665                         new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
666                         get_string('bawards', 'badges', $awarded)
667                     );
668         }
670         echo $this->tabtree($row, $current);
671     }
673     /**
674      * Prints badge status box.
675      * @return Either the status box html as a string or null
676      */
677     public function print_badge_status_box(badge $badge) {
678         if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
679             $table = new html_table();
680             $table->attributes['class'] = 'boxaligncenter statustable';
682             if (!$badge->has_criteria()) {
683                 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
684                 $status = get_string('nocriteria', 'badges');
685                 if ($this->page->url != $criteriaurl) {
686                     $action = $this->output->single_button(
687                         $criteriaurl,
688                         get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
689                 } else {
690                     $action = '';
691                 }
692                 $row = array($status, $action);
693             } else {
694                 $status = get_string('statusmessage_' . $badge->status, 'badges');
695                 if ($badge->is_active()) {
696                     $action = $this->output->single_button(new moodle_url('/badges/action.php',
697                                 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
698                                       'return' => $this->page->url->out_as_local_url(false))),
699                             get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
700                 } else {
701                     $action = $this->output->single_button(new moodle_url('/badges/action.php',
702                                 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
703                                       'return' => $this->page->url->out_as_local_url(false))),
704                             get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
705                 }
706                 $row = array($status . $this->output->help_icon('status', 'badges'), $action);
707             }
708             $table->data[] = $row;
710             $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
711             return $this->output->box(html_writer::table($table), $style);
712         }
714         return null;
715     }
717     // Prints badge criteria.
718     public function print_badge_criteria(badge $badge, $short = '') {
719         $output = "";
720         $agg = $badge->get_aggregation_methods();
721         if (empty($badge->criteria)) {
722             return get_string('nocriteria', 'badges');
723         } else if (count($badge->criteria) == 2) {
724             if (!$short) {
725                 $output .= get_string('criteria_descr', 'badges');
726             }
727         } else {
728             $output .= get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
729                                     strtoupper($agg[$badge->get_aggregation_method()]));
730         }
731         $items = array();
732         unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
733         foreach ($badge->criteria as $type => $c) {
734             if (count($c->params) == 1) {
735                 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') . $c->get_details($short);
736             } else {
737                 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
738                         strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details($short);
739             }
740         }
741         $output .= html_writer::alist($items, array(), 'ul');
742         return $output;
743     }
745     // Prints criteria actions for badge editing.
746     public function print_criteria_actions(badge $badge) {
747         $table = new html_table();
748         $table->attributes = array('class' => 'boxaligncenter', 'id' => 'badgeactions');
749         $table->colclasses = array('activatebadge');
751         $actions = array();
752         if (!$badge->is_active() && !$badge->is_locked()) {
753             $accepted = $badge->get_accepted_criteria();
754             $potential = array_diff($accepted, array_keys($badge->criteria));
756             if (!empty($potential)) {
757                 foreach ($potential as $p) {
758                     if ($p != 0) {
759                         $select[$p] = get_string('criteria_' . $p, 'badges');
760                     }
761                 }
762                 $actions[] = get_string('addbadgecriteria', 'badges');
763                 $actions[] = $this->output->single_select(new moodle_url('/badges/criteria_settings.php',
764                         array('badgeid' => $badge->id, 'add' => true)), 'type', $select);
765             } else {
766                 $actions[] = $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
767             }
768         }
770         $table->data[] = $actions;
771         return html_writer::table($table);
772     }
774     // Renders a table with users who have earned the badge.
775     // Based on stamps collection plugin.
776     protected function render_badge_recipients(badge_recipients $recipients) {
777         $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
778         $htmlpagingbar = $this->render($paging);
779         $table = new html_table();
780         $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
782         $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
783                 'firstname', $recipients->sort, $recipients->dir);
784         $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
785                 'lastname', $recipients->sort, $recipients->dir);
786         if ($this->helper_fullname_format() == 'lf') {
787             $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
788         } else {
789             $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
790         }
792         $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
793                 'dateissued', $recipients->sort, $recipients->dir);
795         $table->head = array($sortbyname, $sortbydate, '');
797         foreach ($recipients->userids as $holder) {
798             $fullname = fullname($holder);
799             $fullname = html_writer::link(
800                             new moodle_url('/user/profile.php', array('id' => $holder->userid)),
801                             $fullname
802                         );
803             $awarded  = userdate($holder->dateissued);
804             $badgeurl = html_writer::link(
805                             new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
806                             get_string('viewbadge', 'badges')
807                         );
809             $row = array($fullname, $awarded, $badgeurl);
810             $table->data[] = $row;
811         }
813         $htmltable = html_writer::table($table);
815         return $htmlpagingbar . $htmltable . $htmlpagingbar;
816     }
818     ////////////////////////////////////////////////////////////////////////////
819     // Helper methods
820     // Reused from stamps collection plugin
821     ////////////////////////////////////////////////////////////////////////////
823     /**
824      * Renders a text with icons to sort by the given column
825      *
826      * This is intended for table headings.
827      *
828      * @param string $text    The heading text
829      * @param string $sortid  The column id used for sorting
830      * @param string $sortby  Currently sorted by (column id)
831      * @param string $sorthow Currently sorted how (ASC|DESC)
832      *
833      * @return string
834      */
835     protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
836         $out = html_writer::tag('span', $text, array('class' => 'text'));
838         if (!is_null($sortid)) {
839             if ($sortby !== $sortid || $sorthow !== 'ASC') {
840                 $url = new moodle_url($this->page->url);
841                 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
842                 $out .= $this->output->action_icon($url,
843                         new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
844             }
845             if ($sortby !== $sortid || $sorthow !== 'DESC') {
846                 $url = new moodle_url($this->page->url);
847                 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
848                 $out .= $this->output->action_icon($url,
849                         new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
850             }
851         }
852         return $out;
853     }
854     /**
855      * Tries to guess the fullname format set at the site
856      *
857      * @return string fl|lf
858      */
859     protected function helper_fullname_format() {
860         $fake = new stdClass();
861         $fake->lastname = 'LLLL';
862         $fake->firstname = 'FFFF';
863         $fullname = get_string('fullnamedisplay', '', $fake);
864         if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
865             return 'lf';
866         } else {
867             return 'fl';
868         }
869     }
870     /**
871      * Renders a search form
872      *
873      * @param string $search Search string
874      * @return string HTML
875      */
876     protected function helper_search_form($search) {
877         global $CFG;
878         require_once($CFG->libdir . '/formslib.php');
880         $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
882         $mform->addElement('hidden', 'sesskey', sesskey());
884         $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
885         $mform->setDefault('search', $search);
886         $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
887         $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
888         $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
890         ob_start();
891         $mform->display();
892         $out = ob_get_clean();
894         return $out;
895     }
898 /**
899  * An issued badges for badge.php page
900  */
901 class issued_badge implements renderable {
902     /** @var issued badge */
903     public $issued;
905     /** @var badge recipient */
906     public $recipient;
908     /** @var badge visibility to others */
909     public $visible = 0;
911     /** @var badge class */
912     public $badgeid = 0;
914     /** @var issued badge unique hash */
915     public $hash = "";
917     /**
918      * Initializes the badge to display
919      *
920      * @param string $hash Issued badge hash
921      */
922     public function __construct($hash) {
923         global $DB;
924         $this->issued = badges_get_issued_badge_info($hash);
925         $this->hash = $hash;
927         $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
928                 FROM {badge_issued}
929                 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
930                 array('hash' => $hash), IGNORE_MISSING);
931         if ($rec) {
932             // Get a recipient from database.
933             $user = $DB->get_record_sql('SELECT u.id, u.lastname, u.firstname,
934                                                 u.email AS accountemail, b.email AS backpackemail
935                         FROM {user} u LEFT JOIN {badge_backpack} b ON u.id = b.userid
936                         WHERE u.id = :userid', array('userid' => $rec->userid));
937             $this->recipient = $user;
938             $this->visible = $rec->visible;
939             $this->badgeid = $rec->badgeid;
940         }
941     }
944 /**
945  * An external badges for external.php page
946  */
947 class external_badge implements renderable {
948     /** @var issued badge */
949     public $issued;
951     /** @var User ID */
952     public $recipient;
954     /** @var validation of external badge */
955     public $valid = true;
957     /**
958      * Initializes the badge to display
959      *
960      * @param object $badge External badge information.
961      * @param int $recipient User id.
962      */
963     public function __construct($badge, $recipient) {
964         global $DB;
965         // At this point a user has connected a backpack. So, we are going to get
966         // their backpack email rather than their account email.
967         $user = $DB->get_record_sql('SELECT u.lastname, u.firstname, b.email
968                     FROM {user} u INNER JOIN {badge_backpack} b ON u.id = b.userid
969                     WHERE userid = :userid', array('userid' => $recipient), IGNORE_MISSING);
971         $this->issued = $badge;
972         $this->recipient = $user;
974         // Check if recipient is valid.
975         // There is no way to be 100% sure that a badge belongs to a user.
976         // Backpack does not return any recipient information.
977         // All we can do is compare that backpack email hashed using salt
978         // provided in the assertion matches a badge recipient from the assertion.
979         if ($user) {
980             if (validate_email($badge->assertion->recipient) && $badge->assertion->recipient == $user->email) {
981                 // If we have email, compare emails.
982                 $this->valid = true;
983             } else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email)) {
984                 // If recipient is hashed, but no salt, compare hashes without salt.
985                 $this->valid = true;
986             } else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email . $badge->assertion->salt)) {
987                 // If recipient is hashed, compare hashes.
988                 $this->valid = true;
989             } else {
990                 // Otherwise, we cannot be sure that this user is a recipient.
991                 $this->valid = false;
992             }
993         } else {
994             $this->valid = false;
995         }
996     }
999 /**
1000  * Badge recipients rendering class
1001  */
1002 class badge_recipients implements renderable {
1003     /** @var string how are the data sorted */
1004     public $sort = 'lastname';
1006     /** @var string how are the data sorted */
1007     public $dir = 'ASC';
1009     /** @var int page number to display */
1010     public $page = 0;
1012     /** @var int number of badge recipients to display per page */
1013     public $perpage = 30;
1015     /** @var int the total number or badge recipients to display */
1016     public $totalcount = null;
1018     /** @var array internal list of  badge recipients ids */
1019     public $userids = array();
1020     /**
1021      * Initializes the list of users to display
1022      *
1023      * @param array $holders List of badge holders
1024      */
1025     public function __construct($holders) {
1026         $this->userids = $holders;
1027     }
1030 /**
1031  * Collection of all badges for view.php page
1032  */
1033 class badge_collection implements renderable {
1035     /** @var string how are the data sorted */
1036     public $sort = 'name';
1038     /** @var string how are the data sorted */
1039     public $dir = 'ASC';
1041     /** @var int page number to display */
1042     public $page = 0;
1044     /** @var int number of badges to display per page */
1045     public $perpage = BADGE_PERPAGE;
1047     /** @var int the total number of badges to display */
1048     public $totalcount = null;
1050     /** @var array list of badges */
1051     public $badges = array();
1053     /**
1054      * Initializes the list of badges to display
1055      *
1056      * @param array $badges Badges to render
1057      */
1058     public function __construct($badges) {
1059         $this->badges = $badges;
1060     }
1063 /**
1064  * Collection of badges used at the index.php page
1065  */
1066 class badge_management extends badge_collection implements renderable {
1069 /**
1070  * Collection of user badges used at the mybadges.php page
1071  */
1072 class badge_user_collection extends badge_collection implements renderable {
1073     /** @var array backpack settings */
1074     public $backpack = null;
1076     /** @var string search */
1077     public $search = '';
1079     /**
1080      * Initializes user badge collection.
1081      *
1082      * @param array $badges Badges to render
1083      * @param int $userid Badges owner
1084      */
1085     public function __construct($badges, $userid) {
1086         global $CFG;
1087         parent::__construct($badges);
1089         if (!empty($CFG->badges_allowexternalbackpack)) {
1090             $this->backpack = get_backpack_settings($userid, true);
1091         }
1092     }