[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/badges/ -> edit_form.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1