b1723dd2620327a3d145da41c5ed14b8fbb5a7f9
[moodle.git] / mod / choice / report.php
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);
15     }
16     if ($download !== '') {
17         $url->param('download', $download);
18     }
19     if ($action !== '') {
20         $url->param('action', $action);
21     }
22     $PAGE->set_url($url);
24     if (! $cm = get_coursemodule_from_id('choice', $id)) {
25         print_error("invalidcoursemodule");
26     }
28     if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
29         print_error("coursemisconf");
30     }
32     require_login($course, false, $cm);
34     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
36     require_capability('mod/choice:readresponses', $context);
38     if (!$choice = choice_get_choice($cm->instance)) {
39         print_error('invalidcoursemodule');
40     }
42     $strchoice = get_string("modulename", "choice");
43     $strchoices = get_string("modulenameplural", "choice");
44     $strresponses = get_string("responses", "choice");
46     add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
48     if (data_submitted() && $action == 'delete' && has_capability('mod/choice:deleteresponses',$context) && confirm_sesskey()) {
49         choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
50         redirect("report.php?id=$cm->id");
51     }
53     if (!$download) {
54         $PAGE->navbar->add($strresponses);
55         $PAGE->set_title(format_string($choice->name).": $strresponses");
56         $PAGE->set_heading($course->fullname);
57         echo $OUTPUT->header();
58         /// Check to see if groups are being used in this choice
59         $groupmode = groups_get_activity_groupmode($cm);
60         if ($groupmode) {
61             groups_get_activity_group($cm, true);
62             groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/report.php?id='.$id);
63         }
64     } else {
65         $groupmode = groups_get_activity_groupmode($cm);
66     }
67     $users = choice_get_response_data($choice, $cm, $groupmode);
69     if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
70         require_once("$CFG->libdir/odslib.class.php");
72     /// Calculate file name
73         $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
74     /// Creating a workbook
75         $workbook = new MoodleODSWorkbook("-");
76     /// Send HTTP headers
77         $workbook->send($filename);
78     /// Creating the first worksheet
79         $myxls =& $workbook->add_worksheet($strresponses);
81     /// Print names of all the fields
82         $myxls->write_string(0,0,get_string("lastname"));
83         $myxls->write_string(0,1,get_string("firstname"));
84         $myxls->write_string(0,2,get_string("idnumber"));
85         $myxls->write_string(0,3,get_string("group"));
86         $myxls->write_string(0,4,get_string("choice","choice"));
88     /// generate the data for the body of the spreadsheet
89         $i=0;
90         $row=1;
91         if ($users) {
92             foreach ($users as $option => $userid) {
93                 $option_text = choice_get_option_text($choice, $option);
94                 foreach($userid as $user) {
95                     $myxls->write_string($row,0,$user->lastname);
96                     $myxls->write_string($row,1,$user->firstname);
97                     $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
98                     $myxls->write_string($row,2,$studentid);
99                     $ug2 = '';
100                     if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
101                         foreach ($usergrps as $ug) {
102                             $ug2 = $ug2. $ug->name;
103                         }
104                     }
105                     $myxls->write_string($row,3,$ug2);
107                     if (isset($option_text)) {
108                         $myxls->write_string($row,4,format_string($option_text,true));
109                     }
110                     $row++;
111                     $pos=4;
112                 }
113             }
114         }
115         /// Close the workbook
116         $workbook->close();
118         exit;
119     }
121     //print spreadsheet if one is asked for:
122     if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
123         require_once("$CFG->libdir/excellib.class.php");
125     /// Calculate file name
126         $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
127     /// Creating a workbook
128         $workbook = new MoodleExcelWorkbook("-");
129     /// Send HTTP headers
130         $workbook->send($filename);
131     /// Creating the first worksheet
132         $myxls =& $workbook->add_worksheet($strresponses);
134     /// Print names of all the fields
135         $myxls->write_string(0,0,get_string("lastname"));
136         $myxls->write_string(0,1,get_string("firstname"));
137         $myxls->write_string(0,2,get_string("idnumber"));
138         $myxls->write_string(0,3,get_string("group"));
139         $myxls->write_string(0,4,get_string("choice","choice"));
142     /// generate the data for the body of the spreadsheet
143         $i=0;
144         $row=1;
145         if ($users) {
146             foreach ($users as $option => $userid) {
147                 $option_text = choice_get_option_text($choice, $option);
148                 foreach($userid as $user) {
149                     $myxls->write_string($row,0,$user->lastname);
150                     $myxls->write_string($row,1,$user->firstname);
151                     $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
152                     $myxls->write_string($row,2,$studentid);
153                     $ug2 = '';
154                     if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
155                         foreach ($usergrps as $ug) {
156                             $ug2 = $ug2. $ug->name;
157                         }
158                     }
159                     $myxls->write_string($row,3,$ug2);
160                     if (isset($option_text)) {
161                         $myxls->write_string($row,4,format_string($option_text,true));
162                     }
163                     $row++;
164                 }
165             }
166             $pos=4;
167         }
168         /// Close the workbook
169         $workbook->close();
170         exit;
171     }
173     // print text file
174     if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
175         $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
177         header("Content-Type: application/download\n");
178         header("Content-Disposition: attachment; filename=\"$filename\"");
179         header("Expires: 0");
180         header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
181         header("Pragma: public");
183         /// Print names of all the fields
185         echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
186         echo get_string("group"). "\t";
187         echo get_string("choice","choice"). "\n";
189         /// generate the data for the body of the spreadsheet
190         $i=0;
191         if ($users) {
192             foreach ($users as $option => $userid) {
193                 $option_text = choice_get_option_text($choice, $option);
194                 foreach($userid as $user) {
195                     echo $user->lastname;
196                     echo "\t".$user->firstname;
197                     $studentid = " ";
198                     if (!empty($user->idnumber)) {
199                         $studentid = $user->idnumber;
200                     }
201                     echo "\t". $studentid."\t";
202                     $ug2 = '';
203                     if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
204                         foreach ($usergrps as $ug) {
205                             $ug2 = $ug2. $ug->name;
206                         }
207                     }
208                     echo $ug2. "\t";
209                     if (isset($option_text)) {
210                         echo format_string($option_text,true);
211                     }
212                     echo "\n";
213                 }
214             }
215         }
216         exit;
217     }
218     // Show those who haven't answered the question.
219     if (!empty($choice->showunanswered)) {
220         $choice->option[0] = get_string('notanswered', 'choice');
221         $choice->maxanswers[0] = 0;
222     }
224     $results = prepare_choice_show_results($choice, $course, $cm, $users);
225     $renderer = $PAGE->get_renderer('mod_choice');
226     echo $renderer->display_result($results, has_capability('mod/choice:readresponses', $context));
228    //now give links for downloading spreadsheets.
229     if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
230         $downloadoptions = array();
231         $options = array();
232         $options["id"] = "$cm->id";
233         $options["download"] = "ods";
234         $button =  $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadods"));
235         $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
237         $options["download"] = "xls";
238         $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadexcel"));
239         $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
241         $options["download"] = "txt";
242         $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadtext"));
243         $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
245         $downloadlist = html_writer::tag('ul', implode('', $downloadoptions));
246         $downloadlist .= html_writer::tag('div', '', array('class'=>'clearfloat'));
247         echo html_writer::tag('div',$downloadlist, array('class'=>'downloadreport'));
248     }
249     echo $OUTPUT->footer();