unique_answer.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /*
  3. DOKEOS - elearning and course management software
  4. For a full list of contributors, see documentation/credits.html
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. See "documentation/licence.html" more details.
  10. Contact:
  11. Dokeos
  12. Rue des Palais 44 Paleizenstraat
  13. B-1030 Brussels - Belgium
  14. Tel. +32 (2) 211 34 56
  15. */
  16. /**
  17. * File containing the UNIQUE_ANSWER class.
  18. * @package dokeos.exercise
  19. * @author Eric Marguin
  20. * @version $Id: admin.php 10680 2007-01-11 21:26:23Z pcool $
  21. */
  22. if(!class_exists('UniqueAnswer')):
  23. /**
  24. CLASS UNIQUE_ANSWER
  25. *
  26. * This class allows to instantiate an object of type UNIQUE_ANSWER (MULTIPLE CHOICE, UNIQUE ANSWER),
  27. * extending the class question
  28. *
  29. * @author Eric Marguin
  30. * @author Julio Montoya
  31. * @package dokeos.exercise
  32. **/
  33. class UniqueAnswer extends Question {
  34. static $typePicture = 'mcua.gif';
  35. static $explanationLangVar = 'UniqueSelect';
  36. /**
  37. * Constructor
  38. */
  39. function UniqueAnswer(){
  40. //this is highly important
  41. parent::question();
  42. $this -> type = UNIQUE_ANSWER;
  43. }
  44. /**
  45. * function which redifines Question::createAnswersForm
  46. * @param the formvalidator instance
  47. * @param the answers number to display
  48. */
  49. function createAnswersForm ($form) {
  50. // getting the exercise list
  51. $obj_ex =$_SESSION['objExercise'];
  52. $editor_config = array('ToolbarSet' => 'TestProposedAnswer', 'Width' => '100%', 'Height' => '125');
  53. //this line define how many question by default appear when creating a choice question
  54. $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 2;
  55. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  56. /*
  57. Types of Feedback
  58. $feedback_option[0]=get_lang('Feedback');
  59. $feedback_option[1]=get_lang('DirectFeedback');
  60. $feedback_option[2]=get_lang('NoFeedback');
  61. */
  62. $feedback_title='';
  63. $comment_title='';
  64. if ($obj_ex->selectFeedbackType()==0) {
  65. $comment_title = '<th>'.get_lang('Comment').'</th>';
  66. } elseif ($obj_ex->selectFeedbackType()==1) {
  67. $editor_config['Width'] = '250';
  68. $editor_config['Height'] = '110';
  69. $comment_title = '<th width="500" >'.get_lang('Comment').'</th>';
  70. $feedback_title = '<th width="350px" >'.get_lang('Scenario').'</th>';
  71. }
  72. $html='
  73. <div class="row">
  74. <div class="label">
  75. '.get_lang('Answers').' <br /> <img src="../img/fill_field.png">
  76. </div>
  77. <div class="formw">
  78. <table class="data_table">
  79. <tr style="text-align: center;">
  80. <th width="10px">
  81. '.get_lang('Number').'
  82. </th>
  83. <th width="10px" >
  84. '.get_lang('True').'
  85. </th>
  86. <th width="45%">
  87. '.get_lang('Answer').'
  88. </th>
  89. '.$comment_title.'
  90. '.$feedback_title.'
  91. <th width="20px">
  92. '.get_lang('Weighting').'
  93. </th>
  94. </tr>';
  95. $form -> addElement ('html', $html);
  96. $defaults = array();
  97. $correct = 0;
  98. if(!empty($this -> id)) {
  99. $answer = new Answer($this -> id);
  100. $answer -> read();
  101. if(count($answer->nbrAnswers)>0 && !$form->isSubmitted())
  102. {
  103. $nb_answers = $answer->nbrAnswers;
  104. }
  105. }
  106. $form -> addElement('hidden', 'nb_answers');
  107. //Feedback SELECT
  108. $question_list=$obj_ex->selectQuestionList();
  109. $select_question=array();
  110. $select_question[0]=get_lang('SelectTargetQuestion');
  111. require_once('../newscorm/learnpathList.class.php');
  112. require_once(api_get_path(LIBRARY_PATH).'text.lib.php');
  113. if (is_array($question_list)) {
  114. foreach ($question_list as $key=>$questionid) {
  115. $question = Question::read($questionid);
  116. $select_question[$questionid]='Q'.$key.' :'.cut($question->selectTitle(),20);
  117. }
  118. }
  119. $select_question[-1]=get_lang('ExitTest');
  120. //LP SELECT
  121. require_once('../newscorm/learnpathList.class.php');
  122. //require_once('../newscorm/learnpath.class.php');
  123. //require_once('../newscorm/learnpathItem.class.php');
  124. $list = new LearnpathList(api_get_user_id());
  125. $flat_list = $list->get_flat_list();
  126. $select_lp_id=array();
  127. $select_lp_id[0]=get_lang('SelectTargetLP');
  128. foreach ($flat_list as $id => $details)
  129. {
  130. $select_lp_id[$id] = cut($details['lp_name'],20);
  131. }
  132. $temp_scenario=array();
  133. if ($nb_answers < 1) {
  134. $nb_answers = 1;
  135. Display::display_normal_message(get_lang('YouHaveToCreateAtLeastOneAnswer'));
  136. }
  137. for($i = 1 ; $i <= $nb_answers ; ++$i)
  138. {
  139. $form -> addElement ('html', '<tr>');
  140. if(is_object($answer))
  141. {
  142. if($answer -> correct[$i])
  143. {
  144. $correct = $i;
  145. }
  146. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  147. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  148. $defaults['weighting['.$i.']'] = float_format($answer -> weighting[$i], 1);
  149. $item_list=explode('@@',$answer -> destination[$i]);
  150. //echo '<pre>'; print_r($item_list);
  151. $try = $item_list[0];
  152. $lp = $item_list[1];
  153. $list_dest= $item_list[2];
  154. $url=$item_list[3];
  155. if ($try==0)
  156. $try_result=0;
  157. else
  158. $try_result=1;
  159. if ($url==0)
  160. $url_result='';
  161. else
  162. $url_result=$url;
  163. $temp_scenario['url'.$i]=$url_result;
  164. $temp_scenario['try'.$i]=$try_result;
  165. $temp_scenario['lp'.$i]=$lp;
  166. $temp_scenario['destination'.$i]=$list_dest;
  167. /*$pre_list_destination=explode(';',$list_dest);
  168. $list_destination=array();
  169. foreach($pre_list_destination as $value)
  170. {
  171. $list_destination[]=$value;
  172. }
  173. $defaults['destination'.$i]=$list_destination;
  174. */
  175. //$defaults['destination'.$i] = $list_destination;
  176. } else {
  177. $defaults['answer[1]'] = get_lang('langDefaultUniqueAnswer1');
  178. $defaults['weighting[1]'] = 10;
  179. $defaults['answer[2]'] = get_lang('langDefaultUniqueAnswer2');
  180. $defaults['weighting[2]'] = 0;
  181. $temp_scenario['destination'.$i] = array('0');
  182. $temp_scenario['lp'.$i] = array('0');
  183. //$defaults['scenario']
  184. }
  185. $defaults['scenario']=$temp_scenario;
  186. $renderer = & $form->defaultRenderer();
  187. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  188. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  189. $answer_number->freeze();
  190. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  191. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  192. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  193. if ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  194. // feedback
  195. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  196. } elseif ($obj_ex->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  197. // direct feedback
  198. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  199. //Adding extra feedback fields
  200. $group = array();
  201. $group['try'.$i] =&$form->createElement('checkbox', 'try'.$i,get_lang('TryAgain').': ' );
  202. $group['lp'.$i] =&$form->createElement('select', 'lp'.$i,get_lang('SeeTheory').': ',$select_lp_id);
  203. $group['destination'.$i]=&$form->createElement('select', 'destination'.$i, get_lang('GoToQuestion').': ' ,$select_question);
  204. $group['url'.$i] =&$form->createElement('text', 'url'.$i,get_lang('Other').': ',array('size'=>'25px'));
  205. $form -> addGroup($group, 'scenario', 'scenario');
  206. $renderer->setGroupElementTemplate('<div class="exercise_scenario_label">{label}</div><div class="exercise_scenario_element">{element}</div>','scenario');
  207. }
  208. //$form->addElement('select', 'destination'.$i, get_lang('SelectQuestion').' : ',$select_question,'multiple');
  209. $form->addElement('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="10"');
  210. $form -> addElement ('html', '</tr>');
  211. }
  212. $form -> addElement ('html', '</table>');
  213. $form -> addElement ('html', '<br />');
  214. $navigator_info = api_get_navigator();
  215. global $text, $class;
  216. //ie6 fix
  217. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  218. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  219. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  220. $form->addElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  221. } else {
  222. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  223. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  224. // setting the save button here and not in the question class.php
  225. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  226. }
  227. $renderer->setElementTemplate('{element}','submitQuestion');
  228. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  229. $renderer->setElementTemplate('{element}','moreAnswers');
  230. $form -> addElement ('html', '</div></div>');
  231. //We check the first radio button to be sure a radio button will be check
  232. if ($correct==0) {
  233. $correct=1;
  234. }
  235. $defaults['correct'] = $correct;
  236. $form -> setDefaults($defaults);
  237. $form->setConstants(array('nb_answers' => $nb_answers));
  238. }
  239. /**
  240. * abstract 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. for($i=1 ; $i <= $nb_answers ; $i++)
  250. {
  251. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  252. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  253. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  254. $scenario= $form -> getSubmitValue('scenario');
  255. echo '<pre>';
  256. //$list_destination = $form -> getSubmitValue('destination'.$i);
  257. //$destination_str = $form -> getSubmitValue('destination'.$i);
  258. $try = $scenario['try'.$i];
  259. $lp= $scenario['lp'.$i];
  260. $destination = $scenario['destination'.$i];
  261. $url = trim($scenario['url'.$i]);
  262. /*
  263. How we are going to parse the destination value
  264. here we parse the destination value which is a string
  265. 1@@3@@2;4;4;@@http://www.dokeos.com
  266. where: try_again@@lp_id@@selected_questions@@url
  267. try_again = is 1 || 0
  268. lp_id = id of a learning path (0 if dont select)
  269. selected_questions= ids of questions
  270. url= an url
  271. */
  272. /*
  273. $destination_str='';
  274. foreach ($list_destination as $destination_id)
  275. {
  276. $destination_str.=$destination_id.';';
  277. }*/
  278. $goodAnswer= ($correct == $i) ? true : false;
  279. if($goodAnswer)
  280. {
  281. $nbrGoodAnswers++;
  282. $weighting = abs($weighting);
  283. if($weighting > 0)
  284. {
  285. $questionWeighting += $weighting;
  286. }
  287. }
  288. if (empty($try))
  289. $try=0;
  290. if (empty($lp))
  291. {
  292. $lp=0;
  293. }
  294. if (empty($destination))
  295. {
  296. $destination=0;
  297. }
  298. if ($url=='')
  299. {
  300. $url=0;
  301. }
  302. //1@@1;2;@@2;4;4;@@http://www.dokeos.com
  303. $dest= $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  304. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  305. }
  306. // saves the answers into the data base
  307. $objAnswer -> save();
  308. // sets the total weighting of the question
  309. $this -> updateWeighting($questionWeighting);
  310. $this -> save();
  311. }
  312. }
  313. endif;
  314. ?>