MDL-59365 enrol: Behat fixes
[moodle.git] / enrol / manual / ajax.php
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/>.
17 /**
18  * This file processes AJAX enrolment actions and returns JSON for the manual enrolments plugin
19  *
20  * The general idea behind this file is that any errors should throw exceptions
21  * which will be returned and acted upon by the calling AJAX script.
22  *
23  * @package    enrol_manual
24  * @copyright  2010 Sam Hemelryk
25  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26  */
28 define('AJAX_SCRIPT', true);
30 require('../../config.php');
31 require_once($CFG->dirroot.'/enrol/locallib.php');
32 require_once($CFG->dirroot.'/group/lib.php');
33 require_once($CFG->dirroot.'/enrol/manual/locallib.php');
34 require_once($CFG->dirroot.'/cohort/lib.php');
36 $id      = required_param('id', PARAM_INT); // Course id.
37 $action  = required_param('action', PARAM_ALPHANUMEXT);
39 $PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
41 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
42 $context = context_course::instance($course->id, MUST_EXIST);
44 if ($course->id == SITEID) {
45     throw new moodle_exception('invalidcourse');
46 }
48 require_login($course);
49 require_capability('moodle/course:enrolreview', $context);
50 require_sesskey();
52 echo $OUTPUT->header(); // Send headers.
54 $manager = new course_enrolment_manager($PAGE, $course);
56 $outcome = new stdClass();
57 $outcome->success = true;
58 $outcome->response = new stdClass();
59 $outcome->error = '';
61 $searchanywhere = get_user_preferences('userselector_searchanywhere', false);
63 switch ($action) {
64     case 'enrol':
65         $enrolid = required_param('enrolid', PARAM_INT);
66         $cohorts = $users = [];
68         $userids = optional_param('userlist', [], PARAM_SEQUENCE);
69         $userid = optional_param('userid', 0, PARAM_INT);
70         if ($userid) {
71             $userids[] = $userid;
72         }
73         if ($userids) {
74             foreach ($userids as $userid) {
75                 $users[] = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
76             }
77         }
78         $cohortids = optional_param('cohortlist', [], PARAM_SEQUENCE);
79         $cohortid = optional_param('cohortid', 0, PARAM_INT);
80         if ($cohortid) {
81             $cohortids[] = $cohortid;
82         }
83         if ($cohortids) {
84             foreach ($cohortids as $cohortid) {
85                 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
86                 if (!cohort_can_view_cohort($cohort, $context)) {
87                     throw new enrol_ajax_exception('invalidenrolinstance'); // TODO error text!
88                 }
89                 $cohorts[] = $cohort;
90             }
91         }
93         $roleid = optional_param('roletoassign', null, PARAM_INT);
94         $duration = optional_param('duration', 0, PARAM_FLOAT);
95         $startdate = optional_param('startdate', 0, PARAM_INT);
96         $recovergrades = optional_param('recovergrades', 0, PARAM_INT);
98         if (empty($roleid)) {
99             $roleid = null;
100         }
102         if (empty($startdate)) {
103             if (!$startdate = get_config('enrol_manual', 'enrolstart')) {
104                 // Default to now if there is no system setting.
105                 $startdate = 4;
106             }
107         }
109         switch($startdate) {
110             case 2:
111                 $timestart = $course->startdate;
112                 break;
113             case 4:
114                 // We mimic get_enrolled_sql round(time(), -2) but always floor as we want users to always access their
115                 // courses once they are enrolled.
116                 $timestart = intval(substr(time(), 0, 8) . '00') - 1;
117                 break;
118             case 3:
119             default:
120                 $today = time();
121                 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
122                 $timestart = $today;
123                 break;
124         }
125         if ($duration <= 0) {
126             $timeend = 0;
127         } else {
128             $timeend = $timestart + intval($duration*24*60*60);
129         }
131         $instances = $manager->get_enrolment_instances();
132         $plugins = $manager->get_enrolment_plugins(true); // Do not allow actions on disabled plugins.
133         if (!array_key_exists($enrolid, $instances)) {
134             throw new enrol_ajax_exception('invalidenrolinstance');
135         }
136         $instance = $instances[$enrolid];
137         if (!isset($plugins[$instance->enrol])) {
138             throw new enrol_ajax_exception('enrolnotpermitted');
139         }
140         $plugin = $plugins[$instance->enrol];
141         if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
142             foreach ($users as $user) {
143                 $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend, null, $recovergrades);
144             }
145             foreach ($cohorts as $cohort) {
146                 $plugin->enrol_cohort($instance, $cohort->id, $roleid, $timestart, $timeend, null, $recovergrades);
147             }
148         } else {
149             throw new enrol_ajax_exception('enrolnotpermitted');
150         }
151         $outcome->success = true;
152         break;
154     default:
155         throw new enrol_ajax_exception('unknowajaxaction');
158 echo json_encode($outcome);