MDL-64441 badges: Remove notice when language is empty
[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 = s($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 btn btn-secondary')
117                 );
118         $actioncell->text .= html_writer::empty_tag('input', array(
119                     'type' => 'submit',
120                     'name' => 'revoke',
121                     'value' => get_string('revoke', 'badges') . ' ' . $this->output->rarrow(),
122                     'class' => 'actionbutton btn btn-secondary')
123                 );
124         $actioncell->text .= html_writer::end_tag('div', array());
125         $actioncell->attributes['class'] = 'actions';
126         $potentialcell = new html_table_cell();
127         $potentialcell->text = $potentialuc->display(true);
128         $potentialcell->attributes['class'] = 'potential';
130         $table = new html_table();
131         $table->attributes['class'] = 'recipienttable boxaligncenter';
132         $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
133         $output .= html_writer::table($table);
135         $output .= html_writer::end_tag('form');
136         return $output;
137     }
139     // Prints a badge overview infomation.
140     public function print_badge_overview($badge, $context) {
141         $display = "";
142         $languages = get_string_manager()->get_list_of_languages();
144         // Badge details.
145         $display .= $this->heading(get_string('badgedetails', 'badges'), 3);
146         $dl = array();
147         $dl[get_string('name')] = $badge->name;
148         $dl[get_string('version', 'badges')] = $badge->version;
149         $dl[get_string('language')] = $languages[$badge->language];
150         $dl[get_string('description', 'badges')] = $badge->description;
151         $dl[get_string('createdon', 'search')] = userdate($badge->timecreated);
152         $dl[get_string('badgeimage', 'badges')] = print_badge_image($badge, $context, 'large');
153         $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname;
154         $dl[get_string('imageauthoremail', 'badges')] =
155             html_writer::tag('a', $badge->imageauthoremail, array('href' => 'mailto:' . $badge->imageauthoremail));
156         $dl[get_string('imageauthorurl', 'badges')] =
157             html_writer::link($badge->imageauthorurl, $badge->imageauthorurl, array('target' => '_blank'));
158         $dl[get_string('imagecaption', 'badges')] = $badge->imagecaption;
159         $display .= $this->definition_list($dl);
161         // Issuer details.
162         $display .= $this->heading(get_string('issuerdetails', 'badges'), 3);
163         $dl = array();
164         $dl[get_string('issuername', 'badges')] = $badge->issuername;
165         $dl[get_string('contact', 'badges')] = html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact));
166         $display .= $this->definition_list($dl);
168         // Issuance details if any.
169         $display .= $this->heading(get_string('issuancedetails', 'badges'), 3);
170         if ($badge->can_expire()) {
171             if ($badge->expiredate) {
172                 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate));
173             } else if ($badge->expireperiod) {
174                 if ($badge->expireperiod < 60) {
175                     $display .= get_string('expireperiods', 'badges', round($badge->expireperiod, 2));
176                 } else if ($badge->expireperiod < 60 * 60) {
177                     $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod / 60, 2));
178                 } else if ($badge->expireperiod < 60 * 60 * 24) {
179                     $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod / 60 / 60, 2));
180                 } else {
181                     $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2));
182                 }
183             }
184         } else {
185             $display .= get_string('noexpiry', 'badges');
186         }
188         // Criteria details if any.
189         $display .= $this->heading(get_string('bcriteria', 'badges'), 3);
190         if ($badge->has_criteria()) {
191             $display .= self::print_badge_criteria($badge);
192         } else {
193             $display .= get_string('nocriteria', 'badges');
194             if (has_capability('moodle/badges:configurecriteria', $context)) {
195                 $display .= $this->output->single_button(
196                     new moodle_url('/badges/criteria.php', array('id' => $badge->id)),
197                     get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
198             }
199         }
201         // Awards details if any.
202         if (has_capability('moodle/badges:viewawarded', $context)) {
203             $display .= $this->heading(get_string('awards', 'badges'), 3);
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         }
223         $display .= self::print_badge_endorsement($badge);
224         $display .= self::print_badge_related($badge);
225         $display .= self::print_badge_competencies($badge);
227         return html_writer::div($display, null, array('id' => 'badge-overview'));
228     }
230     // Prints action icons for the badge.
231     public function print_badge_table_actions($badge, $context) {
232         $actions = "";
234         if (has_capability('moodle/badges:configuredetails', $context) && $badge->has_criteria()) {
235             // Activate/deactivate badge.
236             if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) {
237                 // "Activate" will go to another page and ask for confirmation.
238                 $url = new moodle_url('/badges/action.php');
239                 $url->param('id', $badge->id);
240                 $url->param('activate', true);
241                 $url->param('sesskey', sesskey());
242                 $return = new moodle_url(qualified_me());
243                 $url->param('return', $return->out_as_local_url(false));
244                 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
245             } else {
246                 $url = new moodle_url(qualified_me());
247                 $url->param('lock', $badge->id);
248                 $url->param('sesskey', sesskey());
249                 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
250             }
251         }
253         // Award badge manually.
254         if ($badge->has_manual_award_criteria() &&
255                 has_capability('moodle/badges:awardbadge', $context) &&
256                 $badge->is_active()) {
257             $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
258             $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
259         }
261         // Edit badge.
262         if (has_capability('moodle/badges:configuredetails', $context)) {
263             $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'details'));
264             $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
265         }
267         // Duplicate badge.
268         if (has_capability('moodle/badges:createbadge', $context)) {
269             $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey()));
270             $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
271         }
273         // Delete badge.
274         if (has_capability('moodle/badges:deletebadge', $context)) {
275             $url = new moodle_url(qualified_me());
276             $url->param('delete', $badge->id);
277             $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
278         }
280         return $actions;
281     }
283     // Outputs issued badge with actions available.
284     protected function render_issued_badge(issued_badge $ibadge) {
285         global $USER, $CFG, $DB, $SITE;
286         $issued = $ibadge->issued;
287         $userinfo = $ibadge->recipient;
288         $badgeclass = $ibadge->badgeclass;
289         $badge = new badge($ibadge->badgeid);
290         $now = time();
291         $expiration = isset($issued['expires']) ? $issued['expires'] : $now + 86400;
292         $badgeimage = is_array($badgeclass['image']) ? $badgeclass['image']['id'] : $badgeclass['image'];
293         $languages = get_string_manager()->get_list_of_languages();
295         $output = '';
296         $output .= html_writer::start_tag('div', array('id' => 'badge'));
297         $output .= html_writer::start_tag('div', array('id' => 'badge-image'));
298         $output .= html_writer::empty_tag('img', array('src' => $badgeimage, 'alt' => $badge->name));
299         if ($expiration < $now) {
300             $output .= $this->output->pix_icon('i/expired',
301             get_string('expireddate', 'badges', userdate($issued['expires'])),
302                 'moodle',
303                 array('class' => 'expireimage'));
304         }
306         if ($USER->id == $userinfo->id && !empty($CFG->enablebadges)) {
307             $output .= $this->output->single_button(
308                         new moodle_url('/badges/badge.php', array('hash' => $issued['uid'], 'bake' => true)),
309                         get_string('download'),
310                         'POST');
311             if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $now) && badges_user_has_backpack($USER->id)) {
312                 $assertion = new moodle_url('/badges/assertion.php', array('b' => $issued['uid']));
313                 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
314                 $attributes = array(
315                         'type'  => 'button',
316                         'id'    => 'addbutton',
317                         'value' => get_string('addtobackpack', 'badges'));
318                 $tobackpack = html_writer::tag('input', '', $attributes);
319                 $this->output->add_action_handler($action, 'addbutton');
320                 $output .= $tobackpack;
321             }
322         }
323         $output .= html_writer::end_tag('div');
325         $output .= html_writer::start_tag('div', array('id' => 'badge-details'));
326         // Recipient information.
327         $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3);
328         $dl = array();
329         if ($userinfo->deleted) {
330             $strdata = new stdClass();
331             $strdata->user = fullname($userinfo);
332             $strdata->site = format_string($SITE->fullname, true, array('context' => context_system::instance()));
334             $dl[get_string('name')] = get_string('error:userdeleted', 'badges', $strdata);
335         } else {
336             $dl[get_string('name')] = fullname($userinfo);
337         }
338         $output .= $this->definition_list($dl);
340         $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3);
341         $dl = array();
342         $dl[get_string('issuername', 'badges')] = $badge->issuername;
343         if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
344             $dl[get_string('contact', 'badges')] = obfuscate_mailto($badge->issuercontact);
345         }
346         $output .= $this->definition_list($dl);
348         $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3);
349         $dl = array();
350         $dl[get_string('name')] = $badge->name;
351         if (!empty($badge->version)) {
352             $dl[get_string('version', 'badges')] = $badge->version;
353         }
354         if (!empty($badge->language)) {
355             $dl[get_string('language')] = $languages[$badge->language];
356         }
357         $dl[get_string('description', 'badges')] = $badge->description;
358         if (!empty($badge->imageauthorname)) {
359             $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname;
360         }
361         if (!empty($badge->imageauthoremail)) {
362             $dl[get_string('imageauthoremail', 'badges')] =
363                     html_writer::tag('a', $badge->imageauthoremail, array('href' => 'mailto:' . $badge->imageauthoremail));
364         }
365         if (!empty($badge->imageauthorurl)) {
366             $dl[get_string('imageauthorurl', 'badges')] =
367                     html_writer::link($badge->imageauthorurl, $badge->imageauthorurl, array('target' => '_blank'));
368         }
369         if (!empty($badge->imagecaption)) {
370             $dl[get_string('imagecaption', 'badges')] = $badge->imagecaption;
371         }
373         if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
374             $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid));
375             $dl[get_string('course')] = $coursename;
376         }
377         $dl[get_string('bcriteria', 'badges')] = self::print_badge_criteria($badge);
378         $output .= $this->definition_list($dl);
380         $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3);
381         $dl = array();
382         $issued['issuedOn'] = !preg_match( '~^[1-9][0-9]*$~', $issued['issuedOn'] ) ?
383             strtotime($issued['issuedOn']) : $issued['issuedOn'];
384         $dl[get_string('dateawarded', 'badges')] = userdate($issued['issuedOn']);
385         if (isset($issued['expires'])) {
386             $issued['expires'] = !preg_match( '~^[1-9][0-9]*$~', $issued['expires'] ) ?
387                 strtotime($issued['expires']) : $issued['expires'];
388             if ($issued['expires'] < $now) {
389                 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']) . get_string('warnexpired', 'badges');
391             } else {
392                 $dl[get_string('expirydate', 'badges')] = userdate($issued['expires']);
393             }
394         }
396         // Print evidence.
397         $agg = $badge->get_aggregation_methods();
398         $evidence = $badge->get_criteria_completions($userinfo->id);
399         $eids = array_map(function($o) {
400             return $o->critid;
401         }, $evidence);
402         unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
404         $items = array();
405         foreach ($badge->criteria as $type => $c) {
406             if (in_array($c->id, $eids)) {
407                 if (count($c->params) == 1) {
408                     $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
409                 } else {
410                     $items[] = get_string('criteria_descr_' . $type , 'badges',
411                             core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
412                 }
413             }
414         }
416         $dl[get_string('evidence', 'badges')] = get_string('completioninfo', 'badges') . html_writer::alist($items, array(), 'ul');
417         $output .= $this->definition_list($dl);
418         $endorsement = $badge->get_endorsement();
419         if (!empty($endorsement)) {
420             $output .= self::print_badge_endorsement($badge);
421         }
422         $relatedbadges = $badge->get_related_badges();
423         if (!empty($relatedbadges)) {
424             $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
425             $items = array();
426             foreach ($relatedbadges as $related) {
427                 $items[] = $related->name;
428             }
429             $output .= html_writer::alist($items, array(), 'ul');
430         }
431         $competencies = $badge->get_alignment();
432         if (!empty($competencies)) {
433             $output .= $this->heading(get_string('alignment', 'badges'), 3);
434             $items = array();
435             foreach ($competencies as $competency) {
436                 $items[] = html_writer::link($competency->targeturl, $competency->targetname, array('target' => '_blank'));
437             }
438             $output .= html_writer::alist($items, array(), 'ul');
439         }
440         $output .= html_writer::end_tag('div');
442         return $output;
443     }
445     // Outputs external badge.
446     protected function render_external_badge(external_badge $ibadge) {
447         $issued = $ibadge->issued;
448         $assertion = $issued->assertion;
449         $issuer = $assertion->badge->issuer;
450         $userinfo = $ibadge->recipient;
451         $table = new html_table();
452         $today = strtotime(date('Y-m-d'));
454         $output = '';
455         $output .= html_writer::start_tag('div', array('id' => 'badge'));
456         $output .= html_writer::start_tag('div', array('id' => 'badge-image'));
457         $output .= html_writer::empty_tag('img', array('src' => $issued->imageUrl));
458         if (isset($assertion->expires)) {
459             $expiration = !strtotime($assertion->expires) ? s($assertion->expires) : strtotime($assertion->expires);
460             if ($expiration < $today) {
461                 $output .= $this->output->pix_icon('i/expired',
462                         get_string('expireddate', 'badges', userdate($expiration)),
463                         'moodle',
464                         array('class' => 'expireimage'));
465             }
466         }
467         $output .= html_writer::end_tag('div');
469         $output .= html_writer::start_tag('div', array('id' => 'badge-details'));
471         // Recipient information.
472         $output .= $this->output->heading(get_string('recipientdetails', 'badges'), 3);
473         $dl = array();
474         // Technically, we should alway have a user at this point, but added an extra check just in case.
475         if ($userinfo) {
476             if (!$ibadge->valid) {
477                 $notify = $this->output->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice');
478                 $dl[get_string('name')] = fullname($userinfo) . $notify;
479             } else {
480                 $dl[get_string('name')] = fullname($userinfo);
481             }
482         } else {
483             $notify = $this->output->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice');
484             $dl[get_string('name')] = $notify;
485         }
486         $output .= $this->definition_list($dl);
488         $output .= $this->output->heading(get_string('issuerdetails', 'badges'), 3);
489         $dl = array();
490         $dl[get_string('issuername', 'badges')] = s($issuer->name);
491         $dl[get_string('issuerurl', 'badges')] = html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin));
493         if (isset($issuer->contact)) {
494             $dl[get_string('contact', 'badges')] = obfuscate_mailto($issuer->contact);
495         }
496         $output .= $this->definition_list($dl);
498         $output .= $this->output->heading(get_string('badgedetails', 'badges'), 3);
499         $dl = array();
500         $dl[get_string('name')] = s($assertion->badge->name);
501         $dl[get_string('description', 'badges')] = s($assertion->badge->description);
502         $dl[get_string('bcriteria', 'badges')] = html_writer::tag('a', s($assertion->badge->criteria), array('href' => $assertion->badge->criteria));
503         $output .= $this->definition_list($dl);
505         $output .= $this->output->heading(get_string('issuancedetails', 'badges'), 3);
506         $dl = array();
507         if (isset($assertion->issued_on)) {
508             $issuedate = !strtotime($assertion->issued_on) ? s($assertion->issued_on) : strtotime($assertion->issued_on);
509             $dl[get_string('dateawarded', 'badges')] = userdate($issuedate);
510         }
511         if (isset($assertion->expires)) {
512             if ($expiration < $today) {
513                 $dl[get_string('expirydate', 'badges')] = userdate($expiration) . get_string('warnexpired', 'badges');
514             } else {
515                 $dl[get_string('expirydate', 'badges')] = userdate($expiration);
516             }
517         }
518         if (isset($assertion->evidence)) {
519             $dl[get_string('evidence', 'badges')] = html_writer::tag('a', s($assertion->evidence), array('href' => $assertion->evidence));
520         }
521         $output .= $this->definition_list($dl);
522         $output .= html_writer::end_tag('div');
524         return $output;
525     }
527     // Displays the user badges.
528     protected function render_badge_user_collection(badge_user_collection $badges) {
529         global $CFG, $USER, $SITE;
530         $backpack = $badges->backpack;
531         $mybackpack = new moodle_url('/badges/mybackpack.php');
533         $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
534         $htmlpagingbar = $this->render($paging);
536         // Set backpack connection string.
537         $backpackconnect = '';
538         if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) {
539             $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox');
540         }
541         // Search box.
542         $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
544         // Download all button.
545         $downloadall = $this->output->single_button(
546                     new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
547                     get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
549         // Local badges.
550         $localhtml = html_writer::start_tag('div', array('id' => 'issued-badge-table', 'class' => 'generalbox'));
551         $heading = get_string('localbadges', 'badges', format_string($SITE->fullname, true, array('context' => context_system::instance())));
552         $localhtml .= $this->output->heading_with_help($heading, 'localbadgesh', 'badges');
553         if ($badges->badges) {
554             $downloadbutton = $this->output->heading(get_string('badgesearned', 'badges', $badges->totalcount), 4, 'activatebadge');
555             $downloadbutton .= $downloadall;
557             $htmllist = $this->print_badges_list($badges->badges, $USER->id);
558             $localhtml .= $backpackconnect . $downloadbutton . $searchform . $htmlpagingbar . $htmllist . $htmlpagingbar;
559         } else {
560             $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
561         }
562         $localhtml .= html_writer::end_tag('div');
564         // External badges.
565         $externalhtml = "";
566         if (!empty($CFG->badges_allowexternalbackpack)) {
567             $externalhtml .= html_writer::start_tag('div', array('class' => 'generalbox'));
568             $externalhtml .= $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges');
569             if (!is_null($backpack)) {
570                 if ($backpack->totalcollections == 0) {
571                     $externalhtml .= get_string('nobackpackcollections', 'badges', $backpack);
572                 } else {
573                     if ($backpack->totalbadges == 0) {
574                         $externalhtml .= get_string('nobackpackbadges', 'badges', $backpack);
575                     } else {
576                         $externalhtml .= get_string('backpackbadges', 'badges', $backpack);
577                         $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
578                     }
579                 }
580             } else {
581                 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out());
582             }
584             $externalhtml .= html_writer::end_tag('div');
585         }
587         return $localhtml . $externalhtml;
588     }
590     // Displays the available badges.
591     protected function render_badge_collection(badge_collection $badges) {
592         $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
593         $htmlpagingbar = $this->render($paging);
594         $table = new html_table();
595         $table->attributes['class'] = 'collection';
597         $sortbyname = $this->helper_sortable_heading(get_string('name'),
598                 'name', $badges->sort, $badges->dir);
599         $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
600                 'dateissued', $badges->sort, $badges->dir);
601         $table->head = array(
602                     get_string('badgeimage', 'badges'),
603                     $sortbyname,
604                     get_string('description', 'badges'),
605                     get_string('bcriteria', 'badges'),
606                     $sortbyawarded
607                 );
608         $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
610         foreach ($badges->badges as $badge) {
611             $badgeimage = print_badge_image($badge, $this->page->context, 'large');
612             $name = $badge->name;
613             $description = $badge->description;
614             $criteria = self::print_badge_criteria($badge);
615             if ($badge->dateissued) {
616                 $icon = new pix_icon('i/valid',
617                             get_string('dateearned', 'badges',
618                                 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
619                 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
620                 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
621             } else {
622                 $awarded = "";
623             }
624             $row = array($badgeimage, $name, $description, $criteria, $awarded);
625             $table->data[] = $row;
626         }
628         $htmltable = html_writer::table($table);
630         return $htmlpagingbar . $htmltable . $htmlpagingbar;
631     }
633     // Outputs table of badges with actions available.
634     protected function render_badge_management(badge_management $badges) {
635         $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
637         // New badge button.
638         $htmlnew = '';
639         if (has_capability('moodle/badges:createbadge', $this->page->context)) {
640             $n['type'] = $this->page->url->get_param('type');
641             $n['id'] = $this->page->url->get_param('id');
642             $htmlnew = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
643         }
645         $htmlpagingbar = $this->render($paging);
646         $table = new html_table();
647         $table->attributes['class'] = 'collection';
649         $sortbyname = $this->helper_sortable_heading(get_string('name'),
650                 'name', $badges->sort, $badges->dir);
651         $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
652                 'status', $badges->sort, $badges->dir);
653         $table->head = array(
654                 $sortbyname,
655                 $sortbystatus,
656                 get_string('bcriteria', 'badges'),
657                 get_string('awards', 'badges'),
658                 get_string('actions')
659             );
660         $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
662         foreach ($badges->badges as $b) {
663             $style = !$b->is_active() ? array('class' => 'dimmed') : array();
664             $forlink =  print_badge_image($b, $this->page->context) . ' ' .
665                         html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
666             $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
667             $status = $b->statstring;
668             $criteria = self::print_badge_criteria($b, 'short');
670             if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
671                 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
672             } else {
673                 $awards = $b->awards;
674             }
676             $actions = self::print_badge_table_actions($b, $this->page->context);
678             $row = array($name, $status, $criteria, $awards, $actions);
679             $table->data[] = $row;
680         }
681         $htmltable = html_writer::table($table);
683         return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
684     }
686     // Prints tabs for badge editing.
687     public function print_badge_tabs($badgeid, $context, $current = 'overview') {
688         global $DB;
690         $badge = new badge($badgeid);
691         $row = array();
693         $row[] = new tabobject('overview',
694                     new moodle_url('/badges/overview.php', array('id' => $badgeid)),
695                     get_string('boverview', 'badges')
696                 );
698         if (has_capability('moodle/badges:configuredetails', $context)) {
699             $row[] = new tabobject('details',
700                         new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'details')),
701                         get_string('bdetails', 'badges')
702                     );
703         }
705         if (has_capability('moodle/badges:configurecriteria', $context)) {
706             $row[] = new tabobject('criteria',
707                         new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
708                         get_string('bcriteria', 'badges')
709                     );
710         }
712         if (has_capability('moodle/badges:configuremessages', $context)) {
713             $row[] = new tabobject('message',
714                         new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
715                         get_string('bmessage', 'badges')
716                     );
717         }
719         if (has_capability('moodle/badges:viewawarded', $context)) {
720             $awarded = $DB->count_records_sql('SELECT COUNT(b.userid)
721                                                FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id
722                                                WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid));
723             $row[] = new tabobject('awards',
724                         new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
725                         get_string('bawards', 'badges', $awarded)
726                     );
727         }
729         if (has_capability('moodle/badges:configuredetails', $context)) {
730             $row[] = new tabobject('bendorsement',
731                 new moodle_url('/badges/endorsement.php', array('id' => $badgeid)),
732                 get_string('bendorsement', 'badges')
733             );
734         }
736         if (has_capability('moodle/badges:configuredetails', $context)) {
737             $sql = "SELECT COUNT(br.badgeid)
738                       FROM {badge_related} br
739                      WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)";
740             $related = $DB->count_records_sql($sql, ['badgeid' => $badgeid, 'badgeid2' => $badgeid]);
741             $row[] = new tabobject('brelated',
742                 new moodle_url('/badges/related.php', array('id' => $badgeid)),
743                 get_string('brelated', 'badges', $related)
744             );
745         }
747         if (has_capability('moodle/badges:configuredetails', $context)) {
748             $competencies = $DB->count_records_sql("SELECT COUNT(bc.id)
749                       FROM {badge_competencies} bc WHERE bc.badgeid = :badgeid", array('badgeid' => $badgeid));
750             $row[] = new tabobject('balignment',
751                 new moodle_url('/badges/competency.php', array('id' => $badgeid)),
752                 get_string('balignment', 'badges', $competencies)
753             );
754         }
756         echo $this->tabtree($row, $current);
757     }
759     /**
760      * Prints badge status box.
761      * @return Either the status box html as a string or null
762      */
763     public function print_badge_status_box(badge $badge) {
764         if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
766             if (!$badge->has_criteria()) {
767                 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
768                 $status = get_string('nocriteria', 'badges');
769                 if ($this->page->url != $criteriaurl) {
770                     $action = $this->output->single_button(
771                         $criteriaurl,
772                         get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
773                 } else {
774                     $action = '';
775                 }
777                 $message = $status . $action;
778             } else {
779                 $status = get_string('statusmessage_' . $badge->status, 'badges');
780                 if ($badge->is_active()) {
781                     $action = $this->output->single_button(new moodle_url('/badges/action.php',
782                                 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
783                                       'return' => $this->page->url->out_as_local_url(false))),
784                             get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
785                 } else {
786                     $action = $this->output->single_button(new moodle_url('/badges/action.php',
787                                 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
788                                       'return' => $this->page->url->out_as_local_url(false))),
789                             get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
790                 }
792                 $message = $status . $this->output->help_icon('status', 'badges') . $action;
794             }
796             $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
797             return $this->output->box($message, $style);
798         }
800         return null;
801     }
803     /**
804      * Returns information about badge criteria in a list form.
805      *
806      * @param badge $badge Badge objects
807      * @param string $short Indicates whether to print full info about this badge
808      * @return string $output HTML string to output
809      */
810     public function print_badge_criteria(badge $badge, $short = '') {
811         $agg = $badge->get_aggregation_methods();
812         if (empty($badge->criteria)) {
813             return get_string('nocriteria', 'badges');
814         }
816         $overalldescr = '';
817         $overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL];
818         if (!$short && !empty($overall->description)) {
819             $overalldescr = $this->output->box(
820                 format_text($overall->description, $overall->descriptionformat, array('context' => $badge->get_context())),
821                 'criteria-description'
822                 );
823         }
825         // Get the condition string.
826         if (count($badge->criteria) == 2) {
827             $condition = '';
828             if (!$short) {
829                 $condition = get_string('criteria_descr', 'badges');
830             }
831         } else {
832             $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
833                                       core_text::strtoupper($agg[$badge->get_aggregation_method()]));
834         }
836         unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
838         $items = array();
839         // If only one criterion left, make sure its description goe to the top.
840         if (count($badge->criteria) == 1) {
841             $c = reset($badge->criteria);
842             if (!$short && !empty($c->description)) {
843                 $overalldescr = $this->output->box(
844                     format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
845                     'criteria-description'
846                     );
847             }
848             if (count($c->params) == 1) {
849                 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype , 'badges') .
850                            $c->get_details($short);
851             } else {
852                 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype, 'badges',
853                         core_text::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype)])) .
854                         $c->get_details($short);
855             }
856         } else {
857             foreach ($badge->criteria as $type => $c) {
858                 $criteriadescr = '';
859                 if (!$short && !empty($c->description)) {
860                     $criteriadescr = $this->output->box(
861                         format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
862                         'criteria-description'
863                         );
864                 }
865                 if (count($c->params) == 1) {
866                     $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
867                                $c->get_details($short) . $criteriadescr;
868                 } else {
869                     $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
870                             core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) .
871                             $c->get_details($short) .
872                             $criteriadescr;
873                 }
874             }
875         }
877         return $overalldescr . $condition . html_writer::alist($items, array(), 'ul');;
878     }
880     // Prints criteria actions for badge editing.
881     public function print_criteria_actions(badge $badge) {
882         $output = '';
883         if (!$badge->is_active() && !$badge->is_locked()) {
884             $accepted = $badge->get_accepted_criteria();
885             $potential = array_diff($accepted, array_keys($badge->criteria));
887             if (!empty($potential)) {
888                 foreach ($potential as $p) {
889                     if ($p != 0) {
890                         $select[$p] = get_string('criteria_' . $p, 'badges');
891                     }
892                 }
893                 $output .= $this->output->single_select(
894                     new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id, 'add' => true)),
895                     'type',
896                     $select,
897                     '',
898                     array('' => 'choosedots'),
899                     null,
900                     array('label' => get_string('addbadgecriteria', 'badges'))
901                 );
902             } else {
903                 $output .= $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
904             }
905         }
907         return $output;
908     }
910     // Renders a table with users who have earned the badge.
911     // Based on stamps collection plugin.
912     protected function render_badge_recipients(badge_recipients $recipients) {
913         $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
914         $htmlpagingbar = $this->render($paging);
915         $table = new html_table();
916         $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
918         $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
919                 'firstname', $recipients->sort, $recipients->dir);
920         $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
921                 'lastname', $recipients->sort, $recipients->dir);
922         if ($this->helper_fullname_format() == 'lf') {
923             $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
924         } else {
925             $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
926         }
928         $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
929                 'dateissued', $recipients->sort, $recipients->dir);
931         $table->head = array($sortbyname, $sortbydate, '');
933         foreach ($recipients->userids as $holder) {
934             $fullname = fullname($holder);
935             $fullname = html_writer::link(
936                             new moodle_url('/user/profile.php', array('id' => $holder->userid)),
937                             $fullname
938                         );
939             $awarded  = userdate($holder->dateissued);
940             $badgeurl = html_writer::link(
941                             new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
942                             get_string('viewbadge', 'badges')
943                         );
945             $row = array($fullname, $awarded, $badgeurl);
946             $table->data[] = $row;
947         }
949         $htmltable = html_writer::table($table);
951         return $htmlpagingbar . $htmltable . $htmlpagingbar;
952     }
954     ////////////////////////////////////////////////////////////////////////////
955     // Helper methods
956     // Reused from stamps collection plugin
957     ////////////////////////////////////////////////////////////////////////////
959     /**
960      * Renders a text with icons to sort by the given column
961      *
962      * This is intended for table headings.
963      *
964      * @param string $text    The heading text
965      * @param string $sortid  The column id used for sorting
966      * @param string $sortby  Currently sorted by (column id)
967      * @param string $sorthow Currently sorted how (ASC|DESC)
968      *
969      * @return string
970      */
971     protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
972         $out = html_writer::tag('span', $text, array('class' => 'text'));
974         if (!is_null($sortid)) {
975             if ($sortby !== $sortid || $sorthow !== 'ASC') {
976                 $url = new moodle_url($this->page->url);
977                 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
978                 $out .= $this->output->action_icon($url,
979                         new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
980             }
981             if ($sortby !== $sortid || $sorthow !== 'DESC') {
982                 $url = new moodle_url($this->page->url);
983                 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
984                 $out .= $this->output->action_icon($url,
985                         new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
986             }
987         }
988         return $out;
989     }
990     /**
991      * Tries to guess the fullname format set at the site
992      *
993      * @return string fl|lf
994      */
995     protected function helper_fullname_format() {
996         $fake = new stdClass();
997         $fake->lastname = 'LLLL';
998         $fake->firstname = 'FFFF';
999         $fullname = get_string('fullnamedisplay', '', $fake);
1000         if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
1001             return 'lf';
1002         } else {
1003             return 'fl';
1004         }
1005     }
1006     /**
1007      * Renders a search form
1008      *
1009      * @param string $search Search string
1010      * @return string HTML
1011      */
1012     protected function helper_search_form($search) {
1013         global $CFG;
1014         require_once($CFG->libdir . '/formslib.php');
1016         $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
1018         $mform->addElement('hidden', 'sesskey', sesskey());
1020         $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
1021         $mform->setDefault('search', $search);
1022         $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
1023         $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
1024         $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
1026         ob_start();
1027         $mform->display();
1028         $out = ob_get_clean();
1030         return $out;
1031     }
1033     /**
1034      * Renders a definition list
1035      *
1036      * @param array $items the list of items to define
1037      * @param array
1038      */
1039     protected function definition_list(array $items, array $attributes = array()) {
1040         $output = html_writer::start_tag('dl', $attributes);
1041         foreach ($items as $label => $value) {
1042             $output .= html_writer::tag('dt', $label);
1043             $output .= html_writer::tag('dd', $value);
1044         }
1045         $output .= html_writer::end_tag('dl');
1046         return $output;
1047     }
1049     /**
1050      * Outputs list en badges.
1051      *
1052      * @param badge $badge Badge object.
1053      * @return string $output content endorsement to output.
1054      */
1055     protected function print_badge_endorsement(badge $badge) {
1056         $output = '';
1057         $endorsement = $badge->get_endorsement();
1058         $dl = array();
1059         $output .= $this->heading(get_string('endorsement', 'badges'), 3);
1060         if (!empty($endorsement)) {
1061             $dl[get_string('issuername', 'badges')] = $endorsement->issuername;
1062             $dl[get_string('issueremail', 'badges')] =
1063                 html_writer::tag('a', $endorsement->issueremail, array('href' => 'mailto:' . $endorsement->issueremail));
1064             $dl[get_string('issuerurl', 'badges')] = html_writer::link($endorsement->issuerurl, $endorsement->issuerurl,
1065                 array('target' => '_blank'));
1066             $dl[get_string('dateawarded', 'badges')] = userdate($endorsement->dateissued);
1067             $dl[get_string('claimid', 'badges')] = html_writer::link($endorsement->claimid, $endorsement->claimid,
1068             array('target' => '_blank'));
1069             $dl[get_string('claimcomment', 'badges')] = $endorsement->claimcomment;
1070             $output .= $this->definition_list($dl);
1071         } else {
1072             $output .= get_string('noendorsement', 'badges');
1073         }
1074         return $output;
1075     }
1077     /**
1078      * Print list badges related.
1079      *
1080      * @param badge $badge Badge objects.
1081      * @return string $output List related badges to output.
1082      */
1083     protected function print_badge_related(badge $badge) {
1084         $output = '';
1085         $relatedbadges = $badge->get_related_badges();
1086         $output .= $this->heading(get_string('relatedbages', 'badges'), 3);
1087         if (!empty($relatedbadges)) {
1088             $items = array();
1089             foreach ($relatedbadges as $related) {
1090                 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id));
1091                 $items[] = html_writer::link($relatedurl->out(), $related->name, array('target' => '_blank'));
1092             }
1093             $output .= html_writer::alist($items, array(), 'ul');
1094         } else {
1095             $output .= get_string('norelated', 'badges');
1096         }
1097         return $output;
1098     }
1100     /**
1101      * Print list badge competencies.
1102      *
1103      * @param badge $badge Badge objects.
1104      * @return string $output List competencies to output.
1105      */
1106     protected function print_badge_competencies(badge $badge) {
1107         $output = '';
1108         $output .= $this->heading(get_string('alignment', 'badges'), 3);
1109         $competencies = $badge->get_alignment();
1110         if (!empty($competencies)) {
1111             $items = array();
1112             foreach ($competencies as $competency) {
1113                 $urlaligment = new moodle_url('competency.php',
1114                     array('id' => $badge->id, 'alignmentid' => $competency->id)
1115                 );
1116                 $items[] = html_writer::link($urlaligment, $competency->targetname, array('target' => '_blank'));
1117             }
1118             $output .= html_writer::alist($items, array(), 'ul');
1119         } else {
1120             $output .= get_string('noalignment', 'badges');
1121         }
1122         return $output;
1123     }
1125     /**
1126      * Renders a table for related badges.
1127      *
1128      * @param badge_related $related list related badges.
1129      * @return string list related badges to output.
1130      */
1131     protected function render_badge_related(badge_related $related) {
1132         $currentbadge = new badge($related->currentbadgeid);
1133         $languages = get_string_manager()->get_list_of_languages();
1134         $paging = new paging_bar($related->totalcount, $related->page, $related->perpage, $this->page->url, 'page');
1135         $htmlpagingbar = $this->render($paging);
1136         $table = new html_table();
1137         $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1138         $table->head = array(
1139             get_string('name'),
1140             get_string('version', 'badges'),
1141             get_string('language', 'badges'),
1142             get_string('type', 'badges')
1143         );
1144         if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1145             array_push($table->head, '');
1146         }
1148         foreach ($related->badges as $badge) {
1149             $badgeobject = new badge($badge->id);
1150             $style = array('title' => $badgeobject->name);
1151             if (!$badgeobject->is_active()) {
1152                 $style['class'] = 'dimmed';
1153             }
1154             $context = ($badgeobject->type == BADGE_TYPE_SITE) ?
1155                 context_system::instance() : context_course::instance($badgeobject->courseid);
1156             $forlink = print_badge_image($badgeobject, $context) . ' ' .
1157                 html_writer::start_tag('span') . $badgeobject->name . html_writer::end_tag('span');
1158             $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $badgeobject->id)), $forlink, $style);
1160             $row = array(
1161                 $name,
1162                 $badge->version,
1163                 $badge->language ? $languages[$badge->language] : '',
1164                 $badge->type == BADGE_TYPE_COURSE ? get_string('badgesview', 'badges') : get_string('sitebadges', 'badges')
1165             );
1166             if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1167                 $action = $this->output->action_icon(
1168                     new moodle_url('related_action.php',
1169                         array(
1170                             'badgeid' => $related->currentbadgeid,
1171                             'relatedid' => $badge->id,
1172                             'action' => 'remove'
1173                         )
1174                     ), new pix_icon('t/delete', get_string('delete')));
1175                 $actions = html_writer::tag('div', $action, array('class' => 'badge-actions'));
1176                 array_push($row, $actions);
1177             }
1178             $table->data[] = $row;
1179         }
1180         $htmltable = html_writer::table($table);
1182         return $htmlpagingbar . $htmltable . $htmlpagingbar;
1183     }
1185     /**
1186      * Renders a table with competencies alignment.
1187      *
1188      * @param badge_competencies_alignment $alignment List competencies alignment.
1189      * @return string List competencies aligment to output.
1190      */
1191     protected function render_badge_competencies_alignment(badge_competencies_alignment $alignment) {
1192         $currentbadge = new badge($alignment->currentbadgeid);
1193         $paging = new paging_bar($alignment->totalcount, $alignment->page, $alignment->perpage, $this->page->url, 'page');
1194         $htmlpagingbar = $this->render($paging);
1195         $table = new html_table();
1196         $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide';
1197         $table->head = array('Name', 'URL', '');
1199         foreach ($alignment->alignments as $item) {
1200             $urlaligment = new moodle_url('competency.php',
1201                 array(
1202                     'id' => $currentbadge->id,
1203                     'alignmentid' => $item->id,
1204                 )
1205             );
1206             $row = array(
1207                 html_writer::link($urlaligment, $item->targetname),
1208                 html_writer::link($item->targeturl, $item->targeturl, array('target' => '_blank'))
1209             );
1210             if (!$currentbadge->is_active() && !$currentbadge->is_locked()) {
1211                 $delete = $this->output->action_icon(
1212                     new moodle_url('competency_action.php',
1213                         array(
1214                             'id' => $currentbadge->id,
1215                             'alignmentid' => $item->id,
1216                             'action' => 'remove'
1217                         )
1218                     ), new pix_icon('t/delete', get_string('delete')));
1219                 $edit = $this->output->action_icon(
1220                     new moodle_url('competency.php',
1221                         array(
1222                             'id' => $currentbadge->id,
1223                             'alignmentid' => $item->id,
1224                             'action' => 'edit'
1225                         )
1226                     ), new pix_icon('t/edit', get_string('edit')));
1227                 $actions = html_writer::tag('div', $edit . $delete, array('class' => 'badge-actions'));
1228                 array_push($row, $actions);
1229             }
1230             $table->data[] = $row;
1231         }
1232         $htmltable = html_writer::table($table);
1234         return $htmlpagingbar . $htmltable . $htmlpagingbar;
1235     }
1238 /**
1239  * An issued badges for badge.php page
1240  */
1241 class issued_badge implements renderable {
1242     /** @var issued badge */
1243     public $issued;
1245     /** @var badge recipient */
1246     public $recipient;
1248     /** @var badge class */
1249     public $badgeclass;
1251     /** @var badge visibility to others */
1252     public $visible = 0;
1254     /** @var badge class */
1255     public $badgeid = 0;
1257     /**
1258      * Initializes the badge to display
1259      *
1260      * @param string $hash Issued badge hash
1261      */
1262     public function __construct($hash) {
1263         global $DB;
1265         $assertion = new core_badges_assertion($hash);
1266         $this->issued = $assertion->get_badge_assertion();
1267         $this->badgeclass = $assertion->get_badge_class();
1269         $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
1270                 FROM {badge_issued}
1271                 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
1272                 array('hash' => $hash), IGNORE_MISSING);
1273         if ($rec) {
1274             // Get a recipient from database.
1275             $namefields = get_all_user_name_fields(true, 'u');
1276             $user = $DB->get_record_sql("SELECT u.id, $namefields, u.deleted, u.email
1277                         FROM {user} u WHERE u.id = :userid", array('userid' => $rec->userid));
1278             $this->recipient = $user;
1279             $this->visible = $rec->visible;
1280             $this->badgeid = $rec->badgeid;
1281         }
1282     }
1285 /**
1286  * An external badges for external.php page
1287  */
1288 class external_badge implements renderable {
1289     /** @var issued badge */
1290     public $issued;
1292     /** @var User ID */
1293     public $recipient;
1295     /** @var validation of external badge */
1296     public $valid = true;
1298     /**
1299      * Initializes the badge to display
1300      *
1301      * @param object $badge External badge information.
1302      * @param int $recipient User id.
1303      */
1304     public function __construct($badge, $recipient) {
1305         global $DB;
1306         // At this point a user has connected a backpack. So, we are going to get
1307         // their backpack email rather than their account email.
1308         $namefields = get_all_user_name_fields(true, 'u');
1309         $user = $DB->get_record_sql("SELECT {$namefields}, b.email
1310                     FROM {user} u INNER JOIN {badge_backpack} b ON u.id = b.userid
1311                     WHERE userid = :userid", array('userid' => $recipient), IGNORE_MISSING);
1313         $this->issued = $badge;
1314         $this->recipient = $user;
1316         // Check if recipient is valid.
1317         // There is no way to be 100% sure that a badge belongs to a user.
1318         // Backpack does not return any recipient information.
1319         // All we can do is compare that backpack email hashed using salt
1320         // provided in the assertion matches a badge recipient from the assertion.
1321         if ($user) {
1322             if (validate_email($badge->assertion->recipient) && $badge->assertion->recipient == $user->email) {
1323                 // If we have email, compare emails.
1324                 $this->valid = true;
1325             } else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email)) {
1326                 // If recipient is hashed, but no salt, compare hashes without salt.
1327                 $this->valid = true;
1328             } else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email . $badge->assertion->salt)) {
1329                 // If recipient is hashed, compare hashes.
1330                 $this->valid = true;
1331             } else {
1332                 // Otherwise, we cannot be sure that this user is a recipient.
1333                 $this->valid = false;
1334             }
1335         } else {
1336             $this->valid = false;
1337         }
1338     }
1341 /**
1342  * Badge recipients rendering class
1343  */
1344 class badge_recipients implements renderable {
1345     /** @var string how are the data sorted */
1346     public $sort = 'lastname';
1348     /** @var string how are the data sorted */
1349     public $dir = 'ASC';
1351     /** @var int page number to display */
1352     public $page = 0;
1354     /** @var int number of badge recipients to display per page */
1355     public $perpage = 30;
1357     /** @var int the total number or badge recipients to display */
1358     public $totalcount = null;
1360     /** @var array internal list of  badge recipients ids */
1361     public $userids = array();
1362     /**
1363      * Initializes the list of users to display
1364      *
1365      * @param array $holders List of badge holders
1366      */
1367     public function __construct($holders) {
1368         $this->userids = $holders;
1369     }
1372 /**
1373  * Collection of all badges for view.php page
1374  */
1375 class badge_collection implements renderable {
1377     /** @var string how are the data sorted */
1378     public $sort = 'name';
1380     /** @var string how are the data sorted */
1381     public $dir = 'ASC';
1383     /** @var int page number to display */
1384     public $page = 0;
1386     /** @var int number of badges to display per page */
1387     public $perpage = BADGE_PERPAGE;
1389     /** @var int the total number of badges to display */
1390     public $totalcount = null;
1392     /** @var array list of badges */
1393     public $badges = array();
1395     /**
1396      * Initializes the list of badges to display
1397      *
1398      * @param array $badges Badges to render
1399      */
1400     public function __construct($badges) {
1401         $this->badges = $badges;
1402     }
1405 /**
1406  * Collection of badges used at the index.php page
1407  */
1408 class badge_management extends badge_collection implements renderable {
1411 /**
1412  * Collection of user badges used at the mybadges.php page
1413  */
1414 class badge_user_collection extends badge_collection implements renderable {
1415     /** @var array backpack settings */
1416     public $backpack = null;
1418     /** @var string search */
1419     public $search = '';
1421     /**
1422      * Initializes user badge collection.
1423      *
1424      * @param array $badges Badges to render
1425      * @param int $userid Badges owner
1426      */
1427     public function __construct($badges, $userid) {
1428         global $CFG;
1429         parent::__construct($badges);
1431         if (!empty($CFG->badges_allowexternalbackpack)) {
1432             $this->backpack = get_backpack_settings($userid, true);
1433         }
1434     }
1437 /**
1438  * Collection of all related badges.
1439  *
1440  * @copyright  2018 Tung Thai
1441  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1442  * @author     Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
1443  */
1444 class badge_related implements renderable {
1446     /** @var string how are the data sorted. */
1447     public $sort = 'name';
1449     /** @var string how are the data sorted. */
1450     public $dir = 'ASC';
1452     /** @var int page number to display. */
1453     public $page = 0;
1455     /** @var int number of badges to display per page. */
1456     public $perpage = BADGE_PERPAGE;
1458     /** @var int the total number of badges to display. */
1459     public $totalcount = null;
1461     /** @var int the current badge. */
1462     public $currentbadgeid = 0;
1464     /** @var array list of badges. */
1465     public $badges = array();
1467     /**
1468      * Initializes the list of badges to display.
1469      *
1470      * @param array $badges related badges to render.
1471      * @param int $currentbadgeid ID current badge.
1472      */
1473     public function __construct($badges, $currentbadgeid) {
1474         $this->badges = $badges;
1475         $this->currentbadgeid = $currentbadgeid;
1476     }
1479 /**
1480  * Collection of all competencies.
1481  *
1482  * @copyright  2018 Tung Thai
1483  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1484  * @author     Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
1485  */
1486 class badge_competencies_alignment implements renderable
1488     /** @var string how are the data sorted. */
1489     public $sort = 'name';
1491     /** @var string how are the data sorted. */
1492     public $dir = 'ASC';
1494     /** @var int page number to display. */
1495     public $page = 0;
1497     /** @var int number of badges to display per page. */
1498     public $perpage = BADGE_PERPAGE;
1500     /** @var int the total number of badges to display. */
1501     public $totalcount = null;
1503     /** @var array list of badges. */
1504     public $alignments = array();
1506     /** @var array list of badges. */
1507     public $currentbadgeid = 0;
1509     /**
1510      * Initializes the list of competencies to display.
1511      *
1512      * @param array $alignments List competencies alignment to render.
1513      * @param int $currentbadgeid ID current badge.
1514      */
1515     public function __construct($alignments, $currentbadgeid) {
1516         $this->alignments = $alignments;
1517         $this->currentbadgeid = $currentbadgeid;
1518     }