calendar.lib.php 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132
  1. <?php // $id: $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos S.A.
  6. For a full list of contributors, see "credits.txt".
  7. The full license can be read in "license.txt".
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. See the GNU General Public License for more details.
  13. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  14. ==============================================================================
  15. */
  16. /**
  17. ==============================================================================
  18. * @package dokeos.admin
  19. * @author Carlos Vargas
  20. * This file is the calendar/agenda.inc.php
  21. ==============================================================================
  22. */
  23. /*
  24. -----------------------------------------------------------
  25. Constants and variables
  26. -----------------------------------------------------------
  27. */
  28. // the variables for the days and the months
  29. // Defining the shorts for the days
  30. $DaysShort = array (get_lang("SundayShort"), get_lang("MondayShort"), get_lang("TuesdayShort"), get_lang("WednesdayShort"), get_lang("ThursdayShort"), get_lang("FridayShort"), get_lang("SaturdayShort"));
  31. // Defining the days of the week to allow translation of the days
  32. $DaysLong = array (get_lang("SundayLong"), get_lang("MondayLong"), get_lang("TuesdayLong"), get_lang("WednesdayLong"), get_lang("ThursdayLong"), get_lang("FridayLong"), get_lang("SaturdayLong"));
  33. // Defining the months of the year to allow translation of the months
  34. $MonthsLong = array (get_lang("JanuaryLong"), get_lang("FebruaryLong"), get_lang("MarchLong"), get_lang("AprilLong"), get_lang("MayLong"), get_lang("JuneLong"), get_lang("JulyLong"), get_lang("AugustLong"), get_lang("SeptemberLong"), get_lang("OctoberLong"), get_lang("NovemberLong"), get_lang("DecemberLong"));
  35. /*
  36. ==============================================================================
  37. FUNCTIONS
  38. ==============================================================================
  39. */
  40. /**
  41. * Retrieves all the agenda items from the table
  42. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  43. * @author Yannick Warnier <yannick.warnier@dokeos.com> - cleanup
  44. * @param integer $month: the integer value of the month we are viewing
  45. * @param integer $year: the 4-digit year indication e.g. 2005
  46. * @return array
  47. */
  48. /**
  49. * show the mini calender of the given month
  50. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  51. * @param array an array containing all the agenda items for the given month
  52. * @param integer $month: the integer value of the month we are viewing
  53. * @param integer $year: the 4-digit year indication e.g. 2005
  54. * @param string $monthName: the language variable for the mont name
  55. * @return html code
  56. * @todo refactor this so that $monthName is no longer needed as a parameter
  57. */
  58. function display_minimonthcalendar($agendaitems, $month, $year, $monthName)
  59. {
  60. global $DaysShort;
  61. //Handle leap year
  62. $numberofdays = array (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  63. if (($year % 400 == 0) or ($year % 4 == 0 and $year % 100 <> 0))
  64. $numberofdays[2] = 29;
  65. //Get the first day of the month
  66. $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
  67. //Start the week on monday
  68. $startdayofweek = $dayone['wday'] <> 0 ? ($dayone['wday'] - 1) : 6;
  69. $backwardsURL = api_get_self()."?".api_get_cidreq()."&amp;coursePath=".(empty($_GET['coursePath'])?'':$_GET['coursePath'])."&amp;courseCode=".(empty($_GET['courseCode'])?'':$_GET['courseCode'])."&amp;month=". ($month == 1 ? 12 : $month -1)."&amp;year=". ($month == 1 ? $year -1 : $year);
  70. $forewardsURL = api_get_self()."?".api_get_cidreq()."&amp;coursePath=".(empty($_GET['coursePath'])?'':$_GET['coursePath'])."&amp;courseCode=".(empty($_GET['courseCode'])?'':$_GET['courseCode'])."&amp;month=". ($month == 12 ? 1 : $month +1)."&amp;year=". ($month == 12 ? $year +1 : $year);
  71. echo "<table class=\"data_table\">\n",
  72. "<tr>\n",
  73. "<th width=\"10%\"><a href=\"", $backwardsURL, "\"> &laquo; </a></th>\n",
  74. "<th width=\"80%\" colspan=\"5\">", $monthName, " ", $year, "</th>\n",
  75. "<th width=\"10%\"><a href=\"", $forewardsURL, "\"> &raquo; </a></th>\n", "</tr>\n";
  76. echo "<tr>\n";
  77. for ($ii = 1; $ii < 8; $ii ++)
  78. {
  79. echo "<td class=\"weekdays\">", $DaysShort[$ii % 7], "</td>\n";
  80. }
  81. echo "</tr>\n";
  82. $curday = -1;
  83. $today = getdate();
  84. while ($curday <= $numberofdays[$month])
  85. {
  86. echo "<tr>\n";
  87. for ($ii = 0; $ii < 7; $ii ++)
  88. {
  89. if (($curday == -1) && ($ii == $startdayofweek))
  90. {
  91. $curday = 1;
  92. }
  93. if (($curday > 0) && ($curday <= $numberofdays[$month]))
  94. {
  95. $bgcolor = $ii < 5 ? $class="class=\"days_week\"" : $class="class=\"days_weekend\"";
  96. $dayheader = "$curday";
  97. if (($curday == $today['mday']) && ($year == $today['year']) && ($month == $today['mon']))
  98. {
  99. $dayheader = "$curday";
  100. $class = "class=\"days_today\"";
  101. }
  102. echo "\t<td ".$class.">";
  103. if (!empty($agendaitems[$curday]))
  104. {
  105. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&amp;action=view&amp;view=day&amp;day=".$curday."&amp;month=".$month."&amp;year=".$year."\">".$dayheader."</a>";
  106. }
  107. else
  108. {
  109. echo $dayheader;
  110. }
  111. // "a".$dayheader." <span class=\"agendaitem\">".$agendaitems[$curday]."</span>\n";
  112. echo "</td>\n";
  113. $curday ++;
  114. }
  115. else
  116. {
  117. echo "<td>&nbsp;</td>\n";
  118. }
  119. }
  120. echo "</tr>\n";
  121. }
  122. echo "</table>\n";
  123. }
  124. /**
  125. * show the calender of the given month
  126. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  127. * @param integer $month: the integer value of the month we are viewing
  128. * @param integer $year: the 4-digit year indication e.g. 2005
  129. * @return html code
  130. */
  131. /**
  132. * returns all the javascript that is required for easily selecting the target people/groups this goes into the $htmlHeadXtra[] array
  133. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  134. * @return javascript code
  135. */
  136. function to_javascript()
  137. {
  138. $Send2All=get_lang("Send2All");
  139. return "<script type=\"text/javascript\" language=\"JavaScript\">
  140. <!-- Begin javascript menu swapper
  141. function move(fbox, tbox)
  142. {
  143. var arrFbox = new Array();
  144. var arrTbox = new Array();
  145. var arrLookup = new Array();
  146. var i;
  147. for (i = 0; i < tbox.options.length; i++)
  148. {
  149. arrLookup[tbox.options[i].text] = tbox.options[i].value;
  150. arrTbox[i] = tbox.options[i].text;
  151. }
  152. var fLength = 0;
  153. var tLength = arrTbox.length;
  154. for(i = 0; i < fbox.options.length; i++)
  155. {
  156. arrLookup[fbox.options[i].text] = fbox.options[i].value;
  157. if (fbox.options[i].selected && fbox.options[i].value != \"\")
  158. {
  159. arrTbox[tLength] = fbox.options[i].text;
  160. tLength++;
  161. }
  162. else
  163. {
  164. arrFbox[fLength] = fbox.options[i].text;
  165. fLength++;
  166. }
  167. }
  168. arrFbox.sort();
  169. arrTbox.sort();
  170. fbox.length = 0;
  171. tbox.length = 0;
  172. var c;
  173. for(c = 0; c < arrFbox.length; c++)
  174. {
  175. var no = new Option();
  176. no.value = arrLookup[arrFbox[c]];
  177. no.text = arrFbox[c];
  178. fbox[c] = no;
  179. }
  180. for(c = 0; c < arrTbox.length; c++)
  181. {
  182. var no = new Option();
  183. no.value = arrLookup[arrTbox[c]];
  184. no.text = arrTbox[c];
  185. tbox[c] = no;
  186. }
  187. }
  188. function checkDate(month, day, year)
  189. {
  190. var monthLength =
  191. new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  192. if (!day || !month || !year)
  193. return false;
  194. // check for bisestile year
  195. if (year/4 == parseInt(year/4))
  196. monthLength[1] = 29;
  197. if (month < 1 || month > 12)
  198. return false;
  199. if (day > monthLength[month-1])
  200. return false;
  201. return true;
  202. }
  203. function mktime()
  204. {
  205. var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
  206. d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
  207. var dateManip = {
  208. 0: function(tt){ return d.setHours(tt); },
  209. 1: function(tt){ return d.setMinutes(tt); },
  210. 2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
  211. 3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
  212. 4: function(tt){ return d.setDate(tt+mb); },
  213. 5: function(tt){ return d.setYear(tt+ma); }
  214. };
  215. for( i = 0; i < argc; i++ ){
  216. no = parseInt(argv[i]*1);
  217. if (isNaN(no)) {
  218. return false;
  219. } else {
  220. // arg is number, lets manipulate date object
  221. if(!dateManip[i](no)){
  222. // failed
  223. return false;
  224. }
  225. }
  226. }
  227. return Math.floor(d.getTime()/1000);
  228. }
  229. function validate()
  230. {
  231. var f = document.new_calendar_item;
  232. f.submit();
  233. return true;
  234. }
  235. function validate_date()
  236. {
  237. var start_day = document.new_calendar_item.fday.value;
  238. var start_month = document.new_calendar_item.fmonth.value;
  239. var start_year = document.new_calendar_item.fyear.value;
  240. var start_hour = document.new_calendar_item.fhour.value;
  241. var start_minute = document.new_calendar_item.fminute.value;
  242. var start_date = mktime(start_hour,start_minute,0,start_month,start_day,start_year)
  243. var ends_day = document.new_calendar_item.end_fday.value;
  244. var ends_month = document.new_calendar_item.end_fmonth.value;
  245. var ends_year = document.new_calendar_item.end_fyear.value;
  246. var ends_hour = document.new_calendar_item.end_fhour.value;
  247. var ends_minute = document.new_calendar_item.end_fminute.value;
  248. var ends_date = mktime(ends_hour,ends_minute,0,ends_month,ends_day,ends_year)
  249. msg_err1 = document.getElementById(\"err_date\");
  250. msg_err2 = document.getElementById(\"err_start_date\");
  251. msg_err3 = document.getElementById(\"err_end_date\");
  252. msg_err4 = document.getElementById(\"err_title\");
  253. if (start_date > ends_date) {
  254. msg_err1.style.display =\"block\";
  255. msg_err1.innerHTML=\"".get_lang('EndDateCannotBeBeforeTheStartDate')."\";
  256. msg_err2.innerHTML=\"\";msg_err3.innerHTML=\"\";
  257. } else if (checkDate(start_month,start_day,start_year) == false) {
  258. msg_err2.style.display =\"block\";
  259. msg_err2.innerHTML=\"".get_lang('InvalidDate')."\";
  260. msg_err1.innerHTML=\"\";msg_err3.innerHTML=\"\";
  261. } else if (checkDate(ends_month,ends_day,ends_year) == false) {
  262. msg_err3.style.display =\"block\";
  263. msg_err3.innerHTML=\"".get_lang('InvalidDate')."\";
  264. msg_err1.innerHTML=\"\";msg_err2.innerHTML=\"\";
  265. } else if (document.new_calendar_item.title.value == '') {
  266. msg_err4.style.display =\"block\";
  267. msg_err4.innerHTML=\"".get_lang('FieldRequired')."\";
  268. msg_err1.innerHTML=\"\";msg_err2.innerHTML=\"\";msg_err3.innerHTML=\"\";
  269. } else {
  270. document.new_calendar_item.submit();
  271. }
  272. }
  273. // End -->
  274. </script>";
  275. }
  276. /**
  277. * returns the javascript for setting a filter. This is a jump menu
  278. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  279. * @return javascript code
  280. */
  281. function user_group_filter_javascript()
  282. {
  283. return "<script language=\"JavaScript\" type=\"text/JavaScript\">
  284. <!--
  285. function MM_jumpMenu(targ,selObj,restore){
  286. eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  287. if (restore) selObj.selectedIndex=0;
  288. }
  289. //-->
  290. </script>
  291. ";
  292. }
  293. function display_monthcalendar($month, $year)
  294. {
  295. global $MonthsLong;
  296. global $DaysShort;
  297. global $origin;
  298. // grabbing all the calendar items for this year and storing it in a array
  299. $data=get_calendar_items($month,$year);
  300. //Handle leap year
  301. $numberofdays = array(0,31,28,31,30,31,30,31,31,30,31,30,31);
  302. if (($year%400 == 0) or ($year%4==0 and $year%100<>0)) $numberofdays[2] = 29;
  303. //Get the first day of the month
  304. $dayone = getdate(mktime(0,0,0,$month,1,$year));
  305. //Start the week on monday
  306. $startdayofweek = $dayone['wday']<>0 ? ($dayone['wday']-1) : 6;
  307. $backwardsURL = api_get_self()."?".api_get_cidreq()."&amp;origin=$origin&amp;month=".($month==1 ? 12 : $month-1)."&amp;year=".($month==1 ? $year-1 : $year);
  308. $forewardsURL = api_get_self()."?".api_get_cidreq()."&amp;origin=$origin&amp;month=".($month==12 ? 1 : $month+1)."&amp;year=".($month==12 ? $year+1 : $year);
  309. $maand_array_maandnummer=$month-1;
  310. echo "<table class=\"data_table\">\n",
  311. "<tr>\n",
  312. "<th width=\"10%\"><a href=\"",$backwardsURL,"\"> &laquo; </a></th>\n",
  313. "<th width=\"80%\" colspan=\"5\">",$MonthsLong[$maand_array_maandnummer]," ",$year,"</th>\n",
  314. "<th width=\"10%\"><a href=\"",$forewardsURL,"\"> &raquo; </a></th>\n",
  315. "</tr>\n";
  316. echo "<tr>\n";
  317. for ($ii=1;$ii<8; $ii++)
  318. {
  319. echo "<td class=\"weekdays\" width=\"14%\">",$DaysShort[$ii%7],"</td>\n";
  320. }
  321. echo "</tr>\n";
  322. $curday = -1;
  323. $today = getdate();
  324. while ($curday <=$numberofdays[$month])
  325. {
  326. echo "<tr>\n";
  327. for ($ii=0; $ii<7; $ii++)
  328. {
  329. if (($curday == -1)&&($ii==$startdayofweek))
  330. {
  331. $curday = 1;
  332. }
  333. if (($curday>0)&&($curday<=$numberofdays[$month]))
  334. {
  335. $bgcolor = $ii<5 ? "class=\"row_odd\"" : "class=\"row_even\"";
  336. $dayheader = "$curday";
  337. if (key_exists($curday,$data))
  338. {
  339. $dayheader="<a href='".api_get_self()."?".api_get_cidreq()."&amp;view=list&amp;origin=$origin&amp;month=$month&amp;year=$year&amp;day=$curday#$curday'>".$curday."</a>";
  340. foreach ($data[$curday] as $key=>$agenda_item)
  341. {
  342. foreach ($agenda_item as $key=>$value)
  343. {
  344. $dayheader .= '<br /><b>'.substr($value['start_date'],11,8).'</b>';
  345. $dayheader .= ' - ';
  346. $dayheader .= $value['title'];
  347. }
  348. }
  349. }
  350. if (($curday==$today['mday'])&&($year ==$today['year'])&&($month == $today['mon']))
  351. {
  352. echo "<td id=\"today\" ",$bgcolor,"\">".$dayheader." \n";
  353. }
  354. else
  355. {
  356. echo "<td id=\"days\" ",$bgcolor,"\">".$dayheader." \n";
  357. }
  358. echo "</td>\n";
  359. $curday++;
  360. }
  361. else
  362. {
  363. echo "<td>&nbsp;</td>";
  364. }
  365. }
  366. echo "</tr>";
  367. }
  368. echo "</table>";
  369. }
  370. /**
  371. * this function shows the form with the user that were not selected
  372. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  373. * @return html code
  374. */
  375. /**
  376. * This function stores the Agenda Item in the table calendar_event and updates the item_property table also
  377. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  378. * @return integer the id of the last added agenda item
  379. */
  380. function store_new_agenda_item()
  381. {
  382. global $_user /*, $_course*/;
  383. $TABLEAGENDA = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  384. //$t_agenda_repeat = Database::get_course_Table(TABLE_AGENDA_REPEAT);
  385. // some filtering of the input data
  386. $title=strip_tags(trim($_POST['title'])); // no html allowed in the title
  387. $content=trim($_POST['content']);
  388. $start_date=(int)$_POST['fyear']."-".(int)$_POST['fmonth']."-".(int)$_POST['fday']." ".(int)$_POST['fhour'].":".(int)$_POST['fminute'].":00";
  389. $end_date=(int)$_POST['end_fyear']."-".(int)$_POST['end_fmonth']."-".(int)$_POST['end_fday']." ".(int)$_POST['end_fhour'].":".(int)$_POST['end_fminute'].":00";
  390. // store in the table calendar_event
  391. $sql = "INSERT INTO ".$TABLEAGENDA."
  392. (title,content, start_date, end_date)
  393. VALUES
  394. ('".$title."','".$content."', '".$start_date."','".$end_date."')";
  395. $result = api_sql_query($sql,__FILE__,__LINE__) or die (Database::error());
  396. $last_id=Database::insert_id();
  397. // store in last_tooledit (first the groups, then the users
  398. $to=$_POST['selectedform'];
  399. /* if ((!is_null($to))or (!empty($_SESSION['toolgroup']))) // !is_null($to): when no user is selected we send it to everyone
  400. {
  401. $send_to=separate_users_groups($to);
  402. // storing the selected groups
  403. if (is_array($send_to['groups']))
  404. {
  405. foreach ($send_to['groups'] as $group)
  406. {
  407. api_item_property_update($_course, TOOL_CALENDAR_EVENT, $last_id,"AgendaAdded", $_user['user_id'], $group,'',$start_date, $end_date);
  408. }
  409. }
  410. // storing the selected users
  411. if (is_array($send_to['users']))
  412. {
  413. foreach ($send_to['users'] as $user)
  414. {
  415. api_item_property_update($_course, TOOL_CALENDAR_EVENT, $last_id,"AgendaAdded", $_user['user_id'],'',$user, $start_date,$end_date);
  416. }
  417. }
  418. }
  419. else // the message is sent to everyone, so we set the group to 0
  420. {
  421. api_item_property_update($_course, TOOL_CALENDAR_EVENT, $last_id,"AgendaAdded", $_user['user_id'], '','',$start_date,$end_date);
  422. }*/
  423. // storing the resources
  424. //store_resources($_SESSION['source_type'],$last_id);
  425. //if repetitive, insert element into agenda_repeat table
  426. /* if(!empty($_POST['repeat']) && !empty($_POST['repeat_type']))
  427. {
  428. if(!empty($_POST['repeat_end_year']) && !empty($_POST['repeat_end_month']) && !empty($_POST['repeat_end_day']))
  429. {
  430. $end_y = intval($_POST['repeat_end_year']);
  431. $end_m = intval($_POST['repeat_end_month']);
  432. $end_d = intval($_POST['repeat_end_day']);
  433. $end = mktime((int)$_POST['fhour'],(int)$_POST['fminute'],0,$end_m,$end_d,$end_y);
  434. $now = time();
  435. $type = Database::escape_string($_POST['repeat_type']);
  436. if($end > $now
  437. && in_array($type,array('daily','weekly','monthlyByDate','monthlyByDay','monthlyByDayR','yearly')))
  438. {
  439. $sql = "INSERT INTO $t_agenda_repeat (cal_id, cal_type, cal_end)" .
  440. " VALUES ($last_id,'$type',$end)";
  441. $res = Database::query($sql,__FILE__,__LINE__);
  442. }
  443. }
  444. }
  445. return $last_id;*/
  446. }
  447. /**
  448. * The links that allows the student AND course administrator to show all agenda items and sort up/down
  449. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  450. */
  451. function display_courseadmin_links() {
  452. echo "<a href='".api_get_self()."?".api_get_cidreq()."&action=add&amp;origin=".Security::remove_XSS($_GET['origin'])."'>".Display::return_icon('calendar_personal_add.gif', get_lang('AgendaAdd'))." ".get_lang('AgendaAdd')."</a>";
  453. }
  454. function display_student_links()
  455. {
  456. global $show;
  457. if ($_SESSION['sort'] == 'DESC')
  458. {
  459. echo "<a href='".api_get_self()."?sort=asc&amp;origin=".$_GET['origin']."'>".Display::return_icon('calendar_up.gif',get_lang('AgendaSortChronologicallyUp')).' '.get_lang("AgendaSortChronologicallyUp")."</a>";
  460. }
  461. else
  462. {
  463. echo "<a href='".api_get_self()."?sort=desc&amp;origin=".$_GET['origin']."'>".Display::return_icon('calendar_down.gif',get_lang('AgendaSortChronologicallyDown')).' '.get_lang("AgendaSortChronologicallyDown")."</a>";
  464. }
  465. if ($_SESSION['view'] <> 'month')
  466. {
  467. echo "\t<a href=\"".api_get_self()."?action=view&amp;view=month\"><img src=\"../img/calendar_month.gif\" border=\"0\" alt=\"".get_lang('MonthView')."\" /> ".get_lang('MonthView')."</a>\n";
  468. }
  469. else
  470. {
  471. echo "\t<a href=\"".api_get_self()."?action=view&amp;view=list\"><img src=\"../img/calendar_select.gif\" border=\"0\" alt=\"".get_lang('ListView')."\" /> ".get_lang('ListView')."</a>\n";
  472. }
  473. }
  474. /**
  475. * get all the information of the agenda_item from the database
  476. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  477. * @param integer the id of the agenda item we are getting all the information of
  478. * @return an associative array that contains all the information of the agenda item. The keys are the database fields
  479. */
  480. function get_agenda_item($id)
  481. {
  482. $TABLEAGENDA = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  483. //$t_agenda_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT);
  484. $item = array();
  485. if(empty($id))
  486. {
  487. $id=(int)addslashes($_GET['id']);
  488. }
  489. else
  490. {
  491. $id = (int) $id;
  492. }
  493. if(empty($id)){return $item;}
  494. $sql = "SELECT * FROM ".$TABLEAGENDA." WHERE id='".$id."'";
  495. $result = api_sql_query($sql,__FILE__,__LINE__);
  496. $entry_to_edit = Database::fetch_array($result);
  497. $item['title'] = $entry_to_edit["title"];
  498. $item['content'] = $entry_to_edit["content"];
  499. $item['start_date'] = $entry_to_edit["start_date"];
  500. $item['end_date'] = $entry_to_edit["end_date"];
  501. $item['to'] == "everyone";
  502. // if the item has been sent to everybody then we show the compact to form
  503. if ($item['to']=="everyone")
  504. {
  505. $_SESSION['allow_individual_calendar']="hide";
  506. }
  507. else
  508. {
  509. $_SESSION['allow_individual_calendar']="show";
  510. }
  511. return $item;
  512. }
  513. /**
  514. * This is the function that updates an agenda item. It does 3 things
  515. * 1. storethe start_date, end_date, title and message in the calendar_event table
  516. * 2. store the groups/users who this message is meant for in the item_property table
  517. * 3. modify the attachments (if needed)
  518. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  519. */
  520. function store_edited_agenda_item()
  521. {
  522. // STEP 1: editing the calendar_event table
  523. // 1.a. some filtering of the input data
  524. $id=(int)$_POST['id'];
  525. $title=strip_tags(trim($_POST['title'])); // no html allowed in the title
  526. $content=trim($_POST['content']);
  527. $start_date=(int)$_POST['fyear']."-".(int)$_POST['fmonth']."-".(int)$_POST['fday']." ".(int)$_POST['fhour'].":".(int)$_POST['fminute'].":00";
  528. $end_date=(int)$_POST['end_fyear']."-".(int)$_POST['end_fmonth']."-".(int)$_POST['end_fday']." ".(int)$_POST['end_fhour'].":".(int)$_POST['end_fminute'].":00";
  529. $to=$_POST['selectedform'];
  530. // 1.b. the actual saving in calendar_event table
  531. $edit_result=save_edit_agenda_item($id,$title,$content,$start_date,$end_date);
  532. echo '<br />';
  533. Display::display_normal_message(get_lang("EditSuccess"));
  534. }
  535. /**
  536. * This function stores the Agenda Item in the table calendar_event and updates the item_property table also (after an edit)
  537. * @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  538. */
  539. function save_edit_agenda_item($id,$title,$content,$start_date,$end_date)
  540. {
  541. $TABLEAGENDA = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  542. $id=Database::escape_string($id);
  543. $title=Database::escape_string($title);
  544. $content=Database::escape_string($content);
  545. $start_date=Database::escape_string($start_date);
  546. $end_date=Database::escape_string($end_date);
  547. // store the modifications in the table calendar_event
  548. $sql = "UPDATE ".$TABLEAGENDA."
  549. SET title='".$title."',
  550. content='".$content."',
  551. start_date='".$start_date."',
  552. end_date='".$end_date."'
  553. WHERE id='".$id."'";
  554. $result = api_sql_query($sql,__FILE__,__LINE__) or die (Database::error());
  555. return true;
  556. }
  557. /**
  558. * This is the function that deletes an agenda item.
  559. * The agenda item is no longer fycically deleted but the visibility in the item_property table is set to 2
  560. * which means that it is invisible for the student AND course admin. Only the platform administrator can see it.
  561. * This will in a later stage allow the platform administrator to recover resources that were mistakenly deleted
  562. * by the course administrator
  563. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  564. * @param integer the id of the agenda item wa are deleting
  565. */
  566. function delete_agenda_item($id)
  567. {
  568. global $_course;
  569. $id=Database::escape_string($id);
  570. if (is_allowed_to_edit() && !api_is_anonymous())
  571. {
  572. if (!empty($_GET['id']) && isset($_GET['action']) && $_GET['action']=="delete")
  573. {
  574. $t_agenda = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  575. $id=(int)addslashes($_GET['id']);
  576. $sql = "SELECT * FROM $t_agenda WHERE id = $id";
  577. $res = Database::query($sql,__FILE__,__LINE__);
  578. if(Database::num_rows($res)>0)
  579. {
  580. $sql = "DELETE FROM ".$t_agenda." WHERE id='$id'";
  581. $result = api_sql_query($sql,__FILE__,__LINE__) or die (Database::error());
  582. }
  583. api_item_property_update($_course,TOOL_CALENDAR_EVENT,$id,'delete',api_get_user_id());
  584. $id=null;
  585. echo '<br />';
  586. Display::display_normal_message(get_lang("AgendaDeleteSuccess"));
  587. }
  588. }
  589. }
  590. /**
  591. * Makes an agenda item visible or invisible for a student
  592. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  593. * @param integer id the id of the agenda item we are changing the visibility of
  594. */
  595. function showhide_agenda_item($id)
  596. {
  597. global $nameTools;
  598. /*==================================================
  599. SHOW / HIDE A CALENDAR ITEM
  600. ==================================================*/
  601. // and $_GET['isStudentView']<>"false" is added to prevent that the visibility is changed after you do the following:
  602. // change visibility -> studentview -> course manager view
  603. if ((is_allowed_to_edit() && !api_is_anonymous()) and $_GET['isStudentView']<>"false")
  604. {
  605. if (isset($_GET['id'])&&$_GET['id']&&isset($_GET['action'])&&$_GET['action']=="showhide")
  606. {
  607. $id=(int)addslashes($_GET['id']);
  608. change_visibility($nameTools,$id);
  609. Display::display_normal_message(get_lang("VisibilityChanged"));
  610. }
  611. }
  612. }
  613. /**
  614. * Displays all the agenda items
  615. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  616. * @author Yannick Warnier <yannick.warnier@dokeos.com> - cleanup
  617. */
  618. function display_agenda_items()
  619. {
  620. $TABLEAGENDA = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  621. global $select_month, $select_year;
  622. global $DaysShort, $DaysLong, $MonthsLong;
  623. global $is_courseAdmin;
  624. global $dateFormatLong, $timeNoSecFormat,$charset, $_user, $_course;
  625. // getting the group memberships
  626. //$group_memberships=GroupManager::get_group_ids($_course['dbName'],$_user['user_id']);
  627. // getting the name of the groups
  628. //$group_names=get_course_groups();
  629. /*--------------------------------------------------
  630. CONSTRUCT THE SQL STATEMENT
  631. --------------------------------------------------*/
  632. $start = 0;
  633. $stop = 0;
  634. // this is to make a difference between showing everything (all months) or only the current month)
  635. // $show_all_current is a part of the sql statement
  636. if ($_SESSION['show']!=="showall")
  637. {
  638. $show_all_current=" AND MONTH(start_date)=$select_month AND year(start_date)=$select_year";
  639. $start = mktime(0,0,0,$select_month,1,$select_year);
  640. $stop = 0;
  641. if(empty($select_year)){$select_year = date('Y');}
  642. if(empty($select_month)){$select_month = date('m');}
  643. if($select_month==12)
  644. {
  645. $stop = mktime(0,0,0,1,1,$select_year+1)-1;
  646. }
  647. else
  648. {
  649. $stop = mktime(0,0,0,$select_month+1,1,$select_year)-1;
  650. }
  651. }
  652. else
  653. {
  654. $show_all_current="";
  655. $start = time();
  656. $stop = mktime(0,0,0,1,1,2038);//by default, set year to maximum for mktime()
  657. }
  658. // 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
  659. $repeats = array(); //placeholder for repeated events
  660. if (is_allowed_to_edit() && !api_is_anonymous())
  661. {
  662. $sql="SELECT * FROM ".$TABLEAGENDA.' ORDER BY start_date '.$_SESSION['sort'];
  663. } // you are a student
  664. //echo "<pre>".$sql."</pre>";
  665. $result=api_sql_query($sql,__FILE__,__LINE__) or die(Database::error());
  666. $number_items=Database::num_rows($result);
  667. /*--------------------------------------------------
  668. DISPLAY: NO ITEMS
  669. --------------------------------------------------*/
  670. if ($number_items==0)
  671. {
  672. echo "<table class=\"data_table\" ><tr><td>".get_lang("NoAgendaItems")."</td></tr></table>";
  673. }
  674. /*--------------------------------------------------
  675. DISPLAY: THE ITEMS
  676. --------------------------------------------------*/
  677. $month_bar="";
  678. $event_list="";
  679. $counter=0;
  680. $export_icon = 'export.png';
  681. $export_icon_low = 'export_low_fade.png';
  682. $export_icon_high = 'export_high_fade.png';
  683. while($myrow=Database::fetch_array($result))
  684. {
  685. $is_repeated = !empty($myrow['parent_event_id']);
  686. echo '<table class="data_table">',"\n";
  687. /*--------------------------------------------------
  688. display: the month bar
  689. --------------------------------------------------*/
  690. // Make the month bar appear only once.
  691. if ($month_bar != date("m",strtotime($myrow["start_date"])).date("Y",strtotime($myrow["start_date"])))
  692. {
  693. $month_bar = date("m",strtotime($myrow["start_date"])).date("Y",strtotime($myrow["start_date"]));
  694. echo "\t<tr>\n\t\t<td class=\"agenda_month_divider\" colspan=\"3\" valign=\"top\">".
  695. ucfirst(format_locale_date("%B %Y",strtotime($myrow["start_date"]))).
  696. "</td>\n\t</tr>\n";
  697. }
  698. /*--------------------------------------------------
  699. display: the icon, title, destinees of the item
  700. -------------------------------------------------*/
  701. echo '<tr>';
  702. // highlight: if a date in the small calendar is clicked we highlight the relevant items
  703. $db_date=(int)date("d",strtotime($myrow["start_date"])).date("n",strtotime($myrow["start_date"])).date("Y",strtotime($myrow["start_date"]));
  704. if ($_GET["day"].$_GET["month"].$_GET["year"] <>$db_date)
  705. {
  706. if ($myrow['visibility']=='0')
  707. {
  708. $style="data_hidden";
  709. $stylenotbold="datanotbold_hidden";
  710. $text_style="text_hidden";
  711. }
  712. else
  713. {
  714. $style="data";
  715. $stylenotbold="datanotbold";
  716. $text_style="text";
  717. }
  718. }
  719. else
  720. {
  721. $style="datanow";
  722. $stylenotbold="datanotboldnow";
  723. $text_style="textnow";
  724. }
  725. echo "\t\t<th>\n";
  726. // adding an internal anchor
  727. echo "\t\t\t<a name=\"".(int)date("d",strtotime($myrow["start_date"]))."\"></a>";
  728. // the icons. If the message is sent to one or more specific users/groups
  729. // we add the groups icon
  730. // 2do: if it is sent to groups we display the group icon, if it is sent to a user we show the user icon
  731. Display::display_icon('agenda.gif', get_lang('Agenda'));
  732. if ($myrow['to_group_id']!=='0')
  733. {
  734. echo Display::return_icon('group.gif', get_lang('Group'));
  735. }
  736. echo " ".$myrow['title']."\n";
  737. echo "\t\t</th>\n";
  738. // the message has been sent to
  739. echo "\t\t<th>".get_lang("SentTo").": ".get_lang('AllUsersOfThePlatform');
  740. //$sent_to=sent_to(TOOL_CALENDAR_EVENT, $myrow["ref"]);
  741. //$sent_to_form=sent_to_form($sent_to);
  742. // echo $sent_to_form;
  743. echo "</th>";
  744. if (!$is_repeated && (api_is_allowed_to_edit(false,true) OR (api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous())))
  745. {
  746. if( ! (api_is_course_coach() && !api_is_element_in_the_session(TOOL_AGENDA, $myrow['id'] ) ) )
  747. { // a coach can only delete an element belonging to his session
  748. echo '<th>'.get_lang('Modify');
  749. echo '</th></tr>';
  750. }
  751. }
  752. /*--------------------------------------------------
  753. display: the title
  754. --------------------------------------------------*/
  755. echo "<tr class='row_odd'>";
  756. echo "\t\t<td>".get_lang("StartTimeWindow").": ";
  757. echo ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["start_date"])))."&nbsp;&nbsp;&nbsp;";
  758. echo ucfirst(strftime($timeNoSecFormat,strtotime($myrow["start_date"])))."";
  759. echo "</td>\n";
  760. echo "\t\t<td>";
  761. if ($myrow["end_date"]<>"0000-00-00 00:00:00")
  762. {
  763. echo get_lang("EndTimeWindow").": ";
  764. echo ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["end_date"])))."&nbsp;&nbsp;&nbsp;";
  765. echo ucfirst(strftime($timeNoSecFormat,strtotime($myrow["end_date"])))."";
  766. }
  767. echo "</td>\n";
  768. // attachment list
  769. //$attachment_list=get_attachment($myrow['id']);
  770. /*--------------------------------------------------
  771. display: edit delete button (course admin only)
  772. --------------------------------------------------*/
  773. if (!$is_repeated && (api_is_allowed_to_edit(false,true) OR (api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous())))
  774. {
  775. if( ! (api_is_course_coach() && !api_is_element_in_the_session(TOOL_AGENDA, $myrow['id'] ) ) )
  776. { // a coach can only delete an element belonging to his session
  777. $mylink = api_get_self().'?'.api_get_cidreq().'&amp;origin='.Security::remove_XSS($_GET['origin']).'&amp;id='.$myrow['id'];
  778. echo '<td align="center">';
  779. // edit
  780. echo '<a href="'.$mylink.'&amp;action=edit&amp;title="'.get_lang("ModifyCalendarItem").'">';
  781. echo Display::return_icon('edit.gif', get_lang('ModifyCalendarItem'))."</a>";
  782. echo "<a href=\"".$mylink."&amp;action=delete\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset))."')) return false;\" title=\"".get_lang("Delete")."\"> ";
  783. echo Display::return_icon('delete.gif', get_lang('Delete'))."</a>";
  784. }
  785. if (!$is_repeated && (api_is_allowed_to_edit(false,true) OR (api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous())))
  786. {
  787. if( ! (api_is_course_coach() && !api_is_element_in_the_session(TOOL_AGENDA, $myrow['id'] ) ) )
  788. { // a coach can only delete an element belonging to his session
  789. $td_colspan= '<td colspan="3">';
  790. }
  791. else
  792. {
  793. $td_colspan= '<td colspan="2">';
  794. }
  795. }
  796. else
  797. {
  798. $td_colspan= '<td colspan="2">';
  799. }
  800. $mylink = 'calendar_ical_export.php?'.api_get_cidreq().'&amp;type=course&amp;id='.$myrow['id'];
  801. echo '<a class="ical_export" href="'.$mylink.'&amp;class=confidential" title="'.get_lang('ExportiCalConfidential').'">'.Display::return_icon($export_icon_high, get_lang('ExportiCalConfidential')).'</a> ';
  802. echo '<a class="ical_export" href="'.$mylink.'&amp;class=private" title="'.get_lang('ExportiCalPrivate').'">'.Display::return_icon($export_icon_low, get_lang('ExportiCalPrivate')).'</a> ';
  803. echo '<a class="ical_export" href="'.$mylink.'&amp;class=public" title="'.get_lang('ExportiCalPublic').'">'.Display::return_icon($export_icon, get_lang('ExportiCalPublic')).'</a> ';
  804. echo '<a href="#" onclick="javascript:win_print=window.open(\'calendar_view_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('print.gif', get_lang('Print')).'</a>&nbsp;';
  805. echo '</td>';
  806. echo '</tr>';
  807. }
  808. /*--------------------------------------------------
  809. display: the content
  810. --------------------------------------------------*/
  811. $content = $myrow['content'];
  812. $content = make_clickable($content);
  813. $content = text_filter($content);
  814. echo "<tr class='row_even'>";
  815. echo "<td colspan='3'>";
  816. echo $content;
  817. // show attachment list
  818. if (!empty($attachment_list)) {
  819. $realname=$attachment_list['path'];
  820. $user_filename=$attachment_list['filename'];
  821. $full_file_name = 'download.php?file='.$realname;
  822. echo Display::return_icon('attachment.gif',get_lang('Attachment'));
  823. echo '<a href="'.$full_file_name.'';
  824. echo ' "> '.$user_filename.' </a>';
  825. echo '<span class="forum_attach_comment" >'.$attachment_list['comment'].'</span>';
  826. if (api_is_allowed_to_edit()) {
  827. echo '&nbsp;&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;origin='.Security::remove_XSS($_GET['origin']).'&amp;action=delete_attach&amp;id_attach='.$attachment_list['id'].'" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset)).'\')) return false;">'.Display::return_icon('delete.gif',get_lang('Delete')).'</a><br />';
  828. }
  829. }
  830. echo '</td></tr>';
  831. /*--------------------------------------------------
  832. display: the added resources
  833. --------------------------------------------------
  834. if (check_added_resources("Agenda", $myrow["id"]))
  835. {
  836. echo '<tr>';
  837. echo '<td colspan="3">';
  838. echo "<i>".get_lang("AddedResources")."</i><br/>";
  839. if ($myrow['visibility']==0)
  840. {
  841. $addedresource_style="invisible";
  842. }
  843. display_added_resources("Agenda", $myrow["id"], $addedresource_style);
  844. echo "</td></tr>";
  845. }*/
  846. $event_list.=$myrow['id'].',';
  847. $counter++;
  848. /*--------------------------------------------------
  849. display: jump-to-top icon
  850. --------------------------------------------------*/
  851. echo '<tr>';
  852. echo '<td colspan="3">';
  853. if($is_repeated){echo get_lang('RepeatedEvent'),'<a href="',api_get_self(),'?',api_get_cidreq,'&amp;agenda_id=',$myrow['parent_event_id'],'" alt="',get_lang('RepeatedEventViewOriginalEvent'),'">',get_lang('RepeatedEventViewOriginalEvent'),'</a>';}
  854. echo "<a href=\"#top\">".Display::return_icon('top.gif', get_lang('Top'))."</a></td></tr>";
  855. echo "</table><br /><br />";
  856. } // end while ($myrow=Database::fetch_array($result))
  857. if(!empty($event_list))
  858. {
  859. $event_list=substr($event_list,0,-1);
  860. }
  861. else
  862. {
  863. $event_list='0';
  864. }
  865. echo "<form name=\"event_list_form\"><input type=\"hidden\" name=\"event_list\" value=\"$event_list\" /></form>";
  866. // closing the layout table
  867. echo "</td>",
  868. "</tr>",
  869. "</table>";
  870. }
  871. /**
  872. * Displays only 1 agenda item. This is used when an agenda item is added to the learning path.
  873. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  874. */
  875. function display_one_agenda_item($agenda_id)
  876. {
  877. $TABLEAGENDA = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  878. global $TABLE_ITEM_PROPERTY;
  879. global $select_month, $select_year;
  880. global $DaysShort, $DaysLong, $MonthsLong;
  881. global $is_courseAdmin;
  882. global $dateFormatLong, $timeNoSecFormat, $charset;
  883. global $_user;
  884. $agenda_id=Database::escape_string($agenda_id);
  885. //echo "displaying agenda items";
  886. // getting the name of the groups
  887. //$group_names=get_course_groups();
  888. /*--------------------------------------------------
  889. CONSTRUCT THE SQL STATEMENT
  890. --------------------------------------------------*/
  891. $sql="SELECT * FROM ".$TABLEAGENDA;
  892. $result=api_sql_query($sql,__FILE__,__LINE__) or die(Database::error());
  893. $number_items=Database::num_rows($result);
  894. $myrow=Database::fetch_array($result); // there should be only one item so no need for a while loop
  895. $sql_rep = "SELECT * FROM $TABLEAGENDA WHERE id = $agenda_id";
  896. $res_rep = Database::query($sql_rep,__FILE__,__LINE__);
  897. $repeat = false;
  898. $repeat_id = 0;
  899. if(Database::num_rows($res_rep)>0)
  900. {
  901. $repeat=true;
  902. $row_rep = Database::fetch_array($res_rep);
  903. //$repeat_id = $row_rep['parent_event_id'];
  904. }
  905. /*--------------------------------------------------
  906. DISPLAY: NO ITEMS
  907. --------------------------------------------------*/
  908. if ($number_items==0)
  909. {
  910. echo "<table id=\"data_table\" ><tr><td>".get_lang("NoAgendaItems")."</td></tr></table>";
  911. }
  912. /*--------------------------------------------------
  913. DISPLAY: THE ITEMS
  914. --------------------------------------------------*/
  915. echo "<table id=\"data_table\">\n";
  916. /*--------------------------------------------------
  917. DISPLAY : the icon, title, destinees of the item
  918. --------------------------------------------------*/
  919. echo "\t<tr>\n";
  920. // highlight: if a date in the small calendar is clicked we highlight the relevant items
  921. $db_date=(int)date("d",strtotime($myrow["start_date"])).date("n",strtotime($myrow["start_date"])).date("Y",strtotime($myrow["start_date"]));
  922. if ($_GET["day"].$_GET["month"].$_GET["year"] <>$db_date)
  923. {
  924. if ($myrow['visibility']=='0')
  925. {
  926. $style="data_hidden";
  927. $stylenotbold="datanotbold_hidden";
  928. $text_style="text_hidden";
  929. }
  930. else
  931. {
  932. $style="data";
  933. $stylenotbold="datanotbold";
  934. $text_style="text";
  935. }
  936. }
  937. else
  938. {
  939. $style="datanow";
  940. $stylenotbold="datanotboldnow";
  941. $text_style="textnow";
  942. }
  943. echo "\t\t<td class=\"".$style."\">\n";
  944. // adding an internal anchor
  945. echo "\t\t\t<a name=\"".(int)date("d",strtotime($myrow["start_date"]))."\"></a>";
  946. // the icons. If the message is sent to one or more specific users/groups
  947. // we add the groups icon
  948. // 2do: if it is sent to groups we display the group icon, if it is sent to a user we show the user icon
  949. echo Display::return_icon('agenda.gif');
  950. if ($myrow['to_group_id']!=='0')
  951. {
  952. echo Display::return_icon('group.gif');
  953. }
  954. echo " ".$myrow['title']."\n";
  955. echo "\t\t</td>\n";
  956. // the message has been sent to
  957. echo "\t\t<td class=\"".$stylenotbold."\">".get_lang("SentTo").": ".get_lang('AllUsersOfThePlatform');
  958. $sent_to=sent_to(TOOL_CALENDAR_EVENT, $myrow["ref"]);
  959. $sent_to_form=sent_to_form($sent_to);
  960. echo $sent_to_form;
  961. echo "</td>\n\t</tr>\n";
  962. /*--------------------------------------------------
  963. DISPLAY: the title
  964. --------------------------------------------------*/
  965. echo "\t<tr class=\"".$stylenotbold."\">\n";
  966. echo "\t\t<td>".get_lang("StartTime").": ";
  967. echo ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["start_date"])))."&nbsp;&nbsp;&nbsp;";
  968. echo ucfirst(strftime($timeNoSecFormat,strtotime($myrow["start_date"])))."";
  969. echo "</td>\n";
  970. echo "\t\t<td>".get_lang("EndTime").": ";
  971. echo ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["end_date"])))."&nbsp;&nbsp;&nbsp;";
  972. echo ucfirst(strftime($timeNoSecFormat,strtotime($myrow["end_date"])))."";
  973. echo "</td>\n";
  974. echo "\n\t</tr>\n";
  975. /*--------------------------------------------------
  976. DISPLAY: the content
  977. --------------------------------------------------*/
  978. $export_icon = '../img/export.png';
  979. $export_icon_low = '../img/export_low_fade.png';
  980. $export_icon_high = '../img/export_high_fade.png';
  981. $content = $myrow['content'];
  982. $content = make_clickable($content);
  983. $content = text_filter($content);
  984. //echo "\t<tr>\n\t\t<td class=\"".$text_style."\" colspan='2'>";
  985. //echo $content;
  986. //echo "</td></tr>";
  987. echo "<tr class='row_even'>";
  988. echo '<td colspan="2">';
  989. echo $content;
  990. echo '</td></tr>';
  991. /*--------------------------------------------------
  992. DISPLAY: the added resources
  993. --------------------------------------------------*/
  994. if (check_added_resources("Agenda", $myrow["id"]))
  995. {
  996. echo "<tr><td colspan='2'>";
  997. echo "<i>".get_lang("AddedResources")."</i><br/>";
  998. if ($myrow['visibility']==0)
  999. {
  1000. $addedresource_style="invisible";
  1001. }
  1002. display_added_resources("Agenda", $myrow["id"], $addedresource_style);
  1003. echo "</td></tr>";
  1004. }
  1005. /*--------------------------------------------------
  1006. DISPLAY: edit delete button (course admin only)
  1007. --------------------------------------------------*/
  1008. echo '<tr><td colspan="2">';
  1009. if (!$repeat && api_is_allowed_to_edit(false,true)) {
  1010. // edit
  1011. $mylink = api_get_self()."?".api_get_cidreq()."&amp;origin=".Security::remove_XSS($_GET['origin'])."&amp;id=".$myrow['id'];
  1012. echo "<a href=\"".$mylink."&amp;action=edit\">",
  1013. Display::return_icon('edit.gif', get_lang('ModifyCalendarItem')), "</a>",
  1014. "<a href=\"".$mylink."&amp;action=delete\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset))."')) return false;\">",
  1015. Display::return_icon('delete.gif', get_lang('Delete')),"</a>";
  1016. if ($myrow['visibility']==1) {
  1017. $image_visibility="visible";
  1018. } else {
  1019. $image_visibility="invisible";
  1020. }
  1021. echo '<a href="'.$mylink.'&amp;action=showhide">',Display::return_icon($image_visibility, get_lang('Visible')),'</a><br /><br />';
  1022. }
  1023. $mylink = 'calendar_ical_export.php?'.api_get_cidreq().'&amp;type=course&amp;id='.$myrow['id'];
  1024. echo '<a class="ical_export" href="'.$mylink.'&amp;class=confidential" title="'.get_lang('ExportiCalConfidential').'">'.Display::return_icon($export_icon_high, get_lang('ExportiCalConfidential')).'</a> ';
  1025. echo '<a class="ical_export" href="'.$mylink.'&amp;class=private" title="'.get_lang('ExportiCalPrivate').'">'.Display::return_icon($export_icon_low, get_lang('ExportiCalPrivate')).'</a> ';
  1026. echo '<a class="ical_export" href="'.$mylink.'&amp;class=public" title="'.get_lang('ExportiCalPublic').'">'.Display::return_icon($export_icon, get_lang('ExportiCalPublic')).'</a> ';
  1027. echo '<a href="#" onclick="javascript:win_print=window.open(\'calendar_view_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('print.gif', get_lang('Print')).'</a>&nbsp;';
  1028. echo "</td>";
  1029. if($repeat) {
  1030. echo '<tr>';
  1031. echo '<td colspan="2">',get_lang('RepeatedEvent'),'<a href="',api_get_self(),'?',api_get_cidreq(),'&amp;agenda_id=',$repeat_id,'" alt="',get_lang('RepeatedEventViewOriginalEvent'),'">',get_lang('RepeatedEventViewOriginalEvent'),'</a></td>';
  1032. echo '</tr>';
  1033. }
  1034. echo "</table>";
  1035. // closing the layout table
  1036. echo "</td>",
  1037. "</tr>",
  1038. "</table>";
  1039. }
  1040. /**
  1041. * Show the form for adding a new agenda item. This is the same function that is used whenever we are editing an
  1042. * agenda item. When the id parameter is empty (default behaviour), then we show an empty form, else we are editing and
  1043. * we have to retrieve the information that is in the database and use this information in the forms.
  1044. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1045. * @param integer id, the id of the agenda item we are editing. By default this is empty which means that we are adding an
  1046. * agenda item.
  1047. */
  1048. function show_group_filter_form()
  1049. {
  1050. $group_list=get_course_groups();
  1051. echo "<select name=\"select\" onchange=\"MM_jumpMenu('parent',this,0)\">";
  1052. echo "<option value=\"agenda.php?group=none\">show all groups</option>";
  1053. /*foreach($group_list as $this_group)
  1054. {
  1055. // echo "<option value=\"agenda.php?isStudentView=true&amp;group=".$this_group['id']."\">".$this_group['name']."</option>";
  1056. echo "<option value=\"agenda.php?group=".$this_group['id']."\" ";
  1057. echo ($this_group['id']==$_SESSION['group'])? " selected":"" ;
  1058. echo ">".$this_group['name']."</option>";
  1059. }*/
  1060. echo "</select>";
  1061. }
  1062. function show_user_filter_form()
  1063. {
  1064. $user_list=get_course_users();
  1065. //echo "<select name=\"select\" onchange=\"MM_jumpMenu('parent',this,0)\">";
  1066. //echo "<option value=\"agenda.php?user=none\">show all users</option>";
  1067. /*foreach($user_list as $this_user)
  1068. {
  1069. // echo "<option value=\"agenda.php?isStudentView=true&amp;user=".$this_user['uid']."\">".$this_user['lastName']." ".$this_user['firstName']."</option>";
  1070. echo "<option value=\"agenda.php?user=".$this_user['uid']."\" ";
  1071. echo ($this_user['uid']==$_SESSION['user'])? " selected":"" ;
  1072. echo ">".$this_user['lastName']." ".$this_user['firstName']."</option>";
  1073. }*/
  1074. echo "</select>";
  1075. }
  1076. function show_user_group_filter_form()
  1077. {
  1078. echo "\n<select name=\"select\" onchange=\"MM_jumpMenu('parent',this,0)\">";
  1079. echo "\n\t<option value=\"agenda.php?user=none\">".get_lang("ShowAll")."</option>";
  1080. // Groups
  1081. echo "\n\t<optgroup label=\"".get_lang("Groups")."\">";
  1082. $group_list=get_course_groups();
  1083. /* foreach($group_list as $this_group)
  1084. {
  1085. // echo "<option value=\"agenda.php?isStudentView=true&amp;group=".$this_group['id']."\">".$this_group['name']."</option>";
  1086. echo "\n\t\t<option value=\"agenda.php?group=".$this_group['id']."\" ";
  1087. echo ($this_group['id']==$_SESSION['group'])? " selected":"" ;
  1088. echo ">".$this_group['name']."</option>";
  1089. }*/
  1090. echo "\n\t</optgroup>";
  1091. // Users
  1092. echo "\n\t<optgroup label=\"".get_lang("Users")."\">";
  1093. $user_list=get_course_users();
  1094. /* foreach($user_list as $this_user)
  1095. {
  1096. // echo "<option value=\"agenda.php?isStudentView=true&amp;user=".$this_user['uid']."\">".$this_user['lastName']." ".$this_user['firstName']."</option>";
  1097. echo "\n\t\t<option value=\"agenda.php?user=".$this_user['uid']."\" ";
  1098. echo ($this_user['uid']==$_SESSION['user'])? " selected":"" ;
  1099. echo ">".$this_user['lastName']." ".$this_user['firstName']."</option>";
  1100. }*/
  1101. echo "\n\t</optgroup>";
  1102. echo "</select>";
  1103. }
  1104. function show_add_form($id = '')
  1105. {
  1106. global $MonthsLong;
  1107. $htmlHeadXtra[] = to_javascript();
  1108. // the default values for the forms
  1109. if ($_GET['originalresource'] !== 'no')
  1110. {
  1111. $day = date('d');
  1112. $month = date('m');
  1113. $year = date('Y');
  1114. $hours = 9;
  1115. $minutes= '00';
  1116. $end_day = date('d');
  1117. $end_month = date('m');
  1118. $end_year = date('Y');
  1119. $end_hours = 17;
  1120. $end_minutes= '00';
  1121. $repeat = false;
  1122. }
  1123. else
  1124. {
  1125. // we are coming from the resource linker so there might already have been some information in the form.
  1126. // When we clicked on the button to add resources we stored every form information into a session and now we
  1127. // are doing the opposite thing: getting the information out of the session and putting it into variables to
  1128. // display it in the forms.
  1129. $form_elements=$_SESSION['formelements'];
  1130. $day=$form_elements['day'];
  1131. $month=$form_elements['month'];
  1132. $year=$form_elements['year'];
  1133. $hours=$form_elements['hour'];
  1134. $minutes=$form_elements['minutes'];
  1135. $end_day=$form_elements['end_day'];
  1136. $end_month=$form_elements['end_month'];
  1137. $end_year=$form_elements['end_year'];
  1138. $end_hours=$form_elements['end_hours'];
  1139. $end_minutes=$form_elements['end_minutes'];
  1140. $title=$form_elements['title'];
  1141. $content=$form_elements['content'];
  1142. $id=$form_elements['id'];
  1143. $to=$form_elements['to'];
  1144. $repeat = $form_elements['repeat'];
  1145. }
  1146. // switching the send to all/send to groups/send to users
  1147. if ($_POST['To'])
  1148. {
  1149. $day = $_POST['fday'];
  1150. $month = $_POST['fmonth'];
  1151. $year = $_POST['fyear'];
  1152. $hours = $_POST['fhour'];
  1153. $minutes = $_POST['fminute'];
  1154. $end_day = $_POST['end_fday'];
  1155. $end_month = $_POST['end_fmonth'];
  1156. $end_year = $_POST['end_fyear'];
  1157. $end_hours = $_POST['end_fhour'];
  1158. $end_minutes = $_POST['end_fminute'];
  1159. $title = $_POST['title'];
  1160. $content = $_POST['content'];
  1161. // the invisible fields
  1162. $action = $_POST['action'];
  1163. $id = $_POST['id'];
  1164. $repeat = !empty($_POST['repeat'])?true:false;
  1165. }
  1166. // if the id is set then we are editing an agenda item
  1167. if (is_int($id))
  1168. {
  1169. //echo "before get_agenda_item".$_SESSION['allow_individual_calendar'];
  1170. $item_2_edit=get_agenda_item($id);
  1171. $title = $item_2_edit['title'];
  1172. $content= $item_2_edit['content'];
  1173. // start date
  1174. list($datepart, $timepart) = split(" ", $item_2_edit['start_date']);
  1175. list($year, $month, $day) = explode("-", $datepart);
  1176. list($hours, $minutes, $seconds) = explode(":", $timepart);
  1177. // end date
  1178. list($datepart, $timepart) = split(" ", $item_2_edit['end_date']);
  1179. list($end_year, $end_month, $end_day) = explode("-", $datepart);
  1180. list($end_hours, $end_minutes, $end_seconds) = explode(":", $timepart);
  1181. // attachments
  1182. //edit_added_resources("Agenda", $id);
  1183. $to=$item_2_edit['to'];
  1184. //echo "<br />after get_agenda_item".$_SESSION['allow_individual_calendar'];
  1185. }
  1186. $content=stripslashes($content);
  1187. $title=stripslashes($title);
  1188. // we start a completely new item, we do not come from the resource linker
  1189. if ($_GET['originalresource']!=="no" and $_GET['action']=="add")
  1190. {
  1191. $_SESSION["formelements"]=null;
  1192. //unset_session_resources();//--------------------------------------------------------------------------------------
  1193. }
  1194. ?>
  1195. <!-- START OF THE FORM -->
  1196. <form enctype="multipart/form-data" action="<?php echo api_get_self().'?origin='.$_GET['origin'].'&amp;action='.$_GET['action']; ?>" method="post" name="new_calendar_item">
  1197. <input type="hidden" name="id" value="<?php if (isset($id)) echo $id; ?>" />
  1198. <input type="hidden" name="action" value="<?php if (isset($_GET['action'])) echo $_GET['action']; ?>" />
  1199. <input type="hidden" name="sort" value="asc" />
  1200. <input type="hidden" name="submit_event" value="ok" />
  1201. <?php
  1202. echo '<div class="row"><div class="form_header">';
  1203. echo (isset($id) AND $id<>'')?get_lang('ModifyCalendarItem'):get_lang("AddCalendarItem");
  1204. echo '</div></div>';
  1205. ?>
  1206. <table border="0" cellpadding="5" cellspacing="0" width="80%" id="newedit_form">
  1207. <!-- START date and time -->
  1208. <tr>
  1209. <div>
  1210. <table border="0" width="80%">
  1211. <tr><td colspan="4">
  1212. <div id="err_date" style="display:none;color:red"></div>
  1213. <div id="err_start_date" style="display:none;color:red"></div>
  1214. </td></tr>
  1215. <td width="10%">
  1216. <!-- date: 1 -> 31 -->
  1217. <nobr><?php echo get_lang('StartDate').": \n"; ?></nobr>
  1218. </td>
  1219. <td width="35%">
  1220. <select name="fday" onchange="javascript:document.new_calendar_item.end_fday.value=this.value;">
  1221. <?php
  1222. // small loop for filling all the dates
  1223. // 2do: the available dates should be those of the selected month => february is from 1 to 28 (or 29) and not to 31
  1224. echo "\n";
  1225. foreach (range(1, 31) as $i)
  1226. {
  1227. // values have to have double digits
  1228. $value = ($i <= 9 ? '0'.$i : $i );
  1229. // the current day is indicated with [] around the date
  1230. if ($value==$day)
  1231. {
  1232. echo "\t\t\t\t <option value=\"".$value."\" selected> ".$i." </option>\n";
  1233. }
  1234. else
  1235. {
  1236. echo "\t\t\t\t<option value=\"$value\">$i</option>\n";
  1237. }
  1238. }
  1239. ?>
  1240. </select>
  1241. <!-- month: january -> december -->
  1242. <select name="fmonth" onchange="javascript:document.new_calendar_item.end_fmonth.value=this.value;">
  1243. <?php
  1244. echo "\n";
  1245. for ($i=1; $i<=12; $i++)
  1246. {
  1247. // values have to have double digits
  1248. if ($i<=9)
  1249. {
  1250. $value="0".$i;
  1251. }
  1252. else
  1253. {
  1254. $value=$i;
  1255. }
  1256. if ($value==$month)
  1257. {
  1258. echo "\t\t\t\t <option value=\"".$value."\" selected>".$MonthsLong[$i-1]."</option>\n";
  1259. }
  1260. else
  1261. {
  1262. echo "\t\t\t\t <option value=\"".$value."\">".$MonthsLong[$i-1]."</option>\n";
  1263. }
  1264. } ?>
  1265. </select>
  1266. <select name="fyear" onchange="javascript:document.new_calendar_item.end_fyear.value=this.value;">
  1267. <option value="<?php echo ($year-1); ?>"><?php echo ($year-1); ?></option>
  1268. <option value="<?php echo $year; ?>" selected="selected"><?php echo $year; ?></option>
  1269. <?php
  1270. echo "\n";
  1271. for ($i=1; $i<=5; $i++)
  1272. {
  1273. $value=$year+$i;
  1274. echo "\t\t\t\t<option value=\"$value\">$value</option>\n";
  1275. } ?>
  1276. </select>
  1277. <a href="javascript:openCalendar('new_calendar_item','f')"><?php Display::display_icon('calendar_select.gif', get_lang('Select'), array ('style' => 'vertical-align: middle;')); ?></a>
  1278. </td>
  1279. <td width="10%">
  1280. <nobr><?php echo get_lang('StartTime').": \n"; ?></nobr>
  1281. </td>
  1282. <td>
  1283. <select name="fhour" onchange="javascript:document.new_calendar_item.end_fhour.value=this.value;">
  1284. <!-- <option value="--">--</option> -->
  1285. <?php
  1286. echo "\n";
  1287. foreach (range(0, 23) as $i)
  1288. {
  1289. // values have to have double digits
  1290. $value = ($i <= 9 ? '0'.$i : $i );
  1291. // the current hour is indicated with [] around the hour
  1292. if ($hours==$value)
  1293. {
  1294. echo "\t\t\t\t<option value=\"".$value."\" selected> ".$value." </option>\n";
  1295. }
  1296. else
  1297. {
  1298. echo "\t\t\t\t<option value=\"$value\">$value</option>\n";
  1299. }
  1300. } ?>
  1301. </select>
  1302. <select name="fminute" onchange="javascript:document.new_calendar_item.end_fminute.value=this.value;">
  1303. <!-- <option value="<?php echo $minutes ?>"><?php echo $minutes; ?></option> -->
  1304. <!-- <option value="--">--</option> -->
  1305. <?php
  1306. foreach (range(0, 59) as $i)
  1307. {
  1308. // values have to have double digits
  1309. $value = ($i <= 9 ? '0'.$i : $i );
  1310. echo "\t\t\t\t<option value=\"$value\">$value</option>\n";
  1311. } ?>
  1312. </select>
  1313. </td>
  1314. </div>
  1315. </tr>
  1316. <!-- END date and time -->
  1317. <tr>
  1318. <div>
  1319. <tr><td colspan="4"><div id="err_end_date" style="display:none;color:red"></div></td></tr>
  1320. <td >
  1321. <!-- date: 1 -> 31 -->
  1322. <nobr><?php echo get_lang('EndDate').": "; ?></nobr>
  1323. </td>
  1324. <td >
  1325. <select name="end_fday">
  1326. <?php
  1327. // small loop for filling all the dates
  1328. // 2do: the available dates should be those of the selected month => february is from 1 to 28 (or 29) and not to 31
  1329. echo "\n";
  1330. foreach (range(1, 31) as $i)
  1331. {
  1332. // values have to have double digits
  1333. $value = ($i <= 9 ? '0'.$i : $i );
  1334. // the current day is indicated with [] around the date
  1335. if ($value==$end_day)
  1336. { echo "\t\t\t\t <option value=\"".$value."\" selected> ".$i." </option>\n";}
  1337. else
  1338. { echo "\t\t\t\t <option value=\"".$value."\">".$i."</option>\n"; }
  1339. }?>
  1340. </select>
  1341. <!-- month: january -> december -->
  1342. <select name="end_fmonth">
  1343. <?php
  1344. echo "\n";
  1345. foreach (range(1, 12) as $i)
  1346. {
  1347. // values have to have double digits
  1348. $value = ($i <= 9 ? '0'.$i : $i );
  1349. if ($value==$end_month)
  1350. { echo "\t\t\t\t <option value=\"".$value."\" selected>".$MonthsLong[$i-1]."</option>\n"; }
  1351. else
  1352. { echo "\t\t\t\t <option value=\"".$value."\">".$MonthsLong[$i-1]."</option>\n"; }
  1353. }?>
  1354. </select>
  1355. <select name="end_fyear">
  1356. <option value="<?php echo ($end_year-1) ?>"><?php echo ($end_year-1) ?></option>
  1357. <option value="<?php echo $end_year ?>" selected> <?php echo $end_year ?> </option>
  1358. <?php
  1359. echo "\n";
  1360. for ($i=1; $i<=5; $i++)
  1361. {
  1362. $value=$end_year+$i;
  1363. echo "\t\t\t\t<option value=\"$value\">$value</option>\n";
  1364. } ?>
  1365. </select>
  1366. <a href="javascript:openCalendar('new_calendar_item', 'end_f')"><?php Display::display_icon('calendar_select.gif',get_lang('Select'), array ('style' => 'vertical-align: middle;')); ?></a>
  1367. </td>
  1368. <td >
  1369. <nobr><?php echo get_lang('EndTime').": \n"; ?></nobr>
  1370. </td>
  1371. <td >
  1372. <select name="end_fhour">
  1373. <!-- <option value="--">--</option> -->
  1374. <?php
  1375. echo "\n";
  1376. foreach (range(0, 23) as $i)
  1377. {
  1378. // values have to have double digits
  1379. $value = ($i <= 9 ? '0'.$i : $i );
  1380. // the current hour is indicated with [] around the hour
  1381. if ($end_hours==$value)
  1382. { echo "\t\t\t\t<option value=\"".$value."\" selected> ".$value." </option>\n"; }
  1383. else
  1384. { echo "\t\t\t\t<option value=\"".$value."\"> ".$value." </option>\n"; }
  1385. } ?>
  1386. </select>
  1387. <select name="end_fminute">
  1388. <!-- <option value="<?php echo $end_minutes; ?>"><?php echo $end_minutes; ?></option> -->
  1389. <!-- <option value="--">--</option> -->
  1390. <?php
  1391. foreach (range(0, 59) as $i)
  1392. {
  1393. // values have to have double digits
  1394. $value = ($i <= 9 ? '0'.$i : $i );
  1395. echo "\t\t\t\t<option value=\"$value\">$value</option>\n";
  1396. } ?>
  1397. </select>
  1398. <br>
  1399. </td>
  1400. </div>
  1401. </tr>
  1402. <tr><td colspan="4">
  1403. <hr noshade="noshade" color="#cccccc" />
  1404. <div id="err_title" style="display:none;color:red"></div>
  1405. </td></tr>
  1406. <tr class="subtitle">
  1407. <td colspan="4" valign="top"><?php echo get_lang('ItemTitle'); ?> :
  1408. <!--<div style='margin-left: 80px'><textarea name="title" cols="50" rows="2" wrap="virtual" style="vertical-align:top; width:75%; height:50px;"><?php if (isset($title)) echo $title; ?></textarea></div>-->
  1409. <input type="text" size="60" name="title" value="<?php if (isset($title)) echo $title; ?>" />
  1410. </td>
  1411. </tr>
  1412. <tr>
  1413. <td colspan="4">
  1414. <?php
  1415. require_once(api_get_path(LIBRARY_PATH) . "/fckeditor/fckeditor.php");
  1416. $oFCKeditor = new FCKeditor('content') ;
  1417. $oFCKeditor->ToolbarSet = 'GlobalAgenda';
  1418. $oFCKeditor->Width = '100%';
  1419. $oFCKeditor->Height = '175';
  1420. $oFCKeditor->Value = $content;
  1421. $return = $oFCKeditor->CreateHtml();
  1422. echo $return;
  1423. ?>
  1424. </td>
  1425. </tr>
  1426. <!--<?php /* ADDED BY UGENT, Patrick Cool, march 2004 */ ?>
  1427. <tr>
  1428. <td colspan="4">
  1429. <?php
  1430. //onclick="selectAll(this.form.elements[6],true)"
  1431. // show_addresource_button('onclick="selectAll(this.form.elements[6],true)"');
  1432. ?>
  1433. </td>
  1434. </tr>-->
  1435. <?php
  1436. //if ($_SESSION['addedresource'])
  1437. echo "\t<tr>\n";
  1438. echo "\t\t<td colspan=\"4\">\n";
  1439. //echo display_resources(0);//--------------------------------------------------------
  1440. $test=$_SESSION['addedresource'];
  1441. echo "\t\t</td>\n\t</tr>\n";
  1442. /* END ADDED BY UGENT, Patrick Cool, march 2004 */
  1443. if(empty($id)) //only show repeat fields when adding the first time
  1444. {
  1445. ?>
  1446. <tr>
  1447. <td colspan="4" />
  1448. </tr>
  1449. <?php
  1450. }//only show repeat fields if adding, not if editing
  1451. ?>
  1452. <tr>
  1453. <td colspan="4">
  1454. <?php
  1455. if ($_GET['action']=='edit'){
  1456. $class='save';
  1457. $text=get_lang('ModifyEvent');
  1458. }else{
  1459. $class='add';
  1460. $text=get_lang('AddEvent');
  1461. }
  1462. ?>
  1463. <button type="button" class="add" value="<?php echo get_lang('Ok'); ?>" onclick="validate_date()" ><?php echo $text; ?></button>
  1464. </td>
  1465. </tr>
  1466. </table>
  1467. </form>
  1468. <?php
  1469. }
  1470. function get_agendaitems($month, $year)
  1471. {
  1472. global $_user;
  1473. global $_configuration;
  1474. $month=Database::escape_string($month);
  1475. $year=Database::escape_string($year);
  1476. $items = array ();
  1477. //databases of the courses
  1478. $TABLEAGENDA = Database :: get_course_table(TABLE_MAIN_SYSTEM_CALENDAR);
  1479. //$TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  1480. //$group_memberships = GroupManager :: get_group_ids(Database::get_current_course_database(), $_user['user_id']);
  1481. // if the user is administrator of that course we show all the agenda items
  1482. //if (api_is_allowed_to_edit())
  1483. //{
  1484. //echo "course admin";
  1485. $sqlquery = "SELECT
  1486. DISTINCT *
  1487. FROM ".$TABLEAGENDA."
  1488. WHERE
  1489. MONTH(start_date)='".$month."'
  1490. AND YEAR(start_date)='".$year."'
  1491. GROUP BY id
  1492. ORDER BY start_date ";
  1493. //}
  1494. // if the user is not an administrator of that course
  1495. /* else
  1496. {
  1497. //echo "GEEN course admin";
  1498. if (is_array($group_memberships) && count($group_memberships)>0)
  1499. {
  1500. $sqlquery = "SELECT
  1501. agenda.*
  1502. FROM ".$TABLEAGENDA."
  1503. WHERE
  1504. MONTH(agenda.start_date)='".$month."'
  1505. AND YEAR(agenda.start_date)='".$year."'
  1506. ORDER BY start_date ";
  1507. }
  1508. else
  1509. {
  1510. $sqlquery = "SELECT
  1511. agenda.*, item_property.*
  1512. FROM ".$TABLEAGENDA."
  1513. WHERE
  1514. MONTH(agenda.start_date)='".$month."'
  1515. AND YEAR(agenda.start_date)='".$year."'
  1516. ORDER BY start_date ";
  1517. }
  1518. }*/
  1519. //$mycourse = api_get_course_info();
  1520. $portal_url = $_configuration['root_web'];
  1521. if ($_configuration['multiple_access_urls']==true) {
  1522. $access_url_id = api_get_current_access_url_id();
  1523. if ($access_url_id != -1 ){
  1524. $url = api_get_access_url($access_url_id);
  1525. $portal_url = $url['url'];
  1526. }
  1527. }
  1528. $result = api_sql_query($sqlquery, __FILE__, __LINE__);
  1529. while ($item = Database::fetch_array($result))
  1530. {
  1531. $agendaday = date('j',strtotime($item['start_date']));
  1532. $time= date('H:i',strtotime($item['start_date']));
  1533. $URL = $portal_url.'main/admin/agenda.php?day='.$agendaday."&amp;month=".$month."&amp;year=".$year; // RH //Patrick Cool: to highlight the relevant agenda item
  1534. $items[$agendaday][$item['start_time']] .= '<i>'.$time.'</i> <a href="'.$URL.'" title="'.$item['title'].'<br />';
  1535. }
  1536. // sorting by hour for every day
  1537. $agendaitems = array ();
  1538. while (list ($agendaday, $tmpitems) = each($items))
  1539. {
  1540. sort($tmpitems);
  1541. while (list ($key, $val) = each($tmpitems))
  1542. {
  1543. $agendaitems[$agendaday] .= $val;
  1544. }
  1545. }
  1546. return $agendaitems;
  1547. }
  1548. function display_upcoming_events()
  1549. {
  1550. echo '<b>'.get_lang('UpcomingEvent').'</b><br />';
  1551. $number_of_items_to_show = (int)api_get_setting('number_of_upcoming_events');
  1552. //databases of the courses
  1553. $TABLEAGENDA = Database :: get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  1554. //$TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  1555. //$mycourse = api_get_course_info();
  1556. //$myuser = api_get_user_info();
  1557. //$group_memberships = GroupManager :: get_group_ids($mycourse['dbName'], $myuser['user_id']);
  1558. // if the user is administrator of that course we show all the agenda items
  1559. /*if (api_is_allowed_to_edit())
  1560. {*/
  1561. //echo "course admin";
  1562. $sqlquery = "SELECT
  1563. DISTINCT *
  1564. FROM ".$TABLEAGENDA."
  1565. ORDER BY start_date ";
  1566. //}
  1567. // if the user is not an administrator of that course
  1568. $result = api_sql_query($sqlquery, __FILE__, __LINE__);
  1569. $counter = 0;
  1570. while ($item = Database::fetch_array($result,'ASSOC'))
  1571. {
  1572. if ($counter < $number_of_items_to_show)
  1573. {
  1574. echo $item['start_date'],' - ',$item['title'],'<br />';
  1575. $counter++;
  1576. }
  1577. }
  1578. }
  1579. /**
  1580. * This function calculates the startdate of the week (monday)
  1581. * and the enddate of the week (sunday)
  1582. * and returns it as an array
  1583. */
  1584. function calculate_start_end_of_week($week_number, $year)
  1585. {
  1586. // determine the start and end date
  1587. // step 1: we calculate a timestamp for a day in this week
  1588. $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
  1589. // step 2: we which day this is (0=sunday, 1=monday, ...)
  1590. $number_day_in_week = date('w', $random_day_in_week);
  1591. // step 3: we calculate the timestamp of the monday of the week we are in
  1592. $start_timestamp = $random_day_in_week - (($number_day_in_week -1) * 24 * 60 * 60);
  1593. // step 4: we calculate the timestamp of the sunday of the week we are in
  1594. $end_timestamp = $random_day_in_week + ((7 - $number_day_in_week +1) * 24 * 60 * 60) - 3600;
  1595. // step 5: calculating the start_day, end_day, start_month, end_month, start_year, end_year
  1596. $start_day = date('j', $start_timestamp);
  1597. $start_month = date('n', $start_timestamp);
  1598. $start_year = date('Y', $start_timestamp);
  1599. $end_day = date('j', $end_timestamp);
  1600. $end_month = date('n', $end_timestamp);
  1601. $end_year = date('Y', $end_timestamp);
  1602. $start_end_array['start']['day'] = $start_day;
  1603. $start_end_array['start']['month'] = $start_month;
  1604. $start_end_array['start']['year'] = $start_year;
  1605. $start_end_array['end']['day'] = $end_day;
  1606. $start_end_array['end']['month'] = $end_month;
  1607. $start_end_array['end']['year'] = $end_year;
  1608. return $start_end_array;
  1609. }
  1610. /**
  1611. * Show the mini calendar of the given month
  1612. */
  1613. function display_daycalendar($agendaitems, $day, $month, $year, $weekdaynames, $monthName)
  1614. {
  1615. global $DaysShort, $DaysLong, $course_path;
  1616. global $MonthsLong;
  1617. global $query;
  1618. // timestamp of today
  1619. $today = mktime();
  1620. $nextday = $today + (24 * 60 * 60);
  1621. $previousday = $today - (24 * 60 * 60);
  1622. // the week number of the year
  1623. $week_number = date("W", $today);
  1624. // if we moved to the next / previous day we have to recalculate the $today variable
  1625. if ($_GET['day'])
  1626. {
  1627. $today = mktime(0, 0, 0, $month, $day, $year);
  1628. $nextday = $today + (24 * 60 * 60);
  1629. $previousday = $today - (24 * 60 * 60);
  1630. $week_number = date("W", $today);
  1631. }
  1632. // calculating the start date of the week
  1633. // the date of the monday of this week is the timestamp of today minus
  1634. // number of days that have already passed this week * 24 hours * 60 minutes * 60 seconds
  1635. $current_day = date("j", $today); // Day of the month without leading zeros (1 to 31) of today
  1636. $day_of_the_week = date("w", $today); // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) of today
  1637. //$timestamp_first_date_of_week=$today-(($day_of_the_week-1)*24*60*60); // timestamp of the monday of this week
  1638. //$timestamp_last_date_of_week=$today+((7-$day_of_the_week)*24*60*60); // timestamp of the sunday of this week
  1639. // we are loading all the calendar items of all the courses for today
  1640. echo "<table class=\"data_table\">\n";
  1641. // the forward and backwards url
  1642. $backwardsURL = api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($_GET['courseCode'])."&amp;action=view&amp;view=day&amp;day=".date("j", $previousday)."&amp;month=".date("n", $previousday)."&amp;year=".date("Y", $previousday);
  1643. $forewardsURL = api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($_GET['courseCode'])."&amp;action=view&amp;view=day&amp;day=".date("j", $nextday)."&amp;month=".date("n", $nextday)."&amp;year=".date("Y", $nextday);
  1644. // The title row containing the day
  1645. echo "<tr>\n", "<th width=\"10%\"><a href=\"", $backwardsURL, "\">&#171;</a></th>\n", "<th>";
  1646. echo $DaysLong[$day_of_the_week]." ".date("j", $today)." ".$MonthsLong[date("n", $today) - 1]." ".date("Y", $today);
  1647. echo "</th>";
  1648. echo "<th width=\"10%\"><a href=\"", $forewardsURL, "\">&#187;</a></th>\n";
  1649. echo "</tr>\n";
  1650. // the rows for each half an hour
  1651. for ($i = 10; $i < 48; $i ++)
  1652. {
  1653. if ($i % 2 == 0)
  1654. {
  1655. $class = "class=\"row_even\"";
  1656. }
  1657. else
  1658. {
  1659. $class = "class=\"row_odd\"";
  1660. }
  1661. echo "<tr $class>\n";
  1662. echo "\t";
  1663. if ($i % 2 == 0)
  1664. {
  1665. echo ("<td valign=\"top\" width=\"75\">". (($i) / 2)." ".get_lang("HourShort")." 00</td>\n");
  1666. }
  1667. else
  1668. {
  1669. echo ("<td valign=\"top\" width=\"75\">". ((($i) / 2) - (1 / 2))." ".get_lang("HourShort")." 30</td>\n");
  1670. }
  1671. echo "\t<td $class valign=\"top\" colspan=\"2\">\n";
  1672. if (is_array($agendaitems[$i]))
  1673. {
  1674. foreach ($agendaitems[$i] as $key => $value)
  1675. {
  1676. echo $value;
  1677. }
  1678. }
  1679. else
  1680. {
  1681. echo $agendaitems[$i];
  1682. }
  1683. echo "\t</td>\n";
  1684. echo "</tr>\n";
  1685. }
  1686. echo "</table>\n";
  1687. }
  1688. /**
  1689. * Display the weekly view of the calendar
  1690. */
  1691. function display_weekcalendar($agendaitems, $month, $year, $weekdaynames, $monthName)
  1692. {
  1693. global $DaysShort,$course_path;
  1694. global $MonthsLong;
  1695. // timestamp of today
  1696. $today = time();
  1697. $day_of_the_week = date("w", $today);
  1698. $thisday_of_the_week = date("w", $today);
  1699. // the week number of the year
  1700. $week_number = date("W", $today);
  1701. $thisweek_number = $week_number;
  1702. // if we moved to the next / previous week we have to recalculate the $today variable
  1703. if ($_GET['week'])
  1704. {
  1705. $today = mktime(0, 0, 0, 1, 1, $year);
  1706. $today = $today + (((int)$_GET['week']-1) * (7 * 24 * 60 * 60));
  1707. $week_number = date("W", $today);
  1708. }
  1709. // calculating the start date of the week
  1710. // the date of the monday of this week is the timestamp of today minus
  1711. // number of days that have already passed this week * 24 hours * 60 minutes * 60 seconds
  1712. $current_day = date("j", $today); // Day of the month without leading zeros (1 to 31) of today
  1713. $day_of_the_week = date("w", $today); // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) of today
  1714. $timestamp_first_date_of_week = $today - (($day_of_the_week -1) * 24 * 60 * 60); // timestamp of the monday of this week
  1715. $timestamp_last_date_of_week = $today + ((7 - $day_of_the_week) * 24 * 60 * 60); // timestamp of the sunday of this week
  1716. $backwardsURL = api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($_GET['courseCode'])."&amp;action=view&amp;view=week&amp;week=". ($week_number -1);
  1717. $forewardsURL = api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($_GET['courseCode'])."&amp;action=view&amp;view=week&amp;week=". ($week_number +1);
  1718. echo "<table id=\"agenda_list\">\n";
  1719. // The title row containing the the week information (week of the year (startdate of week - enddate of week)
  1720. echo "<tr class=\"title\">\n";
  1721. echo "<td width=\"10%\"><a href=\"", $backwardsURL, "\">&#171;</a></td>\n";
  1722. echo "<td colspan=\"5\">".get_lang("Week")." ".$week_number;
  1723. 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).')';
  1724. echo "</td>";
  1725. echo "<td width=\"10%\"><a href=\"", $forewardsURL, "\">&#187;</a></td>\n", "</tr>\n";
  1726. // The second row containing the short names of the days of the week
  1727. echo "<tr>\n";
  1728. // this is the Day of the month without leading zeros (1 to 31) of the monday of this week
  1729. $tmp_timestamp = $timestamp_first_date_of_week;
  1730. for ($ii = 1; $ii < 8; $ii ++)
  1731. {
  1732. $is_today = ($ii == $thisday_of_the_week AND (!isset($_GET['week']) OR $_GET['week']==$thisweek_number));
  1733. echo "\t<td class=\"weekdays\">";
  1734. if ($is_today)
  1735. {
  1736. echo "<font color=#CC3300>";
  1737. }
  1738. echo $DaysShort[$ii % 7]." ".date("j", $tmp_timestamp)." ".$MonthsLong[date("n", $tmp_timestamp) - 1];
  1739. if ($is_today)
  1740. {
  1741. echo "</font>";
  1742. }
  1743. echo "</td>\n";
  1744. // we 24 hours * 60 minutes * 60 seconds to the $tmp_timestamp
  1745. $array_tmp_timestamp[] = $tmp_timestamp;
  1746. $tmp_timestamp = $tmp_timestamp + (24 * 60 * 60);
  1747. }
  1748. echo "</tr>\n";
  1749. // the table cells containing all the entries for that day
  1750. echo "<tr>\n";
  1751. $counter = 0;
  1752. foreach ($array_tmp_timestamp as $key => $value)
  1753. {
  1754. if ($counter < 5)
  1755. {
  1756. $class = "class=\"days_week\"";
  1757. }
  1758. else
  1759. {
  1760. $class = "class=\"days_weekend\"";
  1761. }
  1762. if ($counter == $thisday_of_the_week -1 AND (!isset($_GET['week']) OR $_GET['week']==$thisweek_number))
  1763. {
  1764. $class = "class=\"days_today\"";
  1765. }
  1766. echo "\t<td ".$class.">";
  1767. echo "<span class=\"agendaitem\">".$agendaitems[date('j', $value)]."&nbsp;</span> ";
  1768. echo "</td>\n";
  1769. $counter ++;
  1770. }
  1771. echo "</tr>\n";
  1772. echo "</table>\n";
  1773. }
  1774. /**
  1775. * Show the monthcalender of the given month
  1776. */
  1777. function get_day_agendaitems($courses_dbs, $month, $year, $day)
  1778. {
  1779. global $_user;
  1780. global $_configuration;
  1781. global $setting_agenda_link;
  1782. $items = array ();
  1783. // get agenda-items for every course
  1784. //$query=api_sql_query($sql_select_courses);
  1785. foreach ($courses_dbs as $key => $array_course_info)
  1786. {
  1787. //databases of the courses
  1788. $TABLEAGENDA = Database :: get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  1789. //$TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY, $array_course_info['db']);
  1790. // getting all the groups of the user for the current course
  1791. //$group_memberships = GroupManager :: get_group_ids($array_course_info['db'], $_user['user_id']);
  1792. // if the user is administrator of that course we show all the agenda items
  1793. if ($array_course_info['status'] == '1')
  1794. {
  1795. //echo "course admin";
  1796. $sqlquery = "SELECT DISTINCT *
  1797. FROM ".$TABLEAGENDA."
  1798. WHERE
  1799. DAYOFMONTH(start_date)='".$day."' AND MONTH(start_date)='".$month."' AND YEAR(start_date)='".$year."'
  1800. GROUP BY agenda.id
  1801. ORDER BY start_date ";
  1802. }
  1803. // if the user is not an administrator of that course
  1804. /*else
  1805. {
  1806. //echo "GEEN course admin";
  1807. if (is_array($group_memberships) && count($group_memberships)>0)
  1808. {
  1809. $sqlquery = "SELECT
  1810. agenda.*
  1811. FROM ".$TABLEAGENDA."
  1812. WHERE
  1813. DAYOFMONTH(start_date)='".$day."' AND MONTH(start_date)='".$month."' AND YEAR(start_date)='".$year."'
  1814. ORDER BY start_date ";
  1815. }
  1816. else
  1817. {
  1818. $sqlquery = "SELECT
  1819. agenda.*
  1820. FROM ".$TABLEAGENDA."
  1821. WHERE
  1822. DAYOFMONTH(start_date)='".$day."' AND MONTH(start_date)='".$month."' AND YEAR(start_date)='".$year."'
  1823. ORDER BY start_date ";
  1824. }
  1825. }*/
  1826. //$sqlquery = "SELECT * FROM $agendadb WHERE DAYOFMONTH(day)='$day' AND month(day)='$month' AND year(day)='$year'";
  1827. //echo "abc";
  1828. //echo $sqlquery;
  1829. $result = api_sql_query($sqlquery, __FILE__, __LINE__);
  1830. $portal_url = $_configuration['root_web'];
  1831. if ($_configuration['multiple_access_urls']==true) {
  1832. $access_url_id = api_get_current_access_url_id();
  1833. if ($access_url_id != -1 ){
  1834. $url = api_get_access_url($access_url_id);
  1835. $portal_url = $url['url'];
  1836. }
  1837. }
  1838. //echo Database::num_rows($result);
  1839. while ($item = Database::fetch_array($result))
  1840. {
  1841. // in the display_daycalendar function we use $i (ranging from 0 to 47) for each halfhour
  1842. // we want to know for each agenda item for this day to wich halfhour it must be assigned
  1843. list ($datepart, $timepart) = split(" ", $item['start_date']);
  1844. list ($year, $month, $day) = explode("-", $datepart);
  1845. list ($hours, $minutes, $seconds) = explode(":", $timepart);
  1846. $halfhour = 2 * $hours;
  1847. if ($minutes >= '30')
  1848. {
  1849. $halfhour = $halfhour +1;
  1850. }
  1851. if ($setting_agenda_link == 'coursecode')
  1852. {
  1853. $title=$array_course_info['title'];
  1854. $agenda_link = substr($title, 0, 14);
  1855. }
  1856. else
  1857. {
  1858. $agenda_link = Display::return_icon('course_home.gif');
  1859. }
  1860. //$URL = $_configuration['root_web'].$mycours["dir"]."/";
  1861. $URL = $portal_url.'main/admin/agenda.php?cidReq='."&amp;day=$day&amp;month=$month&amp;year=$year#$day"; // RH //Patrick Cool: to highlight the relevant agenda item
  1862. $items[$halfhour][] .= "<i>".$hours.":".$minutes."</i> <a href=\"$URL\" title=\"".$array_course_info['name']."\">".$agenda_link."</a> ".$item['title']."<br />";
  1863. }
  1864. }
  1865. // sorting by hour for every day
  1866. $agendaitems = array();
  1867. while (list($agendaday, $tmpitems) = each($items))
  1868. {
  1869. sort($tmpitems);
  1870. while (list($key,$val) = each($tmpitems))
  1871. {
  1872. $agendaitems[$agendaday].=$val;
  1873. }
  1874. }
  1875. $agendaitems = $items;
  1876. //print_r($agendaitems);
  1877. return $agendaitems;
  1878. }
  1879. /**
  1880. * Return agenda items of the week
  1881. */
  1882. function get_week_agendaitems($courses_dbs, $month, $year, $week = '')
  1883. {
  1884. $TABLEAGENDA = Database :: get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  1885. global $_user;
  1886. global $_configuration;
  1887. global $setting_agenda_link;
  1888. $items = array ();
  1889. // The default value of the week
  1890. if ($week == '')
  1891. {
  1892. $week_number = date("W", time());
  1893. }
  1894. else
  1895. {
  1896. $week_number = $week;
  1897. }
  1898. $start_end = calculate_start_end_of_week($week_number, $year);
  1899. $start_filter = $start_end['start']['year']."-".$start_end['start']['month']."-".$start_end['start']['day'];
  1900. $end_filter = $start_end['end']['year']."-".$start_end['end']['month']."-".$start_end['end']['day'];
  1901. // get agenda-items for every course
  1902. foreach ($courses_dbs as $key => $array_course_info)
  1903. {
  1904. //databases of the courses
  1905. $TABLEAGENDA = Database :: get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  1906. //$TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY, $array_course_info["db"]);
  1907. // getting all the groups of the user for the current course
  1908. // $group_memberships = GroupManager :: get_group_ids($array_course_info["db"], $_user['user_id']);
  1909. // if the user is administrator of that course we show all the agenda items
  1910. //if ($array_course_info['status'] == '1')
  1911. //{
  1912. //echo "course admin";
  1913. $sqlquery = "SELECT DISTINCT * FROM ".$TABLEAGENDA." ORDER BY start_date";
  1914. //}
  1915. // if the user is not an administrator of that course
  1916. //else
  1917. //{
  1918. //echo "GEEN course admin";
  1919. /* if (is_array($group_memberships) && count($group_memberships)>0)
  1920. {
  1921. $sqlquery = "SELECT * FROM ".$TABLEAGENDA." ORDER BY a.start_date";
  1922. }
  1923. else*/
  1924. //{
  1925. // $sqlquery = "SELECT * FROM ".$TABLEAGENDA." ORDER BY a.start_date";
  1926. //}
  1927. //}
  1928. //echo "<pre>".$sqlquery."</pre>";
  1929. // $sqlquery = "SELECT * FROM $agendadb WHERE (DAYOFMONTH(day)>='$start_day' AND DAYOFMONTH(day)<='$end_day')
  1930. // AND (MONTH(day)>='$start_month' AND MONTH(day)<='$end_month')
  1931. // AND (YEAR(day)>='$start_year' AND YEAR(day)<='$end_year')";
  1932. $result = api_sql_query($sqlquery, __FILE__, __LINE__);
  1933. $portal_url = $_configuration['root_web'];
  1934. if ($_configuration['multiple_access_urls']==true) {
  1935. $access_url_id = api_get_current_access_url_id();
  1936. if ($access_url_id != -1 ){
  1937. $url = api_get_access_url($access_url_id);
  1938. $portal_url = $url['url'];
  1939. }
  1940. }
  1941. while ($item = Database::fetch_array($result))
  1942. {
  1943. $agendaday = date("j",strtotime($item['start_date']));
  1944. $time= date("H:i",strtotime($item['start_date']));
  1945. if ($setting_agenda_link == 'coursecode')
  1946. {
  1947. $title=$array_course_info['title'];
  1948. $agenda_link = substr($title, 0, 14);
  1949. }
  1950. else
  1951. {
  1952. $agenda_link = Display::return_icon('course_home.gif');
  1953. }
  1954. $URL = $portal_url."main/admin/agenda.php?cidReq=".urlencode($array_course_info["code"])."&amp;day=$agendaday&amp;month=$month&amp;year=$year#$agendaday"; // RH //Patrick Cool: to highlight the relevant agenda item
  1955. $items[$agendaday][$item['start_time']] .= "<i>$time</i> <a href=\"$URL\" title=\"".$array_course_info["name"]."\">".$agenda_link."</a> ".$item['title']."<br />";
  1956. }
  1957. }
  1958. // sorting by hour for every day
  1959. $agendaitems = array ();
  1960. while (list ($agendaday, $tmpitems) = each($items))
  1961. {
  1962. sort($tmpitems);
  1963. while (list ($key, $val) = each($tmpitems))
  1964. {
  1965. $agendaitems[$agendaday] .= $val;
  1966. }
  1967. }
  1968. //print_r($agendaitems);
  1969. return $agendaitems;
  1970. }
  1971. /**
  1972. * Get repeated events of a course between two dates (timespan of a day).
  1973. * Returns an array containing the events
  1974. * @param string Course info array (as returned by api_get_course_info())
  1975. * @param int UNIX timestamp of span start. Defaults 0, later transformed into today's start
  1976. * @param int UNIX timestamp. Defaults to 0, later transformed into today's end
  1977. * @param array A set of parameters to alter the SQL query
  1978. * @return array [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  1979. */
  1980. function get_repeated_events_day_view($course_info,$start=0,$end=0,$params)
  1981. {
  1982. $events = array();
  1983. //initialise all values
  1984. $y=0;
  1985. $m=0;
  1986. $d=0;
  1987. //block $end if higher than 2038 -- PHP doesn't go past that
  1988. if($end>2145934800){$end = 2145934800;}
  1989. if($start == 0 or $end == 0)
  1990. {
  1991. $y=date('Y');
  1992. $m=date('m');
  1993. $d=date('j');
  1994. }
  1995. if($start==0)
  1996. {
  1997. $start = mktime(0,0,0,$m,$d,$y);
  1998. }
  1999. $db_start = date('Y-m-d H:i:s',$start);
  2000. if($end==0)
  2001. {
  2002. $end = mktime(23,59,59,$m,$d,$y);
  2003. }
  2004. //$db_end = date('Y-m-d H:i:s',$end);
  2005. $t_cal = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR,$course_info['dbName']);
  2006. //$t_cal_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT,$course_info['dbName']);
  2007. $t_ip = Database::get_course_table(TABLE_ITEM_PROPERTY,$course_info['dbName']);
  2008. $sql = "SELECT c.id, c.title, c.content, " .
  2009. " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, " .
  2010. " cr.cal_type, cr.cal_end " .
  2011. " FROM $t_cal c, $t_cal_repeat cr, $t_ip as item_property " .
  2012. " WHERE cr.cal_end >= $start " .
  2013. " AND cr.cal_id = c.id " .
  2014. " AND item_property.ref = c.id ".
  2015. " AND item_property.tool = '".TOOL_CALENDAR_EVENT."' ".
  2016. " AND c.start_date <= '$db_start' "
  2017. .(!empty($params['conditions'])?$params['conditions']:'')
  2018. .(!empty($params['groupby'])?' GROUP BY '.$params['groupby']:'')
  2019. .(!empty($params['orderby'])?' ORDER BY '.$params['orderby']:'');
  2020. $res = api_sql_query($sql,__FILE__,__LINE__);
  2021. if(Database::num_rows($res)>0)
  2022. {
  2023. while($row = Database::fetch_array($res))
  2024. {
  2025. $orig_start = $row['orig_start'];
  2026. $orig_end = $row['orig_end'];
  2027. $repeat_type = $row['cal_type'];
  2028. switch($repeat_type)
  2029. {
  2030. case 'daily':
  2031. //we are in the daily view, so if this element is repeated daily and
  2032. //the repetition is still active today (which is a condition of the SQL query)
  2033. //then the event happens today. Just build today's timestamp for start and end
  2034. $time_orig_h = date('H',$orig_start);
  2035. $time_orig_m = date('i',$orig_start);
  2036. $time_orig_s = date('s',$orig_start);
  2037. $int_time = (($time_orig_h*60)+$time_orig_m)*60+$time_orig_s; //time in seconds since 00:00:00
  2038. $span = $orig_end - $orig_start; //total seconds between start and stop of original event
  2039. $current_start =$start + $int_time; //unixtimestamp start of today's event
  2040. $current_stop = $start+$int_time+$span; //unixtimestamp stop of today's event
  2041. $events[] = array($course_info['id'],$row['id'],$current_start,$current_stop,$row['title'],$row['content']);
  2042. break;
  2043. case 'weekly':
  2044. $time_orig = date('Y/n/W/j/N/G/i/s',$orig_start);
  2045. list($y_orig,$m_orig,$w_orig,$d_orig,$dw_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2046. $time_now = date('Y/n/W/j/N/G/i/s',$end);
  2047. list($y_now,$m_now,$w_now,$d_now,$dw_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2048. if((($y_now>$y_orig) OR (($y_now == $y_orig) && ($w_now>$w_orig))) && ($dw_orig == $dw_now))
  2049. { //if the event is after the original (at least one week) and the day of the week is the same
  2050. $time_orig_end = date('Y/n/W/j/N/G/i/s',$orig_end);
  2051. 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);
  2052. $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']);
  2053. }
  2054. break;
  2055. case 'monthlyByDate':
  2056. $time_orig = date('Y/n/j/G/i/s',$orig_start);
  2057. list($y_orig,$m_orig,$d_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2058. $time_now = date('Y/n/j/G/i/s',$end);
  2059. list($y_now,$m_now,$d_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2060. if((($y_now>$y_orig) OR (($y_now == $y_orig) && ($m_now>$m_orig))) && ($d_orig == $d_now))
  2061. {
  2062. $time_orig_end = date('Y/n/j/G/i/s',$orig_end);
  2063. list($y_orig_e,$m_orig_e,$d_orig_e,$h_orig_e,$n_orig_e,$s_orig_e) = split('/',$time_orig_end);
  2064. $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']);
  2065. }
  2066. break;
  2067. case 'monthlyByDayR':
  2068. //not implemented yet
  2069. break;
  2070. case 'monthlyByDay':
  2071. //not implemented yet
  2072. break;
  2073. case 'yearly':
  2074. $time_orig = date('Y/n/j/z/G/i/s',$orig_start);
  2075. list($y_orig,$m_orig,$d_orig,$dy_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2076. $time_now = date('Y/n/j/z/G/i/s',$end);
  2077. list($y_now,$m_now,$d_now,$dy_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2078. if(($y_now>$y_orig) && ($dy_orig == $dy_now))
  2079. {
  2080. $time_orig_end = date('Y/n/j/G/i/s',$orig_end);
  2081. 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);
  2082. $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']);
  2083. }
  2084. break;
  2085. default:
  2086. break;
  2087. }
  2088. }
  2089. }
  2090. return $events;
  2091. }
  2092. /**
  2093. * Get repeated events of a course between two dates (timespan of a week).
  2094. * Returns an array containing the events
  2095. * @param string Course info array (as returned by api_get_course_info())
  2096. * @param int UNIX timestamp of span start. Defaults 0, later transformed into today's start
  2097. * @param int UNIX timestamp. Defaults to 0, later transformed into today's end
  2098. * @param array A set of parameters to alter the SQL query
  2099. * @return array [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  2100. */
  2101. function get_repeated_events_week_view($course_info,$start=0,$end=0,$params)
  2102. {
  2103. $events = array();
  2104. //block $end if higher than 2038 -- PHP doesn't go past that
  2105. if($end>2145934800){$end = 2145934800;}
  2106. //initialise all values
  2107. $y=0;
  2108. $m=0;
  2109. $d=0;
  2110. if($start == 0 or $end == 0)
  2111. {
  2112. $time = time();
  2113. $dw = date('w',$time);
  2114. $week_start = $time - (($dw-1)*86400);
  2115. $y = date('Y',$week_start);
  2116. $m = date('m',$week_start);
  2117. $d = date('j',$week_start);
  2118. $w = date('W',$week_start);
  2119. }
  2120. if($start==0)
  2121. {
  2122. $start = mktime(0,0,0,$m,$d,$y);
  2123. }
  2124. $db_start = date('Y-m-d H:i:s',$start);
  2125. if($end==0)
  2126. {
  2127. $end = $start+(86400*7)-1; //start of week, more 7 days, minus 1 second to get back to the previoyus day
  2128. }
  2129. //$db_end = date('Y-m-d H:i:s',$end);
  2130. $t_cal = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  2131. //$t_cal_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT,$course_info['dbName']);
  2132. //$t_ip = Database::get_course_table(TABLE_ITEM_PROPERTY,$course_info['dbName']);
  2133. $sql = "SELECT c.id, c.title, c.content " .
  2134. " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, " .
  2135. " FROM". $t_cal ."
  2136. WHERE c.start_date <= '$db_start' "
  2137. .(!empty($params['conditions'])?$params['conditions']:'')
  2138. .(!empty($params['groupby'])?' GROUP BY '.$params['groupby']:'')
  2139. .(!empty($params['orderby'])?' ORDER BY '.$params['orderby']:'');
  2140. $res = api_sql_query($sql,__FILE__,__LINE__);
  2141. if(Database::num_rows($res)>0)
  2142. {
  2143. while($row = Database::fetch_array($res))
  2144. {
  2145. $orig_start = $row['orig_start'];
  2146. $orig_end = $row['orig_end'];
  2147. $repeat_type = $row['cal_type'];
  2148. switch($repeat_type)
  2149. {
  2150. case 'daily':
  2151. $time_orig_h = date('H',$orig_start);
  2152. $time_orig_m = date('i',$orig_start);
  2153. $time_orig_s = date('s',$orig_start);
  2154. $int_time = (($time_orig_h*60)+$time_orig_m)*60+$time_orig_s; //time in seconds since 00:00:00
  2155. $span = $orig_end - $orig_start; //total seconds between start and stop of original event
  2156. for($i=0;$i<7;$i++)
  2157. {
  2158. $current_start = $start + ($i*86400) + $int_time; //unixtimestamp start of today's event
  2159. $current_stop = $start + ($i*86400) + $int_time + $span; //unixtimestamp stop of today's event
  2160. $events[] = array($course_info['id'],$row['id'],$current_start,$current_stop,$row['title'],$row['content']);
  2161. }
  2162. break;
  2163. case 'weekly':
  2164. $time_orig = date('Y/n/W/j/N/G/i/s',$orig_start);
  2165. list($y_orig,$m_orig,$w_orig,$d_orig,$dw_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2166. $time_now = date('Y/n/W/j/N/G/i/s',$end);
  2167. list($y_now,$m_now,$w_now,$d_now,$dw_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2168. if((($y_now>$y_orig) OR (($y_now == $y_orig) && ($w_now>$w_orig))))
  2169. { //if the event is after the original (at least one week) and the day of the week is the same
  2170. $time_orig_end = date('Y/n/W/j/N/G/i/s',$orig_end);
  2171. 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);
  2172. $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']);
  2173. }
  2174. break;
  2175. case 'monthlyByDate':
  2176. $time_orig = date('Y/n/W/j/G/i/s',$orig_start);
  2177. list($y_orig,$m_orig,$w_orig,$d_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2178. $time_now = date('Y/n/W/j/G/i/s',$end);
  2179. list($y_now,$m_now,$w_now,$d_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2180. $event_repetition_time = mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now);
  2181. if((($y_now>$y_orig) OR (($y_now == $y_orig) && ($m_now>$m_orig))) && ($start<$event_repetition_time && $event_repetition_time<$end))
  2182. { //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
  2183. $time_orig_end = date('Y/n/j/G/i/s',$orig_end);
  2184. list($y_orig_e,$m_orig_e,$d_orig_e,$h_orig_e,$n_orig_e,$s_orig_e) = split('/',$time_orig_end);
  2185. $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']);
  2186. }
  2187. break;
  2188. case 'monthlyByDayR':
  2189. //not implemented yet
  2190. break;
  2191. case 'monthlyByDay':
  2192. //not implemented yet
  2193. break;
  2194. case 'yearly':
  2195. $time_orig = date('Y/n/j/z/G/i/s',$orig_start);
  2196. list($y_orig,$m_orig,$d_orig,$dy_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2197. $time_now = date('Y/n/j/z/G/i/s',$end);
  2198. list($y_now,$m_now,$d_now,$dy_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2199. $event_repetition_time = mktime($h_orig,$n_orig,$s_orig,$m_orig,$d_orig,$y_now);
  2200. if((($y_now>$y_orig) && ($start<$event_repetition_time && $event_repetition_time<$end)))
  2201. {
  2202. $time_orig_end = date('Y/n/j/G/i/s',$orig_end);
  2203. 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);
  2204. $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']);
  2205. }
  2206. break;
  2207. default:
  2208. break;
  2209. }
  2210. }
  2211. }
  2212. return $events;
  2213. }
  2214. /**
  2215. * Get repeated events of a course between two dates (timespan of a month).
  2216. * Returns an array containing the events
  2217. * @param string Course info array (as returned by api_get_course_info())
  2218. * @param int UNIX timestamp of span start. Defaults 0, later transformed into today's start
  2219. * @param int UNIX timestamp. Defaults to 0, later transformed into today's end
  2220. * @param array A set of parameters to alter the SQL query
  2221. * @return array [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  2222. */
  2223. function get_repeated_events_month_view($course_info,$start=0,$end=0,$params)
  2224. {
  2225. $events = array();
  2226. //block $end if higher than 2038 -- PHP doesn't go past that
  2227. if($end>2145934800){$end = 2145934800;}
  2228. //initialise all values
  2229. $y=0;
  2230. $m=0;
  2231. $d=0;
  2232. if($start == 0 or $end == 0)
  2233. {
  2234. $time = time();
  2235. $y = date('Y');
  2236. $m = date('m');
  2237. }
  2238. if($start==0)
  2239. {
  2240. $start = mktime(0,0,0,$m,1,$y);
  2241. }
  2242. $db_start = date('Y-m-d H:i:s',$start);
  2243. if($end==0)
  2244. {
  2245. if($m==12)
  2246. {
  2247. $end = mktime(0,0,0,1,1,$y+1)-1; //start of next month, minus 1 second to get back to the previoyus day
  2248. }
  2249. else
  2250. {
  2251. $end = mktime(0,0,0,$m+1,1,$y)-1;
  2252. }
  2253. }
  2254. //$db_end = date('Y-m-d H:i:s',$end);
  2255. $t_cal = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  2256. //$t_cal_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT,$course_info['dbName']);
  2257. //$t_ip = Database::get_course_table(TABLE_ITEM_PROPERTY,$course_info['dbName']);
  2258. $sql = "SELECT c.id, c.title, c.content, " .
  2259. " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, " .
  2260. " cr.cal_type, cr.cal_end " .
  2261. " FROM $t_cal c, $t_cal_repeat cr, $t_ip as item_property " .
  2262. " WHERE cr.cal_end >= $start " .
  2263. " AND cr.cal_id = c.id " .
  2264. " AND item_property.ref = c.id ".
  2265. " AND item_property.tool = '".TOOL_CALENDAR_EVENT."' ".
  2266. " AND c.start_date <= '$db_start' "
  2267. .(!empty($params['conditions'])?$params['conditions']:'')
  2268. .(!empty($params['groupby'])?' GROUP BY '.$params['groupby']:'')
  2269. .(!empty($params['orderby'])?' ORDER BY '.$params['orderby']:'');
  2270. $res = api_sql_query($sql,__FILE__,__LINE__);
  2271. if(Database::num_rows($res)>0)
  2272. {
  2273. while($row = Database::fetch_array($res))
  2274. {
  2275. $orig_start = $row['orig_start'];
  2276. $orig_end = $row['orig_end'];
  2277. $repeat_type = $row['cal_type'];
  2278. switch($repeat_type)
  2279. {
  2280. case 'daily':
  2281. $time_orig_h = date('H',$orig_start);
  2282. $time_orig_m = date('i',$orig_start);
  2283. $time_orig_s = date('s',$orig_start);
  2284. $month_last_day = date('d',$end);
  2285. $int_time = (($time_orig_h*60)+$time_orig_m)*60+$time_orig_s; //time in seconds since 00:00:00
  2286. $span = $orig_end - $orig_start; //total seconds between start and stop of original event
  2287. for($i=0;$i<$month_last_day;$i++)
  2288. {
  2289. $current_start = $start + ($i*86400) + $int_time; //unixtimestamp start of today's event
  2290. $current_stop = $start + ($i*86400) + $int_time + $span; //unixtimestamp stop of today's event
  2291. $events[] = array($course_info['id'],$row['id'],$current_start,$current_stop,$row['title'],$row['content']);
  2292. }
  2293. break;
  2294. case 'weekly':
  2295. //A weekly repeated event is very difficult to catch in a month view,
  2296. //because weeks start before or at the same time as the first day of the month
  2297. //The same can be said for the end of the month.
  2298. // The idea is thus to get all possible events by enlarging the scope of
  2299. // the month to get complete weeks covering the complete month, and then take out
  2300. // the events that start before the 1st ($start) or after the last day of the month ($end)
  2301. $time_orig = date('Y/n/W/j/N/G/i/s',$orig_start);
  2302. list($y_orig,$m_orig,$w_orig,$d_orig,$dw_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2303. $time_orig_end = date('Y/n/W/j/N/G/i/s',$orig_end);
  2304. 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);
  2305. $time_now = date('Y/n/W/j/N/G/i/s',$end);
  2306. list($y_now,$m_now,$w_now,$d_now,$dw_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2307. $month_first_week = date('W',$start);
  2308. $month_last_week = date('W',$end);
  2309. if(($y_now>$y_orig) OR (($y_now == $y_orig) && ($w_now>$w_orig)))
  2310. { //if the event is after the original (at least one week) and the day of the week is the same
  2311. for($i=$month_first_week;$i<=$month_last_week;$i++)
  2312. {
  2313. //the "day of the week" of repetition is the same as the $dw_orig,
  2314. //so to get the "day of the month" from the "day of the week", we have
  2315. //to get the first "day of the week" for this week and add the number
  2316. //of days (in seconds) to reach the $dw_orig
  2317. //example: the first week spans between the 28th of April (Monday) to the
  2318. // 4th of May (Sunday). The event occurs on the 2nd day of each week.
  2319. // This means the event occurs on 29/4, 6/5, 13/5, 20/5 and 27/5.
  2320. // We want to get all of these, and then reject 29/4 because it is out
  2321. // of the month itself.
  2322. //First, to get the start time of the first day of the month view (even if
  2323. // the day is from the past month), we get the month start date (1/5) and
  2324. // see which day of the week it is, and subtract the number of days necessary
  2325. // to get back to the first day of the week.
  2326. $month_first_day_weekday = date('N',$start);
  2327. $first_week_start = $start - (($month_first_day_weekday-1)*86400);
  2328. //Second, we add the week day of the original event, so that we have an
  2329. // absolute time that represents the first repetition of the event in
  2330. // our 4- or 5-weeks timespan
  2331. $first_event_repeat_start = $first_week_start + (($dw_orig-1)*86400) + ($h_orig*3600) + ($n_orig*60) + $s_orig;
  2332. //Third, we start looping through the repetitions and see if they are between
  2333. // $start and $end
  2334. for($i = $first_event_repeat_start; $i<=$end; $i+=604800)
  2335. {
  2336. if($start<$i && $i<$end)
  2337. {
  2338. list($y_repeat,$m_repeat,$d_repeat,$h_repeat,$n_repeat,$s_repeat) = split('/',date('Y/m/j/H/i/s',$i));
  2339. $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']);
  2340. }
  2341. }
  2342. }
  2343. }
  2344. break;
  2345. case 'monthlyByDate':
  2346. $time_orig = date('Y/n/W/j/G/i/s',$orig_start);
  2347. list($y_orig,$m_orig,$w_orig,$d_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2348. $time_now = date('Y/n/W/j/G/i/s',$end);
  2349. list($y_now,$m_now,$w_now,$d_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2350. $event_repetition_time = mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now);
  2351. if(($y_now>$y_orig) OR (($y_now == $y_orig) && ($m_now>$m_orig)))
  2352. { //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
  2353. $time_orig_end = date('Y/n/j/G/i/s',$orig_end);
  2354. list($y_orig_e,$m_orig_e,$d_orig_e,$h_orig_e,$n_orig_e,$s_orig_e) = split('/',$time_orig_end);
  2355. $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']);
  2356. }
  2357. break;
  2358. case 'monthlyByDayR':
  2359. //not implemented yet
  2360. break;
  2361. case 'monthlyByDay':
  2362. //not implemented yet
  2363. break;
  2364. case 'yearly':
  2365. $time_orig = date('Y/n/j/z/G/i/s',$orig_start);
  2366. list($y_orig,$m_orig,$d_orig,$dy_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2367. $time_now = date('Y/n/j/z/G/i/s',$end);
  2368. list($y_now,$m_now,$d_now,$dy_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2369. $event_repetition_time = mktime($h_orig,$n_orig,$s_orig,$m_orig,$d_orig,$y_now);
  2370. if((($y_now>$y_orig) && ($start<$event_repetition_time && $event_repetition_time<$end)))
  2371. {
  2372. $time_orig_end = date('Y/n/j/G/i/s',$orig_end);
  2373. 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);
  2374. $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']);
  2375. }
  2376. break;
  2377. default:
  2378. break;
  2379. }
  2380. }
  2381. }
  2382. return $events;
  2383. }
  2384. /**
  2385. * Get repeated events of a course between two dates (1 year timespan). Used for the list display.
  2386. * 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)
  2387. * Returns an array containing the events
  2388. * @param string Course info array (as returned by api_get_course_info())
  2389. * @param int UNIX timestamp of span start. Defaults 0, later transformed into today's start
  2390. * @param int UNIX timestamp. Defaults to 0, later transformed into today's end
  2391. * @param array A set of parameters to alter the SQL query
  2392. * @return array [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  2393. */
  2394. function get_repeated_events_list_view($course_info,$start=0,$end=0,$params)
  2395. {
  2396. $events = array();
  2397. //block $end if higher than 2038 -- PHP doesn't go past that
  2398. if($end>2145934800){$end = 2145934800;}
  2399. //initialise all values
  2400. $y=0;
  2401. $m=0;
  2402. $d=0;
  2403. if(empty($start) or empty($end))
  2404. {
  2405. $time = time();
  2406. $y = date('Y');
  2407. $m = date('m');
  2408. }
  2409. if(empty($start))
  2410. {
  2411. $start = mktime(0, 0, 0, $m, 1, $y);
  2412. }
  2413. $db_start = date('Y-m-d H:i:s', $start);
  2414. if(empty($end))
  2415. {
  2416. $end = mktime(0, 0, 0, 1, 1, 2037);
  2417. }
  2418. //$db_end = date('Y-m-d H:i:s',$end);
  2419. $t_cal = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR,$course_info['dbName']);
  2420. //$t_cal_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT,$course_info['dbName']);
  2421. //$t_ip = Database::get_course_table(TABLE_ITEM_PROPERTY,$course_info['dbName']);
  2422. $sql = "SELECT c.id, c.title, c.content, " .
  2423. " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, " .
  2424. " cr.cal_type, cr.cal_end " .
  2425. " FROM $t_cal c, $t_cal_repeat cr, $t_ip as item_property " .
  2426. " WHERE cr.cal_end >= $start " .
  2427. " AND cr.cal_id = c.id " .
  2428. " AND item_property.ref = c.id ".
  2429. " AND item_property.tool = '".TOOL_CALENDAR_EVENT."' ".
  2430. " AND c.start_date <= '$db_start' "
  2431. .(!empty($params['conditions'])?$params['conditions']:'')
  2432. .(!empty($params['groupby'])?' GROUP BY '.$params['groupby']:'')
  2433. .(!empty($params['orderby'])?' ORDER BY '.$params['orderby']:'');
  2434. $res = api_sql_query($sql,__FILE__,__LINE__);
  2435. if(Database::num_rows($res)>0)
  2436. {
  2437. while($row = Database::fetch_array($res))
  2438. {
  2439. $orig_start = $row['orig_start'];
  2440. $orig_end = $row['orig_end'];
  2441. $repeat_type = $row['cal_type'];
  2442. $repeat_end = $row['cal_end'];
  2443. switch($repeat_type)
  2444. {
  2445. case 'daily':
  2446. $time_orig_h = date('H',$orig_start);
  2447. $time_orig_m = date('i',$orig_start);
  2448. $time_orig_s = date('s',$orig_start);
  2449. $span = $orig_end - $orig_start; //total seconds between start and stop of original event
  2450. for($i=$orig_start+86400;($i<$end && $i<=$repeat_end);$i+=86400)
  2451. {
  2452. $current_start = $i; //unixtimestamp start of today's event
  2453. $current_stop = $i + $span; //unixtimestamp stop of today's event
  2454. $events[] = array($course_info['id'],$row['id'],$current_start,$current_stop,$row['title'],$row['content']);
  2455. }
  2456. break;
  2457. case 'weekly':
  2458. //A weekly repeated event is very difficult to catch in a month view,
  2459. // because weeks start before or at the same time as the first day of the month
  2460. //The same can be said for the end of the month.
  2461. // The idea is thus to get all possible events by enlarging the scope of
  2462. // the month to get complete weeks covering the complete month, and then take out
  2463. // the events that start before the 1st ($start) or after the last day of the month ($end)
  2464. $time_orig = date('Y/n/W/j/N/G/i/s',$orig_start);
  2465. list($y_orig,$m_orig,$w_orig,$d_orig,$dw_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2466. $time_orig_end = date('Y/n/W/j/N/G/i/s',$orig_end);
  2467. 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);
  2468. $time_now = date('Y/n/W/j/N/G/i/s',$end);
  2469. list($y_now,$m_now,$w_now,$d_now,$dw_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2470. if($w_now==52)
  2471. {
  2472. ++$y_now;
  2473. $w_now=1;
  2474. }
  2475. else
  2476. {
  2477. ++$w_now;
  2478. }
  2479. $month_first_week = date('W',$start);
  2480. $total_weeks = ((date('Y',$end)-$y_orig)-1)*52;
  2481. $month_last_week = $month_first_week + $total_weeks;
  2482. if(($y_now>$y_orig) OR (($y_now == $y_orig) && ($w_now>$w_orig)))
  2483. { //if the event is after the original (at least one week) and the day of the week is the same
  2484. //for($i=$month_first_week;($i<=$month_last_week && $i<1000);$i++)
  2485. //{
  2486. /*
  2487. The "day of the week" of repetition is the same as the $dw_orig,
  2488. so to get the "day of the month" from the "day of the week", we have
  2489. to get the first "day of the week" for this week and add the number
  2490. of days (in seconds) to reach the $dw_orig
  2491. example: the first week spans between the 28th of April (Monday) to the
  2492. 4th of May (Sunday). The event occurs on the 2nd day of each week.
  2493. This means the event occurs on 29/4, 6/5, 13/5, 20/5 and 27/5.
  2494. We want to get all of these, and then reject 29/4 because it is out
  2495. of the month itself.
  2496. First, to get the start time of the first day of the month view (even if
  2497. the day is from the past month), we get the month start date (1/5) and
  2498. see which day of the week it is, and subtract the number of days necessary
  2499. to get back to the first day of the week.
  2500. */
  2501. $month_first_day_weekday = date('N',$start);
  2502. $first_week_start = $start - (($month_first_day_weekday-1)*86400);
  2503. //Second, we add the week day of the original event, so that we have an
  2504. // absolute time that represents the first repetition of the event in
  2505. // our 4- or 5-weeks timespan
  2506. $first_event_repeat_start = $first_week_start + (($dw_orig-1)*86400) + ($h_orig*3600) + ($n_orig*60) + $s_orig;
  2507. //Third, we start looping through the repetitions and see if they are between
  2508. // $start and $end
  2509. for($i = $first_event_repeat_start; ($i<=$end && $i<=$repeat_end); $i+=604800)
  2510. {
  2511. if($start<$i && $i<=$end && $i<=$repeat_end)
  2512. {
  2513. list($y_repeat,$m_repeat,$d_repeat,$h_repeat,$n_repeat,$s_repeat) = split('/',date('Y/m/j/H/i/s',$i));
  2514. $new_start_time = mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now);
  2515. $new_stop_time = mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now);
  2516. $events[] = array($course_info['id'], $row['id'], $new_start_time, $new_stop_time, $row['title'], $row['content']);
  2517. }
  2518. $time_now = date('Y/n/W/j/N/G/i/s',$i+604800);
  2519. list($y_now,$m_now,$w_now,$d_now,$dw_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2520. }
  2521. //}
  2522. }
  2523. break;
  2524. case 'monthlyByDate':
  2525. $time_orig = date('Y/n/W/j/G/i/s',$orig_start);
  2526. list($y_orig,$m_orig,$w_orig,$d_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2527. $time_now = date('Y/n/W/j/G/i/s',$start);
  2528. list($y_now,$m_now,$w_now,$d_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2529. //make sure we are one month ahead (to avoid being the same month as the original event)
  2530. if($m_now==12)
  2531. {
  2532. ++$y_now;
  2533. $m_now = 1;
  2534. }
  2535. else
  2536. {
  2537. ++$m_now;
  2538. }
  2539. $time_orig_end = date('Y/n/j/G/i/s',$orig_end);
  2540. list($y_orig_e,$m_orig_e,$d_orig_e,$h_orig_e,$n_orig_e,$s_orig_e) = split('/',$time_orig_end);
  2541. $event_repetition_time = mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now);
  2542. $diff = $orig_end - $orig_start;
  2543. while((($y_now>$y_orig) OR (($y_now == $y_orig) && ($m_now>$m_orig))) && ($event_repetition_time < $end) && ($event_repetition_time < $repeat_end))
  2544. { //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
  2545. $new_start_time = mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now);
  2546. $new_stop_time = mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now);
  2547. $events[] = array($course_info['id'],$row['id'],$new_start_time,$new_stop_time,$row['title'],$row['content']);
  2548. if($m_now==12)
  2549. {
  2550. ++$y_now;
  2551. $m_now = 1;
  2552. }
  2553. else
  2554. {
  2555. ++$m_now;
  2556. }
  2557. $event_repetition_time = mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now);
  2558. }
  2559. break;
  2560. case 'monthlyByDayR':
  2561. //not implemented yet
  2562. break;
  2563. case 'monthlyByDay':
  2564. //not implemented yet
  2565. break;
  2566. case 'yearly':
  2567. $time_orig = date('Y/n/j/z/G/i/s',$orig_start);
  2568. list($y_orig,$m_orig,$d_orig,$dy_orig,$h_orig,$n_orig,$s_orig) = split('/',$time_orig);
  2569. $time_now = date('Y/n/j/z/G/i/s',$end);
  2570. list($y_now,$m_now,$d_now,$dy_now,$h_now,$n_now,$s_now) = split('/',$time_now);
  2571. $event_repetition_time = mktime($h_orig,$n_orig,$s_orig,$m_orig,$d_orig,$y_now);
  2572. while((($y_now>$y_orig) && ($start<$event_repetition_time && $event_repetition_time<$end && $event_repetition_time<$repeat_end)))
  2573. {
  2574. $time_orig_end = date('Y/n/j/G/i/s',$orig_end);
  2575. 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);
  2576. $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']);
  2577. ++$y_now;
  2578. $event_repetition_time = mktime($h_orig,$n_orig,$s_orig,$m_orig,$d_orig,$y_now);
  2579. }
  2580. break;
  2581. default:
  2582. break;
  2583. }
  2584. }
  2585. }
  2586. return $events;
  2587. }
  2588. /**
  2589. * Tells if an agenda item is repeated
  2590. * @param string Course database
  2591. * @param int The agenda item
  2592. * @return boolean True if repeated, false otherwise
  2593. */
  2594. function is_repeated_event($id,$course=null)
  2595. {
  2596. if(empty($course))
  2597. {
  2598. $course_info = api_get_course_info();
  2599. $course = $course_info['dbName'];
  2600. }
  2601. $id = (int) $id;
  2602. //$t_agenda_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT,$course);
  2603. $sql = "SELECT * FROM $t_agenda_repeat WHERE cal_id = $id";
  2604. $res = Database::query($sql,__FILE__,__LINE__);
  2605. if(Database::num_rows($res)>0)
  2606. {
  2607. return true;
  2608. }
  2609. return false;
  2610. }
  2611. /**
  2612. * Adds x weeks to a UNIX timestamp
  2613. * @param int The timestamp
  2614. * @param int The number of weeks to add
  2615. * @return int The new timestamp
  2616. */
  2617. function add_week($timestamp,$num=1)
  2618. {
  2619. return $timestamp + $num*604800;
  2620. }
  2621. /**
  2622. * Adds x months to a UNIX timestamp
  2623. * @param int The timestamp
  2624. * @param int The number of years to add
  2625. * @return int The new timestamp
  2626. */
  2627. function add_month($timestamp,$num=1)
  2628. {
  2629. list($y, $m, $d, $h, $n, $s) = split('/',date('Y/m/d/h/i/s',$timestamp));
  2630. if($m+$num>12)
  2631. {
  2632. $y += floor($num/12);
  2633. $m += $num%12;
  2634. }
  2635. else
  2636. {
  2637. $m += $num;
  2638. }
  2639. return mktime($h, $n, $s, $m, $d, $y);
  2640. }
  2641. /**
  2642. * Adds x years to a UNIX timestamp
  2643. * @param int The timestamp
  2644. * @param int The number of years to add
  2645. * @return int The new timestamp
  2646. */
  2647. function add_year($timestamp,$num=1)
  2648. {
  2649. list($y, $m, $d, $h, $n, $s) = split('/',date('Y/m/d/h/i/s',$timestamp));
  2650. return mktime($h, $n, $s, $m, $d, $y+$num);
  2651. }
  2652. /**
  2653. * Adds an agenda item in the database. Similar to store_new_agenda_item() except it takes parameters
  2654. * @param array Course info
  2655. * @param string Event title
  2656. * @param string Event content/description
  2657. * @param string Start date
  2658. * @param string End date
  2659. * @param array List of groups to which this event is added
  2660. * @param int Parent id (optional)
  2661. * @return int The new item's DB ID
  2662. */
  2663. function agenda_add_item($course_info, $title, $content, $db_start_date, $db_end_date, $to=array(), $parent_id=null)
  2664. {
  2665. global $_course;
  2666. $user_id = api_get_user_id();
  2667. $t_agenda = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  2668. // some filtering of the input data
  2669. $title = Database::escape_string($title); // no html allowed in the title
  2670. $content = Database::escape_string($content);
  2671. $start_date = Database::escape_string($db_start_date);
  2672. $end_date = Database::escape_string($db_end_date);
  2673. isset($_SESSION['id_session'])?$id_session=intval($_SESSION['id_session']):$id_session=null;
  2674. // store in the table calendar_event
  2675. // check if exists in calendar_event table
  2676. $sql = "SELECT * FROM $t_agenda WHERE title='$title' AND content = '$content' AND start_date = '$start_date'
  2677. AND end_date = '$end_date' ".(!empty($parent_id)? "AND parent_event_id = '$parent_id'":"");
  2678. $result = api_sql_query($sql,__FILE__,__LINE__);
  2679. $count = Database::num_rows($result);
  2680. if ($count > 0) {
  2681. return false;
  2682. }
  2683. $sql = "INSERT INTO ".$t_agenda."
  2684. (title,content, start_date, end_date)
  2685. VALUES
  2686. ('".$title."','".$content."', '".$start_date."','".$end_date."')";
  2687. $result = api_sql_query($sql,__FILE__,__LINE__) or die (Database::error());
  2688. $last_id=Database::insert_id();
  2689. // add a attachment file in agenda
  2690. // add_agenda_attachment_file($file_comment,$last_id);
  2691. // store in last_tooledit (first the groups, then the users
  2692. $done = false;
  2693. if ((!is_null($to))or (!empty($_SESSION['toolgroup']))) // !is_null($to): when no user is selected we send it to everyone
  2694. {
  2695. $send_to=separate_users_groups($to);
  2696. // storing the selected groups
  2697. if (is_array($send_to['groups']))
  2698. {
  2699. foreach ($send_to['groups'] as $group)
  2700. {
  2701. api_item_property_update($course_info, TOOL_CALENDAR_EVENT, $last_id, "AgendaAdded", $user_id, $group,'',$start_date, $end_date);
  2702. $done = true;
  2703. }
  2704. }
  2705. // storing the selected users
  2706. if (is_array($send_to['users']))
  2707. {
  2708. foreach ($send_to['users'] as $user)
  2709. {
  2710. api_item_property_update($course_info, TOOL_CALENDAR_EVENT, $last_id, "AgendaAdded", $user_id,'',$user, $start_date,$end_date);
  2711. $done = true;
  2712. }
  2713. }
  2714. }
  2715. if(!$done) // the message is sent to everyone, so we set the group to 0
  2716. {
  2717. api_item_property_update($course_info, TOOL_CALENDAR_EVENT, $last_id, "AgendaAdded", $user_id, $start_date,$end_date);
  2718. }
  2719. // storing the resources
  2720. if (!empty($_SESSION['source_type']) && !empty($last_id)) {
  2721. //store_resources($_SESSION['source_type'],$last_id);
  2722. }
  2723. return $last_id;
  2724. }
  2725. /**
  2726. * Adds a repetitive item to the database
  2727. * @param array Course info
  2728. * @param int The original event's id
  2729. * @param string Type of repetition
  2730. * @param int Timestamp of end of repetition (repeating until that date)
  2731. * @param array Original event's destination
  2732. * @return boolean False if error, True otherwise
  2733. */
  2734. function get_calendar_items($month, $year)
  2735. {
  2736. global $_user, $_course;
  2737. global $is_allowed_to_edit;
  2738. $month=Database::escape_string($month);
  2739. $year=Database::escape_string($year);
  2740. // database variables
  2741. $TABLEAGENDA = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  2742. //$TABLE_ITEM_PROPERTY=Database::get_course_table(TABLE_ITEM_PROPERTY);
  2743. $month_first_day = mktime(0,0,0,$month,1,$year);
  2744. $month_last_day = mktime(0,0,0,$month+1,1,$year)-1;
  2745. if($month==12) {
  2746. $month_last_day = mktime(0,0,0,1,1,$year+1)-1;
  2747. }
  2748. $repeats = array();
  2749. if (is_allowed_to_edit()) {
  2750. $sql="SELECT
  2751. DISTINCT *
  2752. FROM ".$TABLEAGENDA." agenda
  2753. WHERE MONTH(start_date)='".$month."' AND YEAR(start_date)='".$year."'
  2754. GROUP BY id ".
  2755. "ORDER BY start_date ";
  2756. }
  2757. $result=api_sql_query($sql,__FILE__,__LINE__);
  2758. $data=array();
  2759. while ($row=Database::fetch_array($result)) {
  2760. $datum_item=(int)substr($row["start_date"],8,2);
  2761. $data[$datum_item][intval($datum_item)][] = $row;
  2762. }
  2763. return $data;
  2764. }
  2765. function agenda_add_repeat_item($course_info,$orig_id,$type,$end,$orig_dest)
  2766. {/*
  2767. $t_agenda = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR,$course_info['dbName']);
  2768. // $t_agenda_r = Database::get_course_table(TABLE_AGENDA_REPEAT,$course_info['dbName']);
  2769. //$sql = "SELECT title, content, UNIX_TIMESTAMP(start_date) as sd, UNIX_TIMESTAMP(end_date) as ed FROM $t_agenda WHERE id = $orig_id";
  2770. $sql = "SELECT title, content, start_date as sd, end_date as ed FROM $t_agenda WHERE id = $orig_id";
  2771. $res = Database::query($sql,__FILE__,__LINE__);
  2772. if(Database::num_rows($res)!==1){return false;}
  2773. $row = Database::fetch_array($res);
  2774. //$orig_start = $row['sd'];
  2775. $orig_start = mktime(substr($row['sd'],11,2),substr($row['sd'],14,2),substr($row['sd'],17,2),substr($row['sd'],5,2),substr($row['sd'],8,2),substr($row['sd'],0,4));
  2776. //$orig_end = $row['ed'];
  2777. $orig_end = mktime(substr($row['ed'],11,2),substr($row['ed'],14,2),substr($row['ed'],17,2),substr($row['ed'],5,2),substr($row['ed'],8,2),substr($row['ed'],0,4));
  2778. $diff = $orig_end - $orig_start;
  2779. $orig_title = $row['title'];
  2780. $orig_content = $row['content'];
  2781. $now = time();
  2782. $type = Database::escape_string($type);
  2783. $end = (int) $end;
  2784. if(1<=$end && $end<=500)
  2785. {
  2786. //we assume that, with this type of value, the user actually gives a count of repetitions
  2787. //and that he wants us to calculate the end date with that (particularly in case of imports from ical)
  2788. switch($type)
  2789. {
  2790. case 'daily':
  2791. $end = $orig_start + (86400*$end);
  2792. break;
  2793. case 'weekly':
  2794. $end = add_week($orig_start,$end);
  2795. break;
  2796. case 'monthlyByDate':
  2797. $end = add_month($orig_start,$end);
  2798. break;
  2799. case 'monthlyByDay':
  2800. //TODO
  2801. break;
  2802. case 'monthlyByDayR':
  2803. //TODO
  2804. break;
  2805. case 'yearly':
  2806. $end = add_year($orig_start,$end);
  2807. break;
  2808. }
  2809. }
  2810. if($end > $now
  2811. && in_array($type,array('daily','weekly','monthlyByDate','monthlyByDay','monthlyByDayR','yearly')))
  2812. {
  2813. $sql = "INSERT INTO $t_agenda_r (cal_id, cal_type, cal_end)" .
  2814. " VALUES ($orig_id,'$type',$end)";
  2815. $res = Database::query($sql,__FILE__,__LINE__);
  2816. switch($type)
  2817. {
  2818. case 'daily':
  2819. for($i = $orig_start + 86400; ($i <= $end); $i += 86400)
  2820. {
  2821. $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);
  2822. }
  2823. break;
  2824. case 'weekly':
  2825. for($i = $orig_start + 604800; ($i <= $end); $i += 604800)
  2826. {
  2827. $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);
  2828. }
  2829. break;
  2830. case 'monthlyByDate':
  2831. $next_start = add_month($orig_start);
  2832. while($next_start <= $end)
  2833. {
  2834. $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);
  2835. $next_start = add_month($next_start);
  2836. }
  2837. break;
  2838. case 'monthlyByDay':
  2839. //not yet implemented
  2840. break;
  2841. case 'monthlyByDayR':
  2842. //not yet implemented
  2843. break;
  2844. case 'yearly':
  2845. $next_start = add_year($orig_start);
  2846. while($next_start <= $end)
  2847. {
  2848. $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);
  2849. $next_start = add_year($next_start);
  2850. }
  2851. break;
  2852. }
  2853. }
  2854. return true;*/
  2855. }
  2856. /**
  2857. * Import an iCal file into the database
  2858. * @param array Course info
  2859. * @return boolean True on success, false otherwise
  2860. */
  2861. function agenda_import_ical($course_info,$file)
  2862. {
  2863. require_once(api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  2864. $charset = api_get_setting('platform_charset');
  2865. $filepath = api_get_path(GARBAGE_PATH).$file['name'];
  2866. if(!@move_uploaded_file($file['tmp_name'],$filepath))
  2867. {
  2868. error_log('Problem moving uploaded file: '.$file['error'].' in '.__FILE__.' line '.__LINE__);
  2869. return false;
  2870. }
  2871. require_once (api_get_path(LIBRARY_PATH).'icalcreator/iCalcreator.class.php');
  2872. $ical = new vcalendar();
  2873. $ical->setConfig( 'directory', dirname($filepath) );
  2874. $ical->setConfig( 'filename', basename($filepath) );
  2875. $ical->parse();
  2876. //we need to recover: summary, description, dtstart, dtend, organizer, attendee, location (=course name),
  2877. // rrule
  2878. $ve = $ical->getComponent(0);
  2879. //print_r($ve);
  2880. $ttitle = $ve->getProperty('summary');
  2881. //print_r($ttitle);
  2882. $title = mb_convert_encoding($ttitle,$charset,'UTF-8');
  2883. $tdesc = $ve->getProperty('description');
  2884. $desc = mb_convert_encoding($tdesc,$charset,'UTF-8');
  2885. $ts = $ve->getProperty('dtstart');
  2886. $start_date = $ts['year'].'-'.$ts['month'].'-'.$ts['day'].' '.$ts['hour'].':'.$ts['min'].':'.$ts['sec'];
  2887. $ts = $ve->getProperty('dtend');
  2888. $end_date = $ts['year'].'-'.$ts['month'].'-'.$ts['day'].' '.$ts['hour'].':'.$ts['min'].':'.$ts['sec'];
  2889. //echo $start_date.' - '.$end_date;
  2890. $organizer = $ve->getProperty('organizer');
  2891. $attendee = $ve->getProperty('attendee');
  2892. $course_name = $ve->getProperty('location');
  2893. //insert the event in our database
  2894. $id = agenda_add_item($course_info,$title,$desc,$start_date,$end_date,$_POST['selectedform']);
  2895. $repeat = $ve->getProperty('rrule');
  2896. if(is_array($repeat) && !empty($repeat['FREQ']))
  2897. {
  2898. $trans = array('DAILY'=>'daily','WEEKLY'=>'weekly','MONTHLY'=>'monthlyByDate','YEARLY'=>'yearly');
  2899. $freq = $trans[$repeat['FREQ']];
  2900. $interval = $repeat['INTERVAL'];
  2901. if(isset($repeat['UNTIL']) && is_array($repeat['UNTIL']))
  2902. {
  2903. $until = mktime(23,59,59,$repeat['UNTIL']['month'],$repeat['UNTIL']['day'],$repeat['UNTIL']['year']);
  2904. $res = agenda_add_repeat_item($course_info,$id,$freq,$until,$_POST['selectedform']);
  2905. }
  2906. //TODO: deal with count
  2907. if(!empty($repeat['COUNT']))
  2908. {
  2909. $count = $repeat['COUNT'];
  2910. $res = agenda_add_repeat_item($course_info,$id,$freq,$count,$_POST['selectedform']);
  2911. }
  2912. }
  2913. return true;
  2914. }
  2915. ?>