File: editquestion.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 (198 lines) | stat: -rw-r--r-- 7,128 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
<?PHP // $Id: editquestion.php,v 1.1 2004/07/30 14:50:57 kaipe Exp $

// Get a handle to the question type we are dealing with here
$qtypeobj = $QUIZ_QTYPES[CALCULATED];
if (isset($form->editdatasets) && $form->editdatasets) {
    require("$CFG->dirroot/mod/quiz/questiontypes/datasetdependent/datasetitems.php");
    exit();
}

$calculatedmessages = array();
if ($form) {

    // Verify the quality of the question properties
    if (empty($question->name)) {
        $calculatedmessages[] = get_string('missingname', 'quiz');
    }
    if (empty($question->questiontext)) {
        $calculatedmessages[] = get_string('missingquestiontext', 'quiz');
    }

    // Formula stuff (verify some of them)
    $answers[0]->answer = trim(array_shift($form->answer))
    and false===($formulaerrors =
            quiz_qtype_calculated_find_formula_errors($answers[0]->answer))
    or $answers[0]->answer
    and $calculatedmessages[] = $formulaerrors
    or $calculatedmesages[] = get_string('missingformula', 'quiz');
    
    $answers[0]->tolerance = array_shift($form->tolerance)
    or $answers[0]->tolerance = 0.0;
    is_numeric($answers[0]->tolerance)
    or $calculatedmessages[] = get_string('tolerancemustbenumeric', 'quiz');

    $answers[0]->feedback = array_shift($form->feedback);
    
    // Let's trust the drop down menus.

    $answers[0]->tolerancetype = array_shift($form->tolerancetype);
    $answers[0]->correctanswerlength = array_shift($form->correctanswerlength);
    $answers[0]->fraction = array_shift($form->fraction);

    // Fill with remaining answers, in case calculated.html
    // supports multiple formulas.
    $i = 1;
    foreach ($form->answer as $key => $answer) {
        if (trim($answer)) {
            $answers[$i]->answer = trim($answer);
            $answers[$i]->tolerance = $form->tolerance[$key]
            or $answers[$i]->tolerance = 0.0;
            $answers[$i]->tolerancetype = $form->tolerancetype[$key];
            $answers[$i]->correctanswerlength =
                    $form->correctanswerlength[$key];

            $answers[$i]->fraction = $form->fraction[$key];
            $answers[$i]->feedback = $form->feedback[$key];

            // Check for errors:
            false === ($formulaerrors =
                    quiz_qtype_calculated_find_formula_errors($answer))
            or $calculatedmessages[] = $formulaerrors;
            is_numeric($answers[$i]->tolerance)
            or $calculatedmessages[] = get_string('tolerancemustbenumeric',
                                                  'quiz');
            // Increase answer count
            ++$i;
        }
    }

    // Finally the units:

    // Start with the default units...
    $units[0]->unit = array_shift($form->unit);
    array_shift($form->multiplier); // In case it is not 1.0
    $units[0]->multiplier = 1.0; // Must!
    
    // Accept other units if they have legal multipliers
    $i = 1;
    foreach ($form->multiplier as $key => $multiplier) {
        if ($multiplier && is_numeric($multiplier)) {
            $units[$i]->multiplier = $multiplier;
            $units[$i]->unit = $form->unit[$key];
            ++$i;
        }
    }


    if (empty($calculatedmessages)) {
    // First page calculated.html passed all right!

        if (!empty($form->dataset)) {
            // Dataset definitions have been set
            // Save question!
            $subtypeoptions->answers = $answers;
            $subtypeoptions->units = $units;
            $question = $qtypeobj->save_question
                    ($question, $form, $course, $subtypeoptions);
            require("$CFG->dirroot/mod/quiz/questiontypes/datasetdependent/datasetitems.php");
            exit();
        } else {
            $datasetmessage = '';
        }
        
        // Now continue by preparing for the second page questiondatasets.html
        
        $possibledatasets = $qtypeobj->find_dataset_names(
                $question->questiontext);
    
        $mandatorydatasets = array();
        foreach ($answers as $answer) {
            $mandatorydatasets += $qtypeobj
                    ->find_dataset_names($answer->answer);
        }
        
        $datasets = $qtypeobj->construct_dataset_menus(
                $question, $mandatorydatasets, $possibledatasets);
        print_heading_with_help(get_string("choosedatasetproperties", "quiz"), "questiondatasets", "quiz");
        require("$CFG->dirroot/mod/quiz/questiontypes/datasetdependent/questiondatasets.html");
        exit();
    }
    
} else {
// First page in question wizard - calculated.html!

    // The layout of the editing page will only support
    // one formula alternative for calculated questions.
    // However, the code behind supports up to six formulas
    // and the database store and attempt/review framework 
    // does not have any limit.
    if (!empty($question->id)) {
        $answersraw= $qtypeobj->get_answers($question);
    }
    $answers= array();
    for ($i=0; $i<6; $i++) {
        // Make answer slots with default values
        $answers[$i]->answer              = "";
        $answers[$i]->feedback            = "";
        $answers[$i]->fraction            = "1.0";
        $answers[$i]->tolerance           = "0.01";
        $answers[$i]->tolerancetype       = "1";
        $answers[$i]->correctanswerlength = "2";
    }
    if (!empty($answersraw)) {
        $i=0;
        foreach ($answersraw as $answer) {
            $answers[$i] = $answer;
            $i++;
        }
    }

    // Units are handled the same way
    // as for numerical questions
    $units = array();
    for ($i=0 ; $i<6 ; $i++) {
        // Make unit slots, default as blank...
        $units[$i]->multiplier = '';
        $units[$i]->unit = '';
    }
    if (!empty($question->id) and $unitsraw = get_records(
            'quiz_numerical_units', 'question', $question->id)) {
        /// Find default unit and have it put in the zero slot
        /// This procedure might be overridden later when
        /// the unit is stripped form an answer...
        foreach ($unitsraw as $key => $unit) {
            if (1.0 == $unit->multiplier) {
                /// Default unit found:
                $units[0] = $unit;
                unset($unitsraw[$key]);
                break;
            }
        }
        /// Fill remaining answer slots with whatsever left
        if (!empty($unitsraw)) {
            $i = 1; // The zero slot got the default unit...
            foreach ($unitsraw as $unit) {
                $units[$i] = $unit;
                $i++;
            }
        }
    } else {
        $units[0]->multiplier = 1.0;
    }

    // Strip trailing zeros from multipliers
    foreach ($units as $i => $unit) {
        if (ereg('^(.*\\..(.*[^0])?)0+$', $unit->multiplier, $regs1)) {
            if (ereg('^(.+)\\.0$', $regs1[1], $regs2)) {
                $units[$i]->multiplier = $regs2[1];
            } else {
                $units[$i]->multiplier = $regs1[1];
            }
        }
    }
}

print_heading_with_help(get_string("editingcalculated", "quiz"), "calculated", "quiz");
require("calculated.html");

?>