123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Class Agenda.
- *
- * @author: Julio Montoya <gugli100@gmail.com>
- */
- class Agenda
- {
- public $events = [];
- /** @var string Current type */
- public $type = 'personal';
- public $types = ['personal', 'admin', 'course'];
- public $sessionId = 0;
- public $senderId;
- /** @var array */
- public $course;
- /** @var string */
- public $comment;
- public $eventStudentPublicationColor;
- /** @var array */
- private $sessionInfo;
- /** @var bool */
- private $isAllowedToEdit;
- /**
- * Constructor.
- *
- * @param string $type
- * @param int $senderId Optional The user sender ID
- * @param int $courseId Optional. The course ID
- * @param int $sessionId Optional The session ID
- */
- public function __construct(
- $type,
- $senderId = 0,
- $courseId = 0,
- $sessionId = 0
- ) {
- // Table definitions
- $this->tbl_global_agenda = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
- $this->tbl_personal_agenda = Database::get_main_table(TABLE_PERSONAL_AGENDA);
- $this->tbl_course_agenda = Database::get_course_table(TABLE_AGENDA);
- $this->table_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT);
- $this->setType($type);
- $this->setSenderId($senderId ?: api_get_user_id());
- $isAllowToEdit = false;
- switch ($type) {
- case 'course':
- $sessionId = $sessionId ?: api_get_session_id();
- $sessionInfo = api_get_session_info($sessionId);
- $this->setSessionId($sessionId);
- $this->setSessionInfo($sessionInfo);
- // Setting the course object if we are in a course
- $courseInfo = api_get_course_info_by_id($courseId);
- if (!empty($courseInfo)) {
- $this->set_course($courseInfo);
- }
- // Check if teacher/admin rights.
- $isAllowToEdit = api_is_allowed_to_edit(false, true);
- // Check course setting.
- if (api_get_course_setting('allow_user_edit_agenda') === '1'
- && api_is_allowed_in_course()
- ) {
- $isAllowToEdit = true;
- }
- $groupId = api_get_group_id();
- if (!empty($groupId)) {
- $groupInfo = GroupManager::get_group_properties($groupId);
- $userHasAccess = GroupManager::user_has_access(
- api_get_user_id(),
- $groupInfo['iid'],
- GroupManager::GROUP_TOOL_CALENDAR
- );
- $isTutor = GroupManager::is_tutor_of_group(
- api_get_user_id(),
- $groupInfo
- );
- $isGroupAccess = $userHasAccess || $isTutor;
- $isAllowToEdit = false;
- if ($isGroupAccess) {
- $isAllowToEdit = true;
- }
- }
- if (!empty($sessionId)) {
- $allowDhrToEdit = api_get_configuration_value('allow_agenda_edit_for_hrm');
- if ($allowDhrToEdit) {
- $isHrm = SessionManager::isUserSubscribedAsHRM($sessionId, api_get_user_id());
- if ($isHrm) {
- $isAllowToEdit = true;
- }
- }
- }
- break;
- case 'admin':
- $isAllowToEdit = api_is_platform_admin();
- break;
- case 'personal':
- $isAllowToEdit = !api_is_anonymous();
- break;
- }
- $this->setIsAllowedToEdit($isAllowToEdit);
- $this->events = [];
- $agendaColors = array_merge(
- [
- 'platform' => 'red', //red
- 'course' => '#458B00', //green
- 'group' => '#A0522D', //siena
- 'session' => '#00496D', // kind of green
- 'other_session' => '#999', // kind of green
- 'personal' => 'steel blue', //steel blue
- 'student_publication' => '#FF8C00', //DarkOrange
- ],
- api_get_configuration_value('agenda_colors') ?: []
- );
- // Event colors
- $this->event_platform_color = $agendaColors['platform'];
- $this->event_course_color = $agendaColors['course'];
- $this->event_group_color = $agendaColors['group'];
- $this->event_session_color = $agendaColors['session'];
- $this->eventOtherSessionColor = $agendaColors['other_session'];
- $this->event_personal_color = $agendaColors['personal'];
- $this->eventStudentPublicationColor = $agendaColors['student_publication'];
- }
- /**
- * @param int $senderId
- */
- public function setSenderId($senderId)
- {
- $this->senderId = (int) $senderId;
- }
- /**
- * @return int
- */
- public function getSenderId()
- {
- return $this->senderId;
- }
- /**
- * @param string $type can be 'personal', 'admin' or 'course'
- */
- public function setType($type)
- {
- $typeList = $this->getTypes();
- if (in_array($type, $typeList)) {
- $this->type = $type;
- }
- }
- /**
- * Returns the type previously set (and filtered) through setType
- * If setType() was not called, then type defaults to "personal" as
- * set in the class definition.
- */
- public function getType()
- {
- if (isset($this->type)) {
- return $this->type;
- }
- }
- /**
- * @param int $id
- */
- public function setSessionId($id)
- {
- $this->sessionId = (int) $id;
- }
- /**
- * @param array $sessionInfo
- */
- public function setSessionInfo($sessionInfo)
- {
- $this->sessionInfo = $sessionInfo;
- }
- /**
- * @return int $id
- */
- public function getSessionId()
- {
- return $this->sessionId;
- }
- /**
- * @param array $courseInfo
- */
- public function set_course($courseInfo)
- {
- $this->course = $courseInfo;
- }
- /**
- * @return array
- */
- public function getTypes()
- {
- return $this->types;
- }
- /**
- * Adds an event to the calendar.
- *
- * @param string $start datetime format: 2012-06-14 09:00:00 in local time
- * @param string $end datetime format: 2012-06-14 09:00:00 in local time
- * @param string $allDay (true, false)
- * @param string $title
- * @param string $content
- * @param array $usersToSend array('everyone') or a list of user/group ids
- * @param bool $addAsAnnouncement event as a *course* announcement
- * @param int $parentEventId
- * @param array $attachmentArray array of $_FILES['']
- * @param array $attachmentCommentList
- * @param string $eventComment
- * @param string $color
- *
- * @return int
- */
- public function addEvent(
- $start,
- $end,
- $allDay,
- $title,
- $content,
- $usersToSend = [],
- $addAsAnnouncement = false,
- $parentEventId = null,
- $attachmentArray = [],
- $attachmentCommentList = [],
- $eventComment = null,
- $color = ''
- ) {
- $start = api_get_utc_datetime($start);
- $end = api_get_utc_datetime($end);
- $allDay = isset($allDay) && $allDay === 'true' ? 1 : 0;
- $id = null;
- switch ($this->type) {
- case 'personal':
- $attributes = [
- 'user' => api_get_user_id(),
- 'title' => $title,
- 'text' => $content,
- 'date' => $start,
- 'enddate' => $end,
- 'all_day' => $allDay,
- 'color' => $color,
- ];
- $id = Database::insert(
- $this->tbl_personal_agenda,
- $attributes
- );
- break;
- case 'course':
- $attributes = [
- 'title' => $title,
- 'content' => $content,
- 'start_date' => $start,
- 'end_date' => $end,
- 'all_day' => $allDay,
- 'session_id' => $this->getSessionId(),
- 'c_id' => $this->course['real_id'],
- 'comment' => $eventComment,
- 'color' => $color,
- ];
- if (!empty($parentEventId)) {
- $attributes['parent_event_id'] = $parentEventId;
- }
- $senderId = $this->getSenderId();
- $sessionId = $this->getSessionId();
- // Simple course event.
- $id = Database::insert($this->tbl_course_agenda, $attributes);
- if ($id) {
- $sql = "UPDATE ".$this->tbl_course_agenda." SET id = iid WHERE iid = $id";
- Database::query($sql);
- $groupId = api_get_group_id();
- $groupInfo = [];
- if ($groupId) {
- $groupInfo = GroupManager::get_group_properties(
- $groupId
- );
- }
- if (!empty($usersToSend)) {
- $sendTo = $this->parseSendToArray($usersToSend);
- if ($sendTo['everyone']) {
- api_item_property_update(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 'AgendaAdded',
- $senderId,
- $groupInfo,
- '',
- $start,
- $end,
- $sessionId
- );
- api_item_property_update(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 'visible',
- $senderId,
- $groupInfo,
- '',
- $start,
- $end,
- $sessionId
- );
- } else {
- // Storing the selected groups
- if (!empty($sendTo['groups'])) {
- foreach ($sendTo['groups'] as $group) {
- $groupInfoItem = [];
- if ($group) {
- $groupInfoItem = GroupManager::get_group_properties($group);
- }
- api_item_property_update(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 'AgendaAdded',
- $senderId,
- $groupInfoItem,
- 0,
- $start,
- $end,
- $sessionId
- );
- api_item_property_update(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 'visible',
- $senderId,
- $groupInfoItem,
- 0,
- $start,
- $end,
- $sessionId
- );
- }
- }
- // storing the selected users
- if (!empty($sendTo['users'])) {
- foreach ($sendTo['users'] as $userId) {
- api_item_property_update(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 'AgendaAdded',
- $senderId,
- $groupInfo,
- $userId,
- $start,
- $end,
- $sessionId
- );
- api_item_property_update(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 'visible',
- $senderId,
- $groupInfo,
- $userId,
- $start,
- $end,
- $sessionId
- );
- }
- }
- }
- }
- // Add announcement.
- if ($addAsAnnouncement) {
- $this->storeAgendaEventAsAnnouncement(
- $id,
- $usersToSend
- );
- }
- // Add attachment.
- if (isset($attachmentArray) && !empty($attachmentArray)) {
- $counter = 0;
- foreach ($attachmentArray as $attachmentItem) {
- $this->addAttachment(
- $id,
- $attachmentItem,
- $attachmentCommentList[$counter],
- $this->course
- );
- $counter++;
- }
- }
- }
- break;
- case 'admin':
- if (api_is_platform_admin()) {
- $attributes = [
- 'title' => $title,
- 'content' => $content,
- 'start_date' => $start,
- 'end_date' => $end,
- 'all_day' => $allDay,
- 'access_url_id' => api_get_current_access_url_id(),
- ];
- $id = Database::insert(
- $this->tbl_global_agenda,
- $attributes
- );
- }
- break;
- }
- return $id;
- }
- /**
- * @param int $eventId
- * @param int $courseId
- *
- * @return array
- */
- public function getRepeatedInfoByEvent($eventId, $courseId)
- {
- $repeatTable = Database::get_course_table(TABLE_AGENDA_REPEAT);
- $eventId = (int) $eventId;
- $courseId = (int) $courseId;
- $sql = "SELECT * FROM $repeatTable
- WHERE c_id = $courseId AND cal_id = $eventId";
- $res = Database::query($sql);
- $repeatInfo = [];
- if (Database::num_rows($res) > 0) {
- $repeatInfo = Database::fetch_array($res, 'ASSOC');
- }
- return $repeatInfo;
- }
- /**
- * @param string $type
- * @param string $startEvent in UTC
- * @param string $endEvent in UTC
- * @param string $repeatUntilDate in UTC
- *
- * @throws Exception
- *
- * @return array
- */
- public function generateDatesByType($type, $startEvent, $endEvent, $repeatUntilDate)
- {
- $continue = true;
- $repeatUntilDate = new DateTime($repeatUntilDate, new DateTimeZone('UTC'));
- $loopMax = 365;
- $counter = 0;
- $list = [];
- switch ($type) {
- case 'daily':
- $interval = 'P1D';
- break;
- case 'weekly':
- $interval = 'P1W';
- break;
- case 'monthlyByDate':
- $interval = 'P1M';
- break;
- case 'monthlyByDay':
- // not yet implemented
- break;
- case 'monthlyByDayR':
- // not yet implemented
- break;
- case 'yearly':
- $interval = 'P1Y';
- break;
- }
- if (empty($interval)) {
- return [];
- }
- $timeZone = api_get_timezone();
- while ($continue) {
- $startDate = new DateTime($startEvent, new DateTimeZone('UTC'));
- $endDate = new DateTime($endEvent, new DateTimeZone('UTC'));
- $startDate->add(new DateInterval($interval));
- $endDate->add(new DateInterval($interval));
- $newStartDate = $startDate->format('Y-m-d H:i:s');
- $newEndDate = $endDate->format('Y-m-d H:i:s');
- $startEvent = $newStartDate;
- $endEvent = $newEndDate;
- if ($endDate > $repeatUntilDate) {
- break;
- }
- // @todo remove comment code
- $startDateInLocal = new DateTime($newStartDate, new DateTimeZone($timeZone));
- //$originalOffset = $startDate->getOffset();
- if ($startDateInLocal->format('I') == 0) {
- // Is saving time? Then fix UTC time to add time
- $seconds = $startDateInLocal->getOffset();
- $startDate->add(new DateInterval("PT".$seconds."S"));
- $startDateFixed = $startDate->format('Y-m-d H:i:s');
- $startDateInLocalFixed = new DateTime($startDateFixed, new DateTimeZone($timeZone));
- $newStartDate = $startDateInLocalFixed->format('Y-m-d H:i:s');
- } else {
- /*$seconds = $startDateInLocal->getOffset();
- $startDate->add(new DateInterval("PT".$seconds."S"));
- $startDateFixed = $startDate->format('Y-m-d H:i:s');
- $startDateInLocalFixed = new DateTime($startDateFixed, new DateTimeZone($timeZone));
- $newStartDate = $startDateInLocalFixed->format('Y-m-d H:i:s');*/
- }
- //var_dump($newStartDate.' - '.$startDateInLocal->format('I'));
- $endDateInLocal = new DateTime($newEndDate, new DateTimeZone($timeZone));
- if ($endDateInLocal->format('I') == 0) {
- // Is saving time? Then fix UTC time to add time
- $seconds = $endDateInLocal->getOffset();
- $endDate->add(new DateInterval("PT".$seconds."S"));
- $endDateFixed = $endDate->format('Y-m-d H:i:s');
- $endDateInLocalFixed = new DateTime($endDateFixed, new DateTimeZone($timeZone));
- $newEndDate = $endDateInLocalFixed->format('Y-m-d H:i:s');
- }
- $list[] = ['start' => $newStartDate, 'end' => $newEndDate, 'i' => $startDateInLocal->format('I')];
- $counter++;
- // just in case stop if more than $loopMax
- if ($counter > $loopMax) {
- break;
- }
- }
- return $list;
- }
- /**
- * @param int $eventId
- * @param string $type
- * @param string $end in UTC
- * @param array $sentTo
- *
- * @return bool
- */
- public function addRepeatedItem($eventId, $type, $end, $sentTo = [])
- {
- $t_agenda = Database::get_course_table(TABLE_AGENDA);
- $t_agenda_r = Database::get_course_table(TABLE_AGENDA_REPEAT);
- if (empty($this->course)) {
- return false;
- }
- $courseId = $this->course['real_id'];
- $eventId = (int) $eventId;
- $sql = "SELECT title, content, start_date, end_date, all_day
- FROM $t_agenda
- WHERE c_id = $courseId AND id = $eventId";
- $res = Database::query($sql);
- if (Database::num_rows($res) !== 1) {
- return false;
- }
- $typeList = [
- 'daily',
- 'weekly',
- 'monthlyByDate',
- 'monthlyByDay',
- 'monthlyByDayR',
- 'yearly',
- ];
- if (!in_array($type, $typeList)) {
- return false;
- }
- $now = time();
- // The event has to repeat *in the future*. We don't allow repeated
- // events in the past
- if ($end > $now) {
- return false;
- }
- $row = Database::fetch_array($res);
- $title = $row['title'];
- $content = $row['content'];
- $allDay = $row['all_day'];
- $type = Database::escape_string($type);
- $end = Database::escape_string($end);
- $endTimeStamp = api_strtotime($end, 'UTC');
- $sql = "INSERT INTO $t_agenda_r (c_id, cal_id, cal_type, cal_end)
- VALUES ($courseId, '$eventId', '$type', '$endTimeStamp')";
- Database::query($sql);
- $generatedDates = $this->generateDatesByType($type, $row['start_date'], $row['end_date'], $end);
- if (empty($generatedDates)) {
- return false;
- }
- foreach ($generatedDates as $dateInfo) {
- $start = api_get_local_time($dateInfo['start']);
- $end = api_get_local_time($dateInfo['end']);
- $this->addEvent(
- $start,
- $end,
- $allDay,
- $title,
- $content,
- $sentTo,
- false,
- $eventId
- );
- }
- return true;
- }
- /**
- * @param int $item_id
- * @param array $sentTo
- *
- * @return int
- */
- public function storeAgendaEventAsAnnouncement($item_id, $sentTo = [])
- {
- $table_agenda = Database::get_course_table(TABLE_AGENDA);
- $courseId = api_get_course_int_id();
- // Check params
- if (empty($item_id) || $item_id != strval(intval($item_id))) {
- return -1;
- }
- // Get the agenda item.
- $item_id = intval($item_id);
- $sql = "SELECT * FROM $table_agenda
- WHERE c_id = $courseId AND id = ".$item_id;
- $res = Database::query($sql);
- if (Database::num_rows($res) > 0) {
- $row = Database::fetch_array($res, 'ASSOC');
- // Sending announcement
- if (!empty($sentTo)) {
- $id = AnnouncementManager::add_announcement(
- api_get_course_info(),
- api_get_session_id(),
- $row['title'],
- $row['content'],
- $sentTo,
- null,
- null,
- $row['end_date']
- );
- AnnouncementManager::sendEmail(
- api_get_course_info(),
- api_get_session_id(),
- $id
- );
- return $id;
- }
- }
- return -1;
- }
- /**
- * Edits an event.
- *
- * @param int $id
- * @param string $start datetime format: 2012-06-14 09:00:00
- * @param string $end datetime format: 2012-06-14 09:00:00
- * @param int $allDay is all day 'true' or 'false'
- * @param string $title
- * @param string $content
- * @param array $usersToSend
- * @param array $attachmentArray
- * @param array $attachmentCommentList
- * @param string $comment
- * @param string $color
- * @param bool $addAnnouncement
- * @param bool $updateContent
- * @param int $authorId
- *
- * @return bool
- */
- public function editEvent(
- $id,
- $start,
- $end,
- $allDay,
- $title,
- $content,
- $usersToSend = [],
- $attachmentArray = [],
- $attachmentCommentList = [],
- $comment = null,
- $color = '',
- $addAnnouncement = false,
- $updateContent = true,
- $authorId = 0
- ) {
- $start = api_get_utc_datetime($start);
- $end = api_get_utc_datetime($end);
- $allDay = isset($allDay) && $allDay == 'true' ? 1 : 0;
- $authorId = empty($authorId) ? api_get_user_id() : (int) $authorId;
- switch ($this->type) {
- case 'personal':
- $eventInfo = $this->get_event($id);
- if ($eventInfo['user'] != api_get_user_id()) {
- break;
- }
- $attributes = [
- 'title' => $title,
- 'date' => $start,
- 'enddate' => $end,
- 'all_day' => $allDay,
- ];
- if ($updateContent) {
- $attributes['text'] = $content;
- }
- if (!empty($color)) {
- $attributes['color'] = $color;
- }
- Database::update(
- $this->tbl_personal_agenda,
- $attributes,
- ['id = ?' => $id]
- );
- break;
- case 'course':
- $eventInfo = $this->get_event($id);
- if (empty($eventInfo)) {
- return false;
- }
- $groupId = api_get_group_id();
- $groupIid = 0;
- $groupInfo = [];
- if ($groupId) {
- $groupInfo = GroupManager::get_group_properties($groupId);
- if ($groupInfo) {
- $groupIid = $groupInfo['iid'];
- }
- }
- $courseId = $this->course['real_id'];
- if (empty($courseId)) {
- return false;
- }
- if ($this->getIsAllowedToEdit()) {
- $attributes = [
- 'title' => $title,
- 'start_date' => $start,
- 'end_date' => $end,
- 'all_day' => $allDay,
- 'comment' => $comment,
- ];
- if ($updateContent) {
- $attributes['content'] = $content;
- }
- if (!empty($color)) {
- $attributes['color'] = $color;
- }
- Database::update(
- $this->tbl_course_agenda,
- $attributes,
- [
- 'id = ? AND c_id = ? AND session_id = ? ' => [
- $id,
- $courseId,
- $this->sessionId,
- ],
- ]
- );
- if (!empty($usersToSend)) {
- $sendTo = $this->parseSendToArray($usersToSend);
- $usersToDelete = array_diff(
- $eventInfo['send_to']['users'],
- $sendTo['users']
- );
- $usersToAdd = array_diff(
- $sendTo['users'],
- $eventInfo['send_to']['users']
- );
- $groupsToDelete = array_diff(
- $eventInfo['send_to']['groups'],
- $sendTo['groups']
- );
- $groupToAdd = array_diff(
- $sendTo['groups'],
- $eventInfo['send_to']['groups']
- );
- if ($sendTo['everyone']) {
- // Delete all from group
- if (isset($eventInfo['send_to']['groups']) &&
- !empty($eventInfo['send_to']['groups'])
- ) {
- foreach ($eventInfo['send_to']['groups'] as $group) {
- $groupIidItem = 0;
- if ($group) {
- $groupInfoItem = GroupManager::get_group_properties(
- $group
- );
- if ($groupInfoItem) {
- $groupIidItem = $groupInfoItem['iid'];
- }
- }
- api_item_property_delete(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 0,
- $groupIidItem,
- $this->sessionId
- );
- }
- }
- // Storing the selected users.
- if (isset($eventInfo['send_to']['users']) &&
- !empty($eventInfo['send_to']['users'])
- ) {
- foreach ($eventInfo['send_to']['users'] as $userId) {
- api_item_property_delete(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- $userId,
- $groupIid,
- $this->sessionId
- );
- }
- }
- // Add to everyone only.
- api_item_property_update(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 'visible',
- $authorId,
- $groupInfo,
- null,
- $start,
- $end,
- $this->sessionId
- );
- } else {
- // Delete "everyone".
- api_item_property_delete(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 0,
- 0,
- $this->sessionId
- );
- // Add groups
- if (!empty($groupToAdd)) {
- foreach ($groupToAdd as $group) {
- $groupInfoItem = [];
- if ($group) {
- $groupInfoItem = GroupManager::get_group_properties(
- $group
- );
- }
- api_item_property_update(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 'visible',
- $authorId,
- $groupInfoItem,
- 0,
- $start,
- $end,
- $this->sessionId
- );
- }
- }
- // Delete groups.
- if (!empty($groupsToDelete)) {
- foreach ($groupsToDelete as $group) {
- $groupIidItem = 0;
- $groupInfoItem = [];
- if ($group) {
- $groupInfoItem = GroupManager::get_group_properties(
- $group
- );
- if ($groupInfoItem) {
- $groupIidItem = $groupInfoItem['iid'];
- }
- }
- api_item_property_delete(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 0,
- $groupIidItem,
- $this->sessionId
- );
- }
- }
- // Add users.
- if (!empty($usersToAdd)) {
- foreach ($usersToAdd as $userId) {
- api_item_property_update(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 'visible',
- $authorId,
- $groupInfo,
- $userId,
- $start,
- $end,
- $this->sessionId
- );
- }
- }
- // Delete users.
- if (!empty($usersToDelete)) {
- foreach ($usersToDelete as $userId) {
- api_item_property_delete(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- $userId,
- $groupInfo,
- $this->sessionId
- );
- }
- }
- }
- }
- // Add announcement.
- if (isset($addAnnouncement) && !empty($addAnnouncement)) {
- $this->storeAgendaEventAsAnnouncement(
- $id,
- $usersToSend
- );
- }
- // Add attachment.
- if (isset($attachmentArray) && !empty($attachmentArray)) {
- $counter = 0;
- foreach ($attachmentArray as $attachmentItem) {
- $this->updateAttachment(
- $attachmentItem['id'],
- $id,
- $attachmentItem,
- $attachmentCommentList[$counter],
- $this->course
- );
- $counter++;
- }
- }
- return true;
- } else {
- return false;
- }
- break;
- case 'admin':
- case 'platform':
- if (api_is_platform_admin()) {
- $attributes = [
- 'title' => $title,
- 'start_date' => $start,
- 'end_date' => $end,
- 'all_day' => $allDay,
- ];
- if ($updateContent) {
- $attributes['content'] = $content;
- }
- Database::update(
- $this->tbl_global_agenda,
- $attributes,
- ['id = ?' => $id]
- );
- }
- break;
- }
- }
- /**
- * @param int $id
- * @param bool $deleteAllItemsFromSerie
- */
- public function deleteEvent($id, $deleteAllItemsFromSerie = false)
- {
- switch ($this->type) {
- case 'personal':
- $eventInfo = $this->get_event($id);
- if ($eventInfo['user'] == api_get_user_id()) {
- Database::delete(
- $this->tbl_personal_agenda,
- ['id = ?' => $id]
- );
- }
- break;
- case 'course':
- $courseId = api_get_course_int_id();
- $sessionId = api_get_session_id();
- $isAllowToEdit = api_is_allowed_to_edit(null, true);
- if ($isAllowToEdit == false && !empty($sessionId)) {
- $allowDhrToEdit = api_get_configuration_value('allow_agenda_edit_for_hrm');
- if ($allowDhrToEdit) {
- $isHrm = SessionManager::isUserSubscribedAsHRM(
- $sessionId,
- api_get_user_id()
- );
- if ($isHrm) {
- $isAllowToEdit = true;
- }
- }
- }
- if (!empty($courseId) && $isAllowToEdit) {
- // Delete
- $eventInfo = $this->get_event($id);
- if ($deleteAllItemsFromSerie) {
- /* This is one of the children.
- Getting siblings and delete 'Em all + the father! */
- if (isset($eventInfo['parent_event_id']) && !empty($eventInfo['parent_event_id'])) {
- // Removing items.
- $events = $this->getAllRepeatEvents(
- $eventInfo['parent_event_id']
- );
- if (!empty($events)) {
- foreach ($events as $event) {
- $this->deleteEvent($event['id']);
- }
- }
- // Removing parent.
- $this->deleteEvent($eventInfo['parent_event_id']);
- } else {
- // This is the father looking for the children.
- $events = $this->getAllRepeatEvents($id);
- if (!empty($events)) {
- foreach ($events as $event) {
- $this->deleteEvent($event['id']);
- }
- }
- }
- }
- // Removing from events.
- Database::delete(
- $this->tbl_course_agenda,
- ['id = ? AND c_id = ?' => [$id, $courseId]]
- );
- api_item_property_update(
- $this->course,
- TOOL_CALENDAR_EVENT,
- $id,
- 'delete',
- api_get_user_id()
- );
- // Removing from series.
- Database::delete(
- $this->table_repeat,
- [
- 'cal_id = ? AND c_id = ?' => [
- $id,
- $courseId,
- ],
- ]
- );
- if (isset($eventInfo['attachment']) && !empty($eventInfo['attachment'])) {
- foreach ($eventInfo['attachment'] as $attachment) {
- self::deleteAttachmentFile(
- $attachment['id'],
- $this->course
- );
- }
- }
- }
- break;
- case 'admin':
- if (api_is_platform_admin()) {
- Database::delete(
- $this->tbl_global_agenda,
- ['id = ?' => $id]
- );
- }
- break;
- }
- }
- /**
- * Get agenda events.
- *
- * @param int $start
- * @param int $end
- * @param int $courseId
- * @param int $groupId
- * @param int $user_id
- * @param string $format
- *
- * @return array|string
- */
- public function getEvents(
- $start,
- $end,
- $courseId = null,
- $groupId = null,
- $user_id = 0,
- $format = 'json'
- ) {
- switch ($this->type) {
- case 'admin':
- $this->getPlatformEvents($start, $end);
- break;
- case 'course':
- $courseInfo = api_get_course_info_by_id($courseId);
- // Session coach can see all events inside a session.
- if (api_is_coach()) {
- // Own course
- $this->getCourseEvents(
- $start,
- $end,
- $courseInfo,
- $groupId,
- $this->sessionId,
- $user_id
- );
- // Others
- $this->getSessionEvents(
- $start,
- $end,
- $this->sessionId,
- $user_id,
- $this->eventOtherSessionColor
- );
- } else {
- $this->getCourseEvents(
- $start,
- $end,
- $courseInfo,
- $groupId,
- $this->sessionId,
- $user_id
- );
- }
- break;
- case 'personal':
- default:
- $sessionFilterActive = false;
- if (!empty($this->sessionId)) {
- $sessionFilterActive = true;
- }
- if ($sessionFilterActive == false) {
- // Getting personal events
- $this->getPersonalEvents($start, $end);
- // Getting platform/admin events
- $this->getPlatformEvents($start, $end);
- }
- $ignoreVisibility = api_get_configuration_value('personal_agenda_show_all_session_events');
- // Getting course events
- $my_course_list = [];
- if (!api_is_anonymous()) {
- $session_list = SessionManager::get_sessions_by_user(
- api_get_user_id(),
- $ignoreVisibility
- );
- $my_course_list = CourseManager::get_courses_list_by_user_id(
- api_get_user_id(),
- false
- );
- }
- if (api_is_drh()) {
- if (api_drh_can_access_all_session_content()) {
- $session_list = [];
- $sessionList = SessionManager::get_sessions_followed_by_drh(
- api_get_user_id(),
- null,
- null,
- null,
- true,
- false
- );
- if (!empty($sessionList)) {
- foreach ($sessionList as $sessionItem) {
- $sessionId = $sessionItem['id'];
- $courses = SessionManager::get_course_list_by_session_id(
- $sessionId
- );
- $sessionInfo = [
- 'session_id' => $sessionId,
- 'courses' => $courses,
- ];
- $session_list[] = $sessionInfo;
- }
- }
- }
- }
- if (!empty($session_list)) {
- foreach ($session_list as $session_item) {
- if ($sessionFilterActive) {
- if ($this->sessionId != $session_item['session_id']) {
- continue;
- }
- }
- $my_courses = $session_item['courses'];
- $my_session_id = $session_item['session_id'];
- if (!empty($my_courses)) {
- foreach ($my_courses as $course_item) {
- $courseInfo = api_get_course_info_by_id(
- $course_item['real_id']
- );
- $this->getCourseEvents(
- $start,
- $end,
- $courseInfo,
- 0,
- $my_session_id
- );
- }
- }
- }
- }
- if (!empty($my_course_list) && $sessionFilterActive == false) {
- foreach ($my_course_list as $courseInfoItem) {
- $courseInfo = api_get_course_info_by_id(
- $courseInfoItem['real_id']
- );
- if (isset($courseId) && !empty($courseId)) {
- if ($courseInfo['real_id'] == $courseId) {
- $this->getCourseEvents(
- $start,
- $end,
- $courseInfo,
- 0,
- 0,
- $user_id
- );
- }
- } else {
- $this->getCourseEvents(
- $start,
- $end,
- $courseInfo,
- 0,
- 0,
- $user_id
- );
- }
- }
- }
- break;
- }
- $this->cleanEvents();
- switch ($format) {
- case 'json':
- if (empty($this->events)) {
- return '[]';
- }
- return json_encode($this->events);
- break;
- case 'array':
- if (empty($this->events)) {
- return [];
- }
- return $this->events;
- break;
- }
- }
- /**
- * Clean events.
- *
- * @return bool
- */
- public function cleanEvents()
- {
- if (empty($this->events)) {
- return false;
- }
- foreach ($this->events as &$event) {
- $event['description'] = Security::remove_XSS($event['description']);
- $event['title'] = Security::remove_XSS($event['title']);
- }
- return true;
- }
- /**
- * @param int $id
- * @param int $minute_delta
- *
- * @return int
- */
- public function resizeEvent($id, $minute_delta)
- {
- $id = (int) $id;
- $delta = (int) $minute_delta;
- $event = $this->get_event($id);
- if (!empty($event)) {
- switch ($this->type) {
- case 'personal':
- $sql = "UPDATE $this->tbl_personal_agenda SET
- enddate = DATE_ADD(enddate, INTERVAL $delta MINUTE)
- WHERE id = ".$id;
- Database::query($sql);
- break;
- case 'course':
- $sql = "UPDATE $this->tbl_course_agenda SET
- end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
- WHERE
- c_id = ".$this->course['real_id']." AND
- id = ".$id;
- Database::query($sql);
- break;
- case 'admin':
- $sql = "UPDATE $this->tbl_global_agenda SET
- end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
- WHERE id = ".$id;
- Database::query($sql);
- break;
- }
- }
- return 1;
- }
- /**
- * @param int $id
- * @param int $minute_delta minutes
- * @param int $allDay
- *
- * @return int
- */
- public function move_event($id, $minute_delta, $allDay)
- {
- $id = (int) $id;
- $event = $this->get_event($id);
- if (empty($event)) {
- return false;
- }
- // we convert the hour delta into minutes and add the minute delta
- $delta = (int) $minute_delta;
- $allDay = (int) $allDay;
- if (!empty($event)) {
- switch ($this->type) {
- case 'personal':
- $sql = "UPDATE $this->tbl_personal_agenda SET
- all_day = $allDay, date = DATE_ADD(date, INTERVAL $delta MINUTE),
- enddate = DATE_ADD(enddate, INTERVAL $delta MINUTE)
- WHERE id=".$id;
- Database::query($sql);
- break;
- case 'course':
- $sql = "UPDATE $this->tbl_course_agenda SET
- all_day = $allDay,
- start_date = DATE_ADD(start_date, INTERVAL $delta MINUTE),
- end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
- WHERE
- c_id = ".$this->course['real_id']." AND
- id=".$id;
- Database::query($sql);
- break;
- case 'admin':
- $sql = "UPDATE $this->tbl_global_agenda SET
- all_day = $allDay,
- start_date = DATE_ADD(start_date,INTERVAL $delta MINUTE),
- end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
- WHERE id=".$id;
- Database::query($sql);
- break;
- }
- }
- return 1;
- }
- /**
- * Gets a single event.
- *
- * @param int $id event id
- *
- * @return array
- */
- public function get_event($id)
- {
- // make sure events of the personal agenda can only be seen by the user himself
- $id = (int) $id;
- $event = null;
- switch ($this->type) {
- case 'personal':
- $sql = "SELECT * FROM ".$this->tbl_personal_agenda."
- WHERE id = $id AND user = ".api_get_user_id();
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $event = Database::fetch_array($result, 'ASSOC');
- $event['description'] = $event['text'];
- $event['content'] = $event['text'];
- $event['start_date'] = $event['date'];
- $event['end_date'] = $event['enddate'];
- }
- break;
- case 'course':
- if (!empty($this->course['real_id'])) {
- $sql = "SELECT * FROM ".$this->tbl_course_agenda."
- WHERE c_id = ".$this->course['real_id']." AND id = ".$id;
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $event = Database::fetch_array($result, 'ASSOC');
- $event['description'] = $event['content'];
- // Getting send to array
- $event['send_to'] = $this->getUsersAndGroupSubscribedToEvent(
- $id,
- $this->course['real_id'],
- $this->sessionId
- );
- // Getting repeat info
- $event['repeat_info'] = $this->getRepeatedInfoByEvent(
- $id,
- $this->course['real_id']
- );
- if (!empty($event['parent_event_id'])) {
- $event['parent_info'] = $this->get_event(
- $event['parent_event_id']
- );
- }
- $event['attachment'] = $this->getAttachmentList(
- $id,
- $this->course
- );
- }
- }
- break;
- case 'admin':
- case 'platform':
- $sql = "SELECT * FROM ".$this->tbl_global_agenda."
- WHERE id = $id";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $event = Database::fetch_array($result, 'ASSOC');
- $event['description'] = $event['content'];
- }
- break;
- }
- return $event;
- }
- /**
- * Gets personal events.
- *
- * @param int $start
- * @param int $end
- *
- * @return array
- */
- public function getPersonalEvents($start, $end)
- {
- $start = (int) $start;
- $end = (int) $end;
- $startCondition = '';
- $endCondition = '';
- if ($start !== 0) {
- $startCondition = "AND date >= '".api_get_utc_datetime($start)."'";
- }
- if ($start !== 0) {
- $endCondition = "AND (enddate <= '".api_get_utc_datetime($end)."' OR enddate IS NULL)";
- }
- $user_id = api_get_user_id();
- $sql = "SELECT * FROM ".$this->tbl_personal_agenda."
- WHERE user = $user_id $startCondition $endCondition";
- $result = Database::query($sql);
- $my_events = [];
- if (Database::num_rows($result)) {
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- $event = [];
- $event['id'] = 'personal_'.$row['id'];
- $event['title'] = $row['title'];
- $event['className'] = 'personal';
- $event['borderColor'] = $event['backgroundColor'] = $this->event_personal_color;
- $event['editable'] = true;
- $event['sent_to'] = get_lang('Me');
- $event['type'] = 'personal';
- if (!empty($row['date'])) {
- $event['start'] = $this->formatEventDate($row['date']);
- $event['start_date_localtime'] = api_get_local_time($row['date']);
- }
- if (!empty($row['enddate'])) {
- $event['end'] = $this->formatEventDate($row['enddate']);
- $event['end_date_localtime'] = api_get_local_time($row['enddate']);
- }
- $event['description'] = $row['text'];
- $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
- $event['parent_event_id'] = 0;
- $event['has_children'] = 0;
- $my_events[] = $event;
- $this->events[] = $event;
- }
- }
- // Add plugin personal events
- $this->plugin = new AppPlugin();
- $plugins = $this->plugin->getInstalledPluginListObject();
- /** @var Plugin $plugin */
- foreach ($plugins as $plugin) {
- if ($plugin->hasPersonalEvents && method_exists($plugin, 'getPersonalEvents')) {
- $pluginEvents = $plugin->getPersonalEvents($this, $start, $end);
- if (!empty($pluginEvents)) {
- $this->events = array_merge($this->events, $pluginEvents);
- }
- }
- }
- return $my_events;
- }
- /**
- * Get user/group list per event.
- *
- * @param int $eventId
- * @param int $courseId
- * @param int $sessionId
- * @paraù int $sessionId
- *
- * @return array
- */
- public function getUsersAndGroupSubscribedToEvent(
- $eventId,
- $courseId,
- $sessionId
- ) {
- $eventId = (int) $eventId;
- $courseId = (int) $courseId;
- $sessionId = (int) $sessionId;
- $sessionCondition = "ip.session_id = $sessionId";
- if (empty($sessionId)) {
- $sessionCondition = " (ip.session_id = 0 OR ip.session_id IS NULL) ";
- }
- $tlb_course_agenda = Database::get_course_table(TABLE_AGENDA);
- $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
- // Get sent_tos
- $sql = "SELECT DISTINCT to_user_id, to_group_id
- FROM $tbl_property ip
- INNER JOIN $tlb_course_agenda agenda
- ON (
- ip.ref = agenda.id AND
- ip.c_id = agenda.c_id AND
- ip.tool = '".TOOL_CALENDAR_EVENT."'
- )
- WHERE
- ref = $eventId AND
- ip.visibility = '1' AND
- ip.c_id = $courseId AND
- $sessionCondition
- ";
- $result = Database::query($sql);
- $users = [];
- $groups = [];
- $everyone = false;
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- if (!empty($row['to_group_id'])) {
- $groups[] = $row['to_group_id'];
- }
- if (!empty($row['to_user_id'])) {
- $users[] = $row['to_user_id'];
- }
- if (empty($groups) && empty($users)) {
- if ($row['to_group_id'] == 0) {
- $everyone = true;
- }
- }
- }
- return [
- 'everyone' => $everyone,
- 'users' => $users,
- 'groups' => $groups,
- ];
- }
- /**
- * @param int $start
- * @param int $end
- * @param int $sessionId
- * @param int $userId
- * @param string $color
- *
- * @return array
- */
- public function getSessionEvents(
- $start,
- $end,
- $sessionId = 0,
- $userId = 0,
- $color = ''
- ) {
- $courses = SessionManager::get_course_list_by_session_id($sessionId);
- if (!empty($courses)) {
- foreach ($courses as $course) {
- $this->getCourseEvents(
- $start,
- $end,
- $course,
- 0,
- $sessionId,
- 0,
- $color
- );
- }
- }
- }
- /**
- * @param int $start
- * @param int $end
- * @param array $courseInfo
- * @param int $groupId
- * @param int $sessionId
- * @param int $user_id
- * @param string $color
- *
- * @return array
- */
- public function getCourseEvents(
- $start,
- $end,
- $courseInfo,
- $groupId = 0,
- $sessionId = 0,
- $user_id = 0,
- $color = ''
- ) {
- $start = isset($start) && !empty($start) ? api_get_utc_datetime(intval($start)) : null;
- $end = isset($end) && !empty($end) ? api_get_utc_datetime(intval($end)) : null;
- if (empty($courseInfo)) {
- return [];
- }
- $courseId = $courseInfo['real_id'];
- if (empty($courseId)) {
- return [];
- }
- $sessionId = (int) $sessionId;
- $user_id = (int) $user_id;
- $groupList = GroupManager::get_group_list(
- null,
- $courseInfo,
- null,
- $sessionId
- );
- $groupNameList = [];
- if (!empty($groupList)) {
- foreach ($groupList as $group) {
- $groupNameList[$group['iid']] = $group['name'];
- }
- }
- if (api_is_platform_admin() || api_is_allowed_to_edit()) {
- $isAllowToEdit = true;
- } else {
- $isAllowToEdit = CourseManager::is_course_teacher(
- api_get_user_id(),
- $courseInfo['code']
- );
- }
- $isAllowToEditByHrm = false;
- if (!empty($sessionId)) {
- $allowDhrToEdit = api_get_configuration_value('allow_agenda_edit_for_hrm');
- if ($allowDhrToEdit) {
- $isHrm = SessionManager::isUserSubscribedAsHRM($sessionId, api_get_user_id());
- if ($isHrm) {
- $isAllowToEdit = $isAllowToEditByHrm = true;
- }
- }
- }
- $groupMemberships = [];
- if (!empty($groupId)) {
- $groupMemberships = [$groupId];
- } else {
- if ($isAllowToEdit) {
- if (!empty($groupList)) {
- // c_item_property.to_group_id field was migrated to use
- // c_group_info.iid
- $groupMemberships = array_column($groupList, 'iid');
- }
- } else {
- // get only related groups from user
- $groupMemberships = GroupManager::get_group_ids(
- $courseId,
- api_get_user_id()
- );
- }
- }
- $tlb_course_agenda = Database::get_course_table(TABLE_AGENDA);
- $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
- if (empty($sessionId)) {
- $sessionCondition = "
- (
- agenda.session_id = 0 AND (ip.session_id IS NULL OR ip.session_id = 0)
- ) ";
- } else {
- $sessionCondition = "
- (
- agenda.session_id = $sessionId AND
- ip.session_id = $sessionId
- ) ";
- }
- if ($isAllowToEdit) {
- // No group filter was asked
- if (empty($groupId)) {
- if (empty($user_id)) {
- // Show all events not added in group
- $userCondition = ' (ip.to_group_id IS NULL OR ip.to_group_id = 0) ';
- // admin see only his stuff
- if ($this->type === 'personal') {
- $userCondition = " (ip.to_user_id = ".api_get_user_id()." AND (ip.to_group_id IS NULL OR ip.to_group_id = 0) ) ";
- $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) ) ";
- }
- if (!empty($groupMemberships)) {
- // Show events sent to selected groups
- $userCondition .= " OR (ip.to_user_id = 0 OR ip.to_user_id is NULL) AND (ip.to_group_id IN (".implode(", ", $groupMemberships).")) ";
- }
- } else {
- // Show events of requested user in no group
- $userCondition = " (ip.to_user_id = $user_id AND (ip.to_group_id IS NULL OR ip.to_group_id = 0)) ";
- // Show events sent to selected groups
- if (!empty($groupMemberships)) {
- $userCondition .= " OR (ip.to_user_id = $user_id) AND (ip.to_group_id IN (".implode(", ", $groupMemberships).")) ";
- }
- }
- } else {
- // Show only selected groups (depending of user status)
- $userCondition = " (ip.to_user_id = 0 OR ip.to_user_id is NULL) AND (ip.to_group_id IN (".implode(", ", $groupMemberships).")) ";
- if (!empty($groupMemberships)) {
- // Show send to $user_id in selected groups
- $userCondition .= " OR (ip.to_user_id = $user_id) AND (ip.to_group_id IN (".implode(", ", $groupMemberships).")) ";
- }
- }
- } else {
- // No group filter was asked
- if (empty($groupId)) {
- // Show events sent to everyone and no group
- $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) ';
- // Show events sent to selected groups
- if (!empty($groupMemberships)) {
- $userCondition .= " OR (ip.to_user_id = 0 OR ip.to_user_id is NULL) AND (ip.to_group_id IN (".implode(", ", $groupMemberships)."))) ";
- } else {
- $userCondition .= " ) ";
- }
- $userCondition .= " OR (ip.to_user_id = ".api_get_user_id()." AND (ip.to_group_id IS NULL OR ip.to_group_id = 0)) ";
- } else {
- if (!empty($groupMemberships)) {
- // Show send to everyone - and only selected groups
- $userCondition = " (ip.to_user_id = 0 OR ip.to_user_id is NULL) AND (ip.to_group_id IN (".implode(", ", $groupMemberships).")) ";
- }
- }
- // Show sent to only me and no group
- if (!empty($groupMemberships)) {
- $userCondition .= " OR (ip.to_user_id = ".api_get_user_id().") AND (ip.to_group_id IN (".implode(", ", $groupMemberships).")) ";
- } else {
- // Show sent to only me and selected groups
- }
- }
- if (api_is_allowed_to_edit()) {
- $visibilityCondition = " (ip.visibility IN ('1', '0')) ";
- } else {
- $visibilityCondition = " (ip.visibility = '1') ";
- }
- $sql = "SELECT DISTINCT
- agenda.*,
- ip.visibility,
- ip.to_group_id,
- ip.insert_user_id,
- ip.ref,
- to_user_id
- FROM $tlb_course_agenda agenda
- INNER JOIN $tbl_property ip
- ON (
- agenda.id = ip.ref AND
- agenda.c_id = ip.c_id AND
- ip.tool = '".TOOL_CALENDAR_EVENT."'
- )
- WHERE
- $sessionCondition AND
- ($userCondition) AND
- $visibilityCondition AND
- agenda.c_id = $courseId
- ";
- $dateCondition = '';
- if (!empty($start) && !empty($end)) {
- $dateCondition .= "AND (
- agenda.start_date BETWEEN '".$start."' AND '".$end."' OR
- agenda.end_date BETWEEN '".$start."' AND '".$end."' OR
- (
- agenda.start_date IS NOT NULL AND agenda.end_date IS NOT NULL AND
- YEAR(agenda.start_date) = YEAR(agenda.end_date) AND
- MONTH('$start') BETWEEN MONTH(agenda.start_date) AND MONTH(agenda.end_date)
- )
- )";
- }
- $sql .= $dateCondition;
- $result = Database::query($sql);
- $coachCanEdit = false;
- if (!empty($sessionId)) {
- $coachCanEdit = api_is_coach($sessionId, $courseId) || api_is_platform_admin();
- }
- if (Database::num_rows($result)) {
- $eventsAdded = array_column($this->events, 'unique_id');
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- $event = [];
- $event['id'] = 'course_'.$row['id'];
- $event['unique_id'] = $row['iid'];
- // To avoid doubles
- if (in_array($event['unique_id'], $eventsAdded)) {
- continue;
- }
- $eventsAdded[] = $event['unique_id'];
- $eventId = $row['ref'];
- $items = $this->getUsersAndGroupSubscribedToEvent(
- $eventId,
- $courseId,
- $this->sessionId
- );
- $group_to_array = $items['groups'];
- $user_to_array = $items['users'];
- $attachmentList = $this->getAttachmentList(
- $row['id'],
- $courseInfo
- );
- $event['attachment'] = '';
- if (!empty($attachmentList)) {
- foreach ($attachmentList as $attachment) {
- $has_attachment = Display::return_icon(
- 'attachment.gif',
- get_lang('Attachment')
- );
- $user_filename = $attachment['filename'];
- $url = api_get_path(WEB_CODE_PATH).'calendar/download.php?file='.$attachment['path'].'&course_id='.$courseId.'&'.api_get_cidreq();
- $event['attachment'] .= $has_attachment.
- Display::url(
- $user_filename,
- $url
- ).'<br />';
- }
- }
- $event['title'] = $row['title'];
- $event['className'] = 'course';
- $event['allDay'] = 'false';
- $event['course_id'] = $courseId;
- $event['borderColor'] = $event['backgroundColor'] = $this->event_course_color;
- $sessionInfo = [];
- if (isset($row['session_id']) && !empty($row['session_id'])) {
- $sessionInfo = api_get_session_info($sessionId);
- $event['borderColor'] = $event['backgroundColor'] = $this->event_session_color;
- }
- $event['session_name'] = isset($sessionInfo['name']) ? $sessionInfo['name'] : '';
- $event['course_name'] = isset($courseInfo['title']) ? $courseInfo['title'] : '';
- if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
- $event['borderColor'] = $event['backgroundColor'] = $this->event_group_color;
- }
- if (!empty($color)) {
- $event['borderColor'] = $event['backgroundColor'] = $color;
- }
- if (isset($row['color']) && !empty($row['color'])) {
- $event['borderColor'] = $event['backgroundColor'] = $row['color'];
- }
- $event['editable'] = false;
- if ($this->getIsAllowedToEdit() && $this->type == 'course') {
- $event['editable'] = true;
- if (!empty($sessionId)) {
- if ($coachCanEdit == false) {
- $event['editable'] = false;
- }
- if ($isAllowToEditByHrm) {
- $event['editable'] = true;
- }
- }
- // if user is author then he can edit the item
- if (api_get_user_id() == $row['insert_user_id']) {
- $event['editable'] = true;
- }
- }
- if (!empty($row['start_date'])) {
- $event['start'] = $this->formatEventDate($row['start_date']);
- $event['start_date_localtime'] = api_get_local_time($row['start_date']);
- }
- if (!empty($row['end_date'])) {
- $event['end'] = $this->formatEventDate($row['end_date']);
- $event['end_date_localtime'] = api_get_local_time($row['end_date']);
- }
- $event['sent_to'] = '';
- $event['type'] = 'course';
- if ($row['session_id'] != 0) {
- $event['type'] = 'session';
- }
- // Event Sent to a group?
- if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
- $sent_to = [];
- if (!empty($group_to_array)) {
- foreach ($group_to_array as $group_item) {
- $sent_to[] = $groupNameList[$group_item];
- }
- }
- $sent_to = implode('@@', $sent_to);
- $sent_to = str_replace(
- '@@',
- '</div><div class="label_tag notice">',
- $sent_to
- );
- $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
- $event['type'] = 'group';
- }
- // Event sent to a user?
- if (isset($row['to_user_id'])) {
- $sent_to = [];
- if (!empty($user_to_array)) {
- foreach ($user_to_array as $item) {
- $user_info = api_get_user_info($item);
- // Add username as tooltip for $event['sent_to'] - ref #4226
- $username = api_htmlentities(
- sprintf(
- get_lang('Login: %s'),
- $user_info['username']
- ),
- ENT_QUOTES
- );
- $sent_to[] = "<span title='".$username."'>".$user_info['complete_name']."</span>";
- }
- }
- $sent_to = implode('@@', $sent_to);
- $sent_to = str_replace(
- '@@',
- '</div><div class="label_tag notice">',
- $sent_to
- );
- $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
- }
- //Event sent to everyone!
- if (empty($event['sent_to'])) {
- $event['sent_to'] = '<div class="label_tag notice">'.get_lang('Everyone').'</div>';
- }
- $event['description'] = $row['content'];
- $event['visibility'] = $row['visibility'];
- $event['real_id'] = $row['id'];
- $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
- $event['parent_event_id'] = $row['parent_event_id'];
- $event['has_children'] = $this->hasChildren($row['id'], $courseId) ? 1 : 0;
- $event['comment'] = $row['comment'];
- $this->events[] = $event;
- }
- }
- return $this->events;
- }
- /**
- * @param int $start tms
- * @param int $end tms
- *
- * @return array
- */
- public function getPlatformEvents($start, $end)
- {
- $start = isset($start) && !empty($start) ? api_get_utc_datetime(intval($start)) : null;
- $end = isset($end) && !empty($end) ? api_get_utc_datetime(intval($end)) : null;
- $dateCondition = '';
- if (!empty($start) && !empty($end)) {
- $dateCondition .= "AND (
- start_date BETWEEN '".$start."' AND '".$end."' OR
- end_date BETWEEN '".$start."' AND '".$end."' OR
- (
- start_date IS NOT NULL AND end_date IS NOT NULL AND
- YEAR(start_date) = YEAR(end_date) AND
- MONTH('$start') BETWEEN MONTH(start_date) AND MONTH(end_date)
- )
- )";
- }
- $access_url_id = api_get_current_access_url_id();
- $sql = "SELECT *
- FROM ".$this->tbl_global_agenda."
- WHERE access_url_id = $access_url_id
- $dateCondition";
- $result = Database::query($sql);
- $my_events = [];
- if (Database::num_rows($result)) {
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- $event = [];
- $event['id'] = 'platform_'.$row['id'];
- $event['title'] = $row['title'];
- $event['className'] = 'platform';
- $event['allDay'] = 'false';
- $event['borderColor'] = $event['backgroundColor'] = $this->event_platform_color;
- $event['editable'] = false;
- $event['type'] = 'admin';
- if (api_is_platform_admin() && $this->type === 'admin') {
- $event['editable'] = true;
- }
- if (!empty($row['start_date'])) {
- $event['start'] = $this->formatEventDate($row['start_date']);
- $event['start_date_localtime'] = api_get_local_time($row['start_date']);
- }
- if (!empty($row['end_date'])) {
- $event['end'] = $this->formatEventDate($row['end_date']);
- $event['end_date_localtime'] = api_get_local_time($row['end_date']);
- }
- $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
- $event['parent_event_id'] = 0;
- $event['has_children'] = 0;
- $event['description'] = $row['content'];
- $my_events[] = $event;
- $this->events[] = $event;
- }
- }
- return $my_events;
- }
- /**
- * @param FormValidator $form
- * @param array $groupList
- * @param array $userList
- * @param array $sendTo array('users' => [1, 2], 'groups' => [3, 4])
- * @param array $attributes
- * @param bool $addOnlyItemsInSendTo
- * @param bool $required
- */
- public function setSendToSelect(
- $form,
- $groupList = [],
- $userList = [],
- $sendTo = [],
- $attributes = [],
- $addOnlyItemsInSendTo = false,
- $required = false
- ) {
- $params = [
- 'id' => 'users_to_send_id',
- 'data-placeholder' => get_lang('Select'),
- 'multiple' => 'multiple',
- 'class' => 'multiple-select',
- ];
- if (!empty($attributes)) {
- $params = array_merge($params, $attributes);
- if (empty($params['multiple'])) {
- unset($params['multiple']);
- }
- }
- $sendToGroups = isset($sendTo['groups']) ? $sendTo['groups'] : [];
- $sendToUsers = isset($sendTo['users']) ? $sendTo['users'] : [];
- /** @var HTML_QuickForm_select $select */
- $select = $form->addSelect(
- 'users_to_send',
- get_lang('To'),
- null,
- $params
- );
- if ($required) {
- $form->setRequired($select);
- }
- $selectedEveryoneOptions = [];
- if (isset($sendTo['everyone']) && $sendTo['everyone']) {
- $selectedEveryoneOptions = ['selected'];
- $sendToUsers = [];
- }
- $select->addOption(
- get_lang('Everyone'),
- 'everyone',
- $selectedEveryoneOptions
- );
- $options = [];
- if (is_array($groupList)) {
- foreach ($groupList as $group) {
- $count_users = isset($group['count_users']) ? $group['count_users'] : $group['userNb'];
- $count_users = " – $count_users ".get_lang('Users');
- $option = [
- 'text' => $group['name'].$count_users,
- 'value' => "GROUP:".$group['id'],
- ];
- $selected = in_array(
- $group['id'],
- $sendToGroups
- ) ? true : false;
- if ($selected) {
- $option['selected'] = 'selected';
- }
- if ($addOnlyItemsInSendTo) {
- if ($selected) {
- $options[] = $option;
- }
- } else {
- $options[] = $option;
- }
- }
- $select->addOptGroup($options, get_lang('Groups'));
- }
- // adding the individual users to the select form
- if (is_array($userList)) {
- $options = [];
- foreach ($userList as $user) {
- if ($user['status'] == ANONYMOUS) {
- continue;
- }
- $option = [
- 'text' => api_get_person_name(
- $user['firstname'],
- $user['lastname']
- ).' ('.$user['username'].')',
- 'value' => "USER:".$user['user_id'],
- ];
- $selected = in_array(
- $user['user_id'],
- $sendToUsers
- ) ? true : false;
- if ($selected) {
- $option['selected'] = 'selected';
- }
- if ($addOnlyItemsInSendTo) {
- if ($selected) {
- $options[] = $option;
- }
- } else {
- $options[] = $option;
- }
- }
- $select->addOptGroup($options, get_lang('Users'));
- }
- }
- /**
- * Separates the users and groups array
- * users have a value USER:XXX (with XXX the user id
- * groups have a value GROUP:YYY (with YYY the group id)
- * use the 'everyone' key.
- *
- * @author Julio Montoya based in separate_users_groups in agenda.inc.php
- *
- * @param array $to
- *
- * @return array
- */
- public function parseSendToArray($to)
- {
- $groupList = [];
- $userList = [];
- $sendTo = null;
- $sendTo['everyone'] = false;
- if (is_array($to) && count($to) > 0) {
- foreach ($to as $item) {
- if ($item == 'everyone') {
- $sendTo['everyone'] = true;
- } else {
- list($type, $id) = explode(':', $item);
- switch ($type) {
- case 'GROUP':
- $groupList[] = $id;
- break;
- case 'USER':
- $userList[] = $id;
- break;
- }
- }
- }
- $sendTo['groups'] = $groupList;
- $sendTo['users'] = $userList;
- }
- return $sendTo;
- }
- /**
- * @param array $params
- *
- * @return FormValidator
- */
- public function getForm($params = [])
- {
- $action = isset($params['action']) ? Security::remove_XSS($params['action']) : null;
- $id = isset($params['id']) ? (int) $params['id'] : 0;
- $url = api_get_self().'?action='.$action.'&id='.$id.'&type='.$this->type;
- if ($this->type == 'course') {
- $url = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&id='.$id.'&type='.$this->type;
- }
- $form = new FormValidator(
- 'add_event',
- 'post',
- $url,
- null,
- ['enctype' => 'multipart/form-data']
- );
- $idAttach = isset($params['id_attach']) ? (int) $params['id_attach'] : null;
- $groupId = api_get_group_id();
- $form_Title = get_lang('Add event to agenda');
- if (!empty($id)) {
- $form_Title = get_lang('Edit event');
- }
- $form->addHeader($form_Title);
- $form->addElement('hidden', 'id', $id);
- $form->addElement('hidden', 'action', $action);
- $form->addElement('hidden', 'id_attach', $idAttach);
- $isSubEventEdition = false;
- $isParentFromSerie = false;
- $showAttachmentForm = true;
- if ($this->type == 'course') {
- // Edition mode.
- if (!empty($id)) {
- $showAttachmentForm = false;
- if (isset($params['parent_event_id']) && !empty($params['parent_event_id'])) {
- $isSubEventEdition = true;
- }
- if (!empty($params['repeat_info'])) {
- $isParentFromSerie = true;
- }
- }
- }
- if ($isSubEventEdition) {
- $form->addElement(
- 'label',
- null,
- Display::return_message(
- get_lang('Editing this event will remove it from the serie of events it is currently part of'),
- 'warning'
- )
- );
- }
- $form->addElement('text', 'title', get_lang('Event name'));
- if (isset($groupId) && !empty($groupId)) {
- $form->addElement(
- 'hidden',
- 'users_to_send[]',
- "GROUP:$groupId"
- );
- $form->addElement('hidden', 'to', 'true');
- } else {
- $sendTo = isset($params['send_to']) ? $params['send_to'] : ['everyone' => true];
- if ($this->type == 'course') {
- $this->showToForm($form, $sendTo, [], false, true);
- }
- }
- $form->addDateRangePicker(
- 'date_range',
- get_lang('Date range'),
- false,
- ['id' => 'date_range']
- );
- $form->addElement('checkbox', 'all_day', null, get_lang('All day'));
- if ($this->type == 'course') {
- $repeat = $form->addElement(
- 'checkbox',
- 'repeat',
- null,
- get_lang('Repeat event'),
- ['onclick' => 'return plus_repeated_event();']
- );
- $form->addElement(
- 'html',
- '<div id="options2" style="display:none">'
- );
- $form->addElement(
- 'select',
- 'repeat_type',
- get_lang('Repeat type'),
- self::getRepeatTypes()
- );
- $form->addElement(
- 'date_picker',
- 'repeat_end_day',
- get_lang('Repeat end date'),
- ['id' => 'repeat_end_date_form']
- );
- if ($isSubEventEdition || $isParentFromSerie) {
- $repeatInfo = $params['repeat_info'];
- if ($isSubEventEdition) {
- $parentEvent = $params['parent_info'];
- $repeatInfo = $parentEvent['repeat_info'];
- }
- $params['repeat'] = 1;
- $params['repeat_type'] = $repeatInfo['cal_type'];
- $params['repeat_end_day'] = substr(
- api_get_local_time($repeatInfo['cal_end']),
- 0,
- 10
- );
- $form->freeze(['repeat_type', 'repeat_end_day']);
- $repeat->_attributes['disabled'] = 'disabled';
- }
- $form->addElement('html', '</div>');
- }
- if (!empty($id)) {
- if (empty($params['end_date'])) {
- $params['date_range'] = $params['end_date'];
- }
- $params['date_range'] =
- substr(api_get_local_time($params['start_date']), 0, 16).' / '.
- substr(api_get_local_time($params['end_date']), 0, 16);
- }
- $toolbar = 'Agenda';
- if (!api_is_allowed_to_edit(null, true)) {
- $toolbar = 'AgendaStudent';
- }
- $form->addElement(
- 'html_editor',
- 'content',
- get_lang('Description'),
- null,
- [
- 'ToolbarSet' => $toolbar,
- 'Width' => '100%',
- 'Height' => '200',
- ]
- );
- if ($this->type == 'course') {
- $form->addElement('textarea', 'comment', get_lang('Comment'));
- $form->addLabel(
- get_lang('Files attachments'),
- '<div id="filepaths" class="file-upload-event">
-
- <div id="filepath_1">
- <input type="file" name="attach_1"/>
-
- <label>'.get_lang('Description').'</label>
- <input class="form-control" type="text" name="legend[]" />
- </div>
-
- </div>'
- );
- $form->addLabel(
- '',
- '<span id="link-more-attach">
- <a href="javascript://" onclick="return add_image_form()">'.
- get_lang('Add one more file').'</a>
- </span> ('.sprintf(
- get_lang('Maximun file size: %s'),
- format_file_size(
- api_get_setting('message_max_upload_filesize')
- )
- ).')'
- );
- if (isset($params['attachment']) && !empty($params['attachment'])) {
- $attachmentList = $params['attachment'];
- foreach ($attachmentList as $attachment) {
- $params['file_comment'] = $attachment['comment'];
- if (!empty($attachment['path'])) {
- $form->addElement(
- 'checkbox',
- 'delete_attachment['.$attachment['id'].']',
- null,
- get_lang(
- 'DeleteAttachment'
- ).': '.$attachment['filename']
- );
- }
- }
- }
- $form->addElement(
- 'textarea',
- 'file_comment',
- get_lang('File comment')
- );
- }
- if (empty($id)) {
- $form->addElement(
- 'checkbox',
- 'add_announcement',
- null,
- get_lang('Add an announcement').' ('.get_lang('Send mail').')'
- );
- }
- if ($id) {
- $form->addButtonUpdate(get_lang('Edit event'));
- } else {
- $form->addButtonSave(get_lang('Add event'));
- }
- $form->setDefaults($params);
- $form->addRule(
- 'date_range',
- get_lang('Required field'),
- 'required'
- );
- $form->addRule('title', get_lang('Required field'), 'required');
- return $form;
- }
- /**
- * @param FormValidator $form
- * @param array $sendTo array('everyone' => false, 'users' => [1, 2], 'groups' => [3, 4])
- * @param array $attributes
- * @param bool $addOnlyItemsInSendTo
- * @param bool $required
- *
- * @return bool
- */
- public function showToForm(
- $form,
- $sendTo = [],
- $attributes = [],
- $addOnlyItemsInSendTo = false,
- $required = false
- ) {
- if ($this->type != 'course') {
- return false;
- }
- $order = 'lastname';
- if (api_is_western_name_order()) {
- $order = 'firstname';
- }
- $userList = CourseManager::get_user_list_from_course_code(
- api_get_course_id(),
- $this->sessionId,
- null,
- $order
- );
- $groupList = CourseManager::get_group_list_of_course(
- api_get_course_id(),
- $this->sessionId
- );
- $this->setSendToSelect(
- $form,
- $groupList,
- $userList,
- $sendTo,
- $attributes,
- $addOnlyItemsInSendTo,
- $required
- );
- return true;
- }
- /**
- * @param int $id
- * @param int $visibility 0= invisible, 1 visible
- * @param array $courseInfo
- * @param int $userId
- */
- public static function changeVisibility(
- $id,
- $visibility,
- $courseInfo,
- $userId = null
- ) {
- $id = intval($id);
- if (empty($userId)) {
- $userId = api_get_user_id();
- } else {
- $userId = intval($userId);
- }
- if ($visibility == 0) {
- api_item_property_update(
- $courseInfo,
- TOOL_CALENDAR_EVENT,
- $id,
- 'invisible',
- $userId
- );
- } else {
- api_item_property_update(
- $courseInfo,
- TOOL_CALENDAR_EVENT,
- $id,
- 'visible',
- $userId
- );
- }
- }
- /**
- * Get repeat types.
- *
- * @return array
- */
- public static function getRepeatTypes()
- {
- return [
- 'daily' => get_lang('Daily'),
- 'weekly' => get_lang('Weekly'),
- 'monthlyByDate' => get_lang('Monthly, by date'),
- //monthlyByDay"> get_lang('Monthly, by day');
- //monthlyByDayR' => get_lang('Monthly, by dayR'),
- 'yearly' => get_lang('Yearly'),
- ];
- }
- /**
- * Show a list with all the attachments according to the post's id.
- *
- * @param int $eventId
- * @param array $courseInfo
- *
- * @return array with the post info
- */
- public function getAttachmentList($eventId, $courseInfo)
- {
- $tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
- $courseId = (int) $courseInfo['real_id'];
- $eventId = (int) $eventId;
- $sql = "SELECT id, path, filename, comment
- FROM $tableAttachment
- WHERE
- c_id = $courseId AND
- agenda_id = $eventId";
- $result = Database::query($sql);
- $list = [];
- if (Database::num_rows($result) != 0) {
- $list = Database::store_result($result, 'ASSOC');
- }
- return $list;
- }
- /**
- * Show a list with all the attachments according to the post's id.
- *
- * @param int $attachmentId
- * @param int $eventId
- * @param array $courseInfo
- *
- * @return array with the post info
- */
- public function getAttachment($attachmentId, $eventId, $courseInfo)
- {
- if (empty($courseInfo) || empty($attachmentId) || empty($eventId)) {
- return [];
- }
- $tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
- $courseId = (int) $courseInfo['real_id'];
- $eventId = (int) $eventId;
- $attachmentId = (int) $attachmentId;
- $row = [];
- $sql = "SELECT id, path, filename, comment
- FROM $tableAttachment
- WHERE
- c_id = $courseId AND
- agenda_id = $eventId AND
- id = $attachmentId
- ";
- $result = Database::query($sql);
- if (Database::num_rows($result) != 0) {
- $row = Database::fetch_array($result, 'ASSOC');
- }
- return $row;
- }
- /**
- * Add an attachment file into agenda.
- *
- * @param int $eventId
- * @param array $fileUserUpload ($_FILES['user_upload'])
- * @param string $comment about file
- * @param array $courseInfo
- *
- * @return string
- */
- public function addAttachment(
- $eventId,
- $fileUserUpload,
- $comment,
- $courseInfo
- ) {
- $agenda_table_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
- $eventId = (int) $eventId;
- // Storing the attachments
- $upload_ok = false;
- if (!empty($fileUserUpload['name'])) {
- $upload_ok = process_uploaded_file($fileUserUpload);
- }
- if (!empty($upload_ok)) {
- $courseDir = $courseInfo['directory'].'/upload/calendar';
- $sys_course_path = api_get_path(SYS_COURSE_PATH);
- $uploadDir = $sys_course_path.$courseDir;
- // Try to add an extension to the file if it hasn't one
- $new_file_name = add_ext_on_mime(
- stripslashes($fileUserUpload['name']),
- $fileUserUpload['type']
- );
- // user's file name
- $file_name = $fileUserUpload['name'];
- if (!filter_extension($new_file_name)) {
- return Display::return_message(
- get_lang('File upload failed: this file extension or file type is prohibited'),
- 'error'
- );
- } else {
- $new_file_name = uniqid('');
- $new_path = $uploadDir.'/'.$new_file_name;
- $result = @move_uploaded_file(
- $fileUserUpload['tmp_name'],
- $new_path
- );
- $courseId = api_get_course_int_id();
- $size = intval($fileUserUpload['size']);
- // Storing the attachments if any
- if ($result) {
- $params = [
- 'c_id' => $courseId,
- 'filename' => $file_name,
- 'comment' => $comment,
- 'path' => $new_file_name,
- 'agenda_id' => $eventId,
- 'size' => $size,
- ];
- $id = Database::insert($agenda_table_attachment, $params);
- if ($id) {
- $sql = "UPDATE $agenda_table_attachment
- SET id = iid WHERE iid = $id";
- Database::query($sql);
- api_item_property_update(
- $courseInfo,
- 'calendar_event_attachment',
- $id,
- 'AgendaAttachmentAdded',
- api_get_user_id()
- );
- }
- }
- }
- }
- }
- /**
- * @param int $attachmentId
- * @param int $eventId
- * @param array $fileUserUpload
- * @param string $comment
- * @param array $courseInfo
- */
- public function updateAttachment(
- $attachmentId,
- $eventId,
- $fileUserUpload,
- $comment,
- $courseInfo
- ) {
- $attachment = $this->getAttachment(
- $attachmentId,
- $eventId,
- $courseInfo
- );
- if (!empty($attachment)) {
- $this->deleteAttachmentFile($attachmentId, $courseInfo);
- }
- $this->addAttachment($eventId, $fileUserUpload, $comment, $courseInfo);
- }
- /**
- * This function delete a attachment file by id.
- *
- * @param int $attachmentId
- * @param array $courseInfo
- *
- * @return string
- */
- public function deleteAttachmentFile($attachmentId, $courseInfo)
- {
- $table = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
- $attachmentId = (int) $attachmentId;
- $courseId = $courseInfo['real_id'];
- if (empty($courseId) || empty($attachmentId)) {
- return false;
- }
- $sql = "DELETE FROM $table
- WHERE c_id = $courseId AND id = ".$attachmentId;
- $result = Database::query($sql);
- // update item_property
- api_item_property_update(
- $courseInfo,
- 'calendar_event_attachment',
- $attachmentId,
- 'AgendaAttachmentDeleted',
- api_get_user_id()
- );
- if (!empty($result)) {
- return Display::return_message(
- get_lang("The attached file has been deleted"),
- 'confirmation'
- );
- }
- }
- /**
- * @param int $eventId
- *
- * @return array
- */
- public function getAllRepeatEvents($eventId)
- {
- $events = [];
- $eventId = (int) $eventId;
- switch ($this->type) {
- case 'personal':
- break;
- case 'course':
- if (!empty($this->course['real_id'])) {
- $sql = "SELECT * FROM ".$this->tbl_course_agenda."
- WHERE
- c_id = ".$this->course['real_id']." AND
- parent_event_id = ".$eventId;
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- $events[] = $row;
- }
- }
- }
- break;
- }
- return $events;
- }
- /**
- * @param int $eventId
- * @param int $courseId
- *
- * @return bool
- */
- public function hasChildren($eventId, $courseId)
- {
- $eventId = (int) $eventId;
- $courseId = (int) $courseId;
- $sql = "SELECT count(DISTINCT(id)) as count
- FROM ".$this->tbl_course_agenda."
- WHERE
- c_id = $courseId AND
- parent_event_id = $eventId";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $row = Database::fetch_array($result, 'ASSOC');
- return $row['count'] > 0;
- }
- return false;
- }
- /**
- * @param int $filter
- * @param string $view
- *
- * @return string
- */
- public function displayActions($view, $filter = 0)
- {
- $groupInfo = GroupManager::get_group_properties(api_get_group_id());
- $groupIid = isset($groupInfo['iid']) ? $groupInfo['iid'] : 0;
- $codePath = api_get_path(WEB_CODE_PATH);
- $currentUserId = api_get_user_id();
- $cidReq = api_get_cidreq();
- $actionsLeft = '';
- $actionsLeft .= Display::url(
- Display::return_icon('calendar.png', get_lang('Calendar'), [], ICON_SIZE_MEDIUM),
- $codePath."calendar/agenda_js.php?type={$this->type}&$cidReq"
- );
- $actionsLeft .= Display::url(
- Display::return_icon('week.png', get_lang('Agenda list'), [], ICON_SIZE_MEDIUM),
- $codePath."calendar/agenda_list.php?type={$this->type}&$cidReq"
- );
- $form = '';
- if (api_is_allowed_to_edit(false, true) ||
- (api_get_course_setting('allow_user_edit_agenda') == '1' && !api_is_anonymous()) &&
- api_is_allowed_to_session_edit(false, true)
- || (
- GroupManager::user_has_access($currentUserId, $groupIid, GroupManager::GROUP_TOOL_CALENDAR)
- && GroupManager::is_tutor_of_group($currentUserId, $groupInfo)
- )
- ) {
- $actionsLeft .= Display::url(
- Display::return_icon('new_event.png', get_lang('Add event'), [], ICON_SIZE_MEDIUM),
- $codePath."calendar/agenda.php?action=add&type={$this->type}&$cidReq"
- );
- $actionsLeft .= Display::url(
- Display::return_icon('import_calendar.png', get_lang('Outlook import'), [], ICON_SIZE_MEDIUM),
- $codePath."calendar/agenda.php?action=importical&type={$this->type}&$cidReq"
- );
- if ($this->type === 'course') {
- if (!isset($_GET['action'])) {
- $form = new FormValidator(
- 'form-search',
- 'post',
- '',
- '',
- [],
- FormValidator::LAYOUT_INLINE
- );
- $attributes = [
- 'multiple' => false,
- 'id' => 'select_form_id_search',
- ];
- $selectedValues = $this->parseAgendaFilter($filter);
- $this->showToForm($form, $selectedValues, $attributes);
- $form = $form->returnForm();
- }
- }
- }
- if ($this->type == 'personal' && !api_is_anonymous()) {
- $actionsLeft .= Display::url(
- Display::return_icon('1day.png', get_lang('Sessions plan calendar'), [], ICON_SIZE_MEDIUM),
- $codePath."calendar/planification.php"
- );
- }
- if (api_is_platform_admin() ||
- api_is_teacher() ||
- api_is_student_boss() ||
- api_is_drh() ||
- api_is_session_admin() ||
- api_is_coach()
- ) {
- if ($this->type == 'personal') {
- $form = null;
- if (!isset($_GET['action'])) {
- $form = new FormValidator(
- 'form-search',
- 'get',
- api_get_self().'?type=personal&',
- '',
- [],
- FormValidator::LAYOUT_INLINE
- );
- $sessions = [];
- if (api_is_drh()) {
- $sessionList = SessionManager::get_sessions_followed_by_drh($currentUserId);
- if (!empty($sessionList)) {
- foreach ($sessionList as $sessionItem) {
- $sessions[$sessionItem['id']] = strip_tags($sessionItem['name']);
- }
- }
- } else {
- $sessions = SessionManager::get_sessions_by_user($currentUserId);
- $sessions = array_column($sessions, 'session_name', 'session_id');
- }
- $form->addHidden('type', 'personal');
- $sessions = ['0' => get_lang('Please select an option')] + $sessions;
- $form->addSelect(
- 'session_id',
- get_lang('Session'),
- $sessions,
- ['id' => 'session_id', 'onchange' => 'submit();']
- );
- $form->addButtonReset(get_lang('Reset'));
- $form = $form->returnForm();
- }
- }
- }
- $actionsRight = '';
- if ($view == 'calendar') {
- $actionsRight .= $form;
- }
- $toolbar = Display::toolbarAction(
- 'toolbar-agenda',
- [$actionsLeft, $actionsRight]
- );
- return $toolbar;
- }
- /**
- * @return FormValidator
- */
- public function getImportCalendarForm()
- {
- $form = new FormValidator(
- 'frm_import_ical',
- 'post',
- api_get_self().'?action=importical&type='.$this->type,
- ['enctype' => 'multipart/form-data']
- );
- $form->addHeader(get_lang('Outlook import'));
- $form->addElement('file', 'ical_import', get_lang('Outlook import'));
- $form->addRule(
- 'ical_import',
- get_lang('Required field'),
- 'required'
- );
- $form->addButtonImport(get_lang('Import'), 'ical_submit');
- return $form;
- }
- /**
- * @param array $courseInfo
- * @param $file
- *
- * @return false|string
- */
- public function importEventFile($courseInfo, $file)
- {
- $charset = api_get_system_encoding();
- $filepath = api_get_path(SYS_ARCHIVE_PATH).$file['name'];
- $messages = [];
- if (!@move_uploaded_file($file['tmp_name'], $filepath)) {
- error_log(
- 'Problem moving uploaded file: '.$file['error'].' in '.__FILE__.' line '.__LINE__
- );
- return false;
- }
- $data = file_get_contents($filepath);
- $trans = [
- 'DAILY' => 'daily',
- 'WEEKLY' => 'weekly',
- 'MONTHLY' => 'monthlyByDate',
- 'YEARLY' => 'yearly',
- ];
- $sentTo = ['everyone' => true];
- $calendar = Sabre\VObject\Reader::read($data);
- $currentTimeZone = api_get_timezone();
- if (!empty($calendar->VEVENT)) {
- foreach ($calendar->VEVENT as $event) {
- $start = $event->DTSTART->getDateTime();
- $end = $event->DTEND->getDateTime();
- //Sabre\VObject\DateTimeParser::parseDateTime(string $dt, \Sabre\VObject\DateTimeZone $tz)
- $startDateTime = api_get_local_time(
- $start->format('Y-m-d H:i:s'),
- $currentTimeZone,
- $start->format('e')
- );
- $endDateTime = api_get_local_time(
- $end->format('Y-m-d H:i'),
- $currentTimeZone,
- $end->format('e')
- );
- $title = api_convert_encoding(
- (string) $event->summary,
- $charset,
- 'UTF-8'
- );
- $description = api_convert_encoding(
- (string) $event->description,
- $charset,
- 'UTF-8'
- );
- $id = $this->addEvent(
- $startDateTime,
- $endDateTime,
- 'false',
- $title,
- $description,
- $sentTo
- );
- $messages[] = " $title - ".$startDateTime." - ".$endDateTime;
- //$attendee = (string)$event->attendee;
- /** @var Sabre\VObject\Property\ICalendar\Recur $repeat */
- $repeat = $event->RRULE;
- if ($id && !empty($repeat)) {
- $repeat = $repeat->getParts();
- $freq = $trans[$repeat['FREQ']];
- if (isset($repeat['UNTIL']) && !empty($repeat['UNTIL'])) {
- // Check if datetime or just date (strlen == 8)
- if (strlen($repeat['UNTIL']) == 8) {
- // Fix the datetime format to avoid exception in the next step
- $repeat['UNTIL'] .= 'T000000';
- }
- $until = Sabre\VObject\DateTimeParser::parseDateTime(
- $repeat['UNTIL'],
- new DateTimeZone($currentTimeZone)
- );
- $until = $until->format('Y-m-d H:i:s');
- $this->addRepeatedItem(
- $id,
- $freq,
- $until,
- $sentTo
- );
- }
- if (!empty($repeat['COUNT'])) {
- /*$count = $repeat['COUNT'];
- $interval = $repeat['INTERVAL'];
- $endDate = null;
- switch($freq) {
- case 'daily':
- $start = api_strtotime($startDateTime);
- $date = new DateTime($startDateTime);
- $days = $count * $interval;
- var_dump($days);
- $date->add(new DateInterval("P".$days."D"));
- $endDate = $date->format('Y-m-d H:i');
- //$endDate = $count *
- for ($i = 0; $i < $count; $i++) {
- $days = 86400 * 7
- }
- }
- }*/
- //$res = agenda_add_repeat_item($courseInfo, $id, $freq, $count, $attendee);
- /*$this->addRepeatedItem(
- $id,
- $freq,
- $endDate,
- $sentTo
- );*/
- }
- }
- }
- }
- if (!empty($messages)) {
- $messages = implode('<br /> ', $messages);
- } else {
- $messages = get_lang('There are no events');
- }
- return $messages;
- }
- /**
- * Parse filter turns USER:12 to ['users' => [12])] or G:1 ['groups' => [1]].
- *
- * @param int $filter
- *
- * @return array
- */
- public function parseAgendaFilter($filter)
- {
- $everyone = false;
- $groupId = null;
- $userId = null;
- if ($filter == 'everyone') {
- $everyone = true;
- } else {
- if (substr($filter, 0, 1) == 'G') {
- $groupId = str_replace('GROUP:', '', $filter);
- } else {
- $userId = str_replace('USER:', '', $filter);
- }
- }
- if (empty($userId) && empty($groupId)) {
- $everyone = true;
- }
- return [
- 'everyone' => $everyone,
- 'users' => [$userId],
- 'groups' => [$groupId],
- ];
- }
- /**
- * This function retrieves all the agenda items of all the courses the user is subscribed to.
- */
- public static function get_myagendaitems(
- $user_id,
- $courses_dbs,
- $month,
- $year
- ) {
- $user_id = intval($user_id);
- $items = [];
- $my_list = [];
- // get agenda-items for every course
- foreach ($courses_dbs as $key => $array_course_info) {
- //databases of the courses
- $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
- $TABLE_ITEMPROPERTY = Database::get_course_table(
- TABLE_ITEM_PROPERTY
- );
- $group_memberships = GroupManager::get_group_ids(
- $array_course_info['real_id'],
- $user_id
- );
- $course_user_status = CourseManager::getUserInCourseStatus(
- $user_id,
- $array_course_info['real_id']
- );
- // if the user is administrator of that course we show all the agenda items
- if ($course_user_status == '1') {
- //echo "course admin";
- $sqlquery = "SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
- FROM ".$TABLEAGENDA." agenda,
- ".$TABLE_ITEMPROPERTY." ip
- WHERE agenda.id = ip.ref
- AND MONTH(agenda.start_date)='".$month."'
- AND YEAR(agenda.start_date)='".$year."'
- AND ip.tool='".TOOL_CALENDAR_EVENT."'
- AND ip.visibility='1'
- GROUP BY agenda.id
- ORDER BY start_date ";
- } else {
- // if the user is not an administrator of that course
- if (is_array($group_memberships) && count(
- $group_memberships
- ) > 0
- ) {
- $sqlquery = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
- FROM ".$TABLEAGENDA." agenda,
- ".$TABLE_ITEMPROPERTY." ip
- WHERE agenda.id = ip.ref
- AND MONTH(agenda.start_date)='".$month."'
- AND YEAR(agenda.start_date)='".$year."'
- AND ip.tool='".TOOL_CALENDAR_EVENT."'
- AND ( ip.to_user_id='".$user_id."' OR (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(
- ", ",
- $group_memberships
- ).")) )
- AND ip.visibility='1'
- ORDER BY start_date ";
- } else {
- $sqlquery = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
- FROM ".$TABLEAGENDA." agenda,
- ".$TABLE_ITEMPROPERTY." ip
- WHERE agenda.id = ip.ref
- AND MONTH(agenda.start_date)='".$month."'
- AND YEAR(agenda.start_date)='".$year."'
- AND ip.tool='".TOOL_CALENDAR_EVENT."'
- AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
- AND ip.visibility='1'
- ORDER BY start_date ";
- }
- }
- $result = Database::query($sqlquery);
- while ($item = Database::fetch_array($result, 'ASSOC')) {
- $agendaday = -1;
- if (!empty($item['start_date'])) {
- $item['start_date'] = api_get_local_time(
- $item['start_date']
- );
- $item['start_date_tms'] = api_strtotime(
- $item['start_date']
- );
- $agendaday = date("j", $item['start_date_tms']);
- }
- if (!empty($item['end_date'])) {
- $item['end_date'] = api_get_local_time($item['end_date']);
- }
- $url = api_get_path(
- WEB_CODE_PATH
- )."calendar/agenda.php?cidReq=".urlencode(
- $array_course_info["code"]
- )."&day=$agendaday&month=$month&year=$year#$agendaday";
- $item['url'] = $url;
- $item['course_name'] = $array_course_info['title'];
- $item['calendar_type'] = 'course';
- $item['course_id'] = $array_course_info['course_id'];
- $my_list[$agendaday][] = $item;
- }
- }
- // sorting by hour for every day
- $agendaitems = [];
- foreach ($items as $agendaday => $tmpitems) {
- if (!isset($agendaitems[$agendaday])) {
- $agendaitems[$agendaday] = '';
- }
- sort($tmpitems);
- foreach ($tmpitems as $val) {
- $agendaitems[$agendaday] .= $val;
- }
- }
- return $my_list;
- }
- /**
- * This function retrieves one personal agenda item returns it.
- *
- * @param array The array containing existing events. We add to this array.
- * @param int Day
- * @param int Month
- * @param int Year (4 digits)
- * @param int Week number
- * @param string Type of view (month_view, week_view, day_view)
- *
- * @return array The results of the database query, or null if not found
- */
- public static function get_global_agenda_items(
- $agendaitems,
- $day = "",
- $month = "",
- $year = "",
- $week = "",
- $type
- ) {
- $tbl_global_agenda = Database::get_main_table(
- TABLE_MAIN_SYSTEM_CALENDAR
- );
- $month = intval($month);
- $year = intval($year);
- $week = intval($week);
- $day = intval($day);
- // 1. creating the SQL statement for getting the personal agenda items in MONTH view
- $current_access_url_id = api_get_current_access_url_id();
- if ($type == "month_view" or $type == "") {
- // We are in month view
- $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";
- }
- // 2. creating the SQL statement for getting the personal agenda items in WEEK view
- if ($type == "week_view") { // we are in week view
- $start_end_day_of_week = self::calculate_start_end_of_week(
- $week,
- $year
- );
- $start_day = $start_end_day_of_week['start']['day'];
- $start_month = $start_end_day_of_week['start']['month'];
- $start_year = $start_end_day_of_week['start']['year'];
- $end_day = $start_end_day_of_week['end']['day'];
- $end_month = $start_end_day_of_week['end']['month'];
- $end_year = $start_end_day_of_week['end']['year'];
- // in sql statements you have to use year-month-day for date calculations
- $start_filter = $start_year."-".$start_month."-".$start_day." 00:00:00";
- $start_filter = api_get_utc_datetime($start_filter);
- $end_filter = $end_year."-".$end_month."-".$end_day." 23:59:59";
- $end_filter = api_get_utc_datetime($end_filter);
- $sql = " SELECT * FROM ".$tbl_global_agenda." WHERE start_date>='".$start_filter."' AND start_date<='".$end_filter."' AND access_url_id = $current_access_url_id ";
- }
- // 3. creating the SQL statement for getting the personal agenda items in DAY view
- if ($type == "day_view") { // we are in day view
- // we could use mysql date() function but this is only available from 4.1 and higher
- $start_filter = $year."-".$month."-".$day." 00:00:00";
- $start_filter = api_get_utc_datetime($start_filter);
- $end_filter = $year."-".$month."-".$day." 23:59:59";
- $end_filter = api_get_utc_datetime($end_filter);
- $sql = " SELECT * FROM ".$tbl_global_agenda." WHERE start_date>='".$start_filter."' AND start_date<='".$end_filter."' AND access_url_id = $current_access_url_id";
- }
- $result = Database::query($sql);
- while ($item = Database::fetch_array($result)) {
- if (!empty($item['start_date'])) {
- $item['start_date'] = api_get_local_time($item['start_date']);
- $item['start_date_tms'] = api_strtotime($item['start_date']);
- }
- if (!empty($item['end_date'])) {
- $item['end_date'] = api_get_local_time($item['end_date']);
- }
- // we break the date field in the database into a date and a time part
- $agenda_db_date = explode(" ", $item['start_date']);
- $date = $agenda_db_date[0];
- $time = $agenda_db_date[1];
- // we divide the date part into a day, a month and a year
- $agendadate = explode("-", $date);
- $year = intval($agendadate[0]);
- $month = intval($agendadate[1]);
- $day = intval($agendadate[2]);
- // we divide the time part into hour, minutes, seconds
- $agendatime = explode(":", $time);
- $hour = $agendatime[0];
- $minute = $agendatime[1];
- $second = $agendatime[2];
- if ($type == 'month_view') {
- $item['calendar_type'] = 'global';
- $agendaitems[$day][] = $item;
- continue;
- }
- $start_time = api_format_date(
- $item['start_date'],
- TIME_NO_SEC_FORMAT
- );
- $end_time = '';
- if (!empty($item['end_date'])) {
- $end_time = ' - '.api_format_date(
- $item['end_date'],
- DATE_TIME_FORMAT_LONG
- );
- }
- // if the student has specified a course we a add a link to that course
- if ($item['course'] != "") {
- $url = api_get_path(
- WEB_CODE_PATH
- )."admin/agenda.php?cidReq=".urlencode(
- $item['course']
- )."&day=$day&month=$month&year=$year#$day"; // RH //Patrick Cool: to highlight the relevant agenda item
- $course_link = "<a href=\"$url\" title=\"".$item['course']."\">".$item['course']."</a>";
- } else {
- $course_link = "";
- }
- // Creating the array that will be returned. If we have week or month view we have an array with the date as the key
- // if we have a day_view we use a half hour as index => key 33 = 16h30
- if ($type !== "day_view") {
- // This is the array construction for the WEEK or MONTH view
- //Display the Agenda global in the tab agenda (administrator)
- $agendaitems[$day] .= "<i>$start_time $end_time</i> - ";
- $agendaitems[$day] .= "<b>".get_lang('Platform event')."</b>";
- $agendaitems[$day] .= "<div>".$item['title']."</div><br>";
- } else {
- // this is the array construction for the DAY view
- $halfhour = 2 * $agendatime['0'];
- if ($agendatime['1'] >= '30') {
- $halfhour = $halfhour + 1;
- }
- if (!is_array($agendaitems[$halfhour])) {
- $content = $agendaitems[$halfhour];
- }
- $agendaitems[$halfhour] = $content."<div><i>$hour:$minute</i> <b>".get_lang(
- 'Platform event'
- ).": </b>".$item['title']."</div>";
- }
- }
- return $agendaitems;
- }
- /**
- * This function retrieves all the personal agenda items and add them to the agenda items found by the other
- * functions.
- */
- public static function get_personal_agenda_items(
- $user_id,
- $agendaitems,
- $day = "",
- $month = "",
- $year = "",
- $week = "",
- $type
- ) {
- $tbl_personal_agenda = Database::get_main_table(TABLE_PERSONAL_AGENDA);
- $user_id = intval($user_id);
- // 1. creating the SQL statement for getting the personal agenda items in MONTH view
- if ($type == "month_view" or $type == "") {
- // we are in month view
- $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' and MONTH(date)='".$month."' AND YEAR(date) = '".$year."' ORDER BY date ASC";
- }
- // 2. creating the SQL statement for getting the personal agenda items in WEEK view
- // we are in week view
- if ($type == "week_view") {
- $start_end_day_of_week = self::calculate_start_end_of_week(
- $week,
- $year
- );
- $start_day = $start_end_day_of_week['start']['day'];
- $start_month = $start_end_day_of_week['start']['month'];
- $start_year = $start_end_day_of_week['start']['year'];
- $end_day = $start_end_day_of_week['end']['day'];
- $end_month = $start_end_day_of_week['end']['month'];
- $end_year = $start_end_day_of_week['end']['year'];
- // in sql statements you have to use year-month-day for date calculations
- $start_filter = $start_year."-".$start_month."-".$start_day." 00:00:00";
- $start_filter = api_get_utc_datetime($start_filter);
- $end_filter = $end_year."-".$end_month."-".$end_day." 23:59:59";
- $end_filter = api_get_utc_datetime($end_filter);
- $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' AND date>='".$start_filter."' AND date<='".$end_filter."'";
- }
- // 3. creating the SQL statement for getting the personal agenda items in DAY view
- if ($type == "day_view") {
- // we are in day view
- // we could use mysql date() function but this is only available from 4.1 and higher
- $start_filter = $year."-".$month."-".$day." 00:00:00";
- $start_filter = api_get_utc_datetime($start_filter);
- $end_filter = $year."-".$month."-".$day." 23:59:59";
- $end_filter = api_get_utc_datetime($end_filter);
- $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' AND date>='".$start_filter."' AND date<='".$end_filter."'";
- }
- $result = Database::query($sql);
- while ($item = Database::fetch_array($result, 'ASSOC')) {
- $time_minute = api_convert_and_format_date(
- $item['date'],
- TIME_NO_SEC_FORMAT
- );
- $item['date'] = api_get_local_time($item['date']);
- $item['start_date_tms'] = api_strtotime($item['date']);
- $item['content'] = $item['text'];
- // we break the date field in the database into a date and a time part
- $agenda_db_date = explode(" ", $item['date']);
- $date = $agenda_db_date[0];
- $time = $agenda_db_date[1];
- // we divide the date part into a day, a month and a year
- $agendadate = explode("-", $item['date']);
- $year = intval($agendadate[0]);
- $month = intval($agendadate[1]);
- $day = intval($agendadate[2]);
- // we divide the time part into hour, minutes, seconds
- $agendatime = explode(":", $time);
- $hour = $agendatime[0];
- $minute = $agendatime[1];
- $second = $agendatime[2];
- if ($type == 'month_view') {
- $item['calendar_type'] = 'personal';
- $item['start_date'] = $item['date'];
- $agendaitems[$day][] = $item;
- continue;
- }
- // if the student has specified a course we a add a link to that course
- if ($item['course'] != "") {
- $url = api_get_path(
- WEB_CODE_PATH
- )."calendar/agenda.php?cidReq=".urlencode(
- $item['course']
- )."&day=$day&month=$month&year=$year#$day"; // RH //Patrick Cool: to highlight the relevant agenda item
- $course_link = "<a href=\"$url\" title=\"".$item['course']."\">".$item['course']."</a>";
- } else {
- $course_link = "";
- }
- // Creating the array that will be returned. If we have week or month view we have an array with the date as the key
- // if we have a day_view we use a half hour as index => key 33 = 16h30
- if ($type !== "day_view") {
- // This is the array construction for the WEEK or MONTH view
- //Display events in agenda
- $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 />";
- } else {
- // this is the array construction for the DAY view
- $halfhour = 2 * $agendatime['0'];
- if ($agendatime['1'] >= '30') {
- $halfhour = $halfhour + 1;
- }
- //Display events by list
- $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>";
- }
- }
- return $agendaitems;
- }
- /**
- * Show the monthcalender of the given month.
- *
- * @param array Agendaitems
- * @param int Month number
- * @param int Year number
- * @param array Array of strings containing long week day names (deprecated, you can send an empty array
- * instead)
- * @param string The month name
- */
- public static function display_mymonthcalendar(
- $user_id,
- $agendaitems,
- $month,
- $year,
- $weekdaynames = [],
- $monthName,
- $show_content = true
- ) {
- global $DaysShort, $course_path;
- //Handle leap year
- $numberofdays = [
- 0,
- 31,
- 28,
- 31,
- 30,
- 31,
- 30,
- 31,
- 31,
- 30,
- 31,
- 30,
- 31,
- ];
- if (($year % 400 == 0) or ($year % 4 == 0 and $year % 100 != 0)) {
- $numberofdays[2] = 29;
- }
- //Get the first day of the month
- $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
- //Start the week on monday
- $startdayofweek = $dayone['wday'] != 0 ? ($dayone['wday'] - 1) : 6;
- $g_cc = (isset($_GET['courseCode']) ? $_GET['courseCode'] : '');
- $next_month = ($month == 1 ? 12 : $month - 1);
- $prev_month = ($month == 12 ? 1 : $month + 1);
- $next_year = ($month == 1 ? $year - 1 : $year);
- $prev_year = ($month == 12 ? $year + 1 : $year);
- if ($show_content) {
- $back_url = Display::url(
- get_lang('Previous'),
- api_get_self()."?coursePath=".urlencode(
- $course_path
- )."&courseCode=".Security::remove_XSS(
- $g_cc
- )."&action=view&view=month&month=".$next_month."&year=".$next_year
- );
- $next_url = Display::url(
- get_lang('Next'),
- api_get_self()."?coursePath=".urlencode(
- $course_path
- )."&courseCode=".Security::remove_XSS(
- $g_cc
- )."&action=view&view=month&month=".$prev_month."&year=".$prev_year
- );
- } else {
- $back_url = Display::url(
- get_lang('Previous'),
- '',
- [
- 'onclick' => "load_calendar('".$user_id."','".$next_month."', '".$next_year."'); ",
- 'class' => 'btn ui-button ui-widget ui-state-default',
- ]
- );
- $next_url = Display::url(
- get_lang('Next'),
- '',
- [
- 'onclick' => "load_calendar('".$user_id."','".$prev_month."', '".$prev_year."'); ",
- 'class' => 'pull-right btn ui-button ui-widget ui-state-default',
- ]
- );
- }
- $html = '';
- $html .= '<div class="actions">';
- $html .= '<div class="row">';
- $html .= '<div class="col-md-4">'.$back_url.'</div>';
- $html .= '<div class="col-md-4"><p class="agenda-title text-center">'.$monthName." ".$year.'</p></div>';
- $html .= '<div class="col-md-4">'.$next_url.'</div>';
- $html .= '</div>';
- $html .= '</div>';
- $html .= '<table id="agenda_list2" class="table table-bordered">';
- $html .= '<tr>';
- for ($ii = 1; $ii < 8; $ii++) {
- $html .= '<td class="weekdays">'.$DaysShort[$ii % 7].'</td>';
- }
- $html .= '</tr>';
- $curday = -1;
- $today = getdate();
- while ($curday <= $numberofdays[$month]) {
- $html .= "<tr>";
- for ($ii = 0; $ii < 7; $ii++) {
- if (($curday == -1) && ($ii == $startdayofweek)) {
- $curday = 1;
- }
- if (($curday > 0) && ($curday <= $numberofdays[$month])) {
- $bgcolor = $class = 'class="days_week"';
- $dayheader = Display::div(
- $curday,
- ['class' => 'agenda_day']
- );
- if (($curday == $today['mday']) && ($year == $today['year']) && ($month == $today['mon'])) {
- $class = "class=\"days_today\" style=\"width:10%;\"";
- }
- $html .= "<td ".$class.">".$dayheader;
- if (!empty($agendaitems[$curday])) {
- $items = $agendaitems[$curday];
- $items = msort($items, 'start_date_tms');
- foreach ($items as $value) {
- $value['title'] = Security::remove_XSS(
- $value['title']
- );
- $start_time = api_format_date(
- $value['start_date'],
- TIME_NO_SEC_FORMAT
- );
- $end_time = '';
- if (!empty($value['end_date'])) {
- $end_time = '- <i>'.api_format_date(
- $value['end_date'],
- DATE_TIME_FORMAT_LONG
- ).'</i>';
- }
- $complete_time = '<i>'.api_format_date(
- $value['start_date'],
- DATE_TIME_FORMAT_LONG
- ).'</i> '.$end_time;
- $time = '<i>'.$start_time.'</i>';
- switch ($value['calendar_type']) {
- case 'personal':
- $bg_color = '#D0E7F4';
- $icon = Display::return_icon(
- 'user.png',
- get_lang('Personal agenda'),
- [],
- ICON_SIZE_SMALL
- );
- break;
- case 'global':
- $bg_color = '#FFBC89';
- $icon = Display::return_icon(
- 'view_remove.png',
- get_lang('Platform event'),
- [],
- ICON_SIZE_SMALL
- );
- break;
- case 'course':
- $bg_color = '#CAFFAA';
- $icon_name = 'course.png';
- if (!empty($value['session_id'])) {
- $icon_name = 'session.png';
- }
- if ($show_content) {
- $icon = Display::url(
- Display::return_icon(
- $icon_name,
- $value['course_name'].' '.get_lang(
- 'Course'
- ),
- [],
- ICON_SIZE_SMALL
- ),
- $value['url']
- );
- } else {
- $icon = Display::return_icon(
- $icon_name,
- $value['course_name'].' '.get_lang(
- 'Course'
- ),
- [],
- ICON_SIZE_SMALL
- );
- }
- break;
- default:
- break;
- }
- $result = '<div class="rounded_div_agenda" style="background-color:'.$bg_color.';">';
- if ($show_content) {
- //Setting a personal event to green
- $icon = Display::div(
- $icon,
- ['style' => 'float:right']
- );
- $link = $value['calendar_type'].'_'.$value['id'].'_'.$value['course_id'].'_'.$value['session_id'];
- //Link to bubble
- $url = Display::url(
- cut($value['title'], 40),
- '#',
- ['id' => $link, 'class' => 'opener']
- );
- $result .= $time.' '.$icon.' '.Display::div(
- $url
- );
- //Hidden content
- $content = Display::div(
- $icon.Display::tag(
- 'h2',
- $value['course_name']
- ).'<hr />'.Display::tag(
- 'h3',
- $value['title']
- ).$complete_time.'<hr />'.Security::remove_XSS(
- $value['content']
- )
- );
- //Main div
- $result .= Display::div(
- $content,
- [
- 'id' => 'main_'.$link,
- 'class' => 'dialog',
- 'style' => 'display:none',
- ]
- );
- $result .= '</div>';
- $html .= $result;
- } else {
- $html .= $result .= $icon.'</div>';
- }
- }
- }
- $html .= "</td>";
- $curday++;
- } else {
- $html .= "<td></td>";
- }
- }
- $html .= "</tr>";
- }
- $html .= "</table>";
- echo $html;
- }
- /**
- * Get personal agenda items between two dates (=all events from all registered courses).
- *
- * @param int $user_id user ID of the user
- * @param string Optional start date in datetime format (if no start date is given, uses today)
- * @param string Optional end date in datetime format (if no date is given, uses one year from now)
- *
- * @return array array of events ordered by start date, in
- * [0]('datestart','dateend','title'),[1]('datestart','dateend','title','link','coursetitle') format,
- * where datestart and dateend are in yyyyMMddhhmmss format
- *
- * @deprecated use agenda events
- */
- public static function get_personal_agenda_items_between_dates(
- $user_id,
- $date_start = '',
- $date_end = ''
- ) {
- $items = [];
- if ($user_id != strval(intval($user_id))) {
- return $items;
- }
- if (empty($date_start)) {
- $date_start = date('Y-m-d H:i:s');
- }
- if (empty($date_end)) {
- $date_end = date(
- 'Y-m-d H:i:s',
- mktime(0, 0, 0, date("m"), date("d"), date("Y") + 1)
- );
- }
- $expr = '/\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2}/';
- if (!preg_match($expr, $date_start)) {
- return $items;
- }
- if (!preg_match($expr, $date_end)) {
- return $items;
- }
- // get agenda-items for every course
- $courses = api_get_user_courses($user_id, false);
- foreach ($courses as $id => $course) {
- $c = api_get_course_info_by_id($course['real_id']);
- //databases of the courses
- $t_a = Database::get_course_table(TABLE_AGENDA, $course['db']);
- $t_ip = Database::get_course_table(
- TABLE_ITEM_PROPERTY,
- $course['db']
- );
- // get the groups to which the user belong
- $group_memberships = GroupManager:: get_group_ids(
- $course['db'],
- $user_id
- );
- // if the user is administrator of that course we show all the agenda items
- if ($course['status'] == '1') {
- //echo "course admin";
- $sqlquery = "SELECT ".
- " DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
- " FROM ".$t_a." agenda, ".
- $t_ip." ip ".
- " WHERE agenda.id = ip.ref ".
- " AND agenda.start_date>='$date_start' ".
- " AND agenda.end_date<='$date_end' ".
- " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
- " AND ip.visibility='1' ".
- " GROUP BY agenda.id ".
- " ORDER BY start_date ";
- } else {
- // if the user is not an administrator of that course, then...
- if (is_array($group_memberships) && count(
- $group_memberships
- ) > 0
- ) {
- $sqlquery = "SELECT ".
- "DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
- " FROM ".$t_a." agenda, ".
- $t_ip." ip ".
- " WHERE agenda.id = ip.ref ".
- " AND agenda.start_date>='$date_start' ".
- " AND agenda.end_date<='$date_end' ".
- " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
- " AND ( ip.to_user_id='".$user_id."' OR (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(
- ", ",
- $group_memberships
- ).")) ) ".
- " AND ip.visibility='1' ".
- " ORDER BY start_date ";
- } else {
- $sqlquery = "SELECT ".
- "DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
- " FROM ".$t_a." agenda, ".
- $t_ip." ip ".
- " WHERE agenda.id = ip.ref ".
- " AND agenda.start_date>='$date_start' ".
- " AND agenda.end_date<='$date_end' ".
- " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
- " AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) ".
- " AND ip.visibility='1' ".
- " ORDER BY start_date ";
- }
- }
- $result = Database::query($sqlquery);
- while ($item = Database::fetch_array($result)) {
- $agendaday = date("j", strtotime($item['start_date']));
- $month = date("n", strtotime($item['start_date']));
- $year = date("Y", strtotime($item['start_date']));
- $URL = api_get_path(
- WEB_PATH
- )."main/calendar/agenda.php?cidReq=".urlencode(
- $course["code"]
- )."&day=$agendaday&month=$month&year=$year#$agendaday";
- list($year, $month, $day, $hour, $min, $sec) = explode(
- '[-: ]',
- $item['start_date']
- );
- $start_date = $year.$month.$day.$hour.$min;
- list($year, $month, $day, $hour, $min, $sec) = explode(
- '[-: ]',
- $item['end_date']
- );
- $end_date = $year.$month.$day.$hour.$min;
- $items[] = [
- 'datestart' => $start_date,
- 'dateend' => $end_date,
- 'title' => $item['title'],
- 'link' => $URL,
- 'coursetitle' => $c['name'],
- ];
- }
- }
- return $items;
- }
- /**
- * This function calculates the startdate of the week (monday)
- * and the enddate of the week (sunday)
- * and returns it as an array.
- */
- public static function calculate_start_end_of_week($week_number, $year)
- {
- // determine the start and end date
- // step 1: we calculate a timestamp for a day in this week
- $random_day_in_week = mktime(
- 0,
- 0,
- 0,
- 1,
- 1,
- $year
- ) + ($week_number) * (7 * 24 * 60 * 60); // we calculate a random day in this week
- // step 2: we which day this is (0=sunday, 1=monday, ...)
- $number_day_in_week = date('w', $random_day_in_week);
- // step 3: we calculate the timestamp of the monday of the week we are in
- $start_timestamp = $random_day_in_week - (($number_day_in_week - 1) * 24 * 60 * 60);
- // step 4: we calculate the timestamp of the sunday of the week we are in
- $end_timestamp = $random_day_in_week + ((7 - $number_day_in_week + 1) * 24 * 60 * 60) - 3600;
- // step 5: calculating the start_day, end_day, start_month, end_month, start_year, end_year
- $start_day = date('j', $start_timestamp);
- $start_month = date('n', $start_timestamp);
- $start_year = date('Y', $start_timestamp);
- $end_day = date('j', $end_timestamp);
- $end_month = date('n', $end_timestamp);
- $end_year = date('Y', $end_timestamp);
- $start_end_array['start']['day'] = $start_day;
- $start_end_array['start']['month'] = $start_month;
- $start_end_array['start']['year'] = $start_year;
- $start_end_array['end']['day'] = $end_day;
- $start_end_array['end']['month'] = $end_month;
- $start_end_array['end']['year'] = $end_year;
- return $start_end_array;
- }
- /**
- * @return bool
- */
- public function getIsAllowedToEdit()
- {
- return $this->isAllowedToEdit;
- }
- /**
- * @param bool $isAllowedToEdit
- */
- public function setIsAllowedToEdit($isAllowedToEdit)
- {
- $this->isAllowedToEdit = $isAllowedToEdit;
- }
- /**
- * Format needed for the Fullcalendar js lib.
- *
- * @param string $utcTime
- *
- * @return bool|string
- */
- public function formatEventDate($utcTime)
- {
- $utcTimeZone = new DateTimeZone('UTC');
- $platformTimeZone = new DateTimeZone(api_get_timezone());
- $eventDate = new DateTime($utcTime, $utcTimeZone);
- $eventDate->setTimezone($platformTimeZone);
- return $eventDate->format(DateTime::ISO8601);
- }
- }
|