multiple_answer_combination.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. public function __construct()
  20. {
  21. parent::__construct();
  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. global $text, $class;
  129. //ie6 fix
  130. if ($obj_ex->edit_exercise_in_lp == true) {
  131. // setting the save button here and not in the question class.php
  132. $buttonGroup = [
  133. $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true),
  134. $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true),
  135. $form->addButtonSave($text, 'submitQuestion', true)
  136. ];
  137. $form->addGroup($buttonGroup);
  138. }
  139. $defaults['correct'] = $correct;
  140. if (!empty($this->id)) {
  141. $form->setDefaults($defaults);
  142. } else {
  143. if ($this->isContent == 1) {
  144. $form->setDefaults($defaults);
  145. }
  146. }
  147. $form->setConstants(array('nb_answers' => $nb_answers));
  148. }
  149. /**
  150. * abstract function which creates the form to create / edit the answers of the question
  151. * @param the formvalidator instance
  152. * @param the answers number to display
  153. */
  154. 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. {
  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. {
  177. $questionWeighting += $weighting;
  178. }
  179. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  180. }
  181. // saves the answers into the data base
  182. $objAnswer -> save();
  183. // sets the total weighting of the question
  184. $this -> updateWeighting($questionWeighting);
  185. $this -> save();
  186. }
  187. function return_header($feedback_type = null, $counter = null, $score = null)
  188. {
  189. $header = parent::return_header($feedback_type, $counter, $score);
  190. $header .= '<table class="'.$this->question_table_class .'">
  191. <tr>
  192. <th>'.get_lang("Choice").'</th>
  193. <th>'. get_lang("ExpectedChoice").'</th>
  194. <th>'. get_lang("Answer").'</i></th>';
  195. $header .= '<th>'.get_lang("Comment").'</th>';
  196. $header .= '</tr>';
  197. return $header;
  198. }
  199. }