Updated the HEAD build version to 20100731
[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
63 if ($instance->courseid == SITEID) {
64 return NULL;
65 }
66
67 if (!enrol_is_enabled($name)) {
68 return NULL;
69 }
70
71 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid, MUST_EXIST);
72
91b99e80 73 if (!has_capability('enrol/manual:manage', $context) or !has_capability('enrol/manual:enrol', $context) or !has_capability('enrol/manual:unenrol', $context)) {
df997f84
PS
74 return NULL;
75 }
76
77 return new moodle_url('/enrol/manual/manage.php', array('enrolid'=>$instance->id, 'id'=>$instance->courseid));
78 }
79
80 /**
81 * Returns link to page which may be used to add new instance of enrolment plugin in course.
82 * @param int $courseid
83 * @return moodle_url page url
84 */
85 public function get_candidate_link($courseid) {
86 global $DB;
87
88 if (!has_capability('moodle/course:enrolconfig', get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST))) {
89 return NULL;
90 }
91
92 if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'manual'))) {
93 return NULL;
94 }
95
96 return new moodle_url('/enrol/manual/addinstance.php', array('sesskey'=>sesskey(), 'id'=>$courseid));
97 }
98
99 /**
100 * Adds enrol instance UI to course edit form
101 *
102 * @param object $instance enrol instance or null if does not exist yet
103 * @param MoodleQuickForm $mform
104 * @param object $data
105 * @param object $context context of existing course or parent category if course does not exist
106 * @return void
107 */
108 public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) {
109
110 $i = isset($instance->id) ? $instance->id : 0;
111 $plugin = enrol_get_plugin('manual');
112 $header = $plugin->get_instance_name($instance);
113 $config = has_capability('enrol/manual:config', $context);
114
115 $mform->addElement('header', 'enrol_manual_header_'.$i, $header);
116
117
118 $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
119 ENROL_INSTANCE_DISABLED => get_string('no'));
120 $mform->addElement('select', 'enrol_manual_status_'.$i, get_string('status', 'enrol_manual'), $options);
9f38cdce 121 $mform->addHelpButton('enrol_manual_status_'.$i, 'status', 'enrol_manual');
df997f84
PS
122 $mform->setDefault('enrol_manual_status_'.$i, $this->get_config('status'));
123 $mform->setAdvanced('enrol_manual_status_'.$i, $this->get_config('status_adv'));
124 if (!$config) {
125 $mform->hardFreeze('enrol_manual_status_'.$i);
126 }
127
128
129 $mform->addElement('duration', 'enrol_manual_enrolperiod_'.$i, get_string('defaultperiod', 'enrol_manual'), array('optional' => true, 'defaultunit' => 86400));
130 $mform->setDefault('enrol_manual_enrolperiod_'.$i, $this->get_config('enrolperiod'));
131 $mform->setAdvanced('enrol_manual_enrolperiod_'.$i, $this->get_config('enrolperiod_adv'));
132 if (!$config) {
133 $mform->hardFreeze('enrol_manual_enrolperiod_'.$i);
134 } else {
135 $mform->disabledIf('enrol_manual_enrolperiod_'.$i, 'enrol_manual_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
136 }
137
138
139 if ($instance) {
140 $roles = get_default_enrol_roles($context, $instance->roleid);
141 } else {
142 $roles = get_default_enrol_roles($context, $this->get_config('roleid'));
143 }
144 $mform->addElement('select', 'enrol_manual_roleid_'.$i, get_string('defaultrole', 'role'), $roles);
145 $mform->setDefault('enrol_manual_roleid_'.$i, $this->get_config('roleid'));
146 $mform->setAdvanced('enrol_manual_roleid_'.$i, $this->get_config('roleid_adv'));
147 if (!$config) {
148 $mform->hardFreeze('enrol_manual_roleid_'.$i);
149 } else {
150 $mform->disabledIf('enrol_manual_roleid_'.$i, 'enrol_manual_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
151 }
152
153
154 // now add all values from enrol table
155 if ($instance) {
156 foreach($instance as $key=>$val) {
157 $data->{'enrol_manual_'.$key.'_'.$i} = $val;
158 }
159 }
160 }
161
162 /**
163 * Called after updating/inserting course.
164 *
165 * @param bool $inserted true if course just inserted
166 * @param object $course
167 * @param object $data form data
168 * @return void
169 */
170 public function course_updated($inserted, $course, $data) {
171 global $DB;
172
173 $context = get_context_instance(CONTEXT_COURSE, $course->id);
174 if (has_capability('enrol/manual:config', $context)) {
175 if ($inserted) {
176 if (isset($data->enrol_manual_status_0)) {
177 $fields = array('status'=>$data->enrol_manual_status_0);
178 if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
179 $fields['enrolperiod'] = $data->enrol_manual_enrolperiod_0;
180 $fields['roleid'] = $data->enrol_manual_roleid_0;
181 } else {
182 $fields['enrolperiod'] = $this->get_config('enrolperiod');
183 $fields['roleid'] = $this->get_config('roleid');
184 }
185 $this->add_instance($course, $fields);
186 }
187 } else {
188 $instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'));
189 foreach ($instances as $instance) {
190 $i = $instance->id;
191 if (isset($data->{'enrol_manual_status_'.$i})) {
192 $instance->status = $data->{'enrol_manual_status_'.$i};
193 $instance->timemodified = time();
194 if ($instance->status == ENROL_INSTANCE_ENABLED) {
195 $instance->enrolperiod = $data->{'enrol_manual_enrolperiod_'.$i};
196 $instance->roleid = $data->{'enrol_manual_roleid_'.$i};
197 }
198 $DB->update_record('enrol', $instance);
199 }
200 }
201 }
202
203 } else {
204 if ($inserted) {
205 if ($this->get_config('defaultenrol')) {
206 $this->add_default_instance($course);
207 }
208 } else {
209 // bad luck, user can not change anything
210 }
211 }
212 }
213
214 /**
215 * Add new instance of enrol plugin with default settings.
216 * @param object $course
217 * @return int id of new instance
218 */
219 public function add_default_instance($course) {
220 $fields = array('status'=>$this->get_config('status'), 'enrolperiod'=>$this->get_config('enrolperiod', 0), 'roleid'=>$this->get_config('roleid', 0));
221 return $this->add_instance($course, $fields);
222 }
223
224 public function cron() {
225 // TODO: deal with $CFG->longtimenosee
226 }
227}
228