unique_answer.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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('UniqueAnswer')):
  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 UniqueAnswer extends Question {
  23. static $typePicture = 'mcua.gif';
  24. static $explanationLangVar = 'UniqueSelect';
  25. /**
  26. * Constructor
  27. */
  28. function UniqueAnswer(){
  29. //this is highly important
  30. parent::question();
  31. $this -> type = UNIQUE_ANSWER;
  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'] : 4; // 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. $form -> addElement('hidden', 'nb_answers');
  96. //Feedback SELECT
  97. $question_list=$obj_ex->selectQuestionList();
  98. $select_question=array();
  99. $select_question[0]=get_lang('SelectTargetQuestion');
  100. require_once '../newscorm/learnpathList.class.php';
  101. if (is_array($question_list)) {
  102. foreach ($question_list as $key=>$questionid) {
  103. //To avoid warning messages
  104. if (!is_numeric($questionid)) {
  105. continue;
  106. }
  107. $question = Question::read($questionid);
  108. $select_question[$questionid]='Q'.$key.' :'.cut($question->selectTitle(),20);
  109. }
  110. }
  111. $select_question[-1]=get_lang('ExitTest');
  112. //require_once('../newscorm/learnpath.class.php');
  113. //require_once('../newscorm/learnpathItem.class.php');
  114. $list = new LearnpathList(api_get_user_id());
  115. $flat_list = $list->get_flat_list();
  116. $select_lp_id=array();
  117. $select_lp_id[0]=get_lang('SelectTargetLP');
  118. foreach ($flat_list as $id => $details) {
  119. $select_lp_id[$id] = cut($details['lp_name'],20);
  120. }
  121. $temp_scenario = array();
  122. if ($nb_answers < 1) {
  123. $nb_answers = 1;
  124. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  125. }
  126. for($i = 1 ; $i <= $nb_answers ; ++$i) {
  127. $form -> addElement ('html', '<tr>');
  128. if (is_object($answer)) {
  129. if ($answer -> correct[$i]) {
  130. $correct = $i;
  131. }
  132. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  133. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  134. $defaults['weighting['.$i.']'] = float_format($answer -> weighting[$i], 1);
  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. /*$pre_list_destination=explode(';',$list_dest);
  153. $list_destination=array();
  154. foreach($pre_list_destination as $value)
  155. {
  156. $list_destination[]=$value;
  157. }
  158. $defaults['destination'.$i]=$list_destination;
  159. */
  160. //$defaults['destination'.$i] = $list_destination;
  161. } else {
  162. $defaults['answer[1]'] = get_lang('langDefaultUniqueAnswer1');
  163. $defaults['weighting[1]'] = 10;
  164. $defaults['answer[2]'] = get_lang('langDefaultUniqueAnswer2');
  165. $defaults['weighting[2]'] = 0;
  166. $temp_scenario['destination'.$i] = array('0');
  167. $temp_scenario['lp'.$i] = array('0');
  168. //$defaults['scenario']
  169. }
  170. $defaults['scenario']=$temp_scenario;
  171. $renderer = & $form->defaultRenderer();
  172. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  173. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>','html');
  174. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  175. $answer_number->freeze();
  176. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  177. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  178. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  179. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  180. // feedback
  181. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  182. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  183. // direct feedback
  184. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  185. //Adding extra feedback fields
  186. $group = array();
  187. $group['try'.$i] =&$form->createElement('checkbox', 'try'.$i,get_lang('TryAgain').': ' );
  188. $group['lp'.$i] =&$form->createElement('select', 'lp'.$i,get_lang('SeeTheory').': ',$select_lp_id);
  189. $group['destination'.$i]=&$form->createElement('select', 'destination'.$i, get_lang('GoToQuestion').': ' ,$select_question);
  190. $group['url'.$i] =&$form->createElement('text', 'url'.$i,get_lang('Other').': ',array('size'=>'25px'));
  191. $form -> addGroup($group, 'scenario', 'scenario');
  192. $renderer->setGroupElementTemplate('<div class="exercise_scenario_label">{label}</div><div class="exercise_scenario_element">{element}</div>','scenario');
  193. }
  194. //$form->addElement('select', 'destination'.$i, get_lang('SelectQuestion').' : ',$select_question,'multiple');
  195. $form->addElement('text', 'weighting['.$i.']', null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="0"');
  196. $form->addElement ('html', '</tr>');
  197. }
  198. $form -> addElement ('html', '</table>');
  199. $form -> addElement ('html', '<br />');
  200. $navigator_info = api_get_navigator();
  201. global $text, $class, $show_quiz_edition;
  202. //ie6 fix
  203. if ($show_quiz_edition) {
  204. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  205. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  206. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  207. $form->addElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  208. } else {
  209. //setting the save button here and not in the question class.php
  210. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  211. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  212. $form->addElement('style_submit_button', 'submitQuestion',$text, 'class="'.$class.'"');
  213. }
  214. }
  215. $renderer->setElementTemplate('{element}','submitQuestion');
  216. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  217. $renderer->setElementTemplate('{element}','moreAnswers');
  218. $form -> addElement ('html', '</div></div>');
  219. //We check the first radio button to be sure a radio button will be check
  220. if ($correct==0) {
  221. $correct=1;
  222. }
  223. $defaults['correct'] = $correct;
  224. if (!empty($this -> id)) {
  225. $form -> setDefaults($defaults);
  226. } else {
  227. if ($this -> isContent == 1) {
  228. $form -> setDefaults($defaults);
  229. }
  230. }
  231. $form->setConstants(array('nb_answers' => $nb_answers));
  232. }
  233. /**
  234. * abstract function which creates the form to create / edit the answers of the question
  235. * @param the formvalidator instance
  236. * @param the answers number to display
  237. */
  238. function processAnswersCreation($form) {
  239. $questionWeighting = $nbrGoodAnswers = 0;
  240. $correct = $form -> getSubmitValue('correct');
  241. $objAnswer = new Answer($this->id);
  242. $nb_answers = $form -> getSubmitValue('nb_answers');
  243. for($i=1 ; $i <= $nb_answers ; $i++)
  244. {
  245. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  246. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  247. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  248. $scenario= $form -> getSubmitValue('scenario');
  249. echo '<pre>';
  250. //$list_destination = $form -> getSubmitValue('destination'.$i);
  251. //$destination_str = $form -> getSubmitValue('destination'.$i);
  252. $try = $scenario['try'.$i];
  253. $lp= $scenario['lp'.$i];
  254. $destination = $scenario['destination'.$i];
  255. $url = trim($scenario['url'.$i]);
  256. /*
  257. How we are going to parse the destination value
  258. here we parse the destination value which is a string
  259. 1@@3@@2;4;4;@@http://www.dokeos.com
  260. where: try_again@@lp_id@@selected_questions@@url
  261. try_again = is 1 || 0
  262. lp_id = id of a learning path (0 if dont select)
  263. selected_questions= ids of questions
  264. url= an url
  265. */
  266. /*
  267. $destination_str='';
  268. foreach ($list_destination as $destination_id)
  269. {
  270. $destination_str.=$destination_id.';';
  271. }*/
  272. $goodAnswer= ($correct == $i) ? true : false;
  273. if($goodAnswer)
  274. {
  275. $nbrGoodAnswers++;
  276. $weighting = abs($weighting);
  277. if($weighting > 0)
  278. {
  279. $questionWeighting += $weighting;
  280. }
  281. }
  282. if (empty($try))
  283. $try=0;
  284. if (empty($lp))
  285. {
  286. $lp=0;
  287. }
  288. if (empty($destination))
  289. {
  290. $destination=0;
  291. }
  292. if ($url=='')
  293. {
  294. $url=0;
  295. }
  296. //1@@1;2;@@2;4;4;@@http://www.dokeos.com
  297. $dest= $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  298. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  299. }
  300. // saves the answers into the data base
  301. $objAnswer -> save();
  302. // sets the total weighting of the question
  303. $this -> updateWeighting($questionWeighting);
  304. $this -> save();
  305. }
  306. function return_header($feedback_type, $counter = null) {
  307. parent::return_header($feedback_type, $counter);
  308. $header = '<table width="100%" border="0" cellspacing="3" cellpadding="3">
  309. <tr>
  310. <td>&nbsp;</td>
  311. </tr>
  312. <tr>
  313. <td><i>'.get_lang("Choice").'</i> </td>
  314. <td><i>'. get_lang("ExpectedChoice").'</i></td>
  315. <td><i>'. get_lang("Answer").'</i></td>';
  316. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  317. $header .= '<td><i>'.get_lang("Comment").'</i></td>';
  318. } else {
  319. $header .= '<td>&nbsp;</td>';
  320. }
  321. $header .= '
  322. </tr>
  323. <tr>
  324. <td>&nbsp;</td>
  325. </tr>';
  326. return $header;
  327. }
  328. function create_answer($id, $question_id, $answer_title, $comment, $score = 0, $correct = 0) {
  329. $tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  330. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  331. $position = 1;
  332. $question_id = filter_var($question_id,FILTER_SANITIZE_NUMBER_INT);
  333. $score = filter_var($score,FILTER_SANITIZE_NUMBER_FLOAT);
  334. $correct = filter_var($correct,FILTER_SANITIZE_NUMBER_INT);
  335. // Get the max position
  336. $sql = "SELECT max(position) as max_position FROM $tbl_quiz_answer "
  337. ." WHERE question_id = $question_id";
  338. $rs_max = Database::query($sql);
  339. $row_max = Database::fetch_object($rs_max);
  340. $position = $row_max->max_position + 1;
  341. // Insert a new answer
  342. $sql = "INSERT INTO $tbl_quiz_answer "
  343. ."(id, question_id,answer,correct,comment,ponderation,position,destination)"
  344. ."VALUES ($id,$question_id,'".Database::escape_string($answer_title)."',"
  345. ."$correct,'".Database::escape_string($comment)."',$score,$position, "
  346. ." '0@@0@@0@@0')";
  347. $rs = Database::query($sql);
  348. if ($correct) {
  349. $sql = "UPDATE $tbl_quiz_question "
  350. ." SET ponderation = (ponderation + $score) WHERE id = ".$question_id;
  351. $rs = Database::query($sql, __FILE__, __LINE__);
  352. }
  353. }
  354. }
  355. endif;
  356. ?>