MDL-29920 Performance improvements in the gradebook reports

Both methods get_activity_link() and get_grade_analysis_url() now cache
the results of the check whether the activity module provides grade.php
or not. Previously, get_activity_link() called file_exists() in every
call and get_grade_analysis_url() did not check the existence at all.

Note that this solution is still sub-optimal as apparently both methods
do very similar job. This should be refactored one day by a brave
developer who works on gradebook. Meanwhile, even this suboptimal
solution is more effective than the previous one for most courses.
This commit is contained in:
David Mudrak 2011-11-03 13:44:41 +01:00
parent e14e78abbf
commit eff314b06a

View File

@ -1141,6 +1141,8 @@ class grade_structure {
private function get_activity_link($element) {
global $CFG;
/** @var array static cache of the grade.php file existence flags */
static $hasgradephp = array();
$itemtype = $element['object']->itemtype;
$itemmodule = $element['object']->itemmodule;
@ -1165,9 +1167,16 @@ class grade_structure {
return null;
}
if (!array_key_exists($itemmodule, $hasgradephp)) {
if (file_exists($CFG->dirroot . '/mod/' . $itemmodule . '/grade.php')) {
$hasgradephp[$itemmodule] = true;
} else {
$hasgradephp[$itemmodule] = false;
}
}
// If module has grade.php, link to that, otherwise view.php
$dir = $CFG->dirroot . '/mod/' . $itemmodule;
if (file_exists($dir.'/grade.php')) {
if ($hasgradephp[$itemmodule]) {
return new moodle_url('/mod/' . $itemmodule . '/grade.php', array('id' => $cm->id, 'itemnumber' => $itemnumber));
} else {
return new moodle_url('/mod/' . $itemmodule . '/view.php', array('id' => $cm->id));
@ -1177,16 +1186,16 @@ class grade_structure {
/**
* Returns URL of a page that is supposed to contain detailed grade analysis
*
* Please note this method does not check if the referenced file actually exists,
* the caller is usually able to do it in more effective way.
*
* At the moment, only activity modules are supported. The method generates link
* to the module's file grade.php with the parameters id (cmid), itemid, itemnumber,
* gradeid and userid.
* gradeid and userid. If the grade.php does not exist, null is returned.
*
* @return moodle_url|null URL or null if unable to construct it
*/
public function get_grade_analysis_url(grade_grade $grade) {
global $CFG;
/** @var array static cache of the grade.php file existence flags */
static $hasgradephp = array();
if (empty($grade->grade_item) or !($grade->grade_item instanceof grade_item)) {
throw new coding_exception('Passed grade without the associated grade item');
@ -1204,6 +1213,18 @@ class grade_structure {
return null;
}
if (!array_key_exists($item->itemmodule, $hasgradephp)) {
if (file_exists($CFG->dirroot . '/mod/' . $item->itemmodule . '/grade.php')) {
$hasgradephp[$item->itemmodule] = true;
} else {
$hasgradephp[$item->itemmodule] = false;
}
}
if (!$hasgradephp[$item->itemmodule]) {
return null;
}
$instances = $this->modinfo->get_instances();
if (empty($instances[$item->itemmodule][$item->iteminstance])) {
return null;