Example #1
0
 public function filter($text, array $options = array())
 {
     global $CFG, $USER, $GLOSSARY_EXCLUDEENTRY;
     // Try to get current course.
     $coursectx = $this->context->get_course_context(false);
     if (!$coursectx) {
         // Only global glossaries will be linked.
         $courseid = 0;
     } else {
         $courseid = $coursectx->instanceid;
     }
     if ($this->cachecourseid != $courseid or $this->cacheuserid != $USER->id) {
         // Invalidate the page cache.
         $this->cacheconceptlist = null;
     }
     if (is_array($this->cacheconceptlist) and empty($GLOSSARY_EXCLUDEENTRY)) {
         if (empty($this->cacheconceptlist)) {
             return $text;
         }
         return filter_phrases($text, $this->cacheconceptlist);
     }
     list($glossaries, $allconcepts) = \mod_glossary\local\concept_cache::get_concepts($courseid);
     if (!$allconcepts) {
         $this->cacheuserid = $USER->id;
         $this->cachecourseid = $courseid;
         $this->cacheconcepts = array();
         return $text;
     }
     $strcategory = get_string('category', 'glossary');
     $conceptlist = array();
     $excluded = false;
     foreach ($allconcepts as $concepts) {
         foreach ($concepts as $concept) {
             if (!empty($GLOSSARY_EXCLUDEENTRY) and $concept->id == $GLOSSARY_EXCLUDEENTRY) {
                 $excluded = true;
                 continue;
             }
             if ($concept->category) {
                 // Link to a category.
                 // TODO: Fix this string usage.
                 $title = $glossaries[$concept->glossaryid] . ': ' . $strcategory . ' ' . $concept->concept;
                 $link = new moodle_url('/mod/glossary/view.php', array('g' => $concept->glossaryid, 'mode' => 'cat', 'hook' => $concept->id));
                 $attributes = array('href' => $link, 'title' => $title, 'class' => 'glossary autolink category glossaryid' . $concept->glossaryid);
             } else {
                 // Link to entry or alias
                 $title = $glossaries[$concept->glossaryid] . ': ' . $concept->concept;
                 // Hardcoding dictionary format in the URL rather than defaulting
                 // to the current glossary format which may not work in a popup.
                 // for example "entry list" means the popup would only contain
                 // a link that opens another popup.
                 $link = new moodle_url('/mod/glossary/showentry.php', array('eid' => $concept->id, 'displayformat' => 'dictionary'));
                 $attributes = array('href' => $link, 'title' => str_replace('&', '&', $title), 'class' => 'glossary autolink concept glossaryid' . $concept->glossaryid);
             }
             // This flag is optionally set by resource_pluginfile()
             // if processing an embedded file use target to prevent getting nested Moodles.
             if (!empty($CFG->embeddedsoforcelinktarget)) {
                 $attributes['target'] = '_top';
             }
             $href_tag_begin = html_writer::start_tag('a', $attributes);
             $conceptlist[] = new filterobject($concept->concept, $href_tag_begin, '</a>', $concept->casesensitive, $concept->fullmatch);
         }
     }
     usort($conceptlist, 'filter_glossary::sort_entries_by_length');
     if (!$excluded) {
         // Do not cache the excluded list here, it is used once per page only.
         $this->cacheuserid = $USER->id;
         $this->cachecourseid = $courseid;
         $this->cacheconceptlist = $conceptlist;
     }
     if (empty($conceptlist)) {
         return $text;
     }
     return filter_phrases($text, $conceptlist);
     // Actually search for concepts!
 }
