agenda.lib.php 95 KB

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