Differences Between: [Versions 310 and 311] [Versions 311 and 402] [Versions 311 and 403]
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. 13 // 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/>. 16 17 /** 18 * Blackboard V5 and V6 question importer. 19 * 20 * @package qformat_blackboard_six 21 * @copyright 2005 Michael Penney 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 require_once($CFG->libdir . '/xmlize.php'); 28 require_once($CFG->dirroot . '/question/format/blackboard_six/formatbase.php'); 29 require_once($CFG->dirroot . '/question/format/blackboard_six/formatqti.php'); 30 require_once($CFG->dirroot . '/question/format/blackboard_six/formatpool.php'); 31 32 /** 33 * Class to represent a Blackboard file. 34 * 35 * @package qformat_blackboard_six 36 * @copyright 2005 Michael Penney 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class qformat_blackboard_six_file { 40 /** @var int type of file being imported, one of the constants FILETYPE_QTI or FILETYPE_POOL. */ 41 public $filetype; 42 /** @var string the xml text */ 43 public $text; 44 /** @var string path to path to root of image tree in unzipped archive. */ 45 public $filebase = ''; 46 } 47 48 /** 49 * Blackboard Six QTI file importer class. 50 * 51 * @package qformat_blackboard_six 52 * @copyright 2005 Michael Penney 53 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 54 */ 55 class qformat_blackboard_six extends qformat_blackboard_six_base { 56 /** @var int Blackboard assessment qti files were always imported by the blackboard_six plugin. */ 57 const FILETYPE_QTI = 1; 58 /** @var int Blackboard question pool files were previously handled by the blackboard plugin. */ 59 const FILETYPE_POOL = 2; 60 61 /** 62 * Return the content of a file given by its path in the tempdir directory. 63 * 64 * @param string $path path to the file inside tempdir 65 * @return mixed contents array or false on failure 66 */ 67 public function get_filecontent($path) { 68 $fullpath = $this->tempdir . '/' . clean_param($path, PARAM_PATH); 69 if (is_file($fullpath) && is_readable($fullpath)) { 70 return file_get_contents($fullpath); 71 } 72 return false; 73 } 74 75 /** 76 * Return content of all files containing questions, 77 * as an array one element for each file found, 78 * For each file, the corresponding element is an array of lines. 79 * 80 * @param string $filename name of file 81 * @return mixed contents array or false on failure 82 */ 83 public function readdata($filename) { 84 global $CFG; 85 86 // Find if we are importing a .dat file. 87 if (strtolower(pathinfo($filename, PATHINFO_EXTENSION)) == 'dat') { 88 if (!is_readable($filename)) { 89 $this->error(get_string('filenotreadable', 'error')); 90 return false; 91 } 92 93 $fileobj = new qformat_blackboard_six_file(); 94 95 // As we are not importing a .zip file, 96 // there is no imsmanifest, and it is not possible 97 // to parse it to find the file type. 98 // So we need to guess the file type by looking at the content. 99 // For now we will do that searching for a required tag. 100 // This is certainly not bullet-proof but works for all usual files. 101 $fileobj->text = file_get_contents($filename); 102 if (strpos($fileobj->text, '<questestinterop>')) { 103 $fileobj->filetype = self::FILETYPE_QTI; 104 } 105 if (strpos($fileobj->text, '<POOL>')) { 106 $fileobj->filetype = self::FILETYPE_POOL; 107 } 108 // In all other cases we are not able to handle this question file. 109 110 // Readquestions is now expecting an array of strings. 111 return array($fileobj); 112 } 113 // We are importing a zip file. 114 // Create name for temporary directory. 115 $uniquecode = time(); 116 $this->tempdir = make_temp_directory('bbquiz_import/' . $uniquecode); 117 if (is_readable($filename)) { 118 if (!copy($filename, $this->tempdir . '/bboard.zip')) { 119 $this->error(get_string('cannotcopybackup', 'question')); 120 fulldelete($this->tempdir); 121 return false; 122 } 123 $packer = get_file_packer('application/zip'); 124 if ($packer->extract_to_pathname($this->tempdir . '/bboard.zip', $this->tempdir)) { 125 $dom = new DomDocument(); 126 127 if (!$dom->load($this->tempdir . '/imsmanifest.xml')) { 128 $this->error(get_string('errormanifest', 'qformat_blackboard_six')); 129 fulldelete($this->tempdir); 130 return false; 131 } 132 133 $xpath = new DOMXPath($dom); 134 135 // We starts from the root element. 136 $query = '//resources/resource'; 137 $qfile = array(); 138 139 $examfiles = $xpath->query($query); 140 foreach ($examfiles as $examfile) { 141 $fileobj = new qformat_blackboard_six_file(); 142 143 if ($examfile->getAttribute('type') == 'assessment/x-bb-qti-test' 144 || $examfile->getAttribute('type') == 'assessment/x-bb-qti-pool') { 145 146 if ($content = $this->get_filecontent($examfile->getAttribute('bb:file'))) { 147 $fileobj->filetype = self::FILETYPE_QTI; 148 $fileobj->filebase = $this->tempdir; 149 $fileobj->text = $content; 150 $qfile[] = $fileobj; 151 } 152 } 153 if ($examfile->getAttribute('type') == 'assessment/x-bb-pool') { 154 if ($examfile->getAttribute('baseurl')) { 155 $fileobj->filebase = $this->tempdir. '/' . clean_param($examfile->getAttribute('baseurl'), PARAM_PATH); 156 } 157 if ($content = $this->get_filecontent($examfile->getAttribute('file'))) { 158 $fileobj->filetype = self::FILETYPE_POOL; 159 $fileobj->text = $content; 160 $qfile[] = $fileobj; 161 } 162 } 163 } 164 165 if ($qfile) { 166 return $qfile; 167 } else { 168 $this->error(get_string('cannotfindquestionfile', 'question')); 169 fulldelete($this->tempdir); 170 } 171 } else { 172 $this->error(get_string('cannotunzip', 'question')); 173 fulldelete($this->temp_dir); 174 } 175 } else { 176 $this->error(get_string('cannotreaduploadfile', 'error')); 177 fulldelete($this->tempdir); 178 } 179 return false; 180 } 181 182 /** 183 * Parse the array of objects into an array of questions. 184 * Each object is the content of a .dat questions file. 185 * This *could* burn memory - but it won't happen that much 186 * so fingers crossed! 187 * 188 * @param array $lines array of qformat_blackboard_six_file objects for each input file. 189 * @return array (of objects) question objects. 190 */ 191 public function readquestions($lines) { 192 193 // Set up array to hold all our questions. 194 $questions = array(); 195 196 // Each element of $lines is a qformat_blackboard_six_file object. 197 foreach ($lines as $fileobj) { 198 if ($fileobj->filetype == self::FILETYPE_QTI) { 199 $importer = new qformat_blackboard_six_qti(); 200 } else if ($fileobj->filetype == self::FILETYPE_POOL) { 201 $importer = new qformat_blackboard_six_pool(); 202 } else { 203 // In all other cases we are not able to import the file. 204 debugging('fileobj type not recognised', DEBUG_DEVELOPER); 205 continue; 206 } 207 $importer->set_filebase($fileobj->filebase); 208 $questions = array_merge($questions, $importer->readquestions($fileobj->text)); 209 } 210 211 // Give any unnamed categories generated names. 212 $unnamedcount = 0; 213 foreach ($questions as $question) { 214 if ($question->qtype == 'category' && $question->category == '') { 215 $question->category = get_string('importedcategory', 'qformat_blackboard_six', ++$unnamedcount); 216 } 217 } 218 219 return $questions; 220 } 221 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body