multiple_answer.class.php 7.7 KB

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