MDL-24189 data module restore - Fix 1.x comments and ratings restore. Were missing...
[moodle.git] / grade / export / key.php
blob5d648f9c0dd908fa0e72f90f79cce18c3432eb4a
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/>.
19 require_once('../../config.php');
20 require_once('key_form.php');
22 /// get url variables
23 $courseid = optional_param('courseid', 0, PARAM_INT);
24 $id = optional_param('id', 0, PARAM_INT);
25 $delete = optional_param('delete', 0, PARAM_BOOL);
26 $confirm = optional_param('confirm', 0, PARAM_BOOL);
28 if ($id) {
29 if (!$key = get_record('user_private_key', 'id', $id)) {
30 error('Group ID was incorrect');
32 if (empty($courseid)) {
33 $courseid = $key->instance;
35 } else if ($courseid != $key->instance) {
36 error('Course ID was incorrect');
39 if (!$course = get_record('course', 'id', $courseid)) {
40 error('Course ID was incorrect');
43 } else {
44 if (!$course = get_record('course', 'id', $courseid)) {
45 error('Course ID was incorrect');
47 $key = new object();
50 $key->courseid = $course->id;
52 require_login($course);
53 $context = get_context_instance(CONTEXT_COURSE, $course->id);
54 require_capability('moodle/grade:export', $context);
56 // extra security check
57 if (!empty($key->userid) and $USER->id != $key->userid) {
58 error('You are not owner of this key');
61 $returnurl = $CFG->wwwroot.'/grade/export/keymanager.php?id='.$course->id;
63 if ($id and $delete) {
64 if (!$confirm) {
65 print_header(get_string('deleteselectedkey'), get_string('deleteselectedkey'));
66 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
67 $optionsno = array('id'=>$courseid);
68 notice_yesno(get_string('deletekeyconfirm', 'userkey', $key->value), 'key.php', 'keymanager.php', $optionsyes, $optionsno, 'get', 'get');
69 print_footer();
70 die;
72 } else if (confirm_sesskey()){
73 delete_records('user_private_key', 'id', $id);
74 redirect('keymanager.php?id='.$course->id);
78 /// First create the form
79 $editform = new key_form();
80 $editform->set_data($key);
82 if ($editform->is_cancelled()) {
83 redirect($returnurl);
85 } elseif ($data = $editform->get_data()) {
87 if ($data->id) {
88 $record = new object();
89 $record->id = $data->id;
90 $record->iprestriction = $data->iprestriction;
91 $record->validuntil = $data->validuntil;
92 update_record('user_private_key', $record);
93 } else {
94 create_user_key('grade/export', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
97 redirect($returnurl);
100 $strkeys = get_string('userkeys', 'userkey');
101 $strgrades = get_string('grades');
103 if ($id) {
104 $strheading = get_string('edituserkey', 'userkey');
105 } else {
106 $strheading = get_string('createuserkey', 'userkey');
110 $navlinks = array(array('name'=>$strgrades, 'link'=>$CFG->wwwroot.'/grade/index.php?id='.$courseid, 'type'=>'misc'),
111 array('name'=>$strkeys, 'link'=>$CFG->wwwroot.'/grade/export/keymanager.php?id='.$courseid, 'type'=>'misc'),
112 array('name'=>$strheading, 'link'=>'', 'type'=>'misc'));
113 $navigation = build_navigation($navlinks);
115 /// Print header
116 print_header_simple($strkeys, ': '.$strkeys, $navigation, '', '', true, '', navmenu($course));
118 $editform->display();
119 print_footer($course);