|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00028 defined('MOODLE_INTERNAL') || die(); 00029 00033 class filter_activitynames extends moodle_text_filter { 00034 // Trivial-cache - keyed on $cachedcourseid 00035 static $activitylist = null; 00036 static $cachedcourseid; 00037 00038 function filter($text, array $options = array()) { 00039 global $CFG, $COURSE, $DB; 00040 00041 if (!$courseid = get_courseid_from_context($this->context)) { 00042 return $text; 00043 } 00044 00045 // Initialise/invalidate our trivial cache if dealing with a different course 00046 if (!isset($this->cachedcourseid) || $this->cachedcourseid !== (int)$courseid) { 00047 $this->activitylist = null; 00048 } 00049 $this->cachedcourseid = (int)$courseid; 00050 00052 00053 if (is_null($this->activitylist)) { 00054 $this->activitylist = array(); 00055 00056 if ($COURSE->id == $courseid) { 00057 $course = $COURSE; 00058 } else { 00059 $course = $DB->get_record("course", array("id"=>$courseid)); 00060 } 00061 00062 if (!isset($course->modinfo)) { 00063 return $text; 00064 } 00065 00067 $modinfo = unserialize((string)$course->modinfo); 00068 00069 if (!empty($modinfo)) { 00070 00071 $this->activitylist = array(); 00072 00073 //Sort modinfo by name length 00074 usort($modinfo, 'filter_activitynames_comparemodulenamesbylength'); 00075 00076 foreach ($modinfo as $activity) { 00077 //Exclude labels, hidden activities and activities for group members only 00078 if ($activity->mod != "label" and $activity->visible and empty($activity->groupmembersonly)) { 00079 $title = s(trim(strip_tags($activity->name))); 00080 $currentname = trim($activity->name); 00081 $entitisedname = s($currentname); 00083 if (!empty($title)) { 00084 $href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\">"; 00085 $this->activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true); 00086 if ($currentname != $entitisedname) { 00087 $this->activitylist[] = new filterobject($entitisedname, $href_tag_begin, '</a>', false, true); 00088 } 00089 } 00090 } 00091 } 00092 } 00093 } 00094 00095 if ($this->activitylist) { 00096 return $text = filter_phrases ($text, $this->activitylist); 00097 } else { 00098 return $text; 00099 } 00100 } 00101 } 00102 00103 00104 00105 //This function is used to order module names from longer to shorter 00106 function filter_activitynames_comparemodulenamesbylength($a, $b) { 00107 if (strlen($a->name) == strlen($b->name)) { 00108 return 0; 00109 } 00110 return (strlen($a->name) < strlen($b->name)) ? 1 : -1; 00111 } 00112