1<?php
2
3    require_once("../../config.php");
4    require_once("lib.php");
5
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.
11
12    $url = new moodle_url('/mod/choice/report.php', array('id'=>$id));
13    if ($download !== '') {
14        $url->param('download', $download);
15    }
16    if ($action !== '') {
17        $url->param('action', $action);
18    }
19    $PAGE->set_url($url);
20
21    if (! $cm = get_coursemodule_from_id('choice', $id)) {
22        print_error("invalidcoursemodule");
23    }
24
25    if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
26        print_error("coursemisconf");
27    }
28
29    require_login($course, false, $cm);
30
31    $context = context_module::instance($cm->id);
32
33    require_capability('mod/choice:readresponses', $context);
34
35    if (!$choice = choice_get_choice($cm->instance)) {
36        print_error('invalidcoursemodule');
37    }
38
39    $strchoice = get_string("modulename", "choice");
40    $strchoices = get_string("modulenameplural", "choice");
41    $strresponses = get_string("responses", "choice");
42
43    $eventdata = array();
44    $eventdata['objectid'] = $choice->id;
45    $eventdata['context'] = $context;
46    $eventdata['courseid'] = $course->id;
47    $eventdata['other']['content'] = 'choicereportcontentviewed';
48
49    $event = \mod_choice\event\report_viewed::create($eventdata);
50    $event->trigger();
51
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");
57        }
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");
63        }
64    }
65
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(format_string($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);
77        }
78    } else {
79        $groupmode = groups_get_activity_groupmode($cm);
80
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();
90
91    }
92
93    // Check if we want to include responses from inactive users.
94    $onlyactive = $choice->includeinactive ? false : true;
95
96    $users = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
97
98    $extrafields = get_extra_user_fields($context);
99
100    if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
101        require_once("$CFG->libdir/odslib.class.php");
102
103    /// Calculate file name
104        $shortname = format_string($course->shortname, true, array('context' => $context));
105        $choicename = format_string($choice->name, true, array('context' => $context));
106        $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.ods';
107    /// Creating a workbook
108        $workbook = new MoodleODSWorkbook("-");
109    /// Send HTTP headers
110        $workbook->send($filename);
111    /// Creating the first worksheet
112        $myxls = $workbook->add_worksheet($strresponses);
113
114    /// Print names of all the fields
115        $i = 0;
116        $myxls->write_string(0, $i++, get_string("lastname"));
117        $myxls->write_string(0, $i++, get_string("firstname"));
118
119        // Add headers for extra user fields.
120        foreach ($extrafields as $field) {
121            $myxls->write_string(0, $i++, get_user_field_name($field));
122        }
123
124        $myxls->write_string(0, $i++, get_string("group"));
125        $myxls->write_string(0, $i++, get_string("choice", "choice"));
126
127        // Generate the data for the body of the spreadsheet.
128        $row = 1;
129        if ($users) {
130            foreach ($users as $option => $userid) {
131                $option_text = choice_get_option_text($choice, $option);
132                foreach ($userid as $user) {
133                    $i = 0;
134                    $myxls->write_string($row, $i++, $user->lastname);
135                    $myxls->write_string($row, $i++, $user->firstname);
136                    foreach ($extrafields as $field) {
137                        $myxls->write_string($row, $i++, $user->$field);
138                    }
139                    $ug2 = '';
140                    if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
141                        foreach ($usergrps as $ug) {
142                            $ug2 = $ug2 . $ug->name;
143                        }
144                    }
145                    $myxls->write_string($row, $i++, $ug2);
146
147                    if (isset($option_text)) {
148                        $myxls->write_string($row, $i++, format_string($option_text, true));
149                    }
150                    $row++;
151                }
152            }
153        }
154        /// Close the workbook
155        $workbook->close();
156
157        exit;
158    }
159
160    //print spreadsheet if one is asked for:
161    if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
162        require_once("$CFG->libdir/excellib.class.php");
163
164    /// Calculate file name
165        $shortname = format_string($course->shortname, true, array('context' => $context));
166        $choicename = format_string($choice->name, true, array('context' => $context));
167        $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.xls';
168    /// Creating a workbook
169        $workbook = new MoodleExcelWorkbook("-");
170    /// Send HTTP headers
171        $workbook->send($filename);
172    /// Creating the first worksheet
173        $myxls = $workbook->add_worksheet($strresponses);
174
175    /// Print names of all the fields
176        $i = 0;
177        $myxls->write_string(0, $i++, get_string("lastname"));
178        $myxls->write_string(0, $i++, get_string("firstname"));
179
180        // Add headers for extra user fields.
181        foreach ($extrafields as $field) {
182            $myxls->write_string(0, $i++, get_user_field_name($field));
183        }
184
185        $myxls->write_string(0, $i++, get_string("group"));
186        $myxls->write_string(0, $i++, get_string("choice", "choice"));
187
188        // Generate the data for the body of the spreadsheet.
189        $row = 1;
190        if ($users) {
191            foreach ($users as $option => $userid) {
192                $i = 0;
193                $option_text = choice_get_option_text($choice, $option);
194                foreach($userid as $user) {
195                    $i = 0;
196                    $myxls->write_string($row, $i++, $user->lastname);
197                    $myxls->write_string($row, $i++, $user->firstname);
198                    foreach ($extrafields as $field) {
199                        $myxls->write_string($row, $i++, $user->$field);
200                    }
201                    $ug2 = '';
202                    if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
203                        foreach ($usergrps as $ug) {
204                            $ug2 = $ug2 . $ug->name;
205                        }
206                    }
207                    $myxls->write_string($row, $i++, $ug2);
208                    if (isset($option_text)) {
209                        $myxls->write_string($row, $i++, format_string($option_text, true));
210                    }
211                    $row++;
212                }
213            }
214        }
215        /// Close the workbook
216        $workbook->close();
217        exit;
218    }
219
220    // print text file
221    if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
222        $shortname = format_string($course->shortname, true, array('context' => $context));
223        $choicename = format_string($choice->name, true, array('context' => $context));
224        $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.txt';
225
226        header("Content-Type: application/download\n");
227        header("Content-Disposition: attachment; filename=\"$filename\"");
228        header("Expires: 0");
229        header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
230        header("Pragma: public");
231
232        /// Print names of all the fields
233
234        echo get_string("lastname") . "\t" . get_string("firstname") . "\t";
235
236        // Add headers for extra user fields.
237        foreach ($extrafields as $field) {
238            echo get_user_field_name($field) . "\t";
239        }
240
241        echo get_string("group"). "\t";
242        echo get_string("choice","choice"). "\n";
243
244        /// generate the data for the body of the spreadsheet
245        $i=0;
246        if ($users) {
247            foreach ($users as $option => $userid) {
248                $option_text = choice_get_option_text($choice, $option);
249                foreach($userid as $user) {
250                    echo $user->lastname . "\t";
251                    echo $user->firstname . "\t";
252                    foreach ($extrafields as $field) {
253                        echo $user->$field . "\t";
254                    }
255                    $ug2 = '';
256                    if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
257                        foreach ($usergrps as $ug) {
258                            $ug2 = $ug2. $ug->name;
259                        }
260                    }
261                    echo $ug2. "\t";
262                    if (isset($option_text)) {
263                        echo format_string($option_text,true);
264                    }
265                    echo "\n";
266                }
267            }
268        }
269        exit;
270    }
271    $results = prepare_choice_show_results($choice, $course, $cm, $users);
272    $renderer = $PAGE->get_renderer('mod_choice');
273    echo $renderer->display_result($results, true);
274
275   //now give links for downloading spreadsheets.
276    if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
277        $downloadoptions = array();
278        $options = array();
279        $options["id"] = "$cm->id";
280        $options["download"] = "ods";
281        $button =  $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadods"));
282        $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
283
284        $options["download"] = "xls";
285        $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadexcel"));
286        $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
287
288        $options["download"] = "txt";
289        $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadtext"));
290        $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
291
292        $downloadlist = html_writer::tag('ul', implode('', $downloadoptions), array('class' => 'list-inline inline'));
293        $downloadlist .= html_writer::tag('div', '', array('class' => 'clearfloat'));
294        echo html_writer::tag('div',$downloadlist, array('class' => 'downloadreport mt-1'));
295    }
296    echo $OUTPUT->footer();
297
298