MDL-37516 Conditional activities: user field condition display fixes
[moodle.git] / course / completion_form.php
blobce4880a7e533f478f801838ad5c34a6307a2043e
1 <?php
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.
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/>.
18 /**
19 * Edit course completion settings - the form definition.
21 * @package core_completion
22 * @category completion
23 * @copyright 2009 Catalyst IT Ltd
24 * @author Aaron Barnes <aaronb@catalyst.net.nz>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->libdir.'/formslib.php');
31 require_once($CFG->libdir.'/completionlib.php');
33 /**
34 * Defines the course completion settings form.
36 class course_completion_form extends moodleform {
38 /**
39 * Defines the form fields.
41 public function definition() {
42 global $USER, $CFG, $DB;
44 $courseconfig = get_config('moodlecourse');
45 $mform = $this->_form;
46 $course = $this->_customdata['course'];
47 $completion = new completion_info($course);
49 $params = array(
50 'course' => $course->id
53 // Check if there are existing criteria completions.
54 if ($completion->is_course_locked()) {
55 $mform->addElement('header', 'completionsettingslocked', get_string('completionsettingslocked', 'completion'));
56 $mform->addElement('static', '', '', get_string('err_settingslocked', 'completion'));
57 $mform->addElement('submit', 'settingsunlock', get_string('unlockcompletiondelete', 'completion'));
60 // Get array of all available aggregation methods.
61 $aggregation_methods = $completion->get_aggregation_methods();
63 // Overall criteria aggregation.
64 $mform->addElement('header', 'overallcriteria', get_string('general', 'core_form'));
65 // Map aggregation methods to context-sensitive human readable dropdown menu.
66 $overallaggregationmenu = array();
67 foreach ($aggregation_methods as $methodcode => $methodname) {
68 if ($methodcode === COMPLETION_AGGREGATION_ALL) {
69 $overallaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('overallaggregation_all', 'core_completion');
70 } else if ($methodcode === COMPLETION_AGGREGATION_ANY) {
71 $overallaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('overallaggregation_any', 'core_completion');
72 } else {
73 $overallaggregationmenu[$methodcode] = $methodname;
76 $mform->addElement('select', 'overall_aggregation', get_string('overallaggregation', 'core_completion'), $overallaggregationmenu);
77 $mform->setDefault('overall_aggregation', $completion->get_aggregation_method());
79 // Activity completion criteria
80 $label = get_string('coursecompletioncondition', 'core_completion', get_string('activitiescompleted', 'core_completion'));
81 $mform->addElement('header', 'activitiescompleted', $label);
82 // Get the list of currently specified conditions and expand the section if some are found.
83 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_ACTIVITY);
84 if (!empty($current)) {
85 $mform->setExpanded('activitiescompleted');
88 $activities = $completion->get_activities();
89 if (!empty($activities)) {
91 foreach ($activities as $activity) {
92 $params_a = array('moduleinstance' => $activity->id);
93 $criteria = new completion_criteria_activity(array_merge($params, $params_a));
94 $criteria->config_form_display($mform, $activity);
96 $mform->addElement('static', 'criteria_role_note', '', get_string('activitiescompletednote', 'core_completion'));
98 if (count($activities) > 1) {
99 // Map aggregation methods to context-sensitive human readable dropdown menu.
100 $activityaggregationmenu = array();
101 foreach ($aggregation_methods as $methodcode => $methodname) {
102 if ($methodcode === COMPLETION_AGGREGATION_ALL) {
103 $activityaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('activityaggregation_all', 'core_completion');
104 } else if ($methodcode === COMPLETION_AGGREGATION_ANY) {
105 $activityaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('activityaggregation_any', 'core_completion');
106 } else {
107 $activityaggregationmenu[$methodcode] = $methodname;
110 $mform->addElement('select', 'activity_aggregation', get_string('activityaggregation', 'core_completion'), $activityaggregationmenu);
111 $mform->setDefault('activity_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ACTIVITY));
114 } else {
115 $mform->addElement('static', 'noactivities', '', get_string('err_noactivities', 'completion'));
118 // Course prerequisite completion criteria.
119 $label = get_string('coursecompletioncondition', 'core_completion', get_string('dependenciescompleted', 'core_completion'));
120 $mform->addElement('header', 'courseprerequisites', $label);
121 // Get the list of currently specified conditions and expand the section if some are found.
122 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_COURSE);
123 if (!empty($current)) {
124 $mform->setExpanded('courseprerequisites');
127 // Get applicable courses (prerequisites).
128 $courses = $DB->get_records_sql("
129 SELECT DISTINCT c.id, c.category, c.fullname, cc.id AS selected
130 FROM {course} c
131 LEFT JOIN {course_completion_criteria} cc ON cc.courseinstance = c.id AND cc.course = {$course->id}
132 INNER JOIN {course_completion_criteria} ccc ON ccc.course = c.id
133 WHERE c.enablecompletion = ".COMPLETION_ENABLED."
134 AND c.id <> {$course->id}");
136 if (!empty($courses)) {
137 // Get category list.
138 require_once($CFG->libdir. '/coursecatlib.php');
139 $list = coursecat::make_categories_list();
141 // Get course list for select box.
142 $selectbox = array();
143 $selected = array();
144 foreach ($courses as $c) {
145 $selectbox[$c->id] = $list[$c->category] . ' / ' . format_string($c->fullname, true,
146 array('context' => context_course::instance($c->id)));
148 // If already selected ...
149 if ($c->selected) {
150 $selected[] = $c->id;
154 // Show multiselect box.
155 $mform->addElement('select', 'criteria_course', get_string('coursesavailable', 'completion'), $selectbox,
156 array('multiple' => 'multiple', 'size' => 6));
158 // Select current criteria.
159 $mform->setDefault('criteria_course', $selected);
161 // Explain list.
162 $mform->addElement('static', 'criteria_courses_explaination', '', get_string('coursesavailableexplaination', 'completion'));
164 if (count($courses) > 1) {
165 // Map aggregation methods to context-sensitive human readable dropdown menu.
166 $courseaggregationmenu = array();
167 foreach ($aggregation_methods as $methodcode => $methodname) {
168 if ($methodcode === COMPLETION_AGGREGATION_ALL) {
169 $courseaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('courseaggregation_all', 'core_completion');
170 } else if ($methodcode === COMPLETION_AGGREGATION_ANY) {
171 $courseaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('courseaggregation_any', 'core_completion');
172 } else {
173 $courseaggregationmenu[$methodcode] = $methodname;
176 $mform->addElement('select', 'course_aggregation', get_string('courseaggregation', 'core_completion'), $courseaggregationmenu);
177 $mform->setDefault('course_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE));
180 } else {
181 $mform->addElement('static', 'nocourses', '', get_string('err_nocourses', 'completion'));
184 // Completion on date
185 $label = get_string('coursecompletioncondition', 'core_completion', get_string('completionondate', 'core_completion'));
186 $mform->addElement('header', 'date', $label);
187 // Expand the condition section if it is currently enabled.
188 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_DATE);
189 if (!empty($current)) {
190 $mform->setExpanded('date');
192 $criteria = new completion_criteria_date($params);
193 $criteria->config_form_display($mform);
195 // Completion after enrolment duration
196 $label = get_string('coursecompletioncondition', 'core_completion', get_string('enrolmentduration', 'core_completion'));
197 $mform->addElement('header', 'duration', $label);
198 // Expand the condition section if it is currently enabled.
199 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_DURATION);
200 if (!empty($current)) {
201 $mform->setExpanded('duration');
203 $criteria = new completion_criteria_duration($params);
204 $criteria->config_form_display($mform);
206 // Completion on unenrolment
207 $label = get_string('coursecompletioncondition', 'core_completion', get_string('unenrolment', 'core_completion'));
208 $mform->addElement('header', 'unenrolment', $label);
209 // Expand the condition section if it is currently enabled.
210 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_UNENROL);
211 if (!empty($current)) {
212 $mform->setExpanded('unenrolment');
214 $criteria = new completion_criteria_unenrol($params);
215 $criteria->config_form_display($mform);
217 // Completion on course grade
218 $label = get_string('coursecompletioncondition', 'core_completion', get_string('coursegrade', 'core_completion'));
219 $mform->addElement('header', 'grade', $label);
220 // Expand the condition section if it is currently enabled.
221 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_GRADE);
222 if (!empty($current)) {
223 $mform->setExpanded('grade');
225 $course_grade = $DB->get_field('grade_items', 'gradepass', array('courseid' => $course->id, 'itemtype' => 'course'));
226 if (!$course_grade) {
227 $course_grade = '0.00000';
229 $criteria = new completion_criteria_grade($params);
230 $criteria->config_form_display($mform, $course_grade);
232 // Manual self completion
233 $label = get_string('coursecompletioncondition', 'core_completion', get_string('manualselfcompletion', 'core_completion'));
234 $mform->addElement('header', 'manualselfcompletion', $label);
235 // Expand the condition section if it is currently enabled.
236 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_SELF);
237 if (!empty($current)) {
238 $mform->setExpanded('manualselfcompletion');
240 $criteria = new completion_criteria_self($params);
241 $criteria->config_form_display($mform);
242 $mform->addElement('static', 'criteria_self_note', '', get_string('manualselfcompletionnote', 'core_completion'));
244 // Role completion criteria
245 $label = get_string('coursecompletioncondition', 'core_completion', get_string('manualcompletionby', 'core_completion'));
246 $mform->addElement('header', 'roles', $label);
247 // Expand the condition section if it is currently enabled.
248 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_ROLE);
249 if (!empty($current)) {
250 $mform->setExpanded('roles');
252 $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, context_course::instance($course->id, IGNORE_MISSING));
254 if (!empty($roles)) {
255 foreach ($roles as $role) {
256 $params_a = array('role' => $role->id);
257 $criteria = new completion_criteria_role(array_merge($params, $params_a));
258 $criteria->config_form_display($mform, $role);
260 $mform->addElement('static', 'criteria_role_note', '', get_string('manualcompletionbynote', 'core_completion'));
261 // Map aggregation methods to context-sensitive human readable dropdown menu.
262 $roleaggregationmenu = array();
263 foreach ($aggregation_methods as $methodcode => $methodname) {
264 if ($methodcode === COMPLETION_AGGREGATION_ALL) {
265 $roleaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('roleaggregation_all', 'core_completion');
266 } else if ($methodcode === COMPLETION_AGGREGATION_ANY) {
267 $roleaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('roleaggregation_any', 'core_completion');
268 } else {
269 $roleaggregationmenu[$methodcode] = $methodname;
272 $mform->addElement('select', 'role_aggregation', get_string('roleaggregation', 'core_completion'), $roleaggregationmenu);
273 $mform->setDefault('role_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE));
275 } else {
276 $mform->addElement('static', 'noroles', '', get_string('err_noroles', 'completion'));
279 // Add common action buttons.
280 $this->add_action_buttons();
282 // Add hidden fields.
283 $mform->addElement('hidden', 'id', $course->id);
284 $mform->setType('id', PARAM_INT);
286 // If the criteria are locked, freeze values and submit button.
287 if ($completion->is_course_locked()) {
288 $except = array('settingsunlock');
289 $mform->hardFreezeAllVisibleExcept($except);
290 $mform->addElement('cancel');