Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/format/learnwise/format.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 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00029 
00043 class qformat_learnwise extends qformat_default {
00044 
00045     function provide_import() {
00046         return true;
00047     }
00048 
00049     function readquestions($lines) {
00050         $questions = array();
00051         $currentquestion = array();
00052 
00053         foreach($lines as $line) {
00054             $line = trim($line);
00055             $currentquestion[] = $line;
00056 
00057             if ($question = $this->readquestion($currentquestion)) {
00058                 $questions[] = $question;
00059                 $currentquestion = array();
00060             }
00061         }
00062         return $questions;
00063     }
00064 
00065     function readquestion($lines) {
00066         $text = implode(' ', $lines);
00067         $text = str_replace(array('\t','\n','\r','\''), array('','','','\\\''), $text);
00068 
00069         $startpos = strpos($text, '<question type');
00070         $endpos = strpos($text, '</question>');
00071         if ($startpos === false || $endpos === false) {
00072             return false;
00073         }
00074 
00075         preg_match("/<question type=[\"\']([^\"\']+)[\"\']>/i", $text, $matches);
00076         $type = strtolower($matches[1]); // multichoice or multianswerchoice
00077 
00078         $questiontext = $this->unhtmlentities($this->stringbetween($text, '<text>', '</text>'));
00079         $questionhint = $this->unhtmlentities($this->stringbetween($text, '<hint>', '</hint>'));
00080         $questionaward = $this->stringbetween($text, '<award>', '</award>');
00081         $optionlist = $this->stringbetween($text, '<answer>', '</answer>');
00082 
00083         $optionlist = explode('<option', $optionlist);
00084 
00085         $n = 0;
00086 
00087         $optionscorrect = array();
00088         $optionstext = array();
00089 
00090         if ($type == 'multichoice') {
00091             foreach ($optionlist as $option) {
00092                 $correct = $this->stringbetween($option, ' correct="', '">');
00093                 $answer = $this->stringbetween($option, '">', '</option>');
00094                 $optionscorrect[$n] = $correct;
00095                 $optionstext[$n] = $this->unhtmlentities($answer);
00096                 ++$n;
00097             }
00098         } else if ($type == 'multianswerchoice') {
00099             $numcorrect = 0;
00100             $totalaward = 0;
00101 
00102             $optionsaward = array();
00103 
00104             foreach ($optionlist as $option) {
00105                 preg_match("/correct=\"([^\"]*)\"/i", $option, $correctmatch);
00106                 preg_match("/award=\"([^\"]*)\"/i", $option, $awardmatch);
00107 
00108                 $correct = $correctmatch[1];
00109                 $award = $awardmatch[1];
00110                 if ($correct == 'yes') {
00111                     $totalaward += $award;
00112                     ++$numcorrect;
00113                 }
00114 
00115                 $answer = $this->stringbetween($option, '">', '</option>');
00116 
00117                 $optionscorrect[$n] = $correct;
00118                 $optionstext[$n] = $this->unhtmlentities($answer);
00119                 $optionsaward[$n] = $award;
00120                 ++$n;
00121             }
00122 
00123         } else {
00124             echo "<p>I don't understand this question type (type = <strong>$type</strong>).</p>\n";
00125         }
00126 
00127         $question = $this->defaultquestion();
00128         $question->qtype = MULTICHOICE;
00129         $question->name = substr($questiontext, 0, 30);
00130         if (strlen($questiontext) > 30) {
00131             $question->name .= '...';
00132         }
00133 
00134         $question->questiontext = $questiontext;
00135         $question->single = ($type == 'multichoice') ? 1 : 0;
00136         $question->feedback[] = '';
00137 
00138         $question->fraction = array();
00139         $question->answer = array();
00140         for ($n = 0; $n < count($optionstext); ++$n) {
00141             if ($optionstext[$n]) {
00142                 if (!isset($numcorrect)) { // single answer
00143                     if ($optionscorrect[$n] == 'yes') {
00144                         $fraction = (int) $questionaward;
00145                     } else {
00146                         $fraction = 0;
00147                     }
00148                 } else { // mulitple answers
00149                     if ($optionscorrect[$n] == 'yes') {
00150                         $fraction = $optionsaward[$n] / $totalaward;
00151                     } else {
00152                         $fraction = -$optionsaward[$n] / count($optionstext);
00153                     }
00154                 }
00155                 $question->fraction[] = $fraction;
00156                 $question->answer[] = $optionstext[$n];
00157                 $question->feedback[] = ''; // no feedback in this type
00158             }
00159         }
00160 
00161         return $question;
00162     }
00163 
00164     function stringbetween($text, $start, $end) {
00165         $startpos = strpos($text, $start) + strlen($start);
00166         $endpos = strpos($text, $end);
00167 
00168         if ($startpos <= $endpos) {
00169             return substr($text, $startpos, $endpos - $startpos);
00170         }
00171     }
00172 
00173     function unhtmlentities($string) {
00174         $transtable = get_html_translation_table(HTML_ENTITIES);
00175         $transtable = array_flip($transtable);
00176         return strtr($string, $transtable);
00177     }
00178 
00179 }
00180 
00181 
 All Data Structures Namespaces Files Functions Variables Enumerations