MDL-11340 Almost complete: Just need the "Add grade letter" button
[moodle-pu.git] / grade / edit / gradedisplay / index.php
blobaa67506b626a00ee6a5c3642ecd397ebd50f7568
1 <?php // $Id$
2 // Allows a creator to edit custom scales, and also display help about scales
4 require_once '../../../config.php';
5 require_once $CFG->dirroot.'/grade/lib.php';
6 require_once $CFG->libdir.'/gradelib.php';
7 require_once $CFG->dirroot.'/grade/report/lib.php';
8 require_once 'gradedisplay_form.php';
10 $courseid = optional_param('id', 0, PARAM_INT);
11 $action = optional_param('action', '', PARAM_ALPHA);
13 /// Make sure they can even access this course
14 if ($courseid) {
15 if (!$course = get_record('course', 'id', $courseid)) {
16 print_error('nocourseid');
18 require_login($course);
19 $context = get_context_instance(CONTEXT_COURSE, $course->id);
20 require_capability('moodle/course:managescales', $context);
21 } else {
22 require_once $CFG->libdir.'/adminlib.php';
23 admin_externalpage_setup('scales');
26 $straddelement = get_string('addelement', 'grades');
27 switch ($action) {
28 case $straddelement:
29 // Insert a record in the grade_letters table, with 0 as lower boundary and ' - ' as letter
30 $record = new stdClass();
31 $record->contextid = $context->id;
32 $record->letter = '-';
33 $record->lowerboundary = 0;
34 insert_record('grade_letters', $record);
35 break;
36 default:
37 break;
40 $course_has_letters = get_field('grade_letters', 'contextid', 'contextid', $context->id);
42 /// return tracking object
43 $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'gradedisplay', 'courseid'=>$courseid));
44 $returnurl = $gpr->get_return_url($CFG->wwwroot.'/grade/edit/gradedisplay/index.php?id='.$course->id);
46 $mform = new edit_grade_display_form(null, array('gpr'=>$gpr, 'course_has_letters' => $course_has_letters, 'action' => $action));
48 if ($mform->is_cancelled()) {
49 redirect($returnurl);
51 // form processing
52 } else if ($data = $mform->get_data(false)) {
53 // Delete existing grade_letters for this contextid, whether we add, update or set the grade letters to defaults
54 if ($course_has_letters) {
55 delete_records('grade_letters', 'contextid', $context->id);
58 // Update course item's gradedisplay type
59 if (isset($data->gradedisplaytype)) {
60 set_field('grade_items', 'display', $data->gradedisplaytype, 'courseid', $courseid, 'itemtype', 'course');
63 // If override is present, add/update entries in grade_letters table
64 if (!empty($data->override)) {
65 $records = array();
67 // Loop through grade letters and boundaries
68 foreach ($data as $key => $variable) {
69 preg_match('/[gradeletter|gradeboundary]([0-9]{1,2})/', $key, $matches);
70 $index = null;
71 if (isset($matches[1])) {
72 $index = $matches[1];
75 if (strstr($key, 'gradeletter')) {
76 $records[$index] = new stdClass();
77 $records[$index]->letter = $variable;
78 } elseif (strstr($key, 'gradeboundary')) {
79 if (!empty($records[$index])) {
80 $records[$index]->lowerboundary = $variable;
85 foreach ($records as $key => $record) {
86 // Do not insert if either value is empty or set to "unused"
87 $values_set = isset($record->letter) && isset($record->lowerboundary);
89 if ($values_set && strlen($record->letter) > 0 && strlen($record->lowerboundary) > 0) {
90 $record->contextid = $context->id;
91 if ($id = insert_record('grade_letters', $record)) {
92 $record = new stdClass();
93 } else {
94 debugging('Error inserting grade_letters record!');
95 die();
99 } else {
103 redirect($returnurl, get_string('coursegradedisplayupdated', 'grades'));
106 $strgrades = get_string('grades');
107 $pagename = get_string('gradedisplay');
109 $navigation = grade_build_nav(__FILE__, $pagename, array('courseid' => $courseid));
111 $strname = get_string('name');
112 $strdelete = get_string('delete');
113 $stredit = get_string('edit');
114 $strused = get_string('used');
115 $stredit = get_string('edit');
117 switch ($action) {
118 case 'delete':
119 if (!confirm_sesskey()) {
120 break;
122 $scaleid = required_param('scaleid', PARAM_INT);
123 if (!$scale = grade_scale::fetch(array('id'=>$scaleid))) {
124 break;
127 if (empty($scale->courseid)) {
128 require_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM));
129 } else if ($scale->courseid != $courseid) {
130 error('Incorrect courseid!');
133 if (!$scale->can_delete()) {
134 break;
137 //TODO: add confirmation
138 $scale->delete();
139 break;
142 if ($courseid) {
143 /// Print header
144 print_header_simple($strgrades.': '.$pagename, ': '.$strgrades, $navigation, '', '', true, '', navmenu($course));
145 /// Print the plugin selector at the top
146 print_grade_plugin_selector($courseid, 'edit', 'scale');
148 } else {
149 admin_externalpage_print_header();
152 print_simple_box_start("center");
153 $mform->display();
154 print_simple_box_end();
156 if ($courseid) {
157 print_footer($course);
158 } else {
159 admin_externalpage_print_footer();