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