VotingAPI overwrites old values
Equ - August 17, 2009 - 23:29
| Project: | Voting API |
| Version: | 6.x-2.x-dev |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
Guys, need your help again =)
I was trying to customize nodereview module and ran into a problem. Voting API overwrites old values, when a new review is being added. Originally, nodereview allows you to add one review per node, but I need to let my users write more than one review per node with different votingapi values (they write reviews on the same companies (= same node) every week).
Here is the code from nodereview:
<?php
function nodereview_load($node) {
// Get the basic information from the nodereview node table
$additions = db_fetch_object(db_query('SELECT reviewed_nid FROM {nodereview} WHERE nid = %d', $node->nid));
// And now get the actual review info. The numeric scores are stored with voteapi,
// while textual reviews are in our own auxiliary table. We need to merge them properly
$criteria = array(
'content_type' => 'node',
'content_id' => $additions->reviewed_nid,
'uid' => $node->uid,
);
$votes_obj = votingapi_select_votes($criteria);
// $votes_obj = votingapi_get_user_votes('node', $additions->reviewed_nid, $node->uid);
$votes = array();
foreach ($votes_obj as $vote) {
$votes[$vote['tag']] = $vote;
}
$additions->reviews = array();
$result = db_query('SELECT nr.aid, tag, review FROM {nodereview_reviews} nr INNER JOIN {nodereview_axes} na ON nr.aid=na.aid WHERE nid = %d ORDER BY na.weight, na.tag', $node->nid);
while ($record = db_fetch_object($result)) {
// Add in the numeric scores
$record->score = $votes[$record->tag]['value'];
$additions->reviews[$record->aid] = (array)$record;
}
return $additions;
}
/**
* Implementation of hook_insert().
*
*/
function nodereview_insert($node) {
db_query("INSERT INTO {nodereview} (nid, reviewed_nid) VALUES (%d, %d)", $node->nid, $node->reviewed_nid);
nodereview_save_reviews($node);
}
/**
* Implementation of hook_update().
*
*/
function nodereview_update($node) {
db_query("UPDATE {nodereview} SET reviewed_nid=%d WHERE nid = %d", $node->reviewed_nid, $node->nid);
// Delete and rebuild the textual reviews, but the votingapi takes care of overwriting old values
db_query("DELETE FROM {nodereview_reviews} WHERE nid=%d", $node->nid);
nodereview_save_reviews($node);
}
?>In the code comments it says that votingapi automatically overwrites old values and it really does. But it overwrites values for all reviews from current user to that certain node. So if the user adds two or more reviews to the certain node, they all get the same value (votingapi doesn't insert any new values for the new reviews, but updates the first value).
Now the question is how to *not* overwrite old values?
P.S. sorry for such a big text.

#1
Ok, I've found the problem. It's here:
<?php// Handle clearing out old votes if they exist.
if (!isset($criteria)) {
// If the calling function didn't explicitly set criteria for vote deletion,
// build up the delete queries here.
foreach ($votes as $vote) {
$tmp = votingapi_current_user_identifier();
$tmp += $vote;
votingapi_delete_votes(votingapi_select_votes($tmp));
}
}
?>
If I comment out
votingapi_delete_votesstuff then everything works fine for me. But is there any other way how I can achieve the same effect without hacking the votingapi module?Thanks