weekly release 3.4.5+
[moodle.git] / mod / choice / report.php
blob9e187d518eef2dcfc6bf4a6ff5de9c819b05cc23
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 $extrafields = get_extra_user_fields($context);
100 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
101 require_once("$CFG->libdir/odslib.class.php");
103 /// Calculate file name
104 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
105 /// Creating a workbook
106 $workbook = new MoodleODSWorkbook("-");
107 /// Send HTTP headers
108 $workbook->send($filename);
109 /// Creating the first worksheet
110 $myxls = $workbook->add_worksheet($strresponses);
112 /// Print names of all the fields
113 $i = 0;
114 $myxls->write_string(0, $i++, get_string("lastname"));
115 $myxls->write_string(0, $i++, get_string("firstname"));
117 // Add headers for extra user fields.
118 foreach ($extrafields as $field) {
119 $myxls->write_string(0, $i++, get_user_field_name($field));
122 $myxls->write_string(0, $i++, get_string("group"));
123 $myxls->write_string(0, $i++, get_string("choice", "choice"));
125 // Generate the data for the body of the spreadsheet.
126 $row = 1;
127 if ($users) {
128 foreach ($users as $option => $userid) {
129 $option_text = choice_get_option_text($choice, $option);
130 foreach ($userid as $user) {
131 $i = 0;
132 $myxls->write_string($row, $i++, $user->lastname);
133 $myxls->write_string($row, $i++, $user->firstname);
134 foreach ($extrafields as $field) {
135 $myxls->write_string($row, $i++, $user->$field);
137 $ug2 = '';
138 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
139 foreach ($usergrps as $ug) {
140 $ug2 = $ug2 . $ug->name;
143 $myxls->write_string($row, $i++, $ug2);
145 if (isset($option_text)) {
146 $myxls->write_string($row, $i++, format_string($option_text, true));
148 $row++;
152 /// Close the workbook
153 $workbook->close();
155 exit;
158 //print spreadsheet if one is asked for:
159 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
160 require_once("$CFG->libdir/excellib.class.php");
162 /// Calculate file name
163 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
164 /// Creating a workbook
165 $workbook = new MoodleExcelWorkbook("-");
166 /// Send HTTP headers
167 $workbook->send($filename);
168 /// Creating the first worksheet
169 $myxls = $workbook->add_worksheet($strresponses);
171 /// Print names of all the fields
172 $i = 0;
173 $myxls->write_string(0, $i++, get_string("lastname"));
174 $myxls->write_string(0, $i++, get_string("firstname"));
176 // Add headers for extra user fields.
177 foreach ($extrafields as $field) {
178 $myxls->write_string(0, $i++, get_user_field_name($field));
181 $myxls->write_string(0, $i++, get_string("group"));
182 $myxls->write_string(0, $i++, get_string("choice", "choice"));
184 // Generate the data for the body of the spreadsheet.
185 $row = 1;
186 if ($users) {
187 foreach ($users as $option => $userid) {
188 $i = 0;
189 $option_text = choice_get_option_text($choice, $option);
190 foreach($userid as $user) {
191 $i = 0;
192 $myxls->write_string($row, $i++, $user->lastname);
193 $myxls->write_string($row, $i++, $user->firstname);
194 foreach ($extrafields as $field) {
195 $myxls->write_string($row, $i++, $user->$field);
197 $ug2 = '';
198 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
199 foreach ($usergrps as $ug) {
200 $ug2 = $ug2 . $ug->name;
203 $myxls->write_string($row, $i++, $ug2);
204 if (isset($option_text)) {
205 $myxls->write_string($row, $i++, format_string($option_text, true));
207 $row++;
211 /// Close the workbook
212 $workbook->close();
213 exit;
216 // print text file
217 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
218 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
220 header("Content-Type: application/download\n");
221 header("Content-Disposition: attachment; filename=\"$filename\"");
222 header("Expires: 0");
223 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
224 header("Pragma: public");
226 /// Print names of all the fields
228 echo get_string("lastname") . "\t" . get_string("firstname") . "\t";
230 // Add headers for extra user fields.
231 foreach ($extrafields as $field) {
232 echo get_user_field_name($field) . "\t";
235 echo get_string("group"). "\t";
236 echo get_string("choice","choice"). "\n";
238 /// generate the data for the body of the spreadsheet
239 $i=0;
240 if ($users) {
241 foreach ($users as $option => $userid) {
242 $option_text = choice_get_option_text($choice, $option);
243 foreach($userid as $user) {
244 echo $user->lastname . "\t";
245 echo $user->firstname . "\t";
246 foreach ($extrafields as $field) {
247 echo $user->$field . "\t";
249 $ug2 = '';
250 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
251 foreach ($usergrps as $ug) {
252 $ug2 = $ug2. $ug->name;
255 echo $ug2. "\t";
256 if (isset($option_text)) {
257 echo format_string($option_text,true);
259 echo "\n";
263 exit;
265 $results = prepare_choice_show_results($choice, $course, $cm, $users);
266 $renderer = $PAGE->get_renderer('mod_choice');
267 echo $renderer->display_result($results, true);
269 //now give links for downloading spreadsheets.
270 if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
271 $downloadoptions = array();
272 $options = array();
273 $options["id"] = "$cm->id";
274 $options["download"] = "ods";
275 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadods"));
276 $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
278 $options["download"] = "xls";
279 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadexcel"));
280 $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
282 $options["download"] = "txt";
283 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadtext"));
284 $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
286 $downloadlist = html_writer::tag('ul', implode('', $downloadoptions), array('class' => 'list-inline inline'));
287 $downloadlist .= html_writer::tag('div', '', array('class' => 'clearfloat'));
288 echo html_writer::tag('div',$downloadlist, array('class' => 'downloadreport m-t-1'));
290 echo $OUTPUT->footer();