"MDL-12304, fix double text"
[moodle-linuxchix.git] / grade / edit / outcome / course.php
blob6e45e27cbdced90cd9e4f6f15fd54f9358c69fbb
1 <?php // $Id$
2 // Allows a creator to edit custom outcomes, and also display help about outcomes
4 ///////////////////////////////////////////////////////////////////////////
5 // //
6 // NOTICE OF COPYRIGHT //
7 // //
8 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
9 // http://moodle.com //
10 // //
11 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
12 // //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
17 // //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 require_once '../../../config.php';
28 require_once $CFG->dirroot.'/grade/lib.php';
29 require_once $CFG->libdir.'/gradelib.php';
31 $courseid = required_param('id', PARAM_INT);
33 /// Make sure they can even access this course
34 if (!$course = get_record('course', 'id', $courseid)) {
35 print_error('nocourseid');
37 require_login($course);
38 $context = get_context_instance(CONTEXT_COURSE, $course->id);
39 require_capability('moodle/course:update', $context);
41 /// return tracking object
42 $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'outcomes', 'courseid'=>$courseid));
44 // first of all fix the state of outcomes_course table
45 $standardoutcomes = grade_outcome::fetch_all_global();
46 $co_custom = grade_outcome::fetch_all_local($courseid);
47 $co_standard_used = array();
48 $co_standard_notused = array();
50 if ($courseused = get_records('grade_outcomes_courses', 'courseid', $courseid, '', 'outcomeid')) {
51 $courseused = array_keys($courseused);
52 } else {
53 $courseused = array();
56 // fix wrong entries in outcomes_courses
57 foreach ($courseused as $oid) {
58 if (!array_key_exists($oid, $standardoutcomes) and !array_key_exists($oid, $co_custom)) {
59 delete_records('grade_outcomes_courses', 'outcomeid', $oid, 'courseid', $courseid);
63 // fix local custom outcomes missing in outcomes_course
64 foreach($co_custom as $oid=>$outcome) {
65 if (!in_array($oid, $courseused)) {
66 $courseused[$oid] = $oid;
67 $goc = new object();
68 $goc->courseid = $courseid;
69 $goc->outcomeid = $oid;
70 insert_record('grade_outcomes_courses', $goc);
74 // now check all used standard outcomes are in outcomes_course too
75 $sql = "SELECT DISTINCT outcomeid
76 FROM {$CFG->prefix}grade_items
77 WHERE courseid=$courseid and outcomeid IS NOT NULL";
78 if ($realused = get_records_sql($sql)) {
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 object();
89 $goc->courseid = $courseid;
90 $goc->outcomeid = $oid;
91 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()) {
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 object();
117 $goc->courseid = $courseid;
118 $goc->outcomeid = $add;
119 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 delete_records('grade_outcomes_courses', 'courseid', $courseid, 'outcomeid', $remove);
132 redirect('course.php?id='.$courseid); // we must redirect to get fresh data
135 $strgrades = get_string('grades');
136 $pagename = get_string('outcomescourse', 'grades');
138 $navlinks = array(array('name'=>$strgrades,
139 'link'=>$CFG->wwwroot.'/grade/index.php?id='.$courseid,
140 'type'=>'misc'),
141 array('name'=>$pagename,
142 'link'=>'',
143 'type'=>'misc')
145 $navigation = build_navigation($navlinks);
146 $navigation = grade_build_nav(__FILE__, $pagename, $courseid);
147 /// Print header
148 print_header_simple($strgrades.': '.$pagename, ': '.$strgrades, $navigation, '', '', true, '', navmenu($course));
150 /// Print the plugin selector at the top
151 print_grade_plugin_selector($courseid, 'edit', 'outcome');
153 $currenttab = 'courseoutcomes';
154 require('tabs.php');
156 check_theme_arrows();
157 require('course_form.html');
159 print_footer($course);