exercise.class.php 16 KB

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