course completion MDL-2631 Course completion feature - Thanks to Aaron Barnes and...
[moodle.git] / course / completion_form.php
CommitLineData
2be4d090
MD
1<?php
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26require_once($CFG->libdir.'/formslib.php');
27
28class course_completion_form extends moodleform {
29
30 function definition() {
31 global $USER, $CFG, $DB, $js_enabled;
32
33 $courseconfig = get_config('moodlecourse');
34 $mform =& $this->_form;
35
36 $course = $this->_customdata['course'];
37 $completion = new completion_info($course);
38
39 $params = array(
40 'course' => $course->id
41 );
42
43
44/// form definition
45//--------------------------------------------------------------------------------
46
47 // Check if there is existing criteria completions
48 if ($completion->is_course_locked()) {
49 $mform->addElement('header', '', get_string('completionsettingslocked', 'completion'));
50 $mform->addElement('static', '', '', get_string('err_settingslocked', 'completion'));
51 $mform->addElement('submit', 'settingsunlock', get_string('unlockcompletiondelete', 'completion'));
52 }
53
54 // Get array of all available aggregation methods
55 $aggregation_methods = $completion->get_aggregation_methods();
56
57 // Overall criteria aggregation
58 $mform->addElement('header', 'overallcriteria', get_string('overallcriteriaaggregation', 'completion'));
59 $mform->addElement('select', 'overall_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
60 $mform->setDefault('overall_aggregation', $completion->get_aggregation_method());
61
62 // Course prerequisite completion criteria
63 $mform->addElement('header', 'courseprerequisites', get_string('courseprerequisites', 'completion'));
64
65 // Get applicable courses
66 $courses = $DB->get_records_sql(
67 "
68 SELECT DISTINCT
69 c.id,
70 c.category,
71 c.fullname,
72 cc.id AS selected
73 FROM
74 {course} c
75 LEFT JOIN
76 {course_completion_criteria} cc
77 ON cc.courseinstance = c.id
78 AND cc.course = {$course->id}
79 INNER JOIN
80 {course_completion_criteria} ccc
81 ON ccc.course = c.id
82 WHERE
83 c.enablecompletion = ".COMPLETION_ENABLED."
84 AND c.id <> {$course->id}
85 "
86 );
87
88 if (!empty($courses)) {
89 if (count($courses) > 1) {
90 $mform->addElement('select', 'course_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
91 $mform->setDefault('course_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE));
92 }
93
94 // Get category list
95 $list = array();
96 $parents = array();
97 make_categories_list($list, $parents);
98
99 // Get course list for select box
100 $selectbox = array();
101 $selected = array();
102 foreach ($courses as $c) {
103 $selectbox[$c->id] = $list[$c->category] . ' / ' . s($c->fullname);
104
105 // If already selected
106 if ($c->selected) {
107 $selected[] = $c->id;
108 }
109 }
110
111 // Show multiselect box
112 $mform->addElement('select', 'criteria_course', get_string('coursesavailable', 'completion'), $selectbox, array('multiple' => 'multiple', 'size' => 6));
113
114 // Select current criteria
115 $mform->setDefault('criteria_course', $selected);
116
117 // Explain list
118 $mform->addElement('static', 'criteria_courses_explaination', '', get_string('coursesavailableexplaination', 'completion'));
119
120 } else {
121 $mform->addElement('static', 'nocourses', '', get_string('err_nocourses', 'completion'));
122 }
123
124 // Manual self completion
125 $mform->addElement('header', 'manualselfcompletion', get_string('manualselfcompletion', 'completion'));
126 $criteria = new completion_criteria_self($params);
127 $criteria->config_form_display($mform);
128
129 // Role completion criteria
130 $mform->addElement('header', 'roles', get_string('manualcompletionby', 'completion'));
131
132 $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, get_context_instance(CONTEXT_COURSE, $course->id));
133
134 if (!empty($roles)) {
135 $mform->addElement('select', 'role_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
136 $mform->setDefault('role_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE));
137
138 foreach ($roles as $role) {
139 $params_a = array('role' => $role->id);
140 $criteria = new completion_criteria_role(array_merge($params, $params_a));
141 $criteria->config_form_display($mform, $role);
142 }
143 } else {
144 $mform->addElement('static', 'noroles', '', get_string('err_noroles', 'completion'));
145 }
146
147 // Activity completion criteria
148 $mform->addElement('header', 'activitiescompleted', get_string('activitiescompleted', 'completion'));
149
150 $activities = $completion->get_activities();
151 if (!empty($activities)) {
152 if (count($activities) > 1) {
153 $mform->addElement('select', 'activity_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
154 $mform->setDefault('activity_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ACTIVITY));
155 }
156
157 foreach ($activities as $activity) {
158 $params_a = array('moduleinstance' => $activity->id);
159 $criteria = new completion_criteria_activity(array_merge($params, $params_a));
160 $criteria->config_form_display($mform, $activity);
161 }
162 } else {
163 $mform->addElement('static', 'noactivities', '', get_string('err_noactivities', 'completion'));
164 }
165
166 // Completion on date
167 $mform->addElement('header', 'date', get_string('date'));
168 $criteria = new completion_criteria_date($params);
169 $criteria->config_form_display($mform);
170
171 // Completion after enrolment duration
172 $mform->addElement('header', 'duration', get_string('durationafterenrolment', 'completion'));
173 $criteria = new completion_criteria_duration($params);
174 $criteria->config_form_display($mform);
175
176 // Completion on course grade
177 $mform->addElement('header', 'grade', get_string('grade'));
178
179 // Grade enable and passing grade
180 $course_grade = $DB->get_field('grade_items', 'gradepass', array('courseid' => $course->id, 'itemtype' => 'course'));
181 $criteria = new completion_criteria_grade($params);
182 $criteria->config_form_display($mform, $course_grade);
183
184 // Completion on unenrolment
185 $mform->addElement('header', 'unenrolment', get_string('unenrolment', 'completion'));
186 $criteria = new completion_criteria_unenrol($params);
187 $criteria->config_form_display($mform);
188
189
190//--------------------------------------------------------------------------------
191 $this->add_action_buttons();
192//--------------------------------------------------------------------------------
193 $mform->addElement('hidden', 'id', $course->id);
194 $mform->setType('id', PARAM_INT);
195
196 // If the criteria are locked, freeze values and submit button
197 if ($completion->is_course_locked()) {
198 $except = array('settingsunlock');
199 $mform->hardFreezeAllVisibleExcept($except);
200 $mform->addElement('cancel');
201 }
202 }
203
204
205/// perform some extra moodle validation
206 function validation($data, $files) {
207 global $DB, $CFG;
208
209 $errors = parent::validation($data, $files);
210
211 return $errors;
212 }
213}
214?>