course completion: MDL-2631 Prevent error when no roles are available
[moodle.git] / course / completion.php
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 // Edit course completion settings
28 require_once('../config.php');
29 require_once('lib.php');
30 require_once($CFG->libdir.'/completionlib.php');
31 require_once($CFG->libdir.'/completion/completion_criteria_self.php');
32 require_once($CFG->libdir.'/completion/completion_criteria_date.php');
33 require_once($CFG->libdir.'/completion/completion_criteria_unenrol.php');
34 require_once($CFG->libdir.'/completion/completion_criteria_activity.php');
35 require_once($CFG->libdir.'/completion/completion_criteria_duration.php');
36 require_once($CFG->libdir.'/completion/completion_criteria_grade.php');
37 require_once($CFG->libdir.'/completion/completion_criteria_role.php');
38 require_once($CFG->libdir.'/completion/completion_criteria_course.php');
39 require_once $CFG->libdir.'/gradelib.php';
40 require_once('completion_form.php');
42 $id = required_param('id', PARAM_INT);       // course id
44 /// basic access control checks
45 if ($id) { // editing course
47     if($id == SITEID){
48         // don't allow editing of  'site course' using this from
49         print_error('cannoteditsiteform');
50     }
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));
58 } else {
59     require_login();
60     print_error('needcourseid');
61 }
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);
70 $PAGE->set_pagelayout('standard');
72 /// first create the form
73 $form = new course_completion_form('completion.php?id='.$id, compact('course'));
75 // now override defaults if course already exists
76 if ($form->is_cancelled()){
77     redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
79 } else if ($data = $form->get_data()) {
81     $completion = new completion_info($course);
83 /// process criteria unlocking if requested
84     if (!empty($data->settingsunlock)) {
86         $completion->delete_course_completion_data();
88         // Return to form (now unlocked)
89         redirect($CFG->wwwroot."/course/completion.php?id=$course->id");
90     }
92 /// process data if submitted
93     // Delete old criteria
94     $completion->clear_criteria();
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     }
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();
112     // Activity aggregation
113     if (empty($data->activity_aggregation)) {
114         $data->activity_aggregation = 0;
115     }
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();
123     // Course aggregation
124     if (empty($data->course_aggregation)) {
125         $data->course_aggregation = 0;
126     }
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();
134     // Role aggregation
135     if (empty($data->role_aggregation)) {
136         $data->role_aggregation = 0;
137     }
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();
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     }
153     redirect($CFG->wwwroot."/course/view.php?id=$course->id", get_string('changessaved'));
157 /// Print the form
160 echo $OUTPUT->header();
161 echo $OUTPUT->heading($streditcompletionsettings);
163 $form->display();
165 echo $OUTPUT->footer();