"MDL-14129, add new lang string"
[moodle.git] / mod / choice / report.php
CommitLineData
1c61c8d6 1<?php // $Id$
f9903ed0 2
b0e3a925 3 require_once("../../config.php");
4 require_once("lib.php");
f9903ed0 5
69e3c416 6 $id = required_param('id', PARAM_INT); //moduleid
09972dd3 7 $format = optional_param('format', CHOICE_PUBLISH_NAMES, PARAM_INT);
8 $download = optional_param('download', '', PARAM_ALPHA);
69e3c416 9 $action = optional_param('action', '', PARAM_ALPHA);
bfe97ca6 10
f9d5371b 11 if (! $cm = get_coursemodule_from_id('choice', $id)) {
5a2a5331 12 print_error("Course Module ID was incorrect");
f9903ed0 13 }
14
15 if (! $course = get_record("course", "id", $cm->course)) {
5a2a5331 16 print_error("Course module is misconfigured");
f9903ed0 17 }
18
ec81373f 19 require_login($course->id, false, $cm);
bbbf2d40 20
dabfd0ed 21 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
bbbf2d40 22
0468976c 23 require_capability('mod/choice:readresponses', $context);
bbbf2d40 24
cd3fccff 25 if (!$choice = choice_get_choice($cm->instance)) {
5a2a5331 26 print_error("Course module is incorrect");
f9903ed0 27 }
28
c4016bc1 29 $strchoice = get_string("modulename", "choice");
30 $strchoices = get_string("modulenameplural", "choice");
31 $strresponses = get_string("responses", "choice");
32
408f7c41 33 add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
348630d8 34
0468976c 35 if ($action == 'delete' && has_capability('mod/choice:deleteresponses',$context)) {
348630d8 36 $attemptids = isset($_POST['attemptid']) ? $_POST['attemptid'] : array(); //get array of repsonses to delete.
a61e4188 37 choice_delete_responses($attemptids, $choice->id); //delete responses.
38 redirect("report.php?id=$cm->id");
348630d8 39 }
40
3b27b0fe 41 if (!$download) {
a044c05d 42
a61e4188 43 $navigation = build_navigation($strresponses, $cm);
161fbddf 44 print_header_simple(format_string($choice->name).": $strresponses", "", $navigation, "", '', true,
d921818d 45 update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
3b27b0fe 46 /// Check to see if groups are being used in this choice
6d5b7706 47 $groupmode = groups_get_activity_groupmode($cm);
48 groups_get_activity_group($cm, true);
49 groups_print_activity_menu($cm, 'report.php?id='.$id);
3b27b0fe 50 } else {
6d5b7706 51 $groupmode = groups_get_activity_groupmode($cm);
f9903ed0 52 }
a61e4188 53 $users = choice_get_response_data($choice, $cm, $groupmode);
d81b7ffb 54
55 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
56 require_once("$CFG->libdir/odslib.class.php");
57
58 /// Calculate file name
59 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
60 /// Creating a workbook
61 $workbook = new MoodleODSWorkbook("-");
62 /// Send HTTP headers
63 $workbook->send($filename);
64 /// Creating the first worksheet
65 $myxls =& $workbook->add_worksheet($strresponses);
66
67 /// Print names of all the fields
68 $myxls->write_string(0,0,get_string("lastname"));
69 $myxls->write_string(0,1,get_string("firstname"));
70 $myxls->write_string(0,2,get_string("idnumber"));
71 $myxls->write_string(0,3,get_string("group"));
72 $myxls->write_string(0,4,get_string("choice","choice"));
a61e4188 73
d81b7ffb 74 /// generate the data for the body of the spreadsheet
75 $i=0;
76 $row=1;
77 if ($users) {
a61e4188 78 foreach ($users as $option => $userid) {
79 $option_text = choice_get_option_text($choice, $option);
80 foreach($userid as $user) {
81 $myxls->write_string($row,0,$user->lastname);
82 $myxls->write_string($row,1,$user->firstname);
83 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
84 $myxls->write_string($row,2,$studentid);
85 $ug2 = '';
86 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
87 foreach ($usergrps as $ug) {
88 $ug2 = $ug2. $ug->name;
d81b7ffb 89 }
d81b7ffb 90 }
a61e4188 91 $myxls->write_string($row,3,$ug2);
92
93 if (isset($option_text)) {
94 $myxls->write_string($row,4,format_string($useroption,true));
95 }
96 $row++;
d81b7ffb 97 $pos=4;
98 }
99 }
a61e4188 100 }
101 /// Close the workbook
102 $workbook->close();
d81b7ffb 103
a61e4188 104 exit;
d81b7ffb 105 }
106
64739659 107 //print spreadsheet if one is asked for:
0468976c 108 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
b0898566 109 require_once("$CFG->libdir/excellib.class.php");
64739659 110
31344133 111 /// Calculate file name
b0898566 112 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
113 /// Creating a workbook
114 $workbook = new MoodleExcelWorkbook("-");
115 /// Send HTTP headers
116 $workbook->send($filename);
117 /// Creating the first worksheet
31344133 118 $myxls =& $workbook->add_worksheet($strresponses);
64739659 119
b0898566 120 /// Print names of all the fields
64739659 121 $myxls->write_string(0,0,get_string("lastname"));
122 $myxls->write_string(0,1,get_string("firstname"));
123 $myxls->write_string(0,2,get_string("idnumber"));
348630d8 124 $myxls->write_string(0,3,get_string("group"));
125 $myxls->write_string(0,4,get_string("choice","choice"));
126
64739659 127
64739659 128 /// generate the data for the body of the spreadsheet
b0898566 129 $i=0;
130 $row=1;
131 if ($users) {
a61e4188 132 foreach ($users as $option => $userid) {
133 $option_text = choice_get_option_text($choice, $option);
134 foreach($userid as $user) {
135 $myxls->write_string($row,0,$user->lastname);
136 $myxls->write_string($row,1,$user->firstname);
137 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
138 $myxls->write_string($row,2,$studentid);
139 $ug2 = '';
140 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
141 foreach ($usergrps as $ug) {
142 $ug2 = $ug2. $ug->name;
dabfd0ed 143 }
39b3e7de 144 }
a61e4188 145 $myxls->write_string($row,3,$ug2);
146 if (isset($option_text)) {
147 $myxls->write_string($row,4,format_string($option_text,true));
148 }
149 $row++;
b0898566 150 }
b0898566 151 }
a61e4188 152 $pos=4;
153 }
154 /// Close the workbook
155 $workbook->close();
156 exit;
39b3e7de 157 }
a61e4188 158
bbbf2d40 159 // print text file
0468976c 160 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
09972dd3 161 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
0468976c 162
163 header("Content-Type: application/download\n");
164 header("Content-Disposition: attachment; filename=\"$filename\"");
165 header("Expires: 0");
166 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
167 header("Pragma: public");
168
169 /// Print names of all the fields
64739659 170
171 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
348630d8 172 echo get_string("group"). "\t";
64739659 173 echo get_string("choice","choice"). "\n";
0468976c 174
175 /// generate the data for the body of the spreadsheet
176 $i=0;
a61e4188 177 if ($users) {
178 foreach ($users as $option => $userid) {
179 $option_text = choice_get_option_text($choice, $option);
180 foreach($userid as $user) {
181 echo $user->lastname;
182 echo "\t".$user->firstname;
183 $studentid = " ";
184 if (!empty($user->idnumber)) {
185 $studentid = $user->idnumber;
186 }
187 echo "\t". $studentid."\t";
188 $ug2 = '';
189 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
190 foreach ($usergrps as $ug) {
191 $ug2 = $ug2. $ug->name;
192 }
0468976c 193 }
a61e4188 194 echo $ug2. "\t";
195 if (isset($option_text)) {
196 echo format_string($option_text,true);
197 }
198 echo "\n";
0468976c 199 }
0468976c 200 }
a61e4188 201 }
0468976c 202 exit;
203 }
a61e4188 204 choice_show_results($choice, $course, $cm, $users, $format); //show table with students responses.
0468976c 205
348630d8 206 //now give links for downloading spreadsheets.
64739659 207 echo "<br />\n";
95aa949b 208 echo "<table class=\"downloadreport\"><tr>\n";
64739659 209 echo "<td>";
d81b7ffb 210 $options = array();
64739659 211 $options["id"] = "$cm->id";
d81b7ffb 212 $options["download"] = "ods";
213 print_single_button("report.php", $options, get_string("downloadods"));
214 echo "</td><td>";
64739659 215 $options["download"] = "xls";
216 print_single_button("report.php", $options, get_string("downloadexcel"));
217 echo "</td><td>";
218 $options["download"] = "txt";
219 print_single_button("report.php", $options, get_string("downloadtext"));
220
221 echo "</td></tr></table>";
39b3e7de 222 print_footer($course);
ec81373f 223
f9903ed0 224?>