defaults $values = array( 'title' => '', 'description' => '', 'access_id' => ACCESS_PUBLIC, 'guid' => null, 'entity' => $dmgroup, 'membership' => ACCESS_PRIVATE ); if ($dmgroup) { foreach (array_keys($values) as $field) { if (isset($dmgroup->$field)) { $values[$field] = $dmgroup->$field; } } } if (elgg_is_sticky_form('dmgroup')) { $sticky_values = elgg_get_sticky_values('dmgroup'); foreach ($sticky_values as $key => $value) { $values[$key] = $value; } } elgg_clear_sticky_form('dmgroup'); return $values; } /** * Registers the buttons for title area of the gmgroup view page * * @param ElggObject $dmgroup DigiMinaGroup object */ function digimina_register_dmgroup_view_buttons($dmgroup) { $link_class = 'elgg-button elgg-button-action'; // dmgroup owners if ($dmgroup->canEdit()) { // edit $url = elgg_get_site_url() . "digimina/edit/{$dmgroup->getGUID()}"; /*translation:Edit group*/ $text = elgg_echo('digimina:dmgroup:edit'); $options = array( 'name' => 'digimina:dmgroup:edit', 'href' => $url, 'text' => $text, 'link_class' => $link_class ); elgg_register_menu_item('title', ElggMenuItem::factory($options)); } // gmgroup members if ($dmgroup->isMember(elgg_get_logged_in_user_entity())) { if ($dmgroup->getOwnerGUID() != elgg_get_logged_in_user_guid()) { // leave $url = elgg_get_site_url() . "action/digimina/leave?dmgroup_guid={$dmgroup->getGUID()}"; $url = elgg_add_action_tokens_to_url($url); /*translation:Leave*/ $text = elgg_echo('digimina:dmgroup:leave'); $options = array( 'name' => 'digimina:dmgroup:leave', 'href' => $url, 'text' => $text, 'link_class' => $link_class ); elgg_register_menu_item('title', ElggMenuItem::factory($options)); } } elseif (elgg_is_logged_in()) { // join - admins can always join $url = elgg_get_site_url() . "action/digimina/join?dmgroup_guid={$dmgroup->getGUID()}"; $url = elgg_add_action_tokens_to_url($url); if ($dmgroup->isPublicMembership() || $dmgroup->canEdit()) { /*translation:Join*/ $text = elgg_echo('digimina:dmgroup:join'); $name = 'digimina:dmgroup:join'; } else { /*translation:Request membership*/ $text = elgg_echo('digimina:dmgroup:joinrequest'); $name = 'digimina:dmgroup:joinrequest'; } $options = array( 'name' => $name, 'href' => $url, 'text' => $text, 'link_class' => $link_class ); elgg_register_menu_item('title', ElggMenuItem::factory($options)); } } /** * Return a list of this dmgroup's members. * * @param int $dmgroup_guid The ID of the dmgroup. * @param int $limit The limit * @param int $offset The offset * @param int $site_guid The site * @param book $count Return the users (false) or the count of them (true) * * @return mixed */ function get_dmgroup_members($dmgroup_guid, $limit = 10, $offset = 0, $site_guid = 0, $count = false, $by_name = false) { if (!$site_guid) { $site_guid = ELGG_ENTITIES_ANY_VALUE; } $options = array( 'relationship' => 'dmmember', 'relationship_guid' => $dmgroup_guid, 'inverse_relationship' => TRUE, 'types' => 'user', 'limit' => $limit, 'offset' => $offset, 'count' => $count, 'site_guid' => $site_guid ); if ($by_name) { global $CONFIG; $options['joins'] = array("JOIN {$CONFIG->dbprefix}users_entity u on e.guid = u.guid"); $options['order_by'] = 'u.name DESC'; } return elgg_get_entities_from_relationship($options); } /** * Return whether a given user is a member of the dmgroup or not. * * @param int $dmgroup_guid The dmgroup ID * @param int $user_guid The user guid * * @return bool */ function is_dmgroup_member($dmgroup_guid, $user_guid) { $object = check_entity_relationship($user_guid, 'dmmember', $dmgroup_guid); if ($object) { return true; } else { return false; } } /** * Join a user to a dmgroup. * * @param int $dmgroup_guid The group GUID * @param int $user_guid The user GUID * * @return bool */ function join_dmgroup($dmgroup_guid, $user_guid) { $result = add_entity_relationship($user_guid, 'dmmember', $dmgroup_guid); return $result; } /** * Remove a user from a dmgroup. * * @param int $dmgroup_guid The group GUID * @param int $user_guid The user GUID * * @return bool */ function leave_dmgroup($dmgroup_guid, $user_guid) { $result = remove_entity_relationship($user_guid, 'dmmember', $dmgroup_guid); return $result; } /** * Return all dmgroups a user is a member of. * * @param int $user_guid The user GUID * @param bool $by_title If groups should be ordered by title * * @return array|false */ function get_users_dmmembership($user_guid, $by_title = false) { $options = array( 'relationship' => 'dmmember', 'relationship_guid' => $user_guid, 'inverse_relationship' => FALSE, 'limit' => 0, 'types' => 'object', 'subtypes' => 'dmgroup' ); if ($by_title) { global $CONFIG; $options['joins'] = array("JOIN {$CONFIG->dbprefix}objects_entity oe on e.guid = oe.guid"); $options['order_by'] = "oe.title DESC"; } return elgg_get_entities_from_relationship($options); } /** * Returns competency model object. * * @return mixed */ function digimina_get_competency_model() { $options = array( 'types' => 'object', 'subtypes' => 'dmcompetencymodel', 'limit' => 1 ); $result = elgg_get_entities($options); if ($result && is_array($result) && sizeof($result)) { return $result[0]; } return false; } /** * Returns performance level items * @uses int $performance_level_guid Performance level guid * @return mixed Array, false on errors */ function digimina_get_performance_level_items($performance_level_guid) { $options = array( 'types' => 'object', 'subtypes' => 'dmitem', "limit" => 0, "metadata_name_value_pairs" => array('name' => 'dmperformancelevel', 'value' => $performance_level_guid) ); return elgg_get_entities_from_metadata($options); } /** * Checks if current user is an owner of at least one group where the user is a member * * @uses int $user_guid User GUID * * @return bool */ function digimina_check_dmmembership_group_owner($user_guid) { // Sanity check, can not be an owner if not logged_in if (!elgg_is_logged_in()) { return false; } $options = array( 'relationship' => 'dmmember', 'relationship_guid' => $user_guid, 'count' => TRUE, 'types' => 'object', 'subtypes' => 'dmgroup', 'owner_guids' => array(elgg_get_logged_in_user_guid()) ); if (elgg_get_entities_from_relationship($options) > 0) { return true; } return false; } /** * Checks if current user and the user are members of at least one same group * * @uses int $user_guid User GUID * * @return bool */ function digimina_check_dmmembership_group_member($user_guid) { // Sanity check, can not be a member if not logged_in if (!elgg_is_logged_in()) { return false; } $options = array( 'relationship' => 'dmmember', 'relationship_guid' => $user_guid, 'count' => TRUE, 'types' => 'object', 'subtypes' => 'dmgroup' ); // Get additional SQL $additional = elgg_get_entity_relationship_where_sql('e.guid', 'dmmember', elgg_get_logged_in_user_guid(), FALSE); // Substitute "r" for something else, this way it would work as expected foreach ($additional['wheres'] as $key => $saw) { $additional['wheres'][$key] = str_replace('r.', 'rdmm.', $saw); } foreach ($additional['joins'] as $key => $saj) { $saj = str_replace('r.', 'rdmm.', $saj); $saj = str_replace(' r ', ' rdmm ', $saj); $additional['joins'][$key] = $saj; } // Add SQL with substituted "r" to options $options['wheres'] = $additional['wheres']; $options['joins'] = $additional['joins']; if (elgg_get_entities_from_relationship($options) > 0) { return true; } return false; } /** * Return ongoing test if any * * @param int $owner_guid User GUID, logged in user guide is used if not provided * * @return mixed */ function digimina_get_ongoing_test($owner_guid=NULL) { if (!$owner_guid) { $owner_guid = elgg_get_logged_in_user_guid(); } $options = array( 'type' => 'object', 'subtype' => 'dmtest', 'owner_guids' => array($owner_guid), 'limit' => 1, 'metadata_name_value_pairs' => array('name' => 'state', 'value' => 'ongoing') ); $data = elgg_get_entities_from_metadata($options); if ($data && is_array($data) && sizeof($data)) { return $data[0]; } return false; } /** * Check if a person can start a new test * * @return bool */ function digimina_can_start_new_test() { if (!elgg_is_logged_in()) { return false; } if (!digimina_get_ongoing_test()) { return true; } return false; } /** * Return all tests for a user * * @param int $owner_guid User GUID, logged in user guide is used if not provided * * @return mixed */ function digimina_get_user_tests($owner_guid=NULL) { if (!$owner_guid) { $owner_guid = elgg_get_logged_in_user_guid(); } $options = array( 'type' => 'object', 'subtype' => 'dmtest', 'owner_guids' => array($owner_guid), 'limit' => 0, 'reverse_order_by' => TRUE ); return elgg_get_entities($options); } /** * Return answer object based on test GUID and level GUID * * @param int $test_guid DigiMinaTest GUID * @param int $level_guid DigiMinaPerformanceLevel GUID * * @return mixed */ function digimina_get_answer_by_test_and_level($test_guid, $level_guid) { $options = array( 'type' => 'object', 'subtype' => 'dmanswer', 'container_guids' => array($test_guid), 'limit' => 1, 'metadata_name_value_pairs' => array('name' => 'dmperformancelevel', 'value' => $level_guid) ); $data = elgg_get_entities_from_metadata($options); if ($data && is_array($data) && sizeof($data)) { return $data[0]; } return false; }