agenda.lib.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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. /**
  12. * personal, admin or course
  13. * @var string
  14. */
  15. public $type = 'personal';
  16. /**
  17. *
  18. */
  19. public function __construct()
  20. {
  21. // Table definitions
  22. $this->tbl_global_agenda = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  23. $this->tbl_personal_agenda = Database::get_main_table(TABLE_PERSONAL_AGENDA);
  24. $this->tbl_course_agenda = Database::get_course_table(TABLE_AGENDA);
  25. // Setting the course object if we are in a course
  26. $this->course = null;
  27. $course_info = api_get_course_info();
  28. if (!empty($course_info)) {
  29. $this->course = $course_info;
  30. }
  31. $this->events = array();
  32. //Event colors
  33. $this->event_platform_color = 'red'; //red
  34. $this->event_course_color = '#458B00'; //green
  35. $this->event_group_color = '#A0522D'; //siena
  36. $this->event_session_color = '#00496D'; // kind of green
  37. $this->event_personal_color = 'steel blue'; //steel blue
  38. }
  39. /**
  40. * Agenda type: personal, admin or course
  41. * @param string $type
  42. */
  43. public function setType($type)
  44. {
  45. $this->type = $type;
  46. }
  47. /**
  48. * Sets course info
  49. * @param array $course_info
  50. */
  51. public function set_course($course_info)
  52. {
  53. $this->course = $course_info;
  54. }
  55. /**
  56. *
  57. * Adds an event to the calendar
  58. *
  59. * @param string start datetime format: 2012-06-14 09:00:00
  60. * @param string end datetime format: 2012-06-14 09:00:00
  61. * @param string all day (true, false)
  62. * @param string view agendaDay, agendaWeek, month @todo seems not to be used
  63. * @param string title
  64. * @param string content
  65. * @param array users to send array('everyone') or a list of user ids
  66. * @param bool add event as a *course* announcement
  67. * @param int $parentId
  68. * @param array $attachmentSettings
  69. * @param array $repeatSettings
  70. * @return bool
  71. *
  72. */
  73. public function add_event(
  74. $start,
  75. $end,
  76. $all_day,
  77. $view,
  78. $title,
  79. $content,
  80. $users_to_send = array(),
  81. $add_as_announcement = false,
  82. $parentId = null,
  83. $attachmentSettings = array(),
  84. $repeatSettings = array()
  85. ) {
  86. $start = api_get_utc_datetime($start);
  87. $end = api_get_utc_datetime($end);
  88. $all_day = isset($all_day) && $all_day == 'true' ? 1 : 0;
  89. $attributes = array();
  90. $id = null;
  91. switch ($this->type) {
  92. case 'personal':
  93. $attributes['user'] = api_get_user_id();
  94. $attributes['title'] = $title;
  95. $attributes['text'] = $content;
  96. $attributes['date'] = $start;
  97. $attributes['enddate'] = $end;
  98. $attributes['all_day'] = $all_day;
  99. $id = Database::insert($this->tbl_personal_agenda, $attributes);
  100. break;
  101. case 'course':
  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['session_id'] = api_get_session_id();
  108. $attributes['c_id'] = $this->course['real_id'];
  109. $attributes['parent_event_id'] = $parentId;
  110. // Simple course event
  111. $id = Database::insert($this->tbl_course_agenda, $attributes);
  112. if ($id) {
  113. if (!empty($attachmentSettings)) {
  114. self::addAttachment($id, $attachmentSettings);
  115. }
  116. if (!empty($repeatSettings)) {
  117. $this->addRepeatItem(
  118. $this->course,
  119. $id,
  120. $repeatSettings['repeat_type'],
  121. $repeatSettings['repeat_end'],
  122. $users_to_send
  123. );
  124. }
  125. $group_id = api_get_group_id();
  126. if ((!is_null($users_to_send)) or (!empty($group_id))) {
  127. $send_to = self::separate_users_groups($users_to_send);
  128. if (isset($send_to['everyone']) && $send_to['everyone']) {
  129. api_item_property_update(
  130. $this->course,
  131. TOOL_CALENDAR_EVENT,
  132. $id,
  133. "AgendaAdded",
  134. api_get_user_id(),
  135. $group_id,
  136. '',
  137. $start,
  138. $end
  139. );
  140. } else {
  141. // storing the selected groups
  142. if (isset($send_to['groups']) && is_array($send_to['groups'])) {
  143. foreach ($send_to['groups'] as $group) {
  144. api_item_property_update(
  145. $this->course,
  146. TOOL_CALENDAR_EVENT,
  147. $id,
  148. "AgendaAdded",
  149. api_get_user_id(),
  150. $group,
  151. 0,
  152. $start,
  153. $end
  154. );
  155. }
  156. }
  157. // storing the selected users
  158. if (isset($send_to['groups']) && is_array($send_to['users'])) {
  159. foreach ($send_to['users'] as $to_user_id) {
  160. api_item_property_update(
  161. $this->course,
  162. TOOL_CALENDAR_EVENT,
  163. $id,
  164. "AgendaAdded",
  165. api_get_user_id(),
  166. $group_id,
  167. $to_user_id,
  168. $start,
  169. $end
  170. );
  171. }
  172. }
  173. }
  174. }
  175. if (isset($add_as_announcement) && !empty($add_as_announcement)) {
  176. self::store_agenda_item_as_announcement($id, $users_to_send);
  177. }
  178. }
  179. break;
  180. case 'admin':
  181. if (api_is_platform_admin()) {
  182. $attributes['title'] = $title;
  183. $attributes['content'] = $content;
  184. $attributes['start_date'] = $start;
  185. $attributes['end_date'] = $end;
  186. $attributes['all_day'] = $all_day;
  187. $attributes['access_url_id'] = api_get_current_access_url_id();
  188. $id = Database::insert($this->tbl_global_agenda, $attributes);
  189. }
  190. break;
  191. }
  192. return $id;
  193. }
  194. /**
  195. * @param int id
  196. * @param array sent to
  197. **/
  198. public function store_agenda_item_as_announcement($item_id, $sent_to = array())
  199. {
  200. $table_agenda = Database::get_course_table(TABLE_AGENDA);
  201. $course_id = api_get_course_int_id();
  202. //check params
  203. if (empty($item_id) or $item_id != strval(intval($item_id))) {
  204. return -1;
  205. }
  206. //get the agenda item
  207. $item_id = Database::escape_string($item_id);
  208. $sql = "SELECT * FROM $table_agenda WHERE c_id = $course_id AND id = ".$item_id;
  209. $res = Database::query($sql);
  210. if (Database::num_rows($res) > 0) {
  211. $row = Database::fetch_array($res, 'ASSOC');
  212. //Sending announcement
  213. if (!empty($sent_to)) {
  214. $id = AnnouncementManager::add_announcement(
  215. $row['title'],
  216. $row['content'],
  217. $sent_to,
  218. null,
  219. null,
  220. $row['end_date']
  221. );
  222. AnnouncementManager::send_email($id);
  223. }
  224. return $id;
  225. }
  226. return -1;
  227. }
  228. /**
  229. * Edits an event
  230. *
  231. * @param int event id
  232. * @param string start datetime format: 2012-06-14 09:00:00
  233. * @param string end datetime format: 2012-06-14 09:00:00
  234. * @param int event is all day? 1|0
  235. * @param string view
  236. * @param string event title
  237. * @param string event content
  238. */
  239. public function edit_event($id, $start, $end, $all_day, $view, $title, $content)
  240. {
  241. $start = api_get_utc_datetime($start);
  242. if ($all_day == '0') {
  243. $end = api_get_utc_datetime($end);
  244. }
  245. $all_day = isset($all_day) && $all_day == '1' ? 1 : 0;
  246. $attributes = array();
  247. switch ($this->type) {
  248. case 'personal':
  249. $eventInfo = $this->get_event($id);
  250. if ($eventInfo['user'] != api_get_user_id()) {
  251. break;
  252. }
  253. $attributes['title'] = $title;
  254. $attributes['text'] = $content;
  255. $attributes['date'] = $start;
  256. $attributes['enddate'] = $end;
  257. Database::update($this->tbl_personal_agenda, $attributes, array('id = ?' => $id));
  258. break;
  259. case 'course':
  260. $course_id = api_get_course_int_id();
  261. if (!empty($course_id) && api_is_allowed_to_edit(null, true)) {
  262. $attributes['title'] = $title;
  263. $attributes['content'] = $content;
  264. $attributes['start_date'] = $start;
  265. $attributes['end_date'] = $end;
  266. $attributes['all_day'] = $all_day;
  267. Database::update(
  268. $this->tbl_course_agenda,
  269. $attributes,
  270. array('id = ? AND c_id = ?' => array($id, $course_id))
  271. );
  272. }
  273. break;
  274. case 'admin':
  275. if (api_is_platform_admin()) {
  276. $attributes['title'] = $title;
  277. $attributes['content'] = $content;
  278. $attributes['start_date'] = $start;
  279. $attributes['end_date'] = $end;
  280. Database::update($this->tbl_global_agenda, $attributes, array('id = ?' => $id));
  281. }
  282. break;
  283. }
  284. }
  285. /**
  286. * @param int $id
  287. */
  288. public function delete_event($id)
  289. {
  290. switch ($this->type) {
  291. case 'personal':
  292. $eventInfo = $this->get_event($id);
  293. if ($eventInfo['user'] == api_get_user_id()) {
  294. Database::delete($this->tbl_personal_agenda, array('id = ?' => $id));
  295. }
  296. break;
  297. case 'course':
  298. $course_id = api_get_course_int_id();
  299. if (!empty($course_id) && api_is_allowed_to_edit(null, true)) {
  300. Database::delete($this->tbl_course_agenda, array('id = ? AND c_id = ?' => array($id, $course_id)));
  301. }
  302. break;
  303. case 'admin':
  304. if (api_is_platform_admin()) {
  305. Database::delete($this->tbl_global_agenda, array('id = ?' => $id));
  306. }
  307. break;
  308. }
  309. }
  310. /**
  311. *
  312. * Get agenda events
  313. * @param int start tms
  314. * @param int end tms
  315. * @param int course id *integer* not the course code
  316. * @param int user id
  317. *
  318. */
  319. public function get_events($start, $end, $course_id = null, $group_id = null, $user_id = 0)
  320. {
  321. switch ($this->type) {
  322. case 'admin':
  323. $this->get_platform_events($start, $end);
  324. break;
  325. case 'course':
  326. $session_id = api_get_session_id();
  327. $course_info = api_get_course_info_by_id($course_id);
  328. $this->get_course_events($start, $end, $course_info, $group_id, $session_id, $user_id);
  329. break;
  330. case 'personal':
  331. default:
  332. //Getting personal events
  333. $this->get_personal_events($start, $end);
  334. //Getting platform/admin events
  335. $this->get_platform_events($start, $end);
  336. //Getting course events
  337. $my_course_list = array();
  338. if (!api_is_anonymous()) {
  339. $session_list = SessionManager::get_sessions_by_user(api_get_user_id());
  340. $my_course_list = CourseManager::get_courses_list_by_user_id(api_get_user_id(), true);
  341. }
  342. if (!empty($session_list)) {
  343. foreach ($session_list as $session_item) {
  344. $my_courses = $session_item['courses'];
  345. $my_session_id = $session_item['session_id'];
  346. if (!empty($my_courses)) {
  347. foreach ($my_courses as $course_item) {
  348. $course_info = api_get_course_info_by_id($course_item['id']);
  349. $this->get_course_events($start, $end, $course_info, 0, $my_session_id);
  350. }
  351. }
  352. }
  353. }
  354. if (!empty($my_course_list)) {
  355. foreach ($my_course_list as $course_info_item) {
  356. if (isset($course_id) && !empty($course_id)) {
  357. if ($course_info_item['real_id'] == $course_id) {
  358. $this->get_course_events($start, $end, $course_info_item);
  359. }
  360. } else {
  361. $this->get_course_events($start, $end, $course_info_item);
  362. }
  363. }
  364. }
  365. break;
  366. }
  367. if (!empty($this->events)) {
  368. return json_encode($this->events);
  369. }
  370. return '';
  371. }
  372. /**
  373. * @param int $id
  374. * @param int $day_delta
  375. * @param int $minute_delta
  376. * @return int
  377. */
  378. public function resize_event($id, $day_delta, $minute_delta)
  379. {
  380. // we convert the hour delta into minutes and add the minute delta
  381. $delta = ($day_delta * 60 * 24) + $minute_delta;
  382. $delta = intval($delta);
  383. $event = $this->get_event($id);
  384. if (!empty($event)) {
  385. switch ($this->type) {
  386. case 'personal':
  387. $sql = "UPDATE $this->tbl_personal_agenda SET all_day = 0, enddate = DATE_ADD(enddate, INTERVAL $delta MINUTE)
  388. WHERE id=".intval($id);
  389. $result = Database::query($sql);
  390. break;
  391. case 'course':
  392. $sql = "UPDATE $this->tbl_course_agenda SET all_day = 0, end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  393. WHERE c_id = ".$this->course['real_id']." AND id=".intval($id);
  394. $result = Database::query($sql);
  395. break;
  396. case 'admin':
  397. $sql = "UPDATE $this->tbl_global_agenda SET all_day = 0, end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  398. WHERE id=".intval($id);
  399. $result = Database::query($sql);
  400. break;
  401. }
  402. }
  403. return 1;
  404. }
  405. /**
  406. * @param int $id
  407. * @param int $day_delta
  408. * @param int $minute_delta
  409. * @return int
  410. */
  411. public function move_event($id, $day_delta, $minute_delta)
  412. {
  413. // we convert the hour delta into minutes and add the minute delta
  414. $delta = ($day_delta * 60 * 24) + $minute_delta;
  415. $delta = intval($delta);
  416. $event = $this->get_event($id);
  417. $all_day = 0;
  418. if ($day_delta == 0 && $minute_delta == 0) {
  419. $all_day = 1;
  420. }
  421. if (!empty($event)) {
  422. switch ($this->type) {
  423. case 'personal':
  424. $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)
  425. WHERE id=".intval($id);
  426. $result = Database::query($sql);
  427. break;
  428. case 'course':
  429. $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)
  430. WHERE c_id = ".$this->course['real_id']." AND id=".intval($id);
  431. $result = Database::query($sql);
  432. break;
  433. case 'admin':
  434. $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)
  435. WHERE id=".intval($id);
  436. $result = Database::query($sql);
  437. break;
  438. }
  439. }
  440. return 1;
  441. }
  442. /**
  443. * Gets a single event
  444. *
  445. * @param int event id
  446. * @return array
  447. */
  448. public function get_event($id)
  449. {
  450. // make sure events of the personal agenda can only be seen by the user himself
  451. $id = intval($id);
  452. $event = null;
  453. switch ($this->type) {
  454. case 'personal':
  455. $sql = " SELECT * FROM ".$this->tbl_personal_agenda." WHERE id = $id AND user = ".api_get_user_id();
  456. $result = Database::query($sql);
  457. if (Database::num_rows($result)) {
  458. $event = Database::fetch_array($result, 'ASSOC');
  459. $event['description'] = $event['text'];
  460. $event['start_date'] = $event['date'];
  461. $event['end_date'] = $event['enddate'];
  462. }
  463. break;
  464. case 'course':
  465. if (!empty($this->course['real_id'])) {
  466. $sql = " SELECT * FROM ".$this->tbl_course_agenda." WHERE c_id = ".$this->course['real_id']." AND id = ".$id;
  467. $result = Database::query($sql);
  468. if (Database::num_rows($result)) {
  469. $event = Database::fetch_array($result, 'ASSOC');
  470. $event['description'] = $event['content'];
  471. }
  472. }
  473. break;
  474. case 'admin':
  475. case 'platform':
  476. $sql = " SELECT * FROM ".$this->tbl_global_agenda." WHERE id=".$id;
  477. $result = Database::query($sql);
  478. if (Database::num_rows($result)) {
  479. $event = Database::fetch_array($result, 'ASSOC');
  480. $event['description'] = $event['content'];
  481. }
  482. break;
  483. }
  484. return $event;
  485. }
  486. /**
  487. *
  488. * Gets personal events
  489. * @param int start date tms
  490. * @param int end date tms
  491. */
  492. public function get_personal_events($start, $end)
  493. {
  494. $start = intval($start);
  495. $end = intval($end);
  496. $start = api_get_utc_datetime($start);
  497. $end = api_get_utc_datetime($end);
  498. $user_id = api_get_user_id();
  499. $sql = "SELECT * FROM ".$this->tbl_personal_agenda."
  500. WHERE date >= '".$start."' AND (enddate <='".$end."' OR enddate IS NULL) AND user = $user_id";
  501. $result = Database::query($sql);
  502. $my_events = array();
  503. if (Database::num_rows($result)) {
  504. while ($row = Database::fetch_array($result, 'ASSOC')) {
  505. $event = array();
  506. $event['id'] = 'personal_'.$row['id'];
  507. $event['title'] = $row['title'];
  508. $event['className'] = 'personal';
  509. $event['borderColor'] = $event['backgroundColor'] = $this->event_personal_color;
  510. $event['editable'] = true;
  511. $event['sent_to'] = get_lang('Me');
  512. $event['type'] = 'personal';
  513. if (!empty($row['date']) && $row['date'] != '0000-00-00 00:00:00') {
  514. $event['start'] = $this->format_event_date($row['date']);
  515. }
  516. if (!empty($row['enddate']) && $row['enddate'] != '0000-00-00 00:00:00') {
  517. $event['end'] = $this->format_event_date($row['enddate']);
  518. }
  519. $event['description'] = $row['text'];
  520. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  521. $my_events[] = $event;
  522. $this->events[] = $event;
  523. }
  524. }
  525. return $my_events;
  526. }
  527. /**
  528. * @param int $start
  529. * @param int $end
  530. * @param array $course_info
  531. * @param int $group_id
  532. * @param int $session_id
  533. * @param int $user_id
  534. * @return array
  535. */
  536. public function get_course_events($start, $end, $course_info, $group_id = 0, $session_id = 0, $user_id = 0)
  537. {
  538. $course_id = $course_info['real_id'];
  539. $user_id = intval($user_id);
  540. $group_list = GroupManager::get_group_list(null, $course_info['code']);
  541. $group_name_list = array();
  542. if (!empty($group_list)) {
  543. foreach ($group_list as $group) {
  544. $group_name_list[$group['id']] = $group['name'];
  545. }
  546. }
  547. if (!api_is_allowed_to_edit()) {
  548. $group_memberships = GroupManager::get_group_ids($course_id, api_get_user_id());
  549. $user_id = api_get_user_id();
  550. } else {
  551. $group_memberships = array_keys($group_name_list);
  552. }
  553. $tlb_course_agenda = Database::get_course_table(TABLE_AGENDA);
  554. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  555. if (!empty($group_id)) {
  556. $group_memberships = array($group_id);
  557. }
  558. $session_id = intval($session_id);
  559. if (is_array($group_memberships) && count($group_memberships) > 0) {
  560. if (api_is_allowed_to_edit()) {
  561. if (!empty($user_id)) {
  562. $where_condition = "( ip.to_user_id = $user_id AND ip.to_group_id is null OR ip.to_group_id IN (0, ".implode(
  563. ", ",
  564. $group_memberships
  565. ).") ) ";
  566. } else {
  567. $where_condition = "( ip.to_group_id is null OR ip.to_group_id IN (0, ".implode(
  568. ", ",
  569. $group_memberships
  570. ).") ) ";
  571. }
  572. } else {
  573. $where_condition = "( ip.to_user_id = $user_id OR ip.to_group_id IN (0, ".implode(
  574. ", ",
  575. $group_memberships
  576. ).") ) ";
  577. }
  578. $sql = "SELECT DISTINCT
  579. agenda.*,
  580. ip.visibility,
  581. ip.to_group_id,
  582. ip.insert_user_id,
  583. ip.ref,
  584. to_user_id
  585. FROM ".$tlb_course_agenda." agenda, ".$tbl_property." ip
  586. WHERE agenda.id = ip.ref AND
  587. ip.tool ='".TOOL_CALENDAR_EVENT."' AND
  588. $where_condition AND
  589. ip.visibility = '1' AND
  590. agenda.c_id = $course_id AND
  591. ip.c_id = $course_id
  592. GROUP BY id";
  593. } else {
  594. if (api_is_allowed_to_edit()) {
  595. $where_condition = "";
  596. } else {
  597. $where_condition = "( ip.to_user_id=$user_id OR ip.to_group_id='0') AND ";
  598. }
  599. $sql = "SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref, to_user_id
  600. FROM ".$tlb_course_agenda." agenda, ".$tbl_property." ip
  601. WHERE agenda.id = ip.ref AND
  602. ip.tool='".TOOL_CALENDAR_EVENT."' AND
  603. $where_condition
  604. ip.visibility='1' AND
  605. agenda.c_id = $course_id AND
  606. ip.c_id = $course_id AND
  607. agenda.session_id = $session_id AND
  608. ip.id_session = $session_id
  609. ";
  610. }
  611. $result = Database::query($sql);
  612. $events = array();
  613. if (Database::num_rows($result)) {
  614. $events_added = array();
  615. while ($row = Database::fetch_array($result, 'ASSOC')) {
  616. //to gather sent_tos
  617. $sql = "SELECT to_user_id, to_group_id
  618. FROM ".$tbl_property." ip
  619. WHERE ip.tool = '".TOOL_CALENDAR_EVENT."' AND
  620. ref = {$row['ref']} AND
  621. ip.visibility = '1' AND
  622. ip.c_id = $course_id";
  623. $sent_to_result = Database::query($sql);
  624. $user_to_array = array();
  625. $group_to_array = array();
  626. while ($row_send_to = Database::fetch_array($sent_to_result, 'ASSOC')) {
  627. if (!empty($row_send_to['to_group_id'])) {
  628. $group_to_array[] = $row_send_to['to_group_id'];
  629. }
  630. if (!empty($row_send_to['to_user_id'])) {
  631. $user_to_array[] = $row_send_to['to_user_id'];
  632. }
  633. }
  634. //Only show events from the session
  635. /*if (api_get_course_int_id()) {
  636. if ($row['session_id'] != api_get_session_id()) {
  637. continue;
  638. }
  639. }*/
  640. $event = array();
  641. $event['id'] = 'course_'.$row['id'];
  642. //To avoid doubles
  643. if (in_array($row['id'], $events_added)) {
  644. continue;
  645. }
  646. $events_added[] = $row['id'];
  647. $attachment = get_attachment($row['id'], $course_id);
  648. $has_attachment = '';
  649. if (!empty($attachment)) {
  650. $has_attachment = Display::return_icon('attachment.gif', get_lang('Attachment'));
  651. $user_filename = $attachment['filename'];
  652. $full_file_name = 'download.php?file='.$attachment['path'].'&course_id='.$course_id;
  653. $event['attachment'] = $has_attachment.Display::url($user_filename, $full_file_name);
  654. } else {
  655. $event['attachment'] = '';
  656. }
  657. $event['title'] = $row['title'];
  658. $event['className'] = 'course';
  659. $event['allDay'] = 'false';
  660. $event['course_id'] = $course_id;
  661. $event['borderColor'] = $event['backgroundColor'] = $this->event_course_color;
  662. if (isset($row['session_id']) && !empty($row['session_id'])) {
  663. $event['borderColor'] = $event['backgroundColor'] = $this->event_session_color;
  664. }
  665. if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
  666. $event['borderColor'] = $event['backgroundColor'] = $this->event_group_color;
  667. }
  668. $event['editable'] = false;
  669. if (api_is_allowed_to_edit() && $this->type == 'course') {
  670. $event['editable'] = true;
  671. }
  672. if (!empty($row['start_date']) && $row['start_date'] != '0000-00-00 00:00:00') {
  673. $event['start'] = $this->format_event_date($row['start_date']);
  674. }
  675. if (!empty($row['end_date']) && $row['end_date'] != '0000-00-00 00:00:00') {
  676. $event['end'] = $this->format_event_date($row['end_date']);
  677. }
  678. $event['sent_to'] = '';
  679. //$event['type'] = $this->type;
  680. $event['type'] = 'course';
  681. if ($row['session_id'] != 0) {
  682. $event['type'] = 'session';
  683. }
  684. //Event Sent to a group?
  685. if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
  686. $sent_to = array();
  687. if (!empty($group_to_array)) {
  688. foreach ($group_to_array as $group_item) {
  689. $sent_to[] = $group_name_list[$group_item];
  690. }
  691. }
  692. $sent_to = implode('@@', $sent_to);
  693. $sent_to = str_replace('@@', '</div><div class="label_tag notice">', $sent_to);
  694. $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
  695. $event['type'] = 'group';
  696. }
  697. //Event sent to a user?
  698. if (isset($row['to_user_id'])) {
  699. $sent_to = array();
  700. if (!empty($user_to_array)) {
  701. foreach ($user_to_array as $item) {
  702. $user_info = api_get_user_info($item);
  703. // add username as tooltip for $event['sent_to'] - ref #4226
  704. $username = api_htmlentities(
  705. sprintf(get_lang('LoginX'), $user_info['username']),
  706. ENT_QUOTES
  707. );
  708. $sent_to[] = "<span title='".$username."'>".$user_info['complete_name']."</span>";
  709. }
  710. }
  711. $sent_to = implode('@@', $sent_to);
  712. $sent_to = str_replace('@@', '</div><div class="label_tag notice">', $sent_to);
  713. $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
  714. }
  715. //Event sent to everyone!
  716. if (empty($event['sent_to'])) {
  717. $event['sent_to'] = '<div class="label_tag notice">'.get_lang('Everyone').'</div>';
  718. }
  719. $event['description'] = $row['content'];
  720. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  721. $this->events[] = $event;
  722. }
  723. }
  724. return $this->events;
  725. }
  726. /**
  727. * @param int $start
  728. * @param int $end
  729. * @return array
  730. */
  731. public function get_platform_events($start, $end)
  732. {
  733. $start = intval($start);
  734. $end = intval($end);
  735. $start = api_get_utc_datetime($start);
  736. $end = api_get_utc_datetime($end);
  737. $access_url_id = api_get_current_access_url_id();
  738. $sql = "SELECT * FROM ".$this->tbl_global_agenda."
  739. WHERE start_date >= '".$start."' AND end_date <= '".$end."' AND access_url_id = $access_url_id ";
  740. $result = Database::query($sql);
  741. $my_events = array();
  742. if (Database::num_rows($result)) {
  743. while ($row = Database::fetch_array($result, 'ASSOC')) {
  744. $event = array();
  745. $event['id'] = 'platform_'.$row['id'];
  746. $event['title'] = $row['title'];
  747. $event['className'] = 'platform';
  748. $event['allDay'] = 'false';
  749. $event['borderColor'] = $event['backgroundColor'] = $this->event_platform_color;
  750. $event['editable'] = false;
  751. $event['type'] = 'admin';
  752. if (api_is_platform_admin() && $this->type == 'admin') {
  753. $event['editable'] = true;
  754. }
  755. if (!empty($row['start_date']) && $row['start_date'] != '0000-00-00 00:00:00') {
  756. $event['start'] = $this->format_event_date($row['start_date']);
  757. }
  758. if (!empty($row['end_date']) && $row['end_date'] != '0000-00-00 00:00:00') {
  759. $event['end'] = $this->format_event_date($row['end_date']);
  760. }
  761. $event['description'] = $row['content'];
  762. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  763. $my_events[] = $event;
  764. $this->events[] = $event;
  765. }
  766. }
  767. return $my_events;
  768. }
  769. /**
  770. * Format needed for the Fullcalendar js lib
  771. * @param string UTC time
  772. * @return string
  773. */
  774. public function format_event_date($utc_time)
  775. {
  776. return date('c', api_strtotime(api_get_local_time($utc_time)));
  777. }
  778. /**
  779. * this function shows the form with the user that were not selected
  780. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  781. * @return string html code
  782. */
  783. static function construct_not_selected_select_form(
  784. $group_list = null,
  785. $user_list = null,
  786. $to_already_selected = array()
  787. ) {
  788. $html = '<select id="users_to_send_id" data-placeholder="'.get_lang('Select').'" name="users_to_send[]" multiple="multiple" style="width:250px" >';
  789. if ($to_already_selected == 'everyone') {
  790. $html .= '<option value="everyone" checked="checked">'.get_lang('Everyone').'</option>';
  791. } else {
  792. $html .= '<option value="everyone">'.get_lang('Everyone').'</option>';
  793. }
  794. if (is_array($group_list)) {
  795. $html .= '<optgroup label="'.get_lang('Groups').'">';
  796. foreach ($group_list as $this_group) {
  797. if (!is_array($to_already_selected) || !in_array("GROUP:".$this_group['id'], $to_already_selected)) {
  798. // $to_already_selected is the array containing the groups (and users) that are already selected
  799. $count_users = isset($this_group['count_users']) ? $this_group['count_users'] : $this_group['userNb'];
  800. $count_users = " &ndash; $count_users ".get_lang('Users');
  801. $html .= '<option value="GROUP:'.$this_group['id'].'"> '.$this_group['name'].$count_users.'</option>';
  802. }
  803. }
  804. $html .= '</optgroup>';
  805. }
  806. // adding the individual users to the select form
  807. if (is_array($group_list)) {
  808. $html .= '<optgroup label="'.get_lang('Users').'">';
  809. }
  810. foreach ($user_list as $this_user) {
  811. // $to_already_selected is the array containing the users (and groups) that are already selected
  812. if (!is_array($to_already_selected) || !in_array("USER:".$this_user['user_id'], $to_already_selected)) {
  813. $username = api_htmlentities(sprintf(get_lang('LoginX'), $this_user['username']), ENT_QUOTES);
  814. // @todo : add title attribute $username in the jqdialog window. wait for a chosen version to inherit title attribute
  815. $html .= '<option title="'.$username.'" value="USER:'.$this_user['user_id'].'">'.api_get_person_name(
  816. $this_user['firstname'],
  817. $this_user['lastname']
  818. ).' ('.$this_user['username'].') </option>';
  819. }
  820. }
  821. if (is_array($group_list)) {
  822. $html .= '</optgroup>';
  823. $html .= "</select>";
  824. }
  825. return $html;
  826. }
  827. static function construct_not_selected_select_form_validator(
  828. $form,
  829. $group_list = null,
  830. $user_list = null,
  831. $to_already_selected = array()
  832. ) {
  833. $params = array(
  834. 'id' => 'users_to_send_id',
  835. 'data-placeholder' => get_lang('Select'),
  836. 'multiple' => 'multiple',
  837. 'style' => 'width:250px',
  838. 'class' => 'chzn-select'
  839. );
  840. $select = $form->addElement('select', 'users_to_send', get_lang('To'), null, $params);
  841. $select->addOption(get_lang('Everyone'), 'everyone');
  842. $options = array();
  843. if (is_array($group_list)) {
  844. foreach ($group_list as $this_group) {
  845. if (!is_array($to_already_selected) || !in_array("GROUP:".$this_group['id'], $to_already_selected)) {
  846. // $to_already_selected is the array containing the groups (and users) that are already selected
  847. $count_users = isset($this_group['count_users']) ? $this_group['count_users'] : $this_group['userNb'];
  848. $count_users = " &ndash; $count_users ".get_lang('Users');
  849. $options[] = array(
  850. 'text' => $this_group['name'].$count_users,
  851. 'value' => "GROUP:".$this_group['id']
  852. );
  853. }
  854. }
  855. $select->addOptGroup($options, get_lang('Groups'));
  856. }
  857. // adding the individual users to the select form
  858. if (is_array($group_list)) {
  859. $options = array();
  860. foreach ($user_list as $this_user) {
  861. // $to_already_selected is the array containing the users (and groups) that are already selected
  862. if (!is_array($to_already_selected) || !in_array("USER:".$this_user['user_id'], $to_already_selected)) {
  863. //$username = api_htmlentities(sprintf(get_lang('LoginX'), $this_user['username']), ENT_QUOTES);
  864. // @todo : add title attribute $username in the jqdialog window. wait for a chosen version to inherit title attribute
  865. // from <option> to <li>
  866. //$html .= '<option title="'.$username.'" value="USER:'.$this_user['user_id'].'">'.api_get_person_name($this_user['firstname'], $this_user['lastname']).' ('.$this_user['username'].') </option>';
  867. $options[] = array(
  868. 'text' => api_get_person_name(
  869. $this_user['firstname'],
  870. $this_user['lastname']
  871. ).' ('.$this_user['username'].')',
  872. 'value' => "USER:".$this_user['user_id']
  873. );
  874. }
  875. }
  876. $select->addOptGroup($options, get_lang('Users'));
  877. }
  878. }
  879. /**
  880. * This public function separates the users from the groups
  881. * users have a value USER:XXX (with XXX the dokeos id
  882. * groups have a value GROUP:YYY (with YYY the group id)
  883. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  884. * @param array
  885. * @return array
  886. */
  887. public function separate_users_groups($to)
  888. {
  889. $grouplist = array();
  890. $userlist = array();
  891. $send_to = null;
  892. $send_to['everyone'] = false;
  893. if (is_array($to) && count($to) > 0) {
  894. foreach ($to as $to_item) {
  895. if ($to_item == 'everyone') {
  896. $send_to['everyone'] = true;
  897. } else {
  898. list($type, $id) = explode(':', $to_item);
  899. switch ($type) {
  900. case 'GROUP':
  901. $grouplist[] = $id;
  902. break;
  903. case 'USER':
  904. $userlist[] = $id;
  905. break;
  906. }
  907. }
  908. }
  909. $send_to['groups'] = $grouplist;
  910. $send_to['users'] = $userlist;
  911. }
  912. return $send_to;
  913. }
  914. /**
  915. * @param array $params
  916. */
  917. static function show_form($params = array())
  918. {
  919. $form = new FormValidator('add_event', 'POST', api_get_self().'?'.api_get_cidreq(), null, array('enctype' => 'multipart/form-data'));
  920. $id = isset($params['id']) ? $params['id'] : null;
  921. if ($id) {
  922. $form_title = get_lang('ModifyCalendarItem');
  923. $button = get_lang('ModifyEvent');
  924. } else {
  925. $form_title = get_lang('AddCalendarItem');
  926. $button = get_lang('AgendaAdd');
  927. }
  928. $form->addElement('header', $form_title);
  929. $form->addElement('hidden', 'id', $id);
  930. $form->addElement('hidden', 'action', $params['action']);
  931. $form->addElement('hidden', 'id_attach', $params['id_attach']);
  932. $form->addElement('text', 'title', get_lang('ItemTitle'));
  933. $group_id = api_get_group_id();
  934. if (isset ($group_id) && !empty($group_id)) {
  935. $form->addElement('hidden', 'selected_form[0]', "GROUP:'.$group_id.'");
  936. $form->addElement('hidden', 'to', 'true');
  937. } else {
  938. self::show_to_form($form, $to);
  939. }
  940. $form->addElement('text', 'start_date', get_lang('StartDate'));
  941. $form->addElement('text', 'end_date', get_lang('EndDate'));
  942. if (empty($id)) {
  943. $form->addElement('label', null, Display::url(get_lang('RepeatEvent'), '#', array('id' => 'repeat', 'class' => 'advanced_options')));
  944. $form->addElement('html', '<div id="repeat_options" style="display:block">');
  945. $form->addElement('checkbox', 'repeat', null, get_lang('RepeatEvent'));
  946. $repeat_events = array(
  947. 'daily' => get_lang('RepeatDaily'),
  948. 'weekly' => get_lang('RepeatWeekly'),
  949. 'monthlyByDate' => get_lang('RepeatMonthlyByDate'),
  950. 'yearly' => get_lang('RepeatYearly')
  951. );
  952. $form->addElement('select', 'repeat_type', get_lang('RepeatType'), $repeat_events);
  953. $form->addElement('text', 'repeat_end_day', get_lang('RepeatEnd'));
  954. $form->addElement('html', '</div>');
  955. if (!api_is_allowed_to_edit(null, true)) {
  956. $toolbar = 'AgendaStudent';
  957. } else {
  958. $toolbar = 'Agenda';
  959. }
  960. //$form->addElement('html_editor', 'content', get_lang('Description'), null, array('ToolbarSet' => $toolbar, 'Width' => '100%', 'Height' => '200'));
  961. $form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
  962. $form->addElement('text', 'file_comment', get_lang('Comment'));
  963. }
  964. $form->addElement('button', 'submit', $button);
  965. $form->display();
  966. }
  967. /**
  968. * @param array $form
  969. * @param $to_already_selected
  970. */
  971. public static function show_to_form($form, $to_already_selected)
  972. {
  973. $order = 'lastname';
  974. if (api_is_western_name_order()) {
  975. $order = 'firstname';
  976. }
  977. $user_list = CourseManager::get_user_list_from_course_code(
  978. api_get_course_id(),
  979. api_get_session_id(),
  980. null,
  981. $order
  982. );
  983. $group_list = CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id());
  984. self::construct_not_selected_select_form_validator($form, $group_list, $user_list, $to_already_selected);
  985. }
  986. /**
  987. * Adds a repetitive item to the database
  988. * @param array Course info
  989. * @param int The original event's id
  990. * @param string Type of repetition
  991. * @param int Timestamp of end of repetition (repeating until that date)
  992. * @param array $userList
  993. * @param string a comment about a attachment file into agenda
  994. * @return boolean False if error, True otherwise
  995. */
  996. private function addRepeatItem($course_info, $orig_id, $type, $end, $userList, $file_comment = '')
  997. {
  998. $t_agenda = Database::get_course_table(TABLE_AGENDA);
  999. $t_agenda_r = Database::get_course_table(TABLE_AGENDA_REPEAT);
  1000. $course_id = $course_info['real_id'];
  1001. $sql = 'SELECT title, content, start_date as sd, end_date as ed FROM '.$t_agenda.'
  1002. WHERE c_id = '.$course_id.' AND id ="'.intval($orig_id).'" ';
  1003. $res = Database::query($sql);
  1004. if (Database::num_rows($res) !== 1) {
  1005. return false;
  1006. }
  1007. $row = Database::fetch_array($res);
  1008. $orig_start = api_strtotime(api_get_local_time($row['sd']));
  1009. $orig_end = api_strtotime(api_get_local_time($row['ed']));
  1010. $diff = $orig_end - $orig_start;
  1011. $orig_title = $row['title'];
  1012. $orig_content = $row['content'];
  1013. $now = time();
  1014. $type = Database::escape_string($type);
  1015. $end = intval($end);
  1016. if (1 <= $end && $end <= 500) {
  1017. //we assume that, with this type of value, the user actually gives a count of repetitions
  1018. //and that he wants us to calculate the end date with that (particularly in case of imports from ical)
  1019. switch ($type) {
  1020. case 'daily':
  1021. $end = $orig_start + (86400 * $end);
  1022. break;
  1023. case 'weekly':
  1024. $end = $this->addWeek($orig_start, $end);
  1025. break;
  1026. case 'monthlyByDate':
  1027. $end = $this->addMonth($orig_start, $end);
  1028. break;
  1029. case 'monthlyByDay':
  1030. //TODO
  1031. break;
  1032. case 'monthlyByDayR':
  1033. //TODO
  1034. break;
  1035. case 'yearly':
  1036. $end = $this->addYear($orig_start, $end);
  1037. break;
  1038. }
  1039. }
  1040. if ($end > $now
  1041. && in_array($type, array('daily', 'weekly', 'monthlyByDate', 'monthlyByDay', 'monthlyByDayR', 'yearly'))) {
  1042. $sql = "INSERT INTO $t_agenda_r (c_id, cal_id, cal_type, cal_end)
  1043. VALUES ($course_id, '$orig_id', '$type', $end)";
  1044. Database::query($sql);
  1045. switch ($type) {
  1046. case 'daily':
  1047. for ($i = $orig_start + 86400; ($i <= $end); $i += 86400) {
  1048. $this->add_event(date('Y-m-d H:i:s', $i), date('Y-m-d H:i:s', $i + $diff), false, null, $orig_title, $orig_content, $userList, false, $orig_id);
  1049. }
  1050. break;
  1051. case 'weekly':
  1052. for ($i = $orig_start + 604800; ($i <= $end); $i += 604800) {
  1053. $this->add_event(date('Y-m-d H:i:s', $i), date('Y-m-d H:i:s', $i + $diff), false, null, $orig_title, $orig_content, $userList, false, $orig_id);
  1054. //$res = agenda_add_item($course_info, $orig_title, $orig_content, date('Y-m-d H:i:s', $i), date('Y-m-d H:i:s', $i + $diff), $userList, $orig_id, $file_comment);
  1055. }
  1056. break;
  1057. case 'monthlyByDate':
  1058. $next_start = $this->addMonth($orig_start);
  1059. while ($next_start <= $end) {
  1060. $this->add_event(date('Y-m-d H:i:s', $next_start), date('Y-m-d H:i:s', $next_start + $diff), false, null, $orig_title, $orig_content, $userList, false, $orig_id);
  1061. //$res = agenda_add_item($course_info, $orig_title, $orig_content, date('Y-m-d H:i:s', $next_start), date('Y-m-d H:i:s', $next_start + $diff), $userList, $orig_id, $file_comment);
  1062. $next_start = $this->addMonth($next_start);
  1063. }
  1064. break;
  1065. case 'monthlyByDay':
  1066. //not yet implemented
  1067. break;
  1068. case 'monthlyByDayR':
  1069. //not yet implemented
  1070. break;
  1071. case 'yearly':
  1072. $next_start = $this->addYear($orig_start);
  1073. while ($next_start <= $end) {
  1074. $this->add_event(date('Y-m-d H:i:s', $next_start), date('Y-m-d H:i:s', $next_start + $diff), false, null, $orig_title, $orig_content, $userList, false, $orig_id);
  1075. //agenda_add_item($course_info, $orig_title, $orig_content, date('Y-m-d H:i:s', $next_start), date('Y-m-d H:i:s', $next_start + $diff), $userList, $orig_id, $file_comment);
  1076. $next_start = $this->addYear($next_start);
  1077. }
  1078. break;
  1079. }
  1080. }
  1081. return true;
  1082. }
  1083. /**
  1084. * @param int $eventId
  1085. * @param array $settings = array('comment' => $comment, 'file' => $file
  1086. * @return bool
  1087. */
  1088. private function addAttachment($eventId, $settings)
  1089. {
  1090. $table = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  1091. if (!isset($settings['file'])) {
  1092. return false;
  1093. }
  1094. $file = $settings['file'];
  1095. if (!empty($file['name'])) {
  1096. $upload_ok = FileManager::process_uploaded_file($file);
  1097. }
  1098. $_course = api_get_course_info();
  1099. if (!empty($upload_ok)) {
  1100. $courseDir = $_course['path'].'/upload/calendar';
  1101. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1102. $updir = $sys_course_path.$courseDir;
  1103. // Try to add an extension to the file if it hasn't one
  1104. $new_file_name = FileManager::add_ext_on_mime(
  1105. stripslashes($_FILES['user_upload']['name']),
  1106. $_FILES['user_upload']['type']
  1107. );
  1108. // user's file name
  1109. $file_name = $_FILES['user_upload']['name'];
  1110. if (!FileManager::filter_extension($new_file_name)) {
  1111. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1112. } else {
  1113. $new_file_name = uniqid('');
  1114. $new_path = $updir.'/'.$new_file_name;
  1115. $result = move_uploaded_file($file['tmp_name'], $new_path);
  1116. $safe_file_comment = Database::escape_string($settings['comment']);
  1117. $safe_file_name = Database::escape_string($file_name);
  1118. $safe_new_file_name = Database::escape_string($new_file_name);
  1119. $course_id = api_get_course_int_id();
  1120. // Storing the attachments if any
  1121. if ($result) {
  1122. $sql = 'INSERT INTO '.$table.'(c_id, filename,comment, path,agenda_id,size) '.
  1123. "VALUES ($course_id, '".$safe_file_name."', '".$safe_file_comment."', '".$safe_new_file_name."' , '".$eventId."', '".intval($file['size'])."' )";
  1124. Database::query($sql);
  1125. $last_id_file = Database::insert_id();
  1126. api_item_property_update($_course, 'calendar_event_attachment', $last_id_file, 'AgendaAttachmentAdded', api_get_user_id());
  1127. }
  1128. }
  1129. }
  1130. }
  1131. /**
  1132. * @param $timestamp
  1133. * @param int $num
  1134. * @return int
  1135. */
  1136. private function addMonth($timestamp, $num = 1)
  1137. {
  1138. list($y, $m, $d, $h, $n, $s) = explode('/', date('Y/m/d/h/i/s', $timestamp));
  1139. if ($m + $num > 12) {
  1140. $y += floor($num / 12);
  1141. $m += $num % 12;
  1142. } else {
  1143. $m += $num;
  1144. }
  1145. return mktime($h, $n, $s, $m, $d, $y);
  1146. }
  1147. /**
  1148. * @param $timestamp
  1149. * @param int $num
  1150. * @return int
  1151. */
  1152. private function addYear($timestamp, $num = 1)
  1153. {
  1154. list($y, $m, $d, $h, $n, $s) = explode('/', date('Y/m/d/h/i/s', $timestamp));
  1155. return mktime($h, $n, $s, $m, $d, $y + $num);
  1156. }
  1157. /**
  1158. * Adds x weeks to a UNIX timestamp
  1159. * @param int The timestamp
  1160. * @param int The number of weeks to add
  1161. * @return int The new timestamp
  1162. */
  1163. private function addWeek($timestamp, $num = 1)
  1164. {
  1165. return $timestamp + $num * 604800;
  1166. }
  1167. }