Merge branch 'MDL-41619_master' of https://github.com/markn86/moodle
[moodle.git] / mod / feedback / complete.php
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.
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/>.
17 /**
18  * prints the form so the user can fill out the feedback
19  *
20  * @author Andreas Grabs
21  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22  * @package feedback
23  */
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');
45 }
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;
54 }
56 if (isset($formdata->savevalues)) {
57     $savevalues = true;
58 } else {
59     $savevalues = false;
60 }
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');
73     }
74 } else {
75     $gonextpage = $gopreviouspage = false;
76 }
78 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
79     print_error('invalidcoursemodule');
80 }
82 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
83     print_error('coursemisconf');
84 }
86 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
87     print_error('invalidcoursemodule');
88 }
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;
96 }
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');
109         }
110     }
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);
118     }
119 } else {
120     if ($course->id == SITEID) {
121         require_course_login($course, true);
122     } else {
123         require_course_login($course, true, $cm);
124     }
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');
134     }
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(format_string($course->fullname));
158 $PAGE->set_title(format_string($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) OR
180                       ($feedback->timeclose < $checktime AND
181                             $feedback->timeclose > 0);
183 if ($feedback_is_closed) {
184     echo $OUTPUT->box_start('generalbox boxaligncenter');
185         echo '<h2><font color="red">';
186         echo get_string('feedback_is_not_open', 'feedback');
187         echo '</font></h2>';
188         echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
189     echo $OUTPUT->box_end();
190     echo $OUTPUT->footer();
191     exit;
194 //additional check for multiple-submit (prevent browsers back-button).
195 //the main-check is in view.php
196 $feedback_can_submit = true;
197 if ($feedback->multiple_submit == 0 ) {
198     if (feedback_is_already_submitted($feedback->id, $courseid)) {
199         $feedback_can_submit = false;
200     }
202 if ($feedback_can_submit) {
203     //preserving the items
204     if ($preservevalues == 1) {
205         if (!isset($SESSION->feedback->is_started) OR !$SESSION->feedback->is_started == true) {
206             print_error('error', '', $CFG->wwwroot.'/course/view.php?id='.$course->id);
207         }
208         //checken, ob alle required items einen wert haben
209         if (feedback_check_values($startitempos, $lastitempos)) {
210             $userid = $USER->id; //arb
211             if ($completedid = feedback_save_values($USER->id, true)) {
212                 if ($userid > 0) {
213                     add_to_log($course->id,
214                                'feedback',
215                                'startcomplete',
216                                'view.php?id='.$cm->id,
217                                $feedback->id,
218                                $cm->id,
219                                $userid);
220                 }
221                 if (!$gonextpage AND !$gopreviouspage) {
222                     $preservevalues = false;//es kann gespeichert werden
223                 }
225             } else {
226                 $savereturn = 'failed';
227                 if (isset($lastpage)) {
228                     $gopage = $lastpage;
229                 } else {
230                     print_error('missingparameter');
231                 }
232             }
233         } else {
234             $savereturn = 'missing';
235             $highlightrequired = true;
236             if (isset($lastpage)) {
237                 $gopage = $lastpage;
238             } else {
239                 print_error('missingparameter');
240             }
242         }
243     }
245     //saving the items
246     if ($savevalues AND !$preservevalues) {
247         //exists there any pagebreak, so there are values in the feedback_valuetmp
248         $userid = $USER->id; //arb
250         if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
251             $feedbackcompleted = feedback_get_current_completed($feedback->id, false, $courseid);
252         } else {
253             $feedbackcompleted = false;
254         }
255         $params = array('id' => $completedid);
256         $feedbackcompletedtmp = $DB->get_record('feedback_completedtmp', $params);
257         //fake saving for switchrole
258         $is_switchrole = feedback_check_is_switchrole();
259         if ($is_switchrole) {
260             $savereturn = 'saved';
261             feedback_delete_completedtmp($completedid);
262         } else {
263             $new_completed_id = feedback_save_tmp_values($feedbackcompletedtmp,
264                                                          $feedbackcompleted,
265                                                          $userid);
266             if ($new_completed_id) {
267                 $savereturn = 'saved';
268                 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
269                     add_to_log($course->id,
270                               'feedback',
271                               'submit',
272                               'view.php?id='.$cm->id,
273                               $feedback->id,
274                               $cm->id,
275                               $userid);
277                     feedback_send_email($cm, $feedback, $course, $userid);
278                 } else {
279                     feedback_send_email_anonym($cm, $feedback, $course, $userid);
280                 }
281                 //tracking the submit
282                 $tracking = new stdClass();
283                 $tracking->userid = $USER->id;
284                 $tracking->feedback = $feedback->id;
285                 $tracking->completed = $new_completed_id;
286                 $DB->insert_record('feedback_tracking', $tracking);
287                 unset($SESSION->feedback->is_started);
289                 // Update completion state
290                 $completion = new completion_info($course);
291                 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
292                     $completion->update_state($cm, COMPLETION_COMPLETE);
293                 }
295             } else {
296                 $savereturn = 'failed';
297             }
298         }
300     }
303     if ($allbreaks = feedback_get_all_break_positions($feedback->id)) {
304         if ($gopage <= 0) {
305             $startposition = 0;
306         } else {
307             if (!isset($allbreaks[$gopage - 1])) {
308                 $gopage = count($allbreaks);
309             }
310             $startposition = $allbreaks[$gopage - 1];
311         }
312         $ispagebreak = true;
313     } else {
314         $startposition = 0;
315         $newpage = 0;
316         $ispagebreak = false;
317     }
319     //get the feedbackitems after the last shown pagebreak
320     $select = 'feedback = ? AND position > ?';
321     $params = array($feedback->id, $startposition);
322     $feedbackitems = $DB->get_records_select('feedback_item', $select, $params, 'position');
324     //get the first pagebreak
325     $params = array('feedback' => $feedback->id, 'typ' => 'pagebreak');
326     if ($pagebreaks = $DB->get_records('feedback_item', $params, 'position')) {
327         $pagebreaks = array_values($pagebreaks);
328         $firstpagebreak = $pagebreaks[0];
329     } else {
330         $firstpagebreak = false;
331     }
332     $maxitemcount = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
334     //get the values of completeds before done. Anonymous user can not get these values.
335     if ((!isset($SESSION->feedback->is_started)) AND
336                           (!isset($savereturn)) AND
337                           ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO)) {
339         $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid);
340         if (!$feedbackcompletedtmp) {
341             $feedbackcompleted = feedback_get_current_completed($feedback->id, false, $courseid);
342             if ($feedbackcompleted) {
343                 //copy the values to feedback_valuetmp create a completedtmp
344                 $feedbackcompletedtmp = feedback_set_tmp_values($feedbackcompleted);
345             }
346         }
347     } else {
348         $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid);
349     }
351     /// Print the main part of the page
352     ///////////////////////////////////////////////////////////////////////////
353     ///////////////////////////////////////////////////////////////////////////
354     ///////////////////////////////////////////////////////////////////////////
355     $analysisurl = new moodle_url('/mod/feedback/analysis.php', array('id'=>$id));
356     if ($courseid > 0) {
357         $analysisurl->param('courseid', $courseid);
358     }
359     echo $OUTPUT->heading(format_text($feedback->name));
361     if ( (intval($feedback->publish_stats) == 1) AND
362             ( has_capability('mod/feedback:viewanalysepage', $context)) AND
363             !( has_capability('mod/feedback:viewreports', $context)) ) {
365         $params = array('userid' => $USER->id, 'feedback' => $feedback->id);
366         if ($multiple_count = $DB->count_records('feedback_tracking', $params)) {
367             echo $OUTPUT->box_start('mdl-align');
368             echo '<a href="'.$analysisurl->out().'">';
369             echo get_string('completed_feedbacks', 'feedback').'</a>';
370             echo $OUTPUT->box_end();
371         }
372     }
374     if (isset($savereturn) && $savereturn == 'saved') {
375         if ($feedback->page_after_submit) {
377             require_once($CFG->libdir . '/filelib.php');
379             $page_after_submit_output = file_rewrite_pluginfile_urls($feedback->page_after_submit,
380                                                                     'pluginfile.php',
381                                                                     $context->id,
382                                                                     'mod_feedback',
383                                                                     'page_after_submit',
384                                                                     0);
386             echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
387             echo format_text($page_after_submit_output,
388                              $feedback->page_after_submitformat,
389                              array('overflowdiv' => true));
390             echo $OUTPUT->box_end();
391         } else {
392             echo '<p align="center">';
393             echo '<b><font color="green">';
394             echo get_string('entries_saved', 'feedback');
395             echo '</font></b>';
396             echo '</p>';
397             if ( intval($feedback->publish_stats) == 1) {
398                 echo '<p align="center"><a href="'.$analysisurl->out().'">';
399                 echo get_string('completed_feedbacks', 'feedback').'</a>';
400                 echo '</p>';
401             }
402         }
404         if ($feedback->site_after_submit) {
405             $url = feedback_encode_target_url($feedback->site_after_submit);
406         } else {
407             if ($courseid) {
408                 if ($courseid == SITEID) {
409                     $url = $CFG->wwwroot;
410                 } else {
411                     $url = $CFG->wwwroot.'/course/view.php?id='.$courseid;
412                 }
413             } else {
414                 if ($course->id == SITEID) {
415                     $url = $CFG->wwwroot;
416                 } else {
417                     $url = $CFG->wwwroot.'/course/view.php?id='.$course->id;
418                 }
419             }
420         }
421         echo $OUTPUT->continue_button($url);
422     } else {
423         if (isset($savereturn) && $savereturn == 'failed') {
424             echo $OUTPUT->box_start('mform error');
425             echo get_string('saving_failed', 'feedback');
426             echo $OUTPUT->box_end();
427         }
429         if (isset($savereturn) && $savereturn == 'missing') {
430             echo $OUTPUT->box_start('mform error');
431             echo get_string('saving_failed_because_missing_or_false_values', 'feedback');
432             echo $OUTPUT->box_end();
433         }
435         //print the items
436         if (is_array($feedbackitems)) {
437             echo $OUTPUT->box_start('feedback_form');
438             echo '<form action="complete.php" method="post" onsubmit=" ">';
439             echo '<fieldset>';
440             echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
441             echo $OUTPUT->box_start('feedback_anonymousinfo');
442             switch ($feedback->anonymous) {
443                 case FEEDBACK_ANONYMOUS_YES:
444                     echo '<input type="hidden" name="anonymous" value="1" />';
445                     $inputvalue = 'value="'.FEEDBACK_ANONYMOUS_YES.'"';
446                     echo '<input type="hidden" name="anonymous_response" '.$inputvalue.' />';
447                     echo get_string('mode', 'feedback').': '.get_string('anonymous', 'feedback');
448                     break;
449                 case FEEDBACK_ANONYMOUS_NO:
450                     echo '<input type="hidden" name="anonymous" value="0" />';
451                     $inputvalue = 'value="'.FEEDBACK_ANONYMOUS_NO.'"';
452                     echo '<input type="hidden" name="anonymous_response" '.$inputvalue.' />';
453                     echo get_string('mode', 'feedback').': ';
454                     echo get_string('non_anonymous', 'feedback');
455                     break;
456             }
457             echo $OUTPUT->box_end();
458             //check, if there exists required-elements
459             $params = array('feedback' => $feedback->id, 'required' => 1);
460             $countreq = $DB->count_records('feedback_item', $params);
461             if ($countreq > 0) {
462                 echo '<span class="feedback_required_mark">(*)';
463                 echo get_string('items_are_required', 'feedback');
464                 echo '</span>';
465             }
466             echo $OUTPUT->box_start('feedback_items');
468             unset($startitem);
469             $select = 'feedback = ? AND hasvalue = 1 AND position < ?';
470             $params = array($feedback->id, $startposition);
471             $itemnr = $DB->count_records_select('feedback_item', $select, $params);
472             $lastbreakposition = 0;
473             $align = right_to_left() ? 'right' : 'left';
475             foreach ($feedbackitems as $feedbackitem) {
476                 if (!isset($startitem)) {
477                     //avoid showing double pagebreaks
478                     if ($feedbackitem->typ == 'pagebreak') {
479                         continue;
480                     }
481                     $startitem = $feedbackitem;
482                 }
484                 if ($feedbackitem->dependitem > 0) {
485                     //chech if the conditions are ok
486                     $fb_compare_value = feedback_compare_item_value($feedbackcompletedtmp->id,
487                                                                     $feedbackitem->dependitem,
488                                                                     $feedbackitem->dependvalue,
489                                                                     true);
490                     if (!isset($feedbackcompletedtmp->id) OR !$fb_compare_value) {
491                         $lastitem = $feedbackitem;
492                         $lastbreakposition = $feedbackitem->position;
493                         continue;
494                     }
495                 }
497                 if ($feedbackitem->dependitem > 0) {
498                     $dependstyle = ' feedback_complete_depend';
499                 } else {
500                     $dependstyle = '';
501                 }
503                 echo $OUTPUT->box_start('feedback_item_box_'.$align.$dependstyle);
504                 $value = '';
505                 //get the value
506                 $frmvaluename = $feedbackitem->typ . '_'. $feedbackitem->id;
507                 if (isset($savereturn)) {
508                     $value = isset($formdata->{$frmvaluename}) ? $formdata->{$frmvaluename} : null;
509                     $value = feedback_clean_input_value($feedbackitem, $value);
510                 } else {
511                     if (isset($feedbackcompletedtmp->id)) {
512                         $value = feedback_get_item_value($feedbackcompletedtmp->id,
513                                                          $feedbackitem->id,
514                                                          true);
515                     }
516                 }
517                 if ($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
518                     $itemnr++;
519                     echo $OUTPUT->box_start('feedback_item_number_'.$align);
520                     echo $itemnr;
521                     echo $OUTPUT->box_end();
522                 }
523                 if ($feedbackitem->typ != 'pagebreak') {
524                     echo $OUTPUT->box_start('box generalbox boxalign_'.$align);
525                     feedback_print_item_complete($feedbackitem, $value, $highlightrequired);
526                     echo $OUTPUT->box_end();
527                 }
529                 echo $OUTPUT->box_end();
531                 $lastbreakposition = $feedbackitem->position; //last item-pos (item or pagebreak)
532                 if ($feedbackitem->typ == 'pagebreak') {
533                     break;
534                 } else {
535                     $lastitem = $feedbackitem;
536                 }
537             }
538             echo $OUTPUT->box_end();
539             echo '<input type="hidden" name="id" value="'.$id.'" />';
540             echo '<input type="hidden" name="feedbackid" value="'.$feedback->id.'" />';
541             echo '<input type="hidden" name="lastpage" value="'.$gopage.'" />';
542             if (isset($feedbackcompletedtmp->id)) {
543                 $inputvalue = 'value="'.$feedbackcompletedtmp->id.'"';
544             } else {
545                 $inputvalue = 'value=""';
546             }
547             echo '<input type="hidden" name="completedid" '.$inputvalue.' />';
548             echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
549             echo '<input type="hidden" name="preservevalues" value="1" />';
550             if (isset($startitem)) {
551                 echo '<input type="hidden" name="startitempos" value="'.$startitem->position.'" />';
552                 echo '<input type="hidden" name="lastitempos" value="'.$lastitem->position.'" />';
553             }
555             if ( $ispagebreak AND $lastbreakposition > $firstpagebreak->position) {
556                 $inputvalue = 'value="'.get_string('previous_page', 'feedback').'"';
557                 echo '<input name="gopreviouspage" type="submit" '.$inputvalue.' />';
558             }
559             if ($lastbreakposition < $maxitemcount) {
560                 $inputvalue = 'value="'.get_string('next_page', 'feedback').'"';
561                 echo '<input name="gonextpage" type="submit" '.$inputvalue.' />';
562             }
563             if ($lastbreakposition >= $maxitemcount) { //last page
564                 $inputvalue = 'value="'.get_string('save_entries', 'feedback').'"';
565                 echo '<input name="savevalues" type="submit" '.$inputvalue.' />';
566             }
568             echo '</fieldset>';
569             echo '</form>';
570             echo $OUTPUT->box_end();
572             echo $OUTPUT->box_start('feedback_complete_cancel');
573             if ($courseid) {
574                 $action = 'action="'.$CFG->wwwroot.'/course/view.php?id='.$courseid.'"';
575             } else {
576                 if ($course->id == SITEID) {
577                     $action = 'action="'.$CFG->wwwroot.'"';
578                 } else {
579                     $action = 'action="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'"';
580                 }
581             }
582             echo '<form '.$action.' method="post" onsubmit=" ">';
583             echo '<fieldset>';
584             echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
585             echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
586             echo '<button type="submit">'.get_string('cancel').'</button>';
587             echo '</fieldset>';
588             echo '</form>';
589             echo $OUTPUT->box_end();
590             $SESSION->feedback->is_started = true;
591         }
592     }
593 } else {
594     echo $OUTPUT->box_start('generalbox boxaligncenter');
595         echo '<h2>';
596         echo '<font color="red">';
597         echo get_string('this_feedback_is_already_submitted', 'feedback');
598         echo '</font>';
599         echo '</h2>';
600         echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
601     echo $OUTPUT->box_end();
603 /// Finish the page
604 ///////////////////////////////////////////////////////////////////////////
605 ///////////////////////////////////////////////////////////////////////////
606 ///////////////////////////////////////////////////////////////////////////
608 echo $OUTPUT->footer();