sessionmanager.lib.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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) {
  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. if (empty($visibility)) {
  296. $sql = "SELECT visibility FROM $tbl_session WHERE id_session='$id_session'";
  297. $result = Database::query($sql,__FILE__,__LINE__);
  298. $row = Database::fetch_array($result);
  299. $visibility = $row['visibility'];
  300. if (empty($visibility))
  301. $visibility = SESSION_VISIBLE_READ_ONLY; // by default readonly 1
  302. }
  303. $sql = "SELECT id_user FROM $tbl_session_rel_user WHERE id_session='$id_session'";
  304. $result = Database::query($sql,__FILE__,__LINE__);
  305. $existingUsers = array();
  306. while($row = Database::fetch_array($result)){
  307. $existingUsers[] = $row['id_user'];
  308. }
  309. $sql = "SELECT course_code FROM $tbl_session_rel_course WHERE id_session='$id_session'";
  310. $result=Database::query($sql,__FILE__,__LINE__);
  311. $course_list=array();
  312. while($row=Database::fetch_array($result)) {
  313. $course_list[]=$row['course_code'];
  314. }
  315. foreach ($course_list as $enreg_course) {
  316. // for each course in the session
  317. $nbr_users=0;
  318. $enreg_course = Database::escape_string($enreg_course);
  319. // delete existing users
  320. if ($empty_users!==false) {
  321. foreach ($existingUsers as $existing_user) {
  322. if(!in_array($existing_user, $user_list)) {
  323. $sql = "DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course' AND id_user='$existing_user'";
  324. Database::query($sql,__FILE__,__LINE__);
  325. if(Database::affected_rows()) {
  326. $nbr_users--;
  327. }
  328. }
  329. }
  330. }
  331. // insert new users into session_rel_course_rel_user and ignore if they already exist
  332. foreach ($user_list as $enreg_user) {
  333. if(!in_array($enreg_user, $existingUsers)) {
  334. $enreg_user = Database::escape_string($enreg_user);
  335. $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','$visibility')";
  336. Database::query($insert_sql,__FILE__,__LINE__);
  337. if(Database::affected_rows()) {
  338. $nbr_users++;
  339. }
  340. }
  341. }
  342. // count users in this session-course relation
  343. $sql = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course'";
  344. $rs = Database::query($sql, __FILE__, __LINE__);
  345. list($nbr_users) = Database::fetch_array($rs);
  346. // update the session-course relation to add the users total
  347. $update_sql = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'";
  348. Database::query($update_sql,__FILE__,__LINE__);
  349. }
  350. // delete users from the session
  351. if ($empty_users===true){
  352. Database::query("DELETE FROM $tbl_session_rel_user WHERE id_session = $id_session",__FILE__,__LINE__);
  353. }
  354. // insert missing users into session
  355. $nbr_users = 0;
  356. foreach ($user_list as $enreg_user) {
  357. $enreg_user = Database::escape_string($enreg_user);
  358. $nbr_users++;
  359. $insert_sql = "INSERT IGNORE INTO $tbl_session_rel_user(id_session, id_user) VALUES('$id_session','$enreg_user')";
  360. Database::query($insert_sql,__FILE__,__LINE__);
  361. }
  362. // update number of users in the session
  363. $nbr_users = count($user_list);
  364. $update_sql = "UPDATE $tbl_session SET nbr_users= $nbr_users WHERE id='$id_session' ";
  365. Database::query($update_sql,__FILE__,__LINE__);
  366. }
  367. /** Subscribes courses to the given session and optionally (default) unsubscribes previous users
  368. * @author Carlos Vargas <carlos.vargas@dokeos.com>,from existing code
  369. * @param int Session ID
  370. * @param array List of courses IDs
  371. * @param bool Whether to unsubscribe existing users (true, default) or not (false)
  372. * @return void Nothing, or false on error
  373. **/
  374. public static function add_courses_to_session ($id_session, $course_list, $empty_courses=true) {
  375. // security checks
  376. if ($id_session!= strval(intval($id_session))) { return false; }
  377. foreach($course_list as $intCourse){
  378. if ($intCourse!= strval(intval($intCourse))) { return false; }
  379. }
  380. // initialisation
  381. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  382. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  383. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  384. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  385. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  386. // get general coach ID
  387. $id_coach = Database::query("SELECT id_coach FROM $tbl_session WHERE id=$id_session");
  388. $id_coach = Database::fetch_array($id_coach);
  389. $id_coach = $id_coach[0];
  390. // get list of courses subscribed to this session
  391. $rs = Database::query("SELECT course_code FROM $tbl_session_rel_course WHERE id_session=$id_session");
  392. $existingCourses = Database::store_result($rs);
  393. $nbr_courses=count($existingCourses);
  394. // get list of users subscribed to this session
  395. $sql="SELECT id_user
  396. FROM $tbl_session_rel_user
  397. WHERE id_session = $id_session";
  398. $result=Database::query($sql,__FILE__,__LINE__);
  399. $user_list=Database::store_result($result);
  400. // remove existing courses from the session
  401. if ($empty_courses===true) {
  402. foreach ($existingCourses as $existingCourse) {
  403. if (!in_array($existingCourse['course_code'], $course_list)){
  404. Database::query("DELETE FROM $tbl_session_rel_course WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  405. Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE course_code='".$existingCourse['course_code']."' AND id_session=$id_session");
  406. }
  407. }
  408. $nbr_courses=0;
  409. }
  410. // Pass through the courses list we want to add to the session
  411. foreach ($course_list as $enreg_course) {
  412. $enreg_course = Database::escape_string($enreg_course);
  413. $exists = false;
  414. // check if the course we want to add is already subscribed
  415. foreach ($existingCourses as $existingCourse) {
  416. if ($enreg_course == $existingCourse['course_code']) {
  417. $exists=true;
  418. }
  419. }
  420. if (!$exists) {
  421. //if the course isn't subscribed yet
  422. $sql_insert_rel_course= "INSERT INTO $tbl_session_rel_course (id_session,course_code, id_coach) VALUES ('$id_session','$enreg_course','$id_coach')";
  423. Database::query($sql_insert_rel_course ,__FILE__,__LINE__);
  424. //We add the current course in the existing courses array, to avoid adding another time the current course
  425. $existingCourses[]=array('course_code'=>$enreg_course);
  426. $nbr_courses++;
  427. // subscribe all the users from the session to this course inside the session
  428. $nbr_users=0;
  429. foreach ($user_list as $enreg_user) {
  430. $enreg_user_id = Database::escape_string($enreg_user['id_user']);
  431. $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')";
  432. Database::query($sql_insert,__FILE__,__LINE__);
  433. if (Database::affected_rows()) {
  434. $nbr_users++;
  435. }
  436. }
  437. Database::query("UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'",__FILE__,__LINE__);
  438. }
  439. }
  440. Database::query("UPDATE $tbl_session SET nbr_courses=$nbr_courses WHERE id='$id_session'",__FILE__,__LINE__);
  441. }
  442. /**
  443. * Creates a new extra field for a given session
  444. * @param string Field's internal variable name
  445. * @param int Field's type
  446. * @param string Field's language var name
  447. * @return int new extra field id
  448. */
  449. public static function create_session_extra_field ($fieldvarname, $fieldtype, $fieldtitle) {
  450. // database table definition
  451. $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  452. $fieldvarname = Database::escape_string($fieldvarname);
  453. $fieldtitle = Database::escape_string($fieldtitle);
  454. $fieldtype = (int)$fieldtype;
  455. $time = time();
  456. $sql_field = "SELECT id FROM $t_sf WHERE field_variable = '$fieldvarname'";
  457. $res_field = Database::query($sql_field,__FILE__,__LINE__);
  458. $r_field = Database::fetch_row($res_field);
  459. if (Database::num_rows($res_field)>0) {
  460. $field_id = $r_field[0];
  461. } else {
  462. // save new fieldlabel into course_field table
  463. $sql = "SELECT MAX(field_order) FROM $t_sf";
  464. $res = Database::query($sql,__FILE__,__LINE__);
  465. $order = 0;
  466. if (Database::num_rows($res)>0) {
  467. $row = Database::fetch_row($res);
  468. $order = $row[0]+1;
  469. }
  470. $sql = "INSERT INTO $t_sf
  471. SET field_type = '$fieldtype',
  472. field_variable = '$fieldvarname',
  473. field_display_text = '$fieldtitle',
  474. field_order = '$order',
  475. tms = FROM_UNIXTIME($time)";
  476. $result = Database::query($sql,__FILE__,__LINE__);
  477. $field_id=Database::insert_id();
  478. }
  479. return $field_id;
  480. }
  481. /**
  482. * Update an extra field value for a given session
  483. * @param integer Course ID
  484. * @param string Field variable name
  485. * @param string Field value
  486. * @return boolean true if field updated, false otherwise
  487. */
  488. public static function update_session_extra_field_value ($session_id,$fname,$fvalue='') {
  489. $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  490. $t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  491. $fname = Database::escape_string($fname);
  492. $session_id = (int)$session_id;
  493. $fvalues = '';
  494. if(is_array($fvalue))
  495. {
  496. foreach($fvalue as $val)
  497. {
  498. $fvalues .= Database::escape_string($val).';';
  499. }
  500. if(!empty($fvalues))
  501. {
  502. $fvalues = substr($fvalues,0,-1);
  503. }
  504. }
  505. else
  506. {
  507. $fvalues = Database::escape_string($fvalue);
  508. }
  509. $sqlsf = "SELECT * FROM $t_sf WHERE field_variable='$fname'";
  510. $ressf = Database::query($sqlsf,__FILE__,__LINE__);
  511. if(Database::num_rows($ressf)==1)
  512. { //ok, the field exists
  513. // Check if enumerated field, if the option is available
  514. $rowsf = Database::fetch_array($ressf);
  515. $tms = time();
  516. $sqlsfv = "SELECT * FROM $t_sfv WHERE session_id = '$session_id' AND field_id = '".$rowsf['id']."' ORDER BY id";
  517. $ressfv = Database::query($sqlsfv,__FILE__,__LINE__);
  518. $n = Database::num_rows($ressfv);
  519. if ($n>1) {
  520. //problem, we already have to values for this field and user combination - keep last one
  521. while($rowsfv = Database::fetch_array($ressfv))
  522. {
  523. if($n > 1)
  524. {
  525. $sqld = "DELETE FROM $t_sfv WHERE id = ".$rowsfv['id'];
  526. $resd = Database::query($sqld,__FILE__,__LINE__);
  527. $n--;
  528. }
  529. $rowsfv = Database::fetch_array($ressfv);
  530. if($rowsfv['field_value'] != $fvalues)
  531. {
  532. $sqlu = "UPDATE $t_sfv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms) WHERE id = ".$rowsfv['id'];
  533. $resu = Database::query($sqlu,__FILE__,__LINE__);
  534. return($resu?true:false);
  535. }
  536. return true;
  537. }
  538. } else if ($n==1) {
  539. //we need to update the current record
  540. $rowsfv = Database::fetch_array($ressfv);
  541. if($rowsfv['field_value'] != $fvalues)
  542. {
  543. $sqlu = "UPDATE $t_sfv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms) WHERE id = ".$rowsfv['id'];
  544. //error_log('UM::update_extra_field_value: '.$sqlu);
  545. $resu = Database::query($sqlu,__FILE__,__LINE__);
  546. return($resu?true:false);
  547. }
  548. return true;
  549. } else {
  550. $sqli = "INSERT INTO $t_sfv (session_id,field_id,field_value,tms) " .
  551. "VALUES ('$session_id',".$rowsf['id'].",'$fvalues',FROM_UNIXTIME($tms))";
  552. //error_log('UM::update_extra_field_value: '.$sqli);
  553. $resi = Database::query($sqli,__FILE__,__LINE__);
  554. return($resi?true:false);
  555. }
  556. } else {
  557. return false; //field not found
  558. }
  559. }
  560. /**
  561. * Checks the relationship between a session and a course.
  562. * @param int $session_id
  563. * @param int $course_id
  564. * @return bool Returns TRUE if the session and the course are related, FALSE otherwise.
  565. * */
  566. public static function relation_session_course_exist ($session_id, $course_id) {
  567. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  568. $return_value = false;
  569. $sql= "SELECT course_code FROM $tbl_session_course WHERE id_session = ".Database::escape_string($session_id)." AND course_code = '".Database::escape_string($course_id)."'";
  570. $result = Database::query($sql, __FILE__, __LINE__);
  571. $num = Database::num_rows($result);
  572. if ($num>0) {
  573. $return_value = true;
  574. }
  575. return $return_value;
  576. }
  577. /**
  578. * Get the session information by name
  579. * @param string session name
  580. * @return mixed false if the session does not exist, array if the session exist
  581. * */
  582. public static function get_session_by_name ($session_name) {
  583. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  584. $sql = 'SELECT id, id_coach, date_start, date_end FROM '.$tbl_session.' WHERE name="'.Database::escape_string($session_name).'"';
  585. $result = Database::query($sql, __FILE__, __LINE__);
  586. $num = Database::num_rows($result);
  587. if ($num>0){
  588. return Database::fetch_array($result);
  589. } else {
  590. return false;
  591. }
  592. }
  593. /**
  594. * Create a session category
  595. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>, from existing code
  596. * @param string name
  597. * @param integer year_start
  598. * @param integer month_start
  599. * @param integer day_start
  600. * @param integer year_end
  601. * @param integer month_end
  602. * @param integer day_end
  603. * @return $id_session;
  604. **/
  605. public static function create_category_session($sname,$syear_start,$smonth_start,$sday_start,$syear_end,$smonth_end, $sday_end){
  606. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  607. $name= trim($sname);
  608. $year_start= intval($syear_start);
  609. $month_start=intval($smonth_start);
  610. $day_start=intval($sday_start);
  611. $year_end=intval($syear_end);
  612. $month_end=intval($smonth_end);
  613. $day_end=intval($sday_end);
  614. $date_start = "$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
  615. $date_end = "$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
  616. if (empty($name)) {
  617. $msg=get_lang('SessionCategoryNameIsRequired');
  618. return $msg;
  619. } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start,$day_start,$year_start)) {
  620. $msg=get_lang('InvalidStartDate');
  621. return $msg;
  622. } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end,$day_end,$year_end)) {
  623. $msg=get_lang('InvalidEndDate');
  624. return $msg;
  625. } elseif($date_start >= $date_end) {
  626. $msg=get_lang('StartDateShouldBeBeforeEndDate');
  627. return $msg;
  628. } else {
  629. $sql = "INSERT INTO $tbl_session_category(name, date_start, date_end) VALUES('".Database::escape_string($name)."','$date_start','$date_end')";
  630. Database::query($sql ,__FILE__,__LINE__);
  631. $id_session=Database::insert_id();
  632. // add event to system log
  633. $time = time();
  634. $user_id = api_get_user_id();
  635. event_system(LOG_SESSION_CATEGORY_CREATE, LOG_SESSION_CATEGORY_ID, $id_session, $time, $user_id);
  636. return $id_session;
  637. }
  638. }
  639. /**
  640. * Edit a sessions categories
  641. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,from existing code
  642. * @param integer id
  643. * @param string name
  644. * @param integer year_start
  645. * @param integer month_start
  646. * @param integer day_start
  647. * @param integer year_end
  648. * @param integer month_end
  649. * @param integer day_end
  650. * @return $id;
  651. * The parameter id is a primary key
  652. **/
  653. public static function edit_category_session($id, $sname,$syear_start,$smonth_start,$sday_start,$syear_end,$smonth_end, $sday_end){
  654. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  655. $name= trim($sname);
  656. $year_start= intval($syear_start);
  657. $month_start=intval($smonth_start);
  658. $day_start=intval($sday_start);
  659. $year_end=intval($syear_end);
  660. $month_end=intval($smonth_end);
  661. $day_end=intval($sday_end);
  662. $id=intval($id);
  663. $date_start = "$year_start-".(($month_start < 10)?"0$month_start":$month_start)."-".(($day_start < 10)?"0$day_start":$day_start);
  664. $date_end = "$year_end-".(($month_end < 10)?"0$month_end":$month_end)."-".(($day_end < 10)?"0$day_end":$day_end);
  665. if (empty($name)) {
  666. $msg=get_lang('SessionCategoryNameIsRequired');
  667. return $msg;
  668. } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start,$day_start,$year_start)) {
  669. $msg=get_lang('InvalidStartDate');
  670. return $msg;
  671. } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end,$day_end,$year_end)) {
  672. $msg=get_lang('InvalidEndDate');
  673. return $msg;
  674. } elseif($date_start >= $date_end) {
  675. $msg=get_lang('StartDateShouldBeBeforeEndDate');
  676. return $msg;
  677. } else {
  678. $sql = "UPDATE $tbl_session_category SET name = '".Database::escape_string($name)."', date_start = '$date_start', date_end = '$date_end'
  679. WHERE id= '".$id."' ";
  680. $result = Database::query($sql, __FILE__,__LINE__);
  681. return ($result? true:false);
  682. }
  683. }
  684. /**
  685. * Delete sessions categories
  686. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>, from existing code
  687. * @param array id_checked
  688. * @param bool include delete session
  689. * @param bool optional, true if the function is called by a webservice, false otherwise.
  690. * @return void Nothing, or false on error
  691. * The parameters is a array to delete sessions
  692. **/
  693. public static function delete_session_category($id_checked, $delete_session = false,$from_ws = false){
  694. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  695. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  696. if(is_array($id_checked)) {
  697. $id_checked=Database::escape_string(implode(',',$id_checked));
  698. } else {
  699. $id_checked=intval($id_checked);
  700. }
  701. $sql = "SELECT id FROM $tbl_session WHERE session_category_id IN (".$id_checked.")";
  702. $result = @Database::query($sql,__FILE__,__LINE__);
  703. while ($rows = Database::fetch_array($result)) {
  704. $session_id = $rows['id'];
  705. if($delete_session == true){
  706. if ($from_ws) {
  707. SessionManager::delete_session($session_id,true);
  708. } else {
  709. SessionManager::delete_session($session_id);
  710. }
  711. }
  712. }
  713. $sql = "DELETE FROM $tbl_session_category WHERE id IN (".$id_checked.")";
  714. $rs = @Database::query($sql,__FILE__,__LINE__);
  715. $result = Database::affected_rows();
  716. // add event to system log
  717. $time = time();
  718. $user_id = api_get_user_id();
  719. event_system(LOG_SESSION_CATEGORY_DELETE, LOG_SESSION_CATEGORY_ID, $id_checked, $time, $user_id);
  720. // delete extra session fields where field variable is "PERIODO"
  721. $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  722. $t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  723. $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' ";
  724. $rs_field = Database::query($sql,__FILE__,__LINE__);
  725. $field_id = 0;
  726. if (Database::num_rows($rs_field) > 0) {
  727. $row_field = Database::fetch_row($rs_field);
  728. $field_id = $row_field[0];
  729. $sql_delete_sfv = "DELETE FROM $t_sfv WHERE session_id = '$id_checked' AND field_id = '$field_id'";
  730. $rs_delete_sfv = Database::query($sql_delete_sfv,__FILE__,__LINE__);
  731. }
  732. $sql = "SELECT * FROM $t_sfv WHERE field_id = '$field_id' ";
  733. $rs_field_id = Database::query($sql,__FILE__,__LINE__);
  734. if (Database::num_rows($rs_field_id) == 0) {
  735. $sql_delete_sf = "DELETE FROM $t_sf WHERE id = '$field_id'";
  736. $rs_delete_sf = Database::query($sql_delete_sf,__FILE__,__LINE__);
  737. }
  738. return true;
  739. }
  740. /**
  741. * Get a list of sessions of which the given conditions match with an = 'cond'
  742. * @param array $conditions a list of condition (exemple : status=>STUDENT)
  743. * @param array $order_by a list of fields on which sort
  744. * @return array An array with all sessions of the platform.
  745. * @todo optional course code parameter, optional sorting parameters...
  746. */
  747. public static function get_sessions_list ($conditions = array(), $order_by = array()) {
  748. $session_table =Database::get_main_table(TABLE_MAIN_SESSION);
  749. $session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  750. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  751. $return_array = array();
  752. $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
  753. FROM $session_table s
  754. INNER JOIN $user_table u ON s.id_coach = u.user_id
  755. LEFT JOIN $session_category_table sc ON s.session_category_id = sc.id ";
  756. if (count($conditions)>0) {
  757. $sql_query .= ' WHERE ';
  758. foreach ($conditions as $field=>$value) {
  759. $field = Database::escape_string($field);
  760. $value = Database::escape_string($value);
  761. $sql_query .= $field.' = '.$value;
  762. }
  763. }
  764. if (count($order_by)>0) {
  765. $sql_query .= ' ORDER BY '.Database::escape_string(implode(',',$order_by));
  766. }
  767. $sql_result = Database::query($sql_query,__FILE__,__LINE__);
  768. while ($result = Database::fetch_array($sql_result)) {
  769. $return_array[] = $result;
  770. }
  771. return $return_array;
  772. }
  773. /**
  774. * Get the session category information by id
  775. * @param string session category ID
  776. * @return mixed false if the session category does not exist, array if the session category exists
  777. */
  778. public static function get_session_category ($id) {
  779. $id = intval($id);
  780. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  781. $sql = 'SELECT id, name, date_start, date_end FROM '.$tbl_session_category.' WHERE id="'.$id.'"';
  782. $result = Database::query($sql, __FILE__, __LINE__);
  783. $num = Database::num_rows($result);
  784. if ($num>0){
  785. return Database::fetch_array($result);
  786. } else {
  787. return false;
  788. }
  789. }
  790. /**
  791. * Assign a coach to course in session with status = 2
  792. * @param int - user id
  793. * @param int - session id
  794. * @param string - course code
  795. * @param bool - optional, if is true the user don't be a coach now, otherwise it'll assign a coach
  796. * @return bool true if there are affected rows, otherwise false
  797. */
  798. function set_coach_to_course_session($user_id, $session_id = 0, $course_code = '',$nocoach = false) {
  799. // Definition of variables
  800. $user_id = intval($user_id);
  801. if (!empty($session_id)) {
  802. $session_id = intval($session_id);
  803. } else {
  804. $session_id = api_get_session_id();
  805. }
  806. if (!empty($course_code)) {
  807. $course_code = Database::escape_string($course_code);
  808. } else {
  809. $course_code = api_get_course_id();
  810. }
  811. // definitios of tables
  812. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  813. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  814. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  815. // check if user is a teacher
  816. $sql= "SELECT * FROM $tbl_user WHERE status='1' AND user_id = '$user_id'";
  817. $rs_check_user = Database::query($sql,__FILE__,__LINE__);
  818. if (Database::num_rows($rs_check_user) > 0) {
  819. if ($nocoach) {
  820. // check if user_id exits int session_rel_user
  821. $sql = "SELECT id_user FROM $tbl_session_rel_user WHERE id_session = '$session_id' AND id_user = '$user_id'";
  822. $res = Database::query($sql,__FILE__,__LINE__);
  823. if (Database::num_rows($res) > 0) {
  824. // The user don't be a coach now
  825. $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' ";
  826. $rs_update = Database::query($sql,__FILE__,__LINE__);
  827. if (Database::affected_rows() > 0) return true;
  828. else return false;
  829. } else {
  830. // The user don't be a coach now
  831. $sql = "DELETE FROM $tbl_session_rel_course_rel_user WHERE id_session = '$session_id' AND course_code = '$course_code' AND id_user = '$user_id' ";
  832. $rs_delete = Database::query($sql,__FILE__,__LINE__);
  833. if (Database::affected_rows() > 0) return true;
  834. else return false;
  835. }
  836. } else {
  837. // Assign user like a coach to course
  838. // First check if the user is registered in the course
  839. $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'";
  840. $rs_check = Database::query($sql,__FILE__,__LINE__);
  841. //Then update or insert
  842. if (Database::num_rows($rs_check) > 0) {
  843. $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' ";
  844. $rs_update = Database::query($sql,__FILE__,__LINE__);
  845. if (Database::affected_rows() > 0) return true;
  846. else return false;
  847. } else {
  848. $sql = " INSERT INTO $tbl_session_rel_course_rel_user(id_session, course_code, id_user, status) VALUES('$session_id', '$course_code', '$user_id', 2)";
  849. $rs_insert = Database::query($sql,__FILE__,__LINE__);
  850. if (Database::affected_rows() > 0) return true;
  851. else return false;
  852. }
  853. }
  854. } else {
  855. return false;
  856. }
  857. }
  858. }