agenda.lib.php 31 KB

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