MDL-44366 improve glossary filter performance

This should have when there are no glossaries in course or
when there are no linked entries in existing glossaries.
This commit is contained in:
Petr Škoda 2014-04-07 14:04:31 +08:00
parent 86286bdb7e
commit 1e83288de7
4 changed files with 31 additions and 24 deletions

View File

@ -60,14 +60,12 @@ class filter_glossary extends moodle_text_filter {
$coursectx = $this->context->get_course_context(false);
if (!$coursectx) {
// Only global glossaries will be linked.
$course = null;
$courseid = 0;
} else {
$course = get_course($coursectx->instanceid, false);
$courseid = (int)$course->id;
$courseid = $coursectx->instanceid;
}
if ($this->cachecourseid !== $courseid or $this->cacheuserid !== $USER->id) {
if ($this->cachecourseid != $courseid or $this->cacheuserid != $USER->id) {
// Invalidate the page cache.
$this->cacheconceptlist = null;
}
@ -79,7 +77,7 @@ class filter_glossary extends moodle_text_filter {
return filter_phrases($text, $this->cacheconceptlist);
}
list($glossaries, $allconcepts) = \mod_glossary\local\concept_cache::get_concepts($course);
list($glossaries, $allconcepts) = \mod_glossary\local\concept_cache::get_concepts($courseid);
if (!$allconcepts) {
$this->cacheuserid = $USER->id;

View File

@ -147,33 +147,35 @@ class concept_cache {
/**
* Get all linked concepts from course.
* @param \stdClass $course
* @param int $courseid
* @return array
*/
protected static function get_course_concepts($course) {
protected static function get_course_concepts($courseid) {
global $DB;
if (empty($course->id)) {
if (empty($courseid)) {
return array(array(), array());
}
$courseid = (int)$courseid;
$cache = \cache::make('mod_glossary', 'concepts');
$data = $cache->get((int)$course->id);
$data = $cache->get($courseid);
if (is_array($data)) {
list($glossaries, $allconcepts) = $data;
} else {
// Find all course glossaries.
$sql = "SELECT g.id, g.name
FROM {glossary} g
JOIN {course_modules} cm ON (cm.instance = g.id)
JOIN {modules} m ON (m.name = 'glossary' AND m.id = cm.module)
WHERE g.usedynalink = 1 AND g.course = :course AND cm.visible = 1 AND m.visible = 1
ORDER BY g.globalglossary, g.id";
$glossaries = $DB->get_records_sql_menu($sql, array('course' => $course->id));
FROM {glossary} g
JOIN {course_modules} cm ON (cm.instance = g.id)
JOIN {modules} m ON (m.name = 'glossary' AND m.id = cm.module)
WHERE g.usedynalink = 1 AND g.course = :course AND cm.visible = 1 AND m.visible = 1
ORDER BY g.globalglossary, g.id";
$glossaries = $DB->get_records_sql_menu($sql, array('course' => $courseid));
if (!$glossaries) {
$data = array(array(), array());
$cache->set((int)$course->id, $data);
$cache->set($courseid, $data);
return $data;
}
foreach ($glossaries as $id => $name) {
@ -187,13 +189,19 @@ class concept_cache {
unset($glossaries[$gid]);
}
}
$cache->set((int)$course->id, array($glossaries, $allconcepts));
if (!$glossaries) {
// This means there are no interesting concepts in the existing glossaries.
$data = array(array(), array());
$cache->set($courseid, $data);
return $data;
}
$cache->set($courseid, array($glossaries, $allconcepts));
}
$concepts = $allconcepts;
// Verify access control to glossary instances.
$modinfo = get_fast_modinfo($course);
$modinfo = get_fast_modinfo($courseid);
$cminfos = $modinfo->get_instances_of('glossary');
foreach ($concepts as $modid => $unused) {
if (!isset($cminfos[$modid])) {
@ -256,11 +264,11 @@ class concept_cache {
/**
* Get all concepts that should be linked in the given course.
* @param \stdClass $course
* @param int $courseid
* @return array with two elements - array of glossaries and concepts for each glossary
*/
public static function get_concepts($course) {
list($glossaries, $concepts) = self::get_course_concepts($course);
public static function get_concepts($courseid) {
list($glossaries, $concepts) = self::get_course_concepts($courseid);
list($globalglossaries, $globalconcepts) = self::get_global_concepts();
foreach ($globalconcepts as $gid => $cs) {

View File

@ -55,6 +55,7 @@ $string['attachment_help'] = 'You can optionally attach one or more files to a g
$string['author'] = 'author';
$string['authorview'] = 'Browse by Author';
$string['back'] = 'Back';
$string['cachedef_concepts'] = 'Concept linking';
$string['cantinsertcat'] = 'Can\'t insert category';
$string['cantinsertrec'] = 'Can\'t insert record';
$string['cantinsertrel'] = 'Can\'t insert relation category-entry';

View File

@ -73,7 +73,7 @@ class mod_glossary_concept_cache_testcase extends advanced_testcase {
\mod_glossary\local\concept_cache::reset_caches();
$concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1);
$concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1->id);
$this->assertCount(3, $concepts1[0]);
$this->arrayHasKey($concepts1[0], $glossary1a->id);
$this->arrayHasKey($concepts1[0], $glossary1b->id);
@ -136,7 +136,7 @@ class mod_glossary_concept_cache_testcase extends advanced_testcase {
}
}
$concepts3 = \mod_glossary\local\concept_cache::get_concepts($site);
$concepts3 = \mod_glossary\local\concept_cache::get_concepts($site->id);
$this->assertCount(1, $concepts3[0]);
$this->arrayHasKey($concepts3[0], $glossary3->id);
$this->assertCount(1, $concepts3[1]);
@ -156,7 +156,7 @@ class mod_glossary_concept_cache_testcase extends advanced_testcase {
}
}
$concepts2 = \mod_glossary\local\concept_cache::get_concepts($course2);
$concepts2 = \mod_glossary\local\concept_cache::get_concepts($course2->id);
$this->assertEquals($concepts3, $concepts2);
}
}