agenda.inc.php 192 KB

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