MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / choice / report.php
blobd782383219b44c703a4906b5c21aad029c93dd5e
1 <?php // $Id$
3 require_once("../../config.php");
4 require_once("lib.php");
6 $id = required_param('id', PARAM_INT); //moduleid
7 $format = optional_param('format', CHOICE_PUBLISH_NAMES, PARAM_INT);
8 $download = optional_param('download', '', PARAM_ALPHA);
9 $action = optional_param('action', '', PARAM_ALPHA);
11 if (! $cm = get_coursemodule_from_id('choice', $id)) {
12 error("Course Module ID was incorrect");
15 if (! $course = get_record("course", "id", $cm->course)) {
16 error("Course module is misconfigured");
19 require_login($course->id, false, $cm);
21 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
23 require_capability('mod/choice:readresponses', $context);
25 if (!$choice = choice_get_choice($cm->instance)) {
26 error("Course module is incorrect");
29 $strchoice = get_string("modulename", "choice");
30 $strchoices = get_string("modulenameplural", "choice");
31 $strresponses = get_string("responses", "choice");
33 add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
35 if ($action == 'delete' && has_capability('mod/choice:deleteresponses',$context)) {
36 $attemptids = isset($_POST['attemptid']) ? $_POST['attemptid'] : array(); //get array of repsonses to delete.
37 choice_delete_responses($attemptids); //delete responses.
38 redirect("report.php?id=$cm->id");
41 if (!$download) {
43 $navlinks = array();
44 $navlinks[] = array('name' => $strchoices, 'link' => "index.php?id=$course->id", 'type' => 'activity');
45 $navlinks[] = array('name' => format_string($choice->name), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
46 $navlinks[] = array('name' => $strresponses, 'link' => '', 'type' => 'title');
48 $navigation = build_navigation($navlinks);
50 print_header_simple(format_string($choice->name).": $strresponses", "", $navigation, "", '', true,
51 update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
52 /// Check to see if groups are being used in this choice
53 $groupmode = groups_get_activity_groupmode($cm);
54 groups_get_activity_group($cm, true);
55 groups_print_activity_menu($cm, 'report.php?id='.$id);
56 } else {
57 $groupmode = groups_get_activity_groupmode($cm);
58 groups_get_activity_group($cm, true);
61 $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC');
63 if (!empty($CFG->enablegroupings) && !empty($cm->groupingid) && !empty($users)) {
64 $groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id', 'u.id');
65 foreach($users as $key => $user) {
66 if (!isset($groupingusers[$user->id])) {
67 unset($users[$key]);
72 if (!$users) {
73 print_heading(get_string("nousersyet"));
76 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
77 foreach ($allresponses as $aa) {
78 $answers[$aa->userid] = $aa;
80 } else {
81 $answers = array () ;
84 $timenow = time();
86 foreach ($choice->option as $optionid => $text) {
87 $useranswer[$optionid] = array();
89 foreach ($users as $user) {
90 if (!empty($user->id) and !empty($answers[$user->id])) {
91 $answer = $answers[$user->id];
92 $useranswer[(int)$answer->optionid][] = $user;
93 } else {
94 $useranswer[0][] = $user;
97 foreach ($choice->option as $optionid => $text) {
98 if (!$choice->option[$optionid]) {
99 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
102 ksort($useranswer);
105 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
106 require_once("$CFG->libdir/odslib.class.php");
108 /// Calculate file name
109 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
110 /// Creating a workbook
111 $workbook = new MoodleODSWorkbook("-");
112 /// Send HTTP headers
113 $workbook->send($filename);
114 /// Creating the first worksheet
115 $myxls =& $workbook->add_worksheet($strresponses);
117 /// Print names of all the fields
118 $myxls->write_string(0,0,get_string("lastname"));
119 $myxls->write_string(0,1,get_string("firstname"));
120 $myxls->write_string(0,2,get_string("idnumber"));
121 $myxls->write_string(0,3,get_string("group"));
122 $myxls->write_string(0,4,get_string("choice","choice"));
125 /// generate the data for the body of the spreadsheet
126 $i=0;
127 $row=1;
128 if ($users) {
129 foreach ($users as $user) {
130 // this needs fixing
132 if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) {
134 if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==0)) { // make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
136 $myxls->write_string($row,0,$user->lastname);
137 $myxls->write_string($row,1,$user->firstname);
138 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
139 $myxls->write_string($row,2,$studentid);
140 $ug2 = '';
141 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
142 foreach ($usergrps as $ug) {
143 $ug2 = $ug2. $ug->name;
146 $myxls->write_string($row,3,$ug2);
148 $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
149 if (isset($useroption)) {
150 $myxls->write_string($row,4,format_string($useroption,true));
152 $row++;
154 $pos=4;
158 /// Close the workbook
159 $workbook->close();
161 exit;
166 //print spreadsheet if one is asked for:
167 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
168 require_once("$CFG->libdir/excellib.class.php");
170 /// Calculate file name
171 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
172 /// Creating a workbook
173 $workbook = new MoodleExcelWorkbook("-");
174 /// Send HTTP headers
175 $workbook->send($filename);
176 /// Creating the first worksheet
177 $myxls =& $workbook->add_worksheet($strresponses);
179 /// Print names of all the fields
180 $myxls->write_string(0,0,get_string("lastname"));
181 $myxls->write_string(0,1,get_string("firstname"));
182 $myxls->write_string(0,2,get_string("idnumber"));
183 $myxls->write_string(0,3,get_string("group"));
184 $myxls->write_string(0,4,get_string("choice","choice"));
187 /// generate the data for the body of the spreadsheet
188 $i=0;
189 $row=1;
190 if ($users) {
191 foreach ($users as $user) {
192 // this needs fixing
194 if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) {
196 if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==0)) { // make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
198 $myxls->write_string($row,0,$user->lastname);
199 $myxls->write_string($row,1,$user->firstname);
200 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
201 $myxls->write_string($row,2,$studentid);
202 $ug2 = '';
203 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
204 foreach ($usergrps as $ug) {
205 $ug2 = $ug2. $ug->name;
208 $myxls->write_string($row,3,$ug2);
210 $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
211 if (isset($useroption)) {
212 $myxls->write_string($row,4,format_string($useroption,true));
214 $row++;
216 $pos=4;
220 /// Close the workbook
221 $workbook->close();
223 exit;
226 // print text file
227 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
228 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
230 header("Content-Type: application/download\n");
231 header("Content-Disposition: attachment; filename=\"$filename\"");
232 header("Expires: 0");
233 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
234 header("Pragma: public");
236 /// Print names of all the fields
238 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
239 echo get_string("group"). "\t";
240 echo get_string("choice","choice"). "\n";
242 /// generate the data for the body of the spreadsheet
243 $i=0;
244 $row=1;
245 if ($users) foreach ($users as $user) {
246 if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==0)) { // make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
248 echo $user->lastname;
249 echo "\t".$user->firstname;
250 $studentid = " ";
251 if (!empty($user->idnumber)) {
252 $studentid = $user->idnumber;
254 echo "\t". $studentid."\t";
255 $ug2 = '';
256 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
257 foreach ($usergrps as $ug) {
258 $ug2 = $ug2. $ug->name;
261 echo $ug2. "\t";
262 echo format_string(choice_get_option_text($choice, $answers[$user->id]->optionid),true). "\n";
264 $row++;
266 exit;
269 choice_show_results($choice, $course, $cm, $format); //show table with students responses.
271 //now give links for downloading spreadsheets.
272 echo "<br />\n";
273 echo "<table class=\"downloadreport\"><tr>\n";
274 echo "<td>";
275 $options = array();
276 $options["id"] = "$cm->id";
277 $options["download"] = "ods";
278 print_single_button("report.php", $options, get_string("downloadods"));
279 echo "</td><td>";
280 $options["download"] = "xls";
281 print_single_button("report.php", $options, get_string("downloadexcel"));
282 echo "</td><td>";
283 $options["download"] = "txt";
284 print_single_button("report.php", $options, get_string("downloadtext"));
286 echo "</td></tr></table>";
287 print_footer($course);