testcategory.class.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @author hubert.borderiou & jmontoya
  5. */
  6. class Testcategory
  7. {
  8. public $id;
  9. public $name; //why using name?? Use title as in the db!
  10. public $title;
  11. public $description;
  12. public $parent_id;
  13. public $parent_path;
  14. public $category_array_tree;
  15. public $type;
  16. public $course_id;
  17. /**
  18. * Constructor of the class Category
  19. * @author - Hubert Borderiou
  20. * If you give an in_id and no in_name, you get info concerning the category of id=in_id
  21. * otherwise, you've got an category objet avec your in_id, in_name, in_descr
  22. * @todo fix this function
  23. *
  24. * @param int $in_id
  25. * @param string $in_name
  26. * @param string $in_description
  27. * @param int $parent_id
  28. * @param string $type
  29. * @param null $course_id
  30. */
  31. public function Testcategory($in_id = 0, $in_name = '', $in_description = "", $parent_id = 0, $type = 'simple', $course_id = null)
  32. {
  33. if ($in_id != 0 && $in_name == "") {
  34. $tmpobj = new Testcategory();
  35. $tmpobj->getCategory($in_id);
  36. $this->id = $tmpobj->id;
  37. $this->name = $tmpobj->name;
  38. $this->title = $tmpobj->name;
  39. $this->description = $tmpobj->description;
  40. $this->parent_id = $tmpobj->parent_id;
  41. $this->parent_path = $this->name;
  42. if (!empty($tmpobj->parent_id)) {
  43. $category = new Testcategory($tmpobj->parent_id);
  44. $this->parent_path = $category->parent_path.' > '.$this->name;
  45. }
  46. } else {
  47. $this->id = $in_id;
  48. $this->name = $in_name;
  49. $this->description = $in_description;
  50. $this->parent_id = $parent_id;
  51. }
  52. $this->type = $type;
  53. if (!empty($course_id)) {
  54. $this->course_id = $course_id;
  55. } else {
  56. $this->course_id = api_get_course_int_id();
  57. }
  58. }
  59. /**
  60. * Return the Testcategory object with id=in_id
  61. * @param int $id
  62. * @return bool
  63. * @assert () === false
  64. */
  65. function getCategory($id) {
  66. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  67. $in_id = Database::escape_string($id);
  68. $sql = "SELECT * FROM $t_cattable WHERE iid = $id ";
  69. $res = Database::query($sql);
  70. $numrows = Database::num_rows($res);
  71. if ($numrows > 0) {
  72. $row = Database::fetch_array($res);
  73. $this->id = $row['iid'];
  74. $this->title = $this->name = $row['title'];
  75. $this->description = $row['description'];
  76. $this->parent_id = $row['parent_id'];
  77. } else {
  78. return false;
  79. }
  80. }
  81. /**
  82. * Add Testcategory in the database if name doesn't already exists
  83. *
  84. */
  85. function addCategoryInBDD()
  86. {
  87. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  88. $v_name = Database::escape_string($this->name);
  89. $v_description = Database::escape_string($this->description);
  90. $parent_id = intval($this->parent_id);
  91. $course_id = $this->course_id;
  92. $courseCondition = " AND c_id = $course_id ";
  93. if ($this->type == 'global') {
  94. $course_id = '';
  95. $courseCondition = null;
  96. }
  97. // Check if name already exists
  98. $sql = "SELECT count(*) AS nb FROM $t_cattable WHERE title = '$v_name' $courseCondition";
  99. $result = Database::query($sql);
  100. $data = Database::fetch_array($result);
  101. // lets add in BD if not the same name
  102. if ($data['nb'] <= 0) {
  103. $sql = "INSERT INTO $t_cattable (c_id, title, description, parent_id) VALUES ('$course_id', '$v_name', '$v_description', '$parent_id')";
  104. Database::query($sql);
  105. return Database::insert_id();
  106. } else {
  107. return false;
  108. }
  109. }
  110. /**
  111. * @param string $title
  112. * @param int $course_id
  113. * @return bool
  114. */
  115. function get_category_by_title($title , $course_id = 0) {
  116. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  117. $course_id = intval($course_id);
  118. $sql = "SELECT * FROM $table WHERE title = '$title' AND c_id IN ('0', '$course_id')LIMIT 1";
  119. $title = Database::escape_string($title);
  120. $result = Database::query($sql);
  121. if (Database::num_rows($result)) {
  122. $result = Database::store_result($result, 'ASSOC');
  123. return $result[0];
  124. }
  125. return false;
  126. }
  127. /**
  128. *
  129. * Get categories by title for json calls
  130. * @param string $tag
  131. * @return array
  132. * @assert() === false
  133. */
  134. function get_categories_by_keyword($tag) {
  135. if (empty($tag)) {
  136. return false;
  137. }
  138. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  139. $sql = "SELECT iid, title, c_id FROM $table WHERE 1=1 ";
  140. $tag = Database::escape_string($tag);
  141. $where_condition = array();
  142. if (!empty($tag)) {
  143. $condition = ' LIKE "%'.$tag.'%"';
  144. $where_condition = array(
  145. "title $condition",
  146. );
  147. $where_condition = ' AND ('.implode(' OR ', $where_condition).') ';
  148. }
  149. switch ($this->type) {
  150. case 'simple':
  151. $course_condition = " AND c_id = '".api_get_course_int_id()."' ";
  152. break;
  153. case 'global':
  154. $course_condition = " AND c_id = '0' ";
  155. break;
  156. case 'all':
  157. $course_condition = " AND c_id IN ('0', '".api_get_course_int_id()."')";
  158. break;
  159. }
  160. $where_condition .= $course_condition;
  161. $order_clause = " ORDER BY title";
  162. $sql .= $where_condition.$order_clause;
  163. $result = Database::query($sql);
  164. if (Database::num_rows($result)) {
  165. return Database::store_result($result, 'ASSOC');
  166. }
  167. return false;
  168. }
  169. /**
  170. * Removes the category with id=in_id from the database if no question use this category
  171. * @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
  172. * jmontoya
  173. */
  174. function removeCategory()
  175. {
  176. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  177. $v_id = Database::escape_string($this->id);
  178. $sql = "DELETE FROM $t_cattable WHERE iid = $v_id";
  179. Database::query($sql);
  180. if (Database::affected_rows() <= 0) {
  181. return false;
  182. } else {
  183. return true;
  184. }
  185. }
  186. /**
  187. * Modify category name or description of category with id=in_id
  188. */
  189. function modifyCategory() {
  190. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  191. $v_id = Database::escape_string($this->id);
  192. $v_name = Database::escape_string($this->name);
  193. $v_description = Database::escape_string($this->description);
  194. $parent_id = intval($this->parent_id);
  195. //Avoid recursive categories
  196. if ($parent_id == $v_id) {
  197. $parent_id = 0;
  198. }
  199. $sql = "UPDATE $t_cattable SET
  200. title = '$v_name',
  201. description = '$v_description',
  202. parent_id = '$parent_id'
  203. WHERE iid = '$v_id'";
  204. Database::query($sql);
  205. if (Database::affected_rows() <= 0) {
  206. return false;
  207. } else {
  208. return true;
  209. }
  210. }
  211. /**
  212. * Gets the number of question of category id=in_id
  213. * @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
  214. * jmontoya
  215. */
  216. //function getCategoryQuestionsNumber($in_id) {
  217. function getCategoryQuestionsNumber() {
  218. $t_reltable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  219. $in_id = Database::escape_string($this->id);
  220. $sql = "SELECT count(*) AS nb FROM $t_reltable WHERE category_id = $in_id";
  221. $res = Database::query($sql);
  222. $row = Database::fetch_array($res);
  223. return $row['nb'];
  224. }
  225. function display($in_color="#E0EBF5") {
  226. echo "<textarea style='background-color:$in_color; width:60%; height:100px;'>";
  227. print_r($this);
  228. echo "</textarea>";
  229. }
  230. /** return an array of all Category objects in the database
  231. If in_field=="" Return an array of all category objects in the database
  232. Otherwise, return an array of all in_field value in the database (in_field = id or name or description)
  233. */
  234. public static function getCategoryListInfo($in_field = "", $in_courseid="") {
  235. if (empty($in_courseid) || $in_courseid=="") {
  236. $in_courseid = api_get_course_int_id();
  237. }
  238. $t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  239. $in_field = Database::escape_string($in_field);
  240. $tabres = array();
  241. if ($in_field=="") {
  242. $sql = "SELECT * FROM $t_cattable WHERE c_id = $in_courseid ORDER BY title ASC";
  243. $res = Database::query($sql);
  244. while ($row = Database::fetch_array($res)) {
  245. $tmpcat = new Testcategory($row['iid'], $row['title'], $row['description'], $row['parent_id']);
  246. $tabres[] = $tmpcat;
  247. }
  248. } else {
  249. $sql = "SELECT $in_field
  250. FROM $t_cattable WHERE c_id=$in_courseid
  251. ORDER BY $in_field ASC";
  252. $res = Database::query($sql);
  253. while ($row = Database::fetch_array($res)) {
  254. $tabres[] = $row[$in_field];
  255. }
  256. }
  257. return $tabres;
  258. }
  259. /**
  260. Return the testcategory id for question with question_id = $in_questionid
  261. In this version, a question has only 1 testcategory.
  262. Return the testcategory id, 0 if none
  263. * @assert () === false
  264. */
  265. public static function getCategoryForQuestion($question_id, $in_courseid = null) {
  266. $result = array(); // result
  267. if (empty($in_courseid) || $in_courseid=="") {
  268. $in_courseid = api_get_course_int_id();
  269. }
  270. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  271. $question_id = Database::escape_string($question_id);
  272. $sql = "SELECT category_id FROM $t_cattable WHERE question_id = '$question_id' AND c_id = $in_courseid";
  273. $res = Database::query($sql);
  274. if (Database::num_rows($res) > 0) {
  275. while ($row = Database::fetch_array($res, 'ASSOC')) {
  276. $result[] = $row['category_id'];
  277. }
  278. }
  279. return $result;
  280. }
  281. public static function getCategoryForQuestionWithCategoryData($question_id, $in_courseid = null) {
  282. $result = array(); // result
  283. if (empty($in_courseid) || $in_courseid=="") {
  284. $in_courseid = api_get_course_int_id();
  285. }
  286. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  287. $table_category = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  288. $question_id = Database::escape_string($question_id);
  289. $sql = "SELECT * FROM $t_cattable qc INNER JOIN $table_category c ON (category_id = c.iid) WHERE question_id = '$question_id' AND qc.c_id = $in_courseid";
  290. $res = Database::query($sql);
  291. if (Database::num_rows($res) > 0) {
  292. while ($row = Database::fetch_array($res, 'ASSOC')) {
  293. $result[] = $row;
  294. }
  295. }
  296. return $result;
  297. }
  298. /**
  299. *
  300. * @param int $question_id
  301. * @param int $course_id
  302. * @param bool $display_into_labels
  303. * @return string
  304. */
  305. public static function getCategoryNamesForQuestion($question_id, $course_id = null, $display_into_labels = true) {
  306. $result = array(); // result
  307. if (empty($course_id) || $course_id == "") {
  308. $course_id = api_get_course_int_id();
  309. }
  310. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  311. $table_category = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  312. $question_id = intval($question_id);
  313. $course_id = intval($course_id);
  314. $sql = "SELECT c.title, c.iid FROM $t_cattable qc INNER JOIN $table_category c
  315. ON (qc.category_id = c.iid AND qc.c_id = $course_id)
  316. WHERE question_id = '$question_id' ";
  317. $res = Database::query($sql);
  318. if (Database::num_rows($res) > 0) {
  319. while ($row = Database::fetch_array($res)) {
  320. $cat = new Testcategory($row['iid']);
  321. $result[] = $cat->parent_path;
  322. }
  323. }
  324. if ($display_into_labels) {
  325. $html = self::draw_category_label($result, 'header');
  326. return $html;
  327. }
  328. return $result;
  329. }
  330. /**
  331. * true if question id has a category
  332. * @assert () === false
  333. * @assert (null) === false
  334. * @assert (-1) === false
  335. */
  336. public static function isQuestionHasCategory($question_id) {
  337. $category_list = Testcategory::getCategoryForQuestion($question_id);
  338. if (!empty($category_list)) {
  339. return true;
  340. }
  341. return false;
  342. }
  343. /**
  344. * @todo fix this
  345. Return the category name for question with question_id = $in_questionid
  346. In this version, a question has only 1 category.
  347. Return the category id, "" if none
  348. */
  349. public static function getCategoryNameForQuestion($catid, $course_id = null) {
  350. if (empty($course_id) || $course_id == "") {
  351. $course_id = api_get_course_int_id();
  352. }
  353. $course_id = intval($course_id);
  354. $result = array();
  355. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  356. $catid = Database::escape_string($catid);
  357. $sql = "SELECT title FROM $t_cattable WHERE iid = '$catid' AND c_id = $course_id";
  358. $res = Database::query($sql);
  359. $data = Database::fetch_array($res);
  360. if (Database::num_rows($res) > 0) {
  361. $result = $data['title'];
  362. }
  363. return $result;
  364. }
  365. /**
  366. * return the list of differents categories ID for a test
  367. * input : test_id
  368. * return : array of category id (integer)
  369. * @author hubert.borderiou 07-04-2011, Julio Montoya
  370. */
  371. public static function getListOfCategoriesIDForTest($exercise_id, $grouped_by_category = true) {
  372. // parcourir les questions d'un test, recup les categories uniques dans un tableau
  373. $categories_in_exercise = array();
  374. $exercise = new Exercise();
  375. $exercise->setCategoriesGrouping($grouped_by_category);
  376. $exercise->read($exercise_id);
  377. $question_list = $exercise->selectQuestionList();
  378. // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ???
  379. foreach ($question_list as $question_id) {
  380. $category_list = Testcategory::getCategoryForQuestion($question_id);
  381. if (!empty($category_list)) {
  382. $categories_in_exercise = array_merge($categories_in_exercise, $category_list);
  383. }
  384. }
  385. if (!empty($categories_in_exercise)) {
  386. $categories_in_exercise = array_unique(array_filter($categories_in_exercise));
  387. }
  388. return $categories_in_exercise;
  389. }
  390. public static function getListOfCategoriesIDForTestObject($exercise_obj) {
  391. // parcourir les questions d'un test, recup les categories uniques dans un tableau
  392. $categories_in_exercise = array();
  393. $question_list = $exercise_obj->selectQuestionList();
  394. // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ???
  395. foreach ($question_list as $question_id) {
  396. $category_list = Testcategory::getCategoryForQuestion($question_id);
  397. if (!empty($category_list)) {
  398. $categories_in_exercise = array_merge($categories_in_exercise, $category_list);
  399. }
  400. }
  401. if (!empty($categories_in_exercise)) {
  402. $categories_in_exercise = array_unique(array_filter($categories_in_exercise));
  403. }
  404. return $categories_in_exercise;
  405. }
  406. /**
  407. * return the list of differents categories NAME for a test
  408. * input : test_id
  409. * return : array of string
  410. * hubert.borderiou 07-04-2011
  411. * @author function rewrote by jmontoya
  412. */
  413. public static function getListOfCategoriesNameForTest($exercise_id, $grouped_by_category = true) {
  414. $result = array();
  415. $categories = self::getListOfCategoriesIDForTest($exercise_id, $grouped_by_category);
  416. foreach ($categories as $cat_id) {
  417. $cat = new Testcategory($cat_id);
  418. if (!empty($cat->id)) {
  419. $result[$cat->id] = $cat->name;
  420. }
  421. }
  422. return $result;
  423. }
  424. public static function getListOfCategoriesForTest($exercise_obj) {
  425. $result = array();
  426. $categories = self::getListOfCategoriesIDForTestObject($exercise_obj);
  427. foreach ($categories as $cat_id) {
  428. $cat = new Testcategory($cat_id);
  429. $cat = (array)$cat;
  430. $cat['iid'] = $cat['id'];
  431. $cat['title'] = $cat['name'];
  432. $result[$cat['id']] = $cat;
  433. }
  434. return $result;
  435. }
  436. /**
  437. * return the number of differents categories for a test
  438. * input : test_id
  439. * return : integer
  440. * hubert.borderiou 07-04-2011
  441. */
  442. public static function getNumberOfCategoriesForTest($exercise_id) {
  443. return count(Testcategory::getListOfCategoriesIDForTest($exercise_id));
  444. }
  445. /**
  446. * return the number of question of a category id in a test
  447. * input : test_id, category_id
  448. * return : integer
  449. * hubert.borderiou 07-04-2011
  450. */
  451. public static function getNumberOfQuestionsInCategoryForTest($exercise_id, $category_id) {
  452. $number_questions_in_category = 0;
  453. $exercise = new Exercise();
  454. $exercise->read($exercise_id);
  455. $question_list = $exercise->selectQuestionList();
  456. // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ? ? ?
  457. foreach ($question_list as $question_id) {
  458. $category_in_question = Testcategory::getCategoryForQuestion($question_id);
  459. if (in_array($category_id, $category_in_question)) {
  460. $number_questions_in_category++;
  461. }
  462. }
  463. return $number_questions_in_category;
  464. }
  465. /**
  466. * return the number of question for a test using random by category
  467. * input : test_id, number of random question (min 1)
  468. * hubert.borderiou 07-04-2011
  469. * question witout categories are not counted
  470. */
  471. public static function getNumberOfQuestionRandomByCategory($exercise_id, $in_nbrandom) {
  472. $nbquestionresult = 0;
  473. $list_categories = Testcategory::getListOfCategoriesIDForTest($exercise_id);
  474. if (!empty($list_categories)) {
  475. foreach ($list_categories as $category_item) {
  476. if ($category_item > 0) {
  477. // 0 = no category for this question
  478. $nbQuestionInThisCat = Testcategory::getNumberOfQuestionsInCategoryForTest($exercise_id, $category_item);
  479. if ($nbQuestionInThisCat > $in_nbrandom) {
  480. $nbquestionresult += $in_nbrandom;
  481. } else {
  482. $nbquestionresult += $nbQuestionInThisCat;
  483. }
  484. }
  485. }
  486. }
  487. return $nbquestionresult;
  488. }
  489. /**
  490. * Return an array (id=>name)
  491. * tabresult[0] = get_lang('NoCategory');
  492. *
  493. */
  494. static function getCategoriesIdAndName($in_courseid = "") {
  495. if (empty($in_courseid) || $in_courseid=="") {
  496. $in_courseid = api_get_course_int_id();
  497. }
  498. $tabcatobject = Testcategory::getCategoryListInfo("", $in_courseid);
  499. $tabresult = array("0"=>get_lang('NoCategorySelected'));
  500. for ($i=0; $i < count($tabcatobject); $i++) {
  501. $tabresult[$tabcatobject[$i]->id] = $tabcatobject[$i]->name;
  502. }
  503. return $tabresult;
  504. }
  505. /**
  506. * Returns an array of question ids for each category
  507. * $categories[1][30] = 10, array with category id = 1 and question_id = 10
  508. * A question has "n" categories
  509. */
  510. static function getQuestionsByCat($exerciseId, $check_in_question_list = array()) {
  511. $categories = array();
  512. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  513. $TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  514. $exerciseId = intval($exerciseId);
  515. $sql = "SELECT DISTINCT qrc.question_id, qrc.category_id
  516. FROM $TBL_QUESTION_REL_CATEGORY qrc, $TBL_EXERCICE_QUESTION eq
  517. WHERE exercice_id = $exerciseId AND
  518. eq.question_id = qrc.question_id AND
  519. eq.c_id =".api_get_course_int_id()." AND
  520. eq.c_id = qrc.c_id
  521. ORDER BY category_id, question_id";
  522. $res = Database::query($sql);
  523. while ($data = Database::fetch_array($res)) {
  524. if (!empty($check_in_question_list)) {
  525. if (!in_array($data['question_id'], $check_in_question_list)) {
  526. continue;
  527. }
  528. }
  529. if (!isset($categories[$data['category_id']]) OR !is_array($categories[$data['category_id']])) {
  530. $categories[$data['category_id']] = array();
  531. }
  532. $categories[$data['category_id']][] = $data['question_id'];
  533. }
  534. return $categories;
  535. }
  536. /**
  537. * Return an array of X elements of an array
  538. */
  539. static function getNElementsFromArray($array, $random_number) {
  540. shuffle($array);
  541. if ($random_number < count($array)) {
  542. $array = array_slice($array, 0, $random_number);
  543. }
  544. return $array;
  545. }
  546. /**
  547. * Display signs [+] and/or (>0) after question title if question has options
  548. * scoreAlwaysPositive and/or uncheckedMayScore
  549. */
  550. function displayQuestionOption($in_objQuestion) {
  551. if ($in_objQuestion->type == MULTIPLE_ANSWER && $in_objQuestion->scoreAlwaysPositive) {
  552. echo "<span style='font-size:75%'> (>0)</span>";
  553. }
  554. if ($in_objQuestion->type == MULTIPLE_ANSWER && $in_objQuestion->uncheckedMayScore) {
  555. echo "<span style='font-size:75%'> [+]</span>";
  556. }
  557. }
  558. /**
  559. * key of $array are the categopy id (0 for not in a category)
  560. * value is the array of question id of this category
  561. * Sort question by Category
  562. */
  563. static function sortCategoriesQuestionByName($array) {
  564. $tabResult = array();
  565. $category_array = array();
  566. // tab of category name
  567. while (list($cat_id, $tabquestion) = each($array)) {
  568. $cat = new Testcategory($cat_id);
  569. $category_array[$cat_id] = $cat->title;
  570. }
  571. reset($array);
  572. // sort table by value, keeping keys as they are
  573. asort($category_array);
  574. // keys of $tabCatName are keys order for $in_tab
  575. while (list($key, $val) = each($category_array)) {
  576. $tabResult[$key] = $array[$key];
  577. }
  578. return $tabResult;
  579. }
  580. /**
  581. * return total score for test exe_id for all question in the category $in_cat_id for user
  582. * If no question for this category, return ""
  583. */
  584. public static function getCatScoreForExeidForUserid($in_cat_id, $in_exe_id, $in_user_id) {
  585. $tbl_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  586. $tbl_question_rel_category = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  587. $in_cat_id = intval($in_cat_id);
  588. $in_exe_id = intval($in_exe_id);
  589. $in_user_id = intval($in_user_id);
  590. $query = "SELECT DISTINCT marks, exe_id, user_id, ta.question_id, category_id
  591. FROM $tbl_track_attempt ta , $tbl_question_rel_category qrc
  592. 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";
  593. $res = Database::query($query);
  594. $totalcatscore = "";
  595. while ($data = Database::fetch_array($res)) {
  596. $totalcatscore += $data['marks'];
  597. }
  598. return $totalcatscore;
  599. }
  600. /**
  601. * return the number max of question in a category
  602. * count the number of questions in all categories, and return the max
  603. * @author - hubert borderiou
  604. */
  605. public static function getNumberMaxQuestionByCat($in_testid)
  606. {
  607. $res_num_max = 0;
  608. // foreach question
  609. $tabcatid = Testcategory::getListOfCategoriesIDForTest($in_testid);
  610. for ($i=0; $i < count($tabcatid); $i++) {
  611. if ($tabcatid[$i] > 0) { // 0 = no category for this question
  612. $nbQuestionInThisCat = Testcategory::getNumberOfQuestionsInCategoryForTest($in_testid, $tabcatid[$i]);
  613. if ($nbQuestionInThisCat > $res_num_max) {
  614. $res_num_max = $nbQuestionInThisCat;
  615. }
  616. }
  617. }
  618. return $res_num_max;
  619. }
  620. /**
  621. * @param int $course_id
  622. * @return array
  623. */
  624. public static function getCategoryListName($course_id = null)
  625. {
  626. $category_list = self::getCategoryListInfo(null, $course_id);
  627. $category_name_list = array();
  628. if (!empty($category_list)) {
  629. foreach ($category_list as $category) {
  630. $category_name_list[$category->id] = $category->name;
  631. }
  632. }
  633. return $category_name_list;
  634. }
  635. /**
  636. * @param array $category_list
  637. * @param array $all_categories
  638. * @return null|string
  639. */
  640. public static function return_category_labels($category_list, $all_categories) {
  641. $category_list_to_render = array();
  642. foreach ($category_list as $category_id) {
  643. $category_name = null;
  644. if (!isset($all_categories[$category_id])) {
  645. $category_name = get_lang('Untitled');
  646. } else {
  647. $category_name = Text::cut($all_categories[$category_id], 15);
  648. }
  649. $category_list_to_render[] = $category_name;
  650. }
  651. $html = self::draw_category_label($category_list_to_render, 'label');
  652. return $html;
  653. }
  654. /**
  655. * @param array $category_list
  656. * @param string $type
  657. * @return null|string
  658. */
  659. static function draw_category_label($category_list, $type = 'label') {
  660. $new_category_list = array();
  661. foreach ($category_list as $category_name) {
  662. switch ($type) {
  663. case 'label':
  664. $new_category_list[] = Display::label($category_name, 'info');
  665. break;
  666. case 'header':
  667. $new_category_list[] = $category_name;
  668. break;
  669. }
  670. }
  671. $html = null;
  672. if (!empty($new_category_list)) {
  673. switch ($type) {
  674. case 'label':
  675. $html = implode(' ', $new_category_list);
  676. break;
  677. case 'header':
  678. $html = Display::page_subheader3(get_lang('Category').': '.implode(', ', $new_category_list));
  679. }
  680. }
  681. return $html;
  682. }
  683. /**
  684. * Returns a category summary report
  685. * @params int exercise id
  686. * @params array prefilled array with the category_id, score, and weight example: array(1 => array('score' => '10', 'total' => 20));
  687. */
  688. public static function get_stats_table_by_attempt($exercise_id, $category_list = array()) {
  689. if (empty($category_list)) {
  690. return null;
  691. }
  692. $category_name_list = Testcategory::getListOfCategoriesNameForTest($exercise_id, false);
  693. $table = new HTML_Table(array('class' => 'data_table'));
  694. $table->setHeaderContents(0, 0, get_lang('Categories'));
  695. $table->setHeaderContents(0, 1, get_lang('AbsoluteScore'));
  696. $table->setHeaderContents(0, 2, get_lang('RelativeScore'));
  697. $row = 1;
  698. $none_category = array();
  699. if (isset($category_list['none'])) {
  700. $none_category = $category_list['none'];
  701. unset($category_list['none']);
  702. }
  703. $total = array();
  704. if (isset($category_list['total'])) {
  705. $total = $category_list['total'];
  706. unset($category_list['total']);
  707. }
  708. if (count($category_list) > 1) {
  709. foreach ($category_list as $category_id => $category_item) {
  710. $table->setCellContents($row, 0, $category_name_list[$category_id]);
  711. $table->setCellContents($row, 1, show_score($category_item['score'], $category_item['total'], false));
  712. $table->setCellContents($row, 2, show_score($category_item['score'], $category_item['total'], true, false, true));
  713. $row++;
  714. }
  715. if (!empty($none_category)) {
  716. $table->setCellContents($row, 0, get_lang('None'));
  717. $table->setCellContents($row, 1, show_score($none_category['score'], $none_category['total'], false));
  718. $table->setCellContents($row, 2, show_score($none_category['score'], $none_category['total'], true, false, true));
  719. $row++;
  720. }
  721. if (!empty($total)) {
  722. $table->setCellContents($row, 0, get_lang('Total'));
  723. $table->setCellContents($row, 1, show_score($total['score'], $total['total'], false));
  724. $table->setCellContents($row, 2, show_score($total['score'], $total['total'], true, false, true));
  725. }
  726. return $table->toHtml();
  727. }
  728. return null;
  729. }
  730. /**
  731. * @return array
  732. */
  733. function get_all_categories() {
  734. $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  735. $sql = "SELECT * FROM $table ORDER BY title ASC";
  736. $res = Database::query($sql);
  737. while ($row = Database::fetch_array($res,'ASSOC')) {
  738. $array[] = $row;
  739. }
  740. return $array;
  741. }
  742. /**
  743. * @param int $exercise_id
  744. * @param int $course_id
  745. * @param string $order
  746. * @return bool
  747. */
  748. function get_category_exercise_tree($exercise_id, $course_id, $order = null)
  749. {
  750. $table = Database::get_course_table(TABLE_QUIZ_REL_CATEGORY);
  751. $table_category = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  752. $sql = "SELECT * FROM $table qc INNER JOIN $table_category c ON (category_id = c.iid)
  753. WHERE exercise_id = {$exercise_id} AND qc.c_id = {$course_id} ";
  754. if (!empty($order)) {
  755. $sql .= "ORDER BY $order";
  756. }
  757. $result = Database::query($sql);
  758. if (Database::num_rows($result)) {
  759. while ($row = Database::fetch_array($result, 'ASSOC')) {
  760. $list[$row['category_id']] = $row;
  761. }
  762. if (!empty($list)) {
  763. $array = $this->sort_tree_array($list);
  764. $this->create_tree_array($array);
  765. }
  766. return $this->category_array_tree;
  767. }
  768. return false;
  769. }
  770. /**
  771. * @param $type
  772. * @return mixed
  773. */
  774. function get_category_tree_by_type($type) {
  775. $course_id = api_get_course_int_id();
  776. if ($type == 'global') {
  777. $course_condition = 'c_id = 0';
  778. } else {
  779. $course_condition = " c_id IN ('$course_id', '0') ";
  780. }
  781. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  782. $sql = "SELECT * FROM $t_cattable WHERE $course_condition";
  783. $res = Database::query($sql);
  784. $categories = array();
  785. while ($row = Database::fetch_array($res, 'ASSOC')) {
  786. $row['id'] = $row['iid'];
  787. $categories[] = $row;
  788. }
  789. if (!empty($categories)) {
  790. $array = $this->sort_tree_array($categories);
  791. $this->create_tree_array($array);
  792. }
  793. return $this->category_array_tree;
  794. }
  795. /**
  796. * @param $exercise_obj
  797. * @return string
  798. */
  799. function return_category_form($exercise_obj) {
  800. $categories = $this->getListOfCategoriesForTest($exercise_obj);
  801. if (!empty($categories)) {
  802. $array = $this->sort_tree_array($categories);
  803. $this->create_tree_array($array);
  804. }
  805. $saved_categories = $exercise_obj->get_categories_in_exercise();
  806. $return = null;
  807. if (!empty($this->category_array_tree)) {
  808. $nbQuestionsTotal = $exercise_obj->getNumberQuestionExerciseCategory();
  809. $original_grouping = $exercise_obj->categories_grouping;
  810. $exercise_obj->setCategoriesGrouping(true);
  811. $real_question_count = count($exercise_obj->selectQuestionList(true));
  812. $exercise_obj->setCategoriesGrouping($original_grouping);
  813. $warning = null;
  814. if ($nbQuestionsTotal != $real_question_count) {
  815. $warning = Display::return_message(get_lang('CheckThatYouHaveEnoughQuestionsInYourCategories'), 'warning');
  816. }
  817. $return .= $warning;
  818. $return .= '<table class="data_table">';
  819. $return .= '<tr>';
  820. $return .= '<th height="24">' . get_lang('Categories') . '</th>';
  821. $return .= '<th width="70" height="24">' . get_lang('Number') . '</th></tr>';
  822. for ($i = 0; $i < count($this->category_array_tree); $i++) {
  823. $cat_id = $this->category_array_tree[$i]['id'];
  824. $return .= '<tr>';
  825. $return .= '<td>';
  826. $style = 'margin-left:' . $this->category_array_tree[$i]['depth'] * 20 . 'px; margin-right:10px;';
  827. $return .= Display::div($this->category_array_tree[$i]['title'], array('style' => $style));
  828. $return .= '</td>';
  829. $return .= '<td>';
  830. $value = isset($saved_categories) && isset($saved_categories[$cat_id]) ? $saved_categories[$cat_id]['count_questions'] : 0;
  831. $return .= '<input name="category['.$cat_id.']" value="' .$value.'" />';
  832. $return .= '</td>';
  833. $return .= '</tr>';
  834. }
  835. $return .= '</table>';
  836. return $return;
  837. }
  838. }
  839. /**
  840. * @param $array
  841. * @return mixed
  842. */
  843. public function sort_tree_array($array) {
  844. foreach ($array as $key => $row) {
  845. $parent[$key] = $row['parent_id'];
  846. }
  847. if (count($array) > 0) {
  848. array_multisort($parent, SORT_ASC, $array);
  849. }
  850. return $array;
  851. }
  852. /**
  853. * @param $array
  854. * @param int $parent
  855. * @param $depth
  856. * @param array $tmp
  857. */
  858. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array()) {
  859. if (is_array($array)) {
  860. for ($i = 0; $i < count($array); $i++) {
  861. //var_dump($array[$i], $parent, $tmp);
  862. if ($array[$i]['parent_id'] == $parent) {
  863. if (!in_array($array[$i]['parent_id'], $tmp)) {
  864. $tmp[] = $array[$i]['parent_id'];
  865. $depth++;
  866. }
  867. $row = $array[$i];
  868. $row['id'] = $array[$i]['iid'];
  869. $row['depth'] = $depth;
  870. $this->category_array_tree[] = $row;
  871. $this->create_tree_array($array, $array[$i]['iid'], $depth, $tmp);
  872. }
  873. }
  874. }
  875. }
  876. }