exercise.class.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. <?php
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. * Exercise class: This class allows to instantiate an object of type Exercise
  5. * @package dokeos.exercise
  6. * @author Olivier Brouckaert
  7. * @version $Id: exercise.class.php 22046 2009-07-14 01:45:19Z ivantcholakov $
  8. */
  9. if(!class_exists('Exercise')):
  10. class Exercise
  11. {
  12. var $id;
  13. var $exercise;
  14. var $description;
  15. var $sound;
  16. var $type;
  17. var $random;
  18. var $active;
  19. var $timeLimit;
  20. var $attempts;
  21. var $feedbacktype;
  22. var $end_time;
  23. var $start_time;
  24. var $questionList; // array with the list of this exercise's questions
  25. var $results_disabled;
  26. var $expired_time;
  27. /**
  28. * constructor of the class
  29. *
  30. * @author - Olivier Brouckaert
  31. */
  32. function Exercise()
  33. {
  34. $this->id=0;
  35. $this->exercise='';
  36. $this->description='';
  37. $this->sound='';
  38. $this->type=1;
  39. $this->random=0;
  40. $this->active=1;
  41. $this->questionList=array();
  42. $this->timeLimit = 0;
  43. $this->end_time = '0000-00-00 00:00:00';
  44. $this->start_time = '0000-00-00 00:00:00';
  45. $this->results_disabled =1;
  46. $this->expired_time = '0000-00-00 00:00:00';
  47. }
  48. /**
  49. * reads exercise informations from the data base
  50. *
  51. * @author - Olivier Brouckaert
  52. * @param - integer $id - exercise ID
  53. * @return - boolean - true if exercise exists, otherwise false
  54. */
  55. function read($id)
  56. {
  57. global $_course;
  58. global $_configuration;
  59. global $questionList;
  60. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  61. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  62. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  63. #$TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  64. $sql="SELECT title,description,sound,type,random,active, results_disabled, max_attempt,start_time,end_time,feedback_type,expired_time FROM $TBL_EXERCICES WHERE id='".Database::escape_string($id)."'";
  65. $result=Database::query($sql,__FILE__,__LINE__);
  66. // if the exercise has been found
  67. if($object=Database::fetch_object($result))
  68. {
  69. $this->id=$id;
  70. $this->exercise=$object->title;
  71. $this->description=$object->description;
  72. $this->sound=$object->sound;
  73. $this->type=$object->type;
  74. $this->random=$object->random;
  75. $this->active=$object->active;
  76. $this->results_disabled =$object->results_disabled;
  77. $this->attempts = $object->max_attempt;
  78. $this->feedbacktype = $object->feedback_type;
  79. $this->end_time = $object->end_time;
  80. $this->start_time = $object->start_time;
  81. $this->expired_time = $object->expired_time;
  82. $sql="SELECT question_id, question_order FROM $TBL_EXERCICE_QUESTION,$TBL_QUESTIONS WHERE question_id=id AND exercice_id='".Database::escape_string($id)."' ORDER BY question_order";
  83. $result=Database::query($sql,__FILE__,__LINE__);
  84. // fills the array with the question ID for this exercise
  85. // the key of the array is the question position
  86. while($object=Database::fetch_object($result))
  87. {
  88. // makes sure that the question position is unique
  89. while(isset($this->questionList[$object->question_order]))
  90. {
  91. $object->question_order++;
  92. }
  93. $this->questionList[$object->question_order]=$object->question_id;
  94. }
  95. //var_dump($this->end_time,$object->start_time);
  96. if($this->random > 0){
  97. $this->questionList = $this->selectRandomList();
  98. }
  99. //overload questions list with recorded questions list
  100. //load questions only for exercises of type 'one question per page'
  101. //this is needed only is there is no questions
  102. //
  103. if($this->type == 2 && $_configuration['live_exercise_tracking']==true && $_SERVER['REQUEST_METHOD']!='POST' && defined('QUESTION_LIST_ALREADY_LOGGED'))
  104. {
  105. //if(empty($_SESSION['questionList']))
  106. $this->questionList = $questionList;
  107. }
  108. return true;
  109. }
  110. // exercise not found
  111. return false;
  112. }
  113. /**
  114. * returns the exercise ID
  115. *
  116. * @author - Olivier Brouckaert
  117. * @return - integer - exercise ID
  118. */
  119. function selectId()
  120. {
  121. return $this->id;
  122. }
  123. /**
  124. * returns the exercise title
  125. *
  126. * @author - Olivier Brouckaert
  127. * @return - string - exercise title
  128. */
  129. function selectTitle()
  130. {
  131. return $this->exercise;
  132. }
  133. /**
  134. * returns the number of attempts setted
  135. *
  136. * @return - numeric - exercise attempts
  137. */
  138. function selectAttempts()
  139. {
  140. return $this->attempts;
  141. }
  142. /** returns the number of FeedbackType *
  143. * 0=>Feedback , 1=>DirectFeedback, 2=>NoFeedback
  144. * @return - numeric - exercise attempts
  145. */
  146. function selectFeedbackType()
  147. {
  148. return $this->feedbacktype;
  149. }
  150. /**
  151. * returns the time limit
  152. */
  153. function selectTimeLimit()
  154. {
  155. return $this->timeLimit;
  156. }
  157. /**
  158. * returns the exercise description
  159. *
  160. * @author - Olivier Brouckaert
  161. * @return - string - exercise description
  162. */
  163. function selectDescription()
  164. {
  165. return $this->description;
  166. }
  167. /**
  168. * returns the exercise sound file
  169. *
  170. * @author - Olivier Brouckaert
  171. * @return - string - exercise description
  172. */
  173. function selectSound()
  174. {
  175. return $this->sound;
  176. }
  177. /**
  178. * returns the exercise type
  179. *
  180. * @author - Olivier Brouckaert
  181. * @return - integer - exercise type
  182. */
  183. function selectType()
  184. {
  185. return $this->type;
  186. }
  187. /**
  188. * tells if questions are selected randomly, and if so returns the draws
  189. *
  190. * @author - Carlos Vargas
  191. * @return - integer - results disabled exercise
  192. */
  193. function selectResultsDisabled()
  194. {
  195. return $this->results_disabled;
  196. }
  197. /**
  198. * tells if questions are selected randomly, and if so returns the draws
  199. *
  200. * @author - Olivier Brouckaert
  201. * @return - integer - 0 if not random, otherwise the draws
  202. */
  203. function isRandom()
  204. {
  205. if($this->random > 0){
  206. return true;
  207. }
  208. else{
  209. return false;
  210. }
  211. }
  212. /**
  213. * Same as isRandom() but has a name applied to values different than 0 or 1
  214. */
  215. function getShuffle()
  216. {
  217. return $this->random;
  218. }
  219. /**
  220. * returns the exercise status (1 = enabled ; 0 = disabled)
  221. *
  222. * @author - Olivier Brouckaert
  223. * @return - boolean - true if enabled, otherwise false
  224. */
  225. function selectStatus()
  226. {
  227. return $this->active;
  228. }
  229. /**
  230. * returns the array with the question ID list
  231. *
  232. * @author - Olivier Brouckaert
  233. * @return - array - question ID list
  234. */
  235. function selectQuestionList()
  236. {
  237. return $this->questionList;
  238. }
  239. /**
  240. * returns the number of questions in this exercise
  241. *
  242. * @author - Olivier Brouckaert
  243. * @return - integer - number of questions
  244. */
  245. function selectNbrQuestions()
  246. {
  247. return sizeof($this->questionList);
  248. }
  249. /**
  250. * selects questions randomly in the question list
  251. *
  252. * @author - Olivier Brouckaert
  253. * @return - array - if the exercise is not set to take questions randomly, returns the question list
  254. * without randomizing, otherwise, returns the list with questions selected randomly
  255. */
  256. function selectRandomList()
  257. {
  258. $nbQuestions = $this->selectNbrQuestions();
  259. $temp_list = $this->questionList;
  260. //error_log(print_r($temp_list,true));
  261. //error_log(count($temp_list));
  262. if (count($temp_list)<>0) {
  263. shuffle($temp_list);
  264. return array_combine(range(1,$nbQuestions),$temp_list);
  265. }
  266. $nbQuestions = $this->selectNbrQuestions();
  267. //Not a random exercise, or if there are not at least 2 questions
  268. if($this->random == 0 || $nbQuestions < 2)
  269. {
  270. return $this->questionList;
  271. }
  272. $randQuestionList = array();
  273. $alreadyChosen = array();
  274. for($i=0; $i < $this->random; $i++)
  275. {
  276. if($i < $nbQuestions){
  277. do
  278. {
  279. $rand=rand(1,$nbQuestions);
  280. }
  281. while(in_array($rand,$alreadyChosen));
  282. $alreadyChosen[]=$rand;
  283. $randQuestionList[$rand] = $this->questionList[$rand];
  284. }
  285. }
  286. return $randQuestionList;
  287. }
  288. /**
  289. * returns 'true' if the question ID is in the question list
  290. *
  291. * @author - Olivier Brouckaert
  292. * @param - integer $questionId - question ID
  293. * @return - boolean - true if in the list, otherwise false
  294. */
  295. function isInList($questionId)
  296. {
  297. if (is_array($this->questionList))
  298. return in_array($questionId,$this->questionList);
  299. else
  300. return false;
  301. }
  302. /**
  303. * changes the exercise title
  304. *
  305. * @author - Olivier Brouckaert
  306. * @param - string $title - exercise title
  307. */
  308. function updateTitle($title)
  309. {
  310. $this->exercise=$title;
  311. }
  312. /**
  313. * changes the exercise max attempts
  314. *
  315. * @param - numeric $attempts - exercise max attempts
  316. */
  317. function updateAttempts($attempts)
  318. {
  319. $this->attempts=$attempts;
  320. }
  321. /**
  322. * changes the exercise feedback type
  323. *
  324. * @param - numeric $attempts - exercise max attempts
  325. */
  326. function updateFeedbackType($feedback_type)
  327. {
  328. $this->feedbacktype=$feedback_type;
  329. }
  330. /**
  331. * changes the exercise description
  332. *
  333. * @author - Olivier Brouckaert
  334. * @param - string $description - exercise description
  335. */
  336. function updateDescription($description)
  337. {
  338. $this->description=$description;
  339. }
  340. /**
  341. * changes the exercise description
  342. *
  343. * @author - Isaac flores
  344. * @param - int The expired time of the quiz
  345. */
  346. function updateExpiredTime($expired_time)
  347. {
  348. $this->expired_time = $expired_time;
  349. }
  350. /**
  351. * changes the exercise sound file
  352. *
  353. * @author - Olivier Brouckaert
  354. * @param - string $sound - exercise sound file
  355. * @param - string $delete - ask to delete the file
  356. */
  357. function updateSound($sound,$delete)
  358. {
  359. global $audioPath, $documentPath,$_course, $_user;
  360. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  361. $TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  362. if($sound['size'] && (strstr($sound['type'],'audio') || strstr($sound['type'],'video')))
  363. {
  364. $this->sound=$sound['name'];
  365. if(@move_uploaded_file($sound['tmp_name'],$audioPath.'/'.$this->sound))
  366. {
  367. $query="SELECT 1 FROM $TBL_DOCUMENT "
  368. ." WHERE path='".str_replace($documentPath,'',$audioPath).'/'.$this->sound."'";
  369. $result=Database::query($query,__FILE__,__LINE__);
  370. if(!Database::num_rows($result))
  371. {
  372. /*$query="INSERT INTO $TBL_DOCUMENT(path,filetype) VALUES "
  373. ." ('".str_replace($documentPath,'',$audioPath).'/'.$this->sound."','file')";
  374. Database::query($query,__FILE__,__LINE__);*/
  375. $id = add_document($_course,str_replace($documentPath,'',$audioPath).'/'.$this->sound,'file',$sound['size'],$sound['name']);
  376. //$id = Database::insert_id();
  377. //$time = time();
  378. //$time = date("Y-m-d H:i:s", $time);
  379. // insert into the item_property table, using default visibility of "visible"
  380. /*$query = "INSERT INTO $TBL_ITEM_PROPERTY "
  381. ."(tool, ref, insert_user_id,to_group_id, insert_date, lastedit_date, lastedit_type) "
  382. ." VALUES "
  383. ."('".TOOL_DOCUMENT."', $id, $_user['user_id'], 0, '$time', '$time', 'DocumentAdded' )";
  384. Database::query($query,__FILE__,__LINE__);*/
  385. api_item_property_update($_course, TOOL_DOCUMENT, $id, 'DocumentAdded',$_user['user_id']);
  386. item_property_update_on_folder($_course,str_replace($documentPath,'',$audioPath),$_user['user_id']);
  387. }
  388. }
  389. }
  390. elseif($delete && is_file($audioPath.'/'.$this->sound))
  391. {
  392. $this->sound='';
  393. }
  394. }
  395. /**
  396. * changes the exercise type
  397. *
  398. * @author - Olivier Brouckaert
  399. * @param - integer $type - exercise type
  400. */
  401. function updateType($type)
  402. {
  403. $this->type=$type;
  404. }
  405. /**
  406. * sets to 0 if questions are not selected randomly
  407. * if questions are selected randomly, sets the draws
  408. *
  409. * @author - Olivier Brouckaert
  410. * @param - integer $random - 0 if not random, otherwise the draws
  411. */
  412. function setRandom($random)
  413. {
  414. $this->random=$random;
  415. }
  416. /**
  417. * enables the exercise
  418. *
  419. * @author - Olivier Brouckaert
  420. */
  421. function enable()
  422. {
  423. $this->active=1;
  424. }
  425. /**
  426. * disables the exercise
  427. *
  428. * @author - Olivier Brouckaert
  429. */
  430. function disable()
  431. {
  432. $this->active=0;
  433. }
  434. function disable_results()
  435. {
  436. $this->results_disabled = true;
  437. }
  438. function enable_results()
  439. {
  440. $this->results_disabled = false;
  441. }
  442. function updateResultsDisabled($results_disabled)
  443. {
  444. if ($results_disabled==1){
  445. $this->results_disabled = true;
  446. } else {
  447. $this->results_disabled = false;
  448. }
  449. }
  450. /**
  451. * updates the exercise in the data base
  452. *
  453. * @author - Olivier Brouckaert
  454. */
  455. function save($type_e='')
  456. {
  457. global $_course,$_user;
  458. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  459. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  460. $TBL_QUIZ_QUESTION= Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  461. $id = $this->id;
  462. $exercise = $this->exercise;
  463. $description = $this->description;
  464. $sound = $this->sound;
  465. $type = $this->type;
  466. $attempts = $this->attempts;
  467. $feedbacktype = $this->feedbacktype;
  468. $random = $this->random;
  469. $active = $this->active;
  470. $session_id = api_get_session_id();
  471. $expired_time = $this->expired_time;
  472. if ($feedbacktype==1){
  473. $results_disabled = 1;
  474. } else {
  475. $results_disabled = intval($this->results_disabled);
  476. }
  477. $start_time = Database::escape_string($this->start_time);
  478. $end_time = Database::escape_string($this->end_time);
  479. // exercise already exists
  480. if($id) {
  481. /*
  482. title='".Database::escape_string(Security::remove_XSS($exercise))."',
  483. description='".Database::escape_string(Security::remove_XSS(api_html_entity_decode($description),COURSEMANAGERLOWSECURITY))."'";
  484. */
  485. $sql="UPDATE $TBL_EXERCICES SET
  486. title='".Database::escape_string($exercise)."',
  487. description='".Database::escape_string($description)."'";
  488. if ($type_e != 'simple') {
  489. $sql .= ", sound='".Database::escape_string($sound)."',
  490. type='".Database::escape_string($type)."',
  491. random='".Database::escape_string($random)."',
  492. active='".Database::escape_string($active)."',
  493. feedback_type='".Database::escape_string($feedbacktype)."',
  494. start_time='$start_time',end_time='$end_time',
  495. max_attempt='".Database::escape_string($attempts)."',
  496. expired_time='".Database::escape_string($expired_time)."',
  497. results_disabled='".Database::escape_string($results_disabled)."'";
  498. }
  499. $sql .= " WHERE id='".Database::escape_string($id)."'";
  500. // echo $sql;
  501. Database::query($sql,__FILE__,__LINE__);
  502. // update into the item_property table
  503. api_item_property_update($_course, TOOL_QUIZ, $id,'QuizUpdated',$_user['user_id']);
  504. if (api_get_setting('search_enabled')=='true') {
  505. $this -> search_engine_edit();
  506. }
  507. } else {// creates a new exercise
  508. //add condition by anonymous user
  509. /*if (!api_is_anonymous()) {
  510. //is course manager
  511. $cond1=Database::escape_string($exercise);
  512. $cond2=Database::escape_string($description);
  513. } else {
  514. //is anonymous user
  515. $cond1=Database::escape_string(Security::remove_XSS($exercise));
  516. $cond2=Database::escape_string(Security::remove_XSS(api_html_entity_decode($description),COURSEMANAGERLOWSECURITY));
  517. }*/
  518. $sql="INSERT INTO $TBL_EXERCICES (start_time, end_time, title, description, sound, type, random,active, results_disabled, max_attempt, feedback_type, session_id)
  519. VALUES(
  520. '$start_time','$end_time',
  521. '".Database::escape_string($exercise)."',
  522. '".Database::escape_string($description)."',
  523. '".Database::escape_string($sound)."',
  524. '".Database::escape_string($type)."',
  525. '".Database::escape_string($random)."',
  526. '".Database::escape_string($active)."',
  527. '".Database::escape_string($results_disabled)."',
  528. '".Database::escape_string($attempts)."',
  529. '".Database::escape_string($feedbacktype)."',
  530. '".Database::escape_string($session_id)."'
  531. )";
  532. Database::query($sql,__FILE__,__LINE__);
  533. $this->id=Database::insert_id();
  534. // insert into the item_property table
  535. api_item_property_update($_course, TOOL_QUIZ, $this->id,'QuizAdded',$_user['user_id']);
  536. if (api_get_setting('search_enabled')=='true' && extension_loaded('xapian')) {
  537. $this -> search_engine_save();
  538. }
  539. }
  540. // updates the question position
  541. $this->update_question_positions();
  542. }
  543. function update_question_positions() {
  544. // updates the question position
  545. $TBL_QUIZ_QUESTION= Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  546. foreach($this->questionList as $position=>$questionId)
  547. {
  548. //$sql="UPDATE $TBL_QUESTIONS SET position='".Database::escape_string($position)."' WHERE id='".Database::escape_string($questionId)."'";
  549. $sql="UPDATE $TBL_QUIZ_QUESTION SET question_order='".Database::escape_string($position)."' " .
  550. "WHERE question_id='".Database::escape_string($questionId)."' and exercice_id=".Database::escape_string($this->id)."";
  551. Database::query($sql,__FILE__,__LINE__);
  552. }
  553. }
  554. /**
  555. * moves a question up in the list
  556. *
  557. * @author - Olivier Brouckaert
  558. * @author - Julio Montoya (rewrote the code)
  559. * @param - integer $id - question ID to move up
  560. */
  561. function moveUp($id)
  562. {
  563. // there is a bug with some version of PHP with the key and prev functions
  564. // the script commented was tested in dev.dokeos.com with no success
  565. // Instead of using prev and next this was change with arrays.
  566. /*
  567. foreach($this->questionList as $position=>$questionId)
  568. {
  569. // if question ID found
  570. if($questionId == $id)
  571. {
  572. // position of question in the array
  573. echo $pos1=$position; //1
  574. echo "<br>";
  575. prev($this->questionList);
  576. prev($this->questionList);
  577. // position of previous question in the array
  578. $pos2=key($this->questionList);
  579. //if the cursor of the array hit the end
  580. // then we must reset the array to get the previous key
  581. if($pos2===null)
  582. {
  583. end($this->questionList);
  584. prev($this->questionList);
  585. $pos2=key($this->questionList);
  586. }
  587. // error, can't move question
  588. if(!$pos2)
  589. {
  590. //echo 'cant move!';
  591. $pos2=key($this->questionList);
  592. reset($this->questionList);
  593. }
  594. $id2=$this->questionList[$pos2];
  595. // exits foreach()
  596. break;
  597. }
  598. $i++;
  599. }
  600. */
  601. $question_list =array();
  602. foreach($this->questionList as $position=>$questionId)
  603. {
  604. $question_list[]= $questionId;
  605. }
  606. $len=count($question_list);
  607. $orderlist=array_keys($this->questionList);
  608. for($i=0;$i<$len;$i++)
  609. {
  610. $questionId = $question_list[$i];
  611. if($questionId == $id)
  612. {
  613. // position of question in the array
  614. $pos1=$orderlist[$i];
  615. $pos2=$orderlist[$i-1];
  616. if($pos2===null)
  617. {
  618. $pos2 = $orderlist[$len-1];
  619. }
  620. // error, can't move question
  621. if(!$pos2)
  622. {
  623. $pos2=$orderlist[0];
  624. $i=0;
  625. }
  626. break;
  627. }
  628. }
  629. // permutes questions in the array
  630. $temp=$this->questionList[$pos2];
  631. $this->questionList[$pos2]=$this->questionList[$pos1];
  632. $this->questionList[$pos1]=$temp;
  633. }
  634. /**
  635. * moves a question down in the list
  636. *
  637. * @author - Olivier Brouckaert
  638. * @param - integer $id - question ID to move down
  639. */
  640. function moveDown($id)
  641. {
  642. // there is a bug with some version of PHP with the key and prev functions
  643. // the script commented was tested in dev.dokeos.com with no success
  644. // Instead of using prev and next this was change with arrays.
  645. /*
  646. foreach($this->questionList as $position=>$questionId)
  647. {
  648. // if question ID found
  649. if($questionId == $id)
  650. {
  651. // position of question in the array
  652. $pos1=$position;
  653. //next($this->questionList);
  654. // position of next question in the array
  655. $pos2=key($this->questionList);
  656. // error, can't move question
  657. if(!$pos2)
  658. {
  659. //echo 'cant move!';
  660. return;
  661. }
  662. $id2=$this->questionList[$pos2];
  663. // exits foreach()
  664. break;
  665. }
  666. }
  667. */
  668. $question_list =array();
  669. foreach($this->questionList as $position=>$questionId)
  670. {
  671. $question_list[]= $questionId;
  672. }
  673. $len=count($question_list);
  674. $orderlist=array_keys($this->questionList);
  675. for($i=0;$i<$len;$i++)
  676. {
  677. $questionId = $question_list[$i];
  678. if($questionId == $id)
  679. {
  680. $pos1=$orderlist[$i+1];
  681. $pos2 =$orderlist[$i];
  682. if(!$pos2)
  683. {
  684. //echo 'cant move!';
  685. }
  686. break;
  687. }
  688. }
  689. // permutes questions in the array
  690. $temp=$this->questionList[$pos2];
  691. $this->questionList[$pos2]=$this->questionList[$pos1];
  692. $this->questionList[$pos1]=$temp;
  693. }
  694. /**
  695. * adds a question into the question list
  696. *
  697. * @author - Olivier Brouckaert
  698. * @param - integer $questionId - question ID
  699. * @return - boolean - true if the question has been added, otherwise false
  700. */
  701. function addToList($questionId)
  702. {
  703. // checks if the question ID is not in the list
  704. if(!$this->isInList($questionId))
  705. {
  706. // selects the max position
  707. if(!$this->selectNbrQuestions())
  708. {
  709. $pos=1;
  710. }
  711. else
  712. {
  713. if (is_array($this->questionList))
  714. $pos=max(array_keys($this->questionList))+1;
  715. }
  716. $this->questionList[$pos]=$questionId;
  717. return true;
  718. }
  719. return false;
  720. }
  721. /**
  722. * removes a question from the question list
  723. *
  724. * @author - Olivier Brouckaert
  725. * @param - integer $questionId - question ID
  726. * @return - boolean - true if the question has been removed, otherwise false
  727. */
  728. function removeFromList($questionId)
  729. {
  730. // searches the position of the question ID in the list
  731. $pos=array_search($questionId,$this->questionList);
  732. // question not found
  733. if($pos === false)
  734. {
  735. return false;
  736. }
  737. else
  738. {
  739. // deletes the position from the array containing the wanted question ID
  740. unset($this->questionList[$pos]);
  741. return true;
  742. }
  743. }
  744. /**
  745. * deletes the exercise from the database
  746. * Notice : leaves the question in the data base
  747. *
  748. * @author - Olivier Brouckaert
  749. */
  750. function delete(){
  751. global $_course,$_user;
  752. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  753. $sql="UPDATE $TBL_EXERCICES SET active='-1' WHERE id='".Database::escape_string($this->id)."'";
  754. Database::query($sql);
  755. api_item_property_update($_course, TOOL_QUIZ, $this->id,'QuizDeleted',$_user['user_id']);
  756. if (api_get_setting('search_enabled')=='true' && extension_loaded('xapian') ) {
  757. $this -> search_engine_delete();
  758. }
  759. }
  760. /**
  761. * Creates the form to create / edit an exercise
  762. * @param FormValidator $form the formvalidator instance (by reference)
  763. */
  764. function createForm ($form, $type='full')
  765. {
  766. global $id;
  767. if(empty($type)){
  768. $type='full';
  769. }
  770. // form title
  771. if (!empty($_GET['exerciseId'])) {
  772. $form_title = get_lang('ModifyExercise');
  773. } else {
  774. $form_title = get_lang('NewEx');
  775. }
  776. $form->addElement('header', '', $form_title);
  777. // title
  778. $form -> addElement('text', 'exerciseTitle', get_lang('ExerciseName'),'class="input_titles"');
  779. //$form->applyFilter('exerciseTitle','html_filter');
  780. $form -> addElement('html','<div class="row">
  781. <div class="label"></div>
  782. <div class="formw" style="height:50px">
  783. <a href="javascript://" onclick=" return show_media()"> <span id="media_icon"> <img style="vertical-align: middle;" src="../img/looknfeel.png" alt="" />&nbsp;'.get_lang('ExerciseDescription').'</span></a>
  784. </div>
  785. </div>');
  786. $editor_config = array('ToolbarSet' => 'TestQuestionDescription', 'Width' => '100%', 'Height' => '150');
  787. if(is_array($type)){
  788. $editor_config = array_merge($editor_config, $type);
  789. }
  790. $form -> addElement ('html','<div class="HideFCKEditor" id="HiddenFCKexerciseDescription" >');
  791. $form -> add_html_editor('exerciseDescription', get_lang('langExerciseDescription'), false, false, $editor_config);
  792. $form -> addElement ('html','</div>');
  793. $form -> addElement('html','<div class="row">
  794. <div class="label">&nbsp;</div>
  795. <div class="formw">
  796. <a href="javascript://" onclick=" return advanced_parameters()"><span id="img_plus_and_minus"><div style="vertical-align:top;" ><img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'</div></span></a>
  797. </div>
  798. </div>');
  799. // Random questions
  800. $form -> addElement('html','<div id="options" style="display:none">');
  801. if($type=='full') {
  802. // feedback type
  803. $radios_feedback = array();
  804. $radios_feedback[] = FormValidator :: createElement ('radio', 'exerciseFeedbackType', null, get_lang('ExerciseAtTheEndOfTheTest'),'0');
  805. $radios_feedback[] = FormValidator :: createElement ('radio', 'exerciseFeedbackType', null, get_lang('NoFeedback'),'2');
  806. $form -> addGroup($radios_feedback, null, get_lang('FeedbackType'));
  807. $feedback_option[0]=get_lang('ExerciseAtTheEndOfTheTest');
  808. $feedback_option[1]=get_lang('DirectFeedback');
  809. $feedback_option[2]=get_lang('NoFeedback');
  810. //Can't modify a DirectFeedback question
  811. if ($this->selectFeedbackType() != 1 ) {
  812. // $form -> addElement('select', 'exerciseFeedbackType',get_lang('FeedbackType'),$feedback_option,'onchange="javascript:feedbackselection()"');
  813. // test type
  814. $radios = array();
  815. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('QuestionsPerPageOne'),'2');
  816. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('QuestionsPerPageAll'),'1');
  817. $form -> addGroup($radios, null, get_lang('QuestionsPerPage'));
  818. } else {
  819. // if is Directfeedback but has not questions we can allow to modify the question type
  820. if ($this->selectNbrQuestions()== 0) {
  821. $form -> addElement('select', 'exerciseFeedbackType',get_lang('FeedbackType'),$feedback_option,'onchange="javascript:feedbackselection()"');
  822. // test type
  823. $radios = array();
  824. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('SimpleExercise'),'1');
  825. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('SequentialExercise'),'2');
  826. $form -> addGroup($radios, null, get_lang('ExerciseType'));
  827. } else {
  828. //we force the options to the DirectFeedback exercisetype
  829. $form -> addElement('hidden', 'exerciseFeedbackType','1');
  830. $form -> addElement('hidden', 'exerciseType','2');
  831. }
  832. }
  833. $radios_results_disabled = array();
  834. $radios_results_disabled[] = FormValidator :: createElement ('radio', 'results_disabled', null, get_lang('Yes'),'0');
  835. $radios_results_disabled[] = FormValidator :: createElement ('radio', 'results_disabled', null, get_lang('No'),'1');
  836. $form -> addGroup($radios_results_disabled, null, get_lang('ShowResultsToStudents'));
  837. $random = array();
  838. $option=array();
  839. $max = ($this->id > 0) ? $this->selectNbrQuestions() : 10 ;
  840. $option = range(0,$max);
  841. $option[0]=get_lang('No');
  842. $random[] = FormValidator :: createElement ('select', 'randomQuestions',null,$option);
  843. $random[] = FormValidator :: createElement ('static', 'help','help','<span style="font-style: italic;">'.get_lang('RandomQuestionsHelp').'</span>');
  844. //$random[] = FormValidator :: createElement ('text', 'randomQuestions', null,null,'0');
  845. $form -> addGroup($random,null,get_lang('RandomQuestions'),'<br />');
  846. $attempt_option=range(0,10);
  847. $attempt_option[0]=get_lang('Infinite');
  848. $form -> addElement('select', 'exerciseAttempts',get_lang('ExerciseAttempts'),$attempt_option);
  849. $form -> addElement('checkbox', 'enabletimelimit',get_lang('EnableTimeLimits'),null,'onclick = " return timelimit() "');
  850. $var= Exercise::selectTimeLimit();
  851. if(($this -> start_time!='0000-00-00 00:00:00')||($this -> end_time!='0000-00-00 00:00:00'))
  852. $form -> addElement('html','<div id="options2" style="display:block;">');
  853. else
  854. $form -> addElement('html','<div id="options2" style="display:none;">');
  855. //$form -> addElement('date', 'start_time', get_lang('ExeStartTime'), array('language'=>'es','format' => 'dMYHi'));
  856. //$form -> addElement('date', 'end_time', get_lang('ExeEndTime'), array('language'=>'es','format' => 'dMYHi'));
  857. $form->addElement('datepicker', 'start_time', get_lang('ExeStartTime'), array('form_name'=>'exercise_admin'));
  858. $form->addElement('datepicker', 'end_time', get_lang('ExeEndTime'), array('form_name'=>'exercise_admin'));
  859. //Timer control
  860. $time_hours_option = range(0,12);
  861. $time_minutes_option = range(0,59);
  862. $form -> addElement('checkbox', 'enabletimercontrol',get_lang('EnableTimerControl'),null,'onclick = "option_time_expired()"');
  863. $expired_date = (int)$this->selectExpiredTime();
  864. if(($expired_date!='0')) {
  865. $form -> addElement('html','<div id="timercontrol" style="display:block;">');
  866. } else {
  867. $form -> addElement('html','<div id="timercontrol" style="display:none;">');
  868. }
  869. $form -> addElement('text', 'enabletimercontroltotalminutes',get_lang('ExerciseTotalDurationInMinutes'),array('style' => 'width : 35px'));
  870. //$form -> addElement('select', 'enabletimercontroltotalminutes',get_lang('ExerciseTimerControlMinutes'),$time_minutes_option);
  871. $form -> addElement('html','</div>');
  872. $form -> addElement('html','</div>');
  873. //$form -> addElement('text', 'exerciseAttempts', get_lang('ExerciseAttempts').' : ',array('size'=>'2'));
  874. $form -> addElement('html','</div>'); //End advanced setting
  875. $defaults = array();
  876. if (api_get_setting('search_enabled') === 'true') {
  877. require_once(api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  878. $form -> addElement ('checkbox', 'index_document','', get_lang('SearchFeatureDoIndexDocument'));
  879. $form -> addElement ('html','<br /><div class="row">');
  880. $form -> addElement ('html', '<div class="label">'. get_lang('SearchFeatureDocumentLanguage') .'</div>');
  881. $form -> addElement ('html', '<div class="formw">'. api_get_languages_combo() .'</div>');
  882. $form -> addElement ('html','</div><div class="sub-form">');
  883. $specific_fields = get_specific_field_list();
  884. foreach ($specific_fields as $specific_field) {
  885. $form -> addElement ('text', $specific_field['code'], $specific_field['name']);
  886. $filter = array('course_code'=> "'". api_get_course_id() ."'", 'field_id' => $specific_field['id'], 'ref_id' => $this->id, 'tool_id' => '\''. TOOL_QUIZ .'\'');
  887. $values = get_specific_field_values_list($filter, array('value'));
  888. if ( !empty($values) ) {
  889. $arr_str_values = array();
  890. foreach ($values as $value) {
  891. $arr_str_values[] = $value['value'];
  892. }
  893. $defaults[$specific_field['code']] = implode(', ', $arr_str_values);
  894. }
  895. }
  896. $form -> addElement ('html','</div>');
  897. }
  898. }
  899. // submit
  900. isset($_GET['exerciseId'])?$text=get_lang('ModifyExercise'):$text=get_lang('ProcedToQuestions');
  901. $form -> addElement('html', '<br /><br />');
  902. $form -> addElement('style_submit_button', 'submitExercise', $text, 'class="save"');
  903. $form -> addRule ('exerciseTitle', get_lang('GiveExerciseName'), 'required');
  904. if($type=='full') {
  905. // rules
  906. $form -> addRule ('exerciseAttempts', get_lang('Numeric'), 'numeric');
  907. $form -> addRule ('start_time', get_lang('InvalidDate'), 'date');
  908. $form -> addRule ('end_time', get_lang('InvalidDate'), 'date');
  909. $form -> addRule(array ('start_time', 'end_time'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  910. }
  911. // defaults
  912. if($type=='full') {
  913. if($this -> id > 0) {
  914. if ($this -> random > $this->selectNbrQuestions()) {
  915. $defaults['randomQuestions'] = $this->selectNbrQuestions();
  916. } else {
  917. $defaults['randomQuestions'] = $this -> random;
  918. }
  919. $defaults['exerciseType'] = $this -> selectType();
  920. $defaults['exerciseTitle'] = $this -> selectTitle();
  921. $defaults['exerciseDescription'] = $this -> selectDescription();
  922. $defaults['exerciseAttempts'] = $this->selectAttempts();
  923. $defaults['exerciseFeedbackType'] = $this->selectFeedbackType();
  924. $defaults['results_disabled'] = $this->selectResultsDisabled();
  925. if(($this -> start_time!='0000-00-00 00:00:00')||($this -> end_time!='0000-00-00 00:00:00'))
  926. $defaults['enabletimelimit'] = 1;
  927. $defaults['start_time'] = ($this->start_time!='0000-00-00 00:00:00')? $this -> start_time : date('Y-m-d 12:00:00');
  928. $defaults['end_time'] = ($this->end_time!='0000-00-00 00:00:00')?$this -> end_time : date('Y-m-d 12:00:00',time()+84600);
  929. //Get expired time
  930. if($this -> expired_time != '0') {
  931. $defaults['enabletimercontrol'] = 1;
  932. $defaults['enabletimercontroltotalminutes'] = $this -> expired_time;
  933. } else {
  934. $defaults['enabletimercontroltotalminutes'] = 0;
  935. }
  936. } else {
  937. $defaults['exerciseType'] = 2;
  938. $defaults['exerciseAttempts'] = 0;
  939. $defaults['randomQuestions'] = 0;
  940. $defaults['exerciseDescription'] = '';
  941. $defaults['exerciseFeedbackType'] = 0;
  942. $defaults['results_disabled'] = 0;
  943. $defaults['start_time'] = date('Y-m-d 12:00:00');
  944. $defaults['end_time'] = date('Y-m-d 12:00:00',time()+84600);
  945. }
  946. } else {
  947. $defaults['exerciseTitle'] = $this -> selectTitle();
  948. $defaults['exerciseDescription'] = $this -> selectDescription();
  949. }
  950. if (api_get_setting('search_enabled') === 'true') {
  951. $defaults['index_document'] = 'checked="checked"';
  952. }
  953. $form -> setDefaults($defaults);
  954. }
  955. /**
  956. * function which process the creation of exercises
  957. * @param FormValidator $form the formvalidator instance
  958. */
  959. function processCreation($form,$type='')
  960. {
  961. $this -> updateTitle($form -> getSubmitValue('exerciseTitle'));
  962. $this -> updateDescription($form -> getSubmitValue('exerciseDescription'));
  963. $this -> updateAttempts($form -> getSubmitValue('exerciseAttempts'));
  964. $this -> updateFeedbackType($form -> getSubmitValue('exerciseFeedbackType'));
  965. $this -> updateType($form -> getSubmitValue('exerciseType'));
  966. $this -> setRandom($form -> getSubmitValue('randomQuestions'));
  967. $this -> updateResultsDisabled($form -> getSubmitValue('results_disabled'));
  968. $this -> updateExpiredTime($form -> getSubmitValue('enabletimercontroltotalminutes'));
  969. if($form -> getSubmitValue('enabletimelimit')==1) {
  970. $start_time = $form -> getSubmitValue('start_time');
  971. $this->start_time = $start_time['Y'].'-'.$start_time['F'].'-'.$start_time['d'].' '.$start_time['H'].':'.$start_time['i'].':00';
  972. $end_time = $form -> getSubmitValue('end_time');
  973. $this->end_time = $end_time['Y'].'-'.$end_time['F'].'-'.$end_time['d'].' '.$end_time['H'].':'.$end_time['i'].':00';
  974. } else {
  975. $this->start_time = '0000-00-00 00:00:00';
  976. $this->end_time = '0000-00-00 00:00:00';
  977. }
  978. if($form -> getSubmitValue('enabletimercontrol') == 1) {
  979. $expired_total_time = $form -> getSubmitValue('enabletimercontroltotalminutes');
  980. if ($this->expired_time == 0) {
  981. $this->expired_time = $expired_total_time;
  982. }
  983. } else {
  984. $this->expired_time = 0;
  985. }
  986. $this -> save($type);
  987. }
  988. function search_engine_save() {
  989. if ($_POST['index_document'] != 1) {
  990. return;
  991. }
  992. $course_id = api_get_course_id();
  993. require_once(api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php');
  994. require_once(api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php');
  995. require_once(api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  996. $specific_fields = get_specific_field_list();
  997. $ic_slide = new IndexableChunk();
  998. $all_specific_terms = '';
  999. foreach ($specific_fields as $specific_field) {
  1000. if (isset($_REQUEST[$specific_field['code']])) {
  1001. $sterms = trim($_REQUEST[$specific_field['code']]);
  1002. if (!empty($sterms)) {
  1003. $all_specific_terms .= ' '. $sterms;
  1004. $sterms = explode(',', $sterms);
  1005. foreach ($sterms as $sterm) {
  1006. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  1007. add_specific_field_value($specific_field['id'], $course_id, TOOL_QUIZ, $this->id, $sterm);
  1008. }
  1009. }
  1010. }
  1011. }
  1012. // build the chunk to index
  1013. $ic_slide->addValue("title", $this->exercise);
  1014. $ic_slide->addCourseId($course_id);
  1015. $ic_slide->addToolId(TOOL_QUIZ);
  1016. $xapian_data = array(
  1017. SE_COURSE_ID => $course_id,
  1018. SE_TOOL_ID => TOOL_QUIZ,
  1019. SE_DATA => array('type' => SE_DOCTYPE_EXERCISE_EXERCISE, 'exercise_id' => (int)$this->id),
  1020. SE_USER => (int)api_get_user_id(),
  1021. );
  1022. $ic_slide->xapian_data = serialize($xapian_data);
  1023. $exercise_description = $all_specific_terms .' '. $this->description;
  1024. $ic_slide->addValue("content", $exercise_description);
  1025. $di = new DokeosIndexer();
  1026. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  1027. $di->connectDb(NULL, NULL, $lang);
  1028. $di->addChunk($ic_slide);
  1029. //index and return search engine document id
  1030. $did = $di->index();
  1031. if ($did) {
  1032. // save it to db
  1033. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1034. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  1035. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  1036. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id, $did);
  1037. Database::query($sql,__FILE__,__LINE__);
  1038. }
  1039. }
  1040. function search_engine_edit() {
  1041. // update search enchine and its values table if enabled
  1042. if (api_get_setting('search_enabled')=='true' && extension_loaded('xapian')) {
  1043. $course_id = api_get_course_id();
  1044. // actually, it consists on delete terms from db, insert new ones, create a new search engine document, and remove the old one
  1045. // get search_did
  1046. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1047. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  1048. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  1049. $res = Database::query($sql, __FILE__, __LINE__);
  1050. if (Database::num_rows($res) > 0) {
  1051. require_once(api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php');
  1052. require_once(api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php');
  1053. require_once(api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  1054. $se_ref = Database::fetch_array($res);
  1055. $specific_fields = get_specific_field_list();
  1056. $ic_slide = new IndexableChunk();
  1057. $all_specific_terms = '';
  1058. foreach ($specific_fields as $specific_field) {
  1059. delete_all_specific_field_value($course_id, $specific_field['id'], TOOL_QUIZ, $this->id);
  1060. if (isset($_REQUEST[$specific_field['code']])) {
  1061. $sterms = trim($_REQUEST[$specific_field['code']]);
  1062. $all_specific_terms .= ' '. $sterms;
  1063. $sterms = explode(',', $sterms);
  1064. foreach ($sterms as $sterm) {
  1065. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  1066. add_specific_field_value($specific_field['id'], $course_id, TOOL_QUIZ, $this->id, $sterm);
  1067. }
  1068. }
  1069. }
  1070. // build the chunk to index
  1071. $ic_slide->addValue("title", $this->exercise);
  1072. $ic_slide->addCourseId($course_id);
  1073. $ic_slide->addToolId(TOOL_QUIZ);
  1074. $xapian_data = array(
  1075. SE_COURSE_ID => $course_id,
  1076. SE_TOOL_ID => TOOL_QUIZ,
  1077. SE_DATA => array('type' => SE_DOCTYPE_EXERCISE_EXERCISE, 'exercise_id' => (int)$this->id),
  1078. SE_USER => (int)api_get_user_id(),
  1079. );
  1080. $ic_slide->xapian_data = serialize($xapian_data);
  1081. $exercise_description = $all_specific_terms .' '. $this->description;
  1082. $ic_slide->addValue("content", $exercise_description);
  1083. $di = new DokeosIndexer();
  1084. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  1085. $di->connectDb(NULL, NULL, $lang);
  1086. $di->remove_document((int)$se_ref['search_did']);
  1087. $di->addChunk($ic_slide);
  1088. //index and return search engine document id
  1089. $did = $di->index();
  1090. if ($did) {
  1091. // save it to db
  1092. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\'';
  1093. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  1094. Database::query($sql,__FILE__,__LINE__);
  1095. //var_dump($sql);
  1096. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  1097. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  1098. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id, $did);
  1099. Database::query($sql,__FILE__,__LINE__);
  1100. }
  1101. }
  1102. }
  1103. }
  1104. function search_engine_delete() {
  1105. // remove from search engine if enabled
  1106. if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian') ) {
  1107. $course_id = api_get_course_id();
  1108. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1109. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level IS NULL LIMIT 1';
  1110. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  1111. $res = Database::query($sql, __FILE__, __LINE__);
  1112. if (Database::num_rows($res) > 0) {
  1113. $row = Database::fetch_array($res);
  1114. require_once(api_get_path(LIBRARY_PATH) .'search/DokeosIndexer.class.php');
  1115. $di = new DokeosIndexer();
  1116. $di->remove_document((int)$row['search_did']);
  1117. unset($di);
  1118. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1119. foreach ( $this->questionList as $question_i) {
  1120. $sql = 'SELECT type FROM %s WHERE id=%s';
  1121. $sql = sprintf($sql, $tbl_quiz_question, $question_i);
  1122. $qres = Database::query($sql, __FILE__, __LINE__);
  1123. if (Database::num_rows($qres) > 0) {
  1124. $qrow = Database::fetch_array($qres);
  1125. $objQuestion = Question::getInstance($qrow['type']);
  1126. $objQuestion = Question::read((int)$question_i);
  1127. $objQuestion->search_engine_edit($this->id, FALSE, TRUE);
  1128. unset($objQuestion);
  1129. }
  1130. }
  1131. }
  1132. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level IS NULL LIMIT 1';
  1133. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  1134. Database::query($sql, __FILE__, __LINE__);
  1135. // remove terms from db
  1136. require_once(api_get_path(LIBRARY_PATH) .'specific_fields_manager.lib.php');
  1137. delete_all_values_for_item($course_id, TOOL_QUIZ, $this->id);
  1138. }
  1139. }
  1140. function selectExpiredTime() {
  1141. return $this->expired_time;
  1142. }
  1143. }
  1144. endif;
  1145. ?>