|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 require_once ("../../config.php"); 00028 00029 // Check that all the parameters have been provided. 00030 00031 $id = required_param('id', PARAM_INT); // Course Module ID 00032 $type = optional_param('type', 'xls', PARAM_ALPHA); 00033 $group = optional_param('group', 0, PARAM_INT); 00034 00035 if (! $cm = get_coursemodule_from_id('survey', $id)) { 00036 print_error('invalidcoursemodule'); 00037 } 00038 00039 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) { 00040 print_error('coursemisconf'); 00041 } 00042 00043 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00044 00045 $PAGE->set_url('/mod/survey/download.php', array('id'=>$id, 'type'=>$type, 'group'=>$group)); 00046 00047 require_login($course->id, false, $cm); 00048 require_capability('mod/survey:download', $context) ; 00049 00050 if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) { 00051 print_error('invalidsurveyid', 'survey'); 00052 } 00053 00054 add_to_log($course->id, "survey", "download", $PAGE->url->out(), "$survey->id", $cm->id); 00055 00057 00058 $groupmode = groups_get_activity_groupmode($cm); // Groups are being used 00059 00060 if ($groupmode and $group) { 00061 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $group, null, false); 00062 } else { 00063 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', '', null, false); 00064 $group = false; 00065 } 00066 00067 // The order of the questions 00068 $order = explode(",", $survey->questions); 00069 00070 // Get the actual questions from the database 00071 $questions = $DB->get_records_list("survey_questions", "id", $order); 00072 00073 // Get an ordered array of questions 00074 $orderedquestions = array(); 00075 00076 $virtualscales = false; 00077 foreach ($order as $qid) { 00078 $orderedquestions[$qid] = $questions[$qid]; 00079 // Check if this question is using virtual scales 00080 if (!$virtualscales && $questions[$qid]->type < 0) { 00081 $virtualscales = true; 00082 } 00083 } 00084 $nestedorder = array();//will contain the subquestions attached to the main questions 00085 $preparray = array(); 00086 00087 foreach ($orderedquestions as $qid=>$question) { 00088 //$orderedquestions[$qid]->text = get_string($question->text, "survey"); 00089 if (!empty($question->multi)) { 00090 $actualqids = explode(",", $questions[$qid]->multi); 00091 foreach ($actualqids as $subqid) { 00092 if (!empty($orderedquestions[$subqid]->type)) { 00093 $orderedquestions[$subqid]->type = $questions[$qid]->type; 00094 } 00095 } 00096 } else { 00097 $actualqids = array($qid); 00098 } 00099 if ($virtualscales && $questions[$qid]->type < 0) { 00100 $nestedorder[$qid] = $actualqids; 00101 } else if (!$virtualscales && $question->type >= 0) { 00102 $nestedorder[$qid] = $actualqids; 00103 } else { 00104 //todo andrew this was added by me. Is it correct? 00105 $nestedorder[$qid] = array(); 00106 } 00107 } 00108 00109 $reversednestedorder = array(); 00110 foreach ($nestedorder as $qid=>$subqidarray) { 00111 foreach ($subqidarray as $subqui) { 00112 $reversednestedorder[$subqui] = $qid; 00113 } 00114 } 00115 00116 //need to get info on the sub-questions from the db and merge the arrays of questions 00117 $allquestions = array_merge($questions, $DB->get_records_list("survey_questions", "id", array_keys($reversednestedorder))); 00118 00119 //array_merge() messes up the keys so reinstate them 00120 $questions = array(); 00121 foreach($allquestions as $question) { 00122 $questions[$question->id] = $question; 00123 00124 //while were iterating over the questions get the question text 00125 $questions[$question->id]->text = get_string($questions[$question->id]->text, "survey"); 00126 } 00127 unset($allquestions); 00128 00129 // Get and collate all the results in one big array 00130 if (! $surveyanswers = $DB->get_records("survey_answers", array("survey"=>$survey->id), "time ASC")) { 00131 print_error('cannotfindanswer', 'survey'); 00132 } 00133 00134 $results = array(); 00135 00136 foreach ($surveyanswers as $surveyanswer) { 00137 if (!$group || isset($users[$surveyanswer->userid])) { 00138 //$questionid = $reversednestedorder[$surveyanswer->question]; 00139 $questionid = $surveyanswer->question; 00140 if (!array_key_exists($surveyanswer->userid, $results)) { 00141 $results[$surveyanswer->userid] = array('time'=>$surveyanswer->time); 00142 } 00143 $results[$surveyanswer->userid][$questionid]['answer1'] = $surveyanswer->answer1; 00144 $results[$surveyanswer->userid][$questionid]['answer2'] = $surveyanswer->answer2; 00145 } 00146 } 00147 00148 // Output the file as a valid ODS spreadsheet if required 00149 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); 00150 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext)); 00151 00152 if ($type == "ods") { 00153 require_once("$CFG->libdir/odslib.class.php"); 00154 00156 $downloadfilename = clean_filename(strip_tags($courseshortname.' '.format_string($survey->name, true))).'.ods'; 00158 $workbook = new MoodleODSWorkbook("-"); 00160 $workbook->send($downloadfilename); 00162 $myxls =& $workbook->add_worksheet(textlib::substr(strip_tags(format_string($survey->name,true)), 0, 31)); 00163 00164 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes"); 00165 $col=0; 00166 foreach ($header as $item) { 00167 $myxls->write_string(0,$col++,$item); 00168 } 00169 00170 foreach ($nestedorder as $key => $nestedquestions) { 00171 foreach ($nestedquestions as $key2 => $qid) { 00172 $question = $questions[$qid]; 00173 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { 00174 $myxls->write_string(0,$col++,"$question->text"); 00175 } 00176 if ($question->type == "2" || $question->type == "3") { 00177 $myxls->write_string(0,$col++,"$question->text (preferred)"); 00178 } 00179 } 00180 } 00181 00182 // $date = $workbook->addformat(); 00183 // $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below 00184 00185 $row = 0; 00186 foreach ($results as $user => $rest) { 00187 $col = 0; 00188 $row++; 00189 if (! $u = $DB->get_record("user", array("id"=>$user))) { 00190 print_error('invaliduserid'); 00191 } 00192 if ($n = $DB->get_record("survey_analysis", array("survey"=>$survey->id, "userid"=>$user))) { 00193 $notes = $n->notes; 00194 } else { 00195 $notes = "No notes made"; 00196 } 00197 $myxls->write_string($row,$col++,$survey->id); 00198 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true))); 00199 $myxls->write_string($row,$col++,$user); 00200 $myxls->write_string($row,$col++,$u->firstname); 00201 $myxls->write_string($row,$col++,$u->lastname); 00202 $myxls->write_string($row,$col++,$u->email); 00203 $myxls->write_string($row,$col++,$u->idnumber); 00204 $myxls->write_string($row,$col++, userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p") ); 00205 // $myxls->write_number($row,$col++,$results[$user]["time"],$date); 00206 $myxls->write_string($row,$col++,$notes); 00207 00208 foreach ($nestedorder as $key => $nestedquestions) { 00209 foreach ($nestedquestions as $key2 => $qid) { 00210 $question = $questions[$qid]; 00211 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { 00212 $myxls->write_string($row,$col++, $results[$user][$qid]["answer1"] ); 00213 } 00214 if ($question->type == "2" || $question->type == "3") { 00215 $myxls->write_string($row, $col++, $results[$user][$qid]["answer2"] ); 00216 } 00217 } 00218 } 00219 } 00220 $workbook->close(); 00221 00222 exit; 00223 } 00224 00225 // Output the file as a valid Excel spreadsheet if required 00226 00227 if ($type == "xls") { 00228 require_once("$CFG->libdir/excellib.class.php"); 00229 00231 $downloadfilename = clean_filename(strip_tags($courseshortname.' '.format_string($survey->name,true))).'.xls'; 00233 $workbook = new MoodleExcelWorkbook("-"); 00235 $workbook->send($downloadfilename); 00237 $myxls =& $workbook->add_worksheet(textlib::substr(strip_tags(format_string($survey->name,true)), 0, 31)); 00238 00239 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes"); 00240 $col=0; 00241 foreach ($header as $item) { 00242 $myxls->write_string(0,$col++,$item); 00243 } 00244 00245 foreach ($nestedorder as $key => $nestedquestions) { 00246 foreach ($nestedquestions as $key2 => $qid) { 00247 $question = $questions[$qid]; 00248 00249 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { 00250 $myxls->write_string(0,$col++,"$question->text"); 00251 } 00252 if ($question->type == "2" || $question->type == "3") { 00253 $myxls->write_string(0,$col++,"$question->text (preferred)"); 00254 } 00255 } 00256 } 00257 00258 // $date = $workbook->addformat(); 00259 // $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below 00260 00261 $row = 0; 00262 foreach ($results as $user => $rest) { 00263 $col = 0; 00264 $row++; 00265 if (! $u = $DB->get_record("user", array("id"=>$user))) { 00266 print_error('invaliduserid'); 00267 } 00268 if ($n = $DB->get_record("survey_analysis", array("survey"=>$survey->id, "userid"=>$user))) { 00269 $notes = $n->notes; 00270 } else { 00271 $notes = "No notes made"; 00272 } 00273 $myxls->write_string($row,$col++,$survey->id); 00274 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true))); 00275 $myxls->write_string($row,$col++,$user); 00276 $myxls->write_string($row,$col++,$u->firstname); 00277 $myxls->write_string($row,$col++,$u->lastname); 00278 $myxls->write_string($row,$col++,$u->email); 00279 $myxls->write_string($row,$col++,$u->idnumber); 00280 $myxls->write_string($row,$col++, userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p") ); 00281 // $myxls->write_number($row,$col++,$results[$user]["time"],$date); 00282 $myxls->write_string($row,$col++,$notes); 00283 00284 foreach ($nestedorder as $key => $nestedquestions) { 00285 foreach ($nestedquestions as $key2 => $qid) { 00286 $question = $questions[$qid]; 00287 if (($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") 00288 && array_key_exists($qid, $results[$user]) ){ 00289 $myxls->write_string($row,$col++, $results[$user][$qid]["answer1"] ); 00290 } 00291 if (($question->type == "2" || $question->type == "3") 00292 && array_key_exists($qid, $results[$user]) ){ 00293 $myxls->write_string($row, $col++, $results[$user][$qid]["answer2"] ); 00294 } 00295 } 00296 } 00297 } 00298 $workbook->close(); 00299 00300 exit; 00301 } 00302 00303 // Otherwise, return the text file. 00304 00305 // Print header to force download 00306 00307 header("Content-Type: application/download\n"); 00308 00309 $downloadfilename = clean_filename(strip_tags($courseshortname.' '.format_string($survey->name,true))); 00310 header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\""); 00311 00312 // Print names of all the fields 00313 00314 echo "surveyid surveyname userid firstname lastname email idnumber time "; 00315 00316 foreach ($nestedorder as $key => $nestedquestions) { 00317 foreach ($nestedquestions as $key2 => $qid) { 00318 $question = $questions[$qid]; 00319 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { 00320 echo "$question->text "; 00321 } 00322 if ($question->type == "2" || $question->type == "3") { 00323 echo "$question->text (preferred) "; 00324 } 00325 } 00326 } 00327 echo "\n"; 00328 00329 // Print all the lines of data. 00330 foreach ($results as $user => $rest) { 00331 if (! $u = $DB->get_record("user", array("id"=>$user))) { 00332 print_error('invaliduserid'); 00333 } 00334 echo $survey->id."\t"; 00335 echo strip_tags(format_string($survey->name,true))."\t"; 00336 echo $user."\t"; 00337 echo $u->firstname."\t"; 00338 echo $u->lastname."\t"; 00339 echo $u->email."\t"; 00340 echo $u->idnumber."\t"; 00341 echo userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p")."\t"; 00342 00343 foreach ($nestedorder as $key => $nestedquestions) { 00344 foreach ($nestedquestions as $key2 => $qid) { 00345 $question = $questions[$qid]; 00346 00347 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { 00348 echo $results[$user][$qid]["answer1"]." "; 00349 } 00350 if ($question->type == "2" || $question->type == "3") { 00351 echo $results[$user][$qid]["answer2"]." "; 00352 } 00353 } 00354 } 00355 echo "\n"; 00356 } 00357 00358 exit;