multiple_answer_combination.class.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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="data_table">
  36. <tr style="text-align: center;">
  37. <th width="10px">
  38. '.get_lang('Number').'
  39. </th>
  40. <th width="10px">
  41. '.get_lang('True').'
  42. </th>
  43. <th width="50%">
  44. '.get_lang('Answer').'
  45. </th>';
  46. // show column comment when feedback is enable
  47. $html .='<th>'.get_lang('Comment').'</th>';
  48. $html .= '</tr>';
  49. $form -> addElement ('label', get_lang('Answers').'<br /> <img src="../img/fill_field.png">', $html);
  50. $defaults = array();
  51. $correct = 0;
  52. $answer = false;
  53. if(!empty($this -> id)) {
  54. $answer = new Answer($this -> id);
  55. $answer -> read();
  56. if (count($answer->nbrAnswers)>0 && !$form->isSubmitted()) {
  57. $nb_answers = $answer->nbrAnswers;
  58. }
  59. }
  60. $form -> addElement('hidden', 'nb_answers');
  61. $boxes_names = array();
  62. if ($nb_answers < 1) {
  63. $nb_answers = 1;
  64. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  65. }
  66. for($i = 1 ; $i <= $nb_answers ; ++$i) {
  67. if(is_object($answer)) {
  68. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  69. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  70. $defaults['weighting['.$i.']'] = float_format($answer -> weighting[$i], 1);
  71. $defaults['correct['.$i.']'] = $answer -> correct[$i];
  72. } else {
  73. $defaults['answer[1]'] = get_lang('DefaultMultipleAnswer2');
  74. $defaults['comment[1]'] = get_lang('DefaultMultipleComment2');
  75. $defaults['correct[1]'] = true;
  76. $defaults['weighting[1]'] = 10;
  77. $defaults['answer[2]'] = get_lang('DefaultMultipleAnswer1');
  78. $defaults['comment[2]'] = get_lang('DefaultMultipleComment1');
  79. $defaults['correct[2]'] = false;
  80. }
  81. $renderer = & $form->defaultRenderer();
  82. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'correct['.$i.']');
  83. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'counter['.$i.']');
  84. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'answer['.$i.']');
  85. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'comment['.$i.']');
  86. $answer_number=$form->addElement('text', 'counter['.$i.']', null,'value="'.$i.'"');
  87. $answer_number->freeze();
  88. $form->addElement('checkbox', 'correct['.$i.']', null, null, 'class="checkbox" style="margin-left: 0em;"');
  89. $boxes_names[] = 'correct['.$i.']';
  90. $form->addElement('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  91. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  92. $form->addElement('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"', array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '100'));
  93. //only 1 answer the all deal ...
  94. //$form->addElement('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="10"');
  95. $form -> addElement ('html', '</tr>');
  96. }
  97. $form -> addElement ('html', '</table>');
  98. $form -> addElement ('html', '<br />');
  99. $form -> add_multiple_required_rule ($boxes_names , get_lang('ChooseAtLeastOneCheckbox') , 'multiple_required');
  100. //only 1 answer the all deal ...
  101. $form->addElement('text', 'weighting[1]', get_lang('Score'), array('class' => "span1", 'value' => '10'));
  102. $navigator_info = api_get_navigator();
  103. global $text, $class;
  104. //ie6 fix
  105. if ($obj_ex->edit_exercise_in_lp == true) {
  106. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  107. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  108. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  109. $form->addElement('submit', 'submitQuestion', $text, 'class="'.$class.'"');
  110. } else {
  111. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  112. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  113. $form->addElement('style_submit_button', 'submitQuestion',$text, 'class="'.$class.'"');
  114. // setting the save button here and not in the question class.php
  115. }
  116. }
  117. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  118. $renderer->setElementTemplate('{element}&nbsp;','submitQuestion');
  119. $renderer->setElementTemplate('{element}&nbsp;','moreAnswers');
  120. $form -> addElement ('html', '</div></div>');
  121. $defaults['correct'] = $correct;
  122. if (!empty($this->id)) {
  123. $form -> setDefaults($defaults);
  124. } else {
  125. if ($this -> isContent == 1) {
  126. $form -> setDefaults($defaults);
  127. }
  128. }
  129. $form->setConstants(array('nb_answers' => $nb_answers));
  130. }
  131. /**
  132. * abstract function which creates the form to create / edit the answers of the question
  133. * @param the formvalidator instance
  134. * @param the answers number to display
  135. */
  136. function processAnswersCreation($form)
  137. {
  138. $questionWeighting = $nbrGoodAnswers = 0;
  139. $objAnswer = new Answer($this->id);
  140. $nb_answers = $form -> getSubmitValue('nb_answers');
  141. for($i=1 ; $i <= $nb_answers ; $i++)
  142. {
  143. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  144. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  145. if ($i == 1)
  146. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  147. else {
  148. $weighting = 0;
  149. }
  150. $goodAnswer = trim($form -> getSubmitValue('correct['.$i.']'));
  151. if($goodAnswer){
  152. $weighting = abs($weighting);
  153. } else {
  154. $weighting = abs($weighting);
  155. // $weighting = -$weighting;
  156. }
  157. if($weighting > 0)
  158. {
  159. $questionWeighting += $weighting;
  160. }
  161. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i);
  162. }
  163. // saves the answers into the data base
  164. $objAnswer -> save();
  165. // sets the total weighting of the question
  166. $this -> updateWeighting($questionWeighting);
  167. $this -> save();
  168. }
  169. function return_header($feedback_type = null, $counter = null, $score = null)
  170. {
  171. $header = parent::return_header($feedback_type, $counter, $score);
  172. $header .= '<table class="'.$this->question_table_class .'">
  173. <tr>
  174. <th>'.get_lang("Choice").'</th>
  175. <th>'. get_lang("ExpectedChoice").'</th>
  176. <th>'. get_lang("Answer").'</i></th>';
  177. $header .= '<th>'.get_lang("Comment").'</th>';
  178. $header .= '</tr>';
  179. return $header;
  180. }
  181. }