ch_score.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ch_score.
  5. */
  6. class ch_score extends survey_question
  7. {
  8. /**
  9. * @param array $survey_data
  10. * @param $form_content
  11. */
  12. public function createForm($survey_data, $formData)
  13. {
  14. parent::createForm($survey_data, $formData);
  15. $this->getForm()->addText('maximum_score', get_lang('MaximumScore'));
  16. $config = ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120'];
  17. if (is_array($formData['answers'])) {
  18. foreach ($formData['answers'] as $key => $value) {
  19. $this->getForm()->addHtmlEditor(
  20. 'answers['.$key.']',
  21. null,
  22. false,
  23. false,
  24. $config
  25. );
  26. }
  27. }
  28. parent::addRemoveButtons($formData);
  29. }
  30. /**
  31. * @param FormValidator $form
  32. * @param array $questionData
  33. * @param array $answers
  34. */
  35. public function render(FormValidator $form, $questionData = [], $answers = [])
  36. {
  37. $defaults = [];
  38. foreach ($questionData['options'] as $key => &$value) {
  39. $options = [
  40. '--' => '--',
  41. ];
  42. for ($i = 1; $i <= $questionData['maximum_score']; $i++) {
  43. $options[$i] = $i;
  44. }
  45. $name = 'question'.$questionData['question_id'].'['.$key.']';
  46. $form->addSelect(
  47. $name,
  48. $value,
  49. $options
  50. );
  51. if (!empty($answers)) {
  52. if (in_array($key, array_keys($answers))) {
  53. $defaults[$name] = $answers[$key];
  54. }
  55. }
  56. }
  57. if (!empty($defaults)) {
  58. $form->setDefaults($defaults);
  59. }
  60. }
  61. }