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->dirroot . '/question/type/ddimageortext/questiontypebase.php');
29
 
30
/**
31
 * The drag-and-drop onto image question type class.
32
 *
33
 * @copyright  2009 The Open University
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class qtype_ddimageortext extends qtype_ddtoimage_base {
37
 
38
    protected function make_choice($dragdata) {
39
        return new qtype_ddimageortext_drag_item($dragdata->label, $dragdata->no,
40
                                        $dragdata->draggroup, $dragdata->infinite, $dragdata->id);
41
    }
42
 
43
    protected function make_place($dropzonedata) {
44
        return new qtype_ddimageortext_drop_zone($dropzonedata->label, $dropzonedata->no,
45
                                                    $dropzonedata->group,
46
                                                    $dropzonedata->xleft, $dropzonedata->ytop);
47
    }
48
 
49
    protected function make_hint($hint) {
50
        return question_hint_with_parts::load_from_record($hint);
51
    }
52
 
53
    public function save_defaults_for_new_questions(stdClass $fromform): void {
54
        parent::save_defaults_for_new_questions($fromform);
55
        $this->set_default_value('shuffleanswers', $fromform->shuffleanswers);
1441 ariadna 56
        $this->set_default_value('dropzonevisibility', $fromform->dropzonevisibility);
1 efrain 57
    }
58
 
59
    public function save_question_options($formdata) {
60
        global $DB, $USER;
61
        $context = $formdata->context;
62
 
63
        $options = $DB->get_record('qtype_ddimageortext', array('questionid' => $formdata->id));
64
        if (!$options) {
65
            $options = new stdClass();
66
            $options->questionid = $formdata->id;
67
            $options->correctfeedback = '';
68
            $options->partiallycorrectfeedback = '';
69
            $options->incorrectfeedback = '';
70
            $options->id = $DB->insert_record('qtype_ddimageortext', $options);
71
        }
72
 
73
        $options->shuffleanswers = !empty($formdata->shuffleanswers);
1441 ariadna 74
        $options->dropzonevisibility = !empty($formdata->dropzonevisibility);
1 efrain 75
        $options = $this->save_combined_feedback_helper($options, $formdata, $context, true);
76
        $this->save_hints($formdata, true);
77
        $DB->update_record('qtype_ddimageortext', $options);
78
        $DB->delete_records('qtype_ddimageortext_drops', array('questionid' => $formdata->id));
79
        foreach (array_keys($formdata->drops) as $dropno) {
80
            if ($formdata->drops[$dropno]['choice'] == 0) {
81
                continue;
82
            }
83
            $drop = new stdClass();
84
            $drop->questionid = $formdata->id;
85
            $drop->no = $dropno + 1;
86
            $drop->xleft = $formdata->drops[$dropno]['xleft'];
87
            $drop->ytop = $formdata->drops[$dropno]['ytop'];
88
            $drop->choice = $formdata->drops[$dropno]['choice'];
89
            $drop->label = $formdata->drops[$dropno]['droplabel'];
90
 
91
            $DB->insert_record('qtype_ddimageortext_drops', $drop);
92
        }
93
 
94
        // An array of drag no -> drag id.
95
        $olddragids = $DB->get_records_menu('qtype_ddimageortext_drags',
96
                                    array('questionid' => $formdata->id),
97
                                    '', 'no, id');
98
        foreach (array_keys($formdata->drags) as $dragno) {
99
            $info = file_get_draft_area_info($formdata->dragitem[$dragno]);
100
            if ($info['filecount'] > 0 || (trim($formdata->draglabel[$dragno]) != '')) {
101
                $draftitemid = $formdata->dragitem[$dragno];
102
 
103
                $drag = new stdClass();
104
                $drag->questionid = $formdata->id;
105
                $drag->no = $dragno + 1;
106
                $drag->draggroup = $formdata->drags[$dragno]['draggroup'];
107
                $drag->infinite = empty($formdata->drags[$dragno]['infinite']) ? 0 : 1;
108
                $drag->label = $formdata->draglabel[$dragno];
109
 
110
                if (isset($olddragids[$dragno + 1])) {
111
                    $drag->id = $olddragids[$dragno + 1];
112
                    unset($olddragids[$dragno + 1]);
113
                    $DB->update_record('qtype_ddimageortext_drags', $drag);
114
                } else {
115
                    $drag->id = $DB->insert_record('qtype_ddimageortext_drags', $drag);
116
                }
117
 
118
                if ($formdata->drags[$dragno]['dragitemtype'] == 'image') {
119
                    file_save_draft_area_files($draftitemid, $formdata->context->id,
120
                                        'qtype_ddimageortext', 'dragimage', $drag->id,
121
                                        array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1));
122
                } else {
123
                    // Delete any existing files for draggable text item type.
124
                    $fs = get_file_storage();
125
                    $fs->delete_area_files($formdata->context->id, 'qtype_ddimageortext',
126
                                                                'dragimage', $drag->id);
127
                }
128
 
129
            }
130
 
131
        }
132
 
133
        if (!empty($olddragids)) {
134
            list($sql, $params) = $DB->get_in_or_equal(array_values($olddragids));
135
            $DB->delete_records_select('qtype_ddimageortext_drags', "id $sql", $params);
136
        }
137
        file_save_draft_area_files($formdata->bgimage, $formdata->context->id,
138
                                    'qtype_ddimageortext', 'bgimage', $formdata->id,
139
                                    array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1));
140
    }
141
    public function move_files($questionid, $oldcontextid, $newcontextid) {
142
        global $DB;
143
        $fs = get_file_storage();
144
 
145
        parent::move_files($questionid, $oldcontextid, $newcontextid);
146
        $fs->move_area_files_to_new_context($oldcontextid,
147
                                    $newcontextid, 'qtype_ddimageortext', 'bgimage', $questionid);
148
        $dragids = $DB->get_records_menu('qtype_ddimageortext_drags',
149
                                                array('questionid' => $questionid), 'id', 'id,1');
150
        foreach ($dragids as $dragid => $notused) {
151
            $fs->move_area_files_to_new_context($oldcontextid,
152
                                    $newcontextid, 'qtype_ddimageortext', 'dragimage', $dragid);
153
        }
154
 
155
        $this->move_files_in_combined_feedback($questionid, $oldcontextid, $newcontextid);
156
        $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
157
    }
158
 
159
    /**
160
     * Delete all the files belonging to this question.
161
     * @param int $questionid the question being deleted.
162
     * @param int $contextid the context the question is in.
163
     */
