This module is great! I'm using it on my site http://www.preik.no/ and it's really good!
But I really need a Views filter and sort criteria for Favorite Nodes.

* Filter to only show nodes when lets say 2 or more users has added the node as a favorite.
* Sort criteria to sort by number of users having added the node as a favorite.

I'll gladly pay/donate $25 if anyone can help me with this pretty fast!

CommentFileSizeAuthor
#6 favorite_nodes.zip5.83 KBcostinius
#4 favorite_nodes.zip5.89 KBcostinius
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

CoolDreamZ’s picture

Subscribing as I would also like this functionality, especially the second bullet point (sort criteria in a View)

asund’s picture

I managed to make a list of the most favorited nodes.
I'm sure the code can be improved a lot by someone who actually knows php. :P
It is VERY slow as it is now (at least on my site with loads of content)

<?php
$list_length = 10; //Number of nodes to list
$min_faves = 7; //Minimum amount of faves to be on the list

$sql = "SELECT nid FROM {favorite_nodes}"; //Selects all favorite nodes

$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
  $sql = "SELECT * FROM {favorite_nodes} WHERE nid = $anode->nid"; //Counts number of favorites for each node
  $count = db_num_rows(db_query($sql, $nid));
  if($count>=$min_faves){
  $favnodes[$anode->nid]=array("count" => db_num_rows(db_query($sql, $nid)), "nid" => $anode->nid); //Stores data in an Array
}
}

rsort($favnodes); //Sorts nodes by number of favorites
$output = "";
$prcnt = 0;
while($prcnt<$list_length){
  $node = node_load($favnodes[$prcnt]["nid"]); //Loads node from nid
if($node){
$output .= node_view($node);
}
  $prcnt++;
}
print $output;
?>



costinius’s picture

Subscribing. I've tried to fix it myself (the 2d point) but failed so far.

costinius’s picture

Status: Active » Needs review
FileSize
5.89 KB

Hi, I've fixed the 2d problem "Sort criteria to sort by number of users having added the node as a favorite." It works fine for me.

The updated files are attached. You'll have to reinstall the module to make it work (remember to backup {favorite_nodes} table and restore it after the reinstallation of the module).

Shortly, my code creates a new table {favorite_nodes_count} which stores the data with the number of users who added the node as a favorite. Then making it work in views becomes a piece of cake. If anyone wants to add a filter (1st problem), you can easily add necessary code.

Disclaimer: It took me 15 minutes to update the module, so I can't guarantee that it's perfect. Please test it first.

kbahey’s picture

Priority: Critical » Normal
Status: Needs review » Needs work

Thanks for this.

However, including a patch will make the review much easier than the full module.

Please read about how to create a patch at http://drupal.org/patch

costinius’s picture

FileSize
5.83 KB

Ooops, found one bug already. Attached is the corrected version.

Khalid, I know I need to read that and post patches instead of full modules, sorry. I'm not that advanced at Drupal yet. Will do my best next time :)

kbahey’s picture

Status: Needs work » Needs review

I am divided about this patch. One one hand, it solves the issue of "Count". But from an architecture point of view, it introduces extra (summary) table, which is denormalization. It solves the problem now, but becomes a management and maintenance burden, and introduces the possibility of inconsistencies later.

So, I like to get some feedback on this from an architecture/design point of view.

Hence, I created a cleaned up patch from costinius' .zip file for those who want to review it.

costinius’s picture

Khalid, thanks for creating the patch. Just 2 comments:

1. I didn't find a better way how to solve the problem. If anyone has other ideas, please suggest.
2. The patch updates the summary table only when someone adds/removes a node to/from the favorites. Afterwards the views module retrieves the data directly from this table, which makes this operation much faster than calculating the # of users each time a view is loaded in the browser window. It becomes even more relevant when the view is used often by a big number of users.

SpikeX’s picture

Subscribing.

I need a way to sort by favorite count. I tried to add a Sort views handler to the view API code, but it didn't work right.