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);
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()) {
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.'&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
;
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);
57 if (empty($data->cmid
)) {
59 $grade_item->itemtype
= 'manual';
60 $grade_item->itemmodule
= null;
61 $grade_item->iteminstance
= null;
62 $grade_item->itemnumber
= 0;
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)) {
78 foreach($items as $item) {
79 if (empty($item->outcomeid
)) {
82 if ($item->itemnumber
> $max) {
83 $max = $item->itemnumber
;
86 $grade_item->itemnumber
= $max +
1;
89 $grade_item->itemnumber
= 1000;
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
);
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));
130 print_footer($course);