Updated the 19 build version to 20101023
[moodle.git] / mod / choice / report.php
blob4ecbb36b2dc29260c3cee65775ae175602cd807e
1 <?php // $Id$
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('attemptid', array(), PARAM_INT); //get array of responses to delete.
12 if (! $cm = get_coursemodule_from_id('choice', $id)) {
13 error("Course Module ID was incorrect");
16 if (! $course = get_record("course", "id", $cm->course)) {
17 error("Course module is misconfigured");
20 require_login($course->id, false, $cm);
22 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
24 require_capability('mod/choice:readresponses', $context);
26 if (!$choice = choice_get_choice($cm->instance)) {
27 error("Course module is incorrect");
30 $strchoice = get_string("modulename", "choice");
31 $strchoices = get_string("modulenameplural", "choice");
32 $strresponses = get_string("responses", "choice");
34 add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
36 if (data_submitted() && $action == 'delete' && has_capability('mod/choice:deleteresponses',$context) && confirm_sesskey()) {
37 choice_delete_responses($attemptids, $choice->id); //delete responses.
38 redirect("report.php?id=$cm->id");
41 if (!$download) {
43 $navigation = build_navigation($strresponses, $cm);
44 print_header_simple(format_string($choice->name).": $strresponses", "", $navigation, "", '', true,
45 update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
46 /// Check to see if groups are being used in this choice
47 $groupmode = groups_get_activity_groupmode($cm);
48 if ($groupmode) {
49 groups_get_activity_group($cm, true);
50 groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/report.php?id='.$id);
52 } else {
53 $groupmode = groups_get_activity_groupmode($cm);
55 $users = choice_get_response_data($choice, $cm, $groupmode);
57 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
58 require_once("$CFG->libdir/odslib.class.php");
60 /// Calculate file name
61 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
62 /// Creating a workbook
63 $workbook = new MoodleODSWorkbook("-");
64 /// Send HTTP headers
65 $workbook->send($filename);
66 /// Creating the first worksheet
67 $myxls =& $workbook->add_worksheet($strresponses);
69 /// Print names of all the fields
70 $myxls->write_string(0,0,get_string("lastname"));
71 $myxls->write_string(0,1,get_string("firstname"));
72 $myxls->write_string(0,2,get_string("idnumber"));
73 $myxls->write_string(0,3,get_string("group"));
74 $myxls->write_string(0,4,get_string("choice","choice"));
76 /// generate the data for the body of the spreadsheet
77 $i=0;
78 $row=1;
79 if ($users) {
80 foreach ($users as $option => $userid) {
81 $option_text = choice_get_option_text($choice, $option);
82 foreach($userid as $user) {
83 $myxls->write_string($row,0,$user->lastname);
84 $myxls->write_string($row,1,$user->firstname);
85 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
86 $myxls->write_string($row,2,$studentid);
87 $ug2 = '';
88 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
89 foreach ($usergrps as $ug) {
90 $ug2 = $ug2. $ug->name;
93 $myxls->write_string($row,3,$ug2);
95 if (isset($option_text)) {
96 $myxls->write_string($row,4,format_string($option_text,true));
98 $row++;
99 $pos=4;
103 /// Close the workbook
104 $workbook->close();
106 exit;
109 //print spreadsheet if one is asked for:
110 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
111 require_once("$CFG->libdir/excellib.class.php");
113 /// Calculate file name
114 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
115 /// Creating a workbook
116 $workbook = new MoodleExcelWorkbook("-");
117 /// Send HTTP headers
118 $workbook->send($filename);
119 /// Creating the first worksheet
120 $myxls =& $workbook->add_worksheet($strresponses);
122 /// Print names of all the fields
123 $myxls->write_string(0,0,get_string("lastname"));
124 $myxls->write_string(0,1,get_string("firstname"));
125 $myxls->write_string(0,2,get_string("idnumber"));
126 $myxls->write_string(0,3,get_string("group"));
127 $myxls->write_string(0,4,get_string("choice","choice"));
130 /// generate the data for the body of the spreadsheet
131 $i=0;
132 $row=1;
133 if ($users) {
134 foreach ($users as $option => $userid) {
135 $option_text = choice_get_option_text($choice, $option);
136 foreach($userid as $user) {
137 $myxls->write_string($row,0,$user->lastname);
138 $myxls->write_string($row,1,$user->firstname);
139 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
140 $myxls->write_string($row,2,$studentid);
141 $ug2 = '';
142 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
143 foreach ($usergrps as $ug) {
144 $ug2 = $ug2. $ug->name;
147 $myxls->write_string($row,3,$ug2);
148 if (isset($option_text)) {
149 $myxls->write_string($row,4,format_string($option_text,true));
151 $row++;
154 $pos=4;
156 /// Close the workbook
157 $workbook->close();
158 exit;
161 // print text file
162 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
163 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
165 header("Content-Type: application/download\n");
166 header("Content-Disposition: attachment; filename=\"$filename\"");
167 header("Expires: 0");
168 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
169 header("Pragma: public");
171 /// Print names of all the fields
173 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
174 echo get_string("group"). "\t";
175 echo get_string("choice","choice"). "\n";
177 /// generate the data for the body of the spreadsheet
178 $i=0;
179 if ($users) {
180 foreach ($users as $option => $userid) {
181 $option_text = choice_get_option_text($choice, $option);
182 foreach($userid as $user) {
183 echo $user->lastname;
184 echo "\t".$user->firstname;
185 $studentid = " ";
186 if (!empty($user->idnumber)) {
187 $studentid = $user->idnumber;
189 echo "\t". $studentid."\t";
190 $ug2 = '';
191 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
192 foreach ($usergrps as $ug) {
193 $ug2 = $ug2. $ug->name;
196 echo $ug2. "\t";
197 if (isset($option_text)) {
198 echo format_string($option_text,true);
200 echo "\n";
204 exit;
206 choice_show_results($choice, $course, $cm, $users, $format); //show table with students responses.
208 //now give links for downloading spreadsheets.
209 if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
210 echo "<br />\n";
211 echo "<table class=\"downloadreport\"><tr>\n";
212 echo "<td>";
213 $options = array();
214 $options["id"] = "$cm->id";
215 $options["download"] = "ods";
216 print_single_button("report.php", $options, get_string("downloadods"));
217 echo "</td><td>";
218 $options["download"] = "xls";
219 print_single_button("report.php", $options, get_string("downloadexcel"));
220 echo "</td><td>";
221 $options["download"] = "txt";
222 print_single_button("report.php", $options, get_string("downloadtext"));
224 echo "</td></tr></table>";
226 print_footer($course);