agenda.inc.php 188 KB

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