agenda.inc.php 217 KB

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