Merge branch 'wip-MDL-56788-master' of git://github.com/abgreeve/moodle
[moodle.git] / mod / scorm / report / reportlib.php
blob73c1aaf367a72f1a10dd74621cb45ea5afab964e
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Returns an array of reports to which are currently readable.
19 * @package mod_scorm
20 * @author Ankit Kumar Agarwal
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') || die();
26 /* Generates and returns list of available Scorm report sub-plugins
28 * @param context context level to check caps against
29 * @return array list of valid reports present
31 function scorm_report_list($context) {
32 global $CFG;
33 static $reportlist;
34 if (!empty($reportlist)) {
35 return $reportlist;
37 $installed = core_component::get_plugin_list('scormreport');
38 foreach ($installed as $reportname => $notused) {
40 // Moodle 2.8+ style of autoloaded classes.
41 $classname = "scormreport_$reportname\\report";
42 if (class_exists($classname)) {
43 $report = new $classname();
45 if ($report->canview($context)) {
46 $reportlist[] = $reportname;
48 continue;
51 // Legacy style of naming classes.
52 $pluginfile = $CFG->dirroot.'/mod/scorm/report/'.$reportname.'/report.php';
53 if (is_readable($pluginfile)) {
54 debugging("Please use autoloaded classnames for your plugin. Refer MDL-46469 for details", DEBUG_DEVELOPER);
55 include_once($pluginfile);
56 $reportclassname = "scorm_{$reportname}_report";
57 if (class_exists($reportclassname)) {
58 $report = new $reportclassname();
60 if ($report->canview($context)) {
61 $reportlist[] = $reportname;
66 return $reportlist;
68 /**
69 * Returns The maximum numbers of Questions associated with an Scorm Pack
71 * @param int Scorm ID
72 * @return int an integer representing the question count
74 function get_scorm_question_count($scormid) {
75 global $DB;
76 $count = 0;
77 $params = array();
78 $select = "scormid = ? AND ";
79 $select .= $DB->sql_like("element", "?", false);
80 $params[] = $scormid;
81 $params[] = "cmi.interactions_%.id";
82 $rs = $DB->get_recordset_select("scorm_scoes_track", $select, $params, 'element');
83 $keywords = array("cmi.interactions_", ".id");
84 if ($rs->valid()) {
85 foreach ($rs as $record) {
86 $num = trim(str_ireplace($keywords, '', $record->element));
87 if (is_numeric($num) && $num > $count) {
88 $count = $num;
91 // Done as interactions start at 0 (do only if we have something to report).
92 $count++;
94 $rs->close(); // Closing recordset.
95 return $count;