MDL-32249 completion: Make grade criteria more consistent
[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);
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     $aggdata = array(
107         'course'        => $data->id,
108         'criteriatype'  => null
109     );
110     $aggregation = new completion_aggregation($aggdata);
111     $aggregation->setMethod($data->overall_aggregation);
112     $aggregation->save();
114     // Activity aggregation
115     if (empty($data->activity_aggregation)) {
116         $data->activity_aggregation = 0;
117     }
119     $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY;
120     $aggregation = new completion_aggregation($aggdata);
121     $aggregation->setMethod($data->activity_aggregation);
122     $aggregation->save();
124     // Course aggregation
125     if (empty($data->course_aggregation)) {
126         $data->course_aggregation = 0;
127     }
129     $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_COURSE;
130     $aggregation = new completion_aggregation($aggdata);
131     $aggregation->setMethod($data->course_aggregation);
132     $aggregation->save();
134     // Role aggregation
135     if (empty($data->role_aggregation)) {
136         $data->role_aggregation = 0;
137     }
139     $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ROLE;
140     $aggregation = new completion_aggregation($aggdata);
141     $aggregation->setMethod($data->role_aggregation);
142     $aggregation->save();
144     add_to_log($course->id, 'course', 'completion updated', 'completion.php?id='.$course->id);
146     $url = new moodle_url('/course/view.php', array('id' => $course->id));
147     redirect($url);
151 /// Print the form
154 echo $OUTPUT->header();
155 echo $OUTPUT->heading($streditcompletionsettings);
157 $form->display();
159 echo $OUTPUT->footer();