question.class.php 67 KB

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