[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/enrol/self/ -> 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   * Adds new instance of enrol_self to specified course
  19   * or edits current instance.
  20   *
  21   * @package    enrol_self
  22   * @copyright  2010 Petr Skoda  {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir.'/formslib.php');
  29  require_once($CFG->dirroot.'/cohort/lib.php');
  30  
  31  class enrol_self_edit_form extends moodleform {
  32  
  33      function definition() {
  34          global $DB;
  35  
  36          $mform = $this->_form;
  37  
  38          list($instance, $plugin, $context) = $this->_customdata;
  39  
  40          $mform->addElement('header', 'header', get_string('pluginname', 'enrol_self'));
  41  
  42          $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
  43          $mform->setType('name', PARAM_TEXT);
  44  
  45          $options = array(ENROL_INSTANCE_ENABLED  => get_string('yes'),
  46                           ENROL_INSTANCE_DISABLED => get_string('no'));
  47          $mform->addElement('select', 'status', get_string('status', 'enrol_self'), $options);
  48          $mform->addHelpButton('status', 'status', 'enrol_self');
  49  
  50          $options = array(1 => get_string('yes'), 0 => get_string('no'));
  51          $mform->addElement('select', 'customint6', get_string('newenrols', 'enrol_self'), $options);
  52          $mform->addHelpButton('customint6', 'newenrols', 'enrol_self');
  53          $mform->disabledIf('customint6', 'status', 'eq', ENROL_INSTANCE_DISABLED);
  54  
  55          $mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_self'));
  56          $mform->addHelpButton('password', 'password', 'enrol_self');
  57          if (empty($instance->id) and $plugin->get_config('requirepassword')) {
  58              $mform->addRule('password', get_string('required'), 'required', null, 'client');
  59          }
  60  
  61          $options = array(1 => get_string('yes'),
  62                           0 => get_string('no'));
  63          $mform->addElement('select', 'customint1', get_string('groupkey', 'enrol_self'), $options);
  64          $mform->addHelpButton('customint1', 'groupkey', 'enrol_self');
  65  
  66          $roles = $this->extend_assignable_roles($context, $instance->roleid);
  67          $mform->addElement('select', 'roleid', get_string('role', 'enrol_self'), $roles);
  68  
  69          $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_self'), array('optional' => true, 'defaultunit' => 86400));
  70          $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_self');
  71  
  72          $options = array(0 => get_string('no'), 1 => get_string('expirynotifyenroller', 'core_enrol'), 2 => get_string('expirynotifyall', 'core_enrol'));
  73          $mform->addElement('select', 'expirynotify', get_string('expirynotify', 'core_enrol'), $options);
  74          $mform->addHelpButton('expirynotify', 'expirynotify', 'core_enrol');
  75  
  76          $mform->addElement('duration', 'expirythreshold', get_string('expirythreshold', 'core_enrol'), array('optional' => false, 'defaultunit' => 86400));
  77          $mform->addHelpButton('expirythreshold', 'expirythreshold', 'core_enrol');
  78          $mform->disabledIf('expirythreshold', 'expirynotify', 'eq', 0);
  79  
  80          $mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_self'), array('optional' => true));
  81          $mform->setDefault('enrolstartdate', 0);
  82          $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_self');
  83  
  84          $mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_self'), array('optional' => true));
  85          $mform->setDefault('enrolenddate', 0);
  86          $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_self');
  87  
  88          $options = array(0 => get_string('never'),
  89                   1800 * 3600 * 24 => get_string('numdays', '', 1800),
  90                   1000 * 3600 * 24 => get_string('numdays', '', 1000),
  91                   365 * 3600 * 24 => get_string('numdays', '', 365),
  92                   180 * 3600 * 24 => get_string('numdays', '', 180),
  93                   150 * 3600 * 24 => get_string('numdays', '', 150),
  94                   120 * 3600 * 24 => get_string('numdays', '', 120),
  95                   90 * 3600 * 24 => get_string('numdays', '', 90),
  96                   60 * 3600 * 24 => get_string('numdays', '', 60),
  97                   30 * 3600 * 24 => get_string('numdays', '', 30),
  98                   21 * 3600 * 24 => get_string('numdays', '', 21),
  99                   14 * 3600 * 24 => get_string('numdays', '', 14),
 100                   7 * 3600 * 24 => get_string('numdays', '', 7));
 101          $mform->addElement('select', 'customint2', get_string('longtimenosee', 'enrol_self'), $options);
 102          $mform->addHelpButton('customint2', 'longtimenosee', 'enrol_self');
 103  
 104          $mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_self'));
 105          $mform->addHelpButton('customint3', 'maxenrolled', 'enrol_self');
 106          $mform->setType('customint3', PARAM_INT);
 107  
 108          $cohorts = array(0 => get_string('no'));
 109          $allcohorts = cohort_get_available_cohorts($context);
 110          if ($instance->customint5 && !isset($allcohorts[$instance->customint5]) &&
 111                  ($c = $DB->get_record('cohort', array('id' => $instance->customint5), 'id, name, idnumber, contextid, visible', IGNORE_MISSING))) {
 112              // Current cohort was not found because current user can not see it. Still keep it.
 113              $allcohorts[$instance->customint5] = $c;
 114          }
 115          foreach ($allcohorts as $c) {
 116              $cohorts[$c->id] = format_string($c->name, true, array('context' => context::instance_by_id($c->contextid)));
 117              if ($c->idnumber) {
 118                  $cohorts[$c->id] .= ' ['.s($c->idnumber).']';
 119              }
 120          }
 121          if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) {
 122              // Somebody deleted a cohort, better keep the wrong value so that random ppl can not enrol.
 123              $cohorts[$instance->customint5] = get_string('unknowncohort', 'cohort', $instance->customint5);
 124          }
 125          if (count($cohorts) > 1) {
 126              $mform->addElement('select', 'customint5', get_string('cohortonly', 'enrol_self'), $cohorts);
 127              $mform->addHelpButton('customint5', 'cohortonly', 'enrol_self');
 128          } else {
 129              $mform->addElement('hidden', 'customint5');
 130              $mform->setType('customint5', PARAM_INT);
 131              $mform->setConstant('customint5', 0);
 132          }
 133  
 134          $mform->addElement('advcheckbox', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self'));
 135          $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self');
 136  
 137          $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_self'), array('cols'=>'60', 'rows'=>'8'));
 138          $mform->addHelpButton('customtext1', 'customwelcomemessage', 'enrol_self');
 139  
 140          $mform->addElement('hidden', 'id');
 141          $mform->setType('id', PARAM_INT);
 142          $mform->addElement('hidden', 'courseid');
 143          $mform->setType('courseid', PARAM_INT);
 144  
 145          if (enrol_accessing_via_instance($instance)) {
 146              $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), get_string('instanceeditselfwarningtext', 'core_enrol'));
 147          }
 148  
 149          $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol')));
 150  
 151          $this->set_data($instance);
 152      }
 153  
 154      function validation($data, $files) {
 155          global $DB, $CFG;
 156          $errors = parent::validation($data, $files);
 157  
 158          list($instance, $plugin, $context) = $this->_customdata;
 159          $checkpassword = false;
 160  
 161          if ($instance->id) {
 162              if ($data['status'] == ENROL_INSTANCE_ENABLED) {
 163                  if ($instance->password !== $data['password']) {
 164                      $checkpassword = true;
 165                  }
 166              }
 167          } else {
 168              if ($data['status'] == ENROL_INSTANCE_ENABLED) {
 169                  $checkpassword = true;
 170              }
 171          }
 172  
 173          if ($checkpassword) {
 174              $require = $plugin->get_config('requirepassword');
 175              $policy  = $plugin->get_config('usepasswordpolicy');
 176              if ($require and trim($data['password']) === '') {
 177                  $errors['password'] = get_string('required');
 178              } else if ($policy) {
 179                  $errmsg = '';//prevent eclipse warning
 180                  if (!check_password_policy($data['password'], $errmsg)) {
 181                      $errors['password'] = $errmsg;
 182                  }
 183              }
 184          }
 185  
 186          if ($data['status'] == ENROL_INSTANCE_ENABLED) {
 187              if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) {
 188                  $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_self');
 189              }
 190          }
 191  
 192          if ($data['expirynotify'] > 0 and $data['expirythreshold'] < 86400) {
 193              $errors['expirythreshold'] = get_string('errorthresholdlow', 'core_enrol');
 194          }
 195  
 196          return $errors;
 197      }
 198  
 199      /**
 200      * Gets a list of roles that this user can assign for the course as the default for self-enrolment.
 201      *
 202      * @param context $context the context.
 203      * @param integer $defaultrole the id of the role that is set as the default for self-enrolment
 204      * @return array index is the role id, value is the role name
 205      */
 206      function extend_assignable_roles($context, $defaultrole) {
 207          global $DB;
 208  
 209          $roles = get_assignable_roles($context, ROLENAME_BOTH);
 210          if (!isset($roles[$defaultrole])) {
 211              if ($role = $DB->get_record('role', array('id'=>$defaultrole))) {
 212                  $roles[$defaultrole] = role_get_name($role, $context, ROLENAME_BOTH);
 213              }
 214          }
 215          return $roles;
 216      }
 217  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1