MDL-43656 mod_feedback: availablity date logic incorrect
[moodle.git] / mod / feedback / complete_guest.php
CommitLineData
84973212 1<?php
9c5bc7a5
AG
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * prints the form so an anonymous user can fill out the feedback on the mainsite
19 *
20 * @author Andreas Grabs
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9714baa1 22 * @package mod_feedback
9c5bc7a5
AG
23 */
24
25require_once("../../config.php");
26require_once("lib.php");
27
28feedback_init_feedback_session();
29
30$id = required_param('id', PARAM_INT);
31$completedid = optional_param('completedid', false, PARAM_INT);
32$preservevalues = optional_param('preservevalues', 0, PARAM_INT);
33$courseid = optional_param('courseid', false, PARAM_INT);
34$gopage = optional_param('gopage', -1, PARAM_INT);
35$lastpage = optional_param('lastpage', false, PARAM_INT);
36$startitempos = optional_param('startitempos', 0, PARAM_INT);
37$lastitempos = optional_param('lastitempos', 0, PARAM_INT);
38
39$url = new moodle_url('/mod/feedback/complete_guest.php', array('id'=>$id));
40if ($completedid !== false) {
41 $url->param('completedid', $completedid);
42}
43if ($preservevalues !== 0) {
44 $url->param('preservevalues', $preservevalues);
45}
46if ($courseid !== false) {
47 $url->param('courseid', $courseid);
48}
49if ($gopage !== -1) {
50 $url->param('gopage', $gopage);
51}
52if ($lastpage !== false) {
53 $url->param('lastpage', $lastpage);
54}
55if ($startitempos !== 0) {
56 $url->param('startitempos', $startitempos);
57}
58if ($lastitempos !== 0) {
59 $url->param('lastitempos', $lastitempos);
60}
61$PAGE->set_url($url);
62
63$highlightrequired = false;
64
65if (($formdata = data_submitted()) AND !confirm_sesskey()) {
66 print_error('invalidsesskey');
67}
68
69//if the use hit enter into a textfield so the form should not submit
70if (isset($formdata->sesskey) AND
71 !isset($formdata->savevalues) AND
72 !isset($formdata->gonextpage) AND
73 !isset($formdata->gopreviouspage)) {
74
e7fc2ff4 75 $gopage = (int) $formdata->lastpage;
9c5bc7a5
AG
76}
77if (isset($formdata->savevalues)) {
78 $savevalues = true;
79} else {
80 $savevalues = false;
81}
82
83if ($gopage < 0 AND !$savevalues) {
84 if (isset($formdata->gonextpage)) {
85 $gopage = $lastpage + 1;
86 $gonextpage = true;
87 $gopreviouspage = false;
88 } else if (isset($formdata->gopreviouspage)) {
89 $gopage = $lastpage - 1;
90 $gonextpage = false;
91 $gopreviouspage = true;
92 } else {
93 print_error('parameters_missing', 'feedback');
73043833 94 }
9c5bc7a5
AG
95} else {
96 $gonextpage = $gopreviouspage = false;
97}
98
99if (! $cm = get_coursemodule_from_id('feedback', $id)) {
100 print_error('invalidcoursemodule');
101}
102
103if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
104 print_error('coursemisconf');
105}
106
107if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
108 print_error('invalidcoursemodule');
109}
110
2c979976 111$context = context_module::instance($cm->id);
9c5bc7a5
AG
112
113$feedback_complete_cap = false;
114
115if (isset($CFG->feedback_allowfullanonymous)
116 AND $CFG->feedback_allowfullanonymous
117 AND $course->id == SITEID
118 AND (!$courseid OR $courseid == SITEID)
119 AND $feedback->anonymous == FEEDBACK_ANONYMOUS_YES ) {
120 $feedback_complete_cap = true;
121}
122
123//check whether the feedback is anonymous
124if (isset($CFG->feedback_allowfullanonymous)
73043833 125 AND $CFG->feedback_allowfullanonymous
9c5bc7a5
AG
126 AND $feedback->anonymous == FEEDBACK_ANONYMOUS_YES
127 AND $course->id == SITEID ) {
128 $feedback_complete_cap = true;
129}
130if ($feedback->anonymous != FEEDBACK_ANONYMOUS_YES) {
131 print_error('feedback_is_not_for_anonymous', 'feedback');
132}
133
134//check whether the user has a session
135// there used to be a sesskey test - this could not work - sorry
136
137//check whether the feedback is located and! started from the mainsite
138if ($course->id == SITEID AND !$courseid) {
139 $courseid = SITEID;
140}
141
142require_course_login($course);
143
144if ($courseid AND $courseid != SITEID) {
145 $course2 = $DB->get_record('course', array('id'=>$courseid));
146 require_course_login($course2); //this overwrites the object $course :-(
147 $course = $DB->get_record("course", array("id"=>$cm->course)); // the workaround
148}
149
150if (!$feedback_complete_cap) {
151 print_error('error');
152}
153
154
155/// Print the page header
156$strfeedbacks = get_string("modulenameplural", "feedback");
157$strfeedback = get_string("modulename", "feedback");
158
159$PAGE->set_cm($cm, $course); // set's up global $COURSE
160$PAGE->set_pagelayout('incourse');
161
162$urlparams = array('id'=>$course->id);
163$PAGE->navbar->add($strfeedbacks, new moodle_url('/mod/feedback/index.php', $urlparams));
164$PAGE->navbar->add(format_string($feedback->name));
8c6ca696
RW
165$PAGE->set_heading($course->fullname);
166$PAGE->set_title($feedback->name);
9c5bc7a5
AG
167echo $OUTPUT->header();
168
169//ishidden check.
170//hidden feedbacks except feedbacks on mainsite are only accessible with related capabilities
171if ((empty($cm->visible) AND
172 !has_capability('moodle/course:viewhiddenactivities', $context)) AND
173 $course->id != SITEID) {
174 notice(get_string("activityiscurrentlyhidden"));
175}
176
177//check, if the feedback is open (timeopen, timeclose)
178$checktime = time();
179
1298acc7
TM
180$feedback_is_closed = ($feedback->timeopen > $checktime) ||
181 ($feedback->timeclose < $checktime &&
9c5bc7a5
AG
182 $feedback->timeclose > 0);
183
184if ($feedback_is_closed) {
185 echo $OUTPUT->box_start('generalbox boxaligncenter');
846cebe5
RW
186 echo $OUTPUT->notification(get_string('feedback_is_not_open', 'feedback'));
187 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
9c5bc7a5
AG
188 echo $OUTPUT->box_end();
189 echo $OUTPUT->footer();
190 exit;
191}
192
193//additional check for multiple-submit (prevent browsers back-button).
194//the main-check is in view.php
195$feedback_can_submit = true;
196if ($feedback->multiple_submit == 0 ) {
197 if (feedback_is_already_submitted($feedback->id, $courseid)) {
198 $feedback_can_submit = false;
84973212 199 }
9c5bc7a5
AG
200}
201if ($feedback_can_submit) {
202 //preserving the items
203 if ($preservevalues == 1) {
204 if (!$SESSION->feedback->is_started == true) {
205 print_error('error', 'error', $CFG->wwwroot.'/course/view.php?id='.$course->id);
84973212 206 }
9c5bc7a5
AG
207 //check, if all required items have a value
208 if (feedback_check_values($startitempos, $lastitempos)) {
209 $userid = $USER->id; //arb
210 if ($completedid = feedback_save_guest_values(sesskey())) {
9c5bc7a5
AG
211 //now it can be saved
212 if (!$gonextpage AND !$gopreviouspage) {
213 $preservevalues = false;
84973212 214 }
9c5bc7a5
AG
215
216 } else {
217 $savereturn = 'failed';
218 if (isset($lastpage)) {
73043833 219 $gopage = $lastpage;
9c5bc7a5 220 } else {
73043833 221 print_error('parameters_missing', 'feedback');
c70ad9f7 222 }
223 }
9c5bc7a5
AG
224 } else {
225 $savereturn = 'missing';
226 $highlightrequired = true;
227 if (isset($lastpage)) {
228 $gopage = $lastpage;
229 } else {
230 print_error('parameters_missing', 'feedback');
231 }
84973212 232 }
9c5bc7a5 233 }
73043833 234
9c5bc7a5
AG
235 //saving the items
236 if ($savevalues AND !$preservevalues) {
237 //exists there any pagebreak, so there are values in the feedback_valuetmp
238 //arb changed from 0 to $USER->id
239 //no strict anonymous feedbacks
240 //if it is a guest taking it then I want to know that it was
241 //a guest (at least in the data saved in the feedback tables)
242 $userid = $USER->id;
243
244 $params = array('id'=>$completedid);
245 $feedbackcompletedtmp = $DB->get_record('feedback_completedtmp', $params);
246
247 //fake saving for switchrole
248 $is_switchrole = feedback_check_is_switchrole();
249 if ($is_switchrole) {
250 $savereturn = 'saved';
251 feedback_delete_completedtmp($completedid);
252 } else {
253 $new_completed_id = feedback_save_tmp_values($feedbackcompletedtmp, false, $userid);
254 if ($new_completed_id) {
73043833
AG
255 $savereturn = 'saved';
256 feedback_send_email_anonym($cm, $feedback, $course, $userid);
257 unset($SESSION->feedback->is_started);
258
9c5bc7a5 259 } else {
73043833
AG
260 $savereturn = 'failed';
261 }
262 }
9c5bc7a5 263 }
73043833 264
9c5bc7a5
AG
265 if ($allbreaks = feedback_get_all_break_positions($feedback->id)) {
266 if ($gopage <= 0) {
73043833 267 $startposition = 0;
9c5bc7a5
AG
268 } else {
269 if (!isset($allbreaks[$gopage - 1])) {
270 $gopage = count($allbreaks);
271 }
272 $startposition = $allbreaks[$gopage - 1];
73043833 273 }
9c5bc7a5
AG
274 $ispagebreak = true;
275 } else {
276 $startposition = 0;
277 $newpage = 0;
278 $ispagebreak = false;
279 }
280
281 //get the feedbackitems after the last shown pagebreak
282 $select = 'feedback = ? AND position > ?';
283 $params = array($feedback->id, $startposition);
284 $feedbackitems = $DB->get_records_select('feedback_item', $select, $params, 'position');
285
286 //get the first pagebreak
287 $params = array('feedback'=>$feedback->id, 'typ'=>'pagebreak');
288 if ($pagebreaks = $DB->get_records('feedback_item', $params, 'position')) {
289 $pagebreaks = array_values($pagebreaks);
290 $firstpagebreak = $pagebreaks[0];
291 } else {
292 $firstpagebreak = false;
293 }
294 $maxitemcount = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
295 $feedbackcompletedtmp = feedback_get_current_completed($feedback->id,
296 true,
297 $courseid,
298 sesskey());
73043833 299
9c5bc7a5
AG
300 /// Print the main part of the page
301 ///////////////////////////////////////////////////////////////////////////
302 ///////////////////////////////////////////////////////////////////////////
303 ///////////////////////////////////////////////////////////////////////////
304 $analysisurl = new moodle_url('/mod/feedback/analysis.php', array('id'=>$id));
305 if ($courseid > 0) {
306 $analysisurl->param('courseid', $courseid);
307 }
308 echo $OUTPUT->heading(format_text($feedback->name));
309
310 if ( (intval($feedback->publish_stats) == 1) AND
311 ( has_capability('mod/feedback:viewanalysepage', $context)) AND
312 !( has_capability('mod/feedback:viewreports', $context)) ) {
313 echo $OUTPUT->box_start('mdl-align');
314 echo '<a href="'.$analysisurl->out().'">';
315 echo get_string('completed_feedbacks', 'feedback');
316 echo '</a>';
317 echo $OUTPUT->box_end();
318 }
73043833 319
9c5bc7a5
AG
320 if (isset($savereturn) && $savereturn == 'saved') {
321 if ($feedback->page_after_submit) {
abd2899c
AG
322 require_once($CFG->libdir . '/filelib.php');
323
324 $page_after_submit_output = file_rewrite_pluginfile_urls($feedback->page_after_submit,
325 'pluginfile.php',
326 $context->id,
327 'mod_feedback',
328 'page_after_submit',
329 0);
330
9c5bc7a5 331 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
abd2899c 332 echo format_text($page_after_submit_output,
9c5bc7a5
AG
333 $feedback->page_after_submitformat,
334 array('overflowdiv' => true));
335 echo $OUTPUT->box_end();
336 } else {
337 echo '<p align="center"><b><font color="green">';
338 echo get_string('entries_saved', 'feedback');
339 echo '</font></b></p>';
340 if ( intval($feedback->publish_stats) == 1) {
341 echo '<p align="center"><a href="'.$analysisurl->out().'">';
342 echo get_string('completed_feedbacks', 'feedback').'</a>';
343 echo '</p>';
344 }
345 }
346 if ($feedback->site_after_submit) {
347 $url = feedback_encode_target_url($feedback->site_after_submit);
348 } else {
349 if ($courseid) {
350 if ($courseid == SITEID) {
351 $url = $CFG->wwwroot;
352 } else {
353 $url = $CFG->wwwroot.'/course/view.php?id='.$courseid;
354 }
355 } else {
356 if ($course->id == SITEID) {
357 $url = $CFG->wwwroot;
358 } else {
359 $url = $CFG->wwwroot.'/course/view.php?id='.$course->id;
360 }
361 }
73043833 362 }
9c5bc7a5
AG
363 echo $OUTPUT->continue_button($url);
364 } else {
365 if (isset($savereturn) && $savereturn == 'failed') {
366 echo $OUTPUT->box_start('mform error');
367 echo get_string('saving_failed', 'feedback');
368 echo $OUTPUT->box_end();
84973212 369 }
d4b1d58c 370
9c5bc7a5
AG
371 if (isset($savereturn) && $savereturn == 'missing') {
372 echo $OUTPUT->box_start('mform error');
373 echo get_string('saving_failed_because_missing_or_false_values', 'feedback');
31c0fa53 374 echo $OUTPUT->box_end();
84973212 375 }
376
9c5bc7a5
AG
377 //print the items
378 if (is_array($feedbackitems)) {
379 echo $OUTPUT->box_start('feedback_form');
380 echo '<form action="complete_guest.php" method="post" onsubmit=" ">';
9c5bc7a5
AG
381 echo '<input type="hidden" name="anonymous" value="0" />';
382 $inputvalue = 'value="'.FEEDBACK_ANONYMOUS_YES.'"';
383 echo '<input type="hidden" name="anonymous_response" '.$inputvalue.' />';
384 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
385 //check, if there exists required-elements
386 $params = array('feedback'=>$feedback->id, 'required'=>1);
387 $countreq = $DB->count_records('feedback_item', $params);
388 if ($countreq > 0) {
389 echo '<span class="feedback_required_mark">(*)';
390 echo get_string('items_are_required', 'feedback');
391 echo '</span>';
c70ad9f7 392 }
9c5bc7a5
AG
393 echo $OUTPUT->box_start('feedback_items');
394
395 $startitem = null;
396 $select = 'feedback = ? AND hasvalue = 1 AND position < ?';
397 $params = array($feedback->id, $startposition);
398 $itemnr = $DB->count_records_select('feedback_item', $select, $params);
399 $lastbreakposition = 0;
400 $align = right_to_left() ? 'right' : 'left';
401
402 foreach ($feedbackitems as $feedbackitem) {
403 if (!isset($startitem)) {
404 //avoid showing double pagebreaks
405 if ($feedbackitem->typ == 'pagebreak') {
406 continue;
31c0fa53 407 }
9c5bc7a5 408 $startitem = $feedbackitem;
c70ad9f7 409 }
73043833 410
9c5bc7a5
AG
411 if ($feedbackitem->dependitem > 0) {
412 //chech if the conditions are ok
413 $fb_compare_value = feedback_compare_item_value($feedbackcompletedtmp->id,
414 $feedbackitem->dependitem,
415 $feedbackitem->dependvalue,
416 true);
417 if (!isset($feedbackcompletedtmp->id) OR !$fb_compare_value) {
418 $lastitem = $feedbackitem;
419 $lastbreakposition = $feedbackitem->position;
420 continue;
421 }
422 }
73043833 423
9c5bc7a5
AG
424 if ($feedbackitem->dependitem > 0) {
425 $dependstyle = ' feedback_complete_depend';
426 } else {
427 $dependstyle = '';
73043833 428 }
91c49d92 429
9c5bc7a5
AG
430 echo $OUTPUT->box_start('feedback_item_box_'.$align.$dependstyle);
431 $value = '';
432 //get the value
e7fc2ff4 433 $frmvaluename = $feedbackitem->typ . '_'. $feedbackitem->id;
9c5bc7a5 434 if (isset($savereturn)) {
e7fc2ff4
AG
435 $value = isset($formdata->{$frmvaluename}) ? $formdata->{$frmvaluename} : null;
436 $value = feedback_clean_input_value($feedbackitem, $value);
9c5bc7a5
AG
437 } else {
438 if (isset($feedbackcompletedtmp->id)) {
439 $value = feedback_get_item_value($feedbackcompletedtmp->id,
440 $feedbackitem->id,
441 sesskey());
31c0fa53 442 }
9c5bc7a5
AG
443 }
444 if ($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
445 $itemnr++;
446 echo $OUTPUT->box_start('feedback_item_number_'.$align);
447 echo $itemnr;
73043833 448 echo $OUTPUT->box_end();
73043833 449 }
9c5bc7a5
AG
450 if ($feedbackitem->typ != 'pagebreak') {
451 echo $OUTPUT->box_start('box generalbox boxalign_'.$align);
452 feedback_print_item_complete($feedbackitem, $value, $highlightrequired);
453 echo $OUTPUT->box_end();
c70ad9f7 454 }
e7fc2ff4 455
9c5bc7a5 456 echo $OUTPUT->box_end();
84973212 457
9c5bc7a5
AG
458 $lastbreakposition = $feedbackitem->position; //last item-pos (item or pagebreak)
459 if ($feedbackitem->typ == 'pagebreak') {
460 break;
461 } else {
462 $lastitem = $feedbackitem;
73043833 463 }
9c5bc7a5
AG
464 }
465 echo $OUTPUT->box_end();
466 echo '<input type="hidden" name="id" value="'.$id.'" />';
467 echo '<input type="hidden" name="feedbackid" value="'.$feedback->id.'" />';
468 echo '<input type="hidden" name="lastpage" value="'.$gopage.'" />';
469 if (isset($feedbackcompletedtmp->id)) {
470 $inputvalue = 'value="'.$feedbackcompletedtmp->id;
471 } else {
472 $inputvalue = 'value=""';
473 }
474 echo '<input type="hidden" name="completedid" '.$inputvalue.' />';
475 echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
476 echo '<input type="hidden" name="preservevalues" value="1" />';
477 if (isset($startitem)) {
478 echo '<input type="hidden" name="startitempos" value="'.$startitem->position.'" />';
479 echo '<input type="hidden" name="lastitempos" value="'.$lastitem->position.'" />';
480 }
d4b1d58c 481
9c5bc7a5
AG
482 if ($ispagebreak AND $lastbreakposition > $firstpagebreak->position) {
483 $inputvalue = 'value="'.get_string('previous_page', 'feedback').'"';
484 echo '<input name="gopreviouspage" type="submit" '.$inputvalue.' />';
485 }
486 if ($lastbreakposition < $maxitemcount) {
487 $inputvalue = 'value="'.get_string('next_page', 'feedback').'"';
488 echo '<input name="gonextpage" type="submit" '.$inputvalue.' />';
489 }
490 if ($lastbreakposition >= $maxitemcount) { //last page
491 $inputvalue = 'value="'.get_string('save_entries', 'feedback').'"';
492 echo '<input name="savevalues" type="submit" '.$inputvalue.' />';
493 }
d4b1d58c 494
9c5bc7a5
AG
495 echo '</form>';
496 echo $OUTPUT->box_end();
497
498 echo $OUTPUT->box_start('feedback_complete_cancel');
499 if ($courseid) {
500 $action = 'action="'.$CFG->wwwroot.'/course/view.php?id='.$courseid.'"';
501 } else {
502 if ($course->id == SITEID) {
503 $action = 'action="'.$CFG->wwwroot.'"';
504 } else {
505 $action = 'action="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'"';
c70ad9f7 506 }
c70ad9f7 507 }
9c5bc7a5 508 echo '<form '.$action.' method="post" onsubmit=" ">';
9c5bc7a5
AG
509 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
510 echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
511 echo '<button type="submit">'.get_string('cancel').'</button>';
9c5bc7a5
AG
512 echo '</form>';
513 echo $OUTPUT->box_end();
514 $SESSION->feedback->is_started = true;
c70ad9f7 515 }
c70ad9f7 516 }
9c5bc7a5
AG
517} else {
518 echo $OUTPUT->box_start('generalbox boxaligncenter');
846cebe5
RW
519 echo $OUTPUT->notification(get_string('this_feedback_is_already_submitted', 'feedback'));
520 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
9c5bc7a5
AG
521 echo $OUTPUT->box_end();
522}
523/// Finish the page
524///////////////////////////////////////////////////////////////////////////
525///////////////////////////////////////////////////////////////////////////
526///////////////////////////////////////////////////////////////////////////
527
528echo $OUTPUT->footer();
84973212 529