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 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 classes for editing badges
  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  require_once($CFG->libdir . '/filelib.php');
  34  
  35  use moodleform;
  36  
  37  /**
  38   * Form to edit badge details.
  39   *
  40   */
  41  class badge extends moodleform {
  42  
  43      /**
  44       * Defines the form
  45       */
  46      public function definition() {
  47          global $CFG;
  48  
  49          $mform = $this->_form;
  50          $badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false;
  51          $action = $this->_customdata['action'];
  52  
  53          $mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
  54          $mform->addElement('text', 'name', get_string('name'), array('size' => '70'));
  55          // When downloading badge, it will be necessary to clean the name as PARAM_FILE.
  56          $mform->setType('name', PARAM_TEXT);
  57          $mform->addRule('name', null, 'required');
  58          $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  59  
  60          $mform->addElement('text', 'version', get_string('version', 'badges'), array('size' => '70'));
  61          $mform->setType('version', PARAM_TEXT);
  62          $mform->addHelpButton('version', 'version', 'badges');
  63  
  64          $languages = get_string_manager()->get_list_of_languages();
  65          $mform->addElement('select', 'language', get_string('language'), $languages);
  66          $mform->addHelpButton('language', 'language', 'badges');
  67  
  68          $mform->addElement('textarea', 'description', get_string('description', 'badges'), 'wrap="virtual" rows="8" cols="70"');
  69          $mform->setType('description', PARAM_NOTAGS);
  70          $mform->addRule('description', null, 'required');
  71  
  72          $str = $action == 'new' ? get_string('badgeimage', 'badges') : get_string('newimage', 'badges');
  73          $imageoptions = array('maxbytes' => 262144, 'accepted_types' => array('optimised_image'));
  74          $mform->addElement('filepicker', 'image', $str, null, $imageoptions);
  75  
  76          if ($action == 'new') {
  77              $mform->addRule('image', null, 'required');
  78          } else {
  79              $currentimage = $mform->createElement('static', 'currentimage', get_string('currentimage', 'badges'));
  80              $mform->insertElementBefore($currentimage, 'image');
  81          }
  82          $mform->addHelpButton('image', 'badgeimage', 'badges');
  83          $mform->addElement('text', 'imageauthorname', get_string('imageauthorname', 'badges'), array('size' => '70'));
  84          $mform->setType('imageauthorname', PARAM_TEXT);
  85          $mform->addHelpButton('imageauthorname', 'imageauthorname', 'badges');
  86          $mform->addElement('text', 'imageauthoremail', get_string('imageauthoremail', 'badges'), array('size' => '70'));
  87          $mform->setType('imageauthoremail', PARAM_TEXT);
  88          $mform->addHelpButton('imageauthoremail', 'imageauthoremail', 'badges');
  89          $mform->addElement('text', 'imageauthorurl', get_string('imageauthorurl', 'badges'), array('size' => '70'));
  90          $mform->setType('imageauthorurl', PARAM_URL);
  91          $mform->addHelpButton('imageauthorurl', 'imageauthorurl', 'badges');
  92          $mform->addElement('text', 'imagecaption', get_string('imagecaption', 'badges'), array('size' => '70'));
  93          $mform->setType('imagecaption', PARAM_TEXT);
  94          $mform->addHelpButton('imagecaption', 'imagecaption', 'badges');
  95  
  96  
  97          if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
  98              $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
  99  
 100              $mform->addElement('text', 'issuername', get_string('name'), array('size' => '70'));
 101              $mform->setType('issuername', PARAM_NOTAGS);
 102              $mform->addRule('issuername', null, 'required');
 103              if (isset($CFG->badges_defaultissuername)) {
 104                  $mform->setDefault('issuername', $CFG->badges_defaultissuername);
 105              }
 106              $mform->addHelpButton('issuername', 'issuername', 'badges');
 107  
 108              $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), array('size' => '70'));
 109              if (isset($CFG->badges_defaultissuercontact)) {
 110                  $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
 111              }
 112              $mform->setType('issuercontact', PARAM_RAW);
 113              $mform->addHelpButton('issuercontact', 'contact', 'badges');
 114              // Set issuer URL.
 115              // Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot.
 116              $url = parse_url($CFG->wwwroot);
 117              $mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']);
 118              $mform->setType('issuerurl', PARAM_URL);
 119          }
 120  
 121          $mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
 122  
 123          $issuancedetails = array();
 124          $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('never', 'badges'), 0);
 125          $issuancedetails[] =& $mform->createElement('static', 'none_break', null, '<br/>');
 126          $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('fixed', 'badges'), 1);
 127          $issuancedetails[] =& $mform->createElement('date_selector', 'expiredate', '');
 128          $issuancedetails[] =& $mform->createElement('static', 'expirydate_break', null, '<br/>');
 129          $issuancedetails[] =& $mform->createElement('radio', 'expiry', '', get_string('relative', 'badges'), 2);
 130          $issuancedetails[] =& $mform->createElement('duration', 'expireperiod', '', array('defaultunit' => 86400, 'optional' => false));
 131          $issuancedetails[] =& $mform->createElement('static', 'expiryperiods_break', null, get_string('after', 'badges'));
 132  
 133          $mform->addGroup($issuancedetails, 'expirydategr', get_string('expirydate', 'badges'), array(' '), false);
 134          $mform->addHelpButton('expirydategr', 'expirydate', 'badges');
 135          $mform->setDefault('expiry', 0);
 136          $mform->setDefault('expiredate', strtotime('+1 year'));
 137          $mform->disabledIf('expiredate[day]', 'expiry', 'neq', 1);
 138          $mform->disabledIf('expiredate[month]', 'expiry', 'neq', 1);
 139          $mform->disabledIf('expiredate[year]', 'expiry', 'neq', 1);
 140          $mform->disabledIf('expireperiod[number]', 'expiry', 'neq', 2);
 141          $mform->disabledIf('expireperiod[timeunit]', 'expiry', 'neq', 2);
 142  
 143          $mform->addElement('hidden', 'action', $action);
 144          $mform->setType('action', PARAM_TEXT);
 145  
 146          if ($action == 'new') {
 147              // Try to set default badge language to that of current language, or it's parent.
 148              $language = current_language();
 149              if (isset($languages[$language])) {
 150                  $defaultlanguage = $language;
 151              } else {
 152                  // Calling get_parent_language returns an empty string instead of 'en'.
 153                  $defaultlanguage = get_parent_language($language) ?: 'en';
 154              }
 155  
 156              $mform->setDefault('language', $defaultlanguage);
 157              $this->add_action_buttons(true, get_string('createbutton', 'badges'));
 158          } else {
 159              // Add hidden fields.
 160              $mform->addElement('hidden', 'id', $badge->id);
 161              $mform->setType('id', PARAM_INT);
 162  
 163              $this->add_action_buttons();
 164              $this->set_data($badge);
 165  
 166              // Freeze all elements if badge is active or locked.
 167              if ($badge->is_active() || $badge->is_locked()) {
 168                  $mform->hardFreezeAllVisibleExcept(array());
 169              }
 170          }
 171      }
 172  
 173      /**
 174       * Load in existing data as form defaults
 175       *
 176       * @param stdClass|array $default_values object or array of default values
 177       */
 178      public function set_data($badge) {
 179          $default_values = array();
 180          parent::set_data($badge);
 181  
 182          if (!empty($badge->expiredate)) {
 183              $default_values['expiry'] = 1;
 184              $default_values['expiredate'] = $badge->expiredate;
 185          } else if (!empty($badge->expireperiod)) {
 186              $default_values['expiry'] = 2;
 187              $default_values['expireperiod'] = $badge->expireperiod;
 188          }
 189          $default_values['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
 190  
 191          parent::set_data($default_values);
 192      }
 193  
 194      /**
 195       * Validates form data
 196       */
 197      public function validation($data, $files) {
 198          global $DB;
 199          $errors = parent::validation($data, $files);
 200  
 201          if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
 202              if (!empty($data['issuercontact']) && !validate_email($data['issuercontact'])) {
 203                  $errors['issuercontact'] = get_string('invalidemail');
 204              }
 205          }
 206  
 207          if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
 208              $errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
 209          }
 210  
 211          if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
 212              $errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
 213          }
 214  
 215          if ($data['imageauthoremail'] && !validate_email($data['imageauthoremail'])) {
 216              $errors['imageauthoremail'] = get_string('invalidemail');
 217          }
 218  
 219          // Check for duplicate badge names.
 220          if ($data['action'] == 'new') {
 221              $duplicate = $DB->record_exists_select('badge', 'name = :name AND status != :deleted',
 222                  array('name' => $data['name'], 'deleted' => BADGE_STATUS_ARCHIVED));
 223          } else {
 224              $duplicate = $DB->record_exists_select('badge', 'name = :name AND id != :badgeid AND status != :deleted',
 225                  array('name' => $data['name'], 'badgeid' => $data['id'], 'deleted' => BADGE_STATUS_ARCHIVED));
 226          }
 227  
 228          if ($duplicate) {
 229              $errors['name'] = get_string('error:duplicatename', 'badges');
 230          }
 231  
 232          if ($data['imageauthorurl'] && !preg_match('@^https?://.+@', $data['imageauthorurl'])) {
 233              $errors['imageauthorurl'] = get_string('invalidurl', 'badges');
 234          }
 235  
 236          return $errors;
 237      }
 238  }
 239