multiple_answer.class.php 8.0 KB

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