agenda.inc.php 192 KB

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