Note:

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

Admin reports: Difference between revisions

From MoodleDocs
No edit summary
No edit summary
Line 1: Line 1:
No-one has yet written any documentation explaining how to write a new admin report.
==Overview==


However, it is actually quite easy to do. Just look at the code for some of the other reports, and do something similar. As you do so, feel free to fill in this documentation with what you learn.
An admin report is a folder of code under admin/report. For example, you might make a folder in there called '''myreport'''.


Recommended examples to copy:
In there, the only code you are required to have is an index.php file. This will normally display a simple HTML form for controlling the report, and, the code for displaying the report. You will probably also want a language file, which would be called lang/en_utf8/report_myreport.php following the example above.


* Any of the ones in core, but some of them are quite complicated.
In addition, you can add any other PHP code you like, and then link people to it from index.php.
* [http://moodle.org/mod/data/view.php?d=13&rid=913 System images report] this is a very simple report.
 
==What to do in index.php==
 
The simplest way to work this out is by example. You should look at some of the reports that ship with Moodle. [http://cvs.moodle.org/moodle/admin/report/capability/index.php?view=markup admin/report/capability], is a good one to start with. Let us go through that example to see what is there (I will put into italics the bits that are more advanced, and which are not really required):
# Require some libraries we need.
# Check the user is logged in, and has appropriate permissions. (Normally admin reports are controlled by the 'moodle/site:viewreports' capability, the capability report is a special case.)
# Get the parameters from the URL that control the report, and clean them up a bit.
# ''Include some JavaScript code that enhances the report for people with JavaScript turned on in their browser.''
# Log this request.
# Print the page header. Since this is an administration page, we need to do this using the admin_externalpage... functions. You probably just need to copy these three lines exactly, then replace every occurrence of 'capability' with your report name.
# Display a form to control the report.
# If the URL contains all the parameters needed to generate the report, do so, and display the results. (You don't need to understand the details of what the capability report is doing, because your report will do something different. However, the general structure will probably be the same: get some data out of the database, then display it.)
# Print the footer, using the admin_externalpage... function.
# ''Some functions, defined to help with the work of printing the report.''
 
==How your report gets included in the admin tree==
 
By default, your report will be included in the admin tree under the 'Reports' section, and it will only appear to people with the 'moodle/site:viewreports' capability. If the report is in the ''myreport'' folder, then the report name will be get_string("myreport", "report_''myreport''"). The report name, needed for the call to admin_externalpage_setup, will be "report''myreport''".
 
If you want more control over how your report appears, for example, if you want it to appear somewhere other than under Reports, or if you want it to be controlled by another capability, then you can create a settings.php file. For example, for the capability report, there is a settings.php file that contains:
 
<?php  // $Id$
$ADMIN->add('''"roles"''', new admin_externalpage('reportcapability', get_string('capability', 'report_capability'), "$CFG->wwwroot/$CFG->admin/report/capability/index.php",'''"moodle/role:manage"'''));
?>
 
(The two non-standard bits there have been put into bold.)
 
==Strings you must define in you lang/en_utf8/report''myreport''.php==
 
You just need to define
 
$string["''myreport''"] = 'What my report is called';
 
==See also==
 
* [http://moodle.org/mod/data/view.php?d=13&rid=913 System images report] this is another very simple report, in contrib. Might be a good one to look at.

Revision as of 10:15, 8 September 2008

Overview

An admin report is a folder of code under admin/report. For example, you might make a folder in there called myreport.

In there, the only code you are required to have is an index.php file. This will normally display a simple HTML form for controlling the report, and, the code for displaying the report. You will probably also want a language file, which would be called lang/en_utf8/report_myreport.php following the example above.

In addition, you can add any other PHP code you like, and then link people to it from index.php.

What to do in index.php

The simplest way to work this out is by example. You should look at some of the reports that ship with Moodle. admin/report/capability, is a good one to start with. Let us go through that example to see what is there (I will put into italics the bits that are more advanced, and which are not really required):

  1. Require some libraries we need.
  2. Check the user is logged in, and has appropriate permissions. (Normally admin reports are controlled by the 'moodle/site:viewreports' capability, the capability report is a special case.)
  3. Get the parameters from the URL that control the report, and clean them up a bit.
  4. Include some JavaScript code that enhances the report for people with JavaScript turned on in their browser.
  5. Log this request.
  6. Print the page header. Since this is an administration page, we need to do this using the admin_externalpage... functions. You probably just need to copy these three lines exactly, then replace every occurrence of 'capability' with your report name.
  7. Display a form to control the report.
  8. If the URL contains all the parameters needed to generate the report, do so, and display the results. (You don't need to understand the details of what the capability report is doing, because your report will do something different. However, the general structure will probably be the same: get some data out of the database, then display it.)
  9. Print the footer, using the admin_externalpage... function.
  10. Some functions, defined to help with the work of printing the report.

How your report gets included in the admin tree

By default, your report will be included in the admin tree under the 'Reports' section, and it will only appear to people with the 'moodle/site:viewreports' capability. If the report is in the myreport folder, then the report name will be get_string("myreport", "report_myreport"). The report name, needed for the call to admin_externalpage_setup, will be "reportmyreport".

If you want more control over how your report appears, for example, if you want it to appear somewhere other than under Reports, or if you want it to be controlled by another capability, then you can create a settings.php file. For example, for the capability report, there is a settings.php file that contains:

<?php  // $Id$
$ADMIN->add("roles", new admin_externalpage('reportcapability', get_string('capability', 'report_capability'), "$CFG->wwwroot/$CFG->admin/report/capability/index.php","moodle/role:manage"));
?>

(The two non-standard bits there have been put into bold.)

Strings you must define in you lang/en_utf8/reportmyreport.php

You just need to define

$string["myreport"] = 'What my report is called';

See also

  • System images report this is another very simple report, in contrib. Might be a good one to look at.