exercise.class.php 45 KB

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