ch_score.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 = array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120');
  17. if (is_array($formData['answers'])) {
  18. foreach ($formData['answers'] as $key => $value) {
  19. $this->getForm()->addHtmlEditor('answers['.$key.']', null, false, false, $config);
  20. }
  21. }
  22. parent::addRemoveButtons($formData);
  23. }
  24. /**
  25. * @param FormValidator $form
  26. * @param array $questionData
  27. * @param array $answers
  28. */
  29. public function render(FormValidator $form, $questionData = array(), $answers = array())
  30. {
  31. $defaults = [];
  32. foreach ($questionData['options'] as $key => & $value) {
  33. $options = array();
  34. for ($i=1; $i <= $questionData['maximum_score']; $i++) {
  35. $options[$i] = $i;
  36. }
  37. $name = 'question'.$questionData['question_id'].'['.$key.']';
  38. $form->addSelect(
  39. $name, $value, $options
  40. );
  41. if (!empty($answers)) {
  42. if (in_array($key, array_keys($answers))) {
  43. $defaults[$name] = $answers[$key];
  44. }
  45. }
  46. }
  47. if (!empty($defaults)) {
  48. $form->setDefaults($defaults);
  49. }
  50. }
  51. }