multiple_answer_combination.class.php 8.0 KB

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