|
Moodle
2.2.1
http://www.collinsharper.com
|
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->dirroot.'/grade/report/lib.php'; 00021 require_once 'outcomeitem_form.php'; 00022 00023 $courseid = required_param('courseid', PARAM_INT); 00024 $id = optional_param('id', 0, PARAM_INT); 00025 00026 $url = new moodle_url('/grade/edit/tree/outcomeitem.php', array('courseid'=>$courseid)); 00027 if ($id !== 0) { 00028 $url->param('id', $id); 00029 } 00030 $PAGE->set_url($url); 00031 00032 if (!$course = $DB->get_record('course', array('id' => $courseid))) { 00033 print_error('nocourseid'); 00034 } 00035 00036 require_login($course); 00037 $context = get_context_instance(CONTEXT_COURSE, $course->id); 00038 require_capability('moodle/grade:manage', $context); 00039 00040 00041 // default return url 00042 $gpr = new grade_plugin_return(); 00043 $returnurl = $gpr->get_return_url('index.php?id='.$course->id); 00044 00045 $mform = new edit_outcomeitem_form(null, array('gpr'=>$gpr)); 00046 00047 if ($mform->is_cancelled() || empty($CFG->enableoutcomes)) { 00048 redirect($returnurl); 00049 } 00050 00051 $heading = get_string('outcomeitemsedit', 'grades'); 00052 00053 if ($grade_item = grade_item::fetch(array('id'=>$id, 'courseid'=>$courseid))) { 00054 // redirect if outcomeid present 00055 if (empty($grade_item->outcomeid)) { 00056 $url = $CFG->wwwroot.'/grade/edit/tree/item.php?id='.$id.'&courseid='.$courseid; 00057 redirect($gpr->add_url_params($url)); 00058 } 00059 $item = $grade_item->get_record_data(); 00060 00061 $parent_category = $grade_item->get_parent_category(); 00062 $item->parentcategory = $parent_category->id; 00063 00064 if ($item->itemtype == 'mod') { 00065 $cm = get_coursemodule_from_instance($item->itemmodule, $item->iteminstance, $item->courseid); 00066 $item->cmid = $cm->id; 00067 } else { 00068 $item->cmid = 0; 00069 } 00070 00071 } else { 00072 $heading = get_string('newoutcomeitem', 'grades'); 00073 $grade_item = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual'), false); 00074 $item = $grade_item->get_record_data(); 00075 $item->cmid = 0; 00076 $parent_category = grade_category::fetch_course_category($courseid); 00077 $item->parentcategory = $parent_category->id; 00078 } 00079 00080 $decimalpoints = $grade_item->get_decimals(); 00081 00082 if ($item->hidden > 1) { 00083 $item->hiddenuntil = $item->hidden; 00084 $item->hidden = 0; 00085 } else { 00086 $item->hiddenuntil = 0; 00087 } 00088 00089 $item->locked = !empty($item->locked); 00090 00091 $item->gradepass = format_float($item->gradepass, $decimalpoints); 00092 00093 if (empty($parent_category)) { 00094 $item->aggregationcoef = 0; 00095 } else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) { 00096 $item->aggregationcoef = $item->aggregationcoef > 0 ? 1 : 0; 00097 } else { 00098 $item->aggregationcoef = format_float($item->aggregationcoef, 4); 00099 } 00100 00101 $mform->set_data($item); 00102 00103 00104 if ($data = $mform->get_data()) { 00105 00106 if (!isset($data->aggregationcoef)) { 00107 $data->aggregationcoef = 0; 00108 } 00109 00110 if (property_exists($data, 'calculation')) { 00111 $data->calculation = grade_item::normalize_formula($data->calculation, $course->id); 00112 } 00113 00114 $hidden = empty($data->hidden) ? 0: $data->hidden; 00115 $hiddenuntil = empty($data->hiddenuntil) ? 0: $data->hiddenuntil; 00116 unset($data->hidden); 00117 unset($data->hiddenuntil); 00118 00119 $locked = empty($data->locked) ? 0: $data->locked; 00120 $locktime = empty($data->locktime) ? 0: $data->locktime; 00121 unset($data->locked); 00122 unset($data->locktime); 00123 00124 $convert = array('gradepass', 'aggregationcoef'); 00125 foreach ($convert as $param) { 00126 if (property_exists($data, $param)) { 00127 $data->$param = unformat_float($data->$param); 00128 } 00129 } 00130 00131 $grade_item = new grade_item(array('id'=>$id, 'courseid'=>$courseid)); 00132 grade_item::set_properties($grade_item, $data); 00133 00134 // fix activity links 00135 if (empty($data->cmid)) { 00136 // manual item 00137 $grade_item->itemtype = 'manual'; 00138 $grade_item->itemmodule = null; 00139 $grade_item->iteminstance = null; 00140 $grade_item->itemnumber = 0; 00141 00142 } else { 00143 $params = array($data->cmid); 00144 $module = $DB->get_record_sql("SELECT cm.*, m.name as modname 00145 FROM {modules} m, {course_modules} cm 00146 WHERE cm.id = ? AND cm.module = m.id ", $params); 00147 $grade_item->itemtype = 'mod'; 00148 $grade_item->itemmodule = $module->modname; 00149 $grade_item->iteminstance = $module->instance; 00150 00151 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$grade_item->itemmodule, 00152 'iteminstance'=>$grade_item->iteminstance, 'courseid'=>$COURSE->id))) { 00153 if (!empty($grade_item->id) and in_array($grade_item, $items)) { 00154 //no change needed 00155 } else { 00156 $max = 999; 00157 foreach($items as $item) { 00158 if (empty($item->outcomeid)) { 00159 continue; 00160 } 00161 if ($item->itemnumber > $max) { 00162 $max = $item->itemnumber; 00163 } 00164 } 00165 $grade_item->itemnumber = $max + 1; 00166 } 00167 } else { 00168 $grade_item->itemnumber = 1000; 00169 } 00170 } 00171 00172 // fix scale used 00173 $outcome = grade_outcome::fetch(array('id'=>$data->outcomeid)); 00174 $grade_item->gradetype = GRADE_TYPE_SCALE; 00175 $grade_item->scaleid = $outcome->scaleid; //TODO: we might recalculate existing outcome grades when changing scale 00176 00177 if (empty($grade_item->id)) { 00178 $grade_item->insert(); 00179 // move next to activity if adding linked outcome 00180 if ($grade_item->itemtype == 'mod') { 00181 if ($item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$grade_item->itemmodule, 00182 'iteminstance'=>$grade_item->iteminstance, 'itemnumber'=>0, 'courseid'=>$COURSE->id))) { 00183 $grade_item->set_parent($item->categoryid); 00184 $grade_item->move_after_sortorder($item->sortorder); 00185 } 00186 } else { 00187 // set parent if needed 00188 if (isset($data->parentcategory)) { 00189 $grade_item->set_parent($data->parentcategory, 'gradebook'); 00190 } 00191 } 00192 00193 } else { 00194 $grade_item->update(); 00195 } 00196 00197 // update hiding flag 00198 if ($hiddenuntil) { 00199 $grade_item->set_hidden($hiddenuntil, false); 00200 } else { 00201 $grade_item->set_hidden($hidden, false); 00202 } 00203 00204 $grade_item->set_locktime($locktime); // locktime first - it might be removed when unlocking 00205 $grade_item->set_locked($locked, false, true); 00206 00207 redirect($returnurl); 00208 } 00209 00210 print_grade_page_head($courseid, 'edittree', null, $heading); 00211 00212 if (!grade_outcome::fetch_all_available($COURSE->id)) { 00213 echo $OUTPUT->confirm(get_string('nooutcomes', 'grades'), $CFG->wwwroot.'/grade/edit/outcome/course.php?id='.$courseid, $returnurl); 00214 echo $OUTPUT->footer(); 00215 die(); 00216 } 00217 00218 $mform->display(); 00219 00220 echo $OUTPUT->footer();