164
 
165
    protected function delete_files($questionid, $contextid) {
166
        global $DB;
167
        $fs = get_file_storage();
168
 
169
        parent::delete_files($questionid, $contextid);
170
 
171
        $dragids = $DB->get_records_menu('qtype_ddimageortext_drags',
172
                                                array('questionid' => $questionid), 'id', 'id,1');
173
        foreach ($dragids as $dragid => $notused) {
174
            $fs->delete_area_files($contextid, 'qtype_ddimageortext', 'dragimage', $dragid);
175
        }
176
 
177
        $this->delete_files_in_combined_feedback($questionid, $contextid);
178
        $this->delete_files_in_hints($questionid, $contextid);
179
    }
180
 
181
 
182
    public function export_to_xml($question, qformat_xml $format, $extra = null) {
183
        $fs = get_file_storage();
184
        $contextid = $question->contextid;
185
        $output = '';
186
 
187
        if ($question->options->shuffleanswers) {
188
            $output .= "    <shuffleanswers/>\n";
189
        }
1441 ariadna 190
        if ($question->options->dropzonevisibility) {
191
            $output .= "    <dropzonevisibility/>\n";
192
        }
1 efrain 193
        $output .= $format->write_combined_feedback($question->options,
194
                                                    $question->id,
195
                                                    $question->contextid);
196
        $files = $fs->get_area_files($contextid, 'qtype_ddimageortext', 'bgimage', $question->id);
197
        $output .= "    ".$this->write_files($files, 2)."\n";;
198
 
199
        foreach ($question->options->drags as $drag) {
200
            $files =
201
                    $fs->get_area_files($contextid, 'qtype_ddimageortext', 'dragimage', $drag->id);
202
            $output .= "    <drag>\n";
203
            $output .= "      <no>{$drag->no}</no>\n";
204
            $output .= $format->writetext($drag->label, 3)."\n";
205
            $output .= "      <draggroup>{$drag->draggroup}</draggroup>\n";
206
            if ($drag->infinite) {
207
                $output .= "      <infinite/>\n";
208
            }
209
            $output .= $this->write_files($files, 3);
210
            $output .= "    </drag>\n";
211
        }
212
        foreach ($question->options->drops as $drop) {
213
            $output .= "    <drop>\n";
214
            $output .= $format->writetext($drop->label, 3);
215
            $output .= "      <no>{$drop->no}</no>\n";
216
            $output .= "      <choice>{$drop->choice}</choice>\n";
217
            $output .= "      <xleft>{$drop->xleft}</xleft>\n";
218
            $output .= "      <ytop>{$drop->ytop}</ytop>\n";
219
            $output .= "    </drop>\n";
220
        }
221
 
222
        return $output;
223
    }
