MDL-43656 mod_feedback: availablity date logic incorrect
[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 mod_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($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;
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         // 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.
213                 }
215             } else {
216                 $savereturn = 'failed';
217                 if (isset($lastpage)) {
218                     $gopage = $lastpage;
219                 } else {
220                     print_error('missingparameter');
221                 }
222             }
223         } else {
224             $savereturn = 'missing';
225             $highlightrequired = true;
226             if (isset($lastpage)) {
227                 $gopage = $lastpage;
228             } else {
229                 print_error('missingparameter');
230             }
232         }
233     }
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;
244         }
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);
262                 }
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);
275                 }
277             } else {
278                 $savereturn = 'failed';
279             }
280         }
282     }
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);
291             }
292             $startposition = $allbreaks[$gopage - 1];
293         }
294         $ispagebreak = true;
295     } else {
296         $startposition = 0;
297         $newpage = 0;
298         $ispagebreak = false;
299     }
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;
313     }
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);
327             }
328         }
329     } else {
330         $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid);
331     }
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);
340     }
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();
353         }
354     }
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',
366                                                                     0);
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>';
383             }
384         }
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;
394                 }
395             } else {
396                 if ($course->id == SITEID) {
397                     $url = $CFG->wwwroot;
398                 } else {
399                     $url = $CFG->wwwroot.'/course/view.php?id='.$course->id;
400                 }
401             }
402         }
403         echo $OUTPUT->continue_button($url);
404     } else {
405         if (isset($savereturn) && $savereturn == 'failed') {
406             echo $OUTPUT->box_start('mform error');
407             echo get_string('saving_failed', 'feedback');
408             echo $OUTPUT->box_end();
409         }
411         if (isset($savereturn) && $savereturn == 'missing') {
412             echo $OUTPUT->box_start('mform error');
413             echo get_string('saving_failed_because_missing_or_false_values', 'feedback');
414             echo $OUTPUT->box_end();
415         }
417         //print the items
418         if (is_array($feedbackitems)) {
419             echo $OUTPUT->box_start('feedback_form');
420             echo '<form action="complete.php" 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;
437             }
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="feedback_required_mark">(*)';
444                 echo get_string('items_are_required', 'feedback');
445                 echo '</span>';
446             }
447             echo $OUTPUT->box_start('feedback_items');
449             unset($startitem);
450             $select = 'feedback = ? AND hasvalue = 1 AND position < ?';
451             $params = array($feedback->id, $startposition);
452             $itemnr = $DB->count_records_select('feedback_item', $select, $params);
453             $lastbreakposition = 0;
454             $align = right_to_left() ? 'right' : 'left';
456             foreach ($feedbackitems as $feedbackitem) {
457                 if (!isset($startitem)) {
458                     //avoid showing double pagebreaks
459                     if ($feedbackitem->typ == 'pagebreak') {
460                         continue;
461                     }
462                     $startitem = $feedbackitem;
463                 }
465                 if ($feedbackitem->dependitem > 0) {
466                     //chech if the conditions are ok
467                     $fb_compare_value = feedback_compare_item_value($feedbackcompletedtmp->id,
468                                                                     $feedbackitem->dependitem,
469                                                                     $feedbackitem->dependvalue,
470                                                                     true);
471                     if (!isset($feedbackcompletedtmp->id) OR !$fb_compare_value) {
472                         $lastitem = $feedbackitem;
473                         $lastbreakposition = $feedbackitem->position;
474                         continue;
475                     }
476                 }
478                 if ($feedbackitem->dependitem > 0) {
479                     $dependstyle = ' feedback_complete_depend';
480                 } else {
481                     $dependstyle = '';
482                 }
484                 echo $OUTPUT->box_start('feedback_item_box_'.$align.$dependstyle);
485                 $value = '';
486                 //get the value
487                 $frmvaluename = $feedbackitem->typ . '_'. $feedbackitem->id;
488                 if (isset($savereturn)) {
489                     $value = isset($formdata->{$frmvaluename}) ? $formdata->{$frmvaluename} : null;
490                     $value = feedback_clean_input_value($feedbackitem, $value);
491                 } else {
492                     if (isset($feedbackcompletedtmp->id)) {
493                         $value = feedback_get_item_value($feedbackcompletedtmp->id,
494                                                          $feedbackitem->id,
495                                                          true);
496                     }
497                 }
498                 if ($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
499                     $itemnr++;
500                     echo $OUTPUT->box_start('feedback_item_number_'.$align);
501                     echo $itemnr;
502                     echo $OUTPUT->box_end();
503                 }
504                 if ($feedbackitem->typ != 'pagebreak') {
505                     echo $OUTPUT->box_start('box generalbox boxalign_'.$align);
506                     feedback_print_item_complete($feedbackitem, $value, $highlightrequired);
507                     echo $OUTPUT->box_end();
508                 }
510                 echo $OUTPUT->box_end();
512                 $lastbreakposition = $feedbackitem->position; //last item-pos (item or pagebreak)
513                 if ($feedbackitem->typ == 'pagebreak') {
514                     break;
515                 } else {
516                     $lastitem = $feedbackitem;
517                 }
518             }
519             echo $OUTPUT->box_end();
520             echo '<input type="hidden" name="id" value="'.$id.'" />';
521             echo '<input type="hidden" name="feedbackid" value="'.$feedback->id.'" />';
522             echo '<input type="hidden" name="lastpage" value="'.$gopage.'" />';
523             if (isset($feedbackcompletedtmp->id)) {
524                 $inputvalue = 'value="'.$feedbackcompletedtmp->id.'"';
525             } else {
526                 $inputvalue = 'value=""';
527             }
528             echo '<input type="hidden" name="completedid" '.$inputvalue.' />';
529             echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
530             echo '<input type="hidden" name="preservevalues" value="1" />';
531             if (isset($startitem)) {
532                 echo '<input type="hidden" name="startitempos" value="'.$startitem->position.'" />';
533                 echo '<input type="hidden" name="lastitempos" value="'.$lastitem->position.'" />';
534             }
536             if ( $ispagebreak AND $lastbreakposition > $firstpagebreak->position) {
537                 $inputvalue = 'value="'.get_string('previous_page', 'feedback').'"';
538                 echo '<input name="gopreviouspage" type="submit" '.$inputvalue.' />';
539             }
540             if ($lastbreakposition < $maxitemcount) {
541                 $inputvalue = 'value="'.get_string('next_page', 'feedback').'"';
542                 echo '<input name="gonextpage" type="submit" '.$inputvalue.' />';
543             }
544             if ($lastbreakposition >= $maxitemcount) { //last page
545                 $inputvalue = 'value="'.get_string('save_entries', 'feedback').'"';
546                 echo '<input name="savevalues" type="submit" '.$inputvalue.' />';
547             }
549             echo '</form>';
550             echo $OUTPUT->box_end();
552             echo $OUTPUT->box_start('feedback_complete_cancel');
553             if ($courseid) {
554                 $action = 'action="'.$CFG->wwwroot.'/course/view.php?id='.$courseid.'"';
555             } else {
556                 if ($course->id == SITEID) {
557                     $action = 'action="'.$CFG->wwwroot.'"';
558                 } else {
559                     $action = 'action="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'"';
560                 }
561             }
562             echo '<form '.$action.' method="post" onsubmit=" ">';
563             echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
564             echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
565             echo '<button type="submit">'.get_string('cancel').'</button>';
566             echo '</form>';
567             echo $OUTPUT->box_end();
568             $SESSION->feedback->is_started = true;
569         }
570     }
571 } else {
572     echo $OUTPUT->heading(format_string($feedback->name));
573     echo $OUTPUT->box_start('generalbox boxaligncenter');
574     echo $OUTPUT->notification(get_string('this_feedback_is_already_submitted', 'feedback'));
575     echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
576     echo $OUTPUT->box_end();
578 /// Finish the page
579 ///////////////////////////////////////////////////////////////////////////
580 ///////////////////////////////////////////////////////////////////////////
581 ///////////////////////////////////////////////////////////////////////////
583 echo $OUTPUT->footer();