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
);
11 if (! $cm = get_coursemodule_from_id('choice', $id)) {
12 error("Course Module ID was incorrect");
15 if (! $course = get_record("course", "id", $cm->course
)) {
16 error("Course module is misconfigured");
19 require_login($course->id
, false, $cm);
21 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
23 require_capability('mod/choice:readresponses', $context);
25 if (!$choice = choice_get_choice($cm->instance
)) {
26 error("Course module is incorrect");
29 $strchoice = get_string("modulename", "choice");
30 $strchoices = get_string("modulenameplural", "choice");
31 $strresponses = get_string("responses", "choice");
33 add_to_log($course->id
, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id
);
35 if ($action == 'delete' && has_capability('mod/choice:deleteresponses',$context)) {
36 $attemptids = isset($_POST['attemptid']) ?
$_POST['attemptid'] : array(); //get array of repsonses to delete.
37 choice_delete_responses($attemptids, $choice->id
); //delete responses.
38 redirect("report.php?id=$cm->id");
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 groups_get_activity_group($cm, true);
49 groups_print_activity_menu($cm, 'report.php?id='.$id);
51 $groupmode = groups_get_activity_groupmode($cm);
53 $users = choice_get_response_data($choice, $cm, $groupmode);
55 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
56 require_once("$CFG->libdir/odslib.class.php");
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("-");
63 $workbook->send($filename);
64 /// Creating the first worksheet
65 $myxls =& $workbook->add_worksheet($strresponses);
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"));
74 /// generate the data for the body of the spreadsheet
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);
86 if ($usergrps = groups_get_all_groups($course->id
, $user->id
)) {
87 foreach ($usergrps as $ug) {
88 $ug2 = $ug2. $ug->name
;
91 $myxls->write_string($row,3,$ug2);
93 if (isset($option_text)) {
94 $myxls->write_string($row,4,format_string($useroption,true));
101 /// Close the workbook
107 //print spreadsheet if one is asked for:
108 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
109 require_once("$CFG->libdir/excellib.class.php");
111 /// Calculate file name
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
118 $myxls =& $workbook->add_worksheet($strresponses);
120 /// Print names of all the fields
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"));
124 $myxls->write_string(0,3,get_string("group"));
125 $myxls->write_string(0,4,get_string("choice","choice"));
128 /// generate the data for the body of the spreadsheet
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);
140 if ($usergrps = groups_get_all_groups($course->id
, $user->id
)) {
141 foreach ($usergrps as $ug) {
142 $ug2 = $ug2. $ug->name
;
145 $myxls->write_string($row,3,$ug2);
146 if (isset($option_text)) {
147 $myxls->write_string($row,4,format_string($option_text,true));
154 /// Close the workbook
160 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
161 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name
,true))).'.txt';
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");
169 /// Print names of all the fields
171 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
172 echo get_string("group"). "\t";
173 echo get_string("choice","choice"). "\n";
175 /// generate the data for the body of the spreadsheet
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
;
184 if (!empty($user->idnumber
)) {
185 $studentid = $user->idnumber
;
187 echo "\t". $studentid."\t";
189 if ($usergrps = groups_get_all_groups($course->id
, $user->id
)) {
190 foreach ($usergrps as $ug) {
191 $ug2 = $ug2. $ug->name
;
195 if (isset($option_text)) {
196 echo format_string($option_text,true);
204 choice_show_results($choice, $course, $cm, $users, $format); //show table with students responses.
206 //now give links for downloading spreadsheets.
207 if (has_capability('mod/choice:downloadresponses',$context)) {
209 echo "<table class=\"downloadreport\"><tr>\n";
212 $options["id"] = "$cm->id";
213 $options["download"] = "ods";
214 print_single_button("report.php", $options, get_string("downloadods"));
216 $options["download"] = "xls";
217 print_single_button("report.php", $options, get_string("downloadexcel"));
219 $options["download"] = "txt";
220 print_single_button("report.php", $options, get_string("downloadtext"));
222 echo "</td></tr></table>";
224 print_footer($course);