2009-09-24 06:11:39 +00:00
|
|
|
<?php
|
2011-10-28 00:30:42 +02:00
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
2009-09-24 06:11:39 +00:00
|
|
|
|
2008-04-23 09:33:54 +00:00
|
|
|
/**
|
2011-10-28 00:30:42 +02:00
|
|
|
* prints an analysed excel-spreadsheet of the feedback
|
|
|
|
*
|
|
|
|
* @author Andreas Grabs
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
* @package feedback
|
|
|
|
*/
|
2008-04-23 09:33:54 +00:00
|
|
|
|
2009-09-24 06:11:39 +00:00
|
|
|
require_once("../../config.php");
|
|
|
|
require_once("lib.php");
|
2010-02-28 22:19:30 +00:00
|
|
|
require_once("$CFG->libdir/excellib.class.php");
|
|
|
|
|
|
|
|
feedback_load_feedback_items();
|
2008-04-23 09:33:54 +00:00
|
|
|
|
2009-09-24 06:11:39 +00:00
|
|
|
$id = required_param('id', PARAM_INT); //the POST dominated the GET
|
2009-11-01 14:55:15 +00:00
|
|
|
$coursefilter = optional_param('coursefilter', '0', PARAM_INT);
|
2008-04-23 09:33:54 +00:00
|
|
|
|
2010-01-16 15:39:56 +00:00
|
|
|
$url = new moodle_url('/mod/feedback/analysis_to_excel.php', array('id'=>$id));
|
2009-09-24 06:11:39 +00:00
|
|
|
if ($coursefilter !== '0') {
|
|
|
|
$url->param('coursefilter', $coursefilter);
|
|
|
|
}
|
|
|
|
$PAGE->set_url($url);
|
2008-04-23 09:33:54 +00:00
|
|
|
|
2009-09-24 06:11:39 +00:00
|
|
|
$formdata = data_submitted();
|
2008-04-23 09:33:54 +00:00
|
|
|
|
2010-04-02 21:26:17 +00:00
|
|
|
if (! $cm = get_coursemodule_from_id('feedback', $id)) {
|
|
|
|
print_error('invalidcoursemodule');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
|
|
|
|
print_error('coursemisconf');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
|
|
|
|
print_error('invalidcoursemodule');
|
2009-09-24 06:11:39 +00:00
|
|
|
}
|
2010-04-02 11:34:28 +00:00
|
|
|
|
|
|
|
if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
|
|
|
|
print_error('badcontext');
|
|
|
|
}
|
2009-09-24 06:11:39 +00:00
|
|
|
|
|
|
|
require_login($course->id, true, $cm);
|
|
|
|
|
2010-04-02 20:38:17 +00:00
|
|
|
require_capability('mod/feedback:viewreports', $context);
|
2009-09-24 06:11:39 +00:00
|
|
|
|
|
|
|
//buffering any output
|
|
|
|
//this prevents some output before the excel-header will be send
|
|
|
|
ob_start();
|
2010-09-21 08:37:36 +00:00
|
|
|
$fstring = new stdClass();
|
2009-09-24 06:11:39 +00:00
|
|
|
$fstring->bold = get_string('bold', 'feedback');
|
|
|
|
$fstring->page = get_string('page', 'feedback');
|
|
|
|
$fstring->of = get_string('of', 'feedback');
|
|
|
|
$fstring->modulenameplural = get_string('modulenameplural', 'feedback');
|
|
|
|
$fstring->questions = get_string('questions', 'feedback');
|
|
|
|
$fstring->itemlabel = get_string('item_label', 'feedback');
|
|
|
|
$fstring->question = get_string('question', 'feedback');
|
|
|
|
$fstring->responses = get_string('responses', 'feedback');
|
|
|
|
$fstring->idnumber = get_string('idnumber');
|
|
|
|
$fstring->username = get_string('username');
|
MDL-13481 Strings fullname and shortname are now deprecated
This is a final cleanup commit of fullname and shortname issue. All
places where these strings were detected yet have been replaced with
proper fullnamecourse or fullnameuser or some other context specific
string.
AMOS BEGIN
CPY [fullname,core],[outcomefullname,core_grades]
CPY [shortname,core],[outcomeshortname,core_grades]
CPY [name,core],[rolefullname,core_role]
CPY [shortname,core],[roleshortname,core_role]
AMOS END
2011-02-22 10:12:10 +01:00
|
|
|
$fstring->fullname = get_string('fullnameuser');
|
2009-09-24 06:11:39 +00:00
|
|
|
$fstring->courseid = get_string('courseid', 'feedback');
|
|
|
|
$fstring->course = get_string('course');
|
2011-10-28 00:30:42 +02:00
|
|
|
$fstring->anonymous_user = get_string('anonymous_user', 'feedback');
|
2009-09-24 06:11:39 +00:00
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
//get the questions (item-names)
|
2011-10-28 00:30:42 +02:00
|
|
|
$params = array('feedback' => $feedback->id, 'hasvalue' => 1);
|
|
|
|
if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
|
|
|
|
print_error('no_items_available_yet',
|
|
|
|
'feedback',
|
|
|
|
$CFG->wwwroot.'/mod/feedback/view.php?id='.$id);
|
2008-04-23 09:33:54 +00:00
|
|
|
exit;
|
2009-09-24 06:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$filename = "feedback.xls";
|
|
|
|
|
|
|
|
$mygroupid = groups_get_activity_group($cm);
|
|
|
|
|
|
|
|
// Creating a workbook
|
2010-02-28 22:19:30 +00:00
|
|
|
$workbook = new MoodleExcelWorkbook('-');
|
2009-09-24 06:11:39 +00:00
|
|
|
$workbook->send($filename);
|
2010-02-28 22:19:30 +00:00
|
|
|
|
|
|
|
//creating the needed formats
|
2011-10-28 00:30:42 +02:00
|
|
|
$xls_formats = new stdClass();
|
|
|
|
$xls_formats->head1 = $workbook->add_format(array(
|
2010-02-28 22:19:30 +00:00
|
|
|
'bold'=>1,
|
|
|
|
'size'=>12));
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$xls_formats->head2 = $workbook->add_format(array(
|
2010-02-28 22:19:30 +00:00
|
|
|
'align'=>'left',
|
|
|
|
'bold'=>1,
|
|
|
|
'bottum'=>2));
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$xls_formats->default = $workbook->add_format(array(
|
2010-02-28 22:19:30 +00:00
|
|
|
'align'=>'left',
|
|
|
|
'v_align'=>'top'));
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$xls_formats->value_bold = $workbook->add_format(array(
|
2010-02-28 22:19:30 +00:00
|
|
|
'align'=>'left',
|
|
|
|
'bold'=>1,
|
|
|
|
'v_align'=>'top'));
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$xls_formats->procent = $workbook->add_format(array(
|
2010-02-28 22:19:30 +00:00
|
|
|
'align'=>'left',
|
|
|
|
'bold'=>1,
|
|
|
|
'v_align'=>'top',
|
|
|
|
'num_format'=>'#,##0.00%'));
|
|
|
|
|
2009-09-24 06:11:39 +00:00
|
|
|
// Creating the worksheets
|
|
|
|
$sheetname = clean_param($feedback->name, PARAM_ALPHANUM);
|
|
|
|
error_reporting(0);
|
2010-02-28 22:19:30 +00:00
|
|
|
$worksheet1 =& $workbook->add_worksheet(substr($sheetname, 0, 31));
|
|
|
|
$worksheet2 =& $workbook->add_worksheet('detailed');
|
2009-09-24 06:11:39 +00:00
|
|
|
error_reporting($CFG->debug);
|
2010-02-28 22:19:30 +00:00
|
|
|
$worksheet1->hide_gridlines();
|
|
|
|
$worksheet1->set_column(0, 0, 10);
|
|
|
|
$worksheet1->set_column(1, 1, 30);
|
|
|
|
$worksheet1->set_column(2, 20, 15);
|
2009-09-24 06:11:39 +00:00
|
|
|
|
|
|
|
//writing the table header
|
2011-10-28 00:30:42 +02:00
|
|
|
$row_offset1 = 0;
|
|
|
|
$worksheet1->write_string($row_offset1, 0, userdate(time()), $xls_formats->head1);
|
2009-09-24 06:11:39 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
//print the analysed sheet
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
//get the completeds
|
|
|
|
$completedscount = feedback_get_completeds_group_count($feedback, $mygroupid, $coursefilter);
|
2011-10-28 00:30:42 +02:00
|
|
|
if ($completedscount > 0) {
|
2009-09-24 06:11:39 +00:00
|
|
|
//write the count of completeds
|
2011-10-28 00:30:42 +02:00
|
|
|
$row_offset1++;
|
|
|
|
$worksheet1->write_string($row_offset1,
|
|
|
|
0,
|
|
|
|
$fstring->modulenameplural.': '.strval($completedscount),
|
|
|
|
$xls_formats->head1);
|
2009-09-24 06:11:39 +00:00
|
|
|
}
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
if (is_array($items)) {
|
|
|
|
$row_offset1++;
|
|
|
|
$worksheet1->write_string($row_offset1,
|
|
|
|
0,
|
|
|
|
$fstring->questions.': '. strval(count($items)),
|
|
|
|
$xls_formats->head1);
|
2009-09-24 06:11:39 +00:00
|
|
|
}
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$row_offset1 += 2;
|
|
|
|
$worksheet1->write_string($row_offset1, 0, $fstring->itemlabel, $xls_formats->head1);
|
|
|
|
$worksheet1->write_string($row_offset1, 1, $fstring->question, $xls_formats->head1);
|
|
|
|
$worksheet1->write_string($row_offset1, 2, $fstring->responses, $xls_formats->head1);
|
|
|
|
$row_offset1++;
|
2009-09-24 06:11:39 +00:00
|
|
|
|
|
|
|
if (empty($items)) {
|
|
|
|
$items=array();
|
|
|
|
}
|
2011-10-28 00:30:42 +02:00
|
|
|
foreach ($items as $item) {
|
2009-09-24 06:11:39 +00:00
|
|
|
//get the class of item-typ
|
2010-06-04 20:21:08 +00:00
|
|
|
$itemobj = feedback_get_item_class($item->typ);
|
2011-10-28 00:30:42 +02:00
|
|
|
$row_offset1 = $itemobj->excelprint_item($worksheet1,
|
|
|
|
$row_offset1,
|
|
|
|
$xls_formats,
|
|
|
|
$item,
|
|
|
|
$mygroupid,
|
|
|
|
$coursefilter);
|
2009-09-24 06:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
//print the detailed sheet
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
//get the completeds
|
|
|
|
|
|
|
|
$completeds = feedback_get_completeds_group($feedback, $mygroupid, $coursefilter);
|
|
|
|
//important: for each completed you have to print each item, even if it is not filled out!!!
|
|
|
|
//therefor for each completed we have to iterate over all items of the feedback
|
|
|
|
//this is done by feedback_excelprint_detailed_items
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$row_offset2 = 0;
|
2009-09-24 06:11:39 +00:00
|
|
|
//first we print the table-header
|
2011-10-28 00:30:42 +02:00
|
|
|
$row_offset2 = feedback_excelprint_detailed_head($worksheet2, $xls_formats, $items, $row_offset2);
|
2009-09-24 06:11:39 +00:00
|
|
|
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
if (is_array($completeds)) {
|
|
|
|
foreach ($completeds as $completed) {
|
|
|
|
$row_offset2 = feedback_excelprint_detailed_items($worksheet2,
|
|
|
|
$xls_formats,
|
|
|
|
$completed,
|
|
|
|
$items,
|
|
|
|
$row_offset2);
|
2009-09-24 06:11:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$workbook->close();
|
|
|
|
exit;
|
2009-11-01 14:55:15 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2008-04-23 09:33:54 +00:00
|
|
|
//functions
|
2009-11-01 14:55:15 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2008-04-23 09:33:54 +00:00
|
|
|
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
function feedback_excelprint_detailed_head(&$worksheet, $xls_formats, $items, $row_offset) {
|
2009-09-24 06:11:39 +00:00
|
|
|
global $fstring, $feedback;
|
2008-04-23 09:33:54 +00:00
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
if (!$items) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$col_offset = 0;
|
2008-04-23 09:33:54 +00:00
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$worksheet->write_string($row_offset + 1, $col_offset, $fstring->idnumber, $xls_formats->head2);
|
|
|
|
$col_offset++;
|
2008-04-23 09:33:54 +00:00
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$worksheet->write_string($row_offset + 1, $col_offset, $fstring->username, $xls_formats->head2);
|
|
|
|
$col_offset++;
|
2009-09-24 06:11:39 +00:00
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$worksheet->write_string($row_offset + 1, $col_offset, $fstring->fullname, $xls_formats->head2);
|
|
|
|
$col_offset++;
|
2009-09-24 06:11:39 +00:00
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
foreach ($items as $item) {
|
|
|
|
$worksheet->write_string($row_offset, $col_offset, $item->name, $xls_formats->head2);
|
|
|
|
$worksheet->write_string($row_offset + 1, $col_offset, $item->label, $xls_formats->head2);
|
|
|
|
$col_offset++;
|
2008-04-23 09:33:54 +00:00
|
|
|
}
|
2009-09-24 06:11:39 +00:00
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$worksheet->write_string($row_offset + 1, $col_offset, $fstring->courseid, $xls_formats->head2);
|
|
|
|
$col_offset++;
|
2009-09-24 06:11:39 +00:00
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$worksheet->write_string($row_offset + 1, $col_offset, $fstring->course, $xls_formats->head2);
|
|
|
|
$col_offset++;
|
2009-09-24 06:11:39 +00:00
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
return $row_offset + 2;
|
2009-09-24 06:11:39 +00:00
|
|
|
}
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
function feedback_excelprint_detailed_items(&$worksheet, $xls_formats,
|
|
|
|
$completed, $items, $row_offset) {
|
2009-09-24 06:11:39 +00:00
|
|
|
global $DB, $fstring;
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
if (!$items) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$col_offset = 0;
|
2009-09-24 06:11:39 +00:00
|
|
|
$courseid = 0;
|
|
|
|
|
|
|
|
$feedback = $DB->get_record('feedback', array('id'=>$completed->feedback));
|
|
|
|
//get the username
|
|
|
|
//anonymous users are separated automatically because the userid in the completed is "0"
|
2011-10-28 00:30:42 +02:00
|
|
|
if ($user = $DB->get_record('user', array('id'=>$completed->userid))) {
|
2009-09-24 06:11:39 +00:00
|
|
|
if ($completed->anonymous_response == FEEDBACK_ANONYMOUS_NO) {
|
2011-10-28 00:30:42 +02:00
|
|
|
$worksheet->write_string($row_offset, $col_offset, $user->idnumber, $xls_formats->head2);
|
|
|
|
$col_offset++;
|
2009-09-24 06:11:39 +00:00
|
|
|
$userfullname = fullname($user);
|
2011-10-28 00:30:42 +02:00
|
|
|
$worksheet->write_string($row_offset, $col_offset, $user->username, $xls_formats->head2);
|
|
|
|
$col_offset++;
|
2009-09-24 06:11:39 +00:00
|
|
|
} else {
|
2008-04-23 09:33:54 +00:00
|
|
|
$userfullname = $fstring->anonymous_user;
|
2011-10-28 00:30:42 +02:00
|
|
|
$worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
|
|
|
|
$col_offset++;
|
|
|
|
$worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
|
|
|
|
$col_offset++;
|
2008-04-23 09:33:54 +00:00
|
|
|
}
|
2011-10-28 00:30:42 +02:00
|
|
|
} else {
|
2009-09-24 06:11:39 +00:00
|
|
|
$userfullname = $fstring->anonymous_user;
|
2011-10-28 00:30:42 +02:00
|
|
|
$worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
|
|
|
|
$col_offset++;
|
|
|
|
$worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
|
|
|
|
$col_offset++;
|
2009-09-24 06:11:39 +00:00
|
|
|
}
|
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$worksheet->write_string($row_offset, $col_offset, $userfullname, $xls_formats->head2);
|
2009-09-24 06:11:39 +00:00
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
$col_offset++;
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$params = array('item' => $item->id, 'completed' => $completed->id);
|
|
|
|
$value = $DB->get_record('feedback_value', $params);
|
2009-09-24 06:11:39 +00:00
|
|
|
|
2010-06-04 20:21:08 +00:00
|
|
|
$itemobj = feedback_get_item_class($item->typ);
|
2009-09-24 06:11:39 +00:00
|
|
|
$printval = $itemobj->get_printval($item, $value);
|
2010-08-04 13:53:07 +00:00
|
|
|
$printval = trim($printval);
|
2009-09-24 06:11:39 +00:00
|
|
|
|
2011-10-28 00:30:42 +02:00
|
|
|
if (is_numeric($printval)) {
|
|
|
|
$worksheet->write_number($row_offset, $col_offset, $printval, $xls_formats->default);
|
|
|
|
} else if ($printval != '') {
|
|
|
|
$worksheet->write_string($row_offset, $col_offset, $printval, $xls_formats->default);
|
2008-04-23 09:33:54 +00:00
|
|
|
}
|
2009-09-24 06:11:39 +00:00
|
|
|
$printval = '';
|
2011-10-28 00:30:42 +02:00
|
|
|
$col_offset++;
|
2009-09-24 06:11:39 +00:00
|
|
|
$courseid = isset($value->course_id) ? $value->course_id : 0;
|
2011-10-28 00:30:42 +02:00
|
|
|
if ($courseid == 0) {
|
|
|
|
$courseid = $feedback->course;
|
|
|
|
}
|
2009-09-24 06:11:39 +00:00
|
|
|
}
|
2011-10-28 00:30:42 +02:00
|
|
|
$worksheet->write_number($row_offset, $col_offset, $courseid, $xls_formats->default);
|
|
|
|
$col_offset++;
|
2011-09-07 11:46:28 +12:00
|
|
|
if (isset($courseid) AND $course = $DB->get_record('course', array('id' => $courseid))) {
|
2011-10-28 00:30:42 +02:00
|
|
|
$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
|
|
|
|
$shortname = format_string($course->shortname, true, array('context' => $coursecontext));
|
|
|
|
$worksheet->write_string($row_offset, $col_offset, $shortname, $xls_formats->default);
|
2008-04-23 09:33:54 +00:00
|
|
|
}
|
2011-10-28 00:30:42 +02:00
|
|
|
return $row_offset + 1;
|
2009-09-24 06:11:39 +00:00
|
|
|
}
|