ch_multipleresponse.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ch_multipleresponse.
  5. */
  6. class ch_multipleresponse extends survey_question
  7. {
  8. /**
  9. * @param array $surveyData
  10. * @param array $formData
  11. */
  12. public function createForm($surveyData, $formData)
  13. {
  14. parent::createForm($surveyData, $formData);
  15. $options = [
  16. 'horizontal' => get_lang('Horizontal'),
  17. 'vertical' => get_lang('Vertical'),
  18. ];
  19. $this->getForm()->addRadio('horizontalvertical', get_lang('DisplayAnswersHorVert'), $options);
  20. $formData['horizontalvertical'] = isset($formData['horizontalvertical']) ? $formData['horizontalvertical'] : 'horizontal';
  21. $this->getForm()->setDefaults($formData);
  22. $config = ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120'];
  23. if (is_array($formData['answers'])) {
  24. foreach ($formData['answers'] as $key => $value) {
  25. $this->getForm()->addHtmlEditor(
  26. 'answers['.$key.']',
  27. null,
  28. false,
  29. false,
  30. $config
  31. );
  32. }
  33. }
  34. parent::addRemoveButtons($formData);
  35. }
  36. /**
  37. * @param FormValidator $form
  38. * @param array $questionData
  39. * @param array $answers
  40. */
  41. public function render(
  42. FormValidator $form,
  43. $questionData = [],
  44. $answers = []
  45. ) {
  46. $class = 'checkbox-inline';
  47. $labelClass = 'checkbox-inline';
  48. if ($questionData['display'] == 'vertical') {
  49. $class = 'checkbox-vertical';
  50. }
  51. $name = 'question'.$questionData['question_id'];
  52. $form->addCheckBoxGroup(
  53. $name,
  54. null,
  55. $questionData['options'],
  56. ['checkbox-class' => $class, 'label-class' => $labelClass]
  57. );
  58. $defaults = [];
  59. if (!empty($answers)) {
  60. foreach ($answers as $answer) {
  61. $defaults[$name.'['.$answer.']'] = true;
  62. }
  63. }
  64. $form->setDefaults($defaults);
  65. }
  66. }