unique_answer_no_option.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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="38%">
  77. '.get_lang('Answer').'
  78. </th>
  79. '.$comment_title.'
  80. '.$feedback_title.'
  81. <th width="20px">
  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. //Feedback SELECT
  96. //Not needed right now
  97. /*
  98. $question_list=$obj_ex->selectQuestionList();
  99. $select_question=array();
  100. $select_question[0]=get_lang('SelectTargetQuestion');
  101. require_once '../newscorm/learnpathList.class.php';
  102. if (is_array($question_list)) {
  103. foreach ($question_list as $key=>$questionid) {
  104. //To avoid warning messages
  105. if (!is_numeric($questionid)) {
  106. continue;
  107. }
  108. $question = Question::read($questionid);
  109. $select_question[$questionid]='Q'.$key.' :'.cut($question->selectTitle(),20);
  110. }
  111. }
  112. $select_question[-1]=get_lang('ExitTest');
  113. $list = new LearnpathList(api_get_user_id());
  114. $flat_list = $list->get_flat_list();
  115. $select_lp_id=array();
  116. $select_lp_id[0]=get_lang('SelectTargetLP');
  117. foreach ($flat_list as $id => $details) {
  118. $select_lp_id[$id] = cut($details['lp_name'],20);
  119. }*/
  120. $temp_scenario = array();
  121. if ($nb_answers < 1) {
  122. $nb_answers = 1;
  123. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  124. }
  125. if ($_GET['editQuestion']) {
  126. //fixing $nb_answers
  127. $new_list = array();
  128. $count = 1;
  129. if (isset($_POST['lessAnswers'])) {
  130. if (!isset($_SESSION['less_answer'])) {
  131. $_SESSION['less_answer'] = $this -> id;
  132. $nb_answers--;
  133. }
  134. }
  135. for ($k = 1 ; $k <= $nb_answers; ++$k) {
  136. if ($answer->position[$k] != '666') {
  137. $new_list[$count] = $count;
  138. $count++;
  139. }
  140. }
  141. } else {
  142. for ($k = 1 ; $k <= $nb_answers; ++$k) {
  143. $new_list[$k] = $k;
  144. }
  145. }
  146. $i = 1;
  147. //for ($k = 1 ; $k <= $real_nb_answers; $k++) {
  148. foreach ($new_list as $key) {
  149. $i = $key;
  150. $form -> addElement ('html', '<tr>');
  151. if (is_object($answer)) {
  152. if($answer->position[$i] == 666) {
  153. //we set nothing
  154. } else {
  155. if ($answer->correct[$i]) {
  156. $correct = $i;
  157. }
  158. $answer_result = $answer->answer[$i];
  159. $weight_result = float_format($answer->weighting[$i], 1);
  160. if ($nb_answers == $i) {
  161. $weight_result = '0';
  162. }
  163. $defaults['answer['.$i.']'] = $answer_result;
  164. $defaults['comment['.$i.']'] = $answer->comment[$i];
  165. $defaults['weighting['.$i.']'] = $weight_result;
  166. $item_list=explode('@@',$answer -> destination[$i]);
  167. $try = $item_list[0];
  168. $lp = $item_list[1];
  169. $list_dest = $item_list[2];
  170. $url = $item_list[3];
  171. if ($try==0)
  172. $try_result=0;
  173. else
  174. $try_result=1;
  175. if ($url==0)
  176. $url_result='';
  177. else
  178. $url_result=$url;
  179. $temp_scenario['url'.$i] = $url_result;
  180. $temp_scenario['try'.$i] = $try_result;
  181. $temp_scenario['lp'.$i] = $lp;
  182. $temp_scenario['destination'.$i]= $list_dest;
  183. }
  184. /*$pre_list_destination=explode(';',$list_dest);
  185. $list_destination=array();
  186. foreach($pre_list_destination as $value)
  187. {
  188. $list_destination[]=$value;
  189. }
  190. $defaults['destination'.$i]=$list_destination;
  191. */
  192. //$defaults['destination'.$i] = $list_destination;
  193. }
  194. $defaults['scenario']=$temp_scenario;
  195. $renderer = & $form->defaultRenderer();
  196. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  197. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>','html');
  198. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  199. $answer_number->freeze();
  200. // $form->addElement('hidden', 'position['.$i.']', $answer->position[$i]);
  201. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  202. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  203. //$form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  204. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  205. // feedback
  206. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  207. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  208. /*
  209. // direct feedback
  210. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  211. //Adding extra feedback fields
  212. $group = array();
  213. $group['try'.$i] =&$form->createElement('checkbox', 'try'.$i,get_lang('TryAgain').': ' );
  214. $group['lp'.$i] =&$form->createElement('select', 'lp'.$i,get_lang('SeeTheory').': ',$select_lp_id);
  215. $group['destination'.$i]=&$form->createElement('select', 'destination'.$i, get_lang('GoToQuestion').': ' ,$select_question);
  216. $group['url'.$i] =&$form->createElement('text', 'url'.$i,get_lang('Other').': ',array('size'=>'25px'));
  217. $form -> addGroup($group, 'scenario', 'scenario');
  218. $renderer->setGroupElementTemplate('<div class="exercise_scenario_label">{label}</div><div class="exercise_scenario_element">{element}</div>','scenario');*/
  219. }
  220. //$form->addElement('select', 'destination'.$i, get_lang('SelectQuestion').' : ',$select_question,'multiple');
  221. $form->addElement('text', 'weighting['.$i.']', null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="0"');
  222. $form->addElement('html', '</tr>');
  223. $i++;
  224. }
  225. if (empty($this -> id)) {
  226. $form->addElement('hidden', 'new_question', 1);
  227. }
  228. //Adding the "I don't know" question answer
  229. //if (empty($this -> id)) {
  230. $i = 666;
  231. $form -> addElement ('html', '<tr>');
  232. $defaults['answer['.$i.']'] = get_lang('DontKnow');
  233. $defaults['weighting['.$i.']'] = 0;
  234. $defaults['scenario']=$temp_scenario;
  235. $renderer = & $form->defaultRenderer();
  236. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  237. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>','html');
  238. $answer_number=$form->addElement('text', null,null,'value="-"');
  239. $answer_number->freeze();
  240. $form->addElement('hidden', 'position['.$i.']', '666');
  241. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  242. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  243. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  244. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  245. // feedback
  246. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  247. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  248. /* // direct feedback
  249. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  250. //Adding extra feedback fields
  251. $group = array();
  252. $group['try'.$i] =&$form->createElement('checkbox', 'try'.$i,get_lang('TryAgain').': ' );
  253. $group['lp'.$i] =&$form->createElement('select', 'lp'.$i,get_lang('SeeTheory').': ',$select_lp_id);
  254. $group['destination'.$i]=&$form->createElement('select', 'destination'.$i, get_lang('GoToQuestion').': ' ,$select_question);
  255. $group['url'.$i] =&$form->createElement('text', 'url'.$i,get_lang('Other').': ',array('size'=>'25px'));
  256. $form -> addGroup($group, 'scenario', 'scenario');
  257. $renderer->setGroupElementTemplate('<div class="exercise_scenario_label">{label}</div><div class="exercise_scenario_element">{element}</div>','scenario');*/
  258. }
  259. //$form->addElement('select', 'destination'.$i, get_lang('SelectQuestion').' : ',$select_question,'multiple');
  260. $form->addElement('text', 'weighting['.$i.']', null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="0" readonly="readonly" ');
  261. $form->addElement ('html', '</tr>');
  262. //}
  263. $form -> addElement ('html', '</table>');
  264. $form -> addElement ('html', '<br />');
  265. $navigator_info = api_get_navigator();
  266. global $text, $class, $show_quiz_edition;
  267. //ie6 fix
  268. if ($show_quiz_edition) {
  269. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  270. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  271. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  272. $form->addElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  273. } else {
  274. //setting the save button here and not in the question class.php
  275. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  276. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  277. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  278. }
  279. }
  280. $renderer->setElementTemplate('{element}','submitQuestion');
  281. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  282. $renderer->setElementTemplate('{element}','moreAnswers');
  283. $form -> addElement ('html', '</div></div>');
  284. //We check the first radio button to be sure a radio button will be check
  285. if ($correct==0) {
  286. $correct=1;
  287. }
  288. $defaults['correct'] = $correct;
  289. if (!empty($this -> id)) {
  290. $form -> setDefaults($defaults);
  291. } else {
  292. $form -> setDefaults($defaults);
  293. }
  294. $form->addElement('hidden', 'nb_answers');
  295. $form->setConstants(array('nb_answers' => $nb_answers));
  296. }
  297. /**
  298. * Function which creates the form to create / edit the answers of the question
  299. * @param the formvalidator instance
  300. * @param the answers number to display
  301. */
  302. function processAnswersCreation($form) {
  303. $questionWeighting = $nbrGoodAnswers = 0;
  304. $correct = $form -> getSubmitValue('correct');
  305. $objAnswer = new Answer($this->id);
  306. $nb_answers = $form -> getSubmitValue('nb_answers');
  307. $minus = 1;
  308. if ($form -> getSubmitValue('new_question')) {
  309. $minus = 0;
  310. }
  311. for ($i=1 ; $i <= $nb_answers - $minus; $i++) {
  312. $position = trim($form -> getSubmitValue('position['.$i.']'));
  313. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  314. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  315. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  316. $scenario = $form -> getSubmitValue('scenario');
  317. //
  318. //$list_destination = $form -> getSubmitValue('destination'.$i);
  319. //$destination_str = $form -> getSubmitValue('destination'.$i);
  320. $try = $scenario['try'.$i];
  321. $lp = $scenario['lp'.$i];
  322. $destination = $scenario['destination'.$i];
  323. $url = trim($scenario['url'.$i]);
  324. /*
  325. How we are going to parse the destination value
  326. here we parse the destination value which is a string
  327. 1@@3@@2;4;4;@@http://www.chamilo.org
  328. where: try_again@@lp_id@@selected_questions@@url
  329. try_again = is 1 || 0
  330. lp_id = id of a learning path (0 if dont select)
  331. selected_questions= ids of questions
  332. url= an url
  333. */
  334. /*
  335. $destination_str='';
  336. foreach ($list_destination as $destination_id)
  337. {
  338. $destination_str.=$destination_id.';';
  339. }*/
  340. $goodAnswer= ($correct == $i) ? true : false;
  341. if ($goodAnswer) {
  342. $nbrGoodAnswers++;
  343. $weighting = abs($weighting);
  344. if($weighting > 0) {
  345. $questionWeighting += $weighting;
  346. }
  347. }
  348. if (empty($try))
  349. $try=0;
  350. if (empty($lp)) {
  351. $lp=0;
  352. }
  353. if (empty($destination)) {
  354. $destination=0;
  355. }
  356. if ($url=='') {
  357. $url=0;
  358. }
  359. //1@@1;2;@@2;4;4;@@http://www.chamilo.org
  360. $dest= $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  361. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  362. }
  363. //Create 666 answer
  364. $i = 666;
  365. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  366. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  367. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  368. $goodAnswer= ($correct == $i) ? true : false;
  369. $dest = '';
  370. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  371. // saves the answers into the data base
  372. $objAnswer -> save();
  373. // sets the total weighting of the question
  374. $this -> updateWeighting($questionWeighting);
  375. $this -> save();
  376. }
  377. function return_header($feedback_type, $counter = null) {
  378. parent::return_header($feedback_type, $counter);
  379. $header = '<table width="100%" class="data_table_exercise_result">
  380. <tr>
  381. <td><i>'.get_lang("Choice").'</i> </td>
  382. <td><i>'. get_lang("ExpectedChoice").'</i></td>
  383. <td><i>'. get_lang("Answer").'</i></td>';
  384. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  385. $header .= '<td><i>'.get_lang("Comment").'</i></td>';
  386. } else {
  387. $header .= '<td>&nbsp;</td>';
  388. }
  389. $header .= '</tr>';
  390. return $header;
  391. }
  392. }
  393. endif;