Blogs

Posted At : 13 May 2008 16:22 | Posted By : Alex Lloyd
Related Categories: Ning Development

I did some work recently for a Ning client, and what they wanted was, instead of having to select members to feature, they wanted an embedded widget on the front page, which just selected a random one. Significantly, it had to be someone who had filled in some of the profile details, and who had added a profile picture.



I did this by using the Advanced Members Tutorial from the Ning Developers Network. This allows you to create a search feature, using any/all of the profile questions from your network.

However, I didn't want to search for members, i wanted to always return at least one, so i added a couple of filters to the query:

$query = $query->filter("my->xg_profiles_answer_q34", '<>',"");


In this case, q34 was 'About Me', which any randomly featured member must have filled. Also, since the tutorial was written, the banned members filter has changed, here are my updated generic filters, for almost any query:

//filter out banned and pending and admin members
$query->filter('my->xg_index_status', '<>', 'pending');
$query->filter('my->xg_index_status', '<>', 'blocked');
$query->filter('my->status', '<>', 'banned');
$query->filter(XN_Filter::any(XN_Filter('title', '<>', $ownerName))); //ignore network owner
$query->filter(XN_Filter::any(XN_Filter('my->isAdmin', '<>', 'Y'))); //ignore network administrators


These, in order, filter members who are:
  • Pending User status
  • Blocked Users
  • Banned Users
  • Not the Network Owner
  • Not one of the Administrators

Note: This was for a private network, so you might not want to filter out your administrators

So, there you have your results, the query is always going to return a result, and so far it's going to return all users, who have at least answer q34

Now, onto display one, random user from the above query. This is a massive hack, as I'm not a php, nor Ning developer, this was the easiest way i could achieve the results, so please don't shout at my dumb code!

//this is used to control the conditional loop, once ResultDisplayed equals 1 the loop will stop
$this->resultDisplayed = 0;

while($this->resultDisplayed < 1){

foreach($this->result as $result) {
$this->resultUser = explode(':',$result->id);

$this->userPicture = explode('/', $result->my->thumbnailUrl);
$this->userPicture = explode('.', $this->userPicture[5]);
if($this->userPicture[1] != 'png'){ //ignore people who have a PNG for a profile pic, NING uses it for the default img
if(is_numeric($this->userPicture[0]) != true){ //ignore people who have an entirely numeric file name for a profile pic, NING uses it for the default img

if($this->resultUser[2] == rand(1,9999)){

This code above, sets resultDisplay = 0, this wont get set to 1 until all the other conditions are passed and the member details are output.

It then loops through each result and gets the ID for each user (note: this is not the NingID nor, is it the screenName var, this is a simple four digit numeric assigned by the Ning system, i dont know what it refers to, i found it by accident, but it is unique per member, and is essential for the random number generator).

It then extracts specific information from the thumbnailUrl, this is to exclude people who only have a default image (note: this doesn't pick people who have added a picture, rather it excludes anything that looks like a default image. Each user, who hasn't added an image, gets assigned a unique default image, it's always a .png, and it's always entirely numeric, but more than that it's unique. Fortunately, most users will be uploading .gifs or .jpgs mostly, with some letters in them!).

Then finally it matches the resultUser id to a random 4 digit number, then displays the query results, just like the tutorial, with a couple of additions, the first 500 characters of the 'About Me' question. I did that with the following two bits of code:

<%= truncateString($result->my->xg_profiles_answer_q34, 500) %>

Which uses the truncateString function:

function truncateString($string, $limit, $break=".", $pad='...') {
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit) return $string;

// is $break present between $limit and the end of the string?
if(false !== ($breakpoint = strpos($string, $break, $limit))) {
if($breakpoint < strlen($string) - 1) {
$string = substr($string, 0, $breakpoint) . $pad;
}
}
return $string;
}

The above function rounds down any letters from half a word at the end of the string.

Then i used parts of the HelloWorld embed tutorial to make it an embedded widget, and to show up on the features manager.




As ever, if you have any questions, comment below, and i'll try and get back to you!

Lloyd

Comments

© 1998 - 2010 TalkWebSolutions Ltd. Registered in England No. 04521175.