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
(This page will not be migrated to new devdocs)
 
(11 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Template:WillNotMigrate}}
{{obsolete}}
See [[Migrating logging calls in plugins]], [[Logging 2]] and [[Events API]] for more up-to-date information.
==Overview==
==Overview==


Line 10: Line 15:


Following are the functions that constitute the basic log API for Moodle.
Following are the functions that constitute the basic log API for Moodle.
<code>
<syntaxhighlight lang="php">
  add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0)
  add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0)
  user_accesstime_log($courseid=0)
  user_accesstime_log($courseid=0)
Line 16: Line 21:
  get_logs_usercourse($userid, $courseid, $coursestart)
  get_logs_usercourse($userid, $courseid, $coursestart)
  get_logs_userday($userid, $courseid, $daystart)
  get_logs_userday($userid, $courseid, $daystart)
</code>
</syntaxhighlight>
The basic working of these functions can be categorized in two categories:-
The basic working of these functions can be categorized in two categories:-
# Adding data to logs
# Adding data to logs
Line 23: Line 28:


===Adding data to Logs===
===Adding data to Logs===
In Moodle basically we have two functions that take care of adding data to the logs table:-
In Moodle basically we have two functions that take care of adding data to the logs table :-
<code>
<syntaxhighlight lang="php">
  add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0)
  add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0)
  user_accesstime_log($courseid=0)
  user_accesstime_log($courseid=0)
</code>
</syntaxhighlight>


==== add_to_log() ====
==== 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 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 remember that this function is more "Action" oriented 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:-
This is a simple example:-
<code>
<syntaxhighlight lang="php">
  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>
</syntaxhighlight>
 
This system has been deprecated in Moodle 2.7. See [[Migrating logging calls in plugins]].


==== user_accesstime_log() ====
==== 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.
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 :-
A simple example can be :-
<code>
<syntaxhighlight lang="php">
  $courseid = $course->id;
  $courseid = $course->id;
  user_accesstime_log($courseid);
  user_accesstime_log($courseid);
</code>
</syntaxhighlight>


===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:-
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>
<syntaxhighlight lang="php">
  get_logs($select, array $params=null, $order='l.time DESC', $limitfrom='', $limitnum='', &$totalcount)
  get_logs($select, array $params=null, $order='l.time DESC', $limitfrom='', $limitnum='', &$totalcount)
  get_logs_usercourse($userid, $courseid, $coursestart)
  get_logs_usercourse($userid, $courseid, $coursestart)
  get_logs_userday($userid, $courseid, $daystart)
  get_logs_userday($userid, $courseid, $daystart)
</code>
</syntaxhighlight>
====get_logs()====
====get_logs()====
This is a generic function to fetch data based on a given SQL condition.
This is a generic function to fetch data based on a given SQL condition.
<code>
<syntaxhighlight lang="php">
  $params = array();
  $params = array();
  $selector = "l.course = :courseid";
  $selector = "l.course = :courseid";
  $params['courseid'] = $course->id;
  $params['courseid'] = $course->id;
  $logs = get_logs($selector, $params, $order, $limitfrom, $limitnum, $totalcount);
  $logs = get_logs($selector, $params, $order, $limitfrom, $limitnum, $totalcount);
</code>
</syntaxhighlight>


====get_logs_usercourse()====
====get_logs_usercourse()====
get_logs_usercourse() returns logs data for a given specific course and user.
get_logs_usercourse() returns logs data for a given specific course and user.
<code>
<syntaxhighlight lang="php">
  $coursestart = usergetmidnight($course->startdate);
  $coursestart = usergetmidnight($course->startdate);
  $logs = get_logs_usercourse($user->id, $courseselect, $coursestart);
  $logs = get_logs_usercourse($user->id, $courseselect, $coursestart);
</code>
</syntaxhighlight>
====get_logs_userday()====
====get_logs_userday()====
This function can return logs specific to a given user and course for a given date.
This function can return logs specific to a given user and course for a given date.
<code>
<syntaxhighlight lang="php">
  $daystart = usergetmidnight(time());
  $daystart = usergetmidnight(time());
  $logs = get_logs_userday($user->id, $courseselect, $daystart);
  $logs = get_logs_userday($user->id, $courseselect, $daystart);
</code>
</syntaxhighlight>
 
==Mod/*/db/log.php Files==
==Mod/*/db/log.php Files==
These files specify what information should be associated with a log entry. To understand the working of these files better its necessary to understand the structure of database table 'prefix_log_display'.
These files specify what information should be associated with a log entry. To understand the working of these files better its necessary to understand the structure of database table 'prefix_log_display'.


{| class="nicetable"
{| class="wikitable"
|-
|-
! Field
! Field
Line 117: Line 125:
===Example===
===Example===
This a simple example of what contents of log.php files can be.
This a simple example of what contents of log.php files can be.
<code>
<syntaxhighlight lang="php">
  $logs = array(
  $logs = array(
     array('module'=>'page', 'action'=>'view', 'mtable'=>'page', 'field'=>'name'),
     array('module'=>'page', 'action'=>'view', 'mtable'=>'page', 'field'=>'name'),
Line 124: Line 132:
     array('module'=>'page', 'action'=>'add', 'mtable'=>'page', 'field'=>'name'),
     array('module'=>'page', 'action'=>'add', 'mtable'=>'page', 'field'=>'name'),
  );
  );
</code>
</syntaxhighlight>


==See Also==
==See Also==

Latest revision as of 11:43, 25 August 2023


Warning: This page is no longer in use. The information contained on the page should NOT be seen as relevant or reliable.



Warning: This page is no longer in use. The information contained on the page should NOT be seen as relevant or reliable.

See Migrating logging calls in plugins, Logging 2 and Events API for more up-to-date information.

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 remember that this function is more "Action" oriented 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);

This system has been deprecated in Moodle 2.7. See Migrating logging calls in plugins.

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);

Mod/*/db/log.php Files

These files specify what information should be associated with a log entry. To understand the working of these files better its necessary to understand the structure of database table 'prefix_log_display'.

Field Type Default Info
id bigint(10) auto-incrementing The unique ID for this comment.
module varchar(20) who wrote this comment
action varchar(40) The action associated. Ex- view, delete, update etc
mtable varchar(30) The name of the database table from which info will be fetched to associate with the log entry
field varchar(200) Which filed needs to be fetched from mtable
component varchar(100) The component (Frankenstyle name) associated with the entry

When a log needs to be displayed to a front end user, this table helps us determine what information needs to be displayed to the user along with the log entry, so that they can make perfect sense out of the report. Whenever we have to display a log entry for a given module and action say $module and $action, the information that's shown along with the log is the value of the 'field' column fetched from 'mtable' corresponding to the given $module and $action. Now Mod/*/db/log.php files are used to create this association by adding entries to the log_display table during the install or upgrade of a module.

Example

This a simple example of what contents of log.php files can be.

 $logs = array(
    array('module'=>'page', 'action'=>'view', 'mtable'=>'page', 'field'=>'name'),
    array('module'=>'page', 'action'=>'view all', 'mtable'=>'page', 'field'=>'name'),
    array('module'=>'page', 'action'=>'update', 'mtable'=>'page', 'field'=>'name'),
    array('module'=>'page', 'action'=>'add', 'mtable'=>'page', 'field'=>'name'),
 );

See Also