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