Merge branch 'MDL-48199_M27' of git://github.com/lazydaisy/moodle into MOODLE_27_STABLE
[moodle.git] / course / completion.php
blobe38bace3f985dc6045b5481dc42f4c14dd9d3f6c
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Edit course completion settings
21 * @package core_completion
22 * @category completion
23 * @copyright 2009 Catalyst IT Ltd
24 * @author Aaron Barnes <aaronb@catalyst.net.nz>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once(__DIR__.'/../config.php');
29 require_once($CFG->dirroot.'/course/lib.php');
30 require_once($CFG->libdir.'/completionlib.php');
31 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php');
32 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
33 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_unenrol.php');
34 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php');
35 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_duration.php');
36 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_grade.php');
37 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_role.php');
38 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_course.php');
39 require_once $CFG->libdir.'/gradelib.php';
40 require_once($CFG->dirroot.'/course/completion_form.php');
42 $id = required_param('id', PARAM_INT); // course id
44 // Perform some basic access control checks.
45 if ($id) {
47 if($id == SITEID){
48 // Don't allow editing of 'site course' using this form.
49 print_error('cannoteditsiteform');
52 if (!$course = $DB->get_record('course', array('id'=>$id))) {
53 print_error('invalidcourseid');
55 require_login($course);
56 require_capability('moodle/course:update', context_course::instance($course->id));
58 } else {
59 require_login();
60 print_error('needcourseid');
63 // Set up the page.
64 $PAGE->set_course($course);
65 $PAGE->set_url('/course/completion.php', array('id' => $course->id));
66 $PAGE->set_title($course->shortname);
67 $PAGE->set_heading($course->fullname);
68 $PAGE->set_pagelayout('admin');
70 // Create the settings form instance.
71 $form = new course_completion_form('completion.php?id='.$id, array('course' => $course));
73 if ($form->is_cancelled()){
74 redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
76 } else if ($data = $form->get_data()) {
77 $completion = new completion_info($course);
79 // Process criteria unlocking if requested.
80 if (!empty($data->settingsunlock)) {
81 $completion->delete_course_completion_data();
83 // Return to form (now unlocked).
84 redirect($PAGE->url);
87 // Delete old criteria.
88 $completion->clear_criteria();
90 // Loop through each criteria type and run its update_config() method.
91 global $COMPLETION_CRITERIA_TYPES;
92 foreach ($COMPLETION_CRITERIA_TYPES as $type) {
93 $class = 'completion_criteria_'.$type;
94 $criterion = new $class();
95 $criterion->update_config($data);
98 // Handle overall aggregation.
99 $aggdata = array(
100 'course' => $data->id,
101 'criteriatype' => null
103 $aggregation = new completion_aggregation($aggdata);
104 $aggregation->setMethod($data->overall_aggregation);
105 $aggregation->save();
107 // Handle activity aggregation.
108 if (empty($data->activity_aggregation)) {
109 $data->activity_aggregation = 0;
112 $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY;
113 $aggregation = new completion_aggregation($aggdata);
114 $aggregation->setMethod($data->activity_aggregation);
115 $aggregation->save();
117 // Handle course aggregation.
118 if (empty($data->course_aggregation)) {
119 $data->course_aggregation = 0;
122 $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_COURSE;
123 $aggregation = new completion_aggregation($aggdata);
124 $aggregation->setMethod($data->course_aggregation);
125 $aggregation->save();
127 // Handle role aggregation.
128 if (empty($data->role_aggregation)) {
129 $data->role_aggregation = 0;
132 $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ROLE;
133 $aggregation = new completion_aggregation($aggdata);
134 $aggregation->setMethod($data->role_aggregation);
135 $aggregation->save();
137 // Trigger an event for course module completion changed.
138 $event = \core\event\course_completion_updated::create(
139 array(
140 'courseid' => $course->id,
141 'context' => context_course::instance($course->id)
144 $event->trigger();
146 // Redirect to the course main page.
147 $url = new moodle_url('/course/view.php', array('id' => $course->id));
148 redirect($url);
151 // Print the form.
152 echo $OUTPUT->header();
153 echo $OUTPUT->heading(get_string('editcoursecompletionsettings', 'core_completion'));
155 $form->display();
157 echo $OUTPUT->footer();