1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * A page for managing custom and standard scales
19 *
20 * @package   core_grades
21 * @copyright 2007 Petr Skoda
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once '../../../config.php';
26require_once $CFG->dirroot.'/grade/lib.php';
27require_once $CFG->libdir.'/gradelib.php';
28
29$courseid = optional_param('id', 0, PARAM_INT);
30$action   = optional_param('action', '', PARAM_ALPHA);
31
32$PAGE->set_url('/grade/edit/scale/index.php', array('id' => $courseid));
33
34/// Make sure they can even access this course
35if ($courseid) {
36    if (!$course = $DB->get_record('course', array('id' => $courseid))) {
37        print_error('invalidcourseid');
38    }
39    require_login($course);
40    $context = context_course::instance($course->id);
41    require_capability('moodle/course:managescales', $context);
42    $PAGE->set_pagelayout('admin');
43} else {
44    require_once $CFG->libdir.'/adminlib.php';
45    admin_externalpage_setup('scales');
46}
47
48/// return tracking object
49$gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'scale', 'courseid'=>$courseid));
50
51$strscale          = get_string('scale');
52$strstandardscale  = get_string('scalesstandard');
53$strcustomscales   = get_string('scalescustom');
54$strname           = get_string('name');
55$strdelete         = get_string('delete');
56$stredit           = get_string('edit');
57$srtcreatenewscale = get_string('scalescustomcreate');
58$strused           = get_string('used');
59$stredit           = get_string('edit');
60
61switch ($action) {
62    case 'delete':
63        if (!confirm_sesskey()) {
64            break;
65        }
66        $scaleid = required_param('scaleid', PARAM_INT);
67        if (!$scale = grade_scale::fetch(array('id'=>$scaleid))) {
68            break;
69        }
70
71        if (empty($scale->courseid)) {
72            require_capability('moodle/course:managescales', context_system::instance());
73        } else if ($scale->courseid != $courseid) {
74            print_error('invalidcourseid');
75        }
76
77        if (!$scale->can_delete()) {
78            break;
79        }
80
81        $deleteconfirmed = optional_param('deleteconfirmed', 0, PARAM_BOOL);
82
83        if (!$deleteconfirmed) {
84            $strdeletescale = get_string('delete'). ' '. get_string('scale');
85            $PAGE->navbar->add($strdeletescale);
86            $PAGE->set_title($strdeletescale);
87            $PAGE->set_heading($COURSE->fullname);
88            echo $OUTPUT->header();
89            $confirmurl = new moodle_url('index.php', array(
90                    'id' => $courseid, 'scaleid' => $scale->id,
91                    'action'=> 'delete',
92                    'sesskey' =>  sesskey(),
93                    'deleteconfirmed'=> 1));
94
95            echo $OUTPUT->confirm(get_string('scaleconfirmdelete', 'grades', $scale->get_name()), $confirmurl,
96                "index.php?id={$courseid}");
97            echo $OUTPUT->footer();
98            die;
99        } else {
100            $scale->delete();
101        }
102        break;
103}
104
105if (!$courseid) {
106    echo $OUTPUT->header();
107}
108
109$table = new html_table();
110$table2 = new html_table();
111$heading = '';
112
113if ($courseid and $scales = grade_scale::fetch_all_local($courseid)) {
114    $heading = $strcustomscales;
115
116    $data = array();
117    foreach($scales as $scale) {
118        $line = array();
119        $line[] = $scale->get_name() .'<div class="scale_options">'.str_replace(",", ", ", $scale->scale).'</div>';
120
121        $used = $scale->is_used();
122        $line[] = $used ? get_string('yes') : get_string('no');
123
124        $buttons = "";
125        $buttons .= grade_button('edit', $courseid, $scale);
126        if (!$used) {
127            $buttons .= grade_button('delete', $courseid, $scale);
128        }
129        $line[] = $buttons;
130        $data[] = $line;
131    }
132    $table->head  = array($strscale, $strused, $stredit);
133    $table->size  = array('70%', '20%', '10%');
134    $table->align = array('left', 'center', 'center');
135    $table->attributes['class'] = 'scaletable localscales generaltable';
136    $table->data  = $data;
137}
138
139if ($scales = grade_scale::fetch_all_global()) {
140    $heading = $strstandardscale;
141
142    $data = array();
143    foreach($scales as $scale) {
144        $line = array();
145        $line[] = $scale->get_name().'<div class="scale_options">'.str_replace(",", ", ", $scale->scale).'</div>';
146
147        $used = $scale->is_used();
148        $line[] = $used ? get_string('yes') : get_string('no');
149
150        $buttons = "";
151        if (has_capability('moodle/course:managescales', context_system::instance())) {
152            $buttons .= grade_button('edit', $courseid, $scale);
153        }
154        if (!$used and has_capability('moodle/course:managescales', context_system::instance())) {
155            $buttons .= grade_button('delete', $courseid, $scale);
156        }
157        $line[] = $buttons;
158        $data[] = $line;
159    }
160    $table2->head  = array($strscale, $strused, $stredit);
161    $table->attributes['class'] = 'scaletable globalscales generaltable';
162    $table2->size  = array('70%', '20%', '10%');
163    $table2->align = array('left', 'center', 'center');
164    $table2->data  = $data;
165}
166
167
168if ($courseid) {
169    print_grade_page_head($courseid, 'scale', 'scale', get_string('coursescales', 'grades'));
170}
171
172echo $OUTPUT->heading($strcustomscales, 3, 'main');
173echo html_writer::table($table);
174echo $OUTPUT->heading($strstandardscale, 3, 'main');
175echo html_writer::table($table2);
176echo $OUTPUT->container_start('buttons');
177echo $OUTPUT->single_button(new moodle_url('edit.php', array('courseid'=>$courseid)), $srtcreatenewscale);
178echo $OUTPUT->container_end();
179echo $OUTPUT->footer();
180