Commit | Line | Data |
---|---|---|
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 | ||
28 | defined('MOODLE_INTERNAL') || die(); | |
bfebaf64 | 29 | |
2be4d090 | 30 | require_once($CFG->libdir.'/formslib.php'); |
516c5eca | 31 | require_once($CFG->libdir.'/completionlib.php'); |
2be4d090 | 32 | |
b3b13e99 DM |
33 | /** |
34 | * Defines the course completion settings form. | |
35 | */ | |
2be4d090 MD |
36 | class 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. |
6e5a473c DM |
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; | |
74 | } | |
75 | } | |
76 | $mform->addElement('select', 'overall_aggregation', get_string('overallaggregation', 'core_completion'), $overallaggregationmenu); | |
2be4d090 MD |
77 | $mform->setDefault('overall_aggregation', $completion->get_aggregation_method()); |
78 | ||
6e5a473c DM |
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'); | |
86 | } | |
87 | ||
88 | $activities = $completion->get_activities(); | |
89 | if (!empty($activities)) { | |
90 | ||
6ae9dbc2 | 91 | if (!$completion->is_course_locked()) { |
92 | $this->add_checkbox_controller(1, null, null, 0); | |
93 | } | |
6e5a473c DM |
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); | |
98 | } | |
99 | $mform->addElement('static', 'criteria_role_note', '', get_string('activitiescompletednote', 'core_completion')); | |
100 | ||
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; | |
111 | } | |
112 | } | |
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)); | |
115 | } | |
116 | ||
117 | } else { | |
118 | $mform->addElement('static', 'noactivities', '', get_string('err_noactivities', 'completion')); | |
119 | } | |
120 | ||
b3b13e99 | 121 | // Course prerequisite completion criteria. |
6e5a473c DM |
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'); | |
128 | } | |
2be4d090 | 129 | |
b3b13e99 | 130 | // Get applicable courses (prerequisites). |
76f2d894 MG |
131 | $selectedcourses = $DB->get_fieldset_sql("SELECT cc.courseinstance |
132 | FROM {course_completion_criteria} cc WHERE cc.course = ?", [$course->id]); | |
133 | $hasselectablecourses = core_course_category::search_courses(['onlywithcompletion' => true], ['limit' => 2]); | |
134 | unset($hasselectablecourses[$course->id]); | |
135 | if ($hasselectablecourses) { | |
b3b13e99 | 136 | // Show multiselect box. |
76f2d894 MG |
137 | $mform->addElement('course', 'criteria_course', get_string('coursesavailable', 'completion'), |
138 | array('multiple' => 'multiple', 'onlywithcompletion' => true, 'exclude' => $course->id)); | |
139 | $mform->setDefault('criteria_course', $selectedcourses); | |
140 | ||
141 | // Map aggregation methods to context-sensitive human readable dropdown menu. | |
142 | $courseaggregationmenu = array(); | |
143 | foreach ($aggregation_methods as $methodcode => $methodname) { | |
144 | if ($methodcode === COMPLETION_AGGREGATION_ALL) { | |
145 | $courseaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('courseaggregation_all', 'core_completion'); | |
146 | } else if ($methodcode === COMPLETION_AGGREGATION_ANY) { | |
147 | $courseaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('courseaggregation_any', 'core_completion'); | |
148 | } else { | |
149 | $courseaggregationmenu[$methodcode] = $methodname; | |
6e5a473c | 150 | } |
2be4d090 | 151 | } |
76f2d894 MG |
152 | $mform->addElement('select', 'course_aggregation', get_string('courseaggregation', 'core_completion'), $courseaggregationmenu); |
153 | $mform->setDefault('course_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE)); | |
2be4d090 | 154 | } else { |
6e5a473c | 155 | $mform->addElement('static', 'nocourses', '', get_string('err_nocourses', 'completion')); |
2be4d090 MD |
156 | } |
157 | ||
158 | // Completion on date | |
6e5a473c DM |
159 | $label = get_string('coursecompletioncondition', 'core_completion', get_string('completionondate', 'core_completion')); |
160 | $mform->addElement('header', 'date', $label); | |
161 | // Expand the condition section if it is currently enabled. | |
162 | $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_DATE); | |
163 | if (!empty($current)) { | |
164 | $mform->setExpanded('date'); | |
165 | } | |
2be4d090 MD |
166 | $criteria = new completion_criteria_date($params); |
167 | $criteria->config_form_display($mform); | |
168 | ||
169 | // Completion after enrolment duration | |
6e5a473c DM |
170 | $label = get_string('coursecompletioncondition', 'core_completion', get_string('enrolmentduration', 'core_completion')); |
171 | $mform->addElement('header', 'duration', $label); | |
172 | // Expand the condition section if it is currently enabled. | |
173 | $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_DURATION); | |
174 | if (!empty($current)) { | |
175 | $mform->setExpanded('duration'); | |
176 | } | |
2be4d090 MD |
177 | $criteria = new completion_criteria_duration($params); |
178 | $criteria->config_form_display($mform); | |
179 | ||
6e5a473c DM |
180 | // Completion on unenrolment |
181 | $label = get_string('coursecompletioncondition', 'core_completion', get_string('unenrolment', 'core_completion')); | |
182 | $mform->addElement('header', 'unenrolment', $label); | |
183 | // Expand the condition section if it is currently enabled. | |
184 | $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_UNENROL); | |
185 | if (!empty($current)) { | |
186 | $mform->setExpanded('unenrolment'); | |
187 | } | |
188 | $criteria = new completion_criteria_unenrol($params); | |
189 | $criteria->config_form_display($mform); | |
2be4d090 | 190 | |
6e5a473c DM |
191 | // Completion on course grade |
192 | $label = get_string('coursecompletioncondition', 'core_completion', get_string('coursegrade', 'core_completion')); | |
193 | $mform->addElement('header', 'grade', $label); | |
194 | // Expand the condition section if it is currently enabled. | |
195 | $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_GRADE); | |
196 | if (!empty($current)) { | |
197 | $mform->setExpanded('grade'); | |
198 | } | |
2be4d090 | 199 | $course_grade = $DB->get_field('grade_items', 'gradepass', array('courseid' => $course->id, 'itemtype' => 'course')); |
08f19f48 AB |
200 | if (!$course_grade) { |
201 | $course_grade = '0.00000'; | |
202 | } | |
2be4d090 MD |
203 | $criteria = new completion_criteria_grade($params); |
204 | $criteria->config_form_display($mform, $course_grade); | |
205 | ||
6e5a473c DM |
206 | // Manual self completion |
207 | $label = get_string('coursecompletioncondition', 'core_completion', get_string('manualselfcompletion', 'core_completion')); | |
208 | $mform->addElement('header', 'manualselfcompletion', $label); | |
209 | // Expand the condition section if it is currently enabled. | |
210 | $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_SELF); | |
211 | if (!empty($current)) { | |
212 | $mform->setExpanded('manualselfcompletion'); | |
213 | } | |
214 | $criteria = new completion_criteria_self($params); | |
2be4d090 | 215 | $criteria->config_form_display($mform); |
6e5a473c DM |
216 | $mform->addElement('static', 'criteria_self_note', '', get_string('manualselfcompletionnote', 'core_completion')); |
217 | ||
218 | // Role completion criteria | |
219 | $label = get_string('coursecompletioncondition', 'core_completion', get_string('manualcompletionby', 'core_completion')); | |
220 | $mform->addElement('header', 'roles', $label); | |
221 | // Expand the condition section if it is currently enabled. | |
222 | $current = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_ROLE); | |
223 | if (!empty($current)) { | |
224 | $mform->setExpanded('roles'); | |
225 | } | |
226 | $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, context_course::instance($course->id, IGNORE_MISSING)); | |
227 | ||
228 | if (!empty($roles)) { | |
229 | foreach ($roles as $role) { | |
230 | $params_a = array('role' => $role->id); | |
231 | $criteria = new completion_criteria_role(array_merge($params, $params_a)); | |
232 | $criteria->config_form_display($mform, $role); | |
233 | } | |
234 | $mform->addElement('static', 'criteria_role_note', '', get_string('manualcompletionbynote', 'core_completion')); | |
235 | // Map aggregation methods to context-sensitive human readable dropdown menu. | |
236 | $roleaggregationmenu = array(); | |
237 | foreach ($aggregation_methods as $methodcode => $methodname) { | |
238 | if ($methodcode === COMPLETION_AGGREGATION_ALL) { | |
239 | $roleaggregationmenu[COMPLETION_AGGREGATION_ALL] = get_string('roleaggregation_all', 'core_completion'); | |
240 | } else if ($methodcode === COMPLETION_AGGREGATION_ANY) { | |
241 | $roleaggregationmenu[COMPLETION_AGGREGATION_ANY] = get_string('roleaggregation_any', 'core_completion'); | |
242 | } else { | |
243 | $roleaggregationmenu[$methodcode] = $methodname; | |
244 | } | |
245 | } | |
246 | $mform->addElement('select', 'role_aggregation', get_string('roleaggregation', 'core_completion'), $roleaggregationmenu); | |
247 | $mform->setDefault('role_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE)); | |
248 | ||
249 | } else { | |
250 | $mform->addElement('static', 'noroles', '', get_string('err_noroles', 'completion')); | |
251 | } | |
2be4d090 | 252 | |
b3b13e99 | 253 | // Add common action buttons. |
2be4d090 | 254 | $this->add_action_buttons(); |
b3b13e99 DM |
255 | |
256 | // Add hidden fields. | |
2be4d090 MD |
257 | $mform->addElement('hidden', 'id', $course->id); |
258 | $mform->setType('id', PARAM_INT); | |
259 | ||
b3b13e99 | 260 | // If the criteria are locked, freeze values and submit button. |
2be4d090 MD |
261 | if ($completion->is_course_locked()) { |
262 | $except = array('settingsunlock'); | |
263 | $mform->hardFreezeAllVisibleExcept($except); | |
264 | $mform->addElement('cancel'); | |
265 | } | |
266 | } | |
2be4d090 | 267 | } |