You are here

function opigno_statistics_app_query_quiz_completion_percentage in Opigno Statistics App 7

Retrieve general quiz completion percentage (cached for 1 day)

Parameters

int $month_year:

boolean $filter_month:

int $category_id:

Return value

int

1 call to opigno_statistics_app_query_quiz_completion_percentage()
opigno_statistics_app_present_general_statistics in includes/dashboard/presenters.inc
Present general statistics

File

includes/dashboard/queries.inc, line 128

Code

function opigno_statistics_app_query_quiz_completion_percentage($month_year, $filter_month, $category_id) {
  $cache_key = __FUNCTION__ . ':' . $month_year . ':' . $filter_month . ':' . $category_id;
  $cached_object = cache_get($cache_key);
  if ($cached_object) {
    $percentage = $cached_object->data;
  }
  else {
    if ($filter_month) {
      if ($category_id == '') {
        $result = db_query('SELECT AVG(status) FROM {opigno_statistics_user_course_details}
                          WHERE type = :type
                          AND DATE_FORMAT(FROM_UNIXTIME(timestamp),\'%m-%Y\') = DATE_FORMAT(FROM_UNIXTIME(:timestamp),\'%m-%Y\')', array(
          ':type' => 'quiz',
          ':timestamp' => $month_year,
        ))
          ->fetchCol();
      }
      else {
        $result = db_query('SELECT AVG(cd.status) FROM {opigno_statistics_user_course_details} cd
                          INNER JOIN {opigno_statistics_user_course} uc
                          ON cd.opigno_statistics_user_course_fk = uc.opigno_statistics_user_course_pk
                          INNER JOIN {opigno_statistics_group} g
                          ON uc.course_nid = g.group_nid
                          WHERE type = :type
                          AND DATE_FORMAT(FROM_UNIXTIME(cd.timestamp),\'%m-%Y\') = DATE_FORMAT(FROM_UNIXTIME(:timestamp),\'%m-%Y\')
                          AND g.category_taxonomy_term_id = :category_id', array(
          ':type' => 'quiz',
          ':timestamp' => $month_year,
          ':category_id' => $category_id,
        ))
          ->fetchCol();
      }
    }
    else {
      if ($category_id == '') {
        $result = db_query('SELECT AVG(status) FROM {opigno_statistics_user_course_details}
                          WHERE type = :type
                          AND DATE_FORMAT(FROM_UNIXTIME(timestamp),\'%Y\') = DATE_FORMAT(FROM_UNIXTIME(:timestamp),\'%Y\')', array(
          ':type' => 'quiz',
          ':timestamp' => $month_year,
        ))
          ->fetchCol();
      }
      else {
        $result = db_query('SELECT AVG(cd.status) FROM {opigno_statistics_user_course_details} cd
                          INNER JOIN {opigno_statistics_user_course} uc
                          ON cd.opigno_statistics_user_course_fk = uc.opigno_statistics_user_course_pk
                          INNER JOIN {opigno_statistics_group} g
                          ON uc.course_nid = g.group_nid
                          WHERE type = :type
                          AND DATE_FORMAT(FROM_UNIXTIME(cd.timestamp),\'%Y\') = DATE_FORMAT(FROM_UNIXTIME(:timestamp),\'%Y\')
                          AND g.category_taxonomy_term_id = :category_id', array(
          ':type' => 'quiz',
          ':timestamp' => $month_year,
          ':category_id' => $category_id,
        ))
          ->fetchCol();
      }
    }
    $percentage = $result[0];
    cache_set($cache_key, $percentage, 'cache', time() + 7200);

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