agenda.inc.php 193 KB

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