unique_answer_no_option.class.php 15 KB

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