You are here

function opigno_statistics_app_query_course_students_results in Opigno Statistics App 7

Query students results statistics for a course (cached for 1 day)

Parameters

int $course_nid:

Return value

array

2 calls to opigno_statistics_app_query_course_students_results()
opigno_statistics_app_present_course_students_results in includes/group/course/presenters.inc
Present student results statistics for a course
opigno_statistics_app_query_user_courses_results in includes/user/queries.inc
Query courses results statistics for a user (cached for 1 day)

File

includes/group/course/queries.inc, line 256

Code

function opigno_statistics_app_query_course_students_results($course_nid) {
  $cache_key = __FUNCTION__ . ':' . $course_nid;
  $cached_object = cache_get($cache_key);
  if ($cached_object) {
    $students_results = $cached_object->data;
  }
  else {
    $students_results = array();
    $result = db_query("\n      SELECT uc.username as student_name, j1.page_views as number_of_interactions, uc.score, uc.status, uc.uid\n      FROM {opigno_statistics_user_course} uc\n      INNER JOIN (\n        SELECT SUM(page_views) as page_views, uid\n        FROM {opigno_statistics_user_group} ug\n        WHERE group_nid = :course_nid\n        GROUP BY uid\n      ) j1 ON uc.uid = j1.uid\n      WHERE course_nid = :course_nid\n    ", array(
      ':course_nid' => $course_nid,
    ));
    $total_number_of_interactions = 0;
    $total_score = 0;
    while ($record = $result
      ->fetchAssoc()) {
      $students_results[] = $record;
      $total_number_of_interactions += $record['number_of_interactions'];
      $total_score += $record['score'];
    }
    $avg_number_of_interactions = count($students_results) > 0 ? $total_number_of_interactions / count($students_results) : 0;
    $avg_score = count($students_results) > 0 ? $total_score / count($students_results) : 0;
    foreach ($students_results as &$student_result) {
      $student_result['avg_number_of_interactions'] = round($avg_number_of_interactions, 0);
      $student_result['avg_score'] = round($avg_score, 0);
    }
    cache_set($cache_key, $students_results, 'cache', time() + 7200);

    // 7200s = 2h cache
  }
  return $students_results;
}