Merge branch 'MDL-40687' of git://github.com/rwijaya/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->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;
199     }
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);
206         }
207         //checken, ob alle required items einen wert haben
208         if (feedback_check_values($startitempos, $lastitempos)) {
209             $userid = $USER->id; //arb
210             if ($completedid = feedback_save_values($USER->id, true)) {
211                 if ($userid > 0) {
212                     add_to_log($course->id,
213                                'feedback',
214                                'startcomplete',
215                                'view.php?id='.$cm->id,
216                                $feedback->id,
217                                $cm->id,
218                                $userid);
219                 }
220                 if (!$gonextpage AND !$gopreviouspage) {
221                     $preservevalues = false;//es kann gespeichert werden
222                 }
224             } else {
225                 $savereturn = 'failed';
226                 if (isset($lastpage)) {
227                     $gopage = $lastpage;
228                 } else {
229                     print_error('missingparameter');
230                 }
231             }
232         } else {
233             $savereturn = 'missing';
234             $highlightrequired = true;
235             if (isset($lastpage)) {
236                 $gopage = $lastpage;
237             } else {
238                 print_error('missingparameter');
239             }
241         }
242     }
244     //saving the items
245     if ($savevalues AND !$preservevalues) {
246         //exists there any pagebreak, so there are values in the feedback_valuetmp
247         $userid = $USER->id; //arb
249         if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
250             $feedbackcompleted = feedback_get_current_completed($feedback->id, false, $courseid);
251         } else {
252             $feedbackcompleted = false;
253         }
254         $params = array('id' => $completedid);
255         $feedbackcompletedtmp = $DB->get_record('feedback_completedtmp', $params);
256         //fake saving for switchrole
257         $is_switchrole = feedback_check_is_switchrole();
258         if ($is_switchrole) {
259             $savereturn = 'saved';
260             feedback_delete_completedtmp($completedid);
261         } else {
262             $new_completed_id = feedback_save_tmp_values($feedbackcompletedtmp,
263                                                          $feedbackcompleted,
264                                                          $userid);
265             if ($new_completed_id) {
266                 $savereturn = 'saved';
267                 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
268                     add_to_log($course->id,
269                               'feedback',
270                               'submit',
271                               'view.php?id='.$cm->id,
272                               $feedback->id,
273                               $cm->id,
274                               $userid);
276                     feedback_send_email($cm, $feedback, $course, $userid);
277                 } else {
278                     feedback_send_email_anonym($cm, $feedback, $course, $userid);
279                 }
280                 //tracking the submit
281                 $tracking = new stdClass();
282                 $tracking->userid = $USER->id;
283                 $tracking->feedback = $feedback->id;
284                 $tracking->completed = $new_completed_id;
285                 $DB->insert_record('feedback_tracking', $tracking);
286                 unset($SESSION->feedback->is_started);
288                 // Update completion state
289                 $completion = new completion_info($course);
290                 if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
291                     $completion->update_state($cm, COMPLETION_COMPLETE);
292                 }
294             } else {
295                 $savereturn = 'failed';
296             }
297         }
299     }
302     if ($allbreaks = feedback_get_all_break_positions($feedback->id)) {
303         if ($gopage <= 0) {
304             $startposition = 0;
305         } else {
306             if (!isset($allbreaks[$gopage - 1])) {
307                 $gopage = count($allbreaks);
308             }
309             $startposition = $allbreaks[$gopage - 1];
310         }
311         $ispagebreak = true;
312     } else {
313         $startposition = 0;
314         $newpage = 0;
315         $ispagebreak = false;
316     }
318     //get the feedbackitems after the last shown pagebreak
319     $select = 'feedback = ? AND position > ?';
320     $params = array($feedback->id, $startposition);
321     $feedbackitems = $DB->get_records_select('feedback_item', $select, $params, 'position');
323     //get the first pagebreak
324     $params = array('feedback' => $feedback->id, 'typ' => 'pagebreak');
325     if ($pagebreaks = $DB->get_records('feedback_item', $params, 'position')) {
326         $pagebreaks = array_values($pagebreaks);
327         $firstpagebreak = $pagebreaks[0];
328     } else {
329         $firstpagebreak = false;
330     }
331     $maxitemcount = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
333     //get the values of completeds before done. Anonymous user can not get these values.
334     if ((!isset($SESSION->feedback->is_started)) AND
335                           (!isset($savereturn)) AND
336                           ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO)) {
338         $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid);
339         if (!$feedbackcompletedtmp) {
340             $feedbackcompleted = feedback_get_current_completed($feedback->id, false, $courseid);
341             if ($feedbackcompleted) {
342                 //copy the values to feedback_valuetmp create a completedtmp
343                 $feedbackcompletedtmp = feedback_set_tmp_values($feedbackcompleted);
344             }
345         }
346     } else {
347         $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid);
348     }
350     /// Print the main part of the page
351     ///////////////////////////////////////////////////////////////////////////
352     ///////////////////////////////////////////////////////////////////////////
353     ///////////////////////////////////////////////////////////////////////////
354     $analysisurl = new moodle_url('/mod/feedback/analysis.php', array('id'=>$id));
355     if ($courseid > 0) {
356         $analysisurl->param('courseid', $courseid);
357     }
358     echo $OUTPUT->heading(format_string($feedback->name));
360     if ( (intval($feedback->publish_stats) == 1) AND
361             ( has_capability('mod/feedback:viewanalysepage', $context)) AND
362             !( has_capability('mod/feedback:viewreports', $context)) ) {
364         $params = array('userid' => $USER->id, 'feedback' => $feedback->id);
365         if ($multiple_count = $DB->count_records('feedback_tracking', $params)) {
366             echo $OUTPUT->box_start('mdl-align');
367             echo '<a href="'.$analysisurl->out().'">';
368             echo get_string('completed_feedbacks', 'feedback').'</a>';
369             echo $OUTPUT->box_end();
370         }
371     }
373     if (isset($savereturn) && $savereturn == 'saved') {
374         if ($feedback->page_after_submit) {
376             require_once($CFG->libdir . '/filelib.php');
378             $page_after_submit_output = file_rewrite_pluginfile_urls($feedback->page_after_submit,
379                                                                     'pluginfile.php',
380                                                                     $context->id,
381                                                                     'mod_feedback',
382                                                                     'page_after_submit',
383                                                                     0);
385             echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
386             echo format_text($page_after_submit_output,
387                              $feedback->page_after_submitformat,
388                              array('overflowdiv' => true));
389             echo $OUTPUT->box_end();
390         } else {
391             echo '<p align="center">';
392             echo '<b><font color="green">';
393             echo get_string('entries_saved', 'feedback');
394             echo '</font></b>';
395             echo '</p>';
396             if ( intval($feedback->publish_stats) == 1) {
397                 echo '<p align="center"><a href="'.$analysisurl->out().'">';
398                 echo get_string('completed_feedbacks', 'feedback').'</a>';
399                 echo '</p>';
400             }
401         }
403         if ($feedback->site_after_submit) {
404             $url = feedback_encode_target_url($feedback->site_after_submit);
405         } else {
406             if ($courseid) {
407                 if ($courseid == SITEID) {
408                     $url = $CFG->wwwroot;
409                 } else {
410                     $url = $CFG->wwwroot.'/course/view.php?id='.$courseid;
411                 }
412             } else {
413                 if ($course->id == SITEID) {
414                     $url = $CFG->wwwroot;
415                 } else {
416                     $url = $CFG->wwwroot.'/course/view.php?id='.$course->id;
417                 }
418             }
419         }
420         echo $OUTPUT->continue_button($url);
421     } else {
422         if (isset($savereturn) && $savereturn == 'failed') {
423             echo $OUTPUT->box_start('mform error');
424             echo get_string('saving_failed', 'feedback');
425             echo $OUTPUT->box_end();
426         }
428         if (isset($savereturn) && $savereturn == 'missing') {
429             echo $OUTPUT->box_start('mform error');
430             echo get_string('saving_failed_because_missing_or_false_values', 'feedback');
431             echo $OUTPUT->box_end();
432         }
434         //print the items
435         if (is_array($feedbackitems)) {
436             echo $OUTPUT->box_start('feedback_form');
437             echo '<form action="complete.php" method="post" onsubmit=" ">';
438             echo '<fieldset>';
439             echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
440             echo $OUTPUT->box_start('feedback_anonymousinfo');
441             switch ($feedback->anonymous) {
442                 case FEEDBACK_ANONYMOUS_YES:
443                     echo '<input type="hidden" name="anonymous" value="1" />';
444                     $inputvalue = 'value="'.FEEDBACK_ANONYMOUS_YES.'"';
445                     echo '<input type="hidden" name="anonymous_response" '.$inputvalue.' />';
446                     echo get_string('mode', 'feedback').': '.get_string('anonymous', 'feedback');
447                     break;
448                 case FEEDBACK_ANONYMOUS_NO:
449                     echo '<input type="hidden" name="anonymous" value="0" />';
450                     $inputvalue = 'value="'.FEEDBACK_ANONYMOUS_NO.'"';
451                     echo '<input type="hidden" name="anonymous_response" '.$inputvalue.' />';
452                     echo get_string('mode', 'feedback').': ';
453                     echo get_string('non_anonymous', 'feedback');
454                     break;
455             }
456             echo $OUTPUT->box_end();
457             //check, if there exists required-elements
458             $params = array('feedback' => $feedback->id, 'required' => 1);
459             $countreq = $DB->count_records('feedback_item', $params);
460             if ($countreq > 0) {
461                 echo '<span class="feedback_required_mark">(*)';
462                 echo get_string('items_are_required', 'feedback');
463                 echo '</span>';
464             }
465             echo $OUTPUT->box_start('feedback_items');
467             unset($startitem);
468             $select = 'feedback = ? AND hasvalue = 1 AND position < ?';
469             $params = array($feedback->id, $startposition);
470             $itemnr = $DB->count_records_select('feedback_item', $select, $params);
471             $lastbreakposition = 0;
472             $align = right_to_left() ? 'right' : 'left';
474             foreach ($feedbackitems as $feedbackitem) {
475                 if (!isset($startitem)) {
476                     //avoid showing double pagebreaks
477                     if ($feedbackitem->typ == 'pagebreak') {
478                         continue;
479                     }
480                     $startitem = $feedbackitem;
481                 }
483                 if ($feedbackitem->dependitem > 0) {
484                     //chech if the conditions are ok
485                     $fb_compare_value = feedback_compare_item_value($feedbackcompletedtmp->id,
486                                                                     $feedbackitem->dependitem,
487                                                                     $feedbackitem->dependvalue,
488                                                                     true);
489                     if (!isset($feedbackcompletedtmp->id) OR !$fb_compare_value) {
490                         $lastitem = $feedbackitem;
491                         $lastbreakposition = $feedbackitem->position;
492                         continue;
493                     }
494                 }
496                 if ($feedbackitem->dependitem > 0) {
497                     $dependstyle = ' feedback_complete_depend';
498                 } else {
499                     $dependstyle = '';
500                 }
502                 echo $OUTPUT->box_start('feedback_item_box_'.$align.$dependstyle);
503                 $value = '';
504                 //get the value
505                 $frmvaluename = $feedbackitem->typ . '_'. $feedbackitem->id;
506                 if (isset($savereturn)) {
507                     $value = isset($formdata->{$frmvaluename}) ? $formdata->{$frmvaluename} : null;
508                     $value = feedback_clean_input_value($feedbackitem, $value);
509                 } else {
510                     if (isset($feedbackcompletedtmp->id)) {
511                         $value = feedback_get_item_value($feedbackcompletedtmp->id,
512                                                          $feedbackitem->id,
513                                                          true);
514                     }
515                 }
516                 if ($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
517                     $itemnr++;
518                     echo $OUTPUT->box_start('feedback_item_number_'.$align);
519                     echo $itemnr;
520                     echo $OUTPUT->box_end();
521                 }
522                 if ($feedbackitem->typ != 'pagebreak') {
523                     echo $OUTPUT->box_start('box generalbox boxalign_'.$align);
524                     feedback_print_item_complete($feedbackitem, $value, $highlightrequired);
525                     echo $OUTPUT->box_end();
526                 }
528                 echo $OUTPUT->box_end();
530                 $lastbreakposition = $feedbackitem->position; //last item-pos (item or pagebreak)
531                 if ($feedbackitem->typ == 'pagebreak') {
532                     break;
533                 } else {
534                     $lastitem = $feedbackitem;
535                 }
536             }
537             echo $OUTPUT->box_end();
538             echo '<input type="hidden" name="id" value="'.$id.'" />';
539             echo '<input type="hidden" name="feedbackid" value="'.$feedback->id.'" />';
540             echo '<input type="hidden" name="lastpage" value="'.$gopage.'" />';
541             if (isset($feedbackcompletedtmp->id)) {
542                 $inputvalue = 'value="'.$feedbackcompletedtmp->id.'"';
543             } else {
544                 $inputvalue = 'value=""';
545             }
546             echo '<input type="hidden" name="completedid" '.$inputvalue.' />';
547             echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
548             echo '<input type="hidden" name="preservevalues" value="1" />';
549             if (isset($startitem)) {
550                 echo '<input type="hidden" name="startitempos" value="'.$startitem->position.'" />';
551                 echo '<input type="hidden" name="lastitempos" value="'.$lastitem->position.'" />';
552             }
554             if ( $ispagebreak AND $lastbreakposition > $firstpagebreak->position) {
555                 $inputvalue = 'value="'.get_string('previous_page', 'feedback').'"';
556                 echo '<input name="gopreviouspage" type="submit" '.$inputvalue.' />';
557             }
558             if ($lastbreakposition < $maxitemcount) {
559                 $inputvalue = 'value="'.get_string('next_page', 'feedback').'"';
560                 echo '<input name="gonextpage" type="submit" '.$inputvalue.' />';
561             }
562             if ($lastbreakposition >= $maxitemcount) { //last page
563                 $inputvalue = 'value="'.get_string('save_entries', 'feedback').'"';
564                 echo '<input name="savevalues" type="submit" '.$inputvalue.' />';
565             }
567             echo '</fieldset>';
568             echo '</form>';
569             echo $OUTPUT->box_end();
571             echo $OUTPUT->box_start('feedback_complete_cancel');
572             if ($courseid) {
573                 $action = 'action="'.$CFG->wwwroot.'/course/view.php?id='.$courseid.'"';
574             } else {
575                 if ($course->id == SITEID) {
576                     $action = 'action="'.$CFG->wwwroot.'"';
577                 } else {
578                     $action = 'action="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'"';
579                 }
580             }
581             echo '<form '.$action.' method="post" onsubmit=" ">';
582             echo '<fieldset>';
583             echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
584             echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
585             echo '<button type="submit">'.get_string('cancel').'</button>';
586             echo '</fieldset>';
587             echo '</form>';
588             echo $OUTPUT->box_end();
589             $SESSION->feedback->is_started = true;
590         }
591     }
592 } else {
593     echo $OUTPUT->heading(format_string($feedback->name));
594     echo $OUTPUT->box_start('generalbox boxaligncenter');
595     echo $OUTPUT->notification(get_string('this_feedback_is_already_submitted', 'feedback'));
596     echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
597     echo $OUTPUT->box_end();
599 /// Finish the page
600 ///////////////////////////////////////////////////////////////////////////
601 ///////////////////////////////////////////////////////////////////////////
602 ///////////////////////////////////////////////////////////////////////////
604 echo $OUTPUT->footer();