agenda.lib.php 148 KB

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