classmanager.lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. $em = Database::getManager();
  61. $table_class = Database :: get_main_table(TABLE_MAIN_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. $em
  66. ->createQuery('DELETE FROM ChamiloCoreBundle:CourseRelClass cc WHERE cc.classId = :class')
  67. ->execute([
  68. 'class' => $class_id
  69. ]);
  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. $em = Database::getManager();
  117. $table_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  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. $dql = '
  124. SELECT DISTICT cu.userId
  125. FROM ChamiloCoreBundle:ClassUser cu, ChamiloCoreBundle:CourseRelClass cc
  126. WHERE
  127. cu.classId = cc.classId AND
  128. cc.courseId = :course AND
  129. cu.userId = :user AND
  130. cc.classId != :class
  131. ';
  132. $res = $em
  133. ->createQuery($dql)
  134. ->setParameters([
  135. 'course' => $course['id'],
  136. 'user' => $user_id,
  137. 'class' => $class_id
  138. ])
  139. ->getResult();
  140. if (count($res) === 0 && CourseManager :: get_user_in_course_status($user_id, $course['course_code']) == STUDENT) {
  141. CourseManager :: unsubscribe_user($user_id, $course['course_code']);
  142. }
  143. }
  144. }
  145. $sql = "DELETE FROM $table_class_user WHERE user_id='".$user_id."' AND class_id = '".$class_id."'";
  146. Database::query($sql);
  147. }
  148. /**
  149. * Get all courses in which a class is subscribed
  150. * @param int $class_id
  151. * @return array
  152. */
  153. public static function get_courses($class_id) {
  154. $class_id = intval($class_id);
  155. $table_class_course = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  156. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  157. $sql = "
  158. SELECT *, c.code AS course_code FROM $table_class_course cc, $table_course c
  159. WHERE cc.class_id = '".$class_id."' AND cc.c_id = c.id
  160. ";
  161. $res = Database::query($sql);
  162. $courses = array ();
  163. while ($course = Database::fetch_array($res, 'ASSOC')) {
  164. $courses[] = $course;
  165. }
  166. return $courses;
  167. }
  168. /**
  169. * Subscribe all members of a class to a course
  170. * @param int $class_id The class id
  171. * @param string $course_code The course code
  172. */
  173. public static function subscribe_to_course($class_id, $course_code)
  174. {
  175. $courseId = api_get_course_int_id($course_code);
  176. $tbl_course_class = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  177. $tbl_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  178. $sql = "
  179. INSERT IGNORE INTO $tbl_course_class
  180. SET c_id = " . $courseId . ",
  181. class_id = '" . Database::escape_string($class_id) . "'
  182. ";
  183. Database::query($sql);
  184. $sql = "SELECT user_id FROM $tbl_class_user WHERE class_id = '".intval($class_id)."'";
  185. $res = Database::query($sql);
  186. while ($user = Database::fetch_object($res)) {
  187. CourseManager :: subscribe_user($user->user_id, $course_code);
  188. }
  189. }
  190. /**
  191. * Unsubscribe a class from a course.
  192. * Only students are unsubscribed. If a user is member of 2 classes which
  193. * are both subscribed to the course, the user stays in the course.
  194. * @param int $class_id The class id
  195. * @param string $course_code The course code
  196. */
  197. public static function unsubscribe_from_course($class_id, $course_code)
  198. {
  199. $courseId = api_get_course_int_id($course_code);
  200. $tbl_course_class = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  201. $tbl_class_user = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  202. $sql = "
  203. SELECT cu.user_id, COUNT(cc.class_id)
  204. FROM $tbl_course_class cc, $tbl_class_user cu
  205. WHERE cc.class_id = cu.class_id AND
  206. cc.c_id = " . $courseId . "
  207. GROUP BY cu.user_id HAVING COUNT(cc.class_id) = 1
  208. ";
  209. $single_class_users = Database::query($sql);
  210. while ($single_class_user = Database::fetch_object($single_class_users))
  211. {
  212. $sql = "SELECT * FROM $tbl_class_user WHERE class_id = '".intval($class_id)."' AND user_id = '".Database::escape_string($single_class_user->user_id)."'";
  213. $res = Database::query($sql);
  214. if (Database::num_rows($res) > 0)
  215. {
  216. if (CourseManager :: get_user_in_course_status($single_class_user->user_id, $course_code) == STUDENT)
  217. {
  218. CourseManager :: unsubscribe_user($single_class_user->user_id, $course_code);
  219. }
  220. }
  221. }
  222. $sql = "
  223. DELETE FROM $tbl_course_class
  224. WHERE c_id = " . $courseId . " AND class_id = '" . Database::escape_string($class_id) . "'
  225. ";
  226. Database::query($sql);
  227. }
  228. /**
  229. * Get the class-id
  230. * @param string $name The class name
  231. * @return int the ID of the class
  232. */
  233. public static function get_class_id($name) {
  234. $name = Database::escape_string($name);
  235. $table_class = Database :: get_main_table(TABLE_MAIN_CLASS);
  236. $sql = "SELECT * FROM $table_class WHERE name='".$name."'";
  237. $res = Database::query($sql);
  238. $obj = Database::fetch_object($res);
  239. return $obj->id;
  240. }
  241. /**
  242. * Get all classes subscribed in a course
  243. * @param string $course_code
  244. * @return array An array with all classes (keys: 'id','code','name')
  245. */
  246. public static function get_classes_in_course($course_code) {
  247. $courseId = api_get_course_int_id($course_code);
  248. $table_class = Database :: get_main_table(TABLE_MAIN_CLASS);
  249. $table_course_class = Database :: get_main_table(TABLE_MAIN_COURSE_CLASS);
  250. $sql = "
  251. SELECT cl.* FROM $table_class cl, $table_course_class cc
  252. WHERE cc.c_id = " . $course_code . " AND cc.class_id = cl.id
  253. ";
  254. $res = Database::query($sql);
  255. $classes = array ();
  256. while ($class = Database::fetch_array($res, 'ASSOC')) {
  257. $classes[] = $class;
  258. }
  259. return $classes;
  260. }
  261. }