You are here

function opigno_statistics_app_query_class_course_progress_percentage in Opigno Statistics App 7

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

Parameters

int $class_nid:

int $month_year:

boolean $filter_month:

Return value

int

1 call to opigno_statistics_app_query_class_course_progress_percentage()
opigno_statistics_app_present_class_general_statistics in includes/group/class/presenters.inc
Present class general statistics

File

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

Code

function opigno_statistics_app_query_class_course_progress_percentage($class_nid, $month_year, $filter_month) {
  $cache_key = __FUNCTION__ . ':' . $class_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 = og.group_nid
                          )) FROM {opigno_statistics_group} og
                          INNER JOIN {opigno_statistics_user_course} uc
                          ON og.group_nid = uc.course_nid
                          WHERE uc.uid IN (
                            SELECT uid FROM {opigno_statistics_user_group} ug
                            WHERE ug.group_nid = :group_nid
                          )
                          AND og.group_type = :type
                          AND month_year <= :month_year', array(
        ':type' => 'course',
        ':group_nid' => $class_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 = og.group_nid
                          )) FROM {opigno_statistics_group} og
                          INNER JOIN {opigno_statistics_user_course} uc
                          ON og.group_nid = uc.course_nid
                          WHERE uc.uid IN (
                            SELECT uid FROM {opigno_statistics_user_group} ug
                            WHERE ug.group_nid = :group_nid
                          )
                          AND og.group_type = :type
                          AND EXTRACT(YEAR FROM FROM_UNIXTIME(month_year)) <= :year', array(
        ':type' => 'course',
        ':group_nid' => $class_nid,
        ':year' => $year,
      ))
        ->fetchCol();
    }
    $percentage = $result[0];
    cache_set($cache_key, $percentage, 'cache', time() + 7200);

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