weekly release 5.0dev
[moodle.git] / grade / edit / outcome / course.php
blob6353c82b725b3b4881e9a60a023e020b1493297c
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * A page for selecting outcomes for use in a course
20 * @package core_grades
21 * @copyright 2007 Petr Skoda
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once '../../../config.php';
26 require_once $CFG->dirroot.'/grade/lib.php';
27 require_once $CFG->libdir.'/gradelib.php';
29 $courseid = required_param('id', PARAM_INT);
31 $PAGE->set_url('/grade/edit/outcome/course.php', array('id'=>$courseid));
33 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
35 /// Make sure they can even access this course
36 require_login($course);
37 $context = context_course::instance($course->id);
38 require_capability('moodle/course:update', $context);
40 /// return tracking object
41 $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'outcomes', 'courseid'=>$courseid));
43 // first of all fix the state of outcomes_course table
44 $standardoutcomes = grade_outcome::fetch_all_global();
45 $co_custom = grade_outcome::fetch_all_local($courseid);
46 $co_standard_used = array();
47 $co_standard_notused = array();
49 if ($courseused = $DB->get_records('grade_outcomes_courses', array('courseid' => $courseid), '', 'outcomeid')) {
50 $courseused = array_keys($courseused);
51 } else {
52 $courseused = array();
55 // fix wrong entries in outcomes_courses
56 foreach ($courseused as $oid) {
57 if (!array_key_exists($oid, $standardoutcomes) and !array_key_exists($oid, $co_custom)) {
58 $DB->delete_records('grade_outcomes_courses', array('outcomeid' => $oid, 'courseid' => $courseid));
62 // fix local custom outcomes missing in outcomes_course
63 foreach($co_custom as $oid=>$outcome) {
64 if (!in_array($oid, $courseused)) {
65 $courseused[$oid] = $oid;
66 $goc = new stdClass();
67 $goc->courseid = $courseid;
68 $goc->outcomeid = $oid;
69 $DB->insert_record('grade_outcomes_courses', $goc);
73 // now check all used standard outcomes are in outcomes_course too
74 $params = array($courseid);
75 $sql = "SELECT DISTINCT outcomeid
76 FROM {grade_items}
77 WHERE courseid=? and outcomeid IS NOT NULL";
78 if ($realused = $DB->get_records_sql($sql, $params)) {
79 $realused = array_keys($realused);
80 foreach ($realused as $oid) {
81 if (array_key_exists($oid, $standardoutcomes)) {
83 $co_standard_used[$oid] = $standardoutcomes[$oid];
84 unset($standardoutcomes[$oid]);
86 if (!in_array($oid, $courseused)) {
87 $courseused[$oid] = $oid;
88 $goc = new stdClass();
89 $goc->courseid = $courseid;
90 $goc->outcomeid = $oid;
91 $DB->insert_record('grade_outcomes_courses', $goc);
97 // find all unused standard course outcomes - candidates for removal
98 foreach ($standardoutcomes as $oid=>$outcome) {
99 if (in_array($oid, $courseused)) {
100 $co_standard_notused[$oid] = $standardoutcomes[$oid];
101 unset($standardoutcomes[$oid]);
106 /// form processing
107 if ($data = data_submitted() and confirm_sesskey()) {
108 require_capability('moodle/grade:manageoutcomes', $context);
109 if (!empty($data->add) && !empty($data->addoutcomes)) {
110 /// add all selected to course list
111 foreach ($data->addoutcomes as $add) {
112 $add = clean_param($add, PARAM_INT);
113 if (!array_key_exists($add, $standardoutcomes)) {
114 continue;
116 $goc = new stdClass();
117 $goc->courseid = $courseid;
118 $goc->outcomeid = $add;
119 $DB->insert_record('grade_outcomes_courses', $goc);
122 } else if (!empty($data->remove) && !empty($data->removeoutcomes)) {
123 /// remove all selected from course outcomes list
124 foreach ($data->removeoutcomes as $remove) {
125 $remove = clean_param($remove, PARAM_INT);
126 if (!array_key_exists($remove, $co_standard_notused)) {
127 continue;
129 $DB->delete_records('grade_outcomes_courses', array('courseid' => $courseid, 'outcomeid' => $remove));
132 redirect('course.php?id='.$courseid); // we must redirect to get fresh data
135 $actionbar = new \core_grades\output\course_outcomes_action_bar($context);
136 // Print header.
137 print_grade_page_head($COURSE->id, 'outcome', 'course', false, false, false,
138 true, null, null, null, $actionbar);
140 require('course_form.html');
142 echo $OUTPUT->footer();