TestCategory.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class TestCategory
  5. * @author hubert.borderiou
  6. * @author Julio Montoya - several fixes
  7. * @todo rename to ExerciseCategory
  8. */
  9. class TestCategory
  10. {
  11. public $id;
  12. public $name;
  13. public $description;
  14. /**
  15. * Constructor of the class Category
  16. * @author - Hubert Borderiou
  17. * If you give an in_id and no in_name, you get info concerning the category of id=in_id
  18. * otherwise, you've got an category objet avec your in_id, in_name, in_descr
  19. *
  20. * @param int $in_id
  21. * @param string $in_name
  22. * @param string $in_description
  23. */
  24. public function __construct($in_id=0, $in_name = '', $in_description="")
  25. {
  26. if ($in_id != 0 && $in_name == "") {
  27. $tmpobj = new TestCategory();
  28. $tmpobj->getCategory($in_id);
  29. $this->id = $tmpobj->id;
  30. $this->name = $tmpobj->name;
  31. $this->description = $tmpobj->description;
  32. } else {
  33. $this->id = $in_id;
  34. $this->name = $in_name;
  35. $this->description = $in_description;
  36. }
  37. }
  38. /**
  39. * return the TestCategory object with id=in_id
  40. * @param $in_id
  41. */
  42. public function getCategory($in_id)
  43. {
  44. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  45. $in_id = intval($in_id);
  46. $sql = "SELECT * FROM $t_cattable WHERE id = $in_id AND c_id=".api_get_course_int_id();
  47. $res = Database::query($sql);
  48. $numrows = Database::num_rows($res);
  49. if ($numrows > 0) {
  50. $row = Database::fetch_array($res);
  51. $this->id = $row['id'];
  52. $this->name = $row['title'];
  53. $this->description = $row['description'];
  54. }
  55. }
  56. /**
  57. * add TestCategory in the database if name doesn't already exists
  58. */
  59. public function addCategoryInBDD()
  60. {
  61. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  62. $v_name = $this->name;
  63. $v_name = Database::escape_string($v_name);
  64. $v_description = $this->description;
  65. $v_description = Database::escape_string($v_description);
  66. // check if name already exists
  67. $sql = "SELECT count(*) AS nb FROM $t_cattable
  68. WHERE title = '$v_name' AND c_id=".api_get_course_int_id();
  69. $result_verif = Database::query($sql);
  70. $data_verif = Database::fetch_array($result_verif);
  71. // lets add in BDD if not the same name
  72. if ($data_verif['nb'] <= 0) {
  73. $c_id = api_get_course_int_id();
  74. $params = [
  75. 'c_id' => $c_id,
  76. 'title' => $v_name,
  77. 'description' => $v_description,
  78. ];
  79. $new_id = Database::insert($t_cattable, $params);
  80. if ($new_id) {
  81. $sql = "UPDATE $t_cattable SET id = iid WHERE iid = $new_id";
  82. Database::query($sql);
  83. // add test_category in item_property table
  84. $course_id = api_get_course_int_id();
  85. $course_info = api_get_course_info_by_id($course_id);
  86. api_item_property_update(
  87. $course_info,
  88. TOOL_TEST_CATEGORY,
  89. $new_id,
  90. 'TestCategoryAdded',
  91. api_get_user_id()
  92. );
  93. }
  94. return $new_id;
  95. } else {
  96. return false;
  97. }
  98. }
  99. /**
  100. * Removes the category from the database
  101. * if there were question in this category, the link between question and category is removed
  102. */
  103. public function removeCategory()
  104. {
  105. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  106. $tbl_question_rel_cat = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  107. $v_id = intval($this->id);
  108. $sql = "DELETE FROM $t_cattable WHERE id=$v_id AND c_id=".api_get_course_int_id();
  109. $result = Database::query($sql);
  110. if (Database::affected_rows($result) <= 0) {
  111. return false;
  112. } else {
  113. $course_id = api_get_course_int_id();
  114. // remove link between question and category
  115. $sql2 = "DELETE FROM $tbl_question_rel_cat
  116. WHERE category_id=$v_id AND c_id=".$course_id;
  117. Database::query($sql2);
  118. // item_property update
  119. $course_info = api_get_course_info_by_id($course_id);
  120. api_item_property_update(
  121. $course_info,
  122. TOOL_TEST_CATEGORY,
  123. $this->id,
  124. 'TestCategoryDeleted',
  125. api_get_user_id()
  126. );
  127. return true;
  128. }
  129. }
  130. /**
  131. * Modify category name or description of category with id=in_id
  132. */
  133. public function modifyCategory()
  134. {
  135. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  136. $v_id = intval($this->id);
  137. $v_name = Database::escape_string($this->name);
  138. $v_description = Database::escape_string($this->description);
  139. $sql = "UPDATE $t_cattable SET title='$v_name', description='$v_description'
  140. WHERE id = $v_id AND c_id=".api_get_course_int_id();
  141. $result = Database::query($sql);
  142. if (Database::affected_rows($result) <= 0) {
  143. return false;
  144. } else {
  145. // item_property update
  146. $course_id = api_get_course_int_id();
  147. $course_info = api_get_course_info_by_id($course_id);
  148. api_item_property_update(
  149. $course_info,
  150. TOOL_TEST_CATEGORY,
  151. $this->id,
  152. 'TestCategoryModified',
  153. api_get_user_id()
  154. );
  155. return true;
  156. }
  157. }
  158. /**
  159. * Gets the number of question of category id=in_id
  160. */
  161. public function getCategoryQuestionsNumber()
  162. {
  163. $t_reltable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  164. $in_id = intval($this->id);
  165. $sql = "SELECT count(*) AS nb FROM $t_reltable
  166. WHERE category_id=$in_id AND c_id=".api_get_course_int_id();
  167. $res = Database::query($sql);
  168. $row = Database::fetch_array($res);
  169. return $row['nb'];
  170. }
  171. /**
  172. * @param string $in_color
  173. */
  174. public function display($in_color="#E0EBF5")
  175. {
  176. echo "<textarea style='background-color:$in_color; width:60%; height:100px;'>";
  177. print_r($this);
  178. echo "</textarea>";
  179. }
  180. /**
  181. * Return an array of all Category objects in the database
  182. * If in_field=="" Return an array of all category objects in the database
  183. * Otherwise, return an array of all in_field value
  184. * in the database (in_field = id or name or description)
  185. */
  186. public static function getCategoryListInfo($in_field="", $in_courseid="")
  187. {
  188. if (empty($in_courseid) || $in_courseid=="") {
  189. $in_courseid = api_get_course_int_id();
  190. }
  191. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  192. $in_field = Database::escape_string($in_field);
  193. $tabres = array();
  194. if ($in_field=="") {
  195. $sql = "SELECT * FROM $t_cattable WHERE c_id=$in_courseid ORDER BY title ASC";
  196. $res = Database::query($sql);
  197. while ($row = Database::fetch_array($res)) {
  198. $tmpcat = new TestCategory($row['id'], $row['title'], $row['description']);
  199. $tabres[] = $tmpcat;
  200. }
  201. } else {
  202. $sql = "SELECT $in_field FROM $t_cattable WHERE c_id=$in_courseid ORDER BY $in_field ASC";
  203. $res = Database::query($sql);
  204. while ($row = Database::fetch_array($res)) {
  205. $tabres[] = $row[$in_field];
  206. }
  207. }
  208. return $tabres;
  209. }
  210. /**
  211. * Return the TestCategory id for question with question_id = $in_questionid
  212. * In this version, a question has only 1 TestCategory.
  213. * Return the TestCategory id, 0 if none
  214. * @param int $questionId
  215. * @param int $courseId
  216. *
  217. * @return int
  218. */
  219. public static function getCategoryForQuestion($questionId, $courseId ="")
  220. {
  221. $result = 0;
  222. if (empty($courseId) || $courseId=="") {
  223. $courseId = api_get_course_int_id();
  224. }
  225. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  226. $questionId = intval($questionId);
  227. $sql = "SELECT category_id FROM $table
  228. WHERE question_id = $questionId AND c_id = $courseId";
  229. $res = Database::query($sql);
  230. if (Database::num_rows($res) > 0) {
  231. $data = Database::fetch_array($res);
  232. $result = $data['category_id'];
  233. }
  234. return $result;
  235. }
  236. /**
  237. * true if question id has a category
  238. */
  239. public static function isQuestionHasCategory($in_questionid)
  240. {
  241. if (TestCategory::getCategoryForQuestion($in_questionid) > 0) {
  242. return true;
  243. }
  244. return false;
  245. }
  246. /**
  247. Return the category name for question with question_id = $in_questionid
  248. In this version, a question has only 1 category.
  249. Return the category id, "" if none
  250. */
  251. public static function getCategoryNameForQuestion($in_questionid, $in_courseid="")
  252. {
  253. if (empty($in_courseid) || $in_courseid=="") {
  254. $in_courseid = api_get_course_int_id();
  255. }
  256. $catid = TestCategory::getCategoryForQuestion($in_questionid, $in_courseid);
  257. $result = ""; // result
  258. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  259. $catid = intval($catid);
  260. $sql = "SELECT title FROM $t_cattable WHERE id = $catid AND c_id = $in_courseid";
  261. $res = Database::query($sql);
  262. $data = Database::fetch_array($res);
  263. if (Database::num_rows($res) > 0) {
  264. $result = $data['title'];
  265. }
  266. return $result;
  267. }
  268. /**
  269. * Return the list of differents categories ID for a test in the current course
  270. * input : test_id
  271. * return : array of category id (integer)
  272. * hubert.borderiou 07-04-2011
  273. */
  274. public static function getListOfCategoriesIDForTest($in_testid)
  275. {
  276. // parcourir les questions d'un test, recup les categories uniques dans un tableau
  277. $result = array();
  278. $quiz = new Exercise();
  279. $quiz->read($in_testid);
  280. $tabQuestionList = $quiz->selectQuestionList();
  281. // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ???
  282. for ($i=1; $i <= count($tabQuestionList); $i++) {
  283. if (!in_array(TestCategory::getCategoryForQuestion($tabQuestionList[$i]), $result)) {
  284. $result[] = TestCategory::getCategoryForQuestion($tabQuestionList[$i]);
  285. }
  286. }
  287. return $result;
  288. }
  289. /**
  290. * return the list of different categories NAME for a test
  291. * input : test_id
  292. * return : array of string
  293. * hubert.borderiou 07-04-2011
  294. * @author function rewrote by jmontoya
  295. */
  296. public static function getListOfCategoriesNameForTest($in_testid)
  297. {
  298. $tabcatName = array();
  299. $tabcatID = self::getListOfCategoriesIDForTest($in_testid);
  300. for ($i=0; $i < count($tabcatID); $i++) {
  301. $cat = new TestCategory($tabcatID[$i]);
  302. $tabcatName[$cat->id] = $cat->name;
  303. }
  304. return $tabcatName;
  305. }
  306. /**
  307. * return the number of differents categories for a test
  308. * input : test_id
  309. * return : integer
  310. * hubert.borderiou 07-04-2011
  311. */
  312. public static function getNumberOfCategoriesForTest($in_testid)
  313. {
  314. return count(TestCategory::getListOfCategoriesIDForTest($in_testid));
  315. }
  316. /**
  317. * return the number of question of a category id in a test
  318. * input : test_id, category_id
  319. * return : integer
  320. * hubert.borderiou 07-04-2011
  321. */
  322. public static function getNumberOfQuestionsInCategoryForTest($in_testid, $in_categoryid)
  323. {
  324. $nbCatResult = 0;
  325. $quiz = new Exercise();
  326. $quiz->read($in_testid);
  327. $tabQuestionList = $quiz->selectQuestionList();
  328. // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ? ? ?
  329. for ($i=1; $i <= count($tabQuestionList); $i++) {
  330. if (TestCategory::getCategoryForQuestion($tabQuestionList[$i]) == $in_categoryid) {
  331. $nbCatResult++;
  332. }
  333. }
  334. return $nbCatResult;
  335. }
  336. /**
  337. * return the number of question for a test using random by category
  338. * input : test_id, number of random question (min 1)
  339. * hubert.borderiou 07-04-2011
  340. * question without categories are not counted
  341. */
  342. public static function getNumberOfQuestionRandomByCategory($in_testid, $in_nbrandom)
  343. {
  344. $nbquestionresult = 0;
  345. $tabcatid = TestCategory::getListOfCategoriesIDForTest($in_testid);
  346. for ($i=0; $i < count($tabcatid); $i++) {
  347. if ($tabcatid[$i] > 0) { // 0 = no category for this questio
  348. $nbQuestionInThisCat = TestCategory::getNumberOfQuestionsInCategoryForTest($in_testid, $tabcatid[$i]);
  349. if ($nbQuestionInThisCat > $in_nbrandom) {
  350. $nbquestionresult += $in_nbrandom;
  351. }
  352. else {
  353. $nbquestionresult += $nbQuestionInThisCat;
  354. }
  355. }
  356. }
  357. return $nbquestionresult;
  358. }
  359. /**
  360. * Return an array (id=>name)
  361. * tabresult[0] = get_lang('NoCategory');
  362. *
  363. */
  364. public static function getCategoriesIdAndName($in_courseid="")
  365. {
  366. if (empty($in_courseid) || $in_courseid=="") {
  367. $in_courseid = api_get_course_int_id();
  368. }
  369. $tabcatobject = TestCategory::getCategoryListInfo("", $in_courseid);
  370. $tabresult = array("0"=>get_lang('NoCategorySelected'));
  371. for ($i=0; $i < count($tabcatobject); $i++) {
  372. $tabresult[$tabcatobject[$i]->id] = $tabcatobject[$i]->name;
  373. }
  374. return $tabresult;
  375. }
  376. /**
  377. * return an array of question_id for each category
  378. * tabres[0] = array of question id with category id = 0 (i.e. no category)
  379. * tabres[24] = array of question id with category id = 24
  380. * In this version, a question has 0 or 1 category
  381. */
  382. public static function getQuestionsByCat($in_exerciseId)
  383. {
  384. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  385. $TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  386. $in_exerciseId = intval($in_exerciseId);
  387. $sql = "SELECT qrc.question_id, qrc.category_id
  388. FROM $TBL_QUESTION_REL_CATEGORY qrc, $TBL_EXERCISE_QUESTION eq
  389. WHERE
  390. exercice_id=$in_exerciseId AND
  391. eq.question_id=qrc.question_id AND
  392. eq.c_id=".api_get_course_int_id()." AND
  393. eq.c_id=qrc.c_id
  394. ORDER BY category_id, question_id";
  395. $res = Database::query($sql);
  396. $list = array();
  397. while ($data = Database::fetch_array($res)) {
  398. if (!isset($tabres[$data['category_id']])) {
  399. $list[$data['category_id']] = array();
  400. }
  401. $list[$data['category_id']][] = $data['question_id'];
  402. }
  403. return $list;
  404. }
  405. /**
  406. * return a tab of $in_number random elements of $in_tab
  407. */
  408. public static function getNElementsFromArray($in_tab, $in_number)
  409. {
  410. $tabres = $in_tab;
  411. shuffle($tabres);
  412. if ($in_number < count($tabres)) {
  413. $tabres = array_slice($tabres, 0, $in_number);
  414. }
  415. return $tabres;
  416. }
  417. /**
  418. * display the category
  419. */
  420. public static function displayCategoryAndTitle($in_questionID, $in_display_category_name = 1)
  421. {
  422. echo self::returnCategoryAndTitle($in_questionID, $in_display_category_name);
  423. }
  424. /**
  425. * @param $in_questionID
  426. * @param int $in_display_category_name
  427. * @return null|string
  428. */
  429. public static function returnCategoryAndTitle($in_questionID, $in_display_category_name = 1) {
  430. $is_student = !(api_is_allowed_to_edit(null,true) || api_is_session_admin());
  431. // @todo fix $_SESSION['objExercise']
  432. $objExercise = isset($_SESSION['objExercise']) ? $_SESSION['objExercise'] : null;
  433. if (!empty($objExercise)) {
  434. $in_display_category_name = $objExercise->display_category_name;
  435. }
  436. $content = null;
  437. if (TestCategory::getCategoryNameForQuestion($in_questionID) != "" && ($in_display_category_name == 1 || !$is_student)) {
  438. $content .= '<div class="page-header">';
  439. $content .= '<h4>'.get_lang('Category').": ".TestCategory::getCategoryNameForQuestion($in_questionID).'</h4>';
  440. $content .= "</div>";
  441. }
  442. return $content;
  443. }
  444. /**
  445. * Display signs [+] and/or (>0) after question title if question has options
  446. * scoreAlwaysPositive and/or uncheckedMayScore
  447. */
  448. public function displayQuestionOption($in_objQuestion)
  449. {
  450. if ($in_objQuestion->type == MULTIPLE_ANSWER && $in_objQuestion->scoreAlwaysPositive) {
  451. echo "<span style='font-size:75%'> (>0)</span>";
  452. }
  453. if ($in_objQuestion->type == MULTIPLE_ANSWER && $in_objQuestion->uncheckedMayScore) {
  454. echo "<span style='font-size:75%'> [+]</span>";
  455. }
  456. }
  457. /**
  458. * sortTabByBracketLabel ($tabCategoryQuestions)
  459. * key of $tabCategoryQuestions are the category id (0 for not in a category)
  460. * value is the array of question id of this category
  461. * Sort question by Category
  462. */
  463. public static function sortTabByBracketLabel($in_tab)
  464. {
  465. $tabResult = array();
  466. $tabCatName = array(); // tab of category name
  467. while (list($cat_id, $tabquestion) = each($in_tab)) {
  468. $catTitle = new TestCategory($cat_id);
  469. $tabCatName[$cat_id] = $catTitle->name;
  470. }
  471. reset($in_tab);
  472. // sort table by value, keeping keys as they are
  473. asort($tabCatName);
  474. // keys of $tabCatName are keys order for $in_tab
  475. while (list($key, $val) = each($tabCatName)) {
  476. $tabResult[$key] = $in_tab[$key];
  477. }
  478. return $tabResult;
  479. }
  480. /**
  481. * return total score for test exe_id for all question in the category $in_cat_id for user
  482. * If no question for this category, return ""
  483. */
  484. public static function getCatScoreForExeidForUserid($in_cat_id, $in_exe_id, $in_user_id)
  485. {
  486. $tbl_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  487. $tbl_question_rel_category = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  488. $in_cat_id = intval($in_cat_id);
  489. $in_exe_id = intval($in_exe_id);
  490. $in_user_id = intval($in_user_id);
  491. $query = "SELECT DISTINCT
  492. marks, exe_id, user_id, ta.question_id, category_id
  493. FROM $tbl_track_attempt ta , $tbl_question_rel_category qrc
  494. WHERE
  495. ta.question_id=qrc.question_id AND
  496. qrc.category_id=$in_cat_id AND
  497. exe_id=$in_exe_id AND user_id=$in_user_id";
  498. $res = Database::query($query);
  499. $totalcatscore = "";
  500. while ($data = Database::fetch_array($res)) {
  501. $totalcatscore += $data['marks'];
  502. }
  503. return $totalcatscore;
  504. }
  505. /**
  506. * return the number max of question in a category
  507. * count the number of questions in all categories, and return the max
  508. * @author - hubert borderiou
  509. */
  510. public static function getNumberMaxQuestionByCat($in_testid)
  511. {
  512. $res_num_max = 0;
  513. // foreach question
  514. $tabcatid = TestCategory::getListOfCategoriesIDForTest($in_testid);
  515. for ($i=0; $i < count($tabcatid); $i++) {
  516. if ($tabcatid[$i] > 0) { // 0 = no category for this question
  517. $nbQuestionInThisCat = TestCategory::getNumberOfQuestionsInCategoryForTest($in_testid, $tabcatid[$i]);
  518. if ($nbQuestionInThisCat > $res_num_max) {
  519. $res_num_max = $nbQuestionInThisCat;
  520. }
  521. }
  522. }
  523. return $res_num_max;
  524. }
  525. /**
  526. * Returns a category summary report
  527. * @params int exercise id
  528. * @params array pre filled array with the category_id, score, and weight
  529. * example: array(1 => array('score' => '10', 'total' => 20));
  530. */
  531. public static function get_stats_table_by_attempt($exercise_id, $category_list = array())
  532. {
  533. if (empty($category_list)) {
  534. return null;
  535. }
  536. $category_name_list = TestCategory::getListOfCategoriesNameForTest($exercise_id);
  537. $table = new HTML_Table(array('class' => 'data_table'));
  538. $table->setHeaderContents(0, 0, get_lang('Categories'));
  539. $table->setHeaderContents(0, 1, get_lang('AbsoluteScore'));
  540. $table->setHeaderContents(0, 2, get_lang('RelativeScore'));
  541. $row = 1;
  542. $none_category = array();
  543. if (isset($category_list['none'])) {
  544. $none_category = $category_list['none'];
  545. unset($category_list['none']);
  546. }
  547. $total = array();
  548. if (isset($category_list['total'])) {
  549. $total = $category_list['total'];
  550. unset($category_list['total']);
  551. }
  552. if (count($category_list) > 1) {
  553. foreach ($category_list as $category_id => $category_item) {
  554. $table->setCellContents($row, 0, $category_name_list[$category_id]);
  555. $table->setCellContents($row, 1, ExerciseLib::show_score($category_item['score'], $category_item['total'], false));
  556. $table->setCellContents($row, 2, ExerciseLib::show_score($category_item['score'], $category_item['total'], true, false, true));
  557. $row++;
  558. }
  559. if (!empty($none_category)) {
  560. $table->setCellContents($row, 0, get_lang('None'));
  561. $table->setCellContents($row, 1, ExerciseLib::show_score($none_category['score'], $none_category['total'], false));
  562. $table->setCellContents($row, 2, ExerciseLib::show_score($none_category['score'], $none_category['total'], true, false, true));
  563. $row++;
  564. }
  565. if (!empty($total)) {
  566. $table->setCellContents($row, 0, get_lang('Total'));
  567. $table->setCellContents($row, 1, ExerciseLib::show_score($total['score'], $total['total'], false));
  568. $table->setCellContents($row, 2, ExerciseLib::show_score($total['score'], $total['total'], true, false, true));
  569. }
  570. return $table->toHtml();
  571. }
  572. return null;
  573. }
  574. /**
  575. * Return true if a category already exists with the same name
  576. * @param string $in_name
  577. *
  578. * @return bool
  579. */
  580. public static function category_exists_with_title($in_name)
  581. {
  582. $tab_test_category = TestCategory::getCategoryListInfo("title");
  583. foreach ($tab_test_category as $title) {
  584. if ($title == $in_name) {
  585. return true;
  586. }
  587. }
  588. return false;
  589. }
  590. /**
  591. * Return the id of the test category with title = $in_title
  592. * @param $in_title
  593. * @param int $in_c_id
  594. *
  595. * @return int is id of test category
  596. */
  597. public static function get_category_id_for_title($in_title, $in_c_id = 0)
  598. {
  599. $out_res = 0;
  600. if ($in_c_id == 0) {
  601. $in_c_id = api_get_course_int_id();
  602. }
  603. $tbl_cat = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  604. $sql = "SELECT id FROM $tbl_cat WHERE c_id=$in_c_id AND title = '".Database::escape_string($in_title)."'";
  605. $res = Database::query($sql);
  606. if (Database::num_rows($res) > 0) {
  607. $data = Database::fetch_array($res);
  608. $out_res = $data['id'];
  609. }
  610. return $out_res;
  611. }
  612. /**
  613. * Add a relation between question and category in table c_quiz_question_rel_category
  614. * @param int $in_category_id
  615. * @param int $in_question_id
  616. * @param int $in_course_c_id
  617. */
  618. public static function add_category_for_question_id($in_category_id, $in_question_id, $in_course_c_id)
  619. {
  620. $tbl_reltable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  621. // if question doesn't have a category
  622. // @todo change for 1.10 when a question can have several categories
  623. if (TestCategory::getCategoryForQuestion($in_question_id, $in_course_c_id) == 0 &&
  624. $in_question_id > 0 &&
  625. $in_course_c_id > 0
  626. ) {
  627. $sql = "INSERT INTO $tbl_reltable
  628. VALUES (".intval($in_course_c_id).", ".intval($in_question_id).", ".intval($in_category_id).")";
  629. Database::query($sql);
  630. }
  631. }
  632. /**
  633. * @param int $courseId
  634. * @param int $sessionId
  635. *
  636. * @return array
  637. */
  638. public function getCategories($courseId, $sessionId = 0)
  639. {
  640. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  641. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  642. $sessionId = intval($sessionId);
  643. $courseId = intval($courseId);
  644. if (empty($sessionId)) {
  645. $sessionCondition = api_get_session_condition($sessionId, true, false, 'i.session_id');
  646. } else {
  647. $sessionCondition = api_get_session_condition($sessionId, true, true, 'i.session_id');
  648. }
  649. if (empty($courseId)) {
  650. return array();
  651. }
  652. $sql = "SELECT c.* FROM $table c
  653. INNER JOIN $itemProperty i
  654. ON c.c_id = i.c_id AND i.ref = c.id
  655. WHERE
  656. c.c_id = $courseId AND
  657. i.tool = '".TOOL_TEST_CATEGORY."'
  658. $sessionCondition
  659. ORDER BY title";
  660. $result = Database::query($sql);
  661. return Database::store_result($result, 'ASSOC');
  662. }
  663. /**
  664. * @param int $courseId
  665. * @param int $sessionId
  666. * @return string
  667. */
  668. public function displayCategories($courseId, $sessionId = 0)
  669. {
  670. $categories = $this->getCategories($courseId, $sessionId);
  671. $html = null;
  672. foreach ($categories as $category) {
  673. $tmpobj = new TestCategory($category['id']);
  674. $nb_question = $tmpobj->getCategoryQuestionsNumber();
  675. $rowname = self::protectJSDialogQuote($category['title']);
  676. $nb_question_label = $nb_question == 1 ? $nb_question . ' ' . get_lang('Question') : $nb_question . ' ' . get_lang('Questions');
  677. $html .= '<div class="sectiontitle" id="id_cat' . $category['id'] . '">';
  678. $html .= "<span style='float:right'>" . $nb_question_label . "</span>";
  679. $html .= $category['title'];
  680. $html .= '</div>';
  681. $html .= '<div class="sectioncomment">';
  682. $html .= $category['description'];
  683. $html .= '</div>';
  684. $html .= '<div>';
  685. $html .= '<a href="' . api_get_self() . '?action=editcategory&category_id=' . $category['id'] . '">' .
  686. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL) . '</a>';
  687. $html .= ' <a href="' . api_get_self() . '?action=deletecategory&category_id=' . $category['id'] . '" ';
  688. $html .= 'onclick="return confirmDelete(\'' . self::protectJSDialogQuote(get_lang('DeleteCategoryAreYouSure') . '[' . $rowname) . '] ?\', \'id_cat' . $category['id'] . '\');">';
  689. $html .= Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL) . '</a>';
  690. $html .= '</div>';
  691. }
  692. return $html;
  693. }
  694. // To allowed " in javascript dialog box without bad surprises
  695. // replace " with two '
  696. public function protectJSDialogQuote($in_txt)
  697. {
  698. $res = $in_txt;
  699. $res = str_replace("'", "\'", $res);
  700. $res = str_replace('"', "\'\'", $res); // super astuce pour afficher les " dans les boite de dialogue
  701. return $res;
  702. }
  703. }