classmanager.lib.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the static class library for this application.
  5. * @package chamilo.library
  6. */
  7. /**
  8. * Code
  9. */
  10. class ClassManager
  11. {
  12. /**
  13. * Get class information
  14. * note: This function can't be named get_class() because that's a standard
  15. * php-function.
  16. */
  17. public static function get_class_info($class_id) {
  18. $class_id = intval($class_id);
  19. $table_class = Database :: get_main_table(TABLE_MAIN_CLASS);
  20. $sql = "SELECT * FROM $table_class WHERE id='".$class_id."'";
  21. $res = Database::query($sql);
  22. return Database::fetch_array($res, 'ASSOC');
  23. }
  24. /**
  25. * Change the name of a class
  26. * @param string $name The new name
  27. * @param int $class_id The class id
  28. */
  29. public static function set_name($name, $class_id) {
  30. $class_id = intval($class_id);
  31. $table_class = Database :: get_main_table(TABLE_MAIN_CLASS);
  32. $sql = "UPDATE $table_class SET name='".Database::escape_string($name)."' WHERE id='".$class_id."'";
  33. $res = Database::query($sql);
  34. }
  35. /**
  36. * Create a class
  37. * @param string $name
  38. */
  39. public static function create_class($name) {
  40. $table_class = Database :: get_main_table(TABLE_MAIN_CLASS);
  41. $sql = "INSERT INTO $table_class SET name='".Database::escape_string($name)."'";
  42. Database::query($sql);
  43. return Database::affected_rows() == 1;
  44. }
  45. /**
  46. * Check if a classname is allready in use
  47. * @param string $name
  48. */
  49. public static function class_name_exists($name) {
  50. $table_class = Database :: get_main_table(TABLE_MAIN_CLASS);
  51. $sql = "SELECT * FROM $table_class WHERE name='".Database::escape_string($name)."'";
  52. $res = Database::query($sql);
  53. return Database::num_rows($res) != 0;
  54. }
  55. /**
  56. * Delete a class
  57. * @param int $class_id
  58. * @todo Add option to unsubscribe class-members from the courses where the
  59. * class was subscibed to
  60. */
  61. public static function delete_class($class_id) {
  62. $class_id = intval($class_id);
  63. $table_class = Database :: get_main_table(TABLE_MAIN_CLASS);
  64. $table_class_course = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  65. $table_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  66. $sql = "DELETE FROM $table_class_user WHERE class_id = '".$class_id."'";
  67. Database::query($sql);
  68. $sql = "DELETE FROM $table_class_course WHERE class_id = '".$class_id."'";
  69. Database::query($sql);
  70. $sql = "DELETE FROM $table_class WHERE id = '".$class_id."'";
  71. Database::query($sql);
  72. }
  73. /**
  74. * Get all users from a class
  75. * @param int $class_id
  76. * @return array
  77. */
  78. public static function get_users($class_id) {
  79. $class_id = intval($class_id);
  80. $table_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  81. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  82. $sql = "SELECT * FROM $table_class_user cu, $table_user u WHERE cu.class_id = '".$class_id."' AND cu.user_id = u.user_id";
  83. $res = Database::query($sql);
  84. $users = array ();
  85. while ($user = Database::fetch_array($res, 'ASSOC')) {
  86. $users[] = $user;
  87. }
  88. return $users;
  89. }
  90. /**
  91. * Add a user to a class. If the class is subscribed to a course, the new
  92. * user will also be subscribed to that course.
  93. * @param int $user_id The user id
  94. * @param int $class_id The class id
  95. */
  96. public static function add_user($user_id, $class_id) {
  97. $table_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  98. $user_id = intval($user_id);
  99. $class_id = intval($class_id);
  100. $sql = "INSERT IGNORE INTO $table_class_user SET user_id = '".$user_id."', class_id='".$class_id."'";
  101. Database::query($sql);
  102. $courses = ClassManager :: get_courses($class_id);
  103. foreach ($courses as $index => $course) {
  104. CourseManager :: subscribe_user($user_id, $course['course_code']);
  105. }
  106. }
  107. /**
  108. * Unsubscribe a user from a class. If the class is also subscribed in a
  109. * course, the user will be unsubscribed from that course
  110. * @param int $user_id The user id
  111. * @param int $class_id The class id
  112. */
  113. public static function unsubscribe_user($user_id, $class_id) {
  114. $class_id = intval($class_id);
  115. $user_id = intval($user_id);
  116. $table_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  117. $table_course_class = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  118. $courses = ClassManager :: get_courses($class_id);
  119. if (count($courses) != 0) {
  120. $course_codes = array ();
  121. foreach ($courses as $index => $course) {
  122. $course_codes[] = $course['course_code'];
  123. $sql = "SELECT DISTINCT user_id FROM $table_class_user t1, $table_course_class t2 WHERE t1.class_id=t2.class_id AND course_code = '".$course['course_code']."' AND user_id = $user_id AND t2.class_id<>'$class_id'";
  124. $res = Database::query($sql);
  125. if (Database::num_rows($res) == 0 && CourseManager :: get_user_in_course_status($user_id, $course['course_code']) == STUDENT)
  126. {
  127. CourseManager :: unsubscribe_user($user_id, $course['course_code']);
  128. }
  129. }
  130. }
  131. $sql = "DELETE FROM $table_class_user WHERE user_id='".$user_id."' AND class_id = '".$class_id."'";
  132. Database::query($sql);
  133. }
  134. /**
  135. * Get all courses in which a class is subscribed
  136. * @param int $class_id
  137. * @return array
  138. */
  139. public static function get_courses($class_id) {
  140. $class_id = intval($class_id);
  141. $table_class_course = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  142. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  143. $sql = "SELECT * FROM $table_class_course cc, $table_course c WHERE cc.class_id = '".$class_id."' AND cc.course_code = c.code";
  144. $res = Database::query($sql);
  145. $courses = array ();
  146. while ($course = Database::fetch_array($res, 'ASSOC')) {
  147. $courses[] = $course;
  148. }
  149. return $courses;
  150. }
  151. /**
  152. * Subscribe all members of a class to a course
  153. * @param int $class_id The class id
  154. * @param string $course_code The course code
  155. */
  156. public static function subscribe_to_course($class_id, $course_code) {
  157. $tbl_course_class = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  158. $tbl_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  159. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  160. $sql = "INSERT IGNORE INTO $tbl_course_class SET course_code = '".Database::escape_string($course_code)."', class_id = '".Database::escape_string($class_id)."'";
  161. Database::query($sql);
  162. $sql = "SELECT user_id FROM $tbl_class_user WHERE class_id = '".intval($class_id)."'";
  163. $res = Database::query($sql);
  164. while ($user = Database::fetch_object($res)) {
  165. CourseManager :: subscribe_user($user->user_id, $course_code);
  166. }
  167. }
  168. /**
  169. * Unsubscribe a class from a course.
  170. * Only students are unsubscribed. If a user is member of 2 classes which
  171. * are both subscribed to the course, the user stays in the course.
  172. * @param int $class_id The class id
  173. * @param string $course_code The course code
  174. */
  175. public static function unsubscribe_from_course($class_id, $course_code)
  176. {
  177. $tbl_course_class = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  178. $tbl_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  179. $sql = "SELECT cu.user_id,COUNT(cc.class_id) FROM $tbl_course_class cc, $tbl_class_user cu WHERE cc.class_id = cu.class_id AND cc.course_code = '".Database::escape_string($course_code)."' GROUP BY cu.user_id HAVING COUNT(cc.class_id) = 1";
  180. $single_class_users = Database::query($sql);
  181. while ($single_class_user = Database::fetch_object($single_class_users))
  182. {
  183. $sql = "SELECT * FROM $tbl_class_user WHERE class_id = '".intval($class_id)."' AND user_id = '".Database::escape_string($single_class_user->user_id)."'";
  184. $res = Database::query($sql);
  185. if (Database::num_rows($res) > 0)
  186. {
  187. if (CourseManager :: get_user_in_course_status($single_class_user->user_id, $course_code) == STUDENT)
  188. {
  189. CourseManager :: unsubscribe_user($single_class_user->user_id, $course_code);
  190. }
  191. }
  192. }
  193. $sql = "DELETE FROM $tbl_course_class WHERE course_code = '".Database::escape_string($course_code)."' AND class_id = '".Database::escape_string($class_id)."'";
  194. Database::query($sql);
  195. }
  196. /**
  197. * Get the class-id
  198. * @param string $name The class name
  199. * @return int the ID of the class
  200. */
  201. public static function get_class_id($name) {
  202. $name = Database::escape_string($name);
  203. $table_class = Database :: get_main_table(TABLE_MAIN_CLASS);
  204. $sql = "SELECT * FROM $table_class WHERE name='".$name."'";
  205. $res = Database::query($sql);
  206. $obj = Database::fetch_object($res);
  207. return $obj->id;
  208. }
  209. /**
  210. * Get all classes subscribed in a course
  211. * @param string $course_code
  212. * @return array An array with all classes (keys: 'id','code','name')
  213. */
  214. public static function get_classes_in_course($course_code) {
  215. $table_class = Database :: get_main_table(TABLE_MAIN_CLASS);
  216. $table_course_class = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  217. $sql = "SELECT cl.* FROM $table_class cl, $table_course_class cc WHERE cc.course_code = '".Database::escape_string($course_code)."' AND cc.class_id = cl.id";
  218. $res = Database::query($sql);
  219. $classes = array ();
  220. while ($class = Database::fetch_array($res, 'ASSOC')) {
  221. $classes[] = $class;
  222. }
  223. return $classes;
  224. }
  225. }