agenda.lib.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @author: Julio Montoya <gugli100@gmail.com> Implementing a real agenda lib
  5. */
  6. class Agenda {
  7. var $events = array();
  8. var $type = 'personal'; // personal, admin or course
  9. function __construct() {
  10. //Table definitions
  11. $this->tbl_global_agenda = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  12. $this->tbl_personal_agenda = Database::get_user_personal_table(TABLE_PERSONAL_AGENDA);
  13. $this->tbl_course_agenda = Database::get_course_table(TABLE_AGENDA);
  14. //Setting the course object if we are in a course
  15. $this->course = null;
  16. $course_info = api_get_course_info();
  17. if (!empty($course_info)) {
  18. $this->course = $course_info;
  19. }
  20. $this->events = array();
  21. //Event colors
  22. $this->event_platform_color = 'red';//red
  23. $this->event_course_color = '#458B00'; //green
  24. $this->event_group_color = '#A0522D'; //siena
  25. $this->event_session_color = '#000080'; // blue
  26. $this->event_personal_color = 'steel blue'; //steel blue
  27. }
  28. /**
  29. *
  30. * Adds an event
  31. * @param int start tms
  32. * @param int end tms
  33. * @param string agendaDay, agendaWeek, month
  34. * @param string personal, course or global (only works for personal by now)
  35. */
  36. function add_event($start, $end, $all_day, $view, $title, $content, $users_to_send = array()) {
  37. $start = date('Y-m-d H:i:s', $start);
  38. $end = date('Y-m-d H:i:s', $end);
  39. $start = api_get_utc_datetime($start);
  40. $end = api_get_utc_datetime($end);
  41. $all_day = isset($all_day) && $all_day == 'true' ? 1:0;
  42. $attributes = array();
  43. $id = null;
  44. switch ($this->type) {
  45. case 'personal':
  46. $attributes['user'] = api_get_user_id();
  47. $attributes['title'] = $title;
  48. $attributes['text'] = $content;
  49. $attributes['date'] = $start;
  50. $attributes['enddate'] = $end;
  51. $attributes['all_day'] = $all_day;
  52. $id = Database::insert($this->tbl_personal_agenda, $attributes);
  53. break;
  54. case 'course':
  55. //$attributes['user'] = api_get_user_id();
  56. $attributes['title'] = $title;
  57. $attributes['content'] = $content;
  58. $attributes['start_date'] = $start;
  59. $attributes['end_date'] = $end;
  60. $attributes['all_day'] = $all_day;
  61. $attributes['session_id'] = api_get_session_id();
  62. $attributes['c_id'] = $this->course['real_id'];
  63. //simple course event
  64. $id = Database::insert($this->tbl_course_agenda, $attributes);
  65. if ($id) {
  66. //api_item_property_update($this->course, TOOL_CALENDAR_EVENT, $id, "AgendaAdded", api_get_user_id(), '','',$start, $end);
  67. $group_id = api_get_group_id();
  68. if ((!is_null($users_to_send)) or (!empty($group_id))) {
  69. $send_to = self::separate_users_groups($users_to_send);
  70. if (isset($send_to['everyone']) && $send_to['everyone']) {
  71. api_item_property_update($this->course, TOOL_CALENDAR_EVENT, $id,"AgendaAdded", api_get_user_id(), '','',$start,$end);
  72. } else {
  73. // storing the selected groups
  74. if (is_array($send_to['groups'])) {
  75. foreach ($send_to['groups'] as $group) {
  76. api_item_property_update($this->course, TOOL_CALENDAR_EVENT, $id, "AgendaAdded", api_get_user_id(), $group,0,$start, $end);
  77. }
  78. }
  79. // storing the selected users
  80. if (is_array($send_to['users'])) {
  81. foreach ($send_to['users'] as $my_user_id) {
  82. api_item_property_update($this->course, TOOL_CALENDAR_EVENT, $id, "AgendaAdded", api_get_user_id(), 0, $my_user_id, $start,$end);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. break;
  89. case 'admin':
  90. $attributes['title'] = $title;
  91. $attributes['content'] = $content;
  92. $attributes['start_date'] = $start;
  93. $attributes['end_date'] = $end;
  94. $attributes['all_day'] = $all_day;
  95. $attributes['access_url_id']= api_get_current_access_url_id();
  96. $id = Database::insert($this->tbl_global_agenda, $attributes);
  97. break;
  98. }
  99. return $id;
  100. }
  101. function edit_event($id, $start, $end, $all_day, $view, $title, $content) {
  102. $start = date('Y-m-d H:i:s', $start);
  103. $start = api_get_utc_datetime($start);
  104. if ($all_day == '0') {
  105. $end = date('Y-m-d H:i:s', $end);
  106. $end = api_get_utc_datetime($end);
  107. }
  108. $all_day = isset($all_day) && $all_day == '1' ? 1:0;
  109. $attributes = array();
  110. switch($this->type) {
  111. case 'personal':
  112. $attributes['title'] = $title;
  113. $attributes['text'] = $content;
  114. $attributes['date'] = $start;
  115. $attributes['enddate'] = $end;
  116. Database::update($this->tbl_personal_agenda, $attributes, array('id = ?' => $id));
  117. break;
  118. case 'course':
  119. $attributes['title'] = $title;
  120. $attributes['content'] = $content;
  121. $attributes['start_date'] = $start;
  122. $attributes['end_date'] = $end;
  123. Database::update($this->tbl_course_agenda, $attributes, array('id = ?' => $id));
  124. break;
  125. case 'admin':
  126. $attributes['title'] = $title;
  127. $attributes['content'] = $content;
  128. $attributes['start_date'] = $start;
  129. $attributes['end_date'] = $end;
  130. Database::update($this->tbl_global_agenda, $attributes, array('id = ?' => $id));
  131. break;
  132. break;
  133. }
  134. }
  135. function delete_event($id) {
  136. switch($this->type) {
  137. case 'personal':
  138. Database::delete($this->tbl_personal_agenda, array('id = ?' =>$id));
  139. break;
  140. case 'course':
  141. Database::delete($this->tbl_course_agenda, array('id = ?' =>$id));
  142. break;
  143. case 'admin':
  144. Database::delete($this->tbl_global_agenda, array('id = ?' =>$id));
  145. break;
  146. }
  147. }
  148. /**
  149. *
  150. * Get agenda events
  151. * @param int start tms
  152. * @param int end tms
  153. * @param string agenda type (personal, admin or course)
  154. * @param int user id
  155. * @param int course id *integer* not the course code
  156. *
  157. */
  158. function get_events($start, $end, $user_id, $course_id = null, $group_id = null) {
  159. switch ($this->type) {
  160. case 'admin':
  161. $this->get_platform_events($start, $end);
  162. break;
  163. case 'course':
  164. $course_info = api_get_course_info_by_id($course_id);
  165. $this->get_course_events($start, $end, $course_info, $group_id);
  166. break;
  167. case 'personal':
  168. default:
  169. $this->get_personal_events($start, $end);
  170. $this->get_platform_events($start, $end);
  171. $my_course_list = array();
  172. if (!api_is_anonymous()) {
  173. $my_course_list = CourseManager::get_courses_list_by_user_id(api_get_user_id(), true);
  174. }
  175. if (!empty($my_course_list)) {
  176. foreach($my_course_list as $course_info_item) {
  177. if (isset($course_id) && !empty($course_id)) {
  178. if ($course_info_item['course_id'] == $course_id) {
  179. $this->get_course_events($start, $end, $course_info_item);
  180. }
  181. } else {
  182. $this->get_course_events($start, $end, $course_info_item);
  183. }
  184. }
  185. }
  186. break;
  187. }
  188. if (!empty($this->events)) {
  189. return json_encode($this->events);
  190. }
  191. return '';
  192. }
  193. function move_event($id, $day_delta, $minute_delta) {
  194. // we convert the hour delta into minutes and add the minute delta
  195. $delta = ($day_delta * 60 * 24) + $minute_delta;
  196. $delta = intval($delta);
  197. $event = $this->get_event($id);
  198. if (!empty($event)) {
  199. switch($this->type) {
  200. case 'personal':
  201. $sql = "UPDATE $this->tbl_personal_agenda SET date = DATE_ADD(date, INTERVAL $delta MINUTE), enddate = DATE_ADD(enddate, INTERVAL $delta MINUTE)
  202. WHERE id=".intval($id);
  203. $result = Database::query($sql);
  204. break;
  205. case 'course':
  206. $sql = "UPDATE $this->tbl_course_agenda SET start_date = DATE_ADD(start_date,INTERVAL $delta MINUTE), end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  207. WHERE c_id = ".$this->course['real_id']." AND id=".intval($id);
  208. $result = Database::query($sql);
  209. break;
  210. case 'admin':
  211. $sql = "UPDATE $this->tbl_global_agenda SET start_date = DATE_ADD(start_date,INTERVAL $delta MINUTE), end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  212. WHERE id=".intval($id);
  213. $result = Database::query($sql);
  214. break;
  215. }
  216. }
  217. return 1;
  218. }
  219. /**
  220. * Gets a single event
  221. * @param int event id
  222. */
  223. function get_event($id) {
  224. // make sure events of the personal agenda can only be seen by the user himself
  225. $id = intval($id);
  226. $event = null;
  227. switch ($this->type) {
  228. case 'personal':
  229. $sql = " SELECT * FROM ".$this->tbl_personal_agenda." WHERE id=".$id." AND user = ".api_get_user_id();
  230. $result = Database::query($sql);
  231. if (Database::num_rows($result)) {
  232. $event = Database::fetch_array($result, 'ASSOC');
  233. }
  234. break;
  235. case 'course':
  236. $sql = " SELECT * FROM ".$this->tbl_course_agenda." WHERE id=".$id;
  237. $result = Database::query($sql);
  238. if (Database::num_rows($result)) {
  239. $event = Database::fetch_array($result, 'ASSOC');
  240. }
  241. break;
  242. case 'admin':
  243. $sql = " SELECT * FROM ".$this->tbl_global_agenda." WHERE id=".$id;
  244. $result = Database::query($sql);
  245. if (Database::num_rows($result)) {
  246. $event = Database::fetch_array($result, 'ASSOC');
  247. }
  248. break;
  249. }
  250. return $event;
  251. }
  252. /**
  253. *
  254. * Gets personal events
  255. * @param int start date tms
  256. * @param int end date tms
  257. */
  258. function get_personal_events($start, $end) {
  259. $start = intval($start);
  260. $end = intval($end);
  261. $start = api_get_utc_datetime($start);
  262. $end = api_get_utc_datetime($end);
  263. $user_id = api_get_user_id();
  264. $sql = "SELECT * FROM ".$this->tbl_personal_agenda."
  265. WHERE date >= '".$start."' AND (enddate <='".$end."' OR enddate IS NULL) AND user = $user_id";
  266. $result = Database::query($sql);
  267. if (Database::num_rows($result)) {
  268. while ($row = Database::fetch_array($result, 'ASSOC')) {
  269. $event = array();
  270. $event['id'] = 'personal_'.$row['id'];
  271. $event['title'] = $row['title'];
  272. $event['className'] = 'personal';
  273. $event['borderColor'] = $event['backgroundColor'] = $this->event_personal_color;
  274. $event['editable'] = true;
  275. if (!empty($row['date']) && $row['date'] != '0000-00-00 00:00:00') {
  276. $event['start'] = $this->format_event_date($row['date']);
  277. }
  278. if (!empty($row['enddate']) && $row['enddate'] != '0000-00-00 00:00:00') {
  279. $event['end'] = $this->format_event_date($row['enddate']);
  280. }
  281. $event['description'] = $row['text'];
  282. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  283. $my_events[] = $event;
  284. $this->events[]= $event;
  285. }
  286. }
  287. return $my_events;
  288. }
  289. function get_course_events($start, $end, $course_info, $group_id = 0) {
  290. $course_id = $course_info['real_id'];
  291. $group_memberships = GroupManager::get_group_ids($course_id, api_get_user_id());
  292. $tlb_course_agenda = Database::get_course_table(TABLE_AGENDA);
  293. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  294. $user_id = api_get_user_id();
  295. if (!empty($group_id)) {
  296. $group_memberships = array($group_id);
  297. }
  298. if (is_array($group_memberships) && count($group_memberships) >0 ) {
  299. $sql = "SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  300. FROM ".$tlb_course_agenda." agenda, ".$tbl_property." ip
  301. WHERE agenda.id = ip.ref AND
  302. ip.tool ='".TOOL_CALENDAR_EVENT."' AND
  303. ( ip.to_user_id=$user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") ) AND
  304. ip.visibility = '1' AND
  305. agenda.c_id = $course_id AND
  306. ip.c_id = $course_id";
  307. } else {
  308. if (api_is_allowed_to_edit()) {
  309. $sql="SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  310. FROM ".$tlb_course_agenda." agenda, ".$tbl_property." ip
  311. WHERE agenda.id = ip.ref
  312. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  313. AND ip.visibility='1' AND
  314. agenda.c_id = $course_id AND
  315. ip.c_id = $course_id
  316. ";
  317. } else {
  318. $sql="SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  319. FROM ".$tlb_course_agenda." agenda, ".$tbl_property." ip
  320. WHERE agenda.id = ip.ref
  321. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  322. AND ( ip.to_user_id=$user_id OR ip.to_group_id='0')
  323. AND ip.visibility='1' AND
  324. agenda.c_id = $course_id AND
  325. ip.c_id = $course_id ";
  326. }
  327. }
  328. $result = Database::query($sql);
  329. $events = array();
  330. if (Database::num_rows($result)) {
  331. while ($row = Database::fetch_array($result, 'ASSOC')) {
  332. //Only show events from the session
  333. if (api_get_course_int_id()) {
  334. if ($row['session_id'] != api_get_session_id()) {
  335. continue;
  336. }
  337. }
  338. $event = array();
  339. $event['id'] = 'course_'.$row['id'];
  340. $event['title'] = $row['title'];
  341. $event['className'] = 'course';
  342. $event['allDay'] = 'false';
  343. // var_dump($row);
  344. $event['borderColor'] = $event['backgroundColor'] = $this->event_course_color;
  345. if (isset($row['session_id']) && !empty($row['session_id'])) {
  346. $event['borderColor'] = $event['backgroundColor'] = $this->event_session_color;
  347. }
  348. if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
  349. $event['borderColor'] = $event['backgroundColor'] = $this->event_group_color;
  350. }
  351. $event['editable'] = false;
  352. if (api_is_allowed_to_edit() && $this->type == 'course') {
  353. $event['editable'] = true;
  354. }
  355. if (!empty($row['start_date']) && $row['start_date'] != '0000-00-00 00:00:00') {
  356. $event['start'] = $this->format_event_date($row['start_date']);
  357. }
  358. if (!empty($row['end_date']) && $row['end_date'] != '0000-00-00 00:00:00') {
  359. $event['end'] = $this->format_event_date($row['end_date']);
  360. }
  361. $event['description'] = $row['content'];
  362. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  363. $my_events[] = $event;
  364. $this->events[] = $event;
  365. }
  366. }
  367. return $events;
  368. }
  369. function get_platform_events($start, $end) {
  370. $start = intval($start);
  371. $end = intval($end);
  372. $start = api_get_utc_datetime($start);
  373. $end = api_get_utc_datetime($end);
  374. $access_url_id = api_get_current_access_url_id();
  375. $sql = "SELECT * FROM ".$this->tbl_global_agenda."
  376. WHERE start_date >= '".$start."' AND end_date <= '".$end."' AND access_url_id = $access_url_id ";
  377. $result = Database::query($sql);
  378. $my_events = array();
  379. if (Database::num_rows($result)) {
  380. while ($row = Database::fetch_array($result, 'ASSOC')) {
  381. $event = array();
  382. $event['id'] = 'platform_'.$row['id'];
  383. $event['title'] = $row['title'];
  384. $event['className'] = 'platform';
  385. $event['allDay'] = 'false';
  386. $event['borderColor'] = $event['backgroundColor'] = $this->event_platform_color;
  387. $event['editable'] = false;
  388. if (api_is_platform_admin() && $this->type == 'admin') {
  389. $event['editable'] = true;
  390. }
  391. if (!empty($row['start_date']) && $row['start_date'] != '0000-00-00 00:00:00') {
  392. $event['start'] = $this->format_event_date($row['start_date']);
  393. }
  394. if (!empty($row['end_date']) && $row['end_date'] != '0000-00-00 00:00:00') {
  395. $event['end'] = $this->format_event_date($row['end_date']);
  396. }
  397. $event['description'] = $row['content'];
  398. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  399. $my_events[] = $event;
  400. $this->events[]= $event;
  401. }
  402. }
  403. return $my_events;
  404. }
  405. //Format needed for the Fullcalendar js lib
  406. function format_event_date($utc_time) {
  407. return date('c', api_strtotime(api_get_local_time($utc_time)));
  408. }
  409. /**
  410. * this function shows the form with the user that were not selected
  411. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  412. * @return html code
  413. */
  414. function construct_not_selected_select_form($group_list=null, $user_list=null,$to_already_selected=array()) {
  415. $html = '<select id="users_to_send_id" name="users_to_send[]" size="5" multiple="multiple" style="width:250px" class="chzn-select">';
  416. // adding the groups to the select form
  417. if (isset($to_already_selected) && $to_already_selected==='everyone') {
  418. $html .= '<option value="everyone" selected="selected">'.get_lang('Everyone').'</option>';
  419. }
  420. if (is_array($group_list)) {
  421. foreach($group_list as $this_group) {
  422. //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]);
  423. if (!is_array($to_already_selected) || !in_array("GROUP:".$this_group['id'],$to_already_selected)) // $to_already_selected is the array containing the groups (and users) that are already selected
  424. {
  425. $html .= "<option value=\"GROUP:".$this_group['id']."\">".
  426. "G: ".$this_group['name']." &ndash; " . $this_group['userNb'] . " " . get_lang('Users') .
  427. "</option>";
  428. }
  429. }
  430. // a divider
  431. }
  432. $html .= "<option value=\"\">--------------------------------------------</option>";
  433. // adding the individual users to the select form
  434. foreach($user_list as $this_user) {
  435. // $to_already_selected is the array containing the users (and groups) that are already selected
  436. if (!is_array($to_already_selected) || !in_array("USER:".$this_user['user_id'],$to_already_selected)) {
  437. $html .= "<option value=\"USER:".$this_user['user_id']."\">".api_get_person_name($this_user['firstname'], $this_user['lastname'])."</option>";
  438. }
  439. }
  440. $html .= "</select>";
  441. return $html;
  442. }
  443. /**
  444. * This function separates the users from the groups
  445. * users have a value USER:XXX (with XXX the dokeos id
  446. * groups have a value GROUP:YYY (with YYY the group id)
  447. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  448. * @return array
  449. */
  450. function separate_users_groups($to) {
  451. $grouplist = array();
  452. $userlist = array();
  453. $send_to = null;
  454. $send_to['everyone']= false;
  455. if (is_array($to) && count($to)>0) {
  456. foreach($to as $to_item) {
  457. if ($to_item == 'everyone') {
  458. $send_to['everyone']= true;
  459. }
  460. list($type, $id) = explode(':', $to_item);
  461. switch($type) {
  462. case 'GROUP':
  463. $grouplist[] =$id;
  464. break;
  465. case 'USER':
  466. $userlist[] =$id;
  467. break;
  468. }
  469. }
  470. $send_to['groups']=$grouplist;
  471. $send_to['users']=$userlist;
  472. }
  473. return $send_to;
  474. }
  475. }