multiple_answer_combination.class.php 8.2 KB

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