multiple_answer.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class MultipleAnswer
  5. *
  6. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  7. * extending the class question
  8. *
  9. * @author Eric Marguin
  10. * @package chamilo.exercise
  11. **/
  12. class MultipleAnswer extends Question
  13. {
  14. static $typePicture = 'mcma.png';
  15. static $explanationLangVar = 'MultipleSelect';
  16. /**
  17. * Constructor
  18. */
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this -> type = MULTIPLE_ANSWER;
  23. $this -> isContent = $this-> getIsContent();
  24. }
  25. /**
  26. * function which redifines Question::createAnswersForm
  27. * @param the formvalidator instance
  28. * @param the answers number to display
  29. */
  30. function createAnswersForm($form)
  31. {
  32. $editorConfig = array(
  33. 'ToolbarSet' => 'TestProposedAnswer',
  34. 'Width' => '100%',
  35. 'Height' => '125'
  36. );
  37. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4; // The previous default value was 2. See task #1759.
  38. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  39. $obj_ex = $_SESSION['objExercise'];
  40. $form->addHeader(get_lang('Answers'));
  41. $html = '<table class="table table-striped table-hover">
  42. <thead>
  43. <tr>
  44. <th width="10">' . get_lang('Number') . '</th>
  45. <th width="10">' . get_lang('True') . '</th>
  46. <th width="50%">' . get_lang('Answer') . '</th>
  47. <th width="50%">' . get_lang('Comment') . '</th>
  48. <th width="10">' . get_lang('Weighting') . '</th>
  49. </tr>
  50. </thead>
  51. <tbody>';
  52. $form->addHtml($html);
  53. $defaults = array();
  54. $correct = 0;
  55. $answer = false;
  56. if (!empty($this->id)) {
  57. $answer = new Answer($this->id);
  58. $answer->read();
  59. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  60. $nb_answers = $answer->nbrAnswers;
  61. }
  62. }
  63. $form->addElement('hidden', 'nb_answers');
  64. $boxes_names = array();
  65. if ($nb_answers < 1) {
  66. $nb_answers = 1;
  67. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  68. }
  69. for ($i = 1; $i <= $nb_answers; ++$i) {
  70. $form->addHtml('<tr>');
  71. if (is_object($answer)) {
  72. $defaults['answer[' . $i . ']'] = $answer->answer[$i];
  73. $defaults['comment[' . $i . ']'] = $answer->comment[$i];
  74. $defaults['weighting[' . $i . ']'] = float_format($answer->weighting[$i], 1);
  75. $defaults['correct[' . $i . ']'] = $answer->correct[$i];
  76. } else {
  77. $defaults['answer[1]'] = get_lang('DefaultMultipleAnswer2');
  78. $defaults['comment[1]'] = get_lang('DefaultMultipleComment2');
  79. $defaults['correct[1]'] = true;
  80. $defaults['weighting[1]'] = 10;
  81. $defaults['answer[2]'] = get_lang('DefaultMultipleAnswer1');
  82. $defaults['comment[2]'] = get_lang('DefaultMultipleComment1');
  83. $defaults['correct[2]'] = false;
  84. $defaults['weighting[2]'] = -5;
  85. }
  86. $renderer = & $form->defaultRenderer();
  87. $renderer->setElementTemplate(
  88. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  89. 'correct[' . $i . ']'
  90. );
  91. $renderer->setElementTemplate(
  92. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  93. 'counter[' . $i . ']'
  94. );
  95. $renderer->setElementTemplate(
  96. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  97. 'answer[' . $i . ']'
  98. );
  99. $renderer->setElementTemplate(
  100. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  101. 'comment[' . $i . ']'
  102. );
  103. $renderer->setElementTemplate(
  104. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  105. 'weighting[' . $i . ']'
  106. );
  107. $answer_number = $form->addElement('text', 'counter[' . $i . ']', null, 'value="' . $i . '"');
  108. $answer_number->freeze();
  109. $form->addElement('checkbox', 'correct[' . $i . ']', null, null,
  110. 'class="checkbox" style="margin-left: 0em;"');
  111. $boxes_names[] = 'correct[' . $i . ']';
  112. $form->addHtmlEditor("answer[$i]", null, null, true, $editorConfig);
  113. $form->addRule('answer[' . $i . ']', get_lang('ThisFieldIsRequired'), 'required');
  114. $form->addHtmlEditor("comment[$i]", null, null, true, $editorConfig);
  115. $form->addElement('text', 'weighting[' . $i . ']', null, array('style' => "width: 60px;", 'value' => '0'));
  116. $form->addHtml('</tr>');
  117. }
  118. $form->addHtml('</tbody>');
  119. $form->addHtml('</table>');
  120. $form->add_multiple_required_rule($boxes_names, get_lang('ChooseAtLeastOneCheckbox'), 'multiple_required');
  121. $buttonGroup = [];
  122. global $text;
  123. if ($obj_ex->edit_exercise_in_lp == true) {
  124. // setting the save button here and not in the question class.php
  125. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  126. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  127. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  128. }
  129. $form->addGroup($buttonGroup);
  130. $defaults['correct'] = $correct;
  131. if (!empty($this->id)) {
  132. $form->setDefaults($defaults);
  133. } else {
  134. if ($this->isContent == 1) {
  135. $form->setDefaults($defaults);
  136. }
  137. }
  138. $form->setConstants(array('nb_answers' => $nb_answers));
  139. }
  140. /**
  141. * abstract function which creates the form to create / edit the answers of the question
  142. * @param the formvalidator instance
  143. * @param the answers number to display
  144. */
  145. function processAnswersCreation($form)
  146. {
  147. $questionWeighting = $nbrGoodAnswers = 0;
  148. $objAnswer = new Answer($this->id);
  149. $nb_answers = $form->getSubmitValue('nb_answers');
  150. for($i=1 ; $i <= $nb_answers ; $i++) {
  151. $answer = trim(str_replace(['<p>', '</p>'], '', $form -> getSubmitValue('answer['.$i.']')));
  152. $comment = trim(str_replace(['<p>', '</p>'], '', $form -> getSubmitValue('comment['.$i.']')));
  153. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  154. $goodAnswer = trim($form -> getSubmitValue('correct['.$i.']'));
  155. if ($goodAnswer) {
  156. $weighting = abs($weighting);
  157. } else {
  158. $weighting = abs($weighting);
  159. $weighting = -$weighting;
  160. }
  161. if($weighting > 0) {
  162. $questionWeighting += $weighting;
  163. }
  164. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  165. }
  166. // saves the answers into the data base
  167. $objAnswer -> save();
  168. // sets the total weighting of the question
  169. $this->updateWeighting($questionWeighting);
  170. $this->save();
  171. }
  172. function return_header($feedback_type = null, $counter = null, $score = null)
  173. {
  174. $header = parent::return_header($feedback_type, $counter, $score);
  175. $header .= '<table class="'.$this->question_table_class .'">
  176. <tr>
  177. <th>'.get_lang("Choice").'</th>
  178. <th>'. get_lang("ExpectedChoice").'</th>
  179. <th>'. get_lang("Answer").'</th>';
  180. $header .= '<th>'.get_lang("Comment").'</th>';
  181. $header .= '</tr>';
  182. return $header;
  183. }
  184. }