Merge branch 'MDL-51445_m30v1' of https://github.com/sbourget/moodle into MOODLE_30_S...
[moodle.git] / mod / feedback / complete.php
blob6ee053efd036c7a36da5769c35ef0f786c11adc5
1 <?php
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.
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/>.
17 /**
18 * prints the form so the user can fill out the feedback
20 * @author Andreas Grabs
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package mod_feedback
25 require_once("../../config.php");
26 require_once("lib.php");
27 require_once($CFG->libdir . '/completionlib.php');
29 feedback_init_feedback_session();
31 $id = required_param('id', PARAM_INT);
32 $completedid = optional_param('completedid', false, PARAM_INT);
33 $preservevalues = optional_param('preservevalues', 0, PARAM_INT);
34 $courseid = optional_param('courseid', false, PARAM_INT);
35 $gopage = optional_param('gopage', -1, PARAM_INT);
36 $lastpage = optional_param('lastpage', false, PARAM_INT);
37 $startitempos = optional_param('startitempos', 0, PARAM_INT);
38 $lastitempos = optional_param('lastitempos', 0, PARAM_INT);
39 $anonymous_response = optional_param('anonymous_response', 0, PARAM_INT); //arb
41 $highlightrequired = false;
43 if (($formdata = data_submitted()) AND !confirm_sesskey()) {
44 print_error('invalidsesskey');
47 //if the use hit enter into a textfield so the form should not submit
48 if (isset($formdata->sesskey) AND
49 !isset($formdata->savevalues) AND
50 !isset($formdata->gonextpage) AND
51 !isset($formdata->gopreviouspage)) {
53 $gopage = $formdata->lastpage;
56 if (isset($formdata->savevalues)) {
57 $savevalues = true;
58 } else {
59 $savevalues = false;
62 if ($gopage < 0 AND !$savevalues) {
63 if (isset($formdata->gonextpage)) {
64 $gopage = $lastpage + 1;
65 $gonextpage = true;
66 $gopreviouspage = false;
67 } else if (isset($formdata->gopreviouspage)) {
68 $gopage = $lastpage - 1;
69 $gonextpage = false;
70 $gopreviouspage = true;
71 } else {
72 print_error('missingparameter');
74 } else {
75 $gonextpage = $gopreviouspage = false;
78 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
79 print_error('invalidcoursemodule');
82 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
83 print_error('coursemisconf');
86 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
87 print_error('invalidcoursemodule');
90 $context = context_module::instance($cm->id);
92 $feedback_complete_cap = false;
94 if (has_capability('mod/feedback:complete', $context)) {
95 $feedback_complete_cap = true;
98 //check whether the feedback is located and! started from the mainsite
99 if ($course->id == SITEID AND !$courseid) {
100 $courseid = SITEID;
103 //check whether the feedback is mapped to the given courseid
104 if ($course->id == SITEID AND !has_capability('mod/feedback:edititems', $context)) {
105 if ($DB->get_records('feedback_sitecourse_map', array('feedbackid'=>$feedback->id))) {
106 $params = array('feedbackid'=>$feedback->id, 'courseid'=>$courseid);
107 if (!$DB->get_record('feedback_sitecourse_map', $params)) {
108 print_error('notavailable', 'feedback');
113 if ($feedback->anonymous != FEEDBACK_ANONYMOUS_YES) {
114 if ($course->id == SITEID) {
115 require_login($course, true);
116 } else {
117 require_login($course, true, $cm);
119 } else {
120 if ($course->id == SITEID) {
121 require_course_login($course, true);
122 } else {
123 require_course_login($course, true, $cm);
127 //check whether the given courseid exists
128 if ($courseid AND $courseid != SITEID) {
129 if ($course2 = $DB->get_record('course', array('id'=>$courseid))) {
130 require_course_login($course2); //this overwrites the object $course :-(
131 $course = $DB->get_record("course", array("id"=>$cm->course)); // the workaround
132 } else {
133 print_error('invalidcourseid');
137 if (!$feedback_complete_cap) {
138 print_error('error');
141 // Mark activity viewed for completion-tracking
142 $completion = new completion_info($course);
143 $completion->set_module_viewed($cm);
145 /// Print the page header
146 $strfeedbacks = get_string("modulenameplural", "feedback");
147 $strfeedback = get_string("modulename", "feedback");
149 if ($course->id == SITEID) {
150 $PAGE->set_cm($cm, $course); // set's up global $COURSE
151 $PAGE->set_pagelayout('incourse');
154 $PAGE->navbar->add(get_string('feedback:complete', 'feedback'));
155 $urlparams = array('id'=>$cm->id, 'gopage'=>$gopage, 'courseid'=>$course->id);
156 $PAGE->set_url('/mod/feedback/complete.php', $urlparams);
157 $PAGE->set_heading($course->fullname);
158 $PAGE->set_title($feedback->name);
159 echo $OUTPUT->header();
161 //ishidden check.
162 //feedback in courses
163 if ((empty($cm->visible) AND
164 !has_capability('moodle/course:viewhiddenactivities', $context)) AND
165 $course->id != SITEID) {
166 notice(get_string("activityiscurrentlyhidden"));
169 //ishidden check.
170 //feedback on mainsite
171 if ((empty($cm->visible) AND
172 !has_capability('moodle/course:viewhiddenactivities', $context)) AND
173 $courseid == SITEID) {
174 notice(get_string("activityiscurrentlyhidden"));
177 //check, if the feedback is open (timeopen, timeclose)
178 $checktime = time();
179 $feedback_is_closed = ($feedback->timeopen > $checktime) ||
180 ($feedback->timeclose < $checktime &&
181 $feedback->timeclose > 0);
183 if ($feedback_is_closed) {
184 echo $OUTPUT->heading(format_string($feedback->name));
185 echo $OUTPUT->box_start('generalbox boxaligncenter');
186 echo $OUTPUT->notification(get_string('feedback_is_not_open', 'feedback'));
187 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
188 echo $OUTPUT->box_end();
189 echo $OUTPUT->footer();
190 exit;
193 //additional check for multiple-submit (prevent browsers back-button).
194 //the main-check is in view.php
195 $feedback_can_submit = true;
196 if ($feedback->multiple_submit == 0 ) {
197 if (feedback_is_already_submitted($feedback->id, $courseid)) {
198 $feedback_can_submit = false;
201 if ($feedback_can_submit) {
202 //preserving the items
203 if ($preservevalues == 1) {
204 if (!isset($SESSION->feedback->is_started) OR !$SESSION->feedback->is_started == true) {
205 print_error('error', '', $CFG->wwwroot.'/course/view.php?id='.$course->id);
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_values($USER->id, true)) {
211 if (!$gonextpage AND !$gopreviouspage) {
212 $preservevalues = false;// It can be stored.
215 } else {
216 $savereturn = 'failed';
217 if (isset($lastpage)) {
218 $gopage = $lastpage;
219 } else {
220 print_error('missingparameter');
223 } else {
224 $savereturn = 'missing';
225 $highlightrequired = true;
226 if (isset($lastpage)) {
227 $gopage = $lastpage;
228 } else {
229 print_error('missingparameter');
235 //saving the items
236 if ($savevalues AND !$preservevalues) {
237 //exists there any pagebreak, so there are values in the feedback_valuetmp
238 $userid = $USER->id; //arb
240 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
241 $feedbackcompleted = feedback_get_current_completed($feedback->id, false, $courseid);
242 } else {
243 $feedbackcompleted = false;
245 $params = array('id' => $completedid);
246 $feedbackcompletedtmp = $DB->get_record('feedback_completedtmp', $params);
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,
254 $feedbackcompleted,
255 $userid);
256 if ($new_completed_id) {
257 $savereturn = 'saved';
258 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
259 feedback_send_email($cm, $feedback, $course, $userid);
260 } else {
261 feedback_send_email_anonym($cm, $feedback, $course, $userid);
263 //tracking the submit
264 $tracking = new stdClass();
265 $tracking->userid = $USER->id;
266 $tracking->feedback = $feedback->id;
267 $tracking->completed = $new_completed_id;
268 $DB->insert_record('feedback_tracking', $tracking);
269 unset($SESSION->feedback->is_started);
271 // Update completion state
272 $completion = new completion_info($course);
273 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
274 $completion->update_state($cm, COMPLETION_COMPLETE);
277 } else {
278 $savereturn = 'failed';
285 if ($allbreaks = feedback_get_all_break_positions($feedback->id)) {
286 if ($gopage <= 0) {
287 $startposition = 0;
288 } else {
289 if (!isset($allbreaks[$gopage - 1])) {
290 $gopage = count($allbreaks);
292 $startposition = $allbreaks[$gopage - 1];
294 $ispagebreak = true;
295 } else {
296 $startposition = 0;
297 $newpage = 0;
298 $ispagebreak = false;
301 //get the feedbackitems after the last shown pagebreak
302 $select = 'feedback = ? AND position > ?';
303 $params = array($feedback->id, $startposition);
304 $feedbackitems = $DB->get_records_select('feedback_item', $select, $params, 'position');
306 //get the first pagebreak
307 $params = array('feedback' => $feedback->id, 'typ' => 'pagebreak');
308 if ($pagebreaks = $DB->get_records('feedback_item', $params, 'position')) {
309 $pagebreaks = array_values($pagebreaks);
310 $firstpagebreak = $pagebreaks[0];
311 } else {
312 $firstpagebreak = false;
314 $maxitemcount = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
316 //get the values of completeds before done. Anonymous user can not get these values.
317 if ((!isset($SESSION->feedback->is_started)) AND
318 (!isset($savereturn)) AND
319 ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO)) {
321 $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid);
322 if (!$feedbackcompletedtmp) {
323 $feedbackcompleted = feedback_get_current_completed($feedback->id, false, $courseid);
324 if ($feedbackcompleted) {
325 //copy the values to feedback_valuetmp create a completedtmp
326 $feedbackcompletedtmp = feedback_set_tmp_values($feedbackcompleted);
329 } else {
330 $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid);
333 /// Print the main part of the page
334 ///////////////////////////////////////////////////////////////////////////
335 ///////////////////////////////////////////////////////////////////////////
336 ///////////////////////////////////////////////////////////////////////////
337 $analysisurl = new moodle_url('/mod/feedback/analysis.php', array('id'=>$id));
338 if ($courseid > 0) {
339 $analysisurl->param('courseid', $courseid);
341 echo $OUTPUT->heading(format_string($feedback->name));
343 if ( (intval($feedback->publish_stats) == 1) AND
344 ( has_capability('mod/feedback:viewanalysepage', $context)) AND
345 !( has_capability('mod/feedback:viewreports', $context)) ) {
347 $params = array('userid' => $USER->id, 'feedback' => $feedback->id);
348 if ($multiple_count = $DB->count_records('feedback_tracking', $params)) {
349 echo $OUTPUT->box_start('mdl-align');
350 echo '<a href="'.$analysisurl->out().'">';
351 echo get_string('completed_feedbacks', 'feedback').'</a>';
352 echo $OUTPUT->box_end();
356 if (isset($savereturn) && $savereturn == 'saved') {
357 if ($feedback->page_after_submit) {
359 require_once($CFG->libdir . '/filelib.php');
361 $page_after_submit_output = file_rewrite_pluginfile_urls($feedback->page_after_submit,
362 'pluginfile.php',
363 $context->id,
364 'mod_feedback',
365 'page_after_submit',
368 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
369 echo format_text($page_after_submit_output,
370 $feedback->page_after_submitformat,
371 array('overflowdiv' => true));
372 echo $OUTPUT->box_end();
373 } else {
374 echo '<p align="center">';
375 echo '<b><font color="green">';
376 echo get_string('entries_saved', 'feedback');
377 echo '</font></b>';
378 echo '</p>';
379 if ( intval($feedback->publish_stats) == 1) {
380 echo '<p align="center"><a href="'.$analysisurl->out().'">';
381 echo get_string('completed_feedbacks', 'feedback').'</a>';
382 echo '</p>';
386 if ($feedback->site_after_submit) {
387 $url = feedback_encode_target_url($feedback->site_after_submit);
388 } else {
389 if ($courseid) {
390 if ($courseid == SITEID) {
391 $url = $CFG->wwwroot;
392 } else {
393 $url = $CFG->wwwroot.'/course/view.php?id='.$courseid;
395 } else {
396 if ($course->id == SITEID) {
397 $url = $CFG->wwwroot;
398 } else {
399 $url = $CFG->wwwroot.'/course/view.php?id='.$course->id;
403 echo $OUTPUT->continue_button($url);
404 } else {
405 if (isset($savereturn) && $savereturn == 'failed') {
406 echo $OUTPUT->box_start('mform');
407 echo '<span class="error">'.get_string('saving_failed', 'feedback').'</span>';
408 echo $OUTPUT->box_end();
411 if (isset($savereturn) && $savereturn == 'missing') {
412 echo $OUTPUT->box_start('mform');
413 echo '<span class="error">'.get_string('saving_failed_because_missing_or_false_values', 'feedback').'</span>';
414 echo $OUTPUT->box_end();
417 //print the items
418 if (is_array($feedbackitems)) {
419 echo $OUTPUT->box_start('feedback_form');
420 echo '<form action="complete.php" class="mform" method="post" onsubmit=" ">';
421 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
422 echo $OUTPUT->box_start('feedback_anonymousinfo');
423 switch ($feedback->anonymous) {
424 case FEEDBACK_ANONYMOUS_YES:
425 echo '<input type="hidden" name="anonymous" value="1" />';
426 $inputvalue = 'value="'.FEEDBACK_ANONYMOUS_YES.'"';
427 echo '<input type="hidden" name="anonymous_response" '.$inputvalue.' />';
428 echo get_string('mode', 'feedback').': '.get_string('anonymous', 'feedback');
429 break;
430 case FEEDBACK_ANONYMOUS_NO:
431 echo '<input type="hidden" name="anonymous" value="0" />';
432 $inputvalue = 'value="'.FEEDBACK_ANONYMOUS_NO.'"';
433 echo '<input type="hidden" name="anonymous_response" '.$inputvalue.' />';
434 echo get_string('mode', 'feedback').': ';
435 echo get_string('non_anonymous', 'feedback');
436 break;
438 echo $OUTPUT->box_end();
439 //check, if there exists required-elements
440 $params = array('feedback' => $feedback->id, 'required' => 1);
441 $countreq = $DB->count_records('feedback_item', $params);
442 if ($countreq > 0) {
443 echo '<span class="fdescription required">';
444 echo get_string('somefieldsrequired', 'form', '<img alt="'.get_string('requiredelement', 'form').
445 '" src="'.$OUTPUT->pix_url('req') .'" class="req" />');
446 echo '</span>';
448 echo $OUTPUT->box_start('feedback_items');
450 unset($startitem);
451 $select = 'feedback = ? AND hasvalue = 1 AND position < ?';
452 $params = array($feedback->id, $startposition);
453 $itemnr = $DB->count_records_select('feedback_item', $select, $params);
454 $lastbreakposition = 0;
455 $align = right_to_left() ? 'right' : 'left';
457 foreach ($feedbackitems as $feedbackitem) {
458 if (!isset($startitem)) {
459 //avoid showing double pagebreaks
460 if ($feedbackitem->typ == 'pagebreak') {
461 continue;
463 $startitem = $feedbackitem;
466 if ($feedbackitem->dependitem > 0) {
467 //chech if the conditions are ok
468 $fb_compare_value = feedback_compare_item_value($feedbackcompletedtmp->id,
469 $feedbackitem->dependitem,
470 $feedbackitem->dependvalue,
471 true);
472 if (!isset($feedbackcompletedtmp->id) OR !$fb_compare_value) {
473 $lastitem = $feedbackitem;
474 $lastbreakposition = $feedbackitem->position;
475 continue;
479 if ($feedbackitem->dependitem > 0) {
480 $dependstyle = ' feedback_complete_depend';
481 } else {
482 $dependstyle = '';
485 echo $OUTPUT->box_start('feedback_item_box_'.$align.$dependstyle);
486 $value = '';
487 //get the value
488 $frmvaluename = $feedbackitem->typ . '_'. $feedbackitem->id;
489 if (isset($savereturn)) {
490 $value = isset($formdata->{$frmvaluename}) ? $formdata->{$frmvaluename} : null;
491 $value = feedback_clean_input_value($feedbackitem, $value);
492 } else {
493 if (isset($feedbackcompletedtmp->id)) {
494 $value = feedback_get_item_value($feedbackcompletedtmp->id,
495 $feedbackitem->id,
496 true);
499 if ($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
500 $itemnr++;
501 echo $OUTPUT->box_start('feedback_item_number_'.$align);
502 echo $itemnr;
503 echo $OUTPUT->box_end();
505 if ($feedbackitem->typ != 'pagebreak') {
506 echo $OUTPUT->box_start('box generalbox boxalign_'.$align);
507 feedback_print_item_complete($feedbackitem, $value, $highlightrequired);
508 echo $OUTPUT->box_end();
511 echo $OUTPUT->box_end();
513 $lastbreakposition = $feedbackitem->position; //last item-pos (item or pagebreak)
514 if ($feedbackitem->typ == 'pagebreak') {
515 break;
516 } else {
517 $lastitem = $feedbackitem;
520 echo $OUTPUT->box_end();
521 echo '<input type="hidden" name="id" value="'.$id.'" />';
522 echo '<input type="hidden" name="feedbackid" value="'.$feedback->id.'" />';
523 echo '<input type="hidden" name="lastpage" value="'.$gopage.'" />';
524 if (isset($feedbackcompletedtmp->id)) {
525 $inputvalue = 'value="'.$feedbackcompletedtmp->id.'"';
526 } else {
527 $inputvalue = 'value=""';
529 echo '<input type="hidden" name="completedid" '.$inputvalue.' />';
530 echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
531 echo '<input type="hidden" name="preservevalues" value="1" />';
532 if (isset($startitem)) {
533 echo '<input type="hidden" name="startitempos" value="'.$startitem->position.'" />';
534 echo '<input type="hidden" name="lastitempos" value="'.$lastitem->position.'" />';
537 if ( $ispagebreak AND $lastbreakposition > $firstpagebreak->position) {
538 $inputvalue = 'value="'.get_string('previous_page', 'feedback').'"';
539 echo '<input name="gopreviouspage" type="submit" '.$inputvalue.' />';
541 if ($lastbreakposition < $maxitemcount) {
542 $inputvalue = 'value="'.get_string('next_page', 'feedback').'"';
543 echo '<input name="gonextpage" type="submit" '.$inputvalue.' />';
545 if ($lastbreakposition >= $maxitemcount) { //last page
546 $inputvalue = 'value="'.get_string('save_entries', 'feedback').'"';
547 echo '<input name="savevalues" type="submit" '.$inputvalue.' />';
550 echo '</form>';
551 echo $OUTPUT->box_end();
553 echo $OUTPUT->box_start('feedback_complete_cancel');
554 if ($courseid) {
555 $action = 'action="'.$CFG->wwwroot.'/course/view.php?id='.$courseid.'"';
556 } else {
557 if ($course->id == SITEID) {
558 $action = 'action="'.$CFG->wwwroot.'"';
559 } else {
560 $action = 'action="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'"';
563 echo '<form '.$action.' method="post" onsubmit=" ">';
564 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
565 echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
566 echo '<button type="submit">'.get_string('cancel').'</button>';
567 echo '</form>';
568 echo $OUTPUT->box_end();
569 $SESSION->feedback->is_started = true;
572 } else {
573 echo $OUTPUT->heading(format_string($feedback->name));
574 echo $OUTPUT->box_start('generalbox boxaligncenter');
575 echo $OUTPUT->notification(get_string('this_feedback_is_already_submitted', 'feedback'));
576 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
577 echo $OUTPUT->box_end();
579 /// Finish the page
580 ///////////////////////////////////////////////////////////////////////////
581 ///////////////////////////////////////////////////////////////////////////
582 ///////////////////////////////////////////////////////////////////////////
584 echo $OUTPUT->footer();