Proyectos de Subversion Moodle

Rev

| 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
namespace mod_questionnaire\question;
18
use \html_writer;
19
use mod_questionnaire\responsetype\response\response;
20
 
21
/**
22
 * This file contains the parent class for essay question types.
23
 *
24
 * @author Mike Churchward
25
 * @copyright 2016 onward Mike Churchward (mike.churchward@poetopensource.org)
26
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
27
 * @package mod_questionnaire
28
 */
29
class essay extends text {
30
 
31
    /**
32
     * Each question type must define its response class.
33
     * @return object The response object based off of questionnaire_response_base.
34
     */
35
    protected function responseclass() {
36
        return '\\mod_questionnaire\\responsetype\\text';
37
    }
38
 
39
    /**
40
     * Short name for this question type - no spaces, etc..
41
     * @return string
42
     */
43
    public function helpname() {
44
        return 'essaybox';
45
    }
46
 
47
    /**
48
     * Override and return a form template if provided. Output of question_survey_display is iterpreted based on this.
49
     * @return boolean | string
50
     */
51
    public function question_template() {
52
        return false;
53
    }
54
 
55
    /**
56
     * Override and return a response template if provided. Output of response_survey_display is iterpreted based on this.
57
     * @return boolean | string
58
     */
59
    public function response_template() {
60
        return false;
61
    }
62
 
63
    /**
64
     * Question specific display method.
65
     * @param response $response
66
     * @param array $descendantsdata
67
     * @param bool $blankquestionnaire
68
     *
69
     */
70
    protected function question_survey_display($response, $descendantsdata, $blankquestionnaire=false) {
71
        $output = '';
72
 
73
        // Essay.
74
        // Columns and rows default values.
75
        $cols = 80;
76
        $rows = 15;
77
        // Use HTML editor or not?
78
        if ($this->precise == 0) {
79
            $canusehtmleditor = true;
80
            $rows = $this->length == 0 ? $rows : $this->length;
81
        } else {
82
            $canusehtmleditor = false;
83
            // Prior to version 2.6, "precise" was used for rows number.
84
            $rows = $this->precise > 1 ? $this->precise : $this->length;
85
        }
86
        $name = 'q'.$this->id;
87
        if (isset($response->answers[$this->id][0])) {
88
            $value = $response->answers[$this->id][0]->value;
89
        } else {
90
            $value = '';
91
        }
92
        if ($canusehtmleditor) {
93
            $editor = editors_get_preferred_editor();
94
            $editor->use_editor($name, questionnaire_get_editor_options($this->context));
95
            $texteditor = html_writer::tag('textarea', $value,
96
                            ['id' => $name, 'name' => $name, 'rows' => $rows, 'cols' => $cols, 'class' => 'form-control']);
97
        } else {
98
            $editor = FORMAT_PLAIN;
99
            $texteditor = html_writer::tag('textarea', $value,
100
                            ['id' => $name, 'name' => $name, 'rows' => $rows, 'cols' => $cols]);
101
        }
102
        $output .= $texteditor;
103
 
104
        return $output;
105
    }
106
 
107
    /**
108
     * Question specific response display method.
109
     * @param \stdClass $response
110
     *
111
     */
112
    protected function response_survey_display($response) {
113
        if (isset($response->answers[$this->id])) {
114
            $answer = reset($response->answers[$this->id]);
115
            $answer = format_text($answer->value, FORMAT_HTML);
116
        } else {
117
            $answer = '&nbsp;';
118
        }
119
        $output = '';
120
        $output .= '<div class="response text">';
121
        $output .= $answer;
122
        $output .= '</div>';
123
        return $output;
124
    }
125
 
126
    // Note - intentianally returning 'precise' for length and 'length' for precise.
127
 
128
    /**
129
     * Return the length form element.
130
     * @param \MoodleQuickForm $mform
131
     * @param string $helptext
132
     */
133
    protected function form_length(\MoodleQuickForm $mform, $helptext = '') {
134
        $responseformats = [
135
                        "0" => get_string('formateditor', 'questionnaire'),
136
                        "1" => get_string('formatplain', 'questionnaire')];
137
        $mform->addElement('select', 'precise', get_string('responseformat', 'questionnaire'), $responseformats);
138
        $mform->setType('precise', PARAM_INT);
139
        return $mform;
140
    }
141
 
142
    /**
143
     * True if question provides mobile support.
144
     * @return bool
145
     */
146
    public function supports_mobile() {
147
        return true;
148
    }
149
 
150
    /**
151
     * Return the precision form element.
152
     * @param \MoodleQuickForm $mform
153
     * @param string $helptext
154
     */
155
    protected function form_precise(\MoodleQuickForm $mform, $helptext = '') {
156
        $choices = [];
157
        for ($lines = 5; $lines <= 40; $lines += 5) {
158
            $choices[$lines] = get_string('nlines', 'questionnaire', $lines);
159
        }
160
        $mform->addElement('select', 'length', get_string('responsefieldlines', 'questionnaire'), $choices);
161
        $mform->setType('length', PARAM_INT);
162
        return $mform;
163
    }
164
}