question.class.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * File containing the Question class.
  5. * @package chamilo.exercise
  6. * @author Olivier Brouckaert
  7. * @author Julio Montoya <gugli100@gmail.com> lot of bug fixes
  8. */
  9. /**
  10. * Code
  11. */
  12. if(!class_exists('Question')):
  13. // answer types
  14. define('UNIQUE_ANSWER', 1);
  15. define('MULTIPLE_ANSWER', 2);
  16. define('FILL_IN_BLANKS', 3);
  17. define('MATCHING', 4);
  18. define('FREE_ANSWER', 5);
  19. define('HOT_SPOT', 6);
  20. define('HOT_SPOT_ORDER', 7);
  21. define('HOT_SPOT_DELINEATION', 8);
  22. define('MULTIPLE_ANSWER_COMBINATION', 9);
  23. define('UNIQUE_ANSWER_NO_OPTION', 10);
  24. define('MULTIPLE_ANSWER_TRUE_FALSE', 11);
  25. define('MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE', 12);
  26. /**
  27. QUESTION CLASS
  28. *
  29. * This class allows to instantiate an object of type Question
  30. *
  31. * @author Olivier Brouckaert, original author
  32. * @author Patrick Cool, LaTeX support
  33. * @package chamilo.exercise
  34. */
  35. abstract class Question
  36. {
  37. public $id;
  38. public $question;
  39. public $description;
  40. public $weighting;
  41. public $position;
  42. public $type;
  43. public $level;
  44. public $picture;
  45. public $exerciseList; // array with the list of exercises which this question is in
  46. private $isContent;
  47. public $course;
  48. static $typePicture = 'new_question.png';
  49. static $explanationLangVar = '';
  50. static $questionTypes = array(
  51. UNIQUE_ANSWER => array('unique_answer.class.php' , 'UniqueAnswer'),
  52. MULTIPLE_ANSWER => array('multiple_answer.class.php' , 'MultipleAnswer'),
  53. FILL_IN_BLANKS => array('fill_blanks.class.php' , 'FillBlanks'),
  54. MATCHING => array('matching.class.php' , 'Matching'),
  55. FREE_ANSWER => array('freeanswer.class.php' , 'FreeAnswer'),
  56. HOT_SPOT => array('hotspot.class.php' , 'HotSpot'),
  57. HOT_SPOT_DELINEATION => array('hotspot.class.php' , 'HotspotDelineation'),
  58. MULTIPLE_ANSWER_COMBINATION => array('multiple_answer_combination.class.php' , 'MultipleAnswerCombination'),
  59. UNIQUE_ANSWER_NO_OPTION => array('unique_answer_no_option.class.php' , 'UniqueAnswerNoOption'),
  60. MULTIPLE_ANSWER_TRUE_FALSE => array('multiple_answer_true_false.class.php' , 'MultipleAnswerTrueFalse'),
  61. MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE => array('multiple_answer_combination_true_false.class.php' , 'MultipleAnswerCombinationTrueFalse'),
  62. );
  63. /**
  64. * constructor of the class
  65. *
  66. * @author - Olivier Brouckaert
  67. */
  68. public function Question() {
  69. $this->id=0;
  70. $this->question='';
  71. $this->description='';
  72. $this->weighting=0;
  73. $this->position=1;
  74. $this->picture='';
  75. $this->level = 1;
  76. $this->extra='';
  77. $this->exerciseList=array();
  78. $this->course = api_get_course_info();
  79. }
  80. public function getIsContent() {
  81. $isContent = null;
  82. if (isset($_REQUEST['isContent'])) {
  83. $isContent = intval($_REQUEST['isContent']);
  84. }
  85. return $this->isContent = $isContent;
  86. }
  87. /**
  88. * Reads question informations from the data base
  89. *
  90. * @author - Olivier Brouckaert
  91. * @param - integer $id - question ID
  92. * @return - boolean - true if question exists, otherwise false
  93. */
  94. static function read($id, $course_id = null) {
  95. $id = intval($id);
  96. if (!empty($course_id)) {
  97. $course_info = api_get_course_info_by_id($course_id);
  98. } else {
  99. global $course;
  100. $course_info = api_get_course_info();
  101. }
  102. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST, $course_info['db_name']);
  103. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION, $course_info['db_name']);
  104. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION, $course_info['db_name']);
  105. $sql="SELECT question,description,ponderation,position,type,picture,level,extra FROM $TBL_QUESTIONS WHERE id= $id ";
  106. $result=Database::query($sql);
  107. // if the question has been found
  108. if ($object = Database::fetch_object($result)) {
  109. $objQuestion = Question::getInstance($object->type);
  110. $objQuestion->id = $id;
  111. $objQuestion->question = $object->question;
  112. $objQuestion->description = $object->description;
  113. $objQuestion->weighting = $object->ponderation;
  114. $objQuestion->position = $object->position;
  115. $objQuestion->type = $object->type;
  116. $objQuestion->picture = $object->picture;
  117. $objQuestion->level = (int) $object->level;
  118. $objQuestion->extra = $object->extra;
  119. $objQuestion->course = $course_info;
  120. $sql="SELECT exercice_id FROM $TBL_EXERCICE_QUESTION WHERE question_id='".$id."'";
  121. $result=Database::query($sql);
  122. // fills the array with the exercises which this question is in
  123. while($object=Database::fetch_object($result)) {
  124. $objQuestion->exerciseList[]=$object->exercice_id;
  125. }
  126. return $objQuestion;
  127. }
  128. // question not found
  129. return false;
  130. }
  131. /**
  132. * returns the question ID
  133. *
  134. * @author - Olivier Brouckaert
  135. * @return - integer - question ID
  136. */
  137. function selectId() {
  138. return $this->id;
  139. }
  140. /**
  141. * returns the question title
  142. *
  143. * @author - Olivier Brouckaert
  144. * @return - string - question title
  145. */
  146. function selectTitle() {
  147. $this->question=text_filter($this->question);
  148. return $this->question;
  149. }
  150. /**
  151. * returns the question description
  152. *
  153. * @author - Olivier Brouckaert
  154. * @return - string - question description
  155. */
  156. function selectDescription() {
  157. $this->description=text_filter($this->description);
  158. return $this->description;
  159. }
  160. /**
  161. * returns the question weighting
  162. *
  163. * @author - Olivier Brouckaert
  164. * @return - integer - question weighting
  165. */
  166. function selectWeighting()
  167. {
  168. return $this->weighting;
  169. }
  170. /**
  171. * returns the question position
  172. *
  173. * @author - Olivier Brouckaert
  174. * @return - integer - question position
  175. */
  176. function selectPosition()
  177. {
  178. return $this->position;
  179. }
  180. /**
  181. * returns the answer type
  182. *
  183. * @author - Olivier Brouckaert
  184. * @return - integer - answer type
  185. */
  186. function selectType()
  187. {
  188. return $this->type;
  189. }
  190. /**
  191. * returns the level of the question
  192. *
  193. * @author - Nicolas Raynaud
  194. * @return - integer - level of the question, 0 by default.
  195. */
  196. function selectLevel()
  197. {
  198. return $this->level;
  199. }
  200. /**
  201. * returns the picture name
  202. *
  203. * @author - Olivier Brouckaert
  204. * @return - string - picture name
  205. */
  206. function selectPicture()
  207. {
  208. return $this->picture;
  209. }
  210. function selectPicturePath() {
  211. if (!empty($this->picture)) {
  212. return api_get_path(WEB_COURSE_PATH).$this->course['path'].'/document/images/'.$this->picture;
  213. }
  214. return false;
  215. }
  216. /**
  217. * returns the array with the exercise ID list
  218. *
  219. * @author - Olivier Brouckaert
  220. * @return - array - list of exercise ID which the question is in
  221. */
  222. function selectExerciseList()
  223. {
  224. return $this->exerciseList;
  225. }
  226. /**
  227. * returns the number of exercises which this question is in
  228. *
  229. * @author - Olivier Brouckaert
  230. * @return - integer - number of exercises
  231. */
  232. function selectNbrExercises()
  233. {
  234. return sizeof($this->exerciseList);
  235. }
  236. /**
  237. * changes the question title
  238. *
  239. * @author - Olivier Brouckaert
  240. * @param - string $title - question title
  241. */
  242. function updateTitle($title)
  243. {
  244. $this->question=$title;
  245. }
  246. /**
  247. * changes the question description
  248. *
  249. * @author - Olivier Brouckaert
  250. * @param - string $description - question description
  251. */
  252. function updateDescription($description)
  253. {
  254. $this->description=$description;
  255. }
  256. /**
  257. * changes the question weighting
  258. *
  259. * @author - Olivier Brouckaert
  260. * @param - integer $weighting - question weighting
  261. */
  262. function updateWeighting($weighting)
  263. {
  264. $this->weighting=$weighting;
  265. }
  266. /**
  267. * changes the question position
  268. *
  269. * @author - Olivier Brouckaert
  270. * @param - integer $position - question position
  271. */
  272. function updatePosition($position)
  273. {
  274. $this->position=$position;
  275. }
  276. /**
  277. * changes the question level
  278. *
  279. * @author - Nicolas Raynaud
  280. * @param - integer $level - question level
  281. */
  282. function updateLevel($level)
  283. {
  284. $this->level=$level;
  285. }
  286. /**
  287. * changes the answer type. If the user changes the type from "unique answer" to "multiple answers"
  288. * (or conversely) answers are not deleted, otherwise yes
  289. *
  290. * @author - Olivier Brouckaert
  291. * @param - integer $type - answer type
  292. */
  293. function updateType($type) {
  294. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER, $this->course['db_name']);
  295. // if we really change the type
  296. if($type != $this->type) {
  297. // if we don't change from "unique answer" to "multiple answers" (or conversely)
  298. if(!in_array($this->type,array(UNIQUE_ANSWER,MULTIPLE_ANSWER)) || !in_array($type,array(UNIQUE_ANSWER,MULTIPLE_ANSWER))) {
  299. // removes old answers
  300. $sql="DELETE FROM $TBL_REPONSES WHERE question_id='".Database::escape_string($this->id)."'";
  301. Database::query($sql);
  302. }
  303. $this->type=$type;
  304. }
  305. }
  306. /**
  307. * adds a picture to the question
  308. *
  309. * @author - Olivier Brouckaert
  310. * @param - string $Picture - temporary path of the picture to upload
  311. * @param - string $PictureName - Name of the picture
  312. * @return - boolean - true if uploaded, otherwise false
  313. */
  314. function uploadPicture($Picture,$PictureName) {
  315. global $picturePath;
  316. if (!file_exists($picturePath)) {
  317. if (mkdir($picturePath, api_get_permissions_for_new_directories())) {
  318. // document path
  319. $documentPath = api_get_path(SYS_COURSE_PATH) . $this->course['path'] . "/document";
  320. $path = str_replace($documentPath,'',$picturePath);
  321. $title_path = basename($picturePath);
  322. $doc_id = add_document($this->course, $path, 'folder', 0,$title_path);
  323. api_item_property_update($this->course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', api_get_user_id());
  324. }
  325. }
  326. // if the question has got an ID
  327. if ($this->id) {
  328. $extension = pathinfo($PictureName, PATHINFO_EXTENSION);
  329. $this->picture = 'quiz-'.$this->id.'.jpg';
  330. $o_img = new Image($Picture);
  331. $o_img->send_image($picturePath.'/'.$this->picture, -1, 'jpg');
  332. $document_id = add_document($this->course, '/images/'.$this->picture, 'file', filesize($picturePath.'/'.$this->picture),$this->picture);
  333. if ($document_id) {
  334. return api_item_property_update($this->course, TOOL_DOCUMENT, $document_id, 'DocumentAdded', api_get_user_id);
  335. }
  336. }
  337. return false;
  338. }
  339. /**
  340. * Resizes a picture || Warning!: can only be called after uploadPicture, or if picture is already available in object.
  341. *
  342. * @author - Toon Keppens
  343. * @param - string $Dimension - Resizing happens proportional according to given dimension: height|width|any
  344. * @param - integer $Max - Maximum size
  345. * @return - boolean - true if success, false if failed
  346. */
  347. function resizePicture($Dimension, $Max) {
  348. global $picturePath;
  349. // if the question has an ID
  350. if($this->id) {
  351. // Get dimensions from current image.
  352. $my_image = new Image($picturePath.'/'.$this->picture);
  353. $current_image_size = $my_image->get_image_size();
  354. $current_width = $current_image_size['width'];
  355. $current_height = $current_image_size['height'];
  356. if($current_width < $Max && $current_height <$Max)
  357. return true;
  358. elseif($current_height == "")
  359. return false;
  360. // Resize according to height.
  361. if ($Dimension == "height") {
  362. $resize_scale = $current_height / $Max;
  363. $new_height = $Max;
  364. $new_width = ceil($current_width / $resize_scale);
  365. }
  366. // Resize according to width
  367. if ($Dimension == "width") {
  368. $resize_scale = $current_width / $Max;
  369. $new_width = $Max;
  370. $new_height = ceil($current_height / $resize_scale);
  371. }
  372. // Resize according to height or width, both should not be larger than $Max after resizing.
  373. if ($Dimension == "any") {
  374. if ($current_height > $current_width || $current_height == $current_width)
  375. {
  376. $resize_scale = $current_height / $Max;
  377. $new_height = $Max;
  378. $new_width = ceil($current_width / $resize_scale);
  379. }
  380. if ($current_height < $current_width)
  381. {
  382. $resize_scale = $current_width / $Max;
  383. $new_width = $Max;
  384. $new_height = ceil($current_height / $resize_scale);
  385. }
  386. }
  387. $my_image->resize($new_width, $new_height);
  388. $result = $my_image->send_image($picturePath.'/'.$this->picture);
  389. if ($result) {
  390. return true;
  391. } else {
  392. return false;
  393. }
  394. }
  395. }
  396. /**
  397. * deletes the picture
  398. *
  399. * @author - Olivier Brouckaert
  400. * @return - boolean - true if removed, otherwise false
  401. */
  402. function removePicture()
  403. {
  404. global $picturePath;
  405. // if the question has got an ID and if the picture exists
  406. if($this->id)
  407. {
  408. $picture=$this->picture;
  409. $this->picture='';
  410. return @unlink($picturePath.'/'.$picture)?true:false;
  411. }
  412. return false;
  413. }
  414. /**
  415. * Exports a picture to another question
  416. *
  417. * @author - Olivier Brouckaert
  418. * @param - integer $questionId - ID of the target question
  419. * @return - boolean - true if copied, otherwise false
  420. */
  421. function exportPicture($questionId, $course_info) {
  422. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION, $course_info['db_name']);
  423. $destination_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document/images';
  424. $source_path = api_get_path(SYS_COURSE_PATH).$this->course['path'].'/document/images';
  425. // if the question has got an ID and if the picture exists
  426. if($this->id && !empty($this->picture)) {
  427. $picture=explode('.',$this->picture);
  428. $Extension=$picture[sizeof($picture)-1];
  429. $picture='quiz-'.$questionId.'.'.$Extension;
  430. $sql="UPDATE $TBL_QUESTIONS SET picture='".Database::escape_string($picture)."' WHERE id='".intval($questionId)."'";
  431. Database::query($sql);
  432. return @copy($source_path.'/'.$this->picture, $destination_path.'/'.$picture)?true:false;
  433. }
  434. return false;
  435. }
  436. /**
  437. * Saves the picture coming from POST into a temporary file
  438. * Temporary pictures are used when we don't want to save a picture right after a form submission.
  439. * For example, if we first show a confirmation box.
  440. *
  441. * @author - Olivier Brouckaert
  442. * @param - string $Picture - temporary path of the picture to move
  443. * @param - string $PictureName - Name of the picture
  444. */
  445. function setTmpPicture($Picture,$PictureName) {
  446. global $picturePath;
  447. $PictureName=explode('.',$PictureName);
  448. $Extension=$PictureName[sizeof($PictureName)-1];
  449. // saves the picture into a temporary file
  450. @move_uploaded_file($Picture,$picturePath.'/tmp.'.$Extension);
  451. }
  452. /**
  453. Sets the title
  454. */
  455. public function setTitle($title)
  456. {
  457. $this->question = $title;
  458. }
  459. /**
  460. Sets the title
  461. */
  462. public function setExtra($extra)
  463. {
  464. $this->extra = $extra;
  465. }
  466. /**
  467. * Moves the temporary question "tmp" to "quiz-$questionId"
  468. * Temporary pictures are used when we don't want to save a picture right after a form submission.
  469. * For example, if we first show a confirmation box.
  470. *
  471. * @author - Olivier Brouckaert
  472. * @return - boolean - true if moved, otherwise false
  473. */
  474. function getTmpPicture()
  475. {
  476. global $picturePath;
  477. // if the question has got an ID and if the picture exists
  478. if($this->id)
  479. {
  480. if(file_exists($picturePath.'/tmp.jpg'))
  481. {
  482. $Extension='jpg';
  483. }
  484. elseif(file_exists($picturePath.'/tmp.gif'))
  485. {
  486. $Extension='gif';
  487. }
  488. elseif(file_exists($picturePath.'/tmp.png'))
  489. {
  490. $Extension='png';
  491. }
  492. $this->picture='quiz-'.$this->id.'.'.$Extension;
  493. return @rename($picturePath.'/tmp.'.$Extension,$picturePath.'/'.$this->picture)?true:false;
  494. }
  495. return false;
  496. }
  497. /**
  498. * updates the question in the data base
  499. * if an exercise ID is provided, we add that exercise ID into the exercise list
  500. *
  501. * @author - Olivier Brouckaert
  502. * @param - integer $exerciseId - exercise ID if saving in an exercise
  503. */
  504. function save($exerciseId=0) {
  505. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION, $this->course['db_name']);
  506. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION, $this->course['db_name']);
  507. $id = $this->id;
  508. $question = $this->question;
  509. $description = $this->description;
  510. $weighting = $this->weighting;
  511. $position = $this->position;
  512. $type = $this->type;
  513. $picture = $this->picture;
  514. $level = $this->level;
  515. $extra = $this->extra;
  516. $c_id = $this->course['real_id'];
  517. // question already exists
  518. if(!empty($id)) {
  519. $sql="UPDATE $TBL_QUESTIONS SET
  520. question ='".Database::escape_string($question)."',
  521. description ='".Database::escape_string($description)."',
  522. ponderation ='".Database::escape_string($weighting)."',
  523. position ='".Database::escape_string($position)."',
  524. type ='".Database::escape_string($type)."',
  525. picture ='".Database::escape_string($picture)."',
  526. extra ='".Database::escape_string($extra)."',
  527. level ='".Database::escape_string($level)."'
  528. WHERE id='".Database::escape_string($id)."'";
  529. Database::query($sql);
  530. if (!empty($exerciseId)) {
  531. api_item_property_update($this->course, TOOL_QUIZ, $id,'QuizQuestionUpdated',api_get_user_id);
  532. }
  533. if (api_get_setting('search_enabled')=='true') {
  534. if ($exerciseId != 0) {
  535. $this -> search_engine_edit($exerciseId);
  536. } else {
  537. /**
  538. * actually there is *not* an user interface for
  539. * creating questions without a relation with an exercise
  540. */
  541. }
  542. }
  543. } else {
  544. // creates a new question
  545. $sql = "SELECT max(position) FROM $TBL_QUESTIONS as question, $TBL_EXERCICE_QUESTION as test_question
  546. WHERE question.id = test_question.question_id AND
  547. test_question.exercice_id = '".Database::escape_string($exerciseId)."' AND
  548. question.c_id = $c_id AND
  549. test_question.c_id = $c_id
  550. ";
  551. $result = Database::query($sql);
  552. $current_position = Database::result($result,0,0);
  553. $this->updatePosition($current_position+1);
  554. $position = $this->position;
  555. $sql = "INSERT INTO $TBL_QUESTIONS (c_id, question, description, ponderation, position, type, picture, extra, level) VALUES (
  556. $c_id,
  557. '".Database::escape_string($question)."',
  558. '".Database::escape_string($description)."',
  559. '".Database::escape_string($weighting)."',
  560. '".Database::escape_string($position)."',
  561. '".Database::escape_string($type)."',
  562. '".Database::escape_string($picture)."',
  563. '".Database::escape_string($extra)."',
  564. '".Database::escape_string($level)."'
  565. )";
  566. Database::query($sql);
  567. $this->id = Database::insert_id();
  568. api_item_property_update($this->course, TOOL_QUIZ, $this->id,'QuizQuestionAdded',api_get_user_id());
  569. // If hotspot, create first answer
  570. if ($type == HOT_SPOT || $type == HOT_SPOT_ORDER) {
  571. $TBL_ANSWERS = Database::get_course_table(TABLE_QUIZ_ANSWER);
  572. $sql = "INSERT INTO $TBL_ANSWERS (c_id, id, question_id , answer , correct , comment , ponderation , position , hotspot_coordinates , hotspot_type )
  573. VALUES (".$c_id.", '1', '".Database::escape_string($this->id)."', '', NULL , '', '10' , '1', '0;0|0|0', 'square')";
  574. Database::query($sql);
  575. }
  576. if ($type == HOT_SPOT_DELINEATION ) {
  577. $TBL_ANSWERS = Database::get_course_table(TABLE_QUIZ_ANSWER);
  578. $sql="INSERT INTO $TBL_ANSWERS (c_id, id, question_id , answer , correct , comment , ponderation , position , hotspot_coordinates , hotspot_type )
  579. VALUES (".$c_id.", '1', '".Database::escape_string($this->id)."', '', NULL , '', '10' , '1', '0;0|0|0', 'delineation')";
  580. Database::query($sql);
  581. }
  582. if (api_get_setting('search_enabled')=='true') {
  583. if ($exerciseId != 0) {
  584. $this -> search_engine_edit($exerciseId, TRUE);
  585. } else {
  586. /**
  587. * actually there is *not* an user interface for
  588. * creating questions without a relation with an exercise
  589. */
  590. }
  591. }
  592. }
  593. // if the question is created in an exercise
  594. if($exerciseId) {
  595. /*
  596. $sql = 'UPDATE '.Database::get_course_table(TABLE_LP_ITEM).'
  597. SET max_score = '.intval($weighting).'
  598. WHERE item_type = "'.TOOL_QUIZ.'"
  599. AND path='.intval($exerciseId);
  600. Database::query($sql);
  601. */
  602. // adds the exercise into the exercise list of this question
  603. $this->addToList($exerciseId, TRUE);
  604. }
  605. }
  606. function search_engine_edit($exerciseId, $addQs=FALSE, $rmQs=FALSE) {
  607. // update search engine and its values table if enabled
  608. if (api_get_setting('search_enabled')=='true' && extension_loaded('xapian')) {
  609. $course_id = api_get_course_id();
  610. // get search_did
  611. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  612. if ($addQs || $rmQs) {
  613. //there's only one row per question on normal db and one document per question on search engine db
  614. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_second_level=%s LIMIT 1';
  615. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  616. } else {
  617. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%s LIMIT 1';
  618. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id);
  619. }
  620. $res = Database::query($sql);
  621. if (Database::num_rows($res) > 0 || $addQs) {
  622. require_once(api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php');
  623. require_once(api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php');
  624. $di = new DokeosIndexer();
  625. if ($addQs) {
  626. $question_exercises = array((int)$exerciseId);
  627. } else {
  628. $question_exercises = array();
  629. }
  630. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  631. $di->connectDb(NULL, NULL, $lang);
  632. // retrieve others exercise ids
  633. $se_ref = Database::fetch_array($res);
  634. $se_doc = $di->get_document((int)$se_ref['search_did']);
  635. if ($se_doc !== FALSE) {
  636. if ( ($se_doc_data=$di->get_document_data($se_doc)) !== FALSE ) {
  637. $se_doc_data = unserialize($se_doc_data);
  638. if (isset($se_doc_data[SE_DATA]['type']) && $se_doc_data[SE_DATA]['type'] == SE_DOCTYPE_EXERCISE_QUESTION) {
  639. if (isset($se_doc_data[SE_DATA]['exercise_ids']) && is_array($se_doc_data[SE_DATA]['exercise_ids'])) {
  640. foreach ($se_doc_data[SE_DATA]['exercise_ids'] as $old_value) {
  641. if (!in_array($old_value, $question_exercises)) {
  642. $question_exercises[] = $old_value;
  643. }
  644. }
  645. }
  646. }
  647. }
  648. }
  649. if ($rmQs) {
  650. while ( ($key=array_search($exerciseId, $question_exercises)) !== FALSE) {
  651. unset($question_exercises[$key]);
  652. }
  653. }
  654. // build the chunk to index
  655. $ic_slide = new IndexableChunk();
  656. $ic_slide->addValue("title", $this->question);
  657. $ic_slide->addCourseId($course_id);
  658. $ic_slide->addToolId(TOOL_QUIZ);
  659. $xapian_data = array(
  660. SE_COURSE_ID => $course_id,
  661. SE_TOOL_ID => TOOL_QUIZ,
  662. SE_DATA => array('type' => SE_DOCTYPE_EXERCISE_QUESTION, 'exercise_ids' => $question_exercises, 'question_id' => (int)$this->id),
  663. SE_USER => (int)api_get_user_id(),
  664. );
  665. $ic_slide->xapian_data = serialize($xapian_data);
  666. $ic_slide->addValue("content", $this->description);
  667. //TODO: index answers, see also form validation on question_admin.inc.php
  668. $di->remove_document((int)$se_ref['search_did']);
  669. $di->addChunk($ic_slide);
  670. //index and return search engine document id
  671. if (!empty($question_exercises)) { // if empty there is nothing to index
  672. $did = $di->index();
  673. unset($di);
  674. }
  675. if ($did || $rmQs) {
  676. // save it to db
  677. if ($addQs || $rmQs) {
  678. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_second_level=\'%s\'';
  679. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  680. } else {
  681. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\' AND ref_id_second_level=\'%s\'';
  682. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id);
  683. }
  684. Database::query($sql);
  685. if ($rmQs) {
  686. if (!empty($question_exercises)) {
  687. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
  688. VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
  689. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, array_shift($question_exercises), $this->id, $did);
  690. Database::query($sql);
  691. }
  692. } else {
  693. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
  694. VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
  695. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id, $did);
  696. Database::query($sql);
  697. }
  698. }
  699. }
  700. }
  701. }
  702. /**
  703. * adds an exercise into the exercise list
  704. *
  705. * @author - Olivier Brouckaert
  706. * @param - integer $exerciseId - exercise ID
  707. * @param - boolean $fromSave - comming from $this->save() or not
  708. */
  709. function addToList($exerciseId, $fromSave = FALSE) {
  710. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION, $this->course['db_name']);
  711. $id = $this->id;
  712. // checks if the exercise ID is not in the list
  713. if (!in_array($exerciseId,$this->exerciseList)) {
  714. $this->exerciseList[]=$exerciseId;
  715. $new_exercise = new Exercise();
  716. $new_exercise->read($exerciseId);
  717. $count = $new_exercise->selectNbrQuestions();
  718. $count++;
  719. $sql="INSERT INTO $TBL_EXERCICE_QUESTION (c_id, question_id, exercice_id, question_order) VALUES
  720. ({$this->course['real_id']}, '".Database::escape_string($id)."','".Database::escape_string($exerciseId)."', '$count' )";
  721. Database::query($sql);
  722. // we do not want to reindex if we had just saved adnd indexed the question
  723. if (!$fromSave) {
  724. $this->search_engine_edit($exerciseId, TRUE);
  725. }
  726. }
  727. }
  728. /**
  729. * removes an exercise from the exercise list
  730. *
  731. * @author - Olivier Brouckaert
  732. * @param - integer $exerciseId - exercise ID
  733. * @return - boolean - true if removed, otherwise false
  734. */
  735. function removeFromList($exerciseId) {
  736. global $TBL_EXERCICE_QUESTION;
  737. $id=$this->id;
  738. // searches the position of the exercise ID in the list
  739. $pos=array_search($exerciseId,$this->exerciseList);
  740. // exercise not found
  741. if($pos === false) {
  742. return false;
  743. } else {
  744. // deletes the position in the array containing the wanted exercise ID
  745. unset($this->exerciseList[$pos]);
  746. //update order of other elements
  747. $sql = "SELECT question_order FROM $TBL_EXERCICE_QUESTION WHERE question_id='".Database::escape_string($id)."' AND exercice_id='".Database::escape_string($exerciseId)."'";
  748. $res = Database::query($sql);
  749. if (Database::num_rows($res)>0) {
  750. $row = Database::fetch_array($res);
  751. if (!empty($row['question_order'])) {
  752. $sql = "UPDATE $TBL_EXERCICE_QUESTION SET question_order = question_order-1 WHERE exercice_id='".Database::escape_string($exerciseId)."' AND question_order > ".$row['question_order'];
  753. $res = Database::query($sql);
  754. }
  755. }
  756. $sql="DELETE FROM $TBL_EXERCICE_QUESTION WHERE question_id='".Database::escape_string($id)."' AND exercice_id='".Database::escape_string($exerciseId)."'";
  757. Database::query($sql);
  758. return true;
  759. }
  760. }
  761. /**
  762. * Deletes a question from the database
  763. * the parameter tells if the question is removed from all exercises (value = 0),
  764. * or just from one exercise (value = exercise ID)
  765. *
  766. * @author - Olivier Brouckaert
  767. * @param - integer $deleteFromEx - exercise ID if the question is only removed from one exercise
  768. */
  769. function delete($deleteFromEx=0) {
  770. global $_course,$_user;
  771. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION, $this->course['db_name']);
  772. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION, $this->course['db_name']);
  773. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER, $this->course['db_name']);
  774. $id=$this->id;
  775. // if the question must be removed from all exercises
  776. if(!$deleteFromEx)
  777. {
  778. //update the question_order of each question to avoid inconsistencies
  779. $sql = "SELECT exercice_id, question_order FROM $TBL_EXERCICE_QUESTION WHERE question_id='".Database::escape_string($id)."'";
  780. $res = Database::query($sql);
  781. if (Database::num_rows($res)>0) {
  782. while ($row = Database::fetch_array($res)) {
  783. if (!empty($row['question_order'])) {
  784. $sql = "UPDATE $TBL_EXERCICE_QUESTION SET question_order = question_order-1 WHERE exercice_id='".Database::escape_string($row['exercice_id'])."' AND question_order > ".$row['question_order'];
  785. $res = Database::query($sql);
  786. }
  787. }
  788. }
  789. $sql="DELETE FROM $TBL_EXERCICE_QUESTION WHERE question_id='".Database::escape_string($id)."'";
  790. Database::query($sql);
  791. $sql="DELETE FROM $TBL_QUESTIONS WHERE id='".Database::escape_string($id)."'";
  792. Database::query($sql);
  793. $sql="DELETE FROM $TBL_REPONSES WHERE question_id='".Database::escape_string($id)."'";
  794. Database::query($sql);
  795. api_item_property_update($this->course, TOOL_QUIZ, $id,'QuizQuestionDeleted',api_get_user_id());
  796. $this->removePicture();
  797. // resets the object
  798. $this->Question();
  799. }
  800. // just removes the exercise from the list
  801. else
  802. {
  803. $this->removeFromList($deleteFromEx);
  804. if (api_get_setting('search_enabled')=='true' && extension_loaded('xapian')) {
  805. // disassociate question with this exercise
  806. $this -> search_engine_edit($deleteFromEx, FALSE, TRUE);
  807. }
  808. api_item_property_update($this->course, TOOL_QUIZ, $id,'QuizQuestionDeleted',api_get_user_id());
  809. }
  810. }
  811. /**
  812. * Duplicates the question
  813. *
  814. * @author Olivier Brouckaert
  815. * @param array Course info of the destination course
  816. * @return int ID of the new question
  817. */
  818. function duplicate($course_info = null) {
  819. if (empty($course_info)) {
  820. $course_info = $this->course;
  821. } else {
  822. $course_info = $course_info;
  823. }
  824. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION, $course_info['db_name']);
  825. $TBL_QUESTION_OPTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION, $course_info['db_name']);
  826. $question = $this->question;
  827. $description = $this->description;
  828. $weighting = $this->weighting;
  829. $position = $this->position;
  830. $type = $this->type;
  831. $level = intval($this->level);
  832. $extra = $this->extra;
  833. //Using the same method used in the course copy to transform URLs
  834. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  835. if ($course_info['db_name'] != $this->course['db_name']) {
  836. $description = DocumentManager::replace_urls_inside_content_html_from_copy_course($description, $this->course['id'], $course_info['id']);
  837. $question = DocumentManager::replace_urls_inside_content_html_from_copy_course($question, $this->course['id'], $course_info['id']);
  838. }
  839. $options = self::readQuestionOption($this->id);
  840. //Inserting in the new course db / or the same course db
  841. $sql="INSERT INTO $TBL_QUESTIONS(question, description, ponderation, position, type, level, extra ) VALUES('".Database::escape_string($question)."','".Database::escape_string($description)."','".Database::escape_string($weighting)."','".Database::escape_string($position)."','".Database::escape_string($type)."' ,'".Database::escape_string($level)."' ,'".Database::escape_string($extra)."' )";
  842. Database::query($sql);
  843. $new_question_id =Database::insert_id();
  844. if (!empty($options)) {
  845. //Saving the quiz_options
  846. foreach ($options as $item) {
  847. $item['question_id'] = $new_question_id;
  848. unset($item['id']);
  849. Database::insert($TBL_QUESTION_OPTIONS,$item);
  850. }
  851. }
  852. // Duplicates the picture
  853. $this->exportPicture($new_question_id, $course_info);
  854. return $new_question_id;
  855. }
  856. /**
  857. * Returns an instance of the class corresponding to the type
  858. * @param integer $type the type of the question
  859. * @return an instance of a Question subclass (or of Questionc class by default)
  860. */
  861. static function getInstance ($type) {
  862. if (!is_null($type)) {
  863. list($file_name,$class_name) = self::$questionTypes[$type];
  864. include_once($file_name);
  865. if (class_exists($class_name)) {
  866. return new $class_name();
  867. } else {
  868. echo 'Can\'t instanciate class '.$class_name.' of type '.$type;
  869. return null;
  870. }
  871. }
  872. }
  873. /**
  874. * Creates the form to create / edit a question
  875. * A subclass can redifine this function to add fields...
  876. * @param FormValidator $form the formvalidator instance (by reference)
  877. */
  878. function createForm (&$form,$fck_config=0) {
  879. echo ' <style>
  880. div.row div.label{ width: 8%; }
  881. div.row div.formw{ width: 88%; }
  882. .media { display:none;}
  883. </style>';
  884. echo '<script>
  885. // hack to hide http://cksource.com/forums/viewtopic.php?f=6&t=8700
  886. function FCKeditor_OnComplete( editorInstance )
  887. {
  888. if (document.getElementById ( \'HiddenFCK\' + editorInstance.Name )) {
  889. HideFCKEditorByInstanceName (editorInstance.Name);
  890. }
  891. }
  892. function HideFCKEditorByInstanceName ( editorInstanceName ) {
  893. if (document.getElementById ( \'HiddenFCK\' + editorInstanceName ).className == "HideFCKEditor" ) {
  894. document.getElementById ( \'HiddenFCK\' + editorInstanceName ).className = "media";
  895. }
  896. }
  897. function show_media()
  898. {
  899. var my_display = document.getElementById(\'HiddenFCKquestionDescription\').style.display;
  900. if(my_display== \'none\' || my_display == \'\') {
  901. document.getElementById(\'HiddenFCKquestionDescription\').style.display = \'block\';
  902. document.getElementById(\'media_icon\').innerHTML=\'&nbsp;<img style="vertical-align: middle;" src="../img/looknfeelna.png" alt="" />&nbsp;'.get_lang('EnrichQuestion').'\';
  903. } else {
  904. document.getElementById(\'HiddenFCKquestionDescription\').style.display = \'none\';
  905. document.getElementById(\'media_icon\').innerHTML=\'&nbsp;<img style="vertical-align: middle;" src="../img/looknfeel.png" alt="" />&nbsp;'.get_lang('EnrichQuestion').'\';
  906. }
  907. }
  908. </script>';
  909. $renderer = $form->defaultRenderer();
  910. $form->addElement('html','<div class="form">');
  911. // question name
  912. $form->addElement('text','questionName','<span class="form_required">*</span> '.get_lang('Question'),'size="80"');
  913. //$form->applyFilter('questionName','html_filter');
  914. //$radios_results_enabled[] = $form->createElement('static', null, null, null);
  915. //$test=FormValidator :: createElement ('text', 'questionName');
  916. //$radios_results_enabled[]=$test;
  917. // question level
  918. //@todo move levles into a table
  919. $select_level = array (1,2,3,4,5);
  920. //$radios_results_enabled[] =
  921. foreach($select_level as $val) {
  922. $radios_results_enabled[] = FormValidator :: createElement ('radio', null, null,$val,$val);
  923. }
  924. $form->addGroup($radios_results_enabled,'questionLevel',get_lang('Difficulty'));
  925. $renderer->setElementTemplate('<div class="row"><div class="label">{label}</div><div class="formw" >{element}</div></div>','questionName');
  926. $renderer->setElementTemplate('<div class="row"><div class="label">{label}</div><div class="formw">{element}</div></div>','questionLevel');
  927. $form->addRule('questionName', get_lang('GiveQuestion'), 'required');
  928. // default content
  929. $isContent = isset($_REQUEST['isContent']) ? intval($_REQUEST['isContent']) : null;
  930. // question type
  931. $answerType= intval($_REQUEST['answerType']);
  932. $form->addElement('hidden','answerType',$_REQUEST['answerType']);
  933. // html editor
  934. $editor_config = array('ToolbarSet' => 'TestQuestionDescription', 'Width' => '100%', 'Height' => '150');
  935. if(is_array($fck_config)){
  936. $editor_config = array_merge($editor_config, $fck_config);
  937. }
  938. if(!api_is_allowed_to_edit(null,true)) $editor_config['UserStatus'] = 'student';
  939. $form -> addElement('html','<div class="row">
  940. <div class="label"></div>
  941. <div class="formw" style="height:50px">
  942. <a href="javascript://" onclick=" return show_media()"> <span id="media_icon"> <img style="vertical-align: middle;" src="../img/looknfeel.png" alt="" />&nbsp;'.get_lang('EnrichQuestion').'</span></a>
  943. </div>
  944. </div>');
  945. $form -> addElement ('html','<div class="HideFCKEditor" id="HiddenFCKquestionDescription" >');
  946. $form->add_html_editor('questionDescription', get_lang('langQuestionDescription'), false, false, $editor_config);
  947. $form -> addElement ('html','</div>');
  948. $renderer->setElementTemplate('<div class="row"><div class="label">{label}</div><div class="formw">{element}</div></div>','questionDescription');
  949. // hidden values
  950. $form->addElement('hidden','myid',$_REQUEST['myid']);
  951. if (!isset($_GET['fromExercise'])) {
  952. switch($answerType) {
  953. case 1: $this->question = get_lang('langDefaultUniqueQuestion'); break;
  954. case 2: $this->question = get_lang('langDefaultMultipleQuestion'); break;
  955. case 3: $this->question = get_lang('langDefaultFillBlankQuestion'); break;
  956. case 4: $this->question = get_lang('langDefaultMathingQuestion'); break;
  957. case 5: $this->question = get_lang('langDefaultOpenQuestion'); break;
  958. case 9: $this->question = get_lang('langDefaultMultipleQuestion'); break;
  959. }
  960. }
  961. $form->addElement('html','</div>');
  962. // default values
  963. $defaults = array();
  964. $defaults['questionName'] = $this -> question;
  965. $defaults['questionDescription'] = $this -> description;
  966. $defaults['questionLevel'] = $this -> level;
  967. //Came from he question pool
  968. if (isset($_GET['fromExercise'])) {
  969. $form->setDefaults($defaults);
  970. }
  971. if (!empty($_REQUEST['myid'])) {
  972. $form->setDefaults($defaults);
  973. } else {
  974. if ($isContent == 1) {
  975. $form->setDefaults($defaults);
  976. }
  977. }
  978. }
  979. /**
  980. * function which process the creation of questions
  981. * @param FormValidator $form the formvalidator instance
  982. * @param Exercise $objExercise the Exercise instance
  983. */
  984. function processCreation ($form, $objExercise) {
  985. $this -> updateTitle($form->getSubmitValue('questionName'));
  986. $this -> updateDescription($form->getSubmitValue('questionDescription'));
  987. $this -> updateLevel($form->getSubmitValue('questionLevel'));
  988. $this -> save($objExercise -> id);
  989. // modify the exercise
  990. $objExercise->addToList($this -> id);
  991. $objExercise->update_question_positions();
  992. }
  993. /**
  994. * abstract function which creates the form to create / edit the answers of the question
  995. * @param the formvalidator instance
  996. */
  997. abstract function createAnswersForm ($form);
  998. /**
  999. * abstract function which process the creation of answers
  1000. * @param the formvalidator instance
  1001. */
  1002. abstract function processAnswersCreation ($form);
  1003. /**
  1004. * Displays the menu of question types
  1005. */
  1006. static function display_type_menu ($feedbacktype = 0) {
  1007. global $exerciseId;
  1008. // 1. by default we show all the question types
  1009. $question_type_custom_list = self::$questionTypes;
  1010. if (!isset($feedbacktype)) $feedbacktype=0;
  1011. if ($feedbacktype==1) {
  1012. //2. but if it is a feedback DIRECT we only show the UNIQUE_ANSWER type that is currently available
  1013. //$question_type_custom_list = array ( UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER]);
  1014. $question_type_custom_list = array ( UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER],HOT_SPOT_DELINEATION => self::$questionTypes[HOT_SPOT_DELINEATION]);
  1015. } else {
  1016. unset($question_type_custom_list[HOT_SPOT_DELINEATION]);
  1017. }
  1018. //blocking edition
  1019. $show_quiz_edition = true;
  1020. if (isset($exerciseId) && !empty($exerciseId)) {
  1021. $TBL_LP_ITEM = Database::get_course_table(TABLE_LP_ITEM);
  1022. $sql="SELECT max_score FROM $TBL_LP_ITEM
  1023. WHERE item_type = '".TOOL_QUIZ."' AND path ='".Database::escape_string($exerciseId)."'";
  1024. $result = Database::query($sql);
  1025. if (Database::num_rows($result) > 0) {
  1026. $show_quiz_edition = false;
  1027. }
  1028. }
  1029. echo '<ul class="question_menu">';
  1030. foreach ($question_type_custom_list as $i=>$a_type) {
  1031. // include the class of the type
  1032. require_once($a_type[0]);
  1033. // get the picture of the type and the langvar which describes it
  1034. $img = $explanation = '';
  1035. eval('$img = '.$a_type[1].'::$typePicture;');
  1036. eval('$explanation = get_lang('.$a_type[1].'::$explanationLangVar);');
  1037. echo '<li>';
  1038. echo '<div class="icon_image_content">';
  1039. if ($show_quiz_edition) {
  1040. echo '<a href="admin.php?'.api_get_cidreq().'&newQuestion=yes&answerType='.$i.'">'.Display::return_icon($img, $explanation).'</a>';
  1041. //echo '<br>';
  1042. //echo '<a href="admin.php?'.api_get_cidreq().'&newQuestion=yes&answerType='.$i.'">'.$explanation.'</a>';
  1043. } else {
  1044. $img = pathinfo($img);
  1045. $img = $img['filename'];
  1046. echo ''.Display::return_icon($img.'_na.gif',$explanation).'';
  1047. //echo '<br>';
  1048. //echo ''.$explanation.'';
  1049. }
  1050. echo '</div>';
  1051. echo '</li>';
  1052. }
  1053. echo '<li>';
  1054. echo '<div class="icon_image_content">';
  1055. if ($show_quiz_edition) {
  1056. if ($feedbacktype==1) {
  1057. echo $url = '<a href="question_pool.php?'.api_get_cidreq().'&type=1&fromExercise='.$exerciseId.'">';
  1058. } else {
  1059. echo $url = '<a href="question_pool.php?'.api_get_cidreq().'&fromExercise='.$exerciseId.'">';
  1060. }
  1061. echo Display::return_icon('database.png', get_lang('GetExistingQuestion'), '');
  1062. } else {
  1063. echo Display::return_icon('database_na.png', get_lang('GetExistingQuestion'), '');
  1064. }
  1065. //echo '<br>';
  1066. //echo $url;
  1067. //echo get_lang('GetExistingQuestion');
  1068. echo '</a>';
  1069. echo '</div></li>';
  1070. echo '</ul>';
  1071. }
  1072. static function get_types_information(){
  1073. return self::$questionTypes;
  1074. }
  1075. static function updateId() {
  1076. return self::$questionTypes;
  1077. }
  1078. static function saveQuestionOption($question_id, $name, $position = 0) {
  1079. $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1080. $params['question_id'] = intval($question_id);
  1081. $params['name'] = $name;
  1082. $params['position'] = $position;
  1083. $result = self::readQuestionOption($question_id);
  1084. $last_id = Database::insert($TBL_EXERCICE_QUESTION_OPTION, $params);
  1085. return $last_id;
  1086. }
  1087. static function deleteAllQuestionOptions($question_id) {
  1088. $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1089. Database::delete($TBL_EXERCICE_QUESTION_OPTION, array('question_id = ?'=> $question_id));
  1090. }
  1091. static function updateQuestionOption($id, $params) {
  1092. $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1093. $result = Database::update($TBL_EXERCICE_QUESTION_OPTION, $params, array('id = ?'=>$id));
  1094. return $result;
  1095. }
  1096. static function readQuestionOption($question_id, $db_name = null) {
  1097. if (empty($db_name)) {
  1098. $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1099. } else {
  1100. $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION, $db_name);
  1101. }
  1102. $result = Database::select('*', $TBL_EXERCICE_QUESTION_OPTION, array('where'=>array('question_id = ?' =>$question_id), 'order'=>'id ASC'));
  1103. return $result;
  1104. }
  1105. function return_header($feedback_type = null, $counter = null) {
  1106. $counter_label = '';
  1107. if (!empty($counter)) {
  1108. $counter_label = intval($counter);
  1109. }
  1110. echo Display::div(get_lang("Question").' '.($counter_label).' : '.$this->question, array('id'=>'question_title', 'class'=>'sectiontitle'));
  1111. echo Display::div($this->description, array('id'=>'question_description'));
  1112. }
  1113. /**
  1114. * Create a question from a set of parameters
  1115. * @param int Quiz ID
  1116. * @param string Question name
  1117. * @param int Maximum result for the question
  1118. * @param int Type of question (see constants at beginning of question.class.php)
  1119. * @param int Question level/category
  1120. */
  1121. function create_question ($quiz_id, $question_name, $max_score = 0, $type = 1, $level = 1) {
  1122. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1123. $tbl_quiz_rel_question = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1124. $quiz_id = filter_var($quiz_id,FILTER_SANITIZE_NUMBER_INT);
  1125. $max_score = filter_var($max_score,FILTER_SANITIZE_NUMBER_FLOAT);
  1126. $type = filter_var($type,FILTER_SANITIZE_NUMBER_INT);
  1127. $level = filter_var($level,FILTER_SANITIZE_NUMBER_INT);
  1128. // Get the max position
  1129. $sql = "SELECT max(position) as max_position"
  1130. ." FROM $tbl_quiz_question q INNER JOIN $tbl_quiz_rel_question r"
  1131. ." ON q.id = r.question_id"
  1132. ." AND exercice_id = $quiz_id";
  1133. $rs_max = Database::query($sql, __FILE__, __LINE__);
  1134. $row_max = Database::fetch_object($rs_max);
  1135. $max_position = $row_max->max_position +1;
  1136. // Insert the new question
  1137. $sql = "INSERT INTO $tbl_quiz_question"
  1138. ." (question,ponderation,position,type,level) "
  1139. ." VALUES('".Database::escape_string($question_name)."',"
  1140. ." $max_score , $max_position, $type, $level)";
  1141. $rs = Database::query($sql);
  1142. // Get the question ID
  1143. $question_id = Database::get_last_insert_id();
  1144. // Get the max question_order
  1145. $sql = "SELECT max(question_order) as max_order "
  1146. ."FROM $tbl_quiz_rel_question WHERE exercice_id = $quiz_id ";
  1147. $rs_max_order = Database::query($sql);
  1148. $row_max_order = Database::fetch_object($rs_max_order);
  1149. $max_order = $row_max_order->max_order + 1;
  1150. // Attach questions to quiz
  1151. $sql = "INSERT INTO $tbl_quiz_rel_question "
  1152. ."(question_id,exercice_id,question_order)"
  1153. ." VALUES($question_id, $quiz_id, $max_order)";
  1154. $rs = Database::query($sql);
  1155. return $question_id;
  1156. }
  1157. }
  1158. endif;