Total Complexity | 114 |
Total Lines | 704 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like survey_question often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use survey_question, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class survey_question |
||
11 | { |
||
12 | public $buttonList = []; |
||
13 | /** @var FormValidator */ |
||
14 | private $form; |
||
15 | |||
16 | /** |
||
17 | * @param array $surveyData |
||
18 | */ |
||
19 | public function addParentMenu($formData, FormValidator $form, $surveyData) |
||
20 | { |
||
21 | $surveyId = $surveyData['survey_id']; |
||
22 | $questionId = isset($formData['question_id']) ? $formData['question_id'] : 0; |
||
23 | $parentId = isset($formData['parent_id']) ? $formData['parent_id'] : 0; |
||
24 | $optionId = isset($formData['parent_option_id']) ? $formData['parent_option_id'] : 0; |
||
25 | $questions = SurveyManager::get_questions($surveyId); |
||
26 | |||
27 | $newQuestionList = []; |
||
28 | $allowTypes = ['yesno', 'multiplechoice', 'multipleresponse']; |
||
29 | foreach ($questions as $question) { |
||
30 | if (in_array($question['type'], $allowTypes)) { |
||
31 | $newQuestionList[$question['sort']] = $question; |
||
32 | } |
||
33 | } |
||
34 | ksort($newQuestionList); |
||
35 | |||
36 | $options = []; |
||
37 | foreach ($newQuestionList as $question) { |
||
38 | if (!empty($questionId)) { |
||
39 | if ($question['question_id'] == $questionId) { |
||
40 | break; |
||
41 | } |
||
42 | } |
||
43 | $options[$question['question_id']] = strip_tags($question['question']); |
||
44 | } |
||
45 | $form->addSelect( |
||
46 | 'parent_id', |
||
47 | get_lang('Parent'), |
||
48 | $options, |
||
49 | ['id' => 'parent_id', 'placeholder' => get_lang('SelectAnOption')] |
||
50 | ); |
||
51 | $url = api_get_path(WEB_AJAX_PATH). |
||
52 | 'survey.ajax.php?'.api_get_cidreq().'&a=load_question_options&survey_id='.$surveyId; |
||
53 | $form->addHtml(' |
||
54 | <script> |
||
55 | $(function() { |
||
56 | $("#parent_id").on("change", function() { |
||
57 | var questionId = $(this).val() |
||
58 | var $select = $("#parent_option_id"); |
||
59 | $select.empty(); |
||
60 | |||
61 | if (questionId === "") { |
||
62 | $("#option_list").hide(); |
||
63 | } else { |
||
64 | $.getJSON({ |
||
65 | url: "'.$url.'" + "&question_id=" + questionId, |
||
66 | success: function(data) { |
||
67 | $("#option_list").show(); |
||
68 | $.each(data, function(key, value) { |
||
69 | $("<option>").val(key).text(value).appendTo($select); |
||
70 | }); |
||
71 | } |
||
72 | }); |
||
73 | } |
||
74 | }); |
||
75 | }); |
||
76 | </script> |
||
77 | '); |
||
78 | |||
79 | $style = 'display:none'; |
||
80 | $options = []; |
||
81 | if (!empty($optionId) && !empty($parentId)) { |
||
82 | $parentData = SurveyManager::get_question($parentId); |
||
83 | $style = ''; |
||
84 | foreach ($parentData['answer_data'] as $answer) { |
||
85 | $options[$answer['iid']] = strip_tags($answer['data']); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | $form->addHtml('<div id="option_list" style="'.$style.'">'); |
||
90 | $form->addSelect( |
||
91 | 'parent_option_id', |
||
92 | get_lang('Option'), |
||
93 | $options, |
||
94 | ['id' => 'parent_option_id', 'disable_js' => true] |
||
95 | ); |
||
96 | $form->addHtml('</div>'); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param string $type |
||
101 | * |
||
102 | * @return survey_question |
||
103 | */ |
||
104 | public static function createQuestion($type) |
||
105 | { |
||
106 | switch ($type) { |
||
107 | case 'comment': |
||
108 | return new ch_comment(); |
||
109 | case 'dropdown': |
||
110 | return new ch_dropdown(); |
||
111 | case 'multiplechoice': |
||
112 | return new ch_multiplechoice(); |
||
113 | case 'multipleresponse': |
||
114 | return new ch_multipleresponse(); |
||
115 | case 'open': |
||
116 | return new ch_open(); |
||
117 | case 'pagebreak': |
||
118 | return new ch_pagebreak(); |
||
119 | case 'percentage': |
||
120 | return new ch_percentage(); |
||
121 | case 'personality': |
||
122 | return new ch_personality(); |
||
123 | case 'score': |
||
124 | return new ch_score(); |
||
125 | case 'yesno': |
||
126 | return new ch_yesno(); |
||
127 | case 'selectivedisplay': |
||
128 | return new ch_selectivedisplay(); |
||
129 | case 'multiplechoiceother': |
||
130 | return new ch_multiplechoiceother(); |
||
131 | default: |
||
132 | api_not_allowed(true); |
||
133 | break; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Generic part of any survey question: the question field. |
||
139 | * |
||
140 | * @param array $surveyData |
||
141 | * @param array $formData |
||
142 | * |
||
143 | * @return FormValidator |
||
144 | */ |
||
145 | public function createForm($surveyData, $formData) |
||
146 | { |
||
147 | $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : null; |
||
148 | $questionId = isset($_GET['question_id']) ? (int) $_GET['question_id'] : null; |
||
149 | $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : null; |
||
150 | $type = isset($_GET['type']) ? Security::remove_XSS($_GET['type']) : null; |
||
151 | |||
152 | $actionHeader = get_lang('EditQuestion').': '; |
||
153 | if ($action === 'add') { |
||
154 | $actionHeader = get_lang('AddQuestion').': '; |
||
155 | } |
||
156 | |||
157 | $questionComment = ''; |
||
158 | $allowParent = false; |
||
159 | switch ($type) { |
||
160 | case 'open': |
||
161 | $toolName = get_lang('Open'); |
||
162 | $questionComment = get_lang('QuestionTags'); |
||
163 | $allowParent = true; |
||
164 | break; |
||
165 | case 'yesno': |
||
166 | $toolName = get_lang('YesNo'); |
||
167 | $allowParent = true; |
||
168 | break; |
||
169 | case 'multiplechoice': |
||
170 | $toolName = get_lang('UniqueSelect'); |
||
171 | $allowParent = true; |
||
172 | break; |
||
173 | case 'multipleresponse': |
||
174 | $toolName = get_lang('MultipleResponse'); |
||
175 | $allowParent = true; |
||
176 | break; |
||
177 | case 'selectivedisplay': |
||
178 | $toolName = get_lang('SurveyQuestionSelectiveDisplay'); |
||
179 | $questionComment = get_lang('SurveyQuestionSelectiveDisplayComment'); |
||
180 | $allowParent = true; |
||
181 | break; |
||
182 | case 'multiplechoiceother': |
||
183 | $toolName = get_lang('SurveyQuestionMultipleChoiceWithOther'); |
||
184 | $questionComment = get_lang('SurveyQuestionMultipleChoiceWithOtherComment'); |
||
185 | $allowParent = true; |
||
186 | break; |
||
187 | case 'pagebreak': |
||
188 | $toolName = get_lang(api_ucfirst($type)); |
||
189 | $allowParent = false; |
||
190 | break; |
||
191 | default: |
||
192 | $toolName = get_lang(api_ucfirst($type)); |
||
193 | $allowParent = true; |
||
194 | break; |
||
195 | } |
||
196 | |||
197 | if (false === api_get_configuration_value('survey_question_dependency')) { |
||
198 | $allowParent = false; |
||
199 | } |
||
200 | |||
201 | $icon = Display::return_icon( |
||
202 | SurveyManager::icon_question($type), |
||
203 | $toolName, |
||
204 | ['align' => 'middle', 'height' => '22px'] |
||
205 | ).' '; |
||
206 | |||
207 | $toolName = $icon.$actionHeader.$toolName; |
||
208 | $sharedQuestionId = isset($formData['shared_question_id']) ? $formData['shared_question_id'] : null; |
||
209 | |||
210 | $url = api_get_self().'?action='.$action.'&type='.$type.'&survey_id='.$surveyId.'&question_id='.$questionId.'&'.api_get_cidreq(); |
||
211 | $form = new FormValidator('question_form', 'post', $url); |
||
212 | $form->addHeader($toolName); |
||
213 | if (!empty($questionComment)) { |
||
214 | $form->addHtml(Display::return_message($questionComment, 'info', false)); |
||
215 | } |
||
216 | $form->addHidden('survey_id', $surveyId); |
||
217 | $form->addHidden('question_id', $questionId); |
||
218 | $form->addHidden('shared_question_id', Security::remove_XSS($sharedQuestionId)); |
||
219 | $form->addHidden('type', $type); |
||
220 | |||
221 | $config = [ |
||
222 | 'ToolbarSet' => 'SurveyQuestion', |
||
223 | 'Width' => '100%', |
||
224 | 'Height' => '120', |
||
225 | ]; |
||
226 | $form->addHtmlEditor( |
||
227 | 'question', |
||
228 | get_lang('Question'), |
||
229 | true, |
||
230 | false, |
||
231 | $config |
||
232 | ); |
||
233 | |||
234 | if (api_get_configuration_value('allow_required_survey_questions') && |
||
235 | in_array($_GET['type'], ['yesno', 'multiplechoice'])) { |
||
236 | $form->addCheckBox('is_required', get_lang('IsMandatory'), get_lang('Yes')); |
||
237 | } |
||
238 | |||
239 | if ($allowParent) { |
||
240 | $this->addParentMenu($formData, $form, $surveyData); |
||
241 | } |
||
242 | |||
243 | if (1 == $surveyData['survey_type']) { |
||
244 | $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP); |
||
245 | $sql = 'SELECT id, name FROM '.$table_survey_question_group.' |
||
246 | WHERE survey_id = '.$surveyId.' |
||
247 | ORDER BY name'; |
||
248 | $rs = Database::query($sql); |
||
249 | $glist = null; |
||
250 | while ($row = Database::fetch_array($rs, 'NUM')) { |
||
251 | $glist .= '<option value="'.$row[0].'" >'.$row[1].'</option>'; |
||
252 | } |
||
253 | |||
254 | $grouplist = $grouplist1 = $grouplist2 = $glist; |
||
255 | if (!empty($formData['assigned'])) { |
||
256 | $grouplist = str_replace( |
||
257 | '<option value="'.$formData['assigned'].'"', |
||
258 | '<option value="'.$formData['assigned'].'" selected', |
||
259 | $glist |
||
260 | ); |
||
261 | } |
||
262 | |||
263 | if (!empty($formData['assigned1'])) { |
||
264 | $grouplist1 = str_replace( |
||
265 | '<option value="'.$formData['assigned1'].'"', |
||
266 | '<option value="'.$formData['assigned1'].'" selected', |
||
267 | $glist |
||
268 | ); |
||
269 | } |
||
270 | |||
271 | if (!empty($formData['assigned2'])) { |
||
272 | $grouplist2 = str_replace( |
||
273 | '<option value="'.$formData['assigned2'].'"', |
||
274 | '<option value="'.$formData['assigned2'].'" selected', |
||
275 | $glist |
||
276 | ); |
||
277 | } |
||
278 | |||
279 | $this->html .= '<tr><td colspan=""> |
||
|
|||
280 | <fieldset style="border:1px solid black"> |
||
281 | <legend>'.get_lang('Condition').'</legend> |
||
282 | <b>'.get_lang('Primary').'</b><br /> |
||
283 | <input type="radio" name="choose" value="1" '.(($formData['choose'] == 1) ? 'checked' : '').'> |
||
284 | <select name="assigned">'.$grouplist.'</select><br />'; |
||
285 | $this->html .= ' |
||
286 | <b>'.get_lang('Secondary').'</b><br /> |
||
287 | <input type="radio" name="choose" value="2" '.(($formData['choose'] == 2) ? 'checked' : '').'> |
||
288 | <select name="assigned1">'.$grouplist1.'</select> |
||
289 | <select name="assigned2">'.$grouplist2.'</select> |
||
290 | </fieldset><br />'; |
||
291 | } |
||
292 | |||
293 | $this->setForm($form); |
||
294 | |||
295 | return $form; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Adds submit button. |
||
300 | */ |
||
301 | public function renderForm() |
||
302 | { |
||
303 | if (isset($_GET['question_id']) && !empty($_GET['question_id'])) { |
||
304 | /** |
||
305 | * Prevent the edition of already-answered questions to avoid |
||
306 | * inconsistent answers. Use the configuration option |
||
307 | * survey_allow_answered_question_edit to change this behaviour. |
||
308 | */ |
||
309 | $surveyId = isset($_GET['survey_id']) ? (int) $_GET['survey_id'] : 0; |
||
310 | $answersChecker = SurveyUtil::checkIfSurveyHasAnswers($surveyId); |
||
311 | $allowQuestionEdit = api_get_configuration_value('survey_allow_answered_question_edit') == true; |
||
312 | if ($allowQuestionEdit or !$answersChecker) { |
||
313 | $this->buttonList[] = $this->getForm()->addButtonUpdate(get_lang('ModifyQuestionSurvey'), 'save', true); |
||
314 | } else { |
||
315 | $this->getForm()->addHtml(' |
||
316 | <div class="form-group"> |
||
317 | <label class="col-sm-2 control-label"></label> |
||
318 | <div class="col-sm-8"> |
||
319 | <div class="alert alert-info">'. |
||
320 | get_lang('YouCantNotEditThisQuestionBecauseAlreadyExistAnswers').'</div> |
||
321 | </div> |
||
322 | <div class="col-sm-2"></div> |
||
323 | </div> |
||
324 | '); |
||
325 | } |
||
326 | } else { |
||
327 | $this->buttonList[] = $this->getForm()->addButtonSave(get_lang('CreateQuestionSurvey'), 'save', true); |
||
328 | } |
||
329 | |||
330 | $this->getForm()->addGroup($this->buttonList, 'buttons'); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * @return FormValidator |
||
335 | */ |
||
336 | public function getForm() |
||
337 | { |
||
338 | return $this->form; |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @param FormValidator $form |
||
343 | */ |
||
344 | public function setForm($form) |
||
345 | { |
||
346 | $this->form = $form; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * @param array $formData |
||
351 | * |
||
352 | * @return mixed |
||
353 | */ |
||
354 | public function preSave($formData) |
||
355 | { |
||
356 | $counter = Session::read('answer_count'); |
||
357 | $answerList = Session::read('answer_list'); |
||
358 | |||
359 | if (empty($answerList)) { |
||
360 | $answerList = isset($formData['answers']) ? $formData['answers'] : []; |
||
361 | Session::write('answer_list', $answerList); |
||
362 | } |
||
363 | |||
364 | if (isset($_POST['answers'])) { |
||
365 | $formData['answers'] = $_POST['answers']; |
||
366 | } |
||
367 | |||
368 | if (empty($counter)) { |
||
369 | $counter = count($answerList) - 1; |
||
370 | Session::write('answer_count', $counter); |
||
371 | } |
||
372 | |||
373 | // Moving an answer up |
||
374 | if (isset($_POST['move_up']) && $_POST['move_up']) { |
||
375 | foreach ($_POST['move_up'] as $key => &$value) { |
||
376 | $id1 = $key; |
||
377 | $content1 = $formData['answers'][$id1]; |
||
378 | $id2 = $key - 1; |
||
379 | $content2 = $formData['answers'][$id2]; |
||
380 | $formData['answers'][$id1] = $content2; |
||
381 | $formData['answers'][$id2] = $content1; |
||
382 | } |
||
383 | } |
||
384 | |||
385 | // Moving an answer down |
||
386 | if (isset($_POST['move_down']) && $_POST['move_down']) { |
||
387 | foreach ($_POST['move_down'] as $key => &$value) { |
||
388 | $id1 = $key; |
||
389 | $content1 = $formData['answers'][$id1]; |
||
390 | $id2 = $key + 1; |
||
391 | $content2 = $formData['answers'][$id2]; |
||
392 | $formData['answers'][$id1] = $content2; |
||
393 | $formData['answers'][$id2] = $content1; |
||
394 | } |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Deleting a specific answer is only saved in the session until the |
||
399 | * "Save question" button is pressed. This means all options are kept |
||
400 | * in the survey_question_option table until the question is saved. |
||
401 | */ |
||
402 | if (isset($_POST['delete_answer'])) { |
||
403 | $deleted = false; |
||
404 | foreach ($_POST['delete_answer'] as $key => &$value) { |
||
405 | $deleted = $key; |
||
406 | $counter--; |
||
407 | Session::write('answer_count', $counter); |
||
408 | } |
||
409 | |||
410 | $newAnswers = []; |
||
411 | $newAnswersId = []; |
||
412 | foreach ($formData['answers'] as $key => &$value) { |
||
413 | if ($key > $deleted) { |
||
414 | // swap with previous (deleted) option slot |
||
415 | $newAnswers[$key - 1] = $formData['answers'][$key]; |
||
416 | $newAnswersId[$key - 1] = $formData['answersid'][$key]; |
||
417 | unset($formData['answers'][$key]); |
||
418 | unset($formData['answersid'][$key]); |
||
419 | } elseif ($key === $deleted) { |
||
420 | // delete option |
||
421 | unset($formData['answers'][$deleted]); |
||
422 | unset($formData['answersid'][$deleted]); |
||
423 | } else { |
||
424 | // keep as is |
||
425 | $newAnswers[$key] = $value; |
||
426 | $newAnswersId[$key] = $formData['answersid'][$key]; |
||
427 | } |
||
428 | } |
||
429 | unset($formData['answers']); |
||
430 | unset($formData['answersid']); |
||
431 | $formData['answers'] = $newAnswers; |
||
432 | $formData['answersid'] = $newAnswersId; |
||
433 | } |
||
434 | |||
435 | // Adding an answer |
||
436 | if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) { |
||
437 | if (isset($_REQUEST['type']) && 'multiplechoiceother' === $_REQUEST['type'] && $counter > 2) { |
||
438 | $counter--; |
||
439 | } |
||
440 | $counter++; |
||
441 | Session::write('answer_count', $counter); |
||
442 | } |
||
443 | |||
444 | // Removing an answer |
||
445 | if (isset($_POST['buttons']) && isset($_POST['buttons']['remove_answer'])) { |
||
446 | $counter--; |
||
447 | Session::write('answer_count', $counter); |
||
448 | foreach ($formData['answers'] as $index => &$data) { |
||
449 | if ($index > $counter) { |
||
450 | unset($formData['answers'][$index]); |
||
451 | unset($formData['answersid'][$index]); |
||
452 | } |
||
453 | } |
||
454 | } |
||
455 | |||
456 | if (!isset($_POST['delete_answer'])) { |
||
457 | // Make sure we have an array of answers |
||
458 | if (!isset($formData['answers'])) { |
||
459 | $formData['answers'] = []; |
||
460 | } |
||
461 | // Check if no deleted answer remains at the end of the answers |
||
462 | // array and add empty answers if the array is too short |
||
463 | foreach ($formData['answers'] as $index => $data) { |
||
464 | if ($index > $counter) { |
||
465 | unset($formData['answers'][$index]); |
||
466 | } |
||
467 | } |
||
468 | |||
469 | for ($i = 0; $i <= $counter; $i++) { |
||
470 | if (!isset($formData['answers'][$i])) { |
||
471 | $formData['answers'][$i] = ''; |
||
472 | } |
||
473 | } |
||
474 | } |
||
475 | |||
476 | $formData['answers'] = isset($formData['answers']) ? $formData['answers'] : []; |
||
477 | Session::write('answer_list', $formData['answers']); |
||
478 | |||
479 | if (!isset($formData['is_required']) && api_get_configuration_value('survey_mark_question_as_required')) { |
||
480 | $formData['is_required'] = true; |
||
481 | } |
||
482 | |||
483 | return $formData; |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * @param array $surveyData |
||
488 | * @param array $formData |
||
489 | * |
||
490 | * @return mixed |
||
491 | */ |
||
492 | public function save($surveyData, $formData, $dataFromDatabase = []) |
||
493 | { |
||
494 | // Saving a question |
||
495 | if (isset($_POST['buttons']) && isset($_POST['buttons']['save'])) { |
||
496 | Session::erase('answer_count'); |
||
497 | Session::erase('answer_list'); |
||
498 | $message = SurveyManager::save_question($surveyData, $formData, true, $dataFromDatabase); |
||
499 | |||
500 | if ($message === 'QuestionAdded' || $message === 'QuestionUpdated') { |
||
501 | $url = api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'&message='.$message.'&'.api_get_cidreq(); |
||
502 | header('Location: '.$url); |
||
503 | exit; |
||
504 | } |
||
505 | } |
||
506 | |||
507 | return $formData; |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * Adds two buttons. One to add an option, one to remove an option. |
||
512 | * |
||
513 | * @param array $data |
||
514 | */ |
||
515 | public function addRemoveButtons($data) |
||
537 | ); |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * Get the JS for questions that can depend on a previous question |
||
542 | * (and that hides those questions until something changes in the previous |
||
543 | * question). |
||
544 | * |
||
545 | * @return string HTML code |
||
546 | */ |
||
547 | public static function getJs() |
||
550 | <style> |
||
551 | .with_parent { |
||
552 | display: none; |
||
553 | } |
||
554 | </style> |
||
555 | <script> |
||
556 | $(function() { |
||
557 | }); |
||
558 | </script>'; |
||
559 | } |
||
560 | |||
561 | /** |
||
562 | * Get the question parents recursively, if any. This function depends on |
||
563 | * the existence of a parent_id field, which depends on the |
||
564 | * 'survey_question_dependency' setting and its corresponding SQL |
||
565 | * requirements. |
||
566 | * |
||
567 | * @param int $questionId The c_survey_question.question.id |
||
568 | * @param array $list An array of parents to be extended by this method |
||
569 | * |
||
570 | * @return array The completed array of parents |
||
571 | */ |
||
572 | public static function getParents($questionId, $list = []) |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * Creates the JS code for the given parent question so that it shows |
||
595 | * the children questions when a specific answer of the parent is selected. |
||
596 | * |
||
597 | * @param array $question An array with the question details |
||
598 | * |
||
599 | * @return string JS code to add to the HTML survey question page |
||
600 | */ |
||
601 | public static function getQuestionJs($question) |
||
602 | { |
||
603 | if (empty($question)) { |
||
604 | return ''; |
||
605 | } |
||
606 | |||
607 | $list = self::getDependency($question); |
||
608 | |||
609 | if (empty($list)) { |
||
610 | return ''; |
||
611 | } |
||
612 | |||
613 | $type = $question['type']; |
||
614 | $questionId = $question['question_id']; |
||
615 | $newList = []; |
||
616 | foreach ($list as $child) { |
||
617 | $childQuestionId = $child['question_id']; |
||
618 | $optionId = $child['parent_option_id']; |
||
619 | $newList[$optionId][] = $childQuestionId; |
||
620 | } |
||
621 | |||
622 | if ('multipleresponse' === $type) { |
||
623 | $multiple = ''; |
||
624 | foreach ($newList as $optionId => $child) { |
||
625 | $multiple .= ' |
||
626 | $(\'input[name="question'.$questionId.'['.$optionId.']"]\').on("change", function() { |
||
627 | var isChecked= $(this).is(\':checked\'); |
||
628 | var checkedValue = $(this).val(); |
||
629 | if (isChecked) { |
||
630 | $.each(list, function(index, value) { |
||
631 | $(".with_parent_" + value).find("input").prop("checked", false); |
||
632 | }); |
||
633 | |||
634 | var questionId = $(this).val(); |
||
635 | var questionToShow = list[questionId]; |
||
636 | $(".with_parent_" + questionToShow).show(); |
||
637 | } else { |
||
638 | var checkedValue = list[checkedValue]; |
||
639 | } |
||
640 | }); |
||
641 | '; |
||
642 | } |
||
643 | |||
644 | $js = ' |
||
645 | <script> |
||
646 | $(function() { |
||
647 | var list = '.json_encode($newList).'; |
||
648 | '.$multiple.' |
||
649 | }); |
||
650 | </script>'; |
||
651 | |||
652 | return $js; |
||
653 | } |
||
654 | |||
655 | $js = ' |
||
656 | <script> |
||
657 | $(function() { |
||
658 | var list = '.json_encode($newList).'; |
||
659 | $("input[name=question'.$questionId.']").on("click", function() { |
||
660 | $.each(list, function(index, value) { |
||
661 | $.each(value, function(index, itemValue) { |
||
662 | $(".with_parent_" + itemValue).hide(); |
||
663 | $(".with_parent_" + itemValue).find("input").prop("checked", false); |
||
664 | $(".with_parent_only_hide_" + itemValue).hide(); |
||
665 | }); |
||
666 | }); |
||
667 | |||
668 | var questionId = $(this).val(); |
||
669 | var questionToShowList = list[questionId]; |
||
670 | $.each(questionToShowList, function(index, value) { |
||
671 | $(".with_parent_" + value).show(); |
||
672 | }); |
||
673 | }); |
||
674 | }); |
||
675 | </script>'; |
||
676 | |||
677 | return $js; |
||
678 | } |
||
679 | |||
680 | /** |
||
681 | * Returns the (children) questions that have the given question as parent. |
||
682 | * |
||
683 | * @param array $question An array describing the parent question |
||
684 | * |
||
685 | * @return array The questions that have the given question as parent |
||
686 | */ |
||
687 | public static function getDependency($question) |
||
688 | { |
||
689 | if (true !== api_get_configuration_value('survey_question_dependency')) { |
||
690 | return []; |
||
691 | } |
||
692 | $table = Database::get_course_table(TABLE_SURVEY_QUESTION); |
||
693 | $questionId = $question['question_id']; |
||
694 | $courseId = api_get_course_int_id(); |
||
695 | |||
696 | // Getting the information of the question |
||
697 | $sql = "SELECT * FROM $table |
||
698 | WHERE c_id = $courseId AND parent_id = $questionId "; |
||
699 | $result = Database::query($sql); |
||
700 | $row = Database::store_result($result, 'ASSOC'); |
||
701 | |||
702 | return $row; |
||
703 | } |
||
704 | |||
705 | /** |
||
706 | * This method is not implemented at this level (returns null). |
||
707 | * |
||
708 | * @param array $questionData |
||
709 | * @param array $answers |
||
710 | */ |
||
711 | public function render(FormValidator $form, $questionData = [], $answers = []) |
||
714 | } |
||
715 | } |
||
716 |