1<?php
2
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/>.
17
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
26require_once('../../config.php');
27require_once('key_form.php');
28require_once($CFG->dirroot.'/grade/lib.php');
29
30/// get url variables
31$courseid = optional_param('courseid', 0, PARAM_INT);
32$id       = optional_param('id', 0, PARAM_INT);
33$delete   = optional_param('delete', 0, PARAM_BOOL);
34$confirm  = optional_param('confirm', 0, PARAM_BOOL);
35
36$PAGE->set_url('/grade/import/key.php', array('courseid' => $courseid, 'id' => $id));
37
38if ($id) {
39    if (!$key = $DB->get_record('user_private_key', array('id' => $id))) {
40        print_error('invalidgroupid');
41    }
42    if (empty($courseid)) {
43        $courseid = $key->instance;
44
45    } else if ($courseid != $key->instance) {
46        print_error('invalidcourseid');
47    }
48
49    if (!$course = $DB->get_record('course', array('id' => $courseid))) {
50        print_error('invalidcourseid');
51    }
52
53} else {
54    if (!$course = $DB->get_record('course', array('id' => $courseid))) {
55        print_error('invalidcourseid');
56    }
57    $key = new stdClass();
58}
59
60$key->courseid = $course->id;
61
62require_login($course);
63$context = context_course::instance($course->id);
64require_capability('moodle/grade:import', $context);
65
66// Check if the user has at least one grade publishing capability.
67$plugins = grade_helper::get_plugins_import($course->id);
68if (!isset($plugins['keymanager'])) {
69    print_error('nopermissions');
70}
71
72// extra security check
73if (!empty($key->userid) and $USER->id != $key->userid) {
74    print_error('notownerofkey');
75}
76
77$returnurl = $CFG->wwwroot.'/grade/import/keymanager.php?id='.$course->id;
78
79if ($id and $delete) {
80    if (!$confirm) {
81        $PAGE->set_title(get_string('deleteselectedkey'));
82        $PAGE->set_heading($course->fullname);
83        echo $OUTPUT->header();
84        $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
85        $optionsno  = array('id'=>$courseid);
86        $formcontinue = new single_button(new moodle_url('key.php', $optionsyes), get_string('yes'), 'get');
87        $formcancel = new single_button(new moodle_url('keymanager.php', $optionsno), get_string('no'), 'get');
88        echo $OUTPUT->confirm(get_string('deletekeyconfirm', 'userkey', $key->value), $formcontinue, $formcancel);
89        echo $OUTPUT->footer();
90        die;
91
92    } else if (confirm_sesskey()){
93        $DB->delete_records('user_private_key', array('id' => $id));
94        redirect('keymanager.php?id='.$course->id);
95    }
96}
97
98/// First create the form
99$editform = new key_form();
100$editform->set_data($key);
101
102if ($editform->is_cancelled()) {
103    redirect($returnurl);
104
105} elseif ($data = $editform->get_data()) {
106
107    if ($data->id) {
108        $record = new stdClass();
109        $record->id            = $data->id;
110        $record->iprestriction = $data->iprestriction;
111        $record->validuntil    = $data->validuntil;
112        $DB->update_record('user_private_key', $record);
113    } else {
114        create_user_key('grade/import', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
115    }
116
117    redirect($returnurl);
118}
119
120$strkeys   = get_string('userkeys', 'userkey');
121$strgrades = get_string('grades');
122
123if ($id) {
124    $strheading = get_string('edituserkey', 'userkey');
125} else {
126    $strheading = get_string('createuserkey', 'userkey');
127}
128
129$PAGE->navbar->add($strgrades, new moodle_url('/grade/index.php', array('id'=>$courseid)));
130$PAGE->navbar->add($strkeys, new moodle_url('/grade/import/keymanager.php', array('id'=>$courseid)));
131$PAGE->navbar->add($strheading);
132
133/// Print header
134$PAGE->set_title($strkeys);
135$PAGE->set_heading($course->fullname);
136echo $OUTPUT->header();
137
138$editform->display();
139echo $OUTPUT->footer();
140