question.class.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  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. * Modified by hubert.borderiou@grenet.fr - add question categories
  9. */
  10. /**
  11. * Code
  12. */
  13. // Question 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. define('ORAL_EXPRESSION', 13);
  27. define('GLOBAL_MULTIPLE_ANSWER', 14);
  28. define('MEDIA_QUESTION', 15);
  29. define('UNIQUE_ANSWER_IMAGE', 16);
  30. define('DRAGGABLE', 17);
  31. //Some alias used in the QTI exports
  32. define('MCUA', 1);
  33. define('TF', 1);
  34. define('MCMA', 2);
  35. define('FIB', 3);
  36. /**
  37. * QUESTION CLASS
  38. *
  39. * This class allows to instantiate an object of type Question
  40. *
  41. * @author Olivier Brouckaert, original author
  42. * @author Patrick Cool, LaTeX support
  43. * @package chamilo.exercise
  44. */
  45. abstract class Question
  46. {
  47. public $id;
  48. public $question;
  49. public $description;
  50. public $weighting;
  51. public $position;
  52. public $type;
  53. public $level;
  54. public $picture;
  55. public $exerciseList; // array with the list of exercises which this question is in
  56. public $category_list;
  57. public $parent_id;
  58. public $isContent;
  59. public $course;
  60. static $typePicture = 'new_question.png';
  61. static $explanationLangVar = '';
  62. public $question_table_class = 'table table-striped';
  63. public $editionMode = 'normal';
  64. static $questionTypes = array(
  65. UNIQUE_ANSWER => array('unique_answer.class.php', 'UniqueAnswer'),
  66. MULTIPLE_ANSWER => array('multiple_answer.class.php', 'MultipleAnswer'),
  67. FILL_IN_BLANKS => array('fill_blanks.class.php', 'FillBlanks'),
  68. MATCHING => array('matching.class.php', 'Matching'),
  69. FREE_ANSWER => array('freeanswer.class.php', 'FreeAnswer'),
  70. ORAL_EXPRESSION => array('oral_expression.class.php', 'OralExpression'),
  71. HOT_SPOT => array('hotspot.class.php', 'HotSpot'),
  72. HOT_SPOT_DELINEATION => array('hotspot.class.php', 'HotspotDelineation'),
  73. MULTIPLE_ANSWER_COMBINATION => array('multiple_answer_combination.class.php', 'MultipleAnswerCombination' ),
  74. UNIQUE_ANSWER_NO_OPTION => array('unique_answer_no_option.class.php', 'UniqueAnswerNoOption'),
  75. MULTIPLE_ANSWER_TRUE_FALSE => array('multiple_answer_true_false.class.php', 'MultipleAnswerTrueFalse'),
  76. MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE => array('multiple_answer_combination_true_false.class.php', 'MultipleAnswerCombinationTrueFalse'),
  77. GLOBAL_MULTIPLE_ANSWER => array('global_multiple_answer.class.php', 'GlobalMultipleAnswer'),
  78. MEDIA_QUESTION => array('media_question.class.php', 'MediaQuestion'),
  79. UNIQUE_ANSWER_IMAGE => array('unique_answer_image.class.php', 'UniqueAnswerImage'),
  80. DRAGGABLE => array('draggable.class.php', 'Draggable')
  81. );
  82. /**
  83. * constructor of the class
  84. *
  85. * @author - Olivier Brouckaert
  86. */
  87. public function Question()
  88. {
  89. $this->id = 0;
  90. $this->question = '';
  91. $this->description = '';
  92. $this->weighting = 0;
  93. $this->position = 1;
  94. $this->picture = '';
  95. $this->level = 1;
  96. $this->extra = ''; // This variable is used when loading an exercise like an scenario with an special hotspot: final_overlap, final_missing, final_excess
  97. $this->exerciseList = array();
  98. $this->course = api_get_course_info();
  99. $this->category_list = array();
  100. $this->parent_id = 0;
  101. $this->editionMode = 'normal';
  102. }
  103. public function getIsContent()
  104. {
  105. $isContent = null;
  106. if (isset($_REQUEST['isContent'])) {
  107. $isContent = intval($_REQUEST['isContent']);
  108. }
  109. return $this->isContent = $isContent;
  110. }
  111. /**
  112. * @param string $title
  113. * @param int $course_id
  114. * @return mixed|bool
  115. */
  116. public function readByTitle($title, $course_id = null)
  117. {
  118. if (!empty($course_id)) {
  119. $course_info = api_get_course_info_by_id($course_id);
  120. } else {
  121. $course_info = api_get_course_info();
  122. }
  123. $course_id = $course_info['real_id'];
  124. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  125. $title = Database::escape_string($title);
  126. $sql = "SELECT iid FROM $TBL_QUESTIONS WHERE question = '$title' AND c_id = $course_id ";
  127. $result = Database::query($sql);
  128. if (Database::num_rows($result)) {
  129. $row = Database::fetch_array($result);
  130. return self::read($row['iid'], $course_id);
  131. }
  132. return false;
  133. }
  134. /**
  135. * Reads question informations from the data base
  136. *
  137. * @author - Olivier Brouckaert
  138. * @param - integer $id - question ID
  139. * @return - boolean - true if question exists, otherwise false
  140. */
  141. static function read($id, $course_id = null)
  142. {
  143. $id = intval($id);
  144. if (!empty($course_id)) {
  145. $course_info = api_get_course_info_by_id($course_id);
  146. } else {
  147. $course_info = api_get_course_info();
  148. }
  149. $course_id = $course_info['real_id'];
  150. if (empty($course_id) || $course_id == -1) {
  151. return false;
  152. }
  153. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  154. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  155. $sql = "SELECT * FROM $TBL_QUESTIONS WHERE iid = $id AND c_id = $course_id";
  156. $result = Database::query($sql);
  157. // if the question has been found
  158. if ($object = Database::fetch_object($result)) {
  159. $objQuestion = Question::getInstance($object->type);
  160. if (!empty($objQuestion)) {
  161. $objQuestion->id = $id;
  162. $objQuestion->question = $object->question;
  163. $objQuestion->description = $object->description;
  164. $objQuestion->weighting = $object->ponderation;
  165. $objQuestion->position = $object->position;
  166. $objQuestion->type = $object->type;
  167. $objQuestion->picture = $object->picture;
  168. $objQuestion->level = (int)$object->level;
  169. $objQuestion->extra = $object->extra;
  170. $objQuestion->course = $course_info;
  171. $objQuestion->parent_id = $object->parent_id;
  172. $objQuestion->category_list = Testcategory::getCategoryForQuestion($id);
  173. $sql = "SELECT exercice_id FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND question_id = $id";
  174. $result_exercise_list = Database::query($sql);
  175. // fills the array with the exercises which this question is in
  176. if ($result_exercise_list) {
  177. while ($obj = Database::fetch_object($result_exercise_list)) {
  178. $objQuestion->exerciseList[] = $obj->exercice_id;
  179. }
  180. }
  181. return $objQuestion;
  182. }
  183. }
  184. // question not found
  185. return false;
  186. }
  187. public function setEditionMode($mode)
  188. {
  189. $this->editionMode = $mode;
  190. }
  191. public function getEditionMode()
  192. {
  193. return $this->editionMode;
  194. }
  195. /**
  196. * Returns the question ID
  197. *
  198. * @author - Olivier Brouckaert
  199. * @return int - question ID
  200. */
  201. function selectId()
  202. {
  203. return $this->id;
  204. }
  205. /**
  206. * Returns the question title
  207. *
  208. * @author - Olivier Brouckaert
  209. * @return string - question title
  210. */
  211. function selectTitle()
  212. {
  213. return $this->question;
  214. }
  215. /**
  216. * returns the question description
  217. *
  218. * @author - Olivier Brouckaert
  219. * @return string - question description
  220. */
  221. function selectDescription()
  222. {
  223. $this->description = $this->description;
  224. return $this->description;
  225. }
  226. /**
  227. * returns the question weighting
  228. *
  229. * @author - Olivier Brouckaert
  230. * @return int - question weighting
  231. */
  232. function selectWeighting()
  233. {
  234. return $this->weighting;
  235. }
  236. /**
  237. * returns the question position
  238. *
  239. * @author - Olivier Brouckaert
  240. * @return int - question position
  241. */
  242. function selectPosition()
  243. {
  244. return $this->position;
  245. }
  246. /**
  247. * returns the answer type
  248. *
  249. * @author - Olivier Brouckaert
  250. * @return int - answer type
  251. */
  252. function selectType()
  253. {
  254. return $this->type;
  255. }
  256. /**
  257. * returns the level of the question
  258. *
  259. * @author - Nicolas Raynaud
  260. * @return int - level of the question, 0 by default.
  261. */
  262. function selectLevel()
  263. {
  264. return $this->level;
  265. }
  266. /**
  267. * returns the picture name
  268. *
  269. * @author - Olivier Brouckaert
  270. * @return string - picture name
  271. */
  272. function selectPicture()
  273. {
  274. return $this->picture;
  275. }
  276. function selectPicturePath()
  277. {
  278. if (!empty($this->picture)) {
  279. return api_get_path(WEB_COURSE_PATH).$this->course['path'].'/document/images/'.$this->picture;
  280. }
  281. return false;
  282. }
  283. /**
  284. * returns the array with the exercise ID list
  285. *
  286. * @author - Olivier Brouckaert
  287. * @return array - list of exercise ID which the question is in
  288. */
  289. function selectExerciseList()
  290. {
  291. return $this->exerciseList;
  292. }
  293. /**
  294. * returns the number of exercises which this question is in
  295. *
  296. * @author - Olivier Brouckaert
  297. * @return integer - number of exercises
  298. */
  299. function selectNbrExercises()
  300. {
  301. return sizeof($this->exerciseList);
  302. }
  303. /**
  304. * @param $course_id
  305. */
  306. function updateCourse($course_id)
  307. {
  308. $this->course = api_get_course_info_by_id($course_id);
  309. }
  310. /**
  311. * changes the question title
  312. *
  313. * @author - Olivier Brouckaert
  314. * @param - string $title - question title
  315. */
  316. function updateTitle($title)
  317. {
  318. $this->question = $title;
  319. }
  320. function updateParentId($id)
  321. {
  322. $this->parent_id = intval($id);
  323. }
  324. /**
  325. * changes the question description
  326. *
  327. * @author - Olivier Brouckaert
  328. * @param - string $description - question description
  329. */
  330. function updateDescription($description)
  331. {
  332. $this->description = $description;
  333. }
  334. /**
  335. * changes the question weighting
  336. *
  337. * @author - Olivier Brouckaert
  338. * @param - integer $weighting - question weighting
  339. */
  340. function updateWeighting($weighting)
  341. {
  342. $this->weighting = $weighting;
  343. }
  344. /**
  345. * @author - Hubert Borderiou 12-10-2011
  346. * @param - array of category $in_category
  347. */
  348. function updateCategory($category_list)
  349. {
  350. $this->category_list = $category_list;
  351. }
  352. /**
  353. * @author - Hubert Borderiou 12-10-2011
  354. * @param - interger $in_positive
  355. */
  356. function updateScoreAlwaysPositive($in_positive)
  357. {
  358. $this->scoreAlwaysPositive = $in_positive;
  359. }
  360. /**
  361. * @author - Hubert Borderiou 12-10-2011
  362. * @param - interger $in_positive
  363. */
  364. function updateUncheckedMayScore($in_positive)
  365. {
  366. $this->uncheckedMayScore = $in_positive;
  367. }
  368. /**
  369. * Save category of a question
  370. *
  371. * A question can have n categories
  372. * if category is empty, then question has no category then delete the category entry
  373. *
  374. * @param - int $in_positive
  375. * @author - Julio Montoya - Adding multiple cat support
  376. */
  377. function saveCategories($category_list)
  378. {
  379. $course_id = $this->course['real_id'];
  380. if (!empty($category_list)) {
  381. $this->deleteCategory();
  382. $TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  383. $category_list = array_filter($category_list);
  384. // update or add category for a question
  385. foreach ($category_list as $category_id) {
  386. if (empty($category_id)) {
  387. continue;
  388. }
  389. $category_id = intval($category_id);
  390. $question_id = Database::escape_string($this->id);
  391. $sql = "SELECT count(*) AS nb FROM $TBL_QUESTION_REL_CATEGORY WHERE category_id = $category_id AND question_id = $question_id AND c_id=".$course_id;
  392. $res = Database::query($sql);
  393. $row = Database::fetch_array($res);
  394. if ($row['nb'] > 0) {
  395. //DO nothing
  396. //$sql = "UPDATE $TBL_QUESTION_REL_CATEGORY SET category_id = $category_id WHERE question_id=$question_id AND c_id=".api_get_course_int_id();
  397. //$res = Database::query($sql);
  398. } else {
  399. $sql = "INSERT INTO $TBL_QUESTION_REL_CATEGORY (c_id, question_id, category_id) VALUES (".$course_id.", $question_id, $category_id)";
  400. $res = Database::query($sql);
  401. }
  402. }
  403. }
  404. }
  405. /**
  406. * @author - Hubert Borderiou 12-10-2011
  407. * @param - interger $in_positive
  408. * in this version, a question can only have 1 category
  409. * if category is 0, then question has no category then delete the category entry
  410. */
  411. function saveCategory($in_category)
  412. {
  413. if ($in_category <= 0) {
  414. $this->deleteCategory();
  415. } else {
  416. // update or add category for a question
  417. $TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  418. $category_id = Database::escape_string($in_category);
  419. $question_id = Database::escape_string($this->id);
  420. $sql = "SELECT count(*) AS nb FROM $TBL_QUESTION_REL_CATEGORY WHERE question_id= $question_id AND c_id=".api_get_course_int_id(
  421. );
  422. $res = Database::query($sql);
  423. $row = Database::fetch_array($res);
  424. if ($row['nb'] > 0) {
  425. $sql = "UPDATE $TBL_QUESTION_REL_CATEGORY SET category_id= $category_id WHERE question_id=$question_id AND c_id=".api_get_course_int_id(
  426. );
  427. $res = Database::query($sql);
  428. } else {
  429. $sql = "INSERT INTO $TBL_QUESTION_REL_CATEGORY VALUES (".api_get_course_int_id(
  430. ).", $question_id, $category_id)";
  431. $res = Database::query($sql);
  432. }
  433. }
  434. }
  435. /**
  436. * @author hubert borderiou 12-10-2011
  437. * delete any category entry for question id
  438. * @param : none
  439. * delte the category for question
  440. */
  441. function deleteCategory()
  442. {
  443. $course_id = $this->course['real_id'];
  444. $TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  445. $question_id = Database::escape_string($this->id);
  446. $sql = "DELETE FROM $TBL_QUESTION_REL_CATEGORY WHERE question_id = $question_id AND c_id = ".$course_id;
  447. Database::query($sql);
  448. }
  449. /**
  450. * changes the question position
  451. *
  452. * @author - Olivier Brouckaert
  453. * @param - integer $position - question position
  454. */
  455. function updatePosition($position)
  456. {
  457. $this->position = $position;
  458. }
  459. /**
  460. * changes the question level
  461. *
  462. * @author - Nicolas Raynaud
  463. * @param - integer $level - question level
  464. */
  465. function updateLevel($level)
  466. {
  467. $this->level = $level;
  468. }
  469. /**
  470. * changes the answer type. If the user changes the type from "unique answer" to "multiple answers"
  471. * (or conversely) answers are not deleted, otherwise yes
  472. *
  473. * @author - Olivier Brouckaert
  474. * @param - integer $type - answer type
  475. */
  476. function updateType($type)
  477. {
  478. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  479. $course_id = $this->course['real_id'];
  480. if (empty($course_id)) {
  481. $course_id = api_get_course_int_id();
  482. }
  483. // if we really change the type
  484. if ($type != $this->type) {
  485. // if we don't change from "unique answer" to "multiple answers" (or conversely)
  486. if (!in_array($this->type, array(UNIQUE_ANSWER, MULTIPLE_ANSWER)) || !in_array(
  487. $type,
  488. array(UNIQUE_ANSWER, MULTIPLE_ANSWER)
  489. )
  490. ) {
  491. // removes old answers
  492. $sql = "DELETE FROM $TBL_REPONSES WHERE c_id = $course_id AND question_id='".Database::escape_string(
  493. $this->id
  494. )."'";
  495. Database::query($sql);
  496. }
  497. $this->type = $type;
  498. }
  499. }
  500. /**
  501. * adds a picture to the question
  502. *
  503. * @author - Olivier Brouckaert
  504. * @param - string $Picture - temporary path of the picture to upload
  505. * @param - string $PictureName - Name of the picture
  506. * @return - boolean - true if uploaded, otherwise false
  507. */
  508. function uploadPicture($Picture, $PictureName, $picturePath = null)
  509. {
  510. if (empty($picturePath)) {
  511. global $picturePath;
  512. }
  513. if (!file_exists($picturePath)) {
  514. if (mkdir($picturePath, api_get_permissions_for_new_directories())) {
  515. // document path
  516. $documentPath = api_get_path(SYS_COURSE_PATH).$this->course['path']."/document";
  517. $path = str_replace($documentPath, '', $picturePath);
  518. $title_path = basename($picturePath);
  519. $doc_id = FileManager::add_document($this->course, $path, 'folder', 0, $title_path);
  520. api_item_property_update($this->course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', api_get_user_id());
  521. }
  522. }
  523. // if the question has got an ID
  524. if ($this->id) {
  525. $extension = pathinfo($PictureName, PATHINFO_EXTENSION);
  526. $this->picture = 'quiz-'.$this->id.'.jpg';
  527. $o_img = new Image($Picture);
  528. $o_img->send_image($picturePath.'/'.$this->picture, -1, 'jpg');
  529. $document_id = FileManager::add_document(
  530. $this->course,
  531. '/images/'.$this->picture,
  532. 'file',
  533. filesize($picturePath.'/'.$this->picture),
  534. $this->picture
  535. );
  536. if ($document_id) {
  537. return api_item_property_update(
  538. $this->course,
  539. TOOL_DOCUMENT,
  540. $document_id,
  541. 'DocumentAdded',
  542. api_get_user_id()
  543. );
  544. }
  545. }
  546. return false;
  547. }
  548. /**
  549. * Resizes a picture || Warning!: can only be called after uploadPicture, or if picture is already available in object.
  550. *
  551. * @author - Toon Keppens
  552. * @param - string $Dimension - Resizing happens proportional according to given dimension: height|width|any
  553. * @param - integer $Max - Maximum size
  554. * @return - boolean - true if success, false if failed
  555. */
  556. function resizePicture($Dimension, $Max)
  557. {
  558. global $picturePath;
  559. // if the question has an ID
  560. if ($this->id) {
  561. // Get dimensions from current image.
  562. $my_image = new Image($picturePath.'/'.$this->picture);
  563. $current_image_size = $my_image->get_image_size();
  564. $current_width = $current_image_size['width'];
  565. $current_height = $current_image_size['height'];
  566. if ($current_width < $Max && $current_height < $Max) {
  567. return true;
  568. } elseif ($current_height == "") {
  569. return false;
  570. }
  571. // Resize according to height.
  572. if ($Dimension == "height") {
  573. $resize_scale = $current_height / $Max;
  574. $new_height = $Max;
  575. $new_width = ceil($current_width / $resize_scale);
  576. }
  577. // Resize according to width
  578. if ($Dimension == "width") {
  579. $resize_scale = $current_width / $Max;
  580. $new_width = $Max;
  581. $new_height = ceil($current_height / $resize_scale);
  582. }
  583. // Resize according to height or width, both should not be larger than $Max after resizing.
  584. if ($Dimension == "any") {
  585. if ($current_height > $current_width || $current_height == $current_width) {
  586. $resize_scale = $current_height / $Max;
  587. $new_height = $Max;
  588. $new_width = ceil($current_width / $resize_scale);
  589. }
  590. if ($current_height < $current_width) {
  591. $resize_scale = $current_width / $Max;
  592. $new_width = $Max;
  593. $new_height = ceil($current_height / $resize_scale);
  594. }
  595. }
  596. $my_image->resize($new_width, $new_height);
  597. $result = $my_image->send_image($picturePath.'/'.$this->picture);
  598. if ($result) {
  599. return true;
  600. } else {
  601. return false;
  602. }
  603. }
  604. }
  605. /**
  606. * deletes the picture
  607. *
  608. * @author - Olivier Brouckaert
  609. * @return - boolean - true if removed, otherwise false
  610. */
  611. function removePicture()
  612. {
  613. global $picturePath;
  614. // if the question has got an ID and if the picture exists
  615. if ($this->id) {
  616. $picture = $this->picture;
  617. $this->picture = '';
  618. return @unlink($picturePath.'/'.$picture) ? true : false;
  619. }
  620. return false;
  621. }
  622. /**
  623. * Exports a picture to another question
  624. *
  625. * @author - Olivier Brouckaert
  626. * @param - integer $questionId - ID of the target question
  627. * @return - boolean - true if copied, otherwise false
  628. */
  629. function exportPicture($questionId, $course_info)
  630. {
  631. $course_id = $course_info['real_id'];
  632. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  633. $destination_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document/images';
  634. $source_path = api_get_path(SYS_COURSE_PATH).$this->course['path'].'/document/images';
  635. // if the question has got an ID and if the picture exists
  636. if ($this->id && !empty($this->picture)) {
  637. $picture = explode('.', $this->picture);
  638. $extension = $picture[sizeof($picture) - 1];
  639. $picture = 'quiz-'.$questionId.'.'.$extension;
  640. $result = @copy($source_path.'/'.$this->picture, $destination_path.'/'.$picture) ? true : false;
  641. //If copy was correct then add to the database
  642. if ($result) {
  643. $sql = "UPDATE $TBL_QUESTIONS SET picture='".Database::escape_string(
  644. $picture
  645. )."' WHERE c_id = $course_id AND id='".intval($questionId)."'";
  646. Database::query($sql);
  647. $document_id = FileManager::add_document(
  648. $course_info,
  649. '/images/'.$picture,
  650. 'file',
  651. filesize($destination_path.'/'.$picture),
  652. $picture
  653. );
  654. if ($document_id) {
  655. return api_item_property_update(
  656. $course_info,
  657. TOOL_DOCUMENT,
  658. $document_id,
  659. 'DocumentAdded',
  660. api_get_user_id()
  661. );
  662. }
  663. }
  664. return $result;
  665. }
  666. return false;
  667. }
  668. /**
  669. * Saves the picture coming from POST into a temporary file
  670. * Temporary pictures are used when we don't want to save a picture right after a form submission.
  671. * For example, if we first show a confirmation box.
  672. *
  673. * @author - Olivier Brouckaert
  674. * @param - string $Picture - temporary path of the picture to move
  675. * @param - string $PictureName - Name of the picture
  676. */
  677. function setTmpPicture($Picture, $PictureName)
  678. {
  679. global $picturePath;
  680. $PictureName = explode('.', $PictureName);
  681. $Extension = $PictureName[sizeof($PictureName) - 1];
  682. // saves the picture into a temporary file
  683. @move_uploaded_file($Picture, $picturePath.'/tmp.'.$Extension);
  684. }
  685. /**
  686. Sets the title
  687. */
  688. public function setTitle($title)
  689. {
  690. $this->question = $title;
  691. }
  692. /**
  693. Sets the title
  694. */
  695. public function setExtra($extra)
  696. {
  697. $this->extra = $extra;
  698. }
  699. /**
  700. * Moves the temporary question "tmp" to "quiz-$questionId"
  701. * Temporary pictures are used when we don't want to save a picture right after a form submission.
  702. * For example, if we first show a confirmation box.
  703. *
  704. * @author - Olivier Brouckaert
  705. * @return - boolean - true if moved, otherwise false
  706. */
  707. function getTmpPicture()
  708. {
  709. global $picturePath;
  710. // if the question has got an ID and if the picture exists
  711. if ($this->id) {
  712. if (file_exists($picturePath.'/tmp.jpg')) {
  713. $Extension = 'jpg';
  714. } elseif (file_exists($picturePath.'/tmp.gif')) {
  715. $Extension = 'gif';
  716. } elseif (file_exists($picturePath.'/tmp.png')) {
  717. $Extension = 'png';
  718. }
  719. $this->picture = 'quiz-'.$this->id.'.'.$Extension;
  720. return @rename($picturePath.'/tmp.'.$Extension, $picturePath.'/'.$this->picture) ? true : false;
  721. }
  722. return false;
  723. }
  724. /**
  725. * updates the question in the data base
  726. * if an exercise ID is provided, we add that exercise ID into the exercise list
  727. *
  728. * @author - Olivier Brouckaert
  729. * @param - integer $exerciseId - exercise ID if saving in an exercise
  730. */
  731. function save($exerciseId = 0)
  732. {
  733. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  734. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  735. $id = $this->id;
  736. $question = $this->question;
  737. $description = $this->description;
  738. $weighting = $this->weighting;
  739. $position = $this->position;
  740. $type = $this->type;
  741. $picture = $this->picture;
  742. $level = $this->level;
  743. $extra = $this->extra;
  744. $c_id = $this->course['real_id'];
  745. $category_list = $this->category_list;
  746. // Question already exists
  747. if (!empty($id)) {
  748. $sql = "UPDATE $TBL_QUESTIONS SET
  749. question ='".Database::escape_string($question)."',
  750. description ='".Database::escape_string($description)."',
  751. ponderation ='".Database::escape_string($weighting)."',
  752. position ='".Database::escape_string($position)."',
  753. type ='".Database::escape_string($type)."',
  754. picture ='".Database::escape_string($picture)."',
  755. extra ='".Database::escape_string($extra)."',
  756. level ='".Database::escape_string($level)."',
  757. parent_id = ".$this->parent_id."
  758. WHERE c_id = $c_id AND iid = '".Database::escape_string($id)."'";
  759. Database::query($sql);
  760. $this->saveCategories($category_list);
  761. if (!empty($exerciseId)) {
  762. api_item_property_update($this->course, TOOL_QUIZ, $id, 'QuizQuestionUpdated', api_get_user_id());
  763. }
  764. if (api_get_setting('search_enabled') == 'true') {
  765. if ($exerciseId != 0) {
  766. $this->search_engine_edit($exerciseId);
  767. }
  768. }
  769. } else {
  770. // Creates a new question
  771. $sql = "SELECT max(position) FROM $TBL_QUESTIONS as question, $TBL_EXERCICE_QUESTION as test_question
  772. WHERE question.id = test_question.question_id AND
  773. test_question.exercice_id = '".Database::escape_string($exerciseId)."' AND
  774. question.c_id = $c_id AND
  775. test_question.c_id = $c_id ";
  776. $result = Database::query($sql);
  777. $current_position = Database::result($result, 0, 0);
  778. $this->updatePosition($current_position + 1);
  779. $position = $this->position;
  780. $sql = "INSERT INTO $TBL_QUESTIONS (c_id, question, description, ponderation, position, type, picture, extra, level, parent_id) VALUES ( ".
  781. " $c_id, ".
  782. " '".Database::escape_string($question)."', ".
  783. " '".Database::escape_string($description)."', ".
  784. " '".Database::escape_string($weighting)."', ".
  785. " '".Database::escape_string($position)."', ".
  786. " '".Database::escape_string($type)."', ".
  787. " '".Database::escape_string($picture)."', ".
  788. " '".Database::escape_string($extra)."', ".
  789. " '".Database::escape_string($level)."', ".
  790. " '".$this->parent_id."' ".
  791. " )";
  792. Database::query($sql);
  793. $this->id = Database::insert_id();
  794. api_item_property_update($this->course, TOOL_QUIZ, $this->id, 'QuizQuestionAdded', api_get_user_id());
  795. // If hotspot, create first answer
  796. if ($type == HOT_SPOT || $type == HOT_SPOT_ORDER) {
  797. $TBL_ANSWERS = Database::get_course_table(TABLE_QUIZ_ANSWER);
  798. $sql = "INSERT INTO $TBL_ANSWERS (c_id, question_id , answer , correct , comment , ponderation , position , hotspot_coordinates , hotspot_type )
  799. VALUES (".$c_id.", '".Database::escape_string(
  800. $this->id
  801. )."', '', NULL , '', '10' , '1', '0;0|0|0', 'square')";
  802. Database::query($sql);
  803. }
  804. if ($type == HOT_SPOT_DELINEATION) {
  805. $TBL_ANSWERS = Database::get_course_table(TABLE_QUIZ_ANSWER);
  806. $sql = "INSERT INTO $TBL_ANSWERS (c_id, question_id , answer , correct , comment , ponderation , position , hotspot_coordinates , hotspot_type )
  807. VALUES (".$c_id.", '".Database::escape_string(
  808. $this->id
  809. )."', '', NULL , '', '10' , '1', '0;0|0|0', 'delineation')";
  810. Database::query($sql);
  811. }
  812. if (api_get_setting('search_enabled') == 'true') {
  813. if ($exerciseId != 0) {
  814. $this->search_engine_edit($exerciseId, true);
  815. } else {
  816. /**
  817. * actually there is *not* an user interface for
  818. * creating questions without a relation with an exercise
  819. */
  820. }
  821. }
  822. }
  823. // if the question is created in an exercise
  824. if ($exerciseId) {
  825. // adds the exercise into the exercise list of this question
  826. $this->addToList($exerciseId, true);
  827. }
  828. }
  829. function search_engine_edit($exerciseId, $addQs = false, $rmQs = false)
  830. {
  831. // update search engine and its values table if enabled
  832. if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) {
  833. $course_id = api_get_course_id();
  834. // get search_did
  835. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  836. if ($addQs || $rmQs) {
  837. //there's only one row per question on normal db and one document per question on search engine db
  838. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_second_level=%s LIMIT 1';
  839. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  840. } else {
  841. $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';
  842. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id);
  843. }
  844. $res = Database::query($sql);
  845. if (Database::num_rows($res) > 0 || $addQs) {
  846. require_once(api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php');
  847. require_once(api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php');
  848. $di = new ChamiloIndexer();
  849. if ($addQs) {
  850. $question_exercises = array((int)$exerciseId);
  851. } else {
  852. $question_exercises = array();
  853. }
  854. isset($_POST['language']) ? $lang = Database::escape_string($_POST['language']) : $lang = 'english';
  855. $di->connectDb(null, null, $lang);
  856. // retrieve others exercise ids
  857. $se_ref = Database::fetch_array($res);
  858. $se_doc = $di->get_document((int)$se_ref['search_did']);
  859. if ($se_doc !== false) {
  860. if (($se_doc_data = $di->get_document_data($se_doc)) !== false) {
  861. $se_doc_data = unserialize($se_doc_data);
  862. if (isset($se_doc_data[SE_DATA]['type']) && $se_doc_data[SE_DATA]['type'] == SE_DOCTYPE_EXERCISE_QUESTION) {
  863. if (isset($se_doc_data[SE_DATA]['exercise_ids']) && is_array(
  864. $se_doc_data[SE_DATA]['exercise_ids']
  865. )
  866. ) {
  867. foreach ($se_doc_data[SE_DATA]['exercise_ids'] as $old_value) {
  868. if (!in_array($old_value, $question_exercises)) {
  869. $question_exercises[] = $old_value;
  870. }
  871. }
  872. }
  873. }
  874. }
  875. }
  876. if ($rmQs) {
  877. while (($key = array_search($exerciseId, $question_exercises)) !== false) {
  878. unset($question_exercises[$key]);
  879. }
  880. }
  881. // build the chunk to index
  882. $ic_slide = new IndexableChunk();
  883. $ic_slide->addValue("title", $this->question);
  884. $ic_slide->addCourseId($course_id);
  885. $ic_slide->addToolId(TOOL_QUIZ);
  886. $xapian_data = array(
  887. SE_COURSE_ID => $course_id,
  888. SE_TOOL_ID => TOOL_QUIZ,
  889. SE_DATA => array(
  890. 'type' => SE_DOCTYPE_EXERCISE_QUESTION,
  891. 'exercise_ids' => $question_exercises,
  892. 'question_id' => (int)$this->id
  893. ),
  894. SE_USER => (int)api_get_user_id(),
  895. );
  896. $ic_slide->xapian_data = serialize($xapian_data);
  897. $ic_slide->addValue("content", $this->description);
  898. //TODO: index answers, see also form validation on question_admin.inc.php
  899. $di->remove_document((int)$se_ref['search_did']);
  900. $di->addChunk($ic_slide);
  901. //index and return search engine document id
  902. if (!empty($question_exercises)) { // if empty there is nothing to index
  903. $did = $di->index();
  904. unset($di);
  905. }
  906. if ($did || $rmQs) {
  907. // save it to db
  908. if ($addQs || $rmQs) {
  909. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_second_level=\'%s\'';
  910. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  911. } else {
  912. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\' AND ref_id_second_level=\'%s\'';
  913. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id);
  914. }
  915. Database::query($sql);
  916. if ($rmQs) {
  917. if (!empty($question_exercises)) {
  918. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
  919. VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
  920. $sql = sprintf(
  921. $sql,
  922. $tbl_se_ref,
  923. $course_id,
  924. TOOL_QUIZ,
  925. array_shift($question_exercises),
  926. $this->id,
  927. $did
  928. );
  929. Database::query($sql);
  930. }
  931. } else {
  932. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
  933. VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
  934. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id, $did);
  935. Database::query($sql);
  936. }
  937. }
  938. }
  939. }
  940. }
  941. /**
  942. * adds an exercise into the exercise list
  943. *
  944. * @author - Olivier Brouckaert
  945. * @param - integer $exerciseId - exercise ID
  946. * @param - boolean $fromSave - comming from $this->save() or not
  947. */
  948. function addToList($exerciseId, $fromSave = false)
  949. {
  950. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  951. $id = $this->id;
  952. // checks if the exercise ID is not in the list
  953. if (!in_array($exerciseId, $this->exerciseList)) {
  954. $this->exerciseList[] = $exerciseId;
  955. $new_exercise = new Exercise();
  956. $new_exercise->read($exerciseId);
  957. $count = $new_exercise->selectNbrQuestions();
  958. $count++;
  959. $sql = "INSERT INTO $TBL_EXERCICE_QUESTION (c_id, question_id, exercice_id, question_order) VALUES
  960. ({$this->course['real_id']}, '".Database::escape_string($id)."','".Database::escape_string(
  961. $exerciseId
  962. )."', '$count' )";
  963. Database::query($sql);
  964. // we do not want to reindex if we had just saved adnd indexed the question
  965. if (!$fromSave) {
  966. $this->search_engine_edit($exerciseId, true);
  967. }
  968. }
  969. }
  970. /**
  971. * removes an exercise from the exercise list
  972. *
  973. * @author - Olivier Brouckaert
  974. * @param - integer $exerciseId - exercise ID
  975. * @return - boolean - true if removed, otherwise false
  976. */
  977. function removeFromList($exerciseId)
  978. {
  979. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  980. $id = $this->id;
  981. // searches the position of the exercise ID in the list
  982. $pos = array_search($exerciseId, $this->exerciseList);
  983. $course_id = api_get_course_int_id();
  984. // exercise not found
  985. if ($pos === false) {
  986. return false;
  987. } else {
  988. // deletes the position in the array containing the wanted exercise ID
  989. unset($this->exerciseList[$pos]);
  990. //update order of other elements
  991. $sql = "SELECT question_order FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND question_id='".Database::escape_string(
  992. $id
  993. )."' AND exercice_id='".Database::escape_string($exerciseId)."'";
  994. $res = Database::query($sql);
  995. if (Database::num_rows($res) > 0) {
  996. $row = Database::fetch_array($res);
  997. if (!empty($row['question_order'])) {
  998. $sql = "UPDATE $TBL_EXERCICE_QUESTION SET question_order = question_order-1
  999. WHERE c_id = $course_id AND exercice_id='".Database::escape_string(
  1000. $exerciseId
  1001. )."' AND question_order > ".$row['question_order'];
  1002. $res = Database::query($sql);
  1003. }
  1004. }
  1005. $sql = "DELETE FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND question_id='".Database::escape_string(
  1006. $id
  1007. )."' AND exercice_id='".Database::escape_string($exerciseId)."'";
  1008. Database::query($sql);
  1009. return true;
  1010. }
  1011. }
  1012. /**
  1013. * Deletes a question from the database
  1014. * the parameter tells if the question is removed from all exercises (value = 0),
  1015. * or just from one exercise (value = exercise ID)
  1016. *
  1017. * @author - Olivier Brouckaert
  1018. * @param - integer $deleteFromEx - exercise ID if the question is only removed from one exercise
  1019. */
  1020. function delete($deleteFromEx = 0)
  1021. {
  1022. $course_id = api_get_course_int_id();
  1023. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1024. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1025. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  1026. $TBL_QUIZ_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  1027. $id = $this->id;
  1028. // if the question must be removed from all exercises
  1029. if (!$deleteFromEx) {
  1030. //update the question_order of each question to avoid inconsistencies
  1031. $sql = "SELECT exercice_id, question_order FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND question_id='".Database::escape_string(
  1032. $id
  1033. )."'";
  1034. $res = Database::query($sql);
  1035. if (Database::num_rows($res) > 0) {
  1036. while ($row = Database::fetch_array($res)) {
  1037. if (!empty($row['question_order'])) {
  1038. $sql = "UPDATE $TBL_EXERCICE_QUESTION
  1039. SET question_order = question_order-1
  1040. WHERE c_id = $course_id AND exercice_id='".Database::escape_string(
  1041. $row['exercice_id']
  1042. )."' AND question_order > ".$row['question_order'];
  1043. Database::query($sql);
  1044. }
  1045. }
  1046. }
  1047. $sql = "DELETE FROM $TBL_EXERCICE_QUESTION WHERE c_id = $course_id AND question_id='".Database::escape_string(
  1048. $id
  1049. )."'";
  1050. Database::query($sql);
  1051. $sql = "DELETE FROM $TBL_QUESTIONS WHERE c_id = $course_id AND id='".Database::escape_string($id)."'";
  1052. Database::query($sql);
  1053. $sql = "DELETE FROM $TBL_REPONSES WHERE c_id = $course_id AND question_id='".Database::escape_string(
  1054. $id
  1055. )."'";
  1056. Database::query($sql);
  1057. // remove the category of this question in the question_rel_category table
  1058. $sql = "DELETE FROM $TBL_QUIZ_QUESTION_REL_CATEGORY WHERE c_id = $course_id AND question_id='".Database::escape_string(
  1059. $id
  1060. )."' AND c_id=".api_get_course_int_id();
  1061. Database::query($sql);
  1062. api_item_property_update($this->course, TOOL_QUIZ, $id, 'QuizQuestionDeleted', api_get_user_id());
  1063. $this->removePicture();
  1064. // resets the object
  1065. $this->Question();
  1066. } else {
  1067. // just removes the exercise from the list
  1068. $this->removeFromList($deleteFromEx);
  1069. if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) {
  1070. // disassociate question with this exercise
  1071. $this->search_engine_edit($deleteFromEx, false, true);
  1072. }
  1073. api_item_property_update($this->course, TOOL_QUIZ, $id, 'QuizQuestionDeleted', api_get_user_id());
  1074. }
  1075. }
  1076. /**
  1077. * Duplicates the question
  1078. *
  1079. * @author Olivier Brouckaert
  1080. * @param array Course info of the destination course
  1081. * @return int ID of the new question
  1082. */
  1083. function duplicate($course_info = null)
  1084. {
  1085. if (empty($course_info)) {
  1086. $course_info = $this->course;
  1087. } else {
  1088. $course_info = $course_info;
  1089. }
  1090. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1091. $TBL_QUESTION_OPTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1092. $question = $this->question;
  1093. $description = $this->description;
  1094. $weighting = $this->weighting;
  1095. $position = $this->position;
  1096. $type = $this->type;
  1097. $level = intval($this->level);
  1098. $extra = $this->extra;
  1099. //Using the same method used in the course copy to transform URLs
  1100. if ($this->course['id'] != $course_info['id']) {
  1101. $description = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1102. $description,
  1103. $this->course['id'],
  1104. $course_info['id']
  1105. );
  1106. $question = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1107. $question,
  1108. $this->course['id'],
  1109. $course_info['id']
  1110. );
  1111. }
  1112. $course_id = $course_info['real_id'];
  1113. //Read the source options
  1114. $options = self::readQuestionOption($this->id, $this->course['real_id']);
  1115. //Inserting in the new course db / or the same course db
  1116. $params = array(
  1117. 'c_id' => $course_id,
  1118. 'question' => $question,
  1119. 'description' => $description,
  1120. 'ponderation' => $this->weighting,
  1121. 'position' => $this->position,
  1122. 'type' => $this->type,
  1123. 'level' => $this->level,
  1124. 'extra' => $this->extra,
  1125. 'parent_id' => $this->parent_id,
  1126. );
  1127. $new_question_id = Database::insert($TBL_QUESTIONS, $params);
  1128. if (!empty($options)) {
  1129. //Saving the quiz_options
  1130. foreach ($options as $item) {
  1131. $item['question_id'] = $new_question_id;
  1132. $item['c_id'] = $course_id;
  1133. unset($item['iid']);
  1134. Database::insert($TBL_QUESTION_OPTIONS, $item);
  1135. }
  1136. }
  1137. $this->duplicate_category_question($new_question_id, $course_id);
  1138. // Duplicates the picture of the hotspot
  1139. $this->exportPicture($new_question_id, $course_info);
  1140. return $new_question_id;
  1141. }
  1142. function get_categories_from_question()
  1143. {
  1144. return Testcategory::getCategoryForQuestion($this->id);
  1145. }
  1146. function duplicate_category_question($question_id, $course_id)
  1147. {
  1148. $question = Question::read($question_id, $course_id);
  1149. $categories = $this->get_categories_from_question();
  1150. if (!empty($categories)) {
  1151. $question->saveCategories($categories);
  1152. }
  1153. }
  1154. function get_question_type_name()
  1155. {
  1156. $key = self::$questionTypes[$this->type];
  1157. return get_lang($key[1]);
  1158. }
  1159. static function get_question_type($type)
  1160. {
  1161. if ($type == ORAL_EXPRESSION && api_get_setting('enable_nanogong') != 'true') {
  1162. return null;
  1163. }
  1164. return self::$questionTypes[$type];
  1165. }
  1166. static function get_question_type_list()
  1167. {
  1168. if (api_get_setting('enable_nanogong') != 'true') {
  1169. self::$questionTypes[ORAL_EXPRESSION] = null;
  1170. unset(self::$questionTypes[ORAL_EXPRESSION]);
  1171. }
  1172. return self::$questionTypes;
  1173. }
  1174. /**
  1175. * Returns an instance of the class corresponding to the type
  1176. * @param integer $type the type of the question
  1177. * @return an instance of a Question subclass (or of Questionc class by default)
  1178. */
  1179. static function getInstance($type)
  1180. {
  1181. if (!is_null($type)) {
  1182. list($file_name, $class_name) = self::get_question_type($type);
  1183. if (!empty($file_name)) {
  1184. include_once $file_name;
  1185. if (class_exists($class_name)) {
  1186. return new $class_name();
  1187. } else {
  1188. echo 'Can\'t instanciate class '.$class_name.' of type '.$type;
  1189. }
  1190. }
  1191. }
  1192. return null;
  1193. }
  1194. /**
  1195. * Creates the form to create / edit a question
  1196. * A subclass can redifine this function to add fields...
  1197. * @param FormValidator $form the formvalidator instance (by reference)
  1198. */
  1199. function createForm(&$form, $fck_config = 0)
  1200. {
  1201. $url = api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?1=1';
  1202. echo ' <style>
  1203. .media { display:none;}
  1204. </style>';
  1205. if ($this->type != MEDIA_QUESTION) {
  1206. echo '<script>
  1207. function check() {
  1208. $("#category_id option:selected").each(function() {
  1209. var id = $(this).val();
  1210. var name = $(this).text();
  1211. if (id != "" ) {
  1212. $.ajax({
  1213. async: false,
  1214. url: "'.$url.'&a=exercise_category_exists",
  1215. data: "id="+id,
  1216. success: function(return_value) {
  1217. if (return_value == 0 ) {
  1218. alert("'.get_lang('CategoryDoesNotExists').'");
  1219. //Deleting select option tag
  1220. $("#parent_id").find("option").remove();
  1221. $(".holder li").each(function () {
  1222. if ($(this).attr("rel") == id) {
  1223. $(this).remove();
  1224. }
  1225. });
  1226. }
  1227. },
  1228. });
  1229. }
  1230. });
  1231. }
  1232. $(function() {
  1233. $("#category_id").fcbkcomplete({
  1234. json_url: "'.$url.'&a=search_category_parent&type=all&",
  1235. maxitems: 20,
  1236. addontab: false,
  1237. input_min_size: 1,
  1238. cache: false,
  1239. complete_text:"'.get_lang('StartToType').'",
  1240. firstselected: false,
  1241. onselect: check,
  1242. filter_selected: true,
  1243. newel: true
  1244. });
  1245. });
  1246. </script>';
  1247. }
  1248. echo '<script>
  1249. // hack to hide http://cksource.com/forums/viewtopic.php?f=6&t=8700
  1250. function FCKeditor_OnComplete( editorInstance ) {
  1251. if (document.getElementById ( \'HiddenFCK\' + editorInstance.Name )) {
  1252. HideFCKEditorByInstanceName (editorInstance.Name);
  1253. }
  1254. }
  1255. function HideFCKEditorByInstanceName ( editorInstanceName ) {
  1256. if (document.getElementById ( \'HiddenFCK\' + editorInstanceName ).className == "HideFCKEditor" ) {
  1257. document.getElementById ( \'HiddenFCK\' + editorInstanceName ).className = "media";
  1258. }
  1259. }
  1260. function show_media(){
  1261. var my_display = document.getElementById(\'HiddenFCKquestionDescription\').style.display;
  1262. if(my_display== \'none\' || my_display == \'\') {
  1263. document.getElementById(\'HiddenFCKquestionDescription\').style.display = \'block\';
  1264. document.getElementById(\'media_icon\').innerHTML=\'&nbsp;<img style="vertical-align: middle;" src="../img/looknfeelna.png" alt="" />&nbsp;'.get_lang(
  1265. 'EnrichQuestion'
  1266. ).'\';
  1267. } else {
  1268. document.getElementById(\'HiddenFCKquestionDescription\').style.display = \'none\';
  1269. document.getElementById(\'media_icon\').innerHTML=\'&nbsp;<img style="vertical-align: middle;" src="../img/looknfeel.png" alt="" />&nbsp;'.get_lang(
  1270. 'EnrichQuestion'
  1271. ).'\';
  1272. }
  1273. }
  1274. // hub 13-12-2010
  1275. function visiblerDevisibler(in_id) {
  1276. if (document.getElementById(in_id)) {
  1277. if (document.getElementById(in_id).style.display == "none") {
  1278. document.getElementById(in_id).style.display = "block";
  1279. if (document.getElementById(in_id+"Img")) {
  1280. document.getElementById(in_id+"Img").src = "../img/div_hide.gif";
  1281. }
  1282. }
  1283. else {
  1284. document.getElementById(in_id).style.display = "none";
  1285. if (document.getElementById(in_id+"Img")) {
  1286. document.getElementById(in_id+"Img").src = "../img/div_show.gif";
  1287. }
  1288. }
  1289. }
  1290. }
  1291. </script>';
  1292. // question name
  1293. $form->addElement('text', 'questionName', get_lang('Question'), array('class' => 'span6'));
  1294. $form->addRule('questionName', get_lang('GiveQuestion'), 'required');
  1295. // default content
  1296. $isContent = isset($_REQUEST['isContent']) ? intval($_REQUEST['isContent']) : null;
  1297. // Question type
  1298. $answerType = isset($_REQUEST['answerType']) ? intval($_REQUEST['answerType']) : $this->selectType();
  1299. $form->addElement('hidden', 'answerType', $answerType);
  1300. // html editor
  1301. $editor_config = array('ToolbarSet' => 'TestQuestionDescription', 'Width' => '100%', 'Height' => '150');
  1302. if (is_array($fck_config)) {
  1303. $editor_config = array_merge($editor_config, $fck_config);
  1304. }
  1305. if (!api_is_allowed_to_edit(null, true)) {
  1306. $editor_config['UserStatus'] = 'student';
  1307. }
  1308. $form->addElement(
  1309. 'advanced_settings',
  1310. '<a href="javascript://" onclick=" return show_media()"><span id="media_icon"><img style="vertical-align: middle;" src="../img/looknfeel.png" alt="" />&nbsp;'.get_lang(
  1311. 'EnrichQuestion'
  1312. ).'</span></a>
  1313. '
  1314. );
  1315. $form->addElement('html', '<div class="HideFCKEditor" id="HiddenFCKquestionDescription" >');
  1316. $form->add_html_editor('questionDescription', get_lang('QuestionDescription'), false, false, $editor_config);
  1317. $form->addElement('html', '</div>');
  1318. // hidden values
  1319. $my_id = isset($_REQUEST['myid']) ? intval($_REQUEST['myid']) : null;
  1320. $form->addElement('hidden', 'myid', $my_id);
  1321. if ($this->type != MEDIA_QUESTION) {
  1322. // Advanced parameters
  1323. $form->addElement(
  1324. 'advanced_settings',
  1325. '<a href="javascript:void(0)" onclick="visiblerDevisibler(\'id_advancedOption\')"><img id="id_advancedOptionImg" style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang(
  1326. "AdvancedParameters"
  1327. ).'</a>'
  1328. );
  1329. $form->addElement('html', '<div id="id_advancedOption" style="display:none;">');
  1330. $select_level = Question::get_default_levels();
  1331. $form->addElement('select', 'questionLevel', get_lang('Difficulty'), $select_level);
  1332. if (!empty($this->category_list)) {
  1333. $trigger = '';
  1334. foreach ($this->category_list as $category_id) {
  1335. if (!empty($category_id)) {
  1336. $cat = new Testcategory($category_id);
  1337. if ($cat->id) {
  1338. $trigger .= '$("#category_id").trigger("addItem",[{"title": "'.$cat->parent_path.'", "value": "'.$cat->id.'"}]);';
  1339. }
  1340. }
  1341. }
  1342. echo '<script>$(function() { '.$trigger.' });</script>';
  1343. }
  1344. $form->addElement(
  1345. 'select',
  1346. 'questionCategory',
  1347. get_lang('Category'),
  1348. array(),
  1349. array('id' => 'category_id')
  1350. );
  1351. // Categories
  1352. //$tabCat = Testcategory::getCategoriesIdAndName();
  1353. //$form->addElement('select', 'questionCategory', get_lang('Category'), $tabCat);
  1354. //Medias
  1355. $course_medias = Question::prepare_course_media_select(api_get_course_int_id());
  1356. $form->addElement('select', 'parent_id', get_lang('AttachToMedia'), $course_medias);
  1357. $form->addElement('html', '</div>');
  1358. }
  1359. if (!isset($_GET['fromExercise'])) {
  1360. switch ($answerType) {
  1361. case 1:
  1362. $this->question = get_lang('DefaultUniqueQuestion');
  1363. break;
  1364. case 2:
  1365. $this->question = get_lang('DefaultMultipleQuestion');
  1366. break;
  1367. case 3:
  1368. $this->question = get_lang('DefaultFillBlankQuestion');
  1369. break;
  1370. case 4:
  1371. $this->question = get_lang('DefaultMathingQuestion');
  1372. break;
  1373. case 5:
  1374. $this->question = get_lang('DefaultOpenQuestion');
  1375. break;
  1376. case 9:
  1377. $this->question = get_lang('DefaultMultipleQuestion');
  1378. break;
  1379. }
  1380. }
  1381. // default values
  1382. $defaults = array();
  1383. $defaults['questionName'] = $this->question;
  1384. $defaults['questionDescription'] = $this->description;
  1385. $defaults['questionLevel'] = $this->level;
  1386. $defaults['questionCategory'] = $this->category_list;
  1387. $defaults['parent_id'] = $this->parent_id;
  1388. //Came from he question pool
  1389. if (isset($_GET['fromExercise'])) {
  1390. $form->setDefaults($defaults);
  1391. }
  1392. if (!empty($_REQUEST['myid'])) {
  1393. $form->setDefaults($defaults);
  1394. } else {
  1395. if ($isContent == 1) {
  1396. $form->setDefaults($defaults);
  1397. }
  1398. }
  1399. }
  1400. /**
  1401. * function which process the creation of questions
  1402. * @param FormValidator $form the formvalidator instance
  1403. * @param Exercise $objExercise the Exercise instance
  1404. */
  1405. function processCreation($form, $objExercise = null)
  1406. {
  1407. $this->updateParentId($form->getSubmitValue('parent_id'));
  1408. $this->updateTitle($form->getSubmitValue('questionName'));
  1409. $this->updateDescription($form->getSubmitValue('questionDescription'));
  1410. $this->updateLevel($form->getSubmitValue('questionLevel'));
  1411. $this->updateCategory($form->getSubmitValue('questionCategory'));
  1412. //Save normal question if NOT media
  1413. if ($this->type != MEDIA_QUESTION) {
  1414. $this->save($objExercise->id);
  1415. // modify the exercise
  1416. $objExercise->addToList($this->id);
  1417. $objExercise->update_question_positions();
  1418. }
  1419. }
  1420. /**
  1421. * abstract function which creates the form to create / edit the answers of the question
  1422. * @param the formvalidator instance
  1423. */
  1424. abstract function createAnswersForm($form);
  1425. /**
  1426. * abstract function which process the creation of answers
  1427. * @param the formvalidator instance
  1428. */
  1429. abstract function processAnswersCreation($form);
  1430. /**
  1431. * Displays the menu of question types
  1432. */
  1433. static function display_type_menu($objExercise)
  1434. {
  1435. $feedback_type = $objExercise->feedback_type;
  1436. $exerciseId = $objExercise->id;
  1437. // 1. by default we show all the question types
  1438. $question_type_custom_list = self::get_question_type_list();
  1439. if (!isset($feedback_type)) {
  1440. $feedback_type = 0;
  1441. }
  1442. if ($feedback_type == 1) {
  1443. //2. but if it is a feedback DIRECT we only show the UNIQUE_ANSWER type that is currently available
  1444. $question_type_custom_list = array(
  1445. UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER],
  1446. HOT_SPOT_DELINEATION => self::$questionTypes[HOT_SPOT_DELINEATION]
  1447. );
  1448. } else {
  1449. unset($question_type_custom_list[HOT_SPOT_DELINEATION]);
  1450. }
  1451. echo '<div class="actionsbig">';
  1452. echo '<ul class="question_menu">';
  1453. foreach ($question_type_custom_list as $i => $a_type) {
  1454. // include the class of the type
  1455. require_once $a_type[0];
  1456. // get the picture of the type and the langvar which describes it
  1457. $img = $explanation = '';
  1458. eval('$img = '.$a_type[1].'::$typePicture;');
  1459. eval('$explanation = get_lang('.$a_type[1].'::$explanationLangVar);');
  1460. echo '<li>';
  1461. echo '<div class="icon_image_content">';
  1462. if ($objExercise->exercise_was_added_in_lp == true) {
  1463. $img = pathinfo($img);
  1464. $img = $img['filename'].'_na.'.$img['extension'];
  1465. echo Display::return_icon($img, $explanation, array(), ICON_SIZE_BIG);
  1466. } else {
  1467. echo '<a href="admin.php?'.api_get_cidreq(
  1468. ).'&newQuestion=yes&answerType='.$i.'&exerciseId='.$exerciseId.'">'.Display::return_icon(
  1469. $img,
  1470. $explanation,
  1471. array(),
  1472. ICON_SIZE_BIG
  1473. ).'</a>';
  1474. }
  1475. echo '</div>';
  1476. echo '</li>';
  1477. }
  1478. echo '<li>';
  1479. echo '<div class="icon_image_content">';
  1480. if ($objExercise->exercise_was_added_in_lp == true) {
  1481. echo Display::return_icon('database_na.png', get_lang('GetExistingQuestion'));
  1482. } else {
  1483. if ($feedback_type == 1) {
  1484. echo $url = '<a href="question_pool.php?'.api_get_cidreq().'&type=1&fromExercise='.$exerciseId.'">';
  1485. } else {
  1486. echo $url = '<a href="question_pool.php?'.api_get_cidreq().'&fromExercise='.$exerciseId.'">';
  1487. }
  1488. echo Display::return_icon('database.png', get_lang('GetExistingQuestion'));
  1489. }
  1490. echo '</a>';
  1491. echo '</div></li>';
  1492. echo '</ul>';
  1493. echo '</div>';
  1494. }
  1495. static function saveQuestionOption($question_id, $name, $course_id, $position = 0)
  1496. {
  1497. $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1498. $params['question_id'] = intval($question_id);
  1499. $params['name'] = $name;
  1500. $params['position'] = $position;
  1501. $params['c_id'] = $course_id;
  1502. //$result = self::readQuestionOption($question_id, $course_id);
  1503. $last_id = Database::insert($TBL_EXERCICE_QUESTION_OPTION, $params);
  1504. return $last_id;
  1505. }
  1506. static function deleteAllQuestionOptions($question_id, $course_id)
  1507. {
  1508. $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1509. Database::delete(
  1510. $TBL_EXERCICE_QUESTION_OPTION,
  1511. array('c_id = ? AND question_id = ?' => array($course_id, $question_id))
  1512. );
  1513. }
  1514. static function updateQuestionOption($id, $params, $course_id)
  1515. {
  1516. $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1517. $result = Database::update(
  1518. $TBL_EXERCICE_QUESTION_OPTION,
  1519. $params,
  1520. array('c_id = ? AND id = ?' => array($course_id, $id))
  1521. );
  1522. return $result;
  1523. }
  1524. static function readQuestionOption($question_id, $course_id)
  1525. {
  1526. $TBL_EXERCICE_QUESTION_OPTION = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1527. $result = Database::select(
  1528. '*',
  1529. $TBL_EXERCICE_QUESTION_OPTION,
  1530. array(
  1531. 'where' => array('c_id = ? AND question_id = ?' => array($course_id, $question_id)),
  1532. 'order' => 'iid ASC'
  1533. )
  1534. );
  1535. if (!empty($result)) {
  1536. $new_result = array();
  1537. foreach ($result as $item) {
  1538. $new_result[$item['iid']] = $item;
  1539. }
  1540. return $new_result;
  1541. }
  1542. return array();
  1543. }
  1544. /**
  1545. * Shows question title an description
  1546. *
  1547. * @param type $feedback_type
  1548. * @param type $counter
  1549. * @param type $score
  1550. *
  1551. * @return string
  1552. */
  1553. function return_header($feedback_type = null, $counter = null, $score = null, $show_media)
  1554. {
  1555. $counter_label = '';
  1556. if (!empty($counter)) {
  1557. $counter_label = intval($counter);
  1558. }
  1559. $score_label = get_lang('Wrong');
  1560. $class = 'error';
  1561. if ($score['pass'] == true) {
  1562. $score_label = get_lang('Correct');
  1563. $class = 'success';
  1564. }
  1565. if ($this->type == FREE_ANSWER || $this->type == ORAL_EXPRESSION) {
  1566. if ($score['revised'] == true) {
  1567. $score_label = get_lang('Revised');
  1568. $class = '';
  1569. } else {
  1570. $score_label = get_lang('NotRevised');
  1571. $class = 'error';
  1572. }
  1573. }
  1574. $question_title = $this->question;
  1575. $header = null;
  1576. // Display question category, if any
  1577. if ($show_media) {
  1578. $header .= $this->show_media_content();
  1579. }
  1580. $header .= Display::page_subheader2($counter_label.". ".$question_title);
  1581. //$header .= Display::div('<div class="rib rib-'.$class.'"><h3>'.$score_label.'</h3></div> <h4>'.($score['result']).' </h4><h5 class="'.$class.'">'.$score['result'].' </h5>', array('class'=>'ribbon'));
  1582. $header .= Display::div(
  1583. '<div class="rib rib-'.$class.'"><h3>'.$score_label.'</h3></div> <h4>'.$score['result'].' </h4>',
  1584. array('class' => 'ribbon')
  1585. );
  1586. $header .= Display::div($this->description, array('id' => 'question_description'));
  1587. return $header;
  1588. }
  1589. /**
  1590. * Create a question from a set of parameters
  1591. * @param int Quiz ID
  1592. * @param string Question name
  1593. * @param int Maximum result for the question
  1594. * @param int Type of question (see constants at beginning of question.class.php)
  1595. * @param int Question level/category
  1596. */
  1597. function create_question($quiz_id, $question_name, $max_score = 0, $type = 1, $level = 1)
  1598. {
  1599. $course_id = api_get_course_int_id();
  1600. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1601. $tbl_quiz_rel_question = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1602. $quiz_id = intval($quiz_id);
  1603. $max_score = (float)$max_score;
  1604. $type = intval($type);
  1605. $level = intval($level);
  1606. // Get the max position
  1607. $sql = "SELECT max(position) as max_position"
  1608. ." FROM $tbl_quiz_question q INNER JOIN $tbl_quiz_rel_question r"
  1609. ." ON q.iid = r.question_id"
  1610. ." AND exercice_id = $quiz_id AND q.c_id = $course_id AND r.c_id = $course_id";
  1611. $rs_max = Database::query($sql);
  1612. $row_max = Database::fetch_object($rs_max);
  1613. $max_position = $row_max->max_position + 1;
  1614. // Insert the new question
  1615. $sql = "INSERT INTO $tbl_quiz_question (c_id, question, ponderation, position, type, level)
  1616. VALUES ($course_id, '".Database::escape_string(
  1617. $question_name
  1618. )."', '$max_score', $max_position, $type, $level)";
  1619. Database::query($sql);
  1620. // Get the question ID
  1621. $question_id = Database::get_last_insert_id();
  1622. // Get the max question_order
  1623. $sql = "SELECT max(question_order) as max_order "
  1624. ."FROM $tbl_quiz_rel_question WHERE c_id = $course_id AND exercice_id = $quiz_id ";
  1625. $rs_max_order = Database::query($sql);
  1626. $row_max_order = Database::fetch_object($rs_max_order);
  1627. $max_order = $row_max_order->max_order + 1;
  1628. // Attach questions to quiz
  1629. $sql = "INSERT INTO $tbl_quiz_rel_question (c_id, question_id, exercice_id, question_order)"
  1630. ." VALUES($course_id, $question_id, $quiz_id, $max_order)";
  1631. Database::query($sql);
  1632. return $question_id;
  1633. }
  1634. /**
  1635. * return the image filename of the question type
  1636. *
  1637. */
  1638. public function get_type_icon_html()
  1639. {
  1640. $type = $this->selectType();
  1641. // [0]=file to include [1]=type name
  1642. $tabQuestionList = Question::get_question_type_list();
  1643. require_once $tabQuestionList[$type][0];
  1644. eval('$img = '.$tabQuestionList[$type][1].'::$typePicture;');
  1645. eval('$explanation = get_lang('.$tabQuestionList[$type][1].'::$explanationLangVar);');
  1646. return array($img, $explanation);
  1647. }
  1648. /**
  1649. * Get course medias
  1650. * @param int course id
  1651. */
  1652. static function get_course_medias(
  1653. $course_id,
  1654. $start = 0,
  1655. $limit = 100,
  1656. $sidx = "question",
  1657. $sord = "ASC",
  1658. $where_condition = array()
  1659. ) {
  1660. $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1661. $default_where = array('c_id = ? AND parent_id = 0 AND type = ?' => array($course_id, MEDIA_QUESTION));
  1662. if (!empty($where_condition)) {
  1663. //$where_condition
  1664. }
  1665. $result = Database::select(
  1666. '*',
  1667. $table_question,
  1668. array(
  1669. 'limit' => " $start, $limit",
  1670. 'where' => $default_where,
  1671. 'order' => "$sidx $sord"
  1672. )
  1673. );
  1674. return $result;
  1675. }
  1676. /**
  1677. * Get count course medias
  1678. * @param int course id
  1679. */
  1680. static function get_count_course_medias($course_id)
  1681. {
  1682. $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1683. $result = Database::select(
  1684. 'count(*) as count',
  1685. $table_question,
  1686. array('where' => array('c_id = ? AND parent_id = 0 AND type = ?' => array($course_id, MEDIA_QUESTION))),
  1687. 'first'
  1688. );
  1689. if ($result && isset($result['count'])) {
  1690. return $result['count'];
  1691. }
  1692. return 0;
  1693. }
  1694. static function prepare_course_media_select($course_id)
  1695. {
  1696. $medias = self::get_course_medias($course_id);
  1697. $media_list = array();
  1698. $media_list[0] = get_lang('NoMedia');
  1699. if (!empty($medias)) {
  1700. foreach ($medias as $media) {
  1701. $media_list[$media['iid']] = empty($media['question']) ? get_lang('Untitled') : $media['question'];
  1702. }
  1703. }
  1704. return $media_list;
  1705. }
  1706. static function get_default_levels()
  1707. {
  1708. $select_level = array(
  1709. 1 => 1,
  1710. 2 => 2,
  1711. 3 => 3,
  1712. 4 => 4,
  1713. 5 => 5
  1714. );
  1715. return $select_level;
  1716. }
  1717. function show_media_content()
  1718. {
  1719. $html = null;
  1720. if ($this->parent_id != 0) {
  1721. $parent_question = Question::read($this->parent_id);
  1722. $html = $parent_question->show_media_content();
  1723. } else {
  1724. $html .= Display::page_subheader($this->selectTitle());
  1725. $html .= $this->selectDescription();
  1726. }
  1727. return $html;
  1728. }
  1729. static function question_type_no_review()
  1730. {
  1731. return array(
  1732. HOT_SPOT,
  1733. HOT_SPOT_ORDER,
  1734. HOT_SPOT_DELINEATION
  1735. );
  1736. }
  1737. }