agenda.inc.php 219 KB

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