agenda.lib.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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(), $add_as_announcement = false) {
  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. if (isset($add_as_announcement) && !empty($add_as_announcement)) {
  88. self::store_agenda_item_as_announcement($id);
  89. }
  90. }
  91. break;
  92. case 'admin':
  93. $attributes['title'] = $title;
  94. $attributes['content'] = $content;
  95. $attributes['start_date'] = $start;
  96. $attributes['end_date'] = $end;
  97. $attributes['all_day'] = $all_day;
  98. $attributes['access_url_id']= api_get_current_access_url_id();
  99. $id = Database::insert($this->tbl_global_agenda, $attributes);
  100. break;
  101. }
  102. return $id;
  103. }
  104. /* copycat of the agenda.inc.php @todo try to fix it */
  105. function store_agenda_item_as_announcement($item_id){
  106. $table_agenda = Database::get_course_table(TABLE_AGENDA);
  107. $table_ann = Database::get_course_table(TABLE_ANNOUNCEMENT);
  108. $course_id = api_get_course_int_id();
  109. //check params
  110. if(empty($item_id) or $item_id != strval(intval($item_id))) {return -1;}
  111. //get the agenda item
  112. $item_id = Database::escape_string($item_id);
  113. $sql = "SELECT * FROM $table_agenda WHERE c_id = $course_id AND id = ".$item_id;
  114. $res = Database::query($sql);
  115. if (Database::num_rows($res)>0) {
  116. $row = Database::fetch_array($res);
  117. //we have the agenda event, copy it
  118. //get the maximum value for display order in announcement table
  119. $sql_max = "SELECT MAX(display_order) FROM $table_ann WHERE c_id = $course_id ";
  120. $res_max = Database::query($sql_max);
  121. $row_max = Database::fetch_array($res_max);
  122. $max = intval($row_max[0])+1;
  123. //build the announcement text
  124. $content = $row['content'];
  125. //insert announcement
  126. $session_id = api_get_session_id();
  127. $sql_ins = "INSERT INTO $table_ann (c_id, title,content,end_date,display_order,session_id) " .
  128. "VALUES ($course_id, '".Database::escape_string($row['title'])."','".Database::escape_string($content)."','".Database::escape_string($row['end_date'])."','$max','$session_id')";
  129. $res_ins = Database::query($sql_ins);
  130. if ($res > 0) {
  131. $ann_id = Database::insert_id();
  132. //Now also get the list of item_properties rows for this agenda_item (calendar_event)
  133. //and copy them into announcement item_properties
  134. $table_props = Database::get_course_table(TABLE_ITEM_PROPERTY);
  135. $sql_props = "SELECT * FROM $table_props WHERE c_id = $course_id AND tool ='calendar_event' AND ref='$item_id'";
  136. $res_props = Database::query($sql_props);
  137. if(Database::num_rows($res_props)>0) {
  138. while($row_props = Database::fetch_array($res_props)) {
  139. //insert into announcement item_property
  140. $time = api_get_utc_datetime();
  141. $sql_ins_props = "INSERT INTO $table_props " .
  142. "(c_id, tool, insert_user_id, insert_date, " .
  143. "lastedit_date, ref, lastedit_type," .
  144. "lastedit_user_id, to_group_id, to_user_id, " .
  145. "visibility, start_visible, end_visible)" .
  146. " VALUES " .
  147. "($course_id, 'announcement','".$row_props['insert_user_id']."','".$time."'," .
  148. "'$time','$ann_id','AnnouncementAdded'," .
  149. "'".$row_props['last_edit_user_id']."','".$row_props['to_group_id']."','".$row_props['to_user_id']."'," .
  150. "'".$row_props['visibility']."','".$row_props['start_visible']."','".$row_props['end_visible']."')";
  151. $res_ins_props = Database::query($sql_ins_props);
  152. if($res_ins_props <= 0){
  153. return -1;
  154. } else {
  155. //copy was a success
  156. return $ann_id;
  157. }
  158. }
  159. }
  160. } else {
  161. return -1;
  162. }
  163. }
  164. return -1;
  165. }
  166. function edit_event($id, $start, $end, $all_day, $view, $title, $content) {
  167. $start = date('Y-m-d H:i:s', $start);
  168. $start = api_get_utc_datetime($start);
  169. if ($all_day == '0') {
  170. $end = date('Y-m-d H:i:s', $end);
  171. $end = api_get_utc_datetime($end);
  172. }
  173. $all_day = isset($all_day) && $all_day == '1' ? 1:0;
  174. $attributes = array();
  175. switch($this->type) {
  176. case 'personal':
  177. $attributes['title'] = $title;
  178. $attributes['text'] = $content;
  179. $attributes['date'] = $start;
  180. $attributes['enddate'] = $end;
  181. Database::update($this->tbl_personal_agenda, $attributes, array('id = ?' => $id));
  182. break;
  183. case 'course':
  184. $attributes['title'] = $title;
  185. $attributes['content'] = $content;
  186. $attributes['start_date'] = $start;
  187. $attributes['end_date'] = $end;
  188. Database::update($this->tbl_course_agenda, $attributes, array('id = ?' => $id));
  189. break;
  190. case 'admin':
  191. $attributes['title'] = $title;
  192. $attributes['content'] = $content;
  193. $attributes['start_date'] = $start;
  194. $attributes['end_date'] = $end;
  195. Database::update($this->tbl_global_agenda, $attributes, array('id = ?' => $id));
  196. break;
  197. break;
  198. }
  199. }
  200. function delete_event($id) {
  201. switch($this->type) {
  202. case 'personal':
  203. Database::delete($this->tbl_personal_agenda, array('id = ?' =>$id));
  204. break;
  205. case 'course':
  206. Database::delete($this->tbl_course_agenda, array('id = ?' =>$id));
  207. break;
  208. case 'admin':
  209. Database::delete($this->tbl_global_agenda, array('id = ?' =>$id));
  210. break;
  211. }
  212. }
  213. /**
  214. *
  215. * Get agenda events
  216. * @param int start tms
  217. * @param int end tms
  218. * @param string agenda type (personal, admin or course)
  219. * @param int user id
  220. * @param int course id *integer* not the course code
  221. *
  222. */
  223. function get_events($start, $end, $user_id, $course_id = null, $group_id = null) {
  224. switch ($this->type) {
  225. case 'admin':
  226. $this->get_platform_events($start, $end);
  227. break;
  228. case 'course':
  229. $course_info = api_get_course_info_by_id($course_id);
  230. $this->get_course_events($start, $end, $course_info, $group_id);
  231. break;
  232. case 'personal':
  233. default:
  234. //Getting personal events
  235. $this->get_personal_events($start, $end);
  236. //Getting platform/admin events
  237. $this->get_platform_events($start, $end);
  238. //Getting course events
  239. $my_course_list = array();
  240. if (!api_is_anonymous()) {
  241. $my_course_list = CourseManager::get_courses_list_by_user_id(api_get_user_id(), true);
  242. }
  243. if (!empty($my_course_list)) {
  244. foreach($my_course_list as $course_info_item) {
  245. if (isset($course_id) && !empty($course_id)) {
  246. if ($course_info_item['course_id'] == $course_id) {
  247. $this->get_course_events($start, $end, $course_info_item);
  248. }
  249. } else {
  250. $this->get_course_events($start, $end, $course_info_item);
  251. }
  252. }
  253. }
  254. break;
  255. }
  256. if (!empty($this->events)) {
  257. return json_encode($this->events);
  258. }
  259. return '';
  260. }
  261. function move_event($id, $day_delta, $minute_delta) {
  262. // we convert the hour delta into minutes and add the minute delta
  263. $delta = ($day_delta * 60 * 24) + $minute_delta;
  264. $delta = intval($delta);
  265. $event = $this->get_event($id);
  266. $all_day = 0;
  267. if ($day_delta == 0 && $minute_delta == 0) {
  268. $all_day = 1;
  269. }
  270. if (!empty($event)) {
  271. switch($this->type) {
  272. case 'personal':
  273. $sql = "UPDATE $this->tbl_personal_agenda SET all_day = $all_day, date = DATE_ADD(date, INTERVAL $delta MINUTE), enddate = DATE_ADD(enddate, INTERVAL $delta MINUTE)
  274. WHERE id=".intval($id);
  275. $result = Database::query($sql);
  276. break;
  277. case 'course':
  278. $sql = "UPDATE $this->tbl_course_agenda SET all_day = $all_day, start_date = DATE_ADD(start_date,INTERVAL $delta MINUTE), end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  279. WHERE c_id = ".$this->course['real_id']." AND id=".intval($id);
  280. $result = Database::query($sql);
  281. break;
  282. case 'admin':
  283. $sql = "UPDATE $this->tbl_global_agenda SET all_day = $all_day, start_date = DATE_ADD(start_date,INTERVAL $delta MINUTE), end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  284. WHERE id=".intval($id);
  285. $result = Database::query($sql);
  286. break;
  287. }
  288. }
  289. return 1;
  290. }
  291. /**
  292. * Gets a single event
  293. * @param int event id
  294. */
  295. function get_event($id) {
  296. // make sure events of the personal agenda can only be seen by the user himself
  297. $id = intval($id);
  298. $event = null;
  299. switch ($this->type) {
  300. case 'personal':
  301. $sql = " SELECT * FROM ".$this->tbl_personal_agenda." WHERE id = $id AND user = ".api_get_user_id();
  302. $result = Database::query($sql);
  303. if (Database::num_rows($result)) {
  304. $event = Database::fetch_array($result, 'ASSOC');
  305. }
  306. break;
  307. case 'course':
  308. $sql = " SELECT * FROM ".$this->tbl_course_agenda." WHERE c_id = ".$this->course['real_id']." AND id = ".$id;
  309. $result = Database::query($sql);
  310. if (Database::num_rows($result)) {
  311. $event = Database::fetch_array($result, 'ASSOC');
  312. }
  313. break;
  314. case 'admin':
  315. $sql = " SELECT * FROM ".$this->tbl_global_agenda." WHERE id=".$id;
  316. $result = Database::query($sql);
  317. if (Database::num_rows($result)) {
  318. $event = Database::fetch_array($result, 'ASSOC');
  319. }
  320. break;
  321. }
  322. return $event;
  323. }
  324. /**
  325. *
  326. * Gets personal events
  327. * @param int start date tms
  328. * @param int end date tms
  329. */
  330. function get_personal_events($start, $end) {
  331. $start = intval($start);
  332. $end = intval($end);
  333. $start = api_get_utc_datetime($start);
  334. $end = api_get_utc_datetime($end);
  335. $user_id = api_get_user_id();
  336. $sql = "SELECT * FROM ".$this->tbl_personal_agenda."
  337. WHERE date >= '".$start."' AND (enddate <='".$end."' OR enddate IS NULL) AND user = $user_id";
  338. $result = Database::query($sql);
  339. if (Database::num_rows($result)) {
  340. while ($row = Database::fetch_array($result, 'ASSOC')) {
  341. $event = array();
  342. $event['id'] = 'personal_'.$row['id'];
  343. $event['title'] = $row['title'];
  344. $event['className'] = 'personal';
  345. $event['borderColor'] = $event['backgroundColor'] = $this->event_personal_color;
  346. $event['editable'] = true;
  347. $event['sent_to'] = get_lang('Me');
  348. $event['type'] = 'personal';
  349. if (!empty($row['date']) && $row['date'] != '0000-00-00 00:00:00') {
  350. $event['start'] = $this->format_event_date($row['date']);
  351. }
  352. if (!empty($row['enddate']) && $row['enddate'] != '0000-00-00 00:00:00') {
  353. $event['end'] = $this->format_event_date($row['enddate']);
  354. }
  355. $event['description'] = $row['text'];
  356. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  357. $my_events[] = $event;
  358. $this->events[]= $event;
  359. }
  360. }
  361. return $my_events;
  362. }
  363. function get_course_events($start, $end, $course_info, $group_id = 0) {
  364. $course_id = $course_info['real_id'];
  365. $group_list = GroupManager::get_group_list(null, $course_info['code']);
  366. $group_name_list = array();
  367. if (!empty($group_list)) {
  368. foreach($group_list as $group) {
  369. $group_name_list[$group['id']]= $group['name'];
  370. }
  371. }
  372. if (!api_is_allowed_to_edit()) {
  373. $group_memberships = GroupManager::get_group_ids($course_id, api_get_user_id());
  374. } else {
  375. $group_memberships = array_keys($group_name_list);
  376. }
  377. $tlb_course_agenda = Database::get_course_table(TABLE_AGENDA);
  378. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  379. $user_id = api_get_user_id();
  380. if (!empty($group_id)) {
  381. $group_memberships = array($group_id);
  382. }
  383. if (is_array($group_memberships) && count($group_memberships) >0) {
  384. if (api_is_allowed_to_edit()) {
  385. $where_condition = "( ip.to_group_id is null OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") ) ";
  386. } else {
  387. $where_condition = "( ip.to_user_id = $user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") ) ";
  388. }
  389. $sql = "SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref, to_user_id
  390. FROM ".$tlb_course_agenda." agenda, ".$tbl_property." ip
  391. WHERE agenda.id = ip.ref AND
  392. ip.tool ='".TOOL_CALENDAR_EVENT."' AND
  393. $where_condition AND
  394. ip.visibility = '1' AND
  395. agenda.c_id = $course_id AND
  396. ip.c_id = $course_id
  397. GROUP BY id";
  398. } else {
  399. if (api_is_allowed_to_edit()) {
  400. $where_condition = "";
  401. } else {
  402. $where_condition = "( ip.to_user_id=$user_id OR ip.to_group_id='0') AND ";
  403. }
  404. $sql = "SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref, to_user_id
  405. FROM ".$tlb_course_agenda." agenda, ".$tbl_property." ip
  406. WHERE agenda.id = ip.ref AND
  407. ip.tool='".TOOL_CALENDAR_EVENT."' AND
  408. $where_condition
  409. ip.visibility='1' AND
  410. agenda.c_id = $course_id AND
  411. ip.c_id = $course_id";
  412. }
  413. $result = Database::query($sql);
  414. $events = array();
  415. if (Database::num_rows($result)) {
  416. $events_added = array();
  417. while ($row = Database::fetch_array($result, 'ASSOC')) {
  418. //to gather sent_tos
  419. $sql = "SELECT to_user_id, to_group_id
  420. FROM ".$tbl_property." ip
  421. WHERE ip.tool = '".TOOL_CALENDAR_EVENT."' AND
  422. ref = {$row['ref']} AND
  423. ip.visibility = '1' AND
  424. ip.c_id = $course_id";
  425. $sent_to_result = Database::query($sql);
  426. $user_to_array = array();
  427. $group_to_array = array();
  428. while ($row_send_to = Database::fetch_array($sent_to_result, 'ASSOC')) {
  429. if (!empty($row_send_to['to_group_id'])) {
  430. $group_to_array[] = $row_send_to['to_group_id'];
  431. }
  432. if (!empty($row_send_to['to_user_id'])) {
  433. $user_to_array[] = $row_send_to['to_user_id'];
  434. }
  435. }
  436. //Only show events from the session
  437. if (api_get_course_int_id()) {
  438. if ($row['session_id'] != api_get_session_id()) {
  439. continue;
  440. }
  441. }
  442. $event = array();
  443. $event['id'] = 'course_'.$row['id'];
  444. //To avoid doubles
  445. if (in_array($row['id'], $events_added)) {
  446. continue;
  447. }
  448. $events_added[] = $row['id'];
  449. $attachment = get_attachment($row['id'], $course_id);
  450. $has_attachment = '';
  451. if (!empty($attachment)) {
  452. $has_attachment = Display::return_icon('attachment.gif',get_lang('Attachment'));
  453. $user_filename = $attachment['filename'];
  454. $full_file_name = 'download.php?file='.$attachment['path'].'&course_id='.$course_id;
  455. $event['attachment'] = $has_attachment.Display::url($user_filename, $full_file_name);
  456. } else {
  457. $event['attachment'] = '';
  458. }
  459. $event['title'] = $row['title'];
  460. $event['className'] = 'course';
  461. $event['allDay'] = 'false';
  462. $event['borderColor'] = $event['backgroundColor'] = $this->event_course_color;
  463. if (isset($row['session_id']) && !empty($row['session_id'])) {
  464. $event['borderColor'] = $event['backgroundColor'] = $this->event_session_color;
  465. }
  466. if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
  467. $event['borderColor'] = $event['backgroundColor'] = $this->event_group_color;
  468. }
  469. $event['editable'] = false;
  470. if (api_is_allowed_to_edit() && $this->type == 'course') {
  471. $event['editable'] = true;
  472. }
  473. if (!empty($row['start_date']) && $row['start_date'] != '0000-00-00 00:00:00') {
  474. $event['start'] = $this->format_event_date($row['start_date']);
  475. }
  476. if (!empty($row['end_date']) && $row['end_date'] != '0000-00-00 00:00:00') {
  477. $event['end'] = $this->format_event_date($row['end_date']);
  478. }
  479. $event['sent_to'] = '';
  480. $event['type'] = $this->type;
  481. //Event Sent to a group?
  482. if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
  483. if (!empty($group_to_array)) {
  484. $sent_to = array();
  485. foreach($group_to_array as $group_item) {
  486. $sent_to[] = $group_name_list[$group_item];
  487. }
  488. }
  489. $sent_to = implode('@@', $sent_to);
  490. $sent_to = str_replace('@@', '</div><div class="label_tag notice">', $sent_to);
  491. $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
  492. $event['type'] = 'group';
  493. }
  494. //Event sent to a user?
  495. //var_dump($row);
  496. if (isset($row['to_user_id'])) {
  497. if (!empty($user_to_array)) {
  498. $sent_to = array();
  499. foreach($user_to_array as $item) {
  500. $user_info = api_get_user_info($item);
  501. // add username as tooltip for $event['sent_to'] - ref #4226
  502. $username = api_htmlentities(sprintf(get_lang('LoginX'), $user_info['username']), ENT_QUOTES);
  503. $sent_to[] = "<span title='".$username."'>".$user_info['complete_name']."</span>";
  504. }
  505. }
  506. $sent_to = implode('@@', $sent_to);
  507. $sent_to = str_replace('@@', '</div><div class="label_tag notice">', $sent_to);
  508. $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
  509. }
  510. //Event sent to everyone!
  511. if (empty($event['sent_to'])) {
  512. $event['sent_to'] = '<div class="label_tag notice">'.get_lang('Everyone').'</div>';
  513. }
  514. $event['description'] = $row['content'];
  515. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  516. $this->events[] = $event;
  517. }
  518. }
  519. return $events;
  520. }
  521. function get_platform_events($start, $end) {
  522. $start = intval($start);
  523. $end = intval($end);
  524. $start = api_get_utc_datetime($start);
  525. $end = api_get_utc_datetime($end);
  526. $access_url_id = api_get_current_access_url_id();
  527. $sql = "SELECT * FROM ".$this->tbl_global_agenda."
  528. WHERE start_date >= '".$start."' AND end_date <= '".$end."' AND access_url_id = $access_url_id ";
  529. $result = Database::query($sql);
  530. $my_events = array();
  531. if (Database::num_rows($result)) {
  532. while ($row = Database::fetch_array($result, 'ASSOC')) {
  533. $event = array();
  534. $event['id'] = 'platform_'.$row['id'];
  535. $event['title'] = $row['title'];
  536. $event['className'] = 'platform';
  537. $event['allDay'] = 'false';
  538. $event['borderColor'] = $event['backgroundColor'] = $this->event_platform_color;
  539. $event['editable'] = false;
  540. $event['type'] = 'admin';
  541. if (api_is_platform_admin() && $this->type == 'admin') {
  542. $event['editable'] = true;
  543. }
  544. if (!empty($row['start_date']) && $row['start_date'] != '0000-00-00 00:00:00') {
  545. $event['start'] = $this->format_event_date($row['start_date']);
  546. }
  547. if (!empty($row['end_date']) && $row['end_date'] != '0000-00-00 00:00:00') {
  548. $event['end'] = $this->format_event_date($row['end_date']);
  549. }
  550. $event['description'] = $row['content'];
  551. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  552. $my_events[] = $event;
  553. $this->events[]= $event;
  554. }
  555. }
  556. return $my_events;
  557. }
  558. //Format needed for the Fullcalendar js lib
  559. function format_event_date($utc_time) {
  560. return date('c', api_strtotime(api_get_local_time($utc_time)));
  561. }
  562. /**
  563. * this function shows the form with the user that were not selected
  564. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  565. * @return html code
  566. */
  567. function construct_not_selected_select_form($group_list=null, $user_list=null,$to_already_selected=array()) {
  568. $html = '<select id="users_to_send_id" data-placeholder="'.get_lang('Select').'" name="users_to_send[]" multiple="multiple" style="width:250px" class="chzn-select">';
  569. // adding the groups to the select form
  570. if (isset($to_already_selected) && $to_already_selected==='everyone') {
  571. }
  572. $html .= '<option value="everyone">'.get_lang('Everyone').'</option>';
  573. if (is_array($group_list)) {
  574. $html .= '<optgroup label="'.get_lang('Groups').'">';
  575. foreach($group_list as $this_group) {
  576. //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]);
  577. if (!is_array($to_already_selected) || !in_array("GROUP:".$this_group['id'],$to_already_selected)) {
  578. // $to_already_selected is the array containing the groups (and users) that are already selected
  579. $html .= "<option value=\"GROUP:".$this_group['id']."\">".
  580. $this_group['name']." &ndash; " . $this_group['userNb'] . " " . get_lang('Users') .
  581. "</option>";
  582. }
  583. }
  584. $html .= '</optgroup>';
  585. }
  586. // adding the individual users to the select form
  587. if (is_array($group_list)) {
  588. $html .= '<optgroup label="'.get_lang('Users').'">';
  589. }
  590. foreach ($user_list as $this_user) {
  591. // $to_already_selected is the array containing the users (and groups) that are already selected
  592. if (!is_array($to_already_selected) || !in_array("USER:".$this_user['user_id'],$to_already_selected)) {
  593. $username = api_htmlentities(sprintf(get_lang('LoginX'), $this_user['username']), ENT_QUOTES);
  594. // @todo : add title attribute $username in the jqdialog window. wait for a chosen version to inherit title attribute
  595. // from <option> to <li>
  596. $html .= '<option title="'.$username.'" value="USER:'.$this_user['user_id'].'">'.api_get_person_name($this_user['firstname'], $this_user['lastname']).' ('.$this_user['username'].') </option>';
  597. }
  598. }
  599. if (is_array($group_list)) {
  600. $html .= '</optgroup>';
  601. $html .= "</select>";
  602. }
  603. return $html;
  604. }
  605. /**
  606. * This function separates the users from the groups
  607. * users have a value USER:XXX (with XXX the dokeos id
  608. * groups have a value GROUP:YYY (with YYY the group id)
  609. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  610. * @return array
  611. */
  612. function separate_users_groups($to) {
  613. $grouplist = array();
  614. $userlist = array();
  615. $send_to = null;
  616. $send_to['everyone']= false;
  617. if (is_array($to) && count($to)>0) {
  618. foreach($to as $to_item) {
  619. if ($to_item == 'everyone') {
  620. $send_to['everyone']= true;
  621. }
  622. list($type, $id) = explode(':', $to_item);
  623. switch($type) {
  624. case 'GROUP':
  625. $grouplist[] =$id;
  626. break;
  627. case 'USER':
  628. $userlist[] =$id;
  629. break;
  630. }
  631. }
  632. $send_to['groups']=$grouplist;
  633. $send_to['users']=$userlist;
  634. }
  635. return $send_to;
  636. }
  637. }