Automatically generated installer lang files
[moodle.git] / grade / import / key.php
CommitLineData
e060e33d 1<?php
a700b5de 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 * Import key management.
20 *
21 * @package moodlecore
22 * @copyright 2008 Nicolas Connault
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
a700b5de 26require_once('../../config.php');
27require_once('key_form.php');
28
29/// get url variables
30$courseid = optional_param('courseid', 0, PARAM_INT);
31$id = optional_param('id', 0, PARAM_INT);
32$delete = optional_param('delete', 0, PARAM_BOOL);
33$confirm = optional_param('confirm', 0, PARAM_BOOL);
34
a6855934 35$PAGE->set_url('/grade/import/key.php', array('courseid' => $courseid, 'id' => $id));
eef00ade 36
a700b5de 37if ($id) {
5c75a0a3 38 if (!$key = $DB->get_record('user_private_key', array('id' => $id))) {
f39c16e0 39 print_error('invalidgroupid');
a700b5de 40 }
41 if (empty($courseid)) {
42 $courseid = $key->instance;
43
44 } else if ($courseid != $key->instance) {
f39c16e0 45 print_error('invalidcourseid');
a700b5de 46 }
47
5c75a0a3 48 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
f39c16e0 49 print_error('invalidcourseid');
a700b5de 50 }
51
52} else {
5c75a0a3 53 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
f39c16e0 54 print_error('invalidcourseid');
a700b5de 55 }
ace9051c 56 $key = new stdClass();
a700b5de 57}
58
59$key->courseid = $course->id;
60
61require_login($course);
62$context = get_context_instance(CONTEXT_COURSE, $course->id);
63require_capability('moodle/grade:import', $context);
64
65// extra security check
66if (!empty($key->userid) and $USER->id != $key->userid) {
f39c16e0 67 print_error('notownerofkey');
a700b5de 68}
69
70$returnurl = $CFG->wwwroot.'/grade/import/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();
a700b5de 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();
a700b5de 83 die;
84
85 } else if (confirm_sesskey()){
5c75a0a3 86 $DB->delete_records('user_private_key', array('id' => $id));
a700b5de 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()) {
a700b5de 99
100 if ($data->id) {
ace9051c 101 $record = new stdClass();
a700b5de 102 $record->id = $data->id;
103 $record->iprestriction = $data->iprestriction;
104 $record->validuntil = $data->validuntil;
5c75a0a3 105 $DB->update_record('user_private_key', $record);
a700b5de 106 } else {
107 create_user_key('grade/import', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
108 }
109
110 redirect($returnurl);
111}
112
113$strkeys = get_string('userkeys', 'userkey');
114$strgrades = get_string('grades');
115
116if ($id) {
117 $strheading = get_string('edituserkey', 'userkey');
118} else {
119 $strheading = get_string('createuserkey', 'userkey');
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/import/keymanager.php', array('id'=>$courseid)));
f3df5e14 124$PAGE->navbar->add($strheading);
a700b5de 125
126/// Print header
f3df5e14 127$PAGE->set_title($strkeys);
02114f13 128$PAGE->set_heading($course->fullname);
f3df5e14 129echo $OUTPUT->header();
a700b5de 130
131$editform->display();
5a931394 132echo $OUTPUT->footer();