multiple_answer_combination.class.php 8.2 KB

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