/** * Send push notifications if event is added to the activity river * * @param string $event name of the event * @param string $type type of the event * @param ElggRiverItem $river_item river item object * * @return void */ function ws_pack_created_river_event_handler($event, $type, $river_item) { if (!empty($river_item) && $river_item instanceof ElggRiverItem) { if (!function_exists('str_get_html')) { elgg_load_library("simple_html_dom"); } $message = ""; $html_view = elgg_view_river_item($river_item); if ($res = str_get_html($html_view)) { // get the river summary if ($summary_element = $res->find("div.elgg-river-summary")) { $summary_element = $summary_element[0]; $text = $summary_element->innertext(); list($left, $right) = explode("<span class=\"elgg-river-timestamp\">", $text); $message = trim(elgg_strip_tags($left)); } } if (!empty($message)) { $user_guids = false; switch ($river_item->access_id) { case ACCESS_PRIVATE: // do nothing break; case ACCESS_PUBLIC: case ACCESS_LOGGED_IN: // notify everyone $site_user_options = array("type" => "user", "limit" => false, "relationship" => "member_of_site", "relationship_guid" => elgg_get_site_entity()->getGUID(), "inverse_relationship" => true, "callback" => "ws_pack_row_to_guid"); $user_guids = elgg_get_entities_from_relationship($site_user_options); break; case ACCESS_FRIENDS: // notify friends of subject_guid $friends_options = array("type" => "user", "limit" => false, "relationship" => "friend", "relationship_guid" => $river_item->subject_guid, "callback" => "ws_pack_row_to_guid", "joins" => array("JOIN " . elgg_get_config("dbprefix") . "entity_relationships r2 ON e.guid = r2.guid_one"), "wheres" => array("(r2.relationship = 'member_of_site' AND r2.guid_two = " . elgg_get_site_entity()->getGUID() . ")")); $user_guids = elgg_get_entities_from_relationship($friends_options); break; default: // probably some acl, so notify members of the acl $user_guids = get_members_of_access_collection($river_item->access_id, true); break; } // we found potential interested users, so push if (!empty($user_guids) && is_array($user_guids)) { $api_application_options = array("type" => "object", "subtype" => APIApplication::SUBTYPE, "limit" => false, "annotation_name" => "push_notification_service"); if ($api_applications = elgg_get_entities_from_annotations($api_application_options)) { foreach ($api_applications as $api_application) { $api_application->sendPushNotification($message, $user_guids); } } } } } }
function group_tools_check_group_email_invitation($invite_code, $group_guid = 0) { $result = false; if (!empty($invite_code)) { $options = array("type" => "group", "limit" => 1, "site_guids" => false, "annotation_name_value_pairs" => array("email_invitation" => $invite_code)); if (!empty($group_guid)) { $options["annotation_owner_guids"] = array($group_guid); } if ($groups = elgg_get_entities_from_annotations($options)) { $result = $groups[0]; } } return $result; }
/** * Check if a invitation code results in a group * * @param string $invite_code the invite code * @param int $group_guid (optional) the group to check * * @return boolean|ElggGroup a group for the invitation or false */ function group_tools_check_group_email_invitation($invite_code, $group_guid = 0) { $result = false; if (!empty($invite_code)) { $options = array("type" => "group", "limit" => 1, "site_guids" => false, "annotation_name_value_pairs" => array(array("name" => "email_invitation", "value" => $invite_code), array("name" => "email_invitation", "value" => $invite_code . "|%", "operand" => "LIKE")), "annotation_name_value_pairs_operator" => "OR"); if (!empty($group_guid)) { $options["annotation_owner_guids"] = array($group_guid); } // find hidden groups $ia = elgg_set_ignore_access(true); $groups = elgg_get_entities_from_annotations($options); if (!empty($groups)) { $result = $groups[0]; } // restore access elgg_set_ignore_access($ia); } return $result; }
/** * Unban users whose timeouts have expired * * @return void */ function ban_cron() { $previous = elgg_set_ignore_access(); $dbprefix = get_config('dbprefix'); $params = array('type' => 'user', 'annotation_names' => array('ban_release'), 'joins' => array("JOIN {$dbprefix}users_entity u on e.guid = u.guid"), 'wheres' => array("u.banned='yes'")); $now = time(); $users = elgg_get_entities_from_annotations($params); foreach ($users as $user) { $releases = elgg_get_annotations(array('guid' => $user->guid, 'annotation_name' => 'ban_release', 'limit' => 1, 'order' => 'n_table.time_created desc')); foreach ($releases as $release) { if ($release->value < $now) { if ($user->unban()) { $release->delete(); } } } } elgg_set_ignore_access($previous); }
public function testElggApiGettersEntitiesFromAnnotation() { // grab a few different users to annotation // there will always be at least 2 here because of the construct. $users = elgg_get_entities(array('type' => 'user', 'limit' => 2)); // create some test annotations $subtypes = $this->getRandomValidSubtypes(array('object'), 1); $subtype = $subtypes[0]; $annotation_name = 'test_annotation_name_' . rand(); $annotation_value = rand(1000, 9999); $annotation_name2 = 'test_annotation_name_' . rand(); $annotation_value2 = rand(1000, 9999); $guids = array(); // our targets $valid = new \ElggObject(); $valid->subtype = $subtype; $valid->save(); $guids[] = $valid->getGUID(); create_annotation($valid->getGUID(), $annotation_name, $annotation_value, 'integer', $users[0]->getGUID()); $valid2 = new \ElggObject(); $valid2->subtype = $subtype; $valid2->save(); $guids[] = $valid2->getGUID(); create_annotation($valid2->getGUID(), $annotation_name2, $annotation_value2, 'integer', $users[1]->getGUID()); $options = array('annotation_owner_guid' => $users[0]->getGUID(), 'annotation_name' => $annotation_name); $entities = elgg_get_entities_from_annotations($options); foreach ($entities as $entity) { $this->assertTrue(in_array($entity->getGUID(), $guids)); $annotations = $entity->getAnnotations(array('annotation_name' => $annotation_name)); $this->assertEqual(count($annotations), 1); $this->assertEqual($annotations[0]->name, $annotation_name); $this->assertEqual($annotations[0]->value, $annotation_value); $this->assertEqual($annotations[0]->owner_guid, $users[0]->getGUID()); } foreach ($guids as $guid) { if ($e = get_entity($guid)) { $e->delete(); } } }
/** * Get entities ordered by a mathematical calculation on annotation values * * @tip Note that this function uses { @link elgg_get_annotations() } to return a list of entities ordered by a mathematical * calculation on annotation values, and { @link elgg_get_entities_from_annotations() } to return a count of entities * if $options['count'] is set to a truthy value * * @param array $options An options array: * 'calculation' => The calculation to use. Must be a valid MySQL function. * Defaults to sum. Result selected as 'annotation_calculation'. * Don't confuse this "calculation" option with the * "annotation_calculation" option to elgg_get_annotations(). * This "calculation" option is applied to each entity's set of * annotations and is selected as annotation_calculation for that row. * See the docs for elgg_get_annotations() for proper use of the * "annotation_calculation" option. * 'order_by' => The order for the sorting. Defaults to 'annotation_calculation desc'. * 'annotation_names' => The names of annotations on the entity. * 'annotation_values' => The values of annotations on the entity. * * 'metadata_names' => The name of metadata on the entity. * 'metadata_values' => The value of metadata on the entitiy. * 'callback' => Callback function to pass each row through. * @tip This function is different from other ege* functions, * as it uses a metastring-based getter function { @link elgg_get_annotations() }, * therefore the callback function should be a derivative of { @link entity_row_to_elggstar() } * and not of { @link row_to_annotation() } * * @return \ElggEntity[]|int An array or a count of entities * @see elgg_get_annotations() * @see elgg_get_entities_from_annotations() */ function getEntitiesFromCalculation($options) { if (isset($options['count']) && $options['count']) { return elgg_get_entities_from_annotations($options); } $db_prefix = $this->db->prefix; $defaults = array('calculation' => 'sum', 'order_by' => 'annotation_calculation desc'); $options = array_merge($defaults, $options); $function = sanitize_string(elgg_extract('calculation', $options, 'sum', false)); // you must cast this as an int or it sorts wrong. $options['selects'][] = 'e.*'; $options['selects'][] = "{$function}(CAST(n_table.value AS signed)) AS annotation_calculation"; // don't need access control because it's taken care of by elgg_get_annotations. $options['group_by'] = 'n_table.entity_guid'; // do not default to a callback function used in elgg_get_annotation() if (!isset($options['callback'])) { $options['callback'] = 'entity_row_to_elggstar'; } return elgg_get_annotations($options); }
switch ($filter) { case "mine": $filter_mine["selected"] = true; $title_text = elgg_echo("subsite_manager:subsites:title:mine"); $options["relationship"] = "member_of_site"; $options["relationship_guid"] = elgg_get_logged_in_user_guid(); $body = elgg_list_entities_from_relationship($options); break; case "membership": $filter_membership["selected"] = true; $search_box = false; $title_text = elgg_echo("subsite_manager:subsites:title:membership"); if (!empty($membership_count)) { $mem_options["count"] = false; $mem_options["limit"] = $count; $sites = elgg_get_entities_from_annotations($mem_options); $list_options = array("count" => count($sites), "offset" => $offset, "limit" => $limit, "full_view" => false, "list_type_toggle" => false); $body = elgg_view_entity_list($sites, $list_options); } break; case "open": $filter_open["selected"] = true; $title_text = elgg_echo("subsite_manager:subsites:title:open"); $options["joins"][] = "JOIN " . get_config("dbprefix") . "private_settings ps ON ps.entity_guid = e.guid"; $options["wheres"][] = "(ps.name = 'membership' AND ps.value = '" . Subsite::MEMBERSHIP_OPEN . "')"; unset($options["order_by"]); //order by time created desc $body = elgg_list_entities_from_relationship($options); break; case "closed": $filter_closed["selected"] = true;
/** * Ajax endpoint for chat notifier. * * Provides the number of unread messages and a list of * latest messages. These are then used to populate the * topbar menu item and popup module. */ /** * @todo Could we just make a straight query that checks how many * "unread_messages" annotations the user has? Is it possible * to get also already read messages with the same query? */ // Do not view edit/delete elgg_push_context('chat_preview'); $user = elgg_get_logged_in_user_entity(); $chats = elgg_get_entities_from_annotations(array('type' => 'object', 'subtype' => 'chat', 'annotation_name' => 'unread_messages', 'annotation_owner_guids' => $user->getGUID(), 'limit' => 5)); $message_count = 0; $guids = array(); if ($chats) { foreach ($chats as $chat) { $message_count += $chat->getUnreadMessagesCount(); $guids[] = $chat->getGUID(); } } // If less than 5 unread chats were found, get other chats $num_chats = count($guids); if ($num_chats < 5) { $limit = 5 - $num_chats; $options = array('type' => 'object', 'subtype' => 'chat', 'relationship' => 'member', 'relationship_guid' => $user->getGUID(), 'inverse_relationship' => false, 'limit' => $limit); // Do not get the chats that were fetched earlier if ($num_chats) {
/** * Get all chats with unread messages. * * @param array $options See elgg_get_entities_from_annotations(). */ function chat_get_unread_chats($options = array()) { $user = elgg_get_logged_in_user_entity(); $defaults = array('type' => 'object', 'subtype' => 'chat', 'annotation_names' => 'unread_messages', 'annotation_owner_guids' => $user->getGUID(), 'count' => false); $options = array_merge($defaults, $options); return elgg_get_entities_from_annotations($options); }
<?php /** * User share widget display view */ $num = $vars['entity']->num_display; $options = array('annotation_names' => 'share', 'annotation_values' => 'share', 'annotation_owner_guids' => $vars['entity']->owner_guid, 'limit' => $num, 'full_view' => FALSE, 'pagination' => FALSE); $entities = elgg_get_entities_from_annotations($options); $content = elgg_view_entity_list($entities, array('limit' => $num, 'full_view' => FALSE, 'list_type_toggle' => FALSE, 'pagination' => FALSE)); echo $content; if (!$content) { echo elgg_echo('share:widget:noshares'); }
/** * Get entities ordered by a mathematical calculation * * @param array $options An options array: * 'calculation' => The calculation to use. Must be a valid MySQL function. * Defaults to sum. Result selected as 'calculated'. * 'order_by' => The order for the sorting. Defaults to 'calculated desc'. * * @return mixed */ function elgg_get_entities_from_annotation_calculation($options) { global $CONFIG; $defaults = array('calculation' => 'sum', 'order_by' => 'calculated desc'); $options = array_merge($defaults, $options); $function = sanitize_string(elgg_extract('calculation', $options, 'sum', false)); // you must cast this as an int or it sorts wrong. $options['selects'][] = "{$function}(cast(msv.string as signed)) as calculated"; $options['selects'][] = "msn.string as value"; $options['order_by'] = 'calculated desc'; // need our own join to get the values. $db_prefix = get_config('dbprefix'); $options['joins'][] = "JOIN {$db_prefix}annotations calc_table on e.guid = calc_table.entity_guid"; $options['joins'][] = "JOIN {$db_prefix}metastrings msv on calc_table.value_id = msv.id"; $options['wheres'][] = "calc_table.name_id = n_table.name_id"; return elgg_get_entities_from_annotations($options); }
<?php // Latest forum discussion for the group home page //check to make sure this group forum has been activated if ($vars['entity']->forum_enable != 'no') { ?> <h3><?php echo elgg_echo('groups:latestdiscussion'); ?> </h3> <?php $forum = elgg_get_entities_from_annotations(array('types' => 'object', 'subtypes' => 'groupforumtopic', 'annotation_names' => 'group_topic_post', 'container_guid' => $vars['entity']->guid, 'limit' => 4, 'order_by' => 'maxtime desc')); if ($forum) { foreach ($forum as $f) { $count_annotations = $f->countAnnotations("group_topic_post"); echo "<div class='entity_listing clearfloat'>"; echo "<div class='entity_listing_icon'>" . elgg_view('profile/icon', array('entity' => $f->getOwnerEntity(), 'size' => 'tiny', 'override' => true)) . "</div>"; echo "<div class='entity_listing_info'><p class='entity_title'><a href=\"{$vars['url']}mod/groups/topicposts.php?topic={$f->guid}&group_guid={$vars['entity']->guid}\">" . $f->title . "</a></p>"; echo "<p class='entity_subtext'>" . elgg_echo('groups:posts') . ": " . $count_annotations . "</p></div>"; echo "</div>"; } } else { echo "<p class='margin_top'>" . elgg_echo("grouptopic:notcreated") . "</p>"; } } //end of forum active check
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); }
<?php elgg_push_context('widgets'); $fav_group_params = array('type' => 'group', 'annotation_name' => 'favourite', 'order_by_annotation' => "n_table.time_created desc", 'full_view' => false, 'view_type_toggle' => FALSE, 'annotation_owner_guid' => $current_user->guid); $fav_group_params['count'] = true; if (elgg_get_entities_from_annotations($fav_group_params) == 0) { return true; } $fav_group_params['count'] = false; $fav_group_body = elgg_list_entities_from_annotations($fav_group_params); echo elgg_view_module('aside', elgg_echo('favourites:groups'), $fav_group_body);
/** * Get entities from annotations * * No longer used. * * @deprecated 1.7 Use elgg_get_entities_from_annotations() * * @param mixed $entity_type Type of entity * @param mixed $entity_subtype Subtype of entity * @param string $name Name of annotation * @param string $value Value of annotation * @param int $owner_guid Guid of owner of annotation * @param int $group_guid Guid of group * @param int $limit Limit * @param int $offset Offset * @param string $order_by SQL order by string * @param bool $count Count or return entities * @param int $timelower Lower time limit * @param int $timeupper Upper time limit * * @return unknown_type */ function get_entities_from_annotations($entity_type = "", $entity_subtype = "", $name = "", $value = "", $owner_guid = 0, $group_guid = 0, $limit = 10, $offset = 0, $order_by = "asc", $count = false, $timelower = 0, $timeupper = 0) { $msg = 'get_entities_from_annotations() is deprecated by elgg_get_entities_from_annotations().'; elgg_deprecated_notice($msg, 1.7); $options = array(); if ($entity_type) { $options['types'] = $entity_type; } if ($entity_subtype) { $options['subtypes'] = $entity_subtype; } $options['annotation_names'] = $name; if ($value) { $options['annotation_values'] = $value; } if ($owner_guid) { if (is_array($owner_guid)) { $options['annotation_owner_guids'] = $owner_guid; } else { $options['annotation_owner_guid'] = $owner_guid; } } if ($group_guid) { $options['container_guid'] = $group_guid; } if ($limit) { $options['limit'] = $limit; } if ($offset) { $options['offset'] = $offset; } if ($order_by) { $options['order_by'] = "maxtime {$order_by}"; } if ($count) { $options['count'] = $count; } if ($timelower) { $options['annotation_created_time_lower'] = $timelower; } if ($timeupper) { $options['annotation_created_time_upper'] = $timeupper; } return elgg_get_entities_from_annotations($options); }
/** * Get entities ordered by a mathematical calculation on annotation values * * @tip Note that this function uses { @link elgg_get_annotations() } to return a list of entities ordered by a mathematical * calculation on annotation values, and { @link elgg_get_entities_from_annotations() } to return a count of entities * if $options['count'] is set to a truthy value * * @param array $options An options array: * 'calculation' => The calculation to use. Must be a valid MySQL function. * Defaults to sum. Result selected as 'annotation_calculation'. * Don't confuse this "calculation" option with the * "annotation_calculation" option to elgg_get_annotations(). * This "calculation" option is applied to each entity's set of * annotations and is selected as annotation_calculation for that row. * See the docs for elgg_get_annotations() for proper use of the * "annotation_calculation" option. * 'order_by' => The order for the sorting. Defaults to 'annotation_calculation desc'. * 'annotation_names' => The names of annotations on the entity. * 'annotation_values' => The values of annotations on the entity. * * 'metadata_names' => The name of metadata on the entity. * 'metadata_values' => The value of metadata on the entitiy. * 'callback' => Callback function to pass each row through. * @tip This function is different from other ege* functions, * as it uses a metastring-based getter function { @link elgg_get_annotations() }, * therefore the callback function should be a derivative of { @link entity_row_to_elggstar() } * and not of { @link row_to_annotation() } * * @return \ElggEntity[]|int An array or a count of entities * @see elgg_get_annotations() * @see elgg_get_entities_from_annotations() */ function getEntitiesFromCalculation($options) { if (isset($options['count']) && $options['count']) { return elgg_get_entities_from_annotations($options); } $db_prefix = _elgg_services()->config->get('dbprefix'); $defaults = array('calculation' => 'sum', 'order_by' => 'annotation_calculation desc'); $options = array_merge($defaults, $options); $function = sanitize_string(elgg_extract('calculation', $options, 'sum', false)); // you must cast this as an int or it sorts wrong. $options['selects'][] = 'e.*'; $options['selects'][] = "{$function}(CAST(a_msv.string AS signed)) AS annotation_calculation"; // need our own join to get the values because the lower level functions don't // add all the joins if it's a different callback. $options['joins'][] = "JOIN {$db_prefix}metastrings a_msv ON n_table.value_id = a_msv.id"; // don't need access control because it's taken care of by elgg_get_annotations. $options['group_by'] = 'n_table.entity_guid'; // do not default to a callback function used in elgg_get_annotation() if (!isset($options['callback'])) { $options['callback'] = 'entity_row_to_elggstar'; } return elgg_get_annotations($options); }
<?php $user = elgg_get_page_owner_entity(); if ($user) { echo "<div class='theme-ffd-profile-question-stats'>"; $question_options = array("type" => "object", "subtype" => "question", "count" => true, "owner_guid" => $user->guid); $answer_options = array("type" => "object", "subtype" => "answer", "count" => true, "owner_guid" => $user->guid); $correct_options = array("type" => "object", "subtype" => "answer", "count" => true, "owner_guid" => $user->guid, "metadata_name" => "correct_answer", "metadata_value" => true); $likes_options = array("type" => "object", "subtype" => "question", "count" => true, "annotation_names" => array("likes"), "annotation_owner_guids" => array($user->guid)); $asked_count = (int) elgg_get_entities($question_options); $answered_count = (int) elgg_get_entities($answer_options); $correct_count = (int) elgg_get_entities_from_metadata($correct_options); $liked_count = (int) elgg_get_entities_from_annotations($likes_options); echo "<ul>"; echo "<li>" . elgg_echo("theme_ffd:profile:question_stats:count_questions") . ": "; echo elgg_view("output/url", array("href" => "/questions/owner/" . $user->username, "text" => "<span class='theme-ffd-profile-question-stats-count'>" . $asked_count . "</span>")); echo "</li>"; echo "<li>" . elgg_echo("theme_ffd:profile:question_stats:count_answers") . ": <span class='theme-ffd-profile-question-stats-count'>" . $answered_count . "</span></li>"; echo "</ul>"; echo "</div>"; }