unique_answer.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. for($i = 1 ; $i <= $nb_answers ; ++$i)
  134. {
  135. $form -> addElement ('html', '<tr>');
  136. if(is_object($answer))
  137. {
  138. if($answer -> correct[$i])
  139. {
  140. $correct = $i;
  141. }
  142. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  143. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  144. $defaults['weighting['.$i.']'] = float_format($answer -> weighting[$i], 1);
  145. $item_list=explode('@@',$answer -> destination[$i]);
  146. //echo '<pre>'; print_r($item_list);
  147. $try = $item_list[0];
  148. $lp = $item_list[1];
  149. $list_dest= $item_list[2];
  150. $url=$item_list[3];
  151. if ($try==0)
  152. $try_result=0;
  153. else
  154. $try_result=1;
  155. if ($url==0)
  156. $url_result='';
  157. else
  158. $url_result=$url;
  159. $temp_scenario['url'.$i]=$url_result;
  160. $temp_scenario['try'.$i]=$try_result;
  161. $temp_scenario['lp'.$i]=$lp;
  162. $temp_scenario['destination'.$i]=$list_dest;
  163. /*$pre_list_destination=explode(';',$list_dest);
  164. $list_destination=array();
  165. foreach($pre_list_destination as $value)
  166. {
  167. $list_destination[]=$value;
  168. }
  169. $defaults['destination'.$i]=$list_destination;
  170. */
  171. //$defaults['destination'.$i] = $list_destination;
  172. }else {
  173. $defaults['answer[1]'] = get_lang('langDefaultUniqueAnswer1');
  174. $defaults['weighting[1]'] = 10;
  175. $defaults['answer[2]'] = get_lang('langDefaultUniqueAnswer2');
  176. $defaults['weighting[2]'] = 0;
  177. $temp_scenario['destination'.$i] = array('0');
  178. $temp_scenario['lp'.$i] = array('0');
  179. //$defaults['scenario']
  180. }
  181. $defaults['scenario']=$temp_scenario;
  182. $renderer = & $form->defaultRenderer();
  183. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  184. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  185. $answer_number->freeze();
  186. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  187. $form->addElement('html_editor', 'answer['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  188. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  189. if ($obj_ex->selectFeedbackType()==0) // feedback
  190. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  191. elseif ($obj_ex->selectFeedbackType()==1) // direct feedback
  192. {
  193. $form->addElement('html_editor', 'comment['.$i.']', null, 'style="vertical-align:middle"', $editor_config);
  194. //Adding extra feedback fields
  195. $group = array();
  196. $group['try'.$i] =&$form->createElement('checkbox', 'try'.$i,get_lang('TryAgain').': ' );
  197. $group['lp'.$i] =&$form->createElement('select', 'lp'.$i,get_lang('SeeTheory').': ',$select_lp_id);
  198. $group['destination'.$i]=&$form->createElement('select', 'destination'.$i, get_lang('GoToQuestion').': ' ,$select_question);
  199. $group['url'.$i] =&$form->createElement('text', 'url'.$i,get_lang('Other').': ',array('size'=>'25px'));
  200. $form -> addGroup($group, 'scenario', 'scenario');
  201. $renderer->setGroupElementTemplate('<div class="exercise_scenario_label">{label}</div><div class="exercise_scenario_element">{element}</div>','scenario');
  202. }
  203. //$form->addElement('select', 'destination'.$i, get_lang('SelectQuestion').' : ',$select_question,'multiple');
  204. $form->addElement('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="10"');
  205. $form -> addElement ('html', '</tr>');
  206. }
  207. $form -> addElement ('html', '</table>');
  208. $form -> addElement ('html', '<br />');
  209. $navigator_info = api_get_navigator();
  210. global $text, $class;
  211. //ie6 fix
  212. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  213. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  214. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  215. $form->addElement('submit','submitQuestion',$text, 'class="'.$class.'"');
  216. } else {
  217. $form->addElement('style_submit_button', 'lessAnswers', get_lang('LessAnswer'),'class="minus"');
  218. $form->addElement('style_submit_button', 'moreAnswers', get_lang('PlusAnswer'),'class="plus"');
  219. // setting the save button here and not in the question class.php
  220. $form->addElement('style_submit_button','submitQuestion',$text, 'class="'.$class.'"');
  221. }
  222. $renderer->setElementTemplate('{element}','submitQuestion');
  223. $renderer->setElementTemplate('{element}&nbsp;','lessAnswers');
  224. $renderer->setElementTemplate('{element}','moreAnswers');
  225. $form -> addElement ('html', '</div></div>');
  226. //We check the first radio button to be sure a radio button will be check
  227. if($correct==0){
  228. $correct=1;
  229. }
  230. $defaults['correct'] = $correct;
  231. $form -> setDefaults($defaults);
  232. $form->setConstants(array('nb_answers' => $nb_answers));
  233. }
  234. /**
  235. * abstract function which creates the form to create / edit the answers of the question
  236. * @param the formvalidator instance
  237. * @param the answers number to display
  238. */
  239. function processAnswersCreation($form) {
  240. $questionWeighting = $nbrGoodAnswers = 0;
  241. $correct = $form -> getSubmitValue('correct');
  242. $objAnswer = new Answer($this->id);
  243. $nb_answers = $form -> getSubmitValue('nb_answers');
  244. for($i=1 ; $i <= $nb_answers ; $i++)
  245. {
  246. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  247. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  248. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  249. $scenario= $form -> getSubmitValue('scenario');
  250. echo '<pre>';
  251. //$list_destination = $form -> getSubmitValue('destination'.$i);
  252. //$destination_str = $form -> getSubmitValue('destination'.$i);
  253. $try = $scenario['try'.$i];
  254. $lp= $scenario['lp'.$i];
  255. $destination = $scenario['destination'.$i];
  256. $url = trim($scenario['url'.$i]);
  257. /*
  258. How we are going to parse the destination value
  259. here we parse the destination value which is a string
  260. 1@@3@@2;4;4;@@http://www.dokeos.com
  261. where: try_again@@lp_id@@selected_questions@@url
  262. try_again = is 1 || 0
  263. lp_id = id of a learning path (0 if dont select)
  264. selected_questions= ids of questions
  265. url= an url
  266. */
  267. /*
  268. $destination_str='';
  269. foreach ($list_destination as $destination_id)
  270. {
  271. $destination_str.=$destination_id.';';
  272. }*/
  273. $goodAnswer= ($correct == $i) ? true : false;
  274. if($goodAnswer)
  275. {
  276. $nbrGoodAnswers++;
  277. $weighting = abs($weighting);
  278. if($weighting > 0)
  279. {
  280. $questionWeighting += $weighting;
  281. }
  282. }
  283. if (empty($try))
  284. $try=0;
  285. if (empty($lp))
  286. {
  287. $lp=0;
  288. }
  289. if (empty($destination))
  290. {
  291. $destination=0;
  292. }
  293. if ($url=='')
  294. {
  295. $url=0;
  296. }
  297. //1@@1;2;@@2;4;4;@@http://www.dokeos.com
  298. $dest= $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  299. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  300. }
  301. // saves the answers into the data base
  302. $objAnswer -> save();
  303. // sets the total weighting of the question
  304. $this -> updateWeighting($questionWeighting);
  305. $this -> save();
  306. }
  307. }
  308. endif;
  309. ?>