62e91bf22f949e2ad4b548e211cd1157e709863f
[moodle.git] / mod / feedback / show_entries.php
1 <?php // $Id$
2 /**
3 * print the single entries
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");
13     
14     ////////////////////////////////////////////////////////
15     //get the params
16     ////////////////////////////////////////////////////////
17     $id = required_param('id', PARAM_INT);
18     $userid = optional_param('userid', false, PARAM_INT);
19     $lstgroupid = optional_param('lstgroupid', -2, PARAM_INT); //groupid (choosen from dropdownlist)
20     $do_show = required_param('do_show', PARAM_ALPHA);
21     // $SESSION->feedback->current_tab = $do_show;
22     $current_tab = $do_show;
24     //check, whether a group is selected
25     if($lstgroupid == -1) {
26         $SESSION->feedback->lstgroupid = false;
27     }else {
28         if((!isset($SESSION->feedback->lstgroupid)) || $lstgroupid != -2)
29             $SESSION->feedback->lstgroupid = $lstgroupid;
30     }
31     
32     ////////////////////////////////////////////////////////
33     //get the objects
34     ////////////////////////////////////////////////////////
35     
36     if($userid) {
37         $formdata->userid = intval($userid);
38     }
40     if ($id) {
41         if (! $cm = get_coursemodule_from_id('feedback', $id)) {
42             error("Course Module ID was incorrect");
43         }
44      
45         if (! $course = get_record("course", "id", $cm->course)) {
46             error("Course is misconfigured");
47         }
48      
49         if (! $feedback = get_record("feedback", "id", $cm->instance)) {
50             error("Course module is incorrect");
51         }
52     }
53     
54     if(isset($SESSION->feedback->lstgroupid)) {
55         if($tmpgroup = groups_get_group($SESSION->feedback->lstgroupid)) {
56             if($tmpgroup->courseid != $course->id) {
57                 $SESSION->feedback->lstgroupid = false;
58             }
59         }else {
60             $SESSION->feedback->lstgroupid = false;
61         }
62     }
63     $capabilities = feedback_load_capabilities($cm->id);
65     require_login($course->id, true, $cm);
66     
67     if(($formdata = data_submitted('nomatch')) AND !confirm_sesskey()) {
68         error('no sesskey defined');
69     }
70     
71     if(!$capabilities->viewreports){
72         error(get_string('error'));
73     }
75     ////////////////////////////////////////////////////////
76     //get the responses of given user
77     ////////////////////////////////////////////////////////
78     if($do_show == 'showoneentry') {
79         //get the feedbackitems
80         $feedbackitems = get_records('feedback_item', 'feedback', $feedback->id, 'position');
81         $feedbackcompleted = get_record_select('feedback_completed','feedback='.$feedback->id.' AND userid='.$formdata->userid.' AND anonymous_response='.FEEDBACK_ANONYMOUS_NO); //arb
82     }
83     
84     /// Print the page header
85     $strfeedbacks = get_string("modulenameplural", "feedback");
86     $strfeedback  = get_string("modulename", "feedback");
87     $buttontext = update_module_button($cm->id, $course->id, $strfeedback);
88     
89     $navlinks = array();
90     $navlinks[] = array('name' => $strfeedbacks, 'link' => "index.php?id=$course->id", 'type' => 'activity');
91     $navlinks[] = array('name' => format_string($feedback->name), 'link' => "", 'type' => 'activityinstance');
92     
93     $navigation = build_navigation($navlinks);
94     
95     print_header_simple(format_string($feedback->name), "",
96                  $navigation, "", "", true, $buttontext, navmenu($course, $cm));
97                  
98     include('tabs.php');
100     /// Print the main part of the page
101     ///////////////////////////////////////////////////////////////////////////
102     ///////////////////////////////////////////////////////////////////////////
103     ///////////////////////////////////////////////////////////////////////////
105     ////////////////////////////////////////////////////////
106     /// Print the links to get responses and analysis
107     ////////////////////////////////////////////////////////
108     if($do_show == 'showentries'){
109         //print the link to analysis
110         if($capabilities->viewreports) {
111             //get the effective groupmode of this course and module
112             $groupmode = groupmode($course, $cm);
113             
114             //get students in conjunction with groupmode
115             if($groupmode > 0) {
116                 if($SESSION->feedback->lstgroupid == -2) {
117                     if(has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
118                         $mygroupid = false;
119                         $SESSION->feedback->lstgroupid = false;
120                     }else{
121                         if($mygroupid = mygroupid($course->id)) {
122                             $mygroupid = $mygroupid[0]; //get the first groupid
123                         }
124                     }
125                 }else {
126                     $mygroupid = $SESSION->feedback->lstgroupid;
127                 }
128                 if($mygroupid) {
129                     $students = feedback_get_complete_users($cm->id, $mygroupid);
130                 } else {
131                     $students = feedback_get_complete_users($cm->id);
132                 }
133             }else {
134                 $students = feedback_get_complete_users($cm->id);
135             }
137             $mygroupid=isset($mygroupid)?$mygroupid:NULL;
139             $completedFeedbackCount = feedback_get_completeds_group_count($feedback, $mygroupid);
140             if($feedback->course == SITEID){
141                 echo '<div align="center"><a href="'.htmlspecialchars('analysis_course.php?id=' . $id . '&courseid='.$courseid).'">';
142                 echo get_string('course') .' '. get_string('analysis', 'feedback') . ' ('.get_string('completed_feedbacks', 'feedback').': '.intval($completedFeedbackCount).')</a>';
143                 helpbutton('viewcompleted', '', 'feedback', true, true);
144                 echo '</div>';
145             }else {
146                 echo '<div align="center"><a href="'.htmlspecialchars('analysis.php?id=' . $id . '&courseid='.$courseid).'">';
147                 echo get_string('analysis', 'feedback') . ' ('.get_string('completed_feedbacks', 'feedback').': '.intval($completedFeedbackCount).')</a>';
148                 echo '</div>';
149             }
150         }
151     
152         //####### viewreports-start
153         if($capabilities->viewreports) {
154             //print the list of students
155             // print_simple_box_start('center', '80%');
156             print_box_start('generalbox boxaligncenter boxwidthwide');
158             //available group modes (NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS)
159             $feedbackgroups = groups_get_all_groups($course->id);
160             //if(is_array($feedbackgroups) && $groupmode != SEPARATEGROUPS){
161             if(is_array($feedbackgroups) && $groupmode > 0){
162                 require_once('choose_group_form.php');
163                 //the use_template-form
164                 $choose_group_form = new feedback_choose_group_form();
165                 $choose_group_form->set_feedbackdata(array('groups'=>$feedbackgroups, 'mygroupid'=>$mygroupid));
166                 $choose_group_form->set_form_elements();
167                 $choose_group_form->set_data(array('id'=>$id, 'lstgroupid'=>$SESSION->feedback->lstgroupid, 'do_show'=>$do_show));
168                 $choose_group_form->display();
169             }
170             echo '<div align="center"><table><tr><td width="400">';
171             if (!$students) {
172                 if($courseid != SITEID){
173                     notify(get_string('noexistingstudents'));
174                 }
175             } else{
176                 echo print_string('non_anonymous_entries', 'feedback');
177                 echo ' ('.count_records_select('feedback_completed', 'feedback = ' . $feedback->id.' AND anonymous_response='.FEEDBACK_ANONYMOUS_NO).')<hr />';
179                 foreach ($students as $student){
180                     $completedCount = count_records_select('feedback_completed', 'userid = ' . $student->id . ' AND feedback = ' . $feedback->id.' AND anonymous_response='.FEEDBACK_ANONYMOUS_NO);
181                     if($completedCount > 0) {
182                      // Are we assuming that there is only one response per user? Should westep through a feedbackcompleteds? I added the addition anonymous check to the select so that only non-anonymous submissions are retrieved. 
183                         $feedbackcompleted = get_record_select('feedback_completed','feedback='.$feedback->id.' AND userid='.$student->id.' AND anonymous_response='.FEEDBACK_ANONYMOUS_NO);
184                     ?>
185                         <table width="100%">
186                             <tr>
187                                 <td align="left">
188                                     <?php echo print_user_picture($student->id, $course->id, $student->picture, false, true);?>
189                                 </td>
190                                 <td align="left">
191                                     <?php echo fullname($student);?>
192                                 </td>
193                                 <td align="right">
194                                 <?php
195                                     $show_button_link = $ME;
196                                     $show_button_options = array('sesskey'=>$USER->sesskey, 'userid'=>$student->id, 'do_show'=>'showoneentry', 'id'=>$id);
197                                     $show_button_label = get_string('show_entries', 'feedback');
198                                     print_single_button($show_button_link, $show_button_options, $show_button_label, 'post');
199                                 ?>
200                                 </td>
201                     <?php
202                         if($capabilities->deletesubmissions) {
203                     ?>
204                                 <td align="right">
205                                 <?php
206                                     $delete_button_link = 'delete_completed.php';
207                                     $delete_button_options = array('sesskey'=>$USER->sesskey, 'completedid'=>$feedbackcompleted->id, 'do_show'=>'showoneentry', 'id'=>$id);
208                                     $delete_button_label = get_string('delete_entry', 'feedback');
209                                     print_single_button($delete_button_link, $delete_button_options, $delete_button_label, 'post');
210                                 ?>
211                                 </td>
212                     <?php
213                         }
214                     ?>
215                             </tr>
216                         </table>
217                     <?php
218                     }
219                 }
220             }
221     ?>
222             <hr />
223             <table width="100%">
224                 <tr>
225                     <td align="left" colspan="2">
226                         <?php print_string('anonymous_entries', 'feedback');?>&nbsp;(<?php echo count_records_select('feedback_completed', 'feedback = ' . $feedback->id.' AND anonymous_response='.FEEDBACK_ANONYMOUS_YES);?>)
227                     </td>
228                     <td align="right">
229                         <?php
230                             $show_anon_button_link = 'show_entries_anonym.php';
231                             $show_anon_button_options = array('sesskey'=>$USER->sesskey, 'userid'=>0, 'do_show'=>'showoneentry', 'id'=>$id);
232                             $show_anon_button_label = get_string('show_entries', 'feedback');
233                             print_single_button($show_anon_button_link, $show_anon_button_options, $show_anon_button_label, 'post');
234                         ?>
235                     </td>
236                 </tr>
237             </table> 
238     <?php
239             echo '</td></tr></table></div>';
240             // print_simple_box_end();
241             print_box_end();
242         }
243     
244     }
245     ////////////////////////////////////////////////////////
246     /// Print the responses of the given user
247     ////////////////////////////////////////////////////////
248     if($do_show == 'showoneentry') {
249         print_heading(format_text($feedback->name));
250         
251         //print the items
252         if(is_array($feedbackitems)){
253             $usr = get_record('user', 'id', $formdata->userid);
254             if($feedbackcompleted) {
255                 echo '<p align="center">'.UserDate($feedbackcompleted->timemodified).'<br />('.fullname($usr).')</p>';
256             } else {
257                 echo '<p align="center">'.get_string('not_completed_yet','feedback').'</p>';
258             }
259             // print_simple_box_start("center", '50%');
260             print_box_start('generalbox boxaligncenter boxwidthnormal');
261             echo '<form>';
262             echo '<input type="hidden" name="sesskey" value="' . $USER->sesskey . '" />';
263             echo '<table width="100%">';
264             $itemnr = 0;
265             foreach($feedbackitems as $feedbackitem){
266                 //get the values
267                 $value = get_record_select('feedback_value','completed ='.$feedbackcompleted->id.' AND item='.$feedbackitem->id);
268                 echo '<tr>';
269                 if($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
270                     $itemnr++;
271                     echo '<td valign="top">' . $itemnr . '.&nbsp;</td>';
272                 } else {
273                     echo '<td>&nbsp;</td>';
274                 }
275                 
276                 if($feedbackitem->typ != 'pagebreak') {
277                     if(isset($value->value)) {
278                         feedback_print_item($feedbackitem, $value->value, true);
279                     }else {
280                         feedback_print_item($feedbackitem, false, true);
281                     }
282                 }else {
283                     echo '<td><hr /></td>';
284                 }
285                 echo '</tr>';
286             }
287             echo '<tr><td colspan="2" align="center">';
288             echo '</td></tr>';
289             echo '</table>';
290             echo '</form>';
291             // print_simple_box_end();
292             print_box_end();
293         }
294         print_continue(htmlspecialchars('show_entries.php?id='.$id.'&do_show=showentries'));
295     }
296     /// Finish the page
297     ///////////////////////////////////////////////////////////////////////////
298     ///////////////////////////////////////////////////////////////////////////
299     ///////////////////////////////////////////////////////////////////////////
301     print_footer($course);
303 ?>