course completion: MDL-2631 Prevent error when no roles are available
[moodle.git] / course / completion.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
26// Edit course completion settings
27
28require_once('../config.php');
29require_once('lib.php');
30require_once($CFG->libdir.'/completionlib.php');
31require_once($CFG->libdir.'/completion/completion_criteria_self.php');
32require_once($CFG->libdir.'/completion/completion_criteria_date.php');
33require_once($CFG->libdir.'/completion/completion_criteria_unenrol.php');
34require_once($CFG->libdir.'/completion/completion_criteria_activity.php');
35require_once($CFG->libdir.'/completion/completion_criteria_duration.php');
36require_once($CFG->libdir.'/completion/completion_criteria_grade.php');
37require_once($CFG->libdir.'/completion/completion_criteria_role.php');
38require_once($CFG->libdir.'/completion/completion_criteria_course.php');
39require_once $CFG->libdir.'/gradelib.php';
40require_once('completion_form.php');
41
42$id = required_param('id', PARAM_INT); // course id
43
44/// basic access control checks
45if ($id) { // editing course
46
47 if($id == SITEID){
48 // don't allow editing of 'site course' using this from
49 print_error('cannoteditsiteform');
50 }
51
52 if (!$course = $DB->get_record('course', array('id'=>$id))) {
53 print_error('invalidcourseid');
54 }
55 require_login($course->id);
56 require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));
57
58} else {
59 require_login();
60 print_error('needcourseid');
61}
62
63/// Set up the page
64$streditcompletionsettings = get_string("editcoursecompletionsettings", 'completion');
65$PAGE->set_course($course);
66$PAGE->set_url('/course/completion.php', array('id' => $course->id));
67//$PAGE->navbar->add($streditcompletionsettings);
68$PAGE->set_title($course->shortname);
69$PAGE->set_heading($course->fullname);
d6d07a68 70$PAGE->set_pagelayout('standard');
2be4d090
MD
71
72/// first create the form
73$form = new course_completion_form('completion.php?id='.$id, compact('course'));
74
75// now override defaults if course already exists
76if ($form->is_cancelled()){
77 redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
78
79} else if ($data = $form->get_data()) {
80
81 $completion = new completion_info($course);
82
83/// process criteria unlocking if requested
84 if (!empty($data->settingsunlock)) {
85
86 $completion->delete_course_completion_data();
87
88 // Return to form (now unlocked)
89 redirect($CFG->wwwroot."/course/completion.php?id=$course->id");
90 }
91
92/// process data if submitted
93 // Delete old criteria
94 $completion->clear_criteria();
95
96 // Loop through each criteria type and run update_config
97 global $COMPLETION_CRITERIA_TYPES;
98 foreach ($COMPLETION_CRITERIA_TYPES as $type) {
99 $class = 'completion_criteria_'.$type;
100 $criterion = new $class();
101 $criterion->update_config($data);
102 }
103
104 // Handle aggregation methods
105 // Overall aggregation
106 $aggregation = new completion_aggregation();
107 $aggregation->course = $data->id;
108 $aggregation->criteriatype = null;
109 $aggregation->setMethod($data->overall_aggregation);
110 $aggregation->insert();
111
112 // Activity aggregation
113 if (empty($data->activity_aggregation)) {
114 $data->activity_aggregation = 0;
115 }
116
117 $aggregation = new completion_aggregation();
118 $aggregation->course = $data->id;
119 $aggregation->criteriatype = COMPLETION_CRITERIA_TYPE_ACTIVITY;
120 $aggregation->setMethod($data->activity_aggregation);
121 $aggregation->insert();
122
123 // Course aggregation
124 if (empty($data->course_aggregation)) {
125 $data->course_aggregation = 0;
126 }
127
128 $aggregation = new completion_aggregation();
129 $aggregation->course = $data->id;
130 $aggregation->criteriatype = COMPLETION_CRITERIA_TYPE_COURSE;
131 $aggregation->setMethod($data->course_aggregation);
132 $aggregation->insert();
133
134 // Role aggregation
c919b26d
AB
135 if (empty($data->role_aggregation)) {
136 $data->role_aggregation = 0;
137 }
138
2be4d090
MD
139 $aggregation = new completion_aggregation();
140 $aggregation->course = $data->id;
141 $aggregation->criteriatype = COMPLETION_CRITERIA_TYPE_ROLE;
142 $aggregation->setMethod($data->role_aggregation);
143 $aggregation->insert();
144
145 // Update course total passing grade
146 if ($grade_item = grade_category::fetch_course_category($course->id)->grade_item) {
147 $grade_item->gradepass = $data->criteria_grade_value;
148 if (method_exists($grade_item, 'update')) {
149 $grade_item->update('course/completion.php');
150 }
151 }
152
153 redirect($CFG->wwwroot."/course/view.php?id=$course->id", get_string('changessaved'));
154}
155
156
157/// Print the form
158
159
160echo $OUTPUT->header();
161echo $OUTPUT->heading($streditcompletionsettings);
162
163$form->display();
164
165echo $OUTPUT->footer();