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. 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='<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 /> <img src="../img/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. if ($answer->position[$k] != '666') {
  106. $new_list[$count] = $count;
  107. $count++;
  108. }
  109. }
  110. } else {
  111. for ($k = 1 ; $k <= $nb_answers; ++$k) {
  112. $new_list[$k] = $k;
  113. }
  114. }
  115. $i = 1;
  116. //for ($k = 1 ; $k <= $real_nb_answers; $k++) {
  117. foreach ($new_list as $key) {
  118. $i = $key;
  119. $form -> addElement ('html', '<tr>');
  120. if (is_object($answer)) {
  121. if($answer->position[$i] == 666) {
  122. //we set nothing
  123. } else {
  124. if ($answer->correct[$i]) {
  125. $correct = $i;
  126. }
  127. $answer_result = $answer->answer[$i];
  128. $weight_result = Text::float_format($answer->weighting[$i], 1);
  129. if ($nb_answers == $i) {
  130. $weight_result = '0';
  131. }
  132. $defaults['answer['.$i.']'] = $answer_result;
  133. $defaults['comment['.$i.']'] = $answer->comment[$i];
  134. $defaults['weighting['.$i.']'] = $weight_result;
  135. $item_list=explode('@@',$answer -> destination[$i]);
  136. $try = $item_list[0];
  137. $lp = $item_list[1];
  138. $list_dest = $item_list[2];
  139. $url = $item_list[3];
  140. if ($try==0)
  141. $try_result=0;
  142. else
  143. $try_result=1;
  144. if ($url==0)
  145. $url_result='';
  146. else
  147. $url_result=$url;
  148. $temp_scenario['url'.$i] = $url_result;
  149. $temp_scenario['try'.$i] = $try_result;
  150. $temp_scenario['lp'.$i] = $lp;
  151. $temp_scenario['destination'.$i]= $list_dest;
  152. }
  153. }
  154. $defaults['scenario']=$temp_scenario;
  155. $renderer = & $form->defaultRenderer();
  156. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'correct');
  157. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'counter['.$i.']');
  158. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'answer['.$i.']');
  159. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'comment['.$i.']');
  160. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'weighting['.$i.']');
  161. $answer_number=$form->addElement('text', 'counter['.$i.']', null,'value="'.$i.'"');
  162. $answer_number->freeze();
  163. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  164. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  165. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  166. // feedback
  167. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  168. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  169. }
  170. $form->addElement('text', 'weighting['.$i.']', null, array('class' => "span1", 'value' => '0'));
  171. $form->addElement('html', '</tr>');
  172. $i++;
  173. }
  174. if (empty($this -> id)) {
  175. $form->addElement('hidden', 'new_question', 1);
  176. }
  177. //Adding the "I don't know" question answer
  178. //if (empty($this -> id)) {
  179. $i = 666;
  180. $form -> addElement ('html', '<tr>');
  181. $defaults['answer['.$i.']'] = get_lang('DontKnow');
  182. $defaults['weighting['.$i.']'] = 0;
  183. $defaults['scenario']=$temp_scenario;
  184. $renderer = & $form->defaultRenderer();
  185. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'correct');
  186. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'counter['.$i.']');
  187. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'answer['.$i.']');
  188. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'comment['.$i.']');
  189. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>', 'weighting['.$i.']');
  190. $answer_number=$form->addElement('text', 'counter['.$i.']',null,'value="-"');
  191. $answer_number->freeze();
  192. $form->addElement('hidden', 'position['.$i.']', '666');
  193. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  194. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  195. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  196. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  197. // feedback
  198. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  199. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  200. }
  201. //$form->addElement('select', 'destination'.$i, get_lang('SelectQuestion').' : ',$select_question,'multiple');
  202. $form->addElement('text', 'weighting['.$i.']', null, array('class' => "span1", 'value' => '0', 'readonly' =>'readonly'));
  203. $form->addElement ('html', '</tr>');
  204. //}
  205. $form -> addElement ('html', '</table>');
  206. $form -> addElement ('html', '<br />');
  207. $navigator_info = api_get_navigator();
  208. global $text, $class;
  209. //ie6 fix
  210. if ($obj_ex->edit_exercise_in_lp == true) {
  211. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  212. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  213. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  214. $form->addElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  215. } else {
  216. //setting the save button here and not in the question class.php
  217. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="btn minus"');
  218. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="btn plus"');
  219. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  220. }
  221. }
  222. $renderer->setElementTemplate('{element}&nbsp;','submitQuestion');
  223. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  224. $renderer->setElementTemplate('{element}&nbsp;','moreAnswers');
  225. $form -> addElement ('html', '</div></div>');
  226. //We check the first radio button to be sure a radio button will be check
  227. if ($correct==0) {
  228. $correct=1;
  229. }
  230. $defaults['correct'] = $correct;
  231. if (!empty($this -> id)) {
  232. $form -> setDefaults($defaults);
  233. } else {
  234. $form -> setDefaults($defaults);
  235. }
  236. $form->addElement('hidden', 'nb_answers');
  237. $form->setConstants(array('nb_answers' => $nb_answers));
  238. }
  239. /**
  240. * Function which creates the form to create / edit the answers of the question
  241. * @param the formvalidator instance
  242. * @param the answers number to display
  243. */
  244. function processAnswersCreation($form) {
  245. $questionWeighting = $nbrGoodAnswers = 0;
  246. $correct = $form -> getSubmitValue('correct');
  247. $objAnswer = new Answer($this->id);
  248. $nb_answers = $form -> getSubmitValue('nb_answers');
  249. $minus = 1;
  250. if ($form -> getSubmitValue('new_question')) {
  251. $minus = 0;
  252. }
  253. for ($i=1 ; $i <= $nb_answers - $minus; $i++) {
  254. $position = trim($form -> getSubmitValue('position['.$i.']'));
  255. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  256. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  257. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  258. $scenario = $form -> getSubmitValue('scenario');
  259. //
  260. //$list_destination = $form -> getSubmitValue('destination'.$i);
  261. //$destination_str = $form -> getSubmitValue('destination'.$i);
  262. $try = $scenario['try'.$i];
  263. $lp = $scenario['lp'.$i];
  264. $destination = $scenario['destination'.$i];
  265. $url = trim($scenario['url'.$i]);
  266. /*
  267. How we are going to parse the destination value
  268. here we parse the destination value which is a string
  269. 1@@3@@2;4;4;@@http://www.chamilo.org
  270. where: try_again@@lp_id@@selected_questions@@url
  271. try_again = is 1 || 0
  272. lp_id = id of a learning path (0 if dont select)
  273. selected_questions= ids of questions
  274. url= an url
  275. */
  276. /*
  277. $destination_str='';
  278. foreach ($list_destination as $destination_id)
  279. {
  280. $destination_str.=$destination_id.';';
  281. }*/
  282. $goodAnswer= ($correct == $i) ? true : false;
  283. if ($goodAnswer) {
  284. $nbrGoodAnswers++;
  285. $weighting = abs($weighting);
  286. if($weighting > 0) {
  287. $questionWeighting += $weighting;
  288. }
  289. }
  290. if (empty($try))
  291. $try=0;
  292. if (empty($lp)) {
  293. $lp=0;
  294. }
  295. if (empty($destination)) {
  296. $destination=0;
  297. }
  298. if ($url=='') {
  299. $url=0;
  300. }
  301. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  302. $dest= $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  303. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  304. }
  305. //Create 666 answer
  306. $i = 666;
  307. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  308. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  309. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  310. $goodAnswer= ($correct == $i) ? true : false;
  311. $dest = '';
  312. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  313. // saves the answers into the data base
  314. $objAnswer -> save();
  315. // sets the total weighting of the question
  316. $this -> updateWeighting($questionWeighting);
  317. $this -> save();
  318. }
  319. function return_header($feedback_type = null, $counter = null, $score = null, $show_media = false) {
  320. $header = parent::return_header($feedback_type, $counter, $score, $show_media);
  321. $header .= '<table class="'.$this->question_table_class .'">
  322. <tr>
  323. <th>'.get_lang("Choice").'</th>
  324. <th>'. get_lang("ExpectedChoice").'</th>
  325. <th>'. get_lang("Answer").'</th>';
  326. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  327. $header .= '<th>'.get_lang("Comment").'</th>';
  328. } else {
  329. $header .= '<th>&nbsp;</th>';
  330. }
  331. $header .= '</tr>';
  332. return $header;
  333. }
  334. }
  335. endif;