MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / grade / edit / scale / edit.php
blob4e8023e84e01e1573b5ea24b65289fbaf4597069
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 'edit_form.php';
8 $courseid = optional_param('courseid', 0, PARAM_INT);
9 $id = optional_param('id', 0, PARAM_INT);
11 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
13 // a bit complex access control :-O
14 if ($id) {
15 /// editing existing scale
16 if (!$scale_rec = get_record('scale', 'id', $id)) {
17 error('Incorrect scale id');
19 if ($scale_rec->courseid) {
20 $scale_rec->standard = 0;
21 if (!$course = get_record('course', 'id', $scale_rec->courseid)) {
22 error('Incorrect course id');
24 require_login($course);
25 $context = get_context_instance(CONTEXT_COURSE, $course->id);
26 require_capability('moodle/course:managescales', $context);
27 $courseid = $course->id;
28 } else {
29 if ($courseid) {
30 if (!$course = get_record('course', 'id', $courseid)) {
31 error('Incorrect course id');
34 $scale_rec->standard = 1;
35 $scale_rec->courseid = $courseid;
36 require_login($courseid);
37 require_capability('moodle/course:managescales', $systemcontext);
40 } else if ($courseid){
41 /// adding new scale from course
42 if (!$course = get_record('course', 'id', $courseid)) {
43 print_error('nocourseid');
45 $scale_rec = new object();
46 $scale_rec->standard = 0;
47 $scale_rec->courseid = $courseid;
48 require_login($course);
49 $context = get_context_instance(CONTEXT_COURSE, $course->id);
50 require_capability('moodle/course:managescales', $context);
52 } else {
53 /// adding new scale from admin section
54 $scale_rec = new object();
55 $scale_rec->standard = 1;
56 $scale_rec->courseid = 0;
57 require_login();
58 require_capability('moodle/course:managescales', $systemcontext);
61 // default return url
62 $gpr = new grade_plugin_return();
63 $returnurl = $gpr->get_return_url('index.php?id='.$courseid);
65 $mform = new edit_scale_form(null, array('gpr'=>$gpr));
67 $mform->set_data($scale_rec);
69 if ($mform->is_cancelled()) {
70 redirect($returnurl);
72 } else if ($data = $mform->get_data(false)) {
73 $scale = new grade_scale(array('id'=>$id));
74 $data->userid = $USER->id;
75 grade_scale::set_properties($scale, $data);
77 if (empty($scale->id)) {
78 if (!has_capability('moodle/grade:manage', $systemcontext)) {
79 $data->standard = 0;
81 $scale->courseid = !empty($data->standard) ? 0 : $courseid;
82 $scale->insert();
84 } else {
85 if (isset($data->standard)) {
86 $scale->courseid = !empty($data->standard) ? 0 : $courseid;
87 } else {
88 unset($scale->couseid); // keep previous
90 $scale->update();
93 redirect($returnurl);
96 $strgrades = get_string('grades');
97 $strgraderreport = get_string('graderreport', 'grades');
98 $strscaleedit = get_string('scale');
100 if ($courseid) {
101 $navigation = grade_build_nav(__FILE__, $strscaleedit, $courseid);
102 print_header_simple($strgrades.': '.$strgraderreport, ': '.$strscaleedit, $navigation, '', '', true, '', navmenu($course));
104 } else {
105 require_once $CFG->libdir.'/adminlib.php';
106 admin_externalpage_setup('outcomes');
107 admin_externalpage_print_header();
110 $mform->display();
112 if ($courseid) {
113 print_footer($course);
114 } else {
115 admin_externalpage_print_footer();