Automatically generated installer lang files
[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');
28
29/// get url variables
30$courseid = optional_param('courseid', 0, PARAM_INT);
eef00ade 31$id = optional_param('id', 0, PARAM_INT); // The key's id
eed8e305 32$delete = optional_param('delete', 0, PARAM_BOOL);
33$confirm = optional_param('confirm', 0, PARAM_BOOL);
34
a6855934 35$PAGE->set_url('/grade/export/key.php', array('id' => $id, 'courseid' => $courseid));
eef00ade 36
eed8e305 37if ($id) {
5c75a0a3 38 if (!$key = $DB->get_record('user_private_key', array('id' => $id))) {
9b6a1a4b 39 print_error('invalidgroupid');
eed8e305 40 }
41 if (empty($courseid)) {
42 $courseid = $key->instance;
43
44 } else if ($courseid != $key->instance) {
9b6a1a4b 45 print_error('invalidcourseid');
eed8e305 46 }
47
eef00ade 48 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
9b6a1a4b 49 print_error('invalidcourseid');
eed8e305 50 }
51
52} else {
eef00ade 53 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
9b6a1a4b 54 print_error('invalidcourseid');
eed8e305 55 }
ace9051c 56 $key = new stdClass();
eed8e305 57}
58
59$key->courseid = $course->id;
60
61require_login($course);
62$context = get_context_instance(CONTEXT_COURSE, $course->id);
63require_capability('moodle/grade:export', $context);
64
d575afb8 65// extra security check
66if (!empty($key->userid) and $USER->id != $key->userid) {
9b6a1a4b 67 print_error('notownerofkey');
d575afb8 68}
69
eed8e305 70$returnurl = $CFG->wwwroot.'/grade/export/keymanager.php?id='.$course->id;
71
72if ($id and $delete) {
73 if (!$confirm) {
f3df5e14 74 $PAGE->set_title(get_string('deleteselectedkey'));
02114f13 75 $PAGE->set_heading($course->fullname);
f3df5e14 76 echo $OUTPUT->header();
eed8e305 77 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
78 $optionsno = array('id'=>$courseid);
dc6896ef
PS
79 $formcontinue = new single_button(new moodle_url('key.php', $optionsyes), get_string('yes'), 'get');
80 $formcancel = new single_button(new moodle_url('keymanager.php', $optionsno), get_string('no'), 'get');
03fcc729 81 echo $OUTPUT->confirm(get_string('deletekeyconfirm', 'userkey', $key->value), $formcontinue, $formcancel);
5a931394 82 echo $OUTPUT->footer();
eed8e305 83 die;
84
85 } else if (confirm_sesskey()){
5c75a0a3 86 $DB->delete_records('user_private_key', array('id' => $id));
eed8e305 87 redirect('keymanager.php?id='.$course->id);
88 }
89}
90
91/// First create the form
92$editform = new key_form();
93$editform->set_data($key);
94
95if ($editform->is_cancelled()) {
96 redirect($returnurl);
97
294ce987 98} elseif ($data = $editform->get_data()) {
eed8e305 99
100 if ($data->id) {
ace9051c 101 $record = new stdClass();
eed8e305 102 $record->id = $data->id;
103 $record->iprestriction = $data->iprestriction;
104 $record->validuntil = $data->validuntil;
5c75a0a3 105 $DB->update_record('user_private_key', $record);
eed8e305 106 } else {
107 create_user_key('grade/export', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
108 }
109
110 redirect($returnurl);
111}
112
0f78c4de 113$strkeys = get_string('userkeys', 'userkey');
eed8e305 114$strgrades = get_string('grades');
115
116if ($id) {
0f78c4de 117 $strheading = get_string('edituserkey', 'userkey');
eed8e305 118} else {
0f78c4de 119 $strheading = get_string('createuserkey', 'userkey');
eed8e305 120}
121
a6855934
PS
122$PAGE->navbar->add($strgrades, new moodle_url('/grade/index.php', array('id'=>$courseid)));
123$PAGE->navbar->add($strkeys, new moodle_url('/grade/export/keymanager.php', array('id'=>$courseid)));
f3df5e14 124$PAGE->navbar->add($strheading);
eed8e305 125
126/// Print header
f3df5e14 127$PAGE->set_title($strkeys);
02114f13 128$PAGE->set_heading($course->fullname);
f3df5e14 129echo $OUTPUT->header();
eed8e305 130
131$editform->display();
5a931394 132echo $OUTPUT->footer();
6c3ef410 133