agenda.lib.php 146 KB

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