agenda.inc.php 193 KB

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