agenda.lib.php 38 KB

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