MDL-14522 - new capability "receivemail" for better control who gets the email after
[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                 $multiple_count = null;
198                 $multiple_count->userid = $USER->id;
199                 $multiple_count->feedback = $feedback->id;
200                 $multiple_count->completed = $new_completed_id;
201                 $multiple_count->count = 1;
202                 insert_record('feedback_tracking', $multiple_count);
203                 unset($SESSION->feedback->is_started);
204                 
205             }else {
206                 $savereturn = 'failed';
207             }
208            
209         }
212         if($allbreaks = feedback_get_all_break_positions($feedback->id)){
213             if($gopage <= 0) {
214                 $startposition = 0;
215             }else {
216                 $startposition = $allbreaks[$gopage - 1];
217             }
218             $ispagebreak = true;
219         }else {
220             $startposition = 0;
221             $newpage = 0;
222             $ispagebreak = false;
223         }
224         
225         //get the feedbackitems after the last shown pagebreak
226         $feedbackitems = get_records_select('feedback_item', 'feedback = '.$feedback->id.' AND position > '.$startposition, 'position');
227         
228         //get the first pagebreak
229         if($pagebreaks = get_records_select('feedback_item', "feedback = ".$feedback->id." AND typ = 'pagebreak'", 'position')) {
230             $pagebreaks = array_values($pagebreaks);
231             $firstpagebreak = $pagebreaks[0];
232         }else {
233             $firstpagebreak = false;
234         }
235         $maxitemcount = count_records('feedback_item', 'feedback', $feedback->id);
236         
237         //get the values of completeds before done. Anonymous user can not get these values.
238         if((!isset($SESSION->feedback->is_started)) AND (!isset($savereturn)) AND ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO)) {
239             if(!$feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid)) {
240                 if($feedbackcompleted = feedback_get_current_completed($feedback->id, false, $courseid)) {
241                     //copy the values to feedback_valuetmp create a completedtmp
242                     $feedbackcompletedtmp = feedback_set_tmp_values($feedbackcompleted);
243                 }
244             }
245         }else {
246             $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid);
247         }
249         /// Print the main part of the page
250         ///////////////////////////////////////////////////////////////////////////
251         ///////////////////////////////////////////////////////////////////////////
252         ///////////////////////////////////////////////////////////////////////////
253         print_heading(format_text($feedback->name));
254         
255         if(isset($savereturn) && $savereturn == 'saved') {
256             if($feedback->page_after_submit) {
257                 // print_simple_box_start('center', '75%');
258                 print_box_start('generalbox boxaligncenter boxwidthwide');
259                 echo format_text(stripslashes_safe($feedback->page_after_submit));
260                 // print_simple_box_end();
261                 print_box_end();
262             } else {
263                 echo '<p align="center"><b><font color="green">'.get_string('entries_saved','feedback').'</font></b></p>';
264                 if( intval($feedback->publish_stats) == 1) {
265                     echo '<p align="center"><a href="analysis.php?id=' . $id . '&courseid='.$courseid.'">';
266                     echo get_string('completed_feedbacks', 'feedback').'</a>';
267                     echo '</p>';
268                 }
269             }
270             if($feedback->site_after_submit) {
271                 print_continue(feedback_encode_target_url($feedback->site_after_submit));
272             }else {
273                 if($courseid) {
274                     print_continue($CFG->wwwroot.'/course/view.php?id='.$courseid);
275                 }else {
276                     if($course->id == SITEID) {
277                         print_continue($CFG->wwwroot);
278                     } else {
279                         print_continue($CFG->wwwroot.'/course/view.php?id='.$course->id);
280                     }
281                 }
282             }
283         }else {
284             if(isset($savereturn) && $savereturn == 'failed') {
285                 echo '<p align="center"><b><font color="red">'.get_string('saving_failed','feedback').'</font></b></p>';
286             }
287     
288             if(isset($savereturn) && $savereturn == 'missing') {
289                 echo '<p align="center"><b><font color="red">'.get_string('saving_failed_because_missing_or_false_values','feedback').'</font></b></p>';
290             }
291     
292             //print the items
293             if(is_array($feedbackitems)){
294                 // print_simple_box_start('center', '75%');
295                 print_box_start('generalbox boxaligncenter boxwidthwide');
296                 echo '<div align="center"><form name="frm" action="'.$ME.'" method="post" onsubmit=" ">';
297                 echo '<input type="hidden" name="sesskey" value="' . $USER->sesskey . '" />';
298                 echo '<table>';
299                 switch ($feedback->anonymous) {
300                     case FEEDBACK_ANONYMOUS_YES:
301                         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
302                         break;
303                     case FEEDBACK_ANONYMOUS_NO:
304                         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
305                         break;
306                 }
307                 //check, if there exists required-elements
308                 $countreq = count_records('feedback_item', 'feedback', $feedback->id, 'required', 1);
309                 if($countreq > 0) {
310                     echo '<tr><td colspan="3"><font color="red">(*)' . get_string('items_are_required', 'feedback') . '</font></td></tr>';
311                 }
312                 
313                 unset($startitem);
314                 $itemnr = count_records_select('feedback_item', 'feedback = '. $feedback->id . ' AND hasvalue = 1 AND position < '.$startposition);
315                 foreach($feedbackitems as $feedbackitem){
316                     if(!isset($startitem)) {
317                         //avoid showing double pagebreaks
318                         if($feedbackitem->typ == 'pagebreak') continue;
319                         $startitem = $feedbackitem;
320                     }
321                     $value = '';
322                     //get the value
323                     $frmvaluename = $feedbackitem->typ . '_'. $feedbackitem->id;
324                     if(isset($savereturn)) {
325                         $value =  isset($formdata->{$frmvaluename})?$formdata->{$frmvaluename}:NULL;
326                     }else {
327                         if(isset($feedbackcompletedtmp->id)) {
328                             $value = feedback_get_item_value($feedbackcompletedtmp->id, $feedbackitem->id, true);
329                         }
330                     }
331                     echo '<tr>';
332                     if($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
333                         $itemnr++;
334                         echo '<td valign="top">' . $itemnr . '.&nbsp;</td>';
335                     } else {
336                         echo '<td>&nbsp;</td>';
337                     }
338                     if($feedbackitem->typ != 'pagebreak') {
339                         feedback_print_item($feedbackitem, $value, false, false, $highlightrequired);
340                     }
341                     echo '</tr>';
342                     echo '<tr><td>&nbsp;</td></tr>';
343                     
344                     $lastbreakposition = $feedbackitem->position; //last item-pos (item or pagebreak)
345                     if($feedbackitem->typ == 'pagebreak'){
346                         break;
347                     }else {
348                         $lastitem = $feedbackitem;
349                     }
350                 }
351                 echo '</table>';
352                 echo '<input type="hidden" name="id" value="'.$id.'" />';
353                 echo '<input type="hidden" name="feedbackid" value="'.$feedback->id.'" />';
354                 echo '<input type="hidden" name="lastpage" value="'.$gopage.'" />';
355                 echo '<input type="hidden" name="completedid" value="'.(isset($feedbackcompletedtmp->id)?$feedbackcompletedtmp->id:'').'" />';
356                 echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
357                 echo '<input type="hidden" name="preservevalues" value="1" />';
358                 if(isset($startitem)) {
359                     echo '<input type="hidden" name="startitempos" value="'. $startitem->position . '" />';
360                     echo '<input type="hidden" name="lastitempos" value="'. $lastitem->position . '" />';
361                 }
362                 
363                 if( $ispagebreak AND $lastbreakposition > $firstpagebreak->position) {
364                     echo '<input name="gopreviouspage" type="submit" value="'.get_string('previous_page','feedback').'" />';
365                 }
366                 if($lastbreakposition < $maxitemcount){
367                     echo '<input name="gonextpage" type="submit" value="'.get_string('next_page','feedback').'" />';
368                 }
369                 if($lastbreakposition >= $maxitemcount) { //last page
370                     echo '<input name="savevalues" type="submit" value="'.get_string('save_entries','feedback').'" />';
371                 }
372                 
373                 echo '</form>';
374                 
375                     if($courseid) {
376                         echo '<form name="frm" action="'.$CFG->wwwroot.'/course/view.php?id='.$courseid.'" method="post" onsubmit=" ">';
377                     }else{
378                         if($course->id == SITEID) {
379                             echo '<form name="frm" action="'.$CFG->wwwroot.'" method="post" onsubmit=" ">';
380                         } else {
381                             echo '<form name="frm" action="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'" method="post" onsubmit=" ">';
382                         }
383                     }
384                 echo '<input type="hidden" name="sesskey" value="' . $USER->sesskey . '" />';
385                 echo '<input type="hidden" name="courseid" value="'. $courseid . '" />';
386                 echo '<button type="submit">'.get_string('cancel').'</button>';
387                 echo '</form>';
388                 echo '</div>';
389                 $SESSION->feedback->is_started = true;
390                 // print_simple_box_end();
391                 print_box_end();
392             }
393         }
394     }else {
395         // print_simple_box_start('center');
396         print_box_start('generalbox boxaligncenter');
397             echo '<h2><font color="red">'.get_string('this_feedback_is_already_submitted', 'feedback').'</font></h2>';
398             print_continue($CFG->wwwroot.'/course/view.php?id='.$course->id);
399         // print_simple_box_end();
400         print_box_end();
401     }
402     /// Finish the page
403     ///////////////////////////////////////////////////////////////////////////
404     ///////////////////////////////////////////////////////////////////////////
405     ///////////////////////////////////////////////////////////////////////////
407     print_footer($course);
409 ?>