question.class.php 59 KB

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