Merge branch 'MDL-56627-master' of git://github.com/damyon/moodle
[moodle.git] / mod / choice / report.php
blob3fcde4218c35840ea618ec872a2d1052f04f9104
1 <?php
3 require_once("../../config.php");
4 require_once("lib.php");
6 $id = required_param('id', PARAM_INT); //moduleid
7 $download = optional_param('download', '', PARAM_ALPHA);
8 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
9 $attemptids = optional_param_array('attemptid', array(), PARAM_INT); // Get array of responses to delete or modify.
10 $userids = optional_param_array('userid', array(), PARAM_INT); // Get array of users whose choices need to be modified.
12 $url = new moodle_url('/mod/choice/report.php', array('id'=>$id));
13 if ($download !== '') {
14 $url->param('download', $download);
16 if ($action !== '') {
17 $url->param('action', $action);
19 $PAGE->set_url($url);
21 if (! $cm = get_coursemodule_from_id('choice', $id)) {
22 print_error("invalidcoursemodule");
25 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
26 print_error("coursemisconf");
29 require_login($course, false, $cm);
31 $context = context_module::instance($cm->id);
33 require_capability('mod/choice:readresponses', $context);
35 if (!$choice = choice_get_choice($cm->instance)) {
36 print_error('invalidcoursemodule');
39 $strchoice = get_string("modulename", "choice");
40 $strchoices = get_string("modulenameplural", "choice");
41 $strresponses = get_string("responses", "choice");
43 $eventdata = array();
44 $eventdata['objectid'] = $choice->id;
45 $eventdata['context'] = $context;
46 $eventdata['courseid'] = $course->id;
47 $eventdata['other']['content'] = 'choicereportcontentviewed';
49 $event = \mod_choice\event\report_viewed::create($eventdata);
50 $event->trigger();
52 if (data_submitted() && has_capability('mod/choice:deleteresponses', $context) && confirm_sesskey()) {
53 if ($action === 'delete') {
54 // Delete responses of other users.
55 choice_delete_responses($attemptids, $choice, $cm, $course);
56 redirect("report.php?id=$cm->id");
58 if (preg_match('/^choose_(\d+)$/', $action, $actionmatch)) {
59 // Modify responses of other users.
60 $newoptionid = (int)$actionmatch[1];
61 choice_modify_responses($userids, $attemptids, $newoptionid, $choice, $cm, $course);
62 redirect("report.php?id=$cm->id");
66 if (!$download) {
67 $PAGE->navbar->add($strresponses);
68 $PAGE->set_title(format_string($choice->name).": $strresponses");
69 $PAGE->set_heading($course->fullname);
70 echo $OUTPUT->header();
71 echo $OUTPUT->heading($choice->name, 2, null);
72 /// Check to see if groups are being used in this choice
73 $groupmode = groups_get_activity_groupmode($cm);
74 if ($groupmode) {
75 groups_get_activity_group($cm, true);
76 groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/report.php?id='.$id);
78 } else {
79 $groupmode = groups_get_activity_groupmode($cm);
81 // Trigger the report downloaded event.
82 $eventdata = array();
83 $eventdata['context'] = $context;
84 $eventdata['courseid'] = $course->id;
85 $eventdata['other']['content'] = 'choicereportcontentviewed';
86 $eventdata['other']['format'] = $download;
87 $eventdata['other']['choiceid'] = $choice->id;
88 $event = \mod_choice\event\report_downloaded::create($eventdata);
89 $event->trigger();
93 // Check if we want to include responses from inactive users.
94 $onlyactive = $choice->includeinactive ? false : true;
96 $users = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
98 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
99 require_once("$CFG->libdir/odslib.class.php");
101 /// Calculate file name
102 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
103 /// Creating a workbook
104 $workbook = new MoodleODSWorkbook("-");
105 /// Send HTTP headers
106 $workbook->send($filename);
107 /// Creating the first worksheet
108 $myxls = $workbook->add_worksheet($strresponses);
110 /// Print names of all the fields
111 $myxls->write_string(0,0,get_string("lastname"));
112 $myxls->write_string(0,1,get_string("firstname"));
113 $myxls->write_string(0,2,get_string("idnumber"));
114 $myxls->write_string(0,3,get_string("group"));
115 $myxls->write_string(0,4,get_string("choice","choice"));
117 /// generate the data for the body of the spreadsheet
118 $i=0;
119 $row=1;
120 if ($users) {
121 foreach ($users as $option => $userid) {
122 $option_text = choice_get_option_text($choice, $option);
123 foreach($userid as $user) {
124 $myxls->write_string($row,0,$user->lastname);
125 $myxls->write_string($row,1,$user->firstname);
126 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
127 $myxls->write_string($row,2,$studentid);
128 $ug2 = '';
129 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
130 foreach ($usergrps as $ug) {
131 $ug2 = $ug2. $ug->name;
134 $myxls->write_string($row,3,$ug2);
136 if (isset($option_text)) {
137 $myxls->write_string($row,4,format_string($option_text,true));
139 $row++;
140 $pos=4;
144 /// Close the workbook
145 $workbook->close();
147 exit;
150 //print spreadsheet if one is asked for:
151 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
152 require_once("$CFG->libdir/excellib.class.php");
154 /// Calculate file name
155 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
156 /// Creating a workbook
157 $workbook = new MoodleExcelWorkbook("-");
158 /// Send HTTP headers
159 $workbook->send($filename);
160 /// Creating the first worksheet
161 $myxls = $workbook->add_worksheet($strresponses);
163 /// Print names of all the fields
164 $myxls->write_string(0,0,get_string("lastname"));
165 $myxls->write_string(0,1,get_string("firstname"));
166 $myxls->write_string(0,2,get_string("idnumber"));
167 $myxls->write_string(0,3,get_string("group"));
168 $myxls->write_string(0,4,get_string("choice","choice"));
171 /// generate the data for the body of the spreadsheet
172 $i=0;
173 $row=1;
174 if ($users) {
175 foreach ($users as $option => $userid) {
176 $option_text = choice_get_option_text($choice, $option);
177 foreach($userid as $user) {
178 $myxls->write_string($row,0,$user->lastname);
179 $myxls->write_string($row,1,$user->firstname);
180 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
181 $myxls->write_string($row,2,$studentid);
182 $ug2 = '';
183 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
184 foreach ($usergrps as $ug) {
185 $ug2 = $ug2. $ug->name;
188 $myxls->write_string($row,3,$ug2);
189 if (isset($option_text)) {
190 $myxls->write_string($row,4,format_string($option_text,true));
192 $row++;
195 $pos=4;
197 /// Close the workbook
198 $workbook->close();
199 exit;
202 // print text file
203 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
204 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
206 header("Content-Type: application/download\n");
207 header("Content-Disposition: attachment; filename=\"$filename\"");
208 header("Expires: 0");
209 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
210 header("Pragma: public");
212 /// Print names of all the fields
214 echo get_string("lastname")."\t".get_string("firstname") . "\t". get_string("idnumber") . "\t";
215 echo get_string("group"). "\t";
216 echo get_string("choice","choice"). "\n";
218 /// generate the data for the body of the spreadsheet
219 $i=0;
220 if ($users) {
221 foreach ($users as $option => $userid) {
222 $option_text = choice_get_option_text($choice, $option);
223 foreach($userid as $user) {
224 echo $user->lastname;
225 echo "\t".$user->firstname;
226 $studentid = " ";
227 if (!empty($user->idnumber)) {
228 $studentid = $user->idnumber;
230 echo "\t". $studentid."\t";
231 $ug2 = '';
232 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
233 foreach ($usergrps as $ug) {
234 $ug2 = $ug2. $ug->name;
237 echo $ug2. "\t";
238 if (isset($option_text)) {
239 echo format_string($option_text,true);
241 echo "\n";
245 exit;
247 // Always show those who haven't answered the question.
248 $choice->showunanswered = 1;
249 $results = prepare_choice_show_results($choice, $course, $cm, $users);
250 $renderer = $PAGE->get_renderer('mod_choice');
251 echo $renderer->display_result($results, true);
253 //now give links for downloading spreadsheets.
254 if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
255 $downloadoptions = array();
256 $options = array();
257 $options["id"] = "$cm->id";
258 $options["download"] = "ods";
259 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadods"));
260 $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
262 $options["download"] = "xls";
263 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadexcel"));
264 $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
266 $options["download"] = "txt";
267 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadtext"));
268 $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
270 $downloadlist = html_writer::tag('ul', implode('', $downloadoptions));
271 $downloadlist .= html_writer::tag('div', '', array('class'=>'clearfloat'));
272 echo html_writer::tag('div',$downloadlist, array('class'=>'downloadreport'));
274 echo $OUTPUT->footer();