-
Notifications
You must be signed in to change notification settings - Fork 15
/
view.php
428 lines (361 loc) · 18.2 KB
/
view.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?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/>.
/**
* Starting page to display the surveypro cover page.
*
* @package mod_surveypro
* @copyright 2013 onwards kordan <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_surveypro\utility_layout;
use mod_surveypro\utility_page;
use mod_surveypro\utility_mform;
use mod_surveypro\view_cover;
use mod_surveypro\view_submissionlist;
use mod_surveypro\view_submissionform;
use mod_surveypro\view_submissionsearch;
use mod_surveypro\local\form\userform;
use mod_surveypro\local\form\usersearch;
require_once(dirname(__FILE__).'/../../config.php');
require_once(dirname(__FILE__).'/lib.php');
$defaultsection = surveypro_get_defaults_section_per_area('surveypro');
$id = optional_param('id', 0, PARAM_INT); // Course_module id.
$s = optional_param('s', 0, PARAM_INT); // Surveypro instance id.
$section = optional_param('section', $defaultsection, PARAM_ALPHAEXT); // The section of code to execute.
$edit = optional_param('edit', -1, PARAM_BOOL);
// Verify I used correct names all along the module code.
$validsections = ['cover', 'submissionslist', 'submissionform', 'searchsubmissions'];
if (!in_array($section, $validsections)) {
$message = 'The section param \''.$section.'\' is invalid.';
debugging('Error at line '.__LINE__.' of file '.__FILE__.'. '.$message , DEBUG_DEVELOPER);
}
// End of: Verify I used correct names all along the module code.
if (!empty($id)) {
[$course, $cm] = get_course_and_cm_from_cmid($id, 'surveypro');
$surveypro = $DB->get_record('surveypro', ['id' => $cm->instance], '*', MUST_EXIST);
} else {
$surveypro = $DB->get_record('surveypro', ['id' => $s], '*', MUST_EXIST);
$course = $DB->get_record('course', ['id' => $surveypro->course], '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('surveypro', $surveypro->id, $course->id, false, MUST_EXIST);
}
require_course_login($course, false, $cm);
$context = \context_module::instance($cm->id);
// Utilitypage is going to be used in each section. This is the reason why I load it here.
$utilitypageman = new utility_page($cm, $surveypro);
// MARK cover.
if ($section == 'cover') {
// Get additional specific params.
// Required capability.
$canmanageitems = has_capability('mod/surveypro:manageitems', $context);
$canaccessreserveditems = has_capability('mod/surveypro:accessreserveditems', $context);
// Calculations.
// If you are an admin and no items are in this surveypro, you will be redireted.
$utilitylayoutman = new utility_layout($cm, $surveypro);
$utilitylayoutman->noitem_redirect();
$itemcount = $utilitylayoutman->has_items(0, SURVEYPRO_TYPEFIELD, $canmanageitems, $canaccessreserveditems, true);
$coverman = new view_cover($cm, $context, $surveypro);
// Set $PAGE params.
$paramurl = ['s' => $surveypro->id, 'area' => 'surveypro', 'section' => 'cover'];
$url = new \moodle_url('/mod/surveypro/view.php', $paramurl);
$PAGE->set_url($url);
$PAGE->set_context($context);
$PAGE->set_cm($cm);
$PAGE->set_title($surveypro->name);
$PAGE->set_heading($course->shortname);
$PAGE->navbar->add(get_string('surveypro_dashboard', 'mod_surveypro'));
// Is it useful? $PAGE->add_body_class('mediumwidth');.
$utilitypageman->manage_editbutton($edit);
// Output starts here.
echo $OUTPUT->header();
$actionbar = new \mod_surveypro\output\action_bar($cm, $context, $surveypro);
echo $actionbar->draw_view_action_bar();
if (!$itemcount) { // Admin was redirected. Student gets the alert.
$message = get_string('noitemsfound', 'mod_surveypro');
echo $OUTPUT->notification($message, 'notifyproblem');
} else {
$coverman->display_cover();
}
}
// MARK submissionslist.
// This section serves the page to...
// - display the list of all gathered submissions;
// - duplicate a submission;
// - delete a submission;
// - delete all gathered submissions;
// - print to PDF a submission.
if ($section == 'submissionslist') {
// Get additional specific params.
$tifirst = optional_param('tifirst', '', PARAM_ALPHA); // First letter of the name.
$tilast = optional_param('tilast', '', PARAM_ALPHA); // First letter of the surname.
// $tsort = optional_param('tsort', '', PARAM_ALPHA); // Field asked to sort the table for.
// A response was submitted.
$justsubmitted = optional_param('justsubmitted', 0, PARAM_INT);
$responsestatus = optional_param('responsestatus', 0, PARAM_INT);
// The list is managed.
$submissionid = optional_param('submissionid', 0, PARAM_INT);
$action = optional_param('act', SURVEYPRO_NOACTION, PARAM_INT);
$mode = optional_param('view', SURVEYPRO_NOMODE, PARAM_INT);
$confirm = optional_param('cnf', SURVEYPRO_UNCONFIRMED, PARAM_INT);
$searchquery = optional_param('searchquery', '', PARAM_RAW);
if ($action != SURVEYPRO_NOACTION) {
require_sesskey();
}
// Calculations.
$submissionlistman = new view_submissionlist($cm, $context, $surveypro);
$submissionlistman->setup($submissionid, $action, $mode, $confirm, $searchquery);
if ($action == SURVEYPRO_RESPONSETOPDF) {
$submissionlistman->submission_to_pdf();
die();
}
// Perform action before PAGE. (The content of the admin block depends on the output of these actions).
$submissionlistman->actions_execution();
// Set $PAGE params.
$paramurl = ['s' => $surveypro->id, 'area' => 'surveypro', 'section' => 'submissionslist'];
$url = new \moodle_url('/mod/surveypro/view.php', $paramurl);
$PAGE->set_url($url);
$PAGE->set_context($context);
$PAGE->set_cm($cm);
$PAGE->set_title($surveypro->name);
$PAGE->set_heading($course->shortname);
$PAGE->navbar->add(get_string('surveypro_responses', 'mod_surveypro'));
// Is it useful? $PAGE->add_body_class('mediumwidth');.
$utilitypageman->manage_editbutton($edit);
// Output starts here.
echo $OUTPUT->header();
$actionbar = new \mod_surveypro\output\action_bar($cm, $context, $surveypro);
echo $actionbar->draw_view_action_bar();
if (!empty($justsubmitted)) {
$submissionlistman->show_thanks_page($responsestatus, $justsubmitted);
} else {
$submissionlistman->actions_feedback(); // Action feedback after PAGE.
$submissionlistman->show_action_buttons($tifirst, $tilast);
$submissionlistman->display_submissions_table();
$submissionlistman->trigger_event(); // Event: all_submissions_viewed.
}
}
// MARK submissionform.
// This section serves the page to...
// - add a new submission [$mode = SURVEYPRO_NEWRESPONSEMODE];
// - edit existing submissions [$mode = SURVEYPRO_EDITMODE];
// - view in readonly mode [$mode = SURVEYPRO_READONLYMODE];
// - preview submission form [$mode = SURVEYPRO_PREVIEWMODE];
if ($section == 'submissionform') {
// Get additional specific params.
$submissionid = optional_param('submissionid', 0, PARAM_INT);
$formpage = optional_param('formpage', 1, PARAM_INT); // Form page number.
$mode = optional_param('mode', SURVEYPRO_NOMODE, PARAM_INT);
$begin = optional_param('begin', 0, PARAM_INT);
$overflowpage = optional_param('overflowpage', 0, PARAM_INT); // Went the user to a overflow page?
// Calculations.
mod_surveypro\utility_mform::register_form_elements();
$submissionformman = new view_submissionform($cm, $context, $surveypro);
$submissionformman->setup($submissionid, $formpage, $mode);
$utilitylayoutman = new utility_layout($cm, $surveypro);
$utilitylayoutman->add_custom_css();
// Begin of: define $user_form return url.
$paramurl = ['s' => $cm->instance, 'mode' => $mode, 'section' => 'submissionform'];
$formurl = new \moodle_url('/mod/surveypro/view.php', $paramurl);
// End of: define $user_form return url.
// Begin of: prepare params for the form.
$formparams = new \stdClass();
$formparams->cm = $cm;
$formparams->surveypro = $surveypro;
$formparams->submissionid = $submissionid;
$formparams->mode = $mode;
$formparams->userformpagecount = $submissionformman->get_userformpagecount();
$formparams->canaccessreserveditems = has_capability('mod/surveypro:accessreserveditems', $context);
$formparams->userfirstpage = $submissionformman->get_userfirstpage(); // The user first page
$formparams->userlastpage = $submissionformman->get_userlastpage(); // The user last page
$formparams->overflowpage = $overflowpage; // Went the user to a overflow page?
// End of: prepare params for the form.
if ($begin == 1) {
$submissionformman->next_not_empty_page(true, 0); // True means direction = right.
$nextpage = $submissionformman->get_nextpage(); // The page of the form to select subset of fields
$submissionformman->set_formpage($nextpage);
}
$formparams->formpage = $submissionformman->get_formpage(); // The page of the form to select subset of fields
// End of: prepare params for the form.
$editable = ($mode == SURVEYPRO_READONLYMODE) ? false : true;
$userform = new userform($formurl, $formparams, 'post', '', ['id' => 'userentry'], $editable);
// Begin of: manage form submission.
if ($userform->is_cancelled()) {
$localparamurl = ['s' => $cm->instance, 'mode' => $mode, 'section' => 'submissionslist'];
$redirecturl = new \moodle_url('/mod/surveypro/view.php', $localparamurl);
redirect($redirecturl, get_string('usercanceled', 'mod_surveypro'));
}
if ($submissionformman->formdata = $userform->get_data()) {
$submissionformman->save_user_data(); // SAVE SAVE SAVE SAVE.
// If "pause" button has been pressed, redirect.
$pausebutton = isset($submissionformman->formdata->pausebutton);
if ($pausebutton) {
$localparamurl = ['s' => $cm->instance, 'mode' => $mode, 'section' => 'submissionslist'];
$redirecturl = new \moodle_url('/mod/surveypro/view.php', $localparamurl);
redirect($redirecturl); // Go somewhere.
}
$paramurl['submissionid'] = $submissionformman->get_submissionid();
$paramurl['section'] = 'submissionform';
// If "previous" button has been pressed, redirect.
$prevbutton = isset($submissionformman->formdata->prevbutton);
if ($prevbutton) {
$submissionformman->next_not_empty_page(false);
$paramurl['formpage'] = $submissionformman->get_nextpage();
$paramurl['overflowpage'] = $submissionformman->get_overflowpage();
$redirecturl = new \moodle_url('/mod/surveypro/view.php', $paramurl);
redirect($redirecturl); // Redirect to the first non empty page.
}
// If "next" button has been pressed, redirect.
$nextbutton = isset($submissionformman->formdata->nextbutton);
if ($nextbutton) {
$submissionformman->next_not_empty_page(true);
$paramurl['formpage'] = $submissionformman->get_nextpage();
$paramurl['overflowpage'] = $submissionformman->get_overflowpage();
$redirecturl = new \moodle_url('/mod/surveypro/view.php', $paramurl);
redirect($redirecturl); // Redirect to the first non empty page.
}
// Surveypro has been submitted. Notify people.
$submissionformman->notifypeople();
// If none redirected you, reload THE RIGHT page WITHOUT $paramurl['mode'].
// This is necessary otherwise if the user switches language using the corresponding menu
// just after a new response is submitted
// the browser redirects to http://localhost/head_behat/mod/surveypro/view.php?s=xxx&view=1&lang=it§ion=submissionform
// and not to http://localhost/head_behat/mod/surveypro/view.php?s=xxx&lang=it§ion=collectedsubmissions
// alias it goes to the page to get one more response
// instead of remaining in the view submissions page.
$paramurl = [];
$paramurl['s'] = $surveypro->id;
// $paramurl['responsestatus'] = $submissionformman->get_responsestatus();
$paramurl['justsubmitted'] = 1 + $submissionformman->get_userdeservesthanks();
$paramurl['formview'] = $submissionformman->get_mode(); // In which way am I using this form?
$paramurl['section'] = 'submissionslist';
$redirecturl = new \moodle_url('/mod/surveypro/view.php', $paramurl);
redirect($redirecturl);
}
// End of: manage form submission.
// Set $PAGE params.
$paramurl = ['s' => $surveypro->id, 'area' => 'surveypro', 'section' => 'submissionform', 'mode' => $mode];
if (!empty($submissionid)) {
$paramurl['submissionid'] = $submissionid;
}
if (!empty($mode)) {
$paramurl['mode'] = $mode;
}
if (!empty($begin)) {
$paramurl['begin'] = $begin;
}
$url = new \moodle_url('/mod/surveypro/view.php', $paramurl);
$PAGE->set_url($url);
$PAGE->set_context($context);
$PAGE->set_cm($cm);
$PAGE->set_title($surveypro->name);
$PAGE->set_heading($course->shortname);
switch ($mode) {
case SURVEYPRO_NEWRESPONSEMODE:
$PAGE->navbar->add(get_string('surveypro_insert', 'mod_surveypro'));
break;
case SURVEYPRO_EDITMODE:
$PAGE->navbar->add(get_string('surveypro_edit', 'mod_surveypro'));
break;
case SURVEYPRO_READONLYMODE:
$PAGE->navbar->add(get_string('surveypro_readonly', 'mod_surveypro'));
break;
case SURVEYPRO_PREVIEWMODE:
$PAGE->navbar->add(get_string('layout_preview', 'mod_surveypro'));
break;
case SURVEYPRO_NOMODE:
// It should never be verified.
break;
default:
$message = 'Unexpected $mode = '.$mode;
debugging('Error at line '.__LINE__.' of '.__FILE__.'. '.$message , DEBUG_DEVELOPER);
}
// Is it useful? $PAGE->add_body_class('mediumwidth');.
$utilitypageman->manage_editbutton($edit);
// Output starts here.
echo $OUTPUT->header();
$actionbar = new \mod_surveypro\output\action_bar($cm, $context, $surveypro);
echo $actionbar->draw_view_action_bar();
$submissionformman->noitem_stopexecution();
$submissionformman->nomoresubmissions_stopexecution();
$submissionformman->warning_submission_copy();
$submissionformman->display_page_x_of_y();
// Begin of: calculate prefill for fields and prepare standard editors and filemanager.
// If sumission already exists.
$prefill = $submissionformman->get_prefill_data();
$prefill['formpage'] = $submissionformman->get_formpage();
// End of: calculate prefill for fields and prepare standard editors and filemanager.
$userform->set_data($prefill);
$userform->display();
// If surveypro is multipage and $submissionformman->tabpage == SURVEYPRO_READONLYMODE.
// I need to add navigation buttons manually
// Because the surveypro is not displayed as a form but as a simple list of graphic user items.
$submissionformman->add_readonly_browsing_buttons();
}
// MARK searchsubmissions.
if ($section == 'searchsubmissions') {
// Get additional specific params.
$formpage = optional_param('formpage', 1, PARAM_INT); // Form page number.
// Required capability.
require_capability('mod/surveypro:searchsubmissions', $context);
// Calculations.
mod_surveypro\utility_mform::register_form_elements();
$submissionsearchman = new view_submissionsearch($cm, $context, $surveypro);
// Begin of: define $searchform return url.
$paramurl = ['s' => $cm->instance, 'section' => 'searchsubmissions'];
$formurl = new \moodle_url('/mod/surveypro/view.php', $paramurl);
// End of: define $searchform return url.
// Begin of: prepare params for the search form.
$formparams = new \stdClass();
$formparams->cm = $cm;
$formparams->surveypro = $surveypro;
$formparams->canaccessreserveditems = has_capability('mod/surveypro:accessreserveditems', $context);
$searchform = new usersearch($formurl, $formparams, 'post', '', ['id' => 'usersearch']);
// End of: prepare params for the form.
// Begin of: manage form submission.
if ($searchform->is_cancelled()) {
$paramurl = ['s' => $cm->instance, 'section' => 'submissionslist'];
$returnurl = new \moodle_url('/mod/surveypro/view.php', $paramurl);
redirect($returnurl);
}
if ($submissionsearchman->formdata = $searchform->get_data()) {
// In this routine I do not execute a real search.
// I only define the param searchquery for the url.
$paramurl = ['s' => $cm->instance, 'section' => 'submissionslist'];
if ($searchquery = $submissionsearchman->get_searchparamurl()) {
$paramurl['searchquery'] = $searchquery;
}
$returnurl = new \moodle_url('/mod/surveypro/view.php', $paramurl);
redirect($returnurl);
}
// End of: manage form submission.
// Set $PAGE params.
$paramurl = ['s' => $surveypro->id, 'area' => 'surveypro', 'section' => 'searchsubmissions'];
$url = new \moodle_url('/mod/surveypro/view.php', $paramurl);
$PAGE->set_url($url);
$PAGE->set_context($context);
$PAGE->set_cm($cm);
$PAGE->set_title($surveypro->name);
$PAGE->set_heading($course->shortname);
$PAGE->navbar->add(get_string('surveypro_view_search', 'mod_surveypro'));
// Is it useful? $PAGE->add_body_class('mediumwidth');.
$utilitypageman->manage_editbutton($edit);
// Output starts here.
echo $OUTPUT->header();
$actionbar = new \mod_surveypro\output\action_bar($cm, $context, $surveypro);
echo $actionbar->draw_view_action_bar();
$searchform->display();
}
// Finish the page.
echo $OUTPUT->footer();