| 1 | 
           efrain | 
           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 | 
               /** @var string temporary directory/folder.  */
  | 
        
        
            | 
            | 
           61 | 
               public $temp_dir;
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
               /**
  | 
        
        
            | 
            | 
           64 | 
                * Return the content of a file given by its path in the tempdir directory.
  | 
        
        
            | 
            | 
           65 | 
                *
  | 
        
        
            | 
            | 
           66 | 
                * @param string $path path to the file inside tempdir
  | 
        
        
            | 
            | 
           67 | 
                * @return mixed contents array or false on failure
  | 
        
        
            | 
            | 
           68 | 
                */
  | 
        
        
            | 
            | 
           69 | 
               public function get_filecontent($path) {
  | 
        
        
            | 
            | 
           70 | 
                   $fullpath = $this->tempdir . '/' . clean_param($path, PARAM_PATH);
  | 
        
        
            | 
            | 
           71 | 
                   if (is_file($fullpath) && is_readable($fullpath)) {
  | 
        
        
            | 
            | 
           72 | 
                       return file_get_contents($fullpath);
  | 
        
        
            | 
            | 
           73 | 
                   }
  | 
        
        
            | 
            | 
           74 | 
                   return false;
  | 
        
        
            | 
            | 
           75 | 
               }
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
               /**
  | 
        
        
            | 
            | 
           78 | 
                * Return content of all files containing questions,
  | 
        
        
            | 
            | 
           79 | 
                * as an array one element for each file found,
  | 
        
        
            | 
            | 
           80 | 
                * For each file, the corresponding element is an array of lines.
  | 
        
        
            | 
            | 
           81 | 
                *
  | 
        
        
            | 
            | 
           82 | 
                * @param string $filename name of file
  | 
        
        
            | 
            | 
           83 | 
                * @return mixed contents array or false on failure
  | 
        
        
            | 
            | 
           84 | 
                */
  | 
        
        
            | 
            | 
           85 | 
               public function readdata($filename) {
  | 
        
        
            | 
            | 
           86 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           87 | 
              | 
        
        
            | 
            | 
           88 | 
                   // Find if we are importing a .dat file.
  | 
        
        
            | 
            | 
           89 | 
                   if (strtolower(pathinfo($filename, PATHINFO_EXTENSION)) == 'dat') {
  | 
        
        
            | 
            | 
           90 | 
                       if (!is_readable($filename)) {
  | 
        
        
            | 
            | 
           91 | 
                           $this->error(get_string('filenotreadable', 'error'));
  | 
        
        
            | 
            | 
           92 | 
                           return false;
  | 
        
        
            | 
            | 
           93 | 
                       }
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
                       $fileobj = new qformat_blackboard_six_file();
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
                       // As we are not importing a .zip file,
  | 
        
        
            | 
            | 
           98 | 
                       // there is no imsmanifest, and it is not possible
  | 
        
        
            | 
            | 
           99 | 
                       // to parse it to find the file type.
  | 
        
        
            | 
            | 
           100 | 
                       // So we need to guess the file type by looking at the content.
  | 
        
        
            | 
            | 
           101 | 
                       // For now we will do that searching for a required tag.
  | 
        
        
            | 
            | 
           102 | 
                       // This is certainly not bullet-proof but works for all usual files.
  | 
        
        
            | 
            | 
           103 | 
                       $fileobj->text = file_get_contents($filename);
  | 
        
        
            | 
            | 
           104 | 
                       if (strpos($fileobj->text, '<questestinterop>')) {
  | 
        
        
            | 
            | 
           105 | 
                           $fileobj->filetype = self::FILETYPE_QTI;
  | 
        
        
            | 
            | 
           106 | 
                       }
  | 
        
        
            | 
            | 
           107 | 
                       if (strpos($fileobj->text, '<POOL>')) {
  | 
        
        
            | 
            | 
           108 | 
                           $fileobj->filetype = self::FILETYPE_POOL;
  | 
        
        
            | 
            | 
           109 | 
                       }
  | 
        
        
            | 
            | 
           110 | 
                       // In all other cases we are not able to handle this question file.
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
                       // Readquestions is now expecting an array of strings.
  | 
        
        
            | 
            | 
           113 | 
                       return array($fileobj);
  | 
        
        
            | 
            | 
           114 | 
                   }
  | 
        
        
            | 
            | 
           115 | 
                   // We are importing a zip file.
  | 
        
        
            | 
            | 
           116 | 
                   // Create name for temporary directory.
  | 
        
        
            | 
            | 
           117 | 
                   $uniquecode = time();
  | 
        
        
            | 
            | 
           118 | 
                   $this->tempdir = make_temp_directory('bbquiz_import/' . $uniquecode);
  | 
        
        
            | 
            | 
           119 | 
                   if (is_readable($filename)) {
  | 
        
        
            | 
            | 
           120 | 
                       if (!copy($filename, $this->tempdir . '/bboard.zip')) {
  | 
        
        
            | 
            | 
           121 | 
                           $this->error(get_string('cannotcopybackup', 'question'));
  | 
        
        
            | 
            | 
           122 | 
                           fulldelete($this->tempdir);
  | 
        
        
            | 
            | 
           123 | 
                           return false;
  | 
        
        
            | 
            | 
           124 | 
                       }
  | 
        
        
            | 
            | 
           125 | 
                       $packer = get_file_packer('application/zip');
  | 
        
        
            | 
            | 
           126 | 
                       if ($packer->extract_to_pathname($this->tempdir . '/bboard.zip', $this->tempdir)) {
  | 
        
        
            | 
            | 
           127 | 
                           $dom = new DomDocument();
  | 
        
        
            | 
            | 
           128 | 
              | 
        
        
            | 
            | 
           129 | 
                           if (!$dom->load($this->tempdir . '/imsmanifest.xml')) {
  | 
        
        
            | 
            | 
           130 | 
                               $this->error(get_string('errormanifest', 'qformat_blackboard_six'));
  | 
        
        
            | 
            | 
           131 | 
                               fulldelete($this->tempdir);
  | 
        
        
            | 
            | 
           132 | 
                               return false;
  | 
        
        
            | 
            | 
           133 | 
                           }
  | 
        
        
            | 
            | 
           134 | 
              | 
        
        
            | 
            | 
           135 | 
                           $xpath = new DOMXPath($dom);
  | 
        
        
            | 
            | 
           136 | 
              | 
        
        
            | 
            | 
           137 | 
                           // We starts from the root element.
  | 
        
        
            | 
            | 
           138 | 
                           $query = '//resources/resource';
  | 
        
        
            | 
            | 
           139 | 
                           $qfile = array();
  | 
        
        
            | 
            | 
           140 | 
              | 
        
        
            | 
            | 
           141 | 
                           $examfiles = $xpath->query($query);
  | 
        
        
            | 
            | 
           142 | 
                           foreach ($examfiles as $examfile) {
  | 
        
        
            | 
            | 
           143 | 
                               $fileobj = new qformat_blackboard_six_file();
  | 
        
        
            | 
            | 
           144 | 
              | 
        
        
            | 
            | 
           145 | 
                               if ($examfile->getAttribute('type') == 'assessment/x-bb-qti-test'
  | 
        
        
            | 
            | 
           146 | 
                                       || $examfile->getAttribute('type') == 'assessment/x-bb-qti-pool') {
  | 
        
        
            | 
            | 
           147 | 
              | 
        
        
            | 
            | 
           148 | 
                                   if ($content = $this->get_filecontent($examfile->getAttribute('bb:file'))) {
  | 
        
        
            | 
            | 
           149 | 
                                       $fileobj->filetype = self::FILETYPE_QTI;
  | 
        
        
            | 
            | 
           150 | 
                                       $fileobj->filebase = $this->tempdir;
  | 
        
        
            | 
            | 
           151 | 
                                       $fileobj->text = $content;
  | 
        
        
            | 
            | 
           152 | 
                                       $qfile[] = $fileobj;
  | 
        
        
            | 
            | 
           153 | 
                                   }
  | 
        
        
            | 
            | 
           154 | 
                               }
  | 
        
        
            | 
            | 
           155 | 
                               if ($examfile->getAttribute('type') == 'assessment/x-bb-pool') {
  | 
        
        
            | 
            | 
           156 | 
                                   if ($examfile->getAttribute('baseurl')) {
  | 
        
        
            | 
            | 
           157 | 
                                       $fileobj->filebase = $this->tempdir. '/' . clean_param($examfile->getAttribute('baseurl'), PARAM_PATH);
  | 
        
        
            | 
            | 
           158 | 
                                   }
  | 
        
        
            | 
            | 
           159 | 
                                   if ($content = $this->get_filecontent($examfile->getAttribute('file'))) {
  | 
        
        
            | 
            | 
           160 | 
                                       $fileobj->filetype = self::FILETYPE_POOL;
  | 
        
        
            | 
            | 
           161 | 
                                       $fileobj->text = $content;
  | 
        
        
            | 
            | 
           162 | 
                                       $qfile[] = $fileobj;
  | 
        
        
            | 
            | 
           163 | 
                                   }
  | 
        
        
            | 
            | 
           164 | 
                               }
  | 
        
        
            | 
            | 
           165 | 
                           }
  | 
        
        
            | 
            | 
           166 | 
              | 
        
        
            | 
            | 
           167 | 
                           if ($qfile) {
  | 
        
        
            | 
            | 
           168 | 
                               return $qfile;
  | 
        
        
            | 
            | 
           169 | 
                           } else {
  | 
        
        
            | 
            | 
           170 | 
                               $this->error(get_string('cannotfindquestionfile', 'question'));
  | 
        
        
            | 
            | 
           171 | 
                               fulldelete($this->tempdir);
  | 
        
        
            | 
            | 
           172 | 
                           }
  | 
        
        
            | 
            | 
           173 | 
                       } else {
  | 
        
        
            | 
            | 
           174 | 
                           $this->error(get_string('cannotunzip', 'question'));
  | 
        
        
            | 
            | 
           175 | 
                           fulldelete($this->temp_dir);
  | 
        
        
            | 
            | 
           176 | 
                       }
  | 
        
        
            | 
            | 
           177 | 
                   } else {
  | 
        
        
            | 
            | 
           178 | 
                       $this->error(get_string('cannotreaduploadfile', 'error'));
  | 
        
        
            | 
            | 
           179 | 
                       fulldelete($this->tempdir);
  | 
        
        
            | 
            | 
           180 | 
                   }
  | 
        
        
            | 
            | 
           181 | 
                   return false;
  | 
        
        
            | 
            | 
           182 | 
               }
  | 
        
        
            | 
            | 
           183 | 
              | 
        
        
            | 
            | 
           184 | 
               /**
  | 
        
        
            | 
            | 
           185 | 
                * Parse the array of objects into an array of questions.
  | 
        
        
            | 
            | 
           186 | 
                * Each object is the content of a .dat questions file.
  | 
        
        
            | 
            | 
           187 | 
                * This *could* burn memory - but it won't happen that much
  | 
        
        
            | 
            | 
           188 | 
                * so fingers crossed!
  | 
        
        
            | 
            | 
           189 | 
                *
  | 
        
        
            | 
            | 
           190 | 
                * @param array $lines array of qformat_blackboard_six_file objects for each input file.
  | 
        
        
            | 
            | 
           191 | 
                * @return array (of objects) question objects.
  | 
        
        
            | 
            | 
           192 | 
                */
  | 
        
        
            | 
            | 
           193 | 
               public function readquestions($lines) {
  | 
        
        
            | 
            | 
           194 | 
              | 
        
        
            | 
            | 
           195 | 
                   // Set up array to hold all our questions.
  | 
        
        
            | 
            | 
           196 | 
                   $questions = array();
  | 
        
        
            | 
            | 
           197 | 
              | 
        
        
            | 
            | 
           198 | 
                   // Each element of $lines is a qformat_blackboard_six_file object.
  | 
        
        
            | 
            | 
           199 | 
                   foreach ($lines as $fileobj) {
  | 
        
        
            | 
            | 
           200 | 
                       if ($fileobj->filetype == self::FILETYPE_QTI) {
  | 
        
        
            | 
            | 
           201 | 
                           $importer = new qformat_blackboard_six_qti();
  | 
        
        
            | 
            | 
           202 | 
                       } else if ($fileobj->filetype == self::FILETYPE_POOL) {
  | 
        
        
            | 
            | 
           203 | 
                           $importer = new qformat_blackboard_six_pool();
  | 
        
        
            | 
            | 
           204 | 
                       } else {
  | 
        
        
            | 
            | 
           205 | 
                           // In all other cases we are not able to import the file.
  | 
        
        
            | 
            | 
           206 | 
                           debugging('fileobj type not recognised', DEBUG_DEVELOPER);
  | 
        
        
            | 
            | 
           207 | 
                           continue;
  | 
        
        
            | 
            | 
           208 | 
                       }
  | 
        
        
            | 
            | 
           209 | 
                       $importer->set_filebase($fileobj->filebase);
  | 
        
        
            | 
            | 
           210 | 
                       $questions = array_merge($questions, $importer->readquestions($fileobj->text));
  | 
        
        
            | 
            | 
           211 | 
                   }
  | 
        
        
            | 
            | 
           212 | 
              | 
        
        
            | 
            | 
           213 | 
                   // Give any unnamed categories generated names.
  | 
        
        
            | 
            | 
           214 | 
                   $unnamedcount = 0;
  | 
        
        
            | 
            | 
           215 | 
                   foreach ($questions as $question) {
  | 
        
        
            | 
            | 
           216 | 
                       if ($question->qtype == 'category' && $question->category == '') {
  | 
        
        
            | 
            | 
           217 | 
                           $question->category = get_string('importedcategory', 'qformat_blackboard_six', ++$unnamedcount);
  | 
        
        
            | 
            | 
           218 | 
                       }
  | 
        
        
            | 
            | 
           219 | 
                   }
  | 
        
        
            | 
            | 
           220 | 
              | 
        
        
            | 
            | 
           221 | 
                   return $questions;
  | 
        
        
            | 
            | 
           222 | 
               }
  | 
        
        
            | 
            | 
           223 | 
           }
  |