PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/enrol/self/edit_form.php

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