Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/feedback/analysis_to_excel.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00025 require_once("../../config.php");
00026 require_once("lib.php");
00027 require_once("$CFG->libdir/excellib.class.php");
00028 
00029 feedback_load_feedback_items();
00030 
00031 $id = required_param('id', PARAM_INT);  //the POST dominated the GET
00032 $coursefilter = optional_param('coursefilter', '0', PARAM_INT);
00033 
00034 $url = new moodle_url('/mod/feedback/analysis_to_excel.php', array('id'=>$id));
00035 if ($coursefilter !== '0') {
00036     $url->param('coursefilter', $coursefilter);
00037 }
00038 $PAGE->set_url($url);
00039 
00040 $formdata = data_submitted();
00041 
00042 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
00043     print_error('invalidcoursemodule');
00044 }
00045 
00046 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
00047     print_error('coursemisconf');
00048 }
00049 
00050 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
00051     print_error('invalidcoursemodule');
00052 }
00053 
00054 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
00055         print_error('badcontext');
00056 }
00057 
00058 require_login($course->id, true, $cm);
00059 
00060 require_capability('mod/feedback:viewreports', $context);
00061 
00062 //buffering any output
00063 //this prevents some output before the excel-header will be send
00064 ob_start();
00065 $fstring = new stdClass();
00066 $fstring->bold = get_string('bold', 'feedback');
00067 $fstring->page = get_string('page', 'feedback');
00068 $fstring->of = get_string('of', 'feedback');
00069 $fstring->modulenameplural = get_string('modulenameplural', 'feedback');
00070 $fstring->questions = get_string('questions', 'feedback');
00071 $fstring->itemlabel = get_string('item_label', 'feedback');
00072 $fstring->question = get_string('question', 'feedback');
00073 $fstring->responses = get_string('responses', 'feedback');
00074 $fstring->idnumber = get_string('idnumber');
00075 $fstring->username = get_string('username');
00076 $fstring->fullname = get_string('fullnameuser');
00077 $fstring->courseid = get_string('courseid', 'feedback');
00078 $fstring->course = get_string('course');
00079 $fstring->anonymous_user = get_string('anonymous_user', 'feedback');
00080 ob_end_clean();
00081 
00082 //get the questions (item-names)
00083 $params = array('feedback' => $feedback->id, 'hasvalue' => 1);
00084 if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
00085     print_error('no_items_available_yet',
00086                 'feedback',
00087                 $CFG->wwwroot.'/mod/feedback/view.php?id='.$id);
00088     exit;
00089 }
00090 
00091 $filename = "feedback.xls";
00092 
00093 $mygroupid = groups_get_activity_group($cm);
00094 
00095 // Creating a workbook
00096 $workbook = new MoodleExcelWorkbook('-');
00097 $workbook->send($filename);
00098 
00099 //creating the needed formats
00100 $xls_formats = new stdClass();
00101 $xls_formats->head1 = $workbook->add_format(array(
00102                         'bold'=>1,
00103                         'size'=>12));
00104 
00105 $xls_formats->head2 = $workbook->add_format(array(
00106                         'align'=>'left',
00107                         'bold'=>1,
00108                         'bottum'=>2));
00109 
00110 $xls_formats->default = $workbook->add_format(array(
00111                         'align'=>'left',
00112                         'v_align'=>'top'));
00113 
00114 $xls_formats->value_bold = $workbook->add_format(array(
00115                         'align'=>'left',
00116                         'bold'=>1,
00117                         'v_align'=>'top'));
00118 
00119 $xls_formats->procent = $workbook->add_format(array(
00120                         'align'=>'left',
00121                         'bold'=>1,
00122                         'v_align'=>'top',
00123                         'num_format'=>'#,##0.00%'));
00124 
00125 // Creating the worksheets
00126 $sheetname = clean_param($feedback->name, PARAM_ALPHANUM);
00127 error_reporting(0);
00128 $worksheet1 =& $workbook->add_worksheet(substr($sheetname, 0, 31));
00129 $worksheet2 =& $workbook->add_worksheet('detailed');
00130 error_reporting($CFG->debug);
00131 $worksheet1->hide_gridlines();
00132 $worksheet1->set_column(0, 0, 10);
00133 $worksheet1->set_column(1, 1, 30);
00134 $worksheet1->set_column(2, 20, 15);
00135 
00136 //writing the table header
00137 $row_offset1 = 0;
00138 $worksheet1->write_string($row_offset1, 0, userdate(time()), $xls_formats->head1);
00139 
00141 //print the analysed sheet
00143 //get the completeds
00144 $completedscount = feedback_get_completeds_group_count($feedback, $mygroupid, $coursefilter);
00145 if ($completedscount > 0) {
00146     //write the count of completeds
00147     $row_offset1++;
00148     $worksheet1->write_string($row_offset1,
00149                               0,
00150                               $fstring->modulenameplural.': '.strval($completedscount),
00151                               $xls_formats->head1);
00152 }
00153 
00154 if (is_array($items)) {
00155     $row_offset1++;
00156     $worksheet1->write_string($row_offset1,
00157                               0,
00158                               $fstring->questions.': '. strval(count($items)),
00159                               $xls_formats->head1);
00160 }
00161 
00162 $row_offset1 += 2;
00163 $worksheet1->write_string($row_offset1, 0, $fstring->itemlabel, $xls_formats->head1);
00164 $worksheet1->write_string($row_offset1, 1, $fstring->question, $xls_formats->head1);
00165 $worksheet1->write_string($row_offset1, 2, $fstring->responses, $xls_formats->head1);
00166 $row_offset1++;
00167 
00168 if (empty($items)) {
00169      $items=array();
00170 }
00171 foreach ($items as $item) {
00172     //get the class of item-typ
00173     $itemobj = feedback_get_item_class($item->typ);
00174     $row_offset1 = $itemobj->excelprint_item($worksheet1,
00175                                             $row_offset1,
00176                                             $xls_formats,
00177                                             $item,
00178                                             $mygroupid,
00179                                             $coursefilter);
00180 }
00181 
00183 //print the detailed sheet
00185 //get the completeds
00186 
00187 $completeds = feedback_get_completeds_group($feedback, $mygroupid, $coursefilter);
00188 //important: for each completed you have to print each item, even if it is not filled out!!!
00189 //therefor for each completed we have to iterate over all items of the feedback
00190 //this is done by feedback_excelprint_detailed_items
00191 
00192 $row_offset2 = 0;
00193 //first we print the table-header
00194 $row_offset2 = feedback_excelprint_detailed_head($worksheet2, $xls_formats, $items, $row_offset2);
00195 
00196 
00197 if (is_array($completeds)) {
00198     foreach ($completeds as $completed) {
00199         $row_offset2 = feedback_excelprint_detailed_items($worksheet2,
00200                                                          $xls_formats,
00201                                                          $completed,
00202                                                          $items,
00203                                                          $row_offset2);
00204     }
00205 }
00206 
00207 
00208 $workbook->close();
00209 exit;
00212 //functions
00214 
00215 
00216 function feedback_excelprint_detailed_head(&$worksheet, $xls_formats, $items, $row_offset) {
00217     global $fstring, $feedback;
00218 
00219     if (!$items) {
00220         return;
00221     }
00222     $col_offset = 0;
00223 
00224     $worksheet->write_string($row_offset + 1, $col_offset, $fstring->idnumber, $xls_formats->head2);
00225     $col_offset++;
00226 
00227     $worksheet->write_string($row_offset + 1, $col_offset, $fstring->username, $xls_formats->head2);
00228     $col_offset++;
00229 
00230     $worksheet->write_string($row_offset + 1, $col_offset, $fstring->fullname, $xls_formats->head2);
00231     $col_offset++;
00232 
00233     foreach ($items as $item) {
00234         $worksheet->write_string($row_offset, $col_offset, $item->name, $xls_formats->head2);
00235         $worksheet->write_string($row_offset + 1, $col_offset, $item->label, $xls_formats->head2);
00236         $col_offset++;
00237     }
00238 
00239     $worksheet->write_string($row_offset + 1, $col_offset, $fstring->courseid, $xls_formats->head2);
00240     $col_offset++;
00241 
00242     $worksheet->write_string($row_offset + 1, $col_offset, $fstring->course, $xls_formats->head2);
00243     $col_offset++;
00244 
00245     return $row_offset + 2;
00246 }
00247 
00248 function feedback_excelprint_detailed_items(&$worksheet, $xls_formats,
00249                                             $completed, $items, $row_offset) {
00250     global $DB, $fstring;
00251 
00252     if (!$items) {
00253         return;
00254     }
00255     $col_offset = 0;
00256     $courseid = 0;
00257 
00258     $feedback = $DB->get_record('feedback', array('id'=>$completed->feedback));
00259     //get the username
00260     //anonymous users are separated automatically because the userid in the completed is "0"
00261     if ($user = $DB->get_record('user', array('id'=>$completed->userid))) {
00262         if ($completed->anonymous_response == FEEDBACK_ANONYMOUS_NO) {
00263             $worksheet->write_string($row_offset, $col_offset, $user->idnumber, $xls_formats->head2);
00264             $col_offset++;
00265             $userfullname = fullname($user);
00266             $worksheet->write_string($row_offset, $col_offset, $user->username, $xls_formats->head2);
00267             $col_offset++;
00268         } else {
00269             $userfullname = $fstring->anonymous_user;
00270             $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
00271             $col_offset++;
00272             $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
00273             $col_offset++;
00274         }
00275     } else {
00276         $userfullname = $fstring->anonymous_user;
00277         $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
00278         $col_offset++;
00279         $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
00280         $col_offset++;
00281     }
00282 
00283     $worksheet->write_string($row_offset, $col_offset, $userfullname, $xls_formats->head2);
00284 
00285     $col_offset++;
00286     foreach ($items as $item) {
00287         $params = array('item' => $item->id, 'completed' => $completed->id);
00288         $value = $DB->get_record('feedback_value', $params);
00289 
00290         $itemobj = feedback_get_item_class($item->typ);
00291         $printval = $itemobj->get_printval($item, $value);
00292         $printval = trim($printval);
00293 
00294         if (is_numeric($printval)) {
00295             $worksheet->write_number($row_offset, $col_offset, $printval, $xls_formats->default);
00296         } else if ($printval != '') {
00297             $worksheet->write_string($row_offset, $col_offset, $printval, $xls_formats->default);
00298         }
00299         $printval = '';
00300         $col_offset++;
00301         $courseid = isset($value->course_id) ? $value->course_id : 0;
00302         if ($courseid == 0) {
00303             $courseid = $feedback->course;
00304         }
00305     }
00306     $worksheet->write_number($row_offset, $col_offset, $courseid, $xls_formats->default);
00307     $col_offset++;
00308     if (isset($courseid) AND $course = $DB->get_record('course', array('id' => $courseid))) {
00309         $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
00310         $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
00311         $worksheet->write_string($row_offset, $col_offset, $shortname, $xls_formats->default);
00312     }
00313     return $row_offset + 1;
00314 }
 All Data Structures Namespaces Files Functions Variables Enumerations