function opigno_statistics_app_query_course_total_number_of_page_view in Opigno Statistics App 7
Query total number of page view for a course (cached for 1 day)
Parameters
int $course_nid:
int $month_year:
boolean $filter_month:
Return value
array
1 call to opigno_statistics_app_query_course_total_number_of_page_view()
- opigno_statistics_app_present_course_total_number_of_page_view in includes/
group/ course/ presenters.inc - Present course total number of page view
File
- includes/
group/ course/ queries.inc, line 100
Code
function opigno_statistics_app_query_course_total_number_of_page_view($course_nid, $month_year, $filter_month) {
$cache_key = __FUNCTION__ . ':' . $course_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 course
$all_child_query = db_query("\n SELECT etid\n FROM {og_membership}\n WHERE group_type = 'node'\n AND entity_type = 'node'\n AND gid = :course_id\n ", array(
':course_id' => $course_nid,
));
$all_child_nids = array_unique($all_child_query
->fetchCol());
$all_child_nids[] = $course_nid;
// 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
$result = 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' => $course_nid,
':timestamp' => $month_year,
));
}
else {
$filter_date_format = $filter_month ? '%m-%Y' : '%Y';
$result = 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' => $course_nid,
':month_year' => $month_year,
));
}
while ($record = $result
->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;
}