agenda.inc.php 179 KB

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