agenda.inc.php 179 KB

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