MDL-32676 improve YUI init performance via combo loader caching
[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             if ($courseid != SITEID) {
218                 echo $OUTPUT->notification(get_string('noexistingparticipants', 'enrol'));
219             }
220         } else {
221             echo print_string('non_anonymous_entries', 'feedback');
222             echo ' ('.count($students).')<hr />';
224             foreach ($students as $student) {
225                 $params = array('userid'=>$student->id,
226                                 'feedback'=>$feedback->id,
227                                 'anonymous_response'=>FEEDBACK_ANONYMOUS_NO);
229                 $completed_count = $DB->count_records('feedback_completed', $params);
230                 if ($completed_count > 0) {
232                     //userpicture and link to the profilepage
233                     $fullname_url = $CFG->wwwroot.'/user/view.php?id='.$student->id.'&amp;course='.$course->id;
234                     $profilelink = '<strong><a href="'.$fullname_url.'">'.fullname($student).'</a></strong>';
235                     $data = array ($OUTPUT->user_picture($student, array('courseid'=>$course->id)), $profilelink);
237                     //link to the entry of the user
238                     $params = array('feedback'=>$feedback->id,
239                                     'userid'=>$student->id,
240                                     'anonymous_response'=>FEEDBACK_ANONYMOUS_NO);
242                     $feedbackcompleted = $DB->get_record('feedback_completed', $params);
243                     $showentryurl_params = array('userid'=>$student->id, 'do_show'=>'showoneentry');
244                     $showentryurl = new moodle_url($url, $showentryurl_params);
245                     $showentrylink = '<a href="'.$showentryurl->out().'">'.userdate($feedbackcompleted->timemodified).'</a>';
246                     $data[] = $showentrylink;
248                     //link to delete the entry
249                     if (has_capability('mod/feedback:deletesubmissions', $context)) {
250                         $delete_url_params = array('id' => $cm->id,
251                                             'completedid' => $feedbackcompleted->id,
252                                             'do_show' => 'showoneentry');
254                         $deleteentryurl = new moodle_url($CFG->wwwroot.'/mod/feedback/delete_completed.php', $delete_url_params);
255                         $deleteentrylink = '<a href="'.$deleteentryurl->out().'">'.
256                                                 get_string('delete_entry', 'feedback').
257                                             '</a>';
258                         $data[] = $deleteentrylink;
259                     }
260                     $table->add_data($data);
261                 }
262             }
263             $table->print_html();
265             $allurl = new moodle_url($baseurl);
267             if ($showall) {
268                 $allurl->param('showall', 0);
269                 echo $OUTPUT->container(html_writer::link($allurl, get_string('showperpage', '', FEEDBACK_DEFAULT_PAGE_COUNT)),
270                                         array(),
271                                         'showall');
273             } else if ($matchcount > 0 && $perpage < $matchcount) {
274                 $allurl->param('showall', 1);
275                 echo $OUTPUT->container(html_writer::link($allurl, get_string('showall', '', $matchcount)),
276                                         array(),
277                                         'showall');
278             }
279         }
280         ?>
281         <hr />
282         <table width="100%">
283             <tr>
284                 <td align="left" colspan="2">
285                     <?php
286                     $params = array('feedback' => $feedback->id,
287                                     'anonymous_response' => FEEDBACK_ANONYMOUS_YES);
289                     $feedback_completeds_count = $DB->count_records('feedback_completed', $params);
290                     print_string('anonymous_entries', 'feedback');
291                     echo '&nbsp;('.$feedback_completeds_count.')';
292                     ?>
293                 </td>
294                 <td align="right">
295                     <?php
296                         $url_params = array('sesskey'=>sesskey(),
297                                         'userid'=>0,
298                                         'do_show'=>'showoneentry',
299                                         'id'=>$id);
300                         $aurl = new moodle_url('show_entries_anonym.php', $url_params);
301                         echo $OUTPUT->single_button($aurl, get_string('show_entries', 'feedback'));
302                     ?>
303                 </td>
304             </tr>
305         </table>
306         <?php
307         echo $OUTPUT->box_end();
308         echo $OUTPUT->box_end();
309     }
312 ////////////////////////////////////////////////////////
313 /// Print the responses of the given user
314 ////////////////////////////////////////////////////////
315 if ($do_show == 'showoneentry') {
316     echo $OUTPUT->heading(format_text($feedback->name));
318     //print the items
319     if (is_array($feedbackitems)) {
320         $align = right_to_left() ? 'right' : 'left';
321         $usr = $DB->get_record('user', array('id'=>$userid));
323         if ($feedbackcompleted) {
324             echo $OUTPUT->heading(userdate($feedbackcompleted->timemodified).' ('.fullname($usr).')', 3);
325         } else {
326             echo $OUTPUT->heading(get_string('not_completed_yet', 'feedback'), 3);
327         }
329         echo $OUTPUT->box_start('feedback_items');
330         $itemnr = 0;
331         foreach ($feedbackitems as $feedbackitem) {
332             //get the values
333             $params = array('completed'=>$feedbackcompleted->id, 'item'=>$feedbackitem->id);
334             $value = $DB->get_record('feedback_value', $params);
335             echo $OUTPUT->box_start('feedback_item_box_'.$align);
336             if ($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
337                 $itemnr++;
338                 echo $OUTPUT->box_start('feedback_item_number_'.$align);
339                 echo $itemnr;
340                 echo $OUTPUT->box_end();
341             }
343             if ($feedbackitem->typ != 'pagebreak') {
344                 echo $OUTPUT->box_start('box generalbox boxalign_'.$align);
345                 if (isset($value->value)) {
346                     feedback_print_item_show_value($feedbackitem, $value->value);
347                 } else {
348                     feedback_print_item_show_value($feedbackitem, false);
349                 }
350                 echo $OUTPUT->box_end();
351             }
352             echo $OUTPUT->box_end();
353         }
354         echo $OUTPUT->box_end();
355     }
356     echo $OUTPUT->continue_button(new moodle_url($url, array('do_show'=>'showentries')));
358 /// Finish the page
359 ///////////////////////////////////////////////////////////////////////////
360 ///////////////////////////////////////////////////////////////////////////
361 ///////////////////////////////////////////////////////////////////////////
363 echo $OUTPUT->footer();