exercise.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. * Exercise class: This class allows to instantiate an object of type Exercise
  18. * @package dokeos.exercise
  19. * @author Olivier Brouckaert
  20. * @version $Id: exercise.class.php 14786 2008-04-08 14:11:46Z elixir_inter $
  21. */
  22. if(!class_exists('Exercise')):
  23. class Exercise
  24. {
  25. var $id;
  26. var $exercise;
  27. var $description;
  28. var $sound;
  29. var $type;
  30. var $random;
  31. var $active;
  32. var $timeLimit;
  33. var $questionList; // array with the list of this exercise's questions
  34. /**
  35. * constructor of the class
  36. *
  37. * @author - Olivier Brouckaert
  38. */
  39. function Exercise()
  40. {
  41. $this->id=0;
  42. $this->exercise='';
  43. $this->description='';
  44. $this->sound='';
  45. $this->type=1;
  46. $this->random=0;
  47. $this->active=1;
  48. $this->questionList=array();
  49. $this->timeLimit = 0;
  50. }
  51. /**
  52. * reads exercise informations from the data base
  53. *
  54. * @author - Olivier Brouckaert
  55. * @param - integer $id - exercise ID
  56. * @return - boolean - true if exercise exists, otherwise false
  57. */
  58. function read($id)
  59. {
  60. global $_course;
  61. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  62. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  63. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  64. #$TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  65. $sql="SELECT title,description,sound,type,random,active, results_disabled FROM $TBL_EXERCICES WHERE id='$id'";
  66. $result=api_sql_query($sql,__FILE__,__LINE__);
  67. // if the exercise has been found
  68. if($object=Database::fetch_object($result))
  69. {
  70. $this->id=$id;
  71. $this->exercise=$object->title;
  72. $this->description=$object->description;
  73. $this->sound=$object->sound;
  74. $this->type=$object->type;
  75. $this->random=$object->random;
  76. $this->active=$object->active;
  77. $this->results_disabled =$object->results_disabled;
  78. $sql="SELECT question_id,position FROM $TBL_EXERCICE_QUESTION,$TBL_QUESTIONS WHERE question_id=id AND exercice_id='$id' ORDER BY position";
  79. $result=api_sql_query($sql,__FILE__,__LINE__);
  80. // fills the array with the question ID for this exercise
  81. // the key of the array is the question position
  82. while($object=Database::fetch_object($result))
  83. {
  84. // makes sure that the question position is unique
  85. while(isset($this->questionList[$object->position]))
  86. {
  87. $object->position++;
  88. }
  89. $this->questionList[$object->position]=$object->question_id;
  90. }
  91. return true;
  92. }
  93. // exercise not found
  94. return false;
  95. }
  96. /**
  97. * returns the exercise ID
  98. *
  99. * @author - Olivier Brouckaert
  100. * @return - integer - exercise ID
  101. */
  102. function selectId()
  103. {
  104. return $this->id;
  105. }
  106. /**
  107. * returns the exercise title
  108. *
  109. * @author - Olivier Brouckaert
  110. * @return - string - exercise title
  111. */
  112. function selectTitle()
  113. {
  114. return $this->exercise;
  115. }
  116. /**
  117. * returns the time limit
  118. */
  119. function selectTimeLimit()
  120. {
  121. return $this->timeLimit;
  122. }
  123. /**
  124. * returns the exercise description
  125. *
  126. * @author - Olivier Brouckaert
  127. * @return - string - exercise description
  128. */
  129. function selectDescription()
  130. {
  131. return $this->description;
  132. }
  133. /**
  134. * returns the exercise sound file
  135. *
  136. * @author - Olivier Brouckaert
  137. * @return - string - exercise description
  138. */
  139. function selectSound()
  140. {
  141. return $this->sound;
  142. }
  143. /**
  144. * returns the exercise type
  145. *
  146. * @author - Olivier Brouckaert
  147. * @return - integer - exercise type
  148. */
  149. function selectType()
  150. {
  151. return $this->type;
  152. }
  153. /**
  154. * tells if questions are selected randomly, and if so returns the draws
  155. *
  156. * @author - Olivier Brouckaert
  157. * @return - integer - 0 if not random, otherwise the draws
  158. */
  159. function isRandom()
  160. {
  161. return $this->random;
  162. }
  163. /**
  164. * Same as isRandom() but has a name applied to values different than 0 or 1
  165. */
  166. function getShuffle()
  167. {
  168. return $this->random;
  169. }
  170. /**
  171. * returns the exercise status (1 = enabled ; 0 = disabled)
  172. *
  173. * @author - Olivier Brouckaert
  174. * @return - boolean - true if enabled, otherwise false
  175. */
  176. function selectStatus()
  177. {
  178. return $this->active;
  179. }
  180. /**
  181. * returns the array with the question ID list
  182. *
  183. * @author - Olivier Brouckaert
  184. * @return - array - question ID list
  185. */
  186. function selectQuestionList()
  187. {
  188. return $this->questionList;
  189. }
  190. /**
  191. * returns the number of questions in this exercise
  192. *
  193. * @author - Olivier Brouckaert
  194. * @return - integer - number of questions
  195. */
  196. function selectNbrQuestions()
  197. {
  198. return sizeof($this->questionList);
  199. }
  200. /**
  201. * selects questions randomly in the question list
  202. *
  203. * @author - Olivier Brouckaert
  204. * @return - array - if the exercise is not set to take questions randomly, returns the question list
  205. * without randomizing, otherwise, returns the list with questions selected randomly
  206. */
  207. function selectRandomList()
  208. {
  209. // if the exercise is not a random exercise, or if there are not at least 2 questions
  210. if(!$this->random || $this->selectNbrQuestions() < 2)
  211. {
  212. return $this->questionList;
  213. }
  214. // takes all questions
  215. if($this->random == -1 || $this->random >= $this->selectNbrQuestions())
  216. {
  217. $draws=$this->selectNbrQuestions();
  218. }
  219. else
  220. {
  221. $draws=$this->random;
  222. }
  223. srand((double)microtime()*1000000);
  224. $randQuestionList=array();
  225. $alreadyChosed=array();
  226. // loop for the number of draws
  227. for($i=0;$i < $draws;$i++)
  228. {
  229. // selects a question randomly
  230. do
  231. {
  232. $rand=rand(0,$this->selectNbrQuestions()-1);
  233. }
  234. // if the question has already been selected, continues in the loop
  235. while(in_array($rand,$alreadyChosed));
  236. $alreadyChosed[]=$rand;
  237. $j=0;
  238. foreach($this->questionList as $key=>$val)
  239. {
  240. // if we have found the question chosed above
  241. if($j == $rand)
  242. {
  243. $randQuestionList[$key]=$val;
  244. break;
  245. }
  246. $j++;
  247. }
  248. }
  249. return $randQuestionList;
  250. }
  251. /**
  252. * returns 'true' if the question ID is in the question list
  253. *
  254. * @author - Olivier Brouckaert
  255. * @param - integer $questionId - question ID
  256. * @return - boolean - true if in the list, otherwise false
  257. */
  258. function isInList($questionId)
  259. {
  260. return in_array($questionId,$this->questionList);
  261. }
  262. /**
  263. * changes the exercise title
  264. *
  265. * @author - Olivier Brouckaert
  266. * @param - string $title - exercise title
  267. */
  268. function updateTitle($title)
  269. {
  270. $this->exercise=$title;
  271. }
  272. /**
  273. * changes the exercise description
  274. *
  275. * @author - Olivier Brouckaert
  276. * @param - string $description - exercise description
  277. */
  278. function updateDescription($description)
  279. {
  280. $this->description=$description;
  281. }
  282. /**
  283. * changes the exercise sound file
  284. *
  285. * @author - Olivier Brouckaert
  286. * @param - string $sound - exercise sound file
  287. * @param - string $delete - ask to delete the file
  288. */
  289. function updateSound($sound,$delete)
  290. {
  291. global $audioPath, $documentPath,$_course, $_user;
  292. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  293. $TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  294. if($sound['size'] && (strstr($sound['type'],'audio') || strstr($sound['type'],'video')))
  295. {
  296. $this->sound=$sound['name'];
  297. if(@move_uploaded_file($sound['tmp_name'],$audioPath.'/'.$this->sound))
  298. {
  299. $query="SELECT 1 FROM $TBL_DOCUMENT "
  300. ." WHERE path='".str_replace($documentPath,'',$audioPath).'/'.$this->sound."'";
  301. $result=api_sql_query($query,__FILE__,__LINE__);
  302. if(!mysql_num_rows($result))
  303. {
  304. /*$query="INSERT INTO $TBL_DOCUMENT(path,filetype) VALUES "
  305. ." ('".str_replace($documentPath,'',$audioPath).'/'.$this->sound."','file')";
  306. api_sql_query($query,__FILE__,__LINE__);*/
  307. $id = add_document($_course,str_replace($documentPath,'',$audioPath).'/'.$this->sound,'file',$sound['size'],$sound['name']);
  308. //$id = Database::get_last_insert_id();
  309. //$time = time();
  310. //$time = date("Y-m-d H:i:s", $time);
  311. // insert into the item_property table, using default visibility of "visible"
  312. /*$query = "INSERT INTO $TBL_ITEM_PROPERTY "
  313. ."(tool, ref, insert_user_id,to_group_id, insert_date, lastedit_date, lastedit_type) "
  314. ." VALUES "
  315. ."('".TOOL_DOCUMENT."', $id, $_user['user_id'], 0, '$time', '$time', 'DocumentAdded' )";
  316. api_sql_query($query,__FILE__,__LINE__);*/
  317. api_item_property_update($_course, TOOL_DOCUMENT, $id, 'DocumentAdded',$_user['user_id']);
  318. item_property_update_on_folder($_course,str_replace($documentPath,'',$audioPath),$_user['user_id']);
  319. }
  320. }
  321. }
  322. elseif($delete && is_file($audioPath.'/'.$this->sound))
  323. {
  324. $this->sound='';
  325. }
  326. }
  327. /**
  328. * changes the exercise type
  329. *
  330. * @author - Olivier Brouckaert
  331. * @param - integer $type - exercise type
  332. */
  333. function updateType($type)
  334. {
  335. $this->type=$type;
  336. }
  337. /**
  338. * sets to 0 if questions are not selected randomly
  339. * if questions are selected randomly, sets the draws
  340. *
  341. * @author - Olivier Brouckaert
  342. * @param - integer $random - 0 if not random, otherwise the draws
  343. */
  344. function setRandom($random)
  345. {
  346. $this->random=$random;
  347. }
  348. /**
  349. * enables the exercise
  350. *
  351. * @author - Olivier Brouckaert
  352. */
  353. function enable()
  354. {
  355. $this->active=1;
  356. }
  357. /**
  358. * disables the exercise
  359. *
  360. * @author - Olivier Brouckaert
  361. */
  362. function disable()
  363. {
  364. $this->active=0;
  365. }
  366. function disable_results()
  367. {
  368. $this->results_disabled = true;
  369. }
  370. function enable_results()
  371. {
  372. $this->results_disabled = false;
  373. }
  374. /**
  375. * updates the exercise in the data base
  376. *
  377. * @author - Olivier Brouckaert
  378. */
  379. function save()
  380. {
  381. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  382. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  383. $id=$this->id;
  384. $exercise=addslashes($this->exercise);
  385. $description=addslashes($this->description);
  386. $sound=addslashes($this->sound);
  387. $type=$this->type;
  388. $random=$this->random;
  389. $active=$this->active;
  390. $results_disabled = intval($this->results_disabled);
  391. // exercise already exists
  392. if($id)
  393. {
  394. $sql="UPDATE $TBL_EXERCICES SET title='$exercise',description='$description',sound='$sound',type='$type',random='$random',active='$active',results_disabled='$results_disabled' WHERE id='$id'";
  395. api_sql_query($sql,__FILE__,__LINE__);
  396. }
  397. // creates a new exercise
  398. else
  399. {
  400. $sql="INSERT INTO $TBL_EXERCICES(title,description,sound,type,random,active, results_disabled) VALUES('$exercise','$description','$sound','$type','$random','$active',$results_disabled)";
  401. api_sql_query($sql,__FILE__,__LINE__);
  402. $this->id=mysql_insert_id();
  403. }
  404. // updates the question position
  405. foreach($this->questionList as $position=>$questionId)
  406. {
  407. $sql="UPDATE $TBL_QUESTIONS SET position='$position' WHERE id='$questionId'";
  408. api_sql_query($sql,__FILE__,__LINE__);
  409. }
  410. }
  411. /**
  412. * moves a question up in the list
  413. *
  414. * @author - Olivier Brouckaert
  415. * @param - integer $id - question ID to move up
  416. */
  417. function moveUp($id)
  418. {
  419. foreach($this->questionList as $position=>$questionId)
  420. {
  421. // if question ID found
  422. if($questionId == $id)
  423. {
  424. // position of question in the array
  425. $pos1=$position;
  426. prev($this->questionList);
  427. // position of previous question in the array
  428. $pos2=key($this->questionList);
  429. // error, can't move question
  430. if(!$pos2)
  431. {
  432. return;
  433. }
  434. $id2=$this->questionList[$pos2];
  435. // exits foreach()
  436. break;
  437. }
  438. // goes to next question
  439. next($this->questionList);
  440. }
  441. // permutes questions in the array
  442. $temp=$this->questionList[$pos2];
  443. $this->questionList[$pos2]=$this->questionList[$pos1];
  444. $this->questionList[$pos1]=$temp;
  445. }
  446. /**
  447. * moves a question down in the list
  448. *
  449. * @author - Olivier Brouckaert
  450. * @param - integer $id - question ID to move down
  451. */
  452. function moveDown($id)
  453. {
  454. foreach($this->questionList as $position=>$questionId)
  455. {
  456. // if question ID found
  457. if($questionId == $id)
  458. {
  459. // position of question in the array
  460. $pos1=$position;
  461. next($this->questionList);
  462. // position of next question in the array
  463. $pos2=key($this->questionList);
  464. // error, can't move question
  465. if(!$pos2)
  466. {
  467. return;
  468. }
  469. $id2=$this->questionList[$pos2];
  470. // exits foreach()
  471. break;
  472. }
  473. // goes to next question
  474. next($this->questionList);
  475. }
  476. // permutes questions in the array
  477. $temp=$this->questionList[$pos2];
  478. $this->questionList[$pos2]=$this->questionList[$pos1];
  479. $this->questionList[$pos1]=$temp;
  480. }
  481. /**
  482. * adds a question into the question list
  483. *
  484. * @author - Olivier Brouckaert
  485. * @param - integer $questionId - question ID
  486. * @return - boolean - true if the question has been added, otherwise false
  487. */
  488. function addToList($questionId)
  489. {
  490. // checks if the question ID is not in the list
  491. if(!$this->isInList($questionId))
  492. {
  493. // selects the max position
  494. if(!$this->selectNbrQuestions())
  495. {
  496. $pos=1;
  497. }
  498. else
  499. {
  500. $pos=max(array_keys($this->questionList))+1;
  501. }
  502. $this->questionList[$pos]=$questionId;
  503. return true;
  504. }
  505. return false;
  506. }
  507. /**
  508. * removes a question from the question list
  509. *
  510. * @author - Olivier Brouckaert
  511. * @param - integer $questionId - question ID
  512. * @return - boolean - true if the question has been removed, otherwise false
  513. */
  514. function removeFromList($questionId)
  515. {
  516. // searches the position of the question ID in the list
  517. $pos=array_search($questionId,$this->questionList);
  518. // question not found
  519. if($pos === false)
  520. {
  521. return false;
  522. }
  523. else
  524. {
  525. // deletes the position from the array containing the wanted question ID
  526. unset($this->questionList[$pos]);
  527. return true;
  528. }
  529. }
  530. /**
  531. * deletes the exercise from the database
  532. * Notice : leaves the question in the data base
  533. *
  534. * @author - Olivier Brouckaert
  535. */
  536. function delete(){
  537. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  538. $sql="UPDATE $TBL_EXERCICES SET active='-1' WHERE id='".$this->id."'";
  539. api_sql_query($sql);
  540. }
  541. /**
  542. * Creates the form to create / edit an exercise
  543. * @param FormValidator $form the formvalidator instance (by reference)
  544. */
  545. function createForm ($form)
  546. {
  547. // title
  548. $form -> addElement('text', 'exerciseTitle', get_lang('ExerciseName').' : ','class="input_titles"');
  549. // fck editor
  550. global $fck_attribute;
  551. $fck_attribute = array();
  552. $fck_attribute['Height'] = '250';
  553. $fck_attribute['Width'] = '100%';
  554. $fck_attribute['ToolbarSet'] = 'NewTest';
  555. $form -> addElement ('html_editor', 'exerciseDescription', get_lang('ExerciseDescription').' : ');
  556. // type
  557. $radios = array();
  558. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('SimpleExercise'),'1');
  559. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('SequentialExercise'),'2');
  560. $form -> addGroup($radios, null, get_lang('ExerciseType').' : ', '<br />');
  561. // random
  562. /*$random = array();
  563. $random[] = FormValidator :: createElement ('text', 'randomQuestions', null,null,'0');
  564. $form -> addGroup($random,null,get_lang('RandomQuestions').' : ','<br />');*/
  565. // submit
  566. $form -> addElement('submit', 'submitExercise', get_lang('Ok'));
  567. // rules
  568. $form -> addRule ('exerciseTitle', get_lang('GiveExerciseName'), 'required');
  569. // defaults
  570. $defaults = array();
  571. if($this -> id > 0)
  572. {
  573. $defaults['exerciseType'] = $this -> selectType();
  574. $defaults['exerciseTitle'] = $this -> selectTitle();
  575. $defaults['exerciseDescription'] = $this -> selectDescription();
  576. //$defaults['randomQuestions'] = $this -> isRandom();
  577. }
  578. else{
  579. $defaults['exerciseType'] = 1;
  580. //$defaults['randomQuestions'] = 0;
  581. $defaults['exerciseDescription'] = '';
  582. }
  583. $form -> setDefaults($defaults);
  584. }
  585. /**
  586. * function which process the creation of exercises
  587. * @param FormValidator $form the formvalidator instance
  588. */
  589. function processCreation($form)
  590. {
  591. $this -> updateTitle($form -> getSubmitValue('exerciseTitle'));
  592. $this -> updateDescription($form -> getSubmitValue('exerciseDescription'));
  593. $this -> updateType($form -> getSubmitValue('exerciseType'));
  594. $this -> setRandom($form -> getSubmitValue('randomQuestions'));
  595. $this -> save();
  596. }
  597. }
  598. endif;
  599. ?>