Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Question type class for the drag-and-drop onto image question type.
19
 *
20
 * @package    qtype_ddimageortext
21
 * @copyright  2009 The Open University
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
require_once($CFG->libdir . '/questionlib.php');
29
require_once($CFG->dirroot . '/question/engine/lib.php');
30
require_once($CFG->dirroot . '/question/format/xml/format.php');
31
require_once($CFG->dirroot . '/question/type/gapselect/questiontypebase.php');
32
 
33
/**
34
 * The drag-and-drop onto image question type class.
35
 *
36
 * @copyright  2009 The Open University
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class qtype_ddtoimage_base extends question_type {
40
    /**
41
     * Returns the choice group key.
42
     *
43
     * @return string
44
     */
45
    protected function choice_group_key() {
46
        return 'draggroup';
47
    }
48
 
49
    public function get_question_options($question) {
50
        global $DB;
51
        $dbprefix = 'qtype_'.$this->name();
52
        $question->options = $DB->get_record($dbprefix,
53
                array('questionid' => $question->id), '*', MUST_EXIST);
54
        $question->options->drags = $DB->get_records($dbprefix.'_drags',
55
                array('questionid' => $question->id), 'no ASC', '*');
56
        $question->options->drops = $DB->get_records($dbprefix.'_drops',
57
                array('questionid' => $question->id), 'no ASC', '*');
58
        parent::get_question_options($question);
59
    }
60
 
61
    protected function initialise_question_instance(question_definition $question, $questiondata) {
62
        parent::initialise_question_instance($question, $questiondata);
63
        $question->shufflechoices = $questiondata->options->shuffleanswers;
1441 ariadna 64
        if (isset($questiondata->options->dropzonevisibility)) {
65
            $question->dropzonevisibility = $questiondata->options->dropzonevisibility;
66
        }
1 efrain 67
 
68
        $this->initialise_combined_feedback($question, $questiondata, true);
69
 
70
        $question->choices = array();
71
        $choiceindexmap = array();
72
 
73
        // Store the choices in arrays by group.
74
        // This code is weird. The first choice in each group gets key 1 in the
75
        // $question->choices[$choice->choice_group()] array, and the others get
76
        // key $choice->no. Therefore you need to think carefully whether you
77
        // are using the key, or $choice->no. This is presumably a mistake, but
78
        // one that is now essentially un-fixable, since many questions of this
79
        // type have been attempted, and theys keys get stored in the attempt data.
80
        foreach ($questiondata->options->drags as $dragdata) {
81
 
82
            $choice = $this->make_choice($dragdata);
83
 
84
            if (array_key_exists($choice->choice_group(), $question->choices)) {
85
                $question->choices[$choice->choice_group()][$dragdata->no] = $choice;
86
            } else {
87
                $question->choices[$choice->choice_group()][1] = $choice;
88
            }
89
 
90
            end($question->choices[$choice->choice_group()]);
91
            $choiceindexmap[$dragdata->no] = array($choice->choice_group(),
92
                    key($question->choices[$choice->choice_group()]));
93
        }
94
 
95
        $question->places = array();
96
        $question->rightchoices = array();
97
 
98
        $i = 1;
99
 
100
        foreach ($questiondata->options->drops as $dropdata) {
101
            list($group, $choiceindex) = $choiceindexmap[$dropdata->choice];
102
            $dropdata->group = $group;
103
            $question->places[$dropdata->no] = $this->make_place($dropdata);
104
            $question->rightchoices[$dropdata->no] = $choiceindex;
105
        }
106
    }
107
 
108
    /**
109
     * Convert files into text output in the given format.
110
     * This method is copied from qformat_default as a quick fix, as the method there is
111
     * protected.
112
     * @param array $files
113
     * @param int $indent Number of spaces to indent
114
     * @return string $string
115
     */
116
    public function write_files($files, $indent) {
117
        if (empty($files)) {
118
            return '';
119
        }
120
        $string = '';
121
        foreach ($files as $file) {
122
            if ($file->is_directory()) {
123
                continue;
124
            }
125
            $string .= str_repeat('  ', $indent);
126
            $string .= '<file name="' . $file->get_filename() . '" encoding="base64">';
127
            $string .= base64_encode($file->get_content());
128
            $string .= "</file>\n";
129
        }
130
        return $string;
131
    }
132
 
133
    public function get_possible_responses($questiondata) {
134
        $question = $this->make_question($questiondata);
135
 
136
        $parts = array();
137
        foreach ($question->places as $placeno => $place) {
138
            $choices = array();
139
 
140
            foreach ($question->choices[$place->group] as $i => $choice) {
141
                $correct = $question->rightchoices[$placeno] == $i;
142
                $choices[$choice->no] = new question_possible_response($choice->summarise(), $correct ? 1 : 0);
143
            }
144
            $choices[null] = question_possible_response::no_response();
145
 
146
            $parts[$placeno] = $choices;
147
        }
148
 
149
        return $parts;
150
    }
151
 
152
    public function get_random_guess_score($questiondata) {
153
        $question = $this->make_question($questiondata);
154
        return $question->get_random_guess_score();
155
    }
156
    public function delete_question($questionid, $contextid) {
157
        global $DB;
158
        $DB->delete_records('qtype_'.$this->name(), array('questionid' => $questionid));
159
        $DB->delete_records('qtype_'.$this->name().'_drags', array('questionid' => $questionid));
160
        $DB->delete_records('qtype_'.$this->name().'_drops', array('questionid' => $questionid));
161
        return parent::delete_question($questionid, $contextid);
162
    }
163
}