Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Logging API: Difference between revisions

From MoodleDocs
No edit summary
mNo edit summary
Line 34: Line 34:
This is a simple example:-
This is a simple example:-
<code>
<code>
add_to_log($course->id, 'role', 'assign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
add_to_log($course->id, 'role', 'assign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
</code>
</code>


Line 46: Line 46:


===Fetching Logs===
===Fetching Logs===
we have three functions that can take care of all your needs to interact with the existing log data in the database. Following function can be used to effectively retrieve log data as per your needs:-
<code>
get_logs($select, array $params=null, $order='l.time DESC', $limitfrom='', $limitnum='', &$totalcount)
get_logs_usercourse($userid, $courseid, $coursestart)
get_logs_userday($userid, $courseid, $daystart)
</code>
====get_logs()====
====get_logs()====
This is a generic function to fetch data based on a given SQL condition.
<code>
$params = array();
$selector = "l.course = :courseid";
$params['courseid'] = $course->id;
$logs = get_logs($selector, $params, $order, $limitfrom, $limitnum, $totalcount);
</code>
====get_logs_usercourse()====
====get_logs_usercourse()====
get_logs_usercourse() returns logs data for a given specific course and user.
<code>
$coursestart = usergetmidnight($course->startdate);
$logs = get_logs_usercourse($user->id, $courseselect, $coursestart);
</code>
====get_logs_userday()====
====get_logs_userday()====
This function can return logs specific to a given user and course for a given date.
<code>
$daystart = usergetmidnight(time());
$logs = get_logs_userday($user->id, $courseselect, $daystart);
</cdoe>
==Mod/*/db/log.php Files==
==Mod/*/db/log.php Files==
===Example===
===Example===

Revision as of 04:24, 13 January 2012

This page is under development

Overview

The Logging API allows you to add new entries to the Moodle log and define how they get displayed in reports. Logging is an extremely important and often neglected aspect of Moodle Plugin development. All important actions such as viewing, deleting, editing etc should be logged.

File locations

The Log API is all in lib/datalib.php and is automatically included for you during the page setup.

Functions and Examples

Following are the functions that constitute the basic log API for Moodle.

add_to_log($courseid, $module, $action, $url=, $info=, $cm=0, $user=0)
user_accesstime_log($courseid=0)
get_logs($select, array $params=null, $order='l.time DESC', $limitfrom=, $limitnum=, &$totalcount)
get_logs_usercourse($userid, $courseid, $coursestart)
get_logs_userday($userid, $courseid, $daystart)

The basic working of these functions can be categorized in two categories:-

  1. Adding data to logs
  2. Fetching data from logs

Let us take a deeper look into both of these:-

Adding data to Logs

In Moodle basically we have two functions that take care of adding data to the logs table:-

add_to_log($courseid, $module, $action, $url=, $info=, $cm=0, $user=0)
user_accesstime_log($courseid=0)

add_to_log()

This function is basic core function which you should use to add all your logs to the Moodle Log table. While using this function to add data to logs, please remeber that this function is more of "Action" oriented stuff rather than being based on "webserver hits", i.e the events should be logged with enough information that this data can be effectively used to regenerate the entire flow of events and action associated with any specific user. This is a simple example:-

add_to_log($course->id, 'role', 'assign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, , $USER->id);

user_accesstime_log()

user_accesstime_log() is used to record the last access time for courses and site. This is basically called when a user vists the site or a course page. A simple example can be :-

$courseid = $course->id;
user_accesstime_log($courseid);

Fetching Logs

we have three functions that can take care of all your needs to interact with the existing log data in the database. Following function can be used to effectively retrieve log data as per your needs:-

get_logs($select, array $params=null, $order='l.time DESC', $limitfrom=, $limitnum=, &$totalcount)
get_logs_usercourse($userid, $courseid, $coursestart)
get_logs_userday($userid, $courseid, $daystart)

get_logs()

This is a generic function to fetch data based on a given SQL condition.

$params = array();
$selector = "l.course = :courseid";
$params['courseid'] = $course->id;
$logs = get_logs($selector, $params, $order, $limitfrom, $limitnum, $totalcount);

get_logs_usercourse()

get_logs_usercourse() returns logs data for a given specific course and user.

$coursestart = usergetmidnight($course->startdate);
$logs = get_logs_usercourse($user->id, $courseselect, $coursestart);

get_logs_userday()

This function can return logs specific to a given user and course for a given date.

$daystart = usergetmidnight(time());
$logs = get_logs_userday($user->id, $courseselect, $daystart);

</cdoe>

Mod/*/db/log.php Files

Example

See Also

Logs Reports