ch_yesno.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ch_yesno.
  5. */
  6. class ch_yesno 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. // The options
  23. $config = [
  24. 'ToolbarSet' => 'Survey',
  25. 'Width' => '100%',
  26. 'Height' => '120',
  27. ];
  28. $this->getForm()->addHtmlEditor(
  29. 'answers[0]',
  30. get_lang('AnswerOptions'),
  31. true,
  32. false,
  33. $config
  34. );
  35. $this->getForm()->addHtmlEditor(
  36. 'answers[1]',
  37. null,
  38. true,
  39. false,
  40. $config
  41. );
  42. }
  43. /**
  44. * @param FormValidator $form
  45. * @param array $questionData
  46. * @param array $answers
  47. */
  48. public function render(FormValidator $form, $questionData = [], $answers = null)
  49. {
  50. if (is_array($questionData['options'])) {
  51. $class = 'radio-inline';
  52. $labelClass = 'radio-inline';
  53. if ($questionData['display'] == 'vertical') {
  54. $class = 'radio-vertical';
  55. }
  56. $name = 'question'.$questionData['question_id'];
  57. $radioAttributes = ['radio-class' => $class, 'label-class' => $labelClass];
  58. if (!empty($questionData['is_required'])) {
  59. $radioAttributes['required'] = 'required';
  60. }
  61. $form->addRadio(
  62. $name,
  63. null,
  64. $questionData['options'],
  65. $radioAttributes
  66. );
  67. if (!empty($answers)) {
  68. $form->setDefaults([$name => is_array($answers) ? current($answers) : $answers]);
  69. }
  70. }
  71. }
  72. }