testcategory.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /** @author hubert.borderiou **/
  4. if (!class_exists('Testcategory')):
  5. class Testcategory {
  6. public $id;
  7. public $name;
  8. public $description;
  9. /**
  10. * Constructor of the class Category
  11. * @author - Hubert Borderiou
  12. If you give an in_id and no in_name, you get info concerning the category of id=in_id
  13. otherwise, you've got an category objet avec your in_id, in_name, in_descr
  14. */
  15. function Testcategory($in_id=0, $in_name = '', $in_description="") {
  16. if ($in_id != 0 && $in_name == "") {
  17. $tmpobj = new Testcategory();
  18. $tmpobj->getCategory($in_id);
  19. $this->id = $tmpobj->id;
  20. $this->name = $tmpobj->name;
  21. $this->description = $tmpobj->description;
  22. }
  23. else {
  24. $this->id = $in_id;
  25. $this->name = $in_name;
  26. $this->description = $in_description;
  27. }
  28. }
  29. /** return the Testcategory object with id=in_id
  30. */
  31. function getCategory($in_id) {
  32. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  33. $in_id = Database::escape_string($in_id);
  34. $sql = "SELECT * FROM $t_cattable WHERE id=$in_id AND c_id=".api_get_course_int_id();
  35. $res = Database::query($sql);
  36. $numrows = Database::num_rows($res);
  37. if ($numrows > 0) {
  38. $row = Database::fetch_array($res);
  39. $this->id = $row['id'];
  40. $this->name = $row['title'];
  41. $this->description = $row['description'];
  42. }
  43. }
  44. /** add Testcategory in the database if name doesn't already exists
  45. */
  46. function addCategoryInBDD() {
  47. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  48. $v_name = $this->name;
  49. $v_name = Database::escape_string($v_name);
  50. $v_description = $this->description;
  51. $v_description = Database::escape_string($v_description);
  52. // check if name already exists
  53. $sql_verif = "SELECT count(*) AS nb FROM $t_cattable WHERE title = '$v_name' AND c_id=".api_get_course_int_id();
  54. $result_verif = Database::query($sql_verif);
  55. $data_verif = Database::fetch_array($result_verif);
  56. // lets add in BDD if not the same name
  57. if ($data_verif['nb'] <= 0) {
  58. $c_id = api_get_course_int_id();
  59. $sql = "INSERT INTO $t_cattable VALUES ('$c_id', '', '$v_name', '$v_description')";
  60. $res = Database::query($sql);
  61. return true;
  62. }
  63. else {
  64. return false;
  65. }
  66. }
  67. /**
  68. * Removes the category with id=in_id from the database if no question use this category
  69. * @todo I'm removing the $in_id parameter because it seems that you're using $this->id instead of $in_id after confirmation delete this
  70. * jmontoya
  71. */
  72. //function removeCategory($in_id) {
  73. function removeCategory() {
  74. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  75. $v_id = Database::escape_string($this->id);
  76. $sql = "DELETE FROM $t_cattable WHERE id=$v_id AND c_id=".api_get_course_int_id();
  77. $res = Database::query($sql);
  78. if (Database::affected_rows() <= 0) {
  79. return false;
  80. } else {
  81. return true;
  82. }
  83. }
  84. /** modify category name or description of category with id=in_id
  85. */
  86. //function modifyCategory($in_id, $in_name, $in_description) {
  87. function modifyCategory() {
  88. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  89. $v_id = Database::escape_string($this->id);
  90. $v_name = Database::escape_string($this->name);
  91. $v_description = Database::escape_string($this->description);
  92. $sql = "UPDATE $t_cattable SET title='$v_name', description='$v_description' WHERE id='$v_id' AND c_id=".api_get_course_int_id();
  93. $res = Database::query($sql);
  94. if (Database::affected_rows() <= 0) {
  95. return false;
  96. }
  97. else {
  98. return true;
  99. }
  100. }
  101. /**
  102. * Gets the number of question of category id=in_id
  103. * @todo I'm removing the $in_id parameter because it seems that you're using $this->id instead of $in_id after confirmation delete this
  104. * jmontoya
  105. */
  106. //function getCategoryQuestionsNumber($in_id) {
  107. function getCategoryQuestionsNumber() {
  108. $t_reltable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  109. $in_id = Database::escape_string($this->id);
  110. $sql = "SELECT count(*) AS nb FROM $t_reltable WHERE category_id=$in_id AND c_id=".api_get_course_int_id();
  111. $res = Database::query($sql);
  112. $row = Database::fetch_array($res);
  113. return $row['nb'];
  114. }
  115. function display($in_color="#E0EBF5") {
  116. echo "<textarea style='background-color:$in_color; width:60%; height:100px;'>";
  117. print_r($this);
  118. echo "</textarea>";
  119. }
  120. /** return an array of all Category objects in the database
  121. If in_field=="" Return an array of all category objects in the database
  122. Otherwise, return an array of all in_field value in the database (in_field = id or name or description)
  123. */
  124. public static function getCategoryListInfo($in_field="", $in_courseid="") {
  125. if (empty($in_courseid) || $in_courseid=="") {
  126. $in_courseid = api_get_course_int_id();
  127. }
  128. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  129. $in_field = Database::escape_string($in_field);
  130. $tabres = array();
  131. if ($in_field=="") {
  132. $sql = "SELECT * FROM $t_cattable WHERE c_id=$in_courseid ORDER BY title ASC";
  133. $res = Database::query($sql);
  134. while ($row = Database::fetch_array($res)) {
  135. $tmpcat = new Testcategory($row['id'], $row['title'], $row['description']);
  136. $tabres[] = $tmpcat;
  137. }
  138. }
  139. else {
  140. $sql = "SELECT $in_field FROM $t_cattable WHERE c_id=$in_courseid ORDER BY $in_field ASC";
  141. $res = Database::query($sql);
  142. while ($row = Database::fetch_array($res)) {
  143. $tabres[] = $row[$in_field];
  144. }
  145. }
  146. return $tabres;
  147. }
  148. /**
  149. Return the testcategory id for question with question_id = $in_questionid
  150. In this version, a question has only 1 testcategory.
  151. Return the testcategory id, 0 if none
  152. */
  153. public static function getCategoryForQuestion($in_questionid, $in_courseid="") {
  154. $result = 0; // result
  155. if (empty($in_courseid) || $in_courseid=="") {
  156. $in_courseid = api_get_course_int_id();
  157. }
  158. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  159. $question_id = Database::escape_string($in_questionid);
  160. $sql = "SELECT category_id FROM $t_cattable WHERE question_id='$question_id' AND c_id=$in_courseid";
  161. $res = Database::query($sql);
  162. $data = Database::fetch_array($res);
  163. if (Database::num_rows($res) > 0) {
  164. $result = $data['category_id'];
  165. }
  166. return $result;
  167. }
  168. /**
  169. * true if question id has a category
  170. */
  171. public static function isQuestionHasCategory($in_questionid) {
  172. if (Testcategory::getCategoryForQuestion($in_questionid) > 0) {
  173. return true;
  174. }
  175. return false;
  176. }
  177. /**
  178. Return the category name for question with question_id = $in_questionid
  179. In this version, a question has only 1 category.
  180. Return the category id, "" if none
  181. */
  182. public static function getCategoryNameForQuestion($in_questionid, $in_courseid="") {
  183. if (empty($in_courseid) || $in_courseid=="") {
  184. $in_courseid = api_get_course_int_id();
  185. }
  186. $catid = Testcategory::getCategoryForQuestion($in_questionid, $in_courseid);
  187. $result = ""; // result
  188. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  189. $catid = Database::escape_string($catid);
  190. $sql = "SELECT title FROM $t_cattable WHERE id='$catid' AND c_id=$in_courseid";
  191. $res = Database::query($sql);
  192. $data = Database::fetch_array($res);
  193. if (Database::num_rows($res) > 0) {
  194. $result = $data['title'];
  195. }
  196. return $result;
  197. }
  198. /**
  199. * return the list of differents categories ID for a test
  200. * input : test_id
  201. * return : array of category id (integer)
  202. * hubert.borderiou 07-04-2011
  203. */
  204. public static function getListOfCategoriesIDForTest($in_testid) {
  205. // parcourir les questions d'un test, recup les categories uniques dans un tableau
  206. $tabcat = array();
  207. $quiz = new Exercise();
  208. $quiz->read($in_testid);
  209. $tabQuestionList = $quiz->selectQuestionList();
  210. // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ???
  211. for ($i=1; $i <= count($tabQuestionList); $i++) {
  212. if (!in_array(Testcategory::getCategoryForQuestion($tabQuestionList[$i]), $tabcat)) {
  213. $tabcat[] = Testcategory::getCategoryForQuestion($tabQuestionList[$i]);
  214. }
  215. }
  216. return $tabcat;
  217. }
  218. /**
  219. * return the list of differents categories NAME for a test
  220. * input : test_id
  221. * return : array of string
  222. * hubert.borderiou 07-04-2011
  223. */
  224. public static function getListOfCategoriesNameForTest($in_testid) {
  225. $tabcatName = array();
  226. $tabcatID = getListOfCategoriesNameForTest($in_testid);
  227. for ($i=0; $i < count($tabcatID); $i++) {
  228. $cat = new Testcategory($tabcatID[$i]);
  229. $tabcatName[] = $cat->name;
  230. }
  231. return $tabcatName;
  232. }
  233. /**
  234. * return the number of differents categories for a test
  235. * input : test_id
  236. * return : integer
  237. * hubert.borderiou 07-04-2011
  238. */
  239. public static function getNumberOfCategoriesForTest($in_testid) {
  240. return count(Testcategory::getListOfCategoriesIDForTest($in_testid));
  241. }
  242. /**
  243. * return the number of question of a category id in a test
  244. * input : test_id, category_id
  245. * return : integer
  246. * hubert.borderiou 07-04-2011
  247. */
  248. public static function getNumberOfQuestionsInCategoryForTest($in_testid, $in_categoryid) {
  249. $nbCatResult = 0;
  250. $quiz = new Exercise();
  251. $quiz->read($in_testid);
  252. $tabQuestionList = $quiz->selectQuestionList();
  253. // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ? ? ?
  254. for ($i=1; $i <= count($tabQuestionList); $i++) {
  255. if (Testcategory::getCategoryForQuestion($tabQuestionList[$i]) == $in_categoryid) {
  256. $nbCatResult++;
  257. }
  258. }
  259. return $nbCatResult;
  260. }
  261. /**
  262. * return the number of question for a test using random by category
  263. * input : test_id, number of random question (min 1)
  264. * hubert.borderiou 07-04-2011
  265. * question witout categories are not counted
  266. */
  267. public static function getNumberOfQuestionRandomByCategory($in_testid, $in_nbrandom) {
  268. $nbquestionresult = 0;
  269. $tabcatid = Testcategory::getListOfCategoriesIDForTest($in_testid);
  270. for ($i=0; $i < count($tabcatid); $i++) {
  271. if ($tabcatid[$i] > 0) { // 0 = no category for this questio
  272. $nbQuestionInThisCat = Testcategory::getNumberOfQuestionsInCategoryForTest($in_testid, $tabcatid[$i]);
  273. if ($nbQuestionInThisCat > $in_nbrandom) {
  274. $nbquestionresult += $in_nbrandom;
  275. }
  276. else {
  277. $nbquestionresult += $nbQuestionInThisCat;
  278. }
  279. }
  280. }
  281. return $nbquestionresult;
  282. }
  283. /**
  284. * Return an array (id=>name)
  285. * tabresult[0] = get_lang('NoCategory');
  286. *
  287. */
  288. static function getCategoriesIdAndName($in_courseid="") {
  289. if (empty($in_courseid) || $in_courseid=="") {
  290. $in_courseid = api_get_course_int_id();
  291. }
  292. $tabcatobject = Testcategory::getCategoryListInfo("", $in_courseid);
  293. $tabresult = array("0"=>get_lang('NoCategorySelected'));
  294. for ($i=0; $i < count($tabcatobject); $i++) {
  295. $tabresult[$tabcatobject[$i]->id] = $tabcatobject[$i]->name;
  296. }
  297. return $tabresult;
  298. }
  299. /**
  300. * return an array of question_id for each category
  301. * tabres[0] = array of question id with category id = 0 (i.e. no category)
  302. * tabres[24] = array of question id with category id = 24
  303. * In this version, a question has 0 or 1 category
  304. */
  305. function getQuestionsByCat($in_exerciceId) {
  306. $tabres = array();
  307. $TBL_EXERCICE = Database::get_course_table(TABLE_QUIZ_TEST);
  308. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  309. $TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  310. $in_exerciceId = intval($in_exerciceId);
  311. $sql = "SELECT qrc.question_id, qrc.category_id FROM $TBL_QUESTION_REL_CATEGORY qrc, $TBL_EXERCICE_QUESTION eq
  312. WHERE exercice_id=$in_exerciceId AND eq.question_id=qrc.question_id AND eq.c_id=".api_get_course_int_id()." AND eq.c_id=qrc.c_id ORDER BY category_id, question_id";
  313. $res = Database::query($sql);
  314. while ($data = Database::fetch_array($res)) {
  315. if (!is_array($tabres[$data['category_id']])) {
  316. $tabres[$data['category_id']] = array();
  317. }
  318. $tabres[$data['category_id']][] = $data['question_id'];
  319. }
  320. return $tabres;
  321. }
  322. /**
  323. * return a tab of $in_number random elements of $in_tab
  324. */
  325. function getNElementsFromArray($in_tab, $in_number) {
  326. $tabres = $in_tab;
  327. shuffle($tabres);
  328. if ($in_number < count($tabres)) {
  329. $tabres = array_slice($tabres, 0, $in_number);
  330. }
  331. return $tabres;
  332. }
  333. /**
  334. * display the category
  335. */
  336. public static function displayCategoryAndTitle($in_questionID, $in_display_category_name = 1) {
  337. echo self::returnCategoryAndTitle($in_questionID, $in_display_category_name);
  338. }
  339. public static function returnCategoryAndTitle($in_questionID, $in_display_category_name = 1) {
  340. $is_student = !(api_is_allowed_to_edit(null,true) || api_is_session_admin());
  341. $objExercise = $_SESSION['objExercise'];
  342. if (!empty($objExercise)) {
  343. $in_display_category_name = $objExercise->display_category_name;
  344. }
  345. $content = null;
  346. if (Testcategory::getCategoryNameForQuestion($in_questionID) != "" && ($in_display_category_name == 1 || !$is_student)) {
  347. $content .= '<div class="page-header">';
  348. $content .= '<h4>'.get_lang('Category').": ".Testcategory::getCategoryNameForQuestion($in_questionID).'</h4>';
  349. $content .= "</div>";
  350. }
  351. return $content;
  352. }
  353. /**
  354. * Display signs [+] and/or (>0) after question title if question has options
  355. * scoreAlwaysPositive and/or uncheckedMayScore
  356. */
  357. function displayQuestionOption($in_objQuestion) {
  358. if ($in_objQuestion->type == MULTIPLE_ANSWER && $in_objQuestion->scoreAlwaysPositive) {
  359. echo "<span style='font-size:75%'> (>0)</span>";
  360. }
  361. if ($in_objQuestion->type == MULTIPLE_ANSWER && $in_objQuestion->uncheckedMayScore) {
  362. echo "<span style='font-size:75%'> [+]</span>";
  363. }
  364. }
  365. /**
  366. * sortTabByBracketLabel ($tabCategoryQuestions)
  367. * key of $tabCategoryQuestions are the categopy id (0 for not in a category)
  368. * value is the array of question id of this category
  369. * Sort question by Category
  370. */
  371. function sortTabByBracketLabel($in_tab) {
  372. $tabResult = array();
  373. $tabCatName = array(); // tab of category name
  374. while (list($cat_id, $tabquestion) = each($in_tab)) {
  375. $catTitle = new Testcategory($cat_id);
  376. $tabCatName[$cat_id] = $catTitle->name;
  377. }
  378. reset($in_tab);
  379. // sort table by value, keeping keys as they are
  380. asort($tabCatName);
  381. // keys of $tabCatName are keys order for $in_tab
  382. while (list($key, $val) = each($tabCatName)) {
  383. $tabResult[$key] = $in_tab[$key];
  384. }
  385. return $tabResult;
  386. }
  387. /**
  388. * return total score for test exe_id for all question in the category $in_cat_id for user
  389. * If no question for this category, return ""
  390. */
  391. public static function getCatScoreForExeidForUserid($in_cat_id, $in_exe_id, $in_user_id) {
  392. $tbl_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  393. $tbl_question_rel_category = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  394. $in_cat_id = intval($in_cat_id);
  395. $in_exe_id = intval($in_exe_id);
  396. $in_user_id = intval($in_user_id);
  397. $query = "SELECT DISTINCT marks, exe_id, user_id, ta.question_id, category_id FROM $tbl_track_attempt ta , $tbl_question_rel_category qrc
  398. WHERE ta.question_id=qrc.question_id AND qrc.category_id=$in_cat_id AND exe_id=$in_exe_id AND user_id=$in_user_id";
  399. $res = Database::query($query);
  400. $totalcatscore = "";
  401. while ($data = Database::fetch_array($res)) {
  402. $totalcatscore += $data['marks'];
  403. }
  404. return $totalcatscore;
  405. }
  406. /**
  407. * return the number max of question in a category
  408. * count the number of questions in all categories, and return the max
  409. * @author - hubert borderiou
  410. */
  411. public static function getNumberMaxQuestionByCat($in_testid) {
  412. $res_num_max = 0;
  413. // foreach question
  414. $tabcatid = Testcategory::getListOfCategoriesIDForTest($in_testid);
  415. for ($i=0; $i < count($tabcatid); $i++) {
  416. if ($tabcatid[$i] > 0) { // 0 = no category for this question
  417. $nbQuestionInThisCat = Testcategory::getNumberOfQuestionsInCategoryForTest($in_testid, $tabcatid[$i]);
  418. if ($nbQuestionInThisCat > $res_num_max) {
  419. $res_num_max = $nbQuestionInThisCat;
  420. }
  421. }
  422. }
  423. return $res_num_max;
  424. }
  425. }
  426. endif;