Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
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 27 defined('MOODLE_INTERNAL') || die(); 28 29 require_once($CFG->libdir . '/badgeslib.php'); 30 require_once($CFG->libdir . '/tablelib.php'); 31 32 /** 33 * Standard HTML output renderer for badges 34 */ 35 class core_badges_renderer extends plugin_renderer_base { 36 37 // Outputs badges list. 38 public function print_badges_list($badges, $userid, $profile = false, $external = false) { 39 global $USER, $CFG; 40 foreach ($badges as $badge) { 41 if (!$external) { 42 $context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid); 43 $bname = $badge->name; 44 $imageurl = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', 'f3', false); 45 } else { 46 $bname = ''; 47 $imageurl = ''; 48 if (!empty($badge->name)) { 49 $bname = s($badge->name); 50 } 51 if (!empty($badge->image)) { 52 if (is_object($badge->image)) { 53 if (!empty($badge->image->caption)) { 54 $badge->imagecaption = $badge->image->caption; 55 } 56 $imageurl = $badge->image->id; 57 } else { 58 $imageurl = $badge->image; 59 } 60 } 61 if (isset($badge->assertion->badge->name)) { 62 $bname = s($badge->assertion->badge->name); 63 } 64 if (isset($badge->imageUrl)) { 65 $imageurl = $badge->imageUrl; 66 } 67 } 68 69 $name = html_writer::tag('span', $bname, array('class' => 'badge-name')); 70 71 $imagecaption = $badge->imagecaption ?? ''; 72 $image = html_writer::empty_tag('img', ['src' => $imageurl, 'class' => 'badge-image', 'alt' => $imagecaption]); 73 if (!empty($badge->dateexpire) && $badge->dateexpire < time()) { 74 $image .= $this->output->pix_icon('i/expired', 75 get_string('expireddate', 'badges', userdate($badge->dateexpire)), 76 'moodle', 77 array('class' => 'expireimage')); 78 $name .= '(' . get_string('expired', 'badges') . ')'; 79 } 80 81 $download = $status = $push = ''; 82 if (($userid == $USER->id) && !$profile) { 83 $params = array( 84 'download' => $badge->id, 85 'hash' => $badge->uniquehash, 86 'sesskey' => sesskey() 87 ); 88 $url = new moodle_url( 89 'mybadges.php', 90 $params 91 ); 92 $notexpiredbadge = (empty($badge->dateexpire) || $badge->dateexpire > time()); 93 $userbackpack = badges_get_user_backpack(); 94 if (!empty($CFG->badges_allowexternalbackpack) && $notexpiredbadge && $userbackpack) { 95 $assertion = new moodle_url('/badges/assertion.php', array('b' => $badge->uniquehash)); 96 $icon = new pix_icon('t/backpack', get_string('addtobackpack', 'badges')); 97 if (badges_open_badges_backpack_api($userbackpack->id) == OPEN_BADGES_V2) { 98 $addurl = new moodle_url('/badges/backpack-add.php', array('hash' => $badge->uniquehash)); 99 $push = $this->output->action_icon($addurl, $icon); 100 } else if (badges_open_badges_backpack_api($userbackpack->id) == OPEN_BADGES_V2P1) { 101 $addurl = new moodle_url('/badges/backpack-export.php', array('hash' => $badge->uniquehash)); 102 $push = $this->output->action_icon($addurl, $icon); 103 } 104 } 105 106 $download = $this->output->action_icon($url, new pix_icon('t/download', get_string('download'))); 107 if ($badge->visible) { 108 $url = new moodle_url('mybadges.php', array('hide' => $badge->issuedid, 'sesskey' => sesskey())); 109 $status = $this->output->action_icon($url, new pix_icon('t/hide', get_string('makeprivate', 'badges'))); 110 } else { 111 $url = new moodle_url('mybadges.php', array('show' => $badge->issuedid, 'sesskey' => sesskey())); 112 $status = $this->output->action_icon($url, new pix_icon('t/show', get_string('makepublic', 'badges'))); 113 } 114 } 115 116 if (!$profile) { 117 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash)); 118 } else { 119 if (!$external) { 120 $url = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash)); 121 } else { 122 $hash = hash('md5', $badge->hostedUrl); 123 $url = new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid)); 124 } 125 } 126 $actions = html_writer::tag('div', $push . $download . $status, array('class' => 'badge-actions')); 127 $items[] = html_writer::link($url, $image . $actions . $name, array('title' => $bname)); 128 } 129 130 return html_writer::alist($items, array('class' => 'badges')); 131 } 132 133 // Recipients selection form. 134 public function recipients_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) { 135 $output = ''; 136 $formattributes = array(); 137 $formattributes['id'] = 'recipientform'; 138 $formattributes['action'] = $this->page->url; 139 $formattributes['method'] = 'post'; 140 $output .= html_writer::start_tag('form', $formattributes); 141 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())); 142 143 $existingcell = new html_table_cell(); 144 $existingcell->text = $existinguc->display(true); 145 $existingcell->attributes['class'] = 'existing'; 146 $actioncell = new html_table_cell(); 147 $actioncell->text = html_writer::start_tag('div', array()); 148 $actioncell->text .= html_writer::empty_tag('input', array( 149 'type' => 'submit', 150 'name' => 'award', 151 'value' => $this->output->larrow() . ' ' . get_string('award', 'badges'), 152 'class' => 'actionbutton btn btn-secondary') 153 ); 154 $actioncell->text .= html_writer::empty_tag('input', array( 155 'type' => 'submit', 156 'name' => 'revoke', 157 'value' => get_string('revoke', 'badges') . ' ' . $this->output->rarrow(), 158 'class' => 'actionbutton btn btn-secondary') 159 ); 160 $actioncell->text .= html_writer::end_tag('div', array()); 161 $actioncell->attributes['class'] = 'actions'; 162 $potentialcell = new html_table_cell(); 163 $potentialcell->text = $potentialuc->display(true); 164 $potentialcell->attributes['class'] = 'potential'; 165 166 $table = new html_table(); 167 $table->attributes['class'] = 'recipienttable boxaligncenter'; 168 $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell))); 169 $output .= html_writer::table($table); 170 171 $output .= html_writer::end_tag('form'); 172 return $output; 173 } 174 175 // Prints a badge overview infomation. 176 public function print_badge_overview($badge, $context) { 177 $display = ""; 178 $languages = get_string_manager()->get_list_of_languages(); 179 180 // Badge details. 181 $display .= $this->heading(get_string('badgedetails', 'badges'), 3); 182 $dl = array(); 183 $dl[get_string('name')] = $badge->name; 184 $dl[get_string('version', 'badges')] = $badge->version; 185 $dl[get_string('language')] = $languages[$badge->language]; 186 $dl[get_string('description', 'badges')] = $badge->description; 187 $dl[get_string('createdon', 'search')] = userdate($badge->timecreated); 188 $dl[get_string('badgeimage', 'badges')] = print_badge_image($badge, $context, 'large'); 189 $dl[get_string('imageauthorname', 'badges')] = $badge->imageauthorname; 190 $dl[get_string('imageauthoremail', 'badges')] = 191 html_writer::tag('a', $badge->imageauthoremail, array('href' => 'mailto:' . $badge->imageauthoremail)); 192 $dl[get_string('imageauthorurl', 'badges')] = 193 html_writer::link($badge->imageauthorurl, $badge->imageauthorurl, array('target' => '_blank')); 194 $dl[get_string('imagecaption', 'badges')] = $badge->imagecaption; 195 $tags = \core_tag_tag::get_item_tags('core_badges', 'badge', $badge->id); 196 $dl[get_string('tags', 'badges')] = $this->output->tag_list($tags, ''); 197 $display .= $this->definition_list($dl); 198 199 // Issuer details. 200 $display .= $this->heading(get_string('issuerdetails', 'badges'), 3); 201 $dl = array(); 202 $dl[get_string('issuername', 'badges')] = $badge->issuername; 203 $dl[get_string('contact', 'badges')] = html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact)); 204 $display .= $this->definition_list($dl); 205 206 // Issuance details if any. 207 $display .= $this->heading(get_string('issuancedetails', 'badges'), 3); 208 if ($badge->can_expire()) { 209 if ($badge->expiredate) { 210 $display .= get_string('expiredate', 'badges', userdate($badge->expiredate)); 211 } else if ($badge->expireperiod) { 212 if ($badge->expireperiod < 60) { 213 $display .= get_string('expireperiods', 'badges', round($badge->expireperiod, 2)); 214 } else if ($badge->expireperiod < 60 * 60) { 215 $display .= get_string('expireperiodm', 'badges', round($badge->expireperiod / 60, 2)); 216 } else if ($badge->expireperiod < 60 * 60 * 24) { 217 $display .= get_string('expireperiodh', 'badges', round($badge->expireperiod / 60 / 60, 2)); 218 } else { 219 $display .= get_string('expireperiod', 'badges', round($badge->expireperiod / 60 / 60 / 24, 2)); 220 } 221 } 222 } else { 223 $display .= get_string('noexpiry', 'badges'); 224 } 225 226 // Criteria details if any. 227 $display .= $this->heading(get_string('bcriteria', 'badges'), 3); 228 if ($badge->has_criteria()) { 229 $display .= self::print_badge_criteria($badge); 230 } else { 231 $display .= get_string('nocriteria', 'badges'); 232 if (has_capability('moodle/badges:configurecriteria', $context)) { 233 $display .= $this->output->single_button( 234 new moodle_url('/badges/criteria.php', array('id' => $badge->id)), 235 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge')); 236 } 237 } 238 239 // Awards details if any. 240 if (has_capability('moodle/badges:viewawarded', $context)) { 241 $display .= $this->heading(get_string('awards', 'badges'), 3); 242 if ($badge->has_awards()) { 243 $url = new moodle_url('/badges/recipients.php', array('id' => $badge->id)); 244 $a = new stdClass(); 245 $a->link = $url->out(); 246 $a->count = count($badge->get_awards()); 247 $display .= get_string('numawards', 'badges', $a); 248 } else { 249 $display .= get_string('noawards', 'badges'); 250 } 251 252 if (has_capability('moodle/badges:awardbadge', $context) && 253 $badge->has_manual_award_criteria() && 254 $badge->is_active()) { 255 $display .= $this->output->single_button( 256 new moodle_url('/badges/award.php', array('id' => $badge->id)), 257 get_string('award', 'badges'), 'POST', array('class' => 'activatebadge')); 258 } 259 } 260 261 $display .= self::print_badge_endorsement($badge); 262 $display .= self::print_badge_related($badge); 263 $display .= self::print_badge_alignments($badge); 264 265 return html_writer::div($display, null, array('id' => 'badge-overview')); 266 } 267 268 /** 269 * Prints action icons for the badge. 270 * 271 * @deprecated sinde Moodle 4.3 272 * @param \core_badges\badge $badge 273 * @param \context $context 274 * @return string 275 */ 276 public function print_badge_table_actions($badge, $context) { 277 debugging("print_badge_table_actions() is deprecated.", DEBUG_DEVELOPER); 278 $actions = ""; 279 280 if (has_capability('moodle/badges:configuredetails', $context) && $badge->has_criteria()) { 281 // Activate/deactivate badge. 282 if ($badge->status == BADGE_STATUS_INACTIVE || $badge->status == BADGE_STATUS_INACTIVE_LOCKED) { 283 // "Activate" will go to another page and ask for confirmation. 284 $url = new moodle_url('/badges/action.php'); 285 $url->param('id', $badge->id); 286 $url->param('activate', true); 287 $url->param('sesskey', sesskey()); 288 $return = new moodle_url(qualified_me()); 289 $url->param('return', $return->out_as_local_url(false)); 290 $actions .= $this->output->action_icon($url, new pix_icon('t/show', get_string('activate', 'badges'))) . " "; 291 } else { 292 $url = new moodle_url(qualified_me()); 293 $url->param('lock', $badge->id); 294 $url->param('sesskey', sesskey()); 295 $actions .= $this->output->action_icon($url, new pix_icon('t/hide', get_string('deactivate', 'badges'))) . " "; 296 } 297 } 298 299 // Award badge manually. 300 if ($badge->has_manual_award_criteria() && 301 has_capability('moodle/badges:awardbadge', $context) && 302 $badge->is_active()) { 303 $url = new moodle_url('/badges/award.php', array('id' => $badge->id)); 304 $actions .= $this->output->action_icon($url, new pix_icon('t/award', get_string('award', 'badges'))) . " "; 305 } 306 307 // Edit badge. 308 if (has_capability('moodle/badges:configuredetails', $context)) { 309 $url = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => 'badge')); 310 $actions .= $this->output->action_icon($url, new pix_icon('t/edit', get_string('edit'))) . " "; 311 } 312 313 // Duplicate badge. 314 if (has_capability('moodle/badges:createbadge', $context)) { 315 $url = new moodle_url('/badges/action.php', array('copy' => '1', 'id' => $badge->id, 'sesskey' => sesskey())); 316 $actions .= $this->output->action_icon($url, new pix_icon('t/copy', get_string('copy'))) . " "; 317 } 318 319 // Delete badge. 320 if (has_capability('moodle/badges:deletebadge', $context)) { 321 $url = new moodle_url(qualified_me()); 322 $url->param('delete', $badge->id); 323 $actions .= $this->output->action_icon($url, new pix_icon('t/delete', get_string('delete'))) . " "; 324 } 325 326 return $actions; 327 } 328 329 /** 330 * Render an issued badge. 331 * 332 * @param \core_badges\output\issued_badge $ibadge 333 * @return string 334 */ 335 protected function render_issued_badge(\core_badges\output\issued_badge $ibadge) { 336 $data = $ibadge->export_for_template($this); 337 return parent::render_from_template('core_badges/issued_badge', $data); 338 } 339 340 /** 341 * Render an issued badge. 342 * 343 * @param \core_badges\output\badgeclass $badge 344 * @return string 345 */ 346 protected function render_badgeclass(\core_badges\output\badgeclass $badge) { 347 $data = $badge->export_for_template($this); 348 return parent::render_from_template('core_badges/issued_badge', $data); 349 } 350 351 /** 352 * Render an external badge. 353 * 354 * @param \core_badges\output\external_badge $ibadge 355 * @return string 356 */ 357 protected function render_external_badge(\core_badges\output\external_badge $ibadge) { 358 $data = $ibadge->export_for_template($this); 359 return parent::render_from_template('core_badges/issued_badge', $data); 360 } 361 362 /** 363 * Render a collection of user badges. 364 * 365 * @param \core_badges\output\badge_user_collection $badges 366 * @return string 367 */ 368 protected function render_badge_user_collection(\core_badges\output\badge_user_collection $badges) { 369 global $CFG, $USER, $SITE; 370 $backpack = $badges->backpack; 371 $mybackpack = new moodle_url('/badges/mybackpack.php'); 372 373 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page'); 374 $htmlpagingbar = $this->render($paging); 375 376 // Set backpack connection string. 377 $backpackconnect = ''; 378 if (!empty($CFG->badges_allowexternalbackpack) && is_null($backpack)) { 379 $backpackconnect = $this->output->box(get_string('localconnectto', 'badges', $mybackpack->out()), 'noticebox'); 380 } 381 // Search box. 382 $searchform = $this->output->box($this->helper_search_form($badges->search), 'boxwidthwide boxaligncenter'); 383 384 // Download all button. 385 $actionhtml = $this->output->single_button( 386 new moodle_url('/badges/mybadges.php', array('downloadall' => true, 'sesskey' => sesskey())), 387 get_string('downloadall'), 'POST', array('class' => 'activatebadge')); 388 $downloadall = $this->output->box('', 'col-md-3'); 389 $downloadall .= $this->output->box($actionhtml, 'col-md-9'); 390 $downloadall = $this->output->box($downloadall, 'row ml-5'); 391 392 // Local badges. 393 $localhtml = html_writer::start_tag('div', array('id' => 'issued-badge-table', 'class' => 'generalbox')); 394 $sitename = format_string($SITE->fullname, true, array('context' => context_system::instance())); 395 $heading = get_string('localbadges', 'badges', $sitename); 396 $localhtml .= $this->output->heading_with_help($heading, 'localbadgesh', 'badges'); 397 if ($badges->badges) { 398 $countmessage = $this->output->box(get_string('badgesearned', 'badges', $badges->totalcount)); 399 400 $htmllist = $this->print_badges_list($badges->badges, $USER->id); 401 $localhtml .= $backpackconnect . $countmessage . $searchform; 402 $localhtml .= $htmlpagingbar . $htmllist . $htmlpagingbar . $downloadall; 403 } else { 404 $localhtml .= $searchform . $this->output->notification(get_string('nobadges', 'badges'), 'info'); 405 } 406 $localhtml .= html_writer::end_tag('div'); 407 408 // External badges. 409 $externalhtml = ""; 410 if (!empty($CFG->badges_allowexternalbackpack)) { 411 $externalhtml .= html_writer::start_tag('div', array('class' => 'generalbox')); 412 $externalhtml .= $this->output->heading_with_help(get_string('externalbadges', 'badges'), 'externalbadges', 'badges'); 413 if (!is_null($backpack)) { 414 if ($backpack->totalcollections == 0) { 415 $externalhtml .= get_string('nobackpackcollectionssummary', 'badges', $backpack); 416 } else { 417 if ($backpack->totalbadges == 0) { 418 $externalhtml .= get_string('nobackpackbadgessummary', 'badges', $backpack); 419 } else { 420 $externalhtml .= get_string('backpackbadgessummary', 'badges', $backpack); 421 $externalhtml .= '<br/><br/>' . $this->print_badges_list($backpack->badges, $USER->id, true, true); 422 } 423 } 424 } else { 425 $externalhtml .= get_string('externalconnectto', 'badges', $mybackpack->out()); 426 } 427 428 $externalhtml .= html_writer::end_tag('div'); 429 $attr = ['class' => 'btn btn-secondary']; 430 $label = get_string('backpackbadgessettings', 'badges'); 431 $backpacksettings = html_writer::link(new moodle_url('/badges/mybackpack.php'), $label, $attr); 432 $actionshtml = $this->output->box('', 'col-md-3'); 433 $actionshtml .= $this->output->box($backpacksettings, 'col-md-9'); 434 $actionshtml = $this->output->box($actionshtml, 'row ml-5'); 435 $externalhtml .= $actionshtml; 436 } 437 438 return $localhtml . $externalhtml; 439 } 440 441 /** 442 * Render a collection of badges. 443 * 444 * @param \core_badges\output\badge_collection $badges 445 * @return string 446 */ 447 protected function render_badge_collection(\core_badges\output\badge_collection $badges) { 448 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page'); 449 $htmlpagingbar = $this->render($paging); 450 $table = new html_table(); 451 $table->attributes['class'] = 'table table-bordered table-striped'; 452 453 $sortbyname = $this->helper_sortable_heading(get_string('name'), 454 'name', $badges->sort, $badges->dir); 455 $sortbyawarded = $this->helper_sortable_heading(get_string('awardedtoyou', 'badges'), 456 'dateissued', $badges->sort, $badges->dir); 457 $table->head = array( 458 get_string('badgeimage', 'badges'), 459 $sortbyname, 460 get_string('description', 'badges'), 461 get_string('bcriteria', 'badges'), 462 $sortbyawarded 463 ); 464 $table->colclasses = array('badgeimage', 'name', 'description', 'criteria', 'awards'); 465 466 foreach ($badges->badges as $badge) { 467 $badgeimage = print_badge_image($badge, $this->page->context, 'large'); 468 $name = $badge->name; 469 $description = $badge->description; 470 $criteria = self::print_badge_criteria($badge); 471 if ($badge->dateissued) { 472 $icon = new pix_icon('i/valid', 473 get_string('dateearned', 'badges', 474 userdate($badge->dateissued, get_string('strftimedatefullshort', 'core_langconfig')))); 475 $badgeurl = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash)); 476 $awarded = $this->output->action_icon($badgeurl, $icon, null, null, true); 477 } else { 478 $awarded = ""; 479 } 480 $row = array($badgeimage, $name, $description, $criteria, $awarded); 481 $table->data[] = $row; 482 } 483 484 $htmltable = html_writer::table($table); 485 486 return $htmlpagingbar . $htmltable . $htmlpagingbar; 487 } 488 489 /** 490 * Render a table of badges. 491 * 492 * @deprecated since Moodle 4.3 493 * @param \core_badges\output\badge_management $badges 494 * @return string 495 */ 496 protected function render_badge_management(\core_badges\output\badge_management $badges) { 497 debugging("render_badge_management() is deprecated.", DEBUG_DEVELOPER); 498 $paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page'); 499 500 // New badge button. 501 $htmlnew = ''; 502 $htmlpagingbar = $this->render($paging); 503 $table = new html_table(); 504 $table->attributes['class'] = 'table table-bordered table-striped'; 505 506 $sortbyname = $this->helper_sortable_heading(get_string('name'), 507 'name', $badges->sort, $badges->dir); 508 $sortbystatus = $this->helper_sortable_heading(get_string('status', 'badges'), 509 'status', $badges->sort, $badges->dir); 510 $table->head = array( 511 $sortbyname, 512 $sortbystatus, 513 get_string('bcriteria', 'badges'), 514 get_string('awards', 'badges'), 515 get_string('actions') 516 ); 517 $table->colclasses = array('name', 'status', 'criteria', 'awards', 'actions'); 518 519 foreach ($badges->badges as $b) { 520 $style = !$b->is_active() ? array('class' => 'dimmed') : array(); 521 $forlink = print_badge_image($b, $this->page->context) . ' ' . 522 html_writer::start_tag('span') . $b->name . html_writer::end_tag('span'); 523 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $b->id)), $forlink, $style); 524 $status = $b->statstring; 525 $criteria = self::print_badge_criteria($b, 'short'); 526 527 if (has_capability('moodle/badges:viewawarded', $this->page->context)) { 528 $awards = html_writer::link(new moodle_url('/badges/recipients.php', array('id' => $b->id)), $b->awards); 529 } else { 530 $awards = $b->awards; 531 } 532 533 $actions = self::print_badge_table_actions($b, $this->page->context); 534 535 $row = array($name, $status, $criteria, $awards, $actions); 536 $table->data[] = $row; 537 } 538 $htmltable = html_writer::table($table); 539 540 return $htmlnew . $htmlpagingbar . $htmltable . $htmlpagingbar; 541 } 542 543 /** 544 * Prints tabs for badge editing. 545 * 546 * @deprecated since Moodle 4.0 547 * @todo MDL-73426 Final deprecation. 548 * @param integer $badgeid The badgeid to edit. 549 * @param context $context The current context. 550 * @param string $current The currently selected tab. 551 * @return string 552 */ 553 public function print_badge_tabs($badgeid, $context, $current = 'overview') { 554 global $DB; 555 debugging("print_badge_tabs() is deprecated. " . 556 "This is replaced with the manage_badge_action_bar tertiary navigation.", DEBUG_DEVELOPER); 557 558 $badge = new badge($badgeid); 559 $row = array(); 560 561 $row[] = new tabobject('overview', 562 new moodle_url('/badges/overview.php', array('id' => $badgeid)), 563 get_string('boverview', 'badges') 564 ); 565 566 if (has_capability('moodle/badges:configuredetails', $context)) { 567 $row[] = new tabobject('badge', 568 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'badge')), 569 get_string('bdetails', 'badges') 570 ); 571 } 572 573 if (has_capability('moodle/badges:configurecriteria', $context)) { 574 $row[] = new tabobject('criteria', 575 new moodle_url('/badges/criteria.php', array('id' => $badgeid)), 576 get_string('bcriteria', 'badges') 577 ); 578 } 579 580 if (has_capability('moodle/badges:configuremessages', $context)) { 581 $row[] = new tabobject('message', 582 new moodle_url('/badges/edit.php', array('id' => $badgeid, 'action' => 'message')), 583 get_string('bmessage', 'badges') 584 ); 585 } 586 587 if (has_capability('moodle/badges:viewawarded', $context)) { 588 $awarded = $DB->count_records_sql('SELECT COUNT(b.userid) 589 FROM {badge_issued} b INNER JOIN {user} u ON b.userid = u.id 590 WHERE b.badgeid = :badgeid AND u.deleted = 0', array('badgeid' => $badgeid)); 591 $row[] = new tabobject('awards', 592 new moodle_url('/badges/recipients.php', array('id' => $badgeid)), 593 get_string('bawards', 'badges', $awarded) 594 ); 595 } 596 597 if (has_capability('moodle/badges:configuredetails', $context)) { 598 $row[] = new tabobject('bendorsement', 599 new moodle_url('/badges/endorsement.php', array('id' => $badgeid)), 600 get_string('bendorsement', 'badges') 601 ); 602 } 603 604 if (has_capability('moodle/badges:configuredetails', $context)) { 605 $sql = "SELECT COUNT(br.badgeid) 606 FROM {badge_related} br 607 WHERE (br.badgeid = :badgeid OR br.relatedbadgeid = :badgeid2)"; 608 $related = $DB->count_records_sql($sql, ['badgeid' => $badgeid, 'badgeid2' => $badgeid]); 609 $row[] = new tabobject('brelated', 610 new moodle_url('/badges/related.php', array('id' => $badgeid)), 611 get_string('brelated', 'badges', $related) 612 ); 613 } 614 615 if (has_capability('moodle/badges:configuredetails', $context)) { 616 $alignments = $DB->count_records_sql("SELECT COUNT(bc.id) 617 FROM {badge_alignment} bc WHERE bc.badgeid = :badgeid", array('badgeid' => $badgeid)); 618 $row[] = new tabobject('alignment', 619 new moodle_url('/badges/alignment.php', array('id' => $badgeid)), 620 get_string('balignment', 'badges', $alignments) 621 ); 622 } 623 624 echo $this->tabtree($row, $current); 625 } 626 627 /** 628 * Prints badge status box. 629 * 630 * @param badge $badge 631 * @return Either the status box html as a string or null 632 */ 633 public function print_badge_status_box(badge $badge) { 634 if (has_capability('moodle/badges:configurecriteria', $badge->get_context())) { 635 636 if (!$badge->has_criteria()) { 637 $criteriaurl = new moodle_url('/badges/criteria.php', array('id' => $badge->id)); 638 $status = get_string('nocriteria', 'badges'); 639 if ($this->page->url != $criteriaurl) { 640 $action = $this->output->single_button( 641 $criteriaurl, 642 get_string('addcriteria', 'badges'), 'POST', array('class' => 'activatebadge')); 643 } else { 644 $action = ''; 645 } 646 647 $message = $status . $action; 648 } else { 649 $status = get_string('statusmessage_' . $badge->status, 'badges'); 650 if ($badge->is_active()) { 651 $action = $this->output->single_button(new moodle_url('/badges/action.php', 652 array('id' => $badge->id, 'lock' => 1, 'sesskey' => sesskey(), 653 'return' => $this->page->url->out_as_local_url(false))), 654 get_string('deactivate', 'badges'), 'POST', array('class' => 'activatebadge')); 655 } else { 656 $action = $this->output->single_button(new moodle_url('/badges/action.php', 657 array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(), 658 'return' => $this->page->url->out_as_local_url(false))), 659 get_string('activate', 'badges'), 'POST', array('class' => 'activatebadge')); 660 } 661 662 $message = $status . $this->output->help_icon('status', 'badges') . $action; 663 664 } 665 666 $style = $badge->is_active() ? 'generalbox statusbox active' : 'generalbox statusbox inactive'; 667 return $this->output->box($message, $style); 668 } 669 670 return null; 671 } 672 673 /** 674 * Returns information about badge criteria in a list form. 675 * 676 * @param badge $badge Badge objects 677 * @param string $short Indicates whether to print full info about this badge 678 * @return string $output HTML string to output 679 */ 680 public function print_badge_criteria(badge $badge, $short = '') { 681 $agg = $badge->get_aggregation_methods(); 682 if (empty($badge->criteria)) { 683 return get_string('nocriteria', 'badges'); 684 } 685 686 $overalldescr = ''; 687 $overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]; 688 if (!$short && !empty($overall->description)) { 689 $overalldescr = $this->output->box( 690 format_text($overall->description, $overall->descriptionformat, array('context' => $badge->get_context())), 691 'criteria-description' 692 ); 693 } 694 695 // Get the condition string. 696 $condition = ''; 697 if (count($badge->criteria) != 2) { 698 $condition = get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges', 699 core_text::strtoupper($agg[$badge->get_aggregation_method()])); 700 } 701 702 unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]); 703 704 $items = array(); 705 // If only one criterion left, make sure its description goe to the top. 706 if (count($badge->criteria) == 1) { 707 $c = reset($badge->criteria); 708 if (!$short && !empty($c->description)) { 709 $overalldescr = $this->output->box( 710 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())), 711 'criteria-description' 712 ); 713 } 714 if (count($c->params) == 1) { 715 $items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype , 'badges') . 716 $c->get_details($short); 717 } else { 718 $items[] = get_string('criteria_descr_' . $short . $c->criteriatype, 'badges', 719 core_text::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype)])) . 720 $c->get_details($short); 721 } 722 } else { 723 foreach ($badge->criteria as $type => $c) { 724 $criteriadescr = ''; 725 if (!$short && !empty($c->description)) { 726 $criteriadescr = $this->output->box( 727 format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())), 728 'criteria-description' 729 ); 730 } 731 if (count($c->params) == 1) { 732 $items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') . 733 $c->get_details($short) . $criteriadescr; 734 } else { 735 $items[] = get_string('criteria_descr_' . $short . $type , 'badges', 736 core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) . 737 $c->get_details($short) . 738 $criteriadescr; 739 } 740 } 741 } 742 743 return $overalldescr . $condition . html_writer::alist($items, array(), 'ul');; 744 } 745 746 /** 747 * Prints criteria actions for badge editing. 748 * 749 * @param badge $badge 750 * @return string 751 */ 752 public function print_criteria_actions(badge $badge) { 753 $output = ''; 754 if (!$badge->is_active() && !$badge->is_locked()) { 755 $accepted = $badge->get_accepted_criteria(); 756 $potential = array_diff($accepted, array_keys($badge->criteria)); 757 758 if (!empty($potential)) { 759 foreach ($potential as $p) { 760 if ($p != 0) { 761 $select[$p] = get_string('criteria_' . $p, 'badges'); 762 } 763 } 764 $output .= $this->output->single_select( 765 new moodle_url('/badges/criteria_settings.php', array('badgeid' => $badge->id, 'add' => true)), 766 'type', 767 $select, 768 '', 769 array('' => 'choosedots'), 770 null, 771 array('label' => get_string('addbadgecriteria', 'badges')) 772 ); 773 } else { 774 $output .= $this->output->box(get_string('nothingtoadd', 'badges'), 'clearfix'); 775 } 776 } 777 778 return $output; 779 } 780 781 /** 782 * Renders a table with users who have earned the badge. 783 * Based on stamps collection plugin. 784 * 785 * @param \core_badges\output\badge_recipients $recipients 786 * @return string 787 */ 788 protected function render_badge_recipients(\core_badges\output\badge_recipients $recipients) { 789 $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page'); 790 $htmlpagingbar = $this->render($paging); 791 $table = new html_table(); 792 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide'; 793 794 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 795 'firstname', $recipients->sort, $recipients->dir); 796 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 797 'lastname', $recipients->sort, $recipients->dir); 798 if ($this->helper_fullname_format() == 'lf') { 799 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname; 800 } else { 801 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname; 802 } 803 804 $sortbydate = $this->helper_sortable_heading(get_string('dateawarded', 'badges'), 805 'dateissued', $recipients->sort, $recipients->dir); 806 807 $table->head = array($sortbyname, $sortbydate, ''); 808 809 foreach ($recipients->userids as $holder) { 810 $fullname = fullname($holder); 811 $fullname = html_writer::link( 812 new moodle_url('/user/profile.php', array('id' => $holder->userid)), 813 $fullname 814 ); 815 $awarded = userdate($holder->dateissued); 816 $badgeurl = html_writer::link( 817 new moodle_url('/badges/badge.php', array('hash' => $holder->uniquehash)), 818 get_string('viewbadge', 'badges') 819 ); 820 821 $row = array($fullname, $awarded, $badgeurl); 822 $table->data[] = $row; 823 } 824 825 $htmltable = html_writer::table($table); 826 827 return $htmlpagingbar . $htmltable . $htmlpagingbar; 828 } 829 830 //////////////////////////////////////////////////////////////////////////// 831 // Helper methods 832 // Reused from stamps collection plugin 833 //////////////////////////////////////////////////////////////////////////// 834 835 /** 836 * Renders a text with icons to sort by the given column 837 * 838 * This is intended for table headings. 839 * 840 * @param string $text The heading text 841 * @param string $sortid The column id used for sorting 842 * @param string $sortby Currently sorted by (column id) 843 * @param string $sorthow Currently sorted how (ASC|DESC) 844 * 845 * @return string 846 */ 847 protected function helper_sortable_heading($text, $sortid = null, $sortby = null, $sorthow = null) { 848 $out = html_writer::tag('span', $text, array('class' => 'text')); 849 850 if (!is_null($sortid)) { 851 if ($sortby !== $sortid || $sorthow !== 'ASC') { 852 $url = new moodle_url($this->page->url); 853 $url->params(array('sort' => $sortid, 'dir' => 'ASC')); 854 $out .= $this->output->action_icon($url, 855 new pix_icon('t/sort_asc', get_string('sortbyx', 'core', s($text)), null, array('class' => 'iconsort'))); 856 } 857 if ($sortby !== $sortid || $sorthow !== 'DESC') { 858 $url = new moodle_url($this->page->url); 859 $url->params(array('sort' => $sortid, 'dir' => 'DESC')); 860 $out .= $this->output->action_icon($url, 861 new pix_icon('t/sort_desc', get_string('sortbyxreverse', 'core', s($text)), null, array('class' => 'iconsort'))); 862 } 863 } 864 return $out; 865 } 866 /** 867 * Tries to guess the fullname format set at the site 868 * 869 * @return string fl|lf 870 */ 871 protected function helper_fullname_format() { 872 $fake = new stdClass(); 873 $fake->lastname = 'LLLL'; 874 $fake->firstname = 'FFFF'; 875 $fullname = get_string('fullnamedisplay', '', $fake); 876 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) { 877 return 'lf'; 878 } else { 879 return 'fl'; 880 } 881 } 882 /** 883 * Renders a search form 884 * 885 * @param string $search Search string 886 * @return string HTML 887 */ 888 protected function helper_search_form($search) { 889 global $CFG; 890 require_once($CFG->libdir . '/formslib.php'); 891 892 $mform = new MoodleQuickForm('searchform', 'POST', $this->page->url); 893 894 $mform->addElement('hidden', 'sesskey', sesskey()); 895 896 $el[] = $mform->createElement('text', 'search', get_string('search'), array('size' => 20)); 897 $mform->setDefault('search', $search); 898 $el[] = $mform->createElement('submit', 'submitsearch', get_string('search')); 899 $el[] = $mform->createElement('submit', 'clearsearch', get_string('clear')); 900 $mform->addGroup($el, 'searchgroup', get_string('searchname', 'badges'), ' ', false); 901 902 ob_start(); 903 $mform->display(); 904 $out = ob_get_clean(); 905 906 return $out; 907 } 908 909 /** 910 * Renders a definition list 911 * 912 * @param array $items the list of items to define 913 * @param array 914 */ 915 protected function definition_list(array $items, array $attributes = array()) { 916 $output = html_writer::start_tag('dl', $attributes); 917 foreach ($items as $label => $value) { 918 $output .= html_writer::tag('dt', $label); 919 $output .= html_writer::tag('dd', $value); 920 } 921 $output .= html_writer::end_tag('dl'); 922 return $output; 923 } 924 925 /** 926 * Outputs list en badges. 927 * 928 * @param badge $badge Badge object. 929 * @return string $output content endorsement to output. 930 */ 931 protected function print_badge_endorsement(badge $badge) { 932 $output = ''; 933 $endorsement = $badge->get_endorsement(); 934 $dl = array(); 935 $output .= $this->heading(get_string('endorsement', 'badges'), 3); 936 if (!empty($endorsement)) { 937 $dl[get_string('issuername', 'badges')] = $endorsement->issuername; 938 $dl[get_string('issueremail', 'badges')] = 939 html_writer::tag('a', $endorsement->issueremail, array('href' => 'mailto:' . $endorsement->issueremail)); 940 $dl[get_string('issuerurl', 'badges')] = html_writer::link($endorsement->issuerurl, $endorsement->issuerurl, 941 array('target' => '_blank')); 942 $dl[get_string('dateawarded', 'badges')] = userdate($endorsement->dateissued); 943 $dl[get_string('claimid', 'badges')] = html_writer::link($endorsement->claimid, $endorsement->claimid, 944 array('target' => '_blank')); 945 $dl[get_string('claimcomment', 'badges')] = $endorsement->claimcomment; 946 $output .= $this->definition_list($dl); 947 } else { 948 $output .= get_string('noendorsement', 'badges'); 949 } 950 return $output; 951 } 952 953 /** 954 * Print list badges related. 955 * 956 * @param badge $badge Badge objects. 957 * @return string $output List related badges to output. 958 */ 959 protected function print_badge_related(badge $badge) { 960 $output = ''; 961 $relatedbadges = $badge->get_related_badges(); 962 $output .= $this->heading(get_string('relatedbages', 'badges'), 3); 963 if (!empty($relatedbadges)) { 964 $items = array(); 965 foreach ($relatedbadges as $related) { 966 $relatedurl = new moodle_url('/badges/overview.php', array('id' => $related->id)); 967 $items[] = html_writer::link($relatedurl->out(), $related->name, array('target' => '_blank')); 968 } 969 $output .= html_writer::alist($items, array(), 'ul'); 970 } else { 971 $output .= get_string('norelated', 'badges'); 972 } 973 return $output; 974 } 975 976 /** 977 * Print list badge alignments. 978 * 979 * @param badge $badge Badge objects. 980 * @return string $output List alignments to output. 981 */ 982 protected function print_badge_alignments(badge $badge) { 983 $output = ''; 984 $output .= $this->heading(get_string('alignment', 'badges'), 3); 985 $alignments = $badge->get_alignments(); 986 if (!empty($alignments)) { 987 $items = array(); 988 foreach ($alignments as $alignment) { 989 $urlaligment = new moodle_url('alignment.php', 990 array('id' => $badge->id, 'alignmentid' => $alignment->id) 991 ); 992 $items[] = html_writer::link($urlaligment, $alignment->targetname, array('target' => '_blank')); 993 } 994 $output .= html_writer::alist($items, array(), 'ul'); 995 } else { 996 $output .= get_string('noalignment', 'badges'); 997 } 998 return $output; 999 } 1000 1001 /** 1002 * Renders a table for related badges. 1003 * 1004 * @param \core_badges\output\badge_related $related list related badges. 1005 * @return string list related badges to output. 1006 */ 1007 protected function render_badge_related(\core_badges\output\badge_related $related) { 1008 $currentbadge = new badge($related->currentbadgeid); 1009 $languages = get_string_manager()->get_list_of_languages(); 1010 $paging = new paging_bar($related->totalcount, $related->page, $related->perpage, $this->page->url, 'page'); 1011 $htmlpagingbar = $this->render($paging); 1012 $table = new html_table(); 1013 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide'; 1014 $table->head = array( 1015 get_string('name'), 1016 get_string('version', 'badges'), 1017 get_string('language', 'badges'), 1018 get_string('type', 'badges') 1019 ); 1020 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) { 1021 array_push($table->head, ''); 1022 } 1023 1024 foreach ($related->badges as $badge) { 1025 $badgeobject = new badge($badge->id); 1026 $style = array('title' => $badgeobject->name); 1027 if (!$badgeobject->is_active()) { 1028 $style['class'] = 'dimmed'; 1029 } 1030 $context = ($badgeobject->type == BADGE_TYPE_SITE) ? 1031 context_system::instance() : context_course::instance($badgeobject->courseid); 1032 $forlink = print_badge_image($badgeobject, $context) . ' ' . 1033 html_writer::start_tag('span') . $badgeobject->name . html_writer::end_tag('span'); 1034 $name = html_writer::link(new moodle_url('/badges/overview.php', array('id' => $badgeobject->id)), $forlink, $style); 1035 1036 $row = array( 1037 $name, 1038 $badge->version, 1039 $badge->language ? $languages[$badge->language] : '', 1040 $badge->type == BADGE_TYPE_COURSE ? get_string('badgesview', 'badges') : get_string('sitebadges', 'badges') 1041 ); 1042 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) { 1043 $action = $this->output->action_icon( 1044 new moodle_url('/badges/related_action.php', [ 1045 'badgeid' => $related->currentbadgeid, 1046 'relatedid' => $badge->id, 1047 'sesskey' => sesskey(), 1048 'action' => 'remove' 1049 ]), 1050 new pix_icon('t/delete', get_string('delete'))); 1051 $actions = html_writer::tag('div', $action, array('class' => 'badge-actions')); 1052 array_push($row, $actions); 1053 } 1054 $table->data[] = $row; 1055 } 1056 $htmltable = html_writer::table($table); 1057 1058 return $htmlpagingbar . $htmltable . $htmlpagingbar; 1059 } 1060 1061 /** 1062 * Renders a table with alignment. 1063 * 1064 * @param core_badges\output\badge_alignments $alignments List alignments. 1065 * @return string List alignment to output. 1066 */ 1067 protected function render_badge_alignments(\core_badges\output\badge_alignments $alignments) { 1068 $currentbadge = new badge($alignments->currentbadgeid); 1069 $paging = new paging_bar($alignments->totalcount, $alignments->page, $alignments->perpage, $this->page->url, 'page'); 1070 $htmlpagingbar = $this->render($paging); 1071 $table = new html_table(); 1072 $table->attributes['class'] = 'generaltable boxaligncenter boxwidthwide'; 1073 $table->head = array('Name', 'URL', ''); 1074 1075 foreach ($alignments->alignments as $item) { 1076 $urlaligment = new moodle_url('alignment.php', 1077 array( 1078 'id' => $currentbadge->id, 1079 'alignmentid' => $item->id, 1080 ) 1081 ); 1082 $row = array( 1083 html_writer::link($urlaligment, $item->targetname), 1084 html_writer::link($item->targeturl, $item->targeturl, array('target' => '_blank')) 1085 ); 1086 if (!$currentbadge->is_active() && !$currentbadge->is_locked()) { 1087 $delete = $this->output->action_icon( 1088 new moodle_url('/badges/alignment_action.php', [ 1089 'id' => $currentbadge->id, 1090 'alignmentid' => $item->id, 1091 'sesskey' => sesskey(), 1092 'action' => 'remove' 1093 ]), 1094 new pix_icon('t/delete', get_string('delete')) 1095 ); 1096 $edit = $this->output->action_icon( 1097 new moodle_url('alignment.php', 1098 array( 1099 'id' => $currentbadge->id, 1100 'alignmentid' => $item->id, 1101 'action' => 'edit' 1102 ) 1103 ), new pix_icon('t/edit', get_string('edit'))); 1104 $actions = html_writer::tag('div', $edit . $delete, array('class' => 'badge-actions')); 1105 array_push($row, $actions); 1106 } 1107 $table->data[] = $row; 1108 } 1109 $htmltable = html_writer::table($table); 1110 1111 return $htmlpagingbar . $htmltable . $htmlpagingbar; 1112 } 1113 1114 /** 1115 * Defer to template. 1116 * 1117 * @param \core_badges\output\external_backpacks_page $page 1118 * @return bool|string 1119 */ 1120 public function render_external_backpacks_page(\core_badges\output\external_backpacks_page $page) { 1121 $data = $page->export_for_template($this); 1122 return parent::render_from_template('core_badges/external_backpacks_page', $data); 1123 } 1124 1125 /** 1126 * Get the result of a backpack validation with its settings. It returns: 1127 * - A informative message if the backpack version is different from OBv2. 1128 * - A warning with the error if it's not possible to connect to this backpack. 1129 * - A successful message if the connection has worked. 1130 * 1131 * @param int $backpackid The backpack identifier. 1132 * @return string A message with the validation result. 1133 */ 1134 public function render_test_backpack_result(int $backpackid): string { 1135 // Get the backpack. 1136 $backpack = badges_get_site_backpack($backpackid); 1137 1138 // Add the header to the result. 1139 $result = $this->heading(get_string('testbackpack', 'badges', $backpack->backpackweburl)); 1140 1141 if ($backpack->apiversion != OPEN_BADGES_V2) { 1142 // Only OBv2 supports this validation. 1143 $result .= get_string('backpackconnectionnottested', 'badges'); 1144 } else { 1145 $message = badges_verify_backpack($backpackid); 1146 if (empty($message)) { 1147 $result .= get_string('backpackconnectionok', 'badges'); 1148 } else { 1149 $result .= $message; 1150 } 1151 } 1152 1153 return $result; 1154 } 1155 1156 /** 1157 * Render the tertiary navigation for the page. 1158 * 1159 * @param \core_badges\output\base_action_bar $actionbar 1160 * @return bool|string 1161 */ 1162 public function render_tertiary_navigation(\core_badges\output\base_action_bar $actionbar) { 1163 return $this->render_from_template($actionbar->get_template(), $actionbar->export_for_template($this)); 1164 } 1165 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body