agenda.lib.php 43 KB

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