agenda.inc.php 188 KB

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