ch_score.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. '--' => '--'
  35. );
  36. for ($i = 1; $i <= $questionData['maximum_score']; $i++) {
  37. $options[$i] = $i;
  38. }
  39. $name = 'question'.$questionData['question_id'].'['.$key.']';
  40. $form->addSelect(
  41. $name, $value, $options
  42. );
  43. if (!empty($answers)) {
  44. if (in_array($key, array_keys($answers))) {
  45. $defaults[$name] = $answers[$key];
  46. }
  47. }
  48. }
  49. if (!empty($defaults)) {
  50. $form->setDefaults($defaults);
  51. }
  52. }
  53. }