on demand release 2.5beta+
[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
bfebaf64
MD
26if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28}
29
2be4d090 30require_once($CFG->libdir.'/formslib.php');
516c5eca 31require_once($CFG->libdir.'/completionlib.php');
2be4d090
MD
32
33class course_completion_form extends moodleform {
34
35 function definition() {
36 global $USER, $CFG, $DB, $js_enabled;
37
38 $courseconfig = get_config('moodlecourse');
39 $mform =& $this->_form;
40
41 $course = $this->_customdata['course'];
42 $completion = new completion_info($course);
43
44 $params = array(
45 'course' => $course->id
46 );
47
48
49/// form definition
50//--------------------------------------------------------------------------------
51
52 // Check if there is existing criteria completions
53 if ($completion->is_course_locked()) {
807cf3d0 54 $mform->addElement('header', 'completionsettingslocked', get_string('completionsettingslocked', 'completion'));
2be4d090
MD
55 $mform->addElement('static', '', '', get_string('err_settingslocked', 'completion'));
56 $mform->addElement('submit', 'settingsunlock', get_string('unlockcompletiondelete', 'completion'));
57 }
58
59 // Get array of all available aggregation methods
60 $aggregation_methods = $completion->get_aggregation_methods();
61
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());
66
67 // Course prerequisite completion criteria
eb8f89a9 68 $mform->addElement('header', 'courseprerequisites', get_string('completiondependencies', 'completion'));
2be4d090
MD
69
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 );
92
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 }
98
99 // Get category list
4e0b6025
MG
100 require_once($CFG->libdir. '/coursecatlib.php');
101 $list = coursecat::make_categories_list();
2be4d090
MD
102
103 // Get course list for select box
104 $selectbox = array();
105 $selected = array();
106 foreach ($courses as $c) {
9a5e297b 107 $selectbox[$c->id] = $list[$c->category] . ' / ' . format_string($c->fullname, true, array('context' => context_course::instance($c->id)));
2be4d090
MD
108
109 // If already selected
110 if ($c->selected) {
111 $selected[] = $c->id;
112 }
113 }
114
115 // Show multiselect box
116 $mform->addElement('select', 'criteria_course', get_string('coursesavailable', 'completion'), $selectbox, array('multiple' => 'multiple', 'size' => 6));
117
118 // Select current criteria
119 $mform->setDefault('criteria_course', $selected);
120
121 // Explain list
122 $mform->addElement('static', 'criteria_courses_explaination', '', get_string('coursesavailableexplaination', 'completion'));
123
124 } else {
125 $mform->addElement('static', 'nocourses', '', get_string('err_nocourses', 'completion'));
126 }
127
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);
132
133 // Role completion criteria
134 $mform->addElement('header', 'roles', get_string('manualcompletionby', 'completion'));
135
1f364c87 136 $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, context_course::instance($course->id, IGNORE_MISSING));
2be4d090
MD
137
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));
141
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 }
150
151 // Activity completion criteria
152 $mform->addElement('header', 'activitiescompleted', get_string('activitiescompleted', 'completion'));
153
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 }
160
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 }
169
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);
174
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);
179
180 // Completion on course grade
08f19f48 181 $mform->addElement('header', 'grade', get_string('coursegrade', 'completion'));
2be4d090
MD
182
183 // Grade enable and passing grade
184 $course_grade = $DB->get_field('grade_items', 'gradepass', array('courseid' => $course->id, 'itemtype' => 'course'));
08f19f48
AB
185 if (!$course_grade) {
186 $course_grade = '0.00000';
187 }
2be4d090
MD
188 $criteria = new completion_criteria_grade($params);
189 $criteria->config_form_display($mform, $course_grade);
190
191 // Completion on unenrolment
192 $mform->addElement('header', 'unenrolment', get_string('unenrolment', 'completion'));
193 $criteria = new completion_criteria_unenrol($params);
194 $criteria->config_form_display($mform);
195
196
197//--------------------------------------------------------------------------------
198 $this->add_action_buttons();
199//--------------------------------------------------------------------------------
200 $mform->addElement('hidden', 'id', $course->id);
201 $mform->setType('id', PARAM_INT);
202
203 // If the criteria are locked, freeze values and submit button
204 if ($completion->is_course_locked()) {
205 $except = array('settingsunlock');
206 $mform->hardFreezeAllVisibleExcept($except);
207 $mform->addElement('cancel');
208 }
209 }
210
211
212/// perform some extra moodle validation
213 function validation($data, $files) {
214 global $DB, $CFG;
215
216 $errors = parent::validation($data, $files);
217
218 return $errors;
219 }
220}
221?>