Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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   * Form class for mybackpack.php
  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  namespace core_badges\form;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  require_once($CFG->libdir . '/formslib.php');
  32  require_once($CFG->libdir . '/badgeslib.php');
  33  
  34  use html_writer;
  35  use moodleform;
  36  
  37  /**
  38   * Form to select backpack collections.
  39   *
  40   * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class collections extends moodleform {
  44  
  45      /**
  46       * Defines the form
  47       */
  48      public function definition() {
  49          global $USER;
  50          $mform = $this->_form;
  51          $email = $this->_customdata['email'];
  52          $backpackweburl = $this->_customdata['backpackweburl'];
  53          $selected = $this->_customdata['selected'];
  54  
  55          if (isset($this->_customdata['groups'])) {
  56              $groups = $this->_customdata['groups'];
  57              $nogroups = null;
  58          } else {
  59              $groups = null;
  60              $nogroups = $this->_customdata['nogroups'];
  61          }
  62  
  63          $backpack = get_backpack_settings($USER->id);
  64          $sitebackpack = badges_get_site_backpack($backpack->backpackid);
  65  
  66          $mform->addElement('header', 'backpackheader', get_string('backpackconnection_connected', 'badges'));
  67          $mform->addElement('static', 'url', get_string('url'), $backpackweburl);
  68  
  69          $status = html_writer::tag('span', get_string('connected', 'badges'), array('class' => 'connected'));
  70          $mform->addElement('static', 'status', get_string('status'), $status);
  71          $mform->addElement('static', 'email', get_string('email'), $email);
  72          $mform->addElement('submit', 'disconnect', get_string('disconnect', 'badges'));
  73  
  74          $mform->addElement('header', 'collectionheader', get_string('backpackimport', 'badges'));
  75          $mform->addHelpButton('collectionheader', 'backpackimport', 'badges');
  76  
  77          $hasgroups = false;
  78          if (!empty($groups)) {
  79              foreach ($groups as $group) {
  80                  $count = 0;
  81                  // Handle attributes based on backpack's supported version.
  82                  if ($sitebackpack->apiversion == OPEN_BADGES_V2) {
  83                      // OpenBadges v2 data attributes.
  84                      if (empty($group->published)) {
  85                          // Only public collections.
  86                          continue;
  87                      }
  88  
  89                      // Get the number of badges associated with this collection from the assertions array returned.
  90                      $count = count($group->assertions);
  91                  } else {
  92                      // OpenBadges v1 data attributes.
  93                      $group->entityId = $group->groupId;
  94  
  95                      // Get the number of badges associated with this collection. In that case, the number is returned directly.
  96                      $count = $group->badges;
  97                  }
  98  
  99                  if (!$hasgroups) {
 100                      $mform->addElement('static', 'selectgroup', '', get_string('selectgroup_start', 'badges'));
 101                  }
 102                  $hasgroups = true;
 103                  $name = $group->name . ' (' . $count . ')';
 104                  $mform->addElement(
 105                      'advcheckbox',
 106                      'group[' . $group->entityId . ']',
 107                      null,
 108                      $name,
 109                      array('group' => 1),
 110                      array(false, $group->entityId)
 111                  );
 112                  if (in_array($group->entityId, $selected)) {
 113                      $mform->setDefault('group[' . $group->entityId . ']', $group->entityId);
 114                  }
 115              }
 116              $mform->addElement('static', 'selectgroup', '', get_string('selectgroup_end', 'badges', $backpackweburl));
 117          }
 118          if (!$hasgroups) {
 119              $mform->addElement('static', 'selectgroup', '', $nogroups);
 120          }
 121  
 122          $this->add_action_buttons();
 123      }
 124  }