multiple_answer.class.php 7.5 KB

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