classmanager.lib.php 9.8 KB

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