add_courses_to_session_functions.lib.php 4.8 KB

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