MDL-77656 quiz editing: customisable numbers only in unshuffled sections
[moodle.git] / mod / choice / report.php
blob845ff896d9e4eaf44e83ec6f7d655c3c9aedc8b2
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 throw new \moodle_exception("invalidcoursemodule");
25 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
26 throw new \moodle_exception("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 throw new \moodle_exception('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 $groupmode = groups_get_activity_groupmode($cm);
68 if (!$download) {
69 $PAGE->set_title(format_string($choice->name).": $strresponses");
70 $PAGE->set_heading($course->fullname);
71 $PAGE->activityheader->set_attrs([
72 'hidecompletion' => true,
73 'description' => ''
74 ]);
75 echo $OUTPUT->header();
76 echo $OUTPUT->heading($strresponses);
78 /// Check to see if groups are being used in this choice
79 if ($groupmode) {
80 groups_get_activity_group($cm, true);
81 $groupsactivitymenu = groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/report.php?id=' . $id,
82 true);
83 echo html_writer::div($groupsactivitymenu, 'mb-2');
85 } else {
86 // Trigger the report downloaded event.
87 $eventdata = array();
88 $eventdata['context'] = $context;
89 $eventdata['courseid'] = $course->id;
90 $eventdata['other']['content'] = 'choicereportcontentviewed';
91 $eventdata['other']['format'] = $download;
92 $eventdata['other']['choiceid'] = $choice->id;
93 $event = \mod_choice\event\report_downloaded::create($eventdata);
94 $event->trigger();
98 // Check if we want to include responses from inactive users.
99 $onlyactive = $choice->includeinactive ? false : true;
101 $users = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
103 // TODO Does not support custom user profile fields (MDL-70456).
104 $extrafields = \core_user\fields::get_identity_fields($context, false);
106 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
107 require_once("$CFG->libdir/odslib.class.php");
109 /// Calculate file name
110 $shortname = format_string($course->shortname, true, array('context' => $context));
111 $choicename = format_string($choice->name, true, array('context' => $context));
112 $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.ods';
113 /// Creating a workbook
114 $workbook = new MoodleODSWorkbook("-");
115 /// Send HTTP headers
116 $workbook->send($filename);
117 /// Creating the first worksheet
118 $myxls = $workbook->add_worksheet($strresponses);
120 /// Print names of all the fields
121 $i = 0;
122 $myxls->write_string(0, $i++, get_string("lastname"));
123 $myxls->write_string(0, $i++, get_string("firstname"));
125 // Add headers for extra user fields.
126 foreach ($extrafields as $field) {
127 $myxls->write_string(0, $i++, \core_user\fields::get_display_name($field));
130 $myxls->write_string(0, $i++, get_string("group"));
131 $myxls->write_string(0, $i++, get_string("choice", "choice"));
133 // Generate the data for the body of the spreadsheet.
134 $row = 1;
135 if ($users) {
136 foreach ($users as $option => $userid) {
137 if (!($choice->showunanswered == 0 && $option == 0)) {
138 $option_text = choice_get_option_text($choice, $option);
139 foreach ($userid as $user) {
140 $i = 0;
141 $myxls->write_string($row, $i++, $user->lastname);
142 $myxls->write_string($row, $i++, $user->firstname);
143 foreach ($extrafields as $field) {
144 $myxls->write_string($row, $i++, $user->$field);
146 $ug2 = '';
147 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
148 foreach ($usergrps as $ug) {
149 $ug2 = $ug2 . $ug->name;
152 $myxls->write_string($row, $i++, $ug2);
154 if (isset($option_text)) {
155 $myxls->write_string($row, $i++, format_string($option_text, true));
157 $row++;
162 /// Close the workbook
163 $workbook->close();
165 exit;
168 //print spreadsheet if one is asked for:
169 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
170 require_once("$CFG->libdir/excellib.class.php");
172 /// Calculate file name
173 $shortname = format_string($course->shortname, true, array('context' => $context));
174 $choicename = format_string($choice->name, true, array('context' => $context));
175 $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.xls';
176 /// Creating a workbook
177 $workbook = new MoodleExcelWorkbook("-");
178 /// Send HTTP headers
179 $workbook->send($filename);
180 /// Creating the first worksheet
181 $myxls = $workbook->add_worksheet($strresponses);
183 /// Print names of all the fields
184 $i = 0;
185 $myxls->write_string(0, $i++, get_string("lastname"));
186 $myxls->write_string(0, $i++, get_string("firstname"));
188 // Add headers for extra user fields.
189 foreach ($extrafields as $field) {
190 $myxls->write_string(0, $i++, \core_user\fields::get_display_name($field));
193 $myxls->write_string(0, $i++, get_string("group"));
194 $myxls->write_string(0, $i++, get_string("choice", "choice"));
196 // Generate the data for the body of the spreadsheet.
197 $row = 1;
198 if ($users) {
199 foreach ($users as $option => $userid) {
200 $i = 0;
201 if (!($choice->showunanswered == 0 && $option == 0)) {
202 $option_text = choice_get_option_text($choice, $option);
203 foreach($userid as $user) {
204 $i = 0;
205 $myxls->write_string($row, $i++, $user->lastname);
206 $myxls->write_string($row, $i++, $user->firstname);
207 foreach ($extrafields as $field) {
208 $myxls->write_string($row, $i++, $user->$field);
210 $ug2 = '';
211 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
212 foreach ($usergrps as $ug) {
213 $ug2 = $ug2 . $ug->name;
216 $myxls->write_string($row, $i++, $ug2);
217 if (isset($option_text)) {
218 $myxls->write_string($row, $i++, format_string($option_text, true));
220 $row++;
225 /// Close the workbook
226 $workbook->close();
227 exit;
230 // print text file
231 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
232 $shortname = format_string($course->shortname, true, array('context' => $context));
233 $choicename = format_string($choice->name, true, array('context' => $context));
234 $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.txt';
236 header("Content-Type: application/download\n");
237 header("Content-Disposition: attachment; filename=\"$filename\"");
238 header("Expires: 0");
239 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
240 header("Pragma: public");
242 /// Print names of all the fields
244 echo get_string("lastname") . "\t" . get_string("firstname") . "\t";
246 // Add headers for extra user fields.
247 foreach ($extrafields as $field) {
248 echo \core_user\fields::get_display_name($field) . "\t";
251 echo get_string("group"). "\t";
252 echo get_string("choice","choice"). "\n";
254 /// generate the data for the body of the spreadsheet
255 $i=0;
256 if ($users) {
257 foreach ($users as $option => $userid) {
258 if (!($choice->showunanswered == 0 && $option == 0)) {
259 $option_text = choice_get_option_text($choice, $option);
260 foreach($userid as $user) {
261 echo $user->lastname . "\t";
262 echo $user->firstname . "\t";
263 foreach ($extrafields as $field) {
264 echo $user->$field . "\t";
266 $ug2 = '';
267 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
268 foreach ($usergrps as $ug) {
269 $ug2 = $ug2. $ug->name;
272 echo $ug2. "\t";
273 if (isset($option_text)) {
274 echo format_string($option_text,true);
276 echo "\n";
281 exit;
283 $results = prepare_choice_show_results($choice, $course, $cm, $users);
284 $renderer = $PAGE->get_renderer('mod_choice');
285 echo $renderer->display_result($results, true);
287 //now give links for downloading spreadsheets.
288 if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
289 $downloadoptions = array();
290 $options = array();
291 $options["id"] = "$cm->id";
292 $options["download"] = "ods";
293 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadods"));
294 $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
296 $options["download"] = "xls";
297 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadexcel"));
298 $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
300 $options["download"] = "txt";
301 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadtext"));
302 $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
304 $downloadlist = html_writer::tag('ul', implode('', $downloadoptions), array('class' => 'list-inline inline'));
305 $downloadlist .= html_writer::tag('div', '', array('class' => 'clearfloat'));
306 echo html_writer::tag('div',$downloadlist, array('class' => 'downloadreport mt-1'));
308 echo $OUTPUT->footer();