Merge branch 'MDL-38880' of git://github.com/jmvedrine/moodle
[moodle.git] / badges / renderer.php
CommitLineData
27806552
YB
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/>.
16
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 */
26
27require_once($CFG->libdir . '/badgeslib.php');
28require_once($CFG->libdir . '/tablelib.php');
29require_once($CFG->dirroot . '/user/filters/lib.php');
30
31/**
32 * Standard HTML output renderer for badges
33 */
34class core_badges_renderer extends plugin_renderer_base {
35
36 // Outputs badges list.
37 public function print_badges_list($badges, $userid, $profile = false, $external = false) {
38 global $USER, $CFG;
39 foreach ($badges as $badge) {
40 if (!$external) {
41 $context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid);
42 $bname = $badge->name;
43 $imageurl = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', 'f1', false);
44 } else {
45 $bname = $badge->assertion->badge->name;
46 $imageurl = $badge->imageUrl;
47 }
48
49 $name = html_writer::tag('span', $bname, array('class' => 'badge-name'));
50
51 $image = html_writer::empty_tag('img', array('src' => $imageurl, 'class' => 'badge-image'));
52 if (!empty($badge->dateexpire) && $badge->dateexpire < time()) {
53 $image .= $this->output->pix_icon('i/expired',
54 get_string('expireddate', 'badges', userdate($badge->dateexpire)),
55 'moodle',
56 array('class' => 'expireimage'));
57 $name .= '(' . get_string('expired', 'badges') . ')';
58 }
59
60 $download = $status = $push = '';
61 if (($userid == $USER->id) && !$profile) {
62 $url = new moodle_url('mybadges.php', array('download' => $badge->id, 'hash' => $badge->uniquehash, 'sesskey' => sesskey()));
60d72efb 63 if (!empty($CFG->badges_allowexternalbackpack) && (empty($badge->dateexpire) || $badge->dateexpire > time())) {
27806552
YB
64 $assertion = new moodle_url('/badges/assertion.php', array('b' => $badge->uniquehash));
65 $action = new component_action('click', 'addtobackpack', array('assertion' => $assertion->out(false)));
66 $push = $this->output->action_icon(new moodle_url('#'), new pix_icon('t/backpack', get_string('addtobackpack', 'badges')), $action);
67 }
68
69 $download = $this->output->action_icon($url, new pix_icon('t/download', get_string('download')));
70 if ($badge->visible) {
71 $url = new moodle_url('mybadges.php', array('hide' => $badge->issuedid, 'sesskey' => sesskey()));
72 $status = $this->output->action_icon($url, new pix_icon('t/hide', get_string('makeprivate', 'badges')));
73 } else {
74 $url = new moodle_url('mybadges.php', array('show' => $badge->issuedid, 'sesskey' => sesskey()));
75 $status = $this->output->action_icon($url, new pix_icon('t/show', get_string('makepublic', 'badges')));
76 }
77 }
78
79 if (!$profile) {
80 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash));
81 } else {
82 if (!$external) {
83 $url = new moodle_url($CFG->wwwroot . '/badges/badge.php', array('hash' => $badge->uniquehash));
84 } else {
85 $url = new moodle_url($CFG->wwwroot . '/badges/external.php', array('badge' => serialize($badge)));
86 }
87 }
88 $actions = html_writer::tag('div', $push . $download . $status, array('class' => 'badge-actions'));
89 $items[] = html_writer::link($url, $image . $actions . $name, array('title' => $bname));
90 }
91
92 return html_writer::alist($items, array('class' => 'badges'));
93 }
94
95 // Recipients selection form.
96 public function recipients_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
97 $output = '';
98 $formattributes = array();
99 $formattributes['id'] = 'recipientform';
04da1a5a 100 $formattributes['action'] = $this->page->url;
27806552
YB
101 $formattributes['method'] = 'post';
102 $output .= html_writer::start_tag('form', $formattributes);
103 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
104
105 $existingcell = new html_table_cell();
106 $existingcell->text = $existinguc->display(true);
107 $existingcell->attributes['class'] = 'existing';
108 $actioncell = new html_table_cell();
109 $actioncell->text = html_writer::start_tag('div', array());
110 $actioncell->text .= html_writer::empty_tag('input', array(
111 'type' => 'submit',
112 'name' => 'award',
113 'value' => $this->output->larrow() . ' ' . get_string('award', 'badges'),
114 'class' => 'actionbutton')
115 );
116 $actioncell->text .= html_writer::end_tag('div', array());
117 $actioncell->attributes['class'] = 'actions';
118 $potentialcell = new html_table_cell();
119 $potentialcell->text = $potentialuc->display(true);
120 $potentialcell->attributes['class'] = 'potential';
121
122 $table = new html_table();
123 $table->attributes['class'] = 'recipienttable boxaligncenter';
124 $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
125 $output .= html_writer::table($table);
126
127 $output .= html_writer::end_tag('form');
128 return $output;
129 }
130
131 // Prints a badge overview infomation.
132 public function print_badge_overview($badge, $context) {
133 $display = "";
134
135 // Badge details.
136 $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
137 $display .= html_writer::tag('legend', get_string('badgedetails', 'badges'), array('class' => 'bold'));
138
139 $detailstable = new html_table();
140 $detailstable->attributes = array('class' => 'clearfix', 'id' => 'badgedetails');
141 $detailstable->data[] = array(get_string('name') . ":", $badge->name);
142 $detailstable->data[] = array(get_string('description', 'badges') . ":", $badge->description);
143 $detailstable->data[] = array(get_string('createdon', 'search') . ":", userdate($badge->timecreated));
144 $detailstable->data[] = array(get_string('badgeimage', 'badges') . ":",
145 print_badge_image($badge, $context, 'large'));
146 $display .= html_writer::table($detailstable);
147 $display .= html_writer::end_tag('fieldset');
148
149 // Issuer details.
150 $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
151 $display .= html_writer::tag('legend', get_string('issuerdetails', 'badges'), array('class' => 'bold'));
152
153 $issuertable = new html_table();
154 $issuertable->attributes = array('class' => 'clearfix', 'id' => 'badgeissuer');
155 $issuertable->data[] = array(get_string('issuername', 'badges') . ":", $badge->issuername);
156 $issuertable->data[] = array(get_string('contact', 'badges') . ":",
157 html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact)));
158 $display .= html_writer::table($issuertable);
159 $display .= html_writer::end_tag('fieldset');
160
161 // Issuance details if any.
162 $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
163 $display .= html_writer::tag('legend', get_string('issuancedetails', 'badges'), array('class' => 'bold'));
164 if ($badge->can_expire()) {
165 if ($badge->expiredate) {
166 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate));
167 } else if ($badge->expireperiod) {
168 if ($badge->expireperiod < 60) {
169 $display .= get_string('expireperiods', 'badges', round($badge->expireperiod, 2));
170 } else if ($badge->expireperiod < 60 * 60) {
171 $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod / 60, 2));
172 } else if ($badge->expireperiod < 60 * 60 * 24) {
173 $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod / 60 / 60, 2));
174 } else {
175 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2));
176 }
177 }
178 } else {
179 $display .= get_string('noexpiry', 'badges');
180 }
181 $display .= html_writer::end_tag('fieldset');
182
183 // Criteria details if any.
184 $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
185 $display .= html_writer::tag('legend', get_string('bcriteria', 'badges'), array('class' => 'bold'));
186 if ($badge->has_criteria()) {
187 $display .= self::print_badge_criteria($badge);
188 } else {
189 $display .= get_string('nocriteria', 'badges');
190 if (has_capability('moodle/badges:configurecriteria', $context)) {
191 $display .= $this->output->single_button(
192 new moodle_url('/badges/criteria.php', array('id' => $badge->id)),
193 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
194 }
195 }
196 $display .= html_writer::end_tag('fieldset');
197
198 // Awards details if any.
199 if (has_capability('moodle/badges:viewawarded', $context)) {
200 $display .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
201 $display .= html_writer::tag('legend', get_string('awards', 'badges'), array('class' => 'bold'));
202 if ($badge->has_awards()) {
203 $url = new moodle_url('/badges/recipients.php', array('id' => $badge->id));
204 $a = new stdClass();
205 $a->link = $url->out();
206 $a->count = count($badge->get_awards());
207 $display .= get_string('numawards', 'badges', $a);
208 } else {
209 $display .= get_string('noawards', 'badges');
210 }
211
212 if (has_capability('moodle/badges:awardbadge', $context) &&
213 $badge->has_manual_award_criteria() &&
214 $badge->is_active()) {
215 $display .= $this->output->single_button(
216 new moodle_url('/badges/award.php', array('id' => $badge->id)),
217 get_string('award', 'badges'), 'POST', array('class' => 'activatebadge'));
218 }
219 $display .= html_writer::end_tag('fieldset');
220 }
221
222 return $display;
223 }
224
225 // Prints action icons for the badge.
226 public function print_badge_table_actions($badge, $context) {
227 $actions = "";
228
229 if (has_capability('moodle/badges:configuredetails', $context)) {
230 // Activate/deactivate badge.
231 if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) {
232 $url = new moodle_url(qualified_me());
233 $url->param('activate', $badge->id);
c5177654 234 $url->param('sesskey', sesskey());
27806552
YB
235 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " ";
236 } else {
237 $url = new moodle_url(qualified_me());
238 $url->param('lock', $badge->id);
239 $url->param('sesskey', sesskey());
240 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " ";
241 }
242 }
243
244 // Award badge manually.
245 if ($badge->has_manual_award_criteria() &&
246 has_capability('moodle/badges:awardbadge', $context) &&
247 $badge->is_active()) {
248 $url = new moodle_url('/badges/award.php', array('id' => $badge->id));
249 $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " ";
250 }
251
252 // Edit badge.
253 if (has_capability('moodle/badges:configuredetails', $context)) {
254 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'details'));
255 $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " ";
256 }
257
258 // Duplicate badge.
259 if (has_capability('moodle/badges:createbadge', $context)) {
260 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey()));
261 $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " ";
262 }
263
264 // Delete badge.
265 if (has_capability('moodle/badges:deletebadge', $context)) {
266 $url = new moodle_url(qualified_me());
267 $url->param('delete', $badge->id);
268 $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " ";
269 }
270
271 return $actions;
272 }
273
274 // Outputs issued badge with actions available.
275 protected function render_issued_badge(issued_badge $ibadge) {
276 global $USER, $CFG, $DB;
277 $issued = $ibadge->issued;
278 $badge = new badge($ibadge->badgeid);
279 $today_date = date('Y-m-d');
280 $today = strtotime($today_date);
281
282 if ($ibadge->visible
283 || ($USER->id == $ibadge->recipient)
284 || has_capability('moodle/badges:viewawarded', context_system::instance())) {
285 $table = new html_table();
286
287 $imagetable = new html_table();
288 $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
289 $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued['badge']['image'])));
290 if ($USER->id == $ibadge->recipient && !empty($CFG->enablebadges)) {
291 $imagetable->data[] = array($this->output->single_button(
292 new moodle_url('/badges/badge.php', array('hash' => $ibadge->hash, 'bake' => true)),
293 get_string('download'),
294 'POST'));
295 $expiration = isset($issued['expires']) ? strtotime($issued['expires']) : $today + 1;
60d72efb 296 if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $today)) {
27806552
YB
297 $assertion = new moodle_url('/badges/assertion.php', array('b' => $ibadge->hash));
298 $attributes = array(
299 'type' => 'button',
300 'value' => get_string('addtobackpack', 'badges'),
301 'onclick' => 'OpenBadges.issue(["' . $assertion->out(false) . '"], function(errors, successes) { })');
302 $tobackpack = html_writer::tag('input', '', $attributes);
303 $imagetable->data[] = array($tobackpack);
304 }
305 }
306 $datatable = new html_table();
307 $datatable->attributes = array('class' => 'badgeissuedinfo');
308 $datatable->colclasses = array('bfield', 'bvalue');
309 $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
310 $datatable->data[] = array(get_string('issuername', 'badges'), $badge->issuername);
311 if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
312 $datatable->data[] = array(get_string('contact', 'badges'),
313 html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact)));
314 }
315 $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
316 $datatable->data[] = array(get_string('name'), $badge->name);
317 $datatable->data[] = array(get_string('description', 'badges'), $badge->description);
318
319 if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
320 $coursename = $DB->get_field('course', 'fullname', array('id' => $badge->courseid));
321 $datatable->data[] = array(get_string('course'), $coursename);
322 }
323
324 $datatable->data[] = array(get_string('bcriteria', 'badges'), self::print_badge_criteria($badge));
325 $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
326 $datatable->data[] = array(get_string('dateawarded', 'badges'), $issued['issued_on']);
327 if (isset($issued['expires'])) {
328 $expiration = strtotime($issued['expires']);
329 if ($expiration < $today) {
330 $cell = new html_table_cell($issued['expires'] . get_string('warnexpired', 'badges'));
331 $cell->attributes = array('class' => 'notifyproblem warning');
332 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
333
334 $image = html_writer::start_tag('div', array('class' => 'badge'));
335 $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
336 $image .= $this->output->pix_icon('i/expired',
337 get_string('expireddate', 'badges', $issued['expires']),
338 'moodle',
339 array('class' => 'expireimage'));
340 $image .= html_writer::end_tag('div');
341 $imagetable->data[0] = array($image);
342 } else {
343 $datatable->data[] = array(get_string('expirydate', 'badges'), $issued['expires']);
344 }
345 }
346
347 // Print evidence.
348 $agg = $badge->get_aggregation_methods();
349 $evidence = $badge->get_criteria_completions($ibadge->recipient);
350 $eids = array_map(create_function('$o', 'return $o->critid;'), $evidence);
351 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
352
353 $items = array();
354 foreach ($badge->criteria as $type => $c) {
355 if (in_array($c->id, $eids)) {
356 if (count($c->params) == 1) {
357 $items[] = get_string('criteria_descr_single_' . $type , 'badges') . $c->get_details();
358 } else {
359 $items[] = get_string('criteria_descr_' . $type , 'badges',
360 strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details();
361 }
362 }
363 }
364
365 $datatable->data[] = array(get_string('evidence', 'badges'),
366 get_string('completioninfo', 'badges') .
367 html_writer::alist($items, array(), 'ul'));
368 $table->attributes = array('class' => 'generalbox boxaligncenter issuedbadgebox');
369 $table->data[] = array(html_writer::table($imagetable), html_writer::table($datatable));
370 $htmlbadge = html_writer::table($table);
371
372 return $htmlbadge;
373 } else {
374 return get_string('hiddenbadge', 'badges');
375 }
376 }
377
378 // Outputs external badge.
379 protected function render_external_badge(external_badge $ibadge) {
380 $issued = $ibadge->issued;
381 $assertion = $issued->assertion;
382 $issuer = $assertion->badge->issuer;
383 $table = new html_table();
384
385 $imagetable = new html_table();
386 $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
387 $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued->imageUrl, 'width' => '100px')));
388
389 $datatable = new html_table();
390 $datatable->attributes = array('class' => 'badgeissuedinfo');
391 $datatable->colclasses = array('bfield', 'bvalue');
392 $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
393 $datatable->data[] = array(get_string('issuername', 'badges'), $issuer->name);
394 $datatable->data[] = array(get_string('issuerurl', 'badges'),
395 html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin)));
396 if (isset($issuer->contact)) {
397 $datatable->data[] = array(get_string('contact', 'badges'),
398 html_writer::tag('a', $issuer->contact, array('href' => 'mailto:' . $issuer->contact)));
399 }
400 $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
401 $datatable->data[] = array(get_string('name'), $assertion->badge->name);
402 $datatable->data[] = array(get_string('description', 'badges'), $assertion->badge->description);
403 $datatable->data[] = array(get_string('bcriteria', 'badges'),
404 html_writer::tag('a', $assertion->badge->criteria, array('href' => $assertion->badge->criteria)));
405 $datatable->data[] = array($this->output->heading(get_string('issuancedetails', 'badges'), 3), '');
406 if (isset($assertion->issued_on)) {
407 $datatable->data[] = array(get_string('dateawarded', 'badges'), $assertion->issued_on);
408 }
409 if (isset($assertion->badge->expire)) {
410 $today_date = date('Y-m-d');
411 $today = strtotime($today_date);
412 $expiration = strtotime($assertion->badge->expire);
413 if ($expiration < $today) {
414 $cell = new html_table_cell($assertion->badge->expire . get_string('warnexpired', 'badges'));
415 $cell->attributes = array('class' => 'notifyproblem warning');
416 $datatable->data[] = array(get_string('expirydate', 'badges'), $cell);
417
418 $image = html_writer::start_tag('div', array('class' => 'badge'));
419 $image .= html_writer::empty_tag('img', array('src' => $issued['badge']['image']));
420 $image .= html_writer::start_tag('span', array('class' => 'expired'))
421 . $this->output->pix_icon('i/expired',
422 get_string('expireddate', 'badges', $assertion->badge->expire),
423 'moodle',
424 array('class' => 'expireimage'))
425 . html_writer::end_tag('span');
426 $image .= html_writer::end_tag('div');
427 $imagetable->data[0] = array($image);
428 } else {
429 $datatable->data[] = array(get_string('expirydate', 'badges'), $assertion->badge->expire);
430 }
431 }
432 if (isset($assertion->evidence)) {
433 $datatable->data[] = array(get_string('evidence', 'badges'),
434 html_writer::tag('a', $assertion->evidence, array('href' => $assertion->evidence)));
435 }
436 $table->attributes = array('class' => 'generalbox boxaligncenter issuedbadgebox');
437 $table->data[] = array(html_writer::table($imagetable), html_writer::table($datatable));
438 $htmlbadge = html_writer::table($table);
439
440 return $htmlbadge;
441 }
442
443 // Outputs table of user badges.
444 protected function render_badge_user_collection(badge_user_collection $badges) {
445 global $CFG, $USER, $SITE;
446 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
447 $htmlpagingbar = $this->render($paging);
448
449 // Search box.
450 $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter');
451
452 // Download all button.
453 $downloadall = $this->output->single_button(
454 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())),
455 get_string('downloadall'), 'POST', array('class' => 'activatebadge'));
456
457 // Local badges.
458 $localhtml = html_writer::start_tag('fieldset', array('class' => 'generalbox'));
459 $localhtml .= html_writer::tag('legend',
460 $this->output->heading_with_help(get_string('localbadges', 'badges', $SITE->fullname), 'localbadgesh', 'badges'));
461 if ($badges->badges) {
462 $table = new html_table();
463 $table->attributes['class'] = 'statustable';
464 $table->data[] = array($this->output->heading(get_string('badgesearned', 'badges', $badges->totalcount), 4, 'activatebadge'), $downloadall);
465 $downloadbutton = html_writer::table($table);
466
467 $htmllist = $this->print_badges_list($badges->badges, $USER->id);
468 $localhtml .= $downloadbutton . $searchform . $htmlpagingbar . $htmllist . $htmlpagingbar;
469 } else {
470 $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'));
471 }
472 $localhtml .= html_writer::end_tag('fieldset');
473
474 // External badges.
475 $backpack = $badges->backpack;
476 $externalhtml = "";
60d72efb 477 if (!empty($CFG->badges_allowexternalbackpack)) {
27806552
YB
478 $externalhtml .= html_writer::start_tag('fieldset', array('class' => 'generalbox'));
479 $externalhtml .= html_writer::tag('legend', $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges'));
480 if (!is_null($backpack)) {
481 if ($backpack->totalbadges > 0) {
482 $externalhtml .= get_string('backpackbadges', 'badges', $backpack);
483 } else {
484 $externalhtml .= get_string('nobackpackbadges', 'badges', $backpack);
485 }
486 $label = get_string('editsettings', 'badges');
487 $externalhtml .= $this->output->single_button(
488 new moodle_url('mybackpack.php', array('clear' => true)),
489 get_string('clearsettings', 'badges'),
490 'POST',
491 array('class' => 'backpackform'));
492 } else {
493 $externalhtml .= get_string('nobackpack', 'badges');
494 $label = get_string('setup', 'badges');
495 }
496 $externalhtml .= $this->output->single_button('mybackpack.php', $label, 'POST', array('class' => 'backpackform'));
497
498 if (isset($backpack->totalbadges) && $backpack->totalbadges !== 0) {
499 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true);
500 }
501 $externalhtml .= html_writer::end_tag('fieldset');
502 }
503
504 return $localhtml . $externalhtml;
505 }
506
507 // Outputs table of available badges.
508 protected function render_badge_collection(badge_collection $badges) {
509 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
510 $htmlpagingbar = $this->render($paging);
511 $table = new html_table();
512 $table->attributes['class'] = 'collection boxaligncenter boxwidthwide';
513
514 $sortbyname = $this->helper_sortable_heading(get_string('name'),
515 'name', $badges->sort, $badges->dir);
516 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'),
517 'dateissued', $badges->sort, $badges->dir);
518 $table->head = array(
519 get_string('badgeimage', 'badges'),
520 $sortbyname,
521 get_string('description', 'badges'),
522 get_string('bcriteria', 'badges'),
523 $sortbyawarded
524 );
525 $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards');
526
527 foreach ($badges->badges as $badge) {
528 $badgeimage = print_badge_image($badge, $this->page->context, 'large');
529 $name = $badge->name;
530 $description = $badge->description;
531 $criteria = self::print_badge_criteria($badge);
532 if ($badge->dateissued) {
533 $icon = new pix_icon('i/tick_green_big',
534 get_string('dateearned', 'badges',
535 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig'))));
536 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
537 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true);
538 } else {
539 $awarded = "";
540 }
541 $row = array($badgeimage, $name, $description, $criteria, $awarded);
542 $table->data[] = $row;
543 }
544
545 $htmltable = html_writer::table($table);
546
547 return $htmlpagingbar . $htmltable . $htmlpagingbar;
548 }
549
550 // Outputs table of badges with actions available.
551 protected function render_badge_management(badge_management $badges) {
552 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
553
554 // New badge button.
555 $n['type'] = $this->page->url->get_param('type');
556 $n['id'] = $this->page->url->get_param('id');
557 $htmlnew = $this->output->single_button(new moodle_url('newbadge.php', $n), get_string('newbadge', 'badges'));
558
559 $htmlpagingbar = $this->render($paging);
560 $table = new html_table();
561 $table->attributes['class'] = 'collection';
562
563 $sortbyname = $this->helper_sortable_heading(get_string('name'),
564 'name', $badges->sort, $badges->dir);
565 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'),
566 'status', $badges->sort, $badges->dir);
567 $table->head = array(
568 $sortbyname,
569 $sortbystatus,
570 get_string('bcriteria', 'badges'),
571 get_string('awards', 'badges'),
572 get_string('actions')
573 );
574 $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions');
575
576 foreach ($badges->badges as $b) {
577 $style = !$b->is_active() ? array('class' => 'dimmed') : array();
578 $forlink = print_badge_image($b, $this->page->context) . ' ' .
579 html_writer::start_tag('span') . $b->name . html_writer::end_tag('span');
580 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style);
581 $status = $b->statstring;
582 $criteria = self::print_badge_criteria($b, 'short');
583
584 if (has_capability('moodle/badges:viewawarded', $this->page->context)) {
585 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards);
586 } else {
587 $awards = $b->awards;
588 }
589
590 $actions = self::print_badge_table_actions($b, $this->page->context);
591
592 $row = array($name, $status, $criteria, $awards, $actions);
593 $table->data[] = $row;
594 }
595 $htmltable = html_writer::table($table);
596
597 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar;
598 }
599
600 // Prints tabs for badge editing.
601 public function print_badge_tabs($badgeid, $context, $current = 'overview') {
602 global $DB;
603
a5f82c5b 604 $row = array();
27806552
YB
605
606 $row[] = new tabobject('overview',
607 new moodle_url('/badges/overview.php', array('id' => $badgeid)),
608 get_string('boverview', 'badges')
609 );
610
611 if (has_capability('moodle/badges:configuredetails', $context)) {
612 $row[] = new tabobject('details',
613 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'details')),
614 get_string('bdetails', 'badges')
615 );
616 }
617
618 if (has_capability('moodle/badges:configurecriteria', $context)) {
619 $row[] = new tabobject('criteria',
620 new moodle_url('/badges/criteria.php', array('id' => $badgeid)),
621 get_string('bcriteria', 'badges')
622 );
623 }
624
625 if (has_capability('moodle/badges:configuremessages', $context)) {
626 $row[] = new tabobject('message',
627 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')),
628 get_string('bmessage', 'badges')
629 );
630 }
631
632 if (has_capability('moodle/badges:viewawarded', $context)) {
633 $awarded = $DB->count_records('badge_issued', array('badgeid' => $badgeid));
634 $row[] = new tabobject('awards',
635 new moodle_url('/badges/recipients.php', array('id' => $badgeid)),
636 get_string('bawards', 'badges', $awarded)
637 );
638 }
639
a5f82c5b 640 echo $this->tabtree($row, $current);
27806552
YB
641 }
642
643 // Prints badge status box.
644 public function print_badge_status_box(badge $badge) {
645 $table = new html_table();
646 $table->attributes['class'] = 'boxaligncenter statustable';
647
648 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) {
649 if (!$badge->has_criteria()) {
650 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id));
651 $status = get_string('nocriteria', 'badges');
652 if ($this->page->url != $criteriaurl) {
653 $action = $this->output->single_button(
654 $criteriaurl,
655 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge'));
656 } else {
657 $action = '';
658 }
659 $row = array($status, $action);
660 } else {
661 $status = get_string('statusmessage_' . $badge->status, 'badges');
662 if ($badge->is_active()) {
663 $action = $this->output->single_button(new moodle_url('/badges/action.php',
664 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(),
665 'return' => $this->page->url->out_as_local_url(false))),
666 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge'));
667 } else {
668 $action = $this->output->single_button(new moodle_url('/badges/action.php',
669 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(),
670 'return' => $this->page->url->out_as_local_url(false))),
671 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge'));
672 }
673 $row = array($status . $this->output->help_icon('status', 'badges'), $action);
674 }
675 }
676
677 $table->data[] = $row;
678
679 $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive';
680 return $this->output->box(html_writer::table($table), $style);
681 }
682
683 // Prints badge criteria.
684 public function print_badge_criteria(badge $badge, $short = '') {
685 $output = "";
686 $agg = $badge->get_aggregation_methods();
687 if (empty($badge->criteria)) {
688 return get_string('nocriteria', 'badges');
689 } else if (count($badge->criteria) == 2) {
690 if (!$short) {
691 $output .= get_string('criteria_descr', 'badges');
692 }
693 } else {
694 $output .= get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
695 strtoupper($agg[$badge->get_aggregation_method()]));
696 }
697 $items = array();
698 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
699 foreach ($badge->criteria as $type => $c) {
700 if (count($c->params) == 1) {
701 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') . $c->get_details($short);
702 } else {
703 $items[] = get_string('criteria_descr_' . $short . $type , 'badges',
704 strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details($short);
705 }
706 }
707 $output .= html_writer::alist($items, array(), 'ul');
708 return $output;
709 }
710
711 // Prints criteria actions for badge editing.
712 public function print_criteria_actions(badge $badge) {
713 $table = new html_table();
714 $table->attributes = array('class' => 'boxaligncenter', 'id' => 'badgeactions');
715 $table->colclasses = array('activatebadge');
716
717 $actions = array();
718 if (!$badge->is_active() && !$badge->is_locked()) {
719 $accepted = $badge->get_accepted_criteria();
720 $potential = array_diff($accepted, array_keys($badge->criteria));
721
722 if (!empty($potential)) {
723 foreach ($potential as $p) {
724 if ($p != 0) {
725 $select[$p] = get_string('criteria_' . $p, 'badges');
726 }
727 }
728 $actions[] = get_string('addbadgecriteria', 'badges');
729 $actions[] = $this->output->single_select(new moodle_url('/badges/criteria_settings.php',
730 array('badgeid' => $badge->id, 'add' => true)), 'type', $select);
731 } else {
732 $actions[] = $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix');
733 }
734 }
735
736 $table->data[] = $actions;
737 return html_writer::table($table);
738 }
739
740 // Renders a table with users who have earned the badge.
741 // Based on stamps collection plugin.
742 protected function render_badge_recipients(badge_recipients $recipients) {
743 $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page');
744 $htmlpagingbar = $this->render($paging);
745 $table = new html_table();
746 $table->attributes['class'] = 'generaltable generalbox boxaligncenter boxwidthwide';
747
748 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'),
749 'firstname', $recipients->sort, $recipients->dir);
750 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'),
751 'lastname', $recipients->sort, $recipients->dir);
752 if ($this->helper_fullname_format() == 'lf') {
753 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
754 } else {
755 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
756 }
757
758 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'),
759 'dateissued', $recipients->sort, $recipients->dir);
760
761 $table->head = array($sortbyname, $sortbydate, '');
762
763 foreach ($recipients->userids as $holder) {
764 $fullname = fullname($holder);
765 $fullname = html_writer::link(
766 new moodle_url('/user/profile.php', array('id' => $holder->userid)),
767 $fullname
768 );
769 $awarded = userdate($holder->dateissued);
770 $badgeurl = html_writer::link(
771 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)),
772 get_string('viewbadge', 'badges')
773 );
774
775 $row = array($fullname, $awarded, $badgeurl);
776 $table->data[] = $row;
777 }
778
779 $htmltable = html_writer::table($table);
780
781 return $htmlpagingbar . $htmltable . $htmlpagingbar;
782 }
783
784 ////////////////////////////////////////////////////////////////////////////
785 // Helper methods
786 // Reused from stamps collection plugin
787 ////////////////////////////////////////////////////////////////////////////
788
789 /**
790 * Renders a text with icons to sort by the given column
791 *
792 * This is intended for table headings.
793 *
794 * @param string $text The heading text
795 * @param string $sortid The column id used for sorting
796 * @param string $sortby Currently sorted by (column id)
797 * @param string $sorthow Currently sorted how (ASC|DESC)
798 *
799 * @return string
800 */
801 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) {
802 $out = html_writer::tag('span', $text, array('class' => 'text'));
803
804 if (!is_null($sortid)) {
805 if ($sortby !== $sortid || $sorthow !== 'ASC') {
806 $url = new moodle_url($this->page->url);
807 $url->params(array('sort' => $sortid, 'dir' => 'ASC'));
808 $out .= $this->output->action_icon($url,
809 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort')));
810 }
811 if ($sortby !== $sortid || $sorthow !== 'DESC') {
812 $url = new moodle_url($this->page->url);
813 $url->params(array('sort' => $sortid, 'dir' => 'DESC'));
814 $out .= $this->output->action_icon($url,
815 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort')));
816 }
817 }
818 return $out;
819 }
820 /**
821 * Tries to guess the fullname format set at the site
822 *
823 * @return string fl|lf
824 */
825 protected function helper_fullname_format() {
826 $fake = new stdClass();
827 $fake->lastname = 'LLLL';
828 $fake->firstname = 'FFFF';
829 $fullname = get_string('fullnamedisplay', '', $fake);
830 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
831 return 'lf';
832 } else {
833 return 'fl';
834 }
835 }
836 /**
837 * Renders a search form
838 *
839 * @param string $search Search string
840 * @return string HTML
841 */
842 protected function helper_search_form($search) {
843 global $CFG;
844 require_once($CFG->libdir . '/formslib.php');
845
846 $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url);
847
848 $mform->addElement('hidden', 'sesskey', sesskey());
849
850 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20));
851 $mform->setDefault('search', $search);
852 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search'));
853 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear'));
854 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false);
855
856 ob_start();
857 $mform->display();
858 $out = ob_get_clean();
859
860 return $out;
861 }
862}
863
864/**
865 * An issued badges for badge.php page
866 */
867class issued_badge implements renderable {
868 /** @var issued badge */
869 public $issued;
870
871 /** @var badge recipient */
872 public $recipient = 0;
873
874 /** @var badge visibility to others */
875 public $visible = 0;
876
877 /** @var badge class */
878 public $badgeid = 0;
879
880 /** @var issued badge unique hash */
881 public $hash = "";
882
883 /**
884 * Initializes the badge to display
885 *
886 * @param string $hash Issued badge hash
887 */
888 public function __construct($hash) {
889 global $DB;
890 $this->issued = badges_get_issued_badge_info($hash);
891 $this->hash = $hash;
892
893 $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
894 FROM {badge_issued}
895 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
896 array('hash' => $hash), IGNORE_MISSING);
897 if ($rec) {
898 $this->recipient = $rec->userid;
899 $this->visible = $rec->visible;
900 $this->badgeid = $rec->badgeid;
901 }
902 }
903}
904
905/**
906 * An external badges for external.php page
907 */
908class external_badge implements renderable {
909 /** @var issued badge */
910 public $issued;
911
912 /**
913 * Initializes the badge to display
914 *
915 * @param string $json External badge information.
916 */
917 public function __construct($json) {
918 $this->issued = $json;
919 }
920}
921
922/**
923 * Badge recipients rendering class
924 */
925class badge_recipients implements renderable {
926 /** @var string how are the data sorted */
927 public $sort = 'lastname';
928
929 /** @var string how are the data sorted */
930 public $dir = 'ASC';
931
932 /** @var int page number to display */
933 public $page = 0;
934
935 /** @var int number of badge recipients to display per page */
936 public $perpage = 30;
937
938 /** @var int the total number or badge recipients to display */
939 public $totalcount = null;
940
941 /** @var array internal list of badge recipients ids */
942 public $userids = array();
943 /**
944 * Initializes the list of users to display
945 *
946 * @param array $holders List of badge holders
947 */
948 public function __construct($holders) {
949 $this->userids = $holders;
950 }
951}
952
953/**
954 * Collection of all badges for view.php page
955 */
956class badge_collection implements renderable {
957
958 /** @var string how are the data sorted */
959 public $sort = 'name';
960
961 /** @var string how are the data sorted */
962 public $dir = 'ASC';
963
964 /** @var int page number to display */
965 public $page = 0;
966
967 /** @var int number of badges to display per page */
968 public $perpage = BADGE_PERPAGE;
969
970 /** @var int the total number of badges to display */
971 public $totalcount = null;
972
973 /** @var array list of badges */
974 public $badges = array();
975
976 /**
977 * Initializes the list of badges to display
978 *
979 * @param array $badges Badges to render
980 */
981 public function __construct($badges) {
982 $this->badges = $badges;
983 }
984}
985
986/**
987 * Collection of badges used at the index.php page
988 */
989class badge_management extends badge_collection implements renderable {
990}
991
992/**
993 * Collection of user badges used at the mybadges.php page
994 */
995class badge_user_collection extends badge_collection implements renderable {
996 /** @var array backpack settings */
997 public $backpack;
998
999 /** @var string search */
1000 public $search = '';
1001
1002 /**
1003 * Initializes user badge collection.
1004 *
1005 * @param array $badges Badges to render
1006 * @param int $userid Badges owner
1007 */
1008 public function __construct($badges, $userid) {
1009 parent::__construct($badges);
1010 $this->backpack = get_backpack_settings($userid);
1011 }
1012}