MDL-23502 refactoring towards separate instance config forms
[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 *
21 * @package enrol_manual
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
26defined('MOODLE_INTERNAL') || die;
27
28class enrol_manual_plugin extends enrol_plugin {
29
30 public function roles_protected() {
31 // users may tweak the roles later
32 return false;
33 }
34
91b99e80
PS
35 public function allow_enrol(stdClass $instance) {
36 // users with enrol cap may unenrol other users manually manually
37 return true;
38 }
39
df997f84
PS
40 public function allow_unenrol(stdClass $instance) {
41 // users with unenrol cap may unenrol other users manually manually
42 return true;
43 }
44
45 public function allow_manage(stdClass $instance) {
46 // users with manage cap may tweak period and status
47 return true;
48 }
49
50 /**
51 * Returns link to manual enrol UI if exists.
52 * Does the access control tests automatically.
53 *
54 * @param object $instance
55 * @return moodle_url
56 */
57 public function get_manual_enrol_link($instance) {
58 $name = $this->get_name();
59 if ($instance->enrol !== $name) {
60 throw new coding_exception('invalid enrol instance!');
61 }
62
df997f84
PS
63 if (!enrol_is_enabled($name)) {
64 return NULL;
65 }
66
67 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid, MUST_EXIST);
68
91b99e80 69 if (!has_capability('enrol/manual:manage', $context) or !has_capability('enrol/manual:enrol', $context) or !has_capability('enrol/manual:unenrol', $context)) {
df997f84
PS
70 return NULL;
71 }
72
73 return new moodle_url('/enrol/manual/manage.php', array('enrolid'=>$instance->id, 'id'=>$instance->courseid));
74 }
75
2d4b1f3e
PS
76 /**
77 * Returns enrolment instance manage link.
78 *
79 * By defaults looks for manage.php file and tests for manage capability.
80 *
81 * @param object $instance
82 * @return moodle_url;
83 */
84 public function add_course_navigation($instancesnode, stdClass $instance) {
85 $name = $this->get_name();
86
87 if ($instance->enrol !== $name) {
88 throw new coding_exception('Invalid enrol instance type!');
89 }
90
91 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
92 if (!has_capability('enrol/manual:manage', $context)) {
93 return;
94 }
95
96 $managelink = new moodle_url("/enrol/$name/manage.php", array('enrolid'=>$instance->id));
97 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
98 }
99
100 /**
101 * Returns edit icons for the page with list of instances
102 * @param stdClass $instance
103 * @return array
104 */
105 public function get_action_icons(stdClass $instance) {
106 global $OUTPUT;
107
108 $name = $this->get_name();
109 if ($instance->enrol !== $name) {
110 throw new coding_exception('invalid enrol instance!');
111 }
112
113 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
114 if (!has_capability('enrol/manual:manage', $context)) {
115 return;
116 }
117
118 $managelink = new moodle_url("/enrol/$name/manage.php", array('enrolid'=>$instance->id));
119 $icon = $OUTPUT->action_icon($managelink, new pix_icon('i/users', get_string('enrolusers', 'enrol_manual'), 'core', array('class'=>'iconsmall')));
120 return array($icon);
121 }
122
df997f84
PS
123 /**
124 * Returns link to page which may be used to add new instance of enrolment plugin in course.
125 * @param int $courseid
126 * @return moodle_url page url
127 */
128 public function get_candidate_link($courseid) {
129 global $DB;
130
131 if (!has_capability('moodle/course:enrolconfig', get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST))) {
132 return NULL;
133 }
134
135 if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'manual'))) {
136 return NULL;
137 }
138
139 return new moodle_url('/enrol/manual/addinstance.php', array('sesskey'=>sesskey(), 'id'=>$courseid));
140 }
141
142 /**
143 * Adds enrol instance UI to course edit form
144 *
145 * @param object $instance enrol instance or null if does not exist yet
146 * @param MoodleQuickForm $mform
147 * @param object $data
148 * @param object $context context of existing course or parent category if course does not exist
149 * @return void
150 */
151 public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) {
152
153 $i = isset($instance->id) ? $instance->id : 0;
154 $plugin = enrol_get_plugin('manual');
155 $header = $plugin->get_instance_name($instance);
156 $config = has_capability('enrol/manual:config', $context);
157
158 $mform->addElement('header', 'enrol_manual_header_'.$i, $header);
159
160
161 $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
162 ENROL_INSTANCE_DISABLED => get_string('no'));
163 $mform->addElement('select', 'enrol_manual_status_'.$i, get_string('status', 'enrol_manual'), $options);
9f38cdce 164 $mform->addHelpButton('enrol_manual_status_'.$i, 'status', 'enrol_manual');
df997f84
PS
165 $mform->setDefault('enrol_manual_status_'.$i, $this->get_config('status'));
166 $mform->setAdvanced('enrol_manual_status_'.$i, $this->get_config('status_adv'));
167 if (!$config) {
168 $mform->hardFreeze('enrol_manual_status_'.$i);
169 }
170
171
172 $mform->addElement('duration', 'enrol_manual_enrolperiod_'.$i, get_string('defaultperiod', 'enrol_manual'), array('optional' => true, 'defaultunit' => 86400));
173 $mform->setDefault('enrol_manual_enrolperiod_'.$i, $this->get_config('enrolperiod'));
174 $mform->setAdvanced('enrol_manual_enrolperiod_'.$i, $this->get_config('enrolperiod_adv'));
175 if (!$config) {
176 $mform->hardFreeze('enrol_manual_enrolperiod_'.$i);
177 } else {
178 $mform->disabledIf('enrol_manual_enrolperiod_'.$i, 'enrol_manual_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
179 }
180
181
182 if ($instance) {
183 $roles = get_default_enrol_roles($context, $instance->roleid);
184 } else {
185 $roles = get_default_enrol_roles($context, $this->get_config('roleid'));
186 }
187 $mform->addElement('select', 'enrol_manual_roleid_'.$i, get_string('defaultrole', 'role'), $roles);
188 $mform->setDefault('enrol_manual_roleid_'.$i, $this->get_config('roleid'));
189 $mform->setAdvanced('enrol_manual_roleid_'.$i, $this->get_config('roleid_adv'));
190 if (!$config) {
191 $mform->hardFreeze('enrol_manual_roleid_'.$i);
192 } else {
193 $mform->disabledIf('enrol_manual_roleid_'.$i, 'enrol_manual_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
194 }
195
196
197 // now add all values from enrol table
198 if ($instance) {
199 foreach($instance as $key=>$val) {
200 $data->{'enrol_manual_'.$key.'_'.$i} = $val;
201 }
202 }
203 }
204
205 /**
206 * Called after updating/inserting course.
207 *
208 * @param bool $inserted true if course just inserted
209 * @param object $course
210 * @param object $data form data
211 * @return void
212 */
213 public function course_updated($inserted, $course, $data) {
214 global $DB;
215
216 $context = get_context_instance(CONTEXT_COURSE, $course->id);
217 if (has_capability('enrol/manual:config', $context)) {
218 if ($inserted) {
219 if (isset($data->enrol_manual_status_0)) {
220 $fields = array('status'=>$data->enrol_manual_status_0);
221 if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
222 $fields['enrolperiod'] = $data->enrol_manual_enrolperiod_0;
223 $fields['roleid'] = $data->enrol_manual_roleid_0;
224 } else {
225 $fields['enrolperiod'] = $this->get_config('enrolperiod');
226 $fields['roleid'] = $this->get_config('roleid');
227 }
228 $this->add_instance($course, $fields);
229 }
230 } else {
231 $instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'));
232 foreach ($instances as $instance) {
233 $i = $instance->id;
234 if (isset($data->{'enrol_manual_status_'.$i})) {
235 $instance->status = $data->{'enrol_manual_status_'.$i};
236 $instance->timemodified = time();
237 if ($instance->status == ENROL_INSTANCE_ENABLED) {
238 $instance->enrolperiod = $data->{'enrol_manual_enrolperiod_'.$i};
239 $instance->roleid = $data->{'enrol_manual_roleid_'.$i};
240 }
241 $DB->update_record('enrol', $instance);
242 }
243 }
244 }
245
246 } else {
247 if ($inserted) {
248 if ($this->get_config('defaultenrol')) {
249 $this->add_default_instance($course);
250 }
251 } else {
252 // bad luck, user can not change anything
253 }
254 }
255 }
256
257 /**
258 * Add new instance of enrol plugin with default settings.
259 * @param object $course
260 * @return int id of new instance
261 */
262 public function add_default_instance($course) {
263 $fields = array('status'=>$this->get_config('status'), 'enrolperiod'=>$this->get_config('enrolperiod', 0), 'roleid'=>$this->get_config('roleid', 0));
264 return $this->add_instance($course, $fields);
265 }
266
267 public function cron() {
268 // TODO: deal with $CFG->longtimenosee
269 }
270}
271