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 defines a structured class to hold question choices.
|
|
|
21 |
*
|
|
|
22 |
* @author Mike Churchward
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
24 |
* @package mod_questionnaire
|
|
|
25 |
* @copyright 2019, onwards Poet
|
|
|
26 |
*/
|
|
|
27 |
class choice {
|
|
|
28 |
|
|
|
29 |
// Class properties.
|
|
|
30 |
|
|
|
31 |
/** The table name. */
|
|
|
32 |
const TABLE = 'questionnaire_quest_choice';
|
|
|
33 |
|
|
|
34 |
/** @var int $id The id of the question choice this applies to. */
|
|
|
35 |
public $id;
|
|
|
36 |
|
|
|
37 |
/** @var int $questionid The id of the question this choice applies to. */
|
|
|
38 |
public $questionid;
|
|
|
39 |
|
|
|
40 |
/** @var string $content The display content for this choice. */
|
|
|
41 |
public $content;
|
|
|
42 |
|
|
|
43 |
/** @var string $value Optional value assigned to this choice. */
|
|
|
44 |
public $value;
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Choice constructor.
|
|
|
48 |
* @param int $id
|
|
|
49 |
* @param int $questionid
|
|
|
50 |
* @param string $content
|
|
|
51 |
* @param mixed $value
|
|
|
52 |
*/
|
|
|
53 |
public function __construct($id = null, $questionid = null, $content = null, $value = null) {
|
|
|
54 |
$this->id = $id;
|
|
|
55 |
$this->questionid = $questionid;
|
|
|
56 |
$this->content = $content;
|
|
|
57 |
$this->value = $value;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Create and return a choice object from a data id. If not found, an empty object is returned.
|
|
|
62 |
*
|
|
|
63 |
* @param int $id The data id to load.
|
|
|
64 |
* @return choice
|
|
|
65 |
*/
|
|
|
66 |
public static function create_from_id($id) {
|
|
|
67 |
global $DB;
|
|
|
68 |
|
|
|
69 |
// Rename the data field question_id to questionid to conform with code conventions. Eventually, data table should be
|
|
|
70 |
// changed.
|
|
|
71 |
if ($record = $DB->get_record(self::tablename(), ['id' => $id], 'id,question_id as questionid,content,value')) {
|
|
|
72 |
return new choice($id, $record->questionid, $record->content, $record->value);
|
|
|
73 |
} else {
|
|
|
74 |
return new choice();
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Create and return a choice object from data.
|
|
|
80 |
*
|
|
|
81 |
* @param \stdclass|array $choicedata The data to load.
|
|
|
82 |
* @return choice
|
|
|
83 |
*/
|
|
|
84 |
public static function create_from_data($choicedata) {
|
|
|
85 |
if (!is_array($choicedata)) {
|
|
|
86 |
$choicedata = (array)$choicedata;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
$properties = array_keys(get_class_vars(__CLASS__));
|
|
|
90 |
foreach ($properties as $property) {
|
|
|
91 |
if (!isset($choicedata[$property])) {
|
|
|
92 |
$choicedata[$property] = null;
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
// Since the data table uses 'question_id' instead of 'questionid', look for that field as well. Hack that should be fixed
|
|
|
96 |
// by renaming the data table column.
|
|
|
97 |
if (!empty($choicedata['question_id'])) {
|
|
|
98 |
$choicedata['questionid'] = $choicedata['question_id'];
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
return new choice($choicedata['id'], $choicedata['questionid'], $choicedata['content'], $choicedata['value']);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Return the table name for choice.
|
|
|
106 |
*/
|
|
|
107 |
public static function tablename() {
|
|
|
108 |
return self::TABLE;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Delete the choice record.
|
|
|
113 |
* @param int $id
|
|
|
114 |
* @return bool
|
|
|
115 |
*/
|
|
|
116 |
public static function delete_from_db_by_id($id) {
|
|
|
117 |
global $DB;
|
|
|
118 |
return $DB->delete_records(self::tablename(), ['id' => $id]);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Delete this record from the DB.
|
|
|
123 |
* @return bool
|
|
|
124 |
*/
|
|
|
125 |
public function delete_from_db() {
|
|
|
126 |
return self::delete_from_db_by_id($this->id);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Return true if the content string is an "other" choice.
|
|
|
131 |
*
|
|
|
132 |
* @param string $content
|
|
|
133 |
* @return bool
|
|
|
134 |
*/
|
|
|
135 |
public static function content_is_other_choice($content) {
|
|
|
136 |
return (strpos($content, '!other') === 0);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Return true if the choice object is an "other" choice.
|
|
|
141 |
*
|
|
|
142 |
* @return bool
|
|
|
143 |
*/
|
|
|
144 |
public function is_other_choice() {
|
|
|
145 |
return (self::content_is_other_choice($this->content));
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Return the string to display for an "other" option content string. If the option is not an "other", return false.
|
|
|
150 |
*
|
|
|
151 |
* @param string $content
|
|
|
152 |
* @return string|bool
|
|
|
153 |
*/
|
|
|
154 |
public static function content_other_choice_display($content) {
|
|
|
155 |
if (!self::content_is_other_choice($content)) {
|
|
|
156 |
return false;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
// If there is a defined string display after the "=", return it. Otherwise the "other" language string.
|
|
|
160 |
return preg_replace(["/^!other=/", "/^!other/"], ['', get_string('other', 'questionnaire')], $content);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* Return the string to display for an "other" option for this object. If the option is not an "other", return false.
|
|
|
165 |
*
|
|
|
166 |
* @return string|bool
|
|
|
167 |
*/
|
|
|
168 |
public function other_choice_display() {
|
|
|
169 |
return self::content_other_choice_display($this->content);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Is the content a named degree rate choice.
|
|
|
174 |
* @param string $content
|
|
|
175 |
* @return array|bool
|
|
|
176 |
*/
|
|
|
177 |
public static function content_is_named_degree_choice($content) {
|
|
|
178 |
if (preg_match("/^([0-9]{1,3})=(.*)$/", $content, $ndegrees)) {
|
|
|
179 |
return [$ndegrees[1] => $ndegrees[2]];
|
|
|
180 |
} else {
|
|
|
181 |
return false;
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Is the choice object a named degree rate choice.
|
|
|
187 |
* @return array|bool
|
|
|
188 |
*/
|
|
|
189 |
public function is_named_degree_choice() {
|
|
|
190 |
return self::content_is_named_degree_choice($this->content);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Return the string to use as an input name for an other choice.
|
|
|
195 |
*
|
|
|
196 |
* @param int $choiceid
|
|
|
197 |
* @return string
|
|
|
198 |
*/
|
|
|
199 |
public static function id_other_choice_name($choiceid) {
|
|
|
200 |
return 'o' . $choiceid;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Return the string to use as an input name for an other choice.
|
|
|
205 |
* @return string
|
|
|
206 |
*/
|
|
|
207 |
public function other_choice_name() {
|
|
|
208 |
return self::id_other_choice_name($this->id);
|
|
|
209 |
}
|
|
|
210 |
}
|