1c61c8d6 |
1 | <?php // $Id$ |
f9903ed0 |
2 | |
b0e3a925 |
3 | require_once("../../config.php"); |
4 | require_once("lib.php"); |
f9903ed0 |
5 | |
69e3c416 |
6 | $id = required_param('id', PARAM_INT); //moduleid |
09972dd3 |
7 | $format = optional_param('format', CHOICE_PUBLISH_NAMES, PARAM_INT); |
8 | $download = optional_param('download', '', PARAM_ALPHA); |
69e3c416 |
9 | $action = optional_param('action', '', PARAM_ALPHA); |
bfe97ca6 |
10 | |
f9d5371b |
11 | if (! $cm = get_coursemodule_from_id('choice', $id)) { |
f9903ed0 |
12 | error("Course Module ID was incorrect"); |
13 | } |
14 | |
15 | if (! $course = get_record("course", "id", $cm->course)) { |
16 | error("Course module is misconfigured"); |
17 | } |
18 | |
ec81373f |
19 | require_login($course->id, false, $cm); |
bbbf2d40 |
20 | |
dabfd0ed |
21 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); |
bbbf2d40 |
22 | |
0468976c |
23 | require_capability('mod/choice:readresponses', $context); |
bbbf2d40 |
24 | |
cd3fccff |
25 | if (!$choice = choice_get_choice($cm->instance)) { |
f9903ed0 |
26 | error("Course module is incorrect"); |
27 | } |
28 | |
c4016bc1 |
29 | $strchoice = get_string("modulename", "choice"); |
30 | $strchoices = get_string("modulenameplural", "choice"); |
31 | $strresponses = get_string("responses", "choice"); |
32 | |
408f7c41 |
33 | add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id); |
348630d8 |
34 | |
0468976c |
35 | if ($action == 'delete' && has_capability('mod/choice:deleteresponses',$context)) { |
348630d8 |
36 | $attemptids = isset($_POST['attemptid']) ? $_POST['attemptid'] : array(); //get array of repsonses to delete. |
90ca2ce8 |
37 | choice_delete_responses($attemptids); //delete responses. |
dabfd0ed |
38 | redirect("report.php?id=$cm->id"); |
348630d8 |
39 | } |
40 | |
3b27b0fe |
41 | if (!$download) { |
a044c05d |
42 | |
38e179a4 |
43 | $navigation = build_navigation($strresponses, $cm); |
161fbddf |
44 | print_header_simple(format_string($choice->name).": $strresponses", "", $navigation, "", '', true, |
d921818d |
45 | update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm)); |
3b27b0fe |
46 | /// Check to see if groups are being used in this choice |
6d5b7706 |
47 | $groupmode = groups_get_activity_groupmode($cm); |
48 | groups_get_activity_group($cm, true); |
49 | groups_print_activity_menu($cm, 'report.php?id='.$id); |
3b27b0fe |
50 | } else { |
6d5b7706 |
51 | $groupmode = groups_get_activity_groupmode($cm); |
52 | groups_get_activity_group($cm, true); |
b6ee2d82 |
53 | } |
f9903ed0 |
54 | |
33a893aa |
55 | $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC'); |
6d5b7706 |
56 | |
57 | if (!empty($CFG->enablegroupings) && !empty($cm->groupingid) && !empty($users)) { |
58 | $groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id', 'u.id'); |
59 | foreach($users as $key => $user) { |
60 | if (!isset($groupingusers[$user->id])) { |
61 | unset($users[$key]); |
62 | } |
63 | } |
64 | } |
65 | |
b6ee2d82 |
66 | if (!$users) { |
39ee02a3 |
67 | print_heading(get_string("nousersyet")); |
f9903ed0 |
68 | } |
69 | |
bfe97ca6 |
70 | if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) { |
6fd87e3b |
71 | foreach ($allresponses as $aa) { |
ebc3bd2b |
72 | $answers[$aa->userid] = $aa; |
f9903ed0 |
73 | } |
f9903ed0 |
74 | } else { |
75 | $answers = array () ; |
76 | } |
f9903ed0 |
77 | |
78 | $timenow = time(); |
79 | |
bfe97ca6 |
80 | foreach ($choice->option as $optionid => $text) { |
81 | $useranswer[$optionid] = array(); |
66062dd3 |
82 | } |
da664183 |
83 | foreach ($users as $user) { |
bfe97ca6 |
84 | if (!empty($user->id) and !empty($answers[$user->id])) { |
dcde9f02 |
85 | $answer = $answers[$user->id]; |
bfe97ca6 |
86 | $useranswer[(int)$answer->optionid][] = $user; |
dcde9f02 |
87 | } else { |
bfe97ca6 |
88 | $useranswer[0][] = $user; |
dcde9f02 |
89 | } |
da664183 |
90 | } |
bfe97ca6 |
91 | foreach ($choice->option as $optionid => $text) { |
92 | if (!$choice->option[$optionid]) { |
93 | unset($useranswer[$optionid]); // Throw away any data that doesn't apply |
cd3fccff |
94 | } |
95 | } |
da664183 |
96 | ksort($useranswer); |
d81b7ffb |
97 | |
98 | |
99 | if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) { |
100 | require_once("$CFG->libdir/odslib.class.php"); |
101 | |
102 | /// Calculate file name |
103 | $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods'; |
104 | /// Creating a workbook |
105 | $workbook = new MoodleODSWorkbook("-"); |
106 | /// Send HTTP headers |
107 | $workbook->send($filename); |
108 | /// Creating the first worksheet |
109 | $myxls =& $workbook->add_worksheet($strresponses); |
110 | |
111 | /// Print names of all the fields |
112 | $myxls->write_string(0,0,get_string("lastname")); |
113 | $myxls->write_string(0,1,get_string("firstname")); |
114 | $myxls->write_string(0,2,get_string("idnumber")); |
115 | $myxls->write_string(0,3,get_string("group")); |
116 | $myxls->write_string(0,4,get_string("choice","choice")); |
117 | |
118 | |
119 | /// generate the data for the body of the spreadsheet |
120 | $i=0; |
121 | $row=1; |
122 | if ($users) { |
123 | foreach ($users as $user) { |
124 | // this needs fixing |
125 | |
126 | if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) { |
127 | |
128 | 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. |
129 | |
130 | $myxls->write_string($row,0,$user->lastname); |
131 | $myxls->write_string($row,1,$user->firstname); |
132 | $studentid=(!empty($user->idnumber) ? $user->idnumber : " "); |
133 | $myxls->write_string($row,2,$studentid); |
134 | $ug2 = ''; |
2c386f82 |
135 | if ($usergrps = groups_get_all_groups($course->id, $user->id)) { |
d81b7ffb |
136 | foreach ($usergrps as $ug) { |
137 | $ug2 = $ug2. $ug->name; |
138 | } |
139 | } |
140 | $myxls->write_string($row,3,$ug2); |
141 | |
142 | $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid); |
143 | if (isset($useroption)) { |
144 | $myxls->write_string($row,4,format_string($useroption,true)); |
145 | } |
146 | $row++; |
147 | } |
148 | $pos=4; |
149 | } |
150 | } |
151 | |
152 | /// Close the workbook |
153 | $workbook->close(); |
154 | |
155 | exit; |
156 | } |
157 | } |
158 | |
159 | |
64739659 |
160 | //print spreadsheet if one is asked for: |
0468976c |
161 | if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) { |
b0898566 |
162 | require_once("$CFG->libdir/excellib.class.php"); |
64739659 |
163 | |
31344133 |
164 | /// Calculate file name |
b0898566 |
165 | $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls'; |
166 | /// Creating a workbook |
167 | $workbook = new MoodleExcelWorkbook("-"); |
168 | /// Send HTTP headers |
169 | $workbook->send($filename); |
170 | /// Creating the first worksheet |
31344133 |
171 | $myxls =& $workbook->add_worksheet($strresponses); |
64739659 |
172 | |
b0898566 |
173 | /// Print names of all the fields |
64739659 |
174 | $myxls->write_string(0,0,get_string("lastname")); |
175 | $myxls->write_string(0,1,get_string("firstname")); |
176 | $myxls->write_string(0,2,get_string("idnumber")); |
348630d8 |
177 | $myxls->write_string(0,3,get_string("group")); |
178 | $myxls->write_string(0,4,get_string("choice","choice")); |
179 | |
64739659 |
180 | |
64739659 |
181 | /// generate the data for the body of the spreadsheet |
b0898566 |
182 | $i=0; |
183 | $row=1; |
184 | if ($users) { |
f9a59efa |
185 | foreach ($users as $user) { |
186 | // this needs fixing |
39b3e7de |
187 | |
188 | if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) { |
189 | |
190 | 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. |
191 | |
192 | $myxls->write_string($row,0,$user->lastname); |
193 | $myxls->write_string($row,1,$user->firstname); |
194 | $studentid=(!empty($user->idnumber) ? $user->idnumber : " "); |
195 | $myxls->write_string($row,2,$studentid); |
196 | $ug2 = ''; |
2c386f82 |
197 | if ($usergrps = groups_get_all_groups($course->id, $user->id)) { |
39b3e7de |
198 | foreach ($usergrps as $ug) { |
199 | $ug2 = $ug2. $ug->name; |
200 | } |
dabfd0ed |
201 | } |
39b3e7de |
202 | $myxls->write_string($row,3,$ug2); |
348630d8 |
203 | |
39b3e7de |
204 | $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid); |
205 | if (isset($useroption)) { |
206 | $myxls->write_string($row,4,format_string($useroption,true)); |
207 | } |
208 | $row++; |
209 | } |
210 | $pos=4; |
b0898566 |
211 | } |
b0898566 |
212 | } |
b0898566 |
213 | |
214 | /// Close the workbook |
39b3e7de |
215 | $workbook->close(); |
b0898566 |
216 | |
39b3e7de |
217 | exit; |
218 | } |
219 | } |
bbbf2d40 |
220 | // print text file |
0468976c |
221 | if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) { |
09972dd3 |
222 | $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt'; |
0468976c |
223 | |
224 | header("Content-Type: application/download\n"); |
225 | header("Content-Disposition: attachment; filename=\"$filename\""); |
226 | header("Expires: 0"); |
227 | header("Cache-Control: must-revalidate,post-check=0,pre-check=0"); |
228 | header("Pragma: public"); |
229 | |
230 | /// Print names of all the fields |
64739659 |
231 | |
232 | echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t"; |
348630d8 |
233 | echo get_string("group"). "\t"; |
64739659 |
234 | echo get_string("choice","choice"). "\n"; |
0468976c |
235 | |
236 | /// generate the data for the body of the spreadsheet |
237 | $i=0; |
238 | $row=1; |
239 | if ($users) foreach ($users as $user) { |
39b3e7de |
240 | 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. |
0468976c |
241 | |
242 | echo $user->lastname; |
243 | echo "\t".$user->firstname; |
244 | $studentid = " "; |
245 | if (!empty($user->idnumber)) { |
246 | $studentid = $user->idnumber; |
247 | } |
248 | echo "\t". $studentid."\t"; |
249 | $ug2 = ''; |
2c386f82 |
250 | if ($usergrps = groups_get_all_groups($course->id, $user->id)) { |
0468976c |
251 | foreach ($usergrps as $ug) { |
252 | $ug2 = $ug2. $ug->name; |
253 | } |
254 | } |
255 | echo $ug2. "\t"; |
256 | echo format_string(choice_get_option_text($choice, $answers[$user->id]->optionid),true). "\n"; |
257 | } |
258 | $row++; |
259 | } |
260 | exit; |
261 | } |
262 | |
348630d8 |
263 | choice_show_results($choice, $course, $cm, $format); //show table with students responses. |
264 | |
265 | //now give links for downloading spreadsheets. |
64739659 |
266 | echo "<br />\n"; |
95aa949b |
267 | echo "<table class=\"downloadreport\"><tr>\n"; |
64739659 |
268 | echo "<td>"; |
d81b7ffb |
269 | $options = array(); |
64739659 |
270 | $options["id"] = "$cm->id"; |
d81b7ffb |
271 | $options["download"] = "ods"; |
272 | print_single_button("report.php", $options, get_string("downloadods")); |
273 | echo "</td><td>"; |
64739659 |
274 | $options["download"] = "xls"; |
275 | print_single_button("report.php", $options, get_string("downloadexcel")); |
276 | echo "</td><td>"; |
277 | $options["download"] = "txt"; |
278 | print_single_button("report.php", $options, get_string("downloadtext")); |
279 | |
280 | echo "</td></tr></table>"; |
39b3e7de |
281 | print_footer($course); |
ec81373f |
282 | |
f9903ed0 |
283 | ?> |