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 :)

  • I don't know what the prepare and execute methods exactly do, but I suspect the problem might be that you use the $user and $friend Elgg user entities while you most likely need to use the corresponding GUIDs instead. For the friend you already have the GUID in $friend_guid and the logged-in user GUID can be found out by using elgg_get_logged_in_user_guid().

    Also the check of gender and setting the state of $error depending on the gender match/mismatch is rather pointless, if you set $error = false after the if-clause again. You would need to remove this line. Also, in case of $error == true I assume you don't want the following if-clause to be processed anyway at all, so you would need to check the state of $error already before this if-clause (by embedding the existing if-clause within another if-clause that has the state of $error as condition).

    I don't know why you would need to save the gender in an extra database table. This might be for valid reasons. But wouldn't it be possible to add the gender info simply within a metadata field of the user entities? Then there wouldn't be such complicated database queries necessary but you could simply compare $user->gender with $friend->gender.

  • Hi,

    i did some test and it appears at least one problem comes from the database request. How do we do in elgg to ask the database a value? I know it in general but each time i try in elgg it fails.

    About the gender, i used profile manager to ask it during registering. Profile manager choosed to settle this into metadata. It uses the guid, the field id = 9 and gives a number 27 or 32

    in meta string i see 27 = women and 32 = men.

    So i didn't do any change.

    2nd, about the prepare and exec, it is a way to secure the database just in case one of the $ came from a form, juust to prevent anyone to put sql queries into the fields. It is written a proper way, i took it from a good website.

    3rd, I corrected now about guid and $errors =false

    I still need to correct the database querry (i tried any query i could without the rest of the code and it made it fail)

     

  • If it's a profile field I think you don't need any complicated database queries to retrieve the data. You should also not rely on the ids but use the profile name label (=metadata name) instead (the ids will not be fixed on different websites anyway). Let's assume the profile field label is "gender" you should be able to retrieve the value saved in a user's profile by

    $gender = $user->gender;

    and for the friend

    $gender_friend = $friend->gender;

    It depends on the type of the profile field what exactly is saved (string, integer?) so you need to keep this in mind when making the comparison. Again, I don't think there are ids saved in the database but the metadata value will be rather a string (for example "male" / "female") or maybe a number (0 vs. 1).

    Generally, you can use the elgg_get_*() functions of the Elgg API to retrieve metadata / entities etc. The "*" means there are a great variety of getter functions available and you would need to choose the functions that fits your needs depending on what input data you have and what you want to retrieve. Check out http://reference.elgg.org for all info about the functions and their parameters.

  • I am the perfect noob.

    I went in body.php and tried to call the metadata the label is "Genre" and the value is a string.

    But the only thing i could make work was

    $test = elgg_get_metadata_from_id (28);

    and I got a number, 27 means woman.

    I am lost

  • You should not be using direct queries like that, especially for this case, this is an indication that you do not understand the elgg data model.

    Elgg can handle this for you in a much simpler way

    $friend->gender_entry

    is all you need to do to retrieve the metadata associated with the name 'gender_entry'

    To set that metadata you simply declare it

    $friend->gender_entry = 'male';

  • I thank you SO much, I can now get it so easily.

    Please is there an easy doc to read? As i'm not english and I find the huge doc painfull to read.

    Now I just need to fix the if - else part.

    I made it work actually but I just need to set a special message telling the user that the friendship is not allowed instead of the "servor error"

    here is my code

    //Get our data
        $friend_guid = (int) get_input("friend");
        $friend = get_user($friend_guid);
        
        $user = elgg_get_logged_in_user_entity();
        
        // =====================Check code ===============
        $user_gender = $user->Genre;
        $friend_gender = $friend->Genre;
        // =====================End check  ===============
        
        //Now we need to attempt to create the relationship
                // I add one condition under
        if(empty($user) || empty($friend) || $user_gender!=$friend_gender) {
            $errors = true;
            register_error(elgg_echo("friend_request:add:failure"));
        } else {

  • i tried

    register_error(elgg_echo("friend_request:add:nofriendship"));

    and i added

    'friend_request:add:nofriendship' => "Friendship are not allowed between opposite gender",

    in en.php but it spawn "friend_request:add:nofriendship" instead of the message

  • I found this method but it is heavy and ugly

    <?php
        
        //Get our data
        $friend_guid = (int) get_input("friend");
        $friend = get_user($friend_guid);
        
        $user = elgg_get_logged_in_user_entity();
        
        // =====================Check code ===============
        $user_gender = $user->Genre;
        $friend_gender = $friend->Genre;
        // =====================End check  ===============
        
        //Now we need to attempt to create the relationship
                // I add one condition under
        if(empty($user) || empty($friend) ) {
            $errors = true;
            register_error(elgg_echo("friend_request:add:failure"));

    //----------------------------
        } else if ($user_gender!=$friend_gender)
                {$errors = true;
                $english = array(   
                'friend_request:add:nomix' => 'sorry, %s is %s nd %s %s',   
        );
     
        add_translation("en",$english);
                register_error(elgg_echo("friend_request:add:nomix", array($friend->name, $friend->Genre, $user->name, $user->Genre)));
                }
        else {

  • register_error(elgg_echo("friend_request:add:nofriendship"));

    and i added

    'friend_request:add:nofriendship' => "Friendship are not allowed between opposite gender",

    in en.php but it spawn "friend_request:add:nofriendship" instead of the message

     

    This should work, did you clear caches?  Language files are cached so if you didn't clear it or turn caching off it wouldn't know about your changes.

  • Indeed, it works fine :D

    I want to thank you all for ur support, I finally accomplished what I wanted to due to your help :)

    regards,

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