Merge branch 'MDL-70476_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / grade / export / key.php
CommitLineData
e060e33d 1<?php
eed8e305 2
e060e33d 3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
8ad36f4c 17
eef00ade 18/**
19 * Grade export key edit page.
20 *
21 * @package moodlecore
22 * @copyright 2008 Petr Skoda
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6c3ef410 24 */
eef00ade 25
eed8e305 26require_once('../../config.php');
27require_once('key_form.php');
ead4d41e 28require_once($CFG->dirroot.'/grade/lib.php');
eed8e305 29
30/// get url variables
31$courseid = optional_param('courseid', 0, PARAM_INT);
eef00ade 32$id = optional_param('id', 0, PARAM_INT); // The key's id
eed8e305 33$delete = optional_param('delete', 0, PARAM_BOOL);
34$confirm = optional_param('confirm', 0, PARAM_BOOL);
35
a6855934 36$PAGE->set_url('/grade/export/key.php', array('id' => $id, 'courseid' => $courseid));
eef00ade 37
eed8e305 38if ($id) {
5c75a0a3 39 if (!$key = $DB->get_record('user_private_key', array('id' => $id))) {
9b6a1a4b 40 print_error('invalidgroupid');
eed8e305 41 }
42 if (empty($courseid)) {
43 $courseid = $key->instance;
44
45 } else if ($courseid != $key->instance) {
9b6a1a4b 46 print_error('invalidcourseid');
eed8e305 47 }
48
eef00ade 49 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
9b6a1a4b 50 print_error('invalidcourseid');
eed8e305 51 }
52
53} else {
eef00ade 54 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
9b6a1a4b 55 print_error('invalidcourseid');
eed8e305 56 }
ace9051c 57 $key = new stdClass();
eed8e305 58}
59
60$key->courseid = $course->id;
61
62require_login($course);
d4060472 63$context = context_course::instance($course->id);
eed8e305 64require_capability('moodle/grade:export', $context);
65
ead4d41e
SL
66// Check if the user has at least one grade publishing capability.
67$plugins = grade_helper::get_plugins_export($course->id);
68if (!isset($plugins['keymanager'])) {
69 print_error('nopermissions');
70}
71
d575afb8 72// extra security check
73if (!empty($key->userid) and $USER->id != $key->userid) {
9b6a1a4b 74 print_error('notownerofkey');
d575afb8 75}
76
eed8e305 77$returnurl = $CFG->wwwroot.'/grade/export/keymanager.php?id='.$course->id;
78
79if ($id and $delete) {
80 if (!$confirm) {
f3df5e14 81 $PAGE->set_title(get_string('deleteselectedkey'));
02114f13 82 $PAGE->set_heading($course->fullname);
f3df5e14 83 echo $OUTPUT->header();
eed8e305 84 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
85 $optionsno = array('id'=>$courseid);
dc6896ef
PS
86 $formcontinue = new single_button(new moodle_url('key.php', $optionsyes), get_string('yes'), 'get');
87 $formcancel = new single_button(new moodle_url('keymanager.php', $optionsno), get_string('no'), 'get');
03fcc729 88 echo $OUTPUT->confirm(get_string('deletekeyconfirm', 'userkey', $key->value), $formcontinue, $formcancel);
5a931394 89 echo $OUTPUT->footer();
eed8e305 90 die;
91
92 } else if (confirm_sesskey()){
5c75a0a3 93 $DB->delete_records('user_private_key', array('id' => $id));
eed8e305 94 redirect('keymanager.php?id='.$course->id);
95 }
96}
97
98/// First create the form
99$editform = new key_form();
100$editform->set_data($key);
101
102if ($editform->is_cancelled()) {
103 redirect($returnurl);
104
294ce987 105} elseif ($data = $editform->get_data()) {
eed8e305 106
107 if ($data->id) {
ace9051c 108 $record = new stdClass();
eed8e305 109 $record->id = $data->id;
110 $record->iprestriction = $data->iprestriction;
111 $record->validuntil = $data->validuntil;
5c75a0a3 112 $DB->update_record('user_private_key', $record);
eed8e305 113 } else {
114 create_user_key('grade/export', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
115 }
116
117 redirect($returnurl);
118}
119
0f78c4de 120$strkeys = get_string('userkeys', 'userkey');
eed8e305 121$strgrades = get_string('grades');
122
123if ($id) {
0f78c4de 124 $strheading = get_string('edituserkey', 'userkey');
eed8e305 125} else {
0f78c4de 126 $strheading = get_string('createuserkey', 'userkey');
eed8e305 127}
128
a6855934
PS
129$PAGE->navbar->add($strgrades, new moodle_url('/grade/index.php', array('id'=>$courseid)));
130$PAGE->navbar->add($strkeys, new moodle_url('/grade/export/keymanager.php', array('id'=>$courseid)));
f3df5e14 131$PAGE->navbar->add($strheading);
eed8e305 132
133/// Print header
f3df5e14 134$PAGE->set_title($strkeys);
02114f13 135$PAGE->set_heading($course->fullname);
f3df5e14 136echo $OUTPUT->header();
eed8e305 137
138$editform->display();
5a931394 139echo $OUTPUT->footer();
6c3ef410 140