Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / grade / import / key.php
blobdf9a4d8063c5bb6af29a54a1afaaa226d6676204
1 <?php //$Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
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 if ($id) {
36 if (!$key = get_record('user_private_key', 'id', $id)) {
37 error('Group ID was incorrect');
39 if (empty($courseid)) {
40 $courseid = $key->instance;
42 } else if ($courseid != $key->instance) {
43 error('Course ID was incorrect');
46 if (!$course = get_record('course', 'id', $courseid)) {
47 error('Course ID was incorrect');
50 } else {
51 if (!$course = get_record('course', 'id', $courseid)) {
52 error('Course ID was incorrect');
54 $key = new object();
57 $key->courseid = $course->id;
59 require_login($course);
60 $context = get_context_instance(CONTEXT_COURSE, $course->id);
61 require_capability('moodle/grade:import', $context);
63 // extra security check
64 if (!empty($key->userid) and $USER->id != $key->userid) {
65 error('You are not owner of this key');
68 $returnurl = $CFG->wwwroot.'/grade/import/keymanager.php?id='.$course->id;
70 if ($id and $delete) {
71 if (!$confirm) {
72 print_header(get_string('deleteselectedkey'), get_string('deleteselectedkey'));
73 $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
74 $optionsno = array('id'=>$courseid);
75 notice_yesno(get_string('deletekeyconfirm', 'userkey', $key->value), 'key.php', 'keymanager.php', $optionsyes, $optionsno, 'get', 'get');
76 print_footer();
77 die;
79 } else if (confirm_sesskey()){
80 delete_records('user_private_key', 'id', $id);
81 redirect('keymanager.php?id='.$course->id);
85 /// First create the form
86 $editform = new key_form();
87 $editform->set_data($key);
89 if ($editform->is_cancelled()) {
90 redirect($returnurl);
92 } elseif ($data = $editform->get_data()) {
94 if ($data->id) {
95 $record = new object();
96 $record->id = $data->id;
97 $record->iprestriction = $data->iprestriction;
98 $record->validuntil = $data->validuntil;
99 update_record('user_private_key', $record);
100 } else {
101 create_user_key('grade/import', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
104 redirect($returnurl);
107 $strkeys = get_string('userkeys', 'userkey');
108 $strgrades = get_string('grades');
110 if ($id) {
111 $strheading = get_string('edituserkey', 'userkey');
112 } else {
113 $strheading = get_string('createuserkey', 'userkey');
117 $navlinks = array(array('name'=>$strgrades, 'link'=>$CFG->wwwroot.'/grade/index.php?id='.$courseid, 'type'=>'misc'),
118 array('name'=>$strkeys, 'link'=>$CFG->wwwroot.'/grade/import/keymanager.php?id='.$courseid, 'type'=>'misc'),
119 array('name'=>$strheading, 'link'=>'', 'type'=>'misc'));
120 $navigation = build_navigation($navlinks);
122 /// Print header
123 print_header_simple($strkeys, ': '.$strkeys, $navigation, '', '', true, '', navmenu($course));
125 $editform->display();
126 print_footer($course);