12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519 |
- <?php
- /**
- * Elgg event model
- *
- * @package event_calendar
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
- * @author Kevin Jardine <kevin@radagast.biz>
- * @copyright Radagast Solutions 2008-2011
- * @link http://radagast.biz/
- *
- */
- function event_calendar_get_event_for_edit($event_id) {
- if ($event_id && $event = get_entity($event_id)) {
- if ($event->canEdit()) {
- return $event;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- // converts to time in minutes since midnight
- function event_calendar_convert_to_time($hour,$minute,$meridian) {
- if ($meridian) {
- if ($meridian == 'am') {
- if ($hour == 12) {
- $hour = 0;
- }
- } else {
- if ($hour < 12) {
- $hour += 12;
- }
- }
- }
- return 60*$hour+$minute;
- }
- // returns the event or FALSE
- function event_calendar_set_event_from_form($event_guid,$group_guid) {
- $event_calendar_times = elgg_get_plugin_setting('times', 'event_calendar');
- $event_calendar_region_display = elgg_get_plugin_setting('region_display', 'event_calendar');
- $event_calendar_type_display = elgg_get_plugin_setting('type_display', 'event_calendar');
- $event_calendar_spots_display = elgg_get_plugin_setting('spots_display', 'event_calendar');
- $event_calendar_hide_end = elgg_get_plugin_setting('hide_end', 'event_calendar');
- $event_calendar_more_required = elgg_get_plugin_setting('more_required', 'event_calendar');
- $event_calendar_personal_manage = elgg_get_plugin_setting('personal_manage', 'event_calendar');
- $event_calendar_repeating_events = elgg_get_plugin_setting('repeating_events', 'event_calendar');
- $schedule_type = get_input('schedule_type');
- if ($event_calendar_more_required == 'yes') {
- $required_fields = array('title','venue','start_date',
- 'brief_description','fees','contact','organiser',
- 'tags');
- if ($event_calendar_times != 'no') {
- $required_fields[] = 'start_time';
- if ($event_calendar_hide_end != 'yes') {
- $required_fields[] = 'end_time';
- }
- }
- if ($event_calendar_region_display == 'yes') {
- $required_fields[] = 'region';
- }
- if ($event_calendar_type_display == 'yes') {
- $required_fields[] = 'event_type';
- }
- if ($event_calendar_spots_display == 'yes') {
- $required_fields[] = 'spots';
- }
- } else {
- $required_fields = array('title');
- }
- if ($event_guid) {
- $event = get_entity($event_guid);
- if (!elgg_instanceof($event, 'object', 'event_calendar')) {
- // do nothing because this is a bad event guid
- return FALSE;
- }
- } else {
- $user_guid = elgg_get_logged_in_user_guid();
- $event = new ElggObject();
- $event->subtype = 'event_calendar';
- $event->owner_guid = $user_guid;
- if ($group_guid) {
- $event->container_guid = $group_guid;
- } else {
- $event->container_guid = $event->owner_guid;
- }
- }
- $event->access_id = get_input('access_id');
- $event->title = get_input('title');
- $event->description = get_input('description');
- $event->venue = get_input('venue');
- if ($schedule_type != 'poll') {
- $start_date_text = trim(get_input('start_date'));
- /*$event->original_start_date = get_input('start_date');
- //$end_date = trim(get_input('end_date',''));
- // convert start date from current server time to GMT
- $start_date_text = gmdate("Y-m-d",$start_date);
- //$event->munged_start_date_string = $start_date_text." ".date_default_timezone_get();*/
-
- // TODO: is the timezone bit necessary?
- $event->start_date = strtotime($start_date_text." GMT");
- $end_date_text = trim(get_input('end_date',''));
- //$event->original_end_date = get_input('end_date');
- if ($end_date_text) {
- $event->end_date = strtotime($end_date_text." GMT");
- //$event->munged_end_date_string = $end_date_text." ".date_default_timezone_get();
- } else {
- $event->end_date = '';
- }
-
- if ($event_calendar_times != 'no') {
- $hour = get_input('start_time_hour','');
- $minute = get_input('start_time_minute','');
- $meridian = get_input('start_time_meridian','');
- if (is_numeric($hour) && is_numeric($minute)) {
- $event->start_time = event_calendar_convert_to_time($hour,$minute,$meridian);
- } else {
- $event->start_time = '';
- }
- $hour = get_input('end_time_hour','');
- $minute = get_input('end_time_minute','');
- $meridian = get_input('end_time_meridian','');
- if (is_numeric($hour) && is_numeric($minute)) {
- $event->end_time = event_calendar_convert_to_time($hour,$minute,$meridian);
- } else {
- $event->end_time = '';
- }
- if (is_numeric($event->start_date) && is_numeric($event->start_time)) {
- // Set start date to the Unix start time, if set.
- // This allows sorting by date *and* time.
- $event->start_date += $event->start_time*60;
- }
- }
- }
- if ($event_calendar_spots_display == 'yes') {
- $event->spots = trim(get_input('spots'));
- }
- if ($event_calendar_region_display == 'yes') {
- $event->region = get_input('region');
- }
- if ($event_calendar_type_display == 'yes') {
- $event->event_type = get_input('event_type');
- }
- if ($event_calendar_personal_manage == 'by_event') {
- $event->personal_manage = get_input('personal_manage');
- }
-
- if ($event_calendar_repeating_events != 'no') {
- $repeats = get_input('repeats');
- $event->repeats = $repeats;
- if ($repeats == 'yes') {
- $event->repeat_interval = get_input('repeat_interval');
- $dow = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');
- foreach ($dow as $w) {
- $v = 'event-calendar-repeating-'.$w.'-value';
- $event->$v = get_input($v);
- }
- }
- }
- $event->fees = get_input('fees');
- $event->contact = get_input('contact');
- $event->organiser = get_input('organiser');
- $event->tags = string_to_tag_array(get_input('tags'));
- $event->long_description = get_input('long_description');
- $event->schedule_type = $schedule_type;
- $event->send_reminder = get_input('send_reminder');
- $event->reminder_number = get_input('reminder_number');
- $event->reminder_interval = get_input('reminder_interval');
- $event->web_conference = get_input('web_conference');
- $event->real_end_time = event_calendar_get_end_time($event);
- foreach ($required_fields as $fn) {
- if (!trim($event->$fn)) {
- return FALSE;
- break;
- }
- }
- if ($event->save()) {
- if (!$event_guid && $event->web_conference) {
- if (!event_calendar_create_bbb_conf($event)) {
- register_error(elgg_echo('event_calendar:conference_create_error'));
- }
- }
- if ($group_guid && (elgg_get_plugin_setting('autogroup', 'event_calendar') == 'yes')) {
- event_calendar_add_personal_events_from_group($event->guid,$group_guid);
- }
- if (elgg_get_plugin_setting('add_users', 'event_calendar') == 'yes') {
- if (function_exists('autocomplete_member_to_user')) {
- $addusers = get_input('adduser',array());
- foreach($addusers as $adduser) {
- if ($adduser) {
- $user = autocomplete_member_to_user($adduser);
- $user_id = $user->guid;
- event_calendar_add_personal_event($event->guid,$user_id);
- if (elgg_get_plugin_setting('add_users_notify', 'event_calendar') == 'yes') {
- notify_user($user_id, $CONFIG->site->guid, elgg_echo('event_calendar:add_users_notify:subject'),
- sprintf(
- elgg_echo('event_calendar:add_users_notify:body'),
- $user->name,
- $event->title,
- $event->getURL()
- )
- );
- }
- }
- }
- }
- }
- }
- return $event;
- }
- function event_calendar_get_events_between($start_date,$end_date,$is_count=FALSE,$limit=10,$offset=0,$container_guid=0,$region='-') {
- $polls_supported = elgg_is_active_plugin('event_poll');
- if ($is_count) {
- $count = event_calendar_get_entities_from_metadata_between2('start_date','end_date',
- $start_date, $end_date, "object", "event_calendar", 0, $container_guid, $limit,$offset,"",0,false,true,$region);
- return $count;
- } else {
- $events = event_calendar_get_entities_from_metadata_between2('start_date','end_date',
- $start_date, $end_date, "object", "event_calendar", 0, $container_guid, $limit,$offset,"",0,false,false,$region);
- $repeating_events = event_calendar_get_repeating_events_between($start_date,$end_date,$container_guid,$region);
- $all_events = event_calendar_merge_repeating_events($events, $repeating_events);
- if ($polls_supported) {
- elgg_load_library('elgg:event_poll');
- $all_events = event_poll_merge_poll_events($all_events,$start_date,$end_date);
- }
-
- return $all_events;
- }
- }
- function event_calendar_merge_repeating_events($events, $repeating_events) {
- $non_repeating_events = array();
- foreach($events as $e) {
- if ($e->repeats != 'yes') {
- $non_repeating_events[] = array('event' => $e,'data' => array(array('start_time' => $e->start_date, 'end_time' => $e->real_end_time)));
- }
- }
-
- return array_merge($non_repeating_events, $repeating_events);
- }
- function event_calendar_get_repeating_events_between($start_date,$end_date,$container_guid,$region) {
- // game plan: get all repeating events with start date <= $end_date and then generate all possible events
- // sanity check
- if ($start_date <= $end_date) {
- $options = array(
- 'type' => 'object',
- 'subtype' => 'event_calendar',
- 'limit' => 0,
- 'metadata_name_value_pairs' => array(
- array(
- 'name' => 'start_date',
- 'value' => $end_date,
- 'operand' => '<='
- ),
- array(
- 'name' => 'repeats',
- 'value' => 'yes'
- ),
- )
- );
- if ($container_guid) {
- if (is_array($container_guid)) {
- $options['container_guids'] = $container_guid;
- } else {
- $options['container_guid'] = $container_guid;
- }
- }
-
- if ($region && $region != '-') {
- $options['metadata_name_value_pairs'][] = array(
- 'name' => 'region',
- 'value' => $region
- );
- }
-
- $events = elgg_get_entities_from_metadata($options);
- }
- return event_calendar_get_repeating_event_structure($events, $start_date, $end_date);
- }
- function event_calendar_get_repeating_event_structure($events, $start_date, $end_date) {
- $dow = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');
- $repeating_events = array();
- if ($events) {
- foreach($events as $e) {
- $incs = array();
- $repeat_data = array();
- $day_num = date('N',$e->start_date)-1;
- for($d=0;$d<7;$d++) {
- $fn = 'event-calendar-repeating-'.$dow[$d].'-value';
- if ($e->$fn) {
- $increment = $d - $day_num;
- $incs[] = $increment;
- }
- }
- if ($incs) {
- sort($incs);
-
- $repeat_interval = $e->repeat_interval;
- $event_start_time = $e->start_date;
- $event_end_time = $e->real_end_time;
- $week = 0;
- if ($event_start_time <= $event_end_time) {
- $more_to_do = TRUE;
- $cur_start_time = $event_start_time;
- $cur_end_time = $event_end_time;
- // keep generating events until after $end_date
- // repeat_times is a sanity check to prevent infinite loops in case of bad data
- $repeat_times = 0;
- do {
- foreach($incs as $inc) {
- //$seconds = $inc*60*60*24;
- if ($inc >=0) {
- $tinc = "+ " . $inc;
- } else {
- $tinc = $inc;
- }
- $this_start_time = strtotime($tinc . " day", $cur_start_time);
- $this_end_time = strtotime($tinc . " day", $cur_end_time);
- if ($this_start_time > $end_date) {
- $more_to_do = FALSE;
- break;
- }
- if ($this_start_time >= $event_start_time) {
- $repeat_data[] = array (
- 'start_time' => $this_start_time,
- 'end_time' => $this_end_time,
- );
- }
- }
- // repeat_interval weeks later
- $week += $repeat_interval;
- $cur_start_time = strtotime("+" . $week . " week", $event_start_time);
- $cur_end_time = strtotime("+" . $week ." week", $event_end_time);
- $repeat_times += 1;
- } while ($repeat_times < 1000 && $more_to_do);
- }
- }
- $repeating_events[] = array('event'=>$e,'data'=>$repeat_data);
- }
- }
- return $repeating_events;
- }
- function event_calendar_get_open_events_between($start_date,$end_date,
- $is_count,$limit=10,$offset=0,$container_guid=0,$region='-', $meta_max = 'spots', $annotation_name = 'personal_event') {
- if ($is_count) {
- $count = event_calendar_get_entities_from_metadata_between2('start_date','end_date',
- $start_date, $end_date, "object", "event_calendar", 0, $container_guid, $limit,$offset,"",0,false,true,$region,$meta_max,$annotation_name);
- return $count;
- } else {
- $events = event_calendar_get_entities_from_metadata_between2('start_date','end_date',
- $start_date, $end_date, "object", "event_calendar", 0, $container_guid, $limit,$offset,"",0,false,false,$region,$meta_max,$annotation_name);
- //return event_calendar_vsort($events,'start_date');
- $repeating_events = event_calendar_get_open_repeating_events_between($start_date,$end_date,$container_guid,$region);
- $all_events = event_calendar_merge_repeating_events($events, $repeating_events);
- return $all_events;
- }
- }
- function event_calendar_get_open_repeating_events_between($start_date,$end_date,$container_guid,$region) {
- $db_prefix = elgg_get_config('dbprefix');
- $meta_max = 'spots';
- $annotation_name = 'personal_event';
- $joins = array();
- $wheres = array();
- $meta_max_n = get_metastring_id($meta_max);
- $ann_n = get_metastring_id($annotation_name);
- if (!$meta_max_n || !$ann_n) {
- if ($count) {
- return 0;
- } else {
- return false;
- }
- }
- $joins[] = "LEFT JOIN {$dbprefix}metadata m4 ON (e.guid = m4.entity_guid AND m4.name_id=$meta_max_n) ";
- $joins[] = "LEFT JOIN {$dbprefix}metastrings ms4 ON (m4.value_id = ms4.id) ";
- $wheres[] = "((ms4.string is null) OR (ms4.string = \"\") OR (CONVERT(ms4.string,SIGNED) > (SELECT count(id) FROM {$dbprefix}annotations ann WHERE ann.entity_guid = e.guid AND name_id = $ann_n GROUP BY entity_guid)))";
- // sanity check
- if ($start_date <= $end_date) {
- $options = array(
- 'type' => 'object',
- 'subtype' => 'event_calendar',
- 'limit' => 0,
- 'metadata_name_value_pairs' => array(
- array(
- 'name' => 'start_date',
- 'value' => $end_date,
- 'operand' => '<='
- ),
- array(
- 'name' => 'repeats',
- 'value' => 'yes'
- ),
- ),
- 'joins' => $joins,
- 'wheres' => $wheres,
-
- );
- if ($container_guid) {
- if (is_array($container_guid)) {
- $options['container_guids'] = $container_guid;
- } else {
- $options['container_guid'] = $container_guid;
- }
- }
-
- if ($region && $region != '-') {
- $options['metadata_name_value_pairs'][] = array(
- 'name' => 'region',
- 'value' => $region
- );
- }
-
- $events = elgg_get_entities_from_metadata($options);
- }
- return event_calendar_get_repeating_event_structure($events, $start_date, $end_date);
- }
- function event_calendar_get_events_for_user_between($start_date,$end_date,$is_count,$limit=10,$offset=0,$user_guid,$container_guid=0,$region='-') {
- if ($is_count) {
- // old way
- $count = event_calendar_get_entities_from_metadata_between('start_date','end_date',
- $start_date, $end_date, "object", "event_calendar", $user_guid, $container_guid, $limit,$offset,"",0,true,true,$region);
- return $count;
- } else {
- $events = event_calendar_get_entities_from_metadata_between('start_date','end_date',
- $start_date, $end_date, "object", "event_calendar", $user_guid, $container_guid, $limit,$offset,"",0,true,false,$region);
- //return event_calendar_vsort($events,'start_date');
- return $events;
- }
- }
- function event_calendar_get_events_for_user_between2($start_date,$end_date,$is_count,$limit=10,$offset=0,$user_guid,$container_guid=0,$region='-') {
- $options_new_way = array(
- 'type' => 'object',
- 'subtype' => 'event_calendar',
- 'relationship' => 'personal_event',
- 'relationship_guid' => $user_guid,
- 'metadata_name_value_pairs' => array( array( 'name' => 'start_date',
- 'value' => $start_date,
- 'operand' => '>='),
- array( 'name' => 'real_end_time',
- 'value' => $end_date,
- 'operand' => '<=')
- ),
- );
- if ($container_guid) {
- $options_new_way['container_guid'] = $container_guid;
- }
- if ($region && $region != '-') {
- $options_new_way['metadata_name_value_pairs'][] = array('name'=>'region','value'=>sanitize_string($region));
- }
- if ($is_count) {
- // old way
- $count_old_way = event_calendar_get_entities_from_metadata_between('start_date','real_end_time',
- $start_date, $end_date, "object", "event_calendar", $user_guid, $container_guid, $limit,$offset,"",0,true,true,$region);
- // new way
- $options_new_way['count'] = TRUE;
- $count_new_way = elgg_get_entities_from_relationship($options_new_way);
- return $count_old_way+$count_new_way;
- } else {
- $events_old_way = event_calendar_get_entities_from_metadata_between('start_date','real_end_time',
- $start_date, $end_date, "object", "event_calendar", $user_guid, $container_guid, $limit,$offset,"",0,true,false,$region);
- $options_new_way['limit'] = $limit;
- $options_new_way['offset'] = $offset;
- $options_new_way['order_by_metadata'] = array(array('name'=>'start_date','direction'=>'ASC','as'=>'integer'));
- //print_r($options_new_way);
- $events_new_way = elgg_get_entities_from_relationship($options_new_way);
- //return event_calendar_vsort($events,'start_date');
- $repeating_events = event_calendar_get_repeating_events_for_user_between($user_guid,$start_date,$end_date,$container_guid,$region);
- $all_events = event_calendar_merge_repeating_events(array_merge($events_old_way,$events_new_way), $repeating_events);
- return $all_events;
- }
- }
- function event_calendar_get_repeating_events_for_user_between($user_guid,$start_date,$end_date,$container_guid,$region) {
- $options = array(
- 'type' => 'object',
- 'subtype' => 'event_calendar',
- 'relationship' => 'personal_event',
- 'relationship_guid' => $user_guid,
- 'metadata_name_value_pairs' => array(
- array(
- 'name' => 'start_date',
- 'value' => $end_date,
- 'operand' => '<='
- ),
- array(
- 'name' => 'repeats',
- 'value' => 'yes'
- ),
- )
- );
-
- if ($container_guid) {
- if (is_array($container_guid)) {
- $options['container_guids'] = $container_guid;
- } else {
- $options['container_guid'] = $container_guid;
- }
- }
-
- if ($region && $region != '-') {
- $options['metadata_name_value_pairs'][] = array(
- 'name' => 'region',
- 'value' => $region
- );
- }
-
- $events = elgg_get_entities_from_relationship($options);
- return event_calendar_get_repeating_event_structure($events, $start_date, $end_date);
- }
- function event_calendar_get_repeating_events_for_friends_between($user_guid,$friend_list,$start_date,$end_date,$container_guid=0,$region='-') {
- $db_prefix = elgg_get_config('dbprefix');
- $options = array(
- 'type' => 'object',
- 'subtype' => 'event_calendar',
- 'metadata_name_value_pairs' => array(
- array( 'name' => 'start_date',
- 'value' => $end_date,
- 'operand' => '<='
- ),
- array( 'name' => 'repeats',
- 'value' => 'yes'
- )
- ),
- 'joins' => array("JOIN {$db_prefix}entity_relationships r ON (r.guid_two = e.guid)"),
- 'wheres' => array("r.relationship = 'personal_event'","r.guid_one IN ($friend_list)"),
- );
-
- if ($container_guid) {
- if (is_array($container_guid)) {
- $options['container_guids'] = $container_guid;
- } else {
- $options['container_guid'] = $container_guid;
- }
- }
- if ($region && $region != '-') {
- $options['metadata_name_value_pairs'][] = array('name'=>'region','value'=>sanitize_string($region));
- }
-
- $events = elgg_get_entities_from_relationship($options);
- return event_calendar_get_repeating_event_structure($events, $start_date, $end_date);
- }
- function event_calendar_get_events_for_friends_between($start_date,$end_date,$is_count,$limit=10,$offset=0,$user_guid,$container_guid=0,$region='-') {
- if ($user_guid) {
- $friends = get_user_friends($user_guid,"",5000);
- if ($friends) {
- $friend_guids = array();
- foreach($friends as $friend) {
- $friend_guids[] = $friend->getGUID();
- }
- $friend_list = implode(",",$friend_guids);
- // elgg_get_entities_from_relationship does not take multiple relationship guids, so need some custom joins and wheres
- $db_prefix = elgg_get_config('dbprefix');
- $options_new_way = array(
- 'type' => 'object',
- 'subtype' => 'event_calendar',
- 'metadata_name_value_pairs' => array(array( 'name' => 'start_date',
- 'value' => $start_date,
- 'operand' => '>='),
- array( 'name' => 'real_end_time',
- 'value' => $end_date,
- 'operand' => '<=')
- ),
- 'joins' => array("JOIN {$db_prefix}entity_relationships r ON (r.guid_two = e.guid)"),
- 'wheres' => array("r.relationship = 'personal_event'","r.guid_one IN ($friend_list)"),
- );
-
- if ($container_guid) {
- $options_new_way['container_guid'] = $container_guid;
- }
- if ($region && $region != '-') {
- $options_new_way['metadata_name_value_pairs'][] = array('name'=>'region','value'=>sanitize_string($region));
- }
- if ($is_count) {
- $count_old_way = event_calendar_get_entities_from_metadata_between('start_date','end_date',
- $start_date, $end_date, "object", "event_calendar", $friend_guids, $container_guid, $limit,$offset,"",0,true,true,$region);
- $options_new_way['count'] = TRUE;
- $count_new_way = elgg_get_entities_from_metadata($options_new_way);
- return $count_old_way + $count_new_way;
- } else {
- $events_old_way = event_calendar_get_entities_from_metadata_between('start_date','end_date',
- $start_date, $end_date, "object", "event_calendar", $friend_guids, $container_guid, $limit,$offset,"",0,true,false,$region);
- //return event_calendar_vsort($events,'start_date');
- $options_new_way['limit'] = $limit;
- $options_new_way['offset'] = $offset;
- $options_new_way['order_by_metadata'] = array(array('name'=>'start_date','direction'=>'ASC','as'=>'integer'));
- //print_r($options_new_way);
- $events_new_way = elgg_get_entities_from_metadata($options_new_way);
- $repeating_events = event_calendar_get_repeating_events_for_friends_between($user_guid,$friend_list,$start_date,$end_date,$container_guid,$region);
- $all_events = event_calendar_merge_repeating_events(array_merge($events_old_way,$events_new_way), $repeating_events);
- return $all_events;
- }
- }
- }
- return array();
- }
- function event_calendar_vsort($original,$field, $descending = false) {
- if (!$original) {
- return $original;
- }
- $sortArr = array();
- foreach ( $original as $key => $item ) {
- $sortArr[ $key ] = $item->$field;
- }
- if ( $descending ) {
- arsort( $sortArr );
- } else {
- asort( $sortArr );
- }
- $resultArr = array();
- foreach ( $sortArr as $key => $value ) {
- $resultArr[ $key ] = $original[ $key ];
- }
- return $resultArr;
- }
- // TODO - replace with Elgg API if possible
- /**
- * Return a list of entities based on the given search criteria.
- * In this case, returns entities with the given metadata between two values inclusive
- *
- * @param mixed $meta_start_name
- * @param mixed $meta_end_name
- * @param mixed $meta_start_value - start of metadata range, must be numerical value
- * @param mixed $meta_end_value - end of metadata range, must be numerical value
- * @param string $entity_type The type of entity to look for, eg 'site' or 'object'
- * @param string $entity_subtype The subtype of the entity.
- * @param mixed $owner_guid Either one integer user guid or an array of user guids
- * @param int $container_guid If supplied, the result is restricted to events associated with a specific container
- * @param int $limit
- * @param int $offset
- * @param string $order_by Optional ordering.
- * @param int $site_guid The site to get entities for. Leave as 0 (default) for the current site; -1 for all sites.
- * @param boolean $filter Filter by events in personal calendar if true
- * @param true|false $count If set to true, returns the total number of entities rather than a list. (Default: false)
- *
- * @return int|array A list of entities, or a count if $count is set to true
- */
- function event_calendar_get_entities_from_metadata_between($meta_start_name, $meta_end_name, $meta_start_value, $meta_end_value, $entity_type = "", $entity_subtype = "", $owner_guid = 0, $container_guid = 0, $limit = 10, $offset = 0, $order_by = "", $site_guid = 0, $filter = false, $count = false, $region='-')
- {
- global $CONFIG;
- // This should not be possible, but a sanity check just in case
- if (!is_numeric($meta_start_value) || !is_numeric($meta_end_value)) {
- return FALSE;
- }
- $meta_start_n = get_metastring_id($meta_start_name);
- $meta_end_n = get_metastring_id($meta_end_name);
- if ($region && $region != '-') {
- $region_n = get_metastring_id('region');
- $region_value_n = get_metastring_id($region);
- if (!$region_n || !$region_value_n) {
- if ($count) {
- return 0;
- } else {
- return FALSE;
- }
- }
- }
- $entity_type = sanitise_string($entity_type);
- $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
- $limit = (int)$limit;
- $offset = (int)$offset;
- //if ($order_by == "") $order_by = "e.time_created desc";
- if ($order_by == "") $order_by = "v.string asc";
- $order_by = sanitise_string($order_by);
- $site_guid = (int) $site_guid;
- if ((is_array($owner_guid) && (count($owner_guid)))) {
- foreach($owner_guid as $key => $guid) {
- $owner_guid[$key] = (int) $guid;
- }
- } else {
- $owner_guid = (int) $owner_guid;
- }
- if ((is_array($container_guid) && (count($container_guid)))) {
- foreach($container_guid as $key => $guid) {
- $container_guid[$key] = (int) $guid;
- }
- } else {
- $container_guid = (int) $container_guid;
- }
- if ($site_guid == 0)
- $site_guid = $CONFIG->site_guid;
- //$access = get_access_list();
- $where = array();
- if ($entity_type!="")
- $where[] = "e.type='$entity_type'";
- if ($entity_subtype)
- $where[] = "e.subtype=$entity_subtype";
- $where[] = "m.name_id='$meta_start_n'";
- $where[] = "m2.name_id='$meta_end_n'";
- $where[] = "((v.string >= $meta_start_value AND v.string <= $meta_end_value) OR ( v2.string >= $meta_start_value AND v2.string <= $meta_end_value) OR (v.string <= $meta_start_value AND v2.string >= $meta_start_value) OR ( v2.string <= $meta_end_value AND v2.string >= $meta_end_value))";
- if ($region && $region != '-') {
- $where[] = "m3.name_id='$region_n'";
- $where[] = "m3.value_id='$region_value_n'";
- }
- if ($site_guid > 0)
- $where[] = "e.site_guid = {$site_guid}";
- if ($filter) {
- if (is_array($owner_guid)) {
- $where[] = "ms2.string in (".implode(",",$owner_guid).")";
- } else if ($owner_guid > 0) {
- $where[] = "ms2.string = {$owner_guid}";
- }
-
- $where[] = "ms.string = 'personal_event'";
- } else {
- if (is_array($owner_guid)) {
- $where[] = "e.owner_guid in (".implode(",",$owner_guid).")";
- } else if ($owner_guid > 0) {
- $where[] = "e.owner_guid = {$owner_guid}";
- }
- }
- if (is_array($container_guid)) {
- $where[] = "e.container_guid in (".implode(",",$container_guid).")";
- } else if ($container_guid > 0)
- $where[] = "e.container_guid = {$container_guid}";
- if (!$count) {
- $query = "SELECT distinct e.* ";
- } else {
- $query = "SELECT count(distinct e.guid) as total ";
- }
- $query .= "from {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}metadata m on e.guid = m.entity_guid JOIN {$CONFIG->dbprefix}metadata m2 on e.guid = m2.entity_guid ";
- if ($filter) {
- $query .= "JOIN {$CONFIG->dbprefix}annotations a ON (a.entity_guid = e.guid) ";
- $query .= "JOIN {$CONFIG->dbprefix}metastrings ms ON (a.name_id = ms.id) ";
- $query .= "JOIN {$CONFIG->dbprefix}metastrings ms2 ON (a.value_id = ms2.id) ";
- }
- if ($region && $region != '-') {
- $query .= "JOIN {$CONFIG->dbprefix}metadata m3 ON (e.guid = m3.entity_guid) ";
- }
- $query .= "JOIN {$CONFIG->dbprefix}metastrings v on v.id = m.value_id JOIN {$CONFIG->dbprefix}metastrings v2 on v2.id = m2.value_id where";
- foreach ($where as $w)
- $query .= " $w and ";
- $query .= get_access_sql_suffix("e"); // Add access controls
- $query .= ' and ' . get_access_sql_suffix("m"); // Add access controls
- $query .= ' and ' . get_access_sql_suffix("m2"); // Add access controls
- if (!$count) {
- $query .= " order by $order_by";
- if ($limit) {
- $query .= " limit $offset, $limit"; // Add order and limit
- }
- $entities = get_data($query, "entity_row_to_elggstar");
- if (elgg_get_plugin_setting('add_to_group_calendar', 'event_calendar') == 'yes') {
- if (get_entity($container_guid) instanceOf ElggGroup) {
- $entities = event_calendar_get_entities_from_metadata_between_related($meta_start_name, $meta_end_name,
- $meta_start_value, $meta_end_value, $entity_type,
- $entity_subtype, $owner_guid, $container_guid,
- 0, 0, "", 0,
- false, false, '-',$entities);
- }
- }
- return $entities;
- } else {
- if ($row = get_data_row($query))
- return $row->total;
- }
- return false;
- }
- // adds any related events (has the display_on_group relation)
- // that meet the appropriate criteria
- function event_calendar_get_entities_from_metadata_between_related($meta_start_name, $meta_end_name,
- $meta_start_value, $meta_end_value, $entity_type = "",
- $entity_subtype = "", $owner_guid = 0, $container_guid = 0,
- $limit = 10, $offset = 0, $order_by = "", $site_guid = 0,
- $filter = false, $count = false, $region='-',$main_events) {
- $main_list = array();
- if ($main_events) {
- foreach ($main_events as $event) {
- $main_list[$event->guid] = $event;
- }
- }
- $related_list = array();
- $related_events = elgg_get_entities_from_relationship(array(
- 'relationship' => 'display_on_group',
- 'relationship_guid' => $container_guid,
- 'inverse_relationship' => TRUE,
- ));
- if ($related_events) {
- foreach ($related_events as $event) {
- $related_list[$event->guid] = $event;
- }
- }
- // get all the events (across all containers) that meet the criteria
- $all_events = event_calendar_get_entities_from_metadata_between($meta_start_name, $meta_end_name,
- $meta_start_value, $meta_end_value, $entity_type, $entity_subtype, $owner_guid,
- 0, $limit, $offset, $order_by, $site_guid, $filter, $count, $region);
- if ($all_events) {
- foreach($all_events as $event) {
- if (array_key_exists($event->guid,$related_list)
- && !array_key_exists($event->guid,$main_list)) {
- // add to main events
- $main_events[] = $event;
- }
- }
- }
- return event_calendar_vsort($main_events,$meta_start_name);
- }
- // TODO: try to replace this with new Elgg 1.7 API
- /**
- * Return a list of entities based on the given search criteria.
- * In this case, returns entities with the given metadata between two values inclusive
- *
- * @param mixed $meta_start_name
- * @param mixed $meta_end_name
- * @param mixed $meta_start_value - start of metadata range, must be numerical value
- * @param mixed $meta_end_value - end of metadata range, must be numerical value
- * @param string $entity_type The type of entity to look for, eg 'site' or 'object'
- * @param string $entity_subtype The subtype of the entity.
- * @param mixed $owner_guid Either one integer user guid or an array of user guids
- * @param int $container_guid If supplied, the result is restricted to events associated with a specific container
- * @param int $limit
- * @param int $offset
- * @param string $order_by Optional ordering.
- * @param int $site_guid The site to get entities for. Leave as 0 (default) for the current site; -1 for all sites.
- * @param boolean $filter Filter by events in personal calendar if true
- * @param true|false $count If set to true, returns the total number of entities rather than a list. (Default: false)
- * @param string $meta_max metadata name containing maximum annotation count
- * @param string $annotation_name annotation name to count
- *
- * @return int|array A list of entities, or a count if $count is set to true
- *
- * TODO: see if the new API is robust enough to avoid this custom query
- */
- function event_calendar_get_entities_from_metadata_between2
- ($meta_start_name, $meta_end_name, $meta_start_value, $meta_end_value,
- $entity_type = "", $entity_subtype = "", $owner_guid = 0, $container_guid = 0,
- $limit = 10, $offset = 0, $order_by = "", $site_guid = 0, $filter = false,
- $count = false, $region='-', $meta_max = '', $annotation_name = '')
- {
- global $CONFIG;
- // This should not be possible, but a sanity check just in case
- if (!is_numeric($meta_start_value) || !is_numeric($meta_end_value)) {
- return FALSE;
- }
- $meta_start_n = get_metastring_id($meta_start_name);
- $meta_end_n = get_metastring_id($meta_end_name);
- if ($region && $region != '-') {
- $region_n = get_metastring_id('region');
- $region_value_n = get_metastring_id($region);
- if (!$region_n || !$region_value_n) {
- if ($count) {
- return 0;
- } else {
- return false;
- }
- }
- }
- $entity_type = sanitise_string($entity_type);
- $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
- $limit = (int)$limit;
- $offset = (int)$offset;
- //if ($order_by == "") $order_by = "e.time_created desc";
- if ($order_by == "") $order_by = "v.string asc";
- $order_by = sanitise_string($order_by);
- $site_guid = (int) $site_guid;
- if ((is_array($owner_guid) && (count($owner_guid)))) {
- foreach($owner_guid as $key => $guid) {
- $owner_guid[$key] = (int) $guid;
- }
- } else {
- $owner_guid = (int) $owner_guid;
- }
- if ((is_array($container_guid) && (count($container_guid)))) {
- foreach($container_guid as $key => $guid) {
- $container_guid[$key] = (int) $guid;
- }
- } else {
- $container_guid = (int) $container_guid;
- }
- if ($site_guid == 0)
- $site_guid = $CONFIG->site_guid;
- //$access = get_access_list();
- $where = array();
- if ($entity_type!="")
- $where[] = "e.type='$entity_type'";
- if ($entity_subtype)
- $where[] = "e.subtype=$entity_subtype";
- $where[] = "m.name_id='$meta_start_n'";
- $where[] = "m2.name_id='$meta_end_n'";
- $where[] = "((v.string >= $meta_start_value AND v.string <= $meta_end_value) OR ( v2.string >= $meta_start_value AND v2.string <= $meta_end_value) OR (v.string <= $meta_start_value AND v2.string >= $meta_start_value) OR ( v2.string <= $meta_end_value AND v2.string >= $meta_end_value))";
- if ($region && $region != '-') {
- $where[] = "m3.name_id='$region_n'";
- $where[] = "m3.value_id='$region_value_n'";
- }
- if ($site_guid > 0)
- $where[] = "e.site_guid = {$site_guid}";
- if ($filter) {
- if (is_array($owner_guid)) {
- $where[] = "ms2.string in (".implode(",",$owner_guid).")";
- } else if ($owner_guid > 0) {
- $where[] = "ms2.string = {$owner_guid}";
- }
-
- $where[] = "ms.string = 'personal_event'";
- } else {
- if (is_array($owner_guid)) {
- $where[] = "e.owner_guid in (".implode(",",$owner_guid).")";
- } else if ($owner_guid > 0) {
- $where[] = "e.owner_guid = {$owner_guid}";
- }
- }
- if (is_array($container_guid)) {
- $where[] = "e.container_guid in (".implode(",",$container_guid).")";
- } else if ($container_guid > 0)
- $where[] = "e.container_guid = {$container_guid}";
- if (!$count) {
- $query = "SELECT distinct e.* ";
- } else {
- $query = "SELECT count(distinct e.guid) as total ";
- }
- $query .= "FROM {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}metadata m on e.guid = m.entity_guid JOIN {$CONFIG->dbprefix}metadata m2 on e.guid = m2.entity_guid ";
- if ($filter) {
- $query .= "JOIN {$CONFIG->dbprefix}annotations a ON (a.entity_guid = e.guid) ";
- $query .= "JOIN {$CONFIG->dbprefix}metastrings ms ON (a.name_id = ms.id) ";
- $query .= "JOIN {$CONFIG->dbprefix}metastrings ms2 ON (a.value_id = ms2.id) ";
- }
- if ($region && $region != '-') {
- $query .= "JOIN {$CONFIG->dbprefix}metadata m3 ON (e.guid = m3.entity_guid) ";
- }
- if ($meta_max && $annotation_name) {
- // This groups events for which the meta max name is defined
- // perhaps this should be a left join and accept null values?
- // so it would return groups with no spots defined as well
- $meta_max_n = get_metastring_id($meta_max);
- $ann_n = get_metastring_id($annotation_name);
- if (!$meta_max_n || !$ann_n) {
- if ($count) {
- return 0;
- } else {
- return false;
- }
- }
- $query .= " LEFT JOIN {$CONFIG->dbprefix}metadata m4 ON (e.guid = m4.entity_guid AND m4.name_id=$meta_max_n) ";
- $query .= " LEFT JOIN {$CONFIG->dbprefix}metastrings ms4 ON (m4.value_id = ms4.id) ";
- $where[] = "((ms4.string is null) OR (ms4.string = \"\") OR (CONVERT(ms4.string,SIGNED) > (SELECT count(id) FROM {$CONFIG->dbprefix}annotations ann WHERE ann.entity_guid = e.guid AND name_id = $ann_n GROUP BY entity_guid)))";
- }
- $query .= "JOIN {$CONFIG->dbprefix}metastrings v on v.id = m.value_id JOIN {$CONFIG->dbprefix}metastrings v2 on v2.id = m2.value_id where";
- foreach ($where as $w)
- $query .= " $w AND ";
- $query .= get_access_sql_suffix("e"); // Add access controls
- $query .= ' AND ' . get_access_sql_suffix("m"); // Add access controls
- $query .= ' AND ' . get_access_sql_suffix("m2"); // Add access controls
- if (!$count) {
- $query .= " order by $order_by";
- if ($limit) {
- $query .= " limit $offset, $limit"; // Add order and limit
- }
- $entities = get_data($query, "entity_row_to_elggstar");
- if (elgg_get_plugin_setting('add_to_group_calendar', 'event_calendar') == 'yes') {
- if (get_entity($container_guid) instanceOf ElggGroup) {
- $entities = event_calendar_get_entities_from_metadata_between_related($meta_start_name, $meta_end_name,
- $meta_start_value, $meta_end_value, $entity_type,
- $entity_subtype, $owner_guid, $container_guid,
- 0, 0, "", 0,
- false, false, '-',$entities);
- }
- }
- return $entities;
- } else {
- if ($row = get_data_row($query))
- return $row->total;
- }
- return false;
- }
- function event_calendar_has_personal_event($event_guid,$user_guid) {
- // check legacy implementation and new one
- if (check_entity_relationship($user_guid,'personal_event',$event_guid)) {
- return TRUE;
- } else {
- // use old method for now
- $options = array('guid'=>$event_guid,'annotation_name' => 'personal_event', 'annotation_value' => $user_guid,'count'=>TRUE);
- //$annotations = get_annotations($event_guid, "object", "event_calendar", "personal_event", (int) $user_guid, $user_guid);
- if (elgg_get_annotations($options)) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- }
- function event_calendar_add_personal_event($event_guid,$user_guid) {
- if ($event_guid && $user_guid) {
- if (!event_calendar_has_personal_event($event_guid,$user_guid)
- && !event_calendar_has_collision($event_guid,$user_guid)) {
- if (!event_calendar_is_full($event_guid)) {
- add_entity_relationship($user_guid,'personal_event',$event_guid);
- return TRUE;
- }
- }
- }
- return FALSE;
- }
- function event_calendar_add_personal_events_from_group($event_guid,$group_guid) {
- $members = get_group_members($group_guid, 100000);
- foreach($members as $member) {
- $member_id = $member->getGUID();
- event_calendar_add_personal_event($event_guid,$member_id);
- }
- }
- function event_calendar_remove_personal_event($event_guid,$user_guid) {
- remove_entity_relationship($user_guid,'personal_event',$event_guid);
- // also use old method for now
- $annotations = get_annotations($event_guid, "object", "event_calendar", "personal_event", (int) $user_guid, $user_guid);
- if ($annotations) {
- foreach ($annotations as $annotation) {
- $annotation->delete();
- }
- }
- }
- function event_calendar_get_personal_events_for_user($user_guid,$limit) {
- $events_old_way = elgg_get_entities_from_annotations(array(
- 'type' => 'object',
- 'subtype' => 'event_calendar',
- 'annotation_names' => 'personal_event',
- 'annotation_value' => $user_guid,
- 'limit' => 0,
- ));
- $events_new_way = elgg_get_entities_from_relationship(array(
- 'type' => 'object',
- 'subtype' => 'event_calendar',
- 'relationship' => 'personal_event',
- 'relationship_guid' => $user_guid,
- 'limit' => 0,
- ));
- $events = array_merge($events_old_way,$events_new_way);
- $final_events = array();
- if ($events) {
- $now = time();
- $one_day = 60*60*24;
- // don't show events that have been over for more than a day
- foreach($events as $event) {
- if (($event->start_date > $now-$one_day) || ($event->end_date && ($event->end_date > $now-$one_day))) {
- $final_events[] = $event;
- }
- }
- }
- $sorted = event_calendar_vsort($final_events,'start_date');
- return array_slice($sorted,0,$limit);
- }
- // the old way used annotations, and the new Elgg 1.8 way uses relationships
- // for now this version attempts to bridge the gap by using both methods for older sites
- function event_calendar_get_users_for_event($event_guid,$limit,$offset=0,$is_count=FALSE) {
- $options = array(
- 'type' => 'user',
- 'relationship' => 'personal_event',
- 'relationship_guid' => $event_guid,
- 'inverse_relationship' => TRUE,
- 'limit' => 0,
- );
- if ($is_count) {
- //$count_old_way = count_annotations($event_guid, "object", "event_calendar", "personal_event");
- $count_old_way = elgg_get_annotations(array(
- 'guid'=>$event_guid,
- 'type'=>"object",
- 'subtype'=>"event_calendar",
- 'annotation_name' => "personal_event",
- 'count'=>TRUE)
- );
- $options ['count'] = TRUE;
- $count_new_way = elgg_get_entities_from_relationship($options);
- return $count_old_way + $count_new_way;
- } else {
- $users_old_way = array();
- //$annotations = get_annotations($event_id, "object", "event_calendar", "personal_event", "", 0, $limit, $offset);
- $annotations = elgg_get_annotations(array(
- 'guid'=>$event_guid,
- 'type'=>"object",
- 'subtype'=>"event_calendar",
- 'annotation_name' => "personal_event",
- 'limit' => $limit,
- 'offset' => $offset)
- );
- if ($annotations) {
- foreach($annotations as $annotation) {
- if (($user = get_entity($annotation->value)) && ($user instanceOf ElggUser)) {
- $users_old_way[] = $user;
- }
- }
- }
- $users_new_way = elgg_get_entities_from_relationship($options);
- return array_merge($users_old_way,$users_new_way);
- }
- }
- function event_calendar_security_fields() {
- $ts = time();
- $token = generate_action_token($ts);
- return "__elgg_token=$token&__elgg_ts=$ts";
- }
- function event_calendar_get_events_for_group($group_guid, $limit = 0) {
- $options = array(
- 'type' => 'object',
- 'subtype' => 'event_calendar',
- 'container_guid' => $group_guid,
- 'limit' => $limit,
- );
- return elgg_get_entities($options);
- }
- function event_calendar_convert_time($time) {
- $event_calendar_time_format = elgg_get_plugin_setting('timeformat','event_calendar');
- if ($event_calendar_time_format == '12') {
- $hour = floor($time/60);
- $minute = sprintf("%02d",$time-60*$hour);
- if ($hour < 12) {
- return "$hour:$minute am";
- } else {
- $hour -= 12;
- return "$hour:$minute pm";
- }
- } else {
- $hour = floor($time/60);
- $minute = sprintf("%02d",$time-60*$hour);
- return "$hour:$minute";
- }
- }
- function event_calendar_format_time($date,$time1,$time2='') {
- if (is_numeric($time1)) {
- $t = event_calendar_convert_time($time1);
- if (is_numeric($time2)) {
- $t .= " - ".event_calendar_convert_time($time2);
- }
- return "$t, $date";
- } else {
- return $date;
- }
- }
- function event_calender_get_gmt_from_server_time($server_time) {
- $gmtime = $server_time - (int)substr(date('O'),0,3)*60*60;
- }
- function event_calendar_activated_for_group($group) {
- $group_calendar = elgg_get_plugin_setting('group_calendar', 'event_calendar');
- $group_default = elgg_get_plugin_setting('group_default', 'event_calendar');
- if ($group && ($group_calendar != 'no')) {
- if ( ($group->event_calendar_enable == 'yes') || ((!$group->event_calendar_enable && (!$group_default || $group_default == 'yes')))) {
- return true;
- }
- }
- return false;
- }
- function event_calendar_get_region($event) {
- $event_calendar_region_list_handles = elgg_get_plugin_setting('region_list_handles', 'event_calendar');
- $region = trim($event->region);
- if ($event_calendar_region_list_handles == 'yes') {
- $region = elgg_echo('event_calendar:region:'.$region);
- }
- return htmlspecialchars($region);
- }
- function event_calendar_get_type($event) {
- $event_calendar_type_list_handles = elgg_get_plugin_setting('type_list_handles', 'event_calendar');
- $type = trim($event->event_type);
- if ($type) {
- if ($event_calendar_type_list_handles == 'yes') {
- $type = elgg_echo('event_calendar:type:'.$type);
- }
- return htmlspecialchars($type);
- } else {
- return $type;
- }
- }
- function event_calendar_get_formatted_full_items($event) {
- $time_bit = event_calendar_get_formatted_time($event);
- $event_calendar_region_display = elgg_get_plugin_setting('region_display', 'event_calendar');
- $event_calendar_type_display = elgg_get_plugin_setting('type_display', 'event_calendar');
- $event_items = array();
- if ($time_bit) {
- $item = new stdClass();
- $item->title = elgg_echo('event_calendar:when_label');
- $item->value = $time_bit;
- $event_items[] = $item;
- }
- $item = new stdClass();
- $item->title = elgg_echo('event_calendar:venue_label');
- $item->value = htmlspecialchars($event->venue);
- $event_items[] = $item;
- if ($event_calendar_region_display == 'yes') {
- $item = new stdClass();
- $item->title = elgg_echo('event_calendar:region_label');
- $item->value = event_calendar_get_region($event);
- $event_items[] = $item;
- }
- if ($event_calendar_type_display == 'yes') {
- $event_type = event_calendar_get_type($event);
- if ($event_type) {
- $item = new stdClass();
- $item->title = elgg_echo('event_calendar:type_label');
- $item->value = event_calendar_get_type($event);
- $event_items[] = $item;
- }
- }
- $item = new stdClass();
- $item->title = elgg_echo('event_calendar:fees_label');
- $item->value = htmlspecialchars($event->fees);
- $event_items[] = $item;
- $item = new stdClass();
- $item->title = elgg_echo('event_calendar:organiser_label');
- $item->value = htmlspecialchars($event->organiser);
- $event_items[] = $item;
- $item = new stdClass();
- $item->title = elgg_echo('event_calendar:contact_label');
- $item->value = htmlspecialchars($event->contact);
- $event_items[] = $item;
- return $event_items;
- }
- function event_calendar_get_formatted_time($event) {
- if (!$event->start_date) {
- return '';
- }
- $date_format = 'j M Y';
- $event_calendar_times = elgg_get_plugin_setting('times', 'event_calendar') != 'no';
- $start_date = date($date_format,$event->start_date);
- if ($event->end_date) {
- $end_date = date($date_format,$event->end_date);
- }
- if ((!$event->end_date) || ($end_date == $start_date)) {
- if (!$event->all_day && $event_calendar_times) {
- $start_date = event_calendar_format_time($start_date,$event->start_time,$event->end_time);
- }
- $time_bit = $start_date;
- } else {
- if (!$event->all_day && $event_calendar_times) {
- $start_date = event_calendar_format_time($start_date,$event->start_time);
- $end_date = event_calendar_format_time($end_date,$event->end_time);
- }
- $time_bit = "$start_date - $end_date";
- }
-
- if ($event->repeats == 'yes') {
- $dow = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');
- $r = array();
- foreach ($dow as $w) {
- $fn = 'event-calendar-repeating-'.$w.'-value';
- if ($event->$fn) {
- $r[] = elgg_echo('event_calendar:dow:full:'.$w);
- }
- }
- $week_bit = implode(", ",$r);
- if ($event->repeat_interval > 1) {
- $week_bit .= ' '.elgg_echo('event_calendar:repeated_event:week_interval',array($event->repeat_interval));
- } else {
- $week_bit .= ' '.elgg_echo('event_calendar:repeated_event:week_single');
- }
- $time_bit = elgg_echo('event_calendar:repeated_event:format',array($time_bit, $week_bit));
- }
- return $time_bit;
- }
- function event_calendar_get_formatted_date($ts) {
- // TODO: make the date format configurable
- return date('j/n/Y',$ts);
- }
- function event_calendar_is_full($event_id) {
- $event_calendar_spots_display = elgg_get_plugin_setting('spots_display', 'event_calendar');
- if ($event_calendar_spots_display == 'yes') {
- $count = event_calendar_get_users_for_event($event_id,0,0,TRUE);
- $event = get_entity($event_id);
- if ($event) {
- $spots = $event->spots;
- if (is_numeric($spots)) {
- if ($count >= $spots) {
- return TRUE;
- }
- }
- }
- }
- return FALSE;
- }
- function event_calendar_has_collision($event_id, $user_id) {
- $no_collisions = elgg_get_plugin_setting('no_collisions', 'event_calendar');
- if ($no_collisions == 'yes') {
- $event = get_entity($event_id);
- if ($event) {
- $start_time = $event->start_date;
- $end_time = event_calendar_get_end_time($event);
- // look to see if the user already has events within this period
- $count = event_calendar_get_events_for_user_between2($start_time,$end_time,TRUE,10,0,$user_id);
- if ($count > 0) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- }
- return FALSE;
- }
- // this complicated bit of code determines the event end time
- function event_calendar_get_end_time($event) {
- $default_length = elgg_get_plugin_setting('collision_length', 'event_calendar');
- $start_time = $event->start_date;
- $end_time = $event->end_time;
- $end_date = $event->end_date;
- if($end_date) {
- if ($end_time) {
- $end_time = $end_date+$end_time*60;
- } else if ($start_time == $end_date) {
- if (is_numeric($default_length)) {
- $end_time = $end_date + $default_length;
- } else {
- // default to an hour length
- $end_time = $start_time + 3600;
- }
- } else {
- $end_time = $end_date;
- }
- } else {
- if ($end_time) {
- if ($event->start_time) {
- $end_time = $start_time + ($end_time*60 - $event->start_time*60);
- } else {
- $end_time = $start_time + $end_time*60;
- }
- } else {
- if (is_numeric($default_length)) {
- $end_time = $start_time + $default_length;
- } else {
- // default to an hour length
- $end_time = $start_time + 3600;
- }
- }
- }
- return $end_time;
- }
- // a version to allow for some customised options
- function event_calendar_view_entity_list($entities, $count, $offset, $limit, $fullview = true, $viewtypetoggle = true, $pagination = true) {
- $count = (int) $count;
- $limit = (int) $limit;
- // do not require views to explicitly pass in the offset
- if (!$offset = (int) $offset) {
- $offset = sanitise_int(get_input('offset', 0));
- }
- $context = elgg_get_context();
- $html = elgg_view('event_calendar/entities/entity_list',array(
- 'entities' => $entities,
- 'count' => $count,
- 'offset' => $offset,
- 'limit' => $limit,
- 'baseurl' => $_SERVER['REQUEST_URI'],
- 'fullview' => $fullview,
- 'context' => $context,
- 'viewtypetoggle' => $viewtypetoggle,
- 'viewtype' => get_input('search_viewtype','list'),
- 'pagination' => $pagination
- ));
- return $html;
- }
- // returns open, closed or private for the given event and user
- function event_calendar_personal_can_manage($event,$user_id) {
- $status = 'private';
- $event_calendar_personal_manage = elgg_get_plugin_setting('personal_manage', 'event_calendar');
- if (!$event_calendar_personal_manage
- || $event_calendar_personal_manage == 'open'
- || $event_calendar_personal_manage == 'yes'
- || (($event_calendar_personal_manage == 'by_event' && (!$event->personal_manage || ($event->personal_manage == 'open'))))) {
- $status = 'open';
- } else {
- // in this case only admins or event owners can manage events on their personal calendars
- if(elgg_is_admin_logged_in()) {
- $status = 'open';
- } else if ($event && ($event->owner_guid == $user_id)) {
- $status = 'open';
- } else if (($event_calendar_personal_manage == 'closed')
- || ($event_calendar_personal_manage == 'no')
- || (($event_calendar_personal_manage == 'by_event') && ($event->personal_manage == 'closed'))) {
- $status = 'closed';
- }
- }
- return $status;
- }
- function event_calendar_send_event_request($event,$user_guid) {
- $result = FALSE;
- if(add_entity_relationship($user_guid, 'event_calendar_request', $event->guid)) {
- $subject = elgg_echo('event_calendar:request_subject');
- $name = get_entity($user_guid)->name;
- $title = $event->title;
- $url = $event->getUrl();
- $link = elgg_get_site_url().'event_calendar/review_requests/'.$event->guid;
- $message = sprintf(elgg_echo('event_calendar:request_message'),$name,$title,$url,$link);
- notify_user($event->owner_guid,elgg_get_site_entity()->guid,$subject,$message);
- $result = TRUE;
- }
- return $result;
- }
- // pages
- function event_calendar_get_page_content_list($page_type,$container_guid,$start_date,$display_mode,$filter,$region='-') {
- elgg_load_js('elgg.event_calendar');
- global $autofeed;
- $autofeed = true;
- if ($page_type == 'group') {
- if (!event_calendar_activated_for_group($container_guid)) {
- forward();
- }
- elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'));
- elgg_push_context('groups');
- elgg_set_page_owner_guid($container_guid);
- $user_guid = elgg_get_logged_in_user_guid();
- if(event_calendar_can_add($container_guid)) {
- elgg_register_menu_item('title', array(
- 'name' => 'add',
- 'href' => "event_calendar/add/".$container_guid,
- 'text' => elgg_echo('event_calendar:add'),
- 'class' => 'elgg-button elgg-button-action event-calendar-button-add',
- ));
- }
- } else {
- elgg_push_breadcrumb(elgg_echo('item:object:event_calendar'));
- $user_guid = elgg_get_logged_in_user_guid();
- if(event_calendar_can_add($container_guid)) {
- elgg_register_menu_item('title', array(
- 'name' => 'add',
- 'href' => "event_calendar/add",
- 'text' => elgg_echo('event_calendar:add'),
- 'class' => 'elgg-button elgg-button-action event-calendar-button-add',
- ));
- }
- }
- $params = event_calendar_generate_listing_params($page_type,$container_guid,$start_date,$display_mode,$filter,$region);
-
- $url = current_page_url();
- if (substr_count($url, '?')) {
- $url .= "&view=ical";
- } else {
- $url .= "?view=ical";
- }
-
- $url = elgg_format_url($url);
- $menu_options = array(
- 'name' => 'ical',
- 'id' => 'event-calendar-ical-link',
- 'text' => elgg_view_icon('calendar'),
- 'href' => $url,
- 'title' => elgg_echo('feed:ical'),
- 'priority' => 800,
- 'rel' => 'popup',
- );
- $menu_item = ElggMenuItem::factory($menu_options);
- elgg_register_menu_item('extras', $menu_item);
- $body = elgg_view_layout("content", $params);
- $body .= elgg_view_module('popup', elgg_echo('feed:ical'), elgg_echo('event_calendar:ical_popup_message') . elgg_view('output/url', array(
- 'href' => $url,
- 'text' => elgg_echo('export'),
- 'class' => 'elgg-button elgg-button-action',
- )), array(
- 'id' => 'event-calendar-ical',
- 'class' => 'event-calendar-ical hidden',
- ));
- return elgg_view_page($title, $body);
- }
- function event_calendar_get_page_content_edit($page_type,$guid,$start_date='') {
- elgg_load_js('elgg.event_calendar');
- $vars = array();
- $vars['id'] = 'event-calendar-edit';
- // just in case a feature adds an image upload
- $vars['enctype'] = 'multipart/form-data';
- $body_vars = array();
- if ($page_type == 'edit') {
- $title = elgg_echo('event_calendar:manage_event_title');
- $event = get_entity((int)$guid);
- if (elgg_instanceof($event, 'object', 'event_calendar') && $event->canEdit()) {
- $body_vars['event'] = $event;
- $body_vars['form_data'] = event_calendar_prepare_edit_form_vars($event,$page_type);
-
- $event_container = get_entity($event->container_guid);
- if (elgg_instanceof($event_container, 'group')) {
- elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$event->container_guid);
- $body_vars['group_guid'] = $event_container->guid;
- } else {
- elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
- $body_vars['group_guid'] = 0;
- }
- elgg_push_breadcrumb($event->title,$event->getURL());
- elgg_push_breadcrumb(elgg_echo('event_calendar:manage_event_title'));
- $content = elgg_view_form('event_calendar/edit', $vars,$body_vars);
- } else {
- $content = elgg_echo('event_calendar:error_event_edit');
- }
- } else {
- $title = elgg_echo('event_calendar:add_event_title');
-
- if ($guid) {
- // add to group
- $group = get_entity($guid);
- if (elgg_instanceof($group, 'group')) {
- $body_vars['group_guid'] = $guid;
- elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$guid);
- elgg_push_breadcrumb(elgg_echo('event_calendar:add_event_title'));
- $body_vars['form_data'] = event_calendar_prepare_edit_form_vars(NULL,$page_type,$start_date);
- $content = elgg_view_form('event_calendar/edit', $vars, $body_vars);
- } else {
- $content = elgg_echo('event_calendar:no_group');
- }
- } else {
- $body_vars['group_guid'] = 0;
- elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
- elgg_push_breadcrumb(elgg_echo('event_calendar:add_event_title'));
- $body_vars['form_data'] = event_calendar_prepare_edit_form_vars(NULL,$page_type,$start_date);
- $content = elgg_view_form('event_calendar/edit', $vars, $body_vars);
- }
- }
- $params = array('title' => $title, 'content' => $content,'filter' => '');
- $body = elgg_view_layout("content", $params);
- return elgg_view_page($title,$body);
- }
- /**
- * Pull together variables for the edit form
- *
- * @param ElggObject $event
- * @return array
- */
- function event_calendar_prepare_edit_form_vars($event = NULL, $page_type = '', $start_date = '') {
- // input names => defaults
- $now = time();
- $iso_date = date('Y-m-d',$now);
- $now_midnight = strtotime($iso_date);
- if ($start_date) {
- $start_date = strtotime($start_date);
- } else {
- $start_date = $now+60*60;
- }
- $start_time = floor(($now-$now_midnight)/60) + 60;
- $start_time = floor($start_time/5)*5;
- $values = array(
- 'title' => NULL,
- 'description' => NULL,
- 'venue' => NULL,
- 'start_date' => $start_date,
- 'end_date' => $start_date+60*60,
- 'start_time' => $start_time,
- 'end_time' => $start_time + 60,
- 'spots' => NULL,
- 'region' => '-',
- 'event_type' => '-',
- 'fees' => NULL,
- 'contact' => NULL,
- 'organiser' => NULL,
- 'tags' => NULL,
- 'send_reminder' => NULL,
- 'reminder_number' => 1,
- 'reminder_interval' => 60,
- 'repeats' => NULL,
- 'repeat_interval' => 1,
- 'event-calendar-repeating-monday-value' => 0,
- 'event-calendar-repeating-tuesday-value' => 0,
- 'event-calendar-repeating-wednesday-value' => 0,
- 'event-calendar-repeating-thursday-value' => 0,
- 'event-calendar-repeating-friday-value' => 0,
- 'event-calendar-repeating-saturday-value' => 0,
- 'event-calendar-repeating-sunday-value' => 0,
- 'personal_manage' => 'open',
- 'web_conference' => NULL,
- 'long_description' => NULL,
- 'access_id' => ACCESS_DEFAULT,
- 'group_guid' => NULL,
- );
-
- if ($page_type == 'schedule') {
- $values['schedule_type'] = 'poll';
- } else {
- $values['schedule_type'] = 'all_day';
- }
- if ($event) {
- foreach (array_keys($values) as $field) {
- if (isset($event->$field)) {
- $values[$field] = $event->$field;
- }
- }
- }
- if (elgg_is_sticky_form('event_calendar')) {
- $sticky_values = elgg_get_sticky_values('event_calendar');
- foreach ($sticky_values as $key => $value) {
- $values[$key] = $value;
- }
- }
- elgg_clear_sticky_form('event_calendar');
- return $values;
- }
- function event_calendar_generate_listing_params($page_type,$container_guid,$original_start_date,$display_mode,$filter,$region='-') {
- $event_calendar_listing_format = elgg_get_plugin_setting('listing_format', 'event_calendar');
- $event_calendar_spots_display = trim(elgg_get_plugin_setting('spots_display', 'event_calendar'));
- $event_calendar_first_date = trim(elgg_get_plugin_setting('first_date', 'event_calendar'));
- $event_calendar_last_date = trim(elgg_get_plugin_setting('last_date', 'event_calendar'));
- if (!$original_start_date) {
- $original_start_date = date('Y-m-d');
- }
- if ( $event_calendar_first_date && ($original_start_date < $event_calendar_first_date) ) {
- $original_start_date = $event_calendar_first_date;
- }
- if ( $event_calendar_last_date && ($original_start_date > $event_calendar_last_date) ) {
- $original_start_date = $event_calendar_first_date;
- }
- if ($event_calendar_listing_format == 'paged') {
- $start_ts = strtotime($original_start_date);
- $start_date = $original_start_date;
- if ($event_calendar_last_date) {
- $end_ts = strtotime($event_calendar_last_date);
- } else {
- // set to a large number
- $end_ts = 2000000000;
- }
- $mode = 'paged';
- } else {
- // the default interval is one month
- $day = 60*60*24;
- $week = 7*$day;
- $month = 31*$day;
- $mode = trim($display_mode);
- if (!$mode) {
- $mode = 'month';
- }
- if ($mode == "day") {
- $start_date = $original_start_date;
- $end_date = $start_date;
- $start_ts = strtotime($start_date);
- $end_ts = strtotime($end_date)+$day-1;
- } else if ($mode == "week") {
- // need to adjust start_date to be the beginning of the week
- $start_ts = strtotime($original_start_date);
- $start_ts -= date("w",$start_ts)*$day;
- $end_ts = $start_ts + 6*$day;
-
- $start_date = date('Y-m-d',$start_ts);
- $end_date = date('Y-m-d',$end_ts);
- } else {
- $start_ts = strtotime($original_start_date);
- $month = date('m',$start_ts);
- $year = date('Y',$start_ts);
- $start_date = $year.'-'.$month.'-1';
- $end_date = $year.'-'.$month.'-'.getLastDayOfMonth($month,$year);
- }
- if ($event_calendar_first_date && ($start_date < $event_calendar_first_date)) {
- $start_date = $event_calendar_first_date;
- }
- if ($event_calendar_last_date && ($end_date > $event_calendar_last_date)) {
- $end_date = $event_calendar_last_date;
- }
- $start_ts = strtotime($start_date);
- if ($mode == "day") {
- $end_ts = strtotime($end_date)+$day-1;
- $subtitle = elgg_echo('event_calendar:day_label').': '.date('j F Y',strtotime($start_date));
- } else if ($mode == "week") {
- // KJ - fix for end date bug
- //$end_ts = $start_ts + 6*$day;
- $end_ts = $start_ts + 7*$day;
- $subtitle = elgg_echo('event_calendar:week_label').': '.date('j F',$start_ts) . ' - '.date('j F Y',$end_ts);
- } else {
- // KJ - fix for end date bug
- //$end_ts = strtotime($end_date);
- $end_ts = strtotime($end_date)+24*60*60-1;
- $subtitle = date('F Y',$start_ts);
- }
- }
-
- $current_user_guid = elgg_get_logged_in_user_guid();
-
- $access_status = elgg_get_ignore_access();
-
- if ($page_type == 'owner') {
- $container = get_entity($container_guid);
- if (elgg_instanceof($container, 'user')) {
- $auth_token = get_input('auth_token');
- if ($auth_token) {
- $secret_key = event_calendar_get_secret_key();
- if ($secret_key && ($auth_token === sha1($container->username . $secret_key))) {
- elgg_set_ignore_access(TRUE);
- }
- }
- if ($container->canEdit()) {
- $user_guid = $container_guid;
- $group_guid = 0;
- } else {
- register_error('event_calendar:owner:permissions_error');
- forward();
- exit;
- }
- } else {
- register_error('event_calendar:owner:permissions_error');
- forward();
- exit;
- }
- } else {
- $user_guid = $current_user_guid;
- $group_guid = $container_guid;
- }
- $offset = get_input('offset');
- $limit = get_input('limit',15);
- if ($event_calendar_spots_display == 'yes') {
- if (!$filter) {
- $filter = 'open';
- }
- } else {
- if (!$filter) {
- $filter = 'all';
- }
- }
-
- if ($filter == 'all') {
- $count = event_calendar_get_events_between($start_ts,$end_ts,true,$limit,$offset,$container_guid,$region);
- $events = event_calendar_get_events_between($start_ts,$end_ts,false,$limit,$offset,$container_guid,$region);
- } else if ($filter == 'open') {
- $count = event_calendar_get_open_events_between($start_ts,$end_ts,true,$limit,$offset,$container_guid,$region);
- $events = event_calendar_get_open_events_between($start_ts,$end_ts,false,$limit,$offset,$container_guid,$region);
- } else if ($filter == 'friends') {
- $count = event_calendar_get_events_for_friends_between($start_ts,$end_ts,true,$limit,$offset,$user_guid,$container_guid,$region);
- $events = event_calendar_get_events_for_friends_between($start_ts,$end_ts,false,$limit,$offset,$user_guid,$container_guid,$region);
- } else if ($filter == 'mine') {
- $count = event_calendar_get_events_for_user_between2($start_ts,$end_ts,true,$limit,$offset,$user_guid,$container_guid,$region);
- $events = event_calendar_get_events_for_user_between2($start_ts,$end_ts,false,$limit,$offset,$user_guid,$container_guid,$region);
- }
- $vars = array(
- 'original_start_date' => $original_start_date,
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- 'first_date' => $event_calendar_first_date,
- 'last_date' => $event_calendar_last_date,
- 'mode' => $mode,
- 'events' => $events,
- 'count' => $count,
- 'offset' => $offset,
- 'limit' => $limit,
- 'group_guid' => $group_guid,
- 'filter' => $filter,
- 'region' => $region,
- 'listing_format' => $event_calendar_listing_format,
- );
- $content = elgg_view('event_calendar/show_events', $vars);
- if ($page_type == 'group') {
- $filter_override = '';
- } else {
- $filter_override = elgg_view('event_calendar/filter_menu',$vars);
- }
- if ($event_calendar_listing_format == 'paged') {
- $title = elgg_echo('event_calendar:upcoming_events_title');
- } else if ($event_calendar_listing_format == 'full') {
- $title = elgg_echo('event_calendar:show_events_title');
- } else if ($page_type == 'group') {
- $title = elgg_echo('event_calendar:group'). ' ('.$subtitle.')';
- } else {
- $title = elgg_echo('event_calendar:listing_title:'.$filter). ' ('.$subtitle.')';
- }
- $params = array('title' => $title, 'content' => $content, 'filter_override'=>$filter_override);
- elgg_set_ignore_access($access_status);
- return $params;
- }
- function event_calendar_get_page_content_view($event_guid,$light_box = FALSE) {
- // add personal calendar button and links
- elgg_push_context('event_calendar:view');
- $event = get_entity($event_guid);
- if (!elgg_instanceof($event, 'object', 'event_calendar')) {
- $content = elgg_echo('event_calendar:error_nosuchevent');
- $title = elgg_echo('event_calendar:generic_error_title');
- } else {
- $title = htmlspecialchars($event->title);
- $event_container = get_entity($event->container_guid);
- if (elgg_instanceof($event_container, 'group')) {
- if ($event_container->canEdit()) {
- event_calendar_handle_menu($event_guid);
- }
- elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$event->container_guid);
- } else {
- if ($event->canEdit()) {
- event_calendar_handle_menu($event_guid);
- }
- elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
- }
- elgg_push_breadcrumb($event->title);
- $content = elgg_view_entity($event, array('full_view' => true,'light_box'=>$light_box));
- //check to see if comment are on - TODO - add this feature to all events
- if ($event->comments_on != 'Off') {
- $content .= elgg_view_comments($event);
- }
- }
-
- if ($light_box) {
- return '<div class="event-calendar-lightbox">'.elgg_view_title($title).$content.'</div>';
- } else {
- $params = array('title' => $title, 'content' => $content,'filter' => '');
-
- $body = elgg_view_layout("content", $params);
-
- return elgg_view_page($title,$body);
- }
- }
- function event_calendar_get_page_content_display_users($event_guid) {
- elgg_load_js('elgg.event_calendar');
- $event = get_entity($event_guid);
- if (!elgg_instanceof($event, 'object', 'event_calendar')) {
- $content = elgg_echo('event_calendar:error_nosuchevent');
- $title = elgg_echo('event_calendar:generic_error_title');
- } else {
- event_calendar_handle_menu($event_guid);
- $title = elgg_echo('event_calendar:users_for_event_title',array(htmlspecialchars($event->title)));
- $event_container = get_entity($event->container_guid);
- if (elgg_instanceof($event_container, 'group')) {
- elgg_push_context('groups');
- elgg_set_page_owner_guid($event->container_guid);
- elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$event->container_guid);
- } else {
- elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
- }
- elgg_push_breadcrumb($event->title,$event->getURL());
- elgg_push_breadcrumb(elgg_echo('event_calendar:users_for_event_breadcrumb'));
- $limit = 12;
- $offset = get_input('offset', 0);
- $users = event_calendar_get_users_for_event($event_guid,$limit,$offset,false);
- $options = array(
- 'full_view' => FALSE,
- 'list_type_toggle' => FALSE,
- 'limit'=>$limit,
- 'event_calendar_event'=>$event,
- );
- elgg_extend_view('user/default','event_calendar/calendar_toggle');
- $content = elgg_view_entity_list($users,$options);
- }
- $params = array('title' => $title, 'content' => $content,'filter' => '');
- $body = elgg_view_layout("content", $params);
- return elgg_view_page($title,$body);
- }
- // display a list of all the members of the container of $event_guid and allowing
- // adding or removing them
- function event_calendar_get_page_content_manage_users($event_guid) {
- // TODO: make this an optional feature, toggled off
- elgg_load_js('elgg.event_calendar');
- $event = get_entity($event_guid);
- $limit = 10;
- $offset = get_input('offset', 0);
-
- $event_calendar_add_users = elgg_get_plugin_setting('add_users', 'event_calendar');
- if ($event_calendar_add_users != 'yes') {
- register_error(elgg_echo('event_calendar:feature_not_activated'));
- forward();
- exit;
- }
- if (!elgg_instanceof($event, 'object', 'event_calendar')) {
- $content = elgg_echo('event_calendar:error_nosuchevent');
- $title = elgg_echo('event_calendar:generic_error_title');
- } else {
- event_calendar_handle_menu($event_guid);
- $title = elgg_echo('event_calendar:manage_users:title',array($event->title));
- $event_container = get_entity($event->container_guid);
- if ($event_container->canEdit()) {
- if (elgg_instanceof($event_container, 'group')) {
- elgg_push_context('groups');
- elgg_set_page_owner_guid($event->container_guid);
- elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$event->container_guid);
- elgg_register_menu_item('title', array(
- 'name' => 'remove_from_group_members',
- 'href' => elgg_add_action_tokens_to_url('action/event_calendar/remove_from_group_members?event_guid='.$event_guid),
- 'text' => elgg_echo('event_calendar:remove_from_group_members:button'),
- 'class' => 'elgg-button elgg-button-action',
- ));
- elgg_register_menu_item('title', array(
- 'name' => 'add_to_group_members',
- 'href' => elgg_add_action_tokens_to_url('action/event_calendar/add_to_group_members?event_guid='.$event_guid),
- 'text' => elgg_echo('event_calendar:add_to_group_members:button'),
- 'class' => 'elgg-button elgg-button-action',
- ));
- $users = $event_container->getMembers($limit,$offset);
- $count = $event_container->getMembers($limit,$offset,TRUE);
- elgg_extend_view('user/default','event_calendar/calendar_toggle');
- $options = array(
- 'full_view' => FALSE,
- 'list_type_toggle' => FALSE,
- 'limit'=>$limit,
- 'event_calendar_event'=>$event,
- 'pagination' => TRUE,
- 'count'=>$count,
- );
- $content .= elgg_view_entity_list($users,$options,$offset,$limit);
- } else {
- elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
- $content = '<p>'.elgg_echo('event_calendar:manage_users:description').'</p>';
- $content .= elgg_view_form('event_calendar/manage_subscribers',array(),array('event'=>$event));
- }
- elgg_push_breadcrumb($event->title,$event->getURL());
- elgg_push_breadcrumb(elgg_echo('event_calendar:manage_users:breadcrumb'));
-
- } else {
- $content = elgg_echo('event_calendar:manage_users:unauthorized');
- }
- }
- $params = array('title' => $title, 'content' => $content,'filter' => '');
- $body = elgg_view_layout("content", $params);
- return elgg_view_page($title,$body);
- }
- function event_calendar_get_page_content_review_requests($event_guid) {
- $event = get_entity($event_guid);
- if (!elgg_instanceof($event, 'object', 'event_calendar')) {
- $content = elgg_echo('event_calendar:error_nosuchevent');
- $title = elgg_echo('event_calendar:generic_error_title');
- } else {
- event_calendar_handle_menu($event_guid);
- $title = elgg_echo('event_calendar:review_requests_title',array(htmlspecialchars($event->title)));
- $event_container = get_entity($event->container_guid);
- if (elgg_instanceof($event_container, 'group')) {
- elgg_push_context('groups');
- elgg_set_page_owner_guid($event->container_guid);
- elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$event->container_guid);
- } else {
- elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
- }
- elgg_push_breadcrumb($event->title,$event->getURL());
- elgg_push_breadcrumb(elgg_echo('event_calendar:review_requests_menu_title'));
- if ($event->canEdit()) {
- $requests = elgg_get_entities_from_relationship(
- array(
- 'relationship' => 'event_calendar_request',
- 'relationship_guid' => $event_guid,
- 'inverse_relationship' => TRUE,
- 'limit' => 0)
- );
- if ($requests) {
- $content = elgg_view('event_calendar/review_requests',array('requests' => $requests, 'entity' => $event));
- } else {
- $content = elgg_echo('event_calendar:review_requests_request_none');
- }
- } else {
- $content = elgg_echo('event_calendar:review_requests_error');
- }
- }
- $params = array('title' => $title, 'content' => $content,'filter' => '');
- $body = elgg_view_layout("content", $params);
- return elgg_view_page($title,$body);
- }
- function event_calendar_handle_menu($event_guid) {
- $event = get_entity($event_guid);
- $event_calendar_personal_manage = elgg_get_plugin_setting('personal_manage', 'event_calendar');
- if ((($event_calendar_personal_manage == 'by_event') && ($event->personal_manage == 'closed'))
- || (($event_calendar_personal_manage == 'closed') || ($event_calendar_personal_manage == 'no'))) {
- $url = "event_calendar/review_requests/$event_guid";
- $item = new ElggMenuItem('event-calendar-0review_requests', elgg_echo('event_calendar:review_requests_menu_title'), $url);
- $item->setSection('event_calendar');
- elgg_register_menu_item('page', $item);
- //elgg_register_menu_item(elgg_echo('event_calendar:review_requests_title'), $CONFIG->wwwroot . "pg/event_calendar/review_requests/".$event_id, '0eventcalendaradmin');
- }
- $event_calendar_add_users = elgg_get_plugin_setting('add_users', 'event_calendar');
- if ($event_calendar_add_users == 'yes') {
- $url = "event_calendar/manage_users/$event_guid";
- $item = new ElggMenuItem('event-calendar-1manage_users', elgg_echo('event_calendar:manage_users:breadcrumb'), $url);
- $item->setSection('event_calendar');
- elgg_register_menu_item('page', $item);
- }
- }
- function event_calendar_get_secret_key() {
- $key_file_name = elgg_get_plugin_setting('ical_auth_file_name','event_calendar');
- if ($key_file_name && file_exists($key_file_name)) {
- $key = (require($key_file_name));
-
- return $key['tokenSecretKey'];
- } else {
- return FALSE;
- }
- }
- function getLastDayOfMonth($month,$year) {
- return idate('d', mktime(0, 0, 0, ($month + 1), 0, $year));
- }
- function event_calendar_modify_full_calendar($event_guid,$day_delta,$minute_delta,$start_time,$resend,$minutes,$iso_date) {
- $event = get_entity($event_guid);
- if (elgg_instanceof($event,'object','event_calendar') && $event->canEdit()) {
- if ($event->is_event_poll) {
- if (elgg_is_active_plugin('event_poll')) {
- elgg_load_library('elgg:event_poll');
- return event_poll_change($event_guid,$day_delta,$minute_delta,$start_time,$resend,$minutes,$iso_date);
- } else {
- return FALSE;
- }
- } else {
- $event->start_date = strtotime("$day_delta days",$event->start_date)+60*$minute_delta;
- if ($event->end_date) {
- $event->end_date = strtotime("$day_delta days",$event->end_date);
- }
- $times = elgg_get_plugin_setting('times','event_calendar');
- //$inc = 24*60*60*$day_delta+60*$minute_delta;
-
- //$event->real_end_time += $inc;
- $event->real_end_time = strtotime("$day_delta days",$event->real_end_time)+60*$minute_delta;
- if ($times != 'no') {
- $event->start_time += $minute_delta;
- if ($event->end_time) {
- $event->end_time += $minute_delta;
- }
- }
- $dow = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');
- $week_repeats = array();
- $weekday_delta = (($day_delta % 7) + 7) % 7; // Imagine delta is -12: ((-12 % 7) + 7 % 7) = +2 (thursdays are saturdays)
- foreach ($dow as $i => $w) {
- $v = 'event-calendar-repeating-'.$w.'-value';
- $new_day = $dow[($i+$weekday_delta)%7];
- $week_repeats[$new_day] = $event->$v;
- }
- foreach ($week_repeats as $w => $value) {
- $v = 'event-calendar-repeating-'.$w.'-value';
- $event->$v = $value;
- }
- return TRUE;
- }
- }
- return FALSE;
- }
- function event_calendar_get_page_content_fullcalendar_events($start_date,$end_date,$filter='all',$container_guid=0,$region='-') {
- //print "$start_date - $end_date";
- $start_ts = strtotime($start_date);
- $end_ts = strtotime($end_date);
- if ($filter == 'all') {
- $events = event_calendar_get_events_between($start_ts,$end_ts,false,0,0,$container_guid,$region);
- } else if ($filter == 'open') {
- $events = event_calendar_get_open_events_between($start_ts,$end_ts,false,0,0,$container_guid,$region);
- } else if ($filter == 'friends') {
- $user_guid = elgg_get_logged_in_user_guid();
- $events = event_calendar_get_events_for_friends_between($start_ts,$end_ts,false,0,0,$user_guid,$container_guid,$region);
- } else if ($filter == 'mine') {
- $user_guid = elgg_get_logged_in_user_guid();
- $events = event_calendar_get_events_for_user_between2($start_ts,$end_ts,false,0,0,$user_guid,$container_guid,$region);
- }
- $event_array = array();
- $times_supported = elgg_get_plugin_setting('times','event_calendar') != 'no';
- $polls_supported = elgg_is_active_plugin('event_poll');
- foreach($events as $e) {
- $event = $e['event'];
- $event_data = $e['data'];
- $c = count($event_data);
- foreach($event_data as $ed) {
- $event_item = array(
- 'guid' => $event->guid,
- 'title' => $event->title,
- 'start' => date('c',$ed['start_time']),
- 'end' => date('c',$ed['end_time']),
- );
- if (!$times_supported || ($event->schedule_type == 'all_day')) {
- $event_item['allDay'] = TRUE;
- } else {
- $event_item['allDay'] = FALSE;
- }
-
- if ($polls_supported && isset($e['is_event_poll']) && $e['is_event_poll']) {
- $event_item['className'] = 'event-poll-class';
- $event_item['title'] .= ' '.elgg_echo('event_calendar:poll_suffix');
- $event_item['is_event_poll'] = TRUE;
- $event_item['url'] = elgg_get_site_url().'event_poll/vote/'.$event->guid;
- $event_item['minutes'] = $ed['minutes'];
- $event_item['iso_date'] = $ed['iso_date'];
- } else {
- $event_item['id'] = $event->guid;
- $event_item['is_event_poll'] = FALSE;
- $event_item['url'] = elgg_get_site_url().'event_calendar/view/'.$event->guid;
- }
-
- $event_array[] = $event_item;
- }
- }
-
- $json_events_string = json_encode($event_array);
- return $json_events_string;
- }
- // right now this does not return repeated events in sorted order, so repeated events only really work properly for the full calendar
- // TODO: find another solution for displaying repeated events
- function event_calendar_flatten_event_structure($events) {
- $flattened = array();
- $guids = array();
- foreach($events as $e) {
- $this_event = $e['event'];
- $guid = $this_event->guid;
- if (!in_array($guid,$guids)) {
- $guids[] = $guid;
- $flattened[] = $this_event;
- }
- }
- return $flattened;
- }
- function event_calendar_queue_reminders() {
- // game plan - get all events up to 60 days ahead
- // with no reminder sent
- // compute reminder period
- // if <= current time, set reminder_queued flag and queue the
- // notification message using the message_queue plugin
- if (elgg_plugin_exists('message_queue')) {
- $now = time();
- // oops - this does not work for repeated events
- // need extra stuff for that
- /*$options = array(
- 'type' => 'object',
- 'subtype' => 'event_calendar',
- 'metadata_name_value_pairs' => array(
- array('name' => 'reminder_queued', 'value' => 'no'),
- array('name' => 'send_reminder', 'value' => 1),
- array('name' => 'start_date', 'value' => $now + 60*24*60*60, 'operand' => '>='),
- ),
- 'limit' => 0,
- );
- $events = elgg_get_entities_from_metadata($options);
- */
- $event_list = event_calendar_get_events_between($now,$now + 60*24*60*60,FALSE,0);
- foreach($event_list as $es) {
- $e = $es['event'];
- if ($e->send_reminder) {
- $reminder_period = 60*$e->reminder_interval*$e->reminder_number;
- if ($e->repeats) {
- // repeated events require more complex handing
- foreach($es['data'] as $d) {
- // if event falls in the reminder period
- if ($d->start_time - $reminder_period >= $now) {
- // and the reminder has not already been queued
- if (!event_calendar_repeat_reminder_logged($e,$d->start_time)) {
- // set the reminder queued flag
- event_calendar_repeat_reminder_log($e,$d->start_time);
- // queue the reminder for sending
- event_calendar_queue_reminder($e);
- }
- break;
- }
- }
- } else {
- // if this is just a normal non-repeated event, then we just need to set a flag and queue the reminder
- if (($e->reminder_queued != 'yes') && ($e->start_date - $now <= $reminder_period)) {
- $e->reminder_queued = 'yes';
- event_calendar_queue_reminder($e);
- }
- }
- }
- }
- }
- }
- function event_calendar_repeat_reminder_log($e,$start) {
- // this simple log just uses annotations on the event
- // TODO - remove log entries for past events
- create_annotation($e->guid, 'repeat_reminder_log_item', $start, '',0,ACCESS_PUBLIC);
- }
- function event_calendar_repeat_reminder_logged($e,$start) {
- $options = array(
- 'guid' => $e->guid,
- 'annotation_name' => 'repeat_reminder_log_item',
- 'annotation_value' => $start,
- 'limit' => 1
- );
-
- if (elgg_get_annotations($options)) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- function event_calendar_queue_reminder($e) {
- elgg_load_library('elgg:message_queue');
- $subject = elgg_echo('event_calendar:reminder:subject',array($e->title));
- $time_string = event_calendar_get_formatted_time($e);
- $body = elgg_echo('event_calendar:reminder:body',array($e->title,$time_string,$e->getURL()));
- $m = message_queue_create_message($subject,$body);
- if ($m) {
- $users = event_calendar_get_users_for_event($e->guid,0);
- foreach($users as $u) {
- message_queue_add($m->guid,$u->guid);
- }
- message_queue_set_for_sending($m->guid);
- }
- }
- /*function event_calendar_create_bbb_conf($event) {
- $bbb_security_salt = elgg_get_plugin_setting('bbb_security_salt','event_calendar');
- $bbb_server_url = rtrim(elgg_get_plugin_setting('bbb_server_url','event_calendar'), '/') . '/';
- if ($bbb_security_salt) {
- $day_in_minutes = 60*24;
- $now = time();
- // fix duration bug
- # $duration = (int)(($event->real_end_time-$event->start_date)/60)+$day_in_minutes;
- $duration = (int)(($event->real_end_time-$now)/60)+$day_in_minutes;
- $title = urlencode($event->title);
- $params = "name=$title&meetingID={$event->guid}&duration=$duration";
- $checksum = sha1('create'.$params.$bbb_security_salt);
- $params .= "&checksum=$checksum";
-
- // create curl resource
- $ch = curl_init();
-
- // set url
- curl_setopt($ch, CURLOPT_URL, $bbb_server_url.'api/create?'.$params);
-
- //return the transfer as a string
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
- // $output contains the output string
- $output = curl_exec($ch);
-
- // close curl resource to free up system resources
- curl_close($ch);
-
- #error_log("BBB create request:");
- #error_log($bbb_server_url.'api/create?'.$params);
-
- #error_log("BBB create response:");
- #error_log($output);
-
- $xml = new SimpleXMLElement($output);
- if ($xml->returncode == 'SUCCESS') {
- $event->bbb_attendee_password = (string) $xml->attendeePW;
- $event->bbb_moderator_password = (string) $xml->moderatorPW;
- } else {
- register_error(elgg_echo('event_calendar:bbb_create_error',array($xml->message)));
- }
- } else {
- register_error(elgg_echo('event_calendar:bbb_settings_error'));
- }
- }*/
- // utility function for BBB api calls
- function event_calendar_bbb_api($api_function,$params=NULL) {
-
- $bbb_security_salt = elgg_get_plugin_setting('bbb_security_salt','event_calendar');
- $bbb_server_url = rtrim(elgg_get_plugin_setting('bbb_server_url','event_calendar'), '/') . '/';
- if ($bbb_security_salt) {
- if (isset($params) && is_array($params) && count($params) > 0) {
- $query = array();
- foreach($params as $k => $v) {
- $query[] = $k.'='.rawurlencode($v);
- }
- $qs = implode('&',$query);
- } else {
- $qs = '';
- }
- $checksum = sha1($api_function.$qs.$bbb_security_salt);
- if ($qs) {
- $qs .= "&checksum=$checksum";
- }
-
- // create curl resource
- $ch = curl_init();
-
- // set url
- curl_setopt($ch, CURLOPT_URL, $bbb_server_url.'api/'.$api_function.'?'.$qs);
-
- //return the transfer as a string
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
- // $output contains the output string
- $output = curl_exec($ch);
-
- // close curl resource to free up system resources
- curl_close($ch);
-
- error_log("BBB api call: ".$api_function);
- error_log(print_r($params,TRUE));
- error_log("BBB response: \n".$output);
- return $output;
- } else {
- return FALSE;
- }
- }
- function event_calendar_create_bbb_conf($event) {
- $day_in_minutes = 60*24;
- $now = time();
- // fix duration bug
- # $duration = (int)(($event->real_end_time-$event->start_date)/60)+$day_in_minutes;
- $duration = (int)(($event->real_end_time-$now)/60)+$day_in_minutes;
- if ($duration > 0) {
- $title = urlencode($event->title);
- $output = event_calendar_bbb_api('create',array('meetingID'=>$event->guid,'name'=>$title,'duration'=>$duration));
- if ($output) {
- $xml = new SimpleXMLElement($output);
- if ($xml->returncode == 'SUCCESS') {
- $event->bbb_attendee_password = (string) $xml->attendeePW;
- $event->bbb_moderator_password = (string) $xml->moderatorPW;
- return TRUE;
- } else {
- return FALSE;
- }
- } else {
- return FALSE;
- }
- } else {
- return FALSE;
- }
- }
- // checks to see if a BBB conference is actually running
- function event_calendar_is_conference_running($event) {
- $output = event_calendar_bbb_api('isMeetingRunning',array('meetingID'=>$event->guid));
- if (!$output) {
- return FALSE;
- } else {
- $xml = new SimpleXMLElement($output);
- if ($xml->returncode == 'SUCCESS' && $xml->running == 'true') {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- }
- // checks to see if a BBB conference exists
- function event_calendar_conference_exists($event) {
- $output = event_calendar_bbb_api('getMeetingInfo',array('meetingID'=>$event->guid,'password'=>$event->bbb_moderator_password));
- if (!$output) {
- return FALSE;
- } else {
- $xml = new SimpleXMLElement($output);
- if ($xml->returncode == 'SUCCESS' && $xml->meetingID == $event->guid) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- }
- // forwards to the join link
- // this function assumes that the conference is running
- function event_calendar_join_conference($event) {
- forward(event_calendar_get_join_bbb_url($event));
- }
- function event_calendar_get_join_bbb_url($event) {
- $bbb_security_salt = elgg_get_plugin_setting('bbb_security_salt','event_calendar');
- $bbb_server_url = rtrim(elgg_get_plugin_setting('bbb_server_url','event_calendar'), '/') . '/';
- $user = elgg_get_logged_in_user_entity();
- $full_name = urlencode($user->name);
- if ($event->canEdit()) {
- $password = urlencode($event->bbb_moderator_password);
- } else {
- $password = urlencode($event->bbb_attendee_password);
- }
- $params = "fullName=$full_name&meetingID={$event->guid}&userID={$user->username}&password=$password";
- $checksum = sha1('join'.$params.$bbb_security_salt);
- $params .= "&checksum=$checksum";
- $url = $bbb_server_url.'api/join?'.$params;
- return $url;
- }
- // returns TRUE if the given user can add an event to the given calendar
- // if group_guid is 0, this is assumed to be the site calendar
- function event_calendar_can_add($group_guid=0,$user_guid=0) {
- if (!$user_guid) {
- if (elgg_is_logged_in()) {
- $user_guid = elgg_get_logged_in_user_guid();
- } else {
- return FALSE;
- }
- }
- if ($group_guid) {
- if (!event_calendar_activated_for_group($group_guid)) {
- return FALSE;
- }
- $group = get_entity($group_guid);
- if (elgg_instanceof($group,'group')) {
- $group_calendar = elgg_get_plugin_setting('group_calendar', 'event_calendar');
- if (!$group_calendar || $group_calendar == 'members') {
- return $group->canWriteToContainer($user_guid);
- } else if ($group_calendar == 'admin') {
- if ($group->canEdit($user_guid)) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- } else {
- return FALSE;
- }
- } else {
- $site_calendar = elgg_get_plugin_setting('site_calendar', 'event_calendar');
- if (!$site_calendar || $site_calendar == 'admin') {
- // only admins can post directly to the site-wide calendar
- return elgg_is_admin_user($user_guid);
- } else if ($site_calendar == 'loggedin') {
- // any logged-in user can post to the site calendar
- return TRUE;
- }
- }
- return FALSE;
- }
|