test2pdf.lib.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /* For license terms, see /license.txt */
  3. /**
  4. * Functions.
  5. *
  6. * @package chamilo.plugin.test2pdf
  7. */
  8. $letters = [
  9. 'a',
  10. 'b',
  11. 'c',
  12. 'd',
  13. 'e',
  14. 'f',
  15. 'g',
  16. 'h',
  17. 'i',
  18. 'j',
  19. 'k',
  20. 'l',
  21. 'm',
  22. 'n',
  23. 'o',
  24. 'p',
  25. 'q',
  26. 'r',
  27. 's',
  28. 't',
  29. 'u',
  30. 'v',
  31. 'w',
  32. 'x',
  33. 'y',
  34. 'z',
  35. ];
  36. /**
  37. * List exercises.
  38. *
  39. * @param int $courseId Course ID
  40. * @param int $sessionId Session ID
  41. *
  42. * @throws Exception
  43. *
  44. * @return array Results (list of exercise details)
  45. */
  46. function showExerciseCourse($courseId, $sessionId = 0)
  47. {
  48. $tableQuiz = Database::get_course_table(TABLE_QUIZ_TEST);
  49. $tableLpItem = Database::get_course_table(TABLE_LP_ITEM);
  50. $courseId = (int) $courseId;
  51. $sessionId = (int) $sessionId;
  52. $conditionSession = api_get_session_condition($sessionId, true, true, 'a.session_id');
  53. $sql = "SELECT a.*
  54. FROM $tableQuiz a
  55. LEFT JOIN $tableLpItem b ON a.iid = b.path AND a.c_id = b.c_id
  56. WHERE a.c_id = $courseId
  57. AND (a.active = 1 OR (item_type = 'quiz' AND b.c_id = $courseId))
  58. $conditionSession
  59. ORDER BY a.title ASC;";
  60. $res = Database::query($sql);
  61. $aux = [];
  62. while ($row = Database::fetch_assoc($res)) {
  63. $aux[] = $row;
  64. }
  65. return $aux;
  66. }
  67. /**
  68. * List quiz details.
  69. *
  70. * @throws Exception
  71. *
  72. * @return array Results (list of quiz details)
  73. */
  74. function getInfoQuiz($courseId, $id)
  75. {
  76. $courseId = (int) $courseId;
  77. $id = (int) $id;
  78. $tableQuiz = Database::get_course_table(TABLE_QUIZ_TEST);
  79. $sql = "SELECT * FROM $tableQuiz WHERE c_id = $courseId AND iid = $id";
  80. $res = Database::query($sql);
  81. $row = Database::fetch_assoc($res);
  82. return $row;
  83. }
  84. /**
  85. * List question_id.
  86. *
  87. * @throws Exception
  88. *
  89. * @return array Results (list question ID)
  90. */
  91. function getQuestionsFromCourse($courseId, $quizId, $sessionId = 0)
  92. {
  93. $courseId = (int) $courseId;
  94. $quizId = (int) $quizId;
  95. $sessionId = (int) $sessionId;
  96. $tableQuizQuestion = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  97. $tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION);
  98. $tableQuiz = Database::get_course_table(TABLE_QUIZ_TEST);
  99. $conditionSession = api_get_session_condition($sessionId, true, true, 'q.session_id');
  100. $sql = "SELECT a.question_id AS question_id
  101. FROM $tableQuizQuestion a
  102. INNER JOIN $tableQuestion b ON a.question_id = b.iid
  103. INNER JOIN $tableQuiz q ON q.iid = a.exercice_id
  104. WHERE a.c_id = $courseId AND b.c_id = a.c_id AND a.exercice_id = $quizId
  105. AND (b.type IN (1, 2, 9, 10, 11, 12, 14))
  106. $conditionSession
  107. ORDER BY question_order ASC;";
  108. $res = Database::query($sql);
  109. $aux = [];
  110. while ($row = Database::fetch_assoc($res)) {
  111. $aux[] = $row['question_id'];
  112. }
  113. return $aux;
  114. }
  115. /**
  116. * List question details.
  117. *
  118. * @throws Exception
  119. *
  120. * @return array Results (list of question details)
  121. */
  122. function getInfoQuestion($courseId, $id)
  123. {
  124. $courseId = (int) $courseId;
  125. $id = (int) $id;
  126. $tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION);
  127. $sql = "SELECT * FROM $tableQuestion
  128. WHERE c_id = $courseId
  129. AND iid = $id
  130. AND (type IN (1, 2, 9, 10, 11, 12, 14))";
  131. $res = Database::query($sql);
  132. $row = Database::fetch_assoc($res);
  133. return $row;
  134. }
  135. /**
  136. * List answer details.
  137. *
  138. * @throws Exception
  139. *
  140. * @return array Results (list of answer by question_id)
  141. */
  142. function getAnswers($courseId, $id)
  143. {
  144. $courseId = (int) $courseId;
  145. $id = (int) $id;
  146. $tableQuizAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  147. $sql = "SELECT * FROM $tableQuizAnswer
  148. WHERE c_id = $courseId AND question_id = $id
  149. ORDER BY position ASC;";
  150. $res = Database::query($sql);
  151. $aux = [];
  152. while ($row = Database::fetch_assoc($res)) {
  153. $aux[] = $row;
  154. }
  155. return $aux;
  156. }
  157. /**
  158. * Remove all html tag.
  159. *
  160. * @param string $string The string to be stripped of HTML
  161. *
  162. * @return string clean of html tag
  163. */
  164. function removeHtml($string)
  165. {
  166. $txt = str_replace("<html>", "", $string);
  167. $txt = str_replace("<head>", "", $txt);
  168. $txt = str_replace("<title>", "", $txt);
  169. $txt = str_replace("</title>", "", $txt);
  170. $txt = str_replace("</head>", "", $txt);
  171. $txt = str_replace("<body>", "", $txt);
  172. $txt = str_replace("</body>", "", $txt);
  173. $txt = str_replace("</html>", "", $txt);
  174. $txt = strip_tags($txt);
  175. $txt = str_replace(chr(13).chr(10), "", $txt);
  176. $txt = str_replace("&nbsp;", " ", $txt);
  177. $txt = str_replace("&Aacute;", "Á", $txt);
  178. $txt = str_replace("&aacute;", "á", $txt);
  179. $txt = str_replace("&Eacute;", "É", $txt);
  180. $txt = str_replace("&eacute;", "é", $txt);
  181. $txt = str_replace("&Iacute;", "Í", $txt);
  182. $txt = str_replace("&iacute;", "í", $txt);
  183. $txt = str_replace("&Oacute;", "Ó", $txt);
  184. $txt = str_replace("&oacute;", "ó", $txt);
  185. $txt = str_replace("&Uacute;", "Ú", $txt);
  186. $txt = str_replace("&uacute;", "ú", $txt);
  187. $txt = str_replace("&Ntilde;", "Ñ", $txt);
  188. $txt = str_replace("&ntilde;", "ñ", $txt);
  189. $txt = str_replace("&agrave;", "à", $txt);
  190. $txt = str_replace("&Agrave;", "À", $txt);
  191. $txt = str_replace("&iexcl;", "¡", $txt);
  192. $txt = str_replace("&middot;", "·", $txt);
  193. $txt = str_replace("&Ccedil;", "Ç", $txt);
  194. $txt = str_replace("&ccedil;", "ç", $txt);
  195. $txt = str_replace("&quot;", '"', $txt);
  196. $txt = str_replace("&ordf;", 'ª', $txt);
  197. $txt = str_replace("&ordm;", 'º', $txt);
  198. $txt = str_replace("&amp;", '&', $txt);
  199. $txt = str_replace("&bull;", '•', $txt);
  200. $txt = str_replace("&iquest;", '¿', $txt);
  201. $txt = str_replace("&euro;", 'EUR', $txt);
  202. $txt = str_replace("&uuml;", 'ü', $txt);
  203. $txt = str_replace("&Uuml;", 'Ü', $txt);
  204. $txt = str_replace("&uml;", '¨', $txt);
  205. return $txt;
  206. }
  207. /**
  208. * Remove all html tag.
  209. *
  210. * @param string $string The string to be stripped of accents
  211. *
  212. * @return string clean of html tag
  213. */
  214. function removeQuotes($string)
  215. {
  216. $txt = str_replace("&nbsp;", " ", $string);
  217. $txt = str_replace("&Aacute;", "Á", $txt);
  218. $txt = str_replace("&aacute;", "á", $txt);
  219. $txt = str_replace("&Eacute;", "É", $txt);
  220. $txt = str_replace("&eacute;", "é", $txt);
  221. $txt = str_replace("&Iacute;", "Í", $txt);
  222. $txt = str_replace("&iacute;", "í", $txt);
  223. $txt = str_replace("&Oacute;", "Ó", $txt);
  224. $txt = str_replace("&oacute;", "ó", $txt);
  225. $txt = str_replace("&Uacute;", "Ú", $txt);
  226. $txt = str_replace("&uacute;", "ú", $txt);
  227. $txt = str_replace("&Ntilde;", "Ñ", $txt);
  228. $txt = str_replace("&ntilde;", "ñ", $txt);
  229. $txt = str_replace("&quot;", '"', $txt);
  230. $txt = str_replace("&ordf;", 'ª', $txt);
  231. $txt = str_replace("&ordm;", 'º', $txt);
  232. $txt = str_replace("&amp;", '&', $txt);
  233. $txt = str_replace("&bull;", '•', $txt);
  234. $txt = str_replace("&iquest; &", '¿', $txt);
  235. $txt = str_replace("&agrave;", "à", $txt);
  236. $txt = str_replace("&Agrave;", "À", $txt);
  237. $txt = str_replace("&iexcl;", "¡", $txt);
  238. $txt = str_replace("&middot;", "·", $txt);
  239. $txt = str_replace("&Ccedil;", "Ç", $txt);
  240. $txt = str_replace("&ccedil;", "ç", $txt);
  241. $txt = str_replace("&euro;", 'EUR', $txt);
  242. $txt = str_replace("&uuml;", 'ü', $txt);
  243. $txt = str_replace("&Uuml;", 'Ü', $txt);
  244. $txt = str_replace("uml;", '¨', $txt);
  245. return $txt;
  246. }