agenda.lib.php 36 KB

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