testcategory.class.php 16 KB

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