Merge branch 'w18_MDL-32676_m23_yuibootstrap' of git://github.com/skodak/moodle
[moodle.git] / mod / feedback / show_entries.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  * print the single entries
19  *
20  * @author Andreas Grabs
21  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22  * @package feedback
23  */
25 require_once("../../config.php");
26 require_once("lib.php");
27 require_once($CFG->libdir.'/tablelib.php');
29 ////////////////////////////////////////////////////////
30 //get the params
31 ////////////////////////////////////////////////////////
32 $id = required_param('id', PARAM_INT);
33 $userid = optional_param('userid', false, PARAM_INT);
34 $do_show = required_param('do_show', PARAM_ALPHA);
35 $perpage = optional_param('perpage', FEEDBACK_DEFAULT_PAGE_COUNT, PARAM_INT);  // how many per page
36 $showall = optional_param('showall', false, PARAM_INT);  // should we show all users
37 // $SESSION->feedback->current_tab = $do_show;
38 $current_tab = $do_show;
40 ////////////////////////////////////////////////////////
41 //get the objects
42 ////////////////////////////////////////////////////////
44 if ($userid) {
45     $formdata->userid = intval($userid);
46 }
48 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
49     print_error('invalidcoursemodule');
50 }
52 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
53     print_error('coursemisconf');
54 }
56 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
57     print_error('invalidcoursemodule');
58 }
60 $url = new moodle_url('/mod/feedback/show_entries.php', array('id'=>$cm->id, 'do_show'=>$do_show));
62 $PAGE->set_url($url);
64 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
65         print_error('badcontext');
66 }
68 require_login($course, true, $cm);
70 if (($formdata = data_submitted()) AND !confirm_sesskey()) {
71     print_error('invalidsesskey');
72 }
74 require_capability('mod/feedback:viewreports', $context);
76 ////////////////////////////////////////////////////////
77 //get the responses of given user
78 ////////////////////////////////////////////////////////
79 if ($do_show == 'showoneentry') {
80     //get the feedbackitems
81     $feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id), 'position');
83     $params = array('feedback'=>$feedback->id,
84                     'userid'=>$userid,
85                     'anonymous_response'=>FEEDBACK_ANONYMOUS_NO);
87     $feedbackcompleted = $DB->get_record('feedback_completed', $params); //arb
88 }
90 /// Print the page header
91 $strfeedbacks = get_string("modulenameplural", "feedback");
92 $strfeedback  = get_string("modulename", "feedback");
94 $PAGE->set_heading(format_string($course->fullname));
95 $PAGE->set_title(format_string($feedback->name));
96 echo $OUTPUT->header();
98 require('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 (has_capability('mod/feedback:viewreports', $context)) {
111         //get the effective groupmode of this course and module
112         if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
113             $groupmode =  $cm->groupmode;
114         } else {
115             $groupmode = $course->groupmode;
116         }
118         $groupselect = groups_print_activity_menu($cm, $url->out(), true);
119         $mygroupid = groups_get_activity_group($cm);
121         // preparing the table for output
122         $baseurl = new moodle_url('/mod/feedback/show_entries.php');
123         $baseurl->params(array('id'=>$id, 'do_show'=>$do_show, 'showall'=>$showall));
125         $tablecolumns = array('userpic', 'fullname', 'completed_timemodified');
126         $tableheaders = array(get_string('userpic'), get_string('fullnameuser'), get_string('date'));
128         if (has_capability('mod/feedback:deletesubmissions', $context)) {
129             $tablecolumns[] = 'deleteentry';
130             $tableheaders[] = '';
131         }
133         $table = new flexible_table('feedback-showentry-list-'.$course->id);
135         $table->define_columns($tablecolumns);
136         $table->define_headers($tableheaders);
137         $table->define_baseurl($baseurl);
139         $table->sortable(true, 'lastname', SORT_DESC);
140         $table->set_attribute('cellspacing', '0');
141         $table->set_attribute('id', 'showentrytable');
142         $table->set_attribute('class', 'generaltable generalbox');
143         $table->set_control_variables(array(
144                     TABLE_VAR_SORT    => 'ssort',
145                     TABLE_VAR_IFIRST  => 'sifirst',
146                     TABLE_VAR_ILAST   => 'silast',
147                     TABLE_VAR_PAGE    => 'spage'
148                     ));
149         $table->setup();
151         if ($table->get_sql_sort()) {
152             $sort = $table->get_sql_sort();
153         } else {
154             $sort = '';
155         }
157         list($where, $params) = $table->get_sql_where();
158         if ($where) {
159             $where .= ' AND';
160         }
162         //get students in conjunction with groupmode
163         if ($groupmode > 0) {
164             if ($mygroupid > 0) {
165                 $usedgroupid = $mygroupid;
166             } else {
167                 $usedgroupid = false;
168             }
169         } else {
170             $usedgroupid = false;
171         }
173         $matchcount = feedback_count_complete_users($cm, $usedgroupid);
174         $table->initialbars(true);
176         if ($showall) {
177             $startpage = false;
178             $pagecount = false;
179         } else {
180             $table->pagesize($perpage, $matchcount);
181             $startpage = $table->get_page_start();
182             $pagecount = $table->get_page_size();
183         }
185         $students = feedback_get_complete_users($cm, $usedgroupid, $where, $params, $sort, $startpage, $pagecount);
186         $str_analyse = get_string('analysis', 'feedback');
187         $str_complete = get_string('completed_feedbacks', 'feedback');
188         $str_course = get_string('course');
190         $completed_fb_count = feedback_get_completeds_group_count($feedback, $mygroupid);
191         if ($feedback->course == SITEID) {
192             $analysisurl = new moodle_url('/mod/feedback/analysis_course.php', array('id'=>$id, 'courseid'=>$courseid));
193             echo $OUTPUT->box_start('mdl-align');
194             echo '<a href="'.$analysisurl->out().'">';
195             echo $str_course.' '.$str_analyse.' ('.$str_complete.': '.intval($completed_fb_count).')';
196             echo '</a>';
197             echo $OUTPUT->help_icon('viewcompleted', 'feedback');
198             echo $OUTPUT->box_end();
199         } else {
200             $analysisurl = new moodle_url('/mod/feedback/analysis.php', array('id'=>$id, 'courseid'=>$courseid));
201             echo $OUTPUT->box_start('mdl-align');
202             echo '<a href="'.$analysisurl->out().'">';
203             echo $str_analyse.' ('.$str_complete.': '.intval($completed_fb_count).')';
204             echo '</a>';
205             echo $OUTPUT->box_end();
206         }
207     }
209     //####### viewreports-start
210     if (has_capability('mod/feedback:viewreports', $context)) {
211         //print the list of students
212         echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
213         echo isset($groupselect) ? $groupselect : '';
214         echo '<div class="clearer"></div>';
215         echo $OUTPUT->box_start('mdl-align');
216         if (!$students) {
217             $table->print_html();
218         } else {
219             echo print_string('non_anonymous_entries', 'feedback');
220             echo ' ('.count($students).')<hr />';
222             foreach ($students as $student) {
223                 $params = array('userid'=>$student->id,
224                                 'feedback'=>$feedback->id,
225                                 'anonymous_response'=>FEEDBACK_ANONYMOUS_NO);
227                 $completed_count = $DB->count_records('feedback_completed', $params);
228                 if ($completed_count > 0) {
230                     //userpicture and link to the profilepage
231                     $fullname_url = $CFG->wwwroot.'/user/view.php?id='.$student->id.'&amp;course='.$course->id;
232                     $profilelink = '<strong><a href="'.$fullname_url.'">'.fullname($student).'</a></strong>';
233                     $data = array ($OUTPUT->user_picture($student, array('courseid'=>$course->id)), $profilelink);
235                     //link to the entry of the user
236                     $params = array('feedback'=>$feedback->id,
237                                     'userid'=>$student->id,
238                                     'anonymous_response'=>FEEDBACK_ANONYMOUS_NO);
240                     $feedbackcompleted = $DB->get_record('feedback_completed', $params);
241                     $showentryurl_params = array('userid'=>$student->id, 'do_show'=>'showoneentry');
242                     $showentryurl = new moodle_url($url, $showentryurl_params);
243                     $showentrylink = '<a href="'.$showentryurl->out().'">'.userdate($feedbackcompleted->timemodified).'</a>';
244                     $data[] = $showentrylink;
246                     //link to delete the entry
247                     if (has_capability('mod/feedback:deletesubmissions', $context)) {
248                         $delete_url_params = array('id' => $cm->id,
249                                             'completedid' => $feedbackcompleted->id,
250                                             'do_show' => 'showoneentry');
252                         $deleteentryurl = new moodle_url($CFG->wwwroot.'/mod/feedback/delete_completed.php', $delete_url_params);
253                         $deleteentrylink = '<a href="'.$deleteentryurl->out().'">'.
254                                                 get_string('delete_entry', 'feedback').
255                                             '</a>';
256                         $data[] = $deleteentrylink;
257                     }
258                     $table->add_data($data);
259                 }
260             }
261             $table->print_html();
263             $allurl = new moodle_url($baseurl);
265             if ($showall) {
266                 $allurl->param('showall', 0);
267                 echo $OUTPUT->container(html_writer::link($allurl, get_string('showperpage', '', FEEDBACK_DEFAULT_PAGE_COUNT)),
268                                         array(),
269                                         'showall');
271             } else if ($matchcount > 0 && $perpage < $matchcount) {
272                 $allurl->param('showall', 1);
273                 echo $OUTPUT->container(html_writer::link($allurl, get_string('showall', '', $matchcount)),
274                                         array(),
275                                         'showall');
276             }
277         }
278         ?>
279         <hr />
280         <table width="100%">
281             <tr>
282                 <td align="left" colspan="2">
283                     <?php
284                     $params = array('feedback' => $feedback->id,
285                                     'anonymous_response' => FEEDBACK_ANONYMOUS_YES);
287                     $feedback_completeds_count = $DB->count_records('feedback_completed', $params);
288                     print_string('anonymous_entries', 'feedback');
289                     echo '&nbsp;('.$feedback_completeds_count.')';
290                     ?>
291                 </td>
292                 <td align="right">
293                     <?php
294                         $url_params = array('sesskey'=>sesskey(),
295                                         'userid'=>0,
296                                         'do_show'=>'showoneentry',
297                                         'id'=>$id);
298                         $aurl = new moodle_url('show_entries_anonym.php', $url_params);
299                         echo $OUTPUT->single_button($aurl, get_string('show_entries', 'feedback'));
300                     ?>
301                 </td>
302             </tr>
303         </table>
304         <?php
305         echo $OUTPUT->box_end();
306         echo $OUTPUT->box_end();
307     }
310 ////////////////////////////////////////////////////////
311 /// Print the responses of the given user
312 ////////////////////////////////////////////////////////
313 if ($do_show == 'showoneentry') {
314     echo $OUTPUT->heading(format_text($feedback->name));
316     //print the items
317     if (is_array($feedbackitems)) {
318         $align = right_to_left() ? 'right' : 'left';
319         $usr = $DB->get_record('user', array('id'=>$userid));
321         if ($feedbackcompleted) {
322             echo $OUTPUT->heading(userdate($feedbackcompleted->timemodified).' ('.fullname($usr).')', 3);
323         } else {
324             echo $OUTPUT->heading(get_string('not_completed_yet', 'feedback'), 3);
325         }
327         echo $OUTPUT->box_start('feedback_items');
328         $itemnr = 0;
329         foreach ($feedbackitems as $feedbackitem) {
330             //get the values
331             $params = array('completed'=>$feedbackcompleted->id, 'item'=>$feedbackitem->id);
332             $value = $DB->get_record('feedback_value', $params);
333             echo $OUTPUT->box_start('feedback_item_box_'.$align);
334             if ($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
335                 $itemnr++;
336                 echo $OUTPUT->box_start('feedback_item_number_'.$align);
337                 echo $itemnr;
338                 echo $OUTPUT->box_end();
339             }
341             if ($feedbackitem->typ != 'pagebreak') {
342                 echo $OUTPUT->box_start('box generalbox boxalign_'.$align);
343                 if (isset($value->value)) {
344                     feedback_print_item_show_value($feedbackitem, $value->value);
345                 } else {
346                     feedback_print_item_show_value($feedbackitem, false);
347                 }
348                 echo $OUTPUT->box_end();
349             }
350             echo $OUTPUT->box_end();
351         }
352         echo $OUTPUT->box_end();
353     }
354     echo $OUTPUT->continue_button(new moodle_url($url, array('do_show'=>'showentries')));
356 /// Finish the page
357 ///////////////////////////////////////////////////////////////////////////
358 ///////////////////////////////////////////////////////////////////////////
359 ///////////////////////////////////////////////////////////////////////////
361 echo $OUTPUT->footer();