Repository navigation
Expand file tree
/
Copy pathview.php
More file actions
201 lines (169 loc) · 7.68 KB
/
Copy pathview.php
File metadata and controls
201 lines (169 loc) · 7.68 KB
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
195
196
197
198
199
200
201
<?php
/**
* Test View - Personality Test Block
*
* @package block_personality_test
* @copyright 2026 SAVIO - Sistema de Aprendizaje Virtual Interactivo (UTB)
* @author SAVIO Development Team
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/lib.php');
if( !isloggedin() ){
return;
}
$courseid = required_param('cid', PARAM_INT);
$page = optional_param('page', 1, PARAM_INT);
$error = optional_param('error', 0, PARAM_INT);
$scroll_to_finish = optional_param('scroll_to_finish', 0, PARAM_INT);
if ($courseid == SITEID && !$courseid) {
redirect($CFG->wwwroot);
}
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$PAGE->set_course($course);
$context = $PAGE->context;
require_login($course);
// Check if the block is added to the course
if (!$DB->record_exists('block_instances', array('blockname' => 'personality_test', 'parentcontextid' => $context->id))) {
redirect(new moodle_url('/course/view.php', array('id' => $courseid)));
}
// If a user with reporting capability tries to open the student test view, redirect them to admin silently
if (has_capability('block/personality_test:viewstudentdata', $context) && !has_capability('block/personality_test:taketest', $context)) {
redirect(new moodle_url('/blocks/personality_test/admin_view.php', array('cid' => $courseid)), get_string('teachers_redirect_message', 'block_personality_test'), null, \core\output\notification::NOTIFY_INFO);
}
// Check for existing response
$existing_response = $DB->get_record('personality_test', array('user' => $USER->id));
// If test is completed, redirect to results
if ($existing_response && $existing_response->is_completed) {
redirect(new moodle_url('/blocks/personality_test/view_individual.php', array('cid' => $courseid, 'userid' => $USER->id)),
get_string('test_completed_redirect', 'block_personality_test'),
null, \core\output\notification::NOTIFY_INFO);
}
$PAGE->set_url('/blocks/personality_test/view.php', array('cid'=>$courseid, 'page'=>$page));
$title = get_string('pluginname', 'block_personality_test');
$PAGE->set_pagelayout('incourse');
$PAGE->set_title($title." : ".$course->fullname);
$PAGE->set_heading($title." : ".$course->fullname);
$PAGE->requires->css(new moodle_url('/blocks/personality_test/styles.css'));
// Pagination settings
$questions_per_page = 9;
$total_questions = 72;
$total_pages = ceil($total_questions / $questions_per_page);
// SECURITY: Validate that user cannot skip pages without completing previous ones
if ($existing_response && $page > 1) {
// Check all questions from page 1 to current page - 1
$max_allowed_page = 1;
for ($p = 1; $p < $page; $p++) {
$page_start = ($p - 1) * $questions_per_page + 1;
$page_end = min($p * $questions_per_page, $total_questions);
$page_complete = true;
for ($i = $page_start; $i <= $page_end; $i++) {
$field = "q{$i}";
if (!isset($existing_response->$field) || $existing_response->$field === null) {
$page_complete = false;
break;
}
}
if ($page_complete) {
$max_allowed_page = $p + 1;
} else {
break;
}
}
// If trying to access a page beyond allowed, redirect to max allowed
if ($page > $max_allowed_page) {
redirect(new moodle_url('/blocks/personality_test/view.php',
array('cid' => $courseid, 'page' => $max_allowed_page)));
}
}
// If coming from "continue test" link, calculate which page to show
if ($existing_response && !isset($_GET['page'])) {
// Find first unanswered question
$first_unanswered = null;
for ($i = 1; $i <= $total_questions; $i++) {
$field = "q{$i}";
if (!isset($existing_response->$field) || $existing_response->$field === null) {
$first_unanswered = $i;
break;
}
}
// Calculate page for first unanswered question
if ($first_unanswered !== null) {
$page = ceil($first_unanswered / $questions_per_page);
}
}
$start_question = ($page - 1) * $questions_per_page + 1;
$end_question = min($page * $questions_per_page, $total_questions);
// Calculate how many questions are answered
$answered_count = 0;
if ($existing_response) {
for ($i = 1; $i <= $total_questions; $i++) {
$field = "q{$i}";
if (isset($existing_response->$field) && $existing_response->$field !== null) {
$answered_count++;
}
}
}
echo $OUTPUT->header();
echo $OUTPUT->box_start('generalbox');
$questions = [];
for ($i = $start_question; $i <= $end_question; $i++) {
$field = "q{$i}";
$saved_value = ($existing_response && isset($existing_response->$field)) ? $existing_response->$field : null;
$questions[] = [
'number' => $i,
'question_text' => get_string("personality_test:q".$i, 'block_personality_test'),
'yes_label' => get_string('yes', 'block_personality_test'),
'no_label' => get_string('no', 'block_personality_test'),
'select_option_label' => get_string('select_option', 'block_personality_test'),
'class_yes' => ($saved_value === '1' || $saved_value === 1) ? 'selected' : '',
'class_no' => ($saved_value === '0' || $saved_value === 0) ? 'selected' : '',
'selected_none' => ($saved_value === null) ? 'selected' : '',
'selected_yes' => ($saved_value === '1' || $saved_value === 1) ? 'selected' : '',
'selected_no' => ($saved_value === '0' || $saved_value === 0) ? 'selected' : '',
'is_yes_selected_attr' => ($saved_value === '1' || $saved_value === 1) ? 'selected' : '',
'is_no_selected_attr' => ($saved_value === '0' || $saved_value === 0) ? 'selected' : '',
];
}
$hidden_answers = [];
if ($existing_response) {
for ($i = 1; $i <= 72; $i++) {
if ($i >= $start_question && $i <= $end_question) {
continue;
}
$field = "q{$i}";
if (isset($existing_response->$field) && $existing_response->$field !== null) {
$hidden_answers[] = [
'number' => $i,
'value' => $existing_response->$field
];
}
}
}
$should_auto_scroll = ($existing_response && $answered_count > 0 && $answered_count < 72 && !$scroll_to_finish);
$action_form = new moodle_url('/blocks/personality_test/save.php');
$template_data = [
'iconurl' => (new moodle_url('/blocks/personality_test/pix/icon.svg'))->out(),
'test_page_title' => get_string('test_page_title', 'block_personality_test'),
'test_intro_p1' => get_string('test_intro_p1', 'block_personality_test'),
'test_intro_p2' => get_string('test_intro_p2', 'block_personality_test'),
'test_benefit_note' => get_string('test_benefit_note', 'block_personality_test'),
'test_benefit_required' => get_string('test_benefit_required', 'block_personality_test'),
'action_form' => $action_form->out(),
'questions' => $questions,
'hidden_answers' => $hidden_answers,
'courseid' => $courseid,
'page' => $page,
'sesskey' => sesskey(),
'show_previous' => ($page > 1),
'show_next' => ($page < $total_pages),
'show_finish' => ($page >= $total_pages),
'btn_previous_label' => get_string('btn_previous', 'block_personality_test'),
'btn_next_label' => get_string('btn_next', 'block_personality_test'),
'btn_finish_label' => get_string('btn_finish', 'block_personality_test'),
'wwwroot' => $CFG->wwwroot,
'should_auto_scroll' => $should_auto_scroll,
'scroll_to_finish' => $scroll_to_finish,
];
echo $OUTPUT->render_from_template('block_personality_test/test_view', $template_data);
echo $OUTPUT->box_end();
echo $OUTPUT->footer();