MDL-34837 backup: Restore does not fail when some courses have the same name
[moodle.git] / grade / import / key.php
blob96d191bf9d0dc5c10f811922e84acaf3da4796ac
1 <?php
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.
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/>.
18 /**
19 * Import key management.
21 * @package moodlecore
22 * @copyright 2008 Nicolas Connault
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../../config.php');
27 require_once('key_form.php');
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);
35 $PAGE->set_url('/grade/import/key.php', array('courseid' => $courseid, 'id' => $id));
37 if ($id) {
38 if (!$key = $DB->get_record('user_private_key', array('id' => $id))) {
39 print_error('invalidgroupid');
41 if (empty($courseid)) {
42 $courseid = $key->instance;
44 } else if ($courseid != $key->instance) {
45 print_error('invalidcourseid');
48 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
49 print_error('invalidcourseid');
52 } else {
53 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
54 print_error('invalidcourseid');
56 $key = new stdClass();
59 $key->courseid = $course->id;
61 require_login($course);
62 $context = context_course::instance($course->id);
63 require_capability('moodle/grade:import', $context);
65 // extra security check
66 if (!empty($key->userid) and $USER->id != $key->userid) {
67 print_error('notownerofkey');
70 $returnurl = $CFG->wwwroot.'/grade/import/keymanager.php?id='.$course->id;
72 if ($id and $delete) {
73 if (!$confirm) {
74 $PAGE->set_title(get_string('deleteselectedkey'));
75 $PAGE->set_heading($course->fullname);
76 echo $OUTPUT->header();
77 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
78 $optionsno = array('id'=>$courseid);
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');
81 echo $OUTPUT->confirm(get_string('deletekeyconfirm', 'userkey', $key->value), $formcontinue, $formcancel);
82 echo $OUTPUT->footer();
83 die;
85 } else if (confirm_sesskey()){
86 $DB->delete_records('user_private_key', array('id' => $id));
87 redirect('keymanager.php?id='.$course->id);
91 /// First create the form
92 $editform = new key_form();
93 $editform->set_data($key);
95 if ($editform->is_cancelled()) {
96 redirect($returnurl);
98 } elseif ($data = $editform->get_data()) {
100 if ($data->id) {
101 $record = new stdClass();
102 $record->id = $data->id;
103 $record->iprestriction = $data->iprestriction;
104 $record->validuntil = $data->validuntil;
105 $DB->update_record('user_private_key', $record);
106 } else {
107 create_user_key('grade/import', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
110 redirect($returnurl);
113 $strkeys = get_string('userkeys', 'userkey');
114 $strgrades = get_string('grades');
116 if ($id) {
117 $strheading = get_string('edituserkey', 'userkey');
118 } else {
119 $strheading = get_string('createuserkey', 'userkey');
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)));
124 $PAGE->navbar->add($strheading);
126 /// Print header
127 $PAGE->set_title($strkeys);
128 $PAGE->set_heading($course->fullname);
129 echo $OUTPUT->header();
131 $editform->display();
132 echo $OUTPUT->footer();