Merge branch 'MDL-68722-310' of https://github.com/matt-catalyst/moodle into MOODLE_3...
[moodle.git] / question / type / truefalse / edit_truefalse_form.php
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/>.
17 /**
18  * Defines the editing form for the true-false question type.
19  *
20  * @package    qtype
21  * @subpackage truefalse
22  * @copyright  2007 Jamie Pratt
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot.'/question/type/edit_question_form.php');
33 /**
34  * True-false question editing form definition.
35  *
36  * @copyright  2007 Jamie Pratt
37  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class qtype_truefalse_edit_form extends question_edit_form {
40     /**
41      * Add question-type specific form fields.
42      *
43      * @param object $mform the form being built.
44      */
45     protected function definition_inner($mform) {
46         $mform->addElement('select', 'correctanswer',
47                 get_string('correctanswer', 'qtype_truefalse'), array(
48                 0 => get_string('false', 'qtype_truefalse'),
49                 1 => get_string('true', 'qtype_truefalse')));
51         $mform->addElement('editor', 'feedbacktrue',
52                 get_string('feedbacktrue', 'qtype_truefalse'), array('rows' => 10), $this->editoroptions);
53         $mform->setType('feedbacktrue', PARAM_RAW);
55         $mform->addElement('editor', 'feedbackfalse',
56                 get_string('feedbackfalse', 'qtype_truefalse'), array('rows' => 10), $this->editoroptions);
57         $mform->setType('feedbackfalse', PARAM_RAW);
59         $mform->addElement('header', 'multitriesheader',
60                 get_string('settingsformultipletries', 'question'));
62         $mform->addElement('hidden', 'penalty', 1);
63         $mform->setType('penalty', PARAM_FLOAT);
65         $mform->addElement('static', 'penaltymessage',
66                 get_string('penaltyforeachincorrecttry', 'question'), 1);
67         $mform->addHelpButton('penaltymessage', 'penaltyforeachincorrecttry', 'question');
68     }
70     public function data_preprocessing($question) {
71         $question = parent::data_preprocessing($question);
73         if (!empty($question->options->trueanswer)) {
74             $trueanswer = $question->options->answers[$question->options->trueanswer];
75             $question->correctanswer = ($trueanswer->fraction != 0);
77             $draftid = file_get_submitted_draft_itemid('trueanswer');
78             $answerid = $question->options->trueanswer;
80             $question->feedbacktrue = array();
81             $question->feedbacktrue['format'] = $trueanswer->feedbackformat;
82             $question->feedbacktrue['text'] = file_prepare_draft_area(
83                 $draftid,             // Draftid
84                 $this->context->id,   // context
85                 'question',           // component
86                 'answerfeedback',     // filarea
87                 !empty($answerid) ? (int) $answerid : null, // itemid
88                 $this->fileoptions,   // options
89                 $trueanswer->feedback // text.
90             );
91             $question->feedbacktrue['itemid'] = $draftid;
92         }
94         if (!empty($question->options->falseanswer)) {
95             $falseanswer = $question->options->answers[$question->options->falseanswer];
97             $draftid = file_get_submitted_draft_itemid('falseanswer');
98             $answerid = $question->options->falseanswer;
100             $question->feedbackfalse = array();
101             $question->feedbackfalse['format'] = $falseanswer->feedbackformat;
102             $question->feedbackfalse['text'] = file_prepare_draft_area(
103                 $draftid,              // Draftid
104                 $this->context->id,    // context
105                 'question',            // component
106                 'answerfeedback',      // filarea
107                 !empty($answerid) ? (int) $answerid : null, // itemid
108                 $this->fileoptions,    // options
109                 $falseanswer->feedback // text.
110             );
111             $question->feedbackfalse['itemid'] = $draftid;
112         }
114         return $question;
115     }
117     public function qtype() {
118         return 'truefalse';
119     }