unique_answer_no_option.class.php 14 KB

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