Automatic installer.php lang files by installer_builder (20060808)
[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
f9903ed0 11 if (! $cm = get_record("course_modules", "id", $id)) {
12 error("Course Module ID was incorrect");
13 }
14
15 if (! $course = get_record("course", "id", $cm->course)) {
16 error("Course module is misconfigured");
17 }
18
ec81373f 19 require_login($course->id, false, $cm);
f9903ed0 20
21 if (!isteacher($course->id)) {
22 error("Only teachers can look at this page");
23 }
24
cd3fccff 25 if (!$choice = choice_get_choice($cm->instance)) {
f9903ed0 26 error("Course module is incorrect");
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
35 if ($action == 'delete') { //some responses need to be deleted
36 $attemptids = isset($_POST['attemptid']) ? $_POST['attemptid'] : array(); //get array of repsonses to delete.
90ca2ce8 37 choice_delete_responses($attemptids); //delete responses.
38 redirect("report.php?id=$cm->id");
348630d8 39 }
40
d921818d 41 if ($download <> "xls" and $download <> "txt" ) {
42 print_header_simple(format_string($choice->name).": $strresponses", "",
43 "<a href=\"index.php?id=$course->id\">$strchoices</a> ->
44 <a href=\"view.php?id=$cm->id\">".format_string($choice->name,true)."</a> -> $strresponses", "", '', true,
45 update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
b6ee2d82 46 }
f9903ed0 47
348630d8 48 $users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber') + get_admins();
49
50
b6ee2d82 51 if (!$users) {
39ee02a3 52 print_heading(get_string("nousersyet"));
f9903ed0 53 }
54
bfe97ca6 55 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
6fd87e3b 56 foreach ($allresponses as $aa) {
ebc3bd2b 57 $answers[$aa->userid] = $aa;
f9903ed0 58 }
f9903ed0 59 } else {
60 $answers = array () ;
61 }
f9903ed0 62
63 $timenow = time();
64
bfe97ca6 65 foreach ($choice->option as $optionid => $text) {
66 $useranswer[$optionid] = array();
66062dd3 67 }
da664183 68 foreach ($users as $user) {
bfe97ca6 69 if (!empty($user->id) and !empty($answers[$user->id])) {
dcde9f02 70 $answer = $answers[$user->id];
bfe97ca6 71 $useranswer[(int)$answer->optionid][] = $user;
dcde9f02 72 } else {
bfe97ca6 73 $useranswer[0][] = $user;
dcde9f02 74 }
da664183 75 }
bfe97ca6 76 foreach ($choice->option as $optionid => $text) {
77 if (!$choice->option[$optionid]) {
78 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
cd3fccff 79 }
80 }
da664183 81 ksort($useranswer);
64739659 82
83 //print spreadsheet if one is asked for:
84 if ($download == "xls") {
b0898566 85 require_once("$CFG->libdir/excellib.class.php");
64739659 86
31344133 87 /// Calculate file name
b0898566 88 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
89 /// Creating a workbook
90 $workbook = new MoodleExcelWorkbook("-");
91 /// Send HTTP headers
92 $workbook->send($filename);
93 /// Creating the first worksheet
31344133 94 $myxls =& $workbook->add_worksheet($strresponses);
64739659 95
b0898566 96 /// Print names of all the fields
64739659 97 $myxls->write_string(0,0,get_string("lastname"));
98 $myxls->write_string(0,1,get_string("firstname"));
99 $myxls->write_string(0,2,get_string("idnumber"));
348630d8 100 $myxls->write_string(0,3,get_string("group"));
101 $myxls->write_string(0,4,get_string("choice","choice"));
102
64739659 103
64739659 104 /// generate the data for the body of the spreadsheet
b0898566 105 $i=0;
106 $row=1;
107 if ($users) {
108 foreach ($users as $user) {
109 if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && isadmin($user->id)) &&
110 (!($answers[$user->id]->optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) &&
111 !($choice->showunanswered==0 && $answers[$user->id]->optionid==0) ) { //make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
112
113 $myxls->write_string($row,0,$user->lastname);
114 $myxls->write_string($row,1,$user->firstname);
115 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
116 $myxls->write_string($row,2,$studentid);
348630d8 117 $ug2 = '';
f262874b 118 if ($usergrps = user_group($course->id, $user->id)) {
119 foreach ($usergrps as $ug) {
acaf7b86 120 $ug2 = $ug2. $ug->name;
121 }
122 }
348630d8 123 $myxls->write_string($row,3,$ug2);
124
b0898566 125 $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
126 if (isset($useroption)) {
348630d8 127 $myxls->write_string($row,4,format_string($useroption,true));
b0898566 128 }
129 $row++;
130 }
131 $pos=4;
132 }
133 }
134
135 /// Close the workbook
136 $workbook->close();
137
138 exit;
64739659 139 }
140 // print text file
141 if ($download == "txt") {
09972dd3 142 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
31344133 143
144 header("Content-Type: application/download\n");
145 header("Content-Disposition: attachment; filename=\"$filename\"");
146 header("Expires: 0");
147 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
148 header("Pragma: public");
149
64739659 150 /// Print names of all the fields
151
152 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
348630d8 153 echo get_string("group"). "\t";
64739659 154 echo get_string("choice","choice"). "\n";
155
156 /// generate the data for the body of the spreadsheet
157 $i=0;
158 $row=1;
159 if ($users) foreach ($users as $user) {
acaf7b86 160 if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && isadmin($user->id)) &&
ae2f3b4c 161 (!($answers[$user->id]->optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) &&
162 !($choice->showunanswered==0 && $answers[$user->id]->optionid==0) ) { //make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
163
64739659 164 echo $user->lastname;
165 echo "\t".$user->firstname;
acaf7b86 166 $studentid = " ";
167 if (!empty($user->idnumber)) {
168 $studentid = $user->idnumber;
169 }
64739659 170 echo "\t". $studentid."\t";
348630d8 171 $ug2 = '';
f262874b 172 if ($usergrps = user_group($course->id, $user->id)) {
173 foreach ($usergrps as $ug) {
acaf7b86 174 $ug2 = $ug2. $ug->name;
175 }
348630d8 176 }
177 echo $ug2. "\t";
09972dd3 178 echo format_string(choice_get_option_text($choice, $answers[$user->id]->optionid),true). "\n";
64739659 179 }
180 $row++;
181 }
182 exit;
183}
348630d8 184 choice_show_results($choice, $course, $cm, $format); //show table with students responses.
185
186 //now give links for downloading spreadsheets.
64739659 187 echo "<br />\n";
188 echo "<table border=\"0\" align=\"center\"><tr>\n";
189 echo "<td>";
190 unset($options);
191 $options["id"] = "$cm->id";
192 $options["download"] = "xls";
193 print_single_button("report.php", $options, get_string("downloadexcel"));
194 echo "</td><td>";
195 $options["download"] = "txt";
196 print_single_button("report.php", $options, get_string("downloadtext"));
197
198 echo "</td></tr></table>";
bfe97ca6 199print_footer($course);
f9903ed0 200
ec81373f 201
f9903ed0 202?>