agenda.inc.php 192 KB

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