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