exercise.class.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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, expired_time, 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($expired_time)."',
  531. '".Database::escape_string($session_id)."'
  532. )";
  533. Database::query($sql,__FILE__,__LINE__);
  534. $this->id=Database::insert_id();
  535. // insert into the item_property table
  536. api_item_property_update($_course, TOOL_QUIZ, $this->id,'QuizAdded',$_user['user_id']);
  537. if (api_get_setting('search_enabled')=='true' && extension_loaded('xapian')) {
  538. $this -> search_engine_save();
  539. }
  540. }
  541. // updates the question position
  542. $this->update_question_positions();
  543. }
  544. function update_question_positions() {
  545. // updates the question position
  546. $TBL_QUIZ_QUESTION= Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  547. foreach($this->questionList as $position=>$questionId)
  548. {
  549. //$sql="UPDATE $TBL_QUESTIONS SET position='".Database::escape_string($position)."' WHERE id='".Database::escape_string($questionId)."'";
  550. $sql="UPDATE $TBL_QUIZ_QUESTION SET question_order='".Database::escape_string($position)."' " .
  551. "WHERE question_id='".Database::escape_string($questionId)."' and exercice_id=".Database::escape_string($this->id)."";
  552. Database::query($sql,__FILE__,__LINE__);
  553. }
  554. }
  555. /**
  556. * moves a question up in the list
  557. *
  558. * @author - Olivier Brouckaert
  559. * @author - Julio Montoya (rewrote the code)
  560. * @param - integer $id - question ID to move up
  561. */
  562. function moveUp($id)
  563. {
  564. // there is a bug with some version of PHP with the key and prev functions
  565. // the script commented was tested in dev.dokeos.com with no success
  566. // Instead of using prev and next this was change with arrays.
  567. /*
  568. foreach($this->questionList as $position=>$questionId)
  569. {
  570. // if question ID found
  571. if($questionId == $id)
  572. {
  573. // position of question in the array
  574. echo $pos1=$position; //1
  575. echo "<br>";
  576. prev($this->questionList);
  577. prev($this->questionList);
  578. // position of previous question in the array
  579. $pos2=key($this->questionList);
  580. //if the cursor of the array hit the end
  581. // then we must reset the array to get the previous key
  582. if($pos2===null)
  583. {
  584. end($this->questionList);
  585. prev($this->questionList);
  586. $pos2=key($this->questionList);
  587. }
  588. // error, can't move question
  589. if(!$pos2)
  590. {
  591. //echo 'cant move!';
  592. $pos2=key($this->questionList);
  593. reset($this->questionList);
  594. }
  595. $id2=$this->questionList[$pos2];
  596. // exits foreach()
  597. break;
  598. }
  599. $i++;
  600. }
  601. */
  602. $question_list =array();
  603. foreach($this->questionList as $position=>$questionId)
  604. {
  605. $question_list[]= $questionId;
  606. }
  607. $len=count($question_list);
  608. $orderlist=array_keys($this->questionList);
  609. for($i=0;$i<$len;$i++)
  610. {
  611. $questionId = $question_list[$i];
  612. if($questionId == $id)
  613. {
  614. // position of question in the array
  615. $pos1=$orderlist[$i];
  616. $pos2=$orderlist[$i-1];
  617. if($pos2===null)
  618. {
  619. $pos2 = $orderlist[$len-1];
  620. }
  621. // error, can't move question
  622. if(!$pos2)
  623. {
  624. $pos2=$orderlist[0];
  625. $i=0;
  626. }
  627. break;
  628. }
  629. }
  630. // permutes questions in the array
  631. $temp=$this->questionList[$pos2];
  632. $this->questionList[$pos2]=$this->questionList[$pos1];
  633. $this->questionList[$pos1]=$temp;
  634. }
  635. /**
  636. * moves a question down in the list
  637. *
  638. * @author - Olivier Brouckaert
  639. * @param - integer $id - question ID to move down
  640. */
  641. function moveDown($id)
  642. {
  643. // there is a bug with some version of PHP with the key and prev functions
  644. // the script commented was tested in dev.dokeos.com with no success
  645. // Instead of using prev and next this was change with arrays.
  646. /*
  647. foreach($this->questionList as $position=>$questionId)
  648. {
  649. // if question ID found
  650. if($questionId == $id)
  651. {
  652. // position of question in the array
  653. $pos1=$position;
  654. //next($this->questionList);
  655. // position of next question in the array
  656. $pos2=key($this->questionList);
  657. // error, can't move question
  658. if(!$pos2)
  659. {
  660. //echo 'cant move!';
  661. return;
  662. }
  663. $id2=$this->questionList[$pos2];
  664. // exits foreach()
  665. break;
  666. }
  667. }
  668. */
  669. $question_list =array();
  670. foreach($this->questionList as $position=>$questionId)
  671. {
  672. $question_list[]= $questionId;
  673. }
  674. $len=count($question_list);
  675. $orderlist=array_keys($this->questionList);
  676. for($i=0;$i<$len;$i++)
  677. {
  678. $questionId = $question_list[$i];
  679. if($questionId == $id)
  680. {
  681. $pos1=$orderlist[$i+1];
  682. $pos2 =$orderlist[$i];
  683. if(!$pos2)
  684. {
  685. //echo 'cant move!';
  686. }
  687. break;
  688. }
  689. }
  690. // permutes questions in the array
  691. $temp=$this->questionList[$pos2];
  692. $this->questionList[$pos2]=$this->questionList[$pos1];
  693. $this->questionList[$pos1]=$temp;
  694. }
  695. /**
  696. * adds a question into the question list
  697. *
  698. * @author - Olivier Brouckaert
  699. * @param - integer $questionId - question ID
  700. * @return - boolean - true if the question has been added, otherwise false
  701. */
  702. function addToList($questionId)
  703. {
  704. // checks if the question ID is not in the list
  705. if(!$this->isInList($questionId))
  706. {
  707. // selects the max position
  708. if(!$this->selectNbrQuestions())
  709. {
  710. $pos=1;
  711. }
  712. else
  713. {
  714. if (is_array($this->questionList))
  715. $pos=max(array_keys($this->questionList))+1;
  716. }
  717. $this->questionList[$pos]=$questionId;
  718. return true;
  719. }
  720. return false;
  721. }
  722. /**
  723. * removes a question from the question list
  724. *
  725. * @author - Olivier Brouckaert
  726. * @param - integer $questionId - question ID
  727. * @return - boolean - true if the question has been removed, otherwise false
  728. */
  729. function removeFromList($questionId)
  730. {
  731. // searches the position of the question ID in the list
  732. $pos=array_search($questionId,$this->questionList);
  733. // question not found
  734. if($pos === false)
  735. {
  736. return false;
  737. }
  738. else
  739. {
  740. // deletes the position from the array containing the wanted question ID
  741. unset($this->questionList[$pos]);
  742. return true;
  743. }
  744. }
  745. /**
  746. * deletes the exercise from the database
  747. * Notice : leaves the question in the data base
  748. *
  749. * @author - Olivier Brouckaert
  750. */
  751. function delete(){
  752. global $_course,$_user;
  753. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  754. $sql="UPDATE $TBL_EXERCICES SET active='-1' WHERE id='".Database::escape_string($this->id)."'";
  755. Database::query($sql);
  756. api_item_property_update($_course, TOOL_QUIZ, $this->id,'QuizDeleted',$_user['user_id']);
  757. if (api_get_setting('search_enabled')=='true' && extension_loaded('xapian') ) {
  758. $this -> search_engine_delete();
  759. }
  760. }
  761. /**
  762. * Creates the form to create / edit an exercise
  763. * @param FormValidator $form the formvalidator instance (by reference)
  764. */
  765. function createForm ($form, $type='full')
  766. {
  767. global $id;
  768. if(empty($type)){
  769. $type='full';
  770. }
  771. // form title
  772. if (!empty($_GET['exerciseId'])) {
  773. $form_title = get_lang('ModifyExercise');
  774. } else {
  775. $form_title = get_lang('NewEx');
  776. }
  777. $form->addElement('header', '', $form_title);
  778. // title
  779. $form -> addElement('text', 'exerciseTitle', get_lang('ExerciseName'),'class="input_titles"');
  780. //$form->applyFilter('exerciseTitle','html_filter');
  781. $form -> addElement('html','<div class="row">
  782. <div class="label"></div>
  783. <div class="formw" style="height:50px">
  784. <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>
  785. </div>
  786. </div>');
  787. $editor_config = array('ToolbarSet' => 'TestQuestionDescription', 'Width' => '100%', 'Height' => '150');
  788. if(is_array($type)){
  789. $editor_config = array_merge($editor_config, $type);
  790. }
  791. $form -> addElement ('html','<div class="HideFCKEditor" id="HiddenFCKexerciseDescription" >');
  792. $form -> add_html_editor('exerciseDescription', get_lang('langExerciseDescription'), false, false, $editor_config);
  793. $form -> addElement ('html','</div>');
  794. $form -> addElement('html','<div class="row">
  795. <div class="label">&nbsp;</div>
  796. <div class="formw">
  797. <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>
  798. </div>
  799. </div>');
  800. // Random questions
  801. $form -> addElement('html','<div id="options" style="display:none">');
  802. if($type=='full') {
  803. // feedback type
  804. $radios_feedback = array();
  805. $radios_feedback[] = FormValidator :: createElement ('radio', 'exerciseFeedbackType', null, get_lang('ExerciseAtTheEndOfTheTest'),'0');
  806. $radios_feedback[] = FormValidator :: createElement ('radio', 'exerciseFeedbackType', null, get_lang('NoFeedback'),'2');
  807. $form -> addGroup($radios_feedback, null, get_lang('FeedbackType'));
  808. $feedback_option[0]=get_lang('ExerciseAtTheEndOfTheTest');
  809. $feedback_option[1]=get_lang('DirectFeedback');
  810. $feedback_option[2]=get_lang('NoFeedback');
  811. //Can't modify a DirectFeedback question
  812. if ($this->selectFeedbackType() != 1 ) {
  813. // $form -> addElement('select', 'exerciseFeedbackType',get_lang('FeedbackType'),$feedback_option,'onchange="javascript:feedbackselection()"');
  814. // test type
  815. $radios = array();
  816. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('QuestionsPerPageOne'),'2');
  817. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('QuestionsPerPageAll'),'1');
  818. $form -> addGroup($radios, null, get_lang('QuestionsPerPage'));
  819. } else {
  820. // if is Directfeedback but has not questions we can allow to modify the question type
  821. if ($this->selectNbrQuestions()== 0) {
  822. $form -> addElement('select', 'exerciseFeedbackType',get_lang('FeedbackType'),$feedback_option,'onchange="javascript:feedbackselection()"');
  823. // test type
  824. $radios = array();
  825. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('SimpleExercise'),'1');
  826. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('SequentialExercise'),'2');
  827. $form -> addGroup($radios, null, get_lang('ExerciseType'));
  828. } else {
  829. //we force the options to the DirectFeedback exercisetype
  830. $form -> addElement('hidden', 'exerciseFeedbackType','1');
  831. $form -> addElement('hidden', 'exerciseType','2');
  832. }
  833. }
  834. $radios_results_disabled = array();
  835. $radios_results_disabled[] = FormValidator :: createElement ('radio', 'results_disabled', null, get_lang('Yes'),'0');
  836. $radios_results_disabled[] = FormValidator :: createElement ('radio', 'results_disabled', null, get_lang('No'),'1');
  837. $form -> addGroup($radios_results_disabled, null, get_lang('ShowResultsToStudents'));
  838. $random = array();
  839. $option=array();
  840. $max = ($this->id > 0) ? $this->selectNbrQuestions() : 10 ;
  841. $option = range(0,$max);
  842. $option[0]=get_lang('No');
  843. $random[] = FormValidator :: createElement ('select', 'randomQuestions',null,$option);
  844. $random[] = FormValidator :: createElement ('static', 'help','help','<span style="font-style: italic;">'.get_lang('RandomQuestionsHelp').'</span>');
  845. //$random[] = FormValidator :: createElement ('text', 'randomQuestions', null,null,'0');
  846. $form -> addGroup($random,null,get_lang('RandomQuestions'),'<br />');
  847. $attempt_option=range(0,10);
  848. $attempt_option[0]=get_lang('Infinite');
  849. $form -> addElement('select', 'exerciseAttempts',get_lang('ExerciseAttempts'),$attempt_option);
  850. $form -> addElement('checkbox', 'enabletimelimit',get_lang('EnableTimeLimits'),null,'onclick = " return timelimit() "');
  851. $var= Exercise::selectTimeLimit();
  852. if(($this -> start_time!='0000-00-00 00:00:00')||($this -> end_time!='0000-00-00 00:00:00'))
  853. $form -> addElement('html','<div id="options2" style="display:block;">');
  854. else
  855. $form -> addElement('html','<div id="options2" style="display:none;">');
  856. //$form -> addElement('date', 'start_time', get_lang('ExeStartTime'), array('language'=>'es','format' => 'dMYHi'));
  857. //$form -> addElement('date', 'end_time', get_lang('ExeEndTime'), array('language'=>'es','format' => 'dMYHi'));
  858. $form->addElement('datepicker', 'start_time', get_lang('ExeStartTime'), array('form_name'=>'exercise_admin'));
  859. $form->addElement('datepicker', 'end_time', get_lang('ExeEndTime'), array('form_name'=>'exercise_admin'));
  860. //$form -> addElement('select', 'enabletimercontroltotalminutes',get_lang('ExerciseTimerControlMinutes'),$time_minutes_option);
  861. $form -> addElement('html','</div>');
  862. //Timer control
  863. $time_hours_option = range(0,12);
  864. $time_minutes_option = range(0,59);
  865. $form -> addElement('checkbox', 'enabletimercontrol',get_lang('EnableTimerControl'),null,'onclick = "option_time_expired()"');
  866. $expired_date = (int)$this->selectExpiredTime();
  867. if(($expired_date!='0')) {
  868. $form -> addElement('html','<div id="timercontrol" style="display:block;">');
  869. } else {
  870. $form -> addElement('html','<div id="timercontrol" style="display:none;">');
  871. }
  872. $form -> addElement('text', 'enabletimercontroltotalminutes',get_lang('ExerciseTotalDurationInMinutes'),array('style' => 'width : 35px'));
  873. $form -> addElement('html','</div>');
  874. //$form -> addElement('text', 'exerciseAttempts', get_lang('ExerciseAttempts').' : ',array('size'=>'2'));
  875. $form -> addElement('html','</div>'); //End advanced setting
  876. $defaults = array();
  877. if (api_get_setting('search_enabled') === 'true') {
  878. require_once(api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  879. $form -> addElement ('checkbox', 'index_document','', get_lang('SearchFeatureDoIndexDocument'));
  880. $form -> addElement ('html','<br /><div class="row">');
  881. $form -> addElement ('html', '<div class="label">'. get_lang('SearchFeatureDocumentLanguage') .'</div>');
  882. $form -> addElement ('html', '<div class="formw">'. api_get_languages_combo() .'</div>');
  883. $form -> addElement ('html','</div><div class="sub-form">');
  884. $specific_fields = get_specific_field_list();
  885. foreach ($specific_fields as $specific_field) {
  886. $form -> addElement ('text', $specific_field['code'], $specific_field['name']);
  887. $filter = array('course_code'=> "'". api_get_course_id() ."'", 'field_id' => $specific_field['id'], 'ref_id' => $this->id, 'tool_id' => '\''. TOOL_QUIZ .'\'');
  888. $values = get_specific_field_values_list($filter, array('value'));
  889. if ( !empty($values) ) {
  890. $arr_str_values = array();
  891. foreach ($values as $value) {
  892. $arr_str_values[] = $value['value'];
  893. }
  894. $defaults[$specific_field['code']] = implode(', ', $arr_str_values);
  895. }
  896. }
  897. $form -> addElement ('html','</div>');
  898. }
  899. }
  900. // submit
  901. isset($_GET['exerciseId'])?$text=get_lang('ModifyExercise'):$text=get_lang('ProcedToQuestions');
  902. $form -> addElement('html', '<br /><br />');
  903. $form -> addElement('style_submit_button', 'submitExercise', $text, 'class="save"');
  904. $form -> addRule ('exerciseTitle', get_lang('GiveExerciseName'), 'required');
  905. if($type=='full') {
  906. // rules
  907. $form -> addRule ('exerciseAttempts', get_lang('Numeric'), 'numeric');
  908. $form -> addRule ('start_time', get_lang('InvalidDate'), 'date');
  909. $form -> addRule ('end_time', get_lang('InvalidDate'), 'date');
  910. $form -> addRule(array ('start_time', 'end_time'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  911. }
  912. // defaults
  913. if($type=='full') {
  914. if($this -> id > 0) {
  915. if ($this -> random > $this->selectNbrQuestions()) {
  916. $defaults['randomQuestions'] = $this->selectNbrQuestions();
  917. } else {
  918. $defaults['randomQuestions'] = $this -> random;
  919. }
  920. $defaults['exerciseType'] = $this -> selectType();
  921. $defaults['exerciseTitle'] = $this -> selectTitle();
  922. $defaults['exerciseDescription'] = $this -> selectDescription();
  923. $defaults['exerciseAttempts'] = $this->selectAttempts();
  924. $defaults['exerciseFeedbackType'] = $this->selectFeedbackType();
  925. $defaults['results_disabled'] = $this->selectResultsDisabled();
  926. if(($this -> start_time!='0000-00-00 00:00:00')||($this -> end_time!='0000-00-00 00:00:00'))
  927. $defaults['enabletimelimit'] = 1;
  928. $defaults['start_time'] = ($this->start_time!='0000-00-00 00:00:00')? $this -> start_time : date('Y-m-d 12:00:00');
  929. $defaults['end_time'] = ($this->end_time!='0000-00-00 00:00:00')?$this -> end_time : date('Y-m-d 12:00:00',time()+84600);
  930. //Get expired time
  931. if($this -> expired_time != '0') {
  932. $defaults['enabletimercontrol'] = 1;
  933. $defaults['enabletimercontroltotalminutes'] = $this -> expired_time;
  934. } else {
  935. $defaults['enabletimercontroltotalminutes'] = 0;
  936. }
  937. } else {
  938. $defaults['exerciseType'] = 2;
  939. $defaults['exerciseAttempts'] = 0;
  940. $defaults['randomQuestions'] = 0;
  941. $defaults['exerciseDescription'] = '';
  942. $defaults['exerciseFeedbackType'] = 0;
  943. $defaults['results_disabled'] = 0;
  944. $defaults['start_time'] = date('Y-m-d 12:00:00');
  945. $defaults['end_time'] = date('Y-m-d 12:00:00',time()+84600);
  946. }
  947. } else {
  948. $defaults['exerciseTitle'] = $this -> selectTitle();
  949. $defaults['exerciseDescription'] = $this -> selectDescription();
  950. }
  951. if (api_get_setting('search_enabled') === 'true') {
  952. $defaults['index_document'] = 'checked="checked"';
  953. }
  954. $form -> setDefaults($defaults);
  955. }
  956. /**
  957. * function which process the creation of exercises
  958. * @param FormValidator $form the formvalidator instance
  959. */
  960. function processCreation($form,$type='')
  961. {
  962. $this -> updateTitle($form -> getSubmitValue('exerciseTitle'));
  963. $this -> updateDescription($form -> getSubmitValue('exerciseDescription'));
  964. $this -> updateAttempts($form -> getSubmitValue('exerciseAttempts'));
  965. $this -> updateFeedbackType($form -> getSubmitValue('exerciseFeedbackType'));
  966. $this -> updateType($form -> getSubmitValue('exerciseType'));
  967. $this -> setRandom($form -> getSubmitValue('randomQuestions'));
  968. $this -> updateResultsDisabled($form -> getSubmitValue('results_disabled'));
  969. $this -> updateExpiredTime($form -> getSubmitValue('enabletimercontroltotalminutes'));
  970. if($form -> getSubmitValue('enabletimelimit')==1) {
  971. $start_time = $form -> getSubmitValue('start_time');
  972. $this->start_time = $start_time['Y'].'-'.$start_time['F'].'-'.$start_time['d'].' '.$start_time['H'].':'.$start_time['i'].':00';
  973. $end_time = $form -> getSubmitValue('end_time');
  974. $this->end_time = $end_time['Y'].'-'.$end_time['F'].'-'.$end_time['d'].' '.$end_time['H'].':'.$end_time['i'].':00';
  975. } else {
  976. $this->start_time = '0000-00-00 00:00:00';
  977. $this->end_time = '0000-00-00 00:00:00';
  978. }
  979. if($form -> getSubmitValue('enabletimercontrol') == 1) {
  980. $expired_total_time = $form -> getSubmitValue('enabletimercontroltotalminutes');
  981. if ($this->expired_time == 0) {
  982. $this->expired_time = $expired_total_time;
  983. }
  984. } else {
  985. $this->expired_time = 0;
  986. }
  987. $this -> save($type);
  988. }
  989. function search_engine_save() {
  990. if ($_POST['index_document'] != 1) {
  991. return;
  992. }
  993. $course_id = api_get_course_id();
  994. require_once(api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php');
  995. require_once(api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php');
  996. require_once(api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  997. $specific_fields = get_specific_field_list();
  998. $ic_slide = new IndexableChunk();
  999. $all_specific_terms = '';
  1000. foreach ($specific_fields as $specific_field) {
  1001. if (isset($_REQUEST[$specific_field['code']])) {
  1002. $sterms = trim($_REQUEST[$specific_field['code']]);
  1003. if (!empty($sterms)) {
  1004. $all_specific_terms .= ' '. $sterms;
  1005. $sterms = explode(',', $sterms);
  1006. foreach ($sterms as $sterm) {
  1007. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  1008. add_specific_field_value($specific_field['id'], $course_id, TOOL_QUIZ, $this->id, $sterm);
  1009. }
  1010. }
  1011. }
  1012. }
  1013. // build the chunk to index
  1014. $ic_slide->addValue("title", $this->exercise);
  1015. $ic_slide->addCourseId($course_id);
  1016. $ic_slide->addToolId(TOOL_QUIZ);
  1017. $xapian_data = array(
  1018. SE_COURSE_ID => $course_id,
  1019. SE_TOOL_ID => TOOL_QUIZ,
  1020. SE_DATA => array('type' => SE_DOCTYPE_EXERCISE_EXERCISE, 'exercise_id' => (int)$this->id),
  1021. SE_USER => (int)api_get_user_id(),
  1022. );
  1023. $ic_slide->xapian_data = serialize($xapian_data);
  1024. $exercise_description = $all_specific_terms .' '. $this->description;
  1025. $ic_slide->addValue("content", $exercise_description);
  1026. $di = new DokeosIndexer();
  1027. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  1028. $di->connectDb(NULL, NULL, $lang);
  1029. $di->addChunk($ic_slide);
  1030. //index and return search engine document id
  1031. $did = $di->index();
  1032. if ($did) {
  1033. // save it to db
  1034. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1035. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  1036. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  1037. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id, $did);
  1038. Database::query($sql,__FILE__,__LINE__);
  1039. }
  1040. }
  1041. function search_engine_edit() {
  1042. // update search enchine and its values table if enabled
  1043. if (api_get_setting('search_enabled')=='true' && extension_loaded('xapian')) {
  1044. $course_id = api_get_course_id();
  1045. // actually, it consists on delete terms from db, insert new ones, create a new search engine document, and remove the old one
  1046. // get search_did
  1047. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1048. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  1049. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  1050. $res = Database::query($sql, __FILE__, __LINE__);
  1051. if (Database::num_rows($res) > 0) {
  1052. require_once(api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php');
  1053. require_once(api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php');
  1054. require_once(api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  1055. $se_ref = Database::fetch_array($res);
  1056. $specific_fields = get_specific_field_list();
  1057. $ic_slide = new IndexableChunk();
  1058. $all_specific_terms = '';
  1059. foreach ($specific_fields as $specific_field) {
  1060. delete_all_specific_field_value($course_id, $specific_field['id'], TOOL_QUIZ, $this->id);
  1061. if (isset($_REQUEST[$specific_field['code']])) {
  1062. $sterms = trim($_REQUEST[$specific_field['code']]);
  1063. $all_specific_terms .= ' '. $sterms;
  1064. $sterms = explode(',', $sterms);
  1065. foreach ($sterms as $sterm) {
  1066. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  1067. add_specific_field_value($specific_field['id'], $course_id, TOOL_QUIZ, $this->id, $sterm);
  1068. }
  1069. }
  1070. }
  1071. // build the chunk to index
  1072. $ic_slide->addValue("title", $this->exercise);
  1073. $ic_slide->addCourseId($course_id);
  1074. $ic_slide->addToolId(TOOL_QUIZ);
  1075. $xapian_data = array(
  1076. SE_COURSE_ID => $course_id,
  1077. SE_TOOL_ID => TOOL_QUIZ,
  1078. SE_DATA => array('type' => SE_DOCTYPE_EXERCISE_EXERCISE, 'exercise_id' => (int)$this->id),
  1079. SE_USER => (int)api_get_user_id(),
  1080. );
  1081. $ic_slide->xapian_data = serialize($xapian_data);
  1082. $exercise_description = $all_specific_terms .' '. $this->description;
  1083. $ic_slide->addValue("content", $exercise_description);
  1084. $di = new DokeosIndexer();
  1085. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  1086. $di->connectDb(NULL, NULL, $lang);
  1087. $di->remove_document((int)$se_ref['search_did']);
  1088. $di->addChunk($ic_slide);
  1089. //index and return search engine document id
  1090. $did = $di->index();
  1091. if ($did) {
  1092. // save it to db
  1093. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\'';
  1094. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  1095. Database::query($sql,__FILE__,__LINE__);
  1096. //var_dump($sql);
  1097. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  1098. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  1099. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id, $did);
  1100. Database::query($sql,__FILE__,__LINE__);
  1101. }
  1102. }
  1103. }
  1104. }
  1105. function search_engine_delete() {
  1106. // remove from search engine if enabled
  1107. if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian') ) {
  1108. $course_id = api_get_course_id();
  1109. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1110. $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';
  1111. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  1112. $res = Database::query($sql, __FILE__, __LINE__);
  1113. if (Database::num_rows($res) > 0) {
  1114. $row = Database::fetch_array($res);
  1115. require_once(api_get_path(LIBRARY_PATH) .'search/DokeosIndexer.class.php');
  1116. $di = new DokeosIndexer();
  1117. $di->remove_document((int)$row['search_did']);
  1118. unset($di);
  1119. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1120. foreach ( $this->questionList as $question_i) {
  1121. $sql = 'SELECT type FROM %s WHERE id=%s';
  1122. $sql = sprintf($sql, $tbl_quiz_question, $question_i);
  1123. $qres = Database::query($sql, __FILE__, __LINE__);
  1124. if (Database::num_rows($qres) > 0) {
  1125. $qrow = Database::fetch_array($qres);
  1126. $objQuestion = Question::getInstance($qrow['type']);
  1127. $objQuestion = Question::read((int)$question_i);
  1128. $objQuestion->search_engine_edit($this->id, FALSE, TRUE);
  1129. unset($objQuestion);
  1130. }
  1131. }
  1132. }
  1133. $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';
  1134. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  1135. Database::query($sql, __FILE__, __LINE__);
  1136. // remove terms from db
  1137. require_once(api_get_path(LIBRARY_PATH) .'specific_fields_manager.lib.php');
  1138. delete_all_values_for_item($course_id, TOOL_QUIZ, $this->id);
  1139. }
  1140. }
  1141. function selectExpiredTime() {
  1142. return $this->expired_time;
  1143. }
  1144. }
  1145. endif;
  1146. ?>