Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / grade / edit / letter / edit.php
blob0377da37371b5d45bdc87b928ad444a4ab07efc1
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 require '../../../config.php';
27 require_once $CFG->libdir.'/gradelib.php';
28 require_once $CFG->dirroot.'/grade/lib.php';
29 require_once 'edit_form.php';
32 $contextid = optional_param('id', SYSCONTEXTID, PARAM_INT);
34 if (!$context = get_context_instance_by_id($contextid)) {
35 error('Incorrect context id');
38 if ($context->contextlevel == CONTEXT_SYSTEM or $context->contextlevel == CONTEXT_COURSECAT) {
39 require_once $CFG->libdir.'/adminlib.php';
40 require_login();
41 admin_externalpage_setup('letters');
42 $admin = true;
43 $returnurl = "$CFG->wwwroot/grade/edit/letter/edit.php"; // stay in the same page
46 } else if ($context->contextlevel == CONTEXT_COURSE) {
47 require_login($context->instanceid);
48 $admin = false;
49 $returnurl = $CFG->wwwroot.'/grade/edit/letter/index.php?id='.$context->instanceid;
51 } else {
52 error('Incorrect context level');
55 require_capability('moodle/grade:manageletters', $context);
57 $strgrades = get_string('grades');
58 $pagename = get_string('letters', 'grades');
60 $letters = grade_get_letters($context);
61 $num = count($letters) + 3;
63 $data = new object();
64 $data->id = $context->id;
66 $i = 1;
67 foreach ($letters as $boundary=>$letter) {
68 $gradelettername = 'gradeletter'.$i;
69 $gradeboundaryname = 'gradeboundary'.$i;
71 $data->$gradelettername = $letter;
72 $data->$gradeboundaryname = $boundary;
73 $i++;
75 $data->override = record_exists('grade_letters', 'contextid', $contextid);
77 $mform = new edit_letter_form(null, array('num'=>$num, 'admin'=>$admin));
78 $mform->set_data($data);
80 if ($mform->is_cancelled()) {
81 redirect($returnurl);
83 } else if ($data = $mform->get_data()) {
84 if (!$admin and empty($data->override)) {
85 delete_records('grade_letters', 'contextid', $context->id);
86 redirect($returnurl);
89 $letters = array();
90 for($i=1; $i<$num+1; $i++) {
91 $gradelettername = 'gradeletter'.$i;
92 $gradeboundaryname = 'gradeboundary'.$i;
94 if (array_key_exists($gradeboundaryname, $data) and $data->$gradeboundaryname != -1) {
95 $letter = trim($data->$gradelettername);
96 if ($letter == '') {
97 continue;
99 $letters[$data->$gradeboundaryname] = $letter;
102 krsort($letters, SORT_NUMERIC);
104 $old_ids = array();
105 if ($records = get_records('grade_letters', 'contextid', $context->id, 'lowerboundary ASC', 'id')) {
106 $old_ids = array_keys($records);
109 foreach($letters as $boundary=>$letter) {
110 $record = new object();
111 $record->letter = $letter;
112 $record->lowerboundary = $boundary;
113 $record->contextid = $context->id;
115 if ($old_id = array_pop($old_ids)) {
116 $record->id = $old_id;
117 update_record('grade_letters', $record);
118 } else {
119 insert_record('grade_letters', $record);
123 foreach($old_ids as $old_id) {
124 delete_records('grade_letters', 'id', $old_id);
127 redirect($returnurl);
131 //page header
132 if ($admin) {
133 admin_externalpage_print_header();
135 } else {
136 $navigation = grade_build_nav(__FILE__, $pagename, $COURSE->id);
137 /// Print header
138 print_header_simple($strgrades.': '.$pagename, ': '.$strgrades, $navigation, '', '', true, '', navmenu($COURSE));
140 $currenttab = 'lettersedit';
141 require('tabs.php');
144 $mform->display();
146 print_footer($COURSE);