multiple_answer.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * @inheritdoc
  28. */
  29. public function createAnswersForm($form)
  30. {
  31. $editorConfig = array(
  32. 'ToolbarSet' => 'TestProposedAnswer',
  33. 'Width' => '100%',
  34. 'Height' => '125'
  35. );
  36. $nb_answers = isset($_POST['nb_answers']) ? $_POST['nb_answers'] : 4; // The previous default value was 2. See task #1759.
  37. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  38. $obj_ex = Session::read('objExercise');
  39. $form->addHeader(get_lang('Answers'));
  40. $html = '<table class="table table-striped table-hover">
  41. <thead>
  42. <tr>
  43. <th width="10">' . get_lang('Number').'</th>
  44. <th width="10">' . get_lang('True').'</th>
  45. <th width="50%">' . get_lang('Answer').'</th>
  46. <th width="50%">' . get_lang('Comment').'</th>
  47. <th width="10">' . get_lang('Weighting').'</th>
  48. </tr>
  49. </thead>
  50. <tbody>';
  51. $form->addHtml($html);
  52. $defaults = array();
  53. $correct = 0;
  54. $answer = false;
  55. if (!empty($this->id)) {
  56. $answer = new Answer($this->id);
  57. $answer->read();
  58. if (count($answer->nbrAnswers) > 0 && !$form->isSubmitted()) {
  59. $nb_answers = $answer->nbrAnswers;
  60. }
  61. }
  62. $form->addElement('hidden', 'nb_answers');
  63. $boxes_names = array();
  64. if ($nb_answers < 1) {
  65. $nb_answers = 1;
  66. echo Display::return_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  67. }
  68. for ($i = 1; $i <= $nb_answers; ++$i) {
  69. $form->addHtml('<tr>');
  70. if (is_object($answer)) {
  71. $defaults['answer['.$i.']'] = $answer->answer[$i];
  72. $defaults['comment['.$i.']'] = $answer->comment[$i];
  73. $defaults['weighting['.$i.']'] = float_format($answer->weighting[$i], 1);
  74. $defaults['correct['.$i.']'] = $answer->correct[$i];
  75. } else {
  76. $defaults['answer[1]'] = get_lang('DefaultMultipleAnswer2');
  77. $defaults['comment[1]'] = get_lang('DefaultMultipleComment2');
  78. $defaults['correct[1]'] = true;
  79. $defaults['weighting[1]'] = 10;
  80. $defaults['answer[2]'] = get_lang('DefaultMultipleAnswer1');
  81. $defaults['comment[2]'] = get_lang('DefaultMultipleComment1');
  82. $defaults['correct[2]'] = false;
  83. $defaults['weighting[2]'] = -5;
  84. }
  85. $renderer = & $form->defaultRenderer();
  86. $renderer->setElementTemplate(
  87. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  88. 'correct['.$i.']'
  89. );
  90. $renderer->setElementTemplate(
  91. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  92. 'counter['.$i.']'
  93. );
  94. $renderer->setElementTemplate(
  95. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  96. 'answer['.$i.']'
  97. );
  98. $renderer->setElementTemplate(
  99. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  100. 'comment['.$i.']'
  101. );
  102. $renderer->setElementTemplate(
  103. '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
  104. 'weighting['.$i.']'
  105. );
  106. $answer_number = $form->addElement('text', 'counter['.$i.']', null, 'value="'.$i.'"');
  107. $answer_number->freeze();
  108. $form->addElement(
  109. 'checkbox',
  110. 'correct['.$i.']',
  111. null,
  112. null,
  113. 'class="checkbox" style="margin-left: 0em;"'
  114. );
  115. $boxes_names[] = 'correct['.$i.']';
  116. $form->addHtmlEditor("answer[$i]", null, null, true, $editorConfig);
  117. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  118. $form->addHtmlEditor("comment[$i]", null, null, true, $editorConfig);
  119. $form->addElement('text', 'weighting['.$i.']', null, array('style' => "width: 60px;", 'value' => '0'));
  120. $form->addHtml('</tr>');
  121. }
  122. $form->addHtml('</tbody>');
  123. $form->addHtml('</table>');
  124. $form->add_multiple_required_rule(
  125. $boxes_names,
  126. get_lang('ChooseAtLeastOneCheckbox'),
  127. 'multiple_required'
  128. );
  129. $buttonGroup = [];
  130. global $text;
  131. if ($obj_ex->edit_exercise_in_lp == true) {
  132. // setting the save button here and not in the question class.php
  133. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  134. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  135. $buttonGroup[] = $form->addButton(
  136. 'submitQuestion',
  137. $text,
  138. 'check',
  139. 'primary',
  140. 'default',
  141. null,
  142. ['id' => 'submit-question'],
  143. true
  144. );
  145. }
  146. $form->addGroup($buttonGroup);
  147. $defaults['correct'] = $correct;
  148. if (!empty($this->id)) {
  149. $form->setDefaults($defaults);
  150. } else {
  151. if ($this->isContent == 1) {
  152. $form->setDefaults($defaults);
  153. }
  154. }
  155. $form->setConstants(array('nb_answers' => $nb_answers));
  156. }
  157. /**
  158. * @inheritdoc
  159. */
  160. public function processAnswersCreation($form, $exercise)
  161. {
  162. $questionWeighting = $nbrGoodAnswers = 0;
  163. $objAnswer = new Answer($this->id);
  164. $nb_answers = $form->getSubmitValue('nb_answers');
  165. for ($i = 1; $i <= $nb_answers; $i++) {
  166. $answer = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('answer['.$i.']')));
  167. $comment = trim(str_replace(['<p>', '</p>'], '', $form->getSubmitValue('comment['.$i.']')));
  168. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  169. $goodAnswer = trim($form -> getSubmitValue('correct['.$i.']'));
  170. if ($goodAnswer) {
  171. $weighting = abs($weighting);
  172. } else {
  173. $weighting = abs($weighting);
  174. $weighting = -$weighting;
  175. }
  176. if ($weighting > 0) {
  177. $questionWeighting += $weighting;
  178. }
  179. $objAnswer->createAnswer(
  180. $answer,
  181. $goodAnswer,
  182. $comment,
  183. $weighting,
  184. $i
  185. );
  186. }
  187. // saves the answers into the data base
  188. $objAnswer -> save();
  189. // sets the total weighting of the question
  190. $this->updateWeighting($questionWeighting);
  191. $this->save($exercise);
  192. }
  193. /**
  194. * @inheritdoc
  195. */
  196. public function return_header($exercise, $counter = null, $score = null)
  197. {
  198. $header = parent::return_header($exercise, $counter, $score);
  199. $header .= '<table class="'.$this->question_table_class .'">
  200. <tr>
  201. <th>'.get_lang("Choice").'</th>
  202. <th>'. get_lang("ExpectedChoice").'</th>
  203. <th>'. get_lang("Answer").'</th>';
  204. $header .= '<th>'.get_lang("Status").'</th>';
  205. $header .= '<th>'.get_lang("Comment").'</th>';
  206. $header .= '</tr>';
  207. return $header;
  208. }
  209. }