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
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);
22 require_once $CFG->libdir
.'/adminlib.php';
23 admin_externalpage_setup('scales');
26 $straddelement = get_string('addelement', 'grades');
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);
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()) {
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
)) {
67 // Loop through grade letters and boundaries
68 foreach ($data as $key => $variable) {
69 preg_match('/[gradeletter|gradeboundary]([0-9]{1,2})/', $key, $matches);
71 if (isset($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();
94 debugging('Error inserting grade_letters record!');
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');
119 if (!confirm_sesskey()) {
122 $scaleid = required_param('scaleid', PARAM_INT
);
123 if (!$scale = grade_scale
::fetch(array('id'=>$scaleid))) {
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()) {
137 //TODO: add confirmation
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');
149 admin_externalpage_print_header();
152 print_simple_box_start("center");
154 print_simple_box_end();
157 print_footer($course);
159 admin_externalpage_print_footer();