You are here

function opigno_statistics_app_query_course_course_progress_percentage in Opigno Statistics App 7

Retrieve course progress percentage for a course (cached for 1 day)

Parameters

int $course_nid:

int $month_year:

boolean $filter_month:

Return value

int

1 call to opigno_statistics_app_query_course_course_progress_percentage()
opigno_statistics_app_present_course_general_statistics in includes/group/course/presenters.inc
Present course general statistics

File

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

Code

function opigno_statistics_app_query_course_course_progress_percentage($course_nid, $month_year, $filter_month) {
  $cache_key = __FUNCTION__ . ':' . $course_nid . ':' . $month_year . ':' . $filter_month;
  $cached_object = cache_get($cache_key);
  if ($cached_object) {
    $percentage = $cached_object->data;
  }
  else {
    if ($filter_month) {
      $result = db_query('SELECT AVG(number_passed / (SELECT
                            COUNT(DISTINCT ug.uid) FROM {opigno_statistics_user_group} ug WHERE ug.group_nid = g.group_nid
                          ))
                          FROM {opigno_statistics_group} g
                          WHERE group_nid = :group_nid
                          AND month_year <= :month_year', array(
        ':group_nid' => $course_nid,
        ':month_year' => $month_year,
      ))
        ->fetchCol();
    }
    else {
      $year = gmdate('Y', $month_year + 86400);
      $result = db_query('SELECT AVG(number_passed / (SELECT
                            COUNT(DISTINCT ug.uid) FROM {opigno_statistics_user_group} ug WHERE ug.group_nid = g.group_nid
                          ))
                          FROM {opigno_statistics_group} g
                          WHERE group_nid = :group_nid
                          AND EXTRACT(YEAR FROM FROM_UNIXTIME(month_year)) <= :year', array(
        ':group_nid' => $course_nid,
        ':year' => $year,
      ))
        ->fetchCol();
    }
    $percentage = $result[0];
    cache_set($cache_key, $percentage, 'cache', time() + 7200);

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