Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/grade/edit/outcome/course.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 require_once '../../../config.php';
00019 require_once $CFG->dirroot.'/grade/lib.php';
00020 require_once $CFG->libdir.'/gradelib.php';
00021 
00022 $courseid = required_param('id', PARAM_INT);
00023 
00024 $PAGE->set_url('/grade/edit/outcome/course.php', array('id'=>$courseid));
00025 
00026 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
00027 
00029 require_login($course);
00030 $context = get_context_instance(CONTEXT_COURSE, $course->id);
00031 require_capability('moodle/course:update', $context);
00032 
00034 $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'outcomes', 'courseid'=>$courseid));
00035 
00036 // first of all fix the state of outcomes_course table
00037 $standardoutcomes    = grade_outcome::fetch_all_global();
00038 $co_custom           = grade_outcome::fetch_all_local($courseid);
00039 $co_standard_used    = array();
00040 $co_standard_notused = array();
00041 
00042 if ($courseused = $DB->get_records('grade_outcomes_courses', array('courseid' => $courseid), '', 'outcomeid')) {
00043     $courseused = array_keys($courseused);
00044 } else {
00045     $courseused = array();
00046 }
00047 
00048 // fix wrong entries in outcomes_courses
00049 foreach ($courseused as $oid) {
00050     if (!array_key_exists($oid, $standardoutcomes) and !array_key_exists($oid, $co_custom)) {
00051         $DB->delete_records('grade_outcomes_courses', array('outcomeid' => $oid, 'courseid' => $courseid));
00052     }
00053 }
00054 
00055 // fix local custom outcomes missing in outcomes_course
00056 foreach($co_custom as $oid=>$outcome) {
00057     if (!in_array($oid, $courseused)) {
00058         $courseused[$oid] = $oid;
00059         $goc = new stdClass();
00060         $goc->courseid = $courseid;
00061         $goc->outcomeid = $oid;
00062         $DB->insert_record('grade_outcomes_courses', $goc);
00063     }
00064 }
00065 
00066 // now check all used standard outcomes are in outcomes_course too
00067 $params = array($courseid);
00068 $sql = "SELECT DISTINCT outcomeid
00069           FROM {grade_items}
00070          WHERE courseid=? and outcomeid IS NOT NULL";
00071 if ($realused = $DB->get_records_sql($sql, $params)) {
00072     $realused = array_keys($realused);
00073     foreach ($realused as $oid) {
00074         if (array_key_exists($oid, $standardoutcomes)) {
00075 
00076             $co_standard_used[$oid] = $standardoutcomes[$oid];
00077             unset($standardoutcomes[$oid]);
00078 
00079             if (!in_array($oid, $courseused)) {
00080                 $courseused[$oid] = $oid;
00081                 $goc = new stdClass();
00082                 $goc->courseid = $courseid;
00083                 $goc->outcomeid = $oid;
00084                 $DB->insert_record('grade_outcomes_courses', $goc);
00085             }
00086         }
00087     }
00088 }
00089 
00090 // find all unused standard course outcomes - candidates for removal
00091 foreach ($standardoutcomes as $oid=>$outcome) {
00092     if (in_array($oid, $courseused)) {
00093         $co_standard_notused[$oid] = $standardoutcomes[$oid];
00094         unset($standardoutcomes[$oid]);
00095     }
00096 }
00097 
00098 
00100 if ($data = data_submitted() and confirm_sesskey()) {
00101     require_capability('moodle/grade:manageoutcomes', $context);
00102     if (!empty($data->add) && !empty($data->addoutcomes)) {
00104         foreach ($data->addoutcomes as $add) {
00105             $add = clean_param($add, PARAM_INT);
00106             if (!array_key_exists($add, $standardoutcomes)) {
00107                 continue;
00108             }
00109             $goc = new stdClass();
00110             $goc->courseid = $courseid;
00111             $goc->outcomeid = $add;
00112             $DB->insert_record('grade_outcomes_courses', $goc);
00113         }
00114 
00115     } else if (!empty($data->remove) && !empty($data->removeoutcomes)) {
00117         foreach ($data->removeoutcomes as $remove) {
00118             $remove = clean_param($remove, PARAM_INT);
00119             if (!array_key_exists($remove, $co_standard_notused)) {
00120                 continue;
00121             }
00122             $DB->delete_records('grade_outcomes_courses', array('courseid' => $courseid, 'outcomeid' => $remove));
00123         }
00124     }
00125     redirect('course.php?id='.$courseid); // we must redirect to get fresh data
00126 }
00127 
00129 print_grade_page_head($COURSE->id, 'outcome', 'course');
00130 
00131 require('course_form.html');
00132 
00133 echo $OUTPUT->footer();
00134 
 All Data Structures Namespaces Files Functions Variables Enumerations