agenda.lib.php 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Agenda
  5. *
  6. * @author: Julio Montoya <gugli100@gmail.com>
  7. */
  8. class Agenda
  9. {
  10. public $events = array();
  11. /** @var string Current type */
  12. public $type = 'personal';
  13. public $types = array('personal', 'admin', 'course');
  14. public $sessionId = 0;
  15. /** @var array */
  16. public $course;
  17. /**
  18. *
  19. */
  20. public function __construct()
  21. {
  22. //Table definitions
  23. $this->tbl_global_agenda = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  24. $this->tbl_personal_agenda = Database::get_main_table(TABLE_PERSONAL_AGENDA);
  25. $this->tbl_course_agenda = Database::get_course_table(TABLE_AGENDA);
  26. $this->table_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT);
  27. //Setting the course object if we are in a course
  28. $this->course = null;
  29. $courseInfo = api_get_course_info();
  30. if (!empty($courseInfo)) {
  31. $this->course = $courseInfo;
  32. }
  33. $this->sessionId = api_get_session_id();
  34. $this->events = array();
  35. //Event colors
  36. $this->event_platform_color = 'red'; //red
  37. $this->event_course_color = '#458B00'; //green
  38. $this->event_group_color = '#A0522D'; //siena
  39. $this->event_session_color = '#00496D'; // kind of green
  40. $this->event_personal_color = 'steel blue'; //steel blue
  41. }
  42. /**
  43. * @param array $courseInfo
  44. */
  45. public function set_course($courseInfo)
  46. {
  47. $this->course = $courseInfo;
  48. }
  49. /**
  50. * @return array
  51. */
  52. public function getTypes()
  53. {
  54. return $this->types;
  55. }
  56. /**
  57. * Adds an event to the calendar
  58. * @param string $start datetime format: 2012-06-14 09:00:00
  59. * @param string $end datetime format: 2012-06-14 09:00:00
  60. * @param string $allDay (true, false)
  61. * @param string $title
  62. * @param string $content
  63. * @param array $usersToSend array('everyone') or a list of user/group ids
  64. * @param bool $addAsAnnouncement event as a *course* announcement
  65. * @param int $parentEventId
  66. * @param array $attachmentArray $_FILES['']
  67. * @param string $attachmentComment
  68. *
  69. * @return int
  70. */
  71. public function add_event(
  72. $start,
  73. $end,
  74. $allDay,
  75. $title,
  76. $content,
  77. $usersToSend = array(),
  78. $addAsAnnouncement = false,
  79. $parentEventId = null,
  80. $attachmentArray = array(),
  81. $attachmentComment = null
  82. ) {
  83. $start = api_get_utc_datetime($start);
  84. $end = api_get_utc_datetime($end);
  85. $allDay = isset($allDay) && $allDay == 'true' ? 1 : 0;
  86. $id = null;
  87. switch ($this->type) {
  88. case 'personal':
  89. $attributes = array(
  90. 'user' => api_get_user_id(),
  91. 'title' => $title,
  92. 'text' => $content,
  93. 'date' => $start,
  94. 'enddate' => $end,
  95. 'all_day' => $allDay
  96. );
  97. $id = Database::insert($this->tbl_personal_agenda, $attributes);
  98. break;
  99. case 'course':
  100. $attributes = array(
  101. 'title' => $title,
  102. 'content' => $content,
  103. 'start_date' => $start,
  104. 'end_date' => $end,
  105. 'all_day' => $allDay,
  106. 'session_id' => api_get_session_id(),
  107. 'c_id' => $this->course['real_id']
  108. );
  109. if (!empty($parentEventId)) {
  110. $attributes['parent_event_id'] = $parentEventId;
  111. }
  112. // Simple course event.
  113. $id = Database::insert($this->tbl_course_agenda, $attributes);
  114. if ($id) {
  115. $groupId = api_get_group_id();
  116. if (!empty($usersToSend)) {
  117. $sendTo = $this->parseSendToArray($usersToSend);
  118. if ($sendTo['everyone']) {
  119. api_item_property_update(
  120. $this->course,
  121. TOOL_CALENDAR_EVENT,
  122. $id,
  123. "AgendaAdded",
  124. api_get_user_id(),
  125. $groupId,
  126. '',
  127. $start,
  128. $end
  129. );
  130. api_item_property_update(
  131. $this->course,
  132. TOOL_CALENDAR_EVENT,
  133. $id,
  134. "visible",
  135. api_get_user_id(),
  136. $groupId,
  137. '',
  138. $start,
  139. $end
  140. );
  141. } else {
  142. // Storing the selected groups
  143. if (!empty($sendTo['groups'])) {
  144. foreach ($sendTo['groups'] as $group) {
  145. api_item_property_update(
  146. $this->course,
  147. TOOL_CALENDAR_EVENT,
  148. $id,
  149. "AgendaAdded",
  150. api_get_user_id(),
  151. $group,
  152. 0,
  153. $start,
  154. $end
  155. );
  156. api_item_property_update(
  157. $this->course,
  158. TOOL_CALENDAR_EVENT,
  159. $id,
  160. "visible",
  161. api_get_user_id(),
  162. $group,
  163. 0,
  164. $start,
  165. $end
  166. );
  167. }
  168. }
  169. // storing the selected users
  170. if (!empty($sendTo['users'])) {
  171. foreach ($sendTo['users'] as $userId) {
  172. api_item_property_update(
  173. $this->course,
  174. TOOL_CALENDAR_EVENT,
  175. $id,
  176. "AgendaAdded",
  177. api_get_user_id(),
  178. $groupId,
  179. $userId,
  180. $start,
  181. $end
  182. );
  183. api_item_property_update(
  184. $this->course,
  185. TOOL_CALENDAR_EVENT,
  186. $id,
  187. "visible",
  188. api_get_user_id(),
  189. $groupId,
  190. $userId,
  191. $start,
  192. $end
  193. );
  194. }
  195. }
  196. }
  197. }
  198. // Add announcement.
  199. if ($addAsAnnouncement) {
  200. $this->store_agenda_item_as_announcement($id, $usersToSend);
  201. }
  202. // Add attachment.
  203. if (isset($attachmentArray) && !empty($attachmentArray)) {
  204. $this->addAttachment(
  205. $id,
  206. $attachmentArray,
  207. $attachmentComment,
  208. $this->course
  209. );
  210. }
  211. }
  212. break;
  213. case 'admin':
  214. if (api_is_platform_admin()) {
  215. $attributes = array(
  216. 'title' => $title,
  217. 'content' => $content,
  218. 'start_date' => $start,
  219. 'end_date' => $end,
  220. 'all_day' => $allDay,
  221. 'access_url_id' => api_get_current_access_url_id()
  222. );
  223. $id = Database::insert($this->tbl_global_agenda, $attributes);
  224. }
  225. break;
  226. }
  227. return $id;
  228. }
  229. /**
  230. * @param int $eventId
  231. * @param int $courseId
  232. *
  233. * @return array
  234. */
  235. public function getRepeatedInfoByEvent($eventId, $courseId)
  236. {
  237. $repeatTable = Database::get_course_table(TABLE_AGENDA_REPEAT);
  238. $eventId = intval($eventId);
  239. $courseId = intval($courseId);
  240. $sql = "SELECT * FROM $repeatTable
  241. WHERE c_id = $courseId AND cal_id = $eventId";
  242. $res = Database::query($sql);
  243. $repeatInfo = array();
  244. if (Database::num_rows($res) > 0) {
  245. $repeatInfo = Database::fetch_array($res, 'ASSOC');
  246. }
  247. return $repeatInfo;
  248. }
  249. /**
  250. * @param int $eventId
  251. * @param string $type
  252. * @param string $end in local time
  253. * @param array $sentTo
  254. *
  255. * @return bool
  256. */
  257. public function addRepeatedItem($eventId, $type, $end, $sentTo = array())
  258. {
  259. $t_agenda = Database::get_course_table(TABLE_AGENDA);
  260. $t_agenda_r = Database::get_course_table(TABLE_AGENDA_REPEAT);
  261. if (empty($this->course)) {
  262. return false;
  263. }
  264. $course_id = $this->course['real_id'];
  265. $eventId = intval($eventId);
  266. $sql = "SELECT title, content, start_date, end_date, all_day
  267. FROM $t_agenda
  268. WHERE c_id = $course_id AND id = $eventId";
  269. $res = Database::query($sql);
  270. if (Database::num_rows($res) !== 1) {
  271. return false;
  272. }
  273. $row = Database::fetch_array($res);
  274. $origStartDate = api_strtotime($row['start_date'], 'UTC');
  275. $origEndDate = api_strtotime($row['end_date'], 'UTC');
  276. $diff = $origEndDate - $origStartDate;
  277. $title = $row['title'];
  278. $content = $row['content'];
  279. $allDay = $row['all_day'];
  280. $now = time();
  281. $type = Database::escape_string($type);
  282. $end = api_strtotime($end);
  283. if (1 <= $end && $end <= 500) {
  284. // We assume that, with this type of value, the user actually gives a count of repetitions
  285. //and that he wants us to calculate the end date with that (particularly in case of imports from ical)
  286. switch ($type) {
  287. case 'daily':
  288. $end = $origStartDate + (86400 * $end);
  289. break;
  290. case 'weekly':
  291. $end = $this->addWeek($origStartDate, $end);
  292. break;
  293. case 'monthlyByDate':
  294. $end = $this->addMonth($origStartDate, $end);
  295. break;
  296. case 'monthlyByDay':
  297. //TODO
  298. break;
  299. case 'monthlyByDayR':
  300. //TODO
  301. break;
  302. case 'yearly':
  303. $end = $this->addYear($origStartDate, $end);
  304. break;
  305. }
  306. }
  307. $typeList = array('daily', 'weekly', 'monthlyByDate', 'monthlyByDay', 'monthlyByDayR', 'yearly');
  308. if ($end > $now && in_array($type, $typeList)) {
  309. $sql = "INSERT INTO $t_agenda_r (c_id, cal_id, cal_type, cal_end)
  310. VALUES ($course_id, '$eventId', '$type', '$end')";
  311. Database::query($sql);
  312. switch ($type) {
  313. // @todo improve loop.
  314. case 'daily':
  315. for ($i = $origStartDate + 86400; $i <= $end; $i += 86400) {
  316. $start = date('Y-m-d H:i:s', $i);
  317. $repeatEnd = date('Y-m-d H:i:s', $i + $diff);
  318. $this->add_event(
  319. $start,
  320. $repeatEnd,
  321. $allDay,
  322. $title,
  323. $content,
  324. $sentTo,
  325. false,
  326. $eventId
  327. );
  328. }
  329. break;
  330. case 'weekly':
  331. for ($i = $origStartDate + 604800; $i <= $end; $i += 604800) {
  332. $start = date('Y-m-d H:i:s', $i);
  333. $repeatEnd = date('Y-m-d H:i:s', $i + $diff);
  334. $this->add_event(
  335. $start,
  336. $repeatEnd,
  337. $allDay,
  338. $title,
  339. $content,
  340. $sentTo,
  341. false,
  342. $eventId
  343. );
  344. }
  345. break;
  346. case 'monthlyByDate':
  347. $next_start = $this->addMonth($origStartDate);
  348. while ($next_start <= $end) {
  349. $start = date('Y-m-d H:i:s', $next_start);
  350. $repeatEnd = date('Y-m-d H:i:s', $next_start + $diff);
  351. $this->add_event(
  352. $start,
  353. $repeatEnd,
  354. $allDay,
  355. $title,
  356. $content,
  357. $sentTo,
  358. false,
  359. $eventId
  360. );
  361. $next_start = $this->addMonth($next_start);
  362. }
  363. break;
  364. case 'monthlyByDay':
  365. //not yet implemented
  366. break;
  367. case 'monthlyByDayR':
  368. //not yet implemented
  369. break;
  370. case 'yearly':
  371. $next_start = $this->addYear($origStartDate);
  372. while ($next_start <= $end) {
  373. $start = date('Y-m-d H:i:s', $next_start);
  374. $repeatEnd = date('Y-m-d H:i:s', $next_start + $diff);
  375. $this->add_event(
  376. $start,
  377. $repeatEnd,
  378. $allDay,
  379. $title,
  380. $content,
  381. $sentTo,
  382. false,
  383. $eventId
  384. );
  385. $next_start = $this->addYear($next_start);
  386. }
  387. break;
  388. }
  389. }
  390. return true;
  391. }
  392. /**
  393. * @param int $item_id
  394. * @param array $sentTo
  395. * @return int
  396. */
  397. public function store_agenda_item_as_announcement($item_id, $sentTo = array())
  398. {
  399. $table_agenda = Database::get_course_table(TABLE_AGENDA);
  400. $course_id = api_get_course_int_id();
  401. // Check params
  402. if (empty($item_id) or $item_id != strval(intval($item_id))) {
  403. return -1;
  404. }
  405. // Get the agenda item.
  406. $item_id = Database::escape_string($item_id);
  407. $sql = "SELECT * FROM $table_agenda WHERE c_id = $course_id AND id = ".$item_id;
  408. $res = Database::query($sql);
  409. if (Database::num_rows($res) > 0) {
  410. $row = Database::fetch_array($res, 'ASSOC');
  411. // Sending announcement
  412. if (!empty($sentTo)) {
  413. $id = AnnouncementManager::add_announcement(
  414. $row['title'],
  415. $row['content'],
  416. $sentTo,
  417. null,
  418. null,
  419. $row['end_date']
  420. );
  421. AnnouncementManager::send_email($id);
  422. }
  423. return $id;
  424. }
  425. return -1;
  426. }
  427. /**
  428. * Edits an event
  429. *
  430. * @param int $id
  431. * @param string $start datetime format: 2012-06-14 09:00:00
  432. * @param string $end datetime format: 2012-06-14 09:00:00
  433. * @param int $allDay is all day 'true' or 'false'
  434. * @param string $title
  435. * @param string $content
  436. * @param array $usersToSend
  437. * @param int $editRepeatType
  438. * @param array $attachmentArray
  439. * @param string $attachmentComment
  440. *
  441. * @return bool
  442. */
  443. public function edit_event(
  444. $id,
  445. $start,
  446. $end,
  447. $allDay,
  448. $title,
  449. $content,
  450. $usersToSend = array(),
  451. $attachmentArray = array(),
  452. $attachmentComment = null
  453. ) {
  454. $start = api_get_utc_datetime($start);
  455. $end = api_get_utc_datetime($end);
  456. $allDay = isset($allDay) && $allDay == 'true' ? 1 : 0;
  457. switch ($this->type) {
  458. case 'personal':
  459. $eventInfo = $this->get_event($id);
  460. if ($eventInfo['user'] != api_get_user_id()) {
  461. break;
  462. }
  463. $attributes = array(
  464. 'title' => $title,
  465. 'text' => $content,
  466. 'date' => $start,
  467. 'enddate' => $end,
  468. 'all_day' => $allDay
  469. );
  470. Database::update(
  471. $this->tbl_personal_agenda,
  472. $attributes,
  473. array('id = ?' => $id)
  474. );
  475. break;
  476. case 'course':
  477. $eventInfo = $this->get_event($id);
  478. if (empty($eventInfo)) {
  479. return false;
  480. }
  481. $groupId = api_get_group_id();
  482. $course_id = api_get_course_int_id();
  483. if (empty($course_id)) {
  484. return false;
  485. }
  486. if (api_is_allowed_to_edit(null, true)) {
  487. $attributes = array(
  488. 'title' => $title,
  489. 'content' => $content,
  490. 'start_date' => $start,
  491. 'end_date' => $end,
  492. 'all_day' => $allDay
  493. );
  494. Database::update(
  495. $this->tbl_course_agenda,
  496. $attributes,
  497. array(
  498. 'id = ? AND c_id = ? AND session_id = ? ' => array($id, $course_id, api_get_session_id())
  499. )
  500. );
  501. if (!empty($usersToSend)) {
  502. $sendTo = $this->parseSendToArray($usersToSend);
  503. $usersToDelete = array_diff($eventInfo['send_to']['users'], $sendTo['users']);
  504. $usersToAdd = array_diff($sendTo['users'], $eventInfo['send_to']['users']);
  505. $groupsToDelete = array_diff($eventInfo['send_to']['groups'], $sendTo['groups']);
  506. $groupToAdd = array_diff($sendTo['groups'], $eventInfo['send_to']['groups']);
  507. if ($sendTo['everyone']) {
  508. // Delete all:
  509. if (!empty($eventInfo['send_to']['groups']) &&
  510. isset($eventInfo['send_to']['groups'])
  511. ) {
  512. foreach ($eventInfo['send_to']['groups'] as $group) {
  513. api_item_property_update(
  514. $this->course,
  515. TOOL_CALENDAR_EVENT,
  516. $id,
  517. "delete",
  518. api_get_user_id(),
  519. $group,
  520. 0,
  521. $start,
  522. $end,
  523. $this->sessionId
  524. );
  525. }
  526. }
  527. // storing the selected users
  528. if (!empty($eventInfo['send_to']['users']) &&
  529. isset($eventInfo['send_to']['users'])
  530. ) {
  531. foreach ($eventInfo['send_to']['users'] as $userId) {
  532. api_item_property_update(
  533. $this->course,
  534. TOOL_CALENDAR_EVENT,
  535. $id,
  536. "delete",
  537. api_get_user_id(),
  538. $groupId,
  539. $userId,
  540. $start,
  541. $end,
  542. $this->sessionId
  543. );
  544. }
  545. }
  546. // Add to everyone only.
  547. api_item_property_update(
  548. $this->course,
  549. TOOL_CALENDAR_EVENT,
  550. $id,
  551. "visible",
  552. api_get_user_id(),
  553. $groupId,
  554. '',
  555. $start,
  556. $end,
  557. $this->sessionId
  558. );
  559. } else {
  560. // Groups
  561. if (!empty($groupToAdd)) {
  562. foreach ($groupToAdd as $group) {
  563. api_item_property_update(
  564. $this->course,
  565. TOOL_CALENDAR_EVENT,
  566. $id,
  567. "visible",
  568. api_get_user_id(),
  569. $group,
  570. 0,
  571. $start,
  572. $end,
  573. $this->sessionId
  574. );
  575. }
  576. }
  577. if (!empty($groupsToDelete)) {
  578. foreach ($groupsToDelete as $group) {
  579. api_item_property_update(
  580. $this->course,
  581. TOOL_CALENDAR_EVENT,
  582. $id,
  583. "delete",
  584. api_get_user_id(),
  585. $group,
  586. 0,
  587. $start,
  588. $end,
  589. $this->sessionId
  590. );
  591. }
  592. }
  593. // Users.
  594. if (!empty($usersToAdd)) {
  595. foreach ($usersToAdd as $userId) {
  596. api_item_property_update(
  597. $this->course,
  598. TOOL_CALENDAR_EVENT,
  599. $id,
  600. "visible",
  601. api_get_user_id(),
  602. $groupId,
  603. $userId,
  604. $start,
  605. $end,
  606. $this->sessionId
  607. );
  608. }
  609. }
  610. if (!empty($usersToDelete)) {
  611. foreach ($usersToDelete as $userId) {
  612. api_item_property_update(
  613. $this->course,
  614. TOOL_CALENDAR_EVENT,
  615. $id,
  616. "delete",
  617. api_get_user_id(),
  618. $groupId,
  619. $userId,
  620. $start,
  621. $end,
  622. $this->sessionId
  623. );
  624. }
  625. }
  626. }
  627. }
  628. // Add announcement.
  629. /*if (isset($addAsAnnouncement) && !empty($addAsAnnouncement)) {
  630. $this->store_agenda_item_as_announcement($id);
  631. }*/
  632. // Add attachment.
  633. if (isset($attachmentArray) && !empty($attachmentArray)) {
  634. $this->updateAttachment(
  635. $id,
  636. $attachmentArray,
  637. $attachmentComment,
  638. $this->course
  639. );
  640. }
  641. }
  642. break;
  643. case 'admin':
  644. case 'platform':
  645. if (api_is_platform_admin()) {
  646. $attributes = array(
  647. 'title' => $title,
  648. 'content' => $content,
  649. 'start_date' => $start,
  650. 'end_date' => $end,
  651. 'all_day' => $allDay
  652. );
  653. Database::update(
  654. $this->tbl_global_agenda,
  655. $attributes,
  656. array('id = ?' => $id)
  657. );
  658. }
  659. break;
  660. }
  661. }
  662. /**
  663. * @param int $id
  664. * @param bool $deleteAllItemsFromSerie
  665. */
  666. public function delete_event($id, $deleteAllItemsFromSerie = false)
  667. {
  668. switch ($this->type) {
  669. case 'personal':
  670. $eventInfo = $this->get_event($id);
  671. if ($eventInfo['user'] == api_get_user_id()) {
  672. Database::delete(
  673. $this->tbl_personal_agenda,
  674. array('id = ?' => $id)
  675. );
  676. }
  677. break;
  678. case 'course':
  679. $course_id = api_get_course_int_id();
  680. if (!empty($course_id) && api_is_allowed_to_edit(null, true)) {
  681. // Delete
  682. if ($deleteAllItemsFromSerie) {
  683. $eventInfo = $this->get_event($id);
  684. /* This is one of the children.
  685. Getting siblings and delete 'Em all + the father! */
  686. if (isset($eventInfo['parent_event_id']) && !empty($eventInfo['parent_event_id'])) {
  687. // Removing items.
  688. $events = $this->getAllRepeatEvents($eventInfo['parent_event_id']);
  689. if (!empty($events)) {
  690. foreach ($events as $event) {
  691. $this->delete_event($event['id']);
  692. }
  693. }
  694. // Removing parent.
  695. $this->delete_event($eventInfo['parent_event_id']);
  696. } else {
  697. // This is the father looking for the children.
  698. $events = $this->getAllRepeatEvents($id);
  699. if (!empty($events)) {
  700. foreach ($events as $event) {
  701. $this->delete_event($event['id']);
  702. }
  703. }
  704. }
  705. }
  706. // Removing from events.
  707. Database::delete(
  708. $this->tbl_course_agenda,
  709. array('id = ? AND c_id = ?' => array($id, $course_id))
  710. );
  711. api_item_property_update(
  712. $this->course,
  713. TOOL_CALENDAR_EVENT,
  714. $id,
  715. 'delete',
  716. api_get_user_id()
  717. );
  718. // Removing from series.
  719. Database::delete(
  720. $this->table_repeat,
  721. array('cal_id = ? AND c_id = ?' => array($id, $course_id))
  722. );
  723. }
  724. break;
  725. case 'admin':
  726. if (api_is_platform_admin()) {
  727. Database::delete(
  728. $this->tbl_global_agenda,
  729. array('id = ?' => $id)
  730. );
  731. }
  732. break;
  733. }
  734. }
  735. /**
  736. * Get agenda events
  737. * @param int $start
  738. * @param int $end
  739. * @param int $course_id
  740. * @param int $groupId
  741. * @param int $user_id
  742. * @param string $format
  743. * @return array|string
  744. */
  745. public function get_events($start, $end, $course_id = null, $groupId = null, $user_id = 0, $format = 'json')
  746. {
  747. switch ($this->type) {
  748. case 'admin':
  749. $this->get_platform_events($start, $end);
  750. break;
  751. case 'course':
  752. $session_id = api_get_session_id();
  753. $courseInfo = api_get_course_info_by_id($course_id);
  754. $this->get_course_events(
  755. $start,
  756. $end,
  757. $courseInfo,
  758. $groupId,
  759. $session_id,
  760. $user_id
  761. );
  762. break;
  763. case 'personal':
  764. default:
  765. // Getting personal events
  766. $this->get_personal_events($start, $end);
  767. // Getting platform/admin events
  768. $this->get_platform_events($start, $end);
  769. // Getting course events
  770. $my_course_list = array();
  771. if (!api_is_anonymous()) {
  772. $session_list = SessionManager::get_sessions_by_user(api_get_user_id());
  773. $my_course_list = CourseManager::get_courses_list_by_user_id(api_get_user_id(), true);
  774. }
  775. if (!empty($session_list)) {
  776. foreach ($session_list as $session_item) {
  777. $my_courses = $session_item['courses'];
  778. $my_session_id = $session_item['session_id'];
  779. if (!empty($my_courses)) {
  780. foreach ($my_courses as $course_item) {
  781. $courseInfo = api_get_course_info($course_item['code']);
  782. $this->get_course_events($start, $end, $courseInfo, 0, $my_session_id);
  783. }
  784. }
  785. }
  786. }
  787. if (!empty($my_course_list)) {
  788. foreach ($my_course_list as $course_info_item) {
  789. if (isset($course_id) && !empty($course_id)) {
  790. if ($course_info_item['real_id'] == $course_id) {
  791. $this->get_course_events($start, $end, $course_info_item);
  792. }
  793. } else {
  794. $this->get_course_events($start, $end, $course_info_item);
  795. }
  796. }
  797. }
  798. break;
  799. }
  800. if (!empty($this->events)) {
  801. switch ($format) {
  802. case 'json':
  803. return json_encode($this->events);
  804. break;
  805. case 'array':
  806. return $this->events;
  807. break;
  808. }
  809. }
  810. return '';
  811. }
  812. /**
  813. * @param int $id
  814. * @param int $day_delta
  815. * @param int $minute_delta
  816. * @return int
  817. */
  818. public function resize_event($id, $day_delta, $minute_delta)
  819. {
  820. // we convert the hour delta into minutes and add the minute delta
  821. $delta = ($day_delta * 60 * 24) + $minute_delta;
  822. $delta = intval($delta);
  823. $event = $this->get_event($id);
  824. if (!empty($event)) {
  825. switch ($this->type) {
  826. case 'personal':
  827. $sql = "UPDATE $this->tbl_personal_agenda SET all_day = 0, enddate = DATE_ADD(enddate, INTERVAL $delta MINUTE)
  828. WHERE id=".intval($id);
  829. Database::query($sql);
  830. break;
  831. case 'course':
  832. $sql = "UPDATE $this->tbl_course_agenda SET all_day = 0, end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  833. WHERE c_id = ".$this->course['real_id']." AND id=".intval($id);
  834. Database::query($sql);
  835. break;
  836. case 'admin':
  837. $sql = "UPDATE $this->tbl_global_agenda SET all_day = 0, end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  838. WHERE id=".intval($id);
  839. Database::query($sql);
  840. break;
  841. }
  842. }
  843. return 1;
  844. }
  845. /**
  846. * @param $id
  847. * @param $day_delta
  848. * @param $minute_delta
  849. * @return int
  850. */
  851. public function move_event($id, $day_delta, $minute_delta)
  852. {
  853. // we convert the hour delta into minutes and add the minute delta
  854. $delta = ($day_delta * 60 * 24) + $minute_delta;
  855. $delta = intval($delta);
  856. $event = $this->get_event($id);
  857. $allDay = 0;
  858. if ($day_delta == 0 && $minute_delta == 0) {
  859. $allDay = 1;
  860. }
  861. if (!empty($event)) {
  862. switch ($this->type) {
  863. case 'personal':
  864. $sql = "UPDATE $this->tbl_personal_agenda SET all_day = $allDay, date = DATE_ADD(date, INTERVAL $delta MINUTE), enddate = DATE_ADD(enddate, INTERVAL $delta MINUTE)
  865. WHERE id=".intval($id);
  866. $result = Database::query($sql);
  867. break;
  868. case 'course':
  869. $sql = "UPDATE $this->tbl_course_agenda SET all_day = $allDay, start_date = DATE_ADD(start_date,INTERVAL $delta MINUTE), end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  870. WHERE c_id = ".$this->course['real_id']." AND id=".intval($id);
  871. $result = Database::query($sql);
  872. break;
  873. case 'admin':
  874. $sql = "UPDATE $this->tbl_global_agenda SET all_day = $allDay, start_date = DATE_ADD(start_date,INTERVAL $delta MINUTE), end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  875. WHERE id=".intval($id);
  876. $result = Database::query($sql);
  877. break;
  878. }
  879. }
  880. return 1;
  881. }
  882. /**
  883. * Gets a single event
  884. *
  885. * @param int event id
  886. * @return array
  887. */
  888. public function get_event($id)
  889. {
  890. // make sure events of the personal agenda can only be seen by the user himself
  891. $id = intval($id);
  892. $event = null;
  893. switch ($this->type) {
  894. case 'personal':
  895. $sql = "SELECT * FROM ".$this->tbl_personal_agenda."
  896. WHERE id = $id AND user = ".api_get_user_id();
  897. $result = Database::query($sql);
  898. if (Database::num_rows($result)) {
  899. $event = Database::fetch_array($result, 'ASSOC');
  900. $event['description'] = $event['text'];
  901. $event['content'] = $event['text'];
  902. $event['start_date'] = $event['date'];
  903. $event['end_date'] = $event['enddate'];
  904. }
  905. break;
  906. case 'course':
  907. if (!empty($this->course['real_id'])) {
  908. $sql = "SELECT * FROM ".$this->tbl_course_agenda."
  909. WHERE c_id = ".$this->course['real_id']." AND id = ".$id;
  910. $result = Database::query($sql);
  911. if (Database::num_rows($result)) {
  912. $event = Database::fetch_array($result, 'ASSOC');
  913. $event['description'] = $event['content'];
  914. // Getting send to array
  915. $event['send_to'] = $this->getUsersAndGroupSubscribedToEvent(
  916. $id,
  917. $this->course['real_id'],
  918. $this->sessionId
  919. );
  920. // Getting repeat info
  921. $event['repeat_info'] = $this->getRepeatedInfoByEvent(
  922. $id,
  923. $this->course['real_id']
  924. );
  925. if (!empty($event['parent_event_id'])) {
  926. $event['parent_info'] = $this->get_event($event['parent_event_id']);
  927. }
  928. $event['attachment'] = $this->getAttachment($id, $this->course);
  929. }
  930. }
  931. break;
  932. case 'admin':
  933. case 'platform':
  934. $sql = "SELECT * FROM ".$this->tbl_global_agenda."
  935. WHERE id = ".$id;
  936. $result = Database::query($sql);
  937. if (Database::num_rows($result)) {
  938. $event = Database::fetch_array($result, 'ASSOC');
  939. $event['description'] = $event['content'];
  940. }
  941. break;
  942. }
  943. return $event;
  944. }
  945. /**
  946. * Gets personal events
  947. * @param int $start
  948. * @param int $end
  949. * @return array
  950. */
  951. public function get_personal_events($start, $end)
  952. {
  953. $start = api_get_utc_datetime(intval($start));
  954. $end = api_get_utc_datetime(intval($end));
  955. $user_id = api_get_user_id();
  956. $sql = "SELECT * FROM ".$this->tbl_personal_agenda."
  957. WHERE date >= '".$start."' AND (enddate <='".$end."' OR enddate IS NULL) AND user = $user_id";
  958. $result = Database::query($sql);
  959. $my_events = array();
  960. if (Database::num_rows($result)) {
  961. while ($row = Database::fetch_array($result, 'ASSOC')) {
  962. $event = array();
  963. $event['id'] = 'personal_'.$row['id'];
  964. $event['title'] = $row['title'];
  965. $event['className'] = 'personal';
  966. $event['borderColor'] = $event['backgroundColor'] = $this->event_personal_color;
  967. $event['editable'] = true;
  968. $event['sent_to'] = get_lang('Me');
  969. $event['type'] = 'personal';
  970. if (!empty($row['date']) && $row['date'] != '0000-00-00 00:00:00') {
  971. $event['start'] = $this->format_event_date($row['date']);
  972. $event['start_date_localtime'] = api_get_local_time($row['date']);
  973. }
  974. if (!empty($row['enddate']) && $row['enddate'] != '0000-00-00 00:00:00') {
  975. $event['end'] = $this->format_event_date($row['enddate']);
  976. $event['end_date_localtime'] = api_get_local_time($row['enddate']);
  977. }
  978. $event['description'] = $row['text'];
  979. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  980. $event['parent_event_id'] = 0;
  981. $event['has_children'] = 0;
  982. $my_events[] = $event;
  983. $this->events[] = $event;
  984. }
  985. }
  986. return $my_events;
  987. }
  988. /**
  989. * Get user/group list per event.
  990. *
  991. * @param int $eventId
  992. * @param int $courseId
  993. * @paraù int $sessionId
  994. *
  995. * @return array
  996. */
  997. public function getUsersAndGroupSubscribedToEvent($eventId, $courseId, $sessionId)
  998. {
  999. $eventId = intval($eventId);
  1000. $courseId = intval($courseId);
  1001. $sessionId = intval($sessionId);
  1002. $tlb_course_agenda = Database::get_course_table(TABLE_AGENDA);
  1003. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1004. // Get sent_tos
  1005. $sql = "SELECT DISTINCT to_user_id, to_group_id
  1006. FROM $tbl_property ip
  1007. INNER JOIN $tlb_course_agenda agenda
  1008. ON (ip.ref = agenda.id AND ip.c_id = agenda.c_id)
  1009. WHERE
  1010. ip.tool = '".TOOL_CALENDAR_EVENT."' AND
  1011. ref = $eventId AND
  1012. ip.visibility = '1' AND
  1013. ip.c_id = $courseId AND
  1014. ip.id_session = $sessionId
  1015. ";
  1016. $result = Database::query($sql);
  1017. $users = array();
  1018. $groups = array();
  1019. $everyone = false;
  1020. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1021. if (!empty($row['to_group_id'])) {
  1022. $groups[] = $row['to_group_id'];
  1023. }
  1024. if (!empty($row['to_user_id'])) {
  1025. $users[] = $row['to_user_id'];
  1026. }
  1027. if (empty($groups) && empty($users)) {
  1028. if ($row['to_group_id'] == 0) {
  1029. $everyone = true;
  1030. }
  1031. }
  1032. }
  1033. return array(
  1034. 'everyone' => $everyone,
  1035. 'users' => $users,
  1036. 'groups' => $groups
  1037. );
  1038. }
  1039. /**
  1040. * @param int $start
  1041. * @param int $end
  1042. * @param array $courseInfo
  1043. * @param int $groupId
  1044. * @param int $session_id
  1045. * @param int $user_id
  1046. * @return array
  1047. */
  1048. public function get_course_events($start, $end, $courseInfo, $groupId = 0, $session_id = 0, $user_id = 0)
  1049. {
  1050. $start = isset($start) && !empty($start) ? api_get_utc_datetime(intval($start)) : null;
  1051. $end = isset($end) && !empty($end) ? api_get_utc_datetime(intval($end)) : null;
  1052. if (empty($courseInfo)) {
  1053. return array();
  1054. }
  1055. $course_id = $courseInfo['real_id'];
  1056. if (empty($course_id)) {
  1057. return array();
  1058. }
  1059. $user_id = intval($user_id);
  1060. $groupList = GroupManager::get_group_list(null, $courseInfo['code']);
  1061. $group_name_list = array();
  1062. if (!empty($groupList)) {
  1063. foreach ($groupList as $group) {
  1064. $group_name_list[$group['id']] = $group['name'];
  1065. }
  1066. }
  1067. if (!api_is_allowed_to_edit()) {
  1068. $group_memberships = GroupManager::get_group_ids($course_id, api_get_user_id());
  1069. $user_id = api_get_user_id();
  1070. } else {
  1071. $group_memberships = GroupManager::get_group_ids($course_id, $user_id);
  1072. }
  1073. $tlb_course_agenda = Database::get_course_table(TABLE_AGENDA);
  1074. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1075. if (!empty($groupId)) {
  1076. $group_memberships = array($groupId);
  1077. }
  1078. $session_id = intval($session_id);
  1079. if (is_array($group_memberships) && count($group_memberships) > 0) {
  1080. if (api_is_allowed_to_edit()) {
  1081. if (!empty($groupId)) {
  1082. $where_condition = "( ip.to_group_id IN (0, ".implode(", ", $group_memberships).") ) ";
  1083. } else {
  1084. if (!empty($user_id)) {
  1085. $where_condition = "( ip.to_user_id = $user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") ) ";
  1086. } else {
  1087. $where_condition = "( ip.to_group_id is null OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") ) ";
  1088. }
  1089. }
  1090. } else {
  1091. $where_condition = "( ip.to_user_id = $user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") ) ";
  1092. }
  1093. $sql = "SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref, to_user_id
  1094. FROM $tlb_course_agenda agenda INNER JOIN $tbl_property ip
  1095. ON (agenda.id = ip.ref AND agenda.c_id = ip.c_id)
  1096. WHERE
  1097. ip.tool ='".TOOL_CALENDAR_EVENT."' AND
  1098. $where_condition AND
  1099. ip.visibility = '1' AND
  1100. agenda.c_id = $course_id AND
  1101. ip.c_id = $course_id
  1102. ";
  1103. } else {
  1104. $visibilityCondition = "ip.visibility='1' AND";
  1105. if (api_is_allowed_to_edit()) {
  1106. if ($user_id == 0) {
  1107. $where_condition = "";
  1108. } else {
  1109. $where_condition = "( ip.to_user_id=".$user_id. ") AND ";
  1110. }
  1111. $visibilityCondition = " (ip.visibility IN ('1', '0')) AND ";
  1112. } else {
  1113. $where_condition = "( ip.to_user_id=$user_id OR ip.to_group_id='0') AND ";
  1114. }
  1115. $sql = "SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref, to_user_id
  1116. FROM $tlb_course_agenda agenda INNER JOIN $tbl_property ip
  1117. ON (agenda.id = ip.ref AND agenda.c_id = ip.c_id)
  1118. WHERE
  1119. ip.tool='".TOOL_CALENDAR_EVENT."' AND
  1120. $where_condition
  1121. $visibilityCondition
  1122. agenda.c_id = $course_id AND
  1123. ip.c_id = $course_id AND
  1124. agenda.session_id = $session_id AND
  1125. ip.id_session = $session_id
  1126. ";
  1127. }
  1128. $dateCondition = null;
  1129. if (!empty($start) && !empty($end)) {
  1130. $dateCondition .= "AND (
  1131. (agenda.start_date >= '".$start."' OR agenda.start_date IS NULL) AND
  1132. (agenda.end_date <= '".$end."' OR agenda.end_date IS NULL)
  1133. )";
  1134. }
  1135. $sql .= $dateCondition;
  1136. $result = Database::query($sql);
  1137. if (Database::num_rows($result)) {
  1138. $events_added = array();
  1139. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1140. $eventId = $row['ref'];
  1141. $items = $this->getUsersAndGroupSubscribedToEvent($eventId, $course_id, $this->sessionId);
  1142. $group_to_array = $items['groups'];
  1143. $user_to_array = $items['users'];
  1144. $event = array();
  1145. $event['id'] = 'course_'.$row['id'];
  1146. // To avoid doubles
  1147. if (in_array($row['id'], $events_added)) {
  1148. continue;
  1149. }
  1150. $events_added[] = $row['id'];
  1151. $attachment = $this->getAttachment($row['id'], $courseInfo);
  1152. if (!empty($attachment)) {
  1153. $has_attachment = Display::return_icon('attachment.gif', get_lang('Attachment'));
  1154. $user_filename = $attachment['filename'];
  1155. $url = api_get_path(WEB_CODE_PATH).'calendar/download.php?file='.$attachment['path'].'&course_id='.$course_id.'&'.api_get_cidreq();
  1156. $event['attachment'] = $has_attachment.Display::url($user_filename, $url);
  1157. } else {
  1158. $event['attachment'] = '';
  1159. }
  1160. $event['title'] = $row['title'];
  1161. $event['className'] = 'course';
  1162. $event['allDay'] = 'false';
  1163. $event['course_id'] = $course_id;
  1164. $event['borderColor'] = $event['backgroundColor'] = $this->event_course_color;
  1165. if (isset($row['session_id']) && !empty($row['session_id'])) {
  1166. $event['borderColor'] = $event['backgroundColor'] = $this->event_session_color;
  1167. }
  1168. if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
  1169. $event['borderColor'] = $event['backgroundColor'] = $this->event_group_color;
  1170. }
  1171. $event['editable'] = false;
  1172. if (api_is_allowed_to_edit() && $this->type == 'course') {
  1173. $event['editable'] = true;
  1174. }
  1175. if (!empty($row['start_date']) && $row['start_date'] != '0000-00-00 00:00:00') {
  1176. $event['start'] = $this->format_event_date($row['start_date']);
  1177. $event['start_date_localtime'] = api_get_local_time($row['start_date']);
  1178. }
  1179. if (!empty($row['end_date']) && $row['end_date'] != '0000-00-00 00:00:00') {
  1180. $event['end'] = $this->format_event_date($row['end_date']);
  1181. $event['end_date_localtime'] = api_get_local_time($row['end_date']);
  1182. }
  1183. $event['sent_to'] = '';
  1184. $event['type'] = 'course';
  1185. if ($row['session_id'] != 0) {
  1186. $event['type'] = 'session';
  1187. }
  1188. // Event Sent to a group?
  1189. if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
  1190. $sent_to = array();
  1191. if (!empty($group_to_array)) {
  1192. foreach ($group_to_array as $group_item) {
  1193. $sent_to[] = $group_name_list[$group_item];
  1194. }
  1195. }
  1196. $sent_to = implode('@@', $sent_to);
  1197. $sent_to = str_replace('@@', '</div><div class="label_tag notice">', $sent_to);
  1198. $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
  1199. $event['type'] = 'group';
  1200. }
  1201. //Event sent to a user?
  1202. if (isset($row['to_user_id'])) {
  1203. $sent_to = array();
  1204. if (!empty($user_to_array)) {
  1205. foreach ($user_to_array as $item) {
  1206. $user_info = api_get_user_info($item);
  1207. // Add username as tooltip for $event['sent_to'] - ref #4226
  1208. $username = api_htmlentities(sprintf(get_lang('LoginX'), $user_info['username']), ENT_QUOTES);
  1209. $sent_to[] = "<span title='".$username."'>".$user_info['complete_name']."</span>";
  1210. }
  1211. }
  1212. $sent_to = implode('@@', $sent_to);
  1213. $sent_to = str_replace('@@', '</div><div class="label_tag notice">', $sent_to);
  1214. $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
  1215. }
  1216. //Event sent to everyone!
  1217. if (empty($event['sent_to'])) {
  1218. $event['sent_to'] = '<div class="label_tag notice">'.get_lang('Everyone').'</div>';
  1219. }
  1220. $event['description'] = $row['content'];
  1221. $event['visibility'] = $row['visibility'];
  1222. $event['real_id'] = $row['id'];
  1223. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  1224. $event['parent_event_id'] = $row['parent_event_id'];
  1225. $event['has_children'] = $this->hasChildren($row['id'], $course_id) ? 1 : 0;
  1226. $this->events[] = $event;
  1227. }
  1228. }
  1229. return $this->events;
  1230. }
  1231. /**
  1232. * @param int $start tms
  1233. * @param int $end tms
  1234. * @return array
  1235. */
  1236. public function get_platform_events($start, $end)
  1237. {
  1238. $start = intval($start);
  1239. $end = intval($end);
  1240. $start = api_get_utc_datetime($start);
  1241. $end = api_get_utc_datetime($end);
  1242. $access_url_id = api_get_current_access_url_id();
  1243. $sql = "SELECT * FROM ".$this->tbl_global_agenda."
  1244. WHERE start_date >= '".$start."' AND end_date <= '".$end."' AND access_url_id = $access_url_id ";
  1245. $result = Database::query($sql);
  1246. $my_events = array();
  1247. if (Database::num_rows($result)) {
  1248. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1249. $event = array();
  1250. $event['id'] = 'platform_'.$row['id'];
  1251. $event['title'] = $row['title'];
  1252. $event['className'] = 'platform';
  1253. $event['allDay'] = 'false';
  1254. $event['borderColor'] = $event['backgroundColor'] = $this->event_platform_color;
  1255. $event['editable'] = false;
  1256. $event['type'] = 'admin';
  1257. if (api_is_platform_admin() && $this->type == 'admin') {
  1258. $event['editable'] = true;
  1259. }
  1260. if (!empty($row['start_date']) && $row['start_date'] != '0000-00-00 00:00:00') {
  1261. $event['start'] = $this->format_event_date($row['start_date']);
  1262. $event['start_date_localtime'] = api_get_local_time($row['start_date']);
  1263. }
  1264. if (!empty($row['end_date']) && $row['end_date'] != '0000-00-00 00:00:00') {
  1265. $event['end'] = $this->format_event_date($row['end_date']);
  1266. $event['end_date_localtime'] = api_get_local_time($row['end_date']);
  1267. }
  1268. $event['description'] = $row['content'];
  1269. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  1270. $event['parent_event_id'] = 0;
  1271. $event['has_children'] = 0;
  1272. $my_events[] = $event;
  1273. $this->events[] = $event;
  1274. }
  1275. }
  1276. return $my_events;
  1277. }
  1278. /**
  1279. * Format needed for the Fullcalendar js lib
  1280. *
  1281. * @param string $utc_time
  1282. * @return bool|string
  1283. */
  1284. function format_event_date($utc_time)
  1285. {
  1286. return date('c', api_strtotime(api_get_local_time($utc_time)));
  1287. }
  1288. /**
  1289. * this function shows the form with the user that were not selected
  1290. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1291. * @return string code
  1292. */
  1293. public static function construct_not_selected_select_form($group_list = null, $user_list = null, $to_already_selected = array())
  1294. {
  1295. $html = '<select id="users_to_send_id" data-placeholder="'.get_lang('Select').'" name="users_to_send[]" multiple="multiple" style="width:250px" class="chzn-select">';
  1296. if ($to_already_selected == 'everyone') {
  1297. $html .= '<option value="everyone" checked="checked">'.get_lang('Everyone').'</option>';
  1298. } else {
  1299. $html .= '<option value="everyone">'.get_lang('Everyone').'</option>';
  1300. }
  1301. if (is_array($group_list)) {
  1302. $html .= '<optgroup label="'.get_lang('Groups').'">';
  1303. foreach ($group_list as $this_group) {
  1304. if (!is_array($to_already_selected) || !in_array("GROUP:".$this_group['id'], $to_already_selected)) {
  1305. // $to_already_selected is the array containing the groups (and users) that are already selected
  1306. $count_users = isset($this_group['count_users']) ? $this_group['count_users'] : $this_group['userNb'];
  1307. $count_users = " &ndash; $count_users ".get_lang('Users');
  1308. $html .= '<option value="GROUP:'.$this_group['id'].'"> '.$this_group['name'].$count_users.'</option>';
  1309. }
  1310. }
  1311. $html .= '</optgroup>';
  1312. }
  1313. // adding the individual users to the select form
  1314. if (is_array($group_list)) {
  1315. $html .= '<optgroup label="'.get_lang('Users').'">';
  1316. }
  1317. foreach ($user_list as $this_user) {
  1318. // $to_already_selected is the array containing the users (and groups) that are already selected
  1319. if (!is_array($to_already_selected) || !in_array("USER:".$this_user['user_id'], $to_already_selected)) {
  1320. $username = api_htmlentities(sprintf(get_lang('LoginX'), $this_user['username']), ENT_QUOTES);
  1321. // @todo : add title attribute $username in the jqdialog window. wait for a chosen version to inherit title attribute
  1322. $html .= '<option title="'.$username.'" value="USER:'.$this_user['user_id'].'">'.api_get_person_name($this_user['firstname'], $this_user['lastname']).' ('.$this_user['username'].') </option>';
  1323. }
  1324. }
  1325. if (is_array($group_list)) {
  1326. $html .= '</optgroup>';
  1327. $html .= "</select>";
  1328. }
  1329. return $html;
  1330. }
  1331. /**
  1332. * @param FormValidator $form
  1333. * @param array $groupList
  1334. * @param array $userList
  1335. * @param array $sendTo array('users' => [1, 2], 'groups' => [3, 4])
  1336. * @param array $attributes
  1337. * @param bool $addOnlyItemsInSendTo
  1338. */
  1339. public function setSendToSelect(
  1340. $form,
  1341. $groupList = null,
  1342. $userList = null,
  1343. $sendTo = array(),
  1344. $attributes = array(),
  1345. $addOnlyItemsInSendTo = false
  1346. ) {
  1347. $params = array(
  1348. 'id' => 'users_to_send_id',
  1349. 'data-placeholder' => get_lang('Select'),
  1350. 'multiple' => 'multiple',
  1351. 'style' => 'width:250px',
  1352. 'class' => 'chzn-select'
  1353. );
  1354. if (!empty($attributes)) {
  1355. $params = array_merge($params, $attributes);
  1356. if (empty($params['multiple'])) {
  1357. unset($params['multiple']);
  1358. }
  1359. }
  1360. $sendToGroups = isset($sendTo['groups']) ? $sendTo['groups'] : array();
  1361. $sendToUsers = isset($sendTo['users']) ? $sendTo['users'] : array();
  1362. /** @var HTML_QuickForm_select $select */
  1363. $select = $form->addElement('select', 'users_to_send', get_lang('To'), null, $params);
  1364. $selectedEveryoneOptions = array();
  1365. if (isset($sendTo['everyone']) && $sendTo['everyone']) {
  1366. $selectedEveryoneOptions = array('selected');
  1367. $sendToUsers = array();
  1368. }
  1369. $select->addOption(get_lang('Everyone'), 'everyone', $selectedEveryoneOptions);
  1370. $options = array();
  1371. if (is_array($groupList)) {
  1372. foreach ($groupList as $group) {
  1373. $count_users = isset($group['count_users']) ? $group['count_users'] : $group['userNb'];
  1374. $count_users = " &ndash; $count_users ".get_lang('Users');
  1375. $option = array(
  1376. 'text' => $group['name'].$count_users,
  1377. 'value' => "GROUP:".$group['id']
  1378. );
  1379. $selected = in_array($group['id'], $sendToGroups) ? true : false;
  1380. if ($selected) {
  1381. $option['selected'] = 'selected';
  1382. }
  1383. if ($addOnlyItemsInSendTo) {
  1384. if ($selected) {
  1385. $options[] = $option;
  1386. }
  1387. } else {
  1388. $options[] = $option;
  1389. }
  1390. }
  1391. $select->addOptGroup($options, get_lang('Groups'));
  1392. }
  1393. // adding the individual users to the select form
  1394. if (is_array($userList)) {
  1395. $options = array();
  1396. foreach ($userList as $user) {
  1397. $option = array(
  1398. 'text' => api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')',
  1399. 'value' => "USER:".$user['user_id']
  1400. );
  1401. $selected = in_array($user['user_id'], $sendToUsers) ? true : false;
  1402. if ($selected) {
  1403. $option['selected'] = 'selected';
  1404. }
  1405. if ($addOnlyItemsInSendTo) {
  1406. if ($selected) {
  1407. $options[] = $option;
  1408. }
  1409. } else {
  1410. $options[] = $option;
  1411. }
  1412. }
  1413. $select->addOptGroup($options, get_lang('Users'));
  1414. }
  1415. }
  1416. /**
  1417. * Separates the users and groups array
  1418. * users have a value USER:XXX (with XXX the user id
  1419. * groups have a value GROUP:YYY (with YYY the group id)
  1420. * use the 'everyone' key
  1421. * @author Julio Montoya based in separate_users_groups in agenda.inc.php
  1422. * @param array $to
  1423. * @return array
  1424. */
  1425. public function parseSendToArray($to)
  1426. {
  1427. $groupList = array();
  1428. $userList = array();
  1429. $sendTo = null;
  1430. $sendTo['everyone'] = false;
  1431. if (is_array($to) && count($to) > 0) {
  1432. foreach ($to as $item) {
  1433. if ($item == 'everyone') {
  1434. $sendTo['everyone'] = true;
  1435. } else {
  1436. list($type, $id) = explode(':', $item);
  1437. switch ($type) {
  1438. case 'GROUP':
  1439. $groupList[] = $id;
  1440. break;
  1441. case 'USER':
  1442. $userList[] = $id;
  1443. break;
  1444. }
  1445. }
  1446. }
  1447. $sendTo['groups'] = $groupList;
  1448. $sendTo['users'] = $userList;
  1449. }
  1450. return $sendTo;
  1451. }
  1452. /**
  1453. * @param array $params
  1454. * @return FormValidator
  1455. */
  1456. public function getForm($params = array())
  1457. {
  1458. $action = isset($params['action']) ? Security::remove_XSS($params['action']) : null;
  1459. $id = isset($params['id']) ? intval($params['id']) : null;
  1460. if ($this->type == 'course') {
  1461. $url = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&id='.$id.'&type='.$this->type;
  1462. } else {
  1463. $url = api_get_self().'?action='.$action.'&id='.$id.'&type='.$this->type;
  1464. }
  1465. $form = new FormValidator(
  1466. 'add_event',
  1467. 'post',
  1468. $url,
  1469. null,
  1470. array('enctype' => 'multipart/form-data')
  1471. );
  1472. $idAttach = isset($params['id_attach']) ? intval($params['id_attach']) : null;
  1473. $groupId = api_get_group_id();
  1474. if ($id) {
  1475. $form_title = get_lang('ModifyCalendarItem');
  1476. $button = get_lang('ModifyEvent');
  1477. } else {
  1478. $form_title = get_lang('AddCalendarItem');
  1479. $button = get_lang('AgendaAdd');
  1480. }
  1481. $form->addElement('header', $form_title);
  1482. $form->addElement('hidden', 'id', $id);
  1483. $form->addElement('hidden', 'action', $action);
  1484. $form->addElement('hidden', 'id_attach', $idAttach);
  1485. $isSubEventEdition = false;
  1486. $isParentFromSerie = false;
  1487. $showAttachmentForm = true;
  1488. if ($this->type == 'course') {
  1489. // Edition mode.
  1490. if (!empty($id)) {
  1491. $showAttachmentForm = false;
  1492. if (isset($params['parent_event_id']) && !empty($params['parent_event_id'])) {
  1493. $isSubEventEdition = true;
  1494. }
  1495. if (!empty($params['repeat_info'])) {
  1496. $isParentFromSerie = true;
  1497. }
  1498. }
  1499. }
  1500. if ($isSubEventEdition) {
  1501. $form->addElement(
  1502. 'label',
  1503. null,
  1504. Display::return_message(get_lang('EditingThisEventWillRemoveItFromTheSerie'), 'warning')
  1505. );
  1506. }
  1507. $form->addElement('text', 'title', get_lang('ItemTitle'));
  1508. if (isset($groupId) && !empty($groupId)) {
  1509. $form->addElement('hidden', 'selected_form[0]', "GROUP:'.$groupId.'");
  1510. $form->addElement('hidden', 'to', 'true');
  1511. } else {
  1512. $sendTo = isset($params['send_to']) ? $params['send_to'] : null;
  1513. if ($this->type == 'course') {
  1514. $this->showToForm($form, $sendTo);
  1515. }
  1516. }
  1517. $form->addDateRangePicker('date_range', get_lang('StartDate'), false, array('id' => 'date_range'));
  1518. $form->addElement('checkbox', 'all_day', null, get_lang('AllDay'));
  1519. if ($this->type == 'course') {
  1520. $repeat = $form->addElement('checkbox', 'repeat', null, get_lang('RepeatEvent'), array('onclick' => 'return plus_repeated_event();'));
  1521. $form->addElement('html', '<div id="options2" style="display:none">');
  1522. $form->addElement('select', 'repeat_type', get_lang('RepeatType'), self::getRepeatTypes());
  1523. $form->addElement('date_picker', 'repeat_end_day', get_lang('RepeatEnd'), array('id' => 'repeat_end_date_form'));
  1524. if ($isSubEventEdition || $isParentFromSerie) {
  1525. if ($isSubEventEdition) {
  1526. $parentEvent = $params['parent_info'];
  1527. $repeatInfo = $parentEvent['repeat_info'];
  1528. } else {
  1529. $repeatInfo = $params['repeat_info'];
  1530. }
  1531. $params['repeat'] = 1;
  1532. $params['repeat_type'] = $repeatInfo['cal_type'];
  1533. $params['repeat_end_day'] = substr(api_get_local_time($repeatInfo['cal_end']), 0, 10);
  1534. $form->freeze(array('repeat_type', 'repeat_end_day'));
  1535. $repeat->_attributes['disabled'] = 'disabled';
  1536. }
  1537. $form->addElement('html', '</div>');
  1538. }
  1539. if (!empty($id)) {
  1540. if (empty($params['end_date'])) {
  1541. $params['date_range'] = $params['end_date'];
  1542. }
  1543. $params['date_range'] =
  1544. substr(api_get_local_time($params['start_date']), 0, 16).' / '.
  1545. substr(api_get_local_time($params['end_date']), 0, 16);
  1546. }
  1547. if (!api_is_allowed_to_edit(null, true)) {
  1548. $toolbar = 'AgendaStudent';
  1549. } else {
  1550. $toolbar = 'Agenda';
  1551. }
  1552. $form->addElement(
  1553. 'html_editor',
  1554. 'content',
  1555. get_lang('Description'),
  1556. null,
  1557. array('ToolbarSet' => $toolbar, 'Width' => '100%', 'Height' => '200')
  1558. );
  1559. if ($this->type == 'course') {
  1560. $form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
  1561. if ($showAttachmentForm) {
  1562. if (isset($params['attachment']) && !empty($params['attachment'])) {
  1563. $attachment = $params['attachment'];
  1564. $params['file_comment'] = $attachment['comment'];
  1565. if (!empty($attachment['path'])) {
  1566. $form->addElement(
  1567. 'checkbox',
  1568. 'delete_attachment',
  1569. null,
  1570. get_lang('DeleteAttachment').' '.$attachment['filename']
  1571. );
  1572. }
  1573. }
  1574. }
  1575. $form->addElement('textarea', 'file_comment', get_lang('Comment'));
  1576. }
  1577. if (empty($id)) {
  1578. $form->addElement(
  1579. 'checkbox',
  1580. 'add_announcement',
  1581. null,
  1582. get_lang('AddAnnouncement').'&nbsp('.get_lang('SendMail').')'
  1583. );
  1584. }
  1585. $form->addElement('button', 'submit', $button);
  1586. $form->setDefaults($params);
  1587. $form->addRule('date_range', get_lang('ThisFieldIsRequired'), 'required');
  1588. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  1589. return $form;
  1590. }
  1591. /**
  1592. * @param FormValidator $form
  1593. * @param array $sendTo array('everyone' => false, 'users' => [1, 2], 'groups' => [3, 4])
  1594. * @param array $attributes
  1595. * @param bool $addOnlyItemsInSendTo
  1596. * @return bool
  1597. */
  1598. public function showToForm(
  1599. $form,
  1600. $sendTo = array(),
  1601. $attributes = array(),
  1602. $addOnlyItemsInSendTo = false
  1603. ) {
  1604. if ($this->type != 'course') {
  1605. return false;
  1606. }
  1607. $order = 'lastname';
  1608. if (api_is_western_name_order()) {
  1609. $order = 'firstname';
  1610. }
  1611. $userList = CourseManager::get_user_list_from_course_code(
  1612. api_get_course_id(),
  1613. api_get_session_id(),
  1614. null,
  1615. $order
  1616. );
  1617. $groupList = CourseManager::get_group_list_of_course(
  1618. api_get_course_id(),
  1619. api_get_session_id()
  1620. );
  1621. $this->setSendToSelect(
  1622. $form,
  1623. $groupList,
  1624. $userList,
  1625. $sendTo,
  1626. $attributes,
  1627. $addOnlyItemsInSendTo
  1628. );
  1629. return true;
  1630. }
  1631. /**
  1632. * @param int $id
  1633. * @param int $visibility 0= invisible, 1 visible
  1634. * @param array $courseInfo
  1635. * @param int $userId
  1636. */
  1637. public static function changeVisibility($id, $visibility, $courseInfo, $userId = null)
  1638. {
  1639. $id = Database::escape_string($id);
  1640. if (empty($userId)) {
  1641. $userId = api_get_user_id();
  1642. } else {
  1643. $userId = intval($userId);
  1644. }
  1645. if ($visibility == 0) {
  1646. api_item_property_update($courseInfo, TOOL_CALENDAR_EVENT, $id, "invisible", $userId);
  1647. } else {
  1648. api_item_property_update($courseInfo, TOOL_CALENDAR_EVENT, $id, "visible", $userId);
  1649. }
  1650. }
  1651. /**
  1652. * Get repeat types
  1653. * @return array
  1654. */
  1655. public static function getRepeatTypes()
  1656. {
  1657. return array(
  1658. 'daily' => get_lang('RepeatDaily'),
  1659. 'weekly' => get_lang('RepeatWeekly'),
  1660. 'monthlyByDate' => get_lang('RepeatMonthlyByDate'),
  1661. //monthlyByDay"> get_lang('RepeatMonthlyByDay');
  1662. //monthlyByDayR' => get_lang('RepeatMonthlyByDayR'),
  1663. 'yearly' => get_lang('RepeatYearly')
  1664. );
  1665. }
  1666. /**
  1667. * Show a list with all the attachments according to the post's id
  1668. * @param int $eventId
  1669. * @param array $courseInfo
  1670. * @return array with the post info
  1671. */
  1672. public function getAttachment($eventId, $courseInfo)
  1673. {
  1674. $tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  1675. $courseId = intval($courseInfo['real_id']);
  1676. $eventId = Database::escape_string($eventId);
  1677. $row = array();
  1678. $sql = "SELECT id, path, filename, comment
  1679. FROM $tableAttachment
  1680. WHERE
  1681. c_id = $courseId AND
  1682. agenda_id = $eventId";
  1683. $result = Database::query($sql);
  1684. if (Database::num_rows($result) != 0) {
  1685. $row = Database::fetch_array($result, 'ASSOC');
  1686. }
  1687. return $row;
  1688. }
  1689. /**
  1690. * Add an attachment file into agenda
  1691. * @param int $eventId
  1692. * @param array $fileUserUpload ($_FILES['user_upload'])
  1693. * @param string comment about file
  1694. * @param array $courseInfo
  1695. * @return string
  1696. */
  1697. public function addAttachment($eventId, $fileUserUpload, $comment, $courseInfo)
  1698. {
  1699. $agenda_table_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  1700. $eventId = intval($eventId);
  1701. // Storing the attachments
  1702. $upload_ok = false;
  1703. if (!empty($fileUserUpload['name'])) {
  1704. $upload_ok = process_uploaded_file($fileUserUpload);
  1705. }
  1706. if (!empty($upload_ok)) {
  1707. $courseDir = $courseInfo['directory'].'/upload/calendar';
  1708. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1709. $uploadDir = $sys_course_path.$courseDir;
  1710. // Try to add an extension to the file if it hasn't one
  1711. $new_file_name = add_ext_on_mime(
  1712. stripslashes($fileUserUpload['name']),
  1713. $fileUserUpload['type']
  1714. );
  1715. // user's file name
  1716. $file_name = $fileUserUpload['name'];
  1717. if (!filter_extension($new_file_name)) {
  1718. return Display::return_message(
  1719. get_lang('UplUnableToSaveFileFilteredExtension'),
  1720. 'error'
  1721. );
  1722. } else {
  1723. $new_file_name = uniqid('');
  1724. $new_path = $uploadDir.'/'.$new_file_name;
  1725. $result = @move_uploaded_file($fileUserUpload['tmp_name'], $new_path);
  1726. $comment = Database::escape_string($comment);
  1727. $file_name = Database::escape_string($file_name);
  1728. $course_id = api_get_course_int_id();
  1729. $size = intval($fileUserUpload['size']);
  1730. // Storing the attachments if any
  1731. if ($result) {
  1732. $sql = 'INSERT INTO '.$agenda_table_attachment.'(c_id, filename, comment, path, agenda_id, size) '.
  1733. "VALUES ($course_id, '".$file_name."', '".$comment."', '".$new_file_name."' , '".$eventId."', '".$size."' )";
  1734. Database::query($sql);
  1735. $id = Database::insert_id();
  1736. api_item_property_update(
  1737. $courseInfo,
  1738. 'calendar_event_attachment',
  1739. $id,
  1740. 'AgendaAttachmentAdded',
  1741. api_get_user_id()
  1742. );
  1743. }
  1744. }
  1745. }
  1746. }
  1747. /**
  1748. * @param int $eventId
  1749. * @param array $fileUserUpload
  1750. * @param string $comment
  1751. * @param array $courseInfo
  1752. */
  1753. public function updateAttachment($eventId, $fileUserUpload, $comment, $courseInfo)
  1754. {
  1755. $attachment = $this->getAttachment($eventId, $courseInfo);
  1756. if (!empty($attachment)) {
  1757. $this->deleteAttachmentFile($attachment['id'], $courseInfo);
  1758. }
  1759. $this->addAttachment($eventId, $fileUserUpload, $comment, $courseInfo);
  1760. }
  1761. /**
  1762. * This function delete a attachment file by id
  1763. * @param int $attachmentId
  1764. * @param array $courseInfo
  1765. * @return string
  1766. */
  1767. public function deleteAttachmentFile($attachmentId, $courseInfo)
  1768. {
  1769. $agenda_table_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  1770. $attachmentId = intval($attachmentId);
  1771. $courseId = $courseInfo['real_id'];
  1772. if (empty($courseId) || empty($attachmentId)) {
  1773. return false;
  1774. }
  1775. $sql = "DELETE FROM $agenda_table_attachment
  1776. WHERE c_id = $courseId AND id = ".$attachmentId;
  1777. $result = Database::query($sql);
  1778. // update item_property
  1779. api_item_property_update(
  1780. $courseInfo,
  1781. 'calendar_event_attachment',
  1782. $attachmentId,
  1783. 'AgendaAttachmentDeleted',
  1784. api_get_user_id()
  1785. );
  1786. if (!empty($result)) {
  1787. return Display::return_message(get_lang("AttachmentFileDeleteSuccess"), 'confirmation');
  1788. }
  1789. }
  1790. /**
  1791. * Adds x weeks to a UNIX timestamp
  1792. * @param int The timestamp
  1793. * @param int The number of weeks to add
  1794. * @return int The new timestamp
  1795. */
  1796. function addWeek($timestamp, $num = 1)
  1797. {
  1798. return $timestamp + $num * 604800;
  1799. }
  1800. /**
  1801. * Adds x months to a UNIX timestamp
  1802. * @param int The timestamp
  1803. * @param int The number of years to add
  1804. * @return int The new timestamp
  1805. */
  1806. function addMonth($timestamp, $num = 1)
  1807. {
  1808. list($y, $m, $d, $h, $n, $s) = split('/', date('Y/m/d/h/i/s', $timestamp));
  1809. if ($m + $num > 12) {
  1810. $y += floor($num / 12);
  1811. $m += $num % 12;
  1812. } else {
  1813. $m += $num;
  1814. }
  1815. return mktime($h, $n, $s, $m, $d, $y);
  1816. }
  1817. /**
  1818. * Adds x years to a UNIX timestamp
  1819. * @param int The timestamp
  1820. * @param int The number of years to add
  1821. * @return int The new timestamp
  1822. */
  1823. function addYear($timestamp, $num = 1)
  1824. {
  1825. list($y, $m, $d, $h, $n, $s) = split('/', date('Y/m/d/h/i/s', $timestamp));
  1826. return mktime($h, $n, $s, $m, $d, $y + $num);
  1827. }
  1828. /**
  1829. * @param int $eventId
  1830. * @return array
  1831. */
  1832. public function getAllRepeatEvents($eventId)
  1833. {
  1834. $events = array();
  1835. switch ($this->type) {
  1836. case 'personal':
  1837. break;
  1838. case 'course':
  1839. if (!empty($this->course['real_id'])) {
  1840. $sql = "SELECT * FROM ".$this->tbl_course_agenda."
  1841. WHERE
  1842. c_id = ".$this->course['real_id']." AND
  1843. parent_event_id = ".$eventId;
  1844. $result = Database::query($sql);
  1845. if (Database::num_rows($result)) {
  1846. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1847. $events[] = $row;
  1848. }
  1849. }
  1850. }
  1851. break;
  1852. }
  1853. return $events;
  1854. }
  1855. /**
  1856. * @param int $eventId
  1857. * @param int $courseId
  1858. *
  1859. * @return bool
  1860. */
  1861. public function hasChildren($eventId, $courseId)
  1862. {
  1863. $eventId = intval($eventId);
  1864. $courseId = intval($courseId);
  1865. $sql = "SELECT count(DISTINCT(id)) as count
  1866. FROM ".$this->tbl_course_agenda."
  1867. WHERE
  1868. c_id = ".$courseId." AND
  1869. parent_event_id = ".$eventId;
  1870. $result = Database::query($sql);
  1871. if (Database::num_rows($result)) {
  1872. $row = Database::fetch_array($result, 'ASSOC');
  1873. return $row['count'] > 0;
  1874. }
  1875. return false;
  1876. }
  1877. /**
  1878. * @param int $filter
  1879. * @param string $view
  1880. * @return string
  1881. */
  1882. public function displayActions($view, $filter = 0)
  1883. {
  1884. $actions = "<a href='".api_get_path(WEB_CODE_PATH)."calendar/agenda_js.php?type={$this->type}'>".
  1885. Display::return_icon('calendar.png', get_lang('Calendar'), '', ICON_SIZE_MEDIUM)."</a>";
  1886. $actions .= "<a href='".api_get_path(WEB_CODE_PATH)."calendar/agenda_list.php?type={$this->type}&".api_get_cidreq()."'>".
  1887. Display::return_icon('week.png', get_lang('AgendaList'), '', ICON_SIZE_MEDIUM)."</a>";
  1888. if (api_is_allowed_to_edit(false, true) OR
  1889. (api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous()) && api_is_allowed_to_session_edit(false, true) OR
  1890. GroupManager::user_has_access(api_get_user_id(), api_get_group_id(), GroupManager::GROUP_TOOL_CALENDAR) &&
  1891. GroupManager::is_tutor_of_group(api_get_user_id(), api_get_group_id())
  1892. ) {
  1893. if ($this->type == 'course') {
  1894. $form = null;
  1895. if (!isset($_GET['action'])) {
  1896. $form = new FormValidator('form-search');
  1897. $attributes = array(
  1898. 'multiple' => false,
  1899. 'id' => 'select_form_id_search'
  1900. );
  1901. $selectedValues = $this->parseAgendaFilter($filter);
  1902. $this->showToForm($form, $selectedValues, $attributes);
  1903. $form = $form->return_form();
  1904. }
  1905. $actions .= "<a href='".api_get_path(WEB_CODE_PATH)."calendar/agenda.php?".api_get_cidreq()."&action=add&type=course'>".
  1906. Display::return_icon('new_event.png', get_lang('AgendaAdd'), '', ICON_SIZE_MEDIUM)."</a>";
  1907. $actions .= "<a href='".api_get_path(WEB_CODE_PATH)."calendar/agenda.php?".api_get_cidreq()."&action=importical&type=course'>".
  1908. Display::return_icon('import_calendar.png', get_lang('ICalFileImport'), '', ICON_SIZE_MEDIUM)."</a>";
  1909. if ($view == 'calendar') {
  1910. $actions .= $form;
  1911. }
  1912. }
  1913. }
  1914. return $actions;
  1915. }
  1916. /**
  1917. * @return FormValidator
  1918. */
  1919. public function getImportCalendarForm()
  1920. {
  1921. $form = new FormValidator(
  1922. 'frm_import_ical',
  1923. 'post',
  1924. api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&type='.$this->type,
  1925. array('enctype' => 'multipart/form-data')
  1926. );
  1927. $form->addElement('header', get_lang('ICalFileImport'));
  1928. $form->addElement('file', 'ical_import', get_lang('ICalFileImport'));
  1929. $form->addRule('ical_import', get_lang('ThisFieldIsRequired'), 'required');
  1930. $form->addElement('button', 'ical_submit', get_lang('Import'));
  1931. return $form;
  1932. }
  1933. /**
  1934. * @param array $courseInfo
  1935. * @param $file
  1936. * @return array|bool|string
  1937. */
  1938. public function importEventFile($courseInfo, $file)
  1939. {
  1940. $charset = api_get_system_encoding();
  1941. $filepath = api_get_path(SYS_ARCHIVE_PATH).$file['name'];
  1942. $messages = array();
  1943. if (!@move_uploaded_file($file['tmp_name'], $filepath)) {
  1944. error_log('Problem moving uploaded file: '.$file['error'].' in '.__FILE__.' line '.__LINE__);
  1945. return false;
  1946. }
  1947. $data = file_get_contents($filepath);
  1948. require_once api_get_path(SYS_PATH).'vendor/autoload.php';
  1949. $trans = array(
  1950. 'DAILY' => 'daily',
  1951. 'WEEKLY' => 'weekly',
  1952. 'MONTHLY' => 'monthlyByDate',
  1953. 'YEARLY' => 'yearly'
  1954. );
  1955. $sentTo = array('everyone' => true);
  1956. $calendar = Sabre\VObject\Reader::read($data);
  1957. $currentTimeZone = _api_get_timezone();
  1958. if (!empty($calendar->VEVENT)) {
  1959. foreach ($calendar->VEVENT as $event) {
  1960. $start = $event->DTSTART->getDateTime();
  1961. $end = $event->DTEND->getDateTime();
  1962. //Sabre\VObject\DateTimeParser::parseDateTime(string $dt, \Sabre\VObject\DateTimeZone $tz)
  1963. $startDateTime = api_get_local_time($start->format('Y-m-d H:i:s'), $currentTimeZone, $start->format('e'));
  1964. $endDateTime = api_get_local_time($end->format('Y-m-d H:i'), $currentTimeZone, $end->format('e'));
  1965. $title = api_convert_encoding((string)$event->summary, $charset, 'UTF-8');
  1966. $description = api_convert_encoding((string)$event->description, $charset, 'UTF-8');
  1967. $id = $this->add_event(
  1968. $startDateTime,
  1969. $endDateTime,
  1970. 'false',
  1971. $title,
  1972. $description,
  1973. $sentTo
  1974. );
  1975. $messages[] = " $title - ".$startDateTime." - ".$endDateTime;
  1976. //$attendee = (string)$event->attendee;
  1977. /** @var Sabre\VObject\Property\ICalendar\Recur $repeat */
  1978. $repeat = $event->RRULE;
  1979. if ($id && !empty($repeat)) {
  1980. $repeat = $repeat->getParts();
  1981. $freq = $trans[$repeat['FREQ']];
  1982. if (isset($repeat['UNTIL']) && !empty($repeat['UNTIL'])) {
  1983. $until = Sabre\VObject\DateTimeParser::parseDateTime($repeat['UNTIL'], new DateTimeZone($currentTimeZone));
  1984. $until = $until->format('Y-m-d H:i');
  1985. //$res = agenda_add_repeat_item($courseInfo, $id, $freq, $until, $attendee);
  1986. $this->addRepeatedItem(
  1987. $id,
  1988. $freq,
  1989. $until,
  1990. $sentTo
  1991. );
  1992. }
  1993. if (!empty($repeat['COUNT'])) {
  1994. /*$count = $repeat['COUNT'];
  1995. $interval = $repeat['INTERVAL'];
  1996. $endDate = null;
  1997. switch($freq) {
  1998. case 'daily':
  1999. $start = api_strtotime($startDateTime);
  2000. $date = new DateTime($startDateTime);
  2001. $days = $count * $interval;
  2002. var_dump($days);
  2003. $date->add(new DateInterval("P".$days."D"));
  2004. $endDate = $date->format('Y-m-d H:i');
  2005. //$endDate = $count *
  2006. for ($i = 0; $i < $count; $i++) {
  2007. $days = 86400 * 7
  2008. }
  2009. }
  2010. }*/
  2011. //$res = agenda_add_repeat_item($courseInfo, $id, $freq, $count, $attendee);
  2012. /*$this->addRepeatedItem(
  2013. $id,
  2014. $freq,
  2015. $endDate,
  2016. $sentTo
  2017. );*/
  2018. }
  2019. }
  2020. }
  2021. }
  2022. if (!empty($messages)) {
  2023. $messages = implode('<br /> ', $messages);
  2024. }
  2025. return $messages;
  2026. }
  2027. /**
  2028. * Parse filter turns USER:12 to ['users' => [12])] or G:1 ['groups' => [1]]
  2029. * @param $filter
  2030. * @return array
  2031. */
  2032. public function parseAgendaFilter($filter)
  2033. {
  2034. $everyone = false;
  2035. $groupId = null;
  2036. $userId = null;
  2037. if ($filter == 'everyone') {
  2038. $everyone = true;
  2039. } else {
  2040. if (substr($filter, 0, 1) == 'G') {
  2041. $groupId = str_replace('GROUP:', '', $filter);
  2042. } else {
  2043. $userId = str_replace('USER:', '', $filter);
  2044. }
  2045. }
  2046. if (empty($userId) && empty($groupId)) {
  2047. $everyone = true;
  2048. }
  2049. return array(
  2050. 'everyone' => $everyone,
  2051. 'users' => array($userId),
  2052. 'groups' => array($groupId)
  2053. );
  2054. }
  2055. }