unique_answer_no_option.class.php 17 KB

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