exercise.class.php 16 KB

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