c0ea3790e9a973b2b68ba186a70a3f588e78e5dd
[moodle.git] / mod / feedback / complete.php
1 <?php // $Id$
2 /**
3 * prints the form so the user can fill out the feedback
4 *
5 * @version $Id$
6 * @author Andreas Grabs
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package feedback
9 */
11     require_once("../../config.php");
12     require_once("lib.php");
14     $id = required_param('id', PARAM_INT);
15     $completedid = optional_param('completedid', false, PARAM_INT);
16     $preservevalues  = optional_param('preservevalues', 0,  PARAM_INT);
17     $courseid = optional_param('courseid', false, PARAM_INT);
18     $gopage = optional_param('gopage', -1, PARAM_INT);
19     $lastpage = optional_param('lastpage', false, PARAM_INT);
20     $startitempos = optional_param('startitempos', 0, PARAM_INT);
21     $lastitempos = optional_param('lastitempos', 0, PARAM_INT);
22     $anonymous_response = optional_param('anonymous_response', 0, PARAM_INT); //arb
24     $highlightrequired = false;
26     if(($formdata = data_submitted('nomatch')) AND !confirm_sesskey()) {
27         error('no sesskey defined');
28     }
30     //if the use hit enter into a textfield so the form should not submit
31     if(isset($formdata->sesskey) AND !isset($formdata->savevalues) AND !isset($formdata->gonextpage) AND !isset($formdata->gopreviouspage)) {
32         $gopage = $formdata->lastpage;
33     }
35     if(isset($formdata->savevalues)) {
36         $savevalues = true;
37     }else {
38         $savevalues = false;
39     }
40     
41     if($gopage < 0 AND !$savevalues) {
42         if(isset($formdata->gonextpage)){
43             $gopage = $lastpage + 1;
44             $gonextpage = true;
45             $gopreviouspage = false;
46         }else if(isset($formdata->gopreviouspage)){
47             $gopage = $lastpage - 1;
48             $gonextpage = false;
49             $gopreviouspage = true;
50         }else {
51             error('parameter (gopage) required');
52         }
53     }else {
54         $gonextpage = $gopreviouspage = false;
55     }
56     
58     if ($id) {
59         if (! $cm = get_coursemodule_from_id('feedback', $id)) {
60             error("Course Module ID was incorrect");
61         }
62      
63         if (! $course = get_record("course", "id", $cm->course)) {
64             error("Course is misconfigured");
65         }
66      
67         if (! $feedback = get_record("feedback", "id", $cm->instance)) {
68             error("Course module is incorrect");
69         }
70     }
72     $capabilities = feedback_load_capabilities($cm->id);
73     
74     if($feedback->anonymous == FEEDBACK_ANONYMOUS_YES) {
75         $capabilities->complete = true;
76     }
77     //check whether the feedback is located and! started from the mainsite
78     if($course->id == SITEID AND !$courseid) {
79         $courseid = SITEID;
80     }
81         
82     if($feedback->anonymous != FEEDBACK_ANONYMOUS_YES) {
83         require_login($course->id, true, $cm);
84     } else {
85         require_course_login($course, true, $cm);
86     }
87     
88     if($courseid AND $courseid != SITEID) {
89         $course2 = get_record('course', 'id', $courseid);
90         require_course_login($course2); //this overwrites the object $course :-(
91         $course = get_record("course", "id", $cm->course); // the workaround
92     }
93     
94     if(!$capabilities->complete) {
95         error(get_string('error'));
96     }
97     
98     /// Print the page header
99     $strfeedbacks = get_string("modulenameplural", "feedback");
100     $strfeedback  = get_string("modulename", "feedback");
101     $buttontext = update_module_button($cm->id, $course->id, $strfeedback);
102     
103     $navlinks = array();
104     $navlinks[] = array('name' => $strfeedbacks, 'link' => "index.php?id=$course->id", 'type' => 'activity');
105     $navlinks[] = array('name' => format_string($feedback->name), 'link' => "", 'type' => 'activityinstance');
106     
107     $navigation = build_navigation($navlinks);
108     
109     print_header_simple(format_string($feedback->name), "",
110                  $navigation, "", "", true, $buttontext, navmenu($course, $cm));
112     //ishidden check.
113     if ((empty($cm->visible) and !$capabilities->viewhiddenactivities) AND $course->id != SITEID) {
114         notice(get_string("activityiscurrentlyhidden"));
115     }
117     feedback_print_errors();
118   
119     //check, if the feedback is open (timeopen, timeclose)
120     $checktime = time();
121     if(($feedback->timeopen > $checktime) OR ($feedback->timeclose < $checktime AND $feedback->timeclose > 0)) {
122         // print_simple_box_start('center');
123         print_box_start('generalbox boxaligncenter');
124             echo '<h2><font color="red">'.get_string('feedback_is_not_open', 'feedback').'</font></h2>';
125             print_continue($CFG->wwwroot.'/course/view.php?id='.$course->id);
126         // print_simple_box_end();
127         print_box_end();
128         print_footer($course);
129         exit;
130     }
131     
132     //additional check for multiple-submit (prevent browsers back-button). the main-check is in view.php
133     $feedback_can_submit = true;
134     if($feedback->multiple_submit == 0 ) {
135         if(feedback_is_already_submitted($feedback->id, $courseid)) {
136             $feedback_can_submit = false;
137         }
138     }
139     if($feedback_can_submit) {
140         //preserving the items
141         if($preservevalues == 1){
142             if(!$SESSION->feedback->is_started == true)error('error', $CFG->wwwroot.'/course/view.php?id='.$course->id);
143             //checken, ob alle required items einen wert haben
144             if(feedback_check_values($_POST, $startitempos, $lastitempos)) {
145                     $userid = $USER->id; //arb
146                 if($completedid = feedback_save_values($_POST, $USER->id, true)){
147                     if($userid > 0) {
148                         add_to_log($course->id, 'feedback', 'startcomplete', 'view.php?id='.$cm->id, $feedback->id, $cm->id, $userid);
149                     }
150                     if(!$gonextpage AND !$gopreviouspage) $preservevalues = false;//es kann gespeichert werden
151                     
152                 }else {
153                     $savereturn = 'failed';
154                     if(isset($lastpage)) {
155                         $gopage = $lastpage;
156                     }else {
157                         error('parameter failed');
158                     }
159                 }
160             }else {
161                 $savereturn = 'missing';
162                 $highlightrequired = true;
163                 if(isset($lastpage)) {
164                     $gopage = $lastpage;
165                 }else {
166                     error('parameter failed');
167                 }
168             
169             }
170         }
171         
172         //saving the items
173         if($savevalues AND !$preservevalues){
174             //exists there any pagebreak, so there are values in the feedback_valuetmp
175             $userid = $USER->id; //arb
177             if($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
178                 $feedbackcompleted = feedback_get_current_completed($feedback->id, false, $courseid);
179             }else{
180                 $feedbackcompleted = false;
181             }
182             $feedbackcompletedtmp = get_record('feedback_completedtmp', 'id', $completedid);
183             //fake saving for switchrole
184             $is_switchrole = feedback_check_is_switchrole();
185             if($is_switchrole) {
186                 $savereturn = 'saved';
187                 feedback_delete_completedtmp($completedid);
188             }else if($new_completed_id = feedback_save_tmp_values($feedbackcompletedtmp, $feedbackcompleted, $userid)) {
189                 $savereturn = 'saved';
190                 if($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
191                     add_to_log($course->id, 'feedback', 'submit', 'view.php?id='.$cm->id, $feedback->id, $cm->id, $userid);
192                     feedback_send_email($cm, $feedback, $course, $userid);
193                 }else {
194                     feedback_send_email_anonym($cm, $feedback, $course, $userid);
195                 }
196                 //tracking the submit
197                 $tracking = null;
198                 $tracking->userid = $USER->id;
199                 $tracking->feedback = $feedback->id;
200                 $tracking->completed = $new_completed_id;
201                 insert_record('feedback_tracking', $tracking);
202                 unset($SESSION->feedback->is_started);
203                 
204             }else {
205                 $savereturn = 'failed';
206             }
207            
208         }
211         if($allbreaks = feedback_get_all_break_positions($feedback->id)){
212             if($gopage <= 0) {
213                 $startposition = 0;
214             }else {
215                 $startposition = $allbreaks[$gopage - 1];
216             }
217             $ispagebreak = true;
218         }else {
219             $startposition = 0;
220             $newpage = 0;
221             $ispagebreak = false;
222         }
223         
224         //get the feedbackitems after the last shown pagebreak
225         $feedbackitems = get_records_select('feedback_item', 'feedback = '.$feedback->id.' AND position > '.$startposition, 'position');
226         
227         //get the first pagebreak
228         if($pagebreaks = get_records_select('feedback_item', "feedback = ".$feedback->id." AND typ = 'pagebreak'", 'position')) {
229             $pagebreaks = array_values($pagebreaks);
230             $firstpagebreak = $pagebreaks[0];
231         }else {
232             $firstpagebreak = false;
233         }
234         $maxitemcount = count_records('feedback_item', 'feedback', $feedback->id);
235         
236         //get the values of completeds before done. Anonymous user can not get these values.
237         if((!isset($SESSION->feedback->is_started)) AND (!isset($savereturn)) AND ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO)) {
238             if(!$feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid)) {
239                 if($feedbackcompleted = feedback_get_current_completed($feedback->id, false, $courseid)) {
240                     //copy the values to feedback_valuetmp create a completedtmp
241                     $feedbackcompletedtmp = feedback_set_tmp_values($feedbackcompleted);
242                 }
243             }
244         }else {
245             $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid);
246         }
248         /// Print the main part of the page
249         ///////////////////////////////////////////////////////////////////////////
250         ///////////////////////////////////////////////////////////////////////////
251         ///////////////////////////////////////////////////////////////////////////
252         print_heading(format_text($feedback->name));
253         
254         if(isset($savereturn) && $savereturn == 'saved') {
255             if($feedback->page_after_submit) {
256                 // print_simple_box_start('center', '75%');
257                 print_box_start('generalbox boxaligncenter boxwidthwide');
258                 echo format_text(stripslashes_safe($feedback->page_after_submit));
259                 // print_simple_box_end();
260                 print_box_end();
261             } else {
262                 echo '<p align="center"><b><font color="green">'.get_string('entries_saved','feedback').'</font></b></p>';
263                 if( intval($feedback->publish_stats) == 1) {
264                     echo '<p align="center"><a href="analysis.php?id=' . $id . '&courseid='.$courseid.'">';
265                     echo get_string('completed_feedbacks', 'feedback').'</a>';
266                     echo '</p>';
267                 }
268             }
269             if($feedback->site_after_submit) {
270                 print_continue(feedback_encode_target_url($feedback->site_after_submit));
271             }else {
272                 if($courseid) {
273                     print_continue($CFG->wwwroot.'/course/view.php?id='.$courseid);
274                 }else {
275                     if($course->id == SITEID) {
276                         print_continue($CFG->wwwroot);
277                     } else {
278                         print_continue($CFG->wwwroot.'/course/view.php?id='.$course->id);
279                     }
280                 }
281             }
282         }else {
283             if(isset($savereturn) && $savereturn == 'failed') {
284                 echo '<p align="center"><b><font color="red">'.get_string('saving_failed','feedback').'</font></b></p>';
285             }
286     
287             if(isset($savereturn) && $savereturn == 'missing') {
288                 echo '<p align="center"><b><font color="red">'.get_string('saving_failed_because_missing_or_false_values','feedback').'</font></b></p>';
289             }
290     
291             //print the items
292             if(is_array($feedbackitems)){
293                 // print_simple_box_start('center', '75%');
294                 print_box_start('generalbox boxaligncenter boxwidthwide');
295                 echo '<div align="center"><form name="frm" action="'.$ME.'" method="post" onsubmit=" ">';
296                 echo '<input type="hidden" name="sesskey" value="' . $USER->sesskey . '" />';
297                 echo '<table>';
298                 switch ($feedback->anonymous) {
299                     case FEEDBACK_ANONYMOUS_YES:
300                         echo '<tr><th colspan="3" align="center"><input type="hidden" name="anonymous" value="1" /><input type="hidden" name="anonymous_response" value="'.FEEDBACK_ANONYMOUS_YES.'" />'.get_string('mode', 'feedback').': '.get_string('anonymous', 'feedback').'</th></tr>'; //arb
301                         break;
302                     case FEEDBACK_ANONYMOUS_NO:
303                         echo '<tr><th colspan="3" align="center"><input type="hidden" name="anonymous" value="0" /><input type="hidden" name="anonymous_response" value="'.FEEDBACK_ANONYMOUS_NO.'" />'.get_string('mode', 'feedback').': '.get_string('non_anonymous', 'feedback').'&nbsp;</td></tr>'; //arb
304                         break;
305                 }
306                 //check, if there exists required-elements
307                 $countreq = count_records('feedback_item', 'feedback', $feedback->id, 'required', 1);
308                 if($countreq > 0) {
309                     echo '<tr><td colspan="3"><font color="red">(*)' . get_string('items_are_required', 'feedback') . '</font></td></tr>';
310                 }
311                 
312                 unset($startitem);
313                 $itemnr = count_records_select('feedback_item', 'feedback = '. $feedback->id . ' AND hasvalue = 1 AND position < '.$startposition);
314                 foreach($feedbackitems as $feedbackitem){
315                     if(!isset($startitem)) {
316                         //avoid showing double pagebreaks
317                         if($feedbackitem->typ == 'pagebreak') continue;
318                         $startitem = $feedbackitem;
319                     }
320                     $value = '';
321                     //get the value
322                     $frmvaluename = $feedbackitem->typ . '_'. $feedbackitem->id;
323                     if(isset($savereturn)) {
324                         $value =  isset($formdata->{$frmvaluename})?$formdata->{$frmvaluename}:NULL;
325                     }else {
326                         if(isset($feedbackcompletedtmp->id)) {
327                             $value = feedback_get_item_value($feedbackcompletedtmp->id, $feedbackitem->id, true);
328                         }
329                     }
330                     echo '<tr>';
331                     if($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
332                         $itemnr++;
333                         echo '<td valign="top">' . $itemnr . '.&nbsp;</td>';
334                     } else {
335                         echo '<td>&nbsp;</td>';
336                     }
337                     if($feedbackitem->typ != 'pagebreak') {
338                         feedback_print_item($feedbackitem, $value, false, false, $highlightrequired);
339                     }
340                     echo '</tr>';
341                     echo '<tr><td>&nbsp;</td></tr>';
342                     
343                     $lastbreakposition = $feedbackitem->position; //last item-pos (item or pagebreak)
344                     if($feedbackitem->typ == 'pagebreak'){
345                         break;
346                     }else {
347                         $lastitem = $feedbackitem;
348                     }
349                 }
350                 echo '</table>';
351                 echo '<input type="hidden" name="id" value="'.$id.'" />';
352                 echo '<input type="hidden" name="feedbackid" value="'.$feedback->id.'" />';
353                 echo '<input type="hidden" name="lastpage" value="'.$gopage.'" />';
354                 echo '<input type="hidden" name="completedid" value="'.(isset($feedbackcompletedtmp->id)?$feedbackcompletedtmp->id:'').'" />';
355                 echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
356                 echo '<input type="hidden" name="preservevalues" value="1" />';
357                 if(isset($startitem)) {
358                     echo '<input type="hidden" name="startitempos" value="'. $startitem->position . '" />';
359                     echo '<input type="hidden" name="lastitempos" value="'. $lastitem->position . '" />';
360                 }
361                 
362                 if( $ispagebreak AND $lastbreakposition > $firstpagebreak->position) {
363                     echo '<input name="gopreviouspage" type="submit" value="'.get_string('previous_page','feedback').'" />';
364                 }
365                 if($lastbreakposition < $maxitemcount){
366                     echo '<input name="gonextpage" type="submit" value="'.get_string('next_page','feedback').'" />';
367                 }
368                 if($lastbreakposition >= $maxitemcount) { //last page
369                     echo '<input name="savevalues" type="submit" value="'.get_string('save_entries','feedback').'" />';
370                 }
371                 
372                 echo '</form>';
373                 
374                     if($courseid) {
375                         echo '<form name="frm" action="'.$CFG->wwwroot.'/course/view.php?id='.$courseid.'" method="post" onsubmit=" ">';
376                     }else{
377                         if($course->id == SITEID) {
378                             echo '<form name="frm" action="'.$CFG->wwwroot.'" method="post" onsubmit=" ">';
379                         } else {
380                             echo '<form name="frm" action="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'" method="post" onsubmit=" ">';
381                         }
382                     }
383                 echo '<input type="hidden" name="sesskey" value="' . $USER->sesskey . '" />';
384                 echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
385                 echo '<button type="submit">'.get_string('cancel').'</button>';
386                 echo '</form>';
387                 echo '</div>';
388                 $SESSION->feedback->is_started = true;
389                 // print_simple_box_end();
390                 print_box_end();
391             }
392         }
393     }else {
394         // print_simple_box_start('center');
395         print_box_start('generalbox boxaligncenter');
396             echo '<h2><font color="red">'.get_string('this_feedback_is_already_submitted', 'feedback').'</font></h2>';
397             print_continue($CFG->wwwroot.'/course/view.php?id='.$course->id);
398         // print_simple_box_end();
399         print_box_end();
400     }
401     /// Finish the page
402     ///////////////////////////////////////////////////////////////////////////
403     ///////////////////////////////////////////////////////////////////////////
404     ///////////////////////////////////////////////////////////////////////////
406     print_footer($course);
408 ?>