File: questiontype.php

package info (click to toggle)
moodle 1.4.4.dfsg.1-3sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 57,876 kB
  • ctags: 29,496
  • sloc: php: 271,617; sql: 5,084; xml: 702; perl: 638; sh: 403; java: 283; makefile: 42; pascal: 21
file content (247 lines) | stat: -rw-r--r-- 10,358 bytes parent folder | download
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?PHP  // $Id: questiontype.php,v 1.3 2004/07/30 14:43:08 kaipe Exp $

/////////////////
/// NUMERICAL ///
/////////////////

/// QUESTION TYPE CLASS //////////////////

///
/// This class contains some special features in order to make the
/// question type embeddable within a multianswer (cloze) question
///

/// This question type behaves like shortanswer in most cases.
/// Therefore, it extends the shortanswer question type...

require_once("$CFG->dirroot/mod/quiz/questiontypes/shortanswer/questiontype.php");

class quiz_numerical_qtype extends quiz_shortanswer_qtype {

    function get_answers($question, $addedcondition='') {
        // The added condition is one addition that has been added
        // to the behaviour of this question type in order to make
        // it embeddable within a multianswer (embedded cloze) question

        global $CFG;

        // There can be multiple answers
        return get_records_sql("SELECT a.*, n.min, n.max
                                  FROM {$CFG->prefix}quiz_numerical n,
                                       {$CFG->prefix}quiz_answers a
                                 WHERE a.question = '$question->id'
                                   AND n.answer = a.id "
                                     . $addedcondition);
    }

    function name() {
        return 'numerical';
    }

    function save_question_options($question) {

        if (!$oldanswers = get_records("quiz_answers", "question", $question->id, "id ASC")) {
            $oldanswers = array();
        }

        $answers = array();
        $maxfraction = -1;

        // Insert all the new answers
        foreach ($question->answer as $key => $dataanswer) {
            if ($dataanswer != "") {
                if ($oldanswer = array_shift($oldanswers)) {  // Existing answer, so reuse it
                    $answer = $oldanswer;
                    $answer->answer   = $dataanswer;
                    $answer->fraction = $question->fraction[$key];
                    $answer->feedback = $question->feedback[$key];
                    if (!update_record("quiz_answers", $answer)) {
                        $result->error = "Could not update quiz answer! (id=$answer->id)";
                        return $result;
                    }
                } else {    // This is a completely new answer
                    unset($answer);
                    $answer->answer   = $dataanswer;
                    $answer->question = $question->id;
                    $answer->fraction = $question->fraction[$key];
                    $answer->feedback = $question->feedback[$key];
                    if (!$answer->id = insert_record("quiz_answers", $answer)) {
                        $result->error = "Could not insert quiz answer!";
                        return $result;
                    }
                }
                $answers[] = $answer->id;
                if ($question->fraction[$key] > $maxfraction) {
                    $maxfraction = $question->fraction[$key];
                }

                if ($options = get_record("quiz_numerical", "answer", $answer->id)) {
                    $options->min= $question->min[$key];
                    $options->max= $question->max[$key];
                    if (!update_record("quiz_numerical", $options)) {
                        $result->error = "Could not update quiz numerical options! (id=$options->id)";
                        return $result;
                    }
                } else { // completely new answer
                    unset($options);
                    $options->question = $question->id;
                    $options->answer = $answer->id;
                    $options->min = $question->min[$key];
                    $options->max = $question->max[$key];
                    if (!insert_record("quiz_numerical", $options)) {
                        $result->error = "Could not insert quiz numerical options!";
                        return $result;
                    }
                }
            }
        }

        /// Save units
        /// Make unit records
        $newunits = array();
        unset($tmpunit);
        $tmpunit->question = $question->id;
        foreach ($question->multiplier as $key => $multiplier) {
            if ($multiplier && is_numeric($multiplier)) {
                $tmpunit->multiplier = $multiplier;
                $tmpunit->unit = trim($question->unit[$key]);
                $newunits[] = $tmpunit;
            }
        }
        if (1 == count($newunits) && !$newunits[0]->unit) {
            /// Only default unit and it is empty, so drop it:
            $newunits = array();
        }
        if ($oldunits = get_records('quiz_numerical_units',
                                    'question', $question->id)) {
            foreach ($oldunits as $unit) {
                if ($newunit = array_shift($newunits)) {
                    $unit->multiplier = $newunit->multiplier;
                    $unit->unit = $newunit->unit;
                    if (!update_record('quiz_numerical_units', $unit)) {
                        $result->error = "Could not update quiz_numerical_unit $unit->unit";
                        return $result;
                    }
                } else {
                    delete_records('quiz_numerical_units', 'id', $unit->id);
                }
            }
        }
        foreach ($newunits as $newunit) {
            // Create new records for the remaining units:
            if (!insert_record('quiz_numerical_units', $newunit)) {
                $result->error = "Unable to insert new unit $newunit->unit";
                return $result;
            }
        }

        /// Perform sanity checks on fractional grades
        if ($maxfraction != 1) {
            $maxfraction = $maxfraction * 100;
            $result->noticeyesno = get_string("fractionsnomax", "quiz", $maxfraction);
            return $result;
        } else {
            return true;
        }
    }
    
