agenda.lib.php 30 KB

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