Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 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', 'badges'));
  67          $mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
  68          $mform->addElement('static', 'url', get_string('url'), $backpackweburl);
  69  
  70          $status = html_writer::tag('span', get_string('connected', 'badges'), array('class' => 'connected'));
  71          $mform->addElement('static', 'status', get_string('status'), $status);
  72          $mform->addElement('static', 'email', get_string('email'), $email);
  73          $mform->addHelpButton('email', 'backpackemail', 'badges');
  74          $mform->addElement('submit', 'disconnect', get_string('disconnect', 'badges'));
  75  
  76          $mform->addElement('header', 'collectionheader', get_string('backpackimport', 'badges'));
  77          $mform->addHelpButton('collectionheader', 'backpackimport', 'badges');
  78  
  79          $hasgroups = false;
  80          if (!empty($groups)) {
  81              foreach ($groups as $group) {
  82                  $count = 0;
  83                  // Handle attributes based on backpack's supported version.
  84                  if ($sitebackpack->apiversion == OPEN_BADGES_V2) {
  85                      // OpenBadges v2 data attributes.
  86                      if (empty($group->published)) {
  87                          // Only public collections.
  88                          continue;
  89                      }
  90  
  91                      // Get the number of badges associated with this collection from the assertions array returned.
  92                      $count = count($group->assertions);
  93                  } else {
  94                      // OpenBadges v1 data attributes.
  95                      $group->entityId = $group->groupId;
  96  
  97                      // Get the number of badges associated with this collection. In that case, the number is returned directly.
  98                      $count = $group->badges;
  99                  }
 100  
 101                  if (!$hasgroups) {
 102                      $mform->addElement('static', 'selectgroup', '', get_string('selectgroup_start', 'badges'));
 103                  }
 104                  $hasgroups = true;
 105                  $name = $group->name . ' (' . $count . ')';
 106                  $mform->addElement(
 107                      'advcheckbox',
 108                      'group[' . $group->entityId . ']',
 109                      null,
 110                      $name,
 111                      array('group' => 1),
 112                      array(false, $group->entityId)
 113                  );
 114                  if (in_array($group->entityId, $selected)) {
 115                      $mform->setDefault('group[' . $group->entityId . ']', $group->entityId);
 116                  }
 117              }
 118              $mform->addElement('static', 'selectgroup', '', get_string('selectgroup_end', 'badges', $backpackweburl));
 119          }
 120          if (!$hasgroups) {
 121              $mform->addElement('static', 'selectgroup', '', $nogroups);
 122          }
 123  
 124          $this->add_action_buttons();
 125      }
 126  }