agenda.lib.php 32 KB

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