sessionmanager.lib.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2009 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. Copyright (c) Bart Mollet, Hogeschool Gent
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * This library provides functions for user management.
  23. * Include/require it in your code to use its functionality.
  24. *
  25. * @package dokeos.library
  26. ==============================================================================
  27. */
  28. require_once('display.lib.php');
  29. class SessionManager{
  30. /** Create a session
  31. * @author Carlos Vargas <carlos.vargas@dokeos.com>,
  32. * @param array name, year_start,month_start, day_start,year_end,month_end,day_end,nb_days_acess_before,nb_days_acess_after
  33. **/
  34. function AddSession($name,$year_start,$month_start,$day_start,$year_end,$month_end,$day_end,$nb_days_acess_before,$nb_days_acess_after,$nolimit,$coach_username) {
  35. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  36. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  37. global $_user;
  38. $sql = 'SELECT user_id FROM '.$tbl_user.' WHERE username="'.Database::escape_string($coach_username).'"';
  39. $rs = api_sql_query($sql, __FILE__, __LINE__);
  40. $id_coach = Database::result($rs,0,'user_id');
  41. if (empty($nolimit)){
  42. $date_start="$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
  43. $date_end="$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
  44. } else {
  45. $date_start="000-00-00";
  46. $date_end="000-00-00";
  47. }
  48. if(empty($name)) {
  49. Display::display_normal_message(get_lang('SessionNameIsRequired'));
  50. } elseif (empty($coach_username)) {
  51. Display::display_normal_message(get_lang('CoachIsRequired'));
  52. } elseif (empty($nolimit) && (!$month_start || !$day_start || !$year_start || !checkdate($month_start,$day_start,$year_start))) {
  53. Display::display_normal_message(get_lang('InvalidStartDate'));
  54. } elseif (empty($nolimit) && (!$month_end || !$day_end || !$year_end || !checkdate($month_end,$day_end,$year_end))) {
  55. Display::display_normal_message(get_lang('InvalidEndDate'));
  56. } elseif(empty($nolimit) && $date_start >= $date_end) {
  57. Display::display_normal_message(get_lang('StartDateShouldBeBeforeEndDate'));
  58. } else {
  59. $rs = api_sql_query("SELECT 1 FROM $tbl_session WHERE name='".addslashes($name)."'");
  60. if(Database::num_rows($rs)) {
  61. Display::display_normal_message(get_lang('SessionNameSoonExists'));
  62. } else {
  63. api_sql_query("INSERT INTO $tbl_session(name,date_start,date_end,id_coach,session_admin_id, nb_days_access_before_beginning, nb_days_access_after_end) VALUES('".addslashes($name)."','$date_start','$date_end','$id_coach',".intval($_user['user_id']).",".$nb_days_acess_before.", ".$nb_days_acess_after.")",__FILE__,__LINE__);
  64. $id_session=Database::get_last_insert_id();
  65. return $id_session;
  66. }
  67. }
  68. }
  69. /** Edit a session
  70. * @author Carlos Vargas <carlos.vargas@dokeos.com>,
  71. * @param array name, year_start,month_start, day_start,year_end,month_end,day_end,nb_days_acess_before,nb_days_acess_after,id
  72. * The parameter id is a primary key
  73. **/
  74. function EditSession($name,$year_start,$month_start,$day_start,$year_end,$month_end,$day_end,$nb_days_acess_before,$nb_days_acess_after,$nolimit,$id_coach,$id) {
  75. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  76. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  77. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  78. global $_user;
  79. if (empty($nolimit)) {
  80. $date_start="$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
  81. $date_end="$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
  82. } else {
  83. $date_start="000-00-00";
  84. $date_end="000-00-00";
  85. }
  86. if(empty($name)){
  87. Display::display_normal_message(get_lang('SessionNameIsRequired'));
  88. } elseif(!empty($nolimit) && (!$month_start || !$day_start || !$year_start || !checkdate($month_start,$day_start,$year_start))) {
  89. Display::display_normal_message(get_lang('InvalidStartDate'));
  90. } elseif(!empty($nolimit) && (!$month_end || !$day_end || !$year_end || !checkdate($month_end,$day_end,$year_end))) {
  91. Display::display_normal_message(get_lang('InvalidEndDate'));
  92. } elseif(!empty($nolimit) && $date_start >= $date_end) {
  93. Display::display_normal_message(get_lang('StartDateShouldBeBeforeEndDate'));
  94. } else {
  95. $rs = api_sql_query("SELECT id FROM $tbl_session WHERE name='".addslashes($name)."'");
  96. $exists = false;
  97. while($row = mysql_fetch_array($rs)) {
  98. if($row['id']!=$id)
  99. $exists = true;
  100. }
  101. if ($exists) {
  102. Display::display_normal_message(get_lang('SessionNameSoonExists'));
  103. } else {
  104. $sql="UPDATE $tbl_session " .
  105. "SET name='".addslashes($name)."',
  106. date_start='".$date_start."',
  107. date_end='".$date_end."',
  108. id_coach='".$id_coach."',
  109. nb_days_access_before_beginning = ".$nb_days_acess_before.",
  110. nb_days_access_after_end = ".$nb_days_acess_after."
  111. WHERE id='$id'";
  112. api_sql_query($sql,__FILE__,__LINE__);
  113. $sqlu = "UPDATE $tbl_session_rel_course " .
  114. " SET id_coach='$id_coach'" .
  115. " WHERE id_session='$id'";
  116. api_sql_query($sqlu,__FILE__,__LINE__);
  117. return $id;
  118. }
  119. }
  120. }
  121. /** Delete session
  122. * @author Carlos Vargas <carlos.vargas@dokeos.com>,
  123. * @param array idChecked
  124. * The parameters is a array to delete sessions
  125. **/
  126. function DeleteSession($idChecked) {
  127. $tbl_session=Database::get_main_table(TABLE_MAIN_SESSION);
  128. $tbl_session_rel_course=Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  129. $tbl_session_rel_course_rel_user=Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  130. $tbl_session_rel_user=Database::get_main_table(TABLE_MAIN_SESSION_USER);
  131. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  132. global $_user;
  133. if(is_array($idChecked)) {
  134. $idChecked=Database::escape_string(implode(',',$idChecked));
  135. } else {
  136. $idChecked=intval($idChecked);
  137. }
  138. if (!api_is_platform_admin()) {
  139. $sql = 'SELECT session_admin_id FROM '.Database :: get_main_table(TABLE_MAIN_SESSION).' WHERE id='.$idChecked;
  140. $rs = api_sql_query($sql,__FILE__,__LINE__);
  141. if (Database::result($rs,0,0)!=$_user['user_id']) {
  142. api_not_allowed(true);
  143. }
  144. }
  145. api_sql_query("DELETE FROM $tbl_session WHERE id IN($idChecked)",__FILE__,__LINE__);
  146. api_sql_query("DELETE FROM $tbl_session_rel_course WHERE id_session IN($idChecked)",__FILE__,__LINE__);
  147. api_sql_query("DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session IN($idChecked)",__FILE__,__LINE__);
  148. api_sql_query("DELETE FROM $tbl_session_rel_user WHERE id_session IN($idChecked)",__FILE__,__LINE__);
  149. }
  150. /**Subscribes users to the given session and optionally (default) unsubscribes previous users
  151. * @author Carlos Vargas <carlos.vargas@dokeos.com>,
  152. * @param int Session ID
  153. * @param array List of user IDs
  154. * @param bool Whether to unsubscribe existing users (true, default) or not (false)
  155. * @return void Nothing, or false on error
  156. */
  157. function suscribe_users_to_session($id_session,$UserList,$empty_users=true){
  158. if ($id_session!= strval(intval($id_session))) return false;
  159. foreach($UserList as $intUser){
  160. if ($intUser!= strval(intval($intUser))) return false;
  161. }
  162. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  163. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  164. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  165. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  166. $sql = "SELECT id_user FROM $tbl_session_rel_user WHERE id_session='$id_session'";
  167. $result = api_sql_query($sql,__FILE__,__LINE__);
  168. $existingUsers = array();
  169. while($row = Database::fetch_array($result)){
  170. $existingUsers[] = $row['id_user'];
  171. }
  172. $sql = "SELECT course_code FROM $tbl_session_rel_course WHERE id_session='$id_session'";
  173. $result=api_sql_query($sql,__FILE__,__LINE__);
  174. $CourseList=array();
  175. while($row=Database::fetch_array($result)) {
  176. $CourseList[]=$row['course_code'];
  177. }
  178. foreach ($CourseList as $enreg_course) {
  179. // for each course in the session
  180. $nbr_users=0;
  181. $enreg_course = Database::escape_string($enreg_course);
  182. // delete existing users
  183. if ($empty_users!==false) {
  184. foreach ($existingUsers as $existing_user) {
  185. if(!in_array($existing_user, $UserList)) {
  186. $sql = "DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course' AND id_user='$existing_user'";
  187. api_sql_query($sql,__FILE__,__LINE__);
  188. if(Database::affected_rows()) {
  189. $nbr_users--;
  190. }
  191. }
  192. }
  193. }
  194. // insert new users into session_rel_course_rel_user and ignore if they already exist
  195. foreach ($UserList as $enreg_user) {
  196. if(!in_array($enreg_user, $existingUsers)) {
  197. $enreg_user = Database::escape_string($enreg_user);
  198. $insert_sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$enreg_course','$enreg_user')";
  199. api_sql_query($insert_sql,__FILE__,__LINE__);
  200. if(Database::affected_rows()) {
  201. $nbr_users++;
  202. }
  203. }
  204. }
  205. // count users in this session-course relation
  206. $sql = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course'";
  207. $rs = api_sql_query($sql, __FILE__, __LINE__);
  208. list($nbr_users) = Database::fetch_array($rs);
  209. // update the session-course relation to add the users total
  210. $update_sql = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'";
  211. api_sql_query($update_sql,__FILE__,__LINE__);
  212. }
  213. // delete users from the session
  214. if ($empty_users!==false){
  215. api_sql_query("DELETE FROM $tbl_session_rel_user WHERE id_session = $id_session",__FILE__,__LINE__);
  216. }
  217. // insert missing users into session
  218. $nbr_users = 0;
  219. foreach ($UserList as $enreg_user) {
  220. $enreg_user = Database::escape_string($enreg_user);
  221. $nbr_users++;
  222. $insert_sql = "INSERT IGNORE INTO $tbl_session_rel_user(id_session, id_user) VALUES('$id_session','$enreg_user')";
  223. api_sql_query($insert_sql,__FILE__,__LINE__);
  224. }
  225. // update number of users in the session
  226. $nbr_users = count($UserList);
  227. $update_sql = "UPDATE $tbl_session SET nbr_users= $nbr_users WHERE id='$id_session' ";
  228. api_sql_query($update_sql,__FILE__,__LINE__);
  229. }
  230. /**Subscribes courses to the given session and optionally (default) unsubscribes previous users
  231. * @author Carlos Vargas <carlos.vargas@dokeos.com>,
  232. * @param int Session ID
  233. * @param array List of courses IDs
  234. * @param bool Whether to unsubscribe existing users (true, default) or not (false)
  235. * @return void Nothing, or false on error
  236. */
  237. function add_courses_to_session($id_session, $CourseList, $empty_courses=true){
  238. if ($id_session!= strval(intval($id_session))) return false;
  239. foreach($CourseList as $intCourse){
  240. if ($intCourse!= strval(intval($intCourse))) return false;
  241. }
  242. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  243. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  244. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  245. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  246. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  247. $id_coach = api_sql_query("SELECT id_coach FROM $tbl_session WHERE id=$id_session");
  248. $id_coach = Database::fetch_array($id_coach);
  249. $id_coach = $id_coach[0];
  250. $rs = api_sql_query("SELECT course_code FROM $tbl_session_rel_course WHERE id_session=$id_session");
  251. $existingCourses = api_store_result($rs);
  252. $sql="SELECT id_user
  253. FROM $tbl_session_rel_user
  254. WHERE id_session = $id_session";
  255. $result=api_sql_query($sql,__FILE__,__LINE__);
  256. $UserList=api_store_result($result);
  257. foreach($CourseList as $enreg_course) {
  258. $enreg_course = Database::escape_string($enreg_course);
  259. $exists = false;
  260. foreach($existingCourses as $existingCourse) {
  261. if($enreg_course == $existingCourse['course_code']) {
  262. $exists=true;
  263. }
  264. }
  265. if(!$exists) {
  266. $sql_insert_rel_course= "INSERT INTO $tbl_session_rel_course(id_session,course_code, id_coach) VALUES('$id_session','$enreg_course','$id_coach')";
  267. api_sql_query($sql_insert_rel_course ,__FILE__,__LINE__);
  268. //We add in the existing courses table the current course, to not try to add another time the current course
  269. $existingCourses[]=array('course_code'=>$enreg_course);
  270. $nbr_users=0;
  271. foreach ($UserList as $enreg_user) {
  272. $enreg_user = Database::escape_string($enreg_user['id_user']);
  273. $sql_insert = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$enreg_course','$enreg_user')";
  274. api_sql_query($sql_insert,__FILE__,__LINE__);
  275. if(Database::affected_rows()) {
  276. $nbr_users++;
  277. }
  278. }
  279. api_sql_query("UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'",__FILE__,__LINE__);
  280. }
  281. }
  282. if ($empty_courses!==false) {
  283. foreach($existingCourses as $existingCourse) {
  284. if(!in_array($existingCourse['course_code'], $CourseList)){
  285. api_sql_query("DELETE FROM $tbl_session_rel_course WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  286. api_sql_query("DELETE FROM $tbl_session_rel_course_rel_user WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  287. }
  288. }
  289. }
  290. $nbr_courses=count($CourseList);
  291. api_sql_query("UPDATE $tbl_session SET nbr_courses=$nbr_courses WHERE id='$id_session'",__FILE__,__LINE__);
  292. }
  293. }
  294. ?>