~ubuntu-branches/ubuntu/jaunty/moodle/jaunty

« back to all changes in this revision

Viewing changes to grade/edit/letter/edit.php

  • Committer: Bazaar Package Importer
  • Author(s): Jordan Mantha, Matt Oquist
  • Date: 2009-02-25 15:16:22 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090225151622-0ekt1liwhv2obfza
Tags: 1.9.4.dfsg-0ubuntu1
* Merge with Debian git (Closes LP: #322961, #239481, #334611):
  - use Ubuntu's smarty lib directory for linking
  - use internal yui library 
  - add update-notifier support back in

[Matt Oquist]
  * renamed prerm script
  * significantly rewrote postinst and other maintainer scripts to improve
    user experience and package maintainability
    (Closes LP: #225662, #325450, #327843, #303078, #234609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php // $Id: edit.php,v 1.3.2.1 2008/02/27 08:57:54 skodak Exp $
 
2
 
 
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
///////////////////////////////////////////////////////////////////////////
 
25
 
 
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';
 
30
 
 
31
 
 
32
$contextid = optional_param('id', SYSCONTEXTID, PARAM_INT);
 
33
 
 
34
if (!$context = get_context_instance_by_id($contextid)) {
 
35
    error('Incorrect context id');
 
36
}
 
37
 
 
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
 
44
 
 
45
 
 
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;
 
50
 
 
51
} else {
 
52
    error('Incorrect context level');
 
53
}
 
54
 
 
55
require_capability('moodle/grade:manageletters', $context);
 
56
 
 
57
$strgrades = get_string('grades');
 
58
$pagename  = get_string('letters', 'grades');
 
59
 
 
60
$letters = grade_get_letters($context);
 
61
$num = count($letters) + 3;
 
62
 
 
63
$data = new object();
 
64
$data->id = $context->id;
 
65
 
 
66
$i = 1;
 
67
foreach ($letters as $boundary=>$letter) {
 
68
    $gradelettername = 'gradeletter'.$i;
 
69
    $gradeboundaryname = 'gradeboundary'.$i;
 
70
 
 
71
    $data->$gradelettername   = $letter;
 
72
    $data->$gradeboundaryname = $boundary;
 
73
    $i++;
 
74
}
 
75
$data->override = record_exists('grade_letters', 'contextid', $contextid);
 
76
 
 
77
$mform = new edit_letter_form(null, array('num'=>$num, 'admin'=>$admin));
 
78
$mform->set_data($data);
 
79
 
 
80
if ($mform->is_cancelled()) {
 
81
    redirect($returnurl);
 
82
 
 
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);
 
87
    }
 
88
 
 
89
    $letters = array();
 
90
    for($i=1; $i<$num+1; $i++) {
 
91
        $gradelettername = 'gradeletter'.$i;
 
92
        $gradeboundaryname = 'gradeboundary'.$i;
 
93
 
 
94
        if (array_key_exists($gradeboundaryname, $data) and $data->$gradeboundaryname != -1) {
 
95
            $letter = trim($data->$gradelettername);
 
96
            if ($letter == '') {
 
97
                continue;
 
98
            }
 
99
            $letters[$data->$gradeboundaryname] = $letter;
 
100
        }
 
101
    }
 
102
    krsort($letters, SORT_NUMERIC);
 
103
 
 
104
    $old_ids = array();
 
105
    if ($records = get_records('grade_letters', 'contextid', $context->id, 'lowerboundary ASC', 'id')) {
 
106
        $old_ids = array_keys($records);
 
107
    }
 
108
 
 
109
    foreach($letters as $boundary=>$letter) {
 
110
        $record = new object();
 
111
        $record->letter        = $letter;
 
112
        $record->lowerboundary = $boundary;
 
113
        $record->contextid     = $context->id;
 
114
 
 
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);
 
120
        }
 
121
    }
 
122
 
 
123
    foreach($old_ids as $old_id) {
 
124
        delete_records('grade_letters', 'id', $old_id);
 
125
    }
 
126
 
 
127
    redirect($returnurl);
 
128
}
 
129
 
 
130
 
 
131
//page header
 
132
if ($admin) {
 
133
    admin_externalpage_print_header();
 
134
 
 
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));
 
139
 
 
140
    $currenttab = 'lettersedit';
 
141
    require('tabs.php');
 
142
}
 
143
 
 
144
$mform->display();
 
145
 
 
146
print_footer($COURSE);
 
147
?>