Example #2
0
 /**
  * Test convect fetching.
  */
 public function test_concept_fetching()
 {
     global $CFG, $DB;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $CFG->glossary_linkbydefault = 1;
     $CFG->glossary_linkentries = 0;
     // Create a test courses.
     $course1 = $this->getDataGenerator()->create_course();
     $course2 = $this->getDataGenerator()->create_course();
     $site = $DB->get_record('course', array('id' => SITEID));
     // Create a glossary.
     $glossary1a = $this->getDataGenerator()->create_module('glossary', array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 1));
     $glossary1b = $this->getDataGenerator()->create_module('glossary', array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 1));
     $glossary1c = $this->getDataGenerator()->create_module('glossary', array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 0));
     $glossary2 = $this->getDataGenerator()->create_module('glossary', array('course' => $course2->id, 'mainglossary' => 1, 'usedynalink' => 1));
     $glossary3 = $this->getDataGenerator()->create_module('glossary', array('course' => $site->id, 'mainglossary' => 1, 'usedynalink' => 1, 'globalglossary' => 1));
     /** @var mod_glossary_generator $generator */
     $generator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
     $entry1a1 = $generator->create_content($glossary1a, array('concept' => 'first', 'usedynalink' => 1), array('prvni', 'erste'));
     $entry1a2 = $generator->create_content($glossary1a, array('concept' => 'A&B', 'usedynalink' => 1));
     $entry1a3 = $generator->create_content($glossary1a, array('concept' => 'neee', 'usedynalink' => 0));
     $entry1b1 = $generator->create_content($glossary1b, array('concept' => 'second', 'usedynalink' => 1));
     $entry1c1 = $generator->create_content($glossary1c, array('concept' => 'third', 'usedynalink' => 1));
     $entry31 = $generator->create_content($glossary3, array('concept' => 'global', 'usedynalink' => 1), array('globalni'));
     $cat1 = $generator->create_category($glossary1a, array('name' => 'special'), array($entry1a1, $entry1a2));
     \mod_glossary\local\concept_cache::reset_caches();
     $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);
     $this->arrayHasKey($concepts1[0], $glossary3->id);
     $this->assertCount(3, $concepts1[1]);
     $this->arrayHasKey($concepts1[1], $glossary1a->id);
     $this->arrayHasKey($concepts1[1], $glossary1b->id);
     $this->arrayHasKey($concepts1[0], $glossary3->id);
     $this->assertCount(5, $concepts1[1][$glossary1a->id]);
     foreach ($concepts1[1][$glossary1a->id] as $concept) {
         $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array) $concept));
         if ($concept->concept === 'first') {
             $this->assertEquals($entry1a1->id, $concept->id);
             $this->assertEquals($glossary1a->id, $concept->glossaryid);
             $this->assertEquals(0, $concept->category);
         } else {
             if ($concept->concept === 'prvni') {
                 $this->assertEquals($entry1a1->id, $concept->id);
                 $this->assertEquals($glossary1a->id, $concept->glossaryid);
                 $this->assertEquals(0, $concept->category);
             } else {
                 if ($concept->concept === 'erste') {
                     $this->assertEquals($entry1a1->id, $concept->id);
                     $this->assertEquals($glossary1a->id, $concept->glossaryid);
                     $this->assertEquals(0, $concept->category);
                 } else {
                     if ($concept->concept === 'A&amp;B') {
                         $this->assertEquals($entry1a2->id, $concept->id);
                         $this->assertEquals($glossary1a->id, $concept->glossaryid);
                         $this->assertEquals(0, $concept->category);
                     } else {
                         if ($concept->concept === 'special') {
                             $this->assertEquals($cat1->id, $concept->id);
                             $this->assertEquals($glossary1a->id, $concept->glossaryid);
                             $this->assertEquals(1, $concept->category);
                         } else {
                             $this->fail('Unexpected concept: ' . $concept->concept);
                         }
                     }
                 }
             }
         }
     }
     $this->assertCount(1, $concepts1[1][$glossary1b->id]);
     foreach ($concepts1[1][$glossary1b->id] as $concept) {
         $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array) $concept));
         if ($concept->concept === 'second') {
             $this->assertEquals($entry1b1->id, $concept->id);
             $this->assertEquals($glossary1b->id, $concept->glossaryid);
             $this->assertEquals(0, $concept->category);
         } else {
             $this->fail('Unexpected concept: ' . $concept->concept);
         }
     }
     $this->assertCount(2, $concepts1[1][$glossary3->id]);
     foreach ($concepts1[1][$glossary3->id] as $concept) {
         $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array) $concept));
         if ($concept->concept === 'global') {
             $this->assertEquals($entry31->id, $concept->id);
             $this->assertEquals($glossary3->id, $concept->glossaryid);
             $this->assertEquals(0, $concept->category);
         } else {
             if ($concept->concept === 'globalni') {
                 $this->assertEquals($entry31->id, $concept->id);
                 $this->assertEquals($glossary3->id, $concept->glossaryid);
                 $this->assertEquals(0, $concept->category);
             } else {
                 $this->fail('Unexpected concept: ' . $concept->concept);
             }
         }
     }
     $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]);
     $this->arrayHasKey($concepts3[0], $glossary3->id);
     foreach ($concepts3[1][$glossary3->id] as $concept) {
         $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array) $concept));
         if ($concept->concept === 'global') {
             $this->assertEquals($entry31->id, $concept->id);
             $this->assertEquals($glossary3->id, $concept->glossaryid);
             $this->assertEquals(0, $concept->category);
         } else {
             if ($concept->concept === 'globalni') {
                 $this->assertEquals($entry31->id, $concept->id);
                 $this->assertEquals($glossary3->id, $concept->glossaryid);
                 $this->assertEquals(0, $concept->category);
             } else {
                 $this->fail('Unexpected concept: ' . $concept->concept);
             }
         }
     }
     $concepts2 = \mod_glossary\local\concept_cache::get_concepts($course2->id);
     $this->assertEquals($concepts3, $concepts2);
     // Test uservisible flag.
     set_config('enableavailability', 1);
     $glossary1d = $this->getDataGenerator()->create_module('glossary', array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 1, 'availability' => json_encode(\core_availability\tree::get_root_json(array(\availability_group\condition::get_json())))));
     $entry1d1 = $generator->create_content($glossary1d, array('concept' => 'membersonly', 'usedynalink' => 1));
     $user = $this->getDataGenerator()->create_user();
     $this->getDataGenerator()->enrol_user($user->id, $course1->id);
     $this->getDataGenerator()->enrol_user($user->id, $course2->id);
     \mod_glossary\local\concept_cache::reset_caches();
     $concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1->id);
     $this->assertCount(4, $concepts1[0]);
     $this->assertCount(4, $concepts1[1]);
     $this->setUser($user);
     course_modinfo::clear_instance_cache();
     \mod_glossary\local\concept_cache::reset_caches();
     $concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1->id);
     $this->assertCount(3, $concepts1[0]);
     $this->assertCount(3, $concepts1[1]);
 }