MDL-61138 javascript: add paged content widget
[moodle.git] / course / completion_form.php
blob53d4b4785c34231f1900743e52860e2225d3c359
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 if (!$completion->is_course_locked()) {
92 $this->add_checkbox_controller(1, null, null, 0);
94 foreach ($activities as $activity) {
95 $params_a = array('moduleinstance' => $activity->id);
96 $criteria = new completion_criteria_activity(array_merge($params, $params_a));
97 $criteria->config_form_display($mform, $activity);
99 $mform->addElement('static', 'criteria_role_note', '', get_string('activitiescompletednote', 'core_completion'));
101 if (count($activities) > 1) {
102 // Map aggregation methods to context-sensitive human readable dropdown menu.
103 $activityaggregationmenu = array();
104 foreach ($aggregation_methods as $methodcode => $methodname) {
105 if ($methodcode === COMPLETION_AGGREGATION_ALL) {
106 $activityaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('activityaggregation_all', 'core_completion');
107 } else if ($methodcode === COMPLETION_AGGREGATION_ANY) {
108 $activityaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('activityaggregation_any', 'core_completion');
109 } else {
110 $activityaggregationmenu[$methodcode] = $methodname;
113 $mform->addElement('select', 'activity_aggregation', get_string('activityaggregation', 'core_completion'), $activityaggregationmenu);
114 $mform->setDefault('activity_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ACTIVITY));
117 } else {
118 $mform->addElement('static', 'noactivities', '', get_string('err_noactivities', 'completion'));
121 // Course prerequisite completion criteria.
122 $label = get_string('coursecompletioncondition', 'core_completion', get_string('dependenciescompleted', 'core_completion'));
123 $mform->addElement('header', 'courseprerequisites', $label);
124 // Get the list of currently specified conditions and expand the section if some are found.
125 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_COURSE);
126 if (!empty($current)) {
127 $mform->setExpanded('courseprerequisites');
130 // Get applicable courses (prerequisites).
131 $courses = $DB->get_records_sql("
132 SELECT DISTINCT c.id, c.category, c.fullname, cc.id AS selected
133 FROM {course} c
134 LEFT JOIN {course_completion_criteria} cc ON cc.courseinstance = c.id AND cc.course = {$course->id}
135 INNER JOIN {course_completion_criteria} ccc ON ccc.course = c.id
136 WHERE c.enablecompletion = ".COMPLETION_ENABLED."
137 AND c.id <> {$course->id}");
139 if (!empty($courses)) {
140 // Get category list.
141 require_once($CFG->libdir. '/coursecatlib.php');
142 $list = coursecat::make_categories_list();
144 // Get course list for select box.
145 $selectbox = array();
146 $selected = array();
147 foreach ($courses as $c) {
148 $selectbox[$c->id] = $list[$c->category] . ' / ' . format_string($c->fullname, true,
149 array('context' => context_course::instance($c->id)));
151 // If already selected ...
152 if ($c->selected) {
153 $selected[] = $c->id;
157 // Show multiselect box.
158 $mform->addElement('select', 'criteria_course', get_string('coursesavailable', 'completion'), $selectbox,
159 array('multiple' => 'multiple', 'size' => 6));
161 // Select current criteria.
162 $mform->setDefault('criteria_course', $selected);
164 // Explain list.
165 $mform->addElement('static', 'criteria_courses_explaination', '', get_string('coursesavailableexplaination', 'completion'));
167 if (count($courses) > 1) {
168 // Map aggregation methods to context-sensitive human readable dropdown menu.
169 $courseaggregationmenu = array();
170 foreach ($aggregation_methods as $methodcode => $methodname) {
171 if ($methodcode === COMPLETION_AGGREGATION_ALL) {
172 $courseaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('courseaggregation_all', 'core_completion');
173 } else if ($methodcode === COMPLETION_AGGREGATION_ANY) {
174 $courseaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('courseaggregation_any', 'core_completion');
175 } else {
176 $courseaggregationmenu[$methodcode] = $methodname;
179 $mform->addElement('select', 'course_aggregation', get_string('courseaggregation', 'core_completion'), $courseaggregationmenu);
180 $mform->setDefault('course_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE));
183 } else {
184 $mform->addElement('static', 'nocourses', '', get_string('err_nocourses', 'completion'));
187 // Completion on date
188 $label = get_string('coursecompletioncondition', 'core_completion', get_string('completionondate', 'core_completion'));
189 $mform->addElement('header', 'date', $label);
190 // Expand the condition section if it is currently enabled.
191 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_DATE);
192 if (!empty($current)) {
193 $mform->setExpanded('date');
195 $criteria = new completion_criteria_date($params);
196 $criteria->config_form_display($mform);
198 // Completion after enrolment duration
199 $label = get_string('coursecompletioncondition', 'core_completion', get_string('enrolmentduration', 'core_completion'));
200 $mform->addElement('header', 'duration', $label);
201 // Expand the condition section if it is currently enabled.
202 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_DURATION);
203 if (!empty($current)) {
204 $mform->setExpanded('duration');
206 $criteria = new completion_criteria_duration($params);
207 $criteria->config_form_display($mform);
209 // Completion on unenrolment
210 $label = get_string('coursecompletioncondition', 'core_completion', get_string('unenrolment', 'core_completion'));
211 $mform->addElement('header', 'unenrolment', $label);
212 // Expand the condition section if it is currently enabled.
213 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_UNENROL);
214 if (!empty($current)) {
215 $mform->setExpanded('unenrolment');
217 $criteria = new completion_criteria_unenrol($params);
218 $criteria->config_form_display($mform);
220 // Completion on course grade
221 $label = get_string('coursecompletioncondition', 'core_completion', get_string('coursegrade', 'core_completion'));
222 $mform->addElement('header', 'grade', $label);
223 // Expand the condition section if it is currently enabled.
224 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_GRADE);
225 if (!empty($current)) {
226 $mform->setExpanded('grade');
228 $course_grade = $DB->get_field('grade_items', 'gradepass', array('courseid' => $course->id, 'itemtype' => 'course'));
229 if (!$course_grade) {
230 $course_grade = '0.00000';
232 $criteria = new completion_criteria_grade($params);
233 $criteria->config_form_display($mform, $course_grade);
235 // Manual self completion
236 $label = get_string('coursecompletioncondition', 'core_completion', get_string('manualselfcompletion', 'core_completion'));
237 $mform->addElement('header', 'manualselfcompletion', $label);
238 // Expand the condition section if it is currently enabled.
239 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_SELF);
240 if (!empty($current)) {
241 $mform->setExpanded('manualselfcompletion');
243 $criteria = new completion_criteria_self($params);
244 $criteria->config_form_display($mform);
245 $mform->addElement('static', 'criteria_self_note', '', get_string('manualselfcompletionnote', 'core_completion'));
247 // Role completion criteria
248 $label = get_string('coursecompletioncondition', 'core_completion', get_string('manualcompletionby', 'core_completion'));
249 $mform->addElement('header', 'roles', $label);
250 // Expand the condition section if it is currently enabled.
251 $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_ROLE);
252 if (!empty($current)) {
253 $mform->setExpanded('roles');
255 $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, context_course::instance($course->id, IGNORE_MISSING));
257 if (!empty($roles)) {
258 foreach ($roles as $role) {
259 $params_a = array('role' => $role->id);
260 $criteria = new completion_criteria_role(array_merge($params, $params_a));
261 $criteria->config_form_display($mform, $role);
263 $mform->addElement('static', 'criteria_role_note', '', get_string('manualcompletionbynote', 'core_completion'));
264 // Map aggregation methods to context-sensitive human readable dropdown menu.
265 $roleaggregationmenu = array();
266 foreach ($aggregation_methods as $methodcode => $methodname) {
267 if ($methodcode === COMPLETION_AGGREGATION_ALL) {
268 $roleaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('roleaggregation_all', 'core_completion');
269 } else if ($methodcode === COMPLETION_AGGREGATION_ANY) {
270 $roleaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('roleaggregation_any', 'core_completion');
271 } else {
272 $roleaggregationmenu[$methodcode] = $methodname;
275 $mform->addElement('select', 'role_aggregation', get_string('roleaggregation', 'core_completion'), $roleaggregationmenu);
276 $mform->setDefault('role_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE));
278 } else {
279 $mform->addElement('static', 'noroles', '', get_string('err_noroles', 'completion'));
282 // Add common action buttons.
283 $this->add_action_buttons();
285 // Add hidden fields.
286 $mform->addElement('hidden', 'id', $course->id);
287 $mform->setType('id', PARAM_INT);
289 // If the criteria are locked, freeze values and submit button.
290 if ($completion->is_course_locked()) {
291 $except = array('settingsunlock');
292 $mform->hardFreezeAllVisibleExcept($except);
293 $mform->addElement('cancel');