multiple_answer.class.php 8.2 KB

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