multiple_answer_combination.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class MultipleAnswerCombination
  5. *
  6. * This class allows to instantiate an object of type
  7. * MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  8. * extending the class question
  9. *
  10. * @author Eric Marguin
  11. * @package chamilo.exercise
  12. **/
  13. class MultipleAnswerCombination extends Question
  14. {
  15. static $typePicture = 'mcmac.png';
  16. static $explanationLangVar = 'MultipleSelectCombination';
  17. /**
  18. * Constructor
  19. */
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this -> type = MULTIPLE_ANSWER_COMBINATION;
  24. $this -> isContent = $this-> getIsContent();
  25. }
  26. /**
  27. * function which redefines Question::createAnswersForm
  28. * @param FormValidator $form
  29. */
  30. 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['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('Comment') . '</th>';
  41. $html .= '<th width="50%">' . get_lang('Answer') . '</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. Display::display_normal_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('checkbox',
  99. 'correct[' . $i . ']',
  100. null,
  101. null,
  102. 'class="checkbox" style="margin-left: 0em;"'
  103. );
  104. $boxes_names[] = 'correct[' . $i . ']';
  105. $form->addElement(
  106. 'html_editor',
  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->addElement(
  114. 'html_editor',
  115. 'comment[' . $i . ']',
  116. null,
  117. array(),
  118. array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100')
  119. );
  120. $form->addHtml('</tr>');
  121. }
  122. $form->addElement('html', '</tbody></table>');
  123. $form->add_multiple_required_rule(
  124. $boxes_names,
  125. get_lang('ChooseAtLeastOneCheckbox'),
  126. 'multiple_required'
  127. );
  128. //only 1 answer the all deal ...
  129. $form->addText('weighting[1]', get_lang('Score'), false, ['value' => 10]);
  130. global $text;
  131. //ie6 fix
  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("Comment").'</th>';
  201. $header .= '</tr>';
  202. return $header;
  203. }
  204. }