unique_answer.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 -> type = UNIQUE_ANSWER;
  41. }
  42. /**
  43. * function which redifines Question::createAnswersForm
  44. * @param the formvalidator instance
  45. * @param the answers number to display
  46. */
  47. function createAnswersForm ($form) {
  48. // getting the exercise list
  49. $obj_ex =$_SESSION['objExercise'];
  50. // multiple
  51. global $fck_attribute;
  52. $fck_attribute = array();
  53. $fck_attribute['Width'] = '50%';
  54. $fck_attribute['Height'] = '125px';
  55. $fck_attribute['ToolbarSet'] = 'Test';
  56. $fck_attribute['Config']['IMUploadPath'] = 'upload/test/';
  57. $fck_attribute['Config']['FlashUploadPath'] = 'upload/test/';
  58. $fck_attribute['Config']['InDocument'] = false;
  59. $fck_attribute['Config']['CreateDocumentDir'] = '../../courses/'.api_get_course_path().'/document/';
  60. // we collapse the fckeditor toolbar
  61. $fck_attribute['Config']['ToolbarStartExpanded']='false';
  62. //this line define how many question by default appear when creating a choice question
  63. $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4;
  64. $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
  65. /*
  66. Types of Feedback
  67. $feedback_option[0]=get_lang('Feedback');
  68. $feedback_option[1]=get_lang('DirectFeedback');
  69. $feedback_option[2]=get_lang('NoFeedback');
  70. */
  71. $feedback_title='';
  72. $comment_title='';
  73. if ($obj_ex->selectFeedbackType()==0) {
  74. $comment_title = '<th>'.get_lang('Comment').'</th>';
  75. } elseif ($obj_ex->selectFeedbackType()==1) {
  76. $fck_attribute['Width'] = '250px';
  77. $fck_attribute['Height'] = '110px';
  78. $comment_title = '<th width="'.$fck_attribute['Width'].'" >'.get_lang('Comment').'</th>';
  79. $feedback_title = '<th width="300px" >'.get_lang('Scenario').'</th>';
  80. }
  81. $html='
  82. <div class="row">
  83. <div class="label">
  84. '.get_lang('Answers').'
  85. </div>
  86. <div class="formw">
  87. <table class="data_table">
  88. <tr style="text-align: center;">
  89. <th width="14px">
  90. '.get_lang('Number').'
  91. </th>
  92. <th width="10px" >
  93. '.get_lang('True').'
  94. </th>
  95. <th width="'.$fck_attribute['Width'].' ">
  96. '.get_lang('Answer').'
  97. </th>
  98. '.$comment_title.'
  99. '.$feedback_title.'
  100. <th width="20px">
  101. '.get_lang('Weighting').'
  102. </th>
  103. </tr>';
  104. $form -> addElement ('html', $html);
  105. $defaults = array();
  106. $correct = 0;
  107. if(!empty($this -> id)) {
  108. $answer = new Answer($this -> id);
  109. $answer -> read();
  110. if(count($answer->nbrAnswers)>0 && !$form->isSubmitted())
  111. {
  112. $nb_answers = $answer->nbrAnswers;
  113. }
  114. }
  115. $form -> addElement('hidden', 'nb_answers');
  116. //Feedback SELECT
  117. $question_list=$obj_ex->selectQuestionList();
  118. $select_question=array();
  119. $select_question[0]=get_lang('SelectTargetQuestion');
  120. require_once('../newscorm/learnpathList.class.php');
  121. require_once(api_get_path(LIBRARY_PATH).'text.lib.php');
  122. foreach ($question_list as $key=>$questionid)
  123. {
  124. $question = Question::read($questionid);
  125. $select_question[$questionid]='Q'.$key.' :'.cut($question->selectTitle(),20);
  126. }
  127. $select_question[-1]=get_lang('ExitTest');
  128. //LP SELECT
  129. require_once('../newscorm/learnpathList.class.php');
  130. //require_once('../newscorm/learnpath.class.php');
  131. //require_once('../newscorm/learnpathItem.class.php');
  132. $list = new LearnpathList(api_get_user_id());
  133. $flat_list = $list->get_flat_list();
  134. $select_lp_id=array();
  135. $select_lp_id[0]=get_lang('SelectTargetLP');
  136. foreach ($flat_list as $id => $details)
  137. {
  138. $select_lp_id[$id] = cut($details['lp_name'],20);
  139. }
  140. $temp_scenario=array();
  141. for($i = 1 ; $i <= $nb_answers ; ++$i)
  142. {
  143. $form -> addElement ('html', '<tr>');
  144. if(is_object($answer))
  145. {
  146. if($answer -> correct[$i])
  147. {
  148. $correct = $i;
  149. }
  150. $defaults['answer['.$i.']'] = $answer -> answer[$i];
  151. $defaults['comment['.$i.']'] = $answer -> comment[$i];
  152. $defaults['weighting['.$i.']'] = $answer -> weighting[$i];
  153. $item_list=explode('@@',$answer -> destination[$i]);
  154. //echo '<pre>'; print_r($item_list);
  155. $try = $item_list[0];
  156. $lp = $item_list[1];
  157. $list_dest= $item_list[2];
  158. $url=$item_list[3];
  159. if ($try==0)
  160. $try_result=0;
  161. else
  162. $try_result=1;
  163. if ($url==0)
  164. $url_result='';
  165. else
  166. $url_result=$url;
  167. $temp_scenario['url'.$i]=$url_result;
  168. $temp_scenario['try'.$i]=$try_result;
  169. $temp_scenario['lp'.$i]=$lp;
  170. $temp_scenario['destination'.$i]=$list_dest;
  171. /*$pre_list_destination=explode(';',$list_dest);
  172. $list_destination=array();
  173. foreach($pre_list_destination as $value)
  174. {
  175. $list_destination[]=$value;
  176. }
  177. $defaults['destination'.$i]=$list_destination;
  178. */
  179. //$defaults['destination'.$i] = $list_destination;
  180. }
  181. else
  182. {
  183. $temp_scenario['destination'.$i] = array('0');
  184. $temp_scenario['lp'.$i] = array('0');
  185. //$defaults['scenario']
  186. }
  187. $defaults['scenario']=$temp_scenario;
  188. $renderer = & $form->defaultRenderer();
  189. $renderer->setElementTemplate('<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>');
  190. $answer_number=$form->addElement('text', null,null,'value="'.$i.'"');
  191. $answer_number->freeze();
  192. $form->addElement('radio', 'correct', null, null, $i, 'class="checkbox" style="margin-left: 0em;"');
  193. $form->addElement('html_editor', 'answer['.$i.']',null, 'style="vertical-align:middle"');
  194. $form->addRule('answer['.$i.']', get_lang('ThisFieldIsRequired'), 'required');
  195. if ($obj_ex->selectFeedbackType()==0) // feedback
  196. $form->addElement('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"');
  197. elseif ($obj_ex->selectFeedbackType()==1) // direct feedback
  198. {
  199. $form->addElement('html_editor', 'comment['.$i.']',null, 'style="vertical-align:middle"');
  200. //Adding extra feedback fields
  201. $group = array();
  202. $group['try'.$i] =&$form->createElement('checkbox', 'try'.$i,get_lang('TryAgain').': ' );
  203. $group['lp'.$i] =&$form->createElement('select', 'lp'.$i,get_lang('SeeTheory').': ',$select_lp_id);
  204. $group['destination'.$i]=&$form->createElement('select', 'destination'.$i, get_lang('GoToQuestion').': ' ,$select_question);
  205. $group['url'.$i] =&$form->createElement('text', 'url'.$i,get_lang('Other').': ',array('size'=>'25px'));
  206. $form -> addGroup($group, 'scenario', 'scenario');
  207. $renderer->setGroupElementTemplate('<div class="exercise_scenario_label">{label}</div><div class="exercise_scenario_element">{element}</div>','scenario');
  208. }
  209. //$form->addElement('select', 'destination'.$i, get_lang('SelectQuestion').' : ',$select_question,'multiple');
  210. $form->addElement('text', 'weighting['.$i.']',null, 'style="vertical-align:middle;margin-left: 0em;" size="5" value="0"');
  211. $form -> addElement ('html', '</tr>');
  212. }
  213. $form -> addElement ('html', '</table>');
  214. $form->addElement('submit', 'lessAnswers', get_lang('LessAnswer'));
  215. $form->addElement('submit', 'moreAnswers', get_lang('PlusAnswer'));
  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. $form -> setDefaults($defaults);
  225. $form->setConstants(array('nb_answers' => $nb_answers));
  226. }
  227. /**
  228. * abstract function which creates the form to create / edit the answers of the question
  229. * @param the formvalidator instance
  230. * @param the answers number to display
  231. */
  232. function processAnswersCreation($form) {
  233. $questionWeighting = $nbrGoodAnswers = 0;
  234. $correct = $form -> getSubmitValue('correct');
  235. $objAnswer = new Answer($this->id);
  236. $nb_answers = $form -> getSubmitValue('nb_answers');
  237. for($i=1 ; $i <= $nb_answers ; $i++)
  238. {
  239. $answer = trim($form -> getSubmitValue('answer['.$i.']'));
  240. $comment = trim($form -> getSubmitValue('comment['.$i.']'));
  241. $weighting = trim($form -> getSubmitValue('weighting['.$i.']'));
  242. $scenario= $form -> getSubmitValue('scenario');
  243. echo '<pre>';
  244. //$list_destination = $form -> getSubmitValue('destination'.$i);
  245. //$destination_str = $form -> getSubmitValue('destination'.$i);
  246. $try = $scenario['try'.$i];
  247. $lp= $scenario['lp'.$i];
  248. $destination = $scenario['destination'.$i];
  249. $url = trim($scenario['url'.$i]);
  250. /*
  251. How we are going to parse the destination value
  252. here we parse the destination value which is a string
  253. 1@@3@@2;4;4;@@http://www.dokeos.com
  254. where: try_again@@lp_id@@selected_questions@@url
  255. try_again = is 1 || 0
  256. lp_id = id of a learning path (0 if dont select)
  257. selected_questions= ids of questions
  258. url= an url
  259. */
  260. /*
  261. $destination_str='';
  262. foreach ($list_destination as $destination_id)
  263. {
  264. $destination_str.=$destination_id.';';
  265. }*/
  266. $goodAnswer= ($correct == $i) ? true : false;
  267. if($goodAnswer)
  268. {
  269. $nbrGoodAnswers++;
  270. $weighting = abs($weighting);
  271. if($weighting > 0)
  272. {
  273. $questionWeighting += $weighting;
  274. }
  275. }
  276. if (empty($try))
  277. $try=0;
  278. if (empty($lp))
  279. {
  280. $lp=0;
  281. }
  282. if (empty($destination))
  283. {
  284. $destination=0;
  285. }
  286. if ($url=='')
  287. {
  288. $url=0;
  289. }
  290. //1@@1;2;@@2;4;4;@@http://www.dokeos.com
  291. $dest= $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
  292. $objAnswer -> createAnswer($answer,$goodAnswer,$comment,$weighting,$i,NULL,NULL,$dest);
  293. }
  294. // saves the answers into the data base
  295. $objAnswer -> save();
  296. // sets the total weighting of the question
  297. $this -> updateWeighting($questionWeighting);
  298. $this -> save();
  299. }
  300. }
  301. endif;
  302. ?>