Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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