ch_score.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 array $formData
  11. */
  12. public function createForm($survey_data, $formData)
  13. {
  14. parent::createForm($survey_data, $formData);
  15. $this->getForm()->addText('maximum_score', get_lang('Score'));
  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. }