unique_answer.class.php 14 KB

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