agenda.inc.php 220 KB

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