Index: nodereview_node_nodereview.inc =================================================================== --- nodereview_node_nodereview.inc (revision 51) +++ nodereview_node_nodereview.inc (working copy) @@ -176,6 +176,12 @@ $additions->reviews[$record->aid] = $record; } + // Add the overall score as well. + $additions->reviews['overall'] = (object)array( + 'score' => $votes['vote']->value, + 'tag' => 'vote', + ); + return $additions; } @@ -207,6 +213,7 @@ global $user; $votes = array(); + $scores = array(); /* $result = db_query("SELECT na.aid, na.tag @@ -218,20 +225,52 @@ } */ - $axes = nodereview_list_axes(db_result(db_query("SELECT type FROM {node} WHERE nid=%d", $node->reviewed_nid))); + // Get the list of axes. + $axes = nodereview_list_axes(db_result(db_query( + "SELECT type FROM {node} WHERE nid=%d", + $node->reviewed_nid + ))); foreach ($node->reviews as $aid => $review) { // Save the text review - db_query("INSERT INTO {nodereview_reviews} (nid, aid, review) VALUES (%d, %d, '%s')", $node->nid, $aid, $review['review']); + db_query("INSERT INTO {nodereview_reviews} (nid, aid, review) + VALUES (%d, %d, '%s')", $node->nid, $aid, $review['review']); // And use the votingapi to save the score $votes[] = (object)array('value'=>$review['score'], 'tag'=>$axes[$aid]); + + // Keep a copy for later. We'll need all of the scores to calculate an + // overall score for this review. + $scores[] = $review['score']; } + // Set an overall score for this review. + $votes[] = (object)array( + 'value' => _nodereview_calculate_overall_score($scores), + 'tag' => 'vote', + ); + votingapi_set_vote('node', $node->reviewed_nid, $votes, $user->uid); } /** + * Calculate the overall score for a review. This is based on a mathematical + * function of all scores. + * + * @param $scores + * An array containing all of the scores for the current review. + * + * @return + * The overall score for the review. + */ +function _nodereview_calculate_overall_score($scores) { + + // Ideally, this formula would be configurable from within the module's + // settings page. For now, simply take the mean. + return (array_sum($scores) / count($scores)); +} + +/** * Implementation of hook_delete(). * */ @@ -252,10 +291,16 @@ function nodereview_view(&$node, $teaser = FALSE, $page = FALSE) { //$node = node_prepare($node, $teaser); + // Make sure that the review text we're displaying is valid. $aids = array_keys($node->reviews); $num_aids = sizeof($aids); for ($i=0; $i < $num_aids; $i++) { - $node->reviews[$aids[$i]]->review = check_markup($node->reviews[$aids[$i]]->review); + if ($aids[$i] == 'overall') { + continue; // Skip the overall score since there is no text. + } + $node->reviews[$aids[$i]]->review = check_markup( + $node->reviews[$aids[$i]]->review + ); } if ($teaser) { $node->teaser = theme('nodereview_teaser', $node); @@ -270,10 +315,24 @@ function theme_nodereview_body($node) { $output = ''; foreach ($node->reviews as $review) { - $title = $review->tag; - $title .= ': ' . $review->score/10 . '/10'; - $output .= theme('box', $title, $review->review); + // Skip the overall score for now; we'll add it at the end. + if ($review->tag == 'vote') { + $overall = $review; + } + else { + $title = $review->tag; + $title .= ': ' . $review->score/10 . '/10'; + $output .= theme('box', $title, $review->review); + } } + + // End with the overall score. + $output .= theme( + 'box', + 'Overall: ' . number_format($overall->score/10, 2), + '' + ); + return $output; } @@ -288,14 +347,23 @@ } foreach ($node->reviews as $review) { - $row = array(); - $row[] = $review->tag; - $row[] = $review->score/10 . '/10'; - $review_text = truncate_utf8($review->review, 50, TRUE, TRUE); - $row[] = substr($review_text, 0, strpos($review_text, "\n")); - $rows[] = $row; + // Skip the overall score for now; we'll add it at the end. + if ($review->tag == 'vote') { + $overall = $review; + } + else { + $row = array(); + $row[] = $review->tag; + $row[] = $review->score/10 . '/10'; + $review_text = truncate_utf8($review->review, 50, TRUE, TRUE); + $row[] = substr($review_text, 0, strpos($review_text, "\n")); + $rows[] = $row; + } } + // End with the overall score. + $rows[] = array('Overall', number_format($overall->score/10, 2)); + $output = theme('table', $header, $rows, array('class'=>'review-teaser')); return $output; }