MDL-67264 core_course: Begin set up for Activity chooser
[moodle.git] / course / completion.php
CommitLineData
2be4d090
MD
1<?php
2
b3b13e99
DM
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.
14//
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/>.
17
18/**
19 * Edit course completion settings
20 *
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
26 */
27
28require_once(__DIR__.'/../config.php');
29require_once($CFG->dirroot.'/course/lib.php');
2be4d090 30require_once($CFG->libdir.'/completionlib.php');
60829d80
AB
31require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php');
32require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
33require_once($CFG->dirroot.'/completion/criteria/completion_criteria_unenrol.php');
34require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php');
35require_once($CFG->dirroot.'/completion/criteria/completion_criteria_duration.php');
36require_once($CFG->dirroot.'/completion/criteria/completion_criteria_grade.php');
37require_once($CFG->dirroot.'/completion/criteria/completion_criteria_role.php');
38require_once($CFG->dirroot.'/completion/criteria/completion_criteria_course.php');
2be4d090 39require_once $CFG->libdir.'/gradelib.php';
b3b13e99 40require_once($CFG->dirroot.'/course/completion_form.php');
2be4d090
MD
41
42$id = required_param('id', PARAM_INT); // course id
43
b3b13e99
DM
44// Perform some basic access control checks.
45if ($id) {
2be4d090
MD
46
47 if($id == SITEID){
b3b13e99 48 // Don't allow editing of 'site course' using this form.
2be4d090
MD
49 print_error('cannoteditsiteform');
50 }
51
52 if (!$course = $DB->get_record('course', array('id'=>$id))) {
53 print_error('invalidcourseid');
54 }
cdbea7ee 55 require_login($course);
0cbc248d
MG
56 $context = context_course::instance($course->id);
57 if (!has_capability('moodle/course:update', $context)) {
a64a9f9c
MG
58 // User is not allowed to modify course completion.
59 // Check if they can see default completion or edit bulk completion and redirect there.
60 if ($tabs = core_completion\manager::get_available_completion_tabs($course)) {
61 redirect($tabs[0]->link);
0cbc248d
MG
62 } else {
63 require_capability('moodle/course:update', $context);
64 }
65 }
2be4d090
MD
66
67} else {
68 require_login();
69 print_error('needcourseid');
70}
71
b3b13e99 72// Set up the page.
2be4d090
MD
73$PAGE->set_course($course);
74$PAGE->set_url('/course/completion.php', array('id' => $course->id));
2be4d090
MD
75$PAGE->set_title($course->shortname);
76$PAGE->set_heading($course->fullname);
566889aa 77$PAGE->set_pagelayout('admin');
2be4d090 78
b3b13e99
DM
79// Create the settings form instance.
80$form = new course_completion_form('completion.php?id='.$id, array('course' => $course));
2be4d090 81
2be4d090
MD
82if ($form->is_cancelled()){
83 redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
84
85} else if ($data = $form->get_data()) {
2be4d090
MD
86 $completion = new completion_info($course);
87
b3b13e99 88 // Process criteria unlocking if requested.
2be4d090 89 if (!empty($data->settingsunlock)) {
2be4d090
MD
90 $completion->delete_course_completion_data();
91
b3b13e99
DM
92 // Return to form (now unlocked).
93 redirect($PAGE->url);
2be4d090
MD
94 }
95
b3b13e99 96 // Delete old criteria.
2be4d090
MD
97 $completion->clear_criteria();
98
b3b13e99 99 // Loop through each criteria type and run its update_config() method.
2be4d090
MD
100 global $COMPLETION_CRITERIA_TYPES;
101 foreach ($COMPLETION_CRITERIA_TYPES as $type) {
102 $class = 'completion_criteria_'.$type;
103 $criterion = new $class();
104 $criterion->update_config($data);
105 }
106
b3b13e99 107 // Handle overall aggregation.
dbfcf440
AB
108 $aggdata = array(
109 'course' => $data->id,
110 'criteriatype' => null
111 );
112 $aggregation = new completion_aggregation($aggdata);
2be4d090 113 $aggregation->setMethod($data->overall_aggregation);
dbfcf440 114 $aggregation->save();
2be4d090 115
b3b13e99 116 // Handle activity aggregation.
2be4d090
MD
117 if (empty($data->activity_aggregation)) {
118 $data->activity_aggregation = 0;
119 }
120
dbfcf440
AB
121 $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY;
122 $aggregation = new completion_aggregation($aggdata);
2be4d090 123 $aggregation->setMethod($data->activity_aggregation);
dbfcf440 124 $aggregation->save();
2be4d090 125
b3b13e99 126 // Handle course aggregation.
2be4d090
MD
127 if (empty($data->course_aggregation)) {
128 $data->course_aggregation = 0;
129 }
130
dbfcf440
AB
131 $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_COURSE;
132 $aggregation = new completion_aggregation($aggdata);
2be4d090 133 $aggregation->setMethod($data->course_aggregation);
dbfcf440 134 $aggregation->save();
2be4d090 135
b3b13e99 136 // Handle role aggregation.
c919b26d
AB
137 if (empty($data->role_aggregation)) {
138 $data->role_aggregation = 0;
139 }
140
dbfcf440
AB
141 $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ROLE;
142 $aggregation = new completion_aggregation($aggdata);
2be4d090 143 $aggregation->setMethod($data->role_aggregation);
dbfcf440 144 $aggregation->save();
2be4d090 145
135dde74
RT
146 // Trigger an event for course module completion changed.
147 $event = \core\event\course_completion_updated::create(
148 array(
149 'courseid' => $course->id,
150 'context' => context_course::instance($course->id)
151 )
152 );
153 $event->trigger();
dbfcf440 154
b3b13e99 155 // Redirect to the course main page.
dbfcf440
AB
156 $url = new moodle_url('/course/view.php', array('id' => $course->id));
157 redirect($url);
2be4d090
MD
158}
159
0b620801
AG
160$renderer = $PAGE->get_renderer('core_course', 'bulk_activity_completion');
161
b3b13e99 162// Print the form.
2be4d090 163echo $OUTPUT->header();
b3b13e99 164echo $OUTPUT->heading(get_string('editcoursecompletionsettings', 'core_completion'));
2be4d090 165
a64a9f9c 166echo $renderer->navigation($course, 'completion');
0b620801 167
2be4d090
MD
168$form->display();
169
170echo $OUTPUT->footer();