agenda.lib.php 150 KB

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