multiple_answer.class.php 7.7 KB

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