/** * getAverageRating */ public function getAverageRating($survey_id, $relation_subject = false) { if (!is_numeric($survey_id)) { return false; } require_once 'models/education/education_survey_entry.php'; $SurveyEntry = new education_survey_entry(); $average_rating = $SurveyEntry->getWeightedMean($survey_id, $relation_subject); if (is_numeric($average_rating)) { return $average_rating; } else { return 'n/a'; } }
/** * calculateSurveyAverageRating */ public function calculateSurveyAverageRating($survey_detail, $relation_subject = fals) { if (!is_array($survey_detail)) { return false; } if (!is_array($survey_detail['question_list'])) { return false; } $survey_total_x = 0; $survey_total_sum = 0; foreach ($survey_detail['question_list'] as $k => $item) { $question_total_x = 0; $question_total_sum = 0; if (($item['type'] == 'radio' || $item['type'] == 'select') && is_array($item['answer_list'])) { foreach ($item['answer_list'] as $answer_k => $answer_item) { $question_total_x = $question_total_x + $answer_item['usage_count'] * $answer_item['points']; $question_total_sum = $question_total_sum + $answer_item['usage_count']; } if ($question_total_sum > 0) { $survey_detail['question_list'][$k]['average_rating'] = $question_total_x / $question_total_sum; } else { $survey_detail['question_list'][$k]['average_rating'] = 'n/a'; } } else { $survey_detail['question_list'][$k]['average_rating'] = 'n/a'; } $survey_total_x = $survey_total_x + $question_total_x; $survey_total_sum = $survey_total_sum + $question_total_sum; } if ($survey_total_sum > 0) { $survey_detail['average_rating'] = $survey_total_x / $survey_total_sum; } else { $survey_detail['average_rating'] = 'n/a'; } /** * weighted mean rating * calculating manually at this place, but we could use education_survey_entry->getWeightedMean() */ /* Option 1: manual $weighted_mean_top = 0; $weighted_mean_bottom = 0; foreach ($survey_detail['question_list'] as $k=>$item) { if (is_numeric($item['average_rating'])) { $weighted_mean_top = $weighted_mean_top + $item['weight'] * $item['average_rating']; $weighted_mean_bottom = $weighted_mean_bottom + $item['weight']; } } if ($weighted_mean_bottom > 0) { $survey_detail['weighted_mean'] = $weighted_mean_top / $weighted_mean_bottom; } else { $survey_detail['weighted_mean'] = 'n/a'; }*/ // Option 2: weighted mean calculated from education_survey_entry->getWeightedMean() require_once 'models/education/education_survey_entry.php'; $SurveyEntry = new education_survey_entry(); $weighted_mean = $SurveyEntry->getWeightedMean($survey_detail['id'], $relation_subject); if (is_numeric($weighted_mean)) { $survey_detail['weighted_mean'] = $weighted_mean; } else { $survey_detail['weighted_mean'] = 'n/a'; } return $survey_detail; }