Calculate average of a specific field

Permalink Browser Info Environment
Hi,

I was wondering if there is a known way to calculate the average of all the answers to a specific question within a form and display it through Data Display.
Use case: A form can be filled and you are, among others, asked to rate a service on a scale from 1 to 5. I'd like to be able to not only list all the records and a specific record but also the aggregate rating people submitted through the form.

I know I might have to modify the core of the package or add a function myself, but if anyone already has done something similar or if I have overlooked a built in function that could help me here I'd be very happy to be pointed to it.

Thanks!

Type: Discussion
Status: New
mckoenig
View Replies:
mckoenig replied on at Permalink Reply
mckoenig
If anyone is interested, I created this function in the blocks/sixeightdatadisplay/controller.php to solve my problem:

function calculateAverageRating($fullAnswerSet, $match) {
      $ratingSum = 0;
      $numberOfRatings = 0;
      $formID = $fullAnswerSet[0]->fID;
      $db = Loader::db();
      $fieldID = $db->getRow("SELECT ffID FROM sixeightformsFields WHERE fID = ? AND label = ?", array($formID, $match));
      if(count($fieldID) > 0){
         $fieldID = $fieldID['ffID'];
      } else {
         return false;
      }
      $approvedAnswerSets = $db->Execute("SELECT asID FROM sixeightformsAnswerSets WHERE fID = ? AND isApproved = 1 AND isDeleted = 0", array($formID));
      $counter = 0;
      foreach($approvedAnswerSets as $answerSet){
         $rating = $db->getRow("SELECT value FROM sixeightformsAnswers WHERE asID = ? AND ffID = ?", array($answerSet['asID'], $fieldID));


Should be pretty self explanatory. If questions arise, feel free to reply or sent me a message.

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.