Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.
   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   * Form endorsement for editing.
  18   *
  19   * @package    core
  20   * @subpackage badges
  21   * @copyright  2018 Tung Thai
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @author     Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
  24   */
  25  defined('MOODLE_INTERNAL') || die();
  26  require_once($CFG->libdir . '/formslib.php');
  27  require_once($CFG->libdir . '/badgeslib.php');
  28  /**
  29   * Form to edit endorsement.
  30   *
  31   * @copyright  2018 Tung Thai
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   * @author     Tung Thai
  34   */
  35  class endorsement_form extends moodleform {
  36  
  37      /**
  38       * Defines the form.
  39       */
  40      public function definition() {
  41          $mform = $this->_form;
  42          $badge = $this->_customdata['badge'];
  43          $mform->addElement('header', 'endorsement', get_string('issuerdetails', 'badges'));
  44          $mform->addElement('text', 'issuername', get_string('issuername_endorsement', 'badges'), array('size' => '70'));
  45          $mform->setType('issuername', PARAM_TEXT);
  46          $mform->addRule('issuername', null, 'required');
  47          $mform->addHelpButton('issuername', 'issuername_endorsement', 'badges');
  48          $mform->addElement('text', 'issueremail', get_string('issueremail', 'badges'), array('size' => '70'));
  49          $mform->addRule('issueremail', null, 'required');
  50          $mform->setType('issueremail', PARAM_RAW);
  51          $mform->addHelpButton('issueremail', 'issueremail', 'badges');
  52          $mform->addElement('text', 'issuerurl', get_string('issuerurl', 'badges'), array('size' => '70'));
  53          $mform->setType('issuerurl', PARAM_URL);
  54          $mform->addRule('issuerurl', null, 'required');
  55          $mform->addHelpButton('issuerurl', 'issuerurl', 'badges');
  56          $mform->addElement('date_time_selector', 'dateissued',
  57              get_string('dateawarded', 'badges'));
  58          $mform->addElement('header', 'claim', get_string('claim', 'badges'));
  59          $mform->addElement('text', 'claimid', get_string('claimid', 'badges'), array('size' => '70'));
  60          $mform->setType('claimid', PARAM_URL);
  61          $mform->addRule('claimid', null, 'required');
  62          $mform->addElement('textarea', 'claimcomment', get_string('claimcomment', 'badges'), 'wrap="virtual" rows="8" cols="70"');
  63          $mform->setType('claimcomment', PARAM_NOTAGS);
  64          $endorsement = new stdClass();
  65          $endorsement = $badge->get_endorsement();
  66          if ($endorsement) {
  67              $mform->setDefault('dateissued', $endorsement->dateissued);
  68              $this->set_data($endorsement);
  69          }
  70          $this->add_action_buttons();
  71          // Freeze all elements if badge is active or locked.
  72          if ($badge->is_active() || $badge->is_locked()) {
  73              $mform->hardFreezeAllVisibleExcept(array());
  74          }
  75      }
  76  
  77      /**
  78       * Validates form data.
  79       *
  80       * @param array $data submitted data.
  81       * @param array $files submitted files.
  82       * @return array $errors An array of errors.
  83       */
  84      public function validation($data, $files) {
  85          $errors = parent::validation($data, $files);
  86          if ($data['issueremail'] && !validate_email($data['issueremail'])) {
  87              $errors['issueremail'] = get_string('invalidemail');
  88          }
  89          if ($data['issuerurl'] && !preg_match('@^https?://.+@', $data['issuerurl'])) {
  90              $errors['issuerurl'] = get_string('invalidurl', 'badges');
  91          }
  92          if ($data['claimid'] && !preg_match('@^https?://.+@', $data['claimid'])) {
  93              $errors['claimid'] = get_string('invalidurl', 'badges');
  94          }
  95          return $errors;
  96      }
  97  }