MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[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');
32 class course_completion_form extends moodleform {
34     function definition() {
35         global $USER, $CFG, $DB, $js_enabled;
37         $courseconfig = get_config('moodlecourse');
38         $mform    =& $this->_form;
40         $course   = $this->_customdata['course'];
41         $completion = new completion_info($course);
43         $params = array(
44             'course'  => $course->id
45         );
48 /// form definition
49 //--------------------------------------------------------------------------------
51         // Check if there is existing criteria completions
52         if ($completion->is_course_locked()) {
53             $mform->addElement('header', '', get_string('completionsettingslocked', 'completion'));
54             $mform->addElement('static', '', '', get_string('err_settingslocked', 'completion'));
55             $mform->addElement('submit', 'settingsunlock', get_string('unlockcompletiondelete', 'completion'));
56         }
58         // Get array of all available aggregation methods
59         $aggregation_methods = $completion->get_aggregation_methods();
61         // Overall criteria aggregation
62         $mform->addElement('header', 'overallcriteria', get_string('overallcriteriaaggregation', 'completion'));
63         $mform->addElement('select', 'overall_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
64         $mform->setDefault('overall_aggregation', $completion->get_aggregation_method());
66         // Course prerequisite completion criteria
67         $mform->addElement('header', 'courseprerequisites', get_string('courseprerequisites', 'completion'));
69         // Get applicable courses
70         $courses = $DB->get_records_sql(
71             "
72                 SELECT DISTINCT
73                     c.id,
74                     c.category,
75                     c.fullname,
76                     cc.id AS selected
77                 FROM
78                     {course} c
79                 LEFT JOIN
80                     {course_completion_criteria} cc
81                  ON cc.courseinstance = c.id
82                 AND cc.course = {$course->id}
83                 INNER JOIN
84                     {course_completion_criteria} ccc
85                  ON ccc.course = c.id
86                 WHERE
87                     c.enablecompletion = ".COMPLETION_ENABLED."
88                 AND c.id <> {$course->id}
89             "
90         );
92         if (!empty($courses)) {
93             if (count($courses) > 1) {
94                 $mform->addElement('select', 'course_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
95                 $mform->setDefault('course_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_COURSE));
96             }
98             // Get category list
99             $list = array();
100             $parents = array();
101             make_categories_list($list, $parents);
103             // Get course list for select box
104             $selectbox = array();
105             $selected = array();
106             foreach ($courses as $c) {
107                 $selectbox[$c->id] = $list[$c->category] . ' / ' . s($c->fullname);
109                 // If already selected
110                 if ($c->selected) {
111                     $selected[] = $c->id;
112                 }
113             }
115             // Show multiselect box
116             $mform->addElement('select', 'criteria_course', get_string('coursesavailable', 'completion'), $selectbox, array('multiple' => 'multiple', 'size' => 6));
118             // Select current criteria
119             $mform->setDefault('criteria_course', $selected);
121             // Explain list
122             $mform->addElement('static', 'criteria_courses_explaination', '', get_string('coursesavailableexplaination', 'completion'));
124         } else {
125             $mform->addElement('static', 'nocourses', '', get_string('err_nocourses', 'completion'));
126         }
128         // Manual self completion
129         $mform->addElement('header', 'manualselfcompletion', get_string('manualselfcompletion', 'completion'));
130         $criteria = new completion_criteria_self($params);
131         $criteria->config_form_display($mform);
133         // Role completion criteria
134         $mform->addElement('header', 'roles', get_string('manualcompletionby', 'completion'));
136         $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, get_context_instance(CONTEXT_COURSE, $course->id));
138         if (!empty($roles)) {
139             $mform->addElement('select', 'role_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
140             $mform->setDefault('role_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ROLE));
142             foreach ($roles as $role) {
143                 $params_a = array('role' => $role->id);
144                 $criteria = new completion_criteria_role(array_merge($params, $params_a));
145                 $criteria->config_form_display($mform, $role);
146             }
147         } else {
148             $mform->addElement('static', 'noroles', '', get_string('err_noroles', 'completion'));
149         }
151         // Activity completion criteria
152         $mform->addElement('header', 'activitiescompleted', get_string('activitiescompleted', 'completion'));
154         $activities = $completion->get_activities();
155         if (!empty($activities)) {
156             if (count($activities) > 1) {
157                 $mform->addElement('select', 'activity_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods);
158                 $mform->setDefault('activity_aggregation', $completion->get_aggregation_method(COMPLETION_CRITERIA_TYPE_ACTIVITY));
159             }
161             foreach ($activities as $activity) {
162                 $params_a = array('moduleinstance' => $activity->id);
163                 $criteria = new completion_criteria_activity(array_merge($params, $params_a));
164                 $criteria->config_form_display($mform, $activity);
165             }
166         } else {
167             $mform->addElement('static', 'noactivities', '', get_string('err_noactivities', 'completion'));
168         }
170         // Completion on date
171         $mform->addElement('header', 'date', get_string('date'));
172         $criteria = new completion_criteria_date($params);
173         $criteria->config_form_display($mform);
175         // Completion after enrolment duration
176         $mform->addElement('header', 'duration', get_string('durationafterenrolment', 'completion'));
177         $criteria = new completion_criteria_duration($params);
178         $criteria->config_form_display($mform);
180         // Completion on course grade
181         $mform->addElement('header', 'grade', get_string('grade'));
183         // Grade enable and passing grade
184         $course_grade = $DB->get_field('grade_items', 'gradepass', array('courseid' => $course->id, 'itemtype' => 'course'));
185         $criteria = new completion_criteria_grade($params);
186         $criteria->config_form_display($mform, $course_grade);
188         // Completion on unenrolment
189         $mform->addElement('header', 'unenrolment', get_string('unenrolment', 'completion'));
190         $criteria = new completion_criteria_unenrol($params);
191         $criteria->config_form_display($mform);
194 //--------------------------------------------------------------------------------
195         $this->add_action_buttons();
196 //--------------------------------------------------------------------------------
197         $mform->addElement('hidden', 'id', $course->id);
198         $mform->setType('id', PARAM_INT);
200         // If the criteria are locked, freeze values and submit button
201         if ($completion->is_course_locked()) {
202             $except = array('settingsunlock');
203             $mform->hardFreezeAllVisibleExcept($except);
204             $mform->addElement('cancel');
205         }
206     }
209 /// perform some extra moodle validation
210     function validation($data, $files) {
211         global $DB, $CFG;
213         $errors = parent::validation($data, $files);
215         return $errors;
216     }
218 ?>