429f374a419c87d49b24336e025ecaee82258687
[moodle.git] / mod / feedback / show_entries.php
1 <?php
3 /**
4  * print the single entries
5  *
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 require_once($CFG->libdir.'/tablelib.php');
15 ////////////////////////////////////////////////////////
16 //get the params
17 ////////////////////////////////////////////////////////
18 $id = required_param('id', PARAM_INT);
19 $userid = optional_param('userid', false, PARAM_INT);
20 $do_show = required_param('do_show', PARAM_ALPHA);
21 $perpage = optional_param('perpage', FEEDBACK_DEFAULT_PAGE_COUNT, PARAM_INT);  // how many per page
22 $showall = optional_param('showall', false, PARAM_INT);  // should we show all users
23 // $SESSION->feedback->current_tab = $do_show;
24 $current_tab = $do_show;
26 ////////////////////////////////////////////////////////
27 //get the objects
28 ////////////////////////////////////////////////////////
30 if($userid) {
31     $formdata->userid = intval($userid);
32 }
34 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
35     print_error('invalidcoursemodule');
36 }
38 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
39     print_error('coursemisconf');
40 }
42 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
43     print_error('invalidcoursemodule');
44 }
46 $url = new moodle_url('/mod/feedback/show_entries.php', array('id'=>$cm->id, 'do_show'=>$do_show));
48 $PAGE->set_url($url);
50 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
51         print_error('badcontext');
52 }
54 require_login($course->id, true, $cm);
56 if(($formdata = data_submitted()) AND !confirm_sesskey()) {
57     print_error('invalidsesskey');
58 }
60 require_capability('mod/feedback:viewreports', $context);
62 ////////////////////////////////////////////////////////
63 //get the responses of given user
64 ////////////////////////////////////////////////////////
65 if($do_show == 'showoneentry') {
66     //get the feedbackitems
67     $feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id), 'position');
68     $feedbackcompleted = $DB->get_record('feedback_completed', array('feedback'=>$feedback->id, 'userid'=>$userid, 'anonymous_response'=>FEEDBACK_ANONYMOUS_NO)); //arb
69 }
71 /// Print the page header
72 $strfeedbacks = get_string("modulenameplural", "feedback");
73 $strfeedback  = get_string("modulename", "feedback");
75 $PAGE->set_heading(format_string($course->fullname));
76 $PAGE->set_title(format_string($feedback->name));
77 echo $OUTPUT->header();
79 include('tabs.php');
81 /// Print the main part of the page
82 ///////////////////////////////////////////////////////////////////////////
83 ///////////////////////////////////////////////////////////////////////////
84 ///////////////////////////////////////////////////////////////////////////
86 ////////////////////////////////////////////////////////
87 /// Print the links to get responses and analysis
88 ////////////////////////////////////////////////////////
89 if($do_show == 'showentries'){
90     //print the link to analysis
91     if(has_capability('mod/feedback:viewreports', $context)) {
92         //get the effective groupmode of this course and module
93         if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
94             $groupmode =  $cm->groupmode;
95         } else {
96             $groupmode = $course->groupmode;
97         }
99         // $groupselect = groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/feedback/show_entries.php?id=' . $cm->id.'&do_show=showentries', true);
100         $groupselect = groups_print_activity_menu($cm, $url->out(), true);
101         $mygroupid = groups_get_activity_group($cm);
103         // preparing the table for output
104         $baseurl = new moodle_url('/mod/feedback/show_entries.php');
105         $baseurl->params(array('id'=>$id, 'do_show'=>$do_show, 'showall'=>$showall));
107         $tablecolumns = array('userpic', 'fullname', 'timemodified');
108         $tableheaders = array(get_string('userpic'), get_string('fullnameuser'), get_string('date'));
110         if(has_capability('mod/feedback:deletesubmissions', $context)) {
111             $tablecolumns[] = 'deleteentry';
112             $tableheaders[] = '';
113         }
115         $table = new flexible_table('feedback-showentry-list-'.$course->id);
117         $table->define_columns($tablecolumns);
118         $table->define_headers($tableheaders);
119         $table->define_baseurl($baseurl);
121         $table->sortable(true, 'lastname', SORT_DESC);
122         $table->set_attribute('cellspacing', '0');
123         $table->set_attribute('id', 'showentrytable');
124         $table->set_attribute('class', 'generaltable generalbox');
125         $table->set_control_variables(array(
126                     TABLE_VAR_SORT    => 'ssort',
127                     TABLE_VAR_IFIRST  => 'sifirst',
128                     TABLE_VAR_ILAST   => 'silast',
129                     TABLE_VAR_PAGE    => 'spage'
130                     ));
131         $table->setup();
133         if($table->get_sql_sort()) {
134             $sort = $table->get_sql_sort();
135         }else {
136             $sort = '';
137         }
139         list($where, $params) = $table->get_sql_where();
140         if ($where) {
141             $where .= ' AND';
142         }
144         //get students in conjunction with groupmode
145         if($groupmode > 0) {
146             if($mygroupid > 0) {
147                 $usedgroupid = $mygroupid;
148             } else {
149                 $usedgroupid = false;
150             }
151         }else {
152             $usedgroupid = false;
153         }
155         $matchcount = feedback_count_complete_users($cm, $usedgroupid);
156         $table->initialbars(true);
158         if($showall) {
159             $startpage = false;
160             $pagecount = false;
161         }else {
162             $table->pagesize($perpage, $matchcount);
163             $startpage = $table->get_page_start();
164             $pagecount = $table->get_page_size();
165         }
167         $students = feedback_get_complete_users($cm, $usedgroupid, $where, $params, $sort, $startpage, $pagecount);
169         $completedFeedbackCount = feedback_get_completeds_group_count($feedback, $mygroupid);
170         if($feedback->course == SITEID){
171             $analysisurl = new moodle_url('/mod/feedback/analysis_course.php', array('id'=>$id, 'courseid'=>$courseid));
172             echo $OUTPUT->box_start('mdl-align');
173             echo '<a href="'.$analysisurl->out().'">'.get_string('course').' '.get_string('analysis', 'feedback').' ('.get_string('completed_feedbacks', 'feedback').': '.intval($completedFeedbackCount).')</a>';
174             echo $OUTPUT->help_icon('viewcompleted', 'feedback');
175             echo $OUTPUT->box_end();
176         }else {
177             $analysisurl = new moodle_url('/mod/feedback/analysis.php', array('id'=>$id, 'courseid'=>$courseid));
178             echo $OUTPUT->box_start('mdl-align');
179             echo '<a href="'.$analysisurl->out().'">'.get_string('analysis', 'feedback').' ('.get_string('completed_feedbacks', 'feedback').': '.intval($completedFeedbackCount).')</a>';
180             echo $OUTPUT->box_end();
181         }
182     }
184     //####### viewreports-start
185     if(has_capability('mod/feedback:viewreports', $context)) {
186         //print the list of students
187         echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
188         echo isset($groupselect) ? $groupselect : '';
189         echo '<div class="clearer"></div>';
190         echo $OUTPUT->box_start('mdl-align');
191         // echo '<table><tr><td width="400">';
192         if (!$students) {
193             if($courseid != SITEID){
194                 echo $OUTPUT->notification(get_string('noexistingparticipants', 'enrol'));
195             }
196         } else{
197             echo print_string('non_anonymous_entries', 'feedback');
198             echo ' ('.count($students).')<hr />';
200             foreach ($students as $student){
201                 $completedCount = $DB->count_records('feedback_completed', array('userid'=>$student->id, 'feedback'=>$feedback->id, 'anonymous_response'=>FEEDBACK_ANONYMOUS_NO));
202                 if($completedCount > 0) {
204                     //userpicture and link to the profilepage
205                     $profilelink = '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$student->id.'&amp;course='.$course->id.'">'.fullname($student).'</a></strong>';
206                     $data = array ($OUTPUT->user_picture($student, array('courseid'=>$course->id)), $profilelink);
208                     //link to the entry of the user
209                     $feedbackcompleted = $DB->get_record('feedback_completed', array('feedback'=>$feedback->id, 'userid'=>$student->id, 'anonymous_response'=>FEEDBACK_ANONYMOUS_NO));
210                     $showentryurl = new moodle_url($url, array('userid'=>$student->id, 'do_show'=>'showoneentry'));
211                     $showentrylink = '<a href="'.$showentryurl->out().'">'.UserDate($feedbackcompleted->timemodified).'</a>';
212                     $data[] = $showentrylink;
214                     //link to delete the entry
215                     if(has_capability('mod/feedback:deletesubmissions', $context)) {
216                         $deleteentryurl = new moodle_url($CFG->wwwroot.'/mod/feedback/delete_completed.php', array('id'=>$cm->id, 'completedid'=>$feedbackcompleted->id, 'do_show'=>'showoneentry'));
217                         $deleteentrylink = '<a href="'.$deleteentryurl->out().'">'.get_string('delete_entry', 'feedback').'</a>';
218                         $data[] = $deleteentrylink;
219                     }
220                     $table->add_data($data);
221                 }
222             }
223             $table->print_html();
225             $allurl = new moodle_url($baseurl);
227             if ($showall) {
228                 $allurl->param('showall', 0);
229                 echo $OUTPUT->container(html_writer::link($allurl, get_string('showperpage', '', FEEDBACK_DEFAULT_PAGE_COUNT)), array(), 'showall');
230             } else if ($matchcount > 0 && $perpage < $matchcount) {
231                 $allurl->param('showall', 1);
232                 echo $OUTPUT->container(html_writer::link($allurl, get_string('showall', '', $matchcount)), array(), 'showall');
233             }
234         }
235 ?>
236         <hr />
237         <table width="100%">
238             <tr>
239                 <td align="left" colspan="2">
240                     <?php print_string('anonymous_entries', 'feedback');?>&nbsp;(<?php echo $DB->count_records('feedback_completed', array('feedback'=>$feedback->id, 'anonymous_response'=>FEEDBACK_ANONYMOUS_YES));?>)
241                 </td>
242                 <td align="right">
243                     <?php
244                         $aurl = new moodle_url('show_entries_anonym.php', array('sesskey'=>sesskey(), 'userid'=>0, 'do_show'=>'showoneentry', 'id'=>$id));
245                         echo $OUTPUT->single_button($aurl, get_string('show_entries', 'feedback'));
246                     ?>
247                 </td>
248             </tr>
249         </table>
250 <?php
251         echo $OUTPUT->box_end();
252         echo $OUTPUT->box_end();
253     }
256 ////////////////////////////////////////////////////////
257 /// Print the responses of the given user
258 ////////////////////////////////////////////////////////
259 if($do_show == 'showoneentry') {
260     echo $OUTPUT->heading(format_text($feedback->name));
262     //print the items
263     if(is_array($feedbackitems)){
264         $align = right_to_left() ? 'right' : 'left';
265         $usr = $DB->get_record('user', array('id'=>$userid));
267         if($feedbackcompleted) {
268             echo $OUTPUT->heading(UserDate($feedbackcompleted->timemodified).' ('.fullname($usr).')', 3);
269         } else {
270             echo $OUTPUT->heading(get_string('not_completed_yet','feedback'), 3);
271         }
273         echo $OUTPUT->box_start('feedback_items');
274         $itemnr = 0;
275         foreach($feedbackitems as $feedbackitem){
276             //get the values
277             $value = $DB->get_record('feedback_value', array('completed'=>$feedbackcompleted->id, 'item'=>$feedbackitem->id));
278             echo $OUTPUT->box_start('feedback_item_box_'.$align);
279             if($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
280                 $itemnr++;
281                 echo $OUTPUT->box_start('feedback_item_number_'.$align);
282                 echo $itemnr;
283                 echo $OUTPUT->box_end();
284             }
286             if($feedbackitem->typ != 'pagebreak') {
287                 echo $OUTPUT->box_start('box generalbox boxalign_'.$align);
288                 if(isset($value->value)) {
289                     feedback_print_item_show_value($feedbackitem, $value->value);
290                 }else {
291                     feedback_print_item_show_value($feedbackitem, false);
292                 }
293                 echo $OUTPUT->box_end();
294             }
295             echo $OUTPUT->box_end();
296         }
297         echo $OUTPUT->box_end();
298     }
299     echo $OUTPUT->continue_button(new moodle_url($url, array('do_show'=>'showentries')));
301 /// Finish the page
302 ///////////////////////////////////////////////////////////////////////////
303 ///////////////////////////////////////////////////////////////////////////
304 ///////////////////////////////////////////////////////////////////////////
306 echo $OUTPUT->footer();
308 ?>