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 (154 lines) | stat: -rw-r--r-- 5,658 bytes parent folder | download | duplicates (3)
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
148
149
150
151
152
153
154
<?php  // $Id: format.php,v 1.2 2006/03/01 07:36:08 gustav_delius Exp $ 
/// Modified by Tom Robb 12 June 2003 to include percentage and comment insertion
/// facility.

////////////////////////////////////////////////////////////////////////////
/// MISSING WORD FORMAT
///
/// This Moodle class provides all functions necessary to import and export 
/// one-correct-answer multiple choice questions in this format:
///
///    As soon as we begin to explore our body parts as infants
///    we become students of {=anatomy and physiology ~reflexology 
///    ~science ~experiment}, and in a sense we remain students for life.
/// 
/// Each answer is separated with a tilde ~, and the correct answer is 
/// prefixed with an equals sign =
///
/// Percentage weights can be included by following the tilde with the
/// desired percent.  Comments can be included for each choice by following
/// the comment with a hash mark ("#") and the comment.  Example:
///
///    This is {=the best answer#comment on the best answer ~75%a good
///    answer#comment on the good answer ~a wrong one#comment on the bad answer}
///
////////////////////////////////////////////////////////////////////////////

// Based on format.php, included by ../../import.php

class qformat_missingword extends qformat_default {

    function provide_import() {
      return true;
    }

    function readquestion($lines) {
    /// Given an array of lines known to define a question in 
    /// this format, this function converts it into a question 
    /// object suitable for processing and insertion into Moodle.

        $question = $this->defaultquestion();
        ///$comment added by T Robb
        $comment = NULL;
        $text = implode(" ", $lines);

        /// Find answer section

        $answerstart = strpos($text, "{");
        if ($answerstart === false) {
            if ($this->displayerrors) {
                echo "<p>$text<p>Could not find a {";
            }
            return false;
        }

        $answerfinish = strpos($text, "}");
        if ($answerfinish === false) {
            if ($this->displayerrors) {
                echo "<p>$text<p>Could not find a }";
            }
            return false;
        }

        $answerlength = $answerfinish - $answerstart;
        $answertext = substr($text, $answerstart + 1, $answerlength - 1);

        /// Save the new question text
        $question->questiontext = addslashes(substr_replace($text, "_____", $answerstart, $answerlength+1));
        $question->name = $question->questiontext;


        /// Parse the answers
        $answertext = str_replace("=", "~=", $answertext);
        $answers = explode("~", $answertext);
        if (isset($answers[0])) {
            $answers[0] = trim($answers[0]);
        }
        if (empty($answers[0])) {
            array_shift($answers);
        }

        $countanswers = count($answers);

        switch ($countanswers) {
            case 0:  // invalid question
                if ($this->displayerrors) {
                    echo "<p>No answers found in $answertext";
                }
                return false;

            case 1:
                $question->qtype = SHORTANSWER;

                $answer = trim($answers[0]);
                if ($answer[0] == "=") {
                    $answer = substr($answer, 1);
                }
                $question->answer[]   = addslashes($answer);
                $question->fraction[] = 1;
                $question->feedback[] = "";
    
                return $question;

            default:
                $question->qtype = MULTICHOICE;

                foreach ($answers as $key => $answer) {
                    $answer = trim($answer);

                    // Tom's addition starts here
                    $answeight = 0;
                    if (strspn($answer,"1234567890%") > 0){
                        //Make sure that the percent sign is the last in the span
                        if (strpos($answer,"%") == strspn($answer,"1234567890%") - 1) {
                            $answeight0 = substr($answer,0,strspn($answer,"1234567890%"));
                            $answeight = round(($answeight0/100),2);
                            $answer = substr($answer,(strspn($answer,"1234567890%")));
                        }
                    } 
                    if ($answer[0] == "="){
                        $answeight = 1;
                    }
                    //remove the protective underscore for leading numbers in answers
                    if ($answer[0] == "_"){
                        $answer = substr($answer, 1);
                    }
                    $answer = trim($answer);

                    if (strpos($answer,"#") > 0){
                        $hashpos = strpos($answer,"#");
                        $comment = addslashes(substr(($answer),$hashpos+1));
                        $answer  = substr($answer,0,$hashpos);
                    } else {
                        $comment = " ";
                    }
                    // End of Tom's addition

                    if ($answer[0] == "=") {
#                       $question->fraction[$key] = 1;
                        $question->fraction[$key] = $answeight;
                        $answer = substr($answer, 1);
                    } else {
#                       $question->fraction[$key] = 0;
                        $question->fraction[$key] = $answeight;
                    }
                    $question->answer[$key]   = addslashes($answer);
                    $question->feedback[$key] = $comment;
                }
    
                return $question;
        }
    }
}

?>