You are here

function opigno_statistics_app_query_class_total_number_of_page_view in Opigno Statistics App 7

Query total number of page view for a class (cached for 1 day)

Parameters

int $course_nid:

int $month_year:

boolean $filter_month:

Return value

array

1 call to opigno_statistics_app_query_class_total_number_of_page_view()
opigno_statistics_app_present_class_total_number_of_page_view in includes/group/class/presenters.inc
Present class total number of page view

File

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

Code

function opigno_statistics_app_query_class_total_number_of_page_view($class_nid, $month_year, $filter_month) {
  $cache_key = __FUNCTION__ . ':' . $class_nid . ':' . $month_year . ':' . $filter_month;
  $cached_object = cache_get($cache_key);
  if ($cached_object) {
    $total_number_of_page_view = $cached_object->data;
  }
  else {
    $total_number_of_page_view = array();

    // If the filter is for the current month, we use the accesslog table (because the accesslog is flushed every month)
    //   Else, we use the opigno_statistics_group table
    if ($filter_month && date('Y-m') == date('Y-m', $month_year)) {

      // First, get all the nodes that are in the courses that are in the current class
      $all_child_nids = opigno_statistics_app_query_class_child_nids($class_nid);
      if (empty($all_child_nids)) {
        return array();
      }

      // Construct the node ID filter
      $node_id_filter = '';
      foreach ($all_child_nids as $nid_to_filter) {
        $node_id_filter .= "path LIKE 'node/" . $nid_to_filter . "%' OR ";
      }
      $node_id_filter = substr($node_id_filter, 0, -4);

      // Then, get the number of pages view by the users of the class
      $nbr_pages_query = db_query("\n          SELECT DATE_FORMAT(FROM_UNIXTIME(timestamp),'%Y-%m-%d') as day, COUNT(*) as value\n          FROM {accesslog} a\n          WHERE (" . $node_id_filter . ") AND a.uid IN (\n            SELECT DISTINCT uid\n            FROM {opigno_statistics_user_group} ug\n            WHERE group_nid = :group_nid\n          )\n          AND DATE_FORMAT(FROM_UNIXTIME(a.timestamp),'%m-%Y') = DATE_FORMAT(FROM_UNIXTIME(:timestamp),'%m-%Y')\n          GROUP BY day\n        ", array(
        ':group_nid' => $class_nid,
        ':timestamp' => $month_year,
      ));
    }
    else {
      $filter_date_format = $filter_month ? '%m-%Y' : '%Y';
      $nbr_pages_query = db_query("\n          SELECT DATE_FORMAT(FROM_UNIXTIME(month_year),'%Y-%m-%d') as day, page_views as value\n          FROM {opigno_statistics_group}\n          WHERE DATE_FORMAT(FROM_UNIXTIME(month_year), '" . $filter_date_format . "') = DATE_FORMAT(FROM_UNIXTIME(:month_year), '" . $filter_date_format . "')\n          AND group_nid = :group_nid\n        ", array(
        ':group_nid' => $class_nid,
        ':month_year' => $month_year,
      ));
    }
    while ($record = $nbr_pages_query
      ->fetchAssoc()) {
      $total_number_of_page_view[] = $record;
    }
    cache_set($cache_key, $total_number_of_page_view, 'cache', time() + 7200);

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