multiple_answer_combination.class.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class MultipleAnswer
  5. *
  6. * This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  7. * extending the class question
  8. *
  9. * @author Eric Marguin
  10. * @package chamilo.exercise
  11. **/
  12. class MultipleAnswerCombination extends Question
  13. {
  14. static $typePicture = 'mcmac.png';
  15. static $explanationLangVar = 'MultipleSelectCombination';
  16. /**
  17. * Constructor
  18. */
  19. function MultipleAnswerCombination()
  20. {
  21. parent::question();
  22. $this -> type = MULTIPLE_ANSWER_COMBINATION;
  23. $this -> isContent = $this-> getIsContent();
  24. }
  25. /**
  26. * function which redifines Question::createAnswersForm
  27. * @param $form FormValidator
  28. * @param the answers number to display
  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. //only 1 answer the all deal ...
  121. //$form->addElement('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="10"');
  122. $form->addHtml('</tr>');
  123. }
  124. $form->addElement('html', '</tbody></table>');
  125. $form->add_multiple_required_rule($boxes_names, get_lang('ChooseAtLeastOneCheckbox'), 'multiple_required');
  126. //only 1 answer the all deal ...
  127. $form->addText('weighting[1]', get_lang('Score'), false, ['value' => 10]);
  128. $navigator_info = api_get_navigator();
  129. global $text, $class;
  130. //ie6 fix
  131. if ($obj_ex->edit_exercise_in_lp == true) {
  132. $buttonGroup = [];
  133. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  134. $buttonGroup[] = $form->createElement(
  135. 'submit',
  136. 'lessAnswers',
  137. '<i class="fa fa-minus"></i> '.get_lang('LessAnswer'),
  138. 'class="btn btn-default"'
  139. );
  140. $buttonGroup[] = $form->createElement(
  141. 'submit',
  142. 'moreAnswers',
  143. '<i class="fa fa-plus"></i> '.get_lang('PlusAnswer'),
  144. 'class="btn btn-default"'
  145. );
  146. $buttonGroup[] = $form->createElement(
  147. 'submit',
  148. 'submitQuestion',
  149. $text,
  150. 'class="' . $class . '"'
  151. );
  152. } else {
  153. // setting the save button here and not in the question class.php
  154. $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
  155. $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
  156. $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
  157. }
  158. $form->addGroup($buttonGroup);
  159. }
  160. $defaults['correct'] = $correct;
  161. if (!empty($this->id)) {
  162. $form->setDefaults($defaults);
  163. } else {
  164. if ($this->isContent == 1) {
  165. $form->setDefaults($defaults);
  166. }
  167. }
  168. $form->setConstants(array('nb_answers' => $nb_answers));
  169. }
  170. /**
  171. * abstract function which creates the form to create / edit the answers of the question
  172. * @param the formvalidator instance
  173. * @param the answers number to display
  174. */
  175. function processAnswersCreation($form)
  176. {
  177. $questionWeighting = $nbrGoodAnswers = 0;
  178. $objAnswer = new Answer($this->id);
  179. $nb_answers = $form -> getSubmitValue('nb_answers');
  180. for($i=1 ; $i <= $nb_answers ; $i++)
  181. {
  182. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  183. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  184. if ($i == 1)
  185. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  186. else {
  187. $weighting = 0;
  188. }
  189. $goodAnswer = trim($form -> getSubmitValue('correct['.$i.']'));
  190. if($goodAnswer){
  191. $weighting = abs($weighting);
  192. } else {
  193. $weighting = abs($weighting);
  194. // $weighting = -$weighting;
  195. }
  196. if($weighting > 0)
  197. {
  198. $questionWeighting += $weighting;
  199. }
  200. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  201. }
  202. // saves the answers into the data base
  203. $objAnswer -> save();
  204. // sets the total weighting of the question
  205. $this -> updateWeighting($questionWeighting);
  206. $this -> save();
  207. }
  208. function return_header($feedback_type = null, $counter = null, $score = null)
  209. {
  210. $header = parent::return_header($feedback_type, $counter, $score);
  211. $header .= '<table class="'.$this->question_table_class .'">
  212. <tr>
  213. <th>'.get_lang("Choice").'</th>
  214. <th>'. get_lang("ExpectedChoice").'</th>
  215. <th>'. get_lang("Answer").'</i></th>';
  216. $header .= '<th>'.get_lang("Comment").'</th>';
  217. $header .= '</tr>';
  218. return $header;
  219. }
  220. }