224
 
225
    public function import_from_xml($data, $question, qformat_xml $format, $extra=null) {
226
        if (!isset($data['@']['type']) || $data['@']['type'] != 'ddimageortext') {
227
            return false;
228
        }
229
 
230
        $question = $format->import_headers($data);
231
        $question->qtype = 'ddimageortext';
232
 
233
        $question->shuffleanswers = array_key_exists('shuffleanswers',
234
                                                    $format->getpath($data, array('#'), array()));
1441 ariadna 235
        $question->dropzonevisibility = array_key_exists('dropzonevisibility',
236
            $format->getpath($data, ['#'], []));
1 efrain 237
 
238
        $filexml = $format->getpath($data, array('#', 'file'), array());
239
        $question->bgimage = $format->import_files_as_draft($filexml);
240
        $drags = $data['#']['drag'];
241
        $question->drags = array();
242
 
243
        foreach ($drags as $dragxml) {
244
            $dragno = $format->getpath($dragxml, array('#', 'no', 0, '#'), 0);
245
            $dragindex = $dragno - 1;
246
            $question->drags[$dragindex] = array();
247
            $question->draglabel[$dragindex] =
248
                        $format->getpath($dragxml, array('#', 'text', 0, '#'), '', true);
249
            $question->drags[$dragindex]['infinite'] = array_key_exists('infinite', $dragxml['#']);
250
            $question->drags[$dragindex]['draggroup'] =
251
                        $format->getpath($dragxml, array('#', 'draggroup', 0, '#'), 1);
252
            $filexml = $format->getpath($dragxml, array('#', 'file'), array());
253
            $question->dragitem[$dragindex] = $format->import_files_as_draft($filexml);
254
            if (count($filexml)) {
255
                $question->drags[$dragindex]['dragitemtype'] = 'image';
256
            } else {
257
                $question->drags[$dragindex]['dragitemtype'] = 'word';
258
            }
259
        }
260
 
261
        $drops = $data['#']['drop'];
262
        $question->drops = array();
263
        foreach ($drops as $dropxml) {
264
            $dropno = $format->getpath($dropxml, array('#', 'no', 0, '#'), 0);
265
            $dropindex = $dropno - 1;
266
            $question->drops[$dropindex] = array();
267
            $question->drops[$dropindex]['choice'] =
268
                        $format->getpath($dropxml, array('#', 'choice', 0, '#'), 0);
269
            $question->drops[$dropindex]['droplabel'] =
270
                        $format->getpath($dropxml, array('#', 'text', 0, '#'), '', true);
271
            $question->drops[$dropindex]['xleft'] =
272
                        $format->getpath($dropxml, array('#', 'xleft', 0, '#'), '');
273
            $question->drops[$dropindex]['ytop'] =
274
                        $format->getpath($dropxml, array('#', 'ytop', 0, '#'), '');
275
        }
276
 
277
        $format->import_combined_feedback($question, $data, true);
278
        $format->import_hints($question, $data, true, false,
279
                $format->get_format($question->questiontextformat));
280
 
281
        return $question;
282
    }
283
 
284
}