PageRenderTime 43ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 1ms

/enrol/guest/lib.php

https://bitbucket.org/ngmares/moodle
PHP | 383 lines | 217 code | 59 blank | 107 comment | 59 complexity | 44aba8ea367414e2ed0e12d2a6e8ee75 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  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. * Guest access plugin.
  18. *
  19. * This plugin does not add any entries into the user_enrolments table,
  20. * the access control is granted on the fly via the tricks in require_login().
  21. *
  22. * @package enrol
  23. * @subpackage guest
  24. * @copyright 2010 Petr Skoda {@link http://skodak.org}
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. defined('MOODLE_INTERNAL') || die();
  28. class enrol_guest_plugin extends enrol_plugin {
  29. /**
  30. * Returns optional enrolment information icons.
  31. *
  32. * This is used in course list for quick overview of enrolment options.
  33. *
  34. * We are not using single instance parameter because sometimes
  35. * we might want to prevent icon repetition when multiple instances
  36. * of one type exist. One instance may also produce several icons.
  37. *
  38. * @param array $instances all enrol instances of this type in one course
  39. * @return array of pix_icon
  40. */
  41. public function get_info_icons(array $instances) {
  42. foreach ($instances as $instance) {
  43. if ($instance->password !== '') {
  44. return array(new pix_icon('withpassword', get_string('pluginname', 'enrol_guest'), 'enrol_guest'));
  45. } else {
  46. return array(new pix_icon('withoutpassword', get_string('pluginname', 'enrol_guest'), 'enrol_guest'));
  47. }
  48. }
  49. }
  50. public function enrol_user(stdClass $instance, $userid, $roleid = NULL, $timestart = 0, $timeend = 0, $status = NULL) {
  51. // no real enrolments here!
  52. return;
  53. }
  54. public function unenrol_user(stdClass $instance, $userid) {
  55. // nothing to do, we never enrol here!
  56. return;
  57. }
  58. /**
  59. * Attempt to automatically gain temporary guest access to course,
  60. * calling code has to make sure the plugin and instance are active.
  61. *
  62. * @param stdClass $instance course enrol instance
  63. * @return bool|int false means no guest access, integer means end of cached time
  64. */
  65. public function try_guestaccess(stdClass $instance) {
  66. global $USER, $CFG;
  67. $allow = false;
  68. if ($instance->password === '') {
  69. $allow = true;
  70. } else if (isset($USER->enrol_guest_passwords[$instance->id])) { // this is a hack, ideally we should not add stuff to $USER...
  71. if ($USER->enrol_guest_passwords[$instance->id] === $instance->password) {
  72. $allow = true;
  73. }
  74. }
  75. if ($allow) {
  76. // Temporarily assign them some guest role for this context
  77. $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
  78. load_temp_course_role($context, $CFG->guestroleid);
  79. return ENROL_MAX_TIMESTAMP;
  80. }
  81. return false;
  82. }
  83. /**
  84. * Returns link to page which may be used to add new instance of enrolment plugin in course.
  85. * @param int $courseid
  86. * @return moodle_url page url
  87. */
  88. public function get_newinstance_link($courseid) {
  89. global $DB;
  90. $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
  91. if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/guest:config', $context)) {
  92. return NULL;
  93. }
  94. if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'guest'))) {
  95. return NULL;
  96. }
  97. return new moodle_url('/enrol/guest/addinstance.php', array('sesskey'=>sesskey(), 'id'=>$courseid));
  98. }
  99. /**
  100. * Creates course enrol form, checks if form submitted
  101. * and enrols user if necessary. It can also redirect.
  102. *
  103. * @param stdClass $instance
  104. * @return string html text, usually a form in a text box
  105. */
  106. public function enrol_page_hook(stdClass $instance) {
  107. global $CFG, $OUTPUT, $SESSION, $USER;
  108. if ($instance->password === '') {
  109. return null;
  110. }
  111. if (isset($USER->enrol['tempguest'][$instance->courseid]) and $USER->enrol['tempguest'][$instance->courseid] > time()) {
  112. // no need to show the guest access when user can already enter course as guest
  113. return null;
  114. }
  115. require_once("$CFG->dirroot/enrol/guest/locallib.php");
  116. $form = new enrol_guest_enrol_form(NULL, $instance);
  117. $instanceid = optional_param('instance', 0, PARAM_INT);
  118. if ($instance->id == $instanceid) {
  119. if ($data = $form->get_data()) {
  120. // add guest role
  121. $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
  122. $USER->enrol_guest_passwords[$instance->id] = $data->guestpassword; // this is a hack, ideally we should not add stuff to $USER...
  123. if (isset($USER->enrol['tempguest'][$instance->courseid])) {
  124. remove_temp_course_roles($context);
  125. }
  126. load_temp_course_role($context, $CFG->guestroleid);
  127. $USER->enrol['tempguest'][$instance->courseid] = ENROL_MAX_TIMESTAMP;
  128. // go to the originally requested page
  129. if (!empty($SESSION->wantsurl)) {
  130. $destination = $SESSION->wantsurl;
  131. unset($SESSION->wantsurl);
  132. } else {
  133. $destination = "$CFG->wwwroot/course/view.php?id=$instance->courseid";
  134. }
  135. redirect($destination);
  136. }
  137. }
  138. ob_start();
  139. $form->display();
  140. $output = ob_get_clean();
  141. return $OUTPUT->box($output, 'generalbox');
  142. }
  143. /**
  144. * Adds enrol instance UI to course edit form
  145. *
  146. * @param object $instance enrol instance or null if does not exist yet
  147. * @param MoodleQuickForm $mform
  148. * @param object $data
  149. * @param object $context context of existing course or parent category if course does not exist
  150. * @return void
  151. */
  152. public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) {
  153. $i = isset($instance->id) ? $instance->id : 0;
  154. if (!$i and !$this->get_config('defaultenrol')) {
  155. return;
  156. }
  157. $header = $this->get_instance_name($instance);
  158. $config = has_capability('enrol/guest:config', $context);
  159. $mform->addElement('header', 'enrol_guest_header_'.$i, $header);
  160. $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
  161. ENROL_INSTANCE_DISABLED => get_string('no'));
  162. $mform->addElement('select', 'enrol_guest_status_'.$i, get_string('status', 'enrol_guest'), $options);
  163. $mform->addHelpButton('enrol_guest_status_'.$i, 'status', 'enrol_guest');
  164. $mform->setDefault('enrol_guest_status_'.$i, $this->get_config('status'));
  165. $mform->setAdvanced('enrol_guest_status_'.$i, $this->get_config('status_adv'));
  166. if (!$config) {
  167. $mform->hardFreeze('enrol_guest_status_'.$i);
  168. }
  169. $mform->addElement('passwordunmask', 'enrol_guest_password_'.$i, get_string('password', 'enrol_guest'));
  170. $mform->addHelpButton('enrol_guest_password_'.$i, 'password', 'enrol_guest');
  171. if (!$config) {
  172. $mform->hardFreeze('enrol_guest_password_'.$i);
  173. } else {
  174. $mform->disabledIf('enrol_guest_password_'.$i, 'enrol_guest_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
  175. }
  176. // now add all values from enrol table
  177. if ($instance) {
  178. foreach($instance as $key=>$val) {
  179. $data->{'enrol_guest_'.$key.'_'.$i} = $val;
  180. }
  181. }
  182. }
  183. /**
  184. * Validates course edit form data
  185. *
  186. * @param object $instance enrol instance or null if does not exist yet
  187. * @param array $data
  188. * @param object $context context of existing course or parent category if course does not exist
  189. * @return array errors array
  190. */
  191. public function course_edit_validation($instance, array $data, $context) {
  192. $errors = array();
  193. if (!has_capability('enrol/guest:config', $context)) {
  194. // we are going to ignore the data later anyway, they would nto be able to fix the form anyway
  195. return $errors;
  196. }
  197. $i = isset($instance->id) ? $instance->id : 0;
  198. if (!isset($data['enrol_guest_status_'.$i])) {
  199. return $errors;
  200. }
  201. $password = empty($data['enrol_guest_password_'.$i]) ? '' : $data['enrol_guest_password_'.$i];
  202. $checkpassword = false;
  203. if ($instance) {
  204. if ($data['enrol_guest_status_'.$i] == ENROL_INSTANCE_ENABLED) {
  205. if ($instance->password !== $password) {
  206. $checkpassword = true;
  207. }
  208. }
  209. } else {
  210. if ($data['enrol_guest_status_'.$i] == ENROL_INSTANCE_ENABLED) {
  211. $checkpassword = true;
  212. }
  213. }
  214. if ($checkpassword) {
  215. $require = $this->get_config('requirepassword');
  216. $policy = $this->get_config('usepasswordpolicy');
  217. if ($require and empty($password)) {
  218. $errors['enrol_guest_password_'.$i] = get_string('required');
  219. } else if ($policy) {
  220. $errmsg = '';//prevent eclipse warning
  221. if (!check_password_policy($password, $errmsg)) {
  222. $errors['enrol_guest_password_'.$i] = $errmsg;
  223. }
  224. }
  225. }
  226. return $errors;
  227. }
  228. /**
  229. * Called after updating/inserting course.
  230. *
  231. * @param bool $inserted true if course just inserted
  232. * @param object $course
  233. * @param object $data form data
  234. * @return void
  235. */
  236. public function course_updated($inserted, $course, $data) {
  237. global $DB;
  238. $context = get_context_instance(CONTEXT_COURSE, $course->id);
  239. if (has_capability('enrol/guest:config', $context)) {
  240. if ($inserted) {
  241. if (isset($data->enrol_guest_status_0)) {
  242. $fields = array('status'=>$data->enrol_guest_status_0);
  243. if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
  244. $fields['password'] = $data->enrol_guest_password_0;
  245. } else {
  246. if ($this->get_config('requirepassword')) {
  247. $fields['password'] = generate_password(20);
  248. }
  249. }
  250. $this->add_instance($course, $fields);
  251. } else {
  252. if ($this->get_config('defaultenrol')) {
  253. $this->add_default_instance($course);
  254. }
  255. }
  256. } else {
  257. $instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'guest'));
  258. foreach ($instances as $instance) {
  259. $i = $instance->id;
  260. if (isset($data->{'enrol_guest_status_'.$i})) {
  261. $reset = ($instance->status != $data->{'enrol_guest_status_'.$i});
  262. $instance->status = $data->{'enrol_guest_status_'.$i};
  263. $instance->timemodified = time();
  264. if ($instance->status == ENROL_INSTANCE_ENABLED) {
  265. if ($instance->password !== $data->{'enrol_guest_password_'.$i}) {
  266. $reset = true;
  267. }
  268. $instance->password = $data->{'enrol_guest_password_'.$i};
  269. }
  270. $DB->update_record('enrol', $instance);
  271. if ($reset) {
  272. $context->mark_dirty();
  273. }
  274. }
  275. }
  276. }
  277. } else {
  278. if ($inserted) {
  279. if ($this->get_config('defaultenrol')) {
  280. $this->add_default_instance($course);
  281. }
  282. } else {
  283. // bad luck, user can not change anything
  284. }
  285. }
  286. }
  287. /**
  288. * Add new instance of enrol plugin.
  289. * @param object $course
  290. * @param array instance fields
  291. * @return int id of new instance, null if can not be created
  292. */
  293. public function add_instance($course, array $fields = NULL) {
  294. $fields = (array)$fields;
  295. if (!isset($fields['password'])) {
  296. $fields['password'] = '';
  297. }
  298. return parent::add_instance($course, $fields);
  299. }
  300. /**
  301. * Add new instance of enrol plugin with default settings.
  302. * @param object $course
  303. * @return int id of new instance
  304. */
  305. public function add_default_instance($course) {
  306. $fields = array('status'=>$this->get_config('status'));
  307. if ($this->get_config('requirepassword')) {
  308. $fields['password'] = generate_password(20);
  309. }
  310. return $this->add_instance($course, $fields);
  311. }
  312. }
  313. /**
  314. * Indicates API features that the enrol plugin supports.
  315. *
  316. * @param string $feature
  317. * @return mixed True if yes (some features may use other values)
  318. */
  319. function enrol_guest_supports($feature) {
  320. switch($feature) {
  321. case ENROL_RESTORE_TYPE: return ENROL_RESTORE_NOUSERS;
  322. default: return null;
  323. }
  324. }