CoursesAndSessionsCatalog.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraField;
  4. use Doctrine\Common\Collections\Criteria;
  5. use Doctrine\ORM\Query\Expr\Join;
  6. /**
  7. * @todo change class name
  8. * Class CoursesAndSessionsCatalog
  9. */
  10. class CoursesAndSessionsCatalog
  11. {
  12. const PAGE_LENGTH = 12;
  13. /**
  14. * Check the configuration for the courses and sessions catalog.
  15. *
  16. * @global array $_configuration Configuration
  17. *
  18. * @param int $value The value to check
  19. *
  20. * @return bool Whether the configuration is $value
  21. */
  22. public static function is($value = CATALOG_COURSES)
  23. {
  24. $showCoursesSessions = (int) api_get_setting('catalog_show_courses_sessions');
  25. if ($showCoursesSessions == $value) {
  26. return true;
  27. }
  28. return false;
  29. }
  30. /**
  31. * Check whether to display the sessions list.
  32. *
  33. * @global array $_configuration Configuration
  34. *
  35. * @return bool whether to display
  36. */
  37. public static function showSessions()
  38. {
  39. $catalogShow = (int) api_get_setting('catalog_show_courses_sessions');
  40. if ($catalogShow == CATALOG_SESSIONS || $catalogShow == CATALOG_COURSES_SESSIONS) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. /**
  46. * Check whether to display the courses list.
  47. *
  48. * @global array $_configuration Configuration
  49. *
  50. * @return bool whether to display
  51. */
  52. public static function showCourses()
  53. {
  54. $catalogShow = (int) api_get_setting('catalog_show_courses_sessions');
  55. if ($catalogShow == CATALOG_COURSES || $catalogShow == CATALOG_COURSES_SESSIONS) {
  56. return true;
  57. }
  58. return false;
  59. }
  60. /**
  61. * @return array
  62. */
  63. public static function getCoursesToAvoid()
  64. {
  65. $TABLE_COURSE_FIELD = Database::get_main_table(TABLE_EXTRA_FIELD);
  66. $TABLE_COURSE_FIELD_VALUE = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  67. // Check special courses
  68. $courseListToAvoid = CourseManager::get_special_course_list();
  69. // Checks "hide_from_catalog" extra field
  70. $extraFieldType = ExtraField::COURSE_FIELD_TYPE;
  71. $sql = "SELECT item_id FROM $TABLE_COURSE_FIELD_VALUE tcfv
  72. INNER JOIN $TABLE_COURSE_FIELD tcf
  73. ON tcfv.field_id = tcf.id
  74. WHERE
  75. tcf.extra_field_type = $extraFieldType AND
  76. tcf.variable = 'hide_from_catalog' AND
  77. tcfv.value = 1
  78. ";
  79. $result = Database::query($sql);
  80. if (Database::num_rows($result) > 0) {
  81. while ($row = Database::fetch_array($result)) {
  82. $courseListToAvoid[] = $row['item_id'];
  83. }
  84. }
  85. return $courseListToAvoid;
  86. }
  87. /**
  88. * @return string
  89. */
  90. public static function getAvoidCourseCondition()
  91. {
  92. $courseListToAvoid = self::getCoursesToAvoid();
  93. $condition = '';
  94. if (!empty($courseListToAvoid)) {
  95. $courses = [];
  96. foreach ($courseListToAvoid as $courseId) {
  97. $courses[] = '"'.$courseId.'"';
  98. }
  99. $condition = ' AND course.id NOT IN ('.implode(',', $courses).')';
  100. }
  101. return $condition;
  102. }
  103. /**
  104. * Get available le courses count.
  105. *
  106. * @param int $accessUrlId (optional)
  107. *
  108. * @return int Number of courses
  109. */
  110. public static function countAvailableCoursesToShowInCatalog($accessUrlId = 1)
  111. {
  112. $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
  113. $tableCourseRelAccessUrl = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  114. $courseToAvoidCondition = self::getAvoidCourseCondition();
  115. $visibilityCondition = CourseManager::getCourseVisibilitySQLCondition('course', true);
  116. $accessUrlId = (int) $accessUrlId;
  117. if (empty($accessUrlId)) {
  118. $accessUrlId = 1;
  119. }
  120. $sql = "SELECT count(course.id)
  121. FROM $tableCourse course
  122. INNER JOIN $tableCourseRelAccessUrl u
  123. ON (course.id = u.c_id)
  124. WHERE
  125. u.access_url_id = $accessUrlId AND
  126. course.visibility != 0 AND
  127. course.visibility != 4
  128. $courseToAvoidCondition
  129. $visibilityCondition
  130. ";
  131. $res = Database::query($sql);
  132. $row = Database::fetch_row($res);
  133. return $row[0];
  134. }
  135. public static function getCourseCategoriesTree()
  136. {
  137. $urlId = 1;
  138. if (api_is_multiple_url_enabled()) {
  139. $urlId = api_get_current_access_url_id();
  140. }
  141. $countCourses = self::countAvailableCoursesToShowInCatalog($urlId);
  142. $categories = [];
  143. $list = [];
  144. $categories['ALL'] = [
  145. 'id' => 0,
  146. 'name' => get_lang('DisplayAll'),
  147. 'code' => 'ALL',
  148. 'parent_id' => null,
  149. 'tree_pos' => 0,
  150. 'number_courses' => $countCourses,
  151. 'level' => 0,
  152. ];
  153. $allCategories = CourseCategory::getAllCategories();
  154. foreach ($allCategories as $category) {
  155. if (empty($category['parent_id'])) {
  156. $list[$category['code']] = $category;
  157. $list[$category['code']]['level'] = 0;
  158. list($subList, $childrenCount) = self::buildCourseCategoryTree($allCategories, $category['code'], 0);
  159. foreach ($subList as $item) {
  160. $list[$item['code']] = $item;
  161. }
  162. // Real course count
  163. $countCourses = CourseCategory::countCoursesInCategory($category['code']);
  164. $list[$category['code']]['number_courses'] = $childrenCount + $countCourses;
  165. }
  166. }
  167. // count courses that are in no category
  168. $countCourses = CourseCategory::countCoursesInCategory();
  169. $categories['NONE'] = [
  170. 'id' => 0,
  171. 'name' => get_lang('WithoutCategory'),
  172. 'code' => 'NONE',
  173. 'parent_id' => null,
  174. 'tree_pos' => 0,
  175. 'children_count' => 0,
  176. 'auth_course_child' => true,
  177. 'auth_cat_child' => true,
  178. 'number_courses' => $countCourses,
  179. 'level' => 0,
  180. ];
  181. $result = array_merge($list, $categories);
  182. return $result;
  183. }
  184. /**
  185. * @return array
  186. */
  187. public static function getCourseCategories()
  188. {
  189. $urlId = 1;
  190. if (api_is_multiple_url_enabled()) {
  191. $urlId = api_get_current_access_url_id();
  192. }
  193. $countCourses = self::countAvailableCoursesToShowInCatalog($urlId);
  194. $categories = [];
  195. $categories[0][0] = [
  196. 'id' => 0,
  197. 'name' => get_lang('DisplayAll'),
  198. 'code' => 'ALL',
  199. 'parent_id' => null,
  200. 'tree_pos' => 0,
  201. 'count_courses' => $countCourses,
  202. ];
  203. $categoriesFromDatabase = CourseCategory::getCategories();
  204. foreach ($categoriesFromDatabase as $row) {
  205. $countCourses = CourseCategory::countCoursesInCategory($row['code']);
  206. $row['count_courses'] = $countCourses;
  207. if (empty($row['parent_id'])) {
  208. $categories[0][$row['tree_pos']] = $row;
  209. } else {
  210. $categories[$row['parent_id']][$row['tree_pos']] = $row;
  211. }
  212. }
  213. // count courses that are in no category
  214. $countCourses = CourseCategory::countCoursesInCategory();
  215. $categories[0][count($categories[0]) + 1] = [
  216. 'id' => 0,
  217. 'name' => get_lang('None'),
  218. 'code' => 'NONE',
  219. 'parent_id' => null,
  220. 'tree_pos' => $row['tree_pos'] + 1,
  221. 'children_count' => 0,
  222. 'auth_course_child' => true,
  223. 'auth_cat_child' => true,
  224. 'count_courses' => $countCourses,
  225. ];
  226. return $categories;
  227. }
  228. /**
  229. * Return LIMIT to filter SQL query.
  230. *
  231. * @param array $limit
  232. *
  233. * @return string
  234. */
  235. public static function getLimitFilterFromArray($limit)
  236. {
  237. $limitFilter = '';
  238. if (!empty($limit) && is_array($limit)) {
  239. $limitStart = isset($limit['start']) ? (int) $limit['start'] : 0;
  240. $limitLength = isset($limit['length']) ? (int) $limit['length'] : 12;
  241. $limitFilter = 'LIMIT '.$limitStart.', '.$limitLength;
  242. }
  243. return $limitFilter;
  244. }
  245. /**
  246. * @param string $category_code
  247. * @param int $random_value
  248. * @param array $limit will be used if $random_value is not set.
  249. * This array should contains 'start' and 'length' keys
  250. *
  251. * @return array
  252. */
  253. public static function getCoursesInCategory($category_code, $random_value = null, $limit = [])
  254. {
  255. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  256. $avoidCoursesCondition = self::getAvoidCourseCondition();
  257. $visibilityCondition = CourseManager::getCourseVisibilitySQLCondition('course', true);
  258. if (!empty($random_value)) {
  259. $random_value = (int) $random_value;
  260. $sql = "SELECT COUNT(*) FROM $tbl_course";
  261. $result = Database::query($sql);
  262. list($num_records) = Database::fetch_row($result);
  263. if (api_is_multiple_url_enabled()) {
  264. $urlId = api_get_current_access_url_id();
  265. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  266. $urlCondition = ' access_url_id = '.$urlId.' ';
  267. $allowBaseCategories = api_get_configuration_value('allow_base_course_category');
  268. if ($allowBaseCategories) {
  269. $urlCondition = ' (access_url_id = '.$urlId.' OR access_url_id = 1) ';
  270. }
  271. $sql = "SELECT COUNT(*) FROM $tbl_course course
  272. INNER JOIN $tbl_url_rel_course as url_rel_course
  273. ON (url_rel_course.c_id = course.id)
  274. WHERE access_url_id = '.$urlId.' ";
  275. $result = Database::query($sql);
  276. list($num_records) = Database::fetch_row($result);
  277. $sql = "SELECT course.id, course.id as real_id
  278. FROM $tbl_course course
  279. INNER JOIN $tbl_url_rel_course as url_rel_course
  280. ON (url_rel_course.c_id = course.id)
  281. WHERE
  282. $urlCondition AND
  283. RAND()*$num_records< $random_value
  284. $avoidCoursesCondition
  285. $visibilityCondition
  286. ORDER BY RAND()
  287. LIMIT 0, $random_value";
  288. } else {
  289. $sql = "SELECT id, id as real_id FROM $tbl_course course
  290. WHERE
  291. RAND()*$num_records< $random_value
  292. $avoidCoursesCondition
  293. $visibilityCondition
  294. ORDER BY RAND()
  295. LIMIT 0, $random_value";
  296. }
  297. $result = Database::query($sql);
  298. $id_in = null;
  299. while (list($id) = Database::fetch_row($result)) {
  300. if ($id_in) {
  301. $id_in .= ",$id";
  302. } else {
  303. $id_in = "$id";
  304. }
  305. }
  306. if ($id_in === null) {
  307. return [];
  308. }
  309. $sql = "SELECT *, id as real_id FROM $tbl_course WHERE id IN($id_in)";
  310. } else {
  311. $limitFilter = self::getLimitFilterFromArray($limit);
  312. $category_code = Database::escape_string($category_code);
  313. $listCode = self::childrenCategories($category_code);
  314. $conditionCode = ' ';
  315. if (empty($listCode)) {
  316. if ($category_code === 'NONE') {
  317. $conditionCode .= " category_code='' ";
  318. } else {
  319. $conditionCode .= " category_code='$category_code' ";
  320. }
  321. } else {
  322. foreach ($listCode as $code) {
  323. $conditionCode .= " category_code='$code' OR ";
  324. }
  325. $conditionCode .= " category_code='$category_code' ";
  326. }
  327. if (empty($category_code) || $category_code == 'ALL') {
  328. $sql = "SELECT *, id as real_id
  329. FROM $tbl_course course
  330. WHERE
  331. 1=1
  332. $avoidCoursesCondition
  333. $visibilityCondition
  334. ORDER BY title $limitFilter ";
  335. } else {
  336. $sql = "SELECT *, id as real_id FROM $tbl_course course
  337. WHERE
  338. $conditionCode
  339. $avoidCoursesCondition
  340. $visibilityCondition
  341. ORDER BY title $limitFilter ";
  342. }
  343. // Showing only the courses of the current Chamilo access_url_id
  344. if (api_is_multiple_url_enabled()) {
  345. $urlId = api_get_current_access_url_id();
  346. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  347. $urlCondition = ' access_url_id = '.$urlId.' ';
  348. if ($category_code != 'ALL') {
  349. $sql = "SELECT *, course.id real_id FROM $tbl_course as course
  350. INNER JOIN $tbl_url_rel_course as url_rel_course
  351. ON (url_rel_course.c_id = course.id)
  352. WHERE
  353. $urlCondition AND
  354. $conditionCode
  355. $avoidCoursesCondition
  356. $visibilityCondition
  357. ORDER BY title $limitFilter";
  358. } else {
  359. $sql = "SELECT *, course.id real_id FROM $tbl_course as course
  360. INNER JOIN $tbl_url_rel_course as url_rel_course
  361. ON (url_rel_course.c_id = course.id)
  362. WHERE
  363. $urlCondition
  364. $avoidCoursesCondition
  365. $visibilityCondition
  366. ORDER BY title $limitFilter";
  367. }
  368. }
  369. }
  370. $result = Database::query($sql);
  371. $courses = [];
  372. while ($row = Database::fetch_array($result)) {
  373. $row['registration_code'] = !empty($row['registration_code']);
  374. $count_users = CourseManager::get_users_count_in_course($row['code']);
  375. $count_connections_last_month = Tracking::get_course_connections_count(
  376. $row['id'],
  377. 0,
  378. api_get_utc_datetime(time() - (30 * 86400))
  379. );
  380. if ($row['tutor_name'] == '0') {
  381. $row['tutor_name'] = get_lang('NoManager');
  382. }
  383. $point_info = CourseManager::get_course_ranking($row['id'], 0);
  384. $courses[] = [
  385. 'real_id' => $row['real_id'],
  386. 'point_info' => $point_info,
  387. 'code' => $row['code'],
  388. 'directory' => $row['directory'],
  389. 'visual_code' => $row['visual_code'],
  390. 'title' => $row['title'],
  391. 'tutor' => $row['tutor_name'],
  392. 'subscribe' => $row['subscribe'],
  393. 'unsubscribe' => $row['unsubscribe'],
  394. 'registration_code' => $row['registration_code'],
  395. 'creation_date' => $row['creation_date'],
  396. 'visibility' => $row['visibility'],
  397. 'category' => $row['category_code'],
  398. 'count_users' => $count_users,
  399. 'count_connections' => $count_connections_last_month,
  400. ];
  401. }
  402. return $courses;
  403. }
  404. /**
  405. * Search the courses database for a course that matches the search term.
  406. * The search is done on the code, title and tutor field of the course table.
  407. *
  408. * @param string $search_term The string that the user submitted, what we are looking for
  409. * @param array $limit
  410. * @param bool $justVisible search only on visible courses in the catalogue
  411. *
  412. * @return array an array containing a list of all the courses matching the the search term
  413. */
  414. public static function search_courses($search_term, $limit, $justVisible = false)
  415. {
  416. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  417. $limitFilter = self::getLimitFilterFromArray($limit);
  418. $avoidCoursesCondition = self::getAvoidCourseCondition();
  419. $visibilityCondition = $justVisible ? CourseManager::getCourseVisibilitySQLCondition('course', true) : '';
  420. $search_term_safe = Database::escape_string($search_term);
  421. $sql = "SELECT * FROM $courseTable course
  422. WHERE (
  423. course.code LIKE '%".$search_term_safe."%' OR
  424. course.title LIKE '%".$search_term_safe."%' OR
  425. course.tutor_name LIKE '%".$search_term_safe."%'
  426. )
  427. $avoidCoursesCondition
  428. $visibilityCondition
  429. ORDER BY title, visual_code ASC
  430. $limitFilter
  431. ";
  432. if (api_is_multiple_url_enabled()) {
  433. $urlId = api_get_current_access_url_id();
  434. if ($urlId != -1) {
  435. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  436. $urlCondition = ' access_url_id = '.$urlId.' AND';
  437. $allowBaseCategories = api_get_configuration_value('allow_base_course_category');
  438. if ($allowBaseCategories) {
  439. $urlCondition = ' (access_url_id = '.$urlId.' OR access_url_id = 1) AND ';
  440. }
  441. $sql = "SELECT course.*
  442. FROM $courseTable as course
  443. INNER JOIN $tbl_url_rel_course as url_rel_course
  444. ON (url_rel_course.c_id = course.id)
  445. WHERE
  446. access_url_id = $urlId AND
  447. (
  448. code LIKE '%".$search_term_safe."%' OR
  449. title LIKE '%".$search_term_safe."%' OR
  450. tutor_name LIKE '%".$search_term_safe."%'
  451. )
  452. $avoidCoursesCondition
  453. $visibilityCondition
  454. ORDER BY title, visual_code ASC
  455. $limitFilter
  456. ";
  457. }
  458. }
  459. $result_find = Database::query($sql);
  460. $courses = [];
  461. while ($row = Database::fetch_array($result_find)) {
  462. $row['registration_code'] = !empty($row['registration_code']);
  463. $countUsers = CourseManager::get_user_list_from_course_code(
  464. $row['code'],
  465. 0,
  466. null,
  467. null,
  468. null,
  469. true
  470. );
  471. $connectionsLastMonth = Tracking::get_course_connections_count(
  472. $row['id'],
  473. 0,
  474. api_get_utc_datetime(time() - (30 * 86400))
  475. );
  476. $point_info = CourseManager::get_course_ranking($row['id'], 0);
  477. $courses[] = [
  478. 'real_id' => $row['id'],
  479. 'point_info' => $point_info,
  480. 'code' => $row['code'],
  481. 'directory' => $row['directory'],
  482. 'visual_code' => $row['visual_code'],
  483. 'title' => $row['title'],
  484. 'tutor' => $row['tutor_name'],
  485. 'subscribe' => $row['subscribe'],
  486. 'unsubscribe' => $row['unsubscribe'],
  487. 'registration_code' => $row['registration_code'],
  488. 'creation_date' => $row['creation_date'],
  489. 'visibility' => $row['visibility'],
  490. 'count_users' => $countUsers,
  491. 'count_connections' => $connectionsLastMonth,
  492. ];
  493. }
  494. return $courses;
  495. }
  496. /**
  497. * List the sessions.
  498. *
  499. * @param string $date (optional) The date of sessions
  500. * @param array $limit
  501. *
  502. * @throws Exception
  503. *
  504. * @return array The session list
  505. */
  506. public static function browseSessions($date = null, $limit = [], $returnQueryBuilder = false)
  507. {
  508. $urlId = api_get_current_access_url_id();
  509. $dql = "SELECT s
  510. FROM ChamiloCoreBundle:Session s
  511. WHERE EXISTS
  512. (
  513. SELECT url.sessionId FROM ChamiloCoreBundle:AccessUrlRelSession url
  514. WHERE url.sessionId = s.id AND url.accessUrlId = $urlId
  515. ) AND
  516. s.nbrCourses > 0
  517. ";
  518. //('$date' BETWEEN s.accessStartDate AND s.accessEndDate)
  519. if (!is_null($date)) {
  520. $date = Database::escape_string($date);
  521. $dql .= "
  522. AND (
  523. (s.accessEndDate IS NULL)
  524. OR
  525. (
  526. s.accessStartDate IS NOT NULL AND
  527. s.accessEndDate IS NOT NULL AND
  528. s.accessStartDate >= '$date' AND s.accessEndDate <= '$date')
  529. OR
  530. (
  531. s.accessStartDate IS NULL AND
  532. s.accessEndDate IS NOT NULL AND
  533. s.accessStartDate >= '$date'
  534. )
  535. )
  536. ";
  537. }
  538. $qb = Database::getManager()->createQuery($dql);
  539. if (!empty($limit)) {
  540. $qb
  541. ->setFirstResult($limit['start'])
  542. ->setMaxResults($limit['length'])
  543. ;
  544. }
  545. if ($returnQueryBuilder) {
  546. return $qb;
  547. }
  548. return $qb->getResult();
  549. }
  550. /**
  551. * Search sessions by searched term by session name.
  552. *
  553. * @param string $queryTerm Term for search
  554. * @param array $limit Limit info
  555. *
  556. * @return array The sessions
  557. */
  558. public static function browseSessionsBySearch($queryTerm, array $limit)
  559. {
  560. $sessionsToBrowse = [];
  561. $criteria = Criteria::create()
  562. ->where(
  563. Criteria::expr()->contains('name', $queryTerm)
  564. )
  565. ->setFirstResult($limit['start'])
  566. ->setMaxResults($limit['length']);
  567. $sessions = Database::getManager()
  568. ->getRepository('ChamiloCoreBundle:Session')
  569. ->matching($criteria);
  570. foreach ($sessions as $session) {
  571. if ($session->getNbrCourses() === 0) {
  572. continue;
  573. }
  574. $sessionsToBrowse[] = $session;
  575. }
  576. return $sessionsToBrowse;
  577. }
  578. /**
  579. * Search sessions by the tags in their courses.
  580. *
  581. * @param string $termTag Term for search in tags
  582. * @param array $limit Limit info
  583. *
  584. * @return array The sessions
  585. */
  586. public static function browseSessionsByTags($termTag, array $limit)
  587. {
  588. $em = Database::getManager();
  589. $qb = $em->createQueryBuilder();
  590. $sessions = $qb->select('s')
  591. ->distinct(true)
  592. ->from('ChamiloCoreBundle:Session', 's')
  593. ->innerJoin(
  594. 'ChamiloCoreBundle:SessionRelCourse',
  595. 'src',
  596. Join::WITH,
  597. 's.id = src.session'
  598. )
  599. ->innerJoin(
  600. 'ChamiloCoreBundle:ExtraFieldRelTag',
  601. 'frt',
  602. Join::WITH,
  603. 'src.course = frt.itemId'
  604. )
  605. ->innerJoin(
  606. 'ChamiloCoreBundle:Tag',
  607. 't',
  608. Join::WITH,
  609. 'frt.tagId = t.id'
  610. )
  611. ->innerJoin(
  612. 'ChamiloCoreBundle:ExtraField',
  613. 'f',
  614. Join::WITH,
  615. 'frt.fieldId = f.id'
  616. )
  617. ->where(
  618. $qb->expr()->like('t.tag', ':tag')
  619. )
  620. ->andWhere(
  621. $qb->expr()->eq('f.extraFieldType', ExtraField::COURSE_FIELD_TYPE)
  622. )
  623. ->setFirstResult($limit['start'])
  624. ->setMaxResults($limit['length'])
  625. ->setParameter('tag', "$termTag%")
  626. ->getQuery()
  627. ->getResult();
  628. $sessionsToBrowse = [];
  629. foreach ($sessions as $session) {
  630. if ($session->getNbrCourses() === 0) {
  631. continue;
  632. }
  633. $sessionsToBrowse[] = $session;
  634. }
  635. return $sessionsToBrowse;
  636. }
  637. /**
  638. * Build a recursive tree of course categories.
  639. *
  640. * @param $categories
  641. * @param $parentId
  642. *
  643. * @return array
  644. */
  645. public static function buildCourseCategoryTree($categories, $parentId = 0, $level = 0)
  646. {
  647. $list = [];
  648. $count = 0;
  649. $level++;
  650. foreach ($categories as $category) {
  651. if (empty($category['parent_id'])) {
  652. continue;
  653. }
  654. if ($category['parent_id'] == $parentId) {
  655. $list[$category['code']] = $category;
  656. $count += $category['number_courses'];
  657. $list[$category['code']]['level'] = $level;
  658. list($subList, $childrenCount) = self::buildCourseCategoryTree(
  659. $categories,
  660. $category['code'],
  661. $level
  662. );
  663. $list[$category['code']]['number_courses'] += $childrenCount;
  664. foreach ($subList as $item) {
  665. $list[$item['code']] = $item;
  666. }
  667. $count += $childrenCount;
  668. }
  669. }
  670. return [$list, $count];
  671. }
  672. /**
  673. * List Code Search Category.
  674. *
  675. * @param $code
  676. *
  677. * @return array
  678. */
  679. public static function childrenCategories($code)
  680. {
  681. $allCategories = CourseCategory::getAllCategories();
  682. $list = [];
  683. $row = [];
  684. if ($code != 'ALL' and $code != 'NONE') {
  685. foreach ($allCategories as $category) {
  686. if ($category['code'] === $code) {
  687. $list = self::buildCourseCategoryTree($allCategories, $category['code'], 0);
  688. }
  689. }
  690. foreach ($list[0] as $item) {
  691. $row[] = $item['code'];
  692. }
  693. }
  694. return $row;
  695. }
  696. }