navigation MDL-20324 Fixed typo
[moodle.git] / mod / choice / report.php
CommitLineData
1c61c8d6 1<?php // $Id$
f9903ed0 2
b0e3a925 3 require_once("../../config.php");
4 require_once("lib.php");
f9903ed0 5
aea69033 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);
10 $attemptids = optional_param('attemptid', array(), PARAM_INT); //get array of responses to delete.
bfe97ca6 11
f9d5371b 12 if (! $cm = get_coursemodule_from_id('choice', $id)) {
c7281355 13 print_error("invalidcoursemodule");
f9903ed0 14 }
15
407caeeb 16 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
c7281355 17 print_error("coursemisconf");
f9903ed0 18 }
19
ec81373f 20 require_login($course->id, false, $cm);
407caeeb 21
dabfd0ed 22 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
407caeeb 23
0468976c 24 require_capability('mod/choice:readresponses', $context);
407caeeb 25
cd3fccff 26 if (!$choice = choice_get_choice($cm->instance)) {
c7281355 27 print_error('invalidcoursemodule');
f9903ed0 28 }
29
c4016bc1 30 $strchoice = get_string("modulename", "choice");
31 $strchoices = get_string("modulenameplural", "choice");
32 $strresponses = get_string("responses", "choice");
33
408f7c41 34 add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
407caeeb 35
0468976c 36 if ($action == 'delete' && has_capability('mod/choice:deleteresponses',$context)) {
a61e4188 37 choice_delete_responses($attemptids, $choice->id); //delete responses.
38 redirect("report.php?id=$cm->id");
348630d8 39 }
407caeeb 40
3b27b0fe 41 if (!$download) {
3180e717 42 $PAGE->navbar->add($strresponses);
43 $PAGE->set_title(format_string($choice->name).": $strresponses");
44 $PAGE->set_button(update_module_button($cm->id, $course->id, $strchoice));
45 echo $OUTPUT->header();
3b27b0fe 46 /// Check to see if groups are being used in this choice
6d5b7706 47 $groupmode = groups_get_activity_groupmode($cm);
aea69033 48 if ($groupmode) {
49 groups_get_activity_group($cm, true);
50 groups_print_activity_menu($cm, 'report.php?id='.$id);
51 }
3b27b0fe 52 } else {
6d5b7706 53 $groupmode = groups_get_activity_groupmode($cm);
f9903ed0 54 }
a61e4188 55 $users = choice_get_response_data($choice, $cm, $groupmode);
d81b7ffb 56
57 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
58 require_once("$CFG->libdir/odslib.class.php");
407caeeb 59
60 /// Calculate file name
d81b7ffb 61 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
62 /// Creating a workbook
63 $workbook = new MoodleODSWorkbook("-");
64 /// Send HTTP headers
65 $workbook->send($filename);
66 /// Creating the first worksheet
67 $myxls =& $workbook->add_worksheet($strresponses);
68
69 /// Print names of all the fields
70 $myxls->write_string(0,0,get_string("lastname"));
71 $myxls->write_string(0,1,get_string("firstname"));
72 $myxls->write_string(0,2,get_string("idnumber"));
73 $myxls->write_string(0,3,get_string("group"));
74 $myxls->write_string(0,4,get_string("choice","choice"));
a61e4188 75
d81b7ffb 76 /// generate the data for the body of the spreadsheet
407caeeb 77 $i=0;
d81b7ffb 78 $row=1;
79 if ($users) {
a61e4188 80 foreach ($users as $option => $userid) {
81 $option_text = choice_get_option_text($choice, $option);
82 foreach($userid as $user) {
83 $myxls->write_string($row,0,$user->lastname);
84 $myxls->write_string($row,1,$user->firstname);
85 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
86 $myxls->write_string($row,2,$studentid);
87 $ug2 = '';
88 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
89 foreach ($usergrps as $ug) {
90 $ug2 = $ug2. $ug->name;
d81b7ffb 91 }
d81b7ffb 92 }
a61e4188 93 $myxls->write_string($row,3,$ug2);
94
95 if (isset($option_text)) {
407caeeb 96 $myxls->write_string($row,4,format_string($option_text,true));
a61e4188 97 }
98 $row++;
d81b7ffb 99 $pos=4;
100 }
101 }
a61e4188 102 }
103 /// Close the workbook
104 $workbook->close();
d81b7ffb 105
a61e4188 106 exit;
d81b7ffb 107 }
108
64739659 109 //print spreadsheet if one is asked for:
0468976c 110 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
b0898566 111 require_once("$CFG->libdir/excellib.class.php");
407caeeb 112
113 /// Calculate file name
b0898566 114 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
115 /// Creating a workbook
116 $workbook = new MoodleExcelWorkbook("-");
117 /// Send HTTP headers
118 $workbook->send($filename);
119 /// Creating the first worksheet
31344133 120 $myxls =& $workbook->add_worksheet($strresponses);
64739659 121
b0898566 122 /// Print names of all the fields
64739659 123 $myxls->write_string(0,0,get_string("lastname"));
124 $myxls->write_string(0,1,get_string("firstname"));
125 $myxls->write_string(0,2,get_string("idnumber"));
348630d8 126 $myxls->write_string(0,3,get_string("group"));
127 $myxls->write_string(0,4,get_string("choice","choice"));
407caeeb 128
129
64739659 130 /// generate the data for the body of the spreadsheet
407caeeb 131 $i=0;
b0898566 132 $row=1;
133 if ($users) {
a61e4188 134 foreach ($users as $option => $userid) {
135 $option_text = choice_get_option_text($choice, $option);
136 foreach($userid as $user) {
137 $myxls->write_string($row,0,$user->lastname);
138 $myxls->write_string($row,1,$user->firstname);
139 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
140 $myxls->write_string($row,2,$studentid);
141 $ug2 = '';
142 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
143 foreach ($usergrps as $ug) {
144 $ug2 = $ug2. $ug->name;
dabfd0ed 145 }
39b3e7de 146 }
a61e4188 147 $myxls->write_string($row,3,$ug2);
148 if (isset($option_text)) {
149 $myxls->write_string($row,4,format_string($option_text,true));
150 }
151 $row++;
b0898566 152 }
b0898566 153 }
a61e4188 154 $pos=4;
155 }
156 /// Close the workbook
157 $workbook->close();
158 exit;
39b3e7de 159 }
a61e4188 160
407caeeb 161 // print text file
0468976c 162 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
09972dd3 163 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
0468976c 164
165 header("Content-Type: application/download\n");
166 header("Content-Disposition: attachment; filename=\"$filename\"");
167 header("Expires: 0");
168 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
169 header("Pragma: public");
170
171 /// Print names of all the fields
64739659 172
173 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
348630d8 174 echo get_string("group"). "\t";
407caeeb 175 echo get_string("choice","choice"). "\n";
0468976c 176
177 /// generate the data for the body of the spreadsheet
407caeeb 178 $i=0;
a61e4188 179 if ($users) {
180 foreach ($users as $option => $userid) {
181 $option_text = choice_get_option_text($choice, $option);
182 foreach($userid as $user) {
183 echo $user->lastname;
184 echo "\t".$user->firstname;
185 $studentid = " ";
186 if (!empty($user->idnumber)) {
187 $studentid = $user->idnumber;
188 }
189 echo "\t". $studentid."\t";
190 $ug2 = '';
191 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
192 foreach ($usergrps as $ug) {
193 $ug2 = $ug2. $ug->name;
194 }
0468976c 195 }
a61e4188 196 echo $ug2. "\t";
197 if (isset($option_text)) {
198 echo format_string($option_text,true);
199 }
200 echo "\n";
0468976c 201 }
0468976c 202 }
a61e4188 203 }
0468976c 204 exit;
205 }
a61e4188 206 choice_show_results($choice, $course, $cm, $users, $format); //show table with students responses.
0468976c 207
407caeeb 208 //now give links for downloading spreadsheets.
9acb7700 209 if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
cb4db90d 210 echo "<br />\n";
211 echo "<table class=\"downloadreport\"><tr>\n";
212 echo "<td>";
213 $options = array();
407caeeb 214 $options["id"] = "$cm->id";
cb4db90d 215 $options["download"] = "ods";
d3fd5493 216 echo $OUTPUT->button(html_form::make_button("report.php", $options, get_string("downloadods")));
cb4db90d 217 echo "</td><td>";
218 $options["download"] = "xls";
d3fd5493 219 echo $OUTPUT->button(html_form::make_button("report.php", $options, get_string("downloadexcel")));
cb4db90d 220 echo "</td><td>";
407caeeb 221 $options["download"] = "txt";
d3fd5493 222 echo $OUTPUT->button(html_form::make_button("report.php", $options, get_string("downloadtext")));
cb4db90d 223
224 echo "</td></tr></table>";
225 }
0f4e7a3a 226 echo $OUTPUT->footer();
ec81373f 227
0f4e7a3a 228?>