MDL-44221 editor_atto: convert plugins to use insert_html_at_focus_point
[moodle.git] / mod / choice / report.php
blob83244b171a3700924e45106d90dfab4ed8c6b4fd
1 <?php
3 require_once("../../config.php");
4 require_once("lib.php");
6 $id = required_param('id', PARAM_INT); //moduleid
7 $format = optional_param('format', CHOICE_PUBLISH_NAMES, PARAM_INT);
8 $download = optional_param('download', '', PARAM_ALPHA);
9 $action = optional_param('action', '', PARAM_ALPHA);
10 $attemptids = optional_param_array('attemptid', array(), PARAM_INT); //get array of responses to delete.
12 $url = new moodle_url('/mod/choice/report.php', array('id'=>$id));
13 if ($format !== CHOICE_PUBLISH_NAMES) {
14 $url->param('format', $format);
16 if ($download !== '') {
17 $url->param('download', $download);
19 if ($action !== '') {
20 $url->param('action', $action);
22 $PAGE->set_url($url);
24 if (! $cm = get_coursemodule_from_id('choice', $id)) {
25 print_error("invalidcoursemodule");
28 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
29 print_error("coursemisconf");
32 require_login($course, false, $cm);
34 $context = context_module::instance($cm->id);
36 require_capability('mod/choice:readresponses', $context);
38 if (!$choice = choice_get_choice($cm->instance)) {
39 print_error('invalidcoursemodule');
42 $strchoice = get_string("modulename", "choice");
43 $strchoices = get_string("modulenameplural", "choice");
44 $strresponses = get_string("responses", "choice");
46 $eventdata = array();
47 $eventdata['objectid'] = $choice->id;
48 $eventdata['context'] = $context;
49 $eventdata['courseid'] = $course->id;
50 $eventdata['other']['content'] = 'choicereportcontentviewed';
52 $event = \mod_choice\event\report_viewed::create($eventdata);
53 $event->set_page_detail();
54 $event->trigger();
56 if (data_submitted() && $action == 'delete' && has_capability('mod/choice:deleteresponses',$context) && confirm_sesskey()) {
57 choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
58 redirect("report.php?id=$cm->id");
61 if (!$download) {
62 $PAGE->navbar->add($strresponses);
63 $PAGE->set_title(format_string($choice->name).": $strresponses");
64 $PAGE->set_heading($course->fullname);
65 echo $OUTPUT->header();
66 echo $OUTPUT->heading($choice->name, 2, null);
67 /// Check to see if groups are being used in this choice
68 $groupmode = groups_get_activity_groupmode($cm);
69 if ($groupmode) {
70 groups_get_activity_group($cm, true);
71 groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/report.php?id='.$id);
73 } else {
74 $groupmode = groups_get_activity_groupmode($cm);
76 $users = choice_get_response_data($choice, $cm, $groupmode);
78 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
79 require_once("$CFG->libdir/odslib.class.php");
81 /// Calculate file name
82 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
83 /// Creating a workbook
84 $workbook = new MoodleODSWorkbook("-");
85 /// Send HTTP headers
86 $workbook->send($filename);
87 /// Creating the first worksheet
88 $myxls = $workbook->add_worksheet($strresponses);
90 /// Print names of all the fields
91 $myxls->write_string(0,0,get_string("lastname"));
92 $myxls->write_string(0,1,get_string("firstname"));
93 $myxls->write_string(0,2,get_string("idnumber"));
94 $myxls->write_string(0,3,get_string("group"));
95 $myxls->write_string(0,4,get_string("choice","choice"));
97 /// generate the data for the body of the spreadsheet
98 $i=0;
99 $row=1;
100 if ($users) {
101 foreach ($users as $option => $userid) {
102 $option_text = choice_get_option_text($choice, $option);
103 foreach($userid as $user) {
104 $myxls->write_string($row,0,$user->lastname);
105 $myxls->write_string($row,1,$user->firstname);
106 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
107 $myxls->write_string($row,2,$studentid);
108 $ug2 = '';
109 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
110 foreach ($usergrps as $ug) {
111 $ug2 = $ug2. $ug->name;
114 $myxls->write_string($row,3,$ug2);
116 if (isset($option_text)) {
117 $myxls->write_string($row,4,format_string($option_text,true));
119 $row++;
120 $pos=4;
124 /// Close the workbook
125 $workbook->close();
127 exit;
130 //print spreadsheet if one is asked for:
131 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
132 require_once("$CFG->libdir/excellib.class.php");
134 /// Calculate file name
135 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
136 /// Creating a workbook
137 $workbook = new MoodleExcelWorkbook("-");
138 /// Send HTTP headers
139 $workbook->send($filename);
140 /// Creating the first worksheet
141 $myxls = $workbook->add_worksheet($strresponses);
143 /// Print names of all the fields
144 $myxls->write_string(0,0,get_string("lastname"));
145 $myxls->write_string(0,1,get_string("firstname"));
146 $myxls->write_string(0,2,get_string("idnumber"));
147 $myxls->write_string(0,3,get_string("group"));
148 $myxls->write_string(0,4,get_string("choice","choice"));
151 /// generate the data for the body of the spreadsheet
152 $i=0;
153 $row=1;
154 if ($users) {
155 foreach ($users as $option => $userid) {
156 $option_text = choice_get_option_text($choice, $option);
157 foreach($userid as $user) {
158 $myxls->write_string($row,0,$user->lastname);
159 $myxls->write_string($row,1,$user->firstname);
160 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
161 $myxls->write_string($row,2,$studentid);
162 $ug2 = '';
163 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
164 foreach ($usergrps as $ug) {
165 $ug2 = $ug2. $ug->name;
168 $myxls->write_string($row,3,$ug2);
169 if (isset($option_text)) {
170 $myxls->write_string($row,4,format_string($option_text,true));
172 $row++;
175 $pos=4;
177 /// Close the workbook
178 $workbook->close();
179 exit;
182 // print text file
183 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
184 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
186 header("Content-Type: application/download\n");
187 header("Content-Disposition: attachment; filename=\"$filename\"");
188 header("Expires: 0");
189 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
190 header("Pragma: public");
192 /// Print names of all the fields
194 echo get_string("lastname")."\t".get_string("firstname") . "\t". get_string("idnumber") . "\t";
195 echo get_string("group"). "\t";
196 echo get_string("choice","choice"). "\n";
198 /// generate the data for the body of the spreadsheet
199 $i=0;
200 if ($users) {
201 foreach ($users as $option => $userid) {
202 $option_text = choice_get_option_text($choice, $option);
203 foreach($userid as $user) {
204 echo $user->lastname;
205 echo "\t".$user->firstname;
206 $studentid = " ";
207 if (!empty($user->idnumber)) {
208 $studentid = $user->idnumber;
210 echo "\t". $studentid."\t";
211 $ug2 = '';
212 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
213 foreach ($usergrps as $ug) {
214 $ug2 = $ug2. $ug->name;
217 echo $ug2. "\t";
218 if (isset($option_text)) {
219 echo format_string($option_text,true);
221 echo "\n";
225 exit;
227 // Show those who haven't answered the question.
228 if (!empty($choice->showunanswered)) {
229 $choice->option[0] = get_string('notanswered', 'choice');
230 $choice->maxanswers[0] = 0;
233 $results = prepare_choice_show_results($choice, $course, $cm, $users);
234 $renderer = $PAGE->get_renderer('mod_choice');
235 echo $renderer->display_result($results, has_capability('mod/choice:readresponses', $context));
237 //now give links for downloading spreadsheets.
238 if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
239 $downloadoptions = array();
240 $options = array();
241 $options["id"] = "$cm->id";
242 $options["download"] = "ods";
243 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadods"));
244 $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
246 $options["download"] = "xls";
247 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadexcel"));
248 $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
250 $options["download"] = "txt";
251 $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadtext"));
252 $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
254 $downloadlist = html_writer::tag('ul', implode('', $downloadoptions));
255 $downloadlist .= html_writer::tag('div', '', array('class'=>'clearfloat'));
256 echo html_writer::tag('div',$downloadlist, array('class'=>'downloadreport'));
258 echo $OUTPUT->footer();