MDL-32249 completion: Make grade criteria more consistent
[moodle.git] / course / completion_form.php
1 <?php
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 ///////////////////////////////////////////////////////////////////////////
26 if (!defined('MOODLE_INTERNAL')) {
27     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
28 }
30 require_once($CFG->libdir.'/formslib.php');
31 require_once($CFG->libdir.'/completionlib.php');
33 class course_completion_form extends moodleform {
35     function definition() {
36         global $USER, $CFG, $DB, $js_enabled;
38         $courseconfig = get_config('moodlecourse');
39         $mform    =& $this->_form;
41         $course   = $this->_customdata['course'];
42         $completion = new completion_info($course);
44         $params = array(
45             'course'  => $course->id
46         );
49 /// form definition
50 //--------------------------------------------------------------------------------
52         // Check if there is existing criteria completions
53         if ($completion->is_course_locked()) {
54             $mform->addElement('header', '', get_string('completionsettingslocked', 'completion'));
55             $mform->addElement('static', '', '', get_string('err_settingslocked', 'completion'));
56             $mform->addElement('submit', 'settingsunlock', get_string('unlockcompletiondelete', 'completion'));
57         }
59         // Get array of all available aggregation methods
60         $aggregation_methods = $completion->get_aggregation_methods();
62         // Overall criteria aggregation
63         $mform->addElement('header', 'overallcriteria', get_string('overallcriteriaaggregation', 'completion'));
64         $mform->addElement('select', 'overall_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
65         $mform->setDefault('overall_aggregation', $completion->get_aggregation_method());
67         // Course prerequisite completion criteria
68         $mform->addElement('header', 'courseprerequisites', get_string('courseprerequisites', 'completion'));
70         // Get applicable courses
71         $courses = $DB->get_records_sql(
72             "
73                 SELECT DISTINCT
74                     c.id,
75                     c.category,
76                     c.fullname,
77                     cc.id AS selected
78                 FROM
79                     {course} c
80                 LEFT JOIN
81                     {course_completion_criteria} cc
82                  ON cc.courseinstance = c.id
83                 AND cc.course = {$course->id}
84                 INNER JOIN
85                     {course_completion_criteria} ccc
86                  ON ccc.course = c.id
87                 WHERE
88                     c.enablecompletion = ".COMPLETION_ENABLED."
89                 AND c.id <> {$course->id}
90             "
91         );
93         if (!empty($courses)) {
94             if (count($courses) > 1) {
95                 $mform->addElement('select', 'course_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
96                 $mform->setDefault('course_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE));
97             }
99             // Get category list
100             $list = array();
101             $parents = array();
102             make_categories_list($list, $parents);
104             // Get course list for select box
105             $selectbox = array();
106             $selected = array();
107             foreach ($courses as $c) {
108                 $selectbox[$c->id] = $list[$c->category] . ' / ' . format_string($c->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $c->id)));
110                 // If already selected
111                 if ($c->selected) {
112                     $selected[] = $c->id;
113                 }
114             }
116             // Show multiselect box
117             $mform->addElement('select', 'criteria_course', get_string('coursesavailable', 'completion'), $selectbox, array('multiple' => 'multiple', 'size' => 6));
119             // Select current criteria
120             $mform->setDefault('criteria_course', $selected);
122             // Explain list
123             $mform->addElement('static', 'criteria_courses_explaination', '', get_string('coursesavailableexplaination', 'completion'));
125         } else {
126             $mform->addElement('static', 'nocourses', '', get_string('err_nocourses', 'completion'));
127         }
129         // Manual self completion
130         $mform->addElement('header', 'manualselfcompletion', get_string('manualselfcompletion', 'completion'));
131         $criteria = new completion_criteria_self($params);
132         $criteria->config_form_display($mform);
134         // Role completion criteria
135         $mform->addElement('header', 'roles', get_string('manualcompletionby', 'completion'));
137         $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, get_context_instance(CONTEXT_COURSE, $course->id));
139         if (!empty($roles)) {
140             $mform->addElement('select', 'role_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
141             $mform->setDefault('role_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE));
143             foreach ($roles as $role) {
144                 $params_a = array('role' => $role->id);
145                 $criteria = new completion_criteria_role(array_merge($params, $params_a));
146                 $criteria->config_form_display($mform, $role);
147             }
148         } else {
149             $mform->addElement('static', 'noroles', '', get_string('err_noroles', 'completion'));
150         }
152         // Activity completion criteria
153         $mform->addElement('header', 'activitiescompleted', get_string('activitiescompleted', 'completion'));
155         $activities = $completion->get_activities();
156         if (!empty($activities)) {
157             if (count($activities) > 1) {
158                 $mform->addElement('select', 'activity_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
159                 $mform->setDefault('activity_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ACTIVITY));
160             }
162             foreach ($activities as $activity) {
163                 $params_a = array('moduleinstance' => $activity->id);
164                 $criteria = new completion_criteria_activity(array_merge($params, $params_a));
165                 $criteria->config_form_display($mform, $activity);
166             }
167         } else {
168             $mform->addElement('static', 'noactivities', '', get_string('err_noactivities', 'completion'));
169         }
171         // Completion on date
172         $mform->addElement('header', 'date', get_string('date'));
173         $criteria = new completion_criteria_date($params);
174         $criteria->config_form_display($mform);
176         // Completion after enrolment duration
177         $mform->addElement('header', 'duration', get_string('durationafterenrolment', 'completion'));
178         $criteria = new completion_criteria_duration($params);
179         $criteria->config_form_display($mform);
181         // Completion on course grade
182         $mform->addElement('header', 'grade', get_string('coursegrade', 'completion'));
184         // Grade enable and passing grade
185         $course_grade = $DB->get_field('grade_items', 'gradepass', array('courseid' => $course->id, 'itemtype' => 'course'));
186         if (!$course_grade) {
187             $course_grade = '0.00000';
188         }
189         $criteria = new completion_criteria_grade($params);
190         $criteria->config_form_display($mform, $course_grade);
192         // Completion on unenrolment
193         $mform->addElement('header', 'unenrolment', get_string('unenrolment', 'completion'));
194         $criteria = new completion_criteria_unenrol($params);
195         $criteria->config_form_display($mform);
198 //--------------------------------------------------------------------------------
199         $this->add_action_buttons();
200 //--------------------------------------------------------------------------------
201         $mform->addElement('hidden', 'id', $course->id);
202         $mform->setType('id', PARAM_INT);
204         // If the criteria are locked, freeze values and submit button
205         if ($completion->is_course_locked()) {
206             $except = array('settingsunlock');
207             $mform->hardFreezeAllVisibleExcept($except);
208             $mform->addElement('cancel');
209         }
210     }
213 /// perform some extra moodle validation
214     function validation($data, $files) {
215         global $DB, $CFG;
217         $errors = parent::validation($data, $files);
219         return $errors;
220     }
222 ?>