File: format.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (147 lines) | stat: -rwxr-xr-x 5,231 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php    // $Id: format.php,v 1.2 2006/03/01 07:36:08 gustav_delius Exp $ 
// Alton College, Hampshire, UK - Tom Flannaghan, Andrew Walker
// Imports learnwise multiple choice quizzes (single and multiple answers)
// currently ignores the deduct attribute for multiple answer questions
// deductions are currently simply found by dividing the award for the incorrect 
// answer by the total number of options
// Based on format.php, included by ../../import.php

class qformat_learnwise extends qformat_default {

    function provide_import() {
      return true;
    }

    function readquestions($lines) {
        $questions = array();
        $currentquestion = array();
		
        foreach($lines as $line) {
            $line = trim($line);
            $currentquestion[] = $line;
			
            if ($question = $this->readquestion($currentquestion)) {
                $questions[] = $question;
                $currentquestion = array();
            }
        }				
        return $questions;
    }

    function readquestion($lines) {
        $text = implode(' ', $lines);
        $text = str_replace(array('\t','\n','\r','\''), array('','','','\\\''), $text);

        $startpos = strpos($text, '<question type');
        $endpos = strpos($text, '</question>');
        if ($startpos === false || $endpos === false) {
            return false;
        }

        preg_match("/<question type=[\"\']([^\"\']+)[\"\']>/i", $text, $matches);
        $type = strtolower($matches[1]); // multichoice or multianswerchoice

        $questiontext = $this->unhtmlentities($this->stringbetween($text, '<text>', '</text>'));
        $questionhint = $this->unhtmlentities($this->stringbetween($text, '<hint>', '</hint>'));
        $questionaward = $this->stringbetween($text, '<award>', '</award>');
        $optionlist = $this->stringbetween($text, '<answer>', '</answer>');

        $optionlist = explode('<option', $optionlist);

        $n = 0;

        $optionscorrect = array();
        $optionstext = array();

        if ($type == 'multichoice') {
            foreach ($optionlist as $option) {
                $correct = $this->stringbetween($option, ' correct="', '">');
                $answer = $this->stringbetween($option, '">', '</option>');
                $optionscorrect[$n] = $correct; 
                $optionstext[$n] = $this->unhtmlentities($answer);
                ++$n;
            }
        } else if ($type == 'multianswerchoice') {
            $numcorrect = 0;
            $totalaward = 0;

            $optionsaward = array();

            foreach ($optionlist as $option) {
                preg_match("/correct=\"([^\"]*)\"/i", $option, $correctmatch);
                preg_match("/award=\"([^\"]*)\"/i", $option, $awardmatch);

                $correct = $correctmatch[1];
                $award = $awardmatch[1];
                if ($correct == 'yes') {
                    $totalaward += $award;
                    ++$numcorrect;
                }

                $answer = $this->stringbetween($option, '">', '</option>');

                $optionscorrect[$n] = $correct; 
                $optionstext[$n] = $this->unhtmlentities($answer);
                $optionsaward[$n] = $award;
                ++$n;
            }

        } else {
            echo "<p>I don't understand this question type (type = <strong>$type</strong>).</p>\n";
        }

        $question = $this->defaultquestion();
        $question->qtype = MULTICHOICE;
        $question->name = substr($questiontext, 0, 30);
        if (strlen($questiontext) > 30) {
            $question->name .= '...';
        }

        $question->questiontext = $questiontext;
        $question->single = ($type == 'multichoice') ? 1 : 0;
        $question->feedback[] = '';
			
        $question->fraction = array();
        $question->answer = array();
        for ($n = 0; $n < count($optionstext); ++$n) {
            if ($optionstext[$n]) {
                if (!isset($numcorrect)) { // single answer
                    if ($optionscorrect[$n] == 'yes') {
                        $fraction = (int) $questionaward; 
                    } else {
                        $fraction = 0;
                    } 
                } else { // mulitple answers
                    if ($optionscorrect[$n] == 'yes') {
                        $fraction = $optionsaward[$n] / $totalaward; 
                    } else {
                        $fraction = -$optionsaward[$n] / count($optionstext);
                    }
                }
                $question->fraction[] = $fraction;
                $question->answer[] = $optionstext[$n];
                $question->feedback[] = ''; // no feedback in this type
            }
        }

        return $question;
    }

    function stringbetween($text, $start, $end) {
        $startpos = strpos($text, $start) + strlen($start);
        $endpos = strpos($text, $end);
	
        if ($startpos <= $endpos) {
            return substr($text, $startpos, $endpos - $startpos);
        }
    }

    function unhtmlentities($string) {
        $transtable = get_html_translation_table(HTML_ENTITIES);
        $transtable = array_flip($transtable);
        return strtr($string, $transtable);
    } 

}

?>