Merge branch 'w43_MDL-29984_m22_enroltoday' of git://github.com/skodak/moodle
[moodle.git] / enrol / manual / lib.php
CommitLineData
df997f84
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Manual enrolment plugin main library file.
20 *
eafb7a72
PS
21 * @package enrol
22 * @subpackage manual
23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
df997f84
PS
25 */
26
97795859 27defined('MOODLE_INTERNAL') || die();
df997f84
PS
28
29class enrol_manual_plugin extends enrol_plugin {
30
31 public function roles_protected() {
32 // users may tweak the roles later
33 return false;
34 }
35
91b99e80
PS
36 public function allow_enrol(stdClass $instance) {
37 // users with enrol cap may unenrol other users manually manually
38 return true;
39 }
40
df997f84
PS
41 public function allow_unenrol(stdClass $instance) {
42 // users with unenrol cap may unenrol other users manually manually
43 return true;
44 }
45
46 public function allow_manage(stdClass $instance) {
47 // users with manage cap may tweak period and status
48 return true;
49 }
50
51 /**
52 * Returns link to manual enrol UI if exists.
53 * Does the access control tests automatically.
54 *
55 * @param object $instance
56 * @return moodle_url
57 */
58 public function get_manual_enrol_link($instance) {
59 $name = $this->get_name();
60 if ($instance->enrol !== $name) {
61 throw new coding_exception('invalid enrol instance!');
62 }
63
df997f84
PS
64 if (!enrol_is_enabled($name)) {
65 return NULL;
66 }
67
68 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid, MUST_EXIST);
69
91b99e80 70 if (!has_capability('enrol/manual:manage', $context) or !has_capability('enrol/manual:enrol', $context) or !has_capability('enrol/manual:unenrol', $context)) {
df997f84
PS
71 return NULL;
72 }
73
74 return new moodle_url('/enrol/manual/manage.php', array('enrolid'=>$instance->id, 'id'=>$instance->courseid));
75 }
76
2d4b1f3e
PS
77 /**
78 * Returns enrolment instance manage link.
79 *
80 * By defaults looks for manage.php file and tests for manage capability.
81 *
82 * @param object $instance
83 * @return moodle_url;
84 */
85 public function add_course_navigation($instancesnode, stdClass $instance) {
eafb7a72 86 if ($instance->enrol !== 'manual') {
2d4b1f3e
PS
87 throw new coding_exception('Invalid enrol instance type!');
88 }
89
90 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
eafb7a72
PS
91 if (has_capability('enrol/manual:config', $context)) {
92 $managelink = new moodle_url('/enrol/manual/edit.php', array('courseid'=>$instance->courseid));
93 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
2d4b1f3e 94 }
2d4b1f3e
PS
95 }
96
97 /**
98 * Returns edit icons for the page with list of instances
99 * @param stdClass $instance
100 * @return array
101 */
102 public function get_action_icons(stdClass $instance) {
103 global $OUTPUT;
104
eafb7a72 105 if ($instance->enrol !== 'manual') {
2d4b1f3e
PS
106 throw new coding_exception('invalid enrol instance!');
107 }
2d4b1f3e 108 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
eafb7a72
PS
109
110 $icons = array();
111
112 if (has_capability('enrol/manual:manage', $context)) {
113 $managelink = new moodle_url("/enrol/manual/manage.php", array('enrolid'=>$instance->id));
114 $icons[] = $OUTPUT->action_icon($managelink, new pix_icon('i/users', get_string('enrolusers', 'enrol_manual'), 'core', array('class'=>'iconsmall')));
115 }
116 if (has_capability('enrol/manual:config', $context)) {
117 $editlink = new moodle_url("/enrol/manual/edit.php", array('courseid'=>$instance->courseid));
118 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon')));
2d4b1f3e
PS
119 }
120
eafb7a72 121 return $icons;
2d4b1f3e
PS
122 }
123
df997f84
PS
124 /**
125 * Returns link to page which may be used to add new instance of enrolment plugin in course.
126 * @param int $courseid
127 * @return moodle_url page url
128 */
e25f2466 129 public function get_newinstance_link($courseid) {
df997f84
PS
130 global $DB;
131
eafb7a72
PS
132 $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
133
134 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/manual:config', $context)) {
df997f84
PS
135 return NULL;
136 }
137
138 if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'manual'))) {
139 return NULL;
140 }
141
eafb7a72 142 return new moodle_url('/enrol/manual/edit.php', array('courseid'=>$courseid));
df997f84
PS
143 }
144
145 /**
146 * Add new instance of enrol plugin with default settings.
147 * @param object $course
0848a196 148 * @return int id of new instance, null if can not be created
df997f84
PS
149 */
150 public function add_default_instance($course) {
151 $fields = array('status'=>$this->get_config('status'), 'enrolperiod'=>$this->get_config('enrolperiod', 0), 'roleid'=>$this->get_config('roleid', 0));
152 return $this->add_instance($course, $fields);
153 }
154
0848a196
PS
155 /**
156 * Add new instance of enrol plugin.
157 * @param object $course
158 * @param array instance fields
159 * @return int id of new instance, null if can not be created
160 */
161 public function add_instance($course, array $fields = NULL) {
162 global $DB;
163
164 if ($DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'))) {
165 // only one instance allowed, sorry
166 return NULL;
167 }
168
169 return parent::add_instance($course, $fields);
170 }
b69ca6be
SH
171
172 /**
173 * Returns a button to manually enrol users through the manual enrolment plugin.
174 *
175 * By default the first manual enrolment plugin instance available in the course is used.
176 * If no manual enrolment instances exist within the course then false is returned.
177 *
178 * This function also adds a quickenrolment JS ui to the page so that users can be enrolled
179 * via AJAX.
180 *
b69ca6be
SH
181 * @param course_enrolment_manager $manager
182 * @return enrol_user_button
183 */
184 public function get_manual_enrol_button(course_enrolment_manager $manager) {
d4c98cff
SH
185 global $CFG;
186
b69ca6be
SH
187 $instance = null;
188 $instances = array();
189 foreach ($manager->get_enrolment_instances() as $tempinstance) {
190 if ($tempinstance->enrol == 'manual') {
191 if ($instance === null) {
192 $instance = $tempinstance;
193 }
194 $instances[] = array('id' => $tempinstance->id, 'name' => $this->get_instance_name($tempinstance));
195 }
196 }
197 if (empty($instance)) {
198 return false;
199 }
200
1e3c41c0
PS
201 if (!$manuallink = $this->get_manual_enrol_link($instance)) {
202 return false;
203 }
204
205 $button = new enrol_user_button($manuallink, get_string('enrolusers', 'enrol_manual'), 'get');
b69ca6be
SH
206 $button->class .= ' enrol_manual_plugin';
207
208 $startdate = $manager->get_course()->startdate;
209 $startdateoptions = array();
210 $timeformat = get_string('strftimedatefullshort');
211 if ($startdate > 0) {
b69ca6be
SH
212 $startdateoptions[2] = get_string('coursestart') . ' (' . userdate($startdate, $timeformat) . ')';
213 }
4f500c25
PS
214 $today = time();
215 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
b69ca6be
SH
216 $startdateoptions[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ;
217
218 $modules = array('moodle-enrol_manual-quickenrolment', 'moodle-enrol_manual-quickenrolment-skin');
219 $arguments = array(
d4c98cff
SH
220 'instances' => $instances,
221 'courseid' => $instance->courseid,
222 'ajaxurl' => '/enrol/manual/ajax.php',
223 'url' => $manager->get_moodlepage()->url->out(false),
224 'optionsStartDate' => $startdateoptions,
225 'defaultRole' => $instance->roleid,
226 'disableGradeHistory' => $CFG->disablegradehistory
b69ca6be
SH
227 );
228 $function = 'M.enrol_manual.quickenrolment.init';
229 $button->require_yui_module($modules, $function, array($arguments));
230 $button->strings_for_js(array(
231 'ajaxoneuserfound',
232 'ajaxxusersfound',
233 'ajaxnext25',
234 'enrol',
235 'enrolmentoptions',
236 'enrolusers',
237 'errajaxfailedenrol',
238 'errajaxsearch',
239 'none',
240 'usersearch',
241 'unlimitedduration',
242 'startdatetoday',
243 'durationdays',
d4c98cff
SH
244 'enrolperiod',
245 'finishenrollingusers',
246 'recovergrades'), 'enrol');
b69ca6be
SH
247 $button->strings_for_js('assignroles', 'role');
248 $button->strings_for_js('startingfrom', 'moodle');
249
250 return $button;
251 }
24a0c1ea
SH
252
253 /**
254 * Gets an array of the user enrolment actions
255 *
256 * @param course_enrolment_manager $manager
257 * @param stdClass $ue A user enrolment object
258 * @return array An array of user_enrolment_actions
259 */
260 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
261 $actions = array();
262 $context = $manager->get_context();
263 $instance = $ue->enrolmentinstance;
264 $params = $manager->get_moodlepage()->url->params();
265 $params['ue'] = $ue->id;
266 if ($this->allow_unenrol($instance) && has_capability("enrol/manual:unenrol", $context)) {
267 $url = new moodle_url('/enrol/manual/unenroluser.php', $params);
268 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id));
269 }
270 if ($this->allow_manage($instance) && has_capability("enrol/manual:manage", $context)) {
271 $url = new moodle_url('/enrol/manual/editenrolment.php', $params);
272 $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id));
273 }
274 return $actions;
275 }
75ee207b
SH
276
277 /**
278 * The manual plugin has several bulk operations that can be performed
279 * @return array
280 */
281 public function get_bulk_operations(course_enrolment_manager $manager) {
282 global $CFG;
283 require_once($CFG->dirroot.'/enrol/manual/locallib.php');
284 $bulkoperations = array(
285 'editselectedusers' => new enrol_manual_editselectedusers_operation($manager, $this),
286 'deleteselectedusers' => new enrol_manual_deleteselectedusers_operation($manager, $this)
287 );
288 return $bulkoperations;
289 }
df997f84
PS
290}
291
f2a9be5f
EL
292/**
293 * Indicates API features that the enrol plugin supports.
294 *
295 * @param string $feature
296 * @return mixed True if yes (some features may use other values)
297 */
298function enrol_manual_supports($feature) {
299 switch($feature) {
300 case ENROL_RESTORE_TYPE: return ENROL_RESTORE_EXACT;
301
302 default: return null;
303 }
304}