Get user guid

Hi, I'm a beginner in php and in elgg.

I can ask the database myself and code in php myself BUT, I do have a problem I'm trying to tweak the friendrequest plugin to only allow friendship between group of people I choosed.

I need to ask elgg for the curent user guid and friend guid. I get functions on the web but I can't make it work :/

I am really beginning in functions, if u could get me the really tiny piece of code wich ask elgg the guid and prompt it in a "$guid_searched" I would be so glad.

Thank u all for your support :)

  • Following methods are used to get user guid you can use of your own need.

    • elgg_get_logged_in_user_entity()->guid;
    • elgg_get_page_owner_entity()->guid;
    • get_user_by_username(<username>)->guid;
    • get_user_by_email(<email>)->guid;

    Regards

  • Hi, I didn't get what should I do with these?

    For exemple if I wanna use the first

    shall I write $guid = elgg_get_logged_in_user_entity()->guid; ?

  • If you want the guid of the logged in user you can also get it directly with:

    $guid = elgg_get_logged_in_guid();

    How to retrieve the friend's guid depends on what exactly you are trying to implement, so I can't give you the perfect example. For example

    $users_friends = get_user_friends(elgg_get_logged_in_user_guid(), "", false);

    would return the user entities of all friends of the currently logged in user.

    For reference of Elgg functions you can use http://reference.elgg.org/.

  • Ok I am trying to disallow friendship between different kind of people, I am thinking about disallowing between guys and girls but I could disallow under 18 years old to make friendship with older people as well.

    I am starting from the friendrequest plugin

    So Now I can start from the user Id and find if it's a man or a woman.

    Now I need to get the id of the one he is trying to be friend with. So I have to find the good area in the add.php to tweak as it must act on two kind of buttons (one in the profil page, on in the member list page)

  • I'm going to give you the high level overview of what you need to do, then you can dig in and figure out the details:

    1. Hook into the 'friending' action

    In your hook you can check who is friending who, and if it's not an allowed match register an error message and return false to prevent the action

    2. Hook into the 'create', 'friend' event

    Same kind of check can be done here too, just in case there are other means of friending happening other than the standard action (from other plugins or whatever)

    return false to prevent the relationship from being created

     

    as stated above you can get the currently logged in user guid with elgg_get_logged_in_user_guid();

    You can get the potential friend guid with get_input() in the action hook and by checking the relationship guids in relationship event

     

    Docs pertaining to what I'm talking about:

    http://docs.elgg.org/wiki/Plugin_Hooks

    http://docs.elgg.org/wiki/Events_Overview

  • Thanks for ur support, but I find it so hard. I didn't get what hook is and my difficulty is to find the place inside the code where the plugin ask for the friend data and ask elgg to make friendship.

    here is my add.php

    <?php
        
        //Get our data
        $friend_guid = (int) get_input("friend");
        $friend = get_user($friend_guid);
        
        $user = elgg_get_logged_in_user_entity();
        
        $errors = false;
        
        //Now we need to attempt to create the relationship
        if(empty($user) || empty($friend)) {
            $errors = true;
            register_error(elgg_echo("friend_request:add:failure"));
        } else {
            //New for v1.1 - If the other user is already a friend (fan) of this user we should auto-approve the friend request...
            if(check_entity_relationship($friend->getGUID(), "friend", $user->getGUID())) {
                try {
                    if(isset($CONFIG->events["create"]["friend"])) {
                        $oldEventHander = $CONFIG->events["create"]["friend"];
                        $CONFIG->events["create"]["friend"] = array();            //Removes any event handlers
                    }
                    
                    $user->addFriend($friend->getGUID());
                    system_message(elgg_echo("friends:add:successful", array($friend->name)));
                    
                    if(isset($CONFIG->events["create"]["friend"])) {
                        $CONFIG->events["create"]["friend"] = $oldEventHander;
                    }
                    
                    forward(REFERER);
                } catch (Exception $e) {
                    register_error(elgg_echo("friends:add:failure", array($friend->name)));
                    $errors = true;
                }
            } elseif(check_entity_relationship($friend->getGUID(), "friendrequest", $user->getGUID())){
                // Check if your potential friend already invited you, if so make friends
                if(remove_entity_relationship($friend->getGUID(), "friendrequest", $user->getGUID())){
                    if(isset($CONFIG->events["create"]["friend"])) {
                        $oldEventHander = $CONFIG->events["create"]["friend"];
                        $CONFIG->events["create"]["friend"] = array();            //Removes any event handlers
                    }
                    
                    $user->addFriend($friend->getGUID());
                    $friend->addFriend($user->getGUID());            //Friends mean reciprical...
                    
                    if(isset($CONFIG->events["create"]["friend"])) {
                        $CONFIG->events["create"]["friend"] = $oldEventHander;
                    }
                    
                    system_message(elgg_echo("friend_request:approve:successful", array($friend->name)));
                    // add to river
                    add_to_river("river/relationship/friend/create", "friend", $user->getGUID(), $friend->getGUID());
                    add_to_river("river/relationship/friend/create", "friend", $friend->getGUID(), $user->getGUID());
                    
                    forward(REFERER);
                } else {
                    register_error(elgg_echo("friend_request:approve:fail", array($friend->name)));
                }
            } else {
                try {
                    $result = add_entity_relationship($user->getGUID(), "friendrequest", $friend->getGUID());
                    if($result == false) {
                        $errors = true;
                        register_error(elgg_echo("friend_request:add:exists", array($friend->name)));
                    }
                } catch(Exception $e) {    //register_error calls insert_data which CAN raise Exceptions.
                    $errors = true;
                    register_error(elgg_echo("friend_request:add:exists", array($friend->name)));
                }
            }
        }
        
        if(!$errors) {
            system_message(elgg_echo("friend_request:add:successful", array($friend->name)));
        }
        
        forward(REFERER);

  • The first lines:

    $friend_guid = (int) get_input("friend");
    $friend = get_user($friend_guid);
        
    $user = elgg_get_logged_in_user_entity();

    retrieve the user entities of the user who should be friended and the logged in user. So, you have $friend and $user entities to work with, i.e. do your checks if the friending should be allowed or not. You could either add your code that does the check before the following if-clause:

    $friend_guid = (int) get_input("friend");
    $friend = get_user($friend_guid);
        
    $user = elgg_get_logged_in_user_entity();

    ---------------

    The Check code

    --------------

    if (check allows friending) {

         here comes the original if-clause

    }

    or you could do the check withing the if-clause in the else-part:

    } else {

        DO CHECK HERE and if friending is allowed continue with

        try {

    }

    The second option of adding the check within the existing if-clause would perform the check only if the other requirements are already met (user entities are available, users are not already friends and there's no former friend request).

  • the problem is that i added code in order to print the values into the database in a new table

    $friend_guid = (int) get_input("friend");
    $friend = get_user($friend_guid);
        
    $user = elgg_get_logged_in_user_entity();

    and I found 0 each time so i thought these values are not the one i searched !

  • i did

    $sql = 'INSERT INTO mytable VALUES("$id", "$friend_guid")';

    mysql_query ($sql) or die ('Erreur SQL !'.$sql.'<br />'.mysql_error());

    $id = mysql_insert_id();

     

  • i wrote all back

    please I need help, when I try to add a friend i find a white page I don't know what's wrong in my code

    <?php
        
        //Get our data
        $friend_guid = (int) get_input("friend");
        $friend = get_user($friend_guid);
        
        $user = elgg_get_logged_in_user_entity();
        
        // =====================Check code ===============
        /* in the database, the table metadata contains entity_guid and the gender match with name_id = 9
        value_id = 27 => woman / 32 => man */
        $gender_entry = 9
        
        $gender_user = $bdd->prepare('SELECT value_id FROM Social_metadata WHERE entity_guid = :user_id AND owner_guid = : user_id AND name_id = :gender_entry');
        $gender_user->execute(array('user_id' => $user, 'gender_entry' => $gender_entry));
        
        $gender_friend = $bdd->prepare('SELECT value_id FROM Social_metadata WHERE entity_guid = :friend_id AND owner_guid = : friend_id AND name_id = :gender_entry');
        $gender_friend->execute(array('friend_id' => $friend, 'gender_entry' => $gender_entry));
        // =====================End check  ===============
        // ---------------------If Else    ---------------
        if ($gender_user == $gender_friend)
            {$errors = false}
            else { $errors = true};
        // ---------------------End ifelse ---------------
        $errors = false;
        
        //Now we need to attempt to create the relationship
        if(empty($user) || empty($friend)) {
            $errors = true;
            register_error(elgg_echo("friend_request:add:failure"));
        } else {
            //New for v1.1 - If the other user is already a friend (fan) of this user we should auto-approve the friend request...
            if(check_entity_relationship($friend->getGUID(), "friend", $user->getGUID())) {
                try {
                    if(isset($CONFIG->events["create"]["friend"])) {
                        $oldEventHander = $CONFIG->events["create"]["friend"];
                        $CONFIG->events["create"]["friend"] = array();            //Removes any event handlers
                    }
                    
                    $user->addFriend($friend->getGUID());
                    system_message(elgg_echo("friends:add:successful", array($friend->name)));
                    
                    if(isset($CONFIG->events["create"]["friend"])) {
                        $CONFIG->events["create"]["friend"] = $oldEventHander;
                    }
                    
                    forward(REFERER);
                } catch (Exception $e) {
                    register_error(elgg_echo("friends:add:failure", array($friend->name)));
                    $errors = true;
                }
            } elseif(check_entity_relationship($friend->getGUID(), "friendrequest", $user->getGUID())){
                // Check if your potential friend already invited you, if so make friends
                if(remove_entity_relationship($friend->getGUID(), "friendrequest", $user->getGUID())){
                    if(isset($CONFIG->events["create"]["friend"])) {
                        $oldEventHander = $CONFIG->events["create"]["friend"];
                        $CONFIG->events["create"]["friend"] = array();            //Removes any event handlers
                    }
                    
                    $user->addFriend($friend->getGUID());
                    $friend->addFriend($user->getGUID());            //Friends mean reciprical...
                    
                    if(isset($CONFIG->events["create"]["friend"])) {
                        $CONFIG->events["create"]["friend"] = $oldEventHander;
                    }
                    
                    system_message(elgg_echo("friend_request:approve:successful", array($friend->name)));
                    // add to river
                    add_to_river("river/relationship/friend/create", "friend", $user->getGUID(), $friend->getGUID());
                    add_to_river("river/relationship/friend/create", "friend", $friend->getGUID(), $user->getGUID());
                    
                    forward(REFERER);
                } else {
                    register_error(elgg_echo("friend_request:approve:fail", array($friend->name)));
                }
            } else {
                try {
                    $result = add_entity_relationship($user->getGUID(), "friendrequest", $friend->getGUID());
                    if($result == false) {
                        $errors = true;
                        register_error(elgg_echo("friend_request:add:exists", array($friend->name)));
                    }
                } catch(Exception $e) {    //register_error calls insert_data which CAN raise Exceptions.
                    $errors = true;
                    register_error(elgg_echo("friend_request:add:exists", array($friend->name)));
                }
            }
        }
        
        if(!$errors) {
            system_message(elgg_echo("friend_request:add:successful", array($friend->name)));
        }
        
        forward(REFERER);

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking