|
1
|
|
|
<?php |
|
2
|
|
|
/* For licensing terms, see /license.txt */ |
|
3
|
|
|
|
|
4
|
|
|
use ChamiloSession as Session; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class survey_question |
|
8
|
|
|
*/ |
|
9
|
|
|
class survey_question |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var FormValidator */ |
|
12
|
|
|
private $form; |
|
13
|
|
|
public $buttonList = array(); |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Generic part of any survey question: the question field |
|
17
|
|
|
* @param array $surveyData |
|
18
|
|
|
* @param array $formData |
|
19
|
|
|
* |
|
20
|
|
|
* @return FormValidator |
|
21
|
|
|
*/ |
|
22
|
|
|
public function createForm($surveyData, $formData) |
|
23
|
|
|
{ |
|
24
|
|
|
$action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : null; |
|
25
|
|
|
$questionId = isset($_GET['question_id']) ? intval($_GET['question_id']) : null; |
|
26
|
|
|
$surveyId = isset($_GET['survey_id']) ? intval($_GET['survey_id']) : null; |
|
27
|
|
|
|
|
28
|
|
|
$toolName = Display::return_icon( |
|
29
|
|
|
SurveyManager::icon_question(Security::remove_XSS($_GET['type'])), |
|
30
|
|
|
get_lang(ucfirst(Security::remove_XSS($_GET['type']))), |
|
31
|
|
|
array('align' => 'middle', 'height' => '22px') |
|
32
|
|
|
).' '; |
|
33
|
|
|
|
|
34
|
|
|
if ($action == 'add') { |
|
35
|
|
|
$toolName .= get_lang('AddQuestion'); |
|
36
|
|
|
} |
|
37
|
|
|
if ($action == 'edit') { |
|
38
|
|
|
$toolName .= get_lang('EditQuestion'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ($_GET['type'] == 'yesno') { |
|
42
|
|
|
$toolName .= ': '.get_lang('YesNo'); |
|
43
|
|
|
} else if ($_GET['type'] == 'multiplechoice') { |
|
44
|
|
|
$toolName .= ': '.get_lang('UniqueSelect'); |
|
45
|
|
|
} else { |
|
46
|
|
|
$toolName .= ': '.get_lang(api_ucfirst(Security::remove_XSS($_GET['type']))); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$sharedQuestionId = isset($formData['shared_question_id']) ? $formData['shared_question_id'] : null; |
|
50
|
|
|
|
|
51
|
|
|
$url = api_get_self().'?action='.$action.'&type='.Security::remove_XSS($_GET['type']).'&survey_id='.$surveyId.'&question_id='.$questionId.'&'.api_get_cidreq(); |
|
52
|
|
|
|
|
53
|
|
|
$form = new FormValidator('question_form', 'post', $url); |
|
54
|
|
|
$form->addHeader($toolName); |
|
55
|
|
|
$form->addHidden('survey_id', $surveyId); |
|
56
|
|
|
$form->addHidden('question_id', $questionId); |
|
57
|
|
|
$form->addHidden('shared_question_id', Security::remove_XSS($sharedQuestionId)); |
|
58
|
|
|
$form->addHidden('type', Security::remove_XSS($_GET['type'])); |
|
59
|
|
|
|
|
60
|
|
|
$config = array('ToolbarSet' => 'SurveyQuestion', 'Width' => '100%', 'Height' => '120'); |
|
61
|
|
|
$form->addHtmlEditor('question', get_lang('Question'), true, false, $config); |
|
62
|
|
|
|
|
63
|
|
|
// When survey type = 1?? |
|
64
|
|
|
if ($surveyData['survey_type'] == 1) { |
|
65
|
|
|
$table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP); |
|
66
|
|
|
$sql = 'SELECT id,name FROM '.$table_survey_question_group.' |
|
67
|
|
|
WHERE survey_id = '.(int)$_GET['survey_id'].' |
|
68
|
|
|
ORDER BY name'; |
|
69
|
|
|
$rs = Database::query($sql); |
|
70
|
|
|
$glist = null; |
|
71
|
|
|
while ($row = Database::fetch_array($rs, 'NUM')) { |
|
72
|
|
|
$glist .= '<option value="'.$row[0].'" >'.$row[1].'</option>'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$grouplist = $grouplist1 = $grouplist2 = $glist; |
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
if (!empty($formData['assigned'])) { |
|
78
|
|
|
$grouplist = str_replace('<option value="'.$formData['assigned'].'"','<option value="'.$formData['assigned'].'" selected',$glist); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
View Code Duplication |
if (!empty($formData['assigned1'])) { |
|
82
|
|
|
$grouplist1 = str_replace('<option value="'.$formData['assigned1'].'"','<option value="'.$formData['assigned1'].'" selected',$glist); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
if (!empty($formData['assigned2'])) { |
|
86
|
|
|
$grouplist2 = str_replace('<option value="'.$formData['assigned2'].'"','<option value="'.$formData['assigned2'].'" selected',$glist); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->html .= ' <tr><td colspan=""> |
|
90
|
|
|
<fieldset style="border:1px solid black"><legend>'.get_lang('Condition').'</legend> |
|
91
|
|
|
|
|
92
|
|
|
<b>'.get_lang('Primary').'</b><br /> |
|
93
|
|
|
'.'<input type="radio" name="choose" value="1" '.(($formData['choose'] == 1) ? 'checked' : ''). |
|
94
|
|
|
'><select name="assigned">'.$grouplist.'</select><br />'; |
|
95
|
|
|
|
|
96
|
|
|
$this->html .= ' |
|
97
|
|
|
<b>'.get_lang('Secondary').'</b><br /> |
|
98
|
|
|
'.'<input type="radio" name="choose" value="2" '.(($formData['choose']==2)?'checked':''). |
|
99
|
|
|
'><select name="assigned1">'.$grouplist1.'</select> '. |
|
100
|
|
|
'<select name="assigned2">'.$grouplist2.'</select>' |
|
101
|
|
|
.'</fieldset><br />'; |
|
102
|
|
|
|
|
103
|
|
|
//$form->addRadio('choose', get_lang('Primary')); |
|
104
|
|
|
//$form->addRadio('choose', get_lang('Secondary')); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->setForm($form); |
|
108
|
|
|
|
|
109
|
|
|
return $form; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Adds submit button |
|
114
|
|
|
* |
|
115
|
|
|
*/ |
|
116
|
|
|
public function renderForm() |
|
117
|
|
|
{ |
|
118
|
|
|
if (isset($_GET['question_id']) and !empty($_GET['question_id'])) { |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Check if survey has answers first before update it, this is because if you update it, the question |
|
122
|
|
|
* options will delete and re-insert in database loosing the iid and question_id to verify the correct answers |
|
123
|
|
|
*/ |
|
124
|
|
|
$surveyId = isset($_GET['survey_id']) ? intval($_GET['survey_id']) : 0; |
|
125
|
|
|
$answersChecker = SurveyUtil::checkIfSurveyHasAnswers($surveyId); |
|
126
|
|
|
|
|
127
|
|
|
if (!$answersChecker) { |
|
128
|
|
|
$this->buttonList[] = $this->getForm()->addButtonUpdate(get_lang('ModifyQuestionSurvey'), 'save', true); |
|
129
|
|
|
} else { |
|
130
|
|
|
$this->getForm()->addHtml(' |
|
131
|
|
|
<div class="form-group"> |
|
132
|
|
|
<label class="col-sm-2 control-label"></label> |
|
133
|
|
|
<div class="col-sm-8"> |
|
134
|
|
|
<div class="alert alert-info">' . get_lang('YouCantNotEditThisQuestionBecauseAlreadyExistAnswers') . '</div> |
|
135
|
|
|
</div> |
|
136
|
|
|
<div class="col-sm-2"></div> |
|
137
|
|
|
</div> |
|
138
|
|
|
'); |
|
139
|
|
|
} |
|
140
|
|
|
} else { |
|
141
|
|
|
$this->buttonList[] = $this->getForm()->addButtonSave(get_lang('CreateQuestionSurvey'), 'save', true); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$this->getForm()->addGroup($this->buttonList, 'buttons'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return FormValidator |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getForm() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->form; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param FormValidator $form |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setForm($form) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->form = $form; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param array $formData |
|
165
|
|
|
* |
|
166
|
|
|
* @return mixed |
|
167
|
|
|
*/ |
|
168
|
|
|
public function preSave($formData) |
|
169
|
|
|
{ |
|
170
|
|
|
$counter = Session::read('answer_count'); |
|
171
|
|
|
$answerList = Session::read('answer_list'); |
|
172
|
|
|
|
|
173
|
|
|
if (empty($answerList)) { |
|
174
|
|
|
$answerList = isset($formData['answers']) ? $formData['answers'] : array(); |
|
175
|
|
|
Session::write('answer_list', $answerList); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if (isset($_POST['answers'])) { |
|
179
|
|
|
$formData['answers'] = $_POST['answers']; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if (empty($counter)) { |
|
183
|
|
|
$counter = count($answerList) - 1; |
|
184
|
|
|
Session::write('answer_count', $counter); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
// Moving an answer up |
|
188
|
|
View Code Duplication |
if (isset($_POST['move_up']) && $_POST['move_up']) { |
|
189
|
|
|
foreach ($_POST['move_up'] as $key => & $value) { |
|
190
|
|
|
$id1 = $key; |
|
191
|
|
|
$content1 = $formData['answers'][$id1]; |
|
192
|
|
|
$id2 = $key - 1; |
|
193
|
|
|
$content2 = $formData['answers'][$id2]; |
|
194
|
|
|
$formData['answers'][$id1] = $content2; |
|
195
|
|
|
$formData['answers'][$id2] = $content1; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
// Moving an answer down |
|
200
|
|
View Code Duplication |
if (isset($_POST['move_down']) && $_POST['move_down']) { |
|
201
|
|
|
foreach ($_POST['move_down'] as $key => & $value) { |
|
202
|
|
|
$id1 = $key; |
|
203
|
|
|
$content1 = $formData['answers'][$id1]; |
|
204
|
|
|
$id2 = $key + 1; |
|
205
|
|
|
$content2 = $formData['answers'][$id2]; |
|
206
|
|
|
$formData['answers'][$id1] = $content2; |
|
207
|
|
|
$formData['answers'][$id2] = $content1; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* This solution is a little bit strange but I could not find a different solution. |
|
213
|
|
|
*/ |
|
214
|
|
|
if (isset($_POST['delete_answer'])) { |
|
215
|
|
|
$deleted = false; |
|
216
|
|
|
foreach ($_POST['delete_answer'] as $key => & $value) { |
|
217
|
|
|
$deleted = $key; |
|
218
|
|
|
$counter--; |
|
219
|
|
|
Session::write('answer_count', $counter); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
foreach ($formData['answers'] as $key => & $value) { |
|
223
|
|
|
if ($key > $deleted) { |
|
224
|
|
|
$formData['answers'][$key - 1] = $formData['answers'][$key]; |
|
225
|
|
|
unset($formData['answers'][$key]); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// Adding an answer |
|
231
|
|
|
if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) { |
|
232
|
|
|
$counter++; |
|
233
|
|
|
Session::write('answer_count', $counter); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
// Removing an answer |
|
237
|
|
|
if (isset($_POST['buttons']) && isset($_POST['buttons']['remove_answer'])) { |
|
238
|
|
|
$counter--; |
|
239
|
|
|
Session::write('answer_count', $counter); |
|
240
|
|
View Code Duplication |
foreach ($formData['answers'] as $index => &$data) { |
|
241
|
|
|
if ($index > $counter) { |
|
242
|
|
|
unset($formData['answers'][$index]); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
if (!isset($_POST['delete_answer'])) { |
|
248
|
|
|
if (isset($formData['answers'])) { |
|
249
|
|
View Code Duplication |
foreach ($formData['answers'] as $index => $data) { |
|
250
|
|
|
if ($index > $counter) { |
|
251
|
|
|
unset($formData['answers'][$index]); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
for ($i = 0; $i <= $counter; $i++) { |
|
256
|
|
|
if (!isset($formData['answers'][$i])) { |
|
257
|
|
|
$formData['answers'][$i] = ''; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$formData['answers'] = isset($formData['answers']) ? $formData['answers'] : []; |
|
264
|
|
|
Session::write('answer_list', $formData['answers']); |
|
265
|
|
|
|
|
266
|
|
|
return $formData; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @param array $surveyData |
|
271
|
|
|
* @param array $formData |
|
272
|
|
|
* |
|
273
|
|
|
* @return mixed |
|
274
|
|
|
*/ |
|
275
|
|
|
public function save($surveyData, $formData) |
|
276
|
|
|
{ |
|
277
|
|
|
// Saving a question |
|
278
|
|
|
if (isset($_POST['buttons']) && isset($_POST['buttons']['save'])) { |
|
279
|
|
|
Session::erase('answer_count'); |
|
280
|
|
|
Session::erase('answer_list'); |
|
281
|
|
|
$message = SurveyManager::save_question( |
|
282
|
|
|
$surveyData, |
|
283
|
|
|
$formData |
|
284
|
|
|
); |
|
285
|
|
|
|
|
286
|
|
|
if ($message == 'QuestionAdded' || $message == 'QuestionUpdated') { |
|
287
|
|
|
header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'&message='.$message.'&'.api_get_cidreq()); |
|
288
|
|
|
exit; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
return $formData; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Adds two buttons. One to add an option, one to remove an option |
|
297
|
|
|
* |
|
298
|
|
|
* @param array $data |
|
299
|
|
|
* |
|
300
|
|
|
*/ |
|
301
|
|
|
public function addRemoveButtons($data) |
|
302
|
|
|
{ |
|
303
|
|
|
$this->buttonList['remove_answer'] = $this->getForm()->createElement( |
|
304
|
|
|
'button', |
|
305
|
|
|
'remove_answer', |
|
306
|
|
|
get_lang('RemoveAnswer'), |
|
307
|
|
|
'minus' |
|
308
|
|
|
); |
|
309
|
|
|
|
|
310
|
|
|
if (count($data['answers']) <= 2) { |
|
311
|
|
|
$this->buttonList['remove_answer']->updateAttributes( |
|
312
|
|
|
array('disabled' => 'disabled') |
|
313
|
|
|
); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
$this->buttonList['add_answer'] = $this->getForm()->createElement( |
|
317
|
|
|
'button', |
|
318
|
|
|
'add_answer', |
|
319
|
|
|
get_lang('AddAnswer'), |
|
320
|
|
|
'plus' |
|
321
|
|
|
); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @param FormValidator $form |
|
326
|
|
|
* @param array $questionData |
|
327
|
|
|
* @param array $answers |
|
328
|
|
|
*/ |
|
329
|
|
|
public function render(FormValidator $form, $questionData = array(), $answers = array()) |
|
330
|
|
|
{ |
|
331
|
|
|
return null; |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
|