agenda.inc.php 151 KB

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