function data_filter($courseid, $text)
{
    global $CFG;
    static $nothingtodo;
    static $contentlist;
    if (!empty($nothingtodo)) {
        // We've been here in this page already
        return $text;
    }
    // if we don't have a courseid, we can't run the query, so
    if (empty($courseid)) {
        return $text;
    }
    // Create a list of all the resources to search for. It may be cached already.
    if (empty($contentlist)) {
        // We look for text field contents only, and only if the field has
        // autolink enabled (param1).
        $sql = 'SELECT dc.id AS contentid, ' . 'dr.id AS recordid, ' . 'dc.content AS content, ' . 'd.id AS dataid ' . 'FROM ' . $CFG->prefix . 'data d, ' . $CFG->prefix . 'data_fields df, ' . $CFG->prefix . 'data_records dr, ' . $CFG->prefix . 'data_content dc ' . "WHERE (d.course = '{$courseid}' or d.course = '" . SITEID . "')" . 'AND d.id = df.dataid ' . 'AND df.id = dc.fieldid ' . 'AND d.id = dr.dataid ' . 'AND dr.id = dc.recordid ' . "AND df.type = 'text' " . "AND " . sql_compare_text('df.param1', 1) . " = '1'";
        if (!($datacontents = get_records_sql($sql))) {
            return $text;
        }
        $contentlist = array();
        foreach ($datacontents as $datacontent) {
            $currentcontent = trim($datacontent->content);
            $strippedcontent = strip_tags($currentcontent);
            if (!empty($strippedcontent)) {
                $contentlist[] = new filterobject($currentcontent, '<a class="data autolink" title="' . $strippedcontent . '" href="' . $CFG->wwwroot . '/mod/data/view.php?d=' . $datacontent->dataid . '&amp;rid=' . $datacontent->recordid . '" ' . $CFG->frametarget . '>', '</a>', false, true);
            }
        }
        // End foreach
    }
    return filter_phrases($text, $contentlist);
    // Look for all these links in the text
}
Beispiel #2
0
 function filter($text, array $options = array())
 {
     static $words;
     global $CFG;
     if (!isset($CFG->filter_censor_badwords)) {
         set_config('filter_censor_badwords', '');
     }
     if (empty($words)) {
         $words = array();
         if (empty($CFG->filter_censor_badwords)) {
             $badwords = explode(',', get_string('badwords', 'filter_censor'));
         } else {
             $badwords = explode(',', $CFG->filter_censor_badwords);
         }
         foreach ($badwords as $badword) {
             $badword = trim($badword);
             if ($this->_canseecensor()) {
                 $words[] = new filterobject($badword, '<span class="censoredtexthighlight" title="' . $badword . '">', '</span>', false, false, $badword);
             } else {
                 $words[] = new filterobject($badword, '<span class="censoredtext" title="' . $badword . '">', '</span>', false, false, str_pad('', strlen($badword), '*'));
             }
         }
     }
     return filter_phrases($text, $words);
 }
Beispiel #3
0
function activitynames_filter($courseid, $text)
{
    global $CFG, $COURSE;
    // Trivial-cache - keyed on $cachedcourseid
    static $activitylist = null;
    static $cachedcourseid;
    if (empty($courseid)) {
        $courseid = SITEID;
    }
    // Initialise/invalidate our trivial cache if dealing with a different course
    if (!isset($cachedcourseid) || $cachedcourseid !== (int) $courseid) {
        $activitylist = null;
    }
    $cachedcourseid = (int) $courseid;
    /// It may be cached
    if (is_null($activitylist)) {
        $activitylist = array();
        if ($COURSE->id == $courseid) {
            $course = $COURSE;
        } else {
            $course = get_record("course", "id", $courseid);
        }
        if (!isset($course->modinfo)) {
            return $text;
        }
        /// Casting $course->modinfo to string prevents one notice when the field is null
        $modinfo = unserialize((string) $course->modinfo);
        if (!empty($modinfo)) {
            $activitylist = array();
            /// We will store all the activities here
            //Sort modinfo by name length
            usort($modinfo, 'comparemodulenamesbylength');
            foreach ($modinfo as $activity) {
                //Exclude labels, hidden activities and activities for group members only
                if ($activity->mod != "label" and $activity->visible and empty($activity->groupmembersonly)) {
                    $title = s(trim(strip_tags(urldecode($activity->name))));
                    $currentname = trim(urldecode($activity->name));
                    $entitisedname = s($currentname);
                    /// Avoid empty or unlinkable activity names
                    if (!empty($title)) {
                        $href_tag_begin = "<a class=\"autolink\" title=\"{$title}\" href=\"{$CFG->wwwroot}/mod/{$activity->mod}/view.php?id={$activity->cm}\" {$CFG->frametarget}>";
                        $activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
                        if ($currentname != $entitisedname) {
                            /// If name has some entity (&amp; &quot; &lt; &gt;) add that filter too. MDL-17545
                            $activitylist[] = new filterobject($entitisedname, $href_tag_begin, '</a>', false, true);
                        }
                    }
                }
            }
        }
    }
    if ($activitylist) {
        return $text = filter_phrases($text, $activitylist);
    } else {
        return $text;
    }
}
Beispiel #4
0
 function filter($text, array $options = array())
 {
     if (!($courseid = get_courseid_from_context($this->context))) {
         return $text;
     }
     // Initialise/invalidate our trivial cache if dealing with a different course
     if (!isset(self::$cachedcourseid) || self::$cachedcourseid !== (int) $courseid) {
         self::$activitylist = null;
     }
     self::$cachedcourseid = (int) $courseid;
     /// It may be cached
     if (is_null(self::$activitylist)) {
         self::$activitylist = array();
         $modinfo = get_fast_modinfo($courseid);
         if (!empty($modinfo->cms)) {
             self::$activitylist = array();
             /// We will store all the activities here
             //Sort modinfo by name length
             $sortedactivities = fullclone($modinfo->cms);
             usort($sortedactivities, 'filter_activitynames_comparemodulenamesbylength');
             foreach ($sortedactivities as $cm) {
                 //Exclude labels, hidden activities and activities for group members only
                 if ($cm->visible and empty($cm->groupmembersonly) and $cm->has_view()) {
                     $title = s(trim(strip_tags($cm->name)));
                     $currentname = trim($cm->name);
                     $entitisedname = s($currentname);
                     /// Avoid empty or unlinkable activity names
                     if (!empty($title)) {
                         $href_tag_begin = html_writer::start_tag('a', array('class' => 'autolink', 'title' => $title, 'href' => $cm->get_url()));
                         self::$activitylist[$cm->id] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
                         if ($currentname != $entitisedname) {
                             /// If name has some entity (&amp; &quot; &lt; &gt;) add that filter too. MDL-17545
                             self::$activitylist[$cm->id . '-e'] = new filterobject($entitisedname, $href_tag_begin, '</a>', false, true);
                         }
                     }
                 }
             }
         }
     }
     $filterslist = array();
     if (self::$activitylist) {
         $cmid = $this->context->instanceid;
         if ($this->context->contextlevel == CONTEXT_MODULE && isset(self::$activitylist[$cmid])) {
             // remove filterobjects for the current module
             $filterslist = array_diff_key(self::$activitylist, array($cmid => 1, $cmid . '-e' => 1));
         } else {
             $filterslist = self::$activitylist;
         }
     }
     if ($filterslist) {
         return $text = filter_phrases($text, $filterslist);
     } else {
         return $text;
     }
 }
Beispiel #5
0
function wiki_filter($courseid, $text)
{
    global $CFG, $DB;
    // Trivial-cache - keyed on $cachedcourseid
    static $nothingtodo;
    static $wikipagelist;
    static $cachedcourseid;
    if (empty($courseid)) {
        $courseid = SITEID;
    }
    // Initialise/invalidate our trivial cache if dealing with a different course
    if (!isset($cachedcourseid) || $cachedcourseid !== (int) $courseid) {
        $wikipagelist = array();
        $nothingtodo = false;
    }
    $cachedcourseid = (int) $courseid;
    if (!empty($nothingtodo)) {
        // We've been here in this page already
        return $text;
    }
    /// Create a list of all the wikis to search for.  It may be cached already.
    if (empty($wikipagelist)) {
        /// Get all wikis for this course.
        if (!($wikis = wiki_get_course_wikis($courseid))) {
            $nothingtodo = true;
            return $text;
        }
        $wikipagelist = array();
        /// Walk through each wiki, and get entries.
        foreach ($wikis as $wiki) {
            if ($wiki_entries = wiki_get_entries($wiki)) {
                /// Walk through each entry and get the pages.
                foreach ($wiki_entries as $wiki_entry) {
                    if ($wiki_pages = $DB->get_records('wiki_pages', array('wiki' => $wiki_entry->id), 'pagename, version DESC')) {
                        /// Walk through each page and filter.
                        $wikientries = array();
                        foreach ($wiki_pages as $wiki_page) {
                            if (!in_array($wiki_page->pagename, $wikientries)) {
                                $startlink = '<a class="wiki autolink" title="Wiki" href="' . $CFG->wwwroot . '/mod/wiki/view.php?wid=' . $wiki->id . '&amp;userid=' . $wiki_entry->userid . '&amp;groupid=' . $wiki_entry->groupid . '&amp;page=' . $wiki_page->pagename . '">';
                                $wikipagelist[] = new filterobject($wiki_page->pagename, $startlink, '</a>', false, true);
                                $wikientries[] = $wiki_page->pagename;
                            }
                        }
                    }
                }
            }
        }
    }
    return filter_phrases($text, $wikipagelist);
}
Beispiel #6
0
function resource_filter($courseid, $text)
{
    global $CFG;
    // Trivial-cache - keyed on $cachedcourseid
    static $nothingtodo;
    static $resourcelist;
    static $cachedcourseid;
    // if we don't have a courseid, we can't run the query, so
    if (empty($courseid)) {
        return $text;
    }
    // Initialise/invalidate our trivial cache if dealing with a different course
    if (!isset($cachedcourseid) || $cachedcourseid !== (int) $courseid) {
        $resourcelist = array();
        $nothingtodo = false;
    }
    $cachedcourseid = (int) $courseid;
    if ($nothingtodo === true) {
        return $text;
    }
    /// Create a list of all the resources to search for.  It may be cached already.
    if (empty($resourcelist)) {
        /* get all non-hidden resources from this course
         * sorted from long to short so longer ones can be 
         * linked first. And order by section so we try to 
         * link to the top resource first.
         */
        $resource_sql = "SELECT r.id, r.name \n                FROM {$CFG->prefix}resource r, \n                     {$CFG->prefix}course_modules cm, \n                     {$CFG->prefix}modules m\n                WHERE m.name = 'resource' AND\n                        cm.module = m.id AND\n                        cm.visible =  1 AND\n                        r.id = cm.instance AND\n                        cm.course = {$courseid}\n                ORDER BY " . sql_length('r.name') . " DESC, cm.section ASC";
        if (!($resources = get_records_sql($resource_sql))) {
            $nothingtodo = true;
            return $text;
        }
        $resourcelist = array();
        foreach ($resources as $resource) {
            $currentname = trim($resource->name);
            $entitisedname = s($currentname);
            $strippedname = strip_tags($currentname);
            /// Avoid empty or unlinkable resource names
            if (!empty($strippedname)) {
                $resourcelist[] = new filterobject($currentname, '<a class="resource autolink" title="' . $strippedname . '" href="' . $CFG->wwwroot . '/mod/resource/view.php?r=' . $resource->id . '" ' . $CFG->frametarget . '>', '</a>', false, true);
                if ($currentname != $entitisedname) {
                    /// If name has some entity (&amp; &quot; &lt; &gt;) add that filter too. MDL-17518
                    $resourcelist[] = new filterobject($entitisedname, '<a class="resource autolink" title="' . $strippedname . '" href="' . $CFG->wwwroot . '/mod/resource/view.php?r=' . $resource->id . '" ' . $CFG->frametarget . '>', '</a>', false, true);
                }
            }
        }
    }
    return filter_phrases($text, $resourcelist);
    // Look for all these links in the text
}
Beispiel #7
0
function resource_filter($courseid, $text)
{
    global $CFG;
    // Trivial-cache - keyed on $cachedcourseid
    static $nothingtodo;
    static $resourcelist;
    static $cachedcourseid;
    // if we don't have a courseid, we can't run the query, so
    if (empty($courseid)) {
        return $text;
    }
    // Initialise/invalidate our trivial cache if dealing with a different course
    if (!isset($cachedcourseid) || $cachedcourseid !== (int) $courseid) {
        $resourcelist = array();
        $nothingtodo = false;
    }
    $cachedcourseid = (int) $courseid;
    if ($nothingtodo === true) {
        return $text;
    }
    /// Create a list of all the resources to search for.  It may be cached already.
    if (empty($resourcelist)) {
        /// The resources are sorted from long to short so longer ones can be linked first.
        if (!($resources = get_records('resource', 'course', $courseid, 'CHAR_LENGTH(name) DESC', 'id,name'))) {
            $nothingtodo = true;
            return $text;
        }
        $resourcelist = array();
        foreach ($resources as $resource) {
            $currentname = trim($resource->name);
            $entitisedname = s($currentname);
            $strippedname = strip_tags($currentname);
            /// Avoid empty or unlinkable resource names
            if (!empty($strippedname)) {
                $resourcelist[] = new filterobject($currentname, '<a class="resource autolink" title="' . $strippedname . '" href="' . $CFG->wwwroot . '/mod/resource/view.php?r=' . $resource->id . '" ' . $CFG->frametarget . '>', '</a>', false, true);
                if ($currentname != $entitisedname) {
                    /// If name has some entity (&amp; &quot; &lt; &gt;) add that filter too. MDL-17518
                    $resourcelist[] = new filterobject($entitisedname, '<a class="resource autolink" title="' . $strippedname . '" href="' . $CFG->wwwroot . '/mod/resource/view.php?r=' . $resource->id . '" ' . $CFG->frametarget . '>', '</a>', false, true);
                }
            }
        }
    }
    return filter_phrases($text, $resourcelist);
    // Look for all these links in the text
}
Beispiel #8
0
function activitynames_filter($courseid, $text)
{
    global $CFG;
    // Trivial-cache - keyed on $cachedcourseid
    static $activitylist;
    static $cachedcourseid;
    if (empty($courseid)) {
        $courseid = SITEID;
    }
    // Initialise/invalidate our trivial cache if dealing with a different course
    if (!isset($cachedcourseid) || $cachedcourseid !== (int) $courseid) {
        $activitylist = array();
    }
    $cachedcourseid = (int) $courseid;
    /// It may be cached
    if (empty($activitylist)) {
        $course = get_record("course", "id", $courseid);
        /// Casting $course->modinfo to string prevents one notice when the field is null
        $modinfo = unserialize((string) $course->modinfo);
        if (!empty($modinfo)) {
            $activitylist = array();
            /// We will store all the activities here
            //Sort modinfo by name length
            usort($modinfo, 'comparemodulenamesbylength');
            foreach ($modinfo as $activity) {
                //Exclude labels and hidden items
                if ($activity->mod != "label" && $activity->visible) {
                    $title = trim(strip_tags(urldecode($activity->name)));
                    /// Avoid empty or unlinkable activity names
                    if (!empty($title)) {
                        $title = str_replace('"', "'", $title);
                        $href_tag_begin = "<a class=\"autolink\" title=\"{$title}\" href=\"{$CFG->wwwroot}/mod/{$activity->mod}/view.php?id={$activity->cm}\" {$CFG->frametarget}>";
                        $currentname = urldecode($activity->name);
                        if ($currentname = trim($currentname)) {
                            $activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
                        }
                    }
                }
            }
        }
    }
    return $text = filter_phrases($text, $activitylist);
}
Beispiel #9
0
function censor_filter($courseid, $text)
{
    static $words;
    global $CFG;
    if (!isset($CFG->filter_censor_badwords)) {
        set_config('filter_censor_badwords', '');
    }
    if (empty($words)) {
        $words = array();
        // if no user-specified words, use default list from language pack
        if (empty($CFG->filter_censor_badwords)) {
            $badwords = explode(',', get_string('badwords', 'censor'));
        } else {
            $badwords = explode(',', $CFG->filter_censor_badwords);
        }
        foreach ($badwords as $badword) {
            $badword = trim($badword);
            $words[] = new filterobject($badword, '<span class="censoredtext" title="' . $badword . '">', '</span>', false, false, str_pad('', strlen($badword), '*'));
        }
    }
    return filter_phrases($text, $words);
    // Look for all these words in the text
}
Beispiel #10
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 #11
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 #12
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
 }
Beispiel #13
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('&amp;', '&', $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!
 }
Beispiel #14
0
 function filter($text, array $options = array())
 {
     $coursectx = $this->context->get_course_context(false);
     if (!$coursectx) {
         return $text;
     }
     $courseid = $coursectx->instanceid;
     // Initialise/invalidate our trivial cache if dealing with a different course
     if (!isset(self::$cachedcourseid) || self::$cachedcourseid !== (int) $courseid) {
         self::$activitylist = null;
     }
     self::$cachedcourseid = (int) $courseid;
     /// It may be cached
     if (is_null(self::$activitylist)) {
         self::$activitylist = array();
         $modinfo = get_fast_modinfo($courseid);
         if (!empty($modinfo->cms)) {
             self::$activitylist = array();
             // We will store all the created filters here.
             // Create array of visible activities sorted by the name length (we are only interested in properties name and url).
             $sortedactivities = array();
             foreach ($modinfo->cms as $cm) {
                 // Exclude labels, hidden activities and activities for group members only.
                 if ($cm->visible and empty($cm->groupmembersonly) and $cm->has_view()) {
                     $sortedactivities[] = (object) array('name' => $cm->name, 'url' => $cm->url, 'id' => $cm->id, 'namelen' => strlen($cm->name));
                 }
             }
             core_collator::asort_objects_by_property($sortedactivities, 'namelen', SORT_NUMERIC);
             foreach ($sortedactivities as $cm) {
                 $title = s(trim(strip_tags($cm->name)));
                 $currentname = trim($cm->name);
                 $entitisedname = s($currentname);
                 // Avoid empty or unlinkable activity names.
                 if (!empty($title)) {
                     $href_tag_begin = html_writer::start_tag('a', array('class' => 'autolink', 'title' => $title, 'href' => $cm->url));
                     self::$activitylist[$cm->id] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
                     if ($currentname != $entitisedname) {
                         // If name has some entity (&amp; &quot; &lt; &gt;) add that filter too. MDL-17545.
                         self::$activitylist[$cm->id . '-e'] = new filterobject($entitisedname, $href_tag_begin, '</a>', false, true);
                     }
                 }
             }
         }
     }
     $filterslist = array();
     if (self::$activitylist) {
         $cmid = $this->context->instanceid;
         if ($this->context->contextlevel == CONTEXT_MODULE && isset(self::$activitylist[$cmid])) {
             // remove filterobjects for the current module
             $filterslist = array_values(array_diff_key(self::$activitylist, array($cmid => 1, $cmid . '-e' => 1)));
         } else {
             $filterslist = array_values(self::$activitylist);
         }
     }
     if ($filterslist) {
         return $text = filter_phrases($text, $filterslist);
     } else {
         return $text;
     }
 }