agenda.inc.php 221 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @author: Julio Montoya <gugli100@gmail.com> BeezNest 2011 - Lots of fixes - UI improvements, security fixes,
  5. * @author: Patrick Cool, patrick.cool@UGent.be
  6. * @todo this code should be clean as well as the myagenda.inc.php - jmontoya
  7. * @package chamilo.calendar
  8. */
  9. /**
  10. * Code
  11. */
  12. // the variables for the days and the months
  13. // Defining the shorts for the days
  14. $DaysShort = api_get_week_days_short();
  15. // Defining the days of the week to allow translation of the days
  16. $DaysLong = api_get_week_days_long();
  17. // Defining the months of the year to allow translation of the months
  18. $MonthsLong = api_get_months_long();
  19. $htmlHeadXtra[] = to_javascript();
  20. $htmlHeadXtra[] = '<script>
  21. function setFocus(){
  22. $("#agenda_title").focus();
  23. }
  24. $(function() {
  25. setFocus();
  26. $("#selected_form_id_search").change(function() {
  27. var temp ="&user_id="+$("#selected_form_id_search").val();
  28. var position =String(window.location).indexOf("&user");
  29. var url_length = String(window.location).length;
  30. var url = String(window.location).substring(0,position)+temp;
  31. if (position > 0){
  32. window.location.replace(url);
  33. } else {
  34. url = String(window.location)+temp;
  35. window.location.replace(url);
  36. }
  37. });
  38. });
  39. </script>';
  40. /**
  41. * Retrieves all the agenda items from the table
  42. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  43. * @author Yannick Warnier <yannick.warnier@dokeos.com> - cleanup
  44. * @author Julio Montoya - Refactoring
  45. * @param integer $month: the integer value of the month we are viewing
  46. * @param integer $year: the 4-digit year indication e.g. 2005
  47. * @return array
  48. */
  49. function get_calendar_items($select_month, $select_year, $select_day = false)
  50. {
  51. $course_info = api_get_course_info();
  52. $select_month = intval($select_month);
  53. $select_year = intval($select_year);
  54. if ($select_day) {
  55. $select_day = intval($select_day);
  56. }
  57. if (!empty($select_month) && !empty($select_year)) {
  58. $show_all_current = " AND MONTH(start_date) = $select_month AND year(start_date) = $select_year";
  59. if ($select_day) {
  60. $show_all_current .= ' AND DAY(start_date) = '.$select_day;
  61. }
  62. $show_all_current_personal = " AND MONTH(date) = $select_month AND year(date) = $select_year";
  63. if ($select_day) {
  64. $show_all_current_personal .= ' AND DAY(date) = '.$select_day;
  65. }
  66. } else {
  67. $show_all_current = '';
  68. $show_all_current_personal = '';
  69. }
  70. // database variables
  71. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  72. $TABLE_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  73. $group_memberships = GroupManager::get_group_ids($course_info['real_id'], api_get_user_id());
  74. $repeats = array();
  75. /* CONSTRUCT THE SQL STATEMENT */
  76. // by default we use the id of the current user. The course administrator can see the agenda of other users by using the user / group filter
  77. $user_id = api_get_user_id();
  78. if (isset($_SESSION['user'])) {
  79. $user_id = intval($_SESSION['user']);
  80. }
  81. $group_id = api_get_group_id();
  82. $session_condition = api_get_session_condition(api_get_session_id());
  83. if (api_is_allowed_to_edit(false, true) OR (api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous())
  84. ) {
  85. // A.1. you are a course admin with a USER filter
  86. // => see only the messages of this specific user + the messages of the group (s)he is member of.
  87. if (!empty($_SESSION['user'])) {
  88. $group_memberships = GroupManager::get_group_ids($course_info['real_id'], $_SESSION['user']);
  89. $show_user = true;
  90. $new_group_memberships = array();
  91. foreach ($group_memberships as $id) {
  92. // did i have access to the same
  93. $has_access = GroupManager::user_has_access(api_get_user_id(), $id, GroupManager::GROUP_TOOL_CALENDAR);
  94. $result = GroupManager::get_group_properties($id);
  95. if ($has_access && $result['calendar_state'] != '0') {
  96. $new_group_memberships[] = $id;
  97. }
  98. }
  99. $group_memberships = $new_group_memberships;
  100. if (is_array($group_memberships) && count($group_memberships) > 0) {
  101. $sql = "SELECT
  102. agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  103. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." ip
  104. WHERE agenda.id = ip.ref ".$show_all_current."
  105. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  106. AND ( ip.to_user_id = $user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") )
  107. AND ip.visibility='1'
  108. $session_condition";
  109. } else {
  110. //AND ( ip.to_user_id=$user_id OR ip.to_group_id='0')
  111. $sql = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  112. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." ip
  113. WHERE agenda.id = ip.ref ".$show_all_current."
  114. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  115. AND ( ip.to_user_id = $user_id )
  116. AND ip.visibility='1'
  117. $session_condition";
  118. }
  119. } // A.2. you are a course admin with a GROUP filter
  120. // => see only the messages of this specific group
  121. elseif (!empty($_SESSION['group'])) {
  122. if (!empty($group_id)) {
  123. $result = GroupManager::get_group_properties($group_id);
  124. $has_access = GroupManager::user_has_access(
  125. api_get_user_id(),
  126. $group_id,
  127. GroupManager::GROUP_TOOL_CALENDAR
  128. );
  129. //echo '<pre>';print_R($result);
  130. // lastedit
  131. if (!$has_access || $result['calendar_state'] == '0') {
  132. $group_id = 0;
  133. }
  134. }
  135. $sql = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  136. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." ip
  137. WHERE agenda.id = ip.ref ".$show_all_current."
  138. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  139. AND ( ip.to_group_id=$group_id)
  140. AND ip.lastedit_type<>'CalendareventDeleted'
  141. $session_condition
  142. GROUP BY ip.ref";
  143. //removed - > AND toolitemproperties.visibility='1'
  144. } // A.3 you are a course admin without any group or user filter
  145. else {
  146. // A.3.a you are a course admin without user or group filter but WITH studentview
  147. // => see all the messages of all the users and groups without editing possibilities
  148. if (isset($_GET['isStudentView']) && $_GET['isStudentView'] == 'true') {
  149. $sql = "SELECT
  150. agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  151. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." ip
  152. WHERE agenda.id = ip.ref ".$show_all_current."
  153. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  154. AND ip.visibility='1'
  155. $session_condition
  156. GROUP BY ip.ref
  157. ORDER $sort_item $sort";
  158. } // A.3.b you are a course admin or a student
  159. else {
  160. // A.3.b.1 you are a course admin without user or group filter and WITHOUT studentview (= the normal course admin view)
  161. // => see all the messages of all the users and groups with editing possibilities
  162. if (api_is_course_admin()) {
  163. $sql = "SELECT
  164. agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  165. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." ip
  166. WHERE agenda.id = ip.ref ".$show_all_current."
  167. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  168. AND ( ip.visibility='0' OR ip.visibility='1')
  169. $session_condition
  170. GROUP BY ip.ref";
  171. } else {
  172. // A.3.b.2 you are a student with no group filter possibly showall
  173. //when showing all the events we do not show the group events
  174. //todo showing ALL events including the groups events that are available
  175. $sql = "SELECT
  176. agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  177. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." ip
  178. WHERE agenda.id = ip.ref ".$show_all_current."
  179. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  180. AND ( ip.visibility='0' OR ip.visibility='1')
  181. $session_condition
  182. GROUP BY ip.ref";
  183. /*
  184. if (is_array($group_memberships) && count($group_memberships)>0)
  185. {
  186. echo $sql="SELECT
  187. agenda.*, toolitemproperties.*
  188. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  189. WHERE agenda.id = toolitemproperties.ref ".$show_all_current."
  190. AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  191. AND toolitemproperties.visibility='1' AND toolitemproperties.to_group_id IN (0, ".implode(", ", $group_memberships).")
  192. $session_condition
  193. GROUP BY toolitemproperties.ref
  194. ORDER BY start_date ".$sort;
  195. }
  196. else
  197. {
  198. $sql="SELECT
  199. agenda.*, toolitemproperties.*
  200. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  201. WHERE agenda.id = toolitemproperties.ref ".$show_all_current."
  202. AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  203. AND toolitemproperties.visibility='1' AND toolitemproperties.to_group_id='0'
  204. $session_condition
  205. GROUP BY toolitemproperties.ref
  206. ORDER BY start_date ".$sort;
  207. }
  208. */
  209. }
  210. }
  211. }
  212. } //if (is_allowed_to_edit() OR( api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous()))
  213. // B. you are a student
  214. else {
  215. if (is_array($group_memberships) && count($group_memberships) > 0) {
  216. $sql = "SELECT
  217. agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  218. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." ip
  219. WHERE agenda.id = ip.ref ".$show_all_current."
  220. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  221. AND ( ip.to_user_id=$user_id OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") )
  222. AND ip.visibility='1'
  223. $session_condition";
  224. } else {
  225. if (api_get_user_id()) {
  226. $sql = "SELECT
  227. agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  228. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." ip
  229. WHERE agenda.id = ip.ref ".$show_all_current."
  230. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  231. AND ( ip.to_user_id=$user_id OR ip.to_group_id='0')
  232. AND ip.visibility='1'
  233. $session_condition";
  234. } else {
  235. $sql = "SELECT
  236. agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  237. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." ip
  238. WHERE agenda.id = ip.ref ".$show_all_current."
  239. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  240. AND ip.to_group_id='0'
  241. AND ip.visibility='1'
  242. $session_condition";
  243. }
  244. }
  245. } // you are a student
  246. $my_events = array();
  247. $avoid_doubles = array();
  248. $result = Database::query($sql);
  249. //Course venets
  250. while ($row = Database::fetch_array($result, 'ASSOC')) {
  251. $row['calendar_type'] = 'course';
  252. if (!in_array($row['id'], $avoid_doubles)) {
  253. if (!empty($row['start_date']) && $row['start_date'] != '0000-00-00 00:00:00') {
  254. $row['start_date'] = api_get_local_time($row['start_date']);
  255. $row['start_date_tms'] = api_strtotime($row['start_date']);
  256. }
  257. if (!empty($row['end_date']) && $row['end_date'] != '0000-00-00 00:00:00') {
  258. $row['end_date'] = api_get_local_time($row['end_date']);
  259. $row['end_date_tms'] = api_strtotime($row['end_date']);
  260. }
  261. $my_events[] = $row;
  262. $avoid_doubles[] = $row['id'];
  263. }
  264. }
  265. //Check my personal calendar items
  266. if (api_get_setting('allow_personal_agenda') == 'true' && empty($_SESSION['user']) && empty($_SESSION['group'])) {
  267. $tbl_personal_agenda = Database :: get_main_table(TABLE_PERSONAL_AGENDA);
  268. // 1. creating the SQL statement for getting the personal agenda items in MONTH view
  269. $sql = "SELECT id, title, text as content , date as start_date, enddate as end_date, parent_event_id FROM ".$tbl_personal_agenda."
  270. WHERE user='".api_get_user_id()."' ".$show_all_current_personal;
  271. $result = Database::query($sql);
  272. while ($row = Database::fetch_array($result, 'ASSOC')) {
  273. $row['calendar_type'] = 'personal';
  274. if (!empty($row['start_date']) && $row['start_date'] != '0000-00-00 00:00:00') {
  275. $row['start_date'] = api_get_local_time($row['start_date']);
  276. $row['start_date_tms'] = api_strtotime($row['start_date']);
  277. }
  278. if (!empty($row['end_date']) && $row['end_date'] != '0000-00-00 00:00:00') {
  279. $row['end_date'] = api_get_local_time($row['end_date']);
  280. $row['end_date_tms'] = api_strtotime($row['end_date']);
  281. }
  282. $my_events[] = $row;
  283. }
  284. }
  285. //Check global agenda events
  286. if (empty($_SESSION['user']) && empty($_SESSION['group'])) {
  287. $table_agenda_system = Database :: get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  288. $current_access_url_id = api_get_current_access_url_id();
  289. $sql = "SELECT DISTINCT id, title, content , start_date, end_date FROM ".$table_agenda_system."
  290. WHERE 1=1 ".$show_all_current." AND access_url_id = $current_access_url_id";
  291. $result = Database::query($sql);
  292. while ($row = Database::fetch_array($result, 'ASSOC')) {
  293. if (!empty($row['start_date']) && $row['start_date'] != '0000-00-00 00:00:00') {
  294. $row['start_date'] = api_get_local_time($row['start_date']);
  295. $row['start_date_tms'] = api_strtotime($row['start_date']);
  296. }
  297. if (!empty($row['end_date']) && $row['end_date'] != '0000-00-00 00:00:00') {
  298. $row['end_date'] = api_get_local_time($row['end_date']);
  299. $row['end_date_tms'] = api_strtotime($row['end_date']);
  300. }
  301. $row['calendar_type'] = 'global';
  302. $my_events[] = $row;
  303. }
  304. }
  305. return $my_events;
  306. }
  307. /**
  308. * show the mini calender of the given month
  309. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  310. * @param array an array containing all the agenda items for the given month
  311. * @param integer $month: the integer value of the month we are viewing
  312. * @param integer $year: the 4-digit year indication e.g. 2005
  313. * @param string $monthName: the language variable for the mont name
  314. * @return html code
  315. * @todo refactor this so that $monthName is no longer needed as a parameter
  316. */
  317. function display_minimonthcalendar($agenda_items, $month, $year)
  318. {
  319. global $DaysShort, $MonthsLong;
  320. if (empty($month)) {
  321. $month = intval(date('m'));
  322. }
  323. if (empty($year)) {
  324. $month = date('Y');
  325. }
  326. $month_name = $MonthsLong[$month - 1];
  327. //Handle leap year
  328. $numberofdays = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  329. if (($year % 400 == 0) or ($year % 4 == 0 and $year % 100 <> 0)) {
  330. $numberofdays[2] = 29;
  331. }
  332. //Get the first day of the month
  333. $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
  334. //Start the week on monday
  335. $startdayofweek = $dayone['wday'] <> 0 ? ($dayone['wday'] - 1) : 6;
  336. $backwardsURL = api_get_self()."?".api_get_cidreq(
  337. )."&coursePath=".(empty($_GET['coursePath']) ? '' : Security::remove_XSS(
  338. $_GET['coursePath']
  339. ))."&courseCode=".(empty($_GET['courseCode']) ? '' : Security::remove_XSS(
  340. $_GET['courseCode']
  341. ))."&month=".($month == 1 ? 12 : $month - 1)."&year=".($month == 1 ? $year - 1 : $year);
  342. $forewardsURL = api_get_self()."?".api_get_cidreq(
  343. )."&coursePath=".(empty($_GET['coursePath']) ? '' : Security::remove_XSS(
  344. $_GET['coursePath']
  345. ))."&courseCode=".(empty($_GET['courseCode']) ? '' : Security::remove_XSS(
  346. $_GET['courseCode']
  347. ))."&month=".($month == 12 ? 1 : $month + 1)."&year=".($month == 12 ? $year + 1 : $year);
  348. $month_link = Display::url(
  349. $month_name." ".$year,
  350. api_get_self()."?".api_get_cidreq()."&month=".($month)."&year=".$year
  351. );
  352. $new_items = array();
  353. foreach ($agenda_items as $item) {
  354. $day = intval(substr($item["start_date"], 8, 2));
  355. $my_month = intval(substr($item["start_date"], 5, 2));
  356. if ($my_month == $month) {
  357. $new_items[$day][] = 1;
  358. }
  359. }
  360. $agenda_items = $new_items;
  361. echo "<table class=\"data_table\">",
  362. "<tr>",
  363. "<th width=\"10%\"><a href=\"", $backwardsURL, "\">".Display::return_icon(
  364. 'action_prev.png',
  365. get_lang('Previous')
  366. )." </a></th>",
  367. "<th width=\"80%\" colspan=\"5\">".$month_link."</th>",
  368. "<th width=\"10%\"><a href=\"", $forewardsURL, "\"> ".Display::return_icon(
  369. 'action_next.png',
  370. get_lang('Next')
  371. )." </a></th></tr>";
  372. echo "<tr>";
  373. for ($ii = 1; $ii < 8; $ii++) {
  374. echo "<td class=\"weekdays\">", $DaysShort[$ii % 7], "</td>";
  375. }
  376. echo "</tr>";
  377. $curday = -1;
  378. $today = getdate();
  379. while ($curday <= $numberofdays[$month]) {
  380. echo "<tr>";
  381. for ($ii = 0; $ii < 7; $ii++) {
  382. if (($curday == -1) && ($ii == $startdayofweek)) {
  383. $curday = 1;
  384. }
  385. if (($curday > 0) && ($curday <= $numberofdays[$month])) {
  386. $bgcolor = $ii < 5 ? $class = "class=\"days_week\"" : $class = "class=\"days_weekend\"";
  387. $dayheader = "$curday";
  388. if (($curday == $today['mday']) && ($year == $today['year']) && ($month == $today['mon'])) {
  389. $dayheader = "$curday";
  390. $class = "class=\"days_today\"";
  391. }
  392. echo "<td ".$class.">";
  393. if (!empty($agenda_items[$curday])) {
  394. echo "<a href=\"".api_get_self()."?".api_get_cidreq(
  395. )."&amp;sort=asc&amp;toolgroup=".api_get_group_id(
  396. )."&amp;action=view&amp;view=day&amp;day=".$curday."&amp;month=".$month."&amp;year=".$year."#".$curday."\">".$dayheader."</a>";
  397. } else {
  398. echo $dayheader;
  399. }
  400. // "a".$dayheader." <span class=\"agendaitem\">".$agenda_items[$curday]."</span>";
  401. echo "</td>";
  402. $curday++;
  403. } else {
  404. echo "<td></td>";
  405. }
  406. }
  407. echo "</tr>";
  408. }
  409. echo "</table>";
  410. }
  411. /**
  412. * show the calender of the given month
  413. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  414. * @author Julio Montoya - adding some nice styles
  415. * @param integer $month: the integer value of the month we are viewing
  416. * @param integer $year: the 4-digit year indication e.g. 2005
  417. * @return html code
  418. */
  419. function display_monthcalendar($month, $year, $agenda_items)
  420. {
  421. global $MonthsLong;
  422. global $DaysShort;
  423. global $origin;
  424. //Handle leap year
  425. $numberofdays = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  426. if (($year % 400 == 0) or ($year % 4 == 0 and $year % 100 <> 0)) {
  427. $numberofdays[2] = 29;
  428. }
  429. //Get the first day of the month
  430. $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
  431. //Start the week on monday
  432. $startdayofweek = $dayone['wday'] <> 0 ? ($dayone['wday'] - 1) : 6;
  433. $backwardsURL = api_get_self()."?".api_get_cidreq()."&view=".Security::remove_XSS(
  434. $_GET['view']
  435. )."&origin=$origin&amp;month=".($month == 1 ? 12 : $month - 1)."&amp;year=".($month == 1 ? $year - 1 : $year);
  436. $forewardsURL = api_get_self()."?".api_get_cidreq()."&view=".Security::remove_XSS(
  437. $_GET['view']
  438. )."&origin=$origin&amp;month=".($month == 12 ? 1 : $month + 1)."&amp;year=".($month == 12 ? $year + 1 : $year);
  439. $new_month = $month - 1;
  440. echo '<table id="agenda_list">';
  441. echo '<tr>';
  442. echo '<th width="10%"><a href="'.$backwardsURL.'">'.Display::return_icon(
  443. 'action_prev.png',
  444. get_lang('Previous'),
  445. array(),
  446. 32
  447. ).'</a></th>';
  448. echo '<th width="80%" colspan="5"><br /><h3>'.$MonthsLong[$new_month].' '.$year.'</h3></th>';
  449. echo '<th width="10%"><a href="'.$forewardsURL.'"> '.Display::return_icon(
  450. 'action_next.png',
  451. get_lang('Next'),
  452. array(),
  453. 32
  454. ).'</a></th>';
  455. echo '</tr>';
  456. echo '<tr>';
  457. for ($ii = 1; $ii < 8; $ii++) {
  458. echo '<td class="weekdays" width="14%">'.$DaysShort[$ii % 7].'</td>';
  459. }
  460. echo '</tr>';
  461. $curday = -1;
  462. $today = getdate();
  463. $new_items = array();
  464. foreach ($agenda_items as $item) {
  465. $day = intval(substr($item["start_date"], 8, 2));
  466. $my_month = intval(substr($item["start_date"], 5, 2));
  467. if ($my_month == $month) {
  468. $new_items[$day][] = $item;
  469. }
  470. }
  471. $agenda_items = $new_items;
  472. while ($curday <= $numberofdays[$month]) {
  473. echo '<tr>';
  474. //week
  475. for ($ii = 0; $ii < 7; $ii++) {
  476. if (($curday == -1) && ($ii == $startdayofweek)) {
  477. $curday = 1;
  478. }
  479. if (($curday > 0) && ($curday <= $numberofdays[$month])) {
  480. $bgcolor = $ii < 5 ? 'class="row_odd"' : 'class="row_even"';
  481. $dayheader = Display::div($curday, array('class' => 'agenda_day'));
  482. if (key_exists($curday, $agenda_items)) {
  483. $dayheader = Display::div($curday, array('class' => 'agenda_day'));
  484. $events_in_day = ArrayClass::msort($agenda_items[$curday], 'start_date_tms');
  485. foreach ($events_in_day as $value) {
  486. $some_content = false;
  487. $month_start_date = (int)substr($value['start_date'], 5, 2);
  488. if ($month == $month_start_date) {
  489. $some_content = true;
  490. $start_time = api_format_date($value['start_date'], TIME_NO_SEC_FORMAT);
  491. $end_time = '';
  492. if (!empty($value['end_date']) && $value['end_date'] != '0000-00-00 00:00:00') {
  493. $end_time = '-&nbsp;<i>'.api_format_date(
  494. $value['end_date'],
  495. DATE_TIME_FORMAT_LONG
  496. ).'</i>';
  497. }
  498. $complete_time = '<i>'.api_format_date(
  499. $value['start_date'],
  500. DATE_TIME_FORMAT_LONG
  501. ).'</i>&nbsp;'.$end_time;
  502. $time = '<i>'.$start_time.'</i>';
  503. switch ($value['calendar_type']) {
  504. case 'personal':
  505. $bg_color = '#D0E7F4';
  506. $icon = Display::return_icon('user.png', get_lang('MyAgenda'), array(), 22);
  507. break;
  508. case 'global':
  509. $bg_color = '#FFBC89';
  510. $icon = Display::return_icon(
  511. 'view_remove.png',
  512. get_lang('GlobalEvent'),
  513. array(),
  514. 22
  515. );
  516. break;
  517. case 'course':
  518. $bg_color = '#CAFFAA';
  519. $value['course_name'] = isset($value['course_name']) ? $value['course_name'] : null;
  520. $value['url'] = isset($value['url']) ? $value['url'] : null;
  521. $icon = Display::url(
  522. Display::return_icon(
  523. 'course.png',
  524. $value['course_name'].' '.get_lang('Course'),
  525. array(),
  526. 22
  527. ),
  528. $value['url']
  529. );
  530. break;
  531. default:
  532. break;
  533. }
  534. $icon = Display::div($icon, array('style' => 'float:right'));
  535. //Setting a personal event to green
  536. $dayheader .= '<div class="rounded_div_agenda" style="background-color:'.$bg_color.';">';
  537. //Link to bubble
  538. $url = Display::url(
  539. Text::cut($value['title'], 40),
  540. '#',
  541. array('id' => $value['calendar_type'].'_'.$value['id'], 'class' => 'opener')
  542. );
  543. $dayheader .= $time.' '.$icon.' '.Display::div($url);
  544. //Hidden content
  545. $content = Display::div(
  546. $icon.Display::tag('h1', $value['title']).$complete_time.'<hr />'.$value['content']
  547. );
  548. //Main div
  549. $dayheader .= Display::div(
  550. $content,
  551. array(
  552. 'id' => 'main_'.$value['calendar_type'].'_'.$value['id'],
  553. 'class' => 'dialog',
  554. 'style' => 'display:none'
  555. )
  556. );
  557. $dayheader .= '</div>';
  558. }
  559. //Do not show links with no content
  560. if (!$some_content) {
  561. $dayheader = Display::div($curday, array('class' => 'agenda_day'));
  562. }
  563. }
  564. }
  565. if (($curday == $today['mday']) && ($year == $today['year']) && ($month == $today['mon'])) {
  566. echo '<td class="days_today"'.$bgcolor.' style="height:122px;width:10%">'.$dayheader;
  567. } else {
  568. $class = 'days_week';
  569. echo '<td class="'.$class.'" '.$bgcolor.' style="height:122px;width:10%">'.$dayheader;
  570. }
  571. echo '</td>';
  572. $curday++;
  573. } else {
  574. echo '<td>&nbsp;</td>';
  575. }
  576. }
  577. echo '</tr>';
  578. } // end while
  579. echo '</table>';
  580. }
  581. /**
  582. * returns all the javascript that is required for easily selecting the target people/groups this goes into the $htmlHeadXtra[] array
  583. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  584. * @return javascript code
  585. */
  586. function to_javascript()
  587. {
  588. $showImg = Display::return_icon('div_show.gif');
  589. $hideImg = Display::return_icon('div_hide.gif');
  590. return "<script type=\"text/javascript\">
  591. $(function() {
  592. //js used when generating images on the fly see function Tracking::show_course_detail()
  593. $(\".dialog\").dialog(\"destroy\");
  594. $(\".dialog\").dialog({
  595. autoOpen: false,
  596. show: \"blind\",
  597. resizable: false,
  598. height:300,
  599. width:550,
  600. modal: true
  601. });
  602. $(\".opener\").click(function() {
  603. var my_id = $(this).attr('id');
  604. var big_image = '#main_' + my_id;
  605. $( big_image ).dialog(\"open\");
  606. return false;
  607. });
  608. });
  609. function move(fbox, tbox)
  610. {
  611. // @todo : change associative arrays arrLookup and arrLookupTitle that use firstname/lastnam as key
  612. // so, pb with homonyms
  613. var arrFbox = new Array();
  614. var arrTbox = new Array();
  615. var arrLookup = new Array();
  616. var arrLookupTitle = new Array();
  617. var i;
  618. for (i = 0; i < tbox.options.length; i++)
  619. {
  620. arrLookup[tbox.options[i].text] = tbox.options[i].value;
  621. arrLookupTitle[tbox.options[i].text] = tbox.options[i].title;
  622. arrTbox[i] = tbox.options[i].text;
  623. }
  624. var fLength = 0;
  625. var tLength = arrTbox.length;
  626. for(i = 0; i < fbox.options.length; i++)
  627. {
  628. arrLookup[fbox.options[i].text] = fbox.options[i].value;
  629. arrLookupTitle[fbox.options[i].text] = fbox.options[i].title;
  630. if (fbox.options[i].selected && fbox.options[i].value != \"\")
  631. {
  632. arrTbox[tLength] = fbox.options[i].text;
  633. tLength++;
  634. }
  635. else
  636. {
  637. arrFbox[fLength] = fbox.options[i].text;
  638. fLength++;
  639. }
  640. }
  641. //arrFbox.sort();
  642. //arrTbox.sort()
  643. var arrFboxGroup = new Array();
  644. var arrFboxUser = new Array();
  645. var prefix_x;
  646. for (x = 0; x < arrFbox.length; x++) {
  647. prefix_x = arrFbox[x].substring(0,2);
  648. if (prefix_x == 'G:') {
  649. arrFboxGroup.push(arrFbox[x]);
  650. } else {
  651. arrFboxUser.push(arrFbox[x]);
  652. }
  653. }
  654. arrFboxGroup.sort();
  655. arrFboxUser.sort();
  656. arrFbox = arrFboxGroup.concat(arrFboxUser);
  657. var arrTboxGroup = new Array();
  658. var arrTboxUser = new Array();
  659. var prefix_y;
  660. for (y = 0; y < arrTbox.length; y++) {
  661. prefix_y = arrTbox[y].substring(0,2);
  662. if (prefix_y == 'G:') {
  663. arrTboxGroup.push(arrTbox[y]);
  664. } else {
  665. arrTboxUser.push(arrTbox[y]);
  666. }
  667. }
  668. arrTboxGroup.sort();
  669. arrTboxUser.sort();
  670. arrTbox = arrTboxGroup.concat(arrTboxUser);
  671. fbox.length = 0;
  672. tbox.length = 0;
  673. var c;
  674. for(c = 0; c < arrFbox.length; c++)
  675. {
  676. var no = new Option();
  677. no.value = arrLookup[arrFbox[c]];
  678. no.text = arrFbox[c];
  679. no.title = arrLookupTitle[arrFbox[c]];
  680. fbox[c] = no;
  681. }
  682. for(c = 0; c < arrTbox.length; c++)
  683. {
  684. var no = new Option();
  685. no.value = arrLookup[arrTbox[c]];
  686. no.text = arrTbox[c];
  687. no.title = arrLookupTitle[arrTbox[c]];
  688. tbox[c] = no;
  689. }
  690. }
  691. function checkDate(month, day, year)
  692. {
  693. var monthLength =
  694. new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  695. if (!day || !month || !year)
  696. return false;
  697. // check for bisestile year
  698. if (year/4 == parseInt(year/4))
  699. monthLength[1] = 29;
  700. if (month < 1 || month > 12)
  701. return false;
  702. if (day > monthLength[month-1])
  703. return false;
  704. return true;
  705. }
  706. function mktime() {
  707. var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
  708. d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
  709. var dateManip = {
  710. 0: function(tt){ return d.setHours(tt); },
  711. 1: function(tt){ return d.setMinutes(tt); },
  712. 2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
  713. 3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
  714. 4: function(tt){ return d.setDate(tt+mb); },
  715. 5: function(tt){ return d.setYear(tt+ma); }
  716. };
  717. for( i = 0; i < argc; i++ ){
  718. no = parseInt(argv[i]*1);
  719. if (isNaN(no)) {
  720. return false;
  721. } else {
  722. // arg is number, lets manipulate date object
  723. if(!dateManip[i](no)){
  724. // failed
  725. return false;
  726. }
  727. }
  728. }
  729. return Math.floor(d.getTime()/1000);
  730. }
  731. function validate() {
  732. var f = document.new_calendar_item;
  733. f.submit();
  734. return true;
  735. }
  736. function selectAll(cbList,bSelect,showwarning) {
  737. var start_day = document.new_calendar_item.fday.value;
  738. var start_month = document.new_calendar_item.fmonth.value;
  739. var start_year = document.new_calendar_item.fyear.value;
  740. var start_hour = document.new_calendar_item.fhour.value;
  741. var start_minute = document.new_calendar_item.fminute.value;
  742. var start_date = mktime(start_hour,start_minute,0,start_month,start_day,start_year)
  743. var ends_day = document.new_calendar_item.end_fday.value;
  744. var ends_month = document.new_calendar_item.end_fmonth.value;
  745. var ends_year = document.new_calendar_item.end_fyear.value;
  746. var ends_hour = document.new_calendar_item.end_fhour.value;
  747. var ends_minute = document.new_calendar_item.end_fminute.value;
  748. var ends_date = mktime(ends_hour,ends_minute,0,ends_month,ends_day,ends_year)
  749. msg_err1 = document.getElementById(\"err_date\");
  750. msg_err2 = document.getElementById(\"err_start_date\");
  751. msg_err3 = document.getElementById(\"err_end_date\");
  752. msg_err4 = document.getElementById(\"err_title\");
  753. var error = false;
  754. if (start_date > ends_date) {
  755. if ($('#empty_end_date').is(':checked')) {
  756. msg_err1.innerHTML=\"\";
  757. msg_err2.innerHTML=\"\";
  758. msg_err3.innerHTML=\"\";
  759. } else {
  760. error = true;
  761. msg_err1.style.display =\"block\";
  762. msg_err1.innerHTML=\"".get_lang('EndDateCannotBeBeforeTheStartDate')."\";
  763. msg_err2.innerHTML=\"\";
  764. msg_err3.innerHTML=\"\";
  765. }
  766. }
  767. if (!checkDate(start_month,start_day,start_year)) {
  768. msg_err2.style.display =\"block\";
  769. msg_err2.innerHTML=\"".get_lang('InvalidDate')."\";
  770. msg_err1.innerHTML=\"\";
  771. msg_err3.innerHTML=\"\";
  772. error = true;
  773. }
  774. if (!checkDate(ends_month,ends_day,ends_year)) {
  775. msg_err3.style.display =\"block\";
  776. msg_err3.innerHTML=\"".get_lang('InvalidDate')."\";
  777. msg_err1.innerHTML=\"\";msg_err2.innerHTML=\"\";
  778. error = true;
  779. }
  780. if (document.new_calendar_item.title.value == '') {
  781. msg_err4.style.display =\"block\";
  782. msg_err4.innerHTML=\"".get_lang('FieldRequired')."\";
  783. msg_err1.innerHTML=\"\";msg_err2.innerHTML=\"\";msg_err3.innerHTML=\"\";
  784. error = true;
  785. }
  786. if (error == false) {
  787. if (cbList) {
  788. if (cbList.length < 1) {
  789. if (!confirm(\"".get_lang('Send2All')."\")) {
  790. return false;
  791. }
  792. }
  793. for (var i=0; i<cbList.length; i++)
  794. cbList[i].selected = cbList[i].checked = bSelect;
  795. }
  796. document.new_calendar_item.submit();
  797. }
  798. }
  799. function reverseAll(cbList)
  800. {
  801. for (var i=0; i<cbList.length; i++)
  802. {
  803. cbList[i].checked = !(cbList[i].checked)
  804. cbList[i].selected = !(cbList[i].selected)
  805. }
  806. }
  807. function plus_attachment() {
  808. if (document.getElementById('options').style.display == 'none') {
  809. document.getElementById('options').style.display = 'block';
  810. document.getElementById('plus').innerHTML='&nbsp;".$hideImg."&nbsp;".get_lang(
  811. 'AddAnAttachment'
  812. )."';
  813. } else {
  814. document.getElementById('options').style.display = 'none';
  815. document.getElementById('plus').innerHTML='&nbsp;".$showImg."&nbsp;".get_lang(
  816. 'AddAnAttachment'
  817. )."';
  818. }
  819. }
  820. function plus_repeated_event() {
  821. if (document.getElementById('options2').style.display == 'none') {
  822. document.getElementById('options2').style.display = 'block';
  823. document.getElementById('plus2').innerHTML='&nbsp;".$hideImg."&nbsp;".get_lang(
  824. 'RepeatEvent'
  825. )."';
  826. } else {
  827. document.getElementById('options2').style.display = 'none';
  828. document.getElementById('plus2').innerHTML='&nbsp;".$showImg."&nbsp;".get_lang(
  829. 'RepeatEvent'
  830. )."';
  831. }
  832. }
  833. /*
  834. function plus_ical() {
  835. if (document.getElementById('icalform').style.display == 'none') {
  836. document.getElementById('icalform').style.display = 'block';
  837. document.getElementById('plusical').innerHTML='';
  838. }
  839. }
  840. */
  841. </script>";
  842. }
  843. /**
  844. * returns the javascript for setting a filter. This is a jump menu
  845. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  846. * @return javascript code
  847. */
  848. function user_group_filter_javascript()
  849. {
  850. return "<script type=\"text/JavaScript\">
  851. function MM_jumpMenu(targ,selObj,restore){
  852. eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  853. if (restore) selObj.selectedIndex=0;
  854. }
  855. </script>";
  856. }
  857. /**
  858. * this function gets all the users of the current course
  859. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  860. * @return array: associative array where the key is the id of the user and the value is an array containing
  861. the first name, the last name, the user id
  862. */
  863. function get_course_users()
  864. {
  865. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  866. $tbl_courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  867. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  868. // not 100% if this is necessary, this however prevents a notice
  869. if (!isset($courseadmin_filter)) {
  870. $courseadmin_filter = '';
  871. }
  872. //
  873. $order_clause = api_sort_by_first_name(
  874. ) ? ' ORDER BY u.firstname, u.lastname' : ' ORDER BY u.lastname, u.firstname';
  875. $sql = "SELECT u.user_id uid, u.lastname lastName, u.firstname firstName, u.username
  876. FROM $tbl_user as u, $tbl_courseUser as cu
  877. WHERE cu.c_id = '".api_get_course_int_id()."'
  878. AND cu.user_id = u.user_id $courseadmin_filter".$order_clause;
  879. $result = Database::query($sql);
  880. while ($user = Database::fetch_array($result)) {
  881. $users[$user[0]] = $user;
  882. }
  883. $session_id = api_get_session_id();
  884. if (!empty($session_id)) {
  885. $users = array();
  886. $sql = "SELECT u.user_id uid, u.lastname lastName, u.firstName firstName, u.username
  887. FROM $tbl_session_course_user AS session_course_user
  888. INNER JOIN $tbl_user u ON u.user_id = session_course_user.id_user
  889. WHERE id_session = ".$session_id."
  890. AND c_id = '".api_get_course_int_id()."'";
  891. $result = Database::query($sql);
  892. while ($user = Database::fetch_array($result)) {
  893. $users[$user[0]] = $user;
  894. }
  895. }
  896. return $users;
  897. }
  898. /**
  899. * this function gets all the groups of the course
  900. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  901. * @return array
  902. */
  903. function get_course_groups()
  904. {
  905. $group_list = CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id());
  906. return $group_list;
  907. }
  908. /**
  909. * this function shows the form for sending a message to a specific group or user.
  910. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  911. *
  912. * @return html code
  913. */
  914. function show_to_form($to_already_selected)
  915. {
  916. $order = 'lastname';
  917. if (api_is_western_name_order()) {
  918. $order = 'firstname';
  919. }
  920. $user_list = CourseManager::get_user_list_from_course_code(api_get_course_id(), api_get_session_id(), null, $order);
  921. $group_list = CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id());
  922. construct_not_selected_select_form($group_list, $user_list, $to_already_selected);
  923. }
  924. /**
  925. * this function shows the form with the user that were not selected
  926. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  927. * @return html code
  928. */
  929. function construct_not_selected_select_form($group_list = null, $user_list = null, $to_already_selected = array())
  930. {
  931. echo '<select data-placeholder="'.get_lang('Select').'" style="width:150px;" class="chzn-select" id="selected_form_id" name="selected_form[]" multiple="multiple">';
  932. // Adding the groups to the select form
  933. if (isset($to_already_selected) && $to_already_selected === 'everyone') {
  934. echo '<option selected="selected" value="everyone">'.get_lang('Everyone').'</option>';
  935. //} else {
  936. } else {
  937. echo '<option value="everyone">'.get_lang('Everyone').'</option>';
  938. }
  939. if (is_array($group_list)) {
  940. echo '<optgroup label="'.get_lang('Groups').'">';
  941. foreach ($group_list as $this_group) {
  942. //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]);
  943. if (!is_array($to_already_selected) || !in_array("GROUP:".$this_group['id'], $to_already_selected)) {
  944. // $to_already_selected is the array containing the groups (and users) that are already selected
  945. echo "<option value=\"GROUP:".$this_group['id']."\">",
  946. "G: ", $this_group['name'], " &ndash; ".$this_group['userNb']." ".get_lang('Users').
  947. "</option>";
  948. }
  949. }
  950. // a divider
  951. }
  952. echo "</optgroup>";
  953. // adding the individual users to the select form
  954. if (!empty($user_list)) {
  955. echo '<optgroup label="'.get_lang('Users').'">';
  956. foreach ($user_list as $this_user) {
  957. // $to_already_selected is the array containing the users (and groups) that are already selected
  958. if (!is_array($to_already_selected) || !in_array("USER:".$this_user['user_id'], $to_already_selected)) {
  959. $username = api_htmlentities(sprintf(get_lang('LoginX'), $this_user['username']), ENT_QUOTES);
  960. $user_info = api_get_person_name(
  961. $this_user['firstname'],
  962. $this_user['lastname']
  963. ).' ('.$this_user['username'].')';
  964. echo "<option title='$username' value='USER:".$this_user['user_id']."'>$user_info</option>";
  965. }
  966. }
  967. echo "</optgroup>";
  968. }
  969. echo "</select>";
  970. }
  971. function show_to($filter = 0, $id = null)
  972. {
  973. $order = 'lastname';
  974. if (api_is_western_name_order()) {
  975. $order = 'firstname';
  976. }
  977. $user_list = CourseManager::get_user_list_from_course_code(api_get_course_id(), api_get_session_id(), null, $order);
  978. $group_list = CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id());
  979. return construct_to_select_form($group_list, $user_list, $filter, $id);
  980. }
  981. function construct_to_select_form($group_list = null, $user_list = null, $filter = 0, $id = null)
  982. {
  983. $result = '<form class="form-search" style="margin-top:3px;">';
  984. $result .= '<select data-placeholder= "'.get_lang('FilterAll').'" name="sel_to" class="chzn-select" id="selected_form_id_search">';
  985. // adding the groups to the select form
  986. $result .= '<option value=""></option>';
  987. $result .= '<option value="0">'.get_lang('FilterAll').'</option>';
  988. if (is_array($group_list)) {
  989. $result .= '<optgroup label="'.get_lang('FilterByGroup').'">';
  990. $this_group_name = count($group_list);
  991. foreach ($group_list as $this_group) {
  992. $group_filter = 'G:'.$this_group['id'];
  993. $selected = $group_filter == $filter ? "selected" : null;
  994. $result .= "<option value=G:".$this_group['id']." ".$selected.">".$this_group['name']."</option>";
  995. }
  996. $result .= "</optgroup>";
  997. }
  998. // adding the individual users to the select form
  999. if (!empty($user_list)) {
  1000. $result .= '<optgroup label="'.get_lang('FilterByUser').'">';
  1001. foreach ($user_list as $this_user) {
  1002. $username = api_htmlentities(sprintf(get_lang('LoginX'), $this_user['username']), ENT_QUOTES);
  1003. $user_info = api_get_person_name(
  1004. $this_user['firstname'],
  1005. $this_user['lastname']
  1006. ).' ('.$this_user['username'].')';
  1007. $selected = $this_user['user_id'] == $filter ? "selected" : null;
  1008. $result .= "<option title='$username' value=".$this_user['user_id']." ".$selected.">$user_info</option>";
  1009. }
  1010. $result .= "</optgroup>";
  1011. }
  1012. $result .= "</select>";
  1013. $result .= "</form>";
  1014. return $result;
  1015. }
  1016. /**
  1017. * This function shows the form with the user that were selected
  1018. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1019. * @return html code
  1020. */
  1021. function construct_selected_select_form($group_list = null, $user_list = null, $to_already_selected)
  1022. {
  1023. // we separate the $to_already_selected array (containing groups AND users into
  1024. // two separate arrays
  1025. if (is_array($to_already_selected)) {
  1026. $groupuser = separate_users_groups($to_already_selected);
  1027. }
  1028. $groups_to_already_selected = $groupuser['groups'];
  1029. $users_to_already_selected = $groupuser['users'];
  1030. // we load all the groups and all the users into a reference array that we use to search the name of the group / user
  1031. $ref_array_groups = get_course_groups();
  1032. $ref_array_users = get_course_users();
  1033. // we construct the form of the already selected groups / users
  1034. echo "<select id=\"selected_form2\" name=\"selectedform2[]\" size=\"5\" multiple=\"multiple\" style=\"width:200px\">";
  1035. if (is_array($to_already_selected)) {
  1036. $select_options_group = array();
  1037. $select_options_user = array();
  1038. $select_options_groupuser = array();
  1039. foreach ($to_already_selected as $groupuser) {
  1040. list($type, $id) = explode(":", $groupuser);
  1041. if ($type == "GROUP") {
  1042. $select_options_group[] = "<option value=\"".$groupuser."\">G: ".$ref_array_groups[$id]['name']."</option>";
  1043. //echo "<option value=\"".$groupuser."\">G: ".$ref_array_groups[$id]['name']."</option>";
  1044. } else {
  1045. $username = api_htmlentities(
  1046. sprintf(get_lang('LoginX'), $ref_array_users[$id]['username']),
  1047. ENT_QUOTES
  1048. );
  1049. $user_info = api_get_person_name($ref_array_users[$id]['firstName'], $ref_array_users[$id]['lastName']);
  1050. $select_options_user[] = "<option title='$username' value='".$groupuser."'>$user_info</option>";
  1051. //echo "<option value=\"".$groupuser."\">".api_get_person_name($ref_array_users[$id]['firstName'], $ref_array_users[$id]['lastName'])."</option>";
  1052. }
  1053. }
  1054. $select_options_group[] = "<option value=\"\">--------------------------------------------</option>";
  1055. $select_options_groupuser = array_merge($select_options_group, $select_options_user);
  1056. foreach ($select_options_groupuser as $select_options) {
  1057. echo $select_options;
  1058. }
  1059. } else {
  1060. if ($to_already_selected == 'everyone') {
  1061. // adding the groups to the select form
  1062. if (is_array($group_list)) {
  1063. foreach ($group_list as $this_group) {
  1064. //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]);
  1065. if (!is_array($to_already_selected) || !in_array(
  1066. "GROUP:".$this_group['id'],
  1067. $to_already_selected
  1068. )
  1069. ) { // $to_already_selected is the array containing the groups (and users) that are already selected
  1070. echo "<option value=\"GROUP:".$this_group['id']."\">",
  1071. "G: ", $this_group['name'], " &ndash; ".$this_group['userNb']." ".get_lang('Users').
  1072. "</option>";
  1073. }
  1074. }
  1075. }
  1076. echo "<option value=\"\">--------------------------------------------</option>";
  1077. // adding the individual users to the select form
  1078. foreach ($user_list as $this_user) {
  1079. if (!is_array($to_already_selected) || !in_array(
  1080. "USER:".$this_user['uid'],
  1081. $to_already_selected
  1082. )
  1083. ) { // $to_already_selected is the array containing the users (and groups) that are already selected
  1084. echo "<option value=\"USER:", $this_user['uid'], "\">",
  1085. "", api_get_person_name($this_user['firstName'], $this_user['lastName']),
  1086. "</option>";
  1087. }
  1088. }
  1089. }
  1090. }
  1091. echo "</select>";
  1092. }
  1093. /**
  1094. * This function stores the Agenda Item in the table calendar_event and updates the item_property table also
  1095. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1096. * @return integer the id of the last added agenda item
  1097. */
  1098. function store_new_agenda_item()
  1099. {
  1100. $_course = api_get_course_info();
  1101. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  1102. $t_agenda_repeat = Database::get_course_Table(TABLE_AGENDA_REPEAT);
  1103. $course_id = api_get_course_int_id();
  1104. // some filtering of the input data
  1105. $title = trim($_POST['title']); // no html allowed in the title
  1106. $content = trim($_POST['content']);
  1107. $start_date = (int)$_POST['fyear']."-".(int)$_POST['fmonth']."-".(int)$_POST['fday']." ".(int)$_POST['fhour'].":".(int)$_POST['fminute'].":00";
  1108. $end_date = (int)$_POST['end_fyear']."-".(int)$_POST['end_fmonth']."-".(int)$_POST['end_fday']." ".(int)$_POST['end_fhour'].":".(int)$_POST['end_fminute'].":00";
  1109. $title = Database::escape_string($title);
  1110. $content = Database::escape_string($content);
  1111. $start_date = Database::escape_string($start_date);
  1112. $end_date = Database::escape_string($end_date);
  1113. if ($_POST['empty_end_date'] == 'on') {
  1114. $end_date = "0000-00-00 00:00:00";
  1115. }
  1116. // store in the table calendar_event
  1117. $sql = "INSERT INTO ".$TABLEAGENDA." (c_id, title,content, start_date, end_date)
  1118. VALUES ($course_id, '".$title."','".$content."', '".$start_date."','".$end_date."')";
  1119. $result = Database::query($sql);
  1120. $last_id = Database::insert_id();
  1121. // store in last_tooledit (first the groups, then the users
  1122. $to = $_POST['selectedform'];
  1123. if ((!is_null(
  1124. $to
  1125. )) || (!empty($_SESSION['toolgroup']))
  1126. ) { // !is_null($to): when no user is selected we send it to everyone
  1127. //$send_to=separate_users_groups($to);
  1128. $send_to = separate_users_groups(explode('|', $to));
  1129. // storing the selected groups
  1130. if (is_array($send_to['groups'])) {
  1131. foreach ($send_to['groups'] as $group) {
  1132. api_item_property_update(
  1133. $_course,
  1134. TOOL_CALENDAR_EVENT,
  1135. $last_id,
  1136. "AgendaAdded",
  1137. api_get_user_id(),
  1138. $group,
  1139. '',
  1140. $start_date,
  1141. $end_date
  1142. );
  1143. }
  1144. }
  1145. // storing the selected users
  1146. if (is_array($send_to['users'])) {
  1147. foreach ($send_to['users'] as $user) {
  1148. api_item_property_update(
  1149. $_course,
  1150. TOOL_CALENDAR_EVENT,
  1151. $last_id,
  1152. "AgendaAdded",
  1153. api_get_user_id(),
  1154. '',
  1155. $user,
  1156. $start_date,
  1157. $end_date
  1158. );
  1159. }
  1160. }
  1161. } else { // the message is sent to everyone, so we set the group to 0
  1162. api_item_property_update(
  1163. $_course,
  1164. TOOL_CALENDAR_EVENT,
  1165. $last_id,
  1166. "AgendaAdded",
  1167. api_get_user_id(),
  1168. '',
  1169. '',
  1170. $start_date,
  1171. $end_date
  1172. );
  1173. }
  1174. // storing the resources
  1175. store_resources($_SESSION['source_type'], $last_id);
  1176. $course_id = api_get_course_int_id();
  1177. //if repetitive, insert element into agenda_repeat table
  1178. if (!empty($_POST['repeat']) && !empty($_POST['repeat_type'])) {
  1179. if (!empty($_POST['repeat_end_year']) && !empty($_POST['repeat_end_month']) && !empty($_POST['repeat_end_day'])) {
  1180. $end_y = intval($_POST['repeat_end_year']);
  1181. $end_m = intval($_POST['repeat_end_month']);
  1182. $end_d = intval($_POST['repeat_end_day']);
  1183. $end = mktime((int)$_POST['fhour'], (int)$_POST['fminute'], 0, $end_m, $end_d, $end_y);
  1184. $now = time();
  1185. $type = Database::escape_string($_POST['repeat_type']);
  1186. if ($end > $now && in_array(
  1187. $type,
  1188. array('daily', 'weekly', 'monthlyByDate', 'monthlyByDay', 'monthlyByDayR', 'yearly')
  1189. )
  1190. ) {
  1191. $sql = "INSERT INTO $t_agenda_repeat (c_id, cal_id, cal_type, cal_end)".
  1192. " VALUES ($course_id, $last_id,'$type',$end)";
  1193. $res = Database::query($sql);
  1194. }
  1195. }
  1196. }
  1197. return $last_id;
  1198. }
  1199. /**
  1200. * Stores the given agenda item as an announcement (unlinked copy)
  1201. * @param integer Agenda item's ID
  1202. * @return integer New announcement item's ID
  1203. */
  1204. function store_agenda_item_as_announcement($item_id)
  1205. {
  1206. $table_agenda = Database::get_course_table(TABLE_AGENDA);
  1207. $table_ann = Database::get_course_table(TABLE_ANNOUNCEMENT);
  1208. //check params
  1209. if (empty($item_id) or $item_id != strval(intval($item_id))) {
  1210. return -1;
  1211. }
  1212. //get the agenda item
  1213. $item_id = Database::escape_string($item_id);
  1214. $sql = "SELECT * FROM $table_agenda WHERE id = ".$item_id;
  1215. $res = Database::query($sql);
  1216. $course_id = api_get_course_int_id();
  1217. if (Database::num_rows($res) > 0) {
  1218. $row = Database::fetch_array($res);
  1219. //we have the agenda event, copy it
  1220. //get the maximum value for display order in announcement table
  1221. $sql_max = "SELECT MAX(display_order) FROM $table_ann WHERE c_id = $course_id ";
  1222. $res_max = Database::query($sql_max);
  1223. $row_max = Database::fetch_array($res_max);
  1224. $max = intval($row_max[0]) + 1;
  1225. //build the announcement text
  1226. $content = $row['content'];
  1227. //insert announcement
  1228. $session_id = api_get_session_id();
  1229. $sql_ins = "INSERT INTO $table_ann (c_id, title,content,end_date,display_order,session_id) ".
  1230. "VALUES ($course_id, '".Database::escape_string($row['title'])."','".Database::escape_string(
  1231. $content
  1232. )."','".Database::escape_string($row['end_date'])."','$max','$session_id')";
  1233. $res_ins = Database::query($sql_ins);
  1234. if ($res > 0) {
  1235. $ann_id = Database::insert_id();
  1236. //Now also get the list of item_properties rows for this agenda_item (calendar_event)
  1237. //and copy them into announcement item_properties
  1238. $table_props = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1239. $sql_props = "SELECT * FROM $table_props WHERE tool = 'calendar_event' AND ref='$item_id'";
  1240. $res_props = Database::query($sql_props);
  1241. if (Database::num_rows($res_props) > 0) {
  1242. while ($row_props = Database::fetch_array($res_props)) {
  1243. //insert into announcement item_property
  1244. $time = api_get_utc_datetime();
  1245. $sql_ins_props = "INSERT INTO $table_props ".
  1246. "(c_id, tool, insert_user_id, insert_date, ".
  1247. "lastedit_date, ref, lastedit_type,".
  1248. "lastedit_user_id, to_group_id, to_user_id, ".
  1249. "visibility, start_visible, end_visible)".
  1250. " VALUES ".
  1251. "($course_id, 'announcement','".$row_props['insert_user_id']."','".$time."',".
  1252. "'$time','$ann_id','AnnouncementAdded',".
  1253. "'".$row_props['last_edit_user_id']."','".$row_props['to_group_id']."','".$row_props['to_user_id']."',".
  1254. "'".$row_props['visibility']."','".$row_props['start_visible']."','".$row_props['end_visible']."')";
  1255. $res_ins_props = Database::query($sql_ins_props);
  1256. if ($res_ins_props <= 0) {
  1257. return -1;
  1258. } else {
  1259. //copy was a success
  1260. return $ann_id;
  1261. }
  1262. }
  1263. }
  1264. } else {
  1265. return -1;
  1266. }
  1267. }
  1268. return -1;
  1269. }
  1270. /**
  1271. * This function separates the users from the groups
  1272. * users have a value USER:XXX (with XXX the dokeos id
  1273. * groups have a value GROUP:YYY (with YYY the group id)
  1274. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1275. * @return array
  1276. */
  1277. function separate_users_groups($to)
  1278. {
  1279. $grouplist = array();
  1280. $userlist = array();
  1281. $send_to = null;
  1282. $send_to['everyone'] = false;
  1283. if (is_array($to) && count($to) > 0) {
  1284. foreach ($to as $to_item) {
  1285. list($type, $id) = explode(':', $to_item);
  1286. switch ($type) {
  1287. case 'everyone':
  1288. $send_to['everyone'] = true;
  1289. case 'GROUP':
  1290. $grouplist[] = $id;
  1291. break;
  1292. case 'USER':
  1293. $userlist[] = $id;
  1294. break;
  1295. }
  1296. }
  1297. $send_to['groups'] = $grouplist;
  1298. $send_to['users'] = $userlist;
  1299. }
  1300. return $send_to;
  1301. }
  1302. /**
  1303. * returns all the users and all the groups a specific Agenda item has been sent to
  1304. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1305. * @return array
  1306. */
  1307. function sent_to($tool, $id)
  1308. {
  1309. $TABLE_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1310. $tool = Database::escape_string($tool);
  1311. $id = Database::escape_string($id);
  1312. $sql = "SELECT * FROM $TABLE_ITEM_PROPERTY WHERE tool='".$tool."' AND ref='".$id."'";
  1313. $result = Database::query($sql);
  1314. while ($row = Database::fetch_array($result)) {
  1315. // if to_group_id is null then it is sent to a specific user
  1316. // if to_group_id = 0 then it is sent to everybody
  1317. if (!is_null($row['to_group_id'])) {
  1318. $sent_to_group[] = $row['to_group_id'];
  1319. //echo $row['to_group_id'];
  1320. }
  1321. // if to_user_id <> 0 then it is sent to a specific user
  1322. if ($row['to_user_id'] <> 0) {
  1323. $sent_to_user[] = $row['to_user_id'];
  1324. }
  1325. }
  1326. if (isset($sent_to_group)) {
  1327. $sent_to['groups'] = $sent_to_group;
  1328. }
  1329. if (isset($sent_to_user)) {
  1330. $sent_to['users'] = $sent_to_user;
  1331. }
  1332. return $sent_to;
  1333. }
  1334. /**
  1335. * constructs the form to display all the groups and users the message has been sent to
  1336. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1337. * @param array $sent_to_array: a 2 dimensional array containing the groups and the users
  1338. * the first level is a distinction between groups and users: $sent_to_array['groups'] and $sent_to_array['users']
  1339. * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array containing all the id's of the
  1340. * groups (resp. users) who have received this message.
  1341. * @return html
  1342. */
  1343. function sent_to_form($sent_to_array)
  1344. {
  1345. // we find all the names of the groups
  1346. $group_names = get_course_groups();
  1347. // we count the number of users and the number of groups
  1348. if (isset($sent_to_array['users'])) {
  1349. $number_users = count($sent_to_array['users']);
  1350. } else {
  1351. $number_users = 0;
  1352. }
  1353. if (isset($sent_to_array['groups'])) {
  1354. $number_groups = count($sent_to_array['groups']);
  1355. } else {
  1356. $number_groups = 0;
  1357. }
  1358. $total_numbers = $number_users + $number_groups;
  1359. $output = array();
  1360. // starting the form if there is more than one user/group
  1361. if ($total_numbers > 1) {
  1362. //$output.="<option>".get_lang("SentTo")."</option>";
  1363. // outputting the name of the groups
  1364. if (is_array($sent_to_array['groups'])) {
  1365. foreach ($sent_to_array['groups'] as $group_id) {
  1366. if (isset($group_names[$group_id]['name'])) {
  1367. $output[] = $group_names[$group_id]['name'];
  1368. }
  1369. }
  1370. }
  1371. if (isset($sent_to_array['users'])) {
  1372. if (is_array($sent_to_array['users'])) {
  1373. foreach ($sent_to_array['users'] as $user_id) {
  1374. // @todo add username as tooltip - is this fucntion still used ?
  1375. // $user_info= api_get_user_info($user_id);
  1376. // $username = api_htmlentities(sprintf(get_lang('LoginX'), $user_info['username']), ENT_QUOTES);
  1377. $output[] = api_get_person_name($user_info['firstName'], $user_info['lastName']);
  1378. }
  1379. }
  1380. }
  1381. } else {
  1382. // there is only one user/group
  1383. if (is_array($sent_to_array['users'])) {
  1384. // @todo add username as tooltip - is this fucntion still used ?
  1385. // $user_info = api_get_user_info($sent_to_array['users'][0]);
  1386. // $username = api_htmlentities(sprintf(get_lang('LoginX'), $user_info['username']), ENT_QUOTES);
  1387. $output[] = api_get_person_name($user_info['firstName'], $user_info['lastName']);
  1388. }
  1389. if (is_array($sent_to_array['groups']) and $sent_to_array['groups'][0] !== 0) {
  1390. $group_id = $sent_to_array['groups'][0];
  1391. $output[] = $group_names[$group_id]['name'];
  1392. }
  1393. if (is_array($sent_to_array['groups']) and $sent_to_array['groups'][0] == 0) {
  1394. $output[] = get_lang("Everybody");
  1395. }
  1396. }
  1397. if (!empty($output)) {
  1398. $output = array_filter($output);
  1399. if (count($output) > 0) {
  1400. $output = implode(', ', $output);
  1401. }
  1402. return $output;
  1403. }
  1404. }
  1405. /**
  1406. * This function displays a dropdown list that allows the course administrator do view the calendar items of one specific group
  1407. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1408. */
  1409. function show_group_filter_form()
  1410. {
  1411. $group_list = get_course_groups();
  1412. echo "<select name=\"select\" onchange=\"javascript: MM_jumpMenu('parent',this,0)\">";
  1413. echo "<option value=\"agenda.php?group=none\">".get_lang('ShowAll')."</option>";
  1414. foreach ($group_list as $this_group) {
  1415. echo "<option value=\"agenda.php?action=view&group=".$this_group['id']."\" ";
  1416. echo ($this_group['id'] == $_SESSION['group']) ? " selected" : "";
  1417. echo ">".$this_group['name']."</option>";
  1418. }
  1419. echo "</select>";
  1420. }
  1421. /**
  1422. * This function displays a dropdown list that allows the course administrator do view the calendar items of one specific group
  1423. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1424. */
  1425. function show_user_filter_form()
  1426. {
  1427. $user_list = get_course_users();
  1428. echo "<select name=\"select\" onchange=\"javascript: MM_jumpMenu('parent',this,0)\">";
  1429. echo "<option value=\"agenda.php?user=none\">".get_lang('ShowAll')."</option>";
  1430. foreach ($user_list as $this_user) {
  1431. // echo "<option value=\"agenda.php?isStudentView=true&amp;user=".$this_user['uid']."\">".api_get_person_name($this_user['firstName'], $this_user['lastName'])."</option>";
  1432. echo "<option value=\"agenda.php?action=view&user=".$this_user['uid']."\" ";
  1433. echo ($this_user['uid'] == $_SESSION['user']) ? " selected" : "";
  1434. echo ">".api_get_person_name($this_user['firstName'], $this_user['lastName'])."</option>";
  1435. }
  1436. echo "</select>";
  1437. }
  1438. /**
  1439. * This function displays a dropdown list that allows the course administrator do view the calendar items of one specific group
  1440. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1441. */
  1442. function show_user_group_filter_form()
  1443. {
  1444. echo "<select name=\"select\" onchange=\"javascript: MM_jumpMenu('parent',this,0)\">";
  1445. echo "<option value=\"agenda.php?user=none&action=view\">".get_lang("ShowAll")."</option>";
  1446. // Groups
  1447. $group_list = get_course_groups();
  1448. $group_available_to_access = array();
  1449. $option = '';
  1450. if (!empty($group_list)) {
  1451. $option = "<optgroup label=\"".get_lang("Groups")."\">";
  1452. foreach ($group_list as $this_group) {
  1453. // echo "<option value=\"agenda.php?isStudentView=true&amp;group=".$this_group['id']."\">".$this_group['name']."</option>";
  1454. $has_access = GroupManager::user_has_access(
  1455. api_get_user_id(),
  1456. $this_group['id'],
  1457. GroupManager::GROUP_TOOL_CALENDAR
  1458. );
  1459. $result = GroupManager::get_group_properties($this_group['id']);
  1460. if ($result['calendar_state'] != '0') {
  1461. $group_available_to_access[] = $this_group['id'];
  1462. }
  1463. // lastedit
  1464. if ($has_access || $result['calendar_state'] == '1') {
  1465. $option .= "<option value=\"agenda.php?action=view&group=".$this_group['id']."\" ";
  1466. $option .= ($this_group['id'] == $_SESSION['group']) ? " selected" : "";
  1467. $option .= ">".$this_group['name']."</option>";
  1468. }
  1469. }
  1470. $option .= "</optgroup>";
  1471. }
  1472. echo $option;
  1473. // Users
  1474. $user_list = get_course_users();
  1475. if (!empty($user_list)) {
  1476. echo "<optgroup label=\"".get_lang("Users")."\">";
  1477. foreach ($user_list as $this_user) {
  1478. echo "<option value=\"agenda.php?action=view&user=".$this_user['uid']."\" ";
  1479. echo (isset($_SESSION['user']) && $this_user['uid'] == $_SESSION['user']) ? " selected" : "";
  1480. echo ">".api_get_person_name($this_user['firstName'], $this_user['lastName'])."</option>";
  1481. }
  1482. echo "</optgroup>";
  1483. }
  1484. echo "</select>";
  1485. }
  1486. /**
  1487. * This tools loads all the users and all the groups who have received a specific item (in this case an agenda item)
  1488. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1489. */
  1490. function load_edit_users($tool, $id)
  1491. {
  1492. $tool = Database::escape_string($tool);
  1493. $id = Database::escape_string($id);
  1494. $TABLE_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1495. $course_id = api_get_course_int_id();
  1496. $sql = "SELECT * FROM $TABLE_ITEM_PROPERTY WHERE c_id = $course_id AND tool='$tool' AND ref='$id'";
  1497. $result = Database::query($sql) or die(Database::error());
  1498. $to = array();
  1499. while ($row = Database::fetch_array($result)) {
  1500. $to_group = $row['to_group_id'];
  1501. switch ($to_group) {
  1502. // it was send to one specific user
  1503. case null:
  1504. $to[] = "USER:".$row['to_user_id'];
  1505. break;
  1506. // it was sent to everyone
  1507. case 0:
  1508. return "everyone";
  1509. exit;
  1510. break;
  1511. default:
  1512. $to[] = "GROUP:".$row['to_group_id'];
  1513. }
  1514. }
  1515. return $to;
  1516. }
  1517. /**
  1518. * This functions swithes the visibility a course resource using the visible field in 'last_tooledit' values: 0 = invisible
  1519. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1520. */
  1521. function change_visibility($tool, $id, $visibility)
  1522. {
  1523. $_course = api_get_course_info();
  1524. $TABLE_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1525. $tool = Database::escape_string($tool);
  1526. $id = Database::escape_string($id);
  1527. /*
  1528. $sql="SELECT * FROM $TABLE_ITEM_PROPERTY WHERE tool='".TOOL_CALENDAR_EVENT."' AND ref='$id'";
  1529. $result=Database::query($sql) or die (Database::error());
  1530. $row=Database::fetch_array($result);
  1531. */
  1532. if ($visibility == 0) {
  1533. $sql_visibility = "UPDATE $TABLE_ITEM_PROPERTY SET visibility='0' WHERE tool='$tool' AND ref='$id'";
  1534. api_item_property_update($_course, TOOL_CALENDAR_EVENT, $id, "invisible", api_get_user_id());
  1535. } else {
  1536. $sql_visibility = "UPDATE $TABLE_ITEM_PROPERTY SET visibility='1' WHERE tool='$tool' AND ref='$id'";
  1537. api_item_property_update($_course, TOOL_CALENDAR_EVENT, $id, "visible", api_get_user_id());
  1538. }
  1539. }
  1540. /**
  1541. * The links that allows the course administrator to add a new agenda item, to filter on groups or users
  1542. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1543. */
  1544. function display_courseadmin_links($filter = 0)
  1545. {
  1546. if (!api_get_course_int_id()) {
  1547. $type = isset($_REQUEST['type']) ? Security::remove_XSS($_REQUEST['type']) : 'personal';
  1548. if ($type == 'platform') {
  1549. $type = 'admin';
  1550. }
  1551. return "<a href='agenda_js.php?type=$type'>".Display::return_icon('calendar.png', get_lang('Agenda'), '', ICON_SIZE_MEDIUM)."</a>";
  1552. }
  1553. $form = null;
  1554. if (!isset($_GET['action'])) {
  1555. $form = show_to($filter, 'selected_form_id_search');
  1556. $actions = "<a href='agenda_js.php?type=course&".api_get_cidreq()."'>".Display::return_icon('calendar_na.png', get_lang('Agenda'), '', ICON_SIZE_MEDIUM)."</a>";
  1557. } else {
  1558. $actions = "<a href='agenda_js.php?type=course&".api_get_cidreq()."'>".Display::return_icon('calendar.png', get_lang('Agenda'), '', ICON_SIZE_MEDIUM)."</a>";
  1559. }
  1560. $actions .= "<a href='agenda.php?".api_get_cidreq()."&amp;sort=asc&amp;toolgroup=".api_get_group_id()."&action=add'>".Display::return_icon('new_event.png', get_lang('AgendaAdd'), '', ICON_SIZE_MEDIUM)."</a>";
  1561. $actions .= "<a href='agenda.php?".api_get_cidreq()."&action=importical'>".Display::return_icon('import_calendar.png', get_lang('ICalFileImport'), '', ICON_SIZE_MEDIUM)."</a>";
  1562. $actions .= $form;
  1563. return $actions;
  1564. }
  1565. /**
  1566. * The links that allows the student AND course administrator to show all agenda items and sort up/down
  1567. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1568. * @author Julio Montoya removing options, changing to a simple agenda tool
  1569. */
  1570. function display_student_links()
  1571. {
  1572. global $show;
  1573. $month = isset($_GET['month']) ? intval($_GET['month']) : null;
  1574. $year = isset($_GET['year']) ? intval($_GET['year']) : null;
  1575. $day_url = '&month='.$month.'&year='.$year;
  1576. if ($_SESSION['view'] <> 'month') {
  1577. echo "<a href=\"".api_get_self()."?action=view".$day_url."&toolgroup=".api_get_group_id(
  1578. )."&amp;view=month\">".Display::return_icon(
  1579. 'month_empty.png',
  1580. get_lang('MonthView'),
  1581. '',
  1582. ICON_SIZE_MEDIUM
  1583. )."</a> ";
  1584. } else {
  1585. echo "<a href=\"".api_get_self()."?action=view".$day_url."&toolgroup=".api_get_group_id(
  1586. )."&amp;view=list\">".Display::return_icon('week.png', get_lang('ListView'), '', ICON_SIZE_MEDIUM)."</a> ";
  1587. }
  1588. $day_url = '&month='.date('m').'&year='.date('Y').'&view='.Security::remove_XSS($_GET['view']);
  1589. $today_url = api_get_self()."?action=view".$day_url."&toolgroup=".api_get_group_id();
  1590. echo Display::url(get_lang('Today'), $today_url, array('class' => 'btn'));
  1591. }
  1592. /**
  1593. * get all the information of the agenda_item from the database
  1594. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1595. * @param integer the id of the agenda item we are getting all the information of
  1596. * @return array an associative array that contains all the information of the agenda item. The keys are the database fields
  1597. */
  1598. function get_agenda_item($id)
  1599. {
  1600. global $TABLEAGENDA;
  1601. $t_agenda_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT);
  1602. $id = Database::escape_string($id);
  1603. $item = array();
  1604. if (empty($id)) {
  1605. $id = intval(Database::escape_string(($_GET['id'])));
  1606. } else {
  1607. $id = (int)$id;
  1608. }
  1609. $course_id = api_get_course_int_id();
  1610. if (empty($id)) {
  1611. return $item;
  1612. }
  1613. $sql = "SELECT * FROM ".$TABLEAGENDA." WHERE id='".$id."' AND c_id = $course_id ";
  1614. $result = Database::query($sql);
  1615. $entry_to_edit = Database::fetch_array($result);
  1616. $item['title'] = $entry_to_edit["title"];
  1617. $item['content'] = $entry_to_edit["content"];
  1618. $item['start_date'] = $entry_to_edit["start_date"];
  1619. $item['end_date'] = $entry_to_edit["end_date"];
  1620. $item['to'] = load_edit_users(TOOL_CALENDAR_EVENT, $id);
  1621. // if the item has been sent to everybody then we show the compact to form
  1622. if ($item['to'] == "everyone") {
  1623. $_SESSION['allow_individual_calendar'] = "hide";
  1624. } else {
  1625. $_SESSION['allow_individual_calendar'] = "show";
  1626. }
  1627. $item['repeat'] = false;
  1628. $sql = "SELECT * FROM $t_agenda_repeat WHERE c_id = $course_id AND cal_id = $id";
  1629. $res = Database::query($sql);
  1630. if (Database::num_rows($res) > 0) {
  1631. //this event is repetitive
  1632. $row = Database::fetch_array($res);
  1633. $item['repeat'] = true;
  1634. $item['repeat_type'] = $row['cal_type'];
  1635. $item['repeat_end'] = $row['cal_end'];
  1636. $item['repeat_frequency'] = $row['cal_frequency']; //unused in 1.8.5 RC1 - will be used later to say if once every 2 or 3 weeks, for example
  1637. $item['repeat_days'] = $row['cal_days']; //unused in 1.8.5 RC1 - will be used later
  1638. }
  1639. //TODO - add management of repeat exceptions
  1640. return $item;
  1641. }
  1642. /**
  1643. * This is the function that updates an agenda item. It does 3 things
  1644. * 1. storethe start_date, end_date, title and message in the calendar_event table
  1645. * 2. store the groups/users who this message is meant for in the item_property table
  1646. * 3. modify the attachments (if needed)
  1647. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1648. * @author Julio Montoya Adding UTC support
  1649. */
  1650. function store_edited_agenda_item($event_id, $id_attach, $file_comment)
  1651. {
  1652. // STEP 1: editing the calendar_event table
  1653. // 1.a. some filtering of the input data
  1654. $id = $event_id;
  1655. $title = strip_tags(trim($_POST['title'])); // no html allowed in the title
  1656. $content = trim($_POST['content']);
  1657. $start_date = (int)$_POST['fyear']."-".(int)$_POST['fmonth']."-".(int)$_POST['fday']." ".(int)$_POST['fhour'].":".(int)$_POST['fminute'].":00";
  1658. $end_date = (int)$_POST['end_fyear']."-".(int)$_POST['end_fmonth']."-".(int)$_POST['end_fday']." ".(int)$_POST['end_fhour'].":".(int)$_POST['end_fminute'].":00";
  1659. $to = $_POST['selectedform'];
  1660. if ($_POST['empty_end_date'] == 'on') {
  1661. $end_date = "0000-00-00 00:00:00";
  1662. }
  1663. $course_info = api_get_course_info();
  1664. $agendaObj = new Agenda();
  1665. if (empty($course_info)) {
  1666. $agendaObj->type = 'personal';
  1667. } else {
  1668. $agendaObj->set_course($course_info);
  1669. $agendaObj->type = 'course';
  1670. if (api_is_course_coach() && !api_is_element_in_the_session(TOOL_AGENDA, $event_id)) {
  1671. return false;
  1672. }
  1673. if (!api_is_allowed_to_edit(null, true)) {
  1674. return false;
  1675. }
  1676. }
  1677. $all_day = isset($_REQUEST['all_day']) && !empty($_REQUEST['all_day']) ? 1 : 0;
  1678. $agendaObj->edit_event($id, $start_date, $end_date, $all_day, null, $title, $content);
  1679. if (empty($id_attach)) {
  1680. add_agenda_attachment_file($file_comment, $id);
  1681. } else {
  1682. edit_agenda_attachment_file($file_comment, $id, $id_attach);
  1683. }
  1684. // step 2: editing the item_property table (=delete all and add the new destination users/groups)
  1685. /*if ($edit_result = true) {
  1686. // 2.a. delete everything for the users
  1687. $sql_delete = "DELETE FROM ".$TABLE_ITEM_PROPERTY." WHERE c_id = $course_id AND ref='$id' AND tool='".TOOL_CALENDAR_EVENT."'";
  1688. $result = Database::query($sql_delete) or die(Database::error());
  1689. // 2.b. storing the new users/groups
  1690. if (!is_null($to)) { // !is_null($to): when no user is selected we send it to everyone
  1691. $send_to = separate_users_groups($to);
  1692. // storing the selected groups
  1693. if (is_array($send_to['groups'])) {
  1694. foreach ($send_to['groups'] as $group) {
  1695. api_item_property_update($_course, TOOL_CALENDAR_EVENT, $id, "AgendaModified", api_get_user_id(), $group, '', $start_date, $end_date);
  1696. }
  1697. }
  1698. // storing the selected users
  1699. if (is_array($send_to['users'])) {
  1700. foreach ($send_to['users'] as $user) {
  1701. api_item_property_update($_course, TOOL_CALENDAR_EVENT, $id, "AgendaModified", api_get_user_id(), '', $user, $start_date, $end_date);
  1702. }
  1703. }
  1704. } else {
  1705. // the message is sent to everyone, so we set the group to 0
  1706. api_item_property_update($_course, TOOL_CALENDAR_EVENT, $id, "AgendaModified", api_get_user_id(), '', '', $start_date, $end_date);
  1707. }
  1708. }*/
  1709. // step 3: update the attachments (=delete all and add those in the session
  1710. update_added_resources("Agenda", $id);
  1711. // return the message;
  1712. Display::display_confirmation_message(get_lang("EditSuccess"));
  1713. }
  1714. /**
  1715. * This function stores the Agenda Item in the table calendar_event and updates the item_property table also (after an edit)
  1716. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1717. */
  1718. function save_edit_agenda_item($id, $title, $content, $start_date, $end_date)
  1719. {
  1720. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  1721. $id = Database::escape_string($id);
  1722. $title = Database::escape_string($title);
  1723. $content = Database::escape_string($content);
  1724. $start_date = Database::escape_string($start_date);
  1725. $end_date = Database::escape_string($end_date);
  1726. // store the modifications in the table calendar_event
  1727. $sql = "UPDATE ".$TABLEAGENDA."
  1728. SET title ='".$title."',
  1729. content ='".$content."',
  1730. start_date ='".$start_date."',
  1731. end_date ='".$end_date."'
  1732. WHERE id='".$id."'";
  1733. $result = Database::query($sql);
  1734. return true;
  1735. }
  1736. /**
  1737. * This is the function that deletes an agenda item.
  1738. * The agenda item is no longer physically deleted but the visibility in the item_property table is set to 2
  1739. * which means that it is invisible for the student AND course admin. Only the platform administrator can see it.
  1740. * This will in a later stage allow the platform administrator to recover resources that were mistakenly deleted
  1741. * by the course administrator
  1742. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1743. * @param integer the id of the agenda item wa are deleting
  1744. */
  1745. function delete_agenda_item($id)
  1746. {
  1747. $_course = api_get_course_info();
  1748. $id = Database::escape_string($id);
  1749. if (api_is_allowed_to_edit(false, true) OR (api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous())
  1750. ) {
  1751. if (!empty($_GET['id']) && isset($_GET['action']) && $_GET['action'] == "delete") {
  1752. $t_agenda = Database::get_course_table(TABLE_AGENDA);
  1753. $t_agenda_r = Database::get_course_table(TABLE_AGENDA_REPEAT);
  1754. $id = intval($_GET['id']);
  1755. $sql = "SELECT * FROM $t_agenda_r WHERE cal_id = $id";
  1756. $res = Database::query($sql);
  1757. if (Database::num_rows($res) > 0) {
  1758. $sql_children = "SELECT * FROM $t_agenda WHERE parent_event_id = $id";
  1759. $res_children = Database::query($sql_children);
  1760. if (Database::num_rows($res_children) > 0) {
  1761. while ($row_child = Database::fetch_array($res_children)) {
  1762. api_item_property_update(
  1763. $_course,
  1764. TOOL_CALENDAR_EVENT,
  1765. $row_child['id'],
  1766. 'delete',
  1767. api_get_user_id()
  1768. );
  1769. }
  1770. }
  1771. $sql_del = "DELETE FROM $t_agenda_r WHERE cal_id = $id";
  1772. $res_del = Database::query($sql_del);
  1773. }
  1774. //$sql = "DELETE FROM ".$TABLEAGENDA." WHERE id='$id'";
  1775. //$sql= "UPDATE ".$TABLE_ITEM_PROPERTY." SET visibility='2' WHERE tool='Agenda' and ref='$id'";
  1776. //$result = Database::query($sql) or die (Database::error());
  1777. api_item_property_update($_course, TOOL_CALENDAR_EVENT, $id, 'delete', api_get_user_id());
  1778. // delete the resources that were added to this agenda item
  1779. // 2DO: as we no longer fysically delete the agenda item (to make it possible to 'restore'
  1780. // deleted items, we should not delete the added resources either.
  1781. // delete_added_resource("Agenda", $id); // -> this is no longer needed as the message is not really deleted but only visibility=2 (only platform admin can see it)
  1782. //resetting the $id;
  1783. $id = null;
  1784. // displaying the result message in the yellow box
  1785. Display::display_confirmation_message(get_lang("AgendaDeleteSuccess"));
  1786. } // if (isset($id)&&$id&&isset($action)&&$action=="delete")
  1787. } // if ($is_allowed_to_edit)
  1788. }
  1789. /**
  1790. * Makes an agenda item visible or invisible for a student
  1791. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1792. * @param integer id the id of the agenda item we are changing the visibility of
  1793. */
  1794. function showhide_agenda_item($id)
  1795. {
  1796. global $nameTools;
  1797. /*
  1798. SHOW / HIDE A CALENDAR ITEM
  1799. */
  1800. // and $_GET['isStudentView']<>"false" is added to prevent that the visibility is changed after you do the following:
  1801. // change visibility -> studentview -> course manager view
  1802. if ((api_is_allowed_to_edit(false, true) OR (api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous(
  1803. ))) and $_GET['isStudentView'] <> "false"
  1804. ) {
  1805. if (isset($_GET['id']) && isset($_GET['action']) && $_GET['action'] == "showhide") {
  1806. $id = (int)addslashes($_GET['id']);
  1807. if (isset($_GET['next_action']) && $_GET['next_action'] == strval(intval($_GET['next_action']))) {
  1808. $visibility = $_GET['next_action'];
  1809. change_visibility($nameTools, $id, $visibility);
  1810. Display::display_confirmation_message(get_lang("VisibilityChanged"));
  1811. }
  1812. }
  1813. }
  1814. }
  1815. /**
  1816. * Displays all the agenda items
  1817. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1818. * @author Yannick Warnier <yannick.warnier@beeznest.com> - cleanup
  1819. * @author Julio Montoya <gugli100@gmail.com> - Refactoring
  1820. */
  1821. function display_agenda_items($agenda_items, $day = false)
  1822. {
  1823. global $charset;
  1824. if (isset($day) && $day) {
  1825. $new_items = array();
  1826. foreach ($agenda_items as $item) {
  1827. if (substr($item['start_date'], 8, 2) == $day) {
  1828. $new_items[] = $item;
  1829. }
  1830. }
  1831. $agenda_items = $new_items;
  1832. }
  1833. if (isset($_GET['sort']) && $_GET['sort'] == 'asc') {
  1834. $sort_inverse = 'desc';
  1835. $sort = 'asc';
  1836. } else {
  1837. $sort_inverse = 'asc';
  1838. $sort = 'desc';
  1839. }
  1840. if (isset($_GET['col']) && $_GET['col'] == 'end') {
  1841. $sort_item = 'end_date_tms';
  1842. $col = 'end';
  1843. } else {
  1844. $sort_item = 'start_date_tms';
  1845. $col = 'start';
  1846. }
  1847. $agenda_items = ArrayClass::msort($agenda_items, $sort_item, $sort);
  1848. //DISPLAY: NO ITEMS
  1849. if (empty($agenda_items)) {
  1850. echo Display::display_warning_message(get_lang('NoAgendaItems'));
  1851. } else {
  1852. echo '<table class="data_table">';
  1853. $th = Display::tag('th', get_lang('Title'));
  1854. $month = isset($_GET['month']) ? intval($_GET['month']) : null;
  1855. $year = isset($_GET['year']) ? intval($_GET['year']) : null;
  1856. $day = isset($_GET['day']) ? intval($_GET['day']) : null;
  1857. $url = api_get_self().'?'.api_get_cidreq().'&month='.$month.'&year='.$year.'&day='.$day;
  1858. $th .= Display::tag('th', Display::url(get_lang('StartTimeWindow'), $url.'&sort='.$sort_inverse.'&col=start'));
  1859. $th .= Display::tag('th', Display::url(get_lang('EndTimeWindow'), $url.'&sort='.$sort_inverse.'&col=end'));
  1860. if ((api_is_allowed_to_edit(false, true) OR (api_get_course_setting(
  1861. 'allow_user_edit_agenda'
  1862. ) && !api_is_anonymous()))
  1863. ) {
  1864. $th .= Display::tag('th', get_lang('Modify'));
  1865. }
  1866. echo Display::tag('tr', $th);
  1867. $counter = 0;
  1868. foreach ($agenda_items as $myrow) {
  1869. $is_repeated = !empty($myrow['parent_event_id']);
  1870. $class = 'row_even';
  1871. if ($counter % 2) {
  1872. $class = 'row_odd';
  1873. }
  1874. /* display: the icon, title, destinees of the item */
  1875. echo '<tr class="'.$class.'">';
  1876. //Title
  1877. echo "<td>";
  1878. $attach_icon = '';
  1879. // attachment list
  1880. $attachment_list = get_attachment($myrow['id']);
  1881. if (!empty($attachment_list)) {
  1882. $attach_icon = ' '.Display::return_icon('attachment.gif', get_lang('Attachment'));
  1883. }
  1884. $title_class = '';
  1885. if (isset($myrow['visibility']) && $myrow['visibility'] == 0) {
  1886. $title_class = 'invisible';
  1887. }
  1888. switch ($myrow['calendar_type']) {
  1889. case 'global':
  1890. $icon_type = Display::return_icon('view_remove.png', get_lang('GlobalEvent'), array(), 22);
  1891. echo $icon_type.' '.$myrow['title'].$attach_icon;
  1892. break;
  1893. case 'personal':
  1894. $icon_type = Display::return_icon('user.png', get_lang(' '), array(), 22);
  1895. echo $icon_type.' '.$myrow['title'].$attach_icon;
  1896. break;
  1897. case 'course':
  1898. $icon_type = Display::return_icon('course.png', get_lang('Course'), array(), 22);
  1899. $agenda_url = api_get_path(
  1900. WEB_CODE_PATH
  1901. ).'calendar/agenda.php?agenda_id='.$myrow['id'].'&action=view';
  1902. echo Display::url(
  1903. $icon_type.' '.$myrow['title'].$attach_icon,
  1904. $agenda_url,
  1905. array('class' => $title_class)
  1906. );
  1907. break;
  1908. }
  1909. echo '</td>';
  1910. //Start date
  1911. echo '<td>';
  1912. if (!empty($myrow['start_date']) && $myrow['start_date'] != '0000-00-00 00:00:00') {
  1913. echo api_format_date($myrow['start_date']);
  1914. }
  1915. echo '</td>';
  1916. //End date
  1917. echo '<td>';
  1918. if (!empty($myrow['end_date']) && $myrow['end_date'] != '0000-00-00 00:00:00') {
  1919. echo api_format_date($myrow['end_date']);
  1920. }
  1921. echo '</td>';
  1922. /* Display: edit delete button (course admin only) */
  1923. if (!$is_repeated && (api_is_allowed_to_edit(false, true) OR (api_get_course_setting(
  1924. 'allow_user_edit_agenda'
  1925. ) && !api_is_anonymous())) && $myrow['calendar_type'] == 'course'
  1926. ) {
  1927. echo '<td align="center">';
  1928. if (!(api_is_course_coach() && !api_is_element_in_the_session(TOOL_AGENDA, $myrow['id']))) {
  1929. // a coach can only delete an element belonging to his session
  1930. $mylink = api_get_self().'?'.api_get_cidreq().'&origin='.Security::remove_XSS(
  1931. $_GET['origin']
  1932. ).'&id='.$myrow['id'].'&sort='.$sort.'&col='.$col.'&';
  1933. // edit
  1934. echo '<a href="'.$mylink.api_get_cidreq()."&toolgroup=".Security::remove_XSS(
  1935. $_GET['toolgroup']
  1936. ).'&action=edit&id_attach='.$attachment_list['id'].'" title="'.get_lang("ModifyCalendarItem").'">';
  1937. echo Display::return_icon('edit.png', get_lang('ModifyCalendarItem'), '', ICON_SIZE_SMALL)."</a>";
  1938. echo '<a href="'.$mylink.api_get_cidreq()."&toolgroup=".Security::remove_XSS(
  1939. $_GET['toolgroup']
  1940. ).'&action=announce" title="'.get_lang("AddAnnouncement").'">';
  1941. echo Display::return_icon(
  1942. 'new_announce.png',
  1943. get_lang('AddAnnouncement'),
  1944. array(),
  1945. ICON_SIZE_SMALL
  1946. )."</a> ";
  1947. if ($myrow['visibility'] == 1) {
  1948. $image_visibility = "visible";
  1949. $text_visibility = get_lang("Hide");
  1950. $next_action = 0;
  1951. } else {
  1952. $image_visibility = "invisible";
  1953. $text_visibility = get_lang("Show");
  1954. $next_action = 1;
  1955. }
  1956. echo '<a href="'.$mylink.api_get_cidreq().'&toolgroup='.Security::remove_XSS(
  1957. $_GET['toolgroup']
  1958. ).'&action=showhide&next_action='.$next_action.'" title="'.$text_visibility.'">'.Display::return_icon(
  1959. $image_visibility.'.png',
  1960. $text_visibility,
  1961. '',
  1962. ICON_SIZE_SMALL
  1963. ).'</a> ';
  1964. echo "<a href=\"".$mylink.api_get_cidreq()."&toolgroup=".Security::remove_XSS(
  1965. $_GET['toolgroup']
  1966. )."&action=delete\" onclick=\"javascript:if(!confirm('".addslashes(
  1967. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES, $charset)
  1968. )."')) return false;\" title=\"".get_lang("Delete")."\"> ";
  1969. echo Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL)."&nbsp;</a>";
  1970. }
  1971. $mylink = 'ical_export.php?'.api_get_cidreq().'&type=course&id='.$myrow['id'];
  1972. //echo '<a class="ical_export" href="'.$mylink.'&class=confidential" title="'.get_lang('ExportiCalConfidential').'">'.Display::return_icon($export_icon_high, get_lang('ExportiCalConfidential')).'</a> ';
  1973. //echo '<a class="ical_export" href="'.$mylink.'&class=private" title="'.get_lang('ExportiCalPrivate').'">'.Display::return_icon($export_icon_low, get_lang('ExportiCalPrivate')).'</a> ';
  1974. //echo '<a class="ical_export" href="'.$mylink.'&class=public" title="'.get_lang('ExportiCalPublic').'">'.Display::return_icon($export_icon, get_lang('ExportiCalPublic')).'</a> ';
  1975. echo '<a href="#" onclick="javascript:win_print=window.open(\'print.php?id='.$myrow['id'].'\',\'popup\',\'left=100,top=100,width=700,height=500,scrollbars=1,resizable=0\'); win_print.focus(); return false;">'.Display::return_icon(
  1976. 'printer.png',
  1977. get_lang('Print'),
  1978. '',
  1979. ICON_SIZE_SMALL
  1980. ).'</a>&nbsp;';
  1981. echo '</td>';
  1982. } else {
  1983. if ($is_repeated && (api_is_allowed_to_edit(false, true) || api_get_course_setting(
  1984. 'allow_user_edit_agenda'
  1985. ) && !api_is_anonymous())
  1986. ) {
  1987. echo '<td align="center">';
  1988. echo get_lang('RepeatedEvent'), ' <a href="', api_get_self(), '?', api_get_cidreq(
  1989. ), '&agenda_id=', $myrow['parent_event_id'], '" alt="', get_lang(
  1990. 'RepeatedEventViewOriginalEvent'
  1991. ), '">', get_lang('RepeatedEventViewOriginalEvent'), '</a>';
  1992. echo '</td>';
  1993. }
  1994. if ((api_is_allowed_to_edit(false, true) || (api_get_course_setting(
  1995. 'allow_user_edit_agenda'
  1996. ) && !api_is_anonymous(
  1997. ))) && ($myrow['calendar_type'] == 'personal' OR $myrow['calendar_type'] == 'global')
  1998. ) {
  1999. echo '<td align="center">';
  2000. echo '</td>';
  2001. }
  2002. }
  2003. $counter++;
  2004. echo "</tr>";
  2005. } // end while ($myrow=Database::fetch_array($result))
  2006. echo "</table><br /><br />";
  2007. }
  2008. if (!empty($event_list)) {
  2009. $event_list = api_substr($event_list, 0, -1);
  2010. } else {
  2011. $event_list = '0';
  2012. }
  2013. echo "<form name=\"event_list_form\"><input type=\"hidden\" name=\"event_list\" value=\"$event_list\" /></form>";
  2014. // closing the layout table
  2015. echo "</td>",
  2016. "</tr>",
  2017. "</table>";
  2018. }
  2019. /**
  2020. * Show a list with all the attachments according to the post's id
  2021. * @param the post's id
  2022. * @return array with the post info
  2023. * @author Christian Fasanando
  2024. * @version November 2008, dokeos 1.8.6
  2025. */
  2026. function get_attachment($agenda_id, $course_id = null)
  2027. {
  2028. $agenda_table_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  2029. if (empty($course_id)) {
  2030. $course_id = api_get_course_int_id();
  2031. } else {
  2032. $course_id = intval($course_id);
  2033. }
  2034. $agenda_id = Database::escape_string($agenda_id);
  2035. $row = array();
  2036. $sql = 'SELECT id,path, filename,comment FROM '.$agenda_table_attachment.' WHERE c_id = '.$course_id.' AND agenda_id = '.(int)$agenda_id.'';
  2037. $result = Database::query($sql);
  2038. if (Database::num_rows($result) != 0) {
  2039. $row = Database::fetch_array($result);
  2040. }
  2041. return $row;
  2042. }
  2043. /**
  2044. * Displays only 1 agenda item. This is used when an agenda item is added to the learning path.
  2045. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2046. */
  2047. function display_one_agenda_item($agenda_id)
  2048. {
  2049. global $TABLEAGENDA;
  2050. global $TABLE_ITEM_PROPERTY;
  2051. global $select_month, $select_year;
  2052. global $DaysShort, $DaysLong, $MonthsLong;
  2053. global $dateFormatLong, $timeNoSecFormat, $charset;
  2054. // getting the name of the groups
  2055. $group_names = get_course_groups();
  2056. $agenda_id = intval($agenda_id);
  2057. if (!(api_is_allowed_to_edit(false, true) || (api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous(
  2058. )))
  2059. ) {
  2060. $visibility_condition = " AND ip.visibility='1' ";
  2061. }
  2062. $sql = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  2063. FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." ip
  2064. WHERE agenda.id = ip.ref
  2065. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  2066. $visibility_condition
  2067. AND agenda.id='$agenda_id'";
  2068. $result = Database::query($sql);
  2069. $number_items = Database::num_rows($result);
  2070. $myrow = Database::fetch_array($result, 'ASSOC'); // there should be only one item so no need for a while loop
  2071. $sql_rep = "SELECT * FROM $TABLEAGENDA WHERE id = $agenda_id AND parent_event_id IS NOT NULL AND parent_event_id !=0";
  2072. $res_rep = Database::query($sql_rep);
  2073. $repeat = false;
  2074. $repeat_id = 0;
  2075. if (Database::num_rows($res_rep) > 0) {
  2076. $repeat = true;
  2077. $row_rep = Database::fetch_array($res_rep);
  2078. $repeat_id = $row_rep['parent_event_id'];
  2079. }
  2080. // DISPLAY: NO ITEMS
  2081. if ($number_items == 0) {
  2082. Display::display_warning_message(get_lang("NoAgendaItems"));
  2083. return false;
  2084. }
  2085. // DISPLAY: THE ITEMS
  2086. echo "<table id=\"data_table\" class=\"data_table\">";
  2087. // DISPLAY : the icon, title, destinees of the item
  2088. $myrow["start_date"] = api_get_local_time($myrow["start_date"]);
  2089. // highlight: if a date in the small calendar is clicked we highlight the relevant items
  2090. $db_date = (int)api_format_date($myrow["start_date"], "%d").intval(
  2091. api_format_date($myrow["start_date"], "%m")
  2092. ).api_format_date($myrow["start_date"], "%Y");
  2093. if ($_GET["day"].$_GET["month"].$_GET["year"] <> $db_date) {
  2094. if ($myrow['visibility'] == '0') {
  2095. $style = "data_hidden";
  2096. $stylenotbold = "datanotbold_hidden";
  2097. $text_style = "text_hidden";
  2098. } else {
  2099. $style = "data";
  2100. $stylenotbold = "datanotbold";
  2101. $text_style = "text";
  2102. }
  2103. } else {
  2104. $style = "datanow";
  2105. $stylenotbold = "datanotboldnow";
  2106. $text_style = "textnow";
  2107. }
  2108. echo Display::tag('h2', $myrow['title']);
  2109. echo "<tr>";
  2110. if (api_is_allowed_to_edit(false, true)) {
  2111. if (!(api_is_course_coach() && !api_is_element_in_the_session(TOOL_AGENDA, $myrow['id']))) {
  2112. // a coach can only delete an element belonging to his session
  2113. // DISPLAY: edit delete button (course admin only)
  2114. $export_icon = '../img/export.png';
  2115. $export_icon_low = '../img/export_low_fade.png';
  2116. $export_icon_high = '../img/export_high_fade.png';
  2117. echo '<th style="text-align:right">';
  2118. if (!$repeat && api_is_allowed_to_edit(false, true)) {
  2119. // edit
  2120. $mylink = api_get_self()."?".api_get_cidreq()."&origin=".Security::remove_XSS(
  2121. $_GET['origin']
  2122. )."&id=".$myrow['id'];
  2123. if (!empty($_GET['agenda_id'])) {
  2124. // rather ugly hack because the id parameter is already set above but below we set it again
  2125. $mylink .= '&agenda_id='.Security::remove_XSS($_GET['agenda_id']).'&id='.Security::remove_XSS(
  2126. $_GET['agenda_id']
  2127. );
  2128. }
  2129. if ($myrow['visibility'] == 1) {
  2130. $image_visibility = "visible";
  2131. $next_action = 0;
  2132. } else {
  2133. $image_visibility = "invisible";
  2134. $next_action = 1;
  2135. }
  2136. echo '<a href="'.$mylink.'&action=showhide&next_action='.$next_action.'">'.Display::return_icon(
  2137. $image_visibility.'.png',
  2138. get_lang('Visible'),
  2139. '',
  2140. ICON_SIZE_SMALL
  2141. ).'</a>';
  2142. echo "<a href=\"".$mylink."&action=edit\">",
  2143. Display::return_icon('edit.png', get_lang('ModifyCalendarItem'), '', ICON_SIZE_SMALL), "</a>",
  2144. "<a href=\"".$mylink."&action=delete\" onclick=\"javascript:if(!confirm('".addslashes(
  2145. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES, $charset)
  2146. )."')) return false;\">",
  2147. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL), "</a>";
  2148. }
  2149. $mylink = 'ical_export.php?'.api_get_cidreq().'&type=course&id='.$myrow['id'];
  2150. //echo '<a class="ical_export" href="'.$mylink.'&class=confidential" title="'.get_lang('ExportiCalConfidential').'">'.Display::return_icon($export_icon_high, get_lang('ExportiCalConfidential')).'</a> ';
  2151. //echo '<a class="ical_export" href="'.$mylink.'&class=private" title="'.get_lang('ExportiCalPrivate').'">'.Display::return_icon($export_icon_low, get_lang('ExportiCalPrivate')).'</a> ';
  2152. //echo '<a class="ical_export" href="'.$mylink.'&class=public" title="'.get_lang('ExportiCalPublic').'">'.Display::return_icon($export_icon, get_lang('ExportiCalPublic')).'</a> ';
  2153. echo '<a href="javascript: void(0);" onclick="javascript:win_print=window.open(\'print.php?id='.$myrow['id'].'\',\'popup\',\'left=100,top=100,width=700,height=500,scrollbars=1,resizable=0\'); win_print.focus(); return false;">'.Display::return_icon(
  2154. 'printer.png',
  2155. get_lang('Print'),
  2156. '',
  2157. ICON_SIZE_SMALL
  2158. ).'</a>&nbsp;';
  2159. echo "</th>";
  2160. }
  2161. }
  2162. // title
  2163. echo "<tr class='row_odd'>";
  2164. echo '<td colspan="2">'.get_lang("StartTime").": ";
  2165. echo api_format_date($myrow['start_date']);
  2166. echo "</td>";
  2167. echo "</td>";
  2168. echo "<tr class='row_odd'>";
  2169. echo '<td colspan="2">'.get_lang("EndTime").": ";
  2170. echo api_convert_and_format_date($myrow['end_date']);
  2171. echo "</td>";
  2172. // Content
  2173. $content = $myrow['content'];
  2174. $content = Text::make_clickable($content);
  2175. echo '<tr class="row_even">';
  2176. echo '<td '.(api_is_allowed_to_edit() ? 'colspan="3"' : 'colspan="2"').'>';
  2177. echo $content;
  2178. echo '</td></tr>';
  2179. //Attachments
  2180. $attachment_list = get_attachment($agenda_id);
  2181. if (!empty($attachment_list)) {
  2182. echo '<tr class="row_even"><td colspan="2">';
  2183. $realname = $attachment_list['path'];
  2184. $user_filename = $attachment_list['filename'];
  2185. $full_file_name = 'download.php?file='.$realname;
  2186. echo Display::return_icon('attachment.gif', get_lang('Attachment'));
  2187. echo '<a href="'.$full_file_name.'"> '.$user_filename.'</a>';
  2188. if (api_is_allowed_to_edit()) {
  2189. echo '&nbsp;&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.Security::remove_XSS(
  2190. $_GET['origin']
  2191. ).'&action=delete_attach&id_attach='.$attachment_list['id'].'" onclick="javascript:if(!confirm(\''.addslashes(
  2192. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES, $charset)
  2193. ).'\')) return false;">'.Display::return_icon(
  2194. 'delete.png',
  2195. get_lang('Delete'),
  2196. '',
  2197. ICON_SIZE_SMALL
  2198. ).'</a><br />';
  2199. }
  2200. echo '<br /><span class="forum_attach_comment" >'.$attachment_list['comment'].'</span>';
  2201. echo '</td></tr>';
  2202. }
  2203. // the message has been sent to
  2204. echo '<tr>';
  2205. echo "<td class='announcements_datum'>".get_lang("SentTo").": ";
  2206. $sent_to = sent_to(TOOL_CALENDAR_EVENT, $myrow["ref"]);
  2207. $sent_to_form = sent_to_form($sent_to);
  2208. echo $sent_to_form;
  2209. echo "</td></tr>";
  2210. if ($repeat) {
  2211. echo '<tr>';
  2212. echo '<td colspan="2">';
  2213. echo get_lang('RepeatedEvent').' <a href="', api_get_self(), '?', api_get_cidreq(
  2214. ), '&agenda_id=', $repeat_id, '" alt="', get_lang('RepeatedEventViewOriginalEvent'), '">', get_lang(
  2215. 'RepeatedEventViewOriginalEvent'
  2216. ), '</a>';
  2217. echo '</td>';
  2218. echo '</tr>';
  2219. }
  2220. /* Added resources */
  2221. if (check_added_resources("Agenda", $myrow["id"])) {
  2222. echo "<tr><td colspan='3'>";
  2223. echo "<i>".get_lang("AddedResources")."</i><br/>";
  2224. if ($myrow['visibility'] == 0) {
  2225. $addedresource_style = "invisible";
  2226. }
  2227. display_added_resources("Agenda", $myrow["id"], $addedresource_style);
  2228. echo "</td></tr>";
  2229. }
  2230. // closing the layout table
  2231. echo "</td>",
  2232. "</tr>",
  2233. "</table>";
  2234. }
  2235. /**
  2236. * Show the form for adding a new agenda item. This is the same function that is used whenever we are editing an
  2237. * agenda item. When the id parameter is empty (default behaviour), then we show an empty form, else we are editing and
  2238. * we have to retrieve the information that is in the database and use this information in the forms.
  2239. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2240. * @param integer id, the id of the agenda item we are editing. By default this is empty which means that we are adding an
  2241. * agenda item.
  2242. */
  2243. function show_add_form($id = '', $type = null)
  2244. {
  2245. $showImg = Display::return_icon('div_show.gif');
  2246. $hideImg = Display::return_icon('div_hide.gif');
  2247. $MonthsLong = api_get_months_long();
  2248. $htmlHeadXtra[] = to_javascript();
  2249. // the default values for the forms
  2250. //if (isset(!$_GET['originalresource']) && $_GET['originalresource'] !== 'no') {
  2251. if (!isset($_GET['originalresource'])) {
  2252. $day = date('d');
  2253. $month = date('m');
  2254. $year = date('Y');
  2255. $hours = 9;
  2256. $minutes = '00';
  2257. $title = null;
  2258. $content = null;
  2259. $end_day = date('d');
  2260. $end_month = date('m');
  2261. $end_year = date('Y');
  2262. $end_hours = 17;
  2263. $end_minutes = '00';
  2264. $repeat = false;
  2265. } else {
  2266. // we are coming from the resource linker so there might already have been some information in the form.
  2267. // When we clicked on the button to add resources we stored every form information into a session and now we
  2268. // are doing the opposite thing: getting the information out of the session and putting it into variables to
  2269. // display it in the forms.
  2270. $form_elements = isset($_SESSION['formelements']) ? $_SESSION['formelements'] : null;
  2271. $day = $form_elements['day'];
  2272. $month = $form_elements['month'];
  2273. $year = $form_elements['year'];
  2274. $hours = $form_elements['hour'];
  2275. $minutes = $form_elements['minutes'];
  2276. $end_day = $form_elements['end_day'];
  2277. $end_month = $form_elements['end_month'];
  2278. $end_year = $form_elements['end_year'];
  2279. $end_hours = $form_elements['end_hours'];
  2280. $end_minutes = $form_elements['end_minutes'];
  2281. $title = $form_elements['title'];
  2282. $content = $form_elements['content'];
  2283. $id = $form_elements['id'];
  2284. $to = $form_elements['to'];
  2285. $repeat = $form_elements['repeat'];
  2286. }
  2287. // switching the send to all/send to groups/send to users
  2288. if (isset($_POST['To']) && $_POST['To']) {
  2289. $day = $_POST['fday'];
  2290. $month = $_POST['fmonth'];
  2291. $year = $_POST['fyear'];
  2292. $hours = $_POST['fhour'];
  2293. $minutes = $_POST['fminute'];
  2294. $end_day = $_POST['end_fday'];
  2295. $end_month = $_POST['end_fmonth'];
  2296. $end_year = $_POST['end_fyear'];
  2297. $end_hours = $_POST['end_fhour'];
  2298. $end_minutes = $_POST['end_fminute'];
  2299. $title = $_POST['title'];
  2300. $content = $_POST['content'];
  2301. // the invisible fields
  2302. $action = $_POST['action'];
  2303. $id = $_POST['id'];
  2304. $repeat = !empty($_POST['repeat']) ? true : false;
  2305. }
  2306. $default_no_empty_end_date = 0;
  2307. $course_info = null;
  2308. // if the id is set then we are editing an agenda item
  2309. if (!empty($id)) {
  2310. $course_info = api_get_course_info();
  2311. $agendaObj = new Agenda();
  2312. if (!empty($course_info)) {
  2313. $agendaObj->set_course($course_info);
  2314. $agendaObj->type = 'course';
  2315. } else {
  2316. if (api_is_platform_admin() && $type == 'platform') {
  2317. $agendaObj->type = 'admin';
  2318. } else {
  2319. $agendaObj->type = 'personal';
  2320. }
  2321. }
  2322. $agendaItem = $agendaObj->get_event($id);
  2323. $title = $agendaItem['title'];
  2324. $content = $agendaItem['description'];
  2325. // start date
  2326. if ($agendaItem['start_date'] != '0000-00-00 00:00:00') {
  2327. $agendaItem['start_date'] = api_get_local_time($agendaItem['start_date']);
  2328. list($datepart, $timepart) = explode(" ", $agendaItem['start_date']);
  2329. list($year, $month, $day) = explode("-", $datepart);
  2330. list($hours, $minutes, $seconds) = explode(":", $timepart);
  2331. }
  2332. // end date
  2333. if (!empty($agendaItem['end_date']) && $agendaItem['end_date'] != '0000-00-00 00:00:00') {
  2334. $agendaItem['end_date'] = api_get_local_time($agendaItem['end_date']);
  2335. list($datepart, $timepart) = explode(" ", $agendaItem['end_date']);
  2336. list($end_year, $end_month, $end_day) = explode("-", $datepart);
  2337. list($end_hours, $end_minutes, $end_seconds) = explode(":", $timepart);
  2338. } else {
  2339. if ($agendaItem['all_day']) {
  2340. $end_year = $year;
  2341. $end_month = $month;
  2342. $end_day = $day;
  2343. $end_hours = $hours;
  2344. $end_minutes = $minutes;
  2345. $end_seconds = $seconds;
  2346. }
  2347. }
  2348. // attachments
  2349. //edit_added_resources("Agenda", $id);
  2350. //$to = $item_2_edit['to'];
  2351. } else {
  2352. $to = load_edit_users(TOOL_CALENDAR_EVENT, $id);
  2353. }
  2354. $content = stripslashes($content);
  2355. $title = stripslashes($title);
  2356. // we start a completely new item, we do not come from the resource linker
  2357. if (isset($_GET['originalresource']) && $_GET['originalresource'] !== "no" and $_GET['action'] == "add") {
  2358. $_SESSION["formelements"] = null;
  2359. unset_session_resources();
  2360. }
  2361. $origin = isset($_GET['origin']) ? Security::remove_XSS($_GET['origin']) : null;
  2362. $course_url = empty($course_info) ? null : api_get_cidreq();
  2363. ?>
  2364. <form class="form-horizontal" enctype="multipart/form-data" action="<?php echo api_get_self().'?type='.Security::remove_XSS($type).'&origin='.$origin.'&'.$course_url."&sort=asc&toolgroup=".api_get_group_id().'&action='.Security::remove_XSS($_GET['action']); ?>" method="post" name="new_calendar_item">
  2365. <input type="hidden" name="id" value="<?php if (isset($id)) echo $id; ?>" />
  2366. <input type="hidden" name="action" value="<?php if (isset($_GET['action'])) echo $_GET['action']; ?>" />
  2367. <input type="hidden" name="id_attach" value="<?php echo isset($_REQUEST['id_attach']) ? intval($_REQUEST['id_attach']) : null; ?>" />
  2368. <input type="hidden" name="sort" value="asc"/>
  2369. <input type="hidden" name="submit_event" value="ok"/>
  2370. <?php
  2371. // The form title
  2372. if (isset($id) AND $id <> '') {
  2373. $form_title = get_lang('ModifyCalendarItem');
  2374. } else {
  2375. $form_title = get_lang('AddCalendarItem');
  2376. }
  2377. echo '<legend>'.$form_title.'</legend>';
  2378. // the title of the agenda item
  2379. echo '<div class="control-group">
  2380. <label class="control-label">
  2381. <span class="form_required">*</span>'.get_lang('ItemTitle').'
  2382. </label>
  2383. <div class="controls">
  2384. <div id="err_title" style="display:none;color:red"></div>
  2385. <input type="text" id="agenda_title" class="span5" name="title" value="';
  2386. if (isset($title)) {
  2387. echo $title;
  2388. }
  2389. echo '" />
  2390. </div>
  2391. </div>';
  2392. // selecting the users / groups
  2393. $group_id = api_get_group_id();
  2394. if (empty($id)) {
  2395. if (isset($group_id) && !empty($group_id)) {
  2396. echo '<input type="hidden" name="selected_form[0]" value="GROUP:'.$group_id.'"/>';
  2397. echo '<input type="hidden" name="To" value="true"/>';
  2398. } else {
  2399. echo '<div class="control-group">
  2400. <label class="control-label">
  2401. '.Display::return_icon('group.png', get_lang('To'), array('align' => 'absmiddle'), ICON_SIZE_SMALL).' '.get_lang('To').'</a>
  2402. </label>
  2403. <div class="controls">';
  2404. show_to_form($to);
  2405. echo '</div>
  2406. </div>';
  2407. }
  2408. }
  2409. // start date and time
  2410. echo '<div class="control-group">';
  2411. echo '<label class="control-label">'.get_lang('StartDate').'</label>
  2412. <div class="controls">
  2413. <div id="err_date" style="display:none;color:red"></div>
  2414. <div id="err_start_date" style="display:none;color:red"></div>';
  2415. ?>
  2416. <select name="fday" onchange="javascript:document.new_calendar_item.end_fday.value=this.value;">
  2417. <?php
  2418. // small loop for filling all the dates
  2419. // 2do: the available dates should be those of the selected month => february is from 1 to 28 (or 29) and not to 31
  2420. foreach (range(1, 31) as $i) {
  2421. // values have to have double digits
  2422. $value = ($i <= 9 ? '0'.$i : $i);
  2423. // the current day is indicated with [] around the date
  2424. if ($value == $day) {
  2425. echo "<option value=\"".$value."\" selected> ".$i." </option>";
  2426. } else {
  2427. echo "<option value=\"$value\">$i</option>";
  2428. }
  2429. }
  2430. ?>
  2431. </select>
  2432. <select name="fmonth" onchange="javascript:document.new_calendar_item.end_fmonth.value=this.value;">
  2433. <?php
  2434. for ($i = 1; $i <= 12; $i++) {
  2435. // values have to have double digits
  2436. if ($i <= 9) {
  2437. $value = "0".$i;
  2438. } else {
  2439. $value = $i;
  2440. }
  2441. if ($value == $month) {
  2442. echo "<option value=\"".$value."\" selected>".$MonthsLong[$i - 1]."</option>";
  2443. } else {
  2444. echo "<option value=\"".$value."\">".$MonthsLong[$i - 1]."</option>";
  2445. }
  2446. }
  2447. ?>
  2448. </select>
  2449. <select name="fyear" onchange="javascript:document.new_calendar_item.end_fyear.value=this.value;">
  2450. <option value="<?php echo ($year - 1); ?>"><?php echo ($year - 1); ?></option>
  2451. <option value="<?php echo $year; ?>" selected="selected"><?php echo $year; ?></option>
  2452. <?php
  2453. for ($i = 1; $i <= 5; $i++) {
  2454. $value = $year + $i;
  2455. echo "<option value=\"$value\">$value</option>";
  2456. }
  2457. ?>
  2458. </select>
  2459. <a href="javascript:openCalendar('new_calendar_item', 'f')">
  2460. <?php Display::display_icon(
  2461. 'calendar_select.gif',
  2462. get_lang('Select'),
  2463. array('style' => 'vertical-align: middle;')
  2464. ); ?>
  2465. </a>
  2466. &nbsp;<?php echo get_lang('StartTime').": "; ?>&nbsp;
  2467. <select name="fhour" onchange="javascript:document.new_calendar_item.end_fhour.value=this.value;">
  2468. <!-- <option value="--">--</option> -->
  2469. <?php
  2470. foreach (range(0, 23) as $i) {
  2471. // values have to have double digits
  2472. $value = ($i <= 9 ? '0'.$i : $i);
  2473. // the current hour is indicated with [] around the hour
  2474. if ($hours == $value) {
  2475. echo "<option value=\"".$value."\" selected> ".$value." </option>";
  2476. } else {
  2477. echo "<option value=\"$value\">$value</option>";
  2478. }
  2479. }
  2480. ?>
  2481. </select>
  2482. <select name="fminute" onchange="javascript:document.new_calendar_item.end_fminute.value=this.value;">
  2483. <!-- <option value="<?php echo $minutes ?>"><?php echo $minutes; ?></option> -->
  2484. <?php
  2485. foreach (range(0, 59) as $i) {
  2486. // values have to have double digits
  2487. $value = ($i <= 9 ? '0'.$i : $i);
  2488. if ($minutes == $value) {
  2489. echo "<option value=\"".$value."\" selected> ".$value." </option>";
  2490. } else {
  2491. echo "<option value=\"$value\">$value</option>";
  2492. }
  2493. }
  2494. ?>
  2495. </select>
  2496. <?php
  2497. echo ' </div>
  2498. </div>';
  2499. echo '<script>
  2500. $(function() {
  2501. $("#empty_end_date").click(function(){
  2502. if ($("#empty_end_date").is(":checked")) {
  2503. $("#end_date_span").hide();
  2504. } else {
  2505. $("#end_date_span").show();
  2506. }
  2507. });';
  2508. if (isset($item_2_edit['end_date']) && $item_2_edit['end_date'] == '0000-00-00 00:00:00') {
  2509. echo '$("#end_date_span").hide();';
  2510. } echo '
  2511. });
  2512. </script>';
  2513. // end date and time
  2514. echo '<span id="end_date_span">';
  2515. echo '<div class="control-group">
  2516. <label class="control-label">
  2517. '.get_lang('EndDate').'
  2518. </label>
  2519. <div class="controls">
  2520. <div id="err_end_date" style="display:none;color:red"></div>';
  2521. ?>
  2522. <select id="end_fday" name="end_fday">
  2523. <?php
  2524. // small loop for filling all the dates
  2525. // 2do: the available dates should be those of the selected month => february is from 1 to 28 (or 29) and not to 31
  2526. foreach (range(1, 31) as $i) {
  2527. // values have to have double digits
  2528. $value = ($i <= 9 ? '0'.$i : $i);
  2529. // the current day is indicated with [] around the date
  2530. if ($value == $end_day) {
  2531. echo "<option value=\"".$value."\" selected> ".$i." </option>";
  2532. } else {
  2533. echo "<option value=\"".$value."\">".$i."</option>";
  2534. }
  2535. }
  2536. ?>
  2537. </select>
  2538. <!-- month: january -> december -->
  2539. <select id="end_fmonth" name="end_fmonth">
  2540. <?php
  2541. foreach (range(1, 12) as $i) {
  2542. // values have to have double digits
  2543. $value = ($i <= 9 ? '0'.$i : $i);
  2544. if ($value == $end_month) {
  2545. echo "<option value=\"".$value."\" selected>".$MonthsLong[$i - 1]."</option>";
  2546. } else {
  2547. echo "<option value=\"".$value."\">".$MonthsLong[$i - 1]."</option>";
  2548. }
  2549. }
  2550. ?>
  2551. </select>
  2552. <select id="end_fyear" name="end_fyear">
  2553. <option value="<?php echo ($end_year - 2) ?>"><?php echo ($end_year - 2) ?></option>
  2554. <option value="<?php echo ($end_year - 1) ?>"><?php echo ($end_year - 1) ?></option>
  2555. <option value="<?php echo $end_year ?>" selected> <?php echo $end_year ?> </option>
  2556. <?php
  2557. for ($i = 1; $i <= 5; $i++) {
  2558. $value = $end_year + $i;
  2559. echo "<option value=\"$value\">$value</option>";
  2560. }
  2561. ?>
  2562. </select>
  2563. <a href="javascript:openCalendar('new_calendar_item', 'end_f')">
  2564. <?php echo Display::span(
  2565. Display::return_icon('calendar_select.gif', get_lang('Select'), array('style' => 'vertical-align: middle;')),
  2566. array('id' => 'end_date_calendar_icon')
  2567. ); ?>
  2568. </a>
  2569. &nbsp;<?php echo get_lang('EndTime').": "; ?>&nbsp;
  2570. <select id="end_fhour" name="end_fhour">
  2571. <!-- <option value="--">--</option> -->
  2572. <?php
  2573. foreach (range(0, 23) as $i) {
  2574. // values have to have double digits
  2575. $value = ($i <= 9 ? '0'.$i : $i);
  2576. // the current hour is indicated with [] around the hour
  2577. if ($end_hours == $value) {
  2578. echo "<option value=\"".$value."\" selected> ".$value." </option>";
  2579. } else {
  2580. echo "<option value=\"".$value."\"> ".$value." </option>";
  2581. }
  2582. }
  2583. ?>
  2584. </select>
  2585. <select id="end_fminute" name="end_fminute">
  2586. <!-- <option value="<?php echo $end_minutes; ?>"><?php echo $end_minutes; ?></option> -->
  2587. <!-- <option value="--">--</option> -->
  2588. <?php
  2589. foreach (range(0, 59) as $i) {
  2590. // values have to have double digits
  2591. //$value = ($i <= 9 ? '0'.$i : $i );
  2592. $value = ($i <= 9 ? '0'.$i : $i);
  2593. if ($end_minutes == $value) {
  2594. echo "<option value=\"".$value."\" selected> ".$value." </option>";
  2595. } else {
  2596. echo "<option value=\"$value\">$value</option>";
  2597. }
  2598. }
  2599. ?>
  2600. </select>
  2601. <?php
  2602. echo ' </div>
  2603. </div> </span>';
  2604. // Repeating the calendar item
  2605. if (empty($id)) {
  2606. //only show repeat fields when adding, not for editing an calendar item
  2607. echo '<div class="control-group">
  2608. <label class="control-label"></label>
  2609. <div class="controls">
  2610. <a href="javascript://" onclick="return plus_repeated_event();"><span id="plus2">
  2611. '.$showImg.'&nbsp;'.get_lang('RepeatEvent').'</span>
  2612. </a>';
  2613. ?>
  2614. <table id="options2" style="display: none;">
  2615. <tr>
  2616. <td>
  2617. <label for="repeat_id">
  2618. <input id="repeat_id" type="checkbox" name="repeat" <?php echo ($repeat ? 'checked="checked"' : ''); ?>/>
  2619. <?php echo get_lang('RepeatEvent'); ?>
  2620. </label></td>
  2621. <td>
  2622. </td>
  2623. </tr>
  2624. <tr>
  2625. <td><label><?php echo get_lang('RepeatType'); ?></label></td>
  2626. <td>
  2627. <select name="repeat_type">
  2628. <option value="daily"><?php echo get_lang('RepeatDaily'); ?></option>
  2629. <option value="weekly"><?php echo get_lang('RepeatWeekly'); ?></option>
  2630. <option value="monthlyByDate"><?php echo get_lang('RepeatMonthlyByDate'); ?></option>
  2631. <!--option value="monthlyByDay"><?php echo get_lang('RepeatMonthlyByDay'); ?></option>
  2632. <option value="monthlyByDayR"><?php echo get_lang('RepeatMonthlyByDayR'); ?></option-->
  2633. <option value="yearly"><?php echo get_lang('RepeatYearly'); ?></option>
  2634. </select>
  2635. </td>
  2636. </tr>
  2637. <tr>
  2638. <td><label><?php echo get_lang('RepeatEnd'); ?></label></td>
  2639. <td>
  2640. <select name="repeat_end_day">
  2641. <?php
  2642. // small loop for filling all the dates
  2643. // 2do: the available dates should be those of the selected month => february is from 1 to 28 (or 29) and not to 31
  2644. foreach (range(1, 31) as $i) {
  2645. // values have to have double digits
  2646. $value = ($i <= 9 ? '0'.$i : $i);
  2647. // the current day is indicated with [] around the date
  2648. if ($value == $end_day) {
  2649. echo "<option value=\"".$value."\" selected> ".$i." </option>";
  2650. } else {
  2651. echo "<option value=\"".$value."\">".$i."</option>";
  2652. }
  2653. }
  2654. ?>
  2655. </select>
  2656. <!-- month: january -> december -->
  2657. <select name="repeat_end_month">
  2658. <?php
  2659. foreach (range(1, 12) as $i) {
  2660. // values have to have double digits
  2661. $value = ($i <= 9 ? '0'.$i : $i);
  2662. if ($value == $end_month + 1) {
  2663. echo '<option value="', $value, '" selected="selected">', $MonthsLong[$i - 1], "</option>";
  2664. } else {
  2665. echo '<option value="', $value, '">', $MonthsLong[$i - 1], "</option>";
  2666. }
  2667. }
  2668. ?>
  2669. </select>
  2670. <select name="repeat_end_year">
  2671. <option value="<?php echo ($end_year - 1) ?>"><?php echo ($end_year - 1) ?></option>
  2672. <option value="<?php echo $end_year ?>" selected> <?php echo $end_year ?> </option>
  2673. <?php
  2674. for ($i = 1; $i <= 5; $i++) {
  2675. $value = $end_year + $i;
  2676. echo "<option value=\"$value\">$value</option>";
  2677. }
  2678. ?>
  2679. </select>
  2680. <a href="javascript:openCalendar('new_calendar_item', 'repeat_end_')">
  2681. <?php Display::display_icon('calendar_select.gif', get_lang('Select'), array('style' => 'vertical-align: middle;')); ?>
  2682. </a>
  2683. </td>
  2684. </tr>
  2685. </table>
  2686. <?php
  2687. echo ' </div>
  2688. </div>';
  2689. }
  2690. if (isset($agendaItem['all_day'])) {
  2691. $checked = null;
  2692. if ($agendaItem['all_day']) {
  2693. $checked = 'checked';
  2694. }
  2695. echo ' <div class="control-group">
  2696. <label class="control-label"></label>
  2697. <div class="controls">
  2698. <input type="checkbox" '.$checked.' name="all_day"/> '.get_lang('AllDay').'
  2699. </div>
  2700. </div>';
  2701. }
  2702. //only show repeat fields if adding, not if editing
  2703. // the main area of the agenda item: the wysiwyg editor
  2704. echo ' <div class="control-group">
  2705. <label class="control-label">
  2706. <span class="form_required">*</span>'.get_lang('Description').'
  2707. </label>
  2708. <div class="controls">';
  2709. $oFCKeditor = new FCKeditor('content');
  2710. $oFCKeditor->Width = '100%';
  2711. $oFCKeditor->Height = '200';
  2712. if (!api_is_allowed_to_edit(null, true)) {
  2713. $oFCKeditor->ToolbarSet = 'AgendaStudent';
  2714. } else {
  2715. $oFCKeditor->ToolbarSet = 'Agenda';
  2716. }
  2717. $oFCKeditor->Value = $content;
  2718. $return = $oFCKeditor->CreateHtml();
  2719. echo $return;
  2720. //echo '<textarea class="span5" rows="4" name="content">'.$content.'</textarea>';
  2721. echo '</div>
  2722. </div>';
  2723. if (isset($agendaObj) && $agendaObj->type == 'course') {
  2724. // File attachment
  2725. echo ' <div class="control-group">
  2726. <label class="control-label">
  2727. '.get_lang('AddAnAttachment').'&nbsp;</label>
  2728. <div class="controls">
  2729. <input type="file" name="user_upload"/> '.get_lang('Comment').' <input name="file_comment" type="text" size="20" />
  2730. </div>
  2731. </div>';
  2732. }
  2733. // the submit button for storing the calendar item
  2734. echo ' <div class="control-group">
  2735. <label class="control-label">
  2736. </label>
  2737. <div class="controls">';
  2738. if (isset($_GET['id'])) {
  2739. $class = 'save';
  2740. $text = get_lang('ModifyEvent');
  2741. } else {
  2742. $class = 'add';
  2743. $text = get_lang('AgendaAdd');
  2744. }
  2745. echo '<button class="'.$class.'" type="button" name="name" onclick="selectAll(document.getElementById(\'selected_form\'),true)">'.$text.'</button>';
  2746. echo ' </div>
  2747. </div>';
  2748. ?>
  2749. </form>
  2750. <?php
  2751. }
  2752. function get_agendaitems($month, $year)
  2753. {
  2754. $items = array();
  2755. $month = Database::escape_string($month);
  2756. $year = Database::escape_string($year);
  2757. //databases of the courses
  2758. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  2759. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2760. $course_info = api_get_course_info();
  2761. $group_memberships = GroupManager :: get_group_ids($course_info['real_id'], api_get_user_id());
  2762. // if the user is administrator of that course we show all the agenda items
  2763. if (api_is_allowed_to_edit(false, true)) {
  2764. //echo "course admin";
  2765. $sqlquery = "SELECT
  2766. DISTINCT agenda.*, item_property.*
  2767. FROM ".$TABLEAGENDA." agenda,
  2768. ".$TABLE_ITEMPROPERTY." item_property
  2769. WHERE agenda.id = item_property.ref
  2770. AND MONTH(agenda.start_date)='".$month."'
  2771. AND YEAR(agenda.start_date)='".$year."'
  2772. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2773. AND item_property.visibility='1'
  2774. GROUP BY agenda.id
  2775. ORDER BY start_date ";
  2776. } // if the user is not an administrator of that course
  2777. else {
  2778. //echo "GEEN course admin";
  2779. if (is_array($group_memberships) && count($group_memberships) > 0) {
  2780. $sqlquery = "SELECT
  2781. agenda.*, item_property.*
  2782. FROM ".$TABLEAGENDA." agenda,
  2783. ".$TABLE_ITEMPROPERTY." item_property
  2784. WHERE agenda.id = item_property.ref
  2785. AND MONTH(agenda.start_date)='".$month."'
  2786. AND YEAR(agenda.start_date)='".$year."'
  2787. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2788. AND ( item_property.to_user_id='".api_get_user_id(
  2789. )."' OR item_property.to_group_id IN (0, ".implode(", ", $group_memberships).") )
  2790. AND item_property.visibility='1'
  2791. ORDER BY start_date ";
  2792. } else {
  2793. $sqlquery = "SELECT
  2794. agenda.*, item_property.*
  2795. FROM ".$TABLEAGENDA." agenda,
  2796. ".$TABLE_ITEMPROPERTY." item_property
  2797. WHERE agenda.id = item_property.ref
  2798. AND MONTH(agenda.start_date)='".$month."'
  2799. AND YEAR(agenda.start_date)='".$year."'
  2800. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2801. AND ( item_property.to_user_id='".api_get_user_id()."' OR item_property.to_group_id='0')
  2802. AND item_property.visibility='1'
  2803. ORDER BY start_date ";
  2804. }
  2805. }
  2806. $mycourse = api_get_course_info();
  2807. $result = Database::query($sqlquery);
  2808. while ($item = Database::fetch_array($result)) {
  2809. $agendaday_string = api_convert_and_format_date($item['start_date'], "%d", date_default_timezone_get());
  2810. $agendaday = intval($agendaday_string);
  2811. $time = api_convert_and_format_date($item['start_date'], TIME_NO_SEC_FORMAT);
  2812. $URL = api_get_path(
  2813. WEB_CODE_PATH
  2814. ).'calendar/agenda.php?cidReq='.$mycourse['id']."&day=$agendaday&month=$month&year=$year#$agendaday"; // RH //Patrick Cool: to highlight the relevant agenda item
  2815. $items[$agendaday][$item['start_time']] .= '<i>'.$time.'</i> <a href="'.$URL.'" title="'.$mycourse['name'].'">'.$mycourse['official_code'].'</a> '.$item['title'].'<br />';
  2816. }
  2817. // sorting by hour for every day
  2818. $agendaitems = array();
  2819. while (list ($agendaday, $tmpitems) = each($items)) {
  2820. sort($tmpitems);
  2821. while (list ($key, $val) = each($tmpitems)) {
  2822. $agendaitems[$agendaday] .= $val;
  2823. }
  2824. }
  2825. return $agendaitems;
  2826. }
  2827. function display_upcoming_events()
  2828. {
  2829. $number_of_items_to_show = (int)api_get_setting('number_of_upcoming_events');
  2830. //databases of the courses
  2831. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  2832. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2833. $mycourse = api_get_course_info();
  2834. $myuser = api_get_user_info();
  2835. $session_id = api_get_session_id();
  2836. $course_id = $mycourse['real_id'];
  2837. $group_memberships = GroupManager :: get_group_ids($mycourse['real_id'], $myuser['user_id']);
  2838. // if the user is administrator of that course we show all the agenda items
  2839. if (api_is_allowed_to_edit(false, true)) {
  2840. //echo "course admin";
  2841. $sqlquery = "SELECT
  2842. DISTINCT agenda.*, item_property.*
  2843. FROM ".$TABLEAGENDA." agenda,
  2844. ".$TABLE_ITEMPROPERTY." item_property
  2845. WHERE
  2846. agenda.c_id = $course_id AND
  2847. item_property.c_id = $course_id AND
  2848. agenda.id = item_property.ref
  2849. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2850. AND item_property.visibility='1'
  2851. AND agenda.start_date > NOW()
  2852. AND session_id = '".$session_id."'
  2853. GROUP BY agenda.id
  2854. ORDER BY start_date ";
  2855. } // if the user is not an administrator of that course
  2856. else {
  2857. //echo "GEEN course admin";
  2858. if (is_array($group_memberships) and count($group_memberships) > 0) {
  2859. $sqlquery = "SELECT
  2860. agenda.*, item_property.*
  2861. FROM ".$TABLEAGENDA." agenda,
  2862. ".$TABLE_ITEMPROPERTY." item_property
  2863. WHERE
  2864. agenda.c_id = $course_id AND
  2865. item_property.c_id = $course_id AND
  2866. agenda.id = item_property.ref
  2867. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2868. AND ( item_property.to_user_id='".$myuser['user_id']."' OR item_property.to_group_id IN (0, ".implode(
  2869. ", ",
  2870. $group_memberships
  2871. ).") )
  2872. AND item_property.visibility='1'
  2873. AND agenda.start_date > NOW()
  2874. AND session_id = '".$session_id."'
  2875. ORDER BY start_date ";
  2876. } else {
  2877. $sqlquery = "SELECT
  2878. agenda.*, item_property.*
  2879. FROM ".$TABLEAGENDA." agenda,
  2880. ".$TABLE_ITEMPROPERTY." item_property
  2881. WHERE
  2882. agenda.c_id = $course_id AND
  2883. item_property.c_id = $course_id AND
  2884. agenda.id = item_property.ref
  2885. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2886. AND ( item_property.to_user_id='".$myuser['user_id']."' OR item_property.to_group_id='0')
  2887. AND item_property.visibility='1'
  2888. AND agenda.start_date > NOW()
  2889. AND session_id = '".$session_id."'
  2890. ORDER BY start_date ";
  2891. }
  2892. }
  2893. $result = Database::query($sqlquery);
  2894. $counter = 0;
  2895. if (Database::num_rows($result) > 0) {
  2896. echo '<h4>'.get_lang('UpcomingEvent').'</h4><br />';
  2897. while ($item = Database::fetch_array($result, 'ASSOC')) {
  2898. if ($counter < $number_of_items_to_show) {
  2899. echo api_get_local_time($item['start_date']), ' - ', $item['title'], '<br />';
  2900. $counter++;
  2901. }
  2902. }
  2903. }
  2904. }
  2905. /**
  2906. * This function calculates the startdate of the week (monday)
  2907. * and the enddate of the week (sunday)
  2908. * and returns it as an array
  2909. * @todo check if this function is correct
  2910. */
  2911. function calculate_start_end_of_week($week_number, $year)
  2912. {
  2913. // determine the start and end date
  2914. // step 1: we calculate a timestamp for a day in this week
  2915. //@todo Why ($week_number - 1) ?
  2916. //$random_day_in_week = mktime(0, 0, 0, 1, 1, $year) + ($week_number-1) * (7 * 24 * 60 * 60); // we calculate a random day in this week
  2917. $random_day_in_week = mktime(
  2918. 0,
  2919. 0,
  2920. 0,
  2921. 1,
  2922. 1,
  2923. $year
  2924. ) + ($week_number) * (7 * 24 * 60 * 60); // we calculate a random day in this week
  2925. // step 2: we which day this is (0=sunday, 1=monday, ...)
  2926. $number_day_in_week = date('w', $random_day_in_week);
  2927. // step 3: we calculate the timestamp of the monday of the week we are in
  2928. $start_timestamp = $random_day_in_week - (($number_day_in_week - 1) * 24 * 60 * 60);
  2929. // step 4: we calculate the timestamp of the sunday of the week we are in
  2930. $end_timestamp = $random_day_in_week + ((7 - $number_day_in_week + 1) * 24 * 60 * 60) - 3600;
  2931. // step 5: calculating the start_day, end_day, start_month, end_month, start_year, end_year
  2932. $start_day = date('j', $start_timestamp);
  2933. $start_month = date('n', $start_timestamp);
  2934. $start_year = date('Y', $start_timestamp);
  2935. $end_day = date('j', $end_timestamp);
  2936. $end_month = date('n', $end_timestamp);
  2937. $end_year = date('Y', $end_timestamp);
  2938. $start_end_array['start']['day'] = $start_day;
  2939. $start_end_array['start']['month'] = $start_month;
  2940. $start_end_array['start']['year'] = $start_year;
  2941. $start_end_array['end']['day'] = $end_day;
  2942. $start_end_array['end']['month'] = $end_month;
  2943. $start_end_array['end']['year'] = $end_year;
  2944. return $start_end_array;
  2945. }
  2946. /**
  2947. * Show the mini calendar of the given month
  2948. */
  2949. function display_daycalendar($agendaitems, $day, $month, $year, $weekdaynames, $monthName)
  2950. {
  2951. global $DaysShort, $DaysLong, $course_path;
  2952. global $MonthsLong;
  2953. global $query;
  2954. // timestamp of today
  2955. $today = mktime();
  2956. $nextday = $today + (24 * 60 * 60);
  2957. $previousday = $today - (24 * 60 * 60);
  2958. // the week number of the year
  2959. $week_number = date("W", $today);
  2960. // if we moved to the next / previous day we have to recalculate the $today variable
  2961. if (isset($_GET['day'])) {
  2962. $today = mktime(0, 0, 0, $month, $day, $year);
  2963. $nextday = $today + (24 * 60 * 60);
  2964. $previousday = $today - (24 * 60 * 60);
  2965. $week_number = date("W", $today);
  2966. }
  2967. // calculating the start date of the week
  2968. // the date of the monday of this week is the timestamp of today minus
  2969. // number of days that have already passed this week * 24 hours * 60 minutes * 60 seconds
  2970. $current_day = date("j", $today); // Day of the month without leading zeros (1 to 31) of today
  2971. $day_of_the_week = date(
  2972. "w",
  2973. $today
  2974. ); // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) of today
  2975. // we are loading all the calendar items of all the courses for today
  2976. echo "<table class=\"data_table\">";
  2977. // the forward and backwards url
  2978. $course_code = isset($_GET['courseCode']) ? Security::remove_XSS($_GET['courseCode']) : null;
  2979. $backwardsURL = api_get_self()."?coursePath=".urlencode(
  2980. $course_path
  2981. )."&courseCode=".$course_code."&action=view&view=day&day=".date("j", $previousday)."&month=".date(
  2982. "n",
  2983. $previousday
  2984. )."&year=".date("Y", $previousday);
  2985. $forewardsURL = api_get_self()."?coursePath=".urlencode(
  2986. $course_path
  2987. )."&courseCode=".$course_code."&action=view&view=day&day=".date("j", $nextday)."&month=".date(
  2988. "n",
  2989. $nextday
  2990. )."&year=".date("Y", $nextday);
  2991. // The title row containing the day
  2992. echo "<tr>", "<th width=\"10%\"><a href=\"", $backwardsURL, "\">".Display::return_icon(
  2993. 'action_prev.png',
  2994. get_lang('Previous')
  2995. )."</a></th>", "<th>";
  2996. echo $DaysLong[$day_of_the_week]." ".date("j", $today)." ".$MonthsLong[date("n", $today) - 1]." ".date("Y", $today);
  2997. echo "</th>";
  2998. echo "<th width=\"10%\"><a href=\"", $forewardsURL, "\">".Display::return_icon(
  2999. 'action_next.png',
  3000. get_lang('Next')
  3001. )."</a></th>";
  3002. echo "</tr>";
  3003. // From 0 to 5h
  3004. $class = "class=\"row_even\"";
  3005. echo "<tr $class>";
  3006. echo ("<td valign=\"top\" width=\"75\">0:00 ".get_lang("HourShort")." - 4:30 ".get_lang("HourShort")."</td>");
  3007. echo "<td $class valign=\"top\" colspan=\"2\">";
  3008. for ($i = 0; $i < 10; $i++) {
  3009. if (isset($agendaitems[$i])) {
  3010. if (is_array($agendaitems[$i])) {
  3011. foreach ($agendaitems[$i] as $key => $value) {
  3012. echo $value;
  3013. }
  3014. } else {
  3015. echo $agendaitems[$i];
  3016. }
  3017. }
  3018. }
  3019. echo "</td>";
  3020. echo "</tr>";
  3021. // the rows for each half an hour
  3022. for ($i = 10; $i < 48; $i++) {
  3023. if ($i % 2 == 0) {
  3024. $class = "class=\"row_even\"";
  3025. } else {
  3026. $class = "class=\"row_odd\"";
  3027. }
  3028. echo "<tr $class>";
  3029. echo "";
  3030. if ($i % 2 == 0) {
  3031. echo ("<td valign=\"top\" width=\"75\">".(($i) / 2)." ".get_lang("HourShort")." 00</td>");
  3032. } else {
  3033. echo ("<td valign=\"top\" width=\"75\">".((($i) / 2) - (1 / 2))." ".get_lang("HourShort")." 30</td>");
  3034. }
  3035. echo "<td $class valign=\"top\" colspan=\"2\">";
  3036. if (isset($agendaitems[$i])) {
  3037. if (is_array($agendaitems[$i])) {
  3038. foreach ($agendaitems[$i] as $key => $value) {
  3039. echo $value;
  3040. }
  3041. } else {
  3042. echo $agendaitems[$i];
  3043. }
  3044. }
  3045. echo "</td>";
  3046. echo "</tr>";
  3047. }
  3048. echo "</table>";
  3049. }
  3050. /**
  3051. * Display the weekly view of the calendar
  3052. */
  3053. function display_weekcalendar($agendaitems, $month, $year, $weekdaynames, $monthName)
  3054. {
  3055. global $DaysShort, $course_path;
  3056. global $MonthsLong;
  3057. // timestamp of today
  3058. $today = time();
  3059. $day_of_the_week = date("w", $today);
  3060. $thisday_of_the_week = date("w", $today);
  3061. // the week number of the year
  3062. $week_number = date("W", $today);
  3063. $thisweek_number = $week_number;
  3064. // if we moved to the next / previous week we have to recalculate the $today variable
  3065. if (!isset($_GET['week'])) {
  3066. $week_number = date("W", $today);
  3067. } else {
  3068. $week_number = intval($_GET['week']);
  3069. }
  3070. // calculating the start date of the week
  3071. // the date of the monday of this week is the timestamp of today minus
  3072. // number of days that have already passed this week * 24 hours * 60 minutes * 60 seconds
  3073. $current_day = date("j", $today); // Day of the month without leading zeros (1 to 31) of today
  3074. $day_of_the_week = date(
  3075. "w",
  3076. $today
  3077. ); // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) of today
  3078. //Using the same script to calculate the start/end of a week
  3079. $start_end = calculate_start_end_of_week($week_number, $year);
  3080. $timestamp_first_date_of_week = mktime(
  3081. 0,
  3082. 0,
  3083. 0,
  3084. $start_end['start']['month'],
  3085. $start_end['start']['day'],
  3086. $start_end['start']['year']
  3087. );
  3088. $timestamp_last_date_of_week = mktime(
  3089. 0,
  3090. 0,
  3091. 0,
  3092. $start_end['end']['month'],
  3093. $start_end['end']['day'],
  3094. $start_end['end']['year']
  3095. );
  3096. $backwardsURL = api_get_self()."?coursePath=".urlencode($course_path)."&courseCode=".api_get_course_id(
  3097. )."&action=view&view=week&week=".($week_number - 1);
  3098. $forewardsURL = api_get_self()."?coursePath=".urlencode($course_path)."&courseCode=".api_get_course_id(
  3099. )."&action=view&view=week&week=".($week_number + 1);
  3100. echo '<table class="data_table">';
  3101. // The title row containing the the week information (week of the year (startdate of week - enddate of week)
  3102. echo '<tr>';
  3103. echo "<th width=\"10%\"><a href=\"", $backwardsURL, "\">".Display::return_icon(
  3104. 'action_prev.png',
  3105. get_lang('Previous')
  3106. )."</a></th>";
  3107. echo "<th colspan=\"5\">".get_lang("Week")." ".$week_number;
  3108. echo " (".$DaysShort['1']." ".date("j", $timestamp_first_date_of_week)." ".$MonthsLong[date(
  3109. "n",
  3110. $timestamp_first_date_of_week
  3111. ) - 1]." ".date("Y", $timestamp_first_date_of_week)." - ".$DaysShort['0']." ".date(
  3112. "j",
  3113. $timestamp_last_date_of_week
  3114. )." ".$MonthsLong[date("n", $timestamp_last_date_of_week) - 1]." ".date("Y", $timestamp_last_date_of_week).')';
  3115. echo "</th>";
  3116. echo "<th width=\"10%\"><a href=\"", $forewardsURL, "\">".Display::return_icon(
  3117. 'action_next.png',
  3118. get_lang('Next')
  3119. )."</a></th>", "</tr>";
  3120. // The second row containing the short names of the days of the week
  3121. echo "<tr>";
  3122. //Printing the week days
  3123. // this is the Day of the month without leading zeros (1 to 31) of the monday of this week
  3124. $tmp_timestamp = $timestamp_first_date_of_week;
  3125. for ($ii = 1; $ii < 8; $ii++) {
  3126. $is_today = ($ii == $thisday_of_the_week AND (!isset($_GET['week']) OR $_GET['week'] == $thisweek_number));
  3127. echo "<td class=\"weekdays\">";
  3128. if ($is_today) {
  3129. echo "<font color=#CC3300>";
  3130. }
  3131. echo $DaysShort[$ii % 7]." ".date("j", $tmp_timestamp)." ".$MonthsLong[date("n", $tmp_timestamp) - 1];
  3132. if ($is_today) {
  3133. echo "</font>";
  3134. }
  3135. echo "</td>";
  3136. // we 24 hours * 60 minutes * 60 seconds to the $tmp_timestamp
  3137. $array_tmp_timestamp[] = $tmp_timestamp;
  3138. $tmp_timestamp = $tmp_timestamp + (24 * 60 * 60);
  3139. }
  3140. echo "</tr>";
  3141. // The table cells containing all the entries for that day
  3142. echo "<tr>";
  3143. $counter = 0;
  3144. foreach ($array_tmp_timestamp as $key => $value) {
  3145. if ($counter < 5) {
  3146. $class = "class=\"days_week\"";
  3147. } else {
  3148. $class = "class=\"days_weekend\"";
  3149. }
  3150. if ($counter == $thisday_of_the_week - 1 AND (!isset($_GET['week']) OR $_GET['week'] == $thisweek_number)) {
  3151. $class = "class=\"days_today\"";
  3152. }
  3153. echo "<td ".$class.">";
  3154. $data = isset($agendaitems[date('j', $value)]) ? $agendaitems[date('j', $value)] : null;
  3155. echo "<span class=\"agendaitem\">".$data."&nbsp;</span> ";
  3156. echo "</td>";
  3157. $counter++;
  3158. }
  3159. echo "</tr>";
  3160. echo "</table>";
  3161. }
  3162. /**
  3163. * Show the monthcalender of the given month
  3164. */
  3165. function get_day_agendaitems($courses_dbs, $month, $year, $day)
  3166. {
  3167. global $setting_agenda_link;
  3168. $items = array();
  3169. // get agenda-items for every course
  3170. //$query=Database::query($sql_select_courses);
  3171. foreach ($courses_dbs as $key => $array_course_info) {
  3172. //echo $array_course_info['db'];
  3173. //databases of the courses
  3174. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  3175. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  3176. // getting all the groups of the user for the current course
  3177. $group_memberships = GroupManager :: get_group_ids($array_course_info['real_id'], api_get_user_id());
  3178. $course_user_status = CourseManager::get_user_in_course_status(api_get_user_id(), $array_course_info['code']);
  3179. $start_filter = $year."-".$month."-".$day." 00:00:00";
  3180. $start_filter = api_get_utc_datetime($start_filter);
  3181. $end_filter = $year."-".$month."-".$day." 23:59:59";
  3182. $end_filter = api_get_utc_datetime($end_filter);
  3183. // if the user is administrator of that course we show all the agenda items
  3184. if ($course_user_status == '1') {
  3185. //echo "course admin";
  3186. $sqlquery = "SELECT
  3187. DISTINCT agenda.*, item_property.*
  3188. FROM ".$TABLEAGENDA." agenda,
  3189. ".$TABLE_ITEMPROPERTY." item_property
  3190. WHERE agenda.id = item_property.ref
  3191. AND start_date>='".$start_filter."' AND start_date<='".$end_filter."'
  3192. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  3193. AND item_property.visibility='1'
  3194. GROUP BY agenda.id
  3195. ORDER BY start_date ";
  3196. } // if the user is not an administrator of that course
  3197. else {
  3198. //echo "course admin";
  3199. if (is_array($group_memberships) && count($group_memberships) > 0) {
  3200. $sqlquery = "SELECT
  3201. agenda.*, item_property.*
  3202. FROM ".$TABLEAGENDA." agenda,
  3203. ".$TABLE_ITEMPROPERTY." item_property
  3204. WHERE agenda.id = item_property.ref
  3205. AND start_date>='".$start_filter."' AND start_date<='".$end_filter."'
  3206. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  3207. AND ( item_property.to_user_id='".api_get_user_id(
  3208. )."' OR item_property.to_group_id IN (0, ".implode(", ", $group_memberships).") )
  3209. AND item_property.visibility='1'
  3210. ORDER BY start_date ";
  3211. } else {
  3212. $sqlquery = "SELECT
  3213. agenda.*, item_property.*
  3214. FROM ".$TABLEAGENDA." agenda,
  3215. ".$TABLE_ITEMPROPERTY." item_property
  3216. WHERE agenda.id = item_property.ref
  3217. AND start_date>='".$start_filter."' AND start_date<='".$end_filter."'
  3218. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  3219. AND ( item_property.to_user_id='".api_get_user_id()."' OR item_property.to_group_id='0')
  3220. AND item_property.visibility='1'
  3221. ORDER BY start_date ";
  3222. }
  3223. }
  3224. $result = Database::query($sqlquery);
  3225. while ($item = Database::fetch_array($result)) {
  3226. // in the display_daycalendar function we use $i (ranging from 0 to 47) for each halfhour
  3227. // we want to know for each agenda item for this day to wich halfhour it must be assigned
  3228. $item['start_date'] = api_get_local_time($item['start_date']);
  3229. $time_minute = api_format_date($item['start_date'], TIME_NO_SEC_FORMAT);
  3230. list ($datepart, $timepart) = explode(" ", $item['start_date']);
  3231. list ($year, $month, $day) = explode("-", $datepart);
  3232. list ($hours, $minutes, $seconds) = explode(":", $timepart);
  3233. $halfhour = 2 * $hours;
  3234. if ($minutes >= '30') {
  3235. $halfhour = $halfhour + 1;
  3236. }
  3237. if ($setting_agenda_link == 'coursecode') {
  3238. $title = $array_course_info['title'];
  3239. $agenda_link = Text::cut($title, 14, true);
  3240. } else {
  3241. $agenda_link = Display::return_icon('course_home.png', '&nbsp;', '', ICON_SIZE_SMALL);
  3242. }
  3243. $URL = api_get_path(WEB_CODE_PATH).'calendar/agenda.php?cidReq='.urlencode(
  3244. $array_course_info["code"]
  3245. )."&day=$day&month=$month&year=$year#$day"; // RH //Patrick Cool: to highlight the relevant agenda item
  3246. $items[$halfhour][] .= "<i>$time_minute</i> <a href=\"$URL\" title=\"".$array_course_info['title']."\">".$agenda_link."</a> ".$item['title']."<br />";
  3247. }
  3248. }
  3249. // sorting by hour for every day
  3250. $agendaitems = array();
  3251. while (list($agendaday, $tmpitems) = each($items)) {
  3252. sort($tmpitems);
  3253. while (list($key, $val) = each($tmpitems)) {
  3254. $agendaitems[$agendaday] .= $val;
  3255. }
  3256. }
  3257. return $agendaitems;
  3258. }
  3259. /**
  3260. * Return agenda items of the week
  3261. */
  3262. function get_week_agendaitems($courses_dbs, $month, $year, $week = '')
  3263. {
  3264. global $setting_agenda_link;
  3265. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  3266. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  3267. $items = array();
  3268. // The default value of the week
  3269. if ($week == '') {
  3270. $week_number = date("W", time());
  3271. } else {
  3272. $week_number = $week;
  3273. }
  3274. $start_end = calculate_start_end_of_week($week_number, $year);
  3275. $start_filter = $start_end['start']['year']."-".$start_end['start']['month']."-".$start_end['start']['day']." 00:00:00";
  3276. $start_filter = api_get_utc_datetime($start_filter);
  3277. $end_filter = $start_end['end']['year']."-".$start_end['end']['month']."-".$start_end['end']['day']." 23:59:59";
  3278. $end_filter = api_get_utc_datetime($end_filter);
  3279. // get agenda-items for every course
  3280. foreach ($courses_dbs as $key => $array_course_info) {
  3281. //databases of the courses
  3282. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  3283. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  3284. // getting all the groups of the user for the current course
  3285. $group_memberships = GroupManager :: get_group_ids($array_course_info["real_id"], api_get_user_id());
  3286. $user_course_status = CourseManager::get_user_in_course_status(api_get_user_id(), $array_course_info["code"]);
  3287. // if the user is administrator of that course we show all the agenda items
  3288. if ($user_course_status == '1') {
  3289. //echo "course admin";
  3290. $sqlquery = "SELECT
  3291. DISTINCT a.*, i.*
  3292. FROM ".$TABLEAGENDA." a,
  3293. ".$TABLE_ITEMPROPERTY." i
  3294. WHERE a.id = i.ref
  3295. AND a.start_date>='".$start_filter."' AND a.start_date<='".$end_filter."'
  3296. AND i.tool='".TOOL_CALENDAR_EVENT."'
  3297. AND i.visibility='1'
  3298. GROUP BY a.id
  3299. ORDER BY a.start_date";
  3300. } // if the user is not an administrator of that course
  3301. else {
  3302. //echo "GEEN course admin";
  3303. if (is_array($group_memberships) && count($group_memberships) > 0) {
  3304. $sqlquery = "SELECT
  3305. a.*, i.*
  3306. FROM ".$TABLEAGENDA." a,
  3307. ".$TABLE_ITEMPROPERTY." i
  3308. WHERE a.id = i.ref
  3309. AND a.start_date>='".$start_filter."' AND a.start_date<='".$end_filter."'
  3310. AND i.tool='".TOOL_CALENDAR_EVENT."'
  3311. AND ( i.to_user_id='".api_get_user_id()."' OR i.to_group_id IN (0, ".implode(
  3312. ", ",
  3313. $group_memberships
  3314. ).") )
  3315. AND i.visibility='1'
  3316. ORDER BY a.start_date";
  3317. } else {
  3318. $sqlquery = "SELECT
  3319. a.*, i.*
  3320. FROM ".$TABLEAGENDA." a,
  3321. ".$TABLE_ITEMPROPERTY." i
  3322. WHERE a.id = i.ref
  3323. AND a.start_date>='".$start_filter."' AND a.start_date<='".$end_filter."'
  3324. AND i.tool='".TOOL_CALENDAR_EVENT."'
  3325. AND ( i.to_user_id='".api_get_user_id()."' OR i.to_group_id='0')
  3326. AND i.visibility='1'
  3327. ORDER BY a.start_date";
  3328. }
  3329. }
  3330. //echo "<pre>".$sqlquery."</pre>";
  3331. // $sqlquery = "SELECT * FROM $agendadb WHERE (DAYOFMONTH(day)>='$start_day' AND DAYOFMONTH(day)<='$end_day')
  3332. // AND (MONTH(day)>='$start_month' AND MONTH(day)<='$end_month')
  3333. // AND (YEAR(day)>='$start_year' AND YEAR(day)<='$end_year')";
  3334. $result = Database::query($sqlquery);
  3335. while ($item = Database::fetch_array($result)) {
  3336. $agendaday_string = api_convert_and_format_date($item['start_date'], "%d", date_default_timezone_get());
  3337. $agendaday = intval($agendaday_string);
  3338. $start_time = api_convert_and_format_date($item['start_date'], TIME_NO_SEC_FORMAT);
  3339. $end_time = api_convert_and_format_date($item['end_date'], DATE_TIME_FORMAT_LONG);
  3340. if ($setting_agenda_link == 'coursecode') {
  3341. $title = $array_course_info['title'];
  3342. $agenda_link = Text::cut($title, 14, true);
  3343. } else {
  3344. $agenda_link = Display::return_icon('course_home.png', '&nbsp;', '', ICON_SIZE_SMALL);
  3345. }
  3346. $URL = api_get_path(WEB_CODE_PATH)."calendar/agenda.php?cidReq=".urlencode(
  3347. $array_course_info["code"]
  3348. )."&day=$agendaday&month=$month&year=$year#$agendaday"; // RH //Patrick Cool: to highlight the relevant agenda item
  3349. //Display the events in agenda
  3350. $content = "<i>$start_time - $end_time</i> <a href=\"$URL\" title=\"".$array_course_info["title"]."\"> <br />".$agenda_link."</a>";
  3351. $content .= "<div>".$item['title']."</div><br>";
  3352. $items[$agendaday][$item['start_date']] .= $content;
  3353. }
  3354. }
  3355. $agendaitems = array();
  3356. // sorting by hour for every day
  3357. while (list ($agendaday, $tmpitems) = each($items)) {
  3358. sort($tmpitems);
  3359. while (list ($key, $val) = each($tmpitems)) {
  3360. $agendaitems[$agendaday] .= $val;
  3361. }
  3362. }
  3363. return $agendaitems;
  3364. }
  3365. /**
  3366. * Get repeated events of a course between two dates (timespan of a day).
  3367. * Returns an array containing the events
  3368. * @param string Course info array (as returned by api_get_course_info())
  3369. * @param int UNIX timestamp of span start. Defaults 0, later transformed into today's start
  3370. * @param int UNIX timestamp. Defaults to 0, later transformed into today's end
  3371. * @param array A set of parameters to alter the SQL query
  3372. * @return array [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  3373. */
  3374. function get_repeated_events_day_view($course_info, $start = 0, $end = 0, $params)
  3375. {
  3376. $events = array();
  3377. //initialise all values
  3378. $y = 0;
  3379. $m = 0;
  3380. $d = 0;
  3381. //block $end if higher than 2038 -- PHP doesn't go past that
  3382. if ($end > 2145934800) {
  3383. $end = 2145934800;
  3384. }
  3385. if ($start == 0 or $end == 0) {
  3386. $y = date('Y');
  3387. $m = date('m');
  3388. $d = date('j');
  3389. }
  3390. if ($start == 0) {
  3391. $start = mktime(0, 0, 0, $m, $d, $y);
  3392. }
  3393. $db_start = date('Y-m-d H:i:s', $start);
  3394. if ($end == 0) {
  3395. $end = mktime(23, 59, 59, $m, $d, $y);
  3396. }
  3397. //$db_end = date('Y-m-d H:i:s',$end);
  3398. $t_cal = Database::get_course_table(TABLE_AGENDA, $course_info['dbName']);
  3399. $t_cal_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT, $course_info['dbName']);
  3400. $t_ip = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_info['dbName']);
  3401. $sql = "SELECT c.id, c.title, c.content, ".
  3402. " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, ".
  3403. " cr.cal_type, cr.cal_end ".
  3404. " FROM $t_cal c, $t_cal_repeat cr, $t_ip as item_property ".
  3405. " WHERE cr.cal_end >= $start ".
  3406. " AND cr.cal_id = c.id ".
  3407. " AND item_property.ref = c.id ".
  3408. " AND item_property.tool = '".TOOL_CALENDAR_EVENT."' ".
  3409. " AND c.start_date <= '$db_start' "
  3410. .(!empty($params['conditions']) ? $params['conditions'] : '')
  3411. .(!empty($params['groupby']) ? ' GROUP BY '.$params['groupby'] : '')
  3412. .(!empty($params['orderby']) ? ' ORDER BY '.$params['orderby'] : '');
  3413. $res = Database::query($sql);
  3414. if (Database::num_rows($res) > 0) {
  3415. while ($row = Database::fetch_array($res)) {
  3416. $orig_start = $row['orig_start'];
  3417. $orig_end = $row['orig_end'];
  3418. $repeat_type = $row['cal_type'];
  3419. switch ($repeat_type) {
  3420. case 'daily':
  3421. //we are in the daily view, so if this element is repeated daily and
  3422. //the repetition is still active today (which is a condition of the SQL query)
  3423. //then the event happens today. Just build today's timestamp for start and end
  3424. $time_orig_h = date('H', $orig_start);
  3425. $time_orig_m = date('i', $orig_start);
  3426. $time_orig_s = date('s', $orig_start);
  3427. $int_time = (($time_orig_h * 60) + $time_orig_m) * 60 + $time_orig_s; //time in seconds since 00:00:00
  3428. $span = $orig_end - $orig_start; //total seconds between start and stop of original event
  3429. $current_start = $start + $int_time; //unixtimestamp start of today's event
  3430. $current_stop = $start + $int_time + $span; //unixtimestamp stop of today's event
  3431. $events[] = array(
  3432. $course_info['id'],
  3433. $row['id'],
  3434. $current_start,
  3435. $current_stop,
  3436. $row['title'],
  3437. $row['content']
  3438. );
  3439. break;
  3440. case 'weekly':
  3441. $time_orig = date('Y/n/W/j/N/G/i/s', $orig_start);
  3442. list($y_orig, $m_orig, $w_orig, $d_orig, $dw_orig, $h_orig, $n_orig, $s_orig) = split(
  3443. '/',
  3444. $time_orig
  3445. );
  3446. $time_now = date('Y/n/W/j/N/G/i/s', $end);
  3447. list($y_now, $m_now, $w_now, $d_now, $dw_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  3448. if ((($y_now > $y_orig) OR (($y_now == $y_orig) && ($w_now > $w_orig))) && ($dw_orig == $dw_now)) { //if the event is after the original (at least one week) and the day of the week is the same
  3449. $time_orig_end = date('Y/n/W/j/N/G/i/s', $orig_end);
  3450. list($y_orig_e, $m_orig_e, $w_orig_e, $d_orig_e, $dw_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split(
  3451. '/',
  3452. $time_orig_end
  3453. );
  3454. $events[] = array(
  3455. $course_info['id'],
  3456. $row['id'],
  3457. mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now),
  3458. mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now),
  3459. $row['title'],
  3460. $row['content']
  3461. );
  3462. }
  3463. break;
  3464. case 'monthlyByDate':
  3465. $time_orig = date('Y/n/j/G/i/s', $orig_start);
  3466. list($y_orig, $m_orig, $d_orig, $h_orig, $n_orig, $s_orig) = split('/', $time_orig);
  3467. $time_now = date('Y/n/j/G/i/s', $end);
  3468. list($y_now, $m_now, $d_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  3469. if ((($y_now > $y_orig) OR (($y_now == $y_orig) && ($m_now > $m_orig))) && ($d_orig == $d_now)) {
  3470. $time_orig_end = date('Y/n/j/G/i/s', $orig_end);
  3471. list($y_orig_e, $m_orig_e, $d_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split(
  3472. '/',
  3473. $time_orig_end
  3474. );
  3475. $events[] = array(
  3476. $course_info['id'],
  3477. $row['id'],
  3478. mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now),
  3479. mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now),
  3480. $row['title'],
  3481. $row['content']
  3482. );
  3483. }
  3484. break;
  3485. case 'monthlyByDayR':
  3486. //not implemented yet
  3487. break;
  3488. case 'monthlyByDay':
  3489. //not implemented yet
  3490. break;
  3491. case 'yearly':
  3492. $time_orig = date('Y/n/j/z/G/i/s', $orig_start);
  3493. list($y_orig, $m_orig, $d_orig, $dy_orig, $h_orig, $n_orig, $s_orig) = split('/', $time_orig);
  3494. $time_now = date('Y/n/j/z/G/i/s', $end);
  3495. list($y_now, $m_now, $d_now, $dy_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  3496. if (($y_now > $y_orig) && ($dy_orig == $dy_now)) {
  3497. $time_orig_end = date('Y/n/j/G/i/s', $orig_end);
  3498. list($y_orig_e, $m_orig_e, $d_orig_e, $dy_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split(
  3499. '/',
  3500. $time_orig_end
  3501. );
  3502. $events[] = array(
  3503. $course_info['id'],
  3504. $row['id'],
  3505. mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now),
  3506. mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now),
  3507. $row['title'],
  3508. $row['content']
  3509. );
  3510. }
  3511. break;
  3512. default:
  3513. break;
  3514. }
  3515. }
  3516. }
  3517. return $events;
  3518. }
  3519. /**
  3520. * (This function is not been use in the code)
  3521. * Get repeated events of a course between two dates (timespan of a week).
  3522. * Returns an array containing the events
  3523. * @param string Course info array (as returned by api_get_course_info())
  3524. * @param int UNIX timestamp of span start. Defaults 0, later transformed into today's start
  3525. * @param int UNIX timestamp. Defaults to 0, later transformed into today's end
  3526. * @param array A set of parameters to alter the SQL query
  3527. * @return array [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  3528. */
  3529. function get_repeated_events_week_view($course_info, $start = 0, $end = 0, $params)
  3530. {
  3531. $events = array();
  3532. //block $end if higher than 2038 -- PHP doesn't go past that
  3533. if ($end > 2145934800) {
  3534. $end = 2145934800;
  3535. }
  3536. //initialise all values
  3537. $y = 0;
  3538. $m = 0;
  3539. $d = 0;
  3540. if ($start == 0 or $end == 0) {
  3541. $time = time();
  3542. $dw = date('w', $time);
  3543. $week_start = $time - (($dw - 1) * 86400);
  3544. $y = date('Y', $week_start);
  3545. $m = date('m', $week_start);
  3546. $d = date('j', $week_start);
  3547. $w = date('W', $week_start);
  3548. }
  3549. if ($start == 0) {
  3550. $start = mktime(0, 0, 0, $m, $d, $y);
  3551. }
  3552. $db_start = date('Y-m-d H:i:s', $start);
  3553. if ($end == 0) {
  3554. $end = $start + (86400 * 7) - 1; //start of week, more 7 days, minus 1 second to get back to the previoyus day
  3555. }
  3556. //$db_end = date('Y-m-d H:i:s',$end);
  3557. $t_cal = Database::get_course_table(TABLE_AGENDA, $course_info['dbName']);
  3558. $t_cal_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT, $course_info['dbName']);
  3559. $t_ip = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_info['dbName']);
  3560. $sql = "SELECT c.id, c.title, c.content, ".
  3561. " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, ".
  3562. " cr.cal_type, cr.cal_end ".
  3563. " FROM $t_cal c, $t_cal_repeat cr, $t_ip as item_property ".
  3564. " WHERE cr.cal_end >= $start ".
  3565. " AND cr.cal_id = c.id ".
  3566. " AND item_property.ref = c.id ".
  3567. " AND item_property.tool = '".TOOL_CALENDAR_EVENT."' ".
  3568. " AND c.start_date <= '$db_start' "
  3569. .(!empty($params['conditions']) ? $params['conditions'] : '')
  3570. .(!empty($params['groupby']) ? ' GROUP BY '.$params['groupby'] : '')
  3571. .(!empty($params['orderby']) ? ' ORDER BY '.$params['orderby'] : '');
  3572. $res = Database::query($sql);
  3573. if (Database::num_rows($res) > 0) {
  3574. while ($row = Database::fetch_array($res)) {
  3575. $orig_start = $row['orig_start'];
  3576. $orig_end = $row['orig_end'];
  3577. $repeat_type = $row['cal_type'];
  3578. switch ($repeat_type) {
  3579. case 'daily':
  3580. $time_orig_h = date('H', $orig_start);
  3581. $time_orig_m = date('i', $orig_start);
  3582. $time_orig_s = date('s', $orig_start);
  3583. $int_time = (($time_orig_h * 60) + $time_orig_m) * 60 + $time_orig_s; //time in seconds since 00:00:00
  3584. $span = $orig_end - $orig_start; //total seconds between start and stop of original event
  3585. for ($i = 0; $i < 7; $i++) {
  3586. $current_start = $start + ($i * 86400) + $int_time; //unixtimestamp start of today's event
  3587. $current_stop = $start + ($i * 86400) + $int_time + $span; //unixtimestamp stop of today's event
  3588. $events[] = array(
  3589. $course_info['id'],
  3590. $row['id'],
  3591. $current_start,
  3592. $current_stop,
  3593. $row['title'],
  3594. $row['content']
  3595. );
  3596. }
  3597. break;
  3598. case 'weekly':
  3599. $time_orig = date('Y/n/W/j/N/G/i/s', $orig_start);
  3600. list($y_orig, $m_orig, $w_orig, $d_orig, $dw_orig, $h_orig, $n_orig, $s_orig) = split(
  3601. '/',
  3602. $time_orig
  3603. );
  3604. $time_now = date('Y/n/W/j/N/G/i/s', $end);
  3605. list($y_now, $m_now, $w_now, $d_now, $dw_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  3606. if ((($y_now > $y_orig) OR (($y_now == $y_orig) && ($w_now > $w_orig)))) { //if the event is after the original (at least one week) and the day of the week is the same
  3607. $time_orig_end = date('Y/n/W/j/N/G/i/s', $orig_end);
  3608. list($y_orig_e, $m_orig_e, $w_orig_e, $d_orig_e, $dw_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split(
  3609. '/',
  3610. $time_orig_end
  3611. );
  3612. $events[] = array(
  3613. $course_info['id'],
  3614. $row['id'],
  3615. mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now),
  3616. mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now),
  3617. $row['title'],
  3618. $row['content']
  3619. );
  3620. }
  3621. break;
  3622. case 'monthlyByDate':
  3623. $time_orig = date('Y/n/W/j/G/i/s', $orig_start);
  3624. list($y_orig, $m_orig, $w_orig, $d_orig, $h_orig, $n_orig, $s_orig) = split('/', $time_orig);
  3625. $time_now = date('Y/n/W/j/G/i/s', $end);
  3626. list($y_now, $m_now, $w_now, $d_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  3627. $event_repetition_time = mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now);
  3628. if ((($y_now > $y_orig) OR (($y_now == $y_orig) && ($m_now > $m_orig))) && ($start < $event_repetition_time && $event_repetition_time < $end)) { //if the event is after the original (at least one month) and the original event's day is between the first day of the week and the last day of the week
  3629. $time_orig_end = date('Y/n/j/G/i/s', $orig_end);
  3630. list($y_orig_e, $m_orig_e, $d_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split(
  3631. '/',
  3632. $time_orig_end
  3633. );
  3634. $events[] = array(
  3635. $course_info['id'],
  3636. $row['id'],
  3637. mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now),
  3638. mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now),
  3639. $row['title'],
  3640. $row['content']
  3641. );
  3642. }
  3643. break;
  3644. case 'monthlyByDayR':
  3645. //not implemented yet
  3646. break;
  3647. case 'monthlyByDay':
  3648. //not implemented yet
  3649. break;
  3650. case 'yearly':
  3651. $time_orig = date('Y/n/j/z/G/i/s', $orig_start);
  3652. list($y_orig, $m_orig, $d_orig, $dy_orig, $h_orig, $n_orig, $s_orig) = split('/', $time_orig);
  3653. $time_now = date('Y/n/j/z/G/i/s', $end);
  3654. list($y_now, $m_now, $d_now, $dy_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  3655. $event_repetition_time = mktime($h_orig, $n_orig, $s_orig, $m_orig, $d_orig, $y_now);
  3656. if ((($y_now > $y_orig) && ($start < $event_repetition_time && $event_repetition_time < $end))) {
  3657. $time_orig_end = date('Y/n/j/G/i/s', $orig_end);
  3658. list($y_orig_e, $m_orig_e, $d_orig_e, $dy_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split(
  3659. '/',
  3660. $time_orig_end
  3661. );
  3662. $events[] = array(
  3663. $course_info['id'],
  3664. $row['id'],
  3665. mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now),
  3666. mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now),
  3667. $row['title'],
  3668. $row['content']
  3669. );
  3670. }
  3671. break;
  3672. default:
  3673. break;
  3674. }
  3675. }
  3676. }
  3677. return $events;
  3678. }
  3679. /**
  3680. * Get repeated events of a course between two dates (timespan of a month).
  3681. * Returns an array containing the events
  3682. * @param string Course info array (as returned by api_get_course_info())
  3683. * @param int UNIX timestamp of span start. Defaults 0, later transformed into today's start
  3684. * @param int UNIX timestamp. Defaults to 0, later transformed into today's end
  3685. * @param array A set of parameters to alter the SQL query
  3686. * @return array [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  3687. */
  3688. function get_repeated_events_month_view($course_info, $start = 0, $end = 0, $params)
  3689. {
  3690. $events = array();
  3691. //block $end if higher than 2038 -- PHP doesn't go past that
  3692. if ($end > 2145934800) {
  3693. $end = 2145934800;
  3694. }
  3695. //initialise all values
  3696. $y = 0;
  3697. $m = 0;
  3698. $d = 0;
  3699. if ($start == 0 or $end == 0) {
  3700. $time = time();
  3701. $y = date('Y');
  3702. $m = date('m');
  3703. }
  3704. if ($start == 0) {
  3705. $start = mktime(0, 0, 0, $m, 1, $y);
  3706. }
  3707. $db_start = date('Y-m-d H:i:s', $start);
  3708. if ($end == 0) {
  3709. if ($m == 12) {
  3710. $end = mktime(
  3711. 0,
  3712. 0,
  3713. 0,
  3714. 1,
  3715. 1,
  3716. $y + 1
  3717. ) - 1; //start of next month, minus 1 second to get back to the previoyus day
  3718. } else {
  3719. $end = mktime(0, 0, 0, $m + 1, 1, $y) - 1;
  3720. }
  3721. }
  3722. //$db_end = date('Y-m-d H:i:s',$end);
  3723. $t_cal = Database::get_course_table(TABLE_AGENDA, $course_info['dbName']);
  3724. $t_cal_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT, $course_info['dbName']);
  3725. $t_ip = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_info['dbName']);
  3726. $sql = "SELECT c.id, c.title, c.content, ".
  3727. " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, ".
  3728. " cr.cal_type, cr.cal_end ".
  3729. " FROM $t_cal c, $t_cal_repeat cr, $t_ip as item_property ".
  3730. " WHERE cr.cal_end >= $start ".
  3731. " AND cr.cal_id = c.id ".
  3732. " AND item_property.ref = c.id ".
  3733. " AND item_property.tool = '".TOOL_CALENDAR_EVENT."' ".
  3734. " AND c.start_date <= '$db_start' "
  3735. .(!empty($params['conditions']) ? $params['conditions'] : '')
  3736. .(!empty($params['groupby']) ? ' GROUP BY '.$params['groupby'] : '')
  3737. .(!empty($params['orderby']) ? ' ORDER BY '.$params['orderby'] : '');
  3738. $res = Database::query($sql);
  3739. if (Database::num_rows($res) > 0) {
  3740. while ($row = Database::fetch_array($res)) {
  3741. $orig_start = $row['orig_start'];
  3742. $orig_end = $row['orig_end'];
  3743. $repeat_type = $row['cal_type'];
  3744. switch ($repeat_type) {
  3745. case 'daily':
  3746. $time_orig_h = date('H', $orig_start);
  3747. $time_orig_m = date('i', $orig_start);
  3748. $time_orig_s = date('s', $orig_start);
  3749. $month_last_day = date('d', $end);
  3750. $int_time = (($time_orig_h * 60) + $time_orig_m) * 60 + $time_orig_s; //time in seconds since 00:00:00
  3751. $span = $orig_end - $orig_start; //total seconds between start and stop of original event
  3752. for ($i = 0; $i < $month_last_day; $i++) {
  3753. $current_start = $start + ($i * 86400) + $int_time; //unixtimestamp start of today's event
  3754. $current_stop = $start + ($i * 86400) + $int_time + $span; //unixtimestamp stop of today's event
  3755. $events[] = array(
  3756. $course_info['id'],
  3757. $row['id'],
  3758. $current_start,
  3759. $current_stop,
  3760. $row['title'],
  3761. $row['content']
  3762. );
  3763. }
  3764. break;
  3765. case 'weekly':
  3766. //A weekly repeated event is very difficult to catch in a month view,
  3767. //because weeks start before or at the same time as the first day of the month
  3768. //The same can be said for the end of the month.
  3769. // The idea is thus to get all possible events by enlarging the scope of
  3770. // the month to get complete weeks covering the complete month, and then take out
  3771. // the events that start before the 1st ($start) or after the last day of the month ($end)
  3772. $time_orig = date('Y/n/W/j/N/G/i/s', $orig_start);
  3773. list($y_orig, $m_orig, $w_orig, $d_orig, $dw_orig, $h_orig, $n_orig, $s_orig) = split(
  3774. '/',
  3775. $time_orig
  3776. );
  3777. $time_orig_end = date('Y/n/W/j/N/G/i/s', $orig_end);
  3778. list($y_orig_e, $m_orig_e, $w_orig_e, $d_orig_e, $dw_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split(
  3779. '/',
  3780. $time_orig_end
  3781. );
  3782. $time_now = date('Y/n/W/j/N/G/i/s', $end);
  3783. list($y_now, $m_now, $w_now, $d_now, $dw_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  3784. $month_first_week = date('W', $start);
  3785. $month_last_week = date('W', $end);
  3786. if (($y_now > $y_orig) OR (($y_now == $y_orig) && ($w_now > $w_orig))) { //if the event is after the original (at least one week) and the day of the week is the same
  3787. for ($i = $month_first_week; $i <= $month_last_week; $i++) {
  3788. //the "day of the week" of repetition is the same as the $dw_orig,
  3789. //so to get the "day of the month" from the "day of the week", we have
  3790. //to get the first "day of the week" for this week and add the number
  3791. //of days (in seconds) to reach the $dw_orig
  3792. //example: the first week spans between the 28th of April (Monday) to the
  3793. // 4th of May (Sunday). The event occurs on the 2nd day of each week.
  3794. // This means the event occurs on 29/4, 6/5, 13/5, 20/5 and 27/5.
  3795. // We want to get all of these, and then reject 29/4 because it is out
  3796. // of the month itself.
  3797. //First, to get the start time of the first day of the month view (even if
  3798. // the day is from the past month), we get the month start date (1/5) and
  3799. // see which day of the week it is, and subtract the number of days necessary
  3800. // to get back to the first day of the week.
  3801. $month_first_day_weekday = date('N', $start);
  3802. $first_week_start = $start - (($month_first_day_weekday - 1) * 86400);
  3803. //Second, we add the week day of the original event, so that we have an
  3804. // absolute time that represents the first repetition of the event in
  3805. // our 4- or 5-weeks timespan
  3806. $first_event_repeat_start = $first_week_start + (($dw_orig - 1) * 86400) + ($h_orig * 3600) + ($n_orig * 60) + $s_orig;
  3807. //Third, we start looping through the repetitions and see if they are between
  3808. // $start and $end
  3809. for ($i = $first_event_repeat_start; $i <= $end; $i += 604800) {
  3810. if ($start < $i && $i < $end) {
  3811. list($y_repeat, $m_repeat, $d_repeat, $h_repeat, $n_repeat, $s_repeat) = split(
  3812. '/',
  3813. date('Y/m/j/H/i/s', $i)
  3814. );
  3815. $events[] = array(
  3816. $course_info['id'],
  3817. $row['id'],
  3818. mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now),
  3819. mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now),
  3820. $row['title'],
  3821. $row['content']
  3822. );
  3823. }
  3824. }
  3825. }
  3826. }
  3827. break;
  3828. case 'monthlyByDate':
  3829. $time_orig = date('Y/n/W/j/G/i/s', $orig_start);
  3830. list($y_orig, $m_orig, $w_orig, $d_orig, $h_orig, $n_orig, $s_orig) = split('/', $time_orig);
  3831. $time_now = date('Y/n/W/j/G/i/s', $end);
  3832. list($y_now, $m_now, $w_now, $d_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  3833. $event_repetition_time = mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now);
  3834. if (($y_now > $y_orig) OR (($y_now == $y_orig) && ($m_now > $m_orig))) { //if the event is after the original (at least one month) and the original event's day is between the first day of the week and the last day of the week
  3835. $time_orig_end = date('Y/n/j/G/i/s', $orig_end);
  3836. list($y_orig_e, $m_orig_e, $d_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split(
  3837. '/',
  3838. $time_orig_end
  3839. );
  3840. $events[] = array(
  3841. $course_info['id'],
  3842. $row['id'],
  3843. mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now),
  3844. mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now),
  3845. $row['title'],
  3846. $row['content']
  3847. );
  3848. }
  3849. break;
  3850. case 'monthlyByDayR':
  3851. //not implemented yet
  3852. break;
  3853. case 'monthlyByDay':
  3854. //not implemented yet
  3855. break;
  3856. case 'yearly':
  3857. $time_orig = date('Y/n/j/z/G/i/s', $orig_start);
  3858. list($y_orig, $m_orig, $d_orig, $dy_orig, $h_orig, $n_orig, $s_orig) = split('/', $time_orig);
  3859. $time_now = date('Y/n/j/z/G/i/s', $end);
  3860. list($y_now, $m_now, $d_now, $dy_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  3861. $event_repetition_time = mktime($h_orig, $n_orig, $s_orig, $m_orig, $d_orig, $y_now);
  3862. if ((($y_now > $y_orig) && ($start < $event_repetition_time && $event_repetition_time < $end))) {
  3863. $time_orig_end = date('Y/n/j/G/i/s', $orig_end);
  3864. list($y_orig_e, $m_orig_e, $d_orig_e, $dy_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split(
  3865. '/',
  3866. $time_orig_end
  3867. );
  3868. $events[] = array(
  3869. $course_info['id'],
  3870. $row['id'],
  3871. mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now),
  3872. mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now),
  3873. $row['title'],
  3874. $row['content']
  3875. );
  3876. }
  3877. break;
  3878. default:
  3879. break;
  3880. }
  3881. }
  3882. }
  3883. return $events;
  3884. }
  3885. /**
  3886. * Get repeated events of a course between two dates (1 year timespan). Used for the list display.
  3887. * This is virtually unlimited but by default it shortens to 100 years from now (even a birthday shouldn't be useful more than this time - except for turtles)
  3888. * Returns an array containing the events
  3889. * @param string Course info array (as returned by api_get_course_info())
  3890. * @param int UNIX timestamp of span start. Defaults 0, later transformed into today's start
  3891. * @param int UNIX timestamp. Defaults to 0, later transformed into today's end
  3892. * @param array A set of parameters to alter the SQL query
  3893. * @return array [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  3894. */
  3895. function get_repeated_events_list_view($course_info, $start = 0, $end = 0, $params)
  3896. {
  3897. $events = array();
  3898. //block $end if higher than 2038 -- PHP doesn't go past that
  3899. if ($end > 2145934800) {
  3900. $end = 2145934800;
  3901. }
  3902. //initialise all values
  3903. $y = 0;
  3904. $m = 0;
  3905. $d = 0;
  3906. if (empty($start) or empty($end)) {
  3907. $time = time();
  3908. $y = date('Y');
  3909. $m = date('m');
  3910. }
  3911. if (empty($start)) {
  3912. $start = mktime(0, 0, 0, $m, 1, $y);
  3913. }
  3914. $db_start = date('Y-m-d H:i:s', $start);
  3915. if (empty($end)) {
  3916. $end = mktime(0, 0, 0, 1, 1, 2037);
  3917. }
  3918. //$db_end = date('Y-m-d H:i:s',$end);
  3919. $t_cal = Database::get_course_table(TABLE_AGENDA, $course_info['dbName']);
  3920. $t_cal_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT, $course_info['dbName']);
  3921. $t_ip = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_info['dbName']);
  3922. $sql = "SELECT c.id, c.title, c.content, ".
  3923. " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, ".
  3924. " cr.cal_type, cr.cal_end ".
  3925. " FROM $t_cal c, $t_cal_repeat cr, $t_ip as item_property ".
  3926. " WHERE cr.cal_end >= $start ".
  3927. " AND cr.cal_id = c.id ".
  3928. " AND item_property.ref = c.id ".
  3929. " AND item_property.tool = '".TOOL_CALENDAR_EVENT."' ".
  3930. " AND c.start_date <= '$db_start' "
  3931. .(!empty($params['conditions']) ? $params['conditions'] : '')
  3932. .(!empty($params['groupby']) ? ' GROUP BY '.$params['groupby'] : '')
  3933. .(!empty($params['orderby']) ? ' ORDER BY '.$params['orderby'] : '');
  3934. $res = Database::query($sql);
  3935. if (Database::num_rows($res) > 0) {
  3936. while ($row = Database::fetch_array($res)) {
  3937. $orig_start = $row['orig_start'];
  3938. $orig_end = $row['orig_end'];
  3939. $repeat_type = $row['cal_type'];
  3940. $repeat_end = $row['cal_end'];
  3941. switch ($repeat_type) {
  3942. case 'daily':
  3943. $time_orig_h = date('H', $orig_start);
  3944. $time_orig_m = date('i', $orig_start);
  3945. $time_orig_s = date('s', $orig_start);
  3946. $span = $orig_end - $orig_start; //total seconds between start and stop of original event
  3947. for ($i = $orig_start + 86400; ($i < $end && $i <= $repeat_end); $i += 86400) {
  3948. $current_start = $i; //unixtimestamp start of today's event
  3949. $current_stop = $i + $span; //unixtimestamp stop of today's event
  3950. $events[] = array(
  3951. $course_info['id'],
  3952. $row['id'],
  3953. $current_start,
  3954. $current_stop,
  3955. $row['title'],
  3956. $row['content']
  3957. );
  3958. }
  3959. break;
  3960. case 'weekly':
  3961. //A weekly repeated event is very difficult to catch in a month view,
  3962. // because weeks start before or at the same time as the first day of the month
  3963. //The same can be said for the end of the month.
  3964. // The idea is thus to get all possible events by enlarging the scope of
  3965. // the month to get complete weeks covering the complete month, and then take out
  3966. // the events that start before the 1st ($start) or after the last day of the month ($end)
  3967. $time_orig = date('Y/n/W/j/N/G/i/s', $orig_start);
  3968. list($y_orig, $m_orig, $w_orig, $d_orig, $dw_orig, $h_orig, $n_orig, $s_orig) = split(
  3969. '/',
  3970. $time_orig
  3971. );
  3972. $time_orig_end = date('Y/n/W/j/N/G/i/s', $orig_end);
  3973. list($y_orig_e, $m_orig_e, $w_orig_e, $d_orig_e, $dw_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split(
  3974. '/',
  3975. $time_orig_end
  3976. );
  3977. $time_now = date('Y/n/W/j/N/G/i/s', $end);
  3978. list($y_now, $m_now, $w_now, $d_now, $dw_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  3979. if ($w_now == 52) {
  3980. ++$y_now;
  3981. $w_now = 1;
  3982. } else {
  3983. ++$w_now;
  3984. }
  3985. $month_first_week = date('W', $start);
  3986. $total_weeks = ((date('Y', $end) - $y_orig) - 1) * 52;
  3987. $month_last_week = $month_first_week + $total_weeks;
  3988. if (($y_now > $y_orig) OR (($y_now == $y_orig) && ($w_now > $w_orig))) { //if the event is after the original (at least one week) and the day of the week is the same
  3989. //for($i=$month_first_week;($i<=$month_last_week && $i<1000);$i++)
  3990. //{
  3991. /*
  3992. The "day of the week" of repetition is the same as the $dw_orig,
  3993. so to get the "day of the month" from the "day of the week", we have
  3994. to get the first "day of the week" for this week and add the number
  3995. of days (in seconds) to reach the $dw_orig
  3996. example: the first week spans between the 28th of April (Monday) to the
  3997. 4th of May (Sunday). The event occurs on the 2nd day of each week.
  3998. This means the event occurs on 29/4, 6/5, 13/5, 20/5 and 27/5.
  3999. We want to get all of these, and then reject 29/4 because it is out
  4000. of the month itself.
  4001. First, to get the start time of the first day of the month view (even if
  4002. the day is from the past month), we get the month start date (1/5) and
  4003. see which day of the week it is, and subtract the number of days necessary
  4004. to get back to the first day of the week.
  4005. */
  4006. $month_first_day_weekday = date('N', $start);
  4007. $first_week_start = $start - (($month_first_day_weekday - 1) * 86400);
  4008. //Second, we add the week day of the original event, so that we have an
  4009. // absolute time that represents the first repetition of the event in
  4010. // our 4- or 5-weeks timespan
  4011. $first_event_repeat_start = $first_week_start + (($dw_orig - 1) * 86400) + ($h_orig * 3600) + ($n_orig * 60) + $s_orig;
  4012. //Third, we start looping through the repetitions and see if they are between
  4013. // $start and $end
  4014. for ($i = $first_event_repeat_start; ($i <= $end && $i <= $repeat_end); $i += 604800) {
  4015. if ($start < $i && $i <= $end && $i <= $repeat_end) {
  4016. list($y_repeat, $m_repeat, $d_repeat, $h_repeat, $n_repeat, $s_repeat) = split(
  4017. '/',
  4018. date('Y/m/j/H/i/s', $i)
  4019. );
  4020. $new_start_time = mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now);
  4021. $new_stop_time = mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now);
  4022. $events[] = array(
  4023. $course_info['id'],
  4024. $row['id'],
  4025. $new_start_time,
  4026. $new_stop_time,
  4027. $row['title'],
  4028. $row['content']
  4029. );
  4030. }
  4031. $time_now = date('Y/n/W/j/N/G/i/s', $i + 604800);
  4032. list($y_now, $m_now, $w_now, $d_now, $dw_now, $h_now, $n_now, $s_now) = split(
  4033. '/',
  4034. $time_now
  4035. );
  4036. }
  4037. //}
  4038. }
  4039. break;
  4040. case 'monthlyByDate':
  4041. $time_orig = date('Y/n/W/j/G/i/s', $orig_start);
  4042. list($y_orig, $m_orig, $w_orig, $d_orig, $h_orig, $n_orig, $s_orig) = split('/', $time_orig);
  4043. $time_now = date('Y/n/W/j/G/i/s', $start);
  4044. list($y_now, $m_now, $w_now, $d_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  4045. //make sure we are one month ahead (to avoid being the same month as the original event)
  4046. if ($m_now == 12) {
  4047. ++$y_now;
  4048. $m_now = 1;
  4049. } else {
  4050. ++$m_now;
  4051. }
  4052. $time_orig_end = date('Y/n/j/G/i/s', $orig_end);
  4053. list($y_orig_e, $m_orig_e, $d_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split('/', $time_orig_end);
  4054. $event_repetition_time = mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now);
  4055. $diff = $orig_end - $orig_start;
  4056. while ((($y_now > $y_orig) OR (($y_now == $y_orig) && ($m_now > $m_orig))) && ($event_repetition_time < $end) && ($event_repetition_time < $repeat_end)) { //if the event is after the original (at least one month) and the original event's day is between the first day of the week and the last day of the week
  4057. $new_start_time = mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now);
  4058. $new_stop_time = mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now);
  4059. $events[] = array(
  4060. $course_info['id'],
  4061. $row['id'],
  4062. $new_start_time,
  4063. $new_stop_time,
  4064. $row['title'],
  4065. $row['content']
  4066. );
  4067. if ($m_now == 12) {
  4068. ++$y_now;
  4069. $m_now = 1;
  4070. } else {
  4071. ++$m_now;
  4072. }
  4073. $event_repetition_time = mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now);
  4074. }
  4075. break;
  4076. case 'monthlyByDayR':
  4077. //not implemented yet
  4078. break;
  4079. case 'monthlyByDay':
  4080. //not implemented yet
  4081. break;
  4082. case 'yearly':
  4083. $time_orig = date('Y/n/j/z/G/i/s', $orig_start);
  4084. list($y_orig, $m_orig, $d_orig, $dy_orig, $h_orig, $n_orig, $s_orig) = split('/', $time_orig);
  4085. $time_now = date('Y/n/j/z/G/i/s', $end);
  4086. list($y_now, $m_now, $d_now, $dy_now, $h_now, $n_now, $s_now) = split('/', $time_now);
  4087. $event_repetition_time = mktime($h_orig, $n_orig, $s_orig, $m_orig, $d_orig, $y_now);
  4088. while ((($y_now > $y_orig) && ($start < $event_repetition_time && $event_repetition_time < $end && $event_repetition_time < $repeat_end))) {
  4089. $time_orig_end = date('Y/n/j/G/i/s', $orig_end);
  4090. list($y_orig_e, $m_orig_e, $d_orig_e, $dy_orig_e, $h_orig_e, $n_orig_e, $s_orig_e) = split(
  4091. '/',
  4092. $time_orig_end
  4093. );
  4094. $events[] = array(
  4095. $course_info['id'],
  4096. $row['id'],
  4097. mktime($h_orig, $n_orig, $s_orig, $m_now, $d_orig, $y_now),
  4098. mktime($h_orig_e, $n_orig_e, $s_orig_e, $m_now, $d_orig_e, $y_now),
  4099. $row['title'],
  4100. $row['content']
  4101. );
  4102. ++$y_now;
  4103. $event_repetition_time = mktime($h_orig, $n_orig, $s_orig, $m_orig, $d_orig, $y_now);
  4104. }
  4105. break;
  4106. default:
  4107. break;
  4108. }
  4109. }
  4110. }
  4111. return $events;
  4112. }
  4113. /**
  4114. * Tells if an agenda item is repeated
  4115. * @param string Course database
  4116. * @param int The agenda item
  4117. * @return boolean True if repeated, false otherwise
  4118. */
  4119. function is_repeated_event($id, $course = null)
  4120. {
  4121. if (empty($course)) {
  4122. $course_info = api_get_course_info();
  4123. $course = $course_info['dbName'];
  4124. }
  4125. $id = (int)$id;
  4126. $t_agenda_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT, $course);
  4127. $sql = "SELECT * FROM $t_agenda_repeat WHERE cal_id = $id";
  4128. $res = Database::query($sql);
  4129. if (Database::num_rows($res) > 0) {
  4130. return true;
  4131. }
  4132. return false;
  4133. }
  4134. /**
  4135. * Adds x weeks to a UNIX timestamp
  4136. * @param int The timestamp
  4137. * @param int The number of weeks to add
  4138. * @return int The new timestamp
  4139. */
  4140. function add_week($timestamp, $num = 1)
  4141. {
  4142. return $timestamp + $num * 604800;
  4143. }
  4144. /**
  4145. * Adds x months to a UNIX timestamp
  4146. * @param int The timestamp
  4147. * @param int The number of years to add
  4148. * @return int The new timestamp
  4149. */
  4150. function add_month($timestamp, $num = 1)
  4151. {
  4152. list($y, $m, $d, $h, $n, $s) = split('/', date('Y/m/d/h/i/s', $timestamp));
  4153. if ($m + $num > 12) {
  4154. $y += floor($num / 12);
  4155. $m += $num % 12;
  4156. } else {
  4157. $m += $num;
  4158. }
  4159. return mktime($h, $n, $s, $m, $d, $y);
  4160. }
  4161. /**
  4162. * Adds x years to a UNIX timestamp
  4163. * @param int The timestamp
  4164. * @param int The number of years to add
  4165. * @return int The new timestamp
  4166. */
  4167. function add_year($timestamp, $num = 1)
  4168. {
  4169. list($y, $m, $d, $h, $n, $s) = split('/', date('Y/m/d/h/i/s', $timestamp));
  4170. return mktime($h, $n, $s, $m, $d, $y + $num);
  4171. }
  4172. /**
  4173. * Adds an agenda item in the database. Similar to store_new_agenda_item() except it takes parameters
  4174. * @param array Course info
  4175. * @param string Event title
  4176. * @param string Event content/description
  4177. * @param string Start date
  4178. * @param string End date
  4179. * @param array List of groups to which this event is added
  4180. * @param int Parent id (optional)
  4181. * @return int The new item's DB ID
  4182. */
  4183. function agenda_add_item(
  4184. $course_info,
  4185. $title,
  4186. $content,
  4187. $db_start_date,
  4188. $db_end_date,
  4189. $to = array(),
  4190. $parent_id = null,
  4191. $file_comment = ''
  4192. ) {
  4193. $user_id = api_get_user_id();
  4194. // database table definitions
  4195. $t_agenda = Database::get_course_table(TABLE_AGENDA);
  4196. $item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4197. // some filtering of the input data
  4198. $title = Database::escape_string($title);
  4199. $content = Database::escape_string($content);
  4200. $db_start_date = api_get_utc_datetime($db_start_date);
  4201. $start_date = Database::escape_string($db_start_date);
  4202. if (!empty($db_end_date)) {
  4203. $db_end_date = api_get_utc_datetime($db_end_date);
  4204. }
  4205. $end_date = Database::escape_string($db_end_date);
  4206. $id_session = api_get_session_id();
  4207. $course_id = api_get_course_int_id();
  4208. $group_id = api_get_group_id();
  4209. // check if exists in calendar_event table and if it is not deleted!
  4210. $sql = "SELECT * FROM $t_agenda agenda, $item_property item_property
  4211. WHERE
  4212. agenda.c_id = $course_id AND
  4213. item_property.c_id = $course_id AND
  4214. agenda.title = '$title'
  4215. AND agenda.content = '$content'
  4216. AND agenda.start_date = '$start_date'
  4217. AND agenda.end_date = '$end_date' ".(!empty($parent_id) ? "
  4218. AND agenda.parent_event_id = '$parent_id'" : "")."
  4219. AND agenda.session_id = '$id_session'
  4220. AND item_property.tool = '".TOOL_CALENDAR_EVENT."'
  4221. AND item_property.ref = agenda.id
  4222. AND item_property.visibility <> 2";
  4223. $result = Database::query($sql);
  4224. $count = Database::num_rows($result);
  4225. if ($count > 0) {
  4226. return false;
  4227. }
  4228. $course_id = api_get_course_int_id();
  4229. $sql = "INSERT INTO ".$t_agenda." (c_id, title,content, start_date, end_date".(!empty($parent_id) ? ',parent_event_id' : '').", session_id)
  4230. VALUES($course_id, '".$title."','".$content."', '".$start_date."','".$end_date."'".(!empty($parent_id) ? ','.((int)$parent_id) : '').", '".$id_session."')";
  4231. $result = Database::query($sql);
  4232. $last_id = Database::insert_id();
  4233. // add a attachment file in agenda
  4234. add_agenda_attachment_file($file_comment, $last_id);
  4235. // store in last_tooledit (first the groups, then the users
  4236. if (!empty($to)) {
  4237. $send_to = separate_users_groups($to);
  4238. // storing the selected groups
  4239. if (is_array($send_to['groups'])) {
  4240. foreach ($send_to['groups'] as $group) {
  4241. api_item_property_update(
  4242. $course_info,
  4243. TOOL_CALENDAR_EVENT,
  4244. $last_id,
  4245. "AgendaAdded",
  4246. $user_id,
  4247. $group,
  4248. 0,
  4249. $start_date,
  4250. $end_date
  4251. );
  4252. $done = true;
  4253. }
  4254. }
  4255. // storing the selected users
  4256. if (is_array($send_to['users'])) {
  4257. foreach ($send_to['users'] as $user) {
  4258. api_item_property_update(
  4259. $course_info,
  4260. TOOL_CALENDAR_EVENT,
  4261. $last_id,
  4262. "AgendaAdded",
  4263. $user_id,
  4264. 0,
  4265. $user,
  4266. $start_date,
  4267. $end_date
  4268. );
  4269. $done = true;
  4270. }
  4271. }
  4272. if (isset($send_to['everyone']) && $send_to['everyone']) {
  4273. api_item_property_update(
  4274. $course_info,
  4275. TOOL_CALENDAR_EVENT,
  4276. $last_id,
  4277. "AgendaAdded",
  4278. $user_id,
  4279. 0,
  4280. 0,
  4281. $start_date,
  4282. $end_date
  4283. );
  4284. }
  4285. }
  4286. // storing the resources
  4287. if (!empty($_SESSION['source_type']) && !empty($last_id)) {
  4288. store_resources($_SESSION['source_type'], $last_id);
  4289. }
  4290. return $last_id;
  4291. }
  4292. /**
  4293. * This function delete a attachment file by id
  4294. * @param integer attachment file Id
  4295. *
  4296. */
  4297. function delete_attachment_file($id_attach)
  4298. {
  4299. $_course = api_get_course_info();
  4300. $agenda_table_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  4301. $id_attach = intval($id_attach);
  4302. $sql = "DELETE FROM $agenda_table_attachment WHERE id = ".$id_attach;
  4303. $result = Database::query($sql);
  4304. $last_id_file = Database::insert_id();
  4305. // update item_property
  4306. api_item_property_update(
  4307. $_course,
  4308. 'calendar_event_attachment',
  4309. $id_attach,
  4310. 'AgendaAttachmentDeleted',
  4311. api_get_user_id()
  4312. );
  4313. if (!empty($result)) {
  4314. Display::display_confirmation_message(get_lang("AttachmentFileDeleteSuccess"));
  4315. }
  4316. }
  4317. /**
  4318. * This function add a attachment file into agenda
  4319. * @param string a comment about file
  4320. * @param int last id from calendar table
  4321. *
  4322. */
  4323. function add_agenda_attachment_file($file_comment, $last_id)
  4324. {
  4325. $_course = api_get_course_info();
  4326. $agenda_table_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  4327. $last_id = intval($last_id);
  4328. // Storing the attachments
  4329. if (!empty($_FILES['user_upload']['name'])) {
  4330. $upload_ok = FileManager::process_uploaded_file($_FILES['user_upload']);
  4331. }
  4332. if (!empty($upload_ok)) {
  4333. $courseDir = $_course['path'].'/upload/calendar';
  4334. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  4335. $updir = $sys_course_path.$courseDir;
  4336. // Try to add an extension to the file if it hasn't one
  4337. $new_file_name = FileManager::add_ext_on_mime(
  4338. stripslashes($_FILES['user_upload']['name']),
  4339. $_FILES['user_upload']['type']
  4340. );
  4341. // user's file name
  4342. $file_name = $_FILES['user_upload']['name'];
  4343. if (!FileManager::filter_extension($new_file_name)) {
  4344. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  4345. } else {
  4346. $new_file_name = uniqid('');
  4347. $new_path = $updir.'/'.$new_file_name;
  4348. $result = @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
  4349. $safe_file_comment = Database::escape_string($file_comment);
  4350. $safe_file_name = Database::escape_string($file_name);
  4351. $safe_new_file_name = Database::escape_string($new_file_name);
  4352. $course_id = api_get_course_int_id();
  4353. // Storing the attachments if any
  4354. if ($result) {
  4355. $sql = 'INSERT INTO '.$agenda_table_attachment.'(c_id, filename,comment, path,agenda_id,size) '.
  4356. "VALUES ($course_id, '".$safe_file_name."', '".$safe_file_comment."', '".$safe_new_file_name."' , '".$last_id."', '".intval(
  4357. $_FILES['user_upload']['size']
  4358. )."' )";
  4359. Database::query($sql);
  4360. //$message .= ' / '.get_lang('FileUploadSucces').'<br />';
  4361. $last_id_file = Database::insert_id();
  4362. api_item_property_update(
  4363. $_course,
  4364. 'calendar_event_attachment',
  4365. $last_id_file,
  4366. 'AgendaAttachmentAdded',
  4367. api_get_user_id()
  4368. );
  4369. }
  4370. }
  4371. }
  4372. }
  4373. /**
  4374. * This function edit a attachment file into agenda
  4375. * @param string a comment about file
  4376. * @param int Agenda Id
  4377. * @param int attachment file Id
  4378. */
  4379. function edit_agenda_attachment_file($file_comment, $agenda_id, $id_attach)
  4380. {
  4381. $_course = api_get_course_info();
  4382. $agenda_table_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  4383. // Storing the attachments
  4384. if (!empty($_FILES['user_upload']['name'])) {
  4385. $upload_ok = FileManager::process_uploaded_file($_FILES['user_upload']);
  4386. }
  4387. if (!empty($upload_ok)) {
  4388. $courseDir = $_course['path'].'/upload/calendar';
  4389. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  4390. $updir = $sys_course_path.$courseDir;
  4391. // Try to add an extension to the file if it hasn't one
  4392. $new_file_name = FileManager::add_ext_on_mime(
  4393. stripslashes($_FILES['user_upload']['name']),
  4394. $_FILES['user_upload']['type']
  4395. );
  4396. // user's file name
  4397. $file_name = $_FILES['user_upload']['name'];
  4398. if (!FileManager::filter_extension($new_file_name)) {
  4399. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  4400. } else {
  4401. $new_file_name = uniqid('');
  4402. $new_path = $updir.'/'.$new_file_name;
  4403. $result = @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
  4404. $safe_file_comment = Database::escape_string($file_comment);
  4405. $safe_file_name = Database::escape_string($file_name);
  4406. $safe_new_file_name = Database::escape_string($new_file_name);
  4407. $safe_agenda_id = (int)$agenda_id;
  4408. $safe_id_attach = (int)$id_attach;
  4409. // Storing the attachments if any
  4410. if ($result) {
  4411. $sql = "UPDATE $agenda_table_attachment SET filename = '$safe_file_name', comment = '$safe_file_comment', path = '$safe_new_file_name', agenda_id = '$safe_agenda_id', size ='".intval(
  4412. $_FILES['user_upload']['size']
  4413. )."'
  4414. WHERE id = '$safe_id_attach'";
  4415. Database::query($sql);
  4416. api_item_property_update(
  4417. $_course,
  4418. 'calendar_event_attachment',
  4419. $safe_id_attach,
  4420. 'AgendaAttachmentUpdated',
  4421. api_get_user_id()
  4422. );
  4423. }
  4424. }
  4425. }
  4426. }
  4427. /**
  4428. * Adds a repetitive item to the database
  4429. * @param array Course info
  4430. * @param int The original event's id
  4431. * @param string Type of repetition
  4432. * @param int Timestamp of end of repetition (repeating until that date)
  4433. * @param array Original event's destination (users list)
  4434. * @param string a comment about a attachment file into agenda
  4435. * @return boolean False if error, True otherwise
  4436. */
  4437. function agenda_add_repeat_item($course_info, $orig_id, $type, $end, $orig_dest, $file_comment = '')
  4438. {
  4439. $t_agenda = Database::get_course_table(TABLE_AGENDA);
  4440. $t_agenda_r = Database::get_course_table(TABLE_AGENDA_REPEAT);
  4441. $course_id = $course_info['real_id'];
  4442. $sql = 'SELECT title, content, start_date as sd, end_date as ed FROM '.$t_agenda.' WHERE c_id = '.$course_id.' AND id ="'.intval(
  4443. $orig_id
  4444. ).'" ';
  4445. $res = Database::query($sql);
  4446. if (Database::num_rows($res) !== 1) {
  4447. return false;
  4448. }
  4449. $row = Database::fetch_array($res);
  4450. $orig_start = api_strtotime(api_get_local_time($row['sd']));
  4451. $orig_end = api_strtotime(api_get_local_time($row['ed']));
  4452. $diff = $orig_end - $orig_start;
  4453. $orig_title = $row['title'];
  4454. $orig_content = $row['content'];
  4455. $now = time();
  4456. $type = Database::escape_string($type);
  4457. $end = intval($end);
  4458. if (1 <= $end && $end <= 500) {
  4459. //we assume that, with this type of value, the user actually gives a count of repetitions
  4460. //and that he wants us to calculate the end date with that (particularly in case of imports from ical)
  4461. switch ($type) {
  4462. case 'daily':
  4463. $end = $orig_start + (86400 * $end);
  4464. break;
  4465. case 'weekly':
  4466. $end = add_week($orig_start, $end);
  4467. break;
  4468. case 'monthlyByDate':
  4469. $end = add_month($orig_start, $end);
  4470. break;
  4471. case 'monthlyByDay':
  4472. //TODO
  4473. break;
  4474. case 'monthlyByDayR':
  4475. //TODO
  4476. break;
  4477. case 'yearly':
  4478. $end = add_year($orig_start, $end);
  4479. break;
  4480. }
  4481. }
  4482. if ($end > $now
  4483. && in_array($type, array('daily', 'weekly', 'monthlyByDate', 'monthlyByDay', 'monthlyByDayR', 'yearly'))
  4484. ) {
  4485. $sql = "INSERT INTO $t_agenda_r (c_id, cal_id, cal_type, cal_end) VALUES ($course_id, '$orig_id','$type',$end)";
  4486. $res = Database::query($sql);
  4487. switch ($type) {
  4488. case 'daily':
  4489. for ($i = $orig_start + 86400; ($i <= $end); $i += 86400) {
  4490. $res = agenda_add_item(
  4491. $course_info,
  4492. $orig_title,
  4493. $orig_content,
  4494. date('Y-m-d H:i:s', $i),
  4495. date('Y-m-d H:i:s', $i + $diff),
  4496. $orig_dest,
  4497. $orig_id,
  4498. $file_comment
  4499. );
  4500. }
  4501. break;
  4502. case 'weekly':
  4503. for ($i = $orig_start + 604800; ($i <= $end); $i += 604800) {
  4504. $res = agenda_add_item(
  4505. $course_info,
  4506. $orig_title,
  4507. $orig_content,
  4508. date('Y-m-d H:i:s', $i),
  4509. date('Y-m-d H:i:s', $i + $diff),
  4510. $orig_dest,
  4511. $orig_id,
  4512. $file_comment
  4513. );
  4514. }
  4515. break;
  4516. case 'monthlyByDate':
  4517. $next_start = add_month($orig_start);
  4518. while ($next_start <= $end) {
  4519. $res = agenda_add_item(
  4520. $course_info,
  4521. $orig_title,
  4522. $orig_content,
  4523. date('Y-m-d H:i:s', $next_start),
  4524. date('Y-m-d H:i:s', $next_start + $diff),
  4525. $orig_dest,
  4526. $orig_id,
  4527. $file_comment
  4528. );
  4529. $next_start = add_month($next_start);
  4530. }
  4531. break;
  4532. case 'monthlyByDay':
  4533. //not yet implemented
  4534. break;
  4535. case 'monthlyByDayR':
  4536. //not yet implemented
  4537. break;
  4538. case 'yearly':
  4539. $next_start = add_year($orig_start);
  4540. while ($next_start <= $end) {
  4541. $res = agenda_add_item(
  4542. $course_info,
  4543. $orig_title,
  4544. $orig_content,
  4545. date('Y-m-d H:i:s', $next_start),
  4546. date('Y-m-d H:i:s', $next_start + $diff),
  4547. $orig_dest,
  4548. $orig_id,
  4549. $file_comment
  4550. );
  4551. $next_start = add_year($next_start);
  4552. }
  4553. break;
  4554. }
  4555. }
  4556. return true;
  4557. }
  4558. /**
  4559. * Import an iCal file into the database
  4560. * @param array Course info
  4561. * @return boolean True on success, false otherwise
  4562. */
  4563. function agenda_import_ical($course_info, $file)
  4564. {
  4565. $charset = api_get_system_encoding();
  4566. $filepath = api_get_path(SYS_ARCHIVE_PATH).$file['name'];
  4567. if (!@move_uploaded_file($file['tmp_name'], $filepath)) {
  4568. error_log('Problem moving uploaded file: '.$file['error'].' in '.__FILE__.' line '.__LINE__);
  4569. return false;
  4570. }
  4571. require_once api_get_path(LIBRARY_PATH).'icalcreator/iCalcreator.class.php';
  4572. $ical = new vcalendar();
  4573. $ical->setConfig('directory', dirname($filepath));
  4574. $ical->setConfig('filename', basename($filepath));
  4575. $return = $ical->parse();
  4576. //we need to recover: summary, description, dtstart, dtend, organizer, attendee, location (=course name),
  4577. /*
  4578. $ve = $ical->getComponent(VEVENT);
  4579. $ttitle = $ve->getProperty('summary');
  4580. $title = api_convert_encoding($ttitle,$charset,'UTF-8');
  4581. $tdesc = $ve->getProperty('description');
  4582. $desc = api_convert_encoding($tdesc,$charset,'UTF-8');
  4583. $start_date = $ve->getProperty('dtstart');
  4584. $start_date_string = $start_date['year'].'-'.$start_date['month'].'-'.$start_date['day'].' '.$start_date['hour'].':'.$start_date['min'].':'.$start_date['sec'];
  4585. $ts = $ve->getProperty('dtend');
  4586. if ($ts) {
  4587. $end_date_string = $ts['year'].'-'.$ts['month'].'-'.$ts['day'].' '.$ts['hour'].':'.$ts['min'].':'.$ts['sec'];
  4588. } else {
  4589. //Check duration if dtend does not exist
  4590. $duration = $ve->getProperty('duration');
  4591. if ($duration) {
  4592. $duration = $ve->getProperty('duration');
  4593. $duration_string = $duration['year'].'-'.$duration['month'].'-'.$duration['day'].' '.$duration['hour'].':'.$duration['min'].':'.$duration['sec'];
  4594. $start_date_tms = mktime(intval($start_date['hour']), intval($start_date['min']), intval($start_date['sec']), intval($start_date['month']), intval($start_date['day']), intval($start_date['year']));
  4595. //$start_date_tms = mktime(($start_date['hour']), ($start_date['min']), ($start_date['sec']), ($start_date['month']), ($start_date['day']), ($start_date['year']));
  4596. //echo date('d-m-Y - h:i:s', $start_date_tms);
  4597. $end_date_string = mktime(intval($start_date['hour']) +$duration['hour'], intval($start_date['min']) + $duration['min'], intval($start_date['sec']) + $duration['sec'], intval($start_date['month']) + $duration['month'], intval($start_date['day'])+$duration['day'], intval($start_date['year']) + $duration['year']);
  4598. $end_date_string = date('Y-m-d H:i:s', $end_date_string);
  4599. //echo date('d-m-Y - h:i:s', $end_date_string);
  4600. }
  4601. }
  4602. //echo $start_date.' - '.$end_date;
  4603. $organizer = $ve->getProperty('organizer');
  4604. $attendee = $ve->getProperty('attendee');
  4605. $course_name = $ve->getProperty('location');
  4606. //insert the event in our database
  4607. $id = agenda_add_item($course_info,$title,$desc,$start_date_string,$end_date_string,$_POST['selectedform']);
  4608. $repeat = $ve->getProperty('rrule');
  4609. if(is_array($repeat) && !empty($repeat['FREQ'])) {
  4610. $trans = array('DAILY'=>'daily','WEEKLY'=>'weekly','MONTHLY'=>'monthlyByDate','YEARLY'=>'yearly');
  4611. $freq = $trans[$repeat['FREQ']];
  4612. $interval = $repeat['INTERVAL'];
  4613. if(isset($repeat['UNTIL']) && is_array($repeat['UNTIL'])) {
  4614. $until = mktime(23,59,59,$repeat['UNTIL']['month'],$repeat['UNTIL']['day'],$repeat['UNTIL']['year']);
  4615. $res = agenda_add_repeat_item($course_info,$id,$freq,$until,$_POST['selectedform']);
  4616. } */
  4617. $eventcount = 0;
  4618. $message = array();
  4619. $agenda_obj = new Agenda();
  4620. while (true) {
  4621. //we need to recover: summary, description, dtstart, dtend, organizer, attendee, location (=course name)
  4622. $ve = $ical->getComponent('VEVENT', $eventcount);
  4623. if (!$ve) {
  4624. break;
  4625. }
  4626. $ttitle = $ve->getProperty('summary');
  4627. $title = api_convert_encoding($ttitle, $charset, 'UTF-8');
  4628. $tdesc = $ve->getProperty('description');
  4629. $desc = api_convert_encoding($tdesc, $charset, 'UTF-8');
  4630. $start_date = $ve->getProperty('dtstart', false, true);
  4631. if (isset($start_date['params']['VALUE'])) {
  4632. $start_date_value = $start_date['value'];
  4633. if ($start_date['params']['VALUE'] == 'DATE') {
  4634. $start_date_string = $start_date_value['year'].'-'.$start_date_value['month'].'-'.$start_date_value['day'].'';
  4635. } else {
  4636. $start_date_string = $start_date_value['year'].'-'.$start_date_value['month'].'-'.$start_date_value['day'].' '.$start_date_value['hour'].':'.$start_date_value['min'].':'.$start_date_value['sec'];
  4637. }
  4638. } else {
  4639. continue;
  4640. }
  4641. $ts = $ve->getProperty('dtend');
  4642. if ($ts) {
  4643. $end_date = $ve->getProperty('dtend', false, true);
  4644. if (isset($end_date['params']['VALUE'])) {
  4645. $end_date_value = $end_date['value'];
  4646. if ($end_date['params']['VALUE'] == 'DATE') {
  4647. $end_date_string = $end_date_value['year'].'-'.$end_date_value['month'].'-'.$end_date_value['day'].'';
  4648. } else {
  4649. $end_date_string = $end_date_value['year'].'-'.$end_date_value['month'].'-'.$end_date_value['day'].' '.$end_date_value['hour'].':'.$end_date_value['min'].':'.$end_date_value['sec'];
  4650. }
  4651. } else {
  4652. //Default behaviour
  4653. $end_date_string = $ts['year'].'-'.$ts['month'].'-'.$ts['day'].' '.$ts['hour'].':'.$ts['min'].':'.$ts['sec'];
  4654. }
  4655. } else {
  4656. //Check duration if dtend does not exist
  4657. $duration = $ve->getProperty('duration');
  4658. if ($duration) {
  4659. $duration = $ve->getProperty('duration');
  4660. $duration_string = $duration['year'].'-'.$duration['month'].'-'.$duration['day'].' '.$duration['hour'].':'.$duration['min'].':'.$duration['sec'];
  4661. $start_date_tms = mktime(
  4662. intval($start_date['hour']),
  4663. intval($start_date['min']),
  4664. intval($start_date['sec']),
  4665. intval($start_date['month']),
  4666. intval($start_date['day']),
  4667. intval($start_date['year'])
  4668. );
  4669. //$start_date_tms = mktime(($start_date['hour']), ($start_date['min']), ($start_date['sec']), ($start_date['month']), ($start_date['day']), ($start_date['year']));
  4670. //echo date('d-m-Y - h:i:s', $start_date_tms);
  4671. $end_date_string = mktime(
  4672. intval($start_date['hour']) + $duration['hour'],
  4673. intval($start_date['min']) + $duration['min'],
  4674. intval($start_date['sec']) + $duration['sec'],
  4675. intval($start_date['month']) + $duration['month'],
  4676. intval($start_date['day']) + $duration['day'],
  4677. intval($start_date['year']) + $duration['year']
  4678. );
  4679. $end_date_string = api_get_utc_datetime($end_date_string);
  4680. //echo date('d-m-Y - h:i:s', $end_date_string);
  4681. }
  4682. }
  4683. //echo $start_date.' - '.$end_date;
  4684. $organizer = $ve->getProperty('organizer');
  4685. $attendee = $ve->getProperty('attendee');
  4686. $course_name = $ve->getProperty('location');
  4687. //insert the event in our database
  4688. $agenda_obj->type = 'course';
  4689. $all_day = 'false';
  4690. if ($start_date_string == $end_date_string) {
  4691. $all_day = 'true';
  4692. }
  4693. $date = new DateTime($start_date_string);
  4694. $date->add(new DateInterval('P1D'));
  4695. if ($start_date_string == $date->format('Y-m-d h:i:s')) {
  4696. $all_day = 'true';
  4697. }
  4698. $id = $agenda_obj->add_event(
  4699. $start_date_string,
  4700. $end_date_string,
  4701. $all_day,
  4702. null,
  4703. $title,
  4704. $desc,
  4705. array('everyone')
  4706. );
  4707. $message[] = " $title - ".$start_date_string." - ".$end_date_string;
  4708. $repeat = $ve->getProperty('rrule');
  4709. if (is_array($repeat) && !empty($repeat['FREQ'])) {
  4710. $trans = array(
  4711. 'DAILY' => 'daily',
  4712. 'WEEKLY' => 'weekly',
  4713. 'MONTHLY' => 'monthlyByDate',
  4714. 'YEARLY' => 'yearly'
  4715. );
  4716. $freq = $trans[$repeat['FREQ']];
  4717. $interval = $repeat['INTERVAL'];
  4718. if (isset($repeat['UNTIL']) && is_array($repeat['UNTIL'])) {
  4719. $until = mktime(
  4720. 23,
  4721. 59,
  4722. 59,
  4723. $repeat['UNTIL']['month'],
  4724. $repeat['UNTIL']['day'],
  4725. $repeat['UNTIL']['year']
  4726. );
  4727. $res = agenda_add_repeat_item($course_info, $id, $freq, $until, $attendee);
  4728. }
  4729. //TODO: deal with count
  4730. if (!empty($repeat['COUNT'])) {
  4731. $count = $repeat['COUNT'];
  4732. $res = agenda_add_repeat_item($course_info, $id, $freq, $count, $attendee);
  4733. }
  4734. }
  4735. $eventcount++;
  4736. }
  4737. if (!empty($message)) {
  4738. $message = implode('<br /> ', $message);
  4739. }
  4740. return $message;
  4741. }
  4742. /**
  4743. * This function retrieves one personal agenda item returns it.
  4744. * @param array The array containing existing events. We add to this array.
  4745. * @param int Day
  4746. * @param int Month
  4747. * @param int Year (4 digits)
  4748. * @param int Week number
  4749. * @param string Type of view (month_view, week_view, day_view)
  4750. * @return array The results of the database query, or null if not found
  4751. */
  4752. function get_global_agenda_items($agendaitems, $day = "", $month = "", $year = "", $week = "", $type)
  4753. {
  4754. $tbl_global_agenda = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  4755. $month = Database::escape_string($month);
  4756. $year = Database::escape_string($year);
  4757. $week = Database::escape_string($week);
  4758. $day = Database::escape_string($day);
  4759. // 1. creating the SQL statement for getting the personal agenda items in MONTH view
  4760. $current_access_url_id = api_get_current_access_url_id();
  4761. if ($type == "month_view" or $type == "") {
  4762. // We are in month view
  4763. $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";
  4764. }
  4765. // 2. creating the SQL statement for getting the personal agenda items in WEEK view
  4766. if ($type == "week_view") { // we are in week view
  4767. $start_end_day_of_week = calculate_start_end_of_week($week, $year);
  4768. $start_day = $start_end_day_of_week['start']['day'];
  4769. $start_month = $start_end_day_of_week['start']['month'];
  4770. $start_year = $start_end_day_of_week['start']['year'];
  4771. $end_day = $start_end_day_of_week['end']['day'];
  4772. $end_month = $start_end_day_of_week['end']['month'];
  4773. $end_year = $start_end_day_of_week['end']['year'];
  4774. // in sql statements you have to use year-month-day for date calculations
  4775. $start_filter = $start_year."-".$start_month."-".$start_day." 00:00:00";
  4776. $start_filter = api_get_utc_datetime($start_filter);
  4777. $end_filter = $end_year."-".$end_month."-".$end_day." 23:59:59";
  4778. $end_filter = api_get_utc_datetime($end_filter);
  4779. $sql = " SELECT * FROM ".$tbl_global_agenda." WHERE start_date>='".$start_filter."' AND start_date<='".$end_filter."' AND access_url_id = $current_access_url_id ";
  4780. }
  4781. // 3. creating the SQL statement for getting the personal agenda items in DAY view
  4782. if ($type == "day_view") { // we are in day view
  4783. // we could use mysql date() function but this is only available from 4.1 and higher
  4784. $start_filter = $year."-".$month."-".$day." 00:00:00";
  4785. $start_filter = api_get_utc_datetime($start_filter);
  4786. $end_filter = $year."-".$month."-".$day." 23:59:59";
  4787. $end_filter = api_get_utc_datetime($end_filter);
  4788. $sql = " SELECT * FROM ".$tbl_global_agenda." WHERE start_date>='".$start_filter."' AND start_date<='".$end_filter."' AND access_url_id = $current_access_url_id";
  4789. }
  4790. $result = Database::query($sql);
  4791. while ($item = Database::fetch_array($result)) {
  4792. if ($item['start_date'] != '0000-00-00 00:00:00') {
  4793. $item['start_date'] = api_get_local_time($item['start_date']);
  4794. $item['start_date_tms'] = api_strtotime($item['start_date']);
  4795. }
  4796. if ($item['end_date'] != '0000-00-00 00:00:00') {
  4797. $item['end_date'] = api_get_local_time($item['end_date']);
  4798. }
  4799. // we break the date field in the database into a date and a time part
  4800. $agenda_db_date = explode(" ", $item['start_date']);
  4801. $date = $agenda_db_date[0];
  4802. $time = $agenda_db_date[1];
  4803. // we divide the date part into a day, a month and a year
  4804. $agendadate = explode("-", $date);
  4805. $year = intval($agendadate[0]);
  4806. $month = intval($agendadate[1]);
  4807. $day = intval($agendadate[2]);
  4808. // we divide the time part into hour, minutes, seconds
  4809. $agendatime = explode(":", $time);
  4810. $hour = $agendatime[0];
  4811. $minute = $agendatime[1];
  4812. $second = $agendatime[2];
  4813. if ($type == 'month_view') {
  4814. $item['calendar_type'] = 'global';
  4815. $agendaitems[$day][] = $item;
  4816. continue;
  4817. }
  4818. $start_time = api_format_date($item['start_date'], TIME_NO_SEC_FORMAT);
  4819. $end_time = '';
  4820. if ($item['end_date'] != '0000-00-00 00:00:00') {
  4821. $end_time = ' - '.api_format_date($item['end_date'], DATE_TIME_FORMAT_LONG);
  4822. }
  4823. // if the student has specified a course we a add a link to that course
  4824. if ($item['course'] <> "") {
  4825. $url = api_get_path(WEB_CODE_PATH)."admin/agenda.php?cidReq=".urlencode(
  4826. $item['course']
  4827. )."&day=$day&month=$month&year=$year#$day"; // RH //Patrick Cool: to highlight the relevant agenda item
  4828. $course_link = "<a href=\"$url\" title=\"".$item['course']."\">".$item['course']."</a>";
  4829. } else {
  4830. $course_link = "";
  4831. }
  4832. // Creating the array that will be returned. If we have week or month view we have an array with the date as the key
  4833. // if we have a day_view we use a half hour as index => key 33 = 16h30
  4834. if ($type !== "day_view") {
  4835. // This is the array construction for the WEEK or MONTH view
  4836. //Display the Agenda global in the tab agenda (administrator)
  4837. $agendaitems[$day] .= "<i>$start_time $end_time</i>&nbsp;-&nbsp;";
  4838. $agendaitems[$day] .= "<b>".get_lang('GlobalEvent')."</b>";
  4839. $agendaitems[$day] .= "<div>".$item['title']."</div><br>";
  4840. } else {
  4841. // this is the array construction for the DAY view
  4842. $halfhour = 2 * $agendatime['0'];
  4843. if ($agendatime['1'] >= '30') {
  4844. $halfhour = $halfhour + 1;
  4845. }
  4846. if (!is_array($agendaitems[$halfhour])) {
  4847. $content = $agendaitems[$halfhour];
  4848. }
  4849. $agendaitems[$halfhour] = $content."<div><i>$hour:$minute</i> <b>".get_lang(
  4850. 'GlobalEvent'
  4851. ).": </b>".$item['title']."</div>";
  4852. }
  4853. }
  4854. return $agendaitems;
  4855. }
  4856. function display_ical_import_form()
  4857. {
  4858. $form = new FormValidator('frm_import_ical', 'post', api_get_self().'?action='.Security::remove_XSS(
  4859. $_GET['action']
  4860. ), array('enctype' => 'multipart/form-data'));
  4861. $form->addElement('header', get_lang('ICalFileImport'));
  4862. $form->addElement('file', 'ical_import', get_lang('ICalFileImport'));
  4863. $form->addRule('ical_import', get_lang('ThisFieldIsRequired'), 'required');
  4864. $form->addElement('button', 'ical_submit', get_lang('Import'));
  4865. $form->display();
  4866. }