agenda.inc.php 220 KB

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