format.php
来自「很棒的在线教学系统」· PHP 代码 · 共 922 行 · 第 1/3 页
PHP
922 行
<?php // $Id: format.php,v 1.12.4.3 2008/08/04 10:07:54 thepurpleblob Exp $require_once("$CFG->dirroot/question/format/qti2/qt_common.php");/////////////////////////////////////////////////////////////////////////////// IMS QTI 2.0 FORMAT////// HISTORY: created 28.01.2005 brian@mediagonal.ch////////////////////////////////////////////////////////////////////////////// Based on format.php, included by ../../import.php/** * @package questionbank * @subpackage importexport */define('CLOZE_TRAILING_TEXT_ID', 9999999);class qformat_qti2 extends qformat_default { var $lang; function provide_export() { return true; } function indent_xhtml($source, $indenter = ' ') { // xml tidier-upper // (c) Ari Koivula http://ventionline.com // Remove all pre-existing formatting. // Remove all newlines. $source = str_replace("\n", '', $source); $source = str_replace("\r", '', $source); // Remove all tabs. $source = str_replace("\t", '', $source); // Remove all space after ">" and before "<". $source = ereg_replace(">( )*", ">", $source); $source = ereg_replace("( )*<", "<", $source); // Iterate through the source. $level = 0; $source_len = strlen($source); $pt = 0; while ($pt < $source_len) { if ($source{$pt} === '<') { // We have entered a tag. // Remember the point where the tag starts. $started_at = $pt; $tag_level = 1; // If the second letter of the tag is "/", assume its an ending tag. if ($source{$pt+1} === '/') { $tag_level = -1; } // If the second letter of the tag is "!", assume its an "invisible" tag. if ($source{$pt+1} === '!') { $tag_level = 0; } // Iterate throught the source until the end of tag. while ($source{$pt} !== '>') { $pt++; } // If the second last letter is "/", assume its a self ending tag. if ($source{$pt-1} === '/') { $tag_level = 0; } $tag_lenght = $pt+1-$started_at; // Decide the level of indention for this tag. // If this was an ending tag, decrease indent level for this tag.. if ($tag_level === -1) { $level--; } // Place the tag in an array with proper indention. $array[] = str_repeat($indenter, $level).substr($source, $started_at, $tag_lenght); // If this was a starting tag, increase the indent level after this tag. if ($tag_level === 1) { $level++; } // if it was a self closing tag, dont do anything. } // Were out of the tag. // If next letter exists... if (($pt+1) < $source_len) { // ... and its not an "<". if ($source{$pt+1} !== '<') { $started_at = $pt+1; // Iterate through the source until the start of new tag or until we reach the end of file. while ($source{$pt} !== '<' && $pt < $source_len) { $pt++; } // If we found a "<" (we didnt find the end of file) if ($source{$pt} === '<') { $tag_lenght = $pt-$started_at; // Place the stuff in an array with proper indention. $array[] = str_repeat($indenter, $level).substr($source, $started_at, $tag_lenght); } // If the next tag is "<", just advance pointer and let the tag indenter take care of it. } else { $pt++; } // If the next letter doesnt exist... Were done... well, almost.. } else { break; } } // Replace old source with the new one we just collected into our array. $source = implode($array, "\n"); return $source; } function importpreprocess() { global $CFG; error("Sorry, importing this format is not yet implemented!", "$CFG->wwwroot/mod/quiz/import.php?category=$category->id"); } function exportpreprocess() { global $CFG; require_once("{$CFG->libdir}/smarty/Smarty.class.php"); // assign the language for the export: by parameter, SESSION, USER, or the default of 'en' $lang = current_language(); $this->lang = $lang; return parent::exportpreprocess(); } function export_file_extension() { // override default type so extension is .xml return ".zip"; } function get_qtype( $type_id ) { // translates question type code number into actual name switch( $type_id ) { case TRUEFALSE: $name = 'truefalse'; break; case MULTICHOICE: $name = 'multichoice'; break; case SHORTANSWER: $name = 'shortanswer'; break; case NUMERICAL: $name = 'numerical'; break; case MATCH: $name = 'matching'; break; case DESCRIPTION: $name = 'description'; break; case MULTIANSWER: $name = 'multianswer'; break; default: $name = 'Unknown'; } return $name; } function writetext( $raw ) { // generates <text></text> tags, processing raw text therein // for now, don't allow any additional tags in text // otherwise xml rules would probably get broken $raw = strip_tags( $raw ); return "<text>$raw</text>\n"; }/** * flattens $object['media'], copies $object['media'] to $path, and sets $object['mediamimetype'] * * @param array &$object containing a field 'media' * @param string $path the full path name to where the media files need to be copied * @param int $courseid * @return: mixed - true on success or in case of an empty media field, an error string if the file copy fails */function copy_and_flatten(&$object, $path, $courseid) { global $CFG; if (!empty($object['media'])) { $location = $object['media']; $object['media'] = $this->flatten_image_name($location); if (!@copy("{$CFG->dataroot}/$courseid/$location", "$path/{$object['media']}")) { return "Failed to copy {$CFG->dataroot}/$courseid/$location to $path/{$object['media']}"; } if (empty($object['mediamimetype'])) { $object['mediamimetype'] = mimeinfo('type', $object['media']); } } return true;}/** * copies all files needed by the questions to the given $path, and flattens the file names * * @param array $questions the question objects * @param string $path the full path name to where the media files need to be copied * @param int $courseid * @return mixed true on success, an array of error messages otherwise */function handle_questions_media(&$questions, $path, $courseid) { global $CFG; $errors = array(); foreach ($questions as $key=>$question) { // todo: handle in-line media (specified in the question text) if (!empty($question->image)) { $location = $questions[$key]->image; $questions[$key]->mediaurl = $this->flatten_image_name($location); if (!@copy("{$CFG->dataroot}/$courseid/$location", "$path/{$questions[$key]->mediaurl}")) { $errors[] = "Failed to copy {$CFG->dataroot}/$courseid/$location to $path/{$questions[$key]->mediaurl}"; } if (empty($question->mediamimetype)) { $questions[$key]->mediamimetype = mimeinfo('type', $question->image); } } } return empty($errors) ? true : $errors;}/** * exports the questions in a question category to the given location * * The parent class method was overridden because the IMS export consists of multiple files * * @param string $filename the directory name which will hold the exported files * @return boolean - or errors out */ function exportprocess() { global $CFG; $courseid = $this->course->id; // create a directory for the exports (if not already existing) if (!$export_dir = make_upload_directory($this->question_get_export_dir().'/'.$this->filename)) { error( get_string('cannotcreatepath','quiz',$export_dir) ); } $path = $CFG->dataroot.'/'.$this->question_get_export_dir().'/'.$this->filename; // get the questions (from database) in this category // $questions = get_records("question","category",$this->category->id); $questions = get_questions_category( $this->category ); notify("Exporting ".count($questions)." questions."); $count = 0; // create the imsmanifest file $smarty =& $this->init_smarty(); $this->add_qti_info($questions); // copy files used by the main questions to the export directory $result = $this->handle_questions_media($questions, $path, $courseid); if ($result !== true) { notify(implode("<br />", $result)); } $manifestquestions = $this->objects_to_array($questions); $manifestid = str_replace(array(':', '/'), array('-','_'), "question_category_{$this->category->id}---{$CFG->wwwroot}"); $smarty->assign('externalfiles', 1); $smarty->assign('manifestidentifier', $manifestid); $smarty->assign('quiztitle', "question_category_{$this->category->id}"); $smarty->assign('quizinfo', "All questions in category {$this->category->id}"); $smarty->assign('questions', $manifestquestions); $smarty->assign('lang', $this->lang); $smarty->error_reporting = 99; $expout = $smarty->fetch('imsmanifest.tpl'); $filepath = $path.'/imsmanifest.xml'; if (empty($expout)) { error("Unkown error - empty imsmanifest.xml"); } if (!$fh=fopen($filepath,"w")) { error("Cannot open for writing: $filepath"); } if (!fwrite($fh, $expout)) { error("Cannot write exported questions to $filepath"); } fclose($fh); // iterate through questions foreach($questions as $question) { // results are first written into string (and then to a file) $count++; echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>"; $expout = $this->writequestion( $question , null, true, $path) . "\n"; $expout = $this->presave_process( $expout ); $filepath = $path.'/'.$this->get_assesment_item_id($question) . ".xml"; if (!$fh=fopen($filepath,"w")) { error("Cannot open for writing: $filepath"); } if (!fwrite($fh, $expout)) { error("Cannot write exported questions to $filepath"); } fclose($fh); } // zip files into single export file zip_files( array($path), "$path.zip" );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?