agenda.lib.php 149 KB

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