Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / grade / edit / outcomeitem.php
blobc5109d3f83d4532beb63526fe91b98c52d7384d0
1 <?php //$Id$
3 require_once '../../config.php';
4 require_once $CFG->dirroot.'/grade/lib.php';
5 require_once $CFG->dirroot.'/grade/report/lib.php';
6 require_once 'outcomeitem_form.php';
8 $courseid = required_param('courseid', PARAM_INT);
9 $id = optional_param('id', 0, PARAM_INT);
11 if (!$course = get_record('course', 'id', $courseid)) {
12 print_error('nocourseid');
15 require_login($course);
16 $context = get_context_instance(CONTEXT_COURSE, $course->id);
17 require_capability('moodle/grade:manage', $context);
19 // default return url
20 $gpr = new grade_plugin_return();
21 $returnurl = $gpr->get_return_url('tree.php?id='.$course->id);
23 $mform = new edit_outcomeitem_form(null, array('gpr'=>$gpr));
25 if ($mform->is_cancelled()) {
26 redirect($returnurl);
29 if ($item = get_record('grade_items', 'id', $id, 'courseid', $course->id)) {
30 // redirect if outcomeid present
31 if (empty($item->outcomeid)) {
32 $url = $CFG->wwwroot.'/grade/edit/outcome.php?id='.$id.'&amp;courseid='.$courseid;
33 redirect($gpr->add_url_params($url));
36 // $item->calculation = grade_item::denormalize_formula($item->calculation, $course->id);
38 if ($item->itemtype == 'mod') {
39 $cm = get_coursemodule_from_instance($item->itemmodule, $item->iteminstance, $item->courseid);
40 $item->cmid = $cm->id;
41 } else {
42 $item->cmid = 0;
45 $mform->set_data($item);
48 if ($data = $mform->get_data(false)) {
49 if (array_key_exists('calculation', $data)) {
50 // $data->calculation = grade_item::normalize_formula($data->calculation, $course->id);
53 $grade_item = new grade_item(array('id'=>$id, 'courseid'=>$courseid));
54 grade_item::set_properties($grade_item, $data);
56 // fix activity links
57 if (empty($data->cmid)) {
58 // manual item
59 $grade_item->itemtype = 'manual';
60 $grade_item->itemmodule = null;
61 $grade_item->iteminstance = null;
62 $grade_item->itemnumber = 0;
64 } else {
65 $module = get_record_sql("SELECT cm.*, m.name as modname
66 FROM {$CFG->prefix}modules m, {$CFG->prefix}course_modules cm
67 WHERE cm.id = {$data->cmid} AND cm.module = m.id ");
68 $grade_item->itemtype = 'mod';
69 $grade_item->itemmodule = $module->modname;
70 $grade_item->iteminstance = $module->instance;
72 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$grade_item->itemmodule,
73 'iteminstance'=>$grade_item->iteminstance, 'courseid'=>$COURSE->id))) {
74 if (!empty($grade_item->id) and in_array($grade_item, $items)) {
75 //no change needed
76 } else {
77 $max = 999;
78 foreach($items as $item) {
79 if (empty($item->outcomeid)) {
80 continue;
82 if ($item->itemnumber > $max) {
83 $max = $item->itemnumber;
86 $grade_item->itemnumber = $max + 1;
88 } else {
89 $grade_item->itemnumber = 1000;
93 // fix scale used
94 $outcome = grade_outcome::fetch(array('id'=>$data->outcomeid));
95 $grade_item->gradetype = GRADE_TYPE_SCALE;
96 $grade_item->scaleid = $outcome->scaleid; //TODO: we might recalculate existing outcome grades when changing scale
98 if (empty($grade_item->id)) {
99 $grade_item->insert();
100 // move next to activity if adding linked outcome
101 if ($grade_item->itemtype == 'mod') {
102 if ($item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$grade_item->itemmodule,
103 'iteminstance'=>$grade_item->iteminstance, 'itemnumber'=>0, 'courseid'=>$COURSE->id))) {
104 $grade_item->set_parent($item->categoryid);
105 $grade_item->move_after_sortorder($item->sortorder);
109 } else {
110 $grade_item->update();
113 redirect($returnurl, 'temporary debug delay', 5);
116 $strgrades = get_string('grades');
117 $strgraderreport = get_string('graderreport', 'grades');
118 $stroutcomesedit = get_string('outcomeitemsedit', 'grades');
120 $nav = array(array('name'=>$strgrades,'link'=>$CFG->wwwroot.'/grade/index.php?id='.$courseid, 'type'=>'misc'),
121 array('name'=>$stroutcomesedit, 'link'=>'', 'type'=>'misc'));
123 $navigation = build_navigation($nav);
126 print_header_simple($strgrades . ': ' . $strgraderreport, ': ' . $stroutcomesedit, $navigation, '', '', true, '', navmenu($course));
128 $mform->display();
130 print_footer($course);