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