MDL-26257 Shibboleth auth: run logout handler only if logged in via Shibboleth
[moodle.git] / course / completion_form.php
blobd27c96b086beabe7b1991b1158ae2af088d78592
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
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
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'));
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(
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}
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));
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] . ' / ' . s($c->fullname);
110 // If already selected
111 if ($c->selected) {
112 $selected[] = $c->id;
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'));
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);
148 } else {
149 $mform->addElement('static', 'noroles', '', get_string('err_noroles', 'completion'));
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));
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);
167 } else {
168 $mform->addElement('static', 'noactivities', '', get_string('err_noactivities', 'completion'));
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('grade'));
184 // Grade enable and passing grade
185 $course_grade = $DB->get_field('grade_items', 'gradepass', array('courseid' => $course->id, 'itemtype' => 'course'));
186 $criteria = new completion_criteria_grade($params);
187 $criteria->config_form_display($mform, $course_grade);
189 // Completion on unenrolment
190 $mform->addElement('header', 'unenrolment', get_string('unenrolment', 'completion'));
191 $criteria = new completion_criteria_unenrol($params);
192 $criteria->config_form_display($mform);
195 //--------------------------------------------------------------------------------
196 $this->add_action_buttons();
197 //--------------------------------------------------------------------------------
198 $mform->addElement('hidden', 'id', $course->id);
199 $mform->setType('id', PARAM_INT);
201 // If the criteria are locked, freeze values and submit button
202 if ($completion->is_course_locked()) {
203 $except = array('settingsunlock');
204 $mform->hardFreezeAllVisibleExcept($except);
205 $mform->addElement('cancel');
210 /// perform some extra moodle validation
211 function validation($data, $files) {
212 global $DB, $CFG;
214 $errors = parent::validation($data, $files);
216 return $errors;