-
Notifications
You must be signed in to change notification settings - Fork 3
/
set_responses.php
194 lines (167 loc) · 6.9 KB
/
set_responses.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Security questions response form for users
*
* @package tool_securityquestions
* @copyright Peter Burnett <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../../config.php');
require_once(__DIR__.'/locallib.php');
// PAGE->seturl must be set before require_login to avoid infinite redir loop.
$url = new moodle_url('/admin/tool/securityquestions/set_responses.php');
$PAGE->set_url($url);
// First, check if the require_recent_login function exists.
if (function_exists('require_recent_login')) {
// Require recent login to edit responses to questions.
require_recent_login();
} else {
require_login();
}
// Set session variable here PAST require_login, so redirect loops are resolved.
$SESSION->tool_securityquestions_presentedresponse = true;
$PAGE->set_context(context_user::instance($USER->id));
$PAGE->set_title('Edit Security Question Responses');
$PAGE->set_heading(get_string('setresponsespagestring', 'tool_securityquestions'));
$PAGE->set_cacheable(false);
// Add navigation menu.
if ($node = $PAGE->settingsnav->find('usercurrentsettings', null)) {
$PAGE->navbar->add($node->get_content(), $node->action());
}
$PAGE->navbar->add(get_string('setresponsessettingsmenu', 'tool_securityquestions'), $url);
$notifysuccess = false;
$notifycontent = '';
if (!empty($SESSION->wantsurl)) {
$prevurl = $SESSION->wantsurl;
} else {
$prevurl = new moodle_url('/user/preferences.php');
}
$success = optional_param('success', -1, PARAM_INT);
$active = count(tool_securityquestions_get_active_user_responses($USER));
// If deleting a response, process here.
$delete = optional_param('delete', 0, PARAM_INT);
if ($delete != 0 && confirm_sesskey()) {
if ($active - 1 < get_config('tool_securityquestions', 'minuserquestions')) {
// Unable to delete, not enough responses set.
$deletestatus = false;
} else {
if (tool_securityquestions_delete_response($delete)) {
$deletestatus = true;
} else {
// Unable to delete (question probably doesnt exist).
$deletestatus = false;
}
}
}
$form = new \tool_securityquestions\form\set_responses();
// Get currently answered questions content for JS.
$active = tool_securityquestions_get_active_user_responses($USER);
$questions = array_map(function($element) {
global $DB;
$question = $DB->get_record('tool_securityquestions', array('id' => $element->qid));
return $question->content;
}, $active);
if ($form->is_cancelled()) {
// Unset wantsurl.
unset($SESSION->wantsurl);
redirect($prevurl);
} else if ($form->no_submit_button_pressed()) {
// Delete button pressed somewhere on the form.
$data = $form->get_submitted_data();
$deletes = $data->preset;
for ($i = 0; $i < $deletes; $i++) {
$name = "delete$i";
$qidname = "qid$i";
if (!empty($data->$name)) {
$url = new moodle_url('/admin/tool/securityquestions/set_responses.php',
array('delete' => $data->$qidname, 'sesskey' => sesskey()));
redirect($url);
}
}
} else if ($fromform = $form->get_data()) {
// Check for updated responses.
$updates = $fromform->preset;
for ($i = 0; $i < $updates; $i++) {
$name = "preset$i";
$qidname = "qid$i";
if (!empty($fromform->$name)) {
// Update response.
tool_securityquestions_add_response($fromform->$name, $fromform->$qidname);
}
}
// Add new responses.
$num = $fromform->new;
for ($i = 0; $i < $num; $i++) {
$qname = "questions$i";
$rname = "response$i";
$qid = $fromform->$qname;
$response = $fromform->$rname;
if (!empty($response)) {
// Check for failure before moving on.
tool_securityquestions_add_response($response, $qid);
}
}
// Redirect after DB actions.
if (!empty($SESSION->wantsurl)) {
// Got here from a redirect.
unset($SESSION->wantsurl);
redirect($prevurl);
} else {
redirect($prevurl);
}
}
// Build the page output.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('setresponsespagestring', 'tool_securityquestions'));
echo '<br>';
// Output a notification for grace period time remaining, if set.
$logintime = get_user_preferences('tool_securityquestions_logintime', null);
if (!get_config('tool_securityquestions', 'mandatoryquestions') && $logintime != null
&& get_config('tool_securityquestions', 'graceperiod')) {
// Find remaining time in grace period, and output it formatted nicely.
$timerem = ($logintime + get_config('tool_securityquestions', 'graceperiod')) - time();
// Check if there is time left.
if ($timerem > 0) {
$duration = sprintf('%02d:%02d:%02d', ($timerem / HOURSECS), (($timerem / MINSECS) % MINSECS), $timerem % MINSECS);
echo $OUTPUT->notification(get_string('formgraceperiodtimerem', 'tool_securityquestions', $duration), 'notifymessage');
}
}
// Notification for general information.
$minrequired = get_config('tool_securityquestions', 'minuserquestions');
$generalnotification = get_string('formquestionresponsesreq', 'tool_securityquestions', $minrequired);
if (count($active) > 0) {
// Output a notification explaining how questions work when already set.
$generalnotification .= '<br>' . get_string('formquestioninfo', 'tool_securityquestions');
}
echo $OUTPUT->notification($generalnotification, 'notifymessage');
if ($delete != 0) {
if ($deletestatus) {
echo $OUTPUT->notification(get_string('formresponsedeleted', 'tool_securityquestions'), 'notifysuccess');
} else {
echo $OUTPUT->notification(get_string('formresponsenotdeleted', 'tool_securityquestions'), 'notifyerror');
}
}
// Output notifications for status.
if ($success == 1) {
echo $OUTPUT->notification(get_string('formresponserecorded', 'tool_securityquestions'), 'notifysuccess');
}
if ($success == 0) {
echo $OUTPUT->notification(get_string('formresponsenotrecorded', 'tool_securityquestions'), 'notifyerror');
}
$form->display();
$PAGE->requires->js_call_amd('tool_securityquestions/hide_selector_question', 'init', array($questions));
echo $OUTPUT->footer();