gradebook_functions_users.inc.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Various user related functions
  5. * @author Julio Montoya <gugli100@gmail.com> adding security functions
  6. * @package chamilo.gradebook
  7. */
  8. /**
  9. * returns users within a course given by param
  10. * @param $course_id
  11. */
  12. function get_users_in_course($courseId) {
  13. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  14. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  15. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  16. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname ASC' : ' ORDER BY lastname, firstname ASC';
  17. $current_session = api_get_session_id();
  18. $courseId = Database::escape_string($courseId);
  19. if (!empty($current_session)) {
  20. $sql = "SELECT user.user_id, user.username, lastname, firstname, official_code
  21. FROM $tbl_session_course_user as scru, $tbl_user as user
  22. WHERE scru.id_user=user.user_id
  23. AND scru.status=0
  24. AND scru.c_id ='$courseId' AND id_session = '$current_session'
  25. $order_clause ";
  26. } else {
  27. $sql = 'SELECT user.user_id, user.username, lastname, firstname, official_code
  28. FROM '.$tbl_course_user.' as course_rel_user, '.$tbl_user.' as user
  29. WHERE course_rel_user.user_id=user.user_id AND
  30. course_rel_user.status='.STUDENT.' AND
  31. course_rel_user.c_id = '.$courseId.'
  32. '.$order_clause;
  33. }
  34. $result = Database::query($sql);
  35. return get_user_array_from_sql_result($result);
  36. }
  37. function get_user_array_from_sql_result($result) {
  38. $a_students = array();
  39. while ($user = Database::fetch_array($result)) {
  40. if (!array_key_exists($user['user_id'], $a_students)) {
  41. $a_current_student = array ();
  42. $a_current_student[] = $user['user_id'];
  43. $a_current_student[] = $user['username'];
  44. $a_current_student[] = $user['lastname'];
  45. $a_current_student[] = $user['firstname'];
  46. $a_current_student[] = $user['official_code'];
  47. $a_students['STUD'.$user['user_id']] = $a_current_student;
  48. }
  49. }
  50. return $a_students;
  51. }
  52. function get_all_users ($evals = array(), $links = array()) {
  53. $coursecodes = array();
  54. $users = array();
  55. foreach ($evals as $eval) {
  56. $coursecode = $eval->get_course_code();
  57. $courseInfo = api_get_course_info($coursecode);
  58. // evaluation in course
  59. if (isset($coursecode) && !empty($coursecode)) {
  60. if (!array_key_exists($coursecode,$coursecodes)) {
  61. $coursecodes[$coursecode] = '1';
  62. $users = array_merge($users, get_users_in_course($courseInfo['real_id']));
  63. }
  64. } else {// course independent evaluation
  65. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  66. $tbl_res = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  67. $sql = 'SELECT user.user_id,lastname, firstname, user.official_code
  68. FROM '.$tbl_res.' as res, '.$tbl_user.' as user
  69. WHERE res.evaluation_id = '.intval($eval->get_id()).' AND res.user_id = user.user_id';
  70. $result = Database::query($sql);
  71. $users = array_merge($users, get_user_array_from_sql_result($result));
  72. }
  73. }
  74. foreach ($links as $link) {
  75. // links are always in a course
  76. $coursecode = $link->get_course_code();
  77. $courseInfo = api_get_course_info($coursecode);
  78. if (!array_key_exists($coursecode,$coursecodes)) {
  79. $coursecodes[$coursecode] = '1';
  80. $users = array_merge($users, get_users_in_course($courseInfo['real_id']));
  81. }
  82. }
  83. unset ($coursecodes);
  84. return $users;
  85. }
  86. /**
  87. * Search students matching a given last name and/or first name
  88. * @author Bert Steppé
  89. */
  90. function find_students($mask= '') {
  91. // students shouldn't be here // don't search if mask empty
  92. if (!api_is_allowed_to_edit() || empty($mask)) {
  93. return null;
  94. }
  95. $mask = Database::escape_string($mask);
  96. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  97. $tbl_cru = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  98. $sql= 'SELECT DISTINCT user.user_id, user.lastname, user.firstname, user.email, user.official_code ' . ' FROM ' . $tbl_user . ' user';
  99. if (!api_is_platform_admin()) {
  100. $sql .= ', ' . $tbl_cru . ' cru';
  101. }
  102. $sql .= ' WHERE user.status = ' . STUDENT;
  103. $sql .= ' AND (user.lastname LIKE '."'%" . $mask . "%'";
  104. $sql .= ' OR user.firstname LIKE '."'%" . $mask . "%')";
  105. if (!api_is_platform_admin()) {
  106. $sql .= ' AND user.user_id = cru.user_id AND
  107. cru.relation_type <> '.COURSE_RELATION_TYPE_RRHH.' ' . ' AND
  108. cru.c_id in' . ' (
  109. SELECT c_id FROM '.$tbl_cru.' WHERE user_id = ' . api_get_user_id() . ' AND status = ' . COURSEMANAGER . '
  110. )';
  111. }
  112. $sql .= ' ORDER BY lastname';
  113. $result= Database::query($sql);
  114. $db_users= Database::store_result($result);
  115. return $db_users;
  116. }