1
<?php // $Id: edit.php,v 1.3.2.1 2008/02/27 08:57:54 skodak Exp $
3
///////////////////////////////////////////////////////////////////////////
5
// NOTICE OF COPYRIGHT //
7
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8
// http://moodle.com //
10
// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
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. //
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: //
22
// http://www.gnu.org/copyleft/gpl.html //
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';
41
admin_externalpage_setup('letters');
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);
49
$returnurl = $CFG->wwwroot.'/grade/edit/letter/index.php?id='.$context->instanceid;
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;
64
$data->id = $context->id;
67
foreach ($letters as $boundary=>$letter) {
68
$gradelettername = 'gradeletter'.$i;
69
$gradeboundaryname = 'gradeboundary'.$i;
71
$data->$gradelettername = $letter;
72
$data->$gradeboundaryname = $boundary;
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()) {
83
} else if ($data = $mform->get_data()) {
84
if (!$admin and empty($data->override)) {
85
delete_records('grade_letters', 'contextid', $context->id);
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);
99
$letters[$data->$gradeboundaryname] = $letter;
102
krsort($letters, SORT_NUMERIC);
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);
119
insert_record('grade_letters', $record);
123
foreach($old_ids as $old_id) {
124
delete_records('grade_letters', 'id', $old_id);
127
redirect($returnurl);
133
admin_externalpage_print_header();
136
$navigation = grade_build_nav(__FILE__, $pagename, $COURSE->id);
138
print_header_simple($strgrades.': '.$pagename, ': '.$strgrades, $navigation, '', '', true, '', navmenu($COURSE));
140
$currenttab = 'lettersedit';
146
print_footer($COURSE);