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