MDL-46469 scorm: Support autoloading for report plugins

This commit is contained in:
Ankit Agarwal
2014-08-19 13:56:13 +08:00
parent 750688c741
commit fd57607fdb
5 changed files with 64 additions and 5 deletions
@@ -19,11 +19,13 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_scorm;
/*******************************************************************/
// Default class for Scorm plugins
//
// Doesn't do anything on it's own -- it needs to be extended.
// This class displays scorm reports. Because it is called from
// This class displays scorm reports. Because it is called from
// within /mod/scorm/report.php you can assume that the page header
// and footer are taken care of.
//
@@ -33,7 +35,7 @@
defined('MOODLE_INTERNAL') || die();
class scorm_default_report {
class report {
/**
* displays the full report
* @param stdClass $scorm full SCORM object
+40
View File
@@ -0,0 +1,40 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains mappings for classes that have been renamed so that they meet the requirements of the autoloader.
*
* Renaming isn't always the recommended approach, but can provide benefit in situations where we've already got a
* close structure, OR where lots of classes get included and not necessarily used, or checked for often.
*
* When renaming a class delete the original class and add an entry to the db/renamedclasses.php directory for that
* component.
* This way we don't need to keep around old classes, instead creating aliases only when required.
* One big advantage to this method is that we provide consistent debugging for renamed classes when they are used.
*
* @package mod_scorm
* @copyright 2014 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// Like other files in the db directory this file uses an array.
// The old class name is the key, the new class name is the value.
// The array must be called $renamedclasses.
$renamedclasses = array(
'scorm_default_report' => 'mod_scorm\report'
);
+4 -3
View File
@@ -22,7 +22,7 @@ require_once($CFG->dirroot.'/mod/scorm/locallib.php');
require_once($CFG->dirroot.'/mod/scorm/reportsettings_form.php');
require_once($CFG->dirroot.'/mod/scorm/report/reportlib.php');
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->dirroot.'/mod/scorm/report/default.php'); // Parent class.
define('SCORM_REPORT_DEFAULT_PAGE_SIZE', 20);
define('SCORM_REPORT_ATTEMPTS_ALL_STUDENTS', 0);
define('SCORM_REPORT_ATTEMPTS_STUDENTS_WITH', 1);
@@ -91,8 +91,9 @@ if (empty($noheader)) {
}
// Open the selected Scorm report and display it.
$reportclassname = "scorm_{$mode}_report";
$report = new $reportclassname();
$classname = "scormreport_{$mode}\\report";
$legacyclassname = "scorm_{$mode}_report";
$report = class_exists($classname) ? new $classname() : new $legacyclassname();
$report->display($scorm, $cm, $course, $download); // Run the report!
// Print footer.
+14
View File
@@ -36,8 +36,22 @@ function scorm_report_list($context) {
}
$installed = core_component::get_plugin_list('scormreport');
foreach ($installed as $reportname => $notused) {
// Moodle 2.8+ style of autoloaded classes.
$classname = "scormreport_$reportname\\report";
if (class_exists($classname)) {
$report = new $classname();
if ($report->canview($context)) {
$reportlist[] = $reportname;
}
continue;
}
// Legacy style of naming classes.
$pluginfile = $CFG->dirroot.'/mod/scorm/report/'.$reportname.'/report.php';
if (is_readable($pluginfile)) {
debugging("Please use autoloaded classnames for your plugin. Refer MDL-46469 for details", DEBUG_DEVELOPER);
include_once($pluginfile);
$reportclassname = "scorm_{$reportname}_report";
if (class_exists($reportclassname)) {
+2
View File
@@ -4,3 +4,5 @@ This files describes API changes in the mod_scorm code.
* Coding style fixes. A large number of coding style issues were fixed in MDL-45887. This means any local modifications
to mod_scorm are likely to conflict.
* All scorm report plugins should use namespaced classnames now. Refer MDL-46469 for details.