agenda.inc.php 220 KB

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