    function grade_response($question, $nameprefix, $addedanswercondition='') {

        $result->answers = array();
        $units = get_records('quiz_numerical_units',
                             'question', $question->id);
        if (isset($question->response[$nameprefix])) {
            $response = trim(stripslashes($question->response[$nameprefix]));
            // Arrays with 'wild cards':
            $search = array(' ',',');
            $replace = array('','.');
            $responsenum = str_replace($search, $replace, $response);
            if (empty($units)) {
                if ('' !== $responsenum && is_numeric($responsenum)) {
                    $responsenum = (float)$responsenum;
                } else {
                    unset($responsenum); // Answer is not numeric
                }
            } else if (ereg(
                    '^(([0-9]+(\\.[0-9]*)?|[.][0-9]+)([eE][-+]?[0-9]+)?)([^0-9].*)?$',
                    $responsenum, $responseparts)) {
                $responsenum = (float)$responseparts[1];
                if ($responseparts[5]) {
                    $responseunit = $responseparts[5];
                } else {
                    $responseunit = '';
                }
                $unitidentified = false;
                foreach ($units as $unit) {
                    if (str_replace($search, $replace, $unit->unit)
                            == $responseunit) {
                        $unitidentified = true;
                        $responsenum /= $unit->multiplier;
                        break;
                    }
                }
                if (!$unitidentified) {
                    unset($responsenum); // No unit OK
                }
            } else {
                unset($responsenum); // Answer is not numeric
            }
       } else {
            $response = '';
        }
        $answers = $this->get_answers($question, $addedanswercondition);
        foreach ($answers as $answer) {

            /// Check if response matches answer...
            if ('' !== $response and empty($result->answers)
                    || $answer->fraction
                     > $result->answers[$nameprefix]->fraction
                    and strtolower($response) == strtolower($answer->answer)
                    || '' != trim($answer->min) && isset($responsenum)
                    && $responsenum >= (float)$answer->min
                    && $responsenum <= (float)$answer->max) {
                $result->answers[$nameprefix] = $answer;
            }
        }

        $result->grade = isset($result->answers[$nameprefix])
                ?   $result->answers[$nameprefix]->fraction
                :   0.0;
        $result->correctanswers = quiz_extract_correctanswers($answers,
                                                              $nameprefix);

        /////////////////////////////////////////////////
        // For numerical answer we have the policy to 
        // set feedback for any response, even if the
        // response does not entitle the student to it.
        /////////////////////////////////////////////////
        if ('' !== $response and empty($result->answers)
                || empty($result->answers[$nameprefix]->feedback)) {
            // Look for just any feedback:
            foreach ($result->correctanswers as $correctanswer) {
                if ($correctanswer->feedback) {
                    $result->answers[$nameprefix]->feedback = 
                            $correctanswer->feedback;
                    if (empty($result->answers[$nameprefix]->id)) {
                        // Better fake an answer as well:
                        $result->answers[$nameprefix]->id = 0;
                        $result->answers[$nameprefix]->answer = $response;
                        $result->answers[$nameprefix]->fraction = 0.0;
                        $result->answers[$nameprefix]->question = $question->id;
                    }
                    break;
                }
            }
        }

        return $result;
    }
}
//// END OF CLASS ////

//////////////////////////////////////////////////////////////////////////
//// INITIATION - Without this line the question type is not in use... ///
//////////////////////////////////////////////////////////////////////////
$QUIZ_QTYPES[NUMERICAL]= new quiz_numerical_qtype();

?>