add_courses_to_session_functions.lib.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Definition of the AddCourseToSession class
  5. * @package chamilo.library
  6. */
  7. /**
  8. * Init
  9. */
  10. require_once dirname(__FILE__).'/xajax/xajax.inc.php';
  11. /**
  12. * AddCourseToSession class
  13. */
  14. class AddCourseToSession {
  15. /**
  16. * Searches a course, given a search string and a type of search box
  17. * @param string Search string
  18. * @param string Type of search box ('single' or anything else)
  19. * @return string XajaxResponse
  20. * @assert () !== null
  21. * @assert ('abc', 'single') !== null
  22. * @assert ('abc', 'multiple') !== null
  23. */
  24. public function search_courses($needle,$type) {
  25. global $tbl_course, $tbl_session_rel_course, $id_session;
  26. $xajax_response = new XajaxResponse();
  27. $return = '';
  28. if(!empty($needle) && !empty($type)) {
  29. // xajax send utf8 datas... datas in db can be non-utf8 datas
  30. $charset = api_get_system_encoding();
  31. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  32. $cond_course_code = '';
  33. if (!empty($id_session)) {
  34. $id_session = Database::escape_string($id_session);
  35. // check course_code from session_rel_course table
  36. $sql = 'SELECT course_code FROM '.$tbl_session_rel_course.' WHERE id_session ="'.(int)$id_session.'"';
  37. $res = Database::query($sql);
  38. $course_codes = '';
  39. if (Database::num_rows($res) > 0) {
  40. while ($row = Database::fetch_row($res)) {
  41. $course_codes .= '\''.$row[0].'\',';
  42. }
  43. $course_codes = substr($course_codes,0,(strlen($course_codes)-1));
  44. $cond_course_code = ' AND course.code NOT IN('.$course_codes.') ';
  45. }
  46. }
  47. if ($type=='single') {
  48. // search users where username or firstname or lastname begins likes $needle
  49. $sql = 'SELECT course.code, course.visual_code, course.title, session_rel_course.id_session
  50. FROM '.$tbl_course.' course
  51. LEFT JOIN '.$tbl_session_rel_course.' session_rel_course
  52. ON course.code = session_rel_course.course_code
  53. AND session_rel_course.id_session = '.intval($id_session).'
  54. WHERE course.visual_code LIKE "'.$needle.'%"
  55. OR course.title LIKE "'.$needle.'%"';
  56. } else {
  57. $sql = 'SELECT course.code, course.visual_code, course.title
  58. FROM '.$tbl_course.' course
  59. WHERE course.visual_code LIKE "'.$needle.'%" '.$cond_course_code.' ORDER BY course.code ';
  60. }
  61. if (api_is_multiple_url_enabled()) {
  62. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  63. $access_url_id = api_get_current_access_url_id();
  64. if ($access_url_id != -1){
  65. if ($type=='single') {
  66. $sql = 'SELECT course.code, course.visual_code, course.title, session_rel_course.id_session
  67. FROM '.$tbl_course.' course
  68. LEFT JOIN '.$tbl_session_rel_course.' session_rel_course
  69. ON course.code = session_rel_course.course_code
  70. AND session_rel_course.id_session = '.intval($id_session).'
  71. INNER JOIN '.$tbl_course_rel_access_url.' url_course ON (url_course.c_id = course.id)
  72. WHERE access_url_id = '.$access_url_id.' AND (course.visual_code LIKE "'.$needle.'%"
  73. OR course.title LIKE "'.$needle.'%" )';
  74. } else {
  75. $sql = 'SELECT course.code, course.visual_code, course.title
  76. FROM '.$tbl_course.' course, '.$tbl_course_rel_access_url.' url_course
  77. WHERE url_course.c_id = course.od AND access_url_id = '.$access_url_id.'
  78. AND course.visual_code LIKE "'.$needle.'%" '.$cond_course_code.' ORDER BY course.code ';
  79. }
  80. }
  81. }
  82. $rs = Database::query($sql);
  83. $course_list = array();
  84. if ($type=='single') {
  85. while($course = Database :: fetch_array($rs)) {
  86. $course_list[] = $course['code'];
  87. $course_title=str_replace("'","\'",$course_title);
  88. $return .= '<a href="javascript: void(0);" onclick="javascript: add_course_to_session(\''.$course['code'].'\',\''.$course_title.' ('.$course['visual_code'].')'.'\')">'.$course['title'].' ('.$course['visual_code'].')</a><br />';
  89. }
  90. $xajax_response -> addAssign('ajax_list_courses_single','innerHTML',api_utf8_encode($return));
  91. } else {
  92. $return .= '<select id="origin" name="NoSessionCoursesList[]" multiple="multiple" size="20" style="width:340px;">';
  93. while($course = Database :: fetch_array($rs)) {
  94. $course_list[] = $course['code'];
  95. $course_title=str_replace("'","\'",$course_title);
  96. $return .= '<option value="'.$course['code'].'" title="'.htmlspecialchars($course['title'].' ('.$course['visual_code'].')',ENT_QUOTES).'">'.$course['title'].' ('.$course['visual_code'].')</option>';
  97. }
  98. $return .= '</select>';
  99. $xajax_response -> addAssign('ajax_list_courses_multiple','innerHTML',api_utf8_encode($return));
  100. }
  101. }
  102. $_SESSION['course_list'] = $course_list;
  103. return $xajax_response;
  104. }
  105. }