agenda.lib.php 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553
  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. /**
  21. * Constructor
  22. * @param int $senderId Optional The user sender ID
  23. * @param int $courseId Opitonal. The course ID
  24. * @param int $sessionId Optional The session ID
  25. */
  26. public function __construct($senderId = 0, $courseId = 0, $sessionId = 0)
  27. {
  28. //Table definitions
  29. $this->tbl_global_agenda = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  30. $this->tbl_personal_agenda = Database::get_main_table(TABLE_PERSONAL_AGENDA);
  31. $this->tbl_course_agenda = Database::get_course_table(TABLE_AGENDA);
  32. $this->table_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT);
  33. //Setting the course object if we are in a course
  34. unset($this->course);
  35. $courseInfo = api_get_course_info_by_id($courseId);
  36. if (!empty($courseInfo)) {
  37. $this->course = $courseInfo;
  38. }
  39. $this->setSessionId($sessionId ?: api_get_session_id());
  40. $this->setSenderId($senderId ?: api_get_user_id());
  41. $this->setIsAllowedToEdit(api_is_allowed_to_edit(null, true));
  42. $this->events = array();
  43. // Event colors
  44. $this->event_platform_color = 'red'; //red
  45. $this->event_course_color = '#458B00'; //green
  46. $this->event_group_color = '#A0522D'; //siena
  47. $this->event_session_color = '#00496D'; // kind of green
  48. $this->eventOtherSessionColor = '#999';
  49. $this->event_personal_color = 'steel blue'; //steel blue
  50. }
  51. /**
  52. * @param int $senderId
  53. */
  54. public function setSenderId($senderId)
  55. {
  56. $this->senderId = intval($senderId);
  57. }
  58. /**
  59. * @return int
  60. */
  61. public function getSenderId()
  62. {
  63. return $this->senderId;
  64. }
  65. /**
  66. * @param string $type can be 'personal', 'admin' or 'course'
  67. */
  68. public function setType($type)
  69. {
  70. $typeList = $this->getTypes();
  71. if (in_array($type, $typeList)) {
  72. $this->type = $type;
  73. }
  74. }
  75. /**
  76. * @param int $id
  77. */
  78. public function setSessionId($id)
  79. {
  80. $this->sessionId = intval($id);
  81. }
  82. /**
  83. * @return int $id
  84. */
  85. public function getSessionId()
  86. {
  87. return $this->sessionId;
  88. }
  89. /**
  90. * @param array $courseInfo
  91. */
  92. public function set_course($courseInfo)
  93. {
  94. $this->course = $courseInfo;
  95. }
  96. /**
  97. * @return array
  98. */
  99. public function getTypes()
  100. {
  101. return $this->types;
  102. }
  103. /**
  104. * Adds an event to the calendar
  105. * @param string $start datetime format: 2012-06-14 09:00:00
  106. * @param string $end datetime format: 2012-06-14 09:00:00
  107. * @param string $allDay (true, false)
  108. * @param string $title
  109. * @param string $content
  110. * @param array $usersToSend array('everyone') or a list of user/group ids
  111. * @param bool $addAsAnnouncement event as a *course* announcement
  112. * @param int $parentEventId
  113. * @param array $attachmentArray array of $_FILES['']
  114. * @param array $attachmentCommentList
  115. * @param string $eventComment
  116. * @param string $color
  117. *
  118. * @return int
  119. */
  120. public function addEvent(
  121. $start,
  122. $end,
  123. $allDay,
  124. $title,
  125. $content,
  126. $usersToSend = array(),
  127. $addAsAnnouncement = false,
  128. $parentEventId = null,
  129. $attachmentArray = array(),
  130. $attachmentCommentList = array(),
  131. $eventComment = null,
  132. $color = ''
  133. ) {
  134. $em = Database::getManager();
  135. $start = api_get_utc_datetime($start);
  136. $end = api_get_utc_datetime($end);
  137. $allDay = isset($allDay) && $allDay === 'true' ? 1 : 0;
  138. $id = null;
  139. switch ($this->type) {
  140. case 'personal':
  141. $personalAgenda = new Chamilo\CoreBundle\Entity\PersonalAgenda();
  142. $personalAgenda
  143. ->setUser(api_get_user_id())
  144. ->setTitle($title)
  145. ->setText($content)
  146. ->setDate(
  147. new DateTime($start, new DateTimeZone('UTC'))
  148. )
  149. ->setEnddate(
  150. new DateTime($end, new DateTimeZone('UTC'))
  151. )
  152. ->setAllDay($allDay)
  153. ->setColor($color);
  154. $em->persist($personalAgenda);
  155. $em->flush();
  156. $id = $personalAgenda->getId();
  157. break;
  158. case 'course':
  159. $attributes = array(
  160. 'title' => $title,
  161. 'content' => $content,
  162. 'start_date' => $start,
  163. 'end_date' => $end,
  164. 'all_day' => $allDay,
  165. 'session_id' => $this->getSessionId(),
  166. 'c_id' => $this->course['real_id'],
  167. 'comment' => $eventComment,
  168. 'color' => $color
  169. );
  170. if (!empty($parentEventId)) {
  171. $attributes['parent_event_id'] = $parentEventId;
  172. }
  173. $senderId = $this->getSenderId();
  174. $sessionId = $this->getSessionId();
  175. // Simple course event.
  176. $id = Database::insert($this->tbl_course_agenda, $attributes);
  177. if ($id) {
  178. $sql = "UPDATE ".$this->tbl_course_agenda." SET id = iid WHERE iid = $id";
  179. Database::query($sql);
  180. $groupId = api_get_group_id();
  181. $groupIid = 0;
  182. if ($groupId) {
  183. $groupInfo = GroupManager::get_group_properties($groupId);
  184. if ($groupInfo) {
  185. $groupIid = $groupInfo['iid'];
  186. }
  187. }
  188. if (!empty($usersToSend)) {
  189. $sendTo = $this->parseSendToArray($usersToSend);
  190. if ($sendTo['everyone']) {
  191. api_item_property_update(
  192. $this->course,
  193. TOOL_CALENDAR_EVENT,
  194. $id,
  195. 'AgendaAdded',
  196. $senderId,
  197. $groupIid,
  198. '',
  199. $start,
  200. $end,
  201. $sessionId
  202. );
  203. api_item_property_update(
  204. $this->course,
  205. TOOL_CALENDAR_EVENT,
  206. $id,
  207. 'visible',
  208. $senderId,
  209. $groupIid,
  210. '',
  211. $start,
  212. $end,
  213. $sessionId
  214. );
  215. } else {
  216. // Storing the selected groups
  217. if (!empty($sendTo['groups'])) {
  218. foreach ($sendTo['groups'] as $group) {
  219. $groupIidItem = 0;
  220. if ($group) {
  221. $groupInfo = GroupManager::get_group_properties($group);
  222. if ($groupInfo) {
  223. $groupIidItem = $groupInfo['iid'];
  224. }
  225. }
  226. api_item_property_update(
  227. $this->course,
  228. TOOL_CALENDAR_EVENT,
  229. $id,
  230. 'AgendaAdded',
  231. $senderId,
  232. $groupIidItem,
  233. 0,
  234. $start,
  235. $end,
  236. $sessionId
  237. );
  238. api_item_property_update(
  239. $this->course,
  240. TOOL_CALENDAR_EVENT,
  241. $id,
  242. 'visible',
  243. $senderId,
  244. $groupIidItem,
  245. 0,
  246. $start,
  247. $end,
  248. $sessionId
  249. );
  250. }
  251. }
  252. // storing the selected users
  253. if (!empty($sendTo['users'])) {
  254. foreach ($sendTo['users'] as $userId) {
  255. api_item_property_update(
  256. $this->course,
  257. TOOL_CALENDAR_EVENT,
  258. $id,
  259. 'AgendaAdded',
  260. $senderId,
  261. $groupIid,
  262. $userId,
  263. $start,
  264. $end,
  265. $sessionId
  266. );
  267. api_item_property_update(
  268. $this->course,
  269. TOOL_CALENDAR_EVENT,
  270. $id,
  271. 'visible',
  272. $senderId,
  273. $groupIid,
  274. $userId,
  275. $start,
  276. $end,
  277. $sessionId
  278. );
  279. }
  280. }
  281. }
  282. }
  283. // Add announcement.
  284. if ($addAsAnnouncement) {
  285. $this->storeAgendaEventAsAnnouncement($id, $usersToSend);
  286. }
  287. // Add attachment.
  288. if (isset($attachmentArray) && !empty($attachmentArray)) {
  289. $counter = 0;
  290. foreach ($attachmentArray as $attachmentItem) {
  291. $this->addAttachment(
  292. $id,
  293. $attachmentItem,
  294. $attachmentCommentList[$counter],
  295. $this->course
  296. );
  297. $counter++;
  298. }
  299. }
  300. }
  301. break;
  302. case 'admin':
  303. if (api_is_platform_admin()) {
  304. $attributes = array(
  305. 'title' => $title,
  306. 'content' => $content,
  307. 'start_date' => $start,
  308. 'end_date' => $end,
  309. 'all_day' => $allDay,
  310. 'access_url_id' => api_get_current_access_url_id()
  311. );
  312. $id = Database::insert($this->tbl_global_agenda, $attributes);
  313. }
  314. break;
  315. }
  316. return $id;
  317. }
  318. /**
  319. * @param int $eventId
  320. * @param int $courseId
  321. *
  322. * @return array
  323. */
  324. public function getRepeatedInfoByEvent($eventId, $courseId)
  325. {
  326. $repeatTable = Database::get_course_table(TABLE_AGENDA_REPEAT);
  327. $eventId = intval($eventId);
  328. $courseId = intval($courseId);
  329. $sql = "SELECT * FROM $repeatTable
  330. WHERE c_id = $courseId AND cal_id = $eventId";
  331. $res = Database::query($sql);
  332. $repeatInfo = array();
  333. if (Database::num_rows($res) > 0) {
  334. $repeatInfo = Database::fetch_array($res, 'ASSOC');
  335. }
  336. return $repeatInfo;
  337. }
  338. /**
  339. * @param int $eventId
  340. * @param string $type
  341. * @param string $end in local time
  342. * @param array $sentTo
  343. *
  344. * @return bool
  345. */
  346. public function addRepeatedItem($eventId, $type, $end, $sentTo = array())
  347. {
  348. $t_agenda = Database::get_course_table(TABLE_AGENDA);
  349. $t_agenda_r = Database::get_course_table(TABLE_AGENDA_REPEAT);
  350. if (empty($this->course)) {
  351. return false;
  352. }
  353. $course_id = $this->course['real_id'];
  354. $eventId = intval($eventId);
  355. $sql = "SELECT title, content, start_date, end_date, all_day
  356. FROM $t_agenda
  357. WHERE c_id = $course_id AND id = $eventId";
  358. $res = Database::query($sql);
  359. if (Database::num_rows($res) !== 1) {
  360. return false;
  361. }
  362. $row = Database::fetch_array($res);
  363. $origStartDate = api_strtotime($row['start_date'], 'UTC');
  364. $origEndDate = api_strtotime($row['end_date'], 'UTC');
  365. $diff = $origEndDate - $origStartDate;
  366. $title = $row['title'];
  367. $content = $row['content'];
  368. $allDay = $row['all_day'];
  369. $now = time();
  370. $type = Database::escape_string($type);
  371. $end = api_strtotime($end);
  372. if (1 <= $end && $end <= 500) {
  373. // We assume that, with this type of value, the user actually gives a count of repetitions
  374. //and that he wants us to calculate the end date with that (particularly in case of imports from ical)
  375. switch ($type) {
  376. case 'daily':
  377. $end = $origStartDate + (86400 * $end);
  378. break;
  379. case 'weekly':
  380. $end = $this->addWeek($origStartDate, $end);
  381. break;
  382. case 'monthlyByDate':
  383. $end = $this->addMonth($origStartDate, $end);
  384. break;
  385. case 'monthlyByDay':
  386. //TODO
  387. break;
  388. case 'monthlyByDayR':
  389. //TODO
  390. break;
  391. case 'yearly':
  392. $end = $this->addYear($origStartDate, $end);
  393. break;
  394. }
  395. }
  396. $typeList = array('daily', 'weekly', 'monthlyByDate', 'monthlyByDay', 'monthlyByDayR', 'yearly');
  397. // The event has to repeat *in the future*. We don't allow repeated
  398. // events in the past
  399. if ($end > $now && in_array($type, $typeList)) {
  400. $sql = "INSERT INTO $t_agenda_r (c_id, cal_id, cal_type, cal_end)
  401. VALUES ($course_id, '$eventId', '$type', '$end')";
  402. Database::query($sql);
  403. switch ($type) {
  404. // @todo improve loop.
  405. case 'daily':
  406. for ($i = $origStartDate + 86400; $i <= $end; $i += 86400) {
  407. $start = date('Y-m-d H:i:s', $i);
  408. $repeatEnd = date('Y-m-d H:i:s', $i + $diff);
  409. $this->addEvent(
  410. $start,
  411. $repeatEnd,
  412. $allDay,
  413. $title,
  414. $content,
  415. $sentTo,
  416. false,
  417. $eventId
  418. );
  419. }
  420. break;
  421. case 'weekly':
  422. for ($i = $origStartDate + 604800; $i <= $end; $i += 604800) {
  423. $start = date('Y-m-d H:i:s', $i);
  424. $repeatEnd = date('Y-m-d H:i:s', $i + $diff);
  425. $this->addEvent(
  426. $start,
  427. $repeatEnd,
  428. $allDay,
  429. $title,
  430. $content,
  431. $sentTo,
  432. false,
  433. $eventId
  434. );
  435. }
  436. break;
  437. case 'monthlyByDate':
  438. $next_start = $this->addMonth($origStartDate);
  439. while ($next_start <= $end) {
  440. $start = date('Y-m-d H:i:s', $next_start);
  441. $repeatEnd = date('Y-m-d H:i:s', $next_start + $diff);
  442. $this->addEvent(
  443. $start,
  444. $repeatEnd,
  445. $allDay,
  446. $title,
  447. $content,
  448. $sentTo,
  449. false,
  450. $eventId
  451. );
  452. $next_start = $this->addMonth($next_start);
  453. }
  454. break;
  455. case 'monthlyByDay':
  456. //not yet implemented
  457. break;
  458. case 'monthlyByDayR':
  459. //not yet implemented
  460. break;
  461. case 'yearly':
  462. $next_start = $this->addYear($origStartDate);
  463. while ($next_start <= $end) {
  464. $start = date('Y-m-d H:i:s', $next_start);
  465. $repeatEnd = date('Y-m-d H:i:s', $next_start + $diff);
  466. $this->addEvent(
  467. $start,
  468. $repeatEnd,
  469. $allDay,
  470. $title,
  471. $content,
  472. $sentTo,
  473. false,
  474. $eventId
  475. );
  476. $next_start = $this->addYear($next_start);
  477. }
  478. break;
  479. }
  480. }
  481. return true;
  482. }
  483. /**
  484. * @param int $item_id
  485. * @param array $sentTo
  486. * @return int
  487. */
  488. public function storeAgendaEventAsAnnouncement($item_id, $sentTo = array())
  489. {
  490. $table_agenda = Database::get_course_table(TABLE_AGENDA);
  491. $course_id = api_get_course_int_id();
  492. // Check params
  493. if (empty($item_id) || $item_id != strval(intval($item_id))) {
  494. return -1;
  495. }
  496. // Get the agenda item.
  497. $item_id = intval($item_id);
  498. $sql = "SELECT * FROM $table_agenda
  499. WHERE c_id = $course_id AND id = ".$item_id;
  500. $res = Database::query($sql);
  501. if (Database::num_rows($res) > 0) {
  502. $row = Database::fetch_array($res, 'ASSOC');
  503. // Sending announcement
  504. if (!empty($sentTo)) {
  505. $id = AnnouncementManager::add_announcement(
  506. $row['title'],
  507. $row['content'],
  508. $sentTo,
  509. null,
  510. null,
  511. $row['end_date']
  512. );
  513. AnnouncementManager::send_email($id);
  514. return $id;
  515. }
  516. }
  517. return -1;
  518. }
  519. /**
  520. * Edits an event
  521. *
  522. * @param int $id
  523. * @param string $start datetime format: 2012-06-14 09:00:00
  524. * @param string $end datetime format: 2012-06-14 09:00:00
  525. * @param int $allDay is all day 'true' or 'false'
  526. * @param string $title
  527. * @param string $content
  528. * @param array $usersToSend
  529. * @param array $attachmentArray
  530. * @param array $attachmentCommentList
  531. * @param string $comment
  532. * @param string $color
  533. * @param bool $addAnnouncement
  534. *
  535. * @return null|false
  536. */
  537. public function editEvent(
  538. $id,
  539. $start,
  540. $end,
  541. $allDay,
  542. $title,
  543. $content,
  544. $usersToSend = array(),
  545. $attachmentArray = array(),
  546. $attachmentCommentList = array(),
  547. $comment = null,
  548. $color = '',
  549. $addAnnouncement = false
  550. ) {
  551. $em = Database::getManager();
  552. $start = api_get_utc_datetime($start);
  553. $end = api_get_utc_datetime($end);
  554. $allDay = isset($allDay) && $allDay == 'true' ? 1 : 0;
  555. switch ($this->type) {
  556. case 'personal':
  557. $eventInfo = $this->get_event($id);
  558. if ($eventInfo['user'] != api_get_user_id()) {
  559. break;
  560. }
  561. $personalAgenda = $em->find('ChamiloCoreBundle:PersonalAgenda', $id);
  562. $personalAgenda
  563. ->setTitle($title)
  564. ->setText($content)
  565. ->setDate($start)
  566. ->setEnddate($end)
  567. ->setAllDay($allDay)
  568. ->setColor($color);
  569. $em->persist($personalAgenda);
  570. $em->flush();
  571. break;
  572. case 'course':
  573. $eventInfo = $this->get_event($id);
  574. if (empty($eventInfo)) {
  575. return false;
  576. }
  577. $groupId = api_get_group_id();
  578. $groupIid = 0;
  579. if ($groupId) {
  580. $groupInfo = GroupManager::get_group_properties($groupId);
  581. if ($groupInfo) {
  582. $groupIid = $groupInfo['iid'];
  583. }
  584. }
  585. $course_id = $this->course['real_id'];
  586. if (empty($course_id)) {
  587. return false;
  588. }
  589. if ($this->getIsAllowedToEdit()) {
  590. $attributes = array(
  591. 'title' => $title,
  592. 'content' => $content,
  593. 'start_date' => $start,
  594. 'end_date' => $end,
  595. 'all_day' => $allDay,
  596. 'comment' => $comment,
  597. 'color' => $color
  598. );
  599. Database::update(
  600. $this->tbl_course_agenda,
  601. $attributes,
  602. array(
  603. 'id = ? AND c_id = ? AND session_id = ? ' => array(
  604. $id,
  605. $course_id,
  606. $this->sessionId
  607. )
  608. )
  609. );
  610. if (!empty($usersToSend)) {
  611. $sendTo = $this->parseSendToArray($usersToSend);
  612. $usersToDelete = array_diff($eventInfo['send_to']['users'], $sendTo['users']);
  613. $usersToAdd = array_diff($sendTo['users'], $eventInfo['send_to']['users']);
  614. $groupsToDelete = array_diff($eventInfo['send_to']['groups'], $sendTo['groups']);
  615. $groupToAdd = array_diff($sendTo['groups'], $eventInfo['send_to']['groups']);
  616. if ($sendTo['everyone']) {
  617. // Delete all from group
  618. if (isset($eventInfo['send_to']['groups']) &&
  619. !empty($eventInfo['send_to']['groups'])
  620. ) {
  621. foreach ($eventInfo['send_to']['groups'] as $group) {
  622. $groupIidItem = 0;
  623. if ($group) {
  624. $groupInfo = GroupManager::get_group_properties($group);
  625. if ($groupInfo) {
  626. $groupIidItem = $groupInfo['iid'];
  627. }
  628. }
  629. api_item_property_delete(
  630. $this->course,
  631. TOOL_CALENDAR_EVENT,
  632. $id,
  633. 0,
  634. $groupIidItem,
  635. $this->sessionId
  636. );
  637. }
  638. }
  639. // Storing the selected users.
  640. if (isset($eventInfo['send_to']['users']) &&
  641. !empty($eventInfo['send_to']['users'])
  642. ) {
  643. foreach ($eventInfo['send_to']['users'] as $userId) {
  644. api_item_property_delete(
  645. $this->course,
  646. TOOL_CALENDAR_EVENT,
  647. $id,
  648. $userId,
  649. $groupIid,
  650. $this->sessionId
  651. );
  652. }
  653. }
  654. // Add to everyone only.
  655. api_item_property_update(
  656. $this->course,
  657. TOOL_CALENDAR_EVENT,
  658. $id,
  659. 'visible',
  660. api_get_user_id(),
  661. $groupIid,
  662. null,
  663. $start,
  664. $end,
  665. $this->sessionId
  666. );
  667. } else {
  668. // Delete "everyone".
  669. api_item_property_delete(
  670. $this->course,
  671. TOOL_CALENDAR_EVENT,
  672. $id,
  673. 0,
  674. 0,
  675. $this->sessionId
  676. );
  677. // Add groups
  678. if (!empty($groupToAdd)) {
  679. foreach ($groupToAdd as $group) {
  680. $groupIidItem = 0;
  681. if ($group) {
  682. $groupInfo = GroupManager::get_group_properties($group);
  683. if ($groupInfo) {
  684. $groupIidItem = $groupInfo['iid'];
  685. }
  686. }
  687. api_item_property_update(
  688. $this->course,
  689. TOOL_CALENDAR_EVENT,
  690. $id,
  691. 'visible',
  692. api_get_user_id(),
  693. $groupIidItem,
  694. 0,
  695. $start,
  696. $end,
  697. $this->sessionId
  698. );
  699. }
  700. }
  701. // Delete groups.
  702. if (!empty($groupsToDelete)) {
  703. foreach ($groupsToDelete as $group) {
  704. $groupIidItem = 0;
  705. if ($group) {
  706. $groupInfo = GroupManager::get_group_properties($group);
  707. if ($groupInfo) {
  708. $groupIidItem = $groupInfo['iid'];
  709. }
  710. }
  711. api_item_property_delete(
  712. $this->course,
  713. TOOL_CALENDAR_EVENT,
  714. $id,
  715. 0,
  716. $groupIidItem,
  717. $this->sessionId
  718. );
  719. }
  720. }
  721. // Add users.
  722. if (!empty($usersToAdd)) {
  723. foreach ($usersToAdd as $userId) {
  724. api_item_property_update(
  725. $this->course,
  726. TOOL_CALENDAR_EVENT,
  727. $id,
  728. 'visible',
  729. api_get_user_id(),
  730. $groupIid,
  731. $userId,
  732. $start,
  733. $end,
  734. $this->sessionId
  735. );
  736. }
  737. }
  738. // Delete users.
  739. if (!empty($usersToDelete)) {
  740. foreach ($usersToDelete as $userId) {
  741. api_item_property_delete(
  742. $this->course,
  743. TOOL_CALENDAR_EVENT,
  744. $id,
  745. $userId,
  746. $groupIid,
  747. $this->sessionId
  748. );
  749. }
  750. }
  751. }
  752. }
  753. // Add announcement.
  754. if (isset($addAnnouncement) && !empty($addAnnouncement)) {
  755. $this->storeAgendaEventAsAnnouncement($id, $usersToSend);
  756. }
  757. // Add attachment.
  758. if (isset($attachmentArray) && !empty($attachmentArray)) {
  759. $counter = 0;
  760. foreach ($attachmentArray as $attachmentItem) {
  761. $this->updateAttachment(
  762. $attachmentItem['id'],
  763. $id,
  764. $attachmentItem,
  765. $attachmentCommentList[$counter],
  766. $this->course
  767. );
  768. $counter++;
  769. }
  770. }
  771. return true;
  772. } else {
  773. return false;
  774. }
  775. break;
  776. case 'admin':
  777. case 'platform':
  778. if (api_is_platform_admin()) {
  779. $attributes = array(
  780. 'title' => $title,
  781. 'content' => $content,
  782. 'start_date' => $start,
  783. 'end_date' => $end,
  784. 'all_day' => $allDay
  785. );
  786. Database::update(
  787. $this->tbl_global_agenda,
  788. $attributes,
  789. array('id = ?' => $id)
  790. );
  791. }
  792. break;
  793. }
  794. }
  795. /**
  796. * @param int $id
  797. * @param bool $deleteAllItemsFromSerie
  798. */
  799. public function deleteEvent($id, $deleteAllItemsFromSerie = false)
  800. {
  801. $em = Database::getManager();
  802. switch ($this->type) {
  803. case 'personal':
  804. $eventInfo = $this->get_event($id);
  805. if ($eventInfo['user'] == api_get_user_id()) {
  806. $personalAgenda = $em->find('ChamiloCoreBundle:PersonalAgenda', $id);
  807. $em->remove($personalAgenda);
  808. $em->flush();
  809. }
  810. break;
  811. case 'course':
  812. $course_id = api_get_course_int_id();
  813. if (!empty($course_id) && api_is_allowed_to_edit(null, true)) {
  814. // Delete
  815. $eventInfo = $this->get_event($id);
  816. if ($deleteAllItemsFromSerie) {
  817. /* This is one of the children.
  818. Getting siblings and delete 'Em all + the father! */
  819. if (isset($eventInfo['parent_event_id']) && !empty($eventInfo['parent_event_id'])) {
  820. // Removing items.
  821. $events = $this->getAllRepeatEvents($eventInfo['parent_event_id']);
  822. if (!empty($events)) {
  823. foreach ($events as $event) {
  824. $this->deleteEvent($event['id']);
  825. }
  826. }
  827. // Removing parent.
  828. $this->deleteEvent($eventInfo['parent_event_id']);
  829. } else {
  830. // This is the father looking for the children.
  831. $events = $this->getAllRepeatEvents($id);
  832. if (!empty($events)) {
  833. foreach ($events as $event) {
  834. $this->deleteEvent($event['id']);
  835. }
  836. }
  837. }
  838. }
  839. // Removing from events.
  840. Database::delete(
  841. $this->tbl_course_agenda,
  842. array('id = ? AND c_id = ?' => array($id, $course_id))
  843. );
  844. api_item_property_update(
  845. $this->course,
  846. TOOL_CALENDAR_EVENT,
  847. $id,
  848. 'delete',
  849. api_get_user_id()
  850. );
  851. // Removing from series.
  852. Database::delete(
  853. $this->table_repeat,
  854. array('cal_id = ? AND c_id = ?' => array($id, $course_id))
  855. );
  856. if (isset($eventInfo['attachment']) && !empty($eventInfo['attachment'])) {
  857. foreach ($eventInfo['attachment'] as $attachment) {
  858. self::deleteAttachmentFile($attachment['id'], $this->course);
  859. }
  860. }
  861. }
  862. break;
  863. case 'admin':
  864. if (api_is_platform_admin()) {
  865. Database::delete(
  866. $this->tbl_global_agenda,
  867. array('id = ?' => $id)
  868. );
  869. }
  870. break;
  871. }
  872. }
  873. /**
  874. * Get agenda events
  875. * @param int $start
  876. * @param int $end
  877. * @param int $course_id
  878. * @param int $groupId
  879. * @param int $user_id
  880. * @param string $format
  881. *
  882. * @return array|string
  883. */
  884. public function getEvents(
  885. $start,
  886. $end,
  887. $course_id = null,
  888. $groupId = null,
  889. $user_id = 0,
  890. $format = 'json'
  891. ) {
  892. switch ($this->type) {
  893. case 'admin':
  894. $this->getPlatformEvents($start, $end);
  895. break;
  896. case 'course':
  897. $courseInfo = api_get_course_info_by_id($course_id);
  898. // Session coach can see all events inside a session.
  899. if (api_is_coach()) {
  900. // Own course
  901. $this->getCourseEvents(
  902. $start,
  903. $end,
  904. $courseInfo,
  905. $groupId,
  906. $this->sessionId,
  907. $user_id
  908. );
  909. // Others
  910. $this->getSessionEvents(
  911. $start,
  912. $end,
  913. $this->sessionId,
  914. $user_id,
  915. $this->eventOtherSessionColor
  916. );
  917. } else {
  918. $this->getCourseEvents(
  919. $start,
  920. $end,
  921. $courseInfo,
  922. $groupId,
  923. $this->sessionId,
  924. $user_id
  925. );
  926. }
  927. break;
  928. case 'personal':
  929. default:
  930. $sessionFilterActive = false;
  931. if (!empty($this->sessionId)) {
  932. $sessionFilterActive = true;
  933. }
  934. if ($sessionFilterActive == false) {
  935. // Getting personal events
  936. $this->getPersonalEvents($start, $end);
  937. // Getting platform/admin events
  938. $this->getPlatformEvents($start, $end);
  939. }
  940. // Getting course events
  941. $my_course_list = array();
  942. if (!api_is_anonymous()) {
  943. $session_list = SessionManager::get_sessions_by_user(
  944. api_get_user_id()
  945. );
  946. $my_course_list = CourseManager::get_courses_list_by_user_id(
  947. api_get_user_id(),
  948. false
  949. );
  950. }
  951. if (api_is_drh()) {
  952. if (api_drh_can_access_all_session_content()) {
  953. $session_list = array();
  954. $sessionList = SessionManager::get_sessions_followed_by_drh(
  955. api_get_user_id(),
  956. null,
  957. null,
  958. null,
  959. true,
  960. false
  961. );
  962. if (!empty($sessionList)) {
  963. foreach ($sessionList as $sessionItem) {
  964. $sessionId = $sessionItem['id'];
  965. $courses = SessionManager::get_course_list_by_session_id(
  966. $sessionId
  967. );
  968. $sessionInfo = array(
  969. 'session_id' => $sessionId,
  970. 'courses' => $courses
  971. );
  972. $session_list[] = $sessionInfo;
  973. }
  974. }
  975. }
  976. }
  977. if (!empty($session_list)) {
  978. foreach ($session_list as $session_item) {
  979. if ($sessionFilterActive) {
  980. if ($this->sessionId != $session_item['session_id']) {
  981. continue;
  982. }
  983. }
  984. $my_courses = $session_item['courses'];
  985. $my_session_id = $session_item['session_id'];
  986. if (!empty($my_courses)) {
  987. foreach ($my_courses as $course_item) {
  988. $courseInfo = api_get_course_info_by_id($course_item['real_id']);
  989. $this->getCourseEvents(
  990. $start,
  991. $end,
  992. $courseInfo,
  993. 0,
  994. $my_session_id
  995. );
  996. }
  997. }
  998. }
  999. }
  1000. if (!empty($my_course_list) && $sessionFilterActive == false) {
  1001. foreach ($my_course_list as $courseInfoItem) {
  1002. $courseInfo = api_get_course_info_by_id($courseInfoItem['real_id']);
  1003. if (isset($course_id) && !empty($course_id)) {
  1004. if ($courseInfo['real_id'] == $course_id) {
  1005. $this->getCourseEvents($start, $end, $courseInfo);
  1006. }
  1007. } else {
  1008. $this->getCourseEvents(
  1009. $start,
  1010. $end,
  1011. $courseInfo
  1012. );
  1013. }
  1014. }
  1015. }
  1016. break;
  1017. }
  1018. switch ($format) {
  1019. case 'json':
  1020. if (empty($this->events)) {
  1021. return '';
  1022. }
  1023. return json_encode($this->events);
  1024. break;
  1025. case 'array':
  1026. if (empty($this->events)) {
  1027. return [];
  1028. }
  1029. return $this->events;
  1030. break;
  1031. }
  1032. }
  1033. /**
  1034. * @param int $id
  1035. * @param int $day_delta
  1036. * @param int $minute_delta
  1037. * @return int
  1038. */
  1039. public function resizeEvent($id, $day_delta, $minute_delta)
  1040. {
  1041. $em = Database::getManager();
  1042. // we convert the hour delta into minutes and add the minute delta
  1043. $delta = ($day_delta * 60 * 24) + $minute_delta;
  1044. $delta = intval($delta);
  1045. $event = $this->get_event($id);
  1046. if (!empty($event)) {
  1047. switch ($this->type) {
  1048. case 'personal':
  1049. $personalAgenda = $em->find('ChamiloCoreBundle:PersonalAgenda', $id);
  1050. if ($personalAgenda) {
  1051. $newEndDate = $personalAgenda->getEnddate();
  1052. $newEndDate->add(new DateInterval("PT" . $delta . "M"));
  1053. $personalAgenda
  1054. ->setAllDay(0)
  1055. ->setEnddate($newEndDate);
  1056. $em->persist($newEndDate);
  1057. $em->flush();
  1058. }
  1059. break;
  1060. case 'course':
  1061. $sql = "UPDATE $this->tbl_course_agenda SET
  1062. all_day = 0, end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  1063. WHERE c_id = ".$this->course['real_id']." AND id=".intval($id);
  1064. Database::query($sql);
  1065. break;
  1066. case 'admin':
  1067. $sql = "UPDATE $this->tbl_global_agenda SET
  1068. all_day = 0, end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  1069. WHERE id=".intval($id);
  1070. Database::query($sql);
  1071. break;
  1072. }
  1073. }
  1074. return 1;
  1075. }
  1076. /**
  1077. * @param $id
  1078. * @param $day_delta
  1079. * @param $minute_delta
  1080. * @return int
  1081. */
  1082. public function move_event($id, $day_delta, $minute_delta)
  1083. {
  1084. $em = Database::getManager();
  1085. // we convert the hour delta into minutes and add the minute delta
  1086. $delta = ($day_delta * 60 * 24) + $minute_delta;
  1087. $delta = intval($delta);
  1088. $event = $this->get_event($id);
  1089. $allDay = 0;
  1090. if ($day_delta == 0 && $minute_delta == 0) {
  1091. $allDay = 1;
  1092. }
  1093. if (!empty($event)) {
  1094. switch ($this->type) {
  1095. case 'personal':
  1096. $personalAgenda = $em->find('ChamiloCoreBundle:PersonalAgenda', $id);
  1097. $personalAgenda = new Chamilo\CoreBundle\Entity\PersonalAgenda();
  1098. if ($personalAgenda) {
  1099. $newDate = $personalAgenda->getDate();
  1100. $newDate->add(new DateInterval("PT" . $delta . "M"));
  1101. $newEndDate = $personalAgenda->getEnddate();
  1102. $newEndDate->add(new DateInterval("PT" . $delta . "M"));
  1103. $personalAgenda
  1104. ->setAllDay($allDay)
  1105. ->setDate($newDate)
  1106. ->setEnddate($newEndDate);
  1107. $em->persist($personalAgenda);
  1108. $em->flush();
  1109. }
  1110. break;
  1111. case 'course':
  1112. $sql = "UPDATE $this->tbl_course_agenda SET
  1113. all_day = $allDay, start_date = DATE_ADD(start_date,INTERVAL $delta MINUTE),
  1114. end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  1115. WHERE c_id = ".$this->course['real_id']." AND id=".intval($id);
  1116. Database::query($sql);
  1117. break;
  1118. case 'admin':
  1119. $sql = "UPDATE $this->tbl_global_agenda SET
  1120. all_day = $allDay,
  1121. start_date = DATE_ADD(start_date,INTERVAL $delta MINUTE),
  1122. end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  1123. WHERE id=".intval($id);
  1124. Database::query($sql);
  1125. break;
  1126. }
  1127. }
  1128. return 1;
  1129. }
  1130. /**
  1131. * Gets a single event
  1132. *
  1133. * @param int event id
  1134. * @return array
  1135. */
  1136. public function get_event($id)
  1137. {
  1138. // make sure events of the personal agenda can only be seen by the user himself
  1139. $id = intval($id);
  1140. $event = null;
  1141. $em = Database::getManager();
  1142. switch ($this->type) {
  1143. case 'personal':
  1144. $personalAgenda = $em->find('ChamiloCoreBundle:PersonalAgenda', $id);
  1145. if ($personalAgenda) {
  1146. $event = [
  1147. 'id' => $personalAgenda->getId(),
  1148. 'user' => $personalAgenda->getUser(),
  1149. 'title' => $personalAgenda->getTitle(),
  1150. 'text' => $personalAgenda->getText(),
  1151. 'date' => $personalAgenda->getDate(),
  1152. 'enddate' => $personalAgenda->getEnddate(),
  1153. 'course' => $personalAgenda->getCourse()->getCode(),
  1154. 'parent_event_id' => $personalAgenda->getParentEventId(),
  1155. 'all_day' => $personalAgenda->getAllDay(),
  1156. 'color' => $personalAgenda->getColor(),
  1157. 'description' => $personalAgenda->getText(),
  1158. 'content' => $personalAgenda->getText(),
  1159. 'start_date' => $personalAgenda->getDate(),
  1160. 'end_date' => $personalAgenda->getEnddate()
  1161. ];
  1162. }
  1163. break;
  1164. case 'course':
  1165. if (!empty($this->course['real_id'])) {
  1166. $sql = "SELECT * FROM ".$this->tbl_course_agenda."
  1167. WHERE c_id = ".$this->course['real_id']." AND id = ".$id;
  1168. $result = Database::query($sql);
  1169. if (Database::num_rows($result)) {
  1170. $event = Database::fetch_array($result, 'ASSOC');
  1171. $event['description'] = $event['content'];
  1172. // Getting send to array
  1173. $event['send_to'] = $this->getUsersAndGroupSubscribedToEvent(
  1174. $id,
  1175. $this->course['real_id'],
  1176. $this->sessionId
  1177. );
  1178. // Getting repeat info
  1179. $event['repeat_info'] = $this->getRepeatedInfoByEvent(
  1180. $id,
  1181. $this->course['real_id']
  1182. );
  1183. if (!empty($event['parent_event_id'])) {
  1184. $event['parent_info'] = $this->get_event($event['parent_event_id']);
  1185. }
  1186. $event['attachment'] = $this->getAttachmentList($id, $this->course);
  1187. }
  1188. }
  1189. break;
  1190. case 'admin':
  1191. case 'platform':
  1192. $sql = "SELECT * FROM ".$this->tbl_global_agenda."
  1193. WHERE id = $id";
  1194. $result = Database::query($sql);
  1195. if (Database::num_rows($result)) {
  1196. $event = Database::fetch_array($result, 'ASSOC');
  1197. $event['description'] = $event['content'];
  1198. }
  1199. break;
  1200. }
  1201. return $event;
  1202. }
  1203. /**
  1204. * Gets personal events
  1205. * @param int $start
  1206. * @param int $end
  1207. * @return array
  1208. */
  1209. public function getPersonalEvents($start, $end)
  1210. {
  1211. $em = Database::getManager();
  1212. $start = intval($start);
  1213. $end = intval($end);
  1214. $user_id = api_get_user_id();
  1215. $qb = $em->createQueryBuilder();
  1216. $qb
  1217. ->select('pa')
  1218. ->from('ChamiloCoreBundle:PersonalAgenda', 'pa')
  1219. ->where(
  1220. $qb->expr()->eq('pa.user', $user_id)
  1221. );
  1222. if ($start !== 0) {
  1223. $start = new DateTime(api_get_utc_datetime($start), new DateTimeZone('UTC'));
  1224. $qb
  1225. ->andWhere(
  1226. $qb->expr()->gte('pa.date', ':start')
  1227. )
  1228. ->setParameter('start', $start);
  1229. }
  1230. if ($start !== 0) {
  1231. $end = new DateTime(api_get_utc_datetime($end), new DateTimeZone('UTC'));
  1232. $qb
  1233. ->andWhere(
  1234. $qb->expr()->orX(
  1235. $qb->expr()->lte('pa.enddate', ':end'),
  1236. $qb->expr()->isNull('pa.enddate')
  1237. )
  1238. )
  1239. ->setParameter('end', $end);
  1240. }
  1241. $result = $qb->getQuery()->getResult();
  1242. $my_events = array();
  1243. if (count($result)) {
  1244. foreach ($result as $row) {
  1245. $event = array();
  1246. $event['id'] = 'personal_' . $row->getId();
  1247. $event['title'] = $row->getTitle();
  1248. $event['className'] = 'personal';
  1249. $event['borderColor'] = $event['backgroundColor'] = $this->event_personal_color;
  1250. $event['editable'] = true;
  1251. $event['sent_to'] = get_lang('Me');
  1252. $event['type'] = 'personal';
  1253. if (!empty($row->getDate())) {
  1254. $event['start'] = $this->formatEventDate($row->getDate()->format('Y-m-d h:i:s'));
  1255. $event['start_date_localtime'] = api_get_local_time($row->getDate());
  1256. }
  1257. if (!empty($row->getEnddate())) {
  1258. $event['end'] = $this->formatEventDate($row->getEnddate()->format('Y-m-d h:i:s'));
  1259. $event['end_date_localtime'] = api_get_local_time($row->getEnddate());
  1260. }
  1261. $event['description'] = $row->getText();
  1262. $event['allDay'] = $row->getAllDay();
  1263. $event['parent_event_id'] = 0;
  1264. $event['has_children'] = 0;
  1265. $my_events[] = $event;
  1266. $this->events[] = $event;
  1267. }
  1268. }
  1269. return $my_events;
  1270. }
  1271. /**
  1272. * Get user/group list per event.
  1273. *
  1274. * @param int $eventId
  1275. * @param int $courseId
  1276. * @param integer $sessionId
  1277. * @paraù int $sessionId
  1278. *
  1279. * @return array
  1280. */
  1281. public function getUsersAndGroupSubscribedToEvent($eventId, $courseId, $sessionId)
  1282. {
  1283. $eventId = intval($eventId);
  1284. $courseId = intval($courseId);
  1285. $sessionId = intval($sessionId);
  1286. $sessionCondition = "ip.session_id = $sessionId";
  1287. if (empty($sessionId)) {
  1288. $sessionCondition = " (ip.session_id = 0 OR ip.session_id IS NULL) ";
  1289. }
  1290. $tlb_course_agenda = Database::get_course_table(TABLE_AGENDA);
  1291. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1292. // Get sent_tos
  1293. $sql = "SELECT DISTINCT to_user_id, to_group_id
  1294. FROM $tbl_property ip
  1295. INNER JOIN $tlb_course_agenda agenda
  1296. ON (
  1297. ip.ref = agenda.id AND
  1298. ip.c_id = agenda.c_id AND
  1299. ip.tool = '".TOOL_CALENDAR_EVENT."'
  1300. )
  1301. WHERE
  1302. ref = $eventId AND
  1303. ip.visibility = '1' AND
  1304. ip.c_id = $courseId AND
  1305. $sessionCondition
  1306. ";
  1307. $result = Database::query($sql);
  1308. $users = array();
  1309. $groups = array();
  1310. $everyone = false;
  1311. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1312. if (!empty($row['to_group_id'])) {
  1313. $groups[] = $row['to_group_id'];
  1314. }
  1315. if (!empty($row['to_user_id'])) {
  1316. $users[] = $row['to_user_id'];
  1317. }
  1318. if (empty($groups) && empty($users)) {
  1319. if ($row['to_group_id'] == 0) {
  1320. $everyone = true;
  1321. }
  1322. }
  1323. }
  1324. return array(
  1325. 'everyone' => $everyone,
  1326. 'users' => $users,
  1327. 'groups' => $groups
  1328. );
  1329. }
  1330. /**
  1331. * @param int $start
  1332. * @param int $end
  1333. * @param int $sessionId
  1334. * @param int $userId
  1335. * @param string $color
  1336. *
  1337. * @return array
  1338. */
  1339. public function getSessionEvents($start, $end, $sessionId = 0, $userId = 0, $color = '')
  1340. {
  1341. $courses = SessionManager::get_course_list_by_session_id($sessionId);
  1342. if (!empty($courses)) {
  1343. foreach ($courses as $course) {
  1344. $this->getCourseEvents(
  1345. $start,
  1346. $end,
  1347. $course,
  1348. 0,
  1349. $sessionId,
  1350. 0,
  1351. $color
  1352. );
  1353. }
  1354. }
  1355. }
  1356. /**
  1357. * @param int $start
  1358. * @param int $end
  1359. * @param array $courseInfo
  1360. * @param int $groupId
  1361. * @param int $session_id
  1362. * @param int $user_id
  1363. * @param string $color
  1364. *
  1365. * @return array
  1366. */
  1367. public function getCourseEvents(
  1368. $start,
  1369. $end,
  1370. $courseInfo,
  1371. $groupId = 0,
  1372. $session_id = 0,
  1373. $user_id = 0,
  1374. $color = ''
  1375. ) {
  1376. $start = isset($start) && !empty($start) ? api_get_utc_datetime(intval($start)) : null;
  1377. $end = isset($end) && !empty($end) ? api_get_utc_datetime(intval($end)) : null;
  1378. if (empty($courseInfo)) {
  1379. return array();
  1380. }
  1381. $course_id = $courseInfo['real_id'];
  1382. if (empty($course_id)) {
  1383. return array();
  1384. }
  1385. $session_id = intval($session_id);
  1386. $user_id = intval($user_id);
  1387. $groupList = GroupManager::get_group_list(null, $courseInfo['code']);
  1388. $group_name_list = array();
  1389. if (!empty($groupList)) {
  1390. foreach ($groupList as $group) {
  1391. $group_name_list[$group['id']] = $group['name'];
  1392. }
  1393. }
  1394. if (!empty($groupId)) {
  1395. if (!api_is_allowed_to_edit()) {
  1396. $user_id = api_get_user_id();
  1397. $group_memberships = GroupManager::get_group_ids(
  1398. $course_id,
  1399. $user_id
  1400. );
  1401. } else {
  1402. $group_memberships = GroupManager::get_group_ids(
  1403. $course_id,
  1404. $user_id
  1405. );
  1406. }
  1407. } else {
  1408. // if no group was defined but I am a student reviewing his agenda,
  1409. // group events should show, so we should fetch those groups to which
  1410. // I belong
  1411. if (!api_is_allowed_to_edit()) {
  1412. $user_id = api_get_user_id();
  1413. $group_memberships = GroupManager::get_group_ids(
  1414. $course_id,
  1415. $user_id
  1416. );
  1417. } else {
  1418. // If no group was defined and I am a teacher/admin reviewing
  1419. // someone else's agenda, we should fetch this person's groups
  1420. $group_memberships = GroupManager::get_group_ids(
  1421. $course_id,
  1422. $user_id
  1423. );
  1424. }
  1425. }
  1426. $tlb_course_agenda = Database::get_course_table(TABLE_AGENDA);
  1427. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1428. if (!empty($groupId)) {
  1429. $group_memberships = array($groupId);
  1430. }
  1431. if (is_array($group_memberships) && count($group_memberships) > 0) {
  1432. if (api_is_allowed_to_edit()) {
  1433. if (!empty($groupId)) {
  1434. $where_condition = "( ip.to_group_id IN (".implode(", ", $group_memberships).") ) ";
  1435. } else {
  1436. if (!empty($user_id)) {
  1437. $where_condition = "( ip.to_user_id = $user_id OR ip.to_user_id IS NULL OR (ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) ) ";
  1438. } else {
  1439. $where_condition = "( ip.to_group_id IN (0, ".implode(", ", $group_memberships).") ) ";
  1440. }
  1441. }
  1442. } else {
  1443. $where_condition = "( ip.to_user_id = $user_id OR ip.to_user_id IS NULL OR (ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) ) ";
  1444. }
  1445. if (empty($session_id)) {
  1446. $sessionCondition = "
  1447. (
  1448. agenda.session_id = 0 AND (ip.session_id IS NULL OR ip.session_id = 0)
  1449. ) ";
  1450. } else {
  1451. $sessionCondition = "
  1452. (
  1453. agenda.session_id = $session_id AND
  1454. ip.session_id = $session_id
  1455. ) ";
  1456. }
  1457. $sql = "SELECT DISTINCT
  1458. agenda.*,
  1459. ip.visibility,
  1460. ip.to_group_id,
  1461. ip.insert_user_id,
  1462. ip.ref,
  1463. to_user_id
  1464. FROM $tlb_course_agenda agenda
  1465. INNER JOIN $tbl_property ip
  1466. ON (
  1467. agenda.id = ip.ref AND
  1468. agenda.c_id = ip.c_id AND
  1469. ip.tool = '".TOOL_CALENDAR_EVENT."'
  1470. )
  1471. WHERE
  1472. $where_condition AND
  1473. ip.visibility = '1' AND
  1474. agenda.c_id = $course_id AND
  1475. ip.c_id = agenda.c_id AND
  1476. $sessionCondition
  1477. ";
  1478. } else {
  1479. $visibilityCondition = " ip.visibility='1' AND ";
  1480. if (api_is_allowed_to_edit()) {
  1481. if ($user_id == 0) {
  1482. $where_condition = '';
  1483. } else {
  1484. $where_condition = " (ip.to_user_id = ".$user_id." OR ip.to_user_id IS NULL) AND ip.to_group_id IS NULL AND ";
  1485. }
  1486. $visibilityCondition = " (ip.visibility IN ('1', '0')) AND ";
  1487. } else {
  1488. $where_condition = " ( (ip.to_user_id = ".api_get_user_id()." OR ip.to_user_id IS NULL) AND ip.to_group_id IS NULL) AND ";
  1489. }
  1490. if (empty($session_id)) {
  1491. $sessionCondition = "
  1492. (
  1493. agenda.session_id = 0 AND
  1494. (ip.session_id IS NULL OR ip.session_id = 0)
  1495. ) ";
  1496. } else {
  1497. $sessionCondition = "
  1498. (
  1499. agenda.session_id = $session_id AND
  1500. ip.session_id = $session_id
  1501. ) ";
  1502. }
  1503. $sql = "SELECT DISTINCT
  1504. agenda.*,
  1505. ip.visibility,
  1506. ip.to_group_id,
  1507. ip.insert_user_id,
  1508. ip.ref,
  1509. to_user_id
  1510. FROM $tlb_course_agenda agenda
  1511. INNER JOIN $tbl_property ip
  1512. ON (agenda.id = ip.ref AND agenda.c_id = ip.c_id AND ip.tool='".TOOL_CALENDAR_EVENT."' )
  1513. WHERE
  1514. $where_condition
  1515. $visibilityCondition
  1516. agenda.c_id = $course_id AND
  1517. $sessionCondition
  1518. ";
  1519. }
  1520. $dateCondition = null;
  1521. if (!empty($start) && !empty($end)) {
  1522. $dateCondition .= "AND (
  1523. agenda.start_date BETWEEN '".$start."' AND '".$end."' OR
  1524. agenda.end_date BETWEEN '".$start."' AND '".$end."' OR
  1525. (
  1526. agenda.start_date IS NOT NULL AND agenda.end_date IS NOT NULL AND
  1527. YEAR(agenda.start_date) = YEAR(agenda.end_date) AND
  1528. MONTH('$start') BETWEEN MONTH(agenda.start_date) AND MONTH(agenda.end_date)
  1529. )
  1530. )";
  1531. }
  1532. $sql .= $dateCondition;
  1533. $result = Database::query($sql);
  1534. $coachCanEdit = false;
  1535. if (!empty($session_id)) {
  1536. $coachCanEdit = api_is_coach($session_id, $course_id) || api_is_platform_admin();
  1537. }
  1538. if (Database::num_rows($result)) {
  1539. $eventsAdded = array_column($this->events, 'unique_id');
  1540. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1541. $event = array();
  1542. $event['id'] = 'course_'.$row['id'];
  1543. $event['unique_id'] = $row['iid'];
  1544. // To avoid doubles
  1545. if (in_array($event['unique_id'], $eventsAdded)) {
  1546. continue;
  1547. }
  1548. $eventsAdded[] = $event['unique_id'];
  1549. $eventId = $row['ref'];
  1550. $items = $this->getUsersAndGroupSubscribedToEvent(
  1551. $eventId,
  1552. $course_id,
  1553. $this->sessionId
  1554. );
  1555. $group_to_array = $items['groups'];
  1556. $user_to_array = $items['users'];
  1557. $attachmentList = $this->getAttachmentList($row['id'], $courseInfo);
  1558. $event['attachment'] = '';
  1559. if (!empty($attachmentList)) {
  1560. foreach ($attachmentList as $attachment) {
  1561. $has_attachment = Display::return_icon('attachment.gif', get_lang('Attachment'));
  1562. $user_filename = $attachment['filename'];
  1563. $url = api_get_path(WEB_CODE_PATH).'calendar/download.php?file='.$attachment['path'].'&course_id='.$course_id.'&'.api_get_cidreq();
  1564. $event['attachment'] .= $has_attachment.Display::url($user_filename, $url).'<br />';
  1565. }
  1566. }
  1567. $event['title'] = $row['title'];
  1568. $event['className'] = 'course';
  1569. $event['allDay'] = 'false';
  1570. $event['course_id'] = $course_id;
  1571. $event['borderColor'] = $event['backgroundColor'] = $this->event_course_color;
  1572. $sessionInfo = [];
  1573. if (isset($row['session_id']) && !empty($row['session_id'])) {
  1574. $sessionInfo = api_get_session_info($session_id);
  1575. $event['borderColor'] = $event['backgroundColor'] = $this->event_session_color;
  1576. }
  1577. $event['session_name'] = isset($sessionInfo['name']) ? $sessionInfo['name'] : '';
  1578. $event['course_name'] = isset($courseInfo['title']) ? $courseInfo['title'] : '';
  1579. if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
  1580. $event['borderColor'] = $event['backgroundColor'] = $this->event_group_color;
  1581. }
  1582. if (!empty($color)) {
  1583. $event['borderColor'] = $event['backgroundColor'] = $color;
  1584. }
  1585. if (isset($row['color']) && !empty($row['color'])) {
  1586. $event['borderColor'] = $event['backgroundColor'] = $row['color'];
  1587. }
  1588. $event['editable'] = false;
  1589. if (api_is_allowed_to_edit() && $this->type == 'course') {
  1590. $event['editable'] = true;
  1591. if (!empty($session_id)) {
  1592. if ($coachCanEdit == false) {
  1593. $event['editable'] = false;
  1594. }
  1595. }
  1596. }
  1597. if (!empty($row['start_date'])) {
  1598. $event['start'] = $this->formatEventDate($row['start_date']);
  1599. $event['start_date_localtime'] = api_get_local_time($row['start_date']);
  1600. }
  1601. if (!empty($row['end_date'])) {
  1602. $event['end'] = $this->formatEventDate($row['end_date']);
  1603. $event['end_date_localtime'] = api_get_local_time($row['end_date']);
  1604. }
  1605. $event['sent_to'] = '';
  1606. $event['type'] = 'course';
  1607. if ($row['session_id'] != 0) {
  1608. $event['type'] = 'session';
  1609. }
  1610. // Event Sent to a group?
  1611. if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
  1612. $sent_to = array();
  1613. if (!empty($group_to_array)) {
  1614. foreach ($group_to_array as $group_item) {
  1615. $sent_to[] = $group_name_list[$group_item];
  1616. }
  1617. }
  1618. $sent_to = implode('@@', $sent_to);
  1619. $sent_to = str_replace('@@', '</div><div class="label_tag notice">', $sent_to);
  1620. $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
  1621. $event['type'] = 'group';
  1622. }
  1623. // Event sent to a user?
  1624. if (isset($row['to_user_id'])) {
  1625. $sent_to = array();
  1626. if (!empty($user_to_array)) {
  1627. foreach ($user_to_array as $item) {
  1628. $user_info = api_get_user_info($item);
  1629. // Add username as tooltip for $event['sent_to'] - ref #4226
  1630. $username = api_htmlentities(sprintf(get_lang('LoginX'), $user_info['username']), ENT_QUOTES);
  1631. $sent_to[] = "<span title='".$username."'>".$user_info['complete_name']."</span>";
  1632. }
  1633. }
  1634. $sent_to = implode('@@', $sent_to);
  1635. $sent_to = str_replace('@@', '</div><div class="label_tag notice">', $sent_to);
  1636. $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
  1637. }
  1638. //Event sent to everyone!
  1639. if (empty($event['sent_to'])) {
  1640. $event['sent_to'] = '<div class="label_tag notice">'.get_lang('Everyone').'</div>';
  1641. }
  1642. $event['description'] = $row['content'];
  1643. $event['visibility'] = $row['visibility'];
  1644. $event['real_id'] = $row['id'];
  1645. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  1646. $event['parent_event_id'] = $row['parent_event_id'];
  1647. $event['has_children'] = $this->hasChildren($row['id'], $course_id) ? 1 : 0;
  1648. $event['comment'] = $row['comment'];
  1649. $this->events[] = $event;
  1650. }
  1651. }
  1652. return $this->events;
  1653. }
  1654. /**
  1655. * @param int $start tms
  1656. * @param int $end tms
  1657. * @return array
  1658. */
  1659. public function getPlatformEvents($start, $end)
  1660. {
  1661. $start = isset($start) && !empty($start) ? api_get_utc_datetime(intval($start)) : null;
  1662. $end = isset($end) && !empty($end) ? api_get_utc_datetime(intval($end)) : null;
  1663. $dateCondition = '';
  1664. if (!empty($start) && !empty($end)) {
  1665. $dateCondition .= "AND (
  1666. start_date BETWEEN '".$start."' AND '".$end."' OR
  1667. end_date BETWEEN '".$start."' AND '".$end."' OR
  1668. (
  1669. start_date IS NOT NULL AND end_date IS NOT NULL AND
  1670. YEAR(start_date) = YEAR(end_date) AND
  1671. MONTH('$start') BETWEEN MONTH(start_date) AND MONTH(end_date)
  1672. )
  1673. )";
  1674. }
  1675. $access_url_id = api_get_current_access_url_id();
  1676. $sql = "SELECT *
  1677. FROM ".$this->tbl_global_agenda."
  1678. WHERE access_url_id = $access_url_id
  1679. $dateCondition";
  1680. $result = Database::query($sql);
  1681. $my_events = array();
  1682. if (Database::num_rows($result)) {
  1683. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1684. $event = array();
  1685. $event['id'] = 'platform_'.$row['id'];
  1686. $event['title'] = $row['title'];
  1687. $event['className'] = 'platform';
  1688. $event['allDay'] = 'false';
  1689. $event['borderColor'] = $event['backgroundColor'] = $this->event_platform_color;
  1690. $event['editable'] = false;
  1691. $event['type'] = 'admin';
  1692. if (api_is_platform_admin() && $this->type == 'admin') {
  1693. $event['editable'] = true;
  1694. }
  1695. if (!empty($row['start_date'])) {
  1696. $event['start'] = $this->formatEventDate($row['start_date']);
  1697. $event['start_date_localtime'] = api_get_local_time($row['start_date']);
  1698. }
  1699. if (!empty($row['end_date'])) {
  1700. $event['end'] = $this->formatEventDate($row['end_date']);
  1701. $event['end_date_localtime'] = api_get_local_time($row['end_date']);
  1702. }
  1703. $event['description'] = $row['content'];
  1704. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  1705. $event['parent_event_id'] = 0;
  1706. $event['has_children'] = 0;
  1707. $my_events[] = $event;
  1708. $this->events[] = $event;
  1709. }
  1710. }
  1711. return $my_events;
  1712. }
  1713. /**
  1714. * Format needed for the Fullcalendar js lib
  1715. *
  1716. * @param string $utcTime
  1717. * @return bool|string
  1718. */
  1719. private function formatEventDate($utcTime)
  1720. {
  1721. $utcTimeZone = new DateTimeZone('UTC');
  1722. $platformTimeZone = new DateTimeZone(api_get_timezone());
  1723. $eventDate = new DateTime($utcTime, $utcTimeZone);
  1724. $eventDate->setTimezone($platformTimeZone);
  1725. return $eventDate->format(DateTime::ISO8601);
  1726. }
  1727. /**
  1728. * @param FormValidator $form
  1729. * @param array $groupList
  1730. * @param array $userList
  1731. * @param array $sendTo array('users' => [1, 2], 'groups' => [3, 4])
  1732. * @param array $attributes
  1733. * @param bool $addOnlyItemsInSendTo
  1734. * @param bool $required
  1735. */
  1736. public function setSendToSelect(
  1737. $form,
  1738. $groupList = [],
  1739. $userList = [],
  1740. $sendTo = [],
  1741. $attributes = [],
  1742. $addOnlyItemsInSendTo = false,
  1743. $required = false
  1744. ) {
  1745. $params = array(
  1746. 'id' => 'users_to_send_id',
  1747. 'data-placeholder' => get_lang('Select'),
  1748. 'multiple' => 'multiple',
  1749. 'class' => 'multiple-select'
  1750. );
  1751. if (!empty($attributes)) {
  1752. $params = array_merge($params, $attributes);
  1753. if (empty($params['multiple'])) {
  1754. unset($params['multiple']);
  1755. }
  1756. }
  1757. $sendToGroups = isset($sendTo['groups']) ? $sendTo['groups'] : array();
  1758. $sendToUsers = isset($sendTo['users']) ? $sendTo['users'] : array();
  1759. /** @var HTML_QuickForm_select $select */
  1760. $select = $form->addSelect('users_to_send', get_lang('To'), null, $params);
  1761. if ($required) {
  1762. $form->setRequired($select);
  1763. }
  1764. $selectedEveryoneOptions = array();
  1765. if (isset($sendTo['everyone']) && $sendTo['everyone']) {
  1766. $selectedEveryoneOptions = array('selected');
  1767. $sendToUsers = array();
  1768. }
  1769. $select->addOption(get_lang('Everyone'), 'everyone', $selectedEveryoneOptions);
  1770. $options = array();
  1771. if (is_array($groupList)) {
  1772. foreach ($groupList as $group) {
  1773. $count_users = isset($group['count_users']) ? $group['count_users'] : $group['userNb'];
  1774. $count_users = " &ndash; $count_users ".get_lang('Users');
  1775. $option = array(
  1776. 'text' => $group['name'].$count_users,
  1777. 'value' => "GROUP:".$group['id']
  1778. );
  1779. $selected = in_array($group['id'], $sendToGroups) ? true : false;
  1780. if ($selected) {
  1781. $option['selected'] = 'selected';
  1782. }
  1783. if ($addOnlyItemsInSendTo) {
  1784. if ($selected) {
  1785. $options[] = $option;
  1786. }
  1787. } else {
  1788. $options[] = $option;
  1789. }
  1790. }
  1791. $select->addOptGroup($options, get_lang('Groups'));
  1792. }
  1793. // adding the individual users to the select form
  1794. if (is_array($userList)) {
  1795. $options = array();
  1796. foreach ($userList as $user) {
  1797. if ($user['status'] == ANONYMOUS) {
  1798. continue;
  1799. }
  1800. $option = array(
  1801. 'text' => api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')',
  1802. 'value' => "USER:".$user['user_id']
  1803. );
  1804. $selected = in_array($user['user_id'], $sendToUsers) ? true : false;
  1805. if ($selected) {
  1806. $option['selected'] = 'selected';
  1807. }
  1808. if ($addOnlyItemsInSendTo) {
  1809. if ($selected) {
  1810. $options[] = $option;
  1811. }
  1812. } else {
  1813. $options[] = $option;
  1814. }
  1815. }
  1816. $select->addOptGroup($options, get_lang('Users'));
  1817. }
  1818. }
  1819. /**
  1820. * Separates the users and groups array
  1821. * users have a value USER:XXX (with XXX the user id
  1822. * groups have a value GROUP:YYY (with YYY the group id)
  1823. * use the 'everyone' key
  1824. * @author Julio Montoya based in separate_users_groups in agenda.inc.php
  1825. * @param array $to
  1826. * @return array
  1827. */
  1828. public function parseSendToArray($to)
  1829. {
  1830. $groupList = array();
  1831. $userList = array();
  1832. $sendTo = null;
  1833. $sendTo['everyone'] = false;
  1834. if (is_array($to) && count($to) > 0) {
  1835. foreach ($to as $item) {
  1836. if ($item == 'everyone') {
  1837. $sendTo['everyone'] = true;
  1838. } else {
  1839. list($type, $id) = explode(':', $item);
  1840. switch ($type) {
  1841. case 'GROUP':
  1842. $groupList[] = $id;
  1843. break;
  1844. case 'USER':
  1845. $userList[] = $id;
  1846. break;
  1847. }
  1848. }
  1849. }
  1850. $sendTo['groups'] = $groupList;
  1851. $sendTo['users'] = $userList;
  1852. }
  1853. return $sendTo;
  1854. }
  1855. /**
  1856. * @param array $params
  1857. * @return FormValidator
  1858. */
  1859. public function getForm($params = [])
  1860. {
  1861. $action = isset($params['action']) ? Security::remove_XSS($params['action']) : null;
  1862. $id = isset($params['id']) ? intval($params['id']) : null;
  1863. if ($this->type == 'course') {
  1864. $url = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&id='.$id.'&type='.$this->type;
  1865. } else {
  1866. $url = api_get_self().'?action='.$action.'&id='.$id.'&type='.$this->type;
  1867. }
  1868. $form = new FormValidator(
  1869. 'add_event',
  1870. 'post',
  1871. $url,
  1872. null,
  1873. array('enctype' => 'multipart/form-data')
  1874. );
  1875. $idAttach = isset($params['id_attach']) ? intval($params['id_attach']) : null;
  1876. $groupId = api_get_group_id();
  1877. if ($id) {
  1878. $form_title = get_lang('ModifyCalendarItem');
  1879. } else {
  1880. $form_title = get_lang('AddCalendarItem');
  1881. }
  1882. $form->addElement('header', $form_title);
  1883. $form->addElement('hidden', 'id', $id);
  1884. $form->addElement('hidden', 'action', $action);
  1885. $form->addElement('hidden', 'id_attach', $idAttach);
  1886. $isSubEventEdition = false;
  1887. $isParentFromSerie = false;
  1888. $showAttachmentForm = true;
  1889. if ($this->type == 'course') {
  1890. // Edition mode.
  1891. if (!empty($id)) {
  1892. $showAttachmentForm = false;
  1893. if (isset($params['parent_event_id']) && !empty($params['parent_event_id'])) {
  1894. $isSubEventEdition = true;
  1895. }
  1896. if (!empty($params['repeat_info'])) {
  1897. $isParentFromSerie = true;
  1898. }
  1899. }
  1900. }
  1901. if ($isSubEventEdition) {
  1902. $form->addElement(
  1903. 'label',
  1904. null,
  1905. Display::return_message(get_lang('EditingThisEventWillRemoveItFromTheSerie'), 'warning')
  1906. );
  1907. }
  1908. $form->addElement('text', 'title', get_lang('ItemTitle'));
  1909. if (isset($groupId) && !empty($groupId)) {
  1910. $form->addElement('hidden', 'selected_form[0]', "GROUP:'.$groupId.'");
  1911. $form->addElement('hidden', 'to', 'true');
  1912. } else {
  1913. $sendTo = isset($params['send_to']) ? $params['send_to'] : ['everyone' => true];
  1914. if ($this->type == 'course') {
  1915. $this->showToForm($form, $sendTo, array(), false, true);
  1916. }
  1917. }
  1918. $form->addDateRangePicker('date_range', get_lang('DateRange'), false, array('id' => 'date_range'));
  1919. $form->addElement('checkbox', 'all_day', null, get_lang('AllDay'));
  1920. if ($this->type == 'course') {
  1921. $repeat = $form->addElement('checkbox', 'repeat', null, get_lang('RepeatEvent'), array('onclick' => 'return plus_repeated_event();'));
  1922. $form->addElement('html', '<div id="options2" style="display:none">');
  1923. $form->addElement('select', 'repeat_type', get_lang('RepeatType'), self::getRepeatTypes());
  1924. $form->addElement('date_picker', 'repeat_end_day', get_lang('RepeatEnd'), array('id' => 'repeat_end_date_form'));
  1925. if ($isSubEventEdition || $isParentFromSerie) {
  1926. if ($isSubEventEdition) {
  1927. $parentEvent = $params['parent_info'];
  1928. $repeatInfo = $parentEvent['repeat_info'];
  1929. } else {
  1930. $repeatInfo = $params['repeat_info'];
  1931. }
  1932. $params['repeat'] = 1;
  1933. $params['repeat_type'] = $repeatInfo['cal_type'];
  1934. $params['repeat_end_day'] = substr(api_get_local_time($repeatInfo['cal_end']), 0, 10);
  1935. $form->freeze(array('repeat_type', 'repeat_end_day'));
  1936. $repeat->_attributes['disabled'] = 'disabled';
  1937. }
  1938. $form->addElement('html', '</div>');
  1939. }
  1940. if (!empty($id)) {
  1941. if (empty($params['end_date'])) {
  1942. $params['date_range'] = $params['end_date'];
  1943. }
  1944. $params['date_range'] =
  1945. substr(api_get_local_time($params['start_date']), 0, 16).' / '.
  1946. substr(api_get_local_time($params['end_date']), 0, 16);
  1947. }
  1948. if (!api_is_allowed_to_edit(null, true)) {
  1949. $toolbar = 'AgendaStudent';
  1950. } else {
  1951. $toolbar = 'Agenda';
  1952. }
  1953. $form->addHtmlEditor(
  1954. 'content',
  1955. get_lang('Description'),
  1956. null,
  1957. null,
  1958. array('ToolbarSet' => $toolbar, 'Width' => '100%', 'Height' => '200')
  1959. );
  1960. if ($this->type == 'course') {
  1961. $form->addElement('textarea', 'comment', get_lang('Comment'));
  1962. $form->addLabel(
  1963. get_lang('FilesAttachment'),
  1964. '<span id="filepaths">
  1965. <div id="filepath_1">
  1966. <input type="file" name="attach_1"/><br />
  1967. '.get_lang('Description').'&nbsp;&nbsp;<input type="text" name="legend[]" /><br /><br />
  1968. </div>
  1969. </span>'
  1970. );
  1971. $form->addLabel(
  1972. '',
  1973. '<span id="link-more-attach">
  1974. <a href="javascript://" onclick="return add_image_form()">'.
  1975. get_lang('AddOneMoreFile').'</a>
  1976. </span>&nbsp;('.sprintf(get_lang('MaximunFileSizeX'),format_file_size(api_get_setting('message_max_upload_filesize'))).')'
  1977. );
  1978. if (isset($params['attachment']) && !empty($params['attachment'])) {
  1979. $attachmentList = $params['attachment'];
  1980. foreach ($attachmentList as $attachment) {
  1981. $params['file_comment'] = $attachment['comment'];
  1982. if (!empty($attachment['path'])) {
  1983. $form->addElement(
  1984. 'checkbox',
  1985. 'delete_attachment['.$attachment['id'].']',
  1986. null,
  1987. get_lang('DeleteAttachment').': '.$attachment['filename']
  1988. );
  1989. }
  1990. }
  1991. }
  1992. $form->addElement('textarea', 'file_comment', get_lang('FileComment'));
  1993. }
  1994. if (empty($id)) {
  1995. $form->addElement(
  1996. 'checkbox',
  1997. 'add_announcement',
  1998. null,
  1999. get_lang('AddAnnouncement').'&nbsp('.get_lang('SendMail').')'
  2000. );
  2001. }
  2002. if ($id) {
  2003. $form->addButtonUpdate(get_lang('ModifyEvent'));
  2004. } else {
  2005. $form->addButtonSave(get_lang('AgendaAdd'));
  2006. }
  2007. $form->setDefaults($params);
  2008. $form->addRule('date_range', get_lang('ThisFieldIsRequired'), 'required');
  2009. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  2010. return $form;
  2011. }
  2012. /**
  2013. * @param FormValidator $form
  2014. * @param array $sendTo array('everyone' => false, 'users' => [1, 2], 'groups' => [3, 4])
  2015. * @param array $attributes
  2016. * @param bool $addOnlyItemsInSendTo
  2017. * @param bool $required
  2018. * @return bool
  2019. */
  2020. public function showToForm(
  2021. $form,
  2022. $sendTo = array(),
  2023. $attributes = array(),
  2024. $addOnlyItemsInSendTo = false,
  2025. $required = false
  2026. ) {
  2027. if ($this->type != 'course') {
  2028. return false;
  2029. }
  2030. $order = 'lastname';
  2031. if (api_is_western_name_order()) {
  2032. $order = 'firstname';
  2033. }
  2034. $userList = CourseManager::get_user_list_from_course_code(
  2035. api_get_course_id(),
  2036. $this->sessionId,
  2037. null,
  2038. $order
  2039. );
  2040. $groupList = CourseManager::get_group_list_of_course(
  2041. api_get_course_id(),
  2042. $this->sessionId
  2043. );
  2044. $this->setSendToSelect(
  2045. $form,
  2046. $groupList,
  2047. $userList,
  2048. $sendTo,
  2049. $attributes,
  2050. $addOnlyItemsInSendTo,
  2051. $required
  2052. );
  2053. return true;
  2054. }
  2055. /**
  2056. * @param int $id
  2057. * @param int $visibility 0= invisible, 1 visible
  2058. * @param array $courseInfo
  2059. * @param int $userId
  2060. */
  2061. public static function changeVisibility($id, $visibility, $courseInfo, $userId = null)
  2062. {
  2063. $id = intval($id);
  2064. if (empty($userId)) {
  2065. $userId = api_get_user_id();
  2066. } else {
  2067. $userId = intval($userId);
  2068. }
  2069. if ($visibility == 0) {
  2070. api_item_property_update(
  2071. $courseInfo,
  2072. TOOL_CALENDAR_EVENT,
  2073. $id,
  2074. 'invisible',
  2075. $userId
  2076. );
  2077. } else {
  2078. api_item_property_update(
  2079. $courseInfo,
  2080. TOOL_CALENDAR_EVENT,
  2081. $id,
  2082. 'visible',
  2083. $userId
  2084. );
  2085. }
  2086. }
  2087. /**
  2088. * Get repeat types
  2089. * @return array
  2090. */
  2091. public static function getRepeatTypes()
  2092. {
  2093. return array(
  2094. 'daily' => get_lang('RepeatDaily'),
  2095. 'weekly' => get_lang('RepeatWeekly'),
  2096. 'monthlyByDate' => get_lang('RepeatMonthlyByDate'),
  2097. //monthlyByDay"> get_lang('RepeatMonthlyByDay');
  2098. //monthlyByDayR' => get_lang('RepeatMonthlyByDayR'),
  2099. 'yearly' => get_lang('RepeatYearly')
  2100. );
  2101. }
  2102. /**
  2103. * Show a list with all the attachments according to the post's id
  2104. * @param int $eventId
  2105. * @param array $courseInfo
  2106. * @return array with the post info
  2107. */
  2108. public function getAttachmentList($eventId, $courseInfo)
  2109. {
  2110. $tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  2111. $courseId = intval($courseInfo['real_id']);
  2112. $eventId = intval($eventId);
  2113. $sql = "SELECT id, path, filename, comment
  2114. FROM $tableAttachment
  2115. WHERE
  2116. c_id = $courseId AND
  2117. agenda_id = $eventId";
  2118. $result = Database::query($sql);
  2119. $list = array();
  2120. if (Database::num_rows($result) != 0) {
  2121. $list = Database::store_result($result, 'ASSOC');
  2122. }
  2123. return $list;
  2124. }
  2125. /**
  2126. * Show a list with all the attachments according to the post's id
  2127. * @param int $attachmentId
  2128. * @param int $eventId
  2129. * @param array $courseInfo
  2130. * @return array with the post info
  2131. */
  2132. public function getAttachment($attachmentId, $eventId, $courseInfo)
  2133. {
  2134. $tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  2135. $courseId = intval($courseInfo['real_id']);
  2136. $eventId = intval($eventId);
  2137. $attachmentId = intval($attachmentId);
  2138. $row = array();
  2139. $sql = "SELECT id, path, filename, comment
  2140. FROM $tableAttachment
  2141. WHERE
  2142. c_id = $courseId AND
  2143. agenda_id = $eventId AND
  2144. id = $attachmentId
  2145. ";
  2146. $result = Database::query($sql);
  2147. if (Database::num_rows($result) != 0) {
  2148. $row = Database::fetch_array($result, 'ASSOC');
  2149. }
  2150. return $row;
  2151. }
  2152. /**
  2153. * Add an attachment file into agenda
  2154. * @param int $eventId
  2155. * @param array $fileUserUpload ($_FILES['user_upload'])
  2156. * @param string $comment about file
  2157. * @param array $courseInfo
  2158. * @return string
  2159. */
  2160. public function addAttachment($eventId, $fileUserUpload, $comment, $courseInfo)
  2161. {
  2162. $agenda_table_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  2163. $eventId = intval($eventId);
  2164. // Storing the attachments
  2165. $upload_ok = false;
  2166. if (!empty($fileUserUpload['name'])) {
  2167. $upload_ok = process_uploaded_file($fileUserUpload);
  2168. }
  2169. if (!empty($upload_ok)) {
  2170. $courseDir = $courseInfo['directory'].'/upload/calendar';
  2171. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  2172. $uploadDir = $sys_course_path.$courseDir;
  2173. // Try to add an extension to the file if it hasn't one
  2174. $new_file_name = add_ext_on_mime(
  2175. stripslashes($fileUserUpload['name']),
  2176. $fileUserUpload['type']
  2177. );
  2178. // user's file name
  2179. $file_name = $fileUserUpload['name'];
  2180. if (!filter_extension($new_file_name)) {
  2181. return Display::return_message(
  2182. get_lang('UplUnableToSaveFileFilteredExtension'),
  2183. 'error'
  2184. );
  2185. } else {
  2186. $new_file_name = uniqid('');
  2187. $new_path = $uploadDir.'/'.$new_file_name;
  2188. $result = @move_uploaded_file($fileUserUpload['tmp_name'], $new_path);
  2189. $course_id = api_get_course_int_id();
  2190. $size = intval($fileUserUpload['size']);
  2191. // Storing the attachments if any
  2192. if ($result) {
  2193. $params = [
  2194. 'c_id' => $course_id,
  2195. 'filename' => $file_name,
  2196. 'comment' => $comment,
  2197. 'path' => $new_file_name,
  2198. 'agenda_id' => $eventId,
  2199. 'size' => $size
  2200. ];
  2201. $id = Database::insert($agenda_table_attachment, $params);
  2202. if ($id) {
  2203. $sql = "UPDATE $agenda_table_attachment
  2204. SET id = iid WHERE iid = $id";
  2205. Database::query($sql);
  2206. api_item_property_update(
  2207. $courseInfo,
  2208. 'calendar_event_attachment',
  2209. $id,
  2210. 'AgendaAttachmentAdded',
  2211. api_get_user_id()
  2212. );
  2213. }
  2214. }
  2215. }
  2216. }
  2217. }
  2218. /**
  2219. * @param int $attachmentId
  2220. * @param int $eventId
  2221. * @param array $fileUserUpload
  2222. * @param string $comment
  2223. * @param array $courseInfo
  2224. */
  2225. public function updateAttachment($attachmentId, $eventId, $fileUserUpload, $comment, $courseInfo)
  2226. {
  2227. $attachment = $this->getAttachment($attachmentId, $eventId, $courseInfo);
  2228. if (!empty($attachment)) {
  2229. $this->deleteAttachmentFile($attachmentId, $courseInfo);
  2230. }
  2231. $this->addAttachment($eventId, $fileUserUpload, $comment, $courseInfo);
  2232. }
  2233. /**
  2234. * This function delete a attachment file by id
  2235. * @param int $attachmentId
  2236. * @param array $courseInfo
  2237. * @return string
  2238. */
  2239. public function deleteAttachmentFile($attachmentId, $courseInfo)
  2240. {
  2241. $agenda_table_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  2242. $attachmentId = intval($attachmentId);
  2243. $courseId = $courseInfo['real_id'];
  2244. if (empty($courseId) || empty($attachmentId)) {
  2245. return false;
  2246. }
  2247. $sql = "DELETE FROM $agenda_table_attachment
  2248. WHERE c_id = $courseId AND id = ".$attachmentId;
  2249. $result = Database::query($sql);
  2250. // update item_property
  2251. api_item_property_update(
  2252. $courseInfo,
  2253. 'calendar_event_attachment',
  2254. $attachmentId,
  2255. 'AgendaAttachmentDeleted',
  2256. api_get_user_id()
  2257. );
  2258. if (!empty($result)) {
  2259. return Display::return_message(
  2260. get_lang("AttachmentFileDeleteSuccess"),
  2261. 'confirmation'
  2262. );
  2263. }
  2264. }
  2265. /**
  2266. * Adds x weeks to a UNIX timestamp
  2267. * @param int The timestamp
  2268. * @param int The number of weeks to add
  2269. * @param integer $timestamp
  2270. * @return int The new timestamp
  2271. */
  2272. public function addWeek($timestamp, $num = 1)
  2273. {
  2274. return $timestamp + $num * 604800;
  2275. }
  2276. /**
  2277. * Adds x months to a UNIX timestamp
  2278. * @param int The timestamp
  2279. * @param int The number of years to add
  2280. * @param integer $timestamp
  2281. * @return int The new timestamp
  2282. */
  2283. public function addMonth($timestamp, $num = 1)
  2284. {
  2285. list($y, $m, $d, $h, $n, $s) = split('/', date('Y/m/d/h/i/s', $timestamp));
  2286. if ($m + $num > 12) {
  2287. $y += floor($num / 12);
  2288. $m += $num % 12;
  2289. } else {
  2290. $m += $num;
  2291. }
  2292. return mktime($h, $n, $s, $m, $d, $y);
  2293. }
  2294. /**
  2295. * Adds x years to a UNIX timestamp
  2296. * @param int The timestamp
  2297. * @param int The number of years to add
  2298. * @param integer $timestamp
  2299. * @return int The new timestamp
  2300. */
  2301. public function addYear($timestamp, $num = 1)
  2302. {
  2303. list($y, $m, $d, $h, $n, $s) = split('/', date('Y/m/d/h/i/s', $timestamp));
  2304. return mktime($h, $n, $s, $m, $d, $y + $num);
  2305. }
  2306. /**
  2307. * @param int $eventId
  2308. * @return array
  2309. */
  2310. public function getAllRepeatEvents($eventId)
  2311. {
  2312. $events = array();
  2313. switch ($this->type) {
  2314. case 'personal':
  2315. break;
  2316. case 'course':
  2317. if (!empty($this->course['real_id'])) {
  2318. $sql = "SELECT * FROM ".$this->tbl_course_agenda."
  2319. WHERE
  2320. c_id = ".$this->course['real_id']." AND
  2321. parent_event_id = ".$eventId;
  2322. $result = Database::query($sql);
  2323. if (Database::num_rows($result)) {
  2324. while ($row = Database::fetch_array($result, 'ASSOC')) {
  2325. $events[] = $row;
  2326. }
  2327. }
  2328. }
  2329. break;
  2330. }
  2331. return $events;
  2332. }
  2333. /**
  2334. * @param int $eventId
  2335. * @param int $courseId
  2336. *
  2337. * @return bool
  2338. */
  2339. public function hasChildren($eventId, $courseId)
  2340. {
  2341. $eventId = intval($eventId);
  2342. $courseId = intval($courseId);
  2343. $sql = "SELECT count(DISTINCT(id)) as count
  2344. FROM ".$this->tbl_course_agenda."
  2345. WHERE
  2346. c_id = $courseId AND
  2347. parent_event_id = $eventId";
  2348. $result = Database::query($sql);
  2349. if (Database::num_rows($result)) {
  2350. $row = Database::fetch_array($result, 'ASSOC');
  2351. return $row['count'] > 0;
  2352. }
  2353. return false;
  2354. }
  2355. /**
  2356. * @param int $filter
  2357. * @param string $view
  2358. * @return string
  2359. */
  2360. public function displayActions($view, $filter = 0)
  2361. {
  2362. $courseInfo = api_get_course_info();
  2363. $groupInfo = GroupManager::get_group_properties(api_get_group_id());
  2364. $groupIid = isset($groupInfo['iid']) ? $groupInfo['iid'] : 0;
  2365. $actionsLeft = '';
  2366. $actionsLeft .= "<a href='".api_get_path(WEB_CODE_PATH)."calendar/agenda_js.php?type={$this->type}'>".
  2367. Display::return_icon('calendar.png', get_lang('Calendar'), '', ICON_SIZE_MEDIUM)."</a>";
  2368. $courseCondition = '';
  2369. if (!empty($courseInfo)) {
  2370. $courseCondition = api_get_cidreq();
  2371. }
  2372. $actionsLeft .= "<a href='".api_get_path(WEB_CODE_PATH)."calendar/agenda_list.php?type={$this->type}&".$courseCondition."'>".
  2373. Display::return_icon('week.png', get_lang('AgendaList'), '', ICON_SIZE_MEDIUM)."</a>";
  2374. $form = '';
  2375. if (api_is_allowed_to_edit(false, true) ||
  2376. (api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous()) && api_is_allowed_to_session_edit(false, true) ||
  2377. (GroupManager::user_has_access(api_get_user_id(), $groupIid, GroupManager::GROUP_TOOL_CALENDAR) &&
  2378. GroupManager::is_tutor_of_group(api_get_user_id(), $groupIid))
  2379. ) {
  2380. $actionsLeft .= Display::url(
  2381. Display::return_icon('new_event.png', get_lang('AgendaAdd'), '', ICON_SIZE_MEDIUM),
  2382. api_get_path(WEB_CODE_PATH)."calendar/agenda.php?".api_get_cidreq()."&action=add&type=".$this->type
  2383. );
  2384. $actionsLeft .= Display::url(
  2385. Display::return_icon('import_calendar.png', get_lang('ICalFileImport'), '', ICON_SIZE_MEDIUM),
  2386. api_get_path(WEB_CODE_PATH)."calendar/agenda.php?".api_get_cidreq()."&action=importical&type=".$this->type
  2387. );
  2388. if ($this->type === 'course') {
  2389. if (!isset($_GET['action'])) {
  2390. $form = new FormValidator(
  2391. 'form-search',
  2392. 'post',
  2393. '',
  2394. '',
  2395. array(),
  2396. FormValidator::LAYOUT_INLINE
  2397. );
  2398. $attributes = array(
  2399. 'multiple' => false,
  2400. 'id' => 'select_form_id_search'
  2401. );
  2402. $selectedValues = $this->parseAgendaFilter($filter);
  2403. $this->showToForm($form, $selectedValues, $attributes);
  2404. $form = $form->returnForm();
  2405. }
  2406. }
  2407. }
  2408. if (api_is_platform_admin() ||
  2409. api_is_teacher() ||
  2410. api_is_student_boss() ||
  2411. api_is_drh() ||
  2412. api_is_session_admin() ||
  2413. api_is_coach()
  2414. ) {
  2415. if ($this->type == 'personal') {
  2416. $form = null;
  2417. if (!isset($_GET['action'])) {
  2418. $form = new FormValidator(
  2419. 'form-search',
  2420. 'get',
  2421. api_get_self().'?type=personal&',
  2422. '',
  2423. array(),
  2424. FormValidator::LAYOUT_INLINE
  2425. );
  2426. $sessions = SessionManager::get_sessions_by_user(api_get_user_id());
  2427. $form->addHidden('type', 'personal');
  2428. $sessions = array_column($sessions, 'session_name', 'session_id');
  2429. $sessions = ['0' => get_lang('SelectAnOption')] + $sessions;
  2430. $form->addSelect(
  2431. 'session_id',
  2432. get_lang('Session'),
  2433. $sessions,
  2434. ['id' => 'session_id', 'onchange' => 'submit();']
  2435. );
  2436. $form->addButtonReset(get_lang('Reset'));
  2437. $form = $form->returnForm();
  2438. }
  2439. }
  2440. }
  2441. $actionsRight = '';
  2442. if ($view == 'calendar') {
  2443. $actionsRight .= $form;
  2444. }
  2445. $toolbar = Display::toolbarAction(
  2446. 'toolbar-agenda',
  2447. array(0 => $actionsLeft, 1 => $actionsRight),
  2448. 2,
  2449. false
  2450. );
  2451. return $toolbar;
  2452. }
  2453. /**
  2454. * @return FormValidator
  2455. */
  2456. public function getImportCalendarForm()
  2457. {
  2458. $form = new FormValidator(
  2459. 'frm_import_ical',
  2460. 'post',
  2461. api_get_self().'?action=importical&type='.$this->type,
  2462. array('enctype' => 'multipart/form-data')
  2463. );
  2464. $form->addElement('header', get_lang('ICalFileImport'));
  2465. $form->addElement('file', 'ical_import', get_lang('ICalFileImport'));
  2466. $form->addRule('ical_import', get_lang('ThisFieldIsRequired'), 'required');
  2467. $form->addButtonImport(get_lang('Import'), 'ical_submit');
  2468. return $form;
  2469. }
  2470. /**
  2471. * @param array $courseInfo
  2472. * @param $file
  2473. * @return false|string
  2474. */
  2475. public function importEventFile($courseInfo, $file)
  2476. {
  2477. $charset = api_get_system_encoding();
  2478. $filepath = api_get_path(SYS_ARCHIVE_PATH).$file['name'];
  2479. $messages = array();
  2480. if (!@move_uploaded_file($file['tmp_name'], $filepath)) {
  2481. error_log('Problem moving uploaded file: '.$file['error'].' in '.__FILE__.' line '.__LINE__);
  2482. return false;
  2483. }
  2484. $data = file_get_contents($filepath);
  2485. $trans = array(
  2486. 'DAILY' => 'daily',
  2487. 'WEEKLY' => 'weekly',
  2488. 'MONTHLY' => 'monthlyByDate',
  2489. 'YEARLY' => 'yearly'
  2490. );
  2491. $sentTo = array('everyone' => true);
  2492. $calendar = Sabre\VObject\Reader::read($data);
  2493. $currentTimeZone = api_get_timezone();
  2494. if (!empty($calendar->VEVENT)) {
  2495. foreach ($calendar->VEVENT as $event) {
  2496. $start = $event->DTSTART->getDateTime();
  2497. $end = $event->DTEND->getDateTime();
  2498. //Sabre\VObject\DateTimeParser::parseDateTime(string $dt, \Sabre\VObject\DateTimeZone $tz)
  2499. $startDateTime = api_get_local_time(
  2500. $start->format('Y-m-d H:i:s'),
  2501. $currentTimeZone,
  2502. $start->format('e')
  2503. );
  2504. $endDateTime = api_get_local_time(
  2505. $end->format('Y-m-d H:i'),
  2506. $currentTimeZone,
  2507. $end->format('e')
  2508. );
  2509. $title = api_convert_encoding((string)$event->summary, $charset, 'UTF-8');
  2510. $description = api_convert_encoding((string)$event->description, $charset, 'UTF-8');
  2511. $id = $this->addEvent(
  2512. $startDateTime,
  2513. $endDateTime,
  2514. 'false',
  2515. $title,
  2516. $description,
  2517. $sentTo
  2518. );
  2519. $messages[] = " $title - ".$startDateTime." - ".$endDateTime;
  2520. //$attendee = (string)$event->attendee;
  2521. /** @var Sabre\VObject\Property\ICalendar\Recur $repeat */
  2522. $repeat = $event->RRULE;
  2523. if ($id && !empty($repeat)) {
  2524. $repeat = $repeat->getParts();
  2525. $freq = $trans[$repeat['FREQ']];
  2526. if (isset($repeat['UNTIL']) && !empty($repeat['UNTIL'])) {
  2527. // Check if datetime or just date (strlen == 8)
  2528. if (strlen($repeat['UNTIL']) == 8) {
  2529. // Fix the datetime format to avoid exception in the next step
  2530. $repeat['UNTIL'] .= 'T000000';
  2531. }
  2532. $until = Sabre\VObject\DateTimeParser::parseDateTime($repeat['UNTIL'], new DateTimeZone($currentTimeZone));
  2533. $until = $until->format('Y-m-d H:i');
  2534. //$res = agenda_add_repeat_item($courseInfo, $id, $freq, $until, $attendee);
  2535. $this->addRepeatedItem(
  2536. $id,
  2537. $freq,
  2538. $until,
  2539. $sentTo
  2540. );
  2541. }
  2542. if (!empty($repeat['COUNT'])) {
  2543. /*$count = $repeat['COUNT'];
  2544. $interval = $repeat['INTERVAL'];
  2545. $endDate = null;
  2546. switch($freq) {
  2547. case 'daily':
  2548. $start = api_strtotime($startDateTime);
  2549. $date = new DateTime($startDateTime);
  2550. $days = $count * $interval;
  2551. var_dump($days);
  2552. $date->add(new DateInterval("P".$days."D"));
  2553. $endDate = $date->format('Y-m-d H:i');
  2554. //$endDate = $count *
  2555. for ($i = 0; $i < $count; $i++) {
  2556. $days = 86400 * 7
  2557. }
  2558. }
  2559. }*/
  2560. //$res = agenda_add_repeat_item($courseInfo, $id, $freq, $count, $attendee);
  2561. /*$this->addRepeatedItem(
  2562. $id,
  2563. $freq,
  2564. $endDate,
  2565. $sentTo
  2566. );*/
  2567. }
  2568. }
  2569. }
  2570. }
  2571. if (!empty($messages)) {
  2572. $messages = implode('<br /> ', $messages);
  2573. } else {
  2574. $messages = get_lang('NoAgendaItems');
  2575. }
  2576. return $messages;
  2577. }
  2578. /**
  2579. * Parse filter turns USER:12 to ['users' => [12])] or G:1 ['groups' => [1]]
  2580. * @param integer $filter
  2581. * @return array
  2582. */
  2583. public function parseAgendaFilter($filter)
  2584. {
  2585. $everyone = false;
  2586. $groupId = null;
  2587. $userId = null;
  2588. if ($filter == 'everyone') {
  2589. $everyone = true;
  2590. } else {
  2591. if (substr($filter, 0, 1) == 'G') {
  2592. $groupId = str_replace('GROUP:', '', $filter);
  2593. } else {
  2594. $userId = str_replace('USER:', '', $filter);
  2595. }
  2596. }
  2597. if (empty($userId) && empty($groupId)) {
  2598. $everyone = true;
  2599. }
  2600. return array(
  2601. 'everyone' => $everyone,
  2602. 'users' => array($userId),
  2603. 'groups' => array($groupId)
  2604. );
  2605. }
  2606. /**
  2607. * This function retrieves all the agenda items of all the courses the user is subscribed to
  2608. */
  2609. public static function get_myagendaitems($user_id, $courses_dbs, $month, $year)
  2610. {
  2611. $user_id = intval($user_id);
  2612. $items = array();
  2613. $my_list = array();
  2614. // get agenda-items for every course
  2615. foreach ($courses_dbs as $key => $array_course_info) {
  2616. //databases of the courses
  2617. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  2618. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2619. $group_memberships = GroupManager :: get_group_ids($array_course_info["real_id"], $user_id);
  2620. $course_user_status = CourseManager::get_user_in_course_status($user_id, $array_course_info["code"]);
  2621. // if the user is administrator of that course we show all the agenda items
  2622. if ($course_user_status == '1') {
  2623. //echo "course admin";
  2624. $sqlquery = "SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  2625. FROM ".$TABLEAGENDA." agenda,
  2626. ".$TABLE_ITEMPROPERTY." ip
  2627. WHERE agenda.id = ip.ref
  2628. AND MONTH(agenda.start_date)='".$month."'
  2629. AND YEAR(agenda.start_date)='".$year."'
  2630. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  2631. AND ip.visibility='1'
  2632. GROUP BY agenda.id
  2633. ORDER BY start_date ";
  2634. } else {
  2635. // if the user is not an administrator of that course
  2636. if (is_array($group_memberships) && count($group_memberships)>0) {
  2637. $sqlquery = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  2638. FROM ".$TABLEAGENDA." agenda,
  2639. ".$TABLE_ITEMPROPERTY." ip
  2640. WHERE agenda.id = ip.ref
  2641. AND MONTH(agenda.start_date)='".$month."'
  2642. AND YEAR(agenda.start_date)='".$year."'
  2643. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  2644. AND ( ip.to_user_id='".$user_id."' OR (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) )
  2645. AND ip.visibility='1'
  2646. ORDER BY start_date ";
  2647. } else {
  2648. $sqlquery = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  2649. FROM ".$TABLEAGENDA." agenda,
  2650. ".$TABLE_ITEMPROPERTY." ip
  2651. WHERE agenda.id = ip.ref
  2652. AND MONTH(agenda.start_date)='".$month."'
  2653. AND YEAR(agenda.start_date)='".$year."'
  2654. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  2655. AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
  2656. AND ip.visibility='1'
  2657. ORDER BY start_date ";
  2658. }
  2659. }
  2660. $result = Database::query($sqlquery);
  2661. while ($item = Database::fetch_array($result, 'ASSOC')) {
  2662. $agendaday = -1;
  2663. if (!empty($item['start_date'])) {
  2664. $item['start_date'] = api_get_local_time($item['start_date']);
  2665. $item['start_date_tms'] = api_strtotime($item['start_date']);
  2666. $agendaday = date("j", $item['start_date_tms']);
  2667. }
  2668. if (!empty($item['end_date'])) {
  2669. $item['end_date'] = api_get_local_time($item['end_date']);
  2670. }
  2671. $url = api_get_path(WEB_CODE_PATH)."calendar/agenda.php?cidReq=".urlencode($array_course_info["code"])."&day=$agendaday&month=$month&year=$year#$agendaday";
  2672. $item['url'] = $url;
  2673. $item['course_name'] = $array_course_info['title'];
  2674. $item['calendar_type'] = 'course';
  2675. $item['course_id'] = $array_course_info['course_id'];
  2676. $my_list[$agendaday][] = $item;
  2677. }
  2678. }
  2679. // sorting by hour for every day
  2680. $agendaitems = array ();
  2681. while (list ($agendaday, $tmpitems) = each($items)) {
  2682. if(!isset($agendaitems[$agendaday])) {
  2683. $agendaitems[$agendaday] = '';
  2684. }
  2685. sort($tmpitems);
  2686. while (list ($key, $val) = each($tmpitems)) {
  2687. $agendaitems[$agendaday] .= $val;
  2688. }
  2689. }
  2690. return $my_list;
  2691. }
  2692. /**
  2693. * This function retrieves one personal agenda item returns it.
  2694. * @param array The array containing existing events. We add to this array.
  2695. * @param int Day
  2696. * @param int Month
  2697. * @param int Year (4 digits)
  2698. * @param int Week number
  2699. * @param string Type of view (month_view, week_view, day_view)
  2700. * @return array The results of the database query, or null if not found
  2701. */
  2702. public static function get_global_agenda_items($agendaitems, $day = "", $month = "", $year = "", $week = "", $type)
  2703. {
  2704. $tbl_global_agenda = Database::get_main_table(
  2705. TABLE_MAIN_SYSTEM_CALENDAR
  2706. );
  2707. $month = intval($month);
  2708. $year = intval($year);
  2709. $week = intval($week);
  2710. $day = intval($day);
  2711. // 1. creating the SQL statement for getting the personal agenda items in MONTH view
  2712. $current_access_url_id = api_get_current_access_url_id();
  2713. if ($type == "month_view" or $type == "") {
  2714. // We are in month view
  2715. $sql = "SELECT * FROM ".$tbl_global_agenda." WHERE MONTH(start_date) = ".$month." AND YEAR(start_date) = ".$year." AND access_url_id = $current_access_url_id ORDER BY start_date ASC";
  2716. }
  2717. // 2. creating the SQL statement for getting the personal agenda items in WEEK view
  2718. if ($type == "week_view") { // we are in week view
  2719. $start_end_day_of_week = self::calculate_start_end_of_week($week, $year);
  2720. $start_day = $start_end_day_of_week['start']['day'];
  2721. $start_month = $start_end_day_of_week['start']['month'];
  2722. $start_year = $start_end_day_of_week['start']['year'];
  2723. $end_day = $start_end_day_of_week['end']['day'];
  2724. $end_month = $start_end_day_of_week['end']['month'];
  2725. $end_year = $start_end_day_of_week['end']['year'];
  2726. // in sql statements you have to use year-month-day for date calculations
  2727. $start_filter = $start_year."-".$start_month."-".$start_day." 00:00:00";
  2728. $start_filter = api_get_utc_datetime($start_filter);
  2729. $end_filter = $end_year."-".$end_month."-".$end_day." 23:59:59";
  2730. $end_filter = api_get_utc_datetime($end_filter);
  2731. $sql = " SELECT * FROM ".$tbl_global_agenda." WHERE start_date>='".$start_filter."' AND start_date<='".$end_filter."' AND access_url_id = $current_access_url_id ";
  2732. }
  2733. // 3. creating the SQL statement for getting the personal agenda items in DAY view
  2734. if ($type == "day_view") { // we are in day view
  2735. // we could use mysql date() function but this is only available from 4.1 and higher
  2736. $start_filter = $year."-".$month."-".$day." 00:00:00";
  2737. $start_filter = api_get_utc_datetime($start_filter);
  2738. $end_filter = $year."-".$month."-".$day." 23:59:59";
  2739. $end_filter = api_get_utc_datetime($end_filter);
  2740. $sql = " SELECT * FROM ".$tbl_global_agenda." WHERE start_date>='".$start_filter."' AND start_date<='".$end_filter."' AND access_url_id = $current_access_url_id";
  2741. }
  2742. $result = Database::query($sql);
  2743. while ($item = Database::fetch_array($result)) {
  2744. if (!empty($item['start_date'])) {
  2745. $item['start_date'] = api_get_local_time($item['start_date']);
  2746. $item['start_date_tms'] = api_strtotime($item['start_date']);
  2747. }
  2748. if (!empty($item['end_date'])) {
  2749. $item['end_date'] = api_get_local_time($item['end_date']);
  2750. }
  2751. // we break the date field in the database into a date and a time part
  2752. $agenda_db_date = explode(" ", $item['start_date']);
  2753. $date = $agenda_db_date[0];
  2754. $time = $agenda_db_date[1];
  2755. // we divide the date part into a day, a month and a year
  2756. $agendadate = explode("-", $date);
  2757. $year = intval($agendadate[0]);
  2758. $month = intval($agendadate[1]);
  2759. $day = intval($agendadate[2]);
  2760. // we divide the time part into hour, minutes, seconds
  2761. $agendatime = explode(":", $time);
  2762. $hour = $agendatime[0];
  2763. $minute = $agendatime[1];
  2764. $second = $agendatime[2];
  2765. if ($type == 'month_view') {
  2766. $item['calendar_type'] = 'global';
  2767. $agendaitems[$day][] = $item;
  2768. continue;
  2769. }
  2770. $start_time = api_format_date(
  2771. $item['start_date'],
  2772. TIME_NO_SEC_FORMAT
  2773. );
  2774. $end_time = '';
  2775. if (!empty($item['end_date'])) {
  2776. $end_time = ' - '.api_format_date(
  2777. $item['end_date'],
  2778. DATE_TIME_FORMAT_LONG
  2779. );
  2780. }
  2781. // if the student has specified a course we a add a link to that course
  2782. if ($item['course'] <> "") {
  2783. $url = api_get_path(
  2784. WEB_CODE_PATH
  2785. )."admin/agenda.php?cidReq=".urlencode(
  2786. $item['course']
  2787. )."&day=$day&month=$month&year=$year#$day"; // RH //Patrick Cool: to highlight the relevant agenda item
  2788. $course_link = "<a href=\"$url\" title=\"".$item['course']."\">".$item['course']."</a>";
  2789. } else {
  2790. $course_link = "";
  2791. }
  2792. // Creating the array that will be returned. If we have week or month view we have an array with the date as the key
  2793. // if we have a day_view we use a half hour as index => key 33 = 16h30
  2794. if ($type !== "day_view") {
  2795. // This is the array construction for the WEEK or MONTH view
  2796. //Display the Agenda global in the tab agenda (administrator)
  2797. $agendaitems[$day] .= "<i>$start_time $end_time</i>&nbsp;-&nbsp;";
  2798. $agendaitems[$day] .= "<b>".get_lang('GlobalEvent')."</b>";
  2799. $agendaitems[$day] .= "<div>".$item['title']."</div><br>";
  2800. } else {
  2801. // this is the array construction for the DAY view
  2802. $halfhour = 2 * $agendatime['0'];
  2803. if ($agendatime['1'] >= '30') {
  2804. $halfhour = $halfhour + 1;
  2805. }
  2806. if (!is_array($agendaitems[$halfhour])) {
  2807. $content = $agendaitems[$halfhour];
  2808. }
  2809. $agendaitems[$halfhour] = $content."<div><i>$hour:$minute</i> <b>".get_lang(
  2810. 'GlobalEvent'
  2811. ).": </b>".$item['title']."</div>";
  2812. }
  2813. }
  2814. return $agendaitems;
  2815. }
  2816. /**
  2817. * This function retrieves all the personal agenda items and add them to the agenda items found by the other functions.
  2818. */
  2819. public static function get_personal_agenda_items($user_id, $agendaitems, $day = "", $month = "", $year = "", $week = "", $type)
  2820. {
  2821. $tbl_personal_agenda = Database :: get_main_table(TABLE_PERSONAL_AGENDA);
  2822. $user_id = intval($user_id);
  2823. // 1. creating the SQL statement for getting the personal agenda items in MONTH view
  2824. if ($type == "month_view" or $type == "") {
  2825. // we are in month view
  2826. $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' and MONTH(date)='".$month."' AND YEAR(date) = '".$year."' ORDER BY date ASC";
  2827. }
  2828. // 2. creating the SQL statement for getting the personal agenda items in WEEK view
  2829. // we are in week view
  2830. if ($type == "week_view") {
  2831. $start_end_day_of_week = self::calculate_start_end_of_week($week, $year);
  2832. $start_day = $start_end_day_of_week['start']['day'];
  2833. $start_month = $start_end_day_of_week['start']['month'];
  2834. $start_year = $start_end_day_of_week['start']['year'];
  2835. $end_day = $start_end_day_of_week['end']['day'];
  2836. $end_month = $start_end_day_of_week['end']['month'];
  2837. $end_year = $start_end_day_of_week['end']['year'];
  2838. // in sql statements you have to use year-month-day for date calculations
  2839. $start_filter = $start_year."-".$start_month."-".$start_day." 00:00:00";
  2840. $start_filter = api_get_utc_datetime($start_filter);
  2841. $end_filter = $end_year."-".$end_month."-".$end_day." 23:59:59";
  2842. $end_filter = api_get_utc_datetime($end_filter);
  2843. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' AND date>='".$start_filter."' AND date<='".$end_filter."'";
  2844. }
  2845. // 3. creating the SQL statement for getting the personal agenda items in DAY view
  2846. if ($type == "day_view") {
  2847. // we are in day view
  2848. // we could use mysql date() function but this is only available from 4.1 and higher
  2849. $start_filter = $year."-".$month."-".$day." 00:00:00";
  2850. $start_filter = api_get_utc_datetime($start_filter);
  2851. $end_filter = $year."-".$month."-".$day." 23:59:59";
  2852. $end_filter = api_get_utc_datetime($end_filter);
  2853. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' AND date>='".$start_filter."' AND date<='".$end_filter."'";
  2854. }
  2855. $result = Database::query($sql);
  2856. while ($item = Database::fetch_array($result, 'ASSOC')) {
  2857. $time_minute = api_convert_and_format_date($item['date'], TIME_NO_SEC_FORMAT);
  2858. $item['date'] = api_get_local_time($item['date']);
  2859. $item['start_date_tms'] = api_strtotime($item['date']);
  2860. $item['content'] = $item['text'];
  2861. // we break the date field in the database into a date and a time part
  2862. $agenda_db_date = explode(" ", $item['date']);
  2863. $date = $agenda_db_date[0];
  2864. $time = $agenda_db_date[1];
  2865. // we divide the date part into a day, a month and a year
  2866. $agendadate = explode("-", $item['date']);
  2867. $year = intval($agendadate[0]);
  2868. $month = intval($agendadate[1]);
  2869. $day = intval($agendadate[2]);
  2870. // we divide the time part into hour, minutes, seconds
  2871. $agendatime = explode(":", $time);
  2872. $hour = $agendatime[0];
  2873. $minute = $agendatime[1];
  2874. $second = $agendatime[2];
  2875. if ($type == 'month_view') {
  2876. $item['calendar_type'] = 'personal';
  2877. $item['start_date'] = $item['date'];
  2878. $agendaitems[$day][] = $item;
  2879. continue;
  2880. }
  2881. // if the student has specified a course we a add a link to that course
  2882. if ($item['course'] <> "") {
  2883. $url = api_get_path(WEB_CODE_PATH)."calendar/agenda.php?cidReq=".urlencode($item['course'])."&day=$day&month=$month&year=$year#$day"; // RH //Patrick Cool: to highlight the relevant agenda item
  2884. $course_link = "<a href=\"$url\" title=\"".$item['course']."\">".$item['course']."</a>";
  2885. } else {
  2886. $course_link = "";
  2887. }
  2888. // Creating the array that will be returned. If we have week or month view we have an array with the date as the key
  2889. // if we have a day_view we use a half hour as index => key 33 = 16h30
  2890. if ($type !== "day_view") {
  2891. // This is the array construction for the WEEK or MONTH view
  2892. //Display events in agenda
  2893. $agendaitems[$day] .= "<div><i>$time_minute</i> $course_link <a href=\"myagenda.php?action=view&view=personal&day=$day&month=$month&year=$year&id=".$item['id']."#".$item['id']."\" class=\"personal_agenda\">".$item['title']."</a></div><br />";
  2894. } else {
  2895. // this is the array construction for the DAY view
  2896. $halfhour = 2 * $agendatime['0'];
  2897. if ($agendatime['1'] >= '30') {
  2898. $halfhour = $halfhour +1;
  2899. }
  2900. //Display events by list
  2901. $agendaitems[$halfhour] .= "<div><i>$time_minute</i> $course_link <a href=\"myagenda.php?action=view&view=personal&day=$day&month=$month&year=$year&id=".$item['id']."#".$item['id']."\" class=\"personal_agenda\">".$item['title']."</a></div>";
  2902. }
  2903. }
  2904. return $agendaitems;
  2905. }
  2906. /**
  2907. * Show the monthcalender of the given month
  2908. * @param array Agendaitems
  2909. * @param int Month number
  2910. * @param int Year number
  2911. * @param array Array of strings containing long week day names (deprecated, you can send an empty array instead)
  2912. * @param string The month name
  2913. * @return void Direct output
  2914. */
  2915. public static function display_mymonthcalendar($user_id, $agendaitems, $month, $year, $weekdaynames = array(), $monthName, $show_content = true)
  2916. {
  2917. global $DaysShort, $course_path;
  2918. //Handle leap year
  2919. $numberofdays = array (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  2920. if (($year % 400 == 0) or ($year % 4 == 0 and $year % 100 <> 0))
  2921. $numberofdays[2] = 29;
  2922. //Get the first day of the month
  2923. $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
  2924. //Start the week on monday
  2925. $startdayofweek = $dayone['wday'] <> 0 ? ($dayone['wday'] - 1) : 6;
  2926. $g_cc = (isset($_GET['courseCode'])?$_GET['courseCode']:'');
  2927. $next_month = ($month == 1 ? 12 : $month -1);
  2928. $prev_month = ($month == 12 ? 1 : $month +1);
  2929. $next_year = ($month == 1 ? $year -1 : $year);
  2930. $prev_year = ($month == 12 ? $year +1 : $year);
  2931. if ($show_content) {
  2932. $back_url = Display::url(get_lang('Previous'), api_get_self()."?coursePath=".urlencode($course_path)."&courseCode=".Security::remove_XSS($g_cc)."&action=view&view=month&month=".$next_month."&year=".$next_year);
  2933. $next_url = Display::url(get_lang('Next'), api_get_self()."?coursePath=".urlencode($course_path)."&courseCode=".Security::remove_XSS($g_cc)."&action=view&view=month&month=".$prev_month."&year=".$prev_year);
  2934. } else {
  2935. $back_url = Display::url(get_lang('Previous'), '', array('onclick'=>"load_calendar('".$user_id."','".$next_month."', '".$next_year."'); ", 'class' => 'btn ui-button ui-widget ui-state-default'));
  2936. $next_url = Display::url(get_lang('Next'), '', array('onclick'=>"load_calendar('".$user_id."','".$prev_month."', '".$prev_year."'); ", 'class' => 'pull-right btn ui-button ui-widget ui-state-default'));
  2937. }
  2938. $html = '';
  2939. $html .= '<div class="actions">';
  2940. $html .= '<div class="row">';
  2941. $html .= '<div class="col-md-4">'.$back_url.'</div>';
  2942. $html .= '<div class="col-md-4"><p class="agenda-title text-center">'.$monthName." ".$year.'</p></div>';
  2943. $html .= '<div class="col-md-4">'.$next_url.'</div>';
  2944. $html .= '</div>';
  2945. $html .= '</div>';
  2946. $html .= '<table id="agenda_list2" class="table table-bordered">';
  2947. $html .= '<tr>';
  2948. for ($ii = 1; $ii < 8; $ii ++) {
  2949. $html .= '<td class="weekdays">'.$DaysShort[$ii % 7].'</td>';
  2950. }
  2951. $html .= '</tr>';
  2952. $curday = -1;
  2953. $today = getdate();
  2954. while ($curday <= $numberofdays[$month]) {
  2955. $html .= "<tr>";
  2956. for ($ii = 0; $ii < 7; $ii ++) {
  2957. if (($curday == -1) && ($ii == $startdayofweek)) {
  2958. $curday = 1;
  2959. }
  2960. if (($curday > 0) && ($curday <= $numberofdays[$month])) {
  2961. $bgcolor = $class = 'class="days_week"';
  2962. $dayheader = Display::div($curday, array('class'=>'agenda_day'));
  2963. if (($curday == $today['mday']) && ($year == $today['year']) && ($month == $today['mon'])) {
  2964. $class = "class=\"days_today\" style=\"width:10%;\"";
  2965. }
  2966. $html .= "<td ".$class.">".$dayheader;
  2967. if (!empty($agendaitems[$curday])) {
  2968. $items = $agendaitems[$curday];
  2969. $items = ArrayClass::msort($items, 'start_date_tms');
  2970. foreach($items as $value) {
  2971. $value['title'] = Security::remove_XSS($value['title']);
  2972. $start_time = api_format_date($value['start_date'], TIME_NO_SEC_FORMAT);
  2973. $end_time = '';
  2974. if (!empty($value['end_date'])) {
  2975. $end_time = '-&nbsp;<i>'.api_format_date($value['end_date'], DATE_TIME_FORMAT_LONG).'</i>';
  2976. }
  2977. $complete_time = '<i>'.api_format_date($value['start_date'], DATE_TIME_FORMAT_LONG).'</i>&nbsp;'.$end_time;
  2978. $time = '<i>'.$start_time.'</i>';
  2979. switch($value['calendar_type']) {
  2980. case 'personal':
  2981. $bg_color = '#D0E7F4';
  2982. $icon = Display::return_icon('user.png', get_lang('MyAgenda'), array(), ICON_SIZE_SMALL);
  2983. break;
  2984. case 'global':
  2985. $bg_color = '#FFBC89';
  2986. $icon = Display::return_icon('view_remove.png', get_lang('GlobalEvent'), array(), ICON_SIZE_SMALL);
  2987. break;
  2988. case 'course':
  2989. $bg_color = '#CAFFAA';
  2990. $icon_name = 'course.png';
  2991. if (!empty($value['session_id'])) {
  2992. $icon_name = 'session.png';
  2993. }
  2994. if ($show_content) {
  2995. $icon = Display::url(Display::return_icon($icon_name, $value['course_name'].' '.get_lang('Course'), array(), ICON_SIZE_SMALL), $value['url']);
  2996. } else {
  2997. $icon = Display::return_icon($icon_name, $value['course_name'].' '.get_lang('Course'), array(), ICON_SIZE_SMALL);
  2998. }
  2999. break;
  3000. default:
  3001. break;
  3002. }
  3003. $result = '<div class="rounded_div_agenda" style="background-color:'.$bg_color.';">';
  3004. if ($show_content) {
  3005. //Setting a personal event to green
  3006. $icon = Display::div($icon, array('style'=>'float:right'));
  3007. $link = $value['calendar_type'].'_'.$value['id'].'_'.$value['course_id'].'_'.$value['session_id'];
  3008. //Link to bubble
  3009. $url = Display::url(cut($value['title'], 40), '#', array('id'=>$link, 'class'=>'opener'));
  3010. $result .= $time.' '.$icon.' '.Display::div($url);
  3011. //Hidden content
  3012. $content = Display::div($icon.Display::tag('h2', $value['course_name']).'<hr />'.Display::tag('h3', $value['title']).$complete_time.'<hr />'.Security::remove_XSS($value['content']));
  3013. //Main div
  3014. $result .= Display::div($content, array('id'=>'main_'.$link, 'class' => 'dialog', 'style' => 'display:none'));
  3015. $result .= '</div>';
  3016. $html .= $result;
  3017. //echo Display::div($content, array('id'=>'main_'.$value['calendar_type'].'_'.$value['id'], 'class' => 'dialog'));
  3018. } else {
  3019. $html .= $result .= $icon.'</div>';
  3020. }
  3021. }
  3022. }
  3023. $html .= "</td>";
  3024. $curday ++;
  3025. } else {
  3026. $html .= "<td></td>";
  3027. }
  3028. }
  3029. $html .= "</tr>";
  3030. }
  3031. $html .= "</table>";
  3032. echo $html;
  3033. }
  3034. /**
  3035. * Get personal agenda items between two dates (=all events from all registered courses)
  3036. * @param int user ID of the user
  3037. * @param string Optional start date in datetime format (if no start date is given, uses today)
  3038. * @param string Optional end date in datetime format (if no date is given, uses one year from now)
  3039. * @param integer $user_id
  3040. * @return array Array of events ordered by start date, in
  3041. * [0]('datestart','dateend','title'),[1]('datestart','dateend','title','link','coursetitle') format,
  3042. * where datestart and dateend are in yyyyMMddhhmmss format.
  3043. * @TODO Implement really personal events (from user DB) and global events (from main DB)
  3044. */
  3045. public static function get_personal_agenda_items_between_dates($user_id, $date_start='', $date_end='')
  3046. {
  3047. $items = array ();
  3048. if ($user_id != strval(intval($user_id))) { return $items; }
  3049. if (empty($date_start)) { $date_start = date('Y-m-d H:i:s');}
  3050. if (empty($date_end)) { $date_end = date('Y-m-d H:i:s',mktime(0, 0, 0, date("m"), date("d"), date("Y")+1));}
  3051. $expr = '/\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2}/';
  3052. if(!preg_match($expr,$date_start)) { return $items; }
  3053. if(!preg_match($expr,$date_end)) { return $items; }
  3054. // get agenda-items for every course
  3055. $courses = api_get_user_courses($user_id,false);
  3056. foreach ($courses as $id => $course) {
  3057. $c = api_get_course_info_by_id($course['real_id']);
  3058. //databases of the courses
  3059. $t_a = Database :: get_course_table(TABLE_AGENDA, $course['db']);
  3060. $t_ip = Database :: get_course_table(TABLE_ITEM_PROPERTY, $course['db']);
  3061. // get the groups to which the user belong
  3062. $group_memberships = GroupManager :: get_group_ids($course['db'], $user_id);
  3063. // if the user is administrator of that course we show all the agenda items
  3064. if ($course['status'] == '1') {
  3065. //echo "course admin";
  3066. $sqlquery = "SELECT ".
  3067. " DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
  3068. " FROM ".$t_a." agenda, ".
  3069. $t_ip." ip ".
  3070. " WHERE agenda.id = ip.ref ".
  3071. " AND agenda.start_date>='$date_start' ".
  3072. " AND agenda.end_date<='$date_end' ".
  3073. " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
  3074. " AND ip.visibility='1' ".
  3075. " GROUP BY agenda.id ".
  3076. " ORDER BY start_date ";
  3077. } else {
  3078. // if the user is not an administrator of that course, then...
  3079. if (is_array($group_memberships) && count($group_memberships)>0)
  3080. {
  3081. $sqlquery = "SELECT " .
  3082. "DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
  3083. " FROM ".$t_a." agenda, ".
  3084. $t_ip." ip ".
  3085. " WHERE agenda.id = ip.ref ".
  3086. " AND agenda.start_date>='$date_start' ".
  3087. " AND agenda.end_date<='$date_end' ".
  3088. " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
  3089. " AND ( ip.to_user_id='".$user_id."' OR (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) ) ".
  3090. " AND ip.visibility='1' ".
  3091. " ORDER BY start_date ";
  3092. } else {
  3093. $sqlquery = "SELECT ".
  3094. "DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
  3095. " FROM ".$t_a." agenda, ".
  3096. $t_ip." ip ".
  3097. " WHERE agenda.id = ip.ref ".
  3098. " AND agenda.start_date>='$date_start' ".
  3099. " AND agenda.end_date<='$date_end' ".
  3100. " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
  3101. " AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) ".
  3102. " AND ip.visibility='1' ".
  3103. " ORDER BY start_date ";
  3104. }
  3105. }
  3106. $result = Database::query($sqlquery);
  3107. while ($item = Database::fetch_array($result)) {
  3108. $agendaday = date("j",strtotime($item['start_date']));
  3109. $month = date("n",strtotime($item['start_date']));
  3110. $year = date("Y",strtotime($item['start_date']));
  3111. $URL = api_get_path(WEB_PATH)."main/calendar/agenda.php?cidReq=".urlencode($course["code"])."&day=$agendaday&month=$month&year=$year#$agendaday";
  3112. list($year,$month,$day,$hour,$min,$sec) = split('[-: ]',$item['start_date']);
  3113. $start_date = $year.$month.$day.$hour.$min;
  3114. list($year,$month,$day,$hour,$min,$sec) = split('[-: ]',$item['end_date']);
  3115. $end_date = $year.$month.$day.$hour.$min;
  3116. $items[] = array(
  3117. 'datestart'=>$start_date,
  3118. 'dateend'=>$end_date,
  3119. 'title'=>$item['title'],
  3120. 'link'=>$URL,
  3121. 'coursetitle'=>$c['name'],
  3122. );
  3123. }
  3124. }
  3125. return $items;
  3126. }
  3127. /**
  3128. * This function retrieves one personal agenda item returns it.
  3129. * @param int $id The agenda item ID
  3130. * @return array The results of the database query, or null if not found
  3131. */
  3132. public static function get_personal_agenda_item($id)
  3133. {
  3134. $tbl_personal_agenda = Database :: get_main_table(TABLE_PERSONAL_AGENDA);
  3135. $id = intval($id);
  3136. // make sure events of the personal agenda can only be seen by the user himself
  3137. $user = api_get_user_id();
  3138. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE id=".$id." AND user = ".$user;
  3139. $result = Database::query($sql);
  3140. if(Database::num_rows($result)==1) {
  3141. $item = Database::fetch_array($result);
  3142. } else {
  3143. $item = null;
  3144. }
  3145. return $item;
  3146. }
  3147. /**
  3148. * This function calculates the startdate of the week (monday)
  3149. * and the enddate of the week (sunday)
  3150. * and returns it as an array
  3151. */
  3152. public static function calculate_start_end_of_week($week_number, $year)
  3153. {
  3154. // determine the start and end date
  3155. // step 1: we calculate a timestamp for a day in this week
  3156. $random_day_in_week = mktime(0, 0, 0, 1, 1, $year) + ($week_number) * (7 * 24 * 60 * 60); // we calculate a random day in this week
  3157. // step 2: we which day this is (0=sunday, 1=monday, ...)
  3158. $number_day_in_week = date('w', $random_day_in_week);
  3159. // step 3: we calculate the timestamp of the monday of the week we are in
  3160. $start_timestamp = $random_day_in_week - (($number_day_in_week -1) * 24 * 60 * 60);
  3161. // step 4: we calculate the timestamp of the sunday of the week we are in
  3162. $end_timestamp = $random_day_in_week + ((7 - $number_day_in_week +1) * 24 * 60 * 60) - 3600;
  3163. // step 5: calculating the start_day, end_day, start_month, end_month, start_year, end_year
  3164. $start_day = date('j', $start_timestamp);
  3165. $start_month = date('n', $start_timestamp);
  3166. $start_year = date('Y', $start_timestamp);
  3167. $end_day = date('j', $end_timestamp);
  3168. $end_month = date('n', $end_timestamp);
  3169. $end_year = date('Y', $end_timestamp);
  3170. $start_end_array['start']['day'] = $start_day;
  3171. $start_end_array['start']['month'] = $start_month;
  3172. $start_end_array['start']['year'] = $start_year;
  3173. $start_end_array['end']['day'] = $end_day;
  3174. $start_end_array['end']['month'] = $end_month;
  3175. $start_end_array['end']['year'] = $end_year;
  3176. return $start_end_array;
  3177. }
  3178. /**
  3179. * @return bool
  3180. */
  3181. public function getIsAllowedToEdit()
  3182. {
  3183. return $this->isAllowedToEdit;
  3184. }
  3185. /**
  3186. * @param bool $isAllowedToEdit
  3187. */
  3188. public function setIsAllowedToEdit($isAllowedToEdit)
  3189. {
  3190. $this->isAllowedToEdit = $isAllowedToEdit;
  3191. }
  3192. }