Merge branch 'MDL-35644-MOODLE_22_STABLE' of git://github.com/mouneyrac/moodle into...
[moodle.git] / mod / feedback / analysis_to_excel.php
blob089bc9d601f656c3e178ffb0b61bdd584b1bbb8f
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * prints an analysed excel-spreadsheet of the feedback
20 * @author Andreas Grabs
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package feedback
25 require_once("../../config.php");
26 require_once("lib.php");
27 require_once("$CFG->libdir/excellib.class.php");
29 feedback_load_feedback_items();
31 $id = required_param('id', PARAM_INT); //the POST dominated the GET
32 $coursefilter = optional_param('coursefilter', '0', PARAM_INT);
34 $url = new moodle_url('/mod/feedback/analysis_to_excel.php', array('id'=>$id));
35 if ($coursefilter !== '0') {
36 $url->param('coursefilter', $coursefilter);
38 $PAGE->set_url($url);
40 $formdata = data_submitted();
42 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
43 print_error('invalidcoursemodule');
46 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
47 print_error('coursemisconf');
50 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
51 print_error('invalidcoursemodule');
54 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
55 print_error('badcontext');
58 require_login($course->id, true, $cm);
60 require_capability('mod/feedback:viewreports', $context);
62 //buffering any output
63 //this prevents some output before the excel-header will be send
64 ob_start();
65 $fstring = new stdClass();
66 $fstring->bold = get_string('bold', 'feedback');
67 $fstring->page = get_string('page', 'feedback');
68 $fstring->of = get_string('of', 'feedback');
69 $fstring->modulenameplural = get_string('modulenameplural', 'feedback');
70 $fstring->questions = get_string('questions', 'feedback');
71 $fstring->itemlabel = get_string('item_label', 'feedback');
72 $fstring->question = get_string('question', 'feedback');
73 $fstring->responses = get_string('responses', 'feedback');
74 $fstring->idnumber = get_string('idnumber');
75 $fstring->username = get_string('username');
76 $fstring->fullname = get_string('fullnameuser');
77 $fstring->courseid = get_string('courseid', 'feedback');
78 $fstring->course = get_string('course');
79 $fstring->anonymous_user = get_string('anonymous_user', 'feedback');
80 ob_end_clean();
82 //get the questions (item-names)
83 $params = array('feedback' => $feedback->id, 'hasvalue' => 1);
84 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
85 print_error('no_items_available_yet',
86 'feedback',
87 $CFG->wwwroot.'/mod/feedback/view.php?id='.$id);
88 exit;
91 $filename = "feedback.xls";
93 $mygroupid = groups_get_activity_group($cm);
95 // Creating a workbook
96 $workbook = new MoodleExcelWorkbook('-');
97 $workbook->send($filename);
99 //creating the needed formats
100 $xls_formats = new stdClass();
101 $xls_formats->head1 = $workbook->add_format(array(
102 'bold'=>1,
103 'size'=>12));
105 $xls_formats->head2 = $workbook->add_format(array(
106 'align'=>'left',
107 'bold'=>1,
108 'bottum'=>2));
110 $xls_formats->default = $workbook->add_format(array(
111 'align'=>'left',
112 'v_align'=>'top'));
114 $xls_formats->value_bold = $workbook->add_format(array(
115 'align'=>'left',
116 'bold'=>1,
117 'v_align'=>'top'));
119 $xls_formats->procent = $workbook->add_format(array(
120 'align'=>'left',
121 'bold'=>1,
122 'v_align'=>'top',
123 'num_format'=>'#,##0.00%'));
125 // Creating the worksheets
126 $sheetname = clean_param($feedback->name, PARAM_ALPHANUM);
127 error_reporting(0);
128 $worksheet1 =& $workbook->add_worksheet(substr($sheetname, 0, 31));
129 $worksheet2 =& $workbook->add_worksheet('detailed');
130 error_reporting($CFG->debug);
131 $worksheet1->hide_gridlines();
132 $worksheet1->set_column(0, 0, 10);
133 $worksheet1->set_column(1, 1, 30);
134 $worksheet1->set_column(2, 20, 15);
136 //writing the table header
137 $row_offset1 = 0;
138 $worksheet1->write_string($row_offset1, 0, userdate(time()), $xls_formats->head1);
140 ////////////////////////////////////////////////////////////////////////
141 //print the analysed sheet
142 ////////////////////////////////////////////////////////////////////////
143 //get the completeds
144 $completedscount = feedback_get_completeds_group_count($feedback, $mygroupid, $coursefilter);
145 if ($completedscount > 0) {
146 //write the count of completeds
147 $row_offset1++;
148 $worksheet1->write_string($row_offset1,
150 $fstring->modulenameplural.': '.strval($completedscount),
151 $xls_formats->head1);
154 if (is_array($items)) {
155 $row_offset1++;
156 $worksheet1->write_string($row_offset1,
158 $fstring->questions.': '. strval(count($items)),
159 $xls_formats->head1);
162 $row_offset1 += 2;
163 $worksheet1->write_string($row_offset1, 0, $fstring->itemlabel, $xls_formats->head1);
164 $worksheet1->write_string($row_offset1, 1, $fstring->question, $xls_formats->head1);
165 $worksheet1->write_string($row_offset1, 2, $fstring->responses, $xls_formats->head1);
166 $row_offset1++;
168 if (empty($items)) {
169 $items=array();
171 foreach ($items as $item) {
172 //get the class of item-typ
173 $itemobj = feedback_get_item_class($item->typ);
174 $row_offset1 = $itemobj->excelprint_item($worksheet1,
175 $row_offset1,
176 $xls_formats,
177 $item,
178 $mygroupid,
179 $coursefilter);
182 ////////////////////////////////////////////////////////////////////////
183 //print the detailed sheet
184 ////////////////////////////////////////////////////////////////////////
185 //get the completeds
187 $completeds = feedback_get_completeds_group($feedback, $mygroupid, $coursefilter);
188 //important: for each completed you have to print each item, even if it is not filled out!!!
189 //therefor for each completed we have to iterate over all items of the feedback
190 //this is done by feedback_excelprint_detailed_items
192 $row_offset2 = 0;
193 //first we print the table-header
194 $row_offset2 = feedback_excelprint_detailed_head($worksheet2, $xls_formats, $items, $row_offset2);
197 if (is_array($completeds)) {
198 foreach ($completeds as $completed) {
199 $row_offset2 = feedback_excelprint_detailed_items($worksheet2,
200 $xls_formats,
201 $completed,
202 $items,
203 $row_offset2);
208 $workbook->close();
209 exit;
210 ////////////////////////////////////////////////////////////////////////////////
211 ////////////////////////////////////////////////////////////////////////////////
212 //functions
213 ////////////////////////////////////////////////////////////////////////////////
216 function feedback_excelprint_detailed_head(&$worksheet, $xls_formats, $items, $row_offset) {
217 global $fstring, $feedback;
219 if (!$items) {
220 return;
222 $col_offset = 0;
224 $worksheet->write_string($row_offset + 1, $col_offset, $fstring->idnumber, $xls_formats->head2);
225 $col_offset++;
227 $worksheet->write_string($row_offset + 1, $col_offset, $fstring->username, $xls_formats->head2);
228 $col_offset++;
230 $worksheet->write_string($row_offset + 1, $col_offset, $fstring->fullname, $xls_formats->head2);
231 $col_offset++;
233 foreach ($items as $item) {
234 $worksheet->write_string($row_offset, $col_offset, $item->name, $xls_formats->head2);
235 $worksheet->write_string($row_offset + 1, $col_offset, $item->label, $xls_formats->head2);
236 $col_offset++;
239 $worksheet->write_string($row_offset + 1, $col_offset, $fstring->courseid, $xls_formats->head2);
240 $col_offset++;
242 $worksheet->write_string($row_offset + 1, $col_offset, $fstring->course, $xls_formats->head2);
243 $col_offset++;
245 return $row_offset + 2;
248 function feedback_excelprint_detailed_items(&$worksheet, $xls_formats,
249 $completed, $items, $row_offset) {
250 global $DB, $fstring;
252 if (!$items) {
253 return;
255 $col_offset = 0;
256 $courseid = 0;
258 $feedback = $DB->get_record('feedback', array('id'=>$completed->feedback));
259 //get the username
260 //anonymous users are separated automatically because the userid in the completed is "0"
261 if ($user = $DB->get_record('user', array('id'=>$completed->userid))) {
262 if ($completed->anonymous_response == FEEDBACK_ANONYMOUS_NO) {
263 $worksheet->write_string($row_offset, $col_offset, $user->idnumber, $xls_formats->head2);
264 $col_offset++;
265 $userfullname = fullname($user);
266 $worksheet->write_string($row_offset, $col_offset, $user->username, $xls_formats->head2);
267 $col_offset++;
268 } else {
269 $userfullname = $fstring->anonymous_user;
270 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
271 $col_offset++;
272 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
273 $col_offset++;
275 } else {
276 $userfullname = $fstring->anonymous_user;
277 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
278 $col_offset++;
279 $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
280 $col_offset++;
283 $worksheet->write_string($row_offset, $col_offset, $userfullname, $xls_formats->head2);
285 $col_offset++;
286 foreach ($items as $item) {
287 $params = array('item' => $item->id, 'completed' => $completed->id);
288 $value = $DB->get_record('feedback_value', $params);
290 $itemobj = feedback_get_item_class($item->typ);
291 $printval = $itemobj->get_printval($item, $value);
292 $printval = trim($printval);
294 if (is_numeric($printval)) {
295 $worksheet->write_number($row_offset, $col_offset, $printval, $xls_formats->default);
296 } else if ($printval != '') {
297 $worksheet->write_string($row_offset, $col_offset, $printval, $xls_formats->default);
299 $printval = '';
300 $col_offset++;
301 $courseid = isset($value->course_id) ? $value->course_id : 0;
302 if ($courseid == 0) {
303 $courseid = $feedback->course;
306 $worksheet->write_number($row_offset, $col_offset, $courseid, $xls_formats->default);
307 $col_offset++;
308 if (isset($courseid) AND $course = $DB->get_record('course', array('id' => $courseid))) {
309 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
310 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
311 $worksheet->write_string($row_offset, $col_offset, $shortname, $xls_formats->default);
313 return $row_offset + 1;