question.class.php 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CQuizAnswer;
  4. /**
  5. * Class Question.
  6. *
  7. * This class allows to instantiate an object of type Question
  8. *
  9. * @author Olivier Brouckaert, original author
  10. * @author Patrick Cool, LaTeX support
  11. * @author Julio Montoya <gugli100@gmail.com> lot of bug fixes
  12. * @author hubert.borderiou@grenet.fr - add question categories
  13. *
  14. * @package chamilo.exercise
  15. */
  16. abstract class Question
  17. {
  18. public $id;
  19. public $iid;
  20. public $question;
  21. public $description;
  22. public $weighting;
  23. public $position;
  24. public $type;
  25. public $level;
  26. public $picture;
  27. public $exerciseList; // array with the list of exercises which this question is in
  28. public $category_list;
  29. public $parent_id;
  30. public $category;
  31. public $isContent;
  32. public $course;
  33. public $feedback;
  34. public $typePicture = 'new_question.png';
  35. public $explanationLangVar = '';
  36. public $question_table_class = 'table table-striped';
  37. public $questionTypeWithFeedback;
  38. public $extra;
  39. public $export = false;
  40. public $code;
  41. public static $questionTypes = [
  42. UNIQUE_ANSWER => ['unique_answer.class.php', 'UniqueAnswer'],
  43. MULTIPLE_ANSWER => ['multiple_answer.class.php', 'MultipleAnswer'],
  44. FILL_IN_BLANKS => ['fill_blanks.class.php', 'FillBlanks'],
  45. MATCHING => ['matching.class.php', 'Matching'],
  46. FREE_ANSWER => ['freeanswer.class.php', 'FreeAnswer'],
  47. ORAL_EXPRESSION => ['oral_expression.class.php', 'OralExpression'],
  48. HOT_SPOT => ['hotspot.class.php', 'HotSpot'],
  49. HOT_SPOT_DELINEATION => ['HotSpotDelineation.php', 'HotSpotDelineation'],
  50. MULTIPLE_ANSWER_COMBINATION => ['multiple_answer_combination.class.php', 'MultipleAnswerCombination'],
  51. UNIQUE_ANSWER_NO_OPTION => ['unique_answer_no_option.class.php', 'UniqueAnswerNoOption'],
  52. MULTIPLE_ANSWER_TRUE_FALSE => ['multiple_answer_true_false.class.php', 'MultipleAnswerTrueFalse'],
  53. MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY => [
  54. 'MultipleAnswerTrueFalseDegreeCertainty.php',
  55. 'MultipleAnswerTrueFalseDegreeCertainty',
  56. ],
  57. MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE => [
  58. 'multiple_answer_combination_true_false.class.php',
  59. 'MultipleAnswerCombinationTrueFalse',
  60. ],
  61. GLOBAL_MULTIPLE_ANSWER => ['global_multiple_answer.class.php', 'GlobalMultipleAnswer'],
  62. CALCULATED_ANSWER => ['calculated_answer.class.php', 'CalculatedAnswer'],
  63. UNIQUE_ANSWER_IMAGE => ['UniqueAnswerImage.php', 'UniqueAnswerImage'],
  64. DRAGGABLE => ['Draggable.php', 'Draggable'],
  65. MATCHING_DRAGGABLE => ['MatchingDraggable.php', 'MatchingDraggable'],
  66. //MEDIA_QUESTION => array('media_question.class.php' , 'MediaQuestion')
  67. ANNOTATION => ['Annotation.php', 'Annotation'],
  68. READING_COMPREHENSION => ['ReadingComprehension.php', 'ReadingComprehension'],
  69. ];
  70. /**
  71. * constructor of the class.
  72. *
  73. * @author Olivier Brouckaert
  74. */
  75. public function __construct()
  76. {
  77. $this->id = 0;
  78. $this->iid = 0;
  79. $this->question = '';
  80. $this->description = '';
  81. $this->weighting = 0;
  82. $this->position = 1;
  83. $this->picture = '';
  84. $this->level = 1;
  85. $this->category = 0;
  86. // This variable is used when loading an exercise like an scenario with
  87. // an special hotspot: final_overlap, final_missing, final_excess
  88. $this->extra = '';
  89. $this->exerciseList = [];
  90. $this->course = api_get_course_info();
  91. $this->category_list = [];
  92. $this->parent_id = 0;
  93. // See BT#12611
  94. $this->questionTypeWithFeedback = [
  95. MATCHING,
  96. MATCHING_DRAGGABLE,
  97. DRAGGABLE,
  98. FILL_IN_BLANKS,
  99. FREE_ANSWER,
  100. ORAL_EXPRESSION,
  101. CALCULATED_ANSWER,
  102. ANNOTATION,
  103. ];
  104. }
  105. /**
  106. * @return int|null
  107. */
  108. public function getIsContent()
  109. {
  110. $isContent = null;
  111. if (isset($_REQUEST['isContent'])) {
  112. $isContent = intval($_REQUEST['isContent']);
  113. }
  114. return $this->isContent = $isContent;
  115. }
  116. /**
  117. * Reads question information from the data base.
  118. *
  119. * @param int $id - question ID
  120. * @param array $course_info
  121. * @param bool $getExerciseList
  122. *
  123. * @return Question
  124. *
  125. * @author Olivier Brouckaert
  126. */
  127. public static function read($id, $course_info = [], $getExerciseList = true)
  128. {
  129. $id = (int) $id;
  130. if (empty($course_info)) {
  131. $course_info = api_get_course_info();
  132. }
  133. $course_id = $course_info['real_id'];
  134. if (empty($course_id) || $course_id == -1) {
  135. return false;
  136. }
  137. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  138. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  139. $sql = "SELECT *
  140. FROM $TBL_QUESTIONS
  141. WHERE c_id = $course_id AND id = $id ";
  142. $result = Database::query($sql);
  143. // if the question has been found
  144. if ($object = Database::fetch_object($result)) {
  145. $objQuestion = self::getInstance($object->type);
  146. if (!empty($objQuestion)) {
  147. $objQuestion->id = (int) $id;
  148. $objQuestion->iid = (int) $object->iid;
  149. $objQuestion->question = $object->question;
  150. $objQuestion->description = $object->description;
  151. $objQuestion->weighting = $object->ponderation;
  152. $objQuestion->position = $object->position;
  153. $objQuestion->type = (int) $object->type;
  154. $objQuestion->picture = $object->picture;
  155. $objQuestion->level = (int) $object->level;
  156. $objQuestion->extra = $object->extra;
  157. $objQuestion->course = $course_info;
  158. $objQuestion->feedback = isset($object->feedback) ? $object->feedback : '';
  159. $objQuestion->category = TestCategory::getCategoryForQuestion($id, $course_id);
  160. $objQuestion->code = isset($object->code) ? $object->code : '';
  161. if ($getExerciseList) {
  162. $tblQuiz = Database::get_course_table(TABLE_QUIZ_TEST);
  163. $sql = "SELECT DISTINCT q.exercice_id
  164. FROM $TBL_EXERCISE_QUESTION q
  165. INNER JOIN $tblQuiz e
  166. ON e.c_id = q.c_id AND e.id = q.exercice_id
  167. WHERE
  168. q.c_id = $course_id AND
  169. q.question_id = $id AND
  170. e.active >= 0";
  171. $result = Database::query($sql);
  172. // fills the array with the exercises which this question is in
  173. if ($result) {
  174. while ($obj = Database::fetch_object($result)) {
  175. $objQuestion->exerciseList[] = $obj->exercice_id;
  176. }
  177. }
  178. }
  179. return $objQuestion;
  180. }
  181. }
  182. // question not found
  183. return false;
  184. }
  185. /**
  186. * returns the question ID.
  187. *
  188. * @author Olivier Brouckaert
  189. *
  190. * @return int - question ID
  191. */
  192. public function selectId()
  193. {
  194. return $this->id;
  195. }
  196. /**
  197. * returns the question title.
  198. *
  199. * @author Olivier Brouckaert
  200. *
  201. * @return string - question title
  202. */
  203. public function selectTitle()
  204. {
  205. if (!api_get_configuration_value('save_titles_as_html')) {
  206. return $this->question;
  207. }
  208. return Display::div($this->question, ['style' => 'display: inline-block;']);
  209. }
  210. /**
  211. * @param int $itemNumber
  212. *
  213. * @return string
  214. */
  215. public function getTitleToDisplay($itemNumber)
  216. {
  217. $showQuestionTitleHtml = api_get_configuration_value('save_titles_as_html');
  218. $title = '';
  219. if (api_get_configuration_value('show_question_id')) {
  220. $title .= '<h4>#'.$this->course['code'].'-'.$this->iid.'</h4>';
  221. }
  222. $title .= $showQuestionTitleHtml ? '' : '<strong>';
  223. $title .= $itemNumber.'. '.$this->selectTitle();
  224. $title .= $showQuestionTitleHtml ? '' : '</strong>';
  225. return Display::div(
  226. $title,
  227. ['class' => 'question_title']
  228. );
  229. }
  230. /**
  231. * returns the question description.
  232. *
  233. * @author Olivier Brouckaert
  234. *
  235. * @return string - question description
  236. */
  237. public function selectDescription()
  238. {
  239. return $this->description;
  240. }
  241. /**
  242. * returns the question weighting.
  243. *
  244. * @author Olivier Brouckaert
  245. *
  246. * @return int - question weighting
  247. */
  248. public function selectWeighting()
  249. {
  250. return $this->weighting;
  251. }
  252. /**
  253. * returns the question position.
  254. *
  255. * @author Olivier Brouckaert
  256. *
  257. * @return int - question position
  258. */
  259. public function selectPosition()
  260. {
  261. return $this->position;
  262. }
  263. /**
  264. * returns the answer type.
  265. *
  266. * @author Olivier Brouckaert
  267. *
  268. * @return int - answer type
  269. */
  270. public function selectType()
  271. {
  272. return $this->type;
  273. }
  274. /**
  275. * returns the level of the question.
  276. *
  277. * @author Nicolas Raynaud
  278. *
  279. * @return int - level of the question, 0 by default
  280. */
  281. public function getLevel()
  282. {
  283. return $this->level;
  284. }
  285. /**
  286. * returns the picture name.
  287. *
  288. * @author Olivier Brouckaert
  289. *
  290. * @return string - picture name
  291. */
  292. public function selectPicture()
  293. {
  294. return $this->picture;
  295. }
  296. /**
  297. * @return string
  298. */
  299. public function selectPicturePath()
  300. {
  301. if (!empty($this->picture)) {
  302. return api_get_path(WEB_COURSE_PATH).$this->course['directory'].'/document/images/'.$this->getPictureFilename();
  303. }
  304. return '';
  305. }
  306. /**
  307. * @return int|string
  308. */
  309. public function getPictureId()
  310. {
  311. // for backward compatibility
  312. // when in field picture we had the filename not the document id
  313. if (preg_match("/quiz-.*/", $this->picture)) {
  314. return DocumentManager::get_document_id(
  315. $this->course,
  316. $this->selectPicturePath(),
  317. api_get_session_id()
  318. );
  319. }
  320. return $this->picture;
  321. }
  322. /**
  323. * @param int $courseId
  324. * @param int $sessionId
  325. *
  326. * @return string
  327. */
  328. public function getPictureFilename($courseId = 0, $sessionId = 0)
  329. {
  330. $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
  331. $sessionId = empty($sessionId) ? api_get_session_id() : (int) $sessionId;
  332. if (empty($courseId)) {
  333. return '';
  334. }
  335. // for backward compatibility
  336. // when in field picture we had the filename not the document id
  337. if (preg_match("/quiz-.*/", $this->picture)) {
  338. return $this->picture;
  339. }
  340. $pictureId = $this->getPictureId();
  341. $courseInfo = $this->course;
  342. $documentInfo = DocumentManager::get_document_data_by_id(
  343. $pictureId,
  344. $courseInfo['code'],
  345. false,
  346. $sessionId
  347. );
  348. $documentFilename = '';
  349. if ($documentInfo) {
  350. // document in document/images folder
  351. $documentFilename = pathinfo(
  352. $documentInfo['path'],
  353. PATHINFO_BASENAME
  354. );
  355. }
  356. return $documentFilename;
  357. }
  358. /**
  359. * returns the array with the exercise ID list.
  360. *
  361. * @author Olivier Brouckaert
  362. *
  363. * @return array - list of exercise ID which the question is in
  364. */
  365. public function selectExerciseList()
  366. {
  367. return $this->exerciseList;
  368. }
  369. /**
  370. * returns the number of exercises which this question is in.
  371. *
  372. * @author Olivier Brouckaert
  373. *
  374. * @return int - number of exercises
  375. */
  376. public function selectNbrExercises()
  377. {
  378. return count($this->exerciseList);
  379. }
  380. /**
  381. * changes the question title.
  382. *
  383. * @param string $title - question title
  384. *
  385. * @author Olivier Brouckaert
  386. */
  387. public function updateTitle($title)
  388. {
  389. $this->question = $title;
  390. }
  391. /**
  392. * @param int $id
  393. */
  394. public function updateParentId($id)
  395. {
  396. $this->parent_id = (int) $id;
  397. }
  398. /**
  399. * changes the question description.
  400. *
  401. * @param string $description - question description
  402. *
  403. * @author Olivier Brouckaert
  404. */
  405. public function updateDescription($description)
  406. {
  407. $this->description = $description;
  408. }
  409. /**
  410. * changes the question weighting.
  411. *
  412. * @param int $weighting - question weighting
  413. *
  414. * @author Olivier Brouckaert
  415. */
  416. public function updateWeighting($weighting)
  417. {
  418. $this->weighting = $weighting;
  419. }
  420. /**
  421. * @param array $category
  422. *
  423. * @author Hubert Borderiou 12-10-2011
  424. */
  425. public function updateCategory($category)
  426. {
  427. $this->category = $category;
  428. }
  429. /**
  430. * @param int $value
  431. *
  432. * @author Hubert Borderiou 12-10-2011
  433. */
  434. public function updateScoreAlwaysPositive($value)
  435. {
  436. $this->scoreAlwaysPositive = $value;
  437. }
  438. /**
  439. * @param int $value
  440. *
  441. * @author Hubert Borderiou 12-10-2011
  442. */
  443. public function updateUncheckedMayScore($value)
  444. {
  445. $this->uncheckedMayScore = $value;
  446. }
  447. /**
  448. * Save category of a question.
  449. *
  450. * A question can have n categories if category is empty,
  451. * then question has no category then delete the category entry
  452. *
  453. * @param array $category_list
  454. *
  455. * @author Julio Montoya - Adding multiple cat support
  456. */
  457. public function saveCategories($category_list)
  458. {
  459. if (!empty($category_list)) {
  460. $this->deleteCategory();
  461. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  462. // update or add category for a question
  463. foreach ($category_list as $category_id) {
  464. $category_id = (int) $category_id;
  465. $question_id = (int) $this->id;
  466. $sql = "SELECT count(*) AS nb
  467. FROM $table
  468. WHERE
  469. category_id = $category_id
  470. AND question_id = $question_id
  471. AND c_id=".api_get_course_int_id();
  472. $res = Database::query($sql);
  473. $row = Database::fetch_array($res);
  474. if ($row['nb'] > 0) {
  475. // DO nothing
  476. } else {
  477. $sql = "INSERT INTO $table (c_id, question_id, category_id)
  478. VALUES (".api_get_course_int_id().", $question_id, $category_id)";
  479. Database::query($sql);
  480. }
  481. }
  482. }
  483. }
  484. /**
  485. * in this version, a question can only have 1 category
  486. * if category is 0, then question has no category then delete the category entry.
  487. *
  488. * @param int $categoryId
  489. * @param int $courseId
  490. *
  491. * @return bool
  492. *
  493. * @author Hubert Borderiou 12-10-2011
  494. */
  495. public function saveCategory($categoryId, $courseId = 0)
  496. {
  497. $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
  498. if (empty($courseId)) {
  499. return false;
  500. }
  501. if ($categoryId <= 0) {
  502. $this->deleteCategory($courseId);
  503. } else {
  504. // update or add category for a question
  505. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  506. $categoryId = (int) $categoryId;
  507. $question_id = (int) $this->id;
  508. $sql = "SELECT count(*) AS nb FROM $table
  509. WHERE
  510. question_id = $question_id AND
  511. c_id = ".$courseId;
  512. $res = Database::query($sql);
  513. $row = Database::fetch_array($res);
  514. if ($row['nb'] > 0) {
  515. $sql = "UPDATE $table
  516. SET category_id = $categoryId
  517. WHERE
  518. question_id = $question_id AND
  519. c_id = ".$courseId;
  520. Database::query($sql);
  521. } else {
  522. $sql = "INSERT INTO $table (c_id, question_id, category_id)
  523. VALUES (".$courseId.", $question_id, $categoryId)";
  524. Database::query($sql);
  525. }
  526. return true;
  527. }
  528. }
  529. /**
  530. * @author hubert borderiou 12-10-2011
  531. *
  532. * @param int $courseId
  533. * delete any category entry for question id
  534. * delete the category for question
  535. *
  536. * @return bool
  537. */
  538. public function deleteCategory($courseId = 0)
  539. {
  540. $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
  541. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  542. $questionId = (int) $this->id;
  543. if (empty($courseId) || empty($questionId)) {
  544. return false;
  545. }
  546. $sql = "DELETE FROM $table
  547. WHERE
  548. question_id = $questionId AND
  549. c_id = ".$courseId;
  550. Database::query($sql);
  551. return true;
  552. }
  553. /**
  554. * changes the question position.
  555. *
  556. * @param int $position - question position
  557. *
  558. * @author Olivier Brouckaert
  559. */
  560. public function updatePosition($position)
  561. {
  562. $this->position = $position;
  563. }
  564. /**
  565. * changes the question level.
  566. *
  567. * @param int $level - question level
  568. *
  569. * @author Nicolas Raynaud
  570. */
  571. public function updateLevel($level)
  572. {
  573. $this->level = $level;
  574. }
  575. /**
  576. * changes the answer type. If the user changes the type from "unique answer" to "multiple answers"
  577. * (or conversely) answers are not deleted, otherwise yes.
  578. *
  579. * @param int $type - answer type
  580. *
  581. * @author Olivier Brouckaert
  582. */
  583. public function updateType($type)
  584. {
  585. $table = Database::get_course_table(TABLE_QUIZ_ANSWER);
  586. $course_id = $this->course['real_id'];
  587. if (empty($course_id)) {
  588. $course_id = api_get_course_int_id();
  589. }
  590. // if we really change the type
  591. if ($type != $this->type) {
  592. // if we don't change from "unique answer" to "multiple answers" (or conversely)
  593. if (!in_array($this->type, [UNIQUE_ANSWER, MULTIPLE_ANSWER]) ||
  594. !in_array($type, [UNIQUE_ANSWER, MULTIPLE_ANSWER])
  595. ) {
  596. // removes old answers
  597. $sql = "DELETE FROM $table
  598. WHERE c_id = $course_id AND question_id = ".intval($this->id);
  599. Database::query($sql);
  600. }
  601. $this->type = $type;
  602. }
  603. }
  604. /**
  605. * Get default hot spot folder in documents.
  606. *
  607. * @param array $courseInfo
  608. *
  609. * @return string
  610. */
  611. public function getHotSpotFolderInCourse($courseInfo = [])
  612. {
  613. $courseInfo = empty($courseInfo) ? $this->course : $courseInfo;
  614. if (empty($courseInfo) || empty($courseInfo['directory'])) {
  615. // Stop everything if course is not set.
  616. api_not_allowed();
  617. }
  618. $pictureAbsolutePath = api_get_path(SYS_COURSE_PATH).$courseInfo['directory'].'/document/images/';
  619. $picturePath = basename($pictureAbsolutePath);
  620. if (!is_dir($picturePath)) {
  621. create_unexisting_directory(
  622. $courseInfo,
  623. api_get_user_id(),
  624. 0,
  625. 0,
  626. 0,
  627. dirname($pictureAbsolutePath),
  628. '/'.$picturePath,
  629. $picturePath,
  630. '',
  631. false,
  632. false
  633. );
  634. }
  635. return $pictureAbsolutePath;
  636. }
  637. /**
  638. * adds a picture to the question.
  639. *
  640. * @param string $picture - temporary path of the picture to upload
  641. *
  642. * @return bool - true if uploaded, otherwise false
  643. *
  644. * @author Olivier Brouckaert
  645. */
  646. public function uploadPicture($picture)
  647. {
  648. $picturePath = $this->getHotSpotFolderInCourse();
  649. // if the question has got an ID
  650. if ($this->id) {
  651. $pictureFilename = self::generatePictureName();
  652. $img = new Image($picture);
  653. $img->send_image($picturePath.'/'.$pictureFilename, -1, 'jpg');
  654. $document_id = add_document(
  655. $this->course,
  656. '/images/'.$pictureFilename,
  657. 'file',
  658. filesize($picturePath.'/'.$pictureFilename),
  659. $pictureFilename
  660. );
  661. if ($document_id) {
  662. $this->picture = $document_id;
  663. if (!file_exists($picturePath.'/'.$pictureFilename)) {
  664. return false;
  665. }
  666. api_item_property_update(
  667. $this->course,
  668. TOOL_DOCUMENT,
  669. $document_id,
  670. 'DocumentAdded',
  671. api_get_user_id()
  672. );
  673. $this->resizePicture('width', 800);
  674. return true;
  675. }
  676. }
  677. return false;
  678. }
  679. /**
  680. * return the name for image use in hotspot question
  681. * to be unique, name is quiz-[utc unix timestamp].jpg.
  682. *
  683. * @param string $prefix
  684. * @param string $extension
  685. *
  686. * @return string
  687. */
  688. public function generatePictureName($prefix = 'quiz-', $extension = 'jpg')
  689. {
  690. // image name is quiz-xxx.jpg in folder images/
  691. $utcTime = time();
  692. return $prefix.$utcTime.'.'.$extension;
  693. }
  694. /**
  695. * deletes the picture.
  696. *
  697. * @author Olivier Brouckaert
  698. *
  699. * @return bool - true if removed, otherwise false
  700. */
  701. public function removePicture()
  702. {
  703. $picturePath = $this->getHotSpotFolderInCourse();
  704. // if the question has got an ID and if the picture exists
  705. if ($this->id) {
  706. $picture = $this->picture;
  707. $this->picture = '';
  708. return @unlink($picturePath.'/'.$picture) ? true : false;
  709. }
  710. return false;
  711. }
  712. /**
  713. * Exports a picture to another question.
  714. *
  715. * @author Olivier Brouckaert
  716. *
  717. * @param int $questionId - ID of the target question
  718. * @param array $courseInfo destination course info
  719. *
  720. * @return bool - true if copied, otherwise false
  721. */
  722. public function exportPicture($questionId, $courseInfo)
  723. {
  724. if (empty($questionId) || empty($courseInfo)) {
  725. return false;
  726. }
  727. $course_id = $courseInfo['real_id'];
  728. $destination_path = $this->getHotSpotFolderInCourse($courseInfo);
  729. if (empty($destination_path)) {
  730. return false;
  731. }
  732. $source_path = $this->getHotSpotFolderInCourse();
  733. // if the question has got an ID and if the picture exists
  734. if (!$this->id || empty($this->picture)) {
  735. return false;
  736. }
  737. $sourcePictureName = $this->getPictureFilename($course_id);
  738. $picture = $this->generatePictureName();
  739. $result = false;
  740. if (file_exists($source_path.'/'.$sourcePictureName)) {
  741. // for backward compatibility
  742. $result = copy(
  743. $source_path.'/'.$sourcePictureName,
  744. $destination_path.'/'.$picture
  745. );
  746. } else {
  747. $imageInfo = DocumentManager::get_document_data_by_id(
  748. $this->picture,
  749. $courseInfo['code']
  750. );
  751. if (file_exists($imageInfo['absolute_path'])) {
  752. $result = @copy(
  753. $imageInfo['absolute_path'],
  754. $destination_path.'/'.$picture
  755. );
  756. }
  757. }
  758. // If copy was correct then add to the database
  759. if (!$result) {
  760. return false;
  761. }
  762. $table = Database::get_course_table(TABLE_QUIZ_QUESTION);
  763. $sql = "UPDATE $table SET
  764. picture = '".Database::escape_string($picture)."'
  765. WHERE c_id = $course_id AND id='".intval($questionId)."'";
  766. Database::query($sql);
  767. $documentId = add_document(
  768. $courseInfo,
  769. '/images/'.$picture,
  770. 'file',
  771. filesize($destination_path.'/'.$picture),
  772. $picture
  773. );
  774. if (!$documentId) {
  775. return false;
  776. }
  777. return api_item_property_update(
  778. $courseInfo,
  779. TOOL_DOCUMENT,
  780. $documentId,
  781. 'DocumentAdded',
  782. api_get_user_id()
  783. );
  784. }
  785. /**
  786. * Saves the picture coming from POST into a temporary file
  787. * Temporary pictures are used when we don't want to save a picture right after a form submission.
  788. * For example, if we first show a confirmation box.
  789. *
  790. * @author Olivier Brouckaert
  791. *
  792. * @param string $picture - temporary path of the picture to move
  793. * @param string $pictureName - Name of the picture
  794. */
  795. public function setTmpPicture($picture, $pictureName)
  796. {
  797. $picturePath = $this->getHotSpotFolderInCourse();
  798. $pictureName = explode('.', $pictureName);
  799. $Extension = $pictureName[sizeof($pictureName) - 1];
  800. // saves the picture into a temporary file
  801. @move_uploaded_file($picture, $picturePath.'/tmp.'.$Extension);
  802. }
  803. /**
  804. * Set title.
  805. *
  806. * @param string $title
  807. */
  808. public function setTitle($title)
  809. {
  810. $this->question = $title;
  811. }
  812. /**
  813. * Sets extra info.
  814. *
  815. * @param string $extra
  816. */
  817. public function setExtra($extra)
  818. {
  819. $this->extra = $extra;
  820. }
  821. /**
  822. * updates the question in the data base
  823. * if an exercise ID is provided, we add that exercise ID into the exercise list.
  824. *
  825. * @author Olivier Brouckaert
  826. *
  827. * @param Exercise $exercise
  828. */
  829. public function save($exercise)
  830. {
  831. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  832. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  833. $em = Database::getManager();
  834. $exerciseId = $exercise->id;
  835. $id = $this->id;
  836. $question = $this->question;
  837. $description = $this->description;
  838. $weighting = $this->weighting;
  839. $position = $this->position;
  840. $type = $this->type;
  841. $picture = $this->picture;
  842. $level = $this->level;
  843. $extra = $this->extra;
  844. $c_id = $this->course['real_id'];
  845. $categoryId = $this->category;
  846. // question already exists
  847. if (!empty($id)) {
  848. $params = [
  849. 'question' => $question,
  850. 'description' => $description,
  851. 'ponderation' => $weighting,
  852. 'position' => $position,
  853. 'type' => $type,
  854. 'picture' => $picture,
  855. 'extra' => $extra,
  856. 'level' => $level,
  857. ];
  858. if ($exercise->questionFeedbackEnabled) {
  859. $params['feedback'] = $this->feedback;
  860. }
  861. Database::update(
  862. $TBL_QUESTIONS,
  863. $params,
  864. ['c_id = ? AND id = ?' => [$c_id, $id]]
  865. );
  866. Event::addEvent(
  867. LOG_QUESTION_UPDATED,
  868. LOG_QUESTION_ID,
  869. $this->iid
  870. );
  871. $this->saveCategory($categoryId);
  872. if (!empty($exerciseId)) {
  873. api_item_property_update(
  874. $this->course,
  875. TOOL_QUIZ,
  876. $id,
  877. 'QuizQuestionUpdated',
  878. api_get_user_id()
  879. );
  880. }
  881. if (api_get_setting('search_enabled') === 'true') {
  882. $this->search_engine_edit($exerciseId);
  883. }
  884. } else {
  885. // Creates a new question
  886. $sql = "SELECT max(position)
  887. FROM $TBL_QUESTIONS as question,
  888. $TBL_EXERCISE_QUESTION as test_question
  889. WHERE
  890. question.id = test_question.question_id AND
  891. test_question.exercice_id = ".$exerciseId." AND
  892. question.c_id = $c_id AND
  893. test_question.c_id = $c_id ";
  894. $result = Database::query($sql);
  895. $current_position = Database::result($result, 0, 0);
  896. $this->updatePosition($current_position + 1);
  897. $position = $this->position;
  898. $params = [
  899. 'c_id' => $c_id,
  900. 'question' => $question,
  901. 'description' => $description,
  902. 'ponderation' => $weighting,
  903. 'position' => $position,
  904. 'type' => $type,
  905. 'picture' => $picture,
  906. 'extra' => $extra,
  907. 'level' => $level,
  908. ];
  909. if ($exercise->questionFeedbackEnabled) {
  910. $params['feedback'] = $this->feedback;
  911. }
  912. $this->id = Database::insert($TBL_QUESTIONS, $params);
  913. if ($this->id) {
  914. $sql = "UPDATE $TBL_QUESTIONS SET id = iid WHERE iid = {$this->id}";
  915. Database::query($sql);
  916. Event::addEvent(
  917. LOG_QUESTION_CREATED,
  918. LOG_QUESTION_ID,
  919. $this->id
  920. );
  921. api_item_property_update(
  922. $this->course,
  923. TOOL_QUIZ,
  924. $this->id,
  925. 'QuizQuestionAdded',
  926. api_get_user_id()
  927. );
  928. // If hotspot, create first answer
  929. if ($type == HOT_SPOT || $type == HOT_SPOT_ORDER) {
  930. $quizAnswer = new CQuizAnswer();
  931. $quizAnswer
  932. ->setCId($c_id)
  933. ->setQuestionId($this->id)
  934. ->setAnswer('')
  935. ->setPonderation(10)
  936. ->setPosition(1)
  937. ->setHotspotCoordinates('0;0|0|0')
  938. ->setHotspotType('square');
  939. $em->persist($quizAnswer);
  940. $em->flush();
  941. $id = $quizAnswer->getIid();
  942. if ($id) {
  943. $quizAnswer
  944. ->setId($id)
  945. ->setIdAuto($id);
  946. $em->merge($quizAnswer);
  947. $em->flush();
  948. }
  949. }
  950. if ($type == HOT_SPOT_DELINEATION) {
  951. $quizAnswer = new CQuizAnswer();
  952. $quizAnswer
  953. ->setCId($c_id)
  954. ->setQuestionId($this->id)
  955. ->setAnswer('')
  956. ->setPonderation(10)
  957. ->setPosition(1)
  958. ->setHotspotCoordinates('0;0|0|0')
  959. ->setHotspotType('delineation');
  960. $em->persist($quizAnswer);
  961. $em->flush();
  962. $id = $quizAnswer->getIid();
  963. if ($id) {
  964. $quizAnswer
  965. ->setId($id)
  966. ->setIdAuto($id);
  967. $em->merge($quizAnswer);
  968. $em->flush();
  969. }
  970. }
  971. if (api_get_setting('search_enabled') === 'true') {
  972. $this->search_engine_edit($exerciseId, true);
  973. }
  974. }
  975. }
  976. // if the question is created in an exercise
  977. if (!empty($exerciseId)) {
  978. // adds the exercise into the exercise list of this question
  979. $this->addToList($exerciseId, true);
  980. }
  981. }
  982. /**
  983. * @param int $exerciseId
  984. * @param bool $addQs
  985. * @param bool $rmQs
  986. */
  987. public function search_engine_edit(
  988. $exerciseId,
  989. $addQs = false,
  990. $rmQs = false
  991. ) {
  992. // update search engine and its values table if enabled
  993. if (!empty($exerciseId) && api_get_setting('search_enabled') == 'true' &&
  994. extension_loaded('xapian')
  995. ) {
  996. $course_id = api_get_course_id();
  997. // get search_did
  998. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  999. if ($addQs || $rmQs) {
  1000. //there's only one row per question on normal db and one document per question on search engine db
  1001. $sql = 'SELECT * FROM %s
  1002. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_second_level=%s LIMIT 1';
  1003. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  1004. } else {
  1005. $sql = 'SELECT * FROM %s
  1006. WHERE course_code=\'%s\' AND tool_id=\'%s\'
  1007. AND ref_id_high_level=%s AND ref_id_second_level=%s LIMIT 1';
  1008. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id);
  1009. }
  1010. $res = Database::query($sql);
  1011. if (Database::num_rows($res) > 0 || $addQs) {
  1012. $di = new ChamiloIndexer();
  1013. if ($addQs) {
  1014. $question_exercises = [(int) $exerciseId];
  1015. } else {
  1016. $question_exercises = [];
  1017. }
  1018. isset($_POST['language']) ? $lang = Database::escape_string($_POST['language']) : $lang = 'english';
  1019. $di->connectDb(null, null, $lang);
  1020. // retrieve others exercise ids
  1021. $se_ref = Database::fetch_array($res);
  1022. $se_doc = $di->get_document((int) $se_ref['search_did']);
  1023. if ($se_doc !== false) {
  1024. if (($se_doc_data = $di->get_document_data($se_doc)) !== false) {
  1025. $se_doc_data = UnserializeApi::unserialize(
  1026. 'not_allowed_classes',
  1027. $se_doc_data
  1028. );
  1029. if (isset($se_doc_data[SE_DATA]['type']) &&
  1030. $se_doc_data[SE_DATA]['type'] == SE_DOCTYPE_EXERCISE_QUESTION
  1031. ) {
  1032. if (isset($se_doc_data[SE_DATA]['exercise_ids']) &&
  1033. is_array($se_doc_data[SE_DATA]['exercise_ids'])
  1034. ) {
  1035. foreach ($se_doc_data[SE_DATA]['exercise_ids'] as $old_value) {
  1036. if (!in_array($old_value, $question_exercises)) {
  1037. $question_exercises[] = $old_value;
  1038. }
  1039. }
  1040. }
  1041. }
  1042. }
  1043. }
  1044. if ($rmQs) {
  1045. while (($key = array_search($exerciseId, $question_exercises)) !== false) {
  1046. unset($question_exercises[$key]);
  1047. }
  1048. }
  1049. // build the chunk to index
  1050. $ic_slide = new IndexableChunk();
  1051. $ic_slide->addValue("title", $this->question);
  1052. $ic_slide->addCourseId($course_id);
  1053. $ic_slide->addToolId(TOOL_QUIZ);
  1054. $xapian_data = [
  1055. SE_COURSE_ID => $course_id,
  1056. SE_TOOL_ID => TOOL_QUIZ,
  1057. SE_DATA => [
  1058. 'type' => SE_DOCTYPE_EXERCISE_QUESTION,
  1059. 'exercise_ids' => $question_exercises,
  1060. 'question_id' => (int) $this->id,
  1061. ],
  1062. SE_USER => (int) api_get_user_id(),
  1063. ];
  1064. $ic_slide->xapian_data = serialize($xapian_data);
  1065. $ic_slide->addValue("content", $this->description);
  1066. //TODO: index answers, see also form validation on question_admin.inc.php
  1067. $di->remove_document($se_ref['search_did']);
  1068. $di->addChunk($ic_slide);
  1069. //index and return search engine document id
  1070. if (!empty($question_exercises)) { // if empty there is nothing to index
  1071. $did = $di->index();
  1072. unset($di);
  1073. }
  1074. if ($did || $rmQs) {
  1075. // save it to db
  1076. if ($addQs || $rmQs) {
  1077. $sql = "DELETE FROM %s
  1078. WHERE course_code = '%s' AND tool_id = '%s' AND ref_id_second_level = '%s'";
  1079. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  1080. } else {
  1081. $sql = "DELETE FROM %S
  1082. WHERE
  1083. course_code = '%s'
  1084. AND tool_id = '%s'
  1085. AND tool_id = '%s'
  1086. AND ref_id_high_level = '%s'
  1087. AND ref_id_second_level = '%s'";
  1088. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id);
  1089. }
  1090. Database::query($sql);
  1091. if ($rmQs) {
  1092. if (!empty($question_exercises)) {
  1093. $sql = "INSERT INTO %s (
  1094. id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did
  1095. )
  1096. VALUES (
  1097. NULL, '%s', '%s', %s, %s, %s
  1098. )";
  1099. $sql = sprintf(
  1100. $sql,
  1101. $tbl_se_ref,
  1102. $course_id,
  1103. TOOL_QUIZ,
  1104. array_shift($question_exercises),
  1105. $this->id,
  1106. $did
  1107. );
  1108. Database::query($sql);
  1109. }
  1110. } else {
  1111. $sql = "INSERT INTO %s (
  1112. id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did
  1113. )
  1114. VALUES (
  1115. NULL , '%s', '%s', %s, %s, %s
  1116. )";
  1117. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $exerciseId, $this->id, $did);
  1118. Database::query($sql);
  1119. }
  1120. }
  1121. }
  1122. }
  1123. }
  1124. /**
  1125. * adds an exercise into the exercise list.
  1126. *
  1127. * @author Olivier Brouckaert
  1128. *
  1129. * @param int $exerciseId - exercise ID
  1130. * @param bool $fromSave - from $this->save() or not
  1131. */
  1132. public function addToList($exerciseId, $fromSave = false)
  1133. {
  1134. $exerciseRelQuestionTable = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1135. $id = (int) $this->id;
  1136. $exerciseId = (int) $exerciseId;
  1137. // checks if the exercise ID is not in the list
  1138. if (!empty($exerciseId) && !in_array($exerciseId, $this->exerciseList)) {
  1139. $this->exerciseList[] = $exerciseId;
  1140. $courseId = isset($this->course['real_id']) ? $this->course['real_id'] : 0;
  1141. $newExercise = new Exercise($courseId);
  1142. $newExercise->read($exerciseId, false);
  1143. $count = $newExercise->getQuestionCount();
  1144. $count++;
  1145. $sql = "INSERT INTO $exerciseRelQuestionTable (c_id, question_id, exercice_id, question_order)
  1146. VALUES ({$this->course['real_id']}, ".$id.", ".$exerciseId.", '$count')";
  1147. Database::query($sql);
  1148. // we do not want to reindex if we had just saved adnd indexed the question
  1149. if (!$fromSave) {
  1150. $this->search_engine_edit($exerciseId, true);
  1151. }
  1152. }
  1153. }
  1154. /**
  1155. * removes an exercise from the exercise list.
  1156. *
  1157. * @author Olivier Brouckaert
  1158. *
  1159. * @param int $exerciseId - exercise ID
  1160. * @param int $courseId
  1161. *
  1162. * @return bool - true if removed, otherwise false
  1163. */
  1164. public function removeFromList($exerciseId, $courseId = 0)
  1165. {
  1166. $table = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1167. $id = (int) $this->id;
  1168. $exerciseId = (int) $exerciseId;
  1169. // searches the position of the exercise ID in the list
  1170. $pos = array_search($exerciseId, $this->exerciseList);
  1171. $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
  1172. // exercise not found
  1173. if ($pos === false) {
  1174. return false;
  1175. } else {
  1176. // deletes the position in the array containing the wanted exercise ID
  1177. unset($this->exerciseList[$pos]);
  1178. //update order of other elements
  1179. $sql = "SELECT question_order
  1180. FROM $table
  1181. WHERE
  1182. c_id = $courseId AND
  1183. question_id = $id AND
  1184. exercice_id = $exerciseId";
  1185. $res = Database::query($sql);
  1186. if (Database::num_rows($res) > 0) {
  1187. $row = Database::fetch_array($res);
  1188. if (!empty($row['question_order'])) {
  1189. $sql = "UPDATE $table
  1190. SET question_order = question_order-1
  1191. WHERE
  1192. c_id = $courseId AND
  1193. exercice_id = $exerciseId AND
  1194. question_order > ".$row['question_order'];
  1195. Database::query($sql);
  1196. }
  1197. }
  1198. $sql = "DELETE FROM $table
  1199. WHERE
  1200. c_id = $courseId AND
  1201. question_id = $id AND
  1202. exercice_id = $exerciseId";
  1203. Database::query($sql);
  1204. return true;
  1205. }
  1206. }
  1207. /**
  1208. * Deletes a question from the database
  1209. * the parameter tells if the question is removed from all exercises (value = 0),
  1210. * or just from one exercise (value = exercise ID).
  1211. *
  1212. * @author Olivier Brouckaert
  1213. *
  1214. * @param int $deleteFromEx - exercise ID if the question is only removed from one exercise
  1215. *
  1216. * @return bool
  1217. */
  1218. public function delete($deleteFromEx = 0)
  1219. {
  1220. if (empty($this->course)) {
  1221. return false;
  1222. }
  1223. $courseId = $this->course['real_id'];
  1224. if (empty($courseId)) {
  1225. return false;
  1226. }
  1227. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1228. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1229. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  1230. $TBL_QUIZ_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  1231. $id = (int) $this->id;
  1232. // if the question must be removed from all exercises
  1233. if (!$deleteFromEx) {
  1234. //update the question_order of each question to avoid inconsistencies
  1235. $sql = "SELECT exercice_id, question_order
  1236. FROM $TBL_EXERCISE_QUESTION
  1237. WHERE c_id = $courseId AND question_id = ".$id;
  1238. $res = Database::query($sql);
  1239. if (Database::num_rows($res) > 0) {
  1240. while ($row = Database::fetch_array($res)) {
  1241. if (!empty($row['question_order'])) {
  1242. $sql = "UPDATE $TBL_EXERCISE_QUESTION
  1243. SET question_order = question_order-1
  1244. WHERE
  1245. c_id = $courseId AND
  1246. exercice_id = ".intval($row['exercice_id'])." AND
  1247. question_order > ".$row['question_order'];
  1248. Database::query($sql);
  1249. }
  1250. }
  1251. }
  1252. $sql = "DELETE FROM $TBL_EXERCISE_QUESTION
  1253. WHERE c_id = $courseId AND question_id = ".$id;
  1254. Database::query($sql);
  1255. $sql = "DELETE FROM $TBL_QUESTIONS
  1256. WHERE c_id = $courseId AND id = ".$id;
  1257. Database::query($sql);
  1258. $sql = "DELETE FROM $TBL_REPONSES
  1259. WHERE c_id = $courseId AND question_id = ".$id;
  1260. Database::query($sql);
  1261. // remove the category of this question in the question_rel_category table
  1262. $sql = "DELETE FROM $TBL_QUIZ_QUESTION_REL_CATEGORY
  1263. WHERE
  1264. c_id = $courseId AND
  1265. question_id = ".$id;
  1266. Database::query($sql);
  1267. api_item_property_update(
  1268. $this->course,
  1269. TOOL_QUIZ,
  1270. $id,
  1271. 'QuizQuestionDeleted',
  1272. api_get_user_id()
  1273. );
  1274. $this->removePicture();
  1275. } else {
  1276. // just removes the exercise from the list
  1277. $this->removeFromList($deleteFromEx, $courseId);
  1278. if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) {
  1279. // disassociate question with this exercise
  1280. $this->search_engine_edit($deleteFromEx, false, true);
  1281. }
  1282. api_item_property_update(
  1283. $this->course,
  1284. TOOL_QUIZ,
  1285. $id,
  1286. 'QuizQuestionDeleted',
  1287. api_get_user_id()
  1288. );
  1289. }
  1290. return true;
  1291. }
  1292. /**
  1293. * Duplicates the question.
  1294. *
  1295. * @author Olivier Brouckaert
  1296. *
  1297. * @param array $courseInfo Course info of the destination course
  1298. *
  1299. * @return false|string ID of the new question
  1300. */
  1301. public function duplicate($courseInfo = [])
  1302. {
  1303. $courseInfo = empty($courseInfo) ? $this->course : $courseInfo;
  1304. if (empty($courseInfo)) {
  1305. return false;
  1306. }
  1307. $questionTable = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1308. $TBL_QUESTION_OPTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1309. $question = $this->question;
  1310. $description = $this->description;
  1311. $weighting = $this->weighting;
  1312. $position = $this->position;
  1313. $type = $this->type;
  1314. $level = (int) $this->level;
  1315. $extra = $this->extra;
  1316. // Using the same method used in the course copy to transform URLs
  1317. if ($this->course['id'] != $courseInfo['id']) {
  1318. $description = DocumentManager::replaceUrlWithNewCourseCode(
  1319. $description,
  1320. $this->course['code'],
  1321. $courseInfo['id']
  1322. );
  1323. $question = DocumentManager::replaceUrlWithNewCourseCode(
  1324. $question,
  1325. $this->course['code'],
  1326. $courseInfo['id']
  1327. );
  1328. }
  1329. $course_id = $courseInfo['real_id'];
  1330. // Read the source options
  1331. $options = self::readQuestionOption($this->id, $this->course['real_id']);
  1332. // Inserting in the new course db / or the same course db
  1333. $params = [
  1334. 'c_id' => $course_id,
  1335. 'question' => $question,
  1336. 'description' => $description,
  1337. 'ponderation' => $weighting,
  1338. 'position' => $position,
  1339. 'type' => $type,
  1340. 'level' => $level,
  1341. 'extra' => $extra,
  1342. ];
  1343. $newQuestionId = Database::insert($questionTable, $params);
  1344. if ($newQuestionId) {
  1345. $sql = "UPDATE $questionTable
  1346. SET id = iid
  1347. WHERE iid = $newQuestionId";
  1348. Database::query($sql);
  1349. if (!empty($options)) {
  1350. // Saving the quiz_options
  1351. foreach ($options as $item) {
  1352. $item['question_id'] = $newQuestionId;
  1353. $item['c_id'] = $course_id;
  1354. unset($item['id']);
  1355. unset($item['iid']);
  1356. $id = Database::insert($TBL_QUESTION_OPTIONS, $item);
  1357. if ($id) {
  1358. $sql = "UPDATE $TBL_QUESTION_OPTIONS
  1359. SET id = iid
  1360. WHERE iid = $id";
  1361. Database::query($sql);
  1362. }
  1363. }
  1364. }
  1365. // Duplicates the picture of the hotspot
  1366. $this->exportPicture($newQuestionId, $courseInfo);
  1367. }
  1368. return $newQuestionId;
  1369. }
  1370. /**
  1371. * @return string
  1372. */
  1373. public function get_question_type_name()
  1374. {
  1375. $key = self::$questionTypes[$this->type];
  1376. return get_lang($key[1]);
  1377. }
  1378. /**
  1379. * @param string $type
  1380. */
  1381. public static function get_question_type($type)
  1382. {
  1383. if ($type == ORAL_EXPRESSION && api_get_setting('enable_record_audio') !== 'true') {
  1384. return null;
  1385. }
  1386. return self::$questionTypes[$type];
  1387. }
  1388. /**
  1389. * @return array
  1390. */
  1391. public static function getQuestionTypeList()
  1392. {
  1393. if (api_get_setting('enable_record_audio') !== 'true') {
  1394. self::$questionTypes[ORAL_EXPRESSION] = null;
  1395. unset(self::$questionTypes[ORAL_EXPRESSION]);
  1396. }
  1397. if (api_get_setting('enable_quiz_scenario') !== 'true') {
  1398. self::$questionTypes[HOT_SPOT_DELINEATION] = null;
  1399. unset(self::$questionTypes[HOT_SPOT_DELINEATION]);
  1400. }
  1401. return self::$questionTypes;
  1402. }
  1403. /**
  1404. * Returns an instance of the class corresponding to the type.
  1405. *
  1406. * @param int $type the type of the question
  1407. *
  1408. * @return $this instance of a Question subclass (or of Questionc class by default)
  1409. */
  1410. public static function getInstance($type)
  1411. {
  1412. if (!is_null($type)) {
  1413. list($fileName, $className) = self::get_question_type($type);
  1414. if (!empty($fileName)) {
  1415. include_once $fileName;
  1416. if (class_exists($className)) {
  1417. return new $className();
  1418. } else {
  1419. echo 'Can\'t instanciate class '.$className.' of type '.$type;
  1420. }
  1421. }
  1422. }
  1423. return null;
  1424. }
  1425. /**
  1426. * Creates the form to create / edit a question
  1427. * A subclass can redefine this function to add fields...
  1428. *
  1429. * @param FormValidator $form
  1430. * @param Exercise $exercise
  1431. */
  1432. public function createForm(&$form, $exercise)
  1433. {
  1434. echo '<style>
  1435. .media { display:none;}
  1436. </style>';
  1437. // question name
  1438. if (api_get_configuration_value('save_titles_as_html')) {
  1439. $editorConfig = ['ToolbarSet' => 'TitleAsHtml'];
  1440. $form->addHtmlEditor(
  1441. 'questionName',
  1442. get_lang('Question'),
  1443. false,
  1444. false,
  1445. $editorConfig,
  1446. true
  1447. );
  1448. } else {
  1449. $form->addElement('text', 'questionName', get_lang('Question'));
  1450. }
  1451. $form->addRule('questionName', get_lang('GiveQuestion'), 'required');
  1452. // default content
  1453. $isContent = isset($_REQUEST['isContent']) ? (int) $_REQUEST['isContent'] : null;
  1454. // Question type
  1455. $answerType = isset($_REQUEST['answerType']) ? (int) $_REQUEST['answerType'] : null;
  1456. $form->addElement('hidden', 'answerType', $answerType);
  1457. // html editor
  1458. $editorConfig = [
  1459. 'ToolbarSet' => 'TestQuestionDescription',
  1460. 'Height' => '150',
  1461. ];
  1462. if (!api_is_allowed_to_edit(null, true)) {
  1463. $editorConfig['UserStatus'] = 'student';
  1464. }
  1465. $form->addButtonAdvancedSettings('advanced_params');
  1466. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  1467. $form->addHtmlEditor(
  1468. 'questionDescription',
  1469. get_lang('QuestionDescription'),
  1470. false,
  1471. false,
  1472. $editorConfig
  1473. );
  1474. if ($this->type != MEDIA_QUESTION) {
  1475. // Advanced parameters
  1476. $select_level = self::get_default_levels();
  1477. $form->addElement(
  1478. 'select',
  1479. 'questionLevel',
  1480. get_lang('Difficulty'),
  1481. $select_level
  1482. );
  1483. // Categories
  1484. $tabCat = TestCategory::getCategoriesIdAndName();
  1485. $form->addElement(
  1486. 'select',
  1487. 'questionCategory',
  1488. get_lang('Category'),
  1489. $tabCat
  1490. );
  1491. global $text;
  1492. switch ($this->type) {
  1493. case UNIQUE_ANSWER:
  1494. $buttonGroup = [];
  1495. $buttonGroup[] = $form->addButtonSave(
  1496. $text,
  1497. 'submitQuestion',
  1498. true
  1499. );
  1500. $buttonGroup[] = $form->addButton(
  1501. 'convertAnswer',
  1502. get_lang('ConvertToMultipleAnswer'),
  1503. 'dot-circle-o',
  1504. 'default',
  1505. null,
  1506. null,
  1507. null,
  1508. true
  1509. );
  1510. $form->addGroup($buttonGroup);
  1511. break;
  1512. case MULTIPLE_ANSWER:
  1513. $buttonGroup = [];
  1514. $buttonGroup[] = $form->addButtonSave(
  1515. $text,
  1516. 'submitQuestion',
  1517. true
  1518. );
  1519. $buttonGroup[] = $form->addButton(
  1520. 'convertAnswer',
  1521. get_lang('ConvertToUniqueAnswer'),
  1522. 'check-square-o',
  1523. 'default',
  1524. null,
  1525. null,
  1526. null,
  1527. true
  1528. );
  1529. $form->addGroup($buttonGroup);
  1530. break;
  1531. }
  1532. //Medias
  1533. //$course_medias = self::prepare_course_media_select(api_get_course_int_id());
  1534. //$form->addElement('select', 'parent_id', get_lang('AttachToMedia'), $course_medias);
  1535. }
  1536. $form->addElement('html', '</div>');
  1537. if (!isset($_GET['fromExercise'])) {
  1538. switch ($answerType) {
  1539. case 1:
  1540. $this->question = get_lang('DefaultUniqueQuestion');
  1541. break;
  1542. case 2:
  1543. $this->question = get_lang('DefaultMultipleQuestion');
  1544. break;
  1545. case 3:
  1546. $this->question = get_lang('DefaultFillBlankQuestion');
  1547. break;
  1548. case 4:
  1549. $this->question = get_lang('DefaultMathingQuestion');
  1550. break;
  1551. case 5:
  1552. $this->question = get_lang('DefaultOpenQuestion');
  1553. break;
  1554. case 9:
  1555. $this->question = get_lang('DefaultMultipleQuestion');
  1556. break;
  1557. }
  1558. }
  1559. if (!is_null($exercise)) {
  1560. if ($exercise->questionFeedbackEnabled && $this->showFeedback($exercise)) {
  1561. $form->addTextarea('feedback', get_lang('FeedbackIfNotCorrect'));
  1562. }
  1563. }
  1564. // default values
  1565. $defaults = [];
  1566. $defaults['questionName'] = $this->question;
  1567. $defaults['questionDescription'] = $this->description;
  1568. $defaults['questionLevel'] = $this->level;
  1569. $defaults['questionCategory'] = $this->category;
  1570. $defaults['feedback'] = $this->feedback;
  1571. // Came from he question pool
  1572. if (isset($_GET['fromExercise'])) {
  1573. $form->setDefaults($defaults);
  1574. }
  1575. if (!isset($_GET['newQuestion']) || $isContent) {
  1576. $form->setDefaults($defaults);
  1577. }
  1578. /*if (!empty($_REQUEST['myid'])) {
  1579. $form->setDefaults($defaults);
  1580. } else {
  1581. if ($isContent == 1) {
  1582. $form->setDefaults($defaults);
  1583. }
  1584. }*/
  1585. }
  1586. /**
  1587. * function which process the creation of questions.
  1588. *
  1589. * @param FormValidator $form
  1590. * @param Exercise $exercise
  1591. */
  1592. public function processCreation($form, $exercise)
  1593. {
  1594. $this->updateTitle($form->getSubmitValue('questionName'));
  1595. $this->updateDescription($form->getSubmitValue('questionDescription'));
  1596. $this->updateLevel($form->getSubmitValue('questionLevel'));
  1597. $this->updateCategory($form->getSubmitValue('questionCategory'));
  1598. $this->setFeedback($form->getSubmitValue('feedback'));
  1599. //Save normal question if NOT media
  1600. if ($this->type != MEDIA_QUESTION) {
  1601. $this->save($exercise);
  1602. // modify the exercise
  1603. $exercise->addToList($this->id);
  1604. $exercise->update_question_positions();
  1605. }
  1606. }
  1607. /**
  1608. * abstract function which creates the form to create / edit the answers of the question.
  1609. *
  1610. * @param FormValidator $form
  1611. */
  1612. abstract public function createAnswersForm($form);
  1613. /**
  1614. * abstract function which process the creation of answers.
  1615. *
  1616. * @param FormValidator $form
  1617. * @param Exercise $exercise
  1618. */
  1619. abstract public function processAnswersCreation($form, $exercise);
  1620. /**
  1621. * Displays the menu of question types.
  1622. *
  1623. * @param Exercise $objExercise
  1624. */
  1625. public static function displayTypeMenu($objExercise)
  1626. {
  1627. $feedbackType = $objExercise->getFeedbackType();
  1628. $exerciseId = $objExercise->id;
  1629. // 1. by default we show all the question types
  1630. $questionTypeList = self::getQuestionTypeList();
  1631. if (!isset($feedbackType)) {
  1632. $feedbackType = 0;
  1633. }
  1634. switch ($feedbackType) {
  1635. case EXERCISE_FEEDBACK_TYPE_DIRECT:
  1636. $questionTypeList = [
  1637. UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER],
  1638. HOT_SPOT_DELINEATION => self::$questionTypes[HOT_SPOT_DELINEATION],
  1639. ];
  1640. break;
  1641. case EXERCISE_FEEDBACK_TYPE_POPUP:
  1642. $questionTypeList = [
  1643. UNIQUE_ANSWER => self::$questionTypes[UNIQUE_ANSWER],
  1644. MULTIPLE_ANSWER => self::$questionTypes[MULTIPLE_ANSWER],
  1645. DRAGGABLE => self::$questionTypes[DRAGGABLE],
  1646. HOT_SPOT_DELINEATION => self::$questionTypes[HOT_SPOT_DELINEATION],
  1647. CALCULATED_ANSWER => self::$questionTypes[CALCULATED_ANSWER],
  1648. ];
  1649. break;
  1650. default:
  1651. unset($questionTypeList[HOT_SPOT_DELINEATION]);
  1652. break;
  1653. }
  1654. echo '<div class="panel panel-default">';
  1655. echo '<div class="panel-body">';
  1656. echo '<ul class="question_menu">';
  1657. foreach ($questionTypeList as $i => $type) {
  1658. /** @var Question $type */
  1659. $type = new $type[1]();
  1660. $img = $type->getTypePicture();
  1661. $explanation = get_lang($type->getExplanation());
  1662. echo '<li>';
  1663. echo '<div class="icon-image">';
  1664. $icon = '<a href="admin.php?'.api_get_cidreq().'&newQuestion=yes&answerType='.$i.'">'.
  1665. Display::return_icon($img, $explanation, null, ICON_SIZE_BIG).'</a>';
  1666. if ($objExercise->force_edit_exercise_in_lp === false) {
  1667. if ($objExercise->exercise_was_added_in_lp == true) {
  1668. $img = pathinfo($img);
  1669. $img = $img['filename'].'_na.'.$img['extension'];
  1670. $icon = Display::return_icon($img, $explanation, null, ICON_SIZE_BIG);
  1671. }
  1672. }
  1673. echo $icon;
  1674. echo '</div>';
  1675. echo '</li>';
  1676. }
  1677. echo '<li>';
  1678. echo '<div class="icon_image_content">';
  1679. if ($objExercise->exercise_was_added_in_lp == true) {
  1680. echo Display::return_icon(
  1681. 'database_na.png',
  1682. get_lang('GetExistingQuestion'),
  1683. null,
  1684. ICON_SIZE_BIG
  1685. );
  1686. } else {
  1687. if (in_array($feedbackType, [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP])) {
  1688. echo $url = "<a href=\"question_pool.php?".api_get_cidreq()."&type=1&fromExercise=$exerciseId\">";
  1689. } else {
  1690. echo $url = '<a href="question_pool.php?'.api_get_cidreq().'&fromExercise='.$exerciseId.'">';
  1691. }
  1692. echo Display::return_icon(
  1693. 'database.png',
  1694. get_lang('GetExistingQuestion'),
  1695. null,
  1696. ICON_SIZE_BIG
  1697. );
  1698. }
  1699. echo '</a>';
  1700. echo '</div></li>';
  1701. echo '</ul>';
  1702. echo '</div>';
  1703. echo '</div>';
  1704. }
  1705. /**
  1706. * @param int $question_id
  1707. * @param string $name
  1708. * @param int $course_id
  1709. * @param int $position
  1710. *
  1711. * @return false|string
  1712. */
  1713. public static function saveQuestionOption($question_id, $name, $course_id, $position = 0)
  1714. {
  1715. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1716. $params['question_id'] = (int) $question_id;
  1717. $params['name'] = $name;
  1718. $params['position'] = $position;
  1719. $params['c_id'] = $course_id;
  1720. $result = self::readQuestionOption($question_id, $course_id);
  1721. $last_id = Database::insert($table, $params);
  1722. if ($last_id) {
  1723. $sql = "UPDATE $table SET id = iid WHERE iid = $last_id";
  1724. Database::query($sql);
  1725. }
  1726. return $last_id;
  1727. }
  1728. /**
  1729. * @param int $question_id
  1730. * @param int $course_id
  1731. */
  1732. public static function deleteAllQuestionOptions($question_id, $course_id)
  1733. {
  1734. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1735. Database::delete(
  1736. $table,
  1737. [
  1738. 'c_id = ? AND question_id = ?' => [
  1739. $course_id,
  1740. $question_id,
  1741. ],
  1742. ]
  1743. );
  1744. }
  1745. /**
  1746. * @param int $id
  1747. * @param array $params
  1748. * @param int $course_id
  1749. *
  1750. * @return bool|int
  1751. */
  1752. public static function updateQuestionOption($id, $params, $course_id)
  1753. {
  1754. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1755. $result = Database::update(
  1756. $table,
  1757. $params,
  1758. ['c_id = ? AND id = ?' => [$course_id, $id]]
  1759. );
  1760. return $result;
  1761. }
  1762. /**
  1763. * @param int $question_id
  1764. * @param int $course_id
  1765. *
  1766. * @return array
  1767. */
  1768. public static function readQuestionOption($question_id, $course_id)
  1769. {
  1770. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1771. $result = Database::select(
  1772. '*',
  1773. $table,
  1774. [
  1775. 'where' => [
  1776. 'c_id = ? AND question_id = ?' => [
  1777. $course_id,
  1778. $question_id,
  1779. ],
  1780. ],
  1781. 'order' => 'id ASC',
  1782. ]
  1783. );
  1784. return $result;
  1785. }
  1786. /**
  1787. * Shows question title an description.
  1788. *
  1789. * @param Exercise $exercise
  1790. * @param int $counter
  1791. * @param array $score
  1792. *
  1793. * @return string HTML string with the header of the question (before the answers table)
  1794. */
  1795. public function return_header(Exercise $exercise, $counter = null, $score = [])
  1796. {
  1797. $counterLabel = '';
  1798. if (!empty($counter)) {
  1799. $counterLabel = (int) $counter;
  1800. }
  1801. $scoreLabel = get_lang('Wrong');
  1802. if (in_array($exercise->results_disabled, [
  1803. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  1804. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  1805. ])
  1806. ) {
  1807. $scoreLabel = get_lang('QuizWrongAnswerHereIsTheCorrectOne');
  1808. }
  1809. $class = 'error';
  1810. if (isset($score['pass']) && $score['pass'] == true) {
  1811. $scoreLabel = get_lang('Correct');
  1812. if (in_array($exercise->results_disabled, [
  1813. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  1814. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  1815. ])
  1816. ) {
  1817. $scoreLabel = get_lang('CorrectAnswer');
  1818. }
  1819. $class = 'success';
  1820. }
  1821. switch ($this->type) {
  1822. case FREE_ANSWER:
  1823. case ORAL_EXPRESSION:
  1824. case ANNOTATION:
  1825. $score['revised'] = isset($score['revised']) ? $score['revised'] : false;
  1826. if ($score['revised'] == true) {
  1827. $scoreLabel = get_lang('Revised');
  1828. $class = '';
  1829. } else {
  1830. $scoreLabel = get_lang('NotRevised');
  1831. $class = 'warning';
  1832. if (isset($score['weight'])) {
  1833. $weight = float_format($score['weight'], 1);
  1834. $score['result'] = ' ? / '.$weight;
  1835. }
  1836. $model = ExerciseLib::getCourseScoreModel();
  1837. if (!empty($model)) {
  1838. $score['result'] = ' ? ';
  1839. }
  1840. $hide = api_get_configuration_value('hide_free_question_score');
  1841. if ($hide === true) {
  1842. $score['result'] = '-';
  1843. }
  1844. }
  1845. break;
  1846. case UNIQUE_ANSWER:
  1847. if (in_array($exercise->results_disabled, [
  1848. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  1849. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  1850. ])
  1851. ) {
  1852. if (isset($score['user_answered'])) {
  1853. if ($score['user_answered'] === false) {
  1854. $scoreLabel = get_lang('Unanswered');
  1855. $class = 'info';
  1856. }
  1857. }
  1858. }
  1859. break;
  1860. }
  1861. // display question category, if any
  1862. $header = '';
  1863. if ($exercise->display_category_name) {
  1864. $header = TestCategory::returnCategoryAndTitle($this->id);
  1865. }
  1866. $show_media = '';
  1867. if ($show_media) {
  1868. $header .= $this->show_media_content();
  1869. }
  1870. $scoreCurrent = [
  1871. 'used' => isset($score['score']) ? $score['score'] : '',
  1872. 'missing' => isset($score['weight']) ? $score['weight'] : '',
  1873. ];
  1874. $header .= Display::page_subheader2($counterLabel.'. '.$this->question);
  1875. // dont display score for certainty degree questions
  1876. if ($this->type != MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  1877. if (isset($score['result'])) {
  1878. if (in_array($exercise->results_disabled, [
  1879. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  1880. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  1881. ])
  1882. ) {
  1883. $score['result'] = null;
  1884. }
  1885. $header .= $exercise->getQuestionRibbon($class, $scoreLabel, $score['result'], $scoreCurrent);
  1886. }
  1887. }
  1888. if ($this->type != READING_COMPREHENSION) {
  1889. // Do not show the description (the text to read) if the question is of type READING_COMPREHENSION
  1890. $header .= Display::div(
  1891. $this->description,
  1892. ['class' => 'question_description']
  1893. );
  1894. } else {
  1895. if ($score['pass'] == true) {
  1896. $message = Display::div(
  1897. sprintf(
  1898. get_lang('ReadingQuestionCongratsSpeedXReachedForYWords'),
  1899. ReadingComprehension::$speeds[$this->level],
  1900. $this->getWordsCount()
  1901. )
  1902. );
  1903. } else {
  1904. $message = Display::div(
  1905. sprintf(
  1906. get_lang('ReadingQuestionCongratsSpeedXNotReachedForYWords'),
  1907. ReadingComprehension::$speeds[$this->level],
  1908. $this->getWordsCount()
  1909. )
  1910. );
  1911. }
  1912. $header .= $message.'<br />';
  1913. }
  1914. if (isset($score['pass']) && $score['pass'] === false) {
  1915. if ($this->showFeedback($exercise)) {
  1916. $header .= $this->returnFormatFeedback();
  1917. }
  1918. }
  1919. return $header;
  1920. }
  1921. /**
  1922. * @deprecated
  1923. * Create a question from a set of parameters.
  1924. *
  1925. * @param int Quiz ID
  1926. * @param string Question name
  1927. * @param int Maximum result for the question
  1928. * @param int Type of question (see constants at beginning of question.class.php)
  1929. * @param int Question level/category
  1930. * @param string $quiz_id
  1931. */
  1932. public function create_question(
  1933. $quiz_id,
  1934. $question_name,
  1935. $question_description = '',
  1936. $max_score = 0,
  1937. $type = 1,
  1938. $level = 1
  1939. ) {
  1940. $course_id = api_get_course_int_id();
  1941. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1942. $tbl_quiz_rel_question = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1943. $quiz_id = intval($quiz_id);
  1944. $max_score = (float) $max_score;
  1945. $type = intval($type);
  1946. $level = intval($level);
  1947. // Get the max position
  1948. $sql = "SELECT max(position) as max_position
  1949. FROM $tbl_quiz_question q
  1950. INNER JOIN $tbl_quiz_rel_question r
  1951. ON
  1952. q.id = r.question_id AND
  1953. exercice_id = $quiz_id AND
  1954. q.c_id = $course_id AND
  1955. r.c_id = $course_id";
  1956. $rs_max = Database::query($sql);
  1957. $row_max = Database::fetch_object($rs_max);
  1958. $max_position = $row_max->max_position + 1;
  1959. $params = [
  1960. 'c_id' => $course_id,
  1961. 'question' => $question_name,
  1962. 'description' => $question_description,
  1963. 'ponderation' => $max_score,
  1964. 'position' => $max_position,
  1965. 'type' => $type,
  1966. 'level' => $level,
  1967. ];
  1968. $question_id = Database::insert($tbl_quiz_question, $params);
  1969. if ($question_id) {
  1970. $sql = "UPDATE $tbl_quiz_question
  1971. SET id = iid WHERE iid = $question_id";
  1972. Database::query($sql);
  1973. // Get the max question_order
  1974. $sql = "SELECT max(question_order) as max_order
  1975. FROM $tbl_quiz_rel_question
  1976. WHERE c_id = $course_id AND exercice_id = $quiz_id ";
  1977. $rs_max_order = Database::query($sql);
  1978. $row_max_order = Database::fetch_object($rs_max_order);
  1979. $max_order = $row_max_order->max_order + 1;
  1980. // Attach questions to quiz
  1981. $sql = "INSERT INTO $tbl_quiz_rel_question (c_id, question_id, exercice_id, question_order)
  1982. VALUES($course_id, $question_id, $quiz_id, $max_order)";
  1983. Database::query($sql);
  1984. }
  1985. return $question_id;
  1986. }
  1987. /**
  1988. * @return string
  1989. */
  1990. public function getTypePicture()
  1991. {
  1992. return $this->typePicture;
  1993. }
  1994. /**
  1995. * @return string
  1996. */
  1997. public function getExplanation()
  1998. {
  1999. return $this->explanationLangVar;
  2000. }
  2001. /**
  2002. * Get course medias.
  2003. *
  2004. * @param int $course_id
  2005. *
  2006. * @return array
  2007. */
  2008. public static function get_course_medias(
  2009. $course_id,
  2010. $start = 0,
  2011. $limit = 100,
  2012. $sidx = "question",
  2013. $sord = "ASC",
  2014. $where_condition = []
  2015. ) {
  2016. $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  2017. $default_where = [
  2018. 'c_id = ? AND parent_id = 0 AND type = ?' => [
  2019. $course_id,
  2020. MEDIA_QUESTION,
  2021. ],
  2022. ];
  2023. $result = Database::select(
  2024. '*',
  2025. $table_question,
  2026. [
  2027. 'limit' => " $start, $limit",
  2028. 'where' => $default_where,
  2029. 'order' => "$sidx $sord",
  2030. ]
  2031. );
  2032. return $result;
  2033. }
  2034. /**
  2035. * Get count course medias.
  2036. *
  2037. * @param int course id
  2038. *
  2039. * @return int
  2040. */
  2041. public static function get_count_course_medias($course_id)
  2042. {
  2043. $table_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  2044. $result = Database::select(
  2045. 'count(*) as count',
  2046. $table_question,
  2047. [
  2048. 'where' => [
  2049. 'c_id = ? AND parent_id = 0 AND type = ?' => [
  2050. $course_id,
  2051. MEDIA_QUESTION,
  2052. ],
  2053. ],
  2054. ],
  2055. 'first'
  2056. );
  2057. if ($result && isset($result['count'])) {
  2058. return $result['count'];
  2059. }
  2060. return 0;
  2061. }
  2062. /**
  2063. * @param int $course_id
  2064. *
  2065. * @return array
  2066. */
  2067. public static function prepare_course_media_select($course_id)
  2068. {
  2069. $medias = self::get_course_medias($course_id);
  2070. $media_list = [];
  2071. $media_list[0] = get_lang('NoMedia');
  2072. if (!empty($medias)) {
  2073. foreach ($medias as $media) {
  2074. $media_list[$media['id']] = empty($media['question']) ? get_lang('Untitled') : $media['question'];
  2075. }
  2076. }
  2077. return $media_list;
  2078. }
  2079. /**
  2080. * @return array
  2081. */
  2082. public static function get_default_levels()
  2083. {
  2084. $levels = [
  2085. 1 => 1,
  2086. 2 => 2,
  2087. 3 => 3,
  2088. 4 => 4,
  2089. 5 => 5,
  2090. ];
  2091. return $levels;
  2092. }
  2093. /**
  2094. * @return string
  2095. */
  2096. public function show_media_content()
  2097. {
  2098. $html = '';
  2099. if ($this->parent_id != 0) {
  2100. $parent_question = self::read($this->parent_id);
  2101. $html = $parent_question->show_media_content();
  2102. } else {
  2103. $html .= Display::page_subheader($this->selectTitle());
  2104. $html .= $this->selectDescription();
  2105. }
  2106. return $html;
  2107. }
  2108. /**
  2109. * Swap between unique and multiple type answers.
  2110. *
  2111. * @return UniqueAnswer|MultipleAnswer
  2112. */
  2113. public function swapSimpleAnswerTypes()
  2114. {
  2115. $oppositeAnswers = [
  2116. UNIQUE_ANSWER => MULTIPLE_ANSWER,
  2117. MULTIPLE_ANSWER => UNIQUE_ANSWER,
  2118. ];
  2119. $this->type = $oppositeAnswers[$this->type];
  2120. Database::update(
  2121. Database::get_course_table(TABLE_QUIZ_QUESTION),
  2122. ['type' => $this->type],
  2123. ['c_id = ? AND id = ?' => [$this->course['real_id'], $this->id]]
  2124. );
  2125. $answerClasses = [
  2126. UNIQUE_ANSWER => 'UniqueAnswer',
  2127. MULTIPLE_ANSWER => 'MultipleAnswer',
  2128. ];
  2129. $swappedAnswer = new $answerClasses[$this->type]();
  2130. foreach ($this as $key => $value) {
  2131. $swappedAnswer->$key = $value;
  2132. }
  2133. return $swappedAnswer;
  2134. }
  2135. /**
  2136. * @param array $score
  2137. *
  2138. * @return bool
  2139. */
  2140. public function isQuestionWaitingReview($score)
  2141. {
  2142. $isReview = false;
  2143. if (!empty($score)) {
  2144. if (!empty($score['comments']) || $score['score'] > 0) {
  2145. $isReview = true;
  2146. }
  2147. }
  2148. return $isReview;
  2149. }
  2150. /**
  2151. * @param string $value
  2152. */
  2153. public function setFeedback($value)
  2154. {
  2155. $this->feedback = $value;
  2156. }
  2157. /**
  2158. * @param Exercise $exercise
  2159. *
  2160. * @return bool
  2161. */
  2162. public function showFeedback($exercise)
  2163. {
  2164. return
  2165. in_array($this->type, $this->questionTypeWithFeedback) &&
  2166. $exercise->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_EXAM;
  2167. }
  2168. /**
  2169. * @return string
  2170. */
  2171. public function returnFormatFeedback()
  2172. {
  2173. return '<br />'.Display::return_message($this->feedback, 'normal', false);
  2174. }
  2175. /**
  2176. * Check if this question exists in another exercise.
  2177. *
  2178. * @throws \Doctrine\ORM\Query\QueryException
  2179. *
  2180. * @return bool
  2181. */
  2182. public function existsInAnotherExercise()
  2183. {
  2184. $count = $this->getCountExercise();
  2185. return $count > 1;
  2186. }
  2187. /**
  2188. * @throws \Doctrine\ORM\Query\QueryException
  2189. *
  2190. * @return int
  2191. */
  2192. public function getCountExercise()
  2193. {
  2194. $em = Database::getManager();
  2195. $count = $em
  2196. ->createQuery('
  2197. SELECT COUNT(qq.iid) FROM ChamiloCourseBundle:CQuizRelQuestion qq
  2198. WHERE qq.questionId = :id
  2199. ')
  2200. ->setParameters(['id' => (int) $this->id])
  2201. ->getSingleScalarResult();
  2202. return (int) $count;
  2203. }
  2204. /**
  2205. * Check if this question exists in another exercise.
  2206. *
  2207. * @throws \Doctrine\ORM\Query\QueryException
  2208. *
  2209. * @return mixed
  2210. */
  2211. public function getExerciseListWhereQuestionExists()
  2212. {
  2213. $em = Database::getManager();
  2214. $result = $em
  2215. ->createQuery('
  2216. SELECT e
  2217. FROM ChamiloCourseBundle:CQuizRelQuestion qq
  2218. JOIN ChamiloCourseBundle:CQuiz e
  2219. WHERE e.iid = qq.exerciceId AND qq.questionId = :id
  2220. ')
  2221. ->setParameters(['id' => (int) $this->id])
  2222. ->getResult();
  2223. return $result;
  2224. }
  2225. /**
  2226. * Resizes a picture || Warning!: can only be called after uploadPicture,
  2227. * or if picture is already available in object.
  2228. *
  2229. * @param string $Dimension - Resizing happens proportional according to given dimension: height|width|any
  2230. * @param int $Max - Maximum size
  2231. *
  2232. * @return bool|null - true if success, false if failed
  2233. *
  2234. * @author Toon Keppens
  2235. */
  2236. private function resizePicture($Dimension, $Max)
  2237. {
  2238. // if the question has an ID
  2239. if (!$this->id) {
  2240. return false;
  2241. }
  2242. $picturePath = $this->getHotSpotFolderInCourse().'/'.$this->getPictureFilename();
  2243. // Get dimensions from current image.
  2244. $my_image = new Image($picturePath);
  2245. $current_image_size = $my_image->get_image_size();
  2246. $current_width = $current_image_size['width'];
  2247. $current_height = $current_image_size['height'];
  2248. if ($current_width < $Max && $current_height < $Max) {
  2249. return true;
  2250. } elseif ($current_height == '') {
  2251. return false;
  2252. }
  2253. // Resize according to height.
  2254. if ($Dimension == "height") {
  2255. $resize_scale = $current_height / $Max;
  2256. $new_width = ceil($current_width / $resize_scale);
  2257. }
  2258. // Resize according to width
  2259. if ($Dimension == "width") {
  2260. $new_width = $Max;
  2261. }
  2262. // Resize according to height or width, both should not be larger than $Max after resizing.
  2263. if ($Dimension == "any") {
  2264. if ($current_height > $current_width || $current_height == $current_width) {
  2265. $resize_scale = $current_height / $Max;
  2266. $new_width = ceil($current_width / $resize_scale);
  2267. }
  2268. if ($current_height < $current_width) {
  2269. $new_width = $Max;
  2270. }
  2271. }
  2272. $my_image->resize($new_width);
  2273. $result = $my_image->send_image($picturePath);
  2274. if ($result) {
  2275. return true;
  2276. }
  2277. return false;
  2278. }
  2279. }