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 |
|
|
|
19 |
/**
|
|
|
20 |
* This file contains the parent class for text question types.
|
|
|
21 |
*
|
|
|
22 |
* @author Mike Churchward
|
|
|
23 |
* @copyright 2016 onward Mike Churchward (mike.churchward@poetopensource.org)
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
25 |
* @package mod_questionnaire
|
|
|
26 |
*/
|
|
|
27 |
class text extends question {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* The class constructor
|
|
|
31 |
* @param int $id
|
|
|
32 |
* @param \stdClass $question
|
|
|
33 |
* @param \context $context
|
|
|
34 |
* @param array $params
|
|
|
35 |
*/
|
|
|
36 |
public function __construct($id = 0, $question = null, $context = null, $params = []) {
|
|
|
37 |
$this->length = 20;
|
|
|
38 |
$this->precise = 25;
|
|
|
39 |
return parent::__construct($id, $question, $context, $params);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Each question type must define its response class.
|
|
|
44 |
* @return object The response object based off of questionnaire_response_base.
|
|
|
45 |
*/
|
|
|
46 |
protected function responseclass() {
|
|
|
47 |
return '\\mod_questionnaire\\responsetype\\text';
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Short name for this question type - no spaces, etc..
|
|
|
52 |
* @return string
|
|
|
53 |
*/
|
|
|
54 |
public function helpname() {
|
|
|
55 |
return 'textbox';
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Override and return a form template if provided. Output of question_survey_display is iterpreted based on this.
|
|
|
60 |
* @return boolean | string
|
|
|
61 |
*/
|
|
|
62 |
public function question_template() {
|
|
|
63 |
return 'mod_questionnaire/question_text';
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Override and return a response template if provided. Output of response_survey_display is iterpreted based on this.
|
|
|
68 |
* @return boolean | string
|
|
|
69 |
*/
|
|
|
70 |
public function response_template() {
|
|
|
71 |
return 'mod_questionnaire/response_text';
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Question specific display method.
|
|
|
76 |
* @param \stdClass $response
|
|
|
77 |
* @param array $descendantsdata
|
|
|
78 |
* @param bool $blankquestionnaire
|
|
|
79 |
*
|
|
|
80 |
*/
|
|
|
81 |
protected function question_survey_display($response, $descendantsdata, $blankquestionnaire=false) {
|
|
|
82 |
// Text Box.
|
|
|
83 |
$questiontags = new \stdClass();
|
|
|
84 |
$questiontags->qelements = new \stdClass();
|
|
|
85 |
$choice = new \stdClass();
|
|
|
86 |
$choice->onkeypress = 'return event.keyCode != 13;';
|
|
|
87 |
$choice->size = $this->length;
|
|
|
88 |
$choice->name = 'q'.$this->id;
|
|
|
89 |
if ($this->precise > 0) {
|
|
|
90 |
$choice->maxlength = $this->precise;
|
|
|
91 |
}
|
|
|
92 |
$choice->value = (isset($response->answers[$this->id][0]) ?
|
|
|
93 |
format_string(stripslashes($response->answers[$this->id][0]->value)) : '');
|
|
|
94 |
$choice->id = self::qtypename($this->type_id) . $this->id;
|
|
|
95 |
$questiontags->qelements->choice = $choice;
|
|
|
96 |
return $questiontags;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Question specific response display method.
|
|
|
101 |
* @param \stdClass $response
|
|
|
102 |
*/
|
|
|
103 |
protected function response_survey_display($response) {
|
|
|
104 |
$resptags = new \stdClass();
|
|
|
105 |
if (isset($response->answers[$this->id])) {
|
|
|
106 |
$answer = reset($response->answers[$this->id]);
|
|
|
107 |
$resptags->content = format_text($answer->value, FORMAT_HTML);
|
|
|
108 |
}
|
|
|
109 |
return $resptags;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Return the length form element.
|
|
|
114 |
* @param \MoodleQuickForm $mform
|
|
|
115 |
* @param string $helptext
|
|
|
116 |
*/
|
|
|
117 |
protected function form_length(\MoodleQuickForm $mform, $helptext = '') {
|
|
|
118 |
return parent::form_length($mform, 'fieldlength');
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Return the precision form element.
|
|
|
123 |
* @param \MoodleQuickForm $mform
|
|
|
124 |
* @param string $helptext
|
|
|
125 |
*/
|
|
|
126 |
protected function form_precise(\MoodleQuickForm $mform, $helptext = '') {
|
|
|
127 |
return parent::form_precise($mform, 'maxtextlength');
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* True if question provides mobile support.
|
|
|
132 |
* @return bool
|
|
|
133 |
*/
|
|
|
134 |
public function supports_mobile() {
|
|
|
135 |
return true;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Override and return false if not supporting mobile app.
|
|
|
140 |
* @param int $qnum
|
|
|
141 |
* @param bool $autonum
|
|
|
142 |
* @return \stdClass
|
|
|
143 |
*/
|
|
|
144 |
public function mobile_question_display($qnum, $autonum = false) {
|
|
|
145 |
$mobiledata = parent::mobile_question_display($qnum, $autonum);
|
|
|
146 |
$mobiledata->istextessay = true;
|
|
|
147 |
return $mobiledata;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Override and return false if not supporting mobile app.
|
|
|
152 |
* @return array
|
|
|
153 |
*/
|
|
|
154 |
public function mobile_question_choices_display() {
|
|
|
155 |
$choices = [];
|
|
|
156 |
$choices[0] = new \stdClass();
|
|
|
157 |
$choices[0]->id = 0;
|
|
|
158 |
$choices[0]->choice_id = 0;
|
|
|
159 |
$choices[0]->question_id = $this->id;
|
|
|
160 |
$choices[0]->content = '';
|
|
|
161 |
$choices[0]->value = null;
|
|
|
162 |
return $choices;
|
|
|
163 |
}
|
|
|
164 |
}
|