MDL-41214 notification: now looks at menubar zIndex as well
[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($CFG->wwwroot . '/badges/badge.php', array('hash' => $badge->uniquehash));
85                 } else {
86                     $url = new moodle_url($CFG->wwwroot . '/badges/external.php', array('badge' => serialize($badge)));
87                 }
88             }
89             $actions = html_writer::tag('div', $push . $download . $status, array('class' => 'badge-actions'));
90             $items[] = html_writer::link($url, $image . $actions . $name, array('title' => $bname));
91         }
93         return html_writer::alist($items, array('class' => 'badges'));
94     }
96     // Recipients selection form.
97     public function recipients_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
98         $output = '';
99         $formattributes = array();
100         $formattributes['id'] = 'recipientform';
101         $formattributes['action'] = $this->page->url;
102         $formattributes['method'] = 'post';
103         $output .= html_writer::start_tag('form', $formattributes);
104         $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
106         $existingcell = new html_table_cell();
107         $existingcell->text = $existinguc->display(true);
108         $existingcell->attributes['class'] = 'existing';
109         $actioncell = new html_table_cell();
110         $actioncell->text  = html_writer::start_tag('div', array());
111         $actioncell->text .= html_writer::empty_tag('input', array(
112                     'type' => 'submit',
113                     'name' => 'award',
114                     'value' => $this->output->larrow() . ' ' . get_string('award', 'badges'),
115                     'class' => 'actionbutton')
116                 );
117         $actioncell->text .= html_writer::end_tag('div', array());
118         $actioncell->attributes['class'] = 'actions';
119         $potentialcell = new html_table_cell();
120         $potentialcell->text = $potentialuc->display(true);
121         $potentialcell->attributes['class'] = 'potential';
123         $table = new html_table();
124         $table->attributes['class'] = 'recipienttable boxaligncenter';
125         $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
126         $output .= html_writer::table($table);
128         $output .= html_writer::end_tag('form');
129         return $output;
130     }
132     // Prints a badge overview infomation.
133     public function print_badge_overview($badge, $context) {
134         $display = "";
136         // Badge details.
137         $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
138         $display .= html_writer::tag('legend', get_string('badgedetails', 'badges'), array('class' => 'bold'));
140         $detailstable = new html_table();
141         $detailstable->attributes = array('class' => 'clearfix', 'id' => 'badgedetails');
142         $detailstable->data[] = array(get_string('name') . ":", $badge->name);
143         $detailstable->data[] = array(get_string('description', 'badges') . ":", $badge->description);
144         $detailstable->data[] = array(get_string('createdon', 'search') . ":", userdate($badge->timecreated));
145         $detailstable->data[] = array(get_string('badgeimage', 'badges') . ":",
146                 print_badge_image($badge, $context, 'large'));
147         $display .= html_writer::table($detailstable);
148         $display .= html_writer::end_tag('fieldset');
150         // Issuer details.
151         $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
152         $display .= html_writer::tag('legend', get_string('issuerdetails', 'badges'), array('class' => 'bold'));
154         $issuertable = new html_table();
155         $issuertable->attributes = array('class' => 'clearfix', 'id' => 'badgeissuer');
156         $issuertable->data[] = array(get_string('issuername', 'badges') . ":", $badge->issuername);
157         $issuertable->data[] = array(get_string('contact', 'badges') . ":",
158                 html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact)));
159         $display .= html_writer::table($issuertable);
160         $display .= html_writer::end_tag('fieldset');
162         // Issuance details if any.
163         $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
164         $display .= html_writer::tag('legend', get_string('issuancedetails', 'badges'), array('class' => 'bold'));
165         if ($badge->can_expire()) {
166             if ($badge->expiredate) {
167                 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate));
168             } else if ($badge->expireperiod) {
169                 if ($badge->expireperiod < 60) {
170                     $display .= get_string('expireperiods', 'badges', round($badge->expireperiod, 2));
171                 } else if ($badge->expireperiod < 60 * 60) {
172                     $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod / 60, 2));
173                 } else if ($badge->expireperiod < 60 * 60 * 24) {
174                     $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod / 60 / 60, 2));
175                 } else {
176                     $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2));
177                 }
178             }
179         } else {
180             $display .= get_string('noexpiry', 'badges');
181         }
182         $display .= html_writer::end_tag('fieldset');
184         // Criteria details if any.
185         $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
186         $display .= html_writer::tag('legend', get_string('bcriteria', 'badges'), array('class' => 'bold'));
187         if ($badge->has_criteria()) {
188             $display .= self::print_badge_criteria($badge);
189         } else {
190             $display .= get_string('nocriteria', 'badges');
191             if (has_capability('moodle/badges:configurecriteria', $context)) {
192                 $display .= $this->output->single_button(
193                     new moodle_url('/badges/criteria.php', array('id' => $badge->id)),
194                     get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
195             }
196         }
197         $display .= html_writer::end_tag('fieldset');
199         // Awards details if any.
200         if (has_capability('moodle/badges:viewawarded', $context)) {
201             $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
202             $display .= html_writer::tag('legend', get_string('awards', 'badges'), array('class' => 'bold'));
203             if ($badge->has_awards()) {
204                 $url = new moodle_url('/badges/recipients.php', array('id' => $badge->id));
205                 $a = new stdClass();
206                 $a->link = $url->out();
207                 $a->count = count($badge->get_awards());
208                 $display .= get_string('numawards', 'badges', $a);
209             } else {
210                 $display .= get_string('noawards', 'badges');
211             }
213             if (has_capability('moodle/badges:awardbadge', $context) &&
214                 $badge->has_manual_award_criteria() &&
215                 $badge->is_active()) {
216                 $display .= $this->output->single_button(
217                         new moodle_url('/badges/award.php', array('id' => $badge->id)),
218                         get_string('award', 'badges'), 'POST', array('class' => 'activatebadge'));
219             }
220             $display .= html_writer::end_tag('fieldset');
221         }
223         return $display;
224     }
226     // Prints action icons for the badge.
227     public function print_badge_table_actions($badge, $context) {
228         $actions = "";
230         if (has_capability('moodle/badges:configuredetails', $context)) {
231             // Activate/deactivate badge.
232             if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) {
233                 $url = new moodle_url(qualified_me());
234                 $url->param('activate', $badge->id);
235                 $url->param('sesskey', sesskey());
236                 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
237             } else {
238                 $url = new moodle_url(qualified_me());
239                 $url->param('lock', $badge->id);
240                 $url->param('sesskey', sesskey());
241                 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
242             }
243         }
245         // Award badge manually.
246         if ($badge->has_manual_award_criteria() &&
247                 has_capability('moodle/badges:awardbadge', $context) &&
248                 $badge->is_active()) {
249             $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
250             $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
251         }
253         // Edit badge.
254         if (has_capability('moodle/badges:configuredetails', $context)) {
255             $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'details'));
256             $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
257         }
259         // Duplicate badge.
260         if (has_capability('moodle/badges:createbadge', $context)) {
261             $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey()));
262             $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
263         }
265         // Delete badge.
266         if (has_capability('moodle/badges:deletebadge', $context)) {
267             $url = new moodle_url(qualified_me());
268             $url->param('delete', $badge->id);
269             $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
270         }
272         return $actions;
273     }
275     // Outputs issued badge with actions available.
276     protected function render_issued_badge(issued_badge $ibadge) {
277         global $USER, $CFG, $DB;
278         $issued = $ibadge->issued;
279         $badge = new badge($ibadge->badgeid);
280         $today_date = date('Y-m-d');
281         $today = strtotime($today_date);
283         $table = new html_table();
284         $table->id = 'issued-badge-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 (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $today) && badges_user_has_backpack($USER->id)) {
296                 $assertion = new moodle_url('/badges/assertion.php', array('b' => $ibadge->hash));
297                 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
298                 $attributes = array(
299                         'type'  => 'button',
300                         'id'    => 'addbutton',
301                         'value' => get_string('addtobackpack', 'badges'));
302                 $tobackpack = html_writer::tag('input', '', $attributes);
303                 $this->output->add_action_handler($action, 'addbutton');
304                 $imagetable->data[] = array($tobackpack);
305             }
306         }
307         $datatable = new html_table();
308         $datatable->attributes = array('class' => 'badgeissuedinfo');
309         $datatable->colclasses = array('bfield', 'bvalue');
310         $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
311         $datatable->data[] = array(get_string('issuername', 'badges'), $badge->issuername);
312         if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
313             $datatable->data[] = array(get_string('contact', 'badges'),
314                 html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact)));
315         }
316         $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
317         $datatable->data[] = array(get_string('name'), $badge->name);
318         $datatable->data[] = array(get_string('description', 'badges'), $badge->description);
320         if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
321             $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid));
322             $datatable->data[] = array(get_string('course'), $coursename);
323         }
325         $datatable->data[] = array(get_string('bcriteria', 'badges'), self::print_badge_criteria($badge));
326         $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
327         $datatable->data[] = array(get_string('dateawarded', 'badges'), $issued['issued_on']);
328         if (isset($issued['expires'])) {
329             $expiration = strtotime($issued['expires']);
330             if ($expiration < $today) {
331                 $cell = new html_table_cell($issued['expires'] . get_string('warnexpired', 'badges'));
332                 $cell->attributes = array('class' => 'notifyproblem warning');
333                 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
335                 $image = html_writer::start_tag('div', array('class' => 'badge'));
336                 $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
337                 $image .= $this->output->pix_icon('i/expired',
338                                 get_string('expireddate', 'badges', $issued['expires']),
339                                 'moodle',
340                                 array('class' => 'expireimage'));
341                 $image .= html_writer::end_tag('div');
342                 $imagetable->data[0] = array($image);
343             } else {
344                 $datatable->data[] = array(get_string('expirydate', 'badges'), $issued['expires']);
345             }
346         }
348         // Print evidence.
349         $agg = $badge->get_aggregation_methods();
350         $evidence = $badge->get_criteria_completions($ibadge->recipient);
351         $eids = array_map(create_function('$o', 'return $o->critid;'), $evidence);
352         unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
354         $items = array();
355         foreach ($badge->criteria as $type => $c) {
356             if (in_array($c->id, $eids)) {
357                 if (count($c->params) == 1) {
358                     $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
359                 } else {
360                     $items[] = get_string('criteria_descr_' . $type , 'badges',
361                             strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
362                 }
363             }
364         }
366         $datatable->data[] = array(get_string('evidence', 'badges'),
367                 get_string('completioninfo', 'badges') .
368                 html_writer::alist($items, array(), 'ul'));
369         $table->attributes = array('class' => 'generalbox boxaligncenter issuedbadgebox');
370         $table->data[] = array(html_writer::table($imagetable), html_writer::table($datatable));
371         $htmlbadge = html_writer::table($table);
373         return $htmlbadge;
374     }
376     // Outputs external badge.
377     protected function render_external_badge(external_badge $ibadge) {
378         $issued = $ibadge->issued;
379         $assertion = $issued->assertion;
380         $issuer = $assertion->badge->issuer;
381         $table = new html_table();
383         $imagetable = new html_table();
384         $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
385         $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued->imageUrl, 'width' => '100px')));
387         $datatable = new html_table();
388         $datatable->attributes = array('class' => 'badgeissuedinfo');
389         $datatable->colclasses = array('bfield', 'bvalue');
390         $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
391         $datatable->data[] = array(get_string('issuername', 'badges'), $issuer->name);
392         $datatable->data[] = array(get_string('issuerurl', 'badges'),
393                 html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin)));
394         if (isset($issuer->contact)) {
395             $datatable->data[] = array(get_string('contact', 'badges'),
396                 html_writer::tag('a', $issuer->contact, array('href' => 'mailto:' . $issuer->contact)));
397         }
398         $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
399         $datatable->data[] = array(get_string('name'), $assertion->badge->name);
400         $datatable->data[] = array(get_string('description', 'badges'), $assertion->badge->description);
401         $datatable->data[] = array(get_string('bcriteria', 'badges'),
402                 html_writer::tag('a', $assertion->badge->criteria, array('href' => $assertion->badge->criteria)));
403         $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
404         if (isset($assertion->issued_on)) {
405             $datatable->data[] = array(get_string('dateawarded', 'badges'), $assertion->issued_on);
406         }
407         if (isset($assertion->badge->expire)) {
408             $today_date = date('Y-m-d');
409             $today = strtotime($today_date);
410             $expiration = strtotime($assertion->badge->expire);
411             if ($expiration < $today) {
412                 $cell = new html_table_cell($assertion->badge->expire . get_string('warnexpired', 'badges'));
413                 $cell->attributes = array('class' => 'notifyproblem warning');
414                 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
416                 $image = html_writer::start_tag('div', array('class' => 'badge'));
417                 $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
418                 $image .= html_writer::start_tag('span', array('class' => 'expired'))
419                             . $this->output->pix_icon('i/expired',
420                                 get_string('expireddate', 'badges', $assertion->badge->expire),
421                                 'moodle',
422                                 array('class' => 'expireimage'))
423                             . html_writer::end_tag('span');
424                 $image .= html_writer::end_tag('div');
425                 $imagetable->data[0] = array($image);
426             } else {
427                 $datatable->data[] = array(get_string('expirydate', 'badges'), $assertion->badge->expire);
428             }
429         }
430         if (isset($assertion->evidence)) {
431             $datatable->data[] = array(get_string('evidence', 'badges'),
432                 html_writer::tag('a', $assertion->evidence, array('href' => $assertion->evidence)));
433         }
434         $table->attributes = array('class' => 'generalbox boxaligncenter issuedbadgebox');
435         $table->data[] = array(html_writer::table($imagetable), html_writer::table($datatable));
436         $htmlbadge = html_writer::table($table);
438         return $htmlbadge;
439     }
441     // Outputs table of user badges.
442     protected function render_badge_user_collection(badge_user_collection $badges) {
443         global $CFG, $USER, $SITE;
444         $backpack = $badges->backpack;
445         $mybackpack = new moodle_url('/badges/mybackpack.php');
447         $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
448         $htmlpagingbar = $this->render($paging);
450         // Set backpack connection string.
451         $backpackconnect = '';
452         if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) {
453             $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
454         }
455         // Search box.
456         $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
458         // Download all button.
459         $downloadall = $this->output->single_button(
460                     new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
461                     get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
463         // Local badges.
464         $localhtml = html_writer::start_tag('fieldset', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
465         $localhtml .= html_writer::tag('legend',
466                     $this->output->heading_with_help(get_string('localbadges', 'badges', $SITE->fullname), 'localbadgesh', 'badges'));
467         if ($badges->badges) {
468             $table = new html_table();
469             $table->attributes['class'] = 'statustable';
470             $table->data[] = array($this->output->heading(get_string('badgesearned', 'badges', $badges->totalcount), 4, 'activatebadge'), $downloadall);
471             $downloadbutton = html_writer::table($table);
473             $htmllist = $this->print_badges_list($badges->badges, $USER->id);
474             $localhtml .= $backpackconnect . $downloadbutton . $searchform . $htmlpagingbar . $htmllist . $htmlpagingbar;
475         } else {
476             $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
477         }
478         $localhtml .= html_writer::end_tag('fieldset');
480         // External badges.
481         $externalhtml = "";
482         if (!empty($CFG->badges_allowexternalbackpack)) {
483             $externalhtml .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
484             $externalhtml .= html_writer::tag('legend', $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges'));
485             if (!is_null($backpack)) {
486                 if ($backpack->totalcollections == 0) {
487                     $externalhtml .= get_string('nobackpackcollections', 'badges', $backpack);
488                 } else {
489                     if ($backpack->totalbadges == 0) {
490                         $externalhtml .= get_string('nobackpackbadges', 'badges', $backpack);
491                     } else {
492                         $externalhtml .= get_string('backpackbadges', 'badges', $backpack);
493                         $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
494                     }
495                 }
496             } else {
497                 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
498             }
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';
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/valid',
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         $htmlnew = '';
555         if (has_capability('moodle/badges:createbadge', $this->page->context)) {
556             $n['type'] = $this->page->url->get_param('type');
557             $n['id'] = $this->page->url->get_param('id');
558             $htmlnew = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
559         }
561         $htmlpagingbar = $this->render($paging);
562         $table = new html_table();
563         $table->attributes['class'] = 'collection';
565         $sortbyname = $this->helper_sortable_heading(get_string('name'),
566                 'name', $badges->sort, $badges->dir);
567         $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
568                 'status', $badges->sort, $badges->dir);
569         $table->head = array(
570                 $sortbyname,
571                 $sortbystatus,
572                 get_string('bcriteria', 'badges'),
573                 get_string('awards', 'badges'),
574                 get_string('actions')
575             );
576         $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
578         foreach ($badges->badges as $b) {
579             $style = !$b->is_active() ? array('class' => 'dimmed') : array();
580             $forlink =  print_badge_image($b, $this->page->context) . ' ' .
581                         html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
582             $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
583             $status = $b->statstring;
584             $criteria = self::print_badge_criteria($b, 'short');
586             if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
587                 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
588             } else {
589                 $awards = $b->awards;
590             }
592             $actions = self::print_badge_table_actions($b, $this->page->context);
594             $row = array($name, $status, $criteria, $awards, $actions);
595             $table->data[] = $row;
596         }
597         $htmltable = html_writer::table($table);
599         return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
600     }
602     // Prints tabs for badge editing.
603     public function print_badge_tabs($badgeid, $context, $current = 'overview') {
604         global $DB;
606         $row = array();
608         $row[] = new tabobject('overview',
609                     new moodle_url('/badges/overview.php', array('id' => $badgeid)),
610                     get_string('boverview', 'badges')
611                 );
613         if (has_capability('moodle/badges:configuredetails', $context)) {
614             $row[] = new tabobject('details',
615                         new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'details')),
616                         get_string('bdetails', 'badges')
617                     );
618         }
620         if (has_capability('moodle/badges:configurecriteria', $context)) {
621             $row[] = new tabobject('criteria',
622                         new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
623                         get_string('bcriteria', 'badges')
624                     );
625         }
627         if (has_capability('moodle/badges:configuremessages', $context)) {
628             $row[] = new tabobject('message',
629                         new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
630                         get_string('bmessage', 'badges')
631                     );
632         }
634         if (has_capability('moodle/badges:viewawarded', $context)) {
635             $awarded = $DB->count_records('badge_issued', array('badgeid' => $badgeid));
636             $row[] = new tabobject('awards',
637                         new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
638                         get_string('bawards', 'badges', $awarded)
639                     );
640         }
642         echo $this->tabtree($row, $current);
643     }
645     /**
646      * Prints badge status box.
647      * @return Either the status box html as a string or null
648      */
649     public function print_badge_status_box(badge $badge) {
650         if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
651             $table = new html_table();
652             $table->attributes['class'] = 'boxaligncenter statustable';
654             if (!$badge->has_criteria()) {
655                 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
656                 $status = get_string('nocriteria', 'badges');
657                 if ($this->page->url != $criteriaurl) {
658                     $action = $this->output->single_button(
659                         $criteriaurl,
660                         get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
661                 } else {
662                     $action = '';
663                 }
664                 $row = array($status, $action);
665             } else {
666                 $status = get_string('statusmessage_' . $badge->status, 'badges');
667                 if ($badge->is_active()) {
668                     $action = $this->output->single_button(new moodle_url('/badges/action.php',
669                                 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
670                                       'return' => $this->page->url->out_as_local_url(false))),
671                             get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
672                 } else {
673                     $action = $this->output->single_button(new moodle_url('/badges/action.php',
674                                 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
675                                       'return' => $this->page->url->out_as_local_url(false))),
676                             get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
677                 }
678                 $row = array($status . $this->output->help_icon('status', 'badges'), $action);
679             }
680             $table->data[] = $row;
682             $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
683             return $this->output->box(html_writer::table($table), $style);
684         }
686         return null;
687     }
689     // Prints badge criteria.
690     public function print_badge_criteria(badge $badge, $short = '') {
691         $output = "";
692         $agg = $badge->get_aggregation_methods();
693         if (empty($badge->criteria)) {
694             return get_string('nocriteria', 'badges');
695         } else if (count($badge->criteria) == 2) {
696             if (!$short) {
697                 $output .= get_string('criteria_descr', 'badges');
698             }
699         } else {
700             $output .= get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
701                                     strtoupper($agg[$badge->get_aggregation_method()]));
702         }
703         $items = array();
704         unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
705         foreach ($badge->criteria as $type => $c) {
706             if (count($c->params) == 1) {
707                 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') . $c->get_details($short);
708             } else {
709                 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
710                         strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details($short);
711             }
712         }
713         $output .= html_writer::alist($items, array(), 'ul');
714         return $output;
715     }
717     // Prints criteria actions for badge editing.
718     public function print_criteria_actions(badge $badge) {
719         $table = new html_table();
720         $table->attributes = array('class' => 'boxaligncenter', 'id' => 'badgeactions');
721         $table->colclasses = array('activatebadge');
723         $actions = array();
724         if (!$badge->is_active() && !$badge->is_locked()) {
725             $accepted = $badge->get_accepted_criteria();
726             $potential = array_diff($accepted, array_keys($badge->criteria));
728             if (!empty($potential)) {
729                 foreach ($potential as $p) {
730                     if ($p != 0) {
731                         $select[$p] = get_string('criteria_' . $p, 'badges');
732                     }
733                 }
734                 $actions[] = get_string('addbadgecriteria', 'badges');
735                 $actions[] = $this->output->single_select(new moodle_url('/badges/criteria_settings.php',
736                         array('badgeid' => $badge->id, 'add' => true)), 'type', $select);
737             } else {
738                 $actions[] = $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
739             }
740         }
742         $table->data[] = $actions;
743         return html_writer::table($table);
744     }
746     // Renders a table with users who have earned the badge.
747     // Based on stamps collection plugin.
748     protected function render_badge_recipients(badge_recipients $recipients) {
749         $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
750         $htmlpagingbar = $this->render($paging);
751         $table = new html_table();
752         $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
754         $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
755                 'firstname', $recipients->sort, $recipients->dir);
756         $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
757                 'lastname', $recipients->sort, $recipients->dir);
758         if ($this->helper_fullname_format() == 'lf') {
759             $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
760         } else {
761             $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
762         }
764         $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
765                 'dateissued', $recipients->sort, $recipients->dir);
767         $table->head = array($sortbyname, $sortbydate, '');
769         foreach ($recipients->userids as $holder) {
770             $fullname = fullname($holder);
771             $fullname = html_writer::link(
772                             new moodle_url('/user/profile.php', array('id' => $holder->userid)),
773                             $fullname
774                         );
775             $awarded  = userdate($holder->dateissued);
776             $badgeurl = html_writer::link(
777                             new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
778                             get_string('viewbadge', 'badges')
779                         );
781             $row = array($fullname, $awarded, $badgeurl);
782             $table->data[] = $row;
783         }
785         $htmltable = html_writer::table($table);
787         return $htmlpagingbar . $htmltable . $htmlpagingbar;
788     }
790     ////////////////////////////////////////////////////////////////////////////
791     // Helper methods
792     // Reused from stamps collection plugin
793     ////////////////////////////////////////////////////////////////////////////
795     /**
796      * Renders a text with icons to sort by the given column
797      *
798      * This is intended for table headings.
799      *
800      * @param string $text    The heading text
801      * @param string $sortid  The column id used for sorting
802      * @param string $sortby  Currently sorted by (column id)
803      * @param string $sorthow Currently sorted how (ASC|DESC)
804      *
805      * @return string
806      */
807     protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
808         $out = html_writer::tag('span', $text, array('class' => 'text'));
810         if (!is_null($sortid)) {
811             if ($sortby !== $sortid || $sorthow !== 'ASC') {
812                 $url = new moodle_url($this->page->url);
813                 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
814                 $out .= $this->output->action_icon($url,
815                         new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
816             }
817             if ($sortby !== $sortid || $sorthow !== 'DESC') {
818                 $url = new moodle_url($this->page->url);
819                 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
820                 $out .= $this->output->action_icon($url,
821                         new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
822             }
823         }
824         return $out;
825     }
826     /**
827      * Tries to guess the fullname format set at the site
828      *
829      * @return string fl|lf
830      */
831     protected function helper_fullname_format() {
832         $fake = new stdClass();
833         $fake->lastname = 'LLLL';
834         $fake->firstname = 'FFFF';
835         $fullname = get_string('fullnamedisplay', '', $fake);
836         if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
837             return 'lf';
838         } else {
839             return 'fl';
840         }
841     }
842     /**
843      * Renders a search form
844      *
845      * @param string $search Search string
846      * @return string HTML
847      */
848     protected function helper_search_form($search) {
849         global $CFG;
850         require_once($CFG->libdir . '/formslib.php');
852         $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
854         $mform->addElement('hidden', 'sesskey', sesskey());
856         $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
857         $mform->setDefault('search', $search);
858         $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
859         $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
860         $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
862         ob_start();
863         $mform->display();
864         $out = ob_get_clean();
866         return $out;
867     }
870 /**
871  * An issued badges for badge.php page
872  */
873 class issued_badge implements renderable {
874     /** @var issued badge */
875     public $issued;
877     /** @var badge recipient */
878     public $recipient = 0;
880     /** @var badge visibility to others */
881     public $visible = 0;
883     /** @var badge class */
884     public $badgeid = 0;
886     /** @var issued badge unique hash */
887     public $hash = "";
889     /**
890      * Initializes the badge to display
891      *
892      * @param string $hash Issued badge hash
893      */
894     public function __construct($hash) {
895         global $DB;
896         $this->issued = badges_get_issued_badge_info($hash);
897         $this->hash = $hash;
899         $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
900                 FROM {badge_issued}
901                 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
902                 array('hash' => $hash), IGNORE_MISSING);
903         if ($rec) {
904             $this->recipient = $rec->userid;
905             $this->visible = $rec->visible;
906             $this->badgeid = $rec->badgeid;
907         }
908     }
911 /**
912  * An external badges for external.php page
913  */
914 class external_badge implements renderable {
915     /** @var issued badge */
916     public $issued;
918     /**
919      * Initializes the badge to display
920      *
921      * @param string $json External badge information.
922      */
923     public function __construct($json) {
924         $this->issued = $json;
925     }
928 /**
929  * Badge recipients rendering class
930  */
931 class badge_recipients implements renderable {
932     /** @var string how are the data sorted */
933     public $sort = 'lastname';
935     /** @var string how are the data sorted */
936     public $dir = 'ASC';
938     /** @var int page number to display */
939     public $page = 0;
941     /** @var int number of badge recipients to display per page */
942     public $perpage = 30;
944     /** @var int the total number or badge recipients to display */
945     public $totalcount = null;
947     /** @var array internal list of  badge recipients ids */
948     public $userids = array();
949     /**
950      * Initializes the list of users to display
951      *
952      * @param array $holders List of badge holders
953      */
954     public function __construct($holders) {
955         $this->userids = $holders;
956     }
959 /**
960  * Collection of all badges for view.php page
961  */
962 class badge_collection implements renderable {
964     /** @var string how are the data sorted */
965     public $sort = 'name';
967     /** @var string how are the data sorted */
968     public $dir = 'ASC';
970     /** @var int page number to display */
971     public $page = 0;
973     /** @var int number of badges to display per page */
974     public $perpage = BADGE_PERPAGE;
976     /** @var int the total number of badges to display */
977     public $totalcount = null;
979     /** @var array list of badges */
980     public $badges = array();
982     /**
983      * Initializes the list of badges to display
984      *
985      * @param array $badges Badges to render
986      */
987     public function __construct($badges) {
988         $this->badges = $badges;
989     }
992 /**
993  * Collection of badges used at the index.php page
994  */
995 class badge_management extends badge_collection implements renderable {
998 /**
999  * Collection of user badges used at the mybadges.php page
1000  */
1001 class badge_user_collection extends badge_collection implements renderable {
1002     /** @var array backpack settings */
1003     public $backpack = null;
1005     /** @var string search */
1006     public $search = '';
1008     /**
1009      * Initializes user badge collection.
1010      *
1011      * @param array $badges Badges to render
1012      * @param int $userid Badges owner
1013      */
1014     public function __construct($badges, $userid) {
1015         global $CFG;
1016         parent::__construct($badges);
1018         if (!empty($CFG->badges_allowexternalbackpack)) {
1019             $this->backpack = get_backpack_settings($userid);
1020         }
1021     }