| Total Complexity | 113 |
| Total Lines | 708 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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']) { |
||
| 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) |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Returns the (children) questions that have the given question as parent. |
||
| 686 | * |
||
| 687 | * @param array $question An array describing the parent question |
||
| 688 | * |
||
| 689 | * @return array The questions that have the given question as parent |
||
| 690 | */ |
||
| 691 | public static function getDependency($question) |
||
| 692 | { |
||
| 693 | if (true !== api_get_configuration_value('survey_question_dependency')) { |
||
| 694 | return []; |
||
| 695 | } |
||
| 696 | $table = Database::get_course_table(TABLE_SURVEY_QUESTION); |
||
| 697 | $questionId = $question['question_id']; |
||
| 698 | $courseId = api_get_course_int_id(); |
||
| 699 | |||
| 700 | // Getting the information of the question |
||
| 701 | $sql = "SELECT * FROM $table |
||
| 702 | WHERE c_id = $courseId AND parent_id = $questionId "; |
||
| 703 | $result = Database::query($sql); |
||
| 704 | $row = Database::store_result($result, 'ASSOC'); |
||
| 705 | |||
| 706 | return $row; |
||
| 707 | } |
||
| 708 | |||
| 709 | /** |
||
| 710 | * This method is not implemented at this level (returns null). |
||
| 711 | * |
||
| 712 | * @param array $questionData |
||
| 713 | * @param array $answers |
||
| 714 | */ |
||
| 715 | public function render(FormValidator $form, $questionData = [], $answers = []) |
||
| 718 | } |
||
| 719 | } |
||
| 720 |