unique_answer_no_option.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class UniqueAnswerNoOption
  5. * Allows to instantiate an object of type UNIQUE_ANSWER (MULTIPLE CHOICE, UNIQUE ANSWER),
  6. * extending the class question
  7. * @author Eric Marguin
  8. * @author Julio Montoya
  9. * @package chamilo.exercise
  10. *
  11. */
  12. class UniqueAnswerNoOption extends Question
  13. {
  14. static $typePicture = 'mcuao.png';
  15. static $explanationLangVar = 'UniqueAnswerNoOption';
  16. /**
  17. * Constructor
  18. */
  19. function UniqueAnswerNoOption()
  20. {
  21. //this is highly important
  22. parent::question();
  23. $this -> type = UNIQUE_ANSWER_NO_OPTION;
  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. {
  33. // getting the exercise list
  34. $obj_ex = $_SESSION['objExercise'];
  35. $editor_config = array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '125');
  36. //this line define how many question by default appear when creating a choice question
  37. $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 3; // The previous default value was 2. See task #1759.
  38. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  39. /*
  40. Types of Feedback
  41. $feedback_option[0]=get_lang('Feedback');
  42. $feedback_option[1]=get_lang('DirectFeedback');
  43. $feedback_option[2]=get_lang('NoFeedback');
  44. */
  45. $feedback_title='';
  46. $comment_title='';
  47. if ($obj_ex->selectFeedbackType()==1) {
  48. $editor_config['Width'] = '250';
  49. $editor_config['Height'] = '110';
  50. $comment_title = '<th width="500" >'.get_lang('Comment').'</th>';
  51. $feedback_title = '<th width="350px" >'.get_lang('Scenario').'</th>';
  52. }
  53. else {
  54. $comment_title = '<th>'.get_lang('Comment').'</th>';
  55. }
  56. $html='<table class="data_table">
  57. <tr style="text-align: center;">
  58. <th width="10px">
  59. '.get_lang('Number').'
  60. </th>
  61. <th width="10px" >
  62. '.get_lang('True').'
  63. </th>
  64. <th width="50%">
  65. '.get_lang('Answer').'
  66. </th>
  67. '.$comment_title.'
  68. '.$feedback_title.'
  69. <th width="60px">
  70. '.get_lang('Weighting').'
  71. </th>
  72. </tr>';
  73. $form -> addElement ('label', get_lang('Answers').'<br /> <img src="../img/fill_field.png">', $html);
  74. $defaults = array();
  75. $correct = 0;
  76. $answer = false;
  77. if (!empty($this -> id)) {
  78. $answer = new Answer($this -> id);
  79. $answer -> read();
  80. if (count($answer->nbrAnswers)>0 && !$form->isSubmitted()) {
  81. $nb_answers = $answer->nbrAnswers;
  82. }
  83. }
  84. $temp_scenario = array();
  85. if ($nb_answers < 1) {
  86. $nb_answers = 1;
  87. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  88. }
  89. $editQuestion = isset($_GET['editQuestion']) ? $_GET['editQuestion'] : false;
  90. if ($editQuestion) {
  91. //fixing $nb_answers
  92. $new_list = array();
  93. $count = 1;
  94. if (isset($_POST['lessAnswers'])) {
  95. if (!isset($_SESSION['less_answer'])) {
  96. $_SESSION['less_answer'] = $this -> id;
  97. $nb_answers--;
  98. }
  99. }
  100. for ($k = 1 ; $k <= $nb_answers; ++$k) {
  101. if ($answer->position[$k] != '666') {
  102. $new_list[$count] = $count;
  103. $count++;
  104. }
  105. }
  106. } else {
  107. for ($k = 1 ; $k <= $nb_answers; ++$k) {
  108. $new_list[$k] = $k;
  109. }
  110. }
  111. $i = 1;
  112. //for ($k = 1 ; $k <= $real_nb_answers; $k++) {
  113. foreach ($new_list as $key) {
  114. $i = $key;
  115. $form -> addElement ('html', '<tr>');
  116. if (is_object($answer)) {
  117. if($answer->position[$i] == 666) {
  118. //we set nothing
  119. } else {
  120. if ($answer->correct[$i]) {
  121. $correct = $i;
  122. }
  123. $answer_result = $answer->answer[$i];
  124. $weight_result = float_format($answer->weighting[$i], 1);
  125. if ($nb_answers == $i) {
  126. $weight_result = '0';
  127. }
  128. $defaults['answer['.$i.']'] = $answer_result;
  129. $defaults['comment['.$i.']'] = $answer->comment[$i];
  130. $defaults['weighting['.$i.']'] = $weight_result;
  131. $item_list=explode('@@',$answer -> destination[$i]);
  132. $try = $item_list[0];
  133. $lp = $item_list[1];
  134. $list_dest = $item_list[2];
  135. $url = $item_list[3];
  136. if ($try==0)
  137. $try_result=0;
  138. else
  139. $try_result=1;
  140. if ($url==0)
  141. $url_result='';
  142. else
  143. $url_result=$url;
  144. $temp_scenario['url'.$i] = $url_result;
  145. $temp_scenario['try'.$i] = $try_result;
  146. $temp_scenario['lp'.$i] = $lp;
  147. $temp_scenario['destination'.$i]= $list_dest;
  148. }
  149. }
  150. $defaults['scenario']=$temp_scenario;
  151. $renderer = & $form->defaultRenderer();
  152. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'correct');
  153. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'counter['.$i.']');
  154. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'answer['.$i.']');
  155. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'comment['.$i.']');
  156. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'weighting['.$i.']');
  157. $answer_number=$form->addElement('text', 'counter['.$i.']', null,'value="'.$i.'"');
  158. $answer_number->freeze();
  159. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  160. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  161. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  162. $form->addElement('text', 'weighting['.$i.']', null, array('class' => "span1", 'value' => '0'));
  163. $form->addElement('html', '</tr>');
  164. $i++;
  165. }
  166. if (empty($this -> id)) {
  167. $form->addElement('hidden', 'new_question', 1);
  168. }
  169. //Adding the "I don't know" question answer
  170. //if (empty($this -> id)) {
  171. $i = 666;
  172. $form -> addElement ('html', '<tr>');
  173. $defaults['answer['.$i.']'] = get_lang('DontKnow');
  174. $defaults['weighting['.$i.']'] = 0;
  175. $defaults['scenario']=$temp_scenario;
  176. $renderer = & $form->defaultRenderer();
  177. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'correct');
  178. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'counter['.$i.']');
  179. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'answer['.$i.']');
  180. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'comment['.$i.']');
  181. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'weighting['.$i.']');
  182. $answer_number=$form->addElement('text', 'counter['.$i.']',null,'value="-"');
  183. $answer_number->freeze();
  184. $form->addElement('hidden', 'position['.$i.']', '666');
  185. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  186. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  187. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  188. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  189. //$form->addElement('select', 'destination'.$i, get_lang('SelectQuestion').' : ',$select_question,'multiple');
  190. $form->addElement('text', 'weighting['.$i.']', null, array('class' => "span1", 'value' => '0', 'readonly' =>'readonly'));
  191. $form->addElement ('html', '</tr>');
  192. $form -> addElement ('html', '</table>');
  193. $form -> addElement ('html', '<br />');
  194. $navigator_info = api_get_navigator();
  195. global $text, $class;
  196. //ie6 fix
  197. if ($obj_ex->edit_exercise_in_lp == true) {
  198. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  199. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  200. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  201. $form->addElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  202. } else {
  203. //setting the save button here and not in the question class.php
  204. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  205. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  206. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  207. }
  208. }
  209. $renderer->setElementTemplate('{element}&nbsp;','submitQuestion');
  210. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  211. $renderer->setElementTemplate('{element}&nbsp;','moreAnswers');
  212. $form -> addElement ('html', '</div></div>');
  213. //We check the first radio button to be sure a radio button will be check
  214. if ($correct==0) {
  215. $correct=1;
  216. }
  217. $defaults['correct'] = $correct;
  218. if (!empty($this -> id)) {
  219. $form -> setDefaults($defaults);
  220. } else {
  221. $form -> setDefaults($defaults);
  222. }
  223. $form->addElement('hidden', 'nb_answers');
  224. $form->setConstants(array('nb_answers' => $nb_answers));
  225. }
  226. /**
  227. * Function which creates the form to create / edit the answers of the question
  228. * @param the formvalidator instance
  229. * @param the answers number to display
  230. */
  231. function processAnswersCreation($form)
  232. {
  233. $questionWeighting = $nbrGoodAnswers = 0;
  234. $correct = $form -> getSubmitValue('correct');
  235. $objAnswer = new Answer($this->id);
  236. $nb_answers = $form -> getSubmitValue('nb_answers');
  237. $minus = 1;
  238. if ($form -> getSubmitValue('new_question')) {
  239. $minus = 0;
  240. }
  241. for ($i=1 ; $i <= $nb_answers - $minus; $i++) {
  242. $position = trim($form -> getSubmitValue('position['.$i.']'));
  243. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  244. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  245. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  246. $scenario = $form -> getSubmitValue('scenario');
  247. //$list_destination = $form -> getSubmitValue('destination'.$i);
  248. //$destination_str = $form -> getSubmitValue('destination'.$i);
  249. $try = $scenario['try'.$i];
  250. $lp = $scenario['lp'.$i];
  251. $destination = $scenario['destination'.$i];
  252. $url = trim($scenario['url'.$i]);
  253. /*
  254. How we are going to parse the destination value
  255. here we parse the destination value which is a string
  256. 1@@3@@2;4;4;@@http://www.chamilo.org
  257. where: try_again@@lp_id@@selected_questions@@url
  258. try_again = is 1 || 0
  259. lp_id = id of a learning path (0 if dont select)
  260. selected_questions= ids of questions
  261. url= an url
  262. */
  263. /*
  264. $destination_str='';
  265. foreach ($list_destination as $destination_id)
  266. {
  267. $destination_str.=$destination_id.';';
  268. }*/
  269. $goodAnswer= ($correct == $i) ? true : false;
  270. if ($goodAnswer) {
  271. $nbrGoodAnswers++;
  272. $weighting = abs($weighting);
  273. if($weighting > 0) {
  274. $questionWeighting += $weighting;
  275. }
  276. }
  277. if (empty($try))
  278. $try=0;
  279. if (empty($lp)) {
  280. $lp=0;
  281. }
  282. if (empty($destination)) {
  283. $destination=0;
  284. }
  285. if ($url=='') {
  286. $url=0;
  287. }
  288. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  289. $dest= $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  290. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  291. }
  292. //Create 666 answer
  293. $i = 666;
  294. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  295. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  296. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  297. $goodAnswer= ($correct == $i) ? true : false;
  298. $dest = '';
  299. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  300. // saves the answers into the data base
  301. $objAnswer -> save();
  302. // sets the total weighting of the question
  303. $this -> updateWeighting($questionWeighting);
  304. $this -> save();
  305. }
  306. function return_header($feedback_type = null, $counter = null, $score = null)
  307. {
  308. $header = parent::return_header($feedback_type, $counter, $score);
  309. $header .= '<table class="'.$this->question_table_class .'">
  310. <tr>
  311. <th>'.get_lang("Choice").'</th>
  312. <th>'. get_lang("ExpectedChoice").'</th>
  313. <th>'. get_lang("Answer").'</th>';
  314. $header .= '<th>'.get_lang("Comment").'</th>';
  315. $header .= '</tr>';
  316. return $header;
  317. }
  318. }