exercise.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. For a full list of contributors, see "credits.txt".
  7. The full license can be read in "license.txt".
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. See the GNU General Public License for more details.
  13. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  14. Mail: info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. * Exercise class: This class allows to instantiate an object of type Exercise
  19. * @package dokeos.exercise
  20. * @author Olivier Brouckaert
  21. * @version $Id: exercise.class.php 17311 2008-12-16 00:53:24Z cfasanando $
  22. */
  23. if(!class_exists('Exercise')):
  24. class Exercise
  25. {
  26. var $id;
  27. var $exercise;
  28. var $description;
  29. var $sound;
  30. var $type;
  31. var $random;
  32. var $active;
  33. var $timeLimit;
  34. var $attempts;
  35. var $end_time;
  36. var $start_time;
  37. var $questionList; // array with the list of this exercise's questions
  38. /**
  39. * constructor of the class
  40. *
  41. * @author - Olivier Brouckaert
  42. */
  43. function Exercise()
  44. {
  45. $this->id=0;
  46. $this->exercise='';
  47. $this->description='';
  48. $this->sound='';
  49. $this->type=1;
  50. $this->random=0;
  51. $this->active=1;
  52. $this->questionList=array();
  53. $this->timeLimit = 0;
  54. $this->end_time = '0000-00-00 00:00:00';
  55. $this->start_time = '0000-00-00 00:00:00';
  56. }
  57. /**
  58. * reads exercise informations from the data base
  59. *
  60. * @author - Olivier Brouckaert
  61. * @param - integer $id - exercise ID
  62. * @return - boolean - true if exercise exists, otherwise false
  63. */
  64. function read($id)
  65. {
  66. global $_course;
  67. global $_configuration;
  68. global $questionList;
  69. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  70. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  71. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  72. #$TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  73. $sql="SELECT title,description,sound,type,random,active, results_disabled, max_attempt,start_time,end_time FROM $TBL_EXERCICES WHERE id='".Database::escape_string($id)."'";
  74. $result=api_sql_query($sql,__FILE__,__LINE__);
  75. // if the exercise has been found
  76. if($object=Database::fetch_object($result))
  77. {
  78. $this->id=$id;
  79. $this->exercise=$object->title;
  80. $this->description=$object->description;
  81. $this->sound=$object->sound;
  82. $this->type=$object->type;
  83. $this->random=$object->random;
  84. $this->active=$object->active;
  85. $this->results_disabled =$object->results_disabled;
  86. $this->attempts = $object->max_attempt;
  87. $this->end_time = $object->end_time;
  88. $this->start_time = $object->start_time;
  89. $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";
  90. $result=api_sql_query($sql,__FILE__,__LINE__);
  91. // fills the array with the question ID for this exercise
  92. // the key of the array is the question position
  93. while($object=Database::fetch_object($result))
  94. {
  95. // makes sure that the question position is unique
  96. while(isset($this->questionList[$object->question_order]))
  97. {
  98. $object->question_order++;
  99. }
  100. $this->questionList[$object->question_order]=$object->question_id;
  101. }
  102. //var_dump($this->end_time,$object->start_time);
  103. if($this->random > 0){
  104. $this->questionList = $this->selectRandomList();
  105. }
  106. //overload questions list with recorded questions list
  107. //load questions only for exercises of type 'one question per page'
  108. //this is needed only is there is no questions
  109. //
  110. if($this->type == 2 && $_configuration['live_exercise_tracking']==true && $_SERVER['REQUEST_METHOD']!='POST' && defined('QUESTION_LIST_ALREADY_LOGGED'))
  111. {
  112. //if(empty($_SESSION['questionList']))
  113. $this->questionList = $questionList;
  114. }
  115. return true;
  116. }
  117. // exercise not found
  118. return false;
  119. }
  120. /**
  121. * returns the exercise ID
  122. *
  123. * @author - Olivier Brouckaert
  124. * @return - integer - exercise ID
  125. */
  126. function selectId()
  127. {
  128. return $this->id;
  129. }
  130. /**
  131. * returns the exercise title
  132. *
  133. * @author - Olivier Brouckaert
  134. * @return - string - exercise title
  135. */
  136. function selectTitle()
  137. {
  138. return $this->exercise;
  139. }
  140. /**
  141. * returns the number of attempts setted
  142. *
  143. * @return - numeric - exercise attempts
  144. */
  145. function selectAttempts()
  146. {
  147. return $this->attempts;
  148. }
  149. /**
  150. * returns the time limit
  151. */
  152. function selectTimeLimit()
  153. {
  154. return $this->timeLimit;
  155. }
  156. /**
  157. * returns the exercise description
  158. *
  159. * @author - Olivier Brouckaert
  160. * @return - string - exercise description
  161. */
  162. function selectDescription()
  163. {
  164. return $this->description;
  165. }
  166. /**
  167. * returns the exercise sound file
  168. *
  169. * @author - Olivier Brouckaert
  170. * @return - string - exercise description
  171. */
  172. function selectSound()
  173. {
  174. return $this->sound;
  175. }
  176. /**
  177. * returns the exercise type
  178. *
  179. * @author - Olivier Brouckaert
  180. * @return - integer - exercise type
  181. */
  182. function selectType()
  183. {
  184. return $this->type;
  185. }
  186. /**
  187. * tells if questions are selected randomly, and if so returns the draws
  188. *
  189. * @author - Olivier Brouckaert
  190. * @return - integer - 0 if not random, otherwise the draws
  191. */
  192. function isRandom()
  193. {
  194. if($this->random > 0){
  195. return true;
  196. }
  197. else{
  198. return false;
  199. }
  200. }
  201. /**
  202. * Same as isRandom() but has a name applied to values different than 0 or 1
  203. */
  204. function getShuffle()
  205. {
  206. return $this->random;
  207. }
  208. /**
  209. * returns the exercise status (1 = enabled ; 0 = disabled)
  210. *
  211. * @author - Olivier Brouckaert
  212. * @return - boolean - true if enabled, otherwise false
  213. */
  214. function selectStatus()
  215. {
  216. return $this->active;
  217. }
  218. /**
  219. * returns the array with the question ID list
  220. *
  221. * @author - Olivier Brouckaert
  222. * @return - array - question ID list
  223. */
  224. function selectQuestionList()
  225. {
  226. return $this->questionList;
  227. }
  228. /**
  229. * returns the number of questions in this exercise
  230. *
  231. * @author - Olivier Brouckaert
  232. * @return - integer - number of questions
  233. */
  234. function selectNbrQuestions()
  235. {
  236. return sizeof($this->questionList);
  237. }
  238. /**
  239. * selects questions randomly in the question list
  240. *
  241. * @author - Olivier Brouckaert
  242. * @return - array - if the exercise is not set to take questions randomly, returns the question list
  243. * without randomizing, otherwise, returns the list with questions selected randomly
  244. */
  245. function selectRandomList()
  246. {
  247. $nbQuestions = $this->selectNbrQuestions();
  248. $temp_list = $this->questionList;
  249. shuffle($temp_list);
  250. return array_combine(range(1,$nbQuestions),$temp_list);
  251. $nbQuestions = $this->selectNbrQuestions();
  252. //Not a random exercise, or if there are not at least 2 questions
  253. if($this->random == 0 || $nbQuestions < 2)
  254. {
  255. return $this->questionList;
  256. }
  257. $randQuestionList = array();
  258. $alreadyChosen = array();
  259. for($i=0; $i < $this->random; $i++)
  260. {
  261. if($i < $nbQuestions){
  262. do
  263. {
  264. $rand=rand(1,$nbQuestions);
  265. }
  266. while(in_array($rand,$alreadyChosen));
  267. $alreadyChosen[]=$rand;
  268. $randQuestionList[$rand] = $this->questionList[$rand];
  269. }
  270. }
  271. return $randQuestionList;
  272. }
  273. /**
  274. * returns 'true' if the question ID is in the question list
  275. *
  276. * @author - Olivier Brouckaert
  277. * @param - integer $questionId - question ID
  278. * @return - boolean - true if in the list, otherwise false
  279. */
  280. function isInList($questionId)
  281. {
  282. return in_array($questionId,$this->questionList);
  283. }
  284. /**
  285. * changes the exercise title
  286. *
  287. * @author - Olivier Brouckaert
  288. * @param - string $title - exercise title
  289. */
  290. function updateTitle($title)
  291. {
  292. $this->exercise=$title;
  293. }
  294. /**
  295. * changes the exercise max attempts
  296. *
  297. * @param - numeric $attempts - exercise max attempts
  298. */
  299. function updateAttempts($attempts)
  300. {
  301. $this->attempts=$attempts;
  302. }
  303. /**
  304. * changes the exercise description
  305. *
  306. * @author - Olivier Brouckaert
  307. * @param - string $description - exercise description
  308. */
  309. function updateDescription($description)
  310. {
  311. $this->description=$description;
  312. }
  313. /**
  314. * changes the exercise sound file
  315. *
  316. * @author - Olivier Brouckaert
  317. * @param - string $sound - exercise sound file
  318. * @param - string $delete - ask to delete the file
  319. */
  320. function updateSound($sound,$delete)
  321. {
  322. global $audioPath, $documentPath,$_course, $_user;
  323. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  324. $TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  325. if($sound['size'] && (strstr($sound['type'],'audio') || strstr($sound['type'],'video')))
  326. {
  327. $this->sound=$sound['name'];
  328. if(@move_uploaded_file($sound['tmp_name'],$audioPath.'/'.$this->sound))
  329. {
  330. $query="SELECT 1 FROM $TBL_DOCUMENT "
  331. ." WHERE path='".str_replace($documentPath,'',$audioPath).'/'.$this->sound."'";
  332. $result=api_sql_query($query,__FILE__,__LINE__);
  333. if(!mysql_num_rows($result))
  334. {
  335. /*$query="INSERT INTO $TBL_DOCUMENT(path,filetype) VALUES "
  336. ." ('".str_replace($documentPath,'',$audioPath).'/'.$this->sound."','file')";
  337. api_sql_query($query,__FILE__,__LINE__);*/
  338. $id = add_document($_course,str_replace($documentPath,'',$audioPath).'/'.$this->sound,'file',$sound['size'],$sound['name']);
  339. //$id = Database::get_last_insert_id();
  340. //$time = time();
  341. //$time = date("Y-m-d H:i:s", $time);
  342. // insert into the item_property table, using default visibility of "visible"
  343. /*$query = "INSERT INTO $TBL_ITEM_PROPERTY "
  344. ."(tool, ref, insert_user_id,to_group_id, insert_date, lastedit_date, lastedit_type) "
  345. ." VALUES "
  346. ."('".TOOL_DOCUMENT."', $id, $_user['user_id'], 0, '$time', '$time', 'DocumentAdded' )";
  347. api_sql_query($query,__FILE__,__LINE__);*/
  348. api_item_property_update($_course, TOOL_DOCUMENT, $id, 'DocumentAdded',$_user['user_id']);
  349. item_property_update_on_folder($_course,str_replace($documentPath,'',$audioPath),$_user['user_id']);
  350. }
  351. }
  352. }
  353. elseif($delete && is_file($audioPath.'/'.$this->sound))
  354. {
  355. $this->sound='';
  356. }
  357. }
  358. /**
  359. * changes the exercise type
  360. *
  361. * @author - Olivier Brouckaert
  362. * @param - integer $type - exercise type
  363. */
  364. function updateType($type)
  365. {
  366. $this->type=$type;
  367. }
  368. /**
  369. * sets to 0 if questions are not selected randomly
  370. * if questions are selected randomly, sets the draws
  371. *
  372. * @author - Olivier Brouckaert
  373. * @param - integer $random - 0 if not random, otherwise the draws
  374. */
  375. function setRandom($random)
  376. {
  377. $this->random=$random;
  378. }
  379. /**
  380. * enables the exercise
  381. *
  382. * @author - Olivier Brouckaert
  383. */
  384. function enable()
  385. {
  386. $this->active=1;
  387. }
  388. /**
  389. * disables the exercise
  390. *
  391. * @author - Olivier Brouckaert
  392. */
  393. function disable()
  394. {
  395. $this->active=0;
  396. }
  397. function disable_results()
  398. {
  399. $this->results_disabled = true;
  400. }
  401. function enable_results()
  402. {
  403. $this->results_disabled = false;
  404. }
  405. /**
  406. * updates the exercise in the data base
  407. *
  408. * @author - Olivier Brouckaert
  409. */
  410. function save()
  411. {
  412. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  413. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  414. $TBL_QUIZ_QUESTION= Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  415. $id=$this->id;
  416. $exercise=addslashes($this->exercise);
  417. $description=addslashes($this->description);
  418. $sound=addslashes($this->sound);
  419. $type=$this->type;
  420. $attempts=$this->attempts;
  421. $random=$this->random;
  422. $active=$this->active;
  423. $results_disabled = intval($this->results_disabled);
  424. $start_time = Database::escape_string($this->start_time);
  425. $end_time = Database::escape_string($this->end_time);
  426. // exercise already exists
  427. if($id)
  428. {
  429. $sql="UPDATE $TBL_EXERCICES SET
  430. start_time='$start_time',end_time='$end_time',
  431. title='".Database::escape_string($exercise)."',
  432. description='".Database::escape_string($description)."',
  433. sound='".Database::escape_string($sound)."',
  434. type='".Database::escape_string($type)."',
  435. random='".Database::escape_string($random)."',
  436. active='".Database::escape_string($active)."',
  437. max_attempt='".Database::escape_string($attempts)."', " .
  438. "results_disabled='".Database::escape_string($results_disabled)."'
  439. WHERE id='".Database::escape_string($id)."'";
  440. api_sql_query($sql,__FILE__,__LINE__);
  441. }
  442. // creates a new exercise
  443. else
  444. {
  445. $sql="INSERT INTO $TBL_EXERCICES(start_time,end_time,title,description,sound,type,random,active, results_disabled, max_attempt)
  446. VALUES(
  447. '$start_time','$end_time',
  448. '".Database::escape_string($exercise)."',
  449. '".Database::escape_string($description)."',
  450. '".Database::escape_string($sound)."',
  451. '".Database::escape_string($type)."',
  452. '".Database::escape_string($random)."',
  453. '".Database::escape_string($active)."',
  454. '".Database::escape_string($results_disabled)."',
  455. '".Database::escape_string($attempts)."'
  456. )";
  457. api_sql_query($sql,__FILE__,__LINE__);
  458. $this->id=mysql_insert_id();
  459. }
  460. // updates the question position
  461. foreach($this->questionList as $position=>$questionId)
  462. {
  463. //$sql="UPDATE $TBL_QUESTIONS SET position='".Database::escape_string($position)."' WHERE id='".Database::escape_string($questionId)."'";
  464. $sql="UPDATE $TBL_QUIZ_QUESTION SET question_order='".Database::escape_string($position)."' " .
  465. "WHERE question_id='".Database::escape_string($questionId)."' and exercice_id='".Database::escape_string($id)."'";
  466. api_sql_query($sql,__FILE__,__LINE__);
  467. }
  468. }
  469. /**
  470. * moves a question up in the list
  471. *
  472. * @author - Olivier Brouckaert
  473. * @author - Julio Montoya (rewrote the code)
  474. * @param - integer $id - question ID to move up
  475. */
  476. function moveUp($id)
  477. {
  478. // there is a bug with some version of PHP with the key and prev functions
  479. // the script commented was tested in dev.dokeos.com with no success
  480. // Instead of using prev and next this was change with arrays.
  481. /*
  482. foreach($this->questionList as $position=>$questionId)
  483. {
  484. // if question ID found
  485. if($questionId == $id)
  486. {
  487. // position of question in the array
  488. echo $pos1=$position; //1
  489. echo "<br>";
  490. prev($this->questionList);
  491. prev($this->questionList);
  492. // position of previous question in the array
  493. $pos2=key($this->questionList);
  494. //if the cursor of the array hit the end
  495. // then we must reset the array to get the previous key
  496. if($pos2===null)
  497. {
  498. end($this->questionList);
  499. prev($this->questionList);
  500. $pos2=key($this->questionList);
  501. }
  502. // error, can't move question
  503. if(!$pos2)
  504. {
  505. //echo 'cant move!';
  506. $pos2=key($this->questionList);
  507. reset($this->questionList);
  508. }
  509. $id2=$this->questionList[$pos2];
  510. // exits foreach()
  511. break;
  512. }
  513. $i++;
  514. }
  515. */
  516. $question_list =array();
  517. foreach($this->questionList as $position=>$questionId)
  518. {
  519. $question_list[]= $questionId;
  520. }
  521. $len=count($question_list);
  522. $orderlist=array_keys($this->questionList);
  523. for($i=0;$i<$len;$i++)
  524. {
  525. $questionId = $question_list[$i];
  526. if($questionId == $id)
  527. {
  528. // position of question in the array
  529. $pos1=$orderlist[$i];
  530. $pos2=$orderlist[$i-1];
  531. if($pos2===null)
  532. {
  533. $pos2 = $orderlist[$len-1];
  534. }
  535. // error, can't move question
  536. if(!$pos2)
  537. {
  538. $pos2=$orderlist[0];
  539. $i=0;
  540. }
  541. break;
  542. }
  543. }
  544. // permutes questions in the array
  545. $temp=$this->questionList[$pos2];
  546. $this->questionList[$pos2]=$this->questionList[$pos1];
  547. $this->questionList[$pos1]=$temp;
  548. }
  549. /**
  550. * moves a question down in the list
  551. *
  552. * @author - Olivier Brouckaert
  553. * @param - integer $id - question ID to move down
  554. */
  555. function moveDown($id)
  556. {
  557. // there is a bug with some version of PHP with the key and prev functions
  558. // the script commented was tested in dev.dokeos.com with no success
  559. // Instead of using prev and next this was change with arrays.
  560. /*
  561. foreach($this->questionList as $position=>$questionId)
  562. {
  563. // if question ID found
  564. if($questionId == $id)
  565. {
  566. // position of question in the array
  567. $pos1=$position;
  568. //next($this->questionList);
  569. // position of next question in the array
  570. $pos2=key($this->questionList);
  571. // error, can't move question
  572. if(!$pos2)
  573. {
  574. //echo 'cant move!';
  575. return;
  576. }
  577. $id2=$this->questionList[$pos2];
  578. // exits foreach()
  579. break;
  580. }
  581. }
  582. */
  583. $question_list =array();
  584. foreach($this->questionList as $position=>$questionId)
  585. {
  586. $question_list[]= $questionId;
  587. }
  588. $len=count($question_list);
  589. $orderlist=array_keys($this->questionList);
  590. for($i=0;$i<$len;$i++)
  591. {
  592. $questionId = $question_list[$i];
  593. if($questionId == $id)
  594. {
  595. $pos1=$orderlist[$i+1];
  596. $pos2 =$orderlist[$i];
  597. if(!$pos2)
  598. {
  599. //echo 'cant move!';
  600. }
  601. break;
  602. }
  603. }
  604. // permutes questions in the array
  605. $temp=$this->questionList[$pos2];
  606. $this->questionList[$pos2]=$this->questionList[$pos1];
  607. $this->questionList[$pos1]=$temp;
  608. }
  609. /**
  610. * adds a question into the question list
  611. *
  612. * @author - Olivier Brouckaert
  613. * @param - integer $questionId - question ID
  614. * @return - boolean - true if the question has been added, otherwise false
  615. */
  616. function addToList($questionId)
  617. {
  618. // checks if the question ID is not in the list
  619. if(!$this->isInList($questionId))
  620. {
  621. // selects the max position
  622. if(!$this->selectNbrQuestions())
  623. {
  624. $pos=1;
  625. }
  626. else
  627. {
  628. $pos=max(array_keys($this->questionList))+1;
  629. }
  630. $this->questionList[$pos]=$questionId;
  631. return true;
  632. }
  633. return false;
  634. }
  635. /**
  636. * removes a question from the question list
  637. *
  638. * @author - Olivier Brouckaert
  639. * @param - integer $questionId - question ID
  640. * @return - boolean - true if the question has been removed, otherwise false
  641. */
  642. function removeFromList($questionId)
  643. {
  644. // searches the position of the question ID in the list
  645. $pos=array_search($questionId,$this->questionList);
  646. // question not found
  647. if($pos === false)
  648. {
  649. return false;
  650. }
  651. else
  652. {
  653. // deletes the position from the array containing the wanted question ID
  654. unset($this->questionList[$pos]);
  655. return true;
  656. }
  657. }
  658. /**
  659. * deletes the exercise from the database
  660. * Notice : leaves the question in the data base
  661. *
  662. * @author - Olivier Brouckaert
  663. */
  664. function delete(){
  665. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  666. $sql="UPDATE $TBL_EXERCICES SET active='-1' WHERE id='".Database::escape_string($this->id)."'";
  667. api_sql_query($sql);
  668. }
  669. /**
  670. * Creates the form to create / edit an exercise
  671. * @param FormValidator $form the formvalidator instance (by reference)
  672. */
  673. function createForm ($form)
  674. {
  675. // title
  676. $form -> addElement('text', 'exerciseTitle', get_lang('ExerciseName').' : ','class="input_titles"');
  677. // fck editor
  678. global $fck_attribute;
  679. $fck_attribute = array();
  680. $fck_attribute['Height'] = '250';
  681. $fck_attribute['Width'] = '100%';
  682. $fck_attribute['ToolbarSet'] = 'NewTest';
  683. $fck_attribute['Config']['InDocument'] = false;
  684. $fck_attribute['Config']['CreateDocumentDir'] = '../../courses/'.api_get_course_path().'/document/';
  685. //$fck_attribute['Config']['CreateDocumentWebDir'] = api_get_path('WEB_COURSE_PATH').$_course['path'].'/document/';
  686. $form -> addElement ('html_editor', 'exerciseDescription', get_lang('ExerciseDescription').' : ');
  687. // type
  688. $radios = array();
  689. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('SimpleExercise'),'1');
  690. $radios[] = FormValidator :: createElement ('radio', 'exerciseType', null, get_lang('SequentialExercise'),'2');
  691. $form -> addGroup($radios, null, get_lang('ExerciseType').' : ', '<br />');
  692. $form -> addElement('html','<div class="row">
  693. <div class="label">&nbsp;</div>
  694. <div class="formw">
  695. <a href="javascript://" onclick=" return advanced_parameters()"><span id="img_plus_and_minus"><img src="../img/nolines_plus.gif" alt="" />'.get_lang('AdvancedParameters').'</span></a>
  696. </div>
  697. </div>');
  698. // Random questions
  699. $form -> addElement('html','<div id="options" style="display: none;">');
  700. $random = array();
  701. $max = ($this->id > 0) ? $this->selectNbrQuestions() : 10 ;
  702. $option = range(0,$max);
  703. $option[0]=get_lang('DoNotRandomize');
  704. $random[] = FormValidator :: createElement ('static', 'help','help','<span style="font-style: italic;">'.get_lang('RandomQuestionsHelp').'</span>');
  705. $random[] = FormValidator :: createElement ('select', 'randomQuestions',null,$option);
  706. //$random[] = FormValidator :: createElement ('text', 'randomQuestions', null,null,'0');
  707. $form -> addGroup($random,null,get_lang('RandomQuestions').' : ','<br />');
  708. $attempt_option=range(0,10);
  709. $attempt_option[0]=get_lang('Infinite');
  710. $form -> addElement('select', 'exerciseAttempts',get_lang('ExerciseAttempts').' : ',$attempt_option);
  711. $form -> addElement('checkbox', 'enabletimelimit',null ,get_lang('EnableTimeLimits'));
  712. //$form -> addElement('date', 'start_time', get_lang('ExeStartTime'), array('language'=>'es','format' => 'dMYHi'));
  713. //$form -> addElement('date', 'end_time', get_lang('ExeEndTime'), array('language'=>'es','format' => 'dMYHi'));
  714. $form->addElement('datepicker', 'start_time', get_lang('ExeStartTime'), array('form_name'=>'exercise_admin'));
  715. $form->addElement('datepicker', 'end_time', get_lang('ExeEndTime'), array('form_name'=>'exercise_admin'));
  716. // Exercise attempts
  717. //$form -> addElement('text', 'exerciseAttempts', get_lang('ExerciseAttempts').' : ',array('size'=>'2'));
  718. $form -> addElement('html','</div>');
  719. // submit
  720. $form -> addElement('submit', 'submitExercise', get_lang('Ok'));
  721. // rules
  722. $form -> addRule ('exerciseTitle', get_lang('GiveExerciseName'), 'required');
  723. $form -> addRule ('exerciseAttempts', get_lang('Numeric'), 'numeric');
  724. $form -> addRule ('start_time', get_lang('DateNotValid'), 'date');
  725. $form -> addRule ('end_time', get_lang('DateNotValid'), 'date');
  726. $form->addRule(array ('start_time', 'end_time'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  727. // defaults
  728. $defaults = array();
  729. if($this -> id > 0)
  730. {
  731. if ($this -> random > $this->selectNbrQuestions())
  732. {
  733. $defaults['randomQuestions'] = $this->selectNbrQuestions();
  734. }
  735. else
  736. {
  737. $defaults['randomQuestions'] = $this -> random;
  738. }
  739. $defaults['exerciseType'] = $this -> selectType();
  740. $defaults['exerciseTitle'] = $this -> selectTitle();
  741. $defaults['exerciseDescription'] = $this -> selectDescription();
  742. $defaults['exerciseAttempts'] = $this->selectAttempts();
  743. if(($this -> start_time!='0000-00-00 00:00:00')||($this -> end_time!='0000-00-00 00:00:00'))
  744. $defaults['enabletimelimit'] = 1;
  745. $defaults['start_time'] = ($this->start_time!='0000-00-00 00:00:00')? $this -> start_time : date('Y-m-d 12:00:00');
  746. $defaults['end_time'] = ($this->end_time!='0000-00-00 00:00:00')?$this -> end_time : date('Y-m-d 12:00:00');
  747. }
  748. else
  749. {
  750. $defaults['exerciseType'] = 1;
  751. $defaults['exerciseAttempts'] = 0;
  752. $defaults['randomQuestions'] = 0;
  753. $defaults['exerciseDescription'] = '';
  754. $defaults['start_time'] = date('Y-m-d 12:00:00');
  755. $defaults['end_time'] = date('Y-m-d 12:00:00');
  756. }
  757. $form -> setDefaults($defaults);
  758. }
  759. /**
  760. * function which process the creation of exercises
  761. * @param FormValidator $form the formvalidator instance
  762. */
  763. function processCreation($form)
  764. {
  765. $this -> updateAttempts($form -> getSubmitValue('exerciseAttempts'));
  766. $this -> updateTitle($form -> getSubmitValue('exerciseTitle'));
  767. $this -> updateDescription($form -> getSubmitValue('exerciseDescription'));
  768. $this -> updateType($form -> getSubmitValue('exerciseType'));
  769. $this -> setRandom($form -> getSubmitValue('randomQuestions'));
  770. if($form -> getSubmitValue('enabletimelimit')==1)
  771. {
  772. $start_time = $form -> getSubmitValue('start_time');
  773. $this->start_time = $start_time['Y'].'-'.$start_time['F'].'-'.$start_time['d'].' '.$start_time['H'].':'.$start_time['i'].':00';
  774. $end_time = $form -> getSubmitValue('end_time');
  775. $this->end_time = $end_time['Y'].'-'.$end_time['F'].'-'.$end_time['d'].' '.$end_time['H'].':'.$end_time['i'].':00';
  776. }
  777. else
  778. {
  779. $this->start_time = '0000-00-00 00:00:00';
  780. $this->end_time = '0000-00-00 00:00:00';
  781. }
  782. //echo $end_time;exit;
  783. $this -> save();
  784. }
  785. }
  786. endif;
  787. ?>