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);
17 $url->param('action', $action);
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");
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);
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");
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);
75 groups_get_activity_group($cm, true);
76 groups_print_activity_menu($cm, $CFG->wwwroot
. '/mod/choice/report.php?id='.$id);
79 $groupmode = groups_get_activity_groupmode($cm);
81 // Trigger the report downloaded event.
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);
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 // TODO Does not support custom user profile fields (MDL-70456).
99 $extrafields = \core_user\fields
::get_identity_fields($context, false);
101 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
102 require_once("$CFG->libdir/odslib.class.php");
104 /// Calculate file name
105 $shortname = format_string($course->shortname
, true, array('context' => $context));
106 $choicename = format_string($choice->name
, true, array('context' => $context));
107 $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.ods';
108 /// Creating a workbook
109 $workbook = new MoodleODSWorkbook("-");
110 /// Send HTTP headers
111 $workbook->send($filename);
112 /// Creating the first worksheet
113 $myxls = $workbook->add_worksheet($strresponses);
115 /// Print names of all the fields
117 $myxls->write_string(0, $i++
, get_string("lastname"));
118 $myxls->write_string(0, $i++
, get_string("firstname"));
120 // Add headers for extra user fields.
121 foreach ($extrafields as $field) {
122 $myxls->write_string(0, $i++
, \core_user\fields
::get_display_name($field));
125 $myxls->write_string(0, $i++
, get_string("group"));
126 $myxls->write_string(0, $i++
, get_string("choice", "choice"));
128 // Generate the data for the body of the spreadsheet.
131 foreach ($users as $option => $userid) {
132 $option_text = choice_get_option_text($choice, $option);
133 foreach ($userid as $user) {
135 $myxls->write_string($row, $i++
, $user->lastname
);
136 $myxls->write_string($row, $i++
, $user->firstname
);
137 foreach ($extrafields as $field) {
138 $myxls->write_string($row, $i++
, $user->$field);
141 if ($usergrps = groups_get_all_groups($course->id
, $user->id
)) {
142 foreach ($usergrps as $ug) {
143 $ug2 = $ug2 . $ug->name
;
146 $myxls->write_string($row, $i++
, $ug2);
148 if (isset($option_text)) {
149 $myxls->write_string($row, $i++
, format_string($option_text, true));
155 /// Close the workbook
161 //print spreadsheet if one is asked for:
162 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
163 require_once("$CFG->libdir/excellib.class.php");
165 /// Calculate file name
166 $shortname = format_string($course->shortname
, true, array('context' => $context));
167 $choicename = format_string($choice->name
, true, array('context' => $context));
168 $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.xls';
169 /// Creating a workbook
170 $workbook = new MoodleExcelWorkbook("-");
171 /// Send HTTP headers
172 $workbook->send($filename);
173 /// Creating the first worksheet
174 $myxls = $workbook->add_worksheet($strresponses);
176 /// Print names of all the fields
178 $myxls->write_string(0, $i++
, get_string("lastname"));
179 $myxls->write_string(0, $i++
, get_string("firstname"));
181 // Add headers for extra user fields.
182 foreach ($extrafields as $field) {
183 $myxls->write_string(0, $i++
, \core_user\fields
::get_display_name($field));
186 $myxls->write_string(0, $i++
, get_string("group"));
187 $myxls->write_string(0, $i++
, get_string("choice", "choice"));
189 // Generate the data for the body of the spreadsheet.
192 foreach ($users as $option => $userid) {
194 $option_text = choice_get_option_text($choice, $option);
195 foreach($userid as $user) {
197 $myxls->write_string($row, $i++
, $user->lastname
);
198 $myxls->write_string($row, $i++
, $user->firstname
);
199 foreach ($extrafields as $field) {
200 $myxls->write_string($row, $i++
, $user->$field);
203 if ($usergrps = groups_get_all_groups($course->id
, $user->id
)) {
204 foreach ($usergrps as $ug) {
205 $ug2 = $ug2 . $ug->name
;
208 $myxls->write_string($row, $i++
, $ug2);
209 if (isset($option_text)) {
210 $myxls->write_string($row, $i++
, format_string($option_text, true));
216 /// Close the workbook
222 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
223 $shortname = format_string($course->shortname
, true, array('context' => $context));
224 $choicename = format_string($choice->name
, true, array('context' => $context));
225 $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.txt';
227 header("Content-Type: application/download\n");
228 header("Content-Disposition: attachment; filename=\"$filename\"");
229 header("Expires: 0");
230 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
231 header("Pragma: public");
233 /// Print names of all the fields
235 echo get_string("lastname") . "\t" . get_string("firstname") . "\t";
237 // Add headers for extra user fields.
238 foreach ($extrafields as $field) {
239 echo \core_user\fields
::get_display_name($field) . "\t";
242 echo get_string("group"). "\t";
243 echo get_string("choice","choice"). "\n";
245 /// generate the data for the body of the spreadsheet
248 foreach ($users as $option => $userid) {
249 $option_text = choice_get_option_text($choice, $option);
250 foreach($userid as $user) {
251 echo $user->lastname
. "\t";
252 echo $user->firstname
. "\t";
253 foreach ($extrafields as $field) {
254 echo $user->$field . "\t";
257 if ($usergrps = groups_get_all_groups($course->id
, $user->id
)) {
258 foreach ($usergrps as $ug) {
259 $ug2 = $ug2. $ug->name
;
263 if (isset($option_text)) {
264 echo format_string($option_text,true);
272 $results = prepare_choice_show_results($choice, $course, $cm, $users);
273 $renderer = $PAGE->get_renderer('mod_choice');
274 echo $renderer->display_result($results, true);
276 //now give links for downloading spreadsheets.
277 if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
278 $downloadoptions = array();
280 $options["id"] = "$cm->id";
281 $options["download"] = "ods";
282 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadods"));
283 $downloadoptions[] = html_writer
::tag('li', $button, array('class' => 'reportoption list-inline-item'));
285 $options["download"] = "xls";
286 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadexcel"));
287 $downloadoptions[] = html_writer
::tag('li', $button, array('class' => 'reportoption list-inline-item'));
289 $options["download"] = "txt";
290 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadtext"));
291 $downloadoptions[] = html_writer
::tag('li', $button, array('class' => 'reportoption list-inline-item'));
293 $downloadlist = html_writer
::tag('ul', implode('', $downloadoptions), array('class' => 'list-inline inline'));
294 $downloadlist .= html_writer
::tag('div', '', array('class' => 'clearfloat'));
295 echo html_writer
::tag('div',$downloadlist, array('class' => 'downloadreport mt-1'));
297 echo $OUTPUT->footer();