Beispiel #1
0
function glossary_filter($courseid, $text)
{
    global $CFG;
    // Trivial-cache - keyed on $cachedcourseid
    static $nothingtodo;
    static $conceptlist;
    static $cachedcourseid;
    if (empty($courseid)) {
        $courseid = SITEID;
    }
    // Initialise/invalidate our trivial cache if dealing with a different course
    if (!isset($cachedcourseid) || $cachedcourseid !== (int) $courseid) {
        $conceptlist = array();
        $nothingtodo = false;
    }
    $cachedcourseid = (int) $courseid;
    if ($nothingtodo === true) {
        return $text;
    }
    /// Create a list of all the concepts to search for.  It may be cached already.
    if (empty($conceptlist)) {
        /// Find all the glossaries we need to examine
        if (!($glossaries = get_records_sql_menu('SELECT g.id, g.name
                                                    FROM ' . $CFG->prefix . 'glossary g,
                                                         ' . $CFG->prefix . 'course_modules cm,
                                                         ' . $CFG->prefix . 'modules m
                                                    WHERE m.name = \'glossary\' AND
                                                          cm.module = m.id AND
                                                          cm.visible = 1 AND
                                                          g.id = cm.instance AND
                                                          g.usedynalink != 0 AND
                                                          (g.course = \'' . $courseid . '\' OR g.globalglossary = 1)
                                                    ORDER BY g.globalglossary, g.id'))) {
            $nothingtodo = true;
            return $text;
        }
        /// Make a list of glossary IDs for searching
        $glossarylist = '';
        foreach ($glossaries as $glossaryid => $glossaryname) {
            $glossarylist .= $glossaryid . ',';
        }
        $glossarylist = substr($glossarylist, 0, -1);
        /// Pull out all the raw data from the database for entries, categories and aliases
        $entries = get_records_select('glossary_entries', 'glossaryid IN (' . $glossarylist . ') AND usedynalink != 0 AND approved != 0 ', '', 'id,glossaryid, concept, casesensitive, 0 AS category, fullmatch');
        $categories = get_records_select('glossary_categories', 'glossaryid IN (' . $glossarylist . ') AND usedynalink != 0', '', 'id,glossaryid,name AS concept, 1 AS casesensitive, 1 AS category, 1 AS fullmatch');
        $aliases = get_records_sql('SELECT ga.id, ge.glossaryid, ga.alias as concept, ge.concept as originalconcept,
                                           casesensitive, 0 AS category, fullmatch
                                      FROM ' . $CFG->prefix . 'glossary_alias ga,
                                           ' . $CFG->prefix . 'glossary_entries ge
                                     WHERE ga.entryid = ge.id
                                       AND ge.glossaryid IN (' . $glossarylist . ')
                                       AND ge.usedynalink != 0
                                       AND ge.approved != 0');
        /// Combine them into one big list
        $concepts = array();
        if ($entries and $categories) {
            $concepts = array_merge($entries, $categories);
        } else {
            if ($categories) {
                $concepts = $categories;
            } else {
                if ($entries) {
                    $concepts = $entries;
                }
            }
        }
        if ($aliases) {
            $concepts = array_merge($concepts, $aliases);
        }
        if (!empty($concepts)) {
            foreach ($concepts as $key => $concept) {
                /// Trim empty or unlinkable concepts
                $currentconcept = trim(strip_tags($concept->concept));
                if (empty($currentconcept)) {
                    unset($concepts[$key]);
                    continue;
                } else {
                    $concepts[$key]->concept = $currentconcept;
                }
                /// Rule out any small integers.  See bug 1446
                $currentint = intval($currentconcept);
                if ($currentint && strval($currentint) == $currentconcept && $currentint < 1000) {
                    unset($concepts[$key]);
                }
            }
        }
        if (empty($concepts)) {
            $nothingtodo = true;
            return $text;
        }
        usort($concepts, 'glossary_sort_entries_by_length');
        $strcategory = get_string('category', 'glossary');
        /// Loop through all the concepts, setting up our data structure for the filter
        $conceptlist = array();
        /// We will store all the concepts here
        foreach ($concepts as $concept) {
            $glossaryname = $glossaries[$concept->glossaryid];
            if ($concept->category) {
                // Link to a category
                $title = strip_tags($glossaryname . ': ' . $strcategory . ' ' . $concept->concept);
                $href_tag_begin = '<a class="glossary autolink glossaryid' . $concept->glossaryid . '" title="' . $title . '" ' . 'href="' . $CFG->wwwroot . '/mod/glossary/view.php?g=' . $concept->glossaryid . '&amp;mode=cat&amp;hook=' . $concept->id . '">';
            } else {
                if (!empty($concept->originalconcept)) {
                    // We are dealing with an alias (so show original)
                    $encodedconcept = urlencode($concept->originalconcept);
                    $title = str_replace('"', "'", strip_tags($glossaryname . ': ' . $concept->originalconcept));
                } else {
                    $encodedconcept = urlencode($concept->concept);
                    $title = str_replace('"', "'", strip_tags($glossaryname . ': ' . $concept->concept));
                }
                $href_tag_begin = '<a class="glossary autolink glossaryid' . $concept->glossaryid . '" title="' . $title . '" ' . 'href="' . $CFG->wwwroot . '/mod/glossary/showentry.php?courseid=' . $courseid . '&amp;concept=' . $encodedconcept . '" ' . 'onclick="return openpopup(\'/mod/glossary/showentry.php?courseid=' . $courseid . '\\&amp;concept=' . $encodedconcept . '\', \'entry\', ' . '\'menubar=0,location=0,scrollbars,resizable,width=600,height=450\', 0);">';
            }
            $conceptlist[] = new filterobject($concept->concept, $href_tag_begin, '</a>', $concept->casesensitive, $concept->fullmatch);
        }
        $conceptlist = filter_remove_duplicates($conceptlist);
    }
    return filter_phrases($text, $conceptlist);
    // Actually search for concepts!
}
Beispiel #2
0
 public function filter($text, array $options = array())
 {
     global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS, $PAGE;
     // Trivial-cache - keyed on $cachedcontextid
     static $cachedcontextid;
     static $conceptlist;
     static $jsinitialised;
     // To control unique js init
     static $nothingtodo;
     // To avoid processing if no glossaries / concepts are found
     // Try to get current course
     if (!($courseid = get_courseid_from_context($this->context))) {
         $courseid = 0;
     }
     // Initialise/invalidate our trivial cache if dealing with a different context
     if (!isset($cachedcontextid) || $cachedcontextid !== $this->context->id) {
         $cachedcontextid = $this->context->id;
         $conceptlist = array();
         $nothingtodo = false;
     }
     if ($nothingtodo === true || !has_capability('mod/glossary:view', $this->context)) {
         return $text;
     }
     // Create a list of all the concepts to search for.  It may be cached already.
     if (empty($conceptlist)) {
         // Find all the glossaries we need to examine
         if (!($glossaries = $DB->get_records_sql_menu('
                 SELECT g.id, g.name
                   FROM {glossary} g, {course_modules} cm, {modules} m
                  WHERE m.name = \'glossary\'
                    AND cm.module = m.id
                    AND cm.visible = 1
                    AND g.id = cm.instance
                    AND g.usedynalink != 0
                    AND (g.course = ? OR g.globalglossary = 1)
               ORDER BY g.globalglossary, g.id', array($courseid)))) {
             $nothingtodo = true;
             return $text;
         }
         // Make a list of glossary IDs for searching
         $glossarylist = implode(',', array_keys($glossaries));
         // Pull out all the raw data from the database for entries, categories and aliases
         $entries = $DB->get_records_select('glossary_entries', 'glossaryid IN (' . $glossarylist . ') AND usedynalink != 0 AND approved != 0 ', null, '', 'id,glossaryid, concept, casesensitive, 0 AS category, fullmatch');
         $categories = $DB->get_records_select('glossary_categories', 'glossaryid IN (' . $glossarylist . ') AND usedynalink != 0', null, '', 'id,glossaryid,name AS concept, 1 AS casesensitive, 1 AS category, 1 AS fullmatch');
         $aliases = $DB->get_records_sql('
                 SELECT ga.id, ge.id AS entryid, ge.glossaryid,
                        ga.alias AS concept, ge.concept AS originalconcept,
                        casesensitive, 0 AS category, fullmatch
                   FROM {glossary_alias} ga,
                        {glossary_entries} ge
                   WHERE ga.entryid = ge.id
                     AND ge.glossaryid IN (' . $glossarylist . ')
                     AND ge.usedynalink != 0
                     AND ge.approved != 0', null);
         // Combine them into one big list
         $concepts = array();
         if ($entries and $categories) {
             $concepts = array_merge($entries, $categories);
         } else {
             if ($categories) {
                 $concepts = $categories;
             } else {
                 if ($entries) {
                     $concepts = $entries;
                 }
             }
         }
         if ($aliases) {
             $concepts = array_merge($concepts, $aliases);
         }
         if (!empty($concepts)) {
             foreach ($concepts as $key => $concept) {
                 // Trim empty or unlinkable concepts
                 $currentconcept = trim(strip_tags($concept->concept));
                 if (empty($currentconcept)) {
                     unset($concepts[$key]);
                     continue;
                 } else {
                     $concepts[$key]->concept = $currentconcept;
                 }
                 // Rule out any small integers.  See bug 1446
                 $currentint = intval($currentconcept);
                 if ($currentint && strval($currentint) == $currentconcept && $currentint < 1000) {
                     unset($concepts[$key]);
                 }
             }
         }
         if (empty($concepts)) {
             $nothingtodo = true;
             return $text;
         }
         usort($concepts, 'filter_glossary::sort_entries_by_length');
         $strcategory = get_string('category', 'glossary');
         // Loop through all the concepts, setting up our data structure for the filter
         $conceptlist = array();
         // We will store all the concepts here
         foreach ($concepts as $concept) {
             $glossaryname = str_replace(':', '-', $glossaries[$concept->glossaryid]);
             if ($concept->category) {
                 // Link to a category
                 // TODO: Fix this string usage
                 $title = strip_tags($glossaryname . ': ' . $strcategory . ' ' . $concept->concept);
                 $href_tag_begin = '<a class="glossary autolink category glossaryid' . $concept->glossaryid . '" title="' . $title . '" ' . 'href="' . $CFG->wwwroot . '/mod/glossary/view.php?g=' . $concept->glossaryid . '&amp;mode=cat&amp;hook=' . $concept->id . '">';
             } else {
                 // Link to entry or alias
                 if (!empty($concept->originalconcept)) {
                     // We are dealing with an alias (so show and point to original)
                     $title = str_replace('"', "'", strip_tags($glossaryname . ': ' . $concept->originalconcept));
                     $concept->id = $concept->entryid;
                 } else {
                     // This is an entry
                     $title = str_replace('"', "'", strip_tags($glossaryname . ': ' . $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('courseid' => $courseid, 'eid' => $concept->id, 'displayformat' => 'dictionary'));
                 $attributes = array('href' => $link, 'title' => $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 (isset($CFG->embeddedsoforcelinktarget) && $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);
         }
         $conceptlist = filter_remove_duplicates($conceptlist);
         if (empty($jsinitialised)) {
             // Add a JavaScript event to open popup's here. This only ever need to be
             // added once!
             $PAGE->requires->yui_module('moodle-filter_glossary-autolinker', 'M.filter_glossary.init_filter_autolinking', array(array('courseid' => $courseid)));
             $jsinitialised = true;
         }
     }
     if (!empty($GLOSSARY_EXCLUDECONCEPTS)) {
         $reducedconceptlist = array();
         foreach ($conceptlist as $concept) {
             if (!in_array($concept->phrase, $GLOSSARY_EXCLUDECONCEPTS)) {
                 $reducedconceptlist[] = $concept;
             }
         }
         return filter_phrases($text, $reducedconceptlist);
     }
     return filter_phrases($text, $conceptlist);
     // Actually search for concepts!
 }
Beispiel #3
0
 public function filter($text, array $options = array())
 {
     global $CFG, $DB;
     // Trivial-cache - keyed on $cachedcontextid
     static $cachedcontextid;
     static $contentlist;
     static $nothingtodo;
     // Try to get current course
     if (!($courseid = get_courseid_from_context($this->context))) {
         $courseid = 0;
     }
     // Initialise/invalidate our trivial cache if dealing with a different context
     if (!isset($cachedcontextid) || $cachedcontextid !== $this->context->id) {
         $cachedcontextid = $this->context->id;
         $contentlist = array();
         $nothingtodo = false;
     }
     if ($nothingtodo === true) {
         return $text;
     }
     // Create a list of all the resources to search for. It may be cached already.
     if (empty($contentlist)) {
         $coursestosearch = $courseid ? array($courseid) : array();
         // Add courseid if found
         if (get_site()->id != $courseid) {
             // Add siteid if was not courseid
             $coursestosearch[] = get_site()->id;
         }
         // We look for text field contents only if have autolink enabled (param1)
         list($coursesql, $params) = $DB->get_in_or_equal($coursestosearch);
         $sql = 'SELECT dc.id AS contentid, dr.id AS recordid, dc.content AS content, d.id AS dataid
                   FROM {data} d
                   JOIN {data_fields} df ON df.dataid = d.id
                   JOIN {data_records} dr ON dr.dataid = d.id
                   JOIN {data_content} dc ON dc.fieldid = df.id AND dc.recordid = dr.id
                  WHERE d.course ' . $coursesql . '
                    AND df.type = \'text\'
                    AND ' . $DB->sql_compare_text('df.param1', 1) . " = '1'";
         if (!($contents = $DB->get_records_sql($sql, $params))) {
             $nothingtodo = true;
             return $text;
         }
         foreach ($contents as $key => $content) {
             // Trim empty or unlinkable concepts
             $currentcontent = trim(strip_tags($content->content));
             if (empty($currentcontent)) {
                 unset($contents[$key]);
                 continue;
             } else {
                 $contents[$key]->content = $currentcontent;
             }
             // Rule out any small integers.  See bug 1446
             $currentint = intval($currentcontent);
             if ($currentint && strval($currentint) == $currentcontent && $currentint < 1000) {
                 unset($contents[$key]);
             }
         }
         if (empty($contents)) {
             $nothingtodo = true;
             return $text;
         }
         usort($contents, 'filter_data::sort_entries_by_length');
         foreach ($contents as $content) {
             $href_tag_begin = '<a class="data autolink dataid' . $content->dataid . '" title="' . $content->content . '" ' . 'href="' . $CFG->wwwroot . '/mod/data/view.php?d=' . $content->dataid . '&amp;rid=' . $content->recordid . '">';
             $contentlist[] = new filterobject($content->content, $href_tag_begin, '</a>', false, true);
         }
         $contentlist = filter_remove_duplicates($contentlist);
         // Clean dupes
     }
     return filter_phrases($text, $contentlist);
     // Look for all these links in the text
 }