-
Notifications
You must be signed in to change notification settings - Fork 37
/
changestate.php
142 lines (127 loc) · 5.75 KB
/
changestate.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
<?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/>.
/**
* Change state question page.
*
* This code is based on question/classes/bank/view.php
*
* @package mod_studentquiz
* @copyright 2022 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_studentquiz\local\studentquiz_helper;
use mod_studentquiz\local\studentquiz_question;
use mod_studentquiz\utils;
use mod_studentquiz\external\update_question_state;
require_once(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/locallib.php');
$approveselected = optional_param('approveselected', false, PARAM_BOOL);
$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
$cmid = optional_param('cmid', 0, PARAM_INT);
$courseid = optional_param('courseid', 0, PARAM_INT);
$course = get_course($courseid);
$cm = get_coursemodule_from_id('studentquiz', $cmid, $courseid, false, MUST_EXIST);
$context = context_module::instance($cmid);
require_capability('mod/studentquiz:changestate', $context);
if ($returnurl) {
$returnurl = new moodle_url($returnurl);
}
$url = new moodle_url('/mod/studentquiz/changestate.php');
$PAGE->set_url($url);
$PAGE->set_course($course);
$PAGE->set_context($context);
$PAGE->set_title(get_string('state_toggle', 'mod_studentquiz'));
$PAGE->set_heading($course->fullname);
$PAGE->activityheader->disable();
$PAGE->set_secondary_active_tab("studentquiz");
// Load course and course module requested.
if ($cmid) {
if (!$module = get_coursemodule_from_id('studentquiz', $cmid)) {
throw new moodle_exception("invalidcoursemodule");
}
if (!$course = $DB->get_record('course', array('id' => $module->course))) {
throw new moodle_exception("coursemisconf");
}
} else {
throw new moodle_exception("invalidcoursemodule");
}
require_login($module->course, false, $module);
$rawquestionids = mod_studentquiz_helper_get_ids_by_raw_submit($_REQUEST);
// If user has already confirmed the action.
if ($approveselected && ($confirm = optional_param('confirm', '', PARAM_ALPHANUM))
&& confirm_sesskey()) {
// If teacher has already confirmed the action.
$approveselected = required_param('approveselected', PARAM_RAW);
$state = required_param('state', PARAM_INT);
if ($confirm == md5($approveselected)) {
// We should not change the state if state is not being changed.
if ($state !== -1) {
if ($questionlist = explode(',', $approveselected)) {
foreach ($questionlist as $questionid) {
$questionid = (int) $questionid;
$question = question_bank::load_question($questionid);
$studentquizquestion = studentquiz_question::get_studentquiz_question_from_question($question);
update_question_state::execute($course->id, $cmid, $studentquizquestion->get_id(), $state);
}
}
}
redirect($returnurl);
} else {
throw new moodle_exception("invalidconfirm', 'question");
}
}
echo $OUTPUT->header();
if ($approveselected) {
// Make a list of all the questions that are selected.
$rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
$questionlist = ''; // Comma separated list of ids of questions to be deleted.
$questionnames = ''; // String with names of questions separated by <br/> with an asterix in front of those that are in use.
$inuse = false; // Set to true if at least one of the questions is in use.
$questionids = mod_studentquiz_helper_get_ids_by_raw_submit($rawquestions);
$states = utils::get_states($questionids);
$statedesc = studentquiz_helper::get_state_descriptions();
$questionnames = utils::get_question_names($questionids);
$questions = [];
foreach ($rawquestions as $key => $value) { // Parse input for question ids.
if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
$key = $matches[1];
$questionlist .= $key.',';
$question = new stdClass();
$question->name = '';
if (questions_in_use([$key])) {
$question->name .= '* ';
$inuse = true;
}
$question->name .= $questionnames[$key]->name;
$question->state = $statedesc[$states[$key]->state];
$questions[] = $question;
}
}
if (!$questionlist) {
// No questions were selected.
redirect($returnurl);
}
$questionlist = rtrim($questionlist, ',');
// Add an explanation about questions in use.
$approveurl = new \moodle_url($url, ['approveselected' => $questionlist, 'state' => -1,
'confirm' => md5($questionlist), 'sesskey' => sesskey(), 'returnurl' => $returnurl,
'cmid' => $cmid, 'courseid' => $courseid]);
$continue = new \single_button($approveurl, get_string('state_toggle', 'studentquiz'), 'get');
$renderer = $PAGE->get_renderer('mod_studentquiz', 'overview');
$currentstatequestions = $renderer->render_current_state_questions($questions, $inuse);
echo $renderer->render_change_state_dialog($currentstatequestions, $continue, $returnurl);
}
echo $OUTPUT->footer();