MDL-38559 Clean-up the course completion settings code
[moodle.git] / course / completion_form.php
CommitLineData
2be4d090
MD
1<?php
2
b3b13e99
DM
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 * Edit course completion settings - the form definition.
20 *
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
26 */
27
28defined('MOODLE_INTERNAL') || die();
bfebaf64 29
2be4d090 30require_once($CFG->libdir.'/formslib.php');
516c5eca 31require_once($CFG->libdir.'/completionlib.php');
2be4d090 32
b3b13e99
DM
33/**
34 * Defines the course completion settings form.
35 */
2be4d090
MD
36class course_completion_form extends moodleform {
37
b3b13e99
DM
38 /**
39 * Defines the form fields.
40 */
41 public function definition() {
42 global $USER, $CFG, $DB;
2be4d090
MD
43
44 $courseconfig = get_config('moodlecourse');
b3b13e99
DM
45 $mform = $this->_form;
46 $course = $this->_customdata['course'];
2be4d090
MD
47 $completion = new completion_info($course);
48
49 $params = array(
50 'course' => $course->id
51 );
52
b3b13e99 53 // Check if there are existing criteria completions.
2be4d090 54 if ($completion->is_course_locked()) {
807cf3d0 55 $mform->addElement('header', 'completionsettingslocked', get_string('completionsettingslocked', 'completion'));
2be4d090
MD
56 $mform->addElement('static', '', '', get_string('err_settingslocked', 'completion'));
57 $mform->addElement('submit', 'settingsunlock', get_string('unlockcompletiondelete', 'completion'));
58 }
59
b3b13e99 60 // Get array of all available aggregation methods.
2be4d090
MD
61 $aggregation_methods = $completion->get_aggregation_methods();
62
b3b13e99 63 // Overall criteria aggregation.
2be4d090
MD
64 $mform->addElement('header', 'overallcriteria', get_string('overallcriteriaaggregation', 'completion'));
65 $mform->addElement('select', 'overall_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
66 $mform->setDefault('overall_aggregation', $completion->get_aggregation_method());
67
b3b13e99 68 // Course prerequisite completion criteria.
eb8f89a9 69 $mform->addElement('header', 'courseprerequisites', get_string('completiondependencies', 'completion'));
2be4d090 70
b3b13e99
DM
71 // Get applicable courses (prerequisites).
72 $courses = $DB->get_records_sql("
73 SELECT DISTINCT c.id, c.category, c.fullname, cc.id AS selected
74 FROM {course} c
75 LEFT JOIN {course_completion_criteria} cc ON cc.courseinstance = c.id AND cc.course = {$course->id}
76 INNER JOIN {course_completion_criteria} ccc ON ccc.course = c.id
77 WHERE c.enablecompletion = ".COMPLETION_ENABLED."
78 AND c.id <> {$course->id}");
2be4d090
MD
79
80 if (!empty($courses)) {
81 if (count($courses) > 1) {
82 $mform->addElement('select', 'course_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
83 $mform->setDefault('course_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE));
84 }
85
b3b13e99 86 // Get category list.
4e0b6025
MG
87 require_once($CFG->libdir. '/coursecatlib.php');
88 $list = coursecat::make_categories_list();
2be4d090 89
b3b13e99 90 // Get course list for select box.
2be4d090
MD
91 $selectbox = array();
92 $selected = array();
93 foreach ($courses as $c) {
b3b13e99
DM
94 $selectbox[$c->id] = $list[$c->category] . ' / ' . format_string($c->fullname, true,
95 array('context' => context_course::instance($c->id)));
2be4d090 96
b3b13e99 97 // If already selected ...
2be4d090
MD
98 if ($c->selected) {
99 $selected[] = $c->id;
100 }
101 }
102
b3b13e99
DM
103 // Show multiselect box.
104 $mform->addElement('select', 'criteria_course', get_string('coursesavailable', 'completion'), $selectbox,
105 array('multiple' => 'multiple', 'size' => 6));
2be4d090 106
b3b13e99 107 // Select current criteria.
2be4d090
MD
108 $mform->setDefault('criteria_course', $selected);
109
b3b13e99 110 // Explain list.
2be4d090
MD
111 $mform->addElement('static', 'criteria_courses_explaination', '', get_string('coursesavailableexplaination', 'completion'));
112
113 } else {
114 $mform->addElement('static', 'nocourses', '', get_string('err_nocourses', 'completion'));
115 }
116
117 // Manual self completion
118 $mform->addElement('header', 'manualselfcompletion', get_string('manualselfcompletion', 'completion'));
119 $criteria = new completion_criteria_self($params);
120 $criteria->config_form_display($mform);
121
122 // Role completion criteria
123 $mform->addElement('header', 'roles', get_string('manualcompletionby', 'completion'));
124
1f364c87 125 $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, context_course::instance($course->id, IGNORE_MISSING));
2be4d090
MD
126
127 if (!empty($roles)) {
128 $mform->addElement('select', 'role_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
129 $mform->setDefault('role_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE));
130
131 foreach ($roles as $role) {
132 $params_a = array('role' => $role->id);
133 $criteria = new completion_criteria_role(array_merge($params, $params_a));
134 $criteria->config_form_display($mform, $role);
135 }
136 } else {
137 $mform->addElement('static', 'noroles', '', get_string('err_noroles', 'completion'));
138 }
139
140 // Activity completion criteria
141 $mform->addElement('header', 'activitiescompleted', get_string('activitiescompleted', 'completion'));
142
143 $activities = $completion->get_activities();
144 if (!empty($activities)) {
145 if (count($activities) > 1) {
146 $mform->addElement('select', 'activity_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
147 $mform->setDefault('activity_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ACTIVITY));
148 }
149
150 foreach ($activities as $activity) {
151 $params_a = array('moduleinstance' => $activity->id);
152 $criteria = new completion_criteria_activity(array_merge($params, $params_a));
153 $criteria->config_form_display($mform, $activity);
154 }
155 } else {
156 $mform->addElement('static', 'noactivities', '', get_string('err_noactivities', 'completion'));
157 }
158
159 // Completion on date
160 $mform->addElement('header', 'date', get_string('date'));
161 $criteria = new completion_criteria_date($params);
162 $criteria->config_form_display($mform);
163
164 // Completion after enrolment duration
165 $mform->addElement('header', 'duration', get_string('durationafterenrolment', 'completion'));
166 $criteria = new completion_criteria_duration($params);
167 $criteria->config_form_display($mform);
168
169 // Completion on course grade
08f19f48 170 $mform->addElement('header', 'grade', get_string('coursegrade', 'completion'));
2be4d090
MD
171
172 // Grade enable and passing grade
173 $course_grade = $DB->get_field('grade_items', 'gradepass', array('courseid' => $course->id, 'itemtype' => 'course'));
08f19f48
AB
174 if (!$course_grade) {
175 $course_grade = '0.00000';
176 }
2be4d090
MD
177 $criteria = new completion_criteria_grade($params);
178 $criteria->config_form_display($mform, $course_grade);
179
180 // Completion on unenrolment
181 $mform->addElement('header', 'unenrolment', get_string('unenrolment', 'completion'));
182 $criteria = new completion_criteria_unenrol($params);
183 $criteria->config_form_display($mform);
184
b3b13e99 185 // Add common action buttons.
2be4d090 186 $this->add_action_buttons();
b3b13e99
DM
187
188 // Add hidden fields.
2be4d090
MD
189 $mform->addElement('hidden', 'id', $course->id);
190 $mform->setType('id', PARAM_INT);
191
b3b13e99 192 // If the criteria are locked, freeze values and submit button.
2be4d090
MD
193 if ($completion->is_course_locked()) {
194 $except = array('settingsunlock');
195 $mform->hardFreezeAllVisibleExcept($except);
196 $mform->addElement('cancel');
197 }
198 }
2be4d090 199}