sessionmanager.lib.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. <?php //$id: $
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. ==============================================================================
  5. * This class provides methods for sessions management.
  6. * Include/require it in your code to use its features.
  7. *
  8. * @package dokeos.library
  9. ==============================================================================
  10. */
  11. require_once 'display.lib.php';
  12. class SessionManager {
  13. private function __construct() {
  14. }
  15. /**
  16. * Fetches a session from the database
  17. * @param int Session ID
  18. * @return array Session details (id, id_coach, name, nbr_courses, nbr_users, nbr_classes, date_start, date_end, nb_days_access_before_beginning,nb_days_access_after_end, session_admin_id)
  19. */
  20. public static function fetch($id) {
  21. $t = Database::get_main_table(TABLE_MAIN_SESSION);
  22. if ($id != strval(intval($id))) { return array(); }
  23. $s = "SELECT * FROM $t WHERE id = $id";
  24. $r = Database::query($s,__FILE__,__LINE__);
  25. if (Database::num_rows($r) != 1) { return array(); }
  26. return Database::fetch_array($r,'ASSOC');
  27. }
  28. /**
  29. * Create a session
  30. * @author Carlos Vargas <carlos.vargas@dokeos.com>,from existing code
  31. * @param string name
  32. * @param integer year_start
  33. * @param integer month_start
  34. * @param integer day_start
  35. * @param integer year_end
  36. * @param integer month_end
  37. * @param integer day_end
  38. * @param integer nb_days_acess_before
  39. * @param integer nb_days_acess_after
  40. * @param integer nolimit
  41. * @param string coach_username
  42. * @param integer id_session_category
  43. * @return $id_session;
  44. **/
  45. public static function create_session ($sname,$syear_start,$smonth_start,$sday_start,$syear_end,$smonth_end,$sday_end,$snb_days_acess_before,$snb_days_acess_after,$nolimit,$coach_username, $id_session_category,$id_visibility) {
  46. global $_user;
  47. $name= trim($sname);
  48. $year_start= intval($syear_start);
  49. $month_start=intval($smonth_start);
  50. $day_start=intval($sday_start);
  51. $year_end=intval($syear_end);
  52. $month_end=intval($smonth_end);
  53. $day_end=intval($sday_end);
  54. $nb_days_acess_before = intval($snb_days_acess_before);
  55. $nb_days_acess_after = intval($snb_days_acess_after);
  56. $id_session_category = intval($id_session_category);
  57. $id_visibility = intval($id_visibility);
  58. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  59. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  60. $sql = 'SELECT user_id FROM '.$tbl_user.' WHERE username="'.Database::escape_string($coach_username).'"';
  61. $rs = Database::query($sql, __FILE__, __LINE__);
  62. $id_coach = Database::result($rs,0,'user_id');
  63. if (empty($nolimit)) {
  64. $date_start="$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
  65. $date_end="$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
  66. } else {
  67. $id_visibility = 1; // by default is read only
  68. $date_start="000-00-00";
  69. $date_end="000-00-00";
  70. }
  71. if (empty($name)) {
  72. $msg=get_lang('SessionNameIsRequired');
  73. return $msg;
  74. } elseif (empty($coach_username)) {
  75. $msg=get_lang('CoachIsRequired');
  76. return $msg;
  77. } elseif (empty($nolimit) && (!$month_start || !$day_start || !$year_start || !checkdate($month_start,$day_start,$year_start))) {
  78. $msg=get_lang('InvalidStartDate');
  79. return $msg;
  80. } elseif (empty($nolimit) && (!$month_end || !$day_end || !$year_end || !checkdate($month_end,$day_end,$year_end))) {
  81. $msg=get_lang('InvalidEndDate');
  82. return $msg;
  83. } elseif(empty($nolimit) && $date_start >= $date_end) {
  84. $msg=get_lang('StartDateShouldBeBeforeEndDate');
  85. return $msg;
  86. } else {
  87. $rs = Database::query("SELECT 1 FROM $tbl_session WHERE name='".addslashes($name)."'");
  88. if(Database::num_rows($rs)) {
  89. $msg=get_lang('SessionNameAlreadyExists');
  90. return $msg;
  91. } else {
  92. $sql_insert = "INSERT INTO $tbl_session(name,date_start,date_end,id_coach,session_admin_id, nb_days_access_before_beginning, nb_days_access_after_end, session_category_id,visibility)
  93. VALUES('".Database::escape_string($name)."','$date_start','$date_end','$id_coach',".intval($_user['user_id']).",".$nb_days_acess_before.", ".$nb_days_acess_after.", ".$id_session_category.", ".$id_visibility.")";
  94. Database::query($sql_insert ,__FILE__,__LINE__);
  95. $id_session=Database::insert_id();
  96. // add event to system log
  97. $time = time();
  98. $user_id = api_get_user_id();
  99. event_system(LOG_SESSION_CREATE, LOG_SESSION_ID, $id_session, $time, $user_id);
  100. return $id_session;
  101. }
  102. }
  103. }
  104. /**
  105. * Edit a session
  106. * @author Carlos Vargas <carlos.vargas@dokeos.com>,from existing code
  107. * @param integer id
  108. * @param string name
  109. * @param integer year_start
  110. * @param integer month_start
  111. * @param integer day_start
  112. * @param integer year_end
  113. * @param integer month_end
  114. * @param integer day_end
  115. * @param integer nb_days_acess_before
  116. * @param integer nb_days_acess_after
  117. * @param integer nolimit
  118. * @param integer id_coach
  119. * @param integer id_session_category
  120. * @return $id;
  121. * The parameter id is a primary key
  122. **/
  123. public static function edit_session ($id,$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_session_category, $id_visibility) {
  124. global $_user;
  125. $name=trim(stripslashes($name));
  126. $year_start=intval($year_start);
  127. $month_start=intval($month_start);
  128. $day_start=intval($day_start);
  129. $year_end=intval($year_end);
  130. $month_end=intval($month_end);
  131. $day_end=intval($day_end);
  132. $id_coach= intval($id_coach);
  133. $nb_days_acess_before= intval($nb_days_acess_before);
  134. $nb_days_acess_after = intval($nb_days_acess_after);
  135. $id_session_category = intval($id_session_category);
  136. $id_visibility = intval($id_visibility);
  137. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  138. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  139. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  140. if (empty($nolimit)) {
  141. $date_start="$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
  142. $date_end="$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
  143. } else {
  144. $date_start="000-00-00";
  145. $date_end="000-00-00";
  146. $id_visibility = 1;//force read only
  147. }
  148. if (empty($name)) {
  149. $msg=get_lang('SessionNameIsRequired');
  150. return $msg;
  151. } elseif (empty($id_coach)) {
  152. $msg=get_lang('CoachIsRequired');
  153. return $msg;
  154. } elseif (empty($nolimit) && (!$month_start || !$day_start || !$year_start || !checkdate($month_start,$day_start,$year_start))) {
  155. $msg=get_lang('InvalidStartDate');
  156. return $msg;
  157. } elseif (empty($nolimit) && (!$month_end || !$day_end || !$year_end || !checkdate($month_end,$day_end,$year_end))) {
  158. $msg=get_lang('InvalidEndDate');
  159. return $msg;
  160. } elseif (empty($nolimit) && $date_start >= $date_end) {
  161. $msg=get_lang('StartDateShouldBeBeforeEndDate');
  162. return $msg;
  163. } else {
  164. $rs = Database::query("SELECT id FROM $tbl_session WHERE name='".Database::escape_string($name)."'");
  165. $exists = false;
  166. while ($row = Database::fetch_array($rs)) {
  167. if($row['id']!=$id)
  168. $exists = true;
  169. }
  170. if ($exists) {
  171. $msg=get_lang('SessionNameAlreadyExists');
  172. return $msg;
  173. } else {
  174. $sql="UPDATE $tbl_session " .
  175. "SET name='".Database::escape_string($name)."',
  176. date_start='".$date_start."',
  177. date_end='".$date_end."',
  178. id_coach='".$id_coach."',
  179. nb_days_access_before_beginning = ".$nb_days_acess_before.",
  180. nb_days_access_after_end = ".$nb_days_acess_after.",
  181. session_category_id = ".$id_session_category." ,
  182. visibility= ".$id_visibility."
  183. WHERE id='$id'";
  184. Database::query($sql,__FILE__,__LINE__);
  185. /*$sqlu = "UPDATE $tbl_session_rel_course " .
  186. " SET id_coach='$id_coach'" .
  187. " WHERE id_session='$id'";
  188. Database::query($sqlu,__FILE__,__LINE__);*/
  189. return $id;
  190. }
  191. }
  192. }
  193. /**
  194. * Delete session
  195. * @author Carlos Vargas <carlos.vargas@dokeos.com>, from existing code
  196. * @param array id_checked
  197. * @param boolean optional, true if the function is called by a webservice, false otherwise.
  198. * @return void Nothing, or false on error
  199. * The parameters is a array to delete sessions
  200. **/
  201. public static function delete_session ($id_checked,$from_ws = false) {
  202. $tbl_session= Database::get_main_table(TABLE_MAIN_SESSION);
  203. $tbl_session_rel_course= Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  204. $tbl_session_rel_course_rel_user= Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  205. $tbl_session_rel_user= Database::get_main_table(TABLE_MAIN_SESSION_USER);
  206. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  207. global $_user;
  208. if(is_array($id_checked)) {
  209. $id_checked=Database::escape_string(implode(',',$id_checked));
  210. } else {
  211. $id_checked=intval($id_checked);
  212. }
  213. if (!api_is_platform_admin() && !$from_ws) {
  214. $sql = 'SELECT session_admin_id FROM '.Database :: get_main_table(TABLE_MAIN_SESSION).' WHERE id='.$id_checked;
  215. $rs = Database::query($sql,__FILE__,__LINE__);
  216. if (Database::result($rs,0,0)!=$_user['user_id']) {
  217. api_not_allowed(true);
  218. }
  219. }
  220. Database::query("DELETE FROM $tbl_session WHERE id IN($id_checked)",__FILE__,__LINE__);
  221. Database::query("DELETE FROM $tbl_session_rel_course WHERE id_session IN($id_checked)",__FILE__,__LINE__);
  222. Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session IN($id_checked)",__FILE__,__LINE__);
  223. Database::query("DELETE FROM $tbl_session_rel_user WHERE id_session IN($id_checked)",__FILE__,__LINE__);
  224. // delete extra session fields
  225. $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  226. $t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  227. // Delete extra fields from session where field variable is "SECCION"
  228. $sql = "SELECT t_sfv.field_id FROM $t_sfv t_sfv, $t_sf t_sf WHERE t_sfv.session_id = '$id_checked' AND t_sf.field_variable = 'SECCION' ";
  229. $rs_field = Database::query($sql,__FILE__,__LINE__);
  230. $field_id = 0;
  231. if (Database::num_rows($rs_field) == 1) {
  232. $row_field = Database::fetch_row($rs_field);
  233. $field_id = $row_field[0];
  234. $sql_delete_sfv = "DELETE FROM $t_sfv WHERE session_id = '$id_checked' AND field_id = '$field_id'";
  235. $rs_delete_sfv = Database::query($sql_delete_sfv,__FILE__,__LINE__);
  236. }
  237. $sql = "SELECT * FROM $t_sfv WHERE field_id = '$field_id' ";
  238. $rs_field_id = Database::query($sql,__FILE__,__LINE__);
  239. if (Database::num_rows($rs_field_id) == 0) {
  240. $sql_delete_sf = "DELETE FROM $t_sf WHERE id = '$field_id'";
  241. $rs_delete_sf = Database::query($sql_delete_sf,__FILE__,__LINE__);
  242. }
  243. /*
  244. $sql = "SELECT distinct field_id FROM $t_sfv WHERE session_id = '$id_checked'";
  245. $res_field_ids = @Database::query($sql,__FILE__,__LINE__);
  246. if (Database::num_rows($res_field_ids) > 0) {
  247. while($row_field_id = Database::fetch_row($res_field_ids)){
  248. $field_ids[] = $row_field_id[0];
  249. }
  250. }
  251. //delete from table_session_field_value from a given session id
  252. $sql_session_field_value = "DELETE FROM $t_sfv WHERE session_id = '$id_checked'";
  253. @Database::query($sql_session_field_value,__FILE__,__LINE__);
  254. $sql = "SELECT distinct field_id FROM $t_sfv";
  255. $res_field_all_ids = @Database::query($sql,__FILE__,__LINE__);
  256. if (Database::num_rows($res_field_all_ids) > 0) {
  257. while($row_field_all_id = Database::fetch_row($res_field_all_ids)){
  258. $field_all_ids[] = $row_field_all_id[0];
  259. }
  260. }
  261. if (count($field_ids) > 0 && count($field_all_ids) > 0) {
  262. foreach($field_ids as $field_id) {
  263. // check if field id is used into table field value
  264. if (in_array($field_id,$field_all_ids)) {
  265. continue;
  266. } else {
  267. $sql_session_field = "DELETE FROM $t_sf WHERE id = '$field_id'";
  268. Database::query($sql_session_field,__FILE__,__LINE__);
  269. }
  270. }
  271. }
  272. */
  273. // add event to system log
  274. $time = time();
  275. $user_id = api_get_user_id();
  276. event_system(LOG_SESSION_DELETE, LOG_SESSION_ID, $id_checked, $time, $user_id);
  277. }
  278. /**
  279. * Subscribes users to the given session and optionally (default) unsubscribes previous users
  280. * @author Carlos Vargas <carlos.vargas@dokeos.com>,from existing code
  281. * @param integer Session ID
  282. * @param array List of user IDs
  283. * @param bool Whether to unsubscribe existing users (true, default) or not (false)
  284. * @return void Nothing, or false on error
  285. **/
  286. public static function suscribe_users_to_session ($id_session,$user_list, $visibility=SESSION_VISIBLE_READ_ONLY, $empty_users=true,$send_email=false) {
  287. if ($id_session!= strval(intval($id_session))) return false;
  288. foreach($user_list as $intUser){
  289. if ($intUser!= strval(intval($intUser))) return false;
  290. }
  291. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  292. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  293. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  294. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  295. $session_info = api_get_session_info($id_session);
  296. $session_name = $session_info['name'];
  297. //from function parameter
  298. $session_visibility = $visibility;
  299. if (empty($session_visibility)) {
  300. $session_visibility = $session_info['name'];
  301. $session_visivility = $session_info['visibility']; //loaded from DB
  302. //default status loaded if empty
  303. if (empty($session_visivility))
  304. $session_visibility = SESSION_VISIBLE_READ_ONLY; // by default readonly 1
  305. }
  306. $session_info = api_get_session_info($id_session);
  307. $session_name = $session_info['name'];
  308. $sql = "SELECT id_user FROM $tbl_session_rel_user WHERE id_session='$id_session'";
  309. $result = Database::query($sql,__FILE__,__LINE__);
  310. $existingUsers = array();
  311. while($row = Database::fetch_array($result)){
  312. $existingUsers[] = $row['id_user'];
  313. }
  314. $sql = "SELECT course_code FROM $tbl_session_rel_course WHERE id_session='$id_session'";
  315. $result=Database::query($sql,__FILE__,__LINE__);
  316. $course_list=array();
  317. while($row=Database::fetch_array($result)) {
  318. $course_list[]=$row['course_code'];
  319. }
  320. if ($send_email == true) {
  321. global $_configuration;
  322. //sending emails only
  323. if(is_array($user_list) && count($user_list)>0) {
  324. foreach($user_list as $enreg_user) {
  325. if (!in_array($enreg_user,$existingUsers )) {
  326. //send email
  327. $emailbody = '';
  328. $emailheaders = '';
  329. $user_info = UserManager::get_user_info_by_id($enreg_user);
  330. $firstname = $user_info['firstname'];
  331. $lastname = $user_info['lastname'];
  332. $email = $user_info['email'];
  333. $emailto = '"'.$firstname.' '.$lastname.'" <'.$email.'>';
  334. $emailsubject = '['.get_setting('siteName').'] '.get_lang('YourReg').' '.get_setting('siteName');
  335. $emailheaders = 'From: '.get_setting('administratorName').' '.get_setting('administratorSurname').' <'.get_setting('emailAdministrator').">\n";
  336. $emailheaders .= 'Reply-To: '.get_setting('emailAdministrator');
  337. if ($_configuration['multiple_access_urls']==true) {
  338. $access_url_id = api_get_current_access_url_id();
  339. if ($access_url_id != -1 ){
  340. $url = api_get_access_url($access_url_id);
  341. $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n".get_lang('YouAreRegisterToSession')." : ". $session_name ." \n\n" .get_lang('Address') ." ". get_setting('siteName') ." ". get_lang('Is') ." : ". $url['url'] ."\n\n". get_lang('Problem'). "\n\n". get_lang('Formula').",\n\n".get_setting('administratorName')." ".get_setting('administratorSurname')."\n". get_lang('Manager'). " ".get_setting('siteName')."\nT. ".get_setting('administratorTelephone')."\n" .get_lang('Email') ." : ".get_setting('emailAdministrator');
  342. }
  343. } else {
  344. $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n".get_lang('YouAreRegisterToSession')." : ". $session_name ." \n\n" .get_lang('Address') ." ". get_setting('siteName') ." ". get_lang('Is') ." : ". $_configuration['root_web'] ."\n\n". get_lang('Problem'). "\n\n". get_lang('Formula').",\n\n".get_setting('administratorName')." ".get_setting('administratorSurname')."\n". get_lang('Manager'). " ".get_setting('siteName')."\nT. ".get_setting('administratorTelephone')."\n" .get_lang('Email') ." : ".get_setting('emailAdministrator');
  345. }
  346. @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  347. }
  348. }
  349. }
  350. }
  351. foreach ($course_list as $enreg_course) {
  352. // for each course in the session
  353. $nbr_users=0;
  354. $enreg_course = Database::escape_string($enreg_course);
  355. // delete existing users
  356. if ($empty_users!==false) {
  357. foreach ($existingUsers as $existing_user) {
  358. if(!in_array($existing_user, $user_list)) {
  359. $sql = "DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course' AND id_user='$existing_user' AND status != 2 ";
  360. Database::query($sql,__FILE__,__LINE__);
  361. if(Database::affected_rows()) {
  362. $nbr_users--;
  363. }
  364. }
  365. }
  366. }
  367. // insert new users into session_rel_course_rel_user and ignore if they already exist
  368. foreach ($user_list as $enreg_user) {
  369. if(!in_array($enreg_user, $existingUsers)) {
  370. $enreg_user = Database::escape_string($enreg_user);
  371. $insert_sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user,visibility) VALUES('$id_session','$enreg_course','$enreg_user','$session_visivility')";
  372. Database::query($insert_sql,__FILE__,__LINE__);
  373. if(Database::affected_rows()) {
  374. $nbr_users++;
  375. }
  376. }
  377. }
  378. // count users in this session-course relation
  379. $sql = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course'";
  380. $rs = Database::query($sql, __FILE__, __LINE__);
  381. list($nbr_users) = Database::fetch_array($rs);
  382. // update the session-course relation to add the users total
  383. $update_sql = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'";
  384. Database::query($update_sql,__FILE__,__LINE__);
  385. }
  386. // delete users from the session
  387. if ($empty_users===true){
  388. Database::query("DELETE FROM $tbl_session_rel_user WHERE id_session = $id_session",__FILE__,__LINE__);
  389. }
  390. // insert missing users into session
  391. $nbr_users = 0;
  392. foreach ($user_list as $enreg_user) {
  393. $enreg_user = Database::escape_string($enreg_user);
  394. $nbr_users++;
  395. $insert_sql = "INSERT IGNORE INTO $tbl_session_rel_user(id_session, id_user) VALUES('$id_session','$enreg_user')";
  396. Database::query($insert_sql,__FILE__,__LINE__);
  397. }
  398. // update number of users in the session
  399. $nbr_users = count($user_list);
  400. $update_sql = "UPDATE $tbl_session SET nbr_users= $nbr_users WHERE id='$id_session' ";
  401. Database::query($update_sql,__FILE__,__LINE__);
  402. }
  403. /**
  404. * Subscribes sessions to user (Dashboard feature)
  405. * @param int User id
  406. * @param int Session id
  407. * @param int Relation type
  408. **/
  409. public static function suscribe_sessions_to_user($user_id,$session_list, $relation_stype) {
  410. if ($user_id!= strval(intval($user_id))) return false;
  411. foreach($session_list as $session_id){
  412. if ($session_id!= strval(intval($session_id))) return false;
  413. }
  414. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  415. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  416. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  417. $sql = "SELECT id_session FROM $tbl_session_rel_user WHERE id_user = $user_id AND relation_type = 1 ";
  418. $result = Database::query($sql,__FILE__,__LINE__);
  419. $existing_sessions = array();
  420. while($row = Database::fetch_array($result)){
  421. $existing_sessions[] = $row['id_session'];
  422. }
  423. //Deleting existing session_rel_user
  424. foreach ($existing_sessions as $existing_session) {
  425. $sql = "DELETE FROM $tbl_session_rel_user WHERE id_session=$existing_session AND id_user = $user_id AND relation_type = 1 ";
  426. Database::query($sql,__FILE__,__LINE__);
  427. }
  428. foreach ($session_list as $session_id) {
  429. // for each session
  430. $enreg_user = Database::escape_string($enreg_user);
  431. $insert_sql = "INSERT IGNORE INTO $tbl_session_rel_user(id_session,id_user,relation_type) VALUES('$session_id','$user_id','1')";
  432. Database::query($insert_sql,__FILE__,__LINE__);
  433. }
  434. }
  435. /** Subscribes courses to the given session and optionally (default) unsubscribes previous users
  436. * @author Carlos Vargas <carlos.vargas@dokeos.com>,from existing code
  437. * @param int Session ID
  438. * @param array List of courses IDs
  439. * @param bool Whether to unsubscribe existing users (true, default) or not (false)
  440. * @return void Nothing, or false on error
  441. **/
  442. public static function add_courses_to_session ($id_session, $course_list, $empty_courses=true) {
  443. // security checks
  444. if ($id_session!= strval(intval($id_session))) { return false; }
  445. foreach($course_list as $intCourse){
  446. if ($intCourse!= strval(intval($intCourse))) { return false; }
  447. }
  448. // initialisation
  449. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  450. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  451. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  452. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  453. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  454. // get general coach ID
  455. $id_coach = Database::query("SELECT id_coach FROM $tbl_session WHERE id=$id_session");
  456. $id_coach = Database::fetch_array($id_coach);
  457. $id_coach = $id_coach[0];
  458. // get list of courses subscribed to this session
  459. $rs = Database::query("SELECT course_code FROM $tbl_session_rel_course WHERE id_session=$id_session");
  460. $existingCourses = Database::store_result($rs);
  461. $nbr_courses=count($existingCourses);
  462. // get list of users subscribed to this session
  463. $sql="SELECT id_user
  464. FROM $tbl_session_rel_user
  465. WHERE id_session = $id_session";
  466. $result=Database::query($sql,__FILE__,__LINE__);
  467. $user_list=Database::store_result($result);
  468. // remove existing courses from the session
  469. if ($empty_courses===true) {
  470. foreach ($existingCourses as $existingCourse) {
  471. if (!in_array($existingCourse['course_code'], $course_list)){
  472. Database::query("DELETE FROM $tbl_session_rel_course WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  473. Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  474. }
  475. }
  476. $nbr_courses=0;
  477. }
  478. // Pass through the courses list we want to add to the session
  479. foreach ($course_list as $enreg_course) {
  480. $enreg_course = Database::escape_string($enreg_course);
  481. $exists = false;
  482. // check if the course we want to add is already subscribed
  483. foreach ($existingCourses as $existingCourse) {
  484. if ($enreg_course == $existingCourse['course_code']) {
  485. $exists=true;
  486. }
  487. }
  488. if (!$exists) {
  489. //if the course isn't subscribed yet
  490. $sql_insert_rel_course= "INSERT INTO $tbl_session_rel_course (id_session,course_code) VALUES ('$id_session','$enreg_course')";
  491. Database::query($sql_insert_rel_course ,__FILE__,__LINE__);
  492. //We add the current course in the existing courses array, to avoid adding another time the current course
  493. $existingCourses[]=array('course_code'=>$enreg_course);
  494. $nbr_courses++;
  495. // subscribe all the users from the session to this course inside the session
  496. $nbr_users=0;
  497. foreach ($user_list as $enreg_user) {
  498. $enreg_user_id = Database::escape_string($enreg_user['id_user']);
  499. $sql_insert = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (id_session,course_code,id_user) VALUES ('$id_session','$enreg_course','$enreg_user_id')";
  500. Database::query($sql_insert,__FILE__,__LINE__);
  501. if (Database::affected_rows()) {
  502. $nbr_users++;
  503. }
  504. }
  505. Database::query("UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'",__FILE__,__LINE__);
  506. }
  507. }
  508. Database::query("UPDATE $tbl_session SET nbr_courses=$nbr_courses WHERE id='$id_session'",__FILE__,__LINE__);
  509. }
  510. /**
  511. * Creates a new extra field for a given session
  512. * @param string Field's internal variable name
  513. * @param int Field's type
  514. * @param string Field's language var name
  515. * @return int new extra field id
  516. */
  517. public static function create_session_extra_field ($fieldvarname, $fieldtype, $fieldtitle) {
  518. // database table definition
  519. $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  520. $fieldvarname = Database::escape_string($fieldvarname);
  521. $fieldtitle = Database::escape_string($fieldtitle);
  522. $fieldtype = (int)$fieldtype;
  523. $time = time();
  524. $sql_field = "SELECT id FROM $t_sf WHERE field_variable = '$fieldvarname'";
  525. $res_field = Database::query($sql_field,__FILE__,__LINE__);
  526. $r_field = Database::fetch_row($res_field);
  527. if (Database::num_rows($res_field)>0) {
  528. $field_id = $r_field[0];
  529. } else {
  530. // save new fieldlabel into course_field table
  531. $sql = "SELECT MAX(field_order) FROM $t_sf";
  532. $res = Database::query($sql,__FILE__,__LINE__);
  533. $order = 0;
  534. if (Database::num_rows($res)>0) {
  535. $row = Database::fetch_row($res);
  536. $order = $row[0]+1;
  537. }
  538. $sql = "INSERT INTO $t_sf
  539. SET field_type = '$fieldtype',
  540. field_variable = '$fieldvarname',
  541. field_display_text = '$fieldtitle',
  542. field_order = '$order',
  543. tms = FROM_UNIXTIME($time)";
  544. $result = Database::query($sql,__FILE__,__LINE__);
  545. $field_id=Database::insert_id();
  546. }
  547. return $field_id;
  548. }
  549. /**
  550. * Update an extra field value for a given session
  551. * @param integer Course ID
  552. * @param string Field variable name
  553. * @param string Field value
  554. * @return boolean true if field updated, false otherwise
  555. */
  556. public static function update_session_extra_field_value ($session_id,$fname,$fvalue='') {
  557. $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  558. $t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  559. $fname = Database::escape_string($fname);
  560. $session_id = (int)$session_id;
  561. $fvalues = '';
  562. if(is_array($fvalue))
  563. {
  564. foreach($fvalue as $val)
  565. {
  566. $fvalues .= Database::escape_string($val).';';
  567. }
  568. if(!empty($fvalues))
  569. {
  570. $fvalues = substr($fvalues,0,-1);
  571. }
  572. }
  573. else
  574. {
  575. $fvalues = Database::escape_string($fvalue);
  576. }
  577. $sqlsf = "SELECT * FROM $t_sf WHERE field_variable='$fname'";
  578. $ressf = Database::query($sqlsf,__FILE__,__LINE__);
  579. if(Database::num_rows($ressf)==1)
  580. { //ok, the field exists
  581. // Check if enumerated field, if the option is available
  582. $rowsf = Database::fetch_array($ressf);
  583. $tms = time();
  584. $sqlsfv = "SELECT * FROM $t_sfv WHERE session_id = '$session_id' AND field_id = '".$rowsf['id']."' ORDER BY id";
  585. $ressfv = Database::query($sqlsfv,__FILE__,__LINE__);
  586. $n = Database::num_rows($ressfv);
  587. if ($n>1) {
  588. //problem, we already have to values for this field and user combination - keep last one
  589. while($rowsfv = Database::fetch_array($ressfv))
  590. {
  591. if($n > 1)
  592. {
  593. $sqld = "DELETE FROM $t_sfv WHERE id = ".$rowsfv['id'];
  594. $resd = Database::query($sqld,__FILE__,__LINE__);
  595. $n--;
  596. }
  597. $rowsfv = Database::fetch_array($ressfv);
  598. if($rowsfv['field_value'] != $fvalues)
  599. {
  600. $sqlu = "UPDATE $t_sfv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms) WHERE id = ".$rowsfv['id'];
  601. $resu = Database::query($sqlu,__FILE__,__LINE__);
  602. return($resu?true:false);
  603. }
  604. return true;
  605. }
  606. } else if ($n==1) {
  607. //we need to update the current record
  608. $rowsfv = Database::fetch_array($ressfv);
  609. if($rowsfv['field_value'] != $fvalues)
  610. {
  611. $sqlu = "UPDATE $t_sfv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms) WHERE id = ".$rowsfv['id'];
  612. //error_log('UM::update_extra_field_value: '.$sqlu);
  613. $resu = Database::query($sqlu,__FILE__,__LINE__);
  614. return($resu?true:false);
  615. }
  616. return true;
  617. } else {
  618. $sqli = "INSERT INTO $t_sfv (session_id,field_id,field_value,tms) " .
  619. "VALUES ('$session_id',".$rowsf['id'].",'$fvalues',FROM_UNIXTIME($tms))";
  620. //error_log('UM::update_extra_field_value: '.$sqli);
  621. $resi = Database::query($sqli,__FILE__,__LINE__);
  622. return($resi?true:false);
  623. }
  624. } else {
  625. return false; //field not found
  626. }
  627. }
  628. /**
  629. * Checks the relationship between a session and a course.
  630. * @param int $session_id
  631. * @param int $course_id
  632. * @return bool Returns TRUE if the session and the course are related, FALSE otherwise.
  633. * */
  634. public static function relation_session_course_exist ($session_id, $course_id) {
  635. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  636. $return_value = false;
  637. $sql= "SELECT course_code FROM $tbl_session_course WHERE id_session = ".Database::escape_string($session_id)." AND course_code = '".Database::escape_string($course_id)."'";
  638. $result = Database::query($sql, __FILE__, __LINE__);
  639. $num = Database::num_rows($result);
  640. if ($num>0) {
  641. $return_value = true;
  642. }
  643. return $return_value;
  644. }
  645. /**
  646. * Get the session information by name
  647. * @param string session name
  648. * @return mixed false if the session does not exist, array if the session exist
  649. * */
  650. public static function get_session_by_name ($session_name) {
  651. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  652. $sql = 'SELECT id, id_coach, date_start, date_end FROM '.$tbl_session.' WHERE name="'.Database::escape_string($session_name).'"';
  653. $result = Database::query($sql, __FILE__, __LINE__);
  654. $num = Database::num_rows($result);
  655. if ($num>0){
  656. return Database::fetch_array($result);
  657. } else {
  658. return false;
  659. }
  660. }
  661. /**
  662. * Create a session category
  663. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>, from existing code
  664. * @param string name
  665. * @param integer year_start
  666. * @param integer month_start
  667. * @param integer day_start
  668. * @param integer year_end
  669. * @param integer month_end
  670. * @param integer day_end
  671. * @return $id_session;
  672. **/
  673. public static function create_category_session($sname,$syear_start,$smonth_start,$sday_start,$syear_end,$smonth_end, $sday_end){
  674. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  675. $name= trim($sname);
  676. $year_start= intval($syear_start);
  677. $month_start=intval($smonth_start);
  678. $day_start=intval($sday_start);
  679. $year_end=intval($syear_end);
  680. $month_end=intval($smonth_end);
  681. $day_end=intval($sday_end);
  682. $date_start = "$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
  683. $date_end = "$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
  684. if (empty($name)) {
  685. $msg=get_lang('SessionCategoryNameIsRequired');
  686. return $msg;
  687. } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start,$day_start,$year_start)) {
  688. $msg=get_lang('InvalidStartDate');
  689. return $msg;
  690. } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end,$day_end,$year_end)) {
  691. $msg=get_lang('InvalidEndDate');
  692. return $msg;
  693. } elseif($date_start >= $date_end) {
  694. $msg=get_lang('StartDateShouldBeBeforeEndDate');
  695. return $msg;
  696. } else {
  697. $sql = "INSERT INTO $tbl_session_category(name, date_start, date_end) VALUES('".Database::escape_string($name)."','$date_start','$date_end')";
  698. Database::query($sql ,__FILE__,__LINE__);
  699. $id_session=Database::insert_id();
  700. // add event to system log
  701. $time = time();
  702. $user_id = api_get_user_id();
  703. event_system(LOG_SESSION_CATEGORY_CREATE, LOG_SESSION_CATEGORY_ID, $id_session, $time, $user_id);
  704. return $id_session;
  705. }
  706. }
  707. /**
  708. * Edit a sessions categories
  709. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,from existing code
  710. * @param integer id
  711. * @param string name
  712. * @param integer year_start
  713. * @param integer month_start
  714. * @param integer day_start
  715. * @param integer year_end
  716. * @param integer month_end
  717. * @param integer day_end
  718. * @return $id;
  719. * The parameter id is a primary key
  720. **/
  721. public static function edit_category_session($id, $sname,$syear_start,$smonth_start,$sday_start,$syear_end,$smonth_end, $sday_end){
  722. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  723. $name= trim($sname);
  724. $year_start= intval($syear_start);
  725. $month_start=intval($smonth_start);
  726. $day_start=intval($sday_start);
  727. $year_end=intval($syear_end);
  728. $month_end=intval($smonth_end);
  729. $day_end=intval($sday_end);
  730. $id=intval($id);
  731. $date_start = "$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
  732. $date_end = "$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
  733. if (empty($name)) {
  734. $msg=get_lang('SessionCategoryNameIsRequired');
  735. return $msg;
  736. } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start,$day_start,$year_start)) {
  737. $msg=get_lang('InvalidStartDate');
  738. return $msg;
  739. } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end,$day_end,$year_end)) {
  740. $msg=get_lang('InvalidEndDate');
  741. return $msg;
  742. } elseif($date_start >= $date_end) {
  743. $msg=get_lang('StartDateShouldBeBeforeEndDate');
  744. return $msg;
  745. } else {
  746. $sql = "UPDATE $tbl_session_category SET name = '".Database::escape_string($name)."', date_start = '$date_start', date_end = '$date_end'
  747. WHERE id= '".$id."' ";
  748. $result = Database::query($sql, __FILE__,__LINE__);
  749. return ($result? true:false);
  750. }
  751. }
  752. /**
  753. * Delete sessions categories
  754. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>, from existing code
  755. * @param array id_checked
  756. * @param bool include delete session
  757. * @param bool optional, true if the function is called by a webservice, false otherwise.
  758. * @return void Nothing, or false on error
  759. * The parameters is a array to delete sessions
  760. **/
  761. public static function delete_session_category($id_checked, $delete_session = false,$from_ws = false){
  762. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  763. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  764. if(is_array($id_checked)) {
  765. $id_checked=Database::escape_string(implode(',',$id_checked));
  766. } else {
  767. $id_checked=intval($id_checked);
  768. }
  769. $sql = "SELECT id FROM $tbl_session WHERE session_category_id IN (".$id_checked.")";
  770. $result = @Database::query($sql,__FILE__,__LINE__);
  771. while ($rows = Database::fetch_array($result)) {
  772. $session_id = $rows['id'];
  773. if($delete_session == true){
  774. if ($from_ws) {
  775. SessionManager::delete_session($session_id,true);
  776. } else {
  777. SessionManager::delete_session($session_id);
  778. }
  779. }
  780. }
  781. $sql = "DELETE FROM $tbl_session_category WHERE id IN (".$id_checked.")";
  782. $rs = @Database::query($sql,__FILE__,__LINE__);
  783. $result = Database::affected_rows();
  784. // add event to system log
  785. $time = time();
  786. $user_id = api_get_user_id();
  787. event_system(LOG_SESSION_CATEGORY_DELETE, LOG_SESSION_CATEGORY_ID, $id_checked, $time, $user_id);
  788. // delete extra session fields where field variable is "PERIODO"
  789. $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  790. $t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  791. $sql = "SELECT t_sfv.field_id FROM $t_sfv t_sfv, $t_sf t_sf WHERE t_sfv.session_id = '$id_checked' AND t_sf.field_variable = 'PERIODO' ";
  792. $rs_field = Database::query($sql,__FILE__,__LINE__);
  793. $field_id = 0;
  794. if (Database::num_rows($rs_field) > 0) {
  795. $row_field = Database::fetch_row($rs_field);
  796. $field_id = $row_field[0];
  797. $sql_delete_sfv = "DELETE FROM $t_sfv WHERE session_id = '$id_checked' AND field_id = '$field_id'";
  798. $rs_delete_sfv = Database::query($sql_delete_sfv,__FILE__,__LINE__);
  799. }
  800. $sql = "SELECT * FROM $t_sfv WHERE field_id = '$field_id' ";
  801. $rs_field_id = Database::query($sql,__FILE__,__LINE__);
  802. if (Database::num_rows($rs_field_id) == 0) {
  803. $sql_delete_sf = "DELETE FROM $t_sf WHERE id = '$field_id'";
  804. $rs_delete_sf = Database::query($sql_delete_sf,__FILE__,__LINE__);
  805. }
  806. return true;
  807. }
  808. /**
  809. * Get a list of sessions of which the given conditions match with an = 'cond'
  810. * @param array $conditions a list of condition (exemple : status=>STUDENT)
  811. * @param array $order_by a list of fields on which sort
  812. * @return array An array with all sessions of the platform.
  813. * @todo optional course code parameter, optional sorting parameters...
  814. */
  815. public static function get_sessions_list ($conditions = array(), $order_by = array()) {
  816. $session_table =Database::get_main_table(TABLE_MAIN_SESSION);
  817. $session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  818. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  819. $return_array = array();
  820. $sql_query = " SELECT s.id, s.name, s.nbr_courses, s.date_start, s.date_end, u.firstname, u.lastname , sc.name as category_name
  821. FROM $session_table s
  822. INNER JOIN $user_table u ON s.id_coach = u.user_id
  823. LEFT JOIN $session_category_table sc ON s.session_category_id = sc.id ";
  824. if (count($conditions)>0) {
  825. $sql_query .= ' WHERE ';
  826. foreach ($conditions as $field=>$value) {
  827. $field = Database::escape_string($field);
  828. $value = Database::escape_string($value);
  829. $sql_query .= $field.' = '.$value;
  830. }
  831. }
  832. if (count($order_by)>0) {
  833. $sql_query .= ' ORDER BY '.Database::escape_string(implode(',',$order_by));
  834. }
  835. $sql_result = Database::query($sql_query,__FILE__,__LINE__);
  836. while ($result = Database::fetch_array($sql_result)) {
  837. $return_array[] = $result;
  838. }
  839. return $return_array;
  840. }
  841. /**
  842. * Get the session category information by id
  843. * @param string session category ID
  844. * @return mixed false if the session category does not exist, array if the session category exists
  845. */
  846. public static function get_session_category ($id) {
  847. $id = intval($id);
  848. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  849. $sql = 'SELECT id, name, date_start, date_end FROM '.$tbl_session_category.' WHERE id="'.$id.'"';
  850. $result = Database::query($sql, __FILE__, __LINE__);
  851. $num = Database::num_rows($result);
  852. if ($num>0){
  853. return Database::fetch_array($result);
  854. } else {
  855. return false;
  856. }
  857. }
  858. /**
  859. * Assign a coach to course in session with status = 2
  860. * @param int - user id
  861. * @param int - session id
  862. * @param string - course code
  863. * @param bool - optional, if is true the user don't be a coach now, otherwise it'll assign a coach
  864. * @return bool true if there are affected rows, otherwise false
  865. */
  866. public static function set_coach_to_course_session($user_id, $session_id = 0, $course_code = '',$nocoach = false) {
  867. // Definition of variables
  868. $user_id = intval($user_id);
  869. if (!empty($session_id)) {
  870. $session_id = intval($session_id);
  871. } else {
  872. $session_id = api_get_session_id();
  873. }
  874. if (!empty($course_code)) {
  875. $course_code = Database::escape_string($course_code);
  876. } else {
  877. $course_code = api_get_course_id();
  878. }
  879. // definitios of tables
  880. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  881. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  882. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  883. // check if user is a teacher
  884. $sql= "SELECT * FROM $tbl_user WHERE status='1' AND user_id = '$user_id'";
  885. $rs_check_user = Database::query($sql,__FILE__,__LINE__);
  886. if (Database::num_rows($rs_check_user) > 0) {
  887. if ($nocoach) {
  888. // check if user_id exits int session_rel_user
  889. $sql = "SELECT id_user FROM $tbl_session_rel_user WHERE id_session = '$session_id' AND id_user = '$user_id'";
  890. $res = Database::query($sql,__FILE__,__LINE__);
  891. if (Database::num_rows($res) > 0) {
  892. // The user don't be a coach now
  893. $sql = "UPDATE $tbl_session_rel_course_rel_user SET status = 0 WHERE id_session = '$session_id' AND course_code = '$course_code' AND id_user = '$user_id' ";
  894. $rs_update = Database::query($sql,__FILE__,__LINE__);
  895. if (Database::affected_rows() > 0) return true;
  896. else return false;
  897. } else {
  898. // The user don't be a coach now
  899. $sql = "DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session = '$session_id' AND course_code = '$course_code' AND id_user = '$user_id' ";
  900. $rs_delete = Database::query($sql,__FILE__,__LINE__);
  901. if (Database::affected_rows() > 0) return true;
  902. else return false;
  903. }
  904. } else {
  905. // Assign user like a coach to course
  906. // First check if the user is registered in the course
  907. $sql = "SELECT id_user FROM $tbl_session_rel_course_rel_user WHERE id_session = '$session_id' AND course_code = '$course_code' AND id_user = '$user_id'";
  908. $rs_check = Database::query($sql,__FILE__,__LINE__);
  909. //Then update or insert
  910. if (Database::num_rows($rs_check) > 0) {
  911. $sql = "UPDATE $tbl_session_rel_course_rel_user SET status = 2 WHERE id_session = '$session_id' AND course_code = '$course_code' AND id_user = '$user_id' ";
  912. $rs_update = Database::query($sql,__FILE__,__LINE__);
  913. if (Database::affected_rows() > 0) return true;
  914. else return false;
  915. } else {
  916. $sql = " INSERT INTO $tbl_session_rel_course_rel_user(id_session, course_code, id_user, status) VALUES('$session_id', '$course_code', '$user_id', 2)";
  917. $rs_insert = Database::query($sql,__FILE__,__LINE__);
  918. if (Database::affected_rows() > 0) return true;
  919. else return false;
  920. }
  921. }
  922. } else {
  923. return false;
  924. }
  925. }
  926. }