agenda.inc.php 179 KB

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