[ Index ]

PHP Cross Reference of Moodle

title

Body

[close]

/mod/resource/ -> filter.php (source)

   1  <?php // $Id: filter.php,v 1.17.2.1 2007/03/19 06:32:24 martinlanghoff Exp $
   2      //This function provides automatic linking to
   3      //resources when its name (title) is found inside every Moodle text
   4      //Williams, Stronk7, Martin D
   5  
   6      function resource_filter($courseid, $text) {
   7  
   8          global $CFG;
   9  
  10          // Trivial-cache - keyed on $cachedcourseid
  11          static $nothingtodo;
  12          static $resourcelist;
  13          static $cachedcourseid;
  14  
  15          // if we don't have a courseid, we can't run the query, so
  16          if (empty($courseid)) {
  17              return $text;
  18          }
  19  
  20          // Initialise/invalidate our trivial cache if dealing with a different course
  21          if (!isset($cachedcourseid) || $cachedcourseid !== (int)$courseid) {
  22              $resourcelist = array();
  23              $nothingtodo = false;
  24          } 
  25          $cachedcourseid = (int)$courseid;
  26  
  27          if ($nothingtodo === true) {
  28              return $text;
  29          }
  30          
  31      /// Create a list of all the resources to search for.  It may be cached already.
  32  
  33          if (empty($resourcelist)) {
  34  
  35          /// The resources are sorted from long to short so longer ones can be linked first.
  36  
  37              if (!$resources = get_records('resource', 'course', $courseid, 'CHAR_LENGTH(name) DESC', 'id,name')) {
  38                  $nothingtodo = true;
  39                  return $text;
  40              }
  41  
  42              $resourcelist = array();
  43  
  44              foreach ($resources as $resource) {
  45                  $currentname = trim($resource->name);
  46                  $strippedname = strip_tags($currentname);
  47                  /// Avoid empty or unlinkable resource names
  48                  if (!empty($strippedname)) {
  49                      $resourcelist[] = new filterobject($currentname,
  50                              '<a class="resource autolink" title="'.$strippedname.'" href="'.
  51                               $CFG->wwwroot.'/mod/resource/view.php?r='.$resource->id.'" '.$CFG->frametarget.'>', 
  52                               '</a>', false, true);
  53                  }
  54              }
  55  
  56          }
  57          return  filter_phrases($text, $resourcelist);  // Look for all these links in the text
  58      }
  59  
  60  ?>


Generated: Wed Nov 14 17:38:08 2007 Cross-referenced by PHPXref 0.7