[LON-CAPA-cvs] cvs: loncom /interface lonnavmaps.pm

bowersj2 lon-capa-cvs@mail.lon-capa.org
Fri, 21 Feb 2003 20:05:00 -0000


bowersj2		Fri Feb 21 15:05:00 2003 EDT

  Modified files:              
    /loncom/interface	lonnavmaps.pm 
  Log:
  Added getResourceByUrl, which gets a resource object for the given
  URL. (If you're reading this, please check my usage of ::clutter to 
  make sure it's right.)
  
  An abortive retrieveResources method can be seen; I'll finish that here
  in a bit but I wanted to commit this in, so I can reference this to
  H. K. Ng.
  
  
  
Index: loncom/interface/lonnavmaps.pm
diff -u loncom/interface/lonnavmaps.pm:1.144 loncom/interface/lonnavmaps.pm:1.145
--- loncom/interface/lonnavmaps.pm:1.144	Thu Feb 20 17:08:55 2003
+++ loncom/interface/lonnavmaps.pm	Fri Feb 21 15:05:00 2003
@@ -2,7 +2,7 @@
 # The LearningOnline Network with CAPA
 # Navigate Maps Handler
 #
-# $Id: lonnavmaps.pm,v 1.144 2003/02/20 22:08:55 bowersj2 Exp $
+# $Id: lonnavmaps.pm,v 1.145 2003/02/21 20:05:00 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -533,6 +533,8 @@
 
 In addition to the parameters you can pass to the renderer, which will be passed through unchange to the column renderers, the renderer will generate the following information which your renderer may find useful:
 
+If you want to know how many rows were printed, the 'counter' element of the hash passed into the render function will contain the count. You may want to check whether any resources were printed at all.
+
 =over 4
 
 =back
@@ -941,9 +943,7 @@
         # See if we're being passed a specific map
         if ($args->{'iterator_map'}) {
             my $map = $args->{'iterator_map'};
-            $map = &Apache::lonnet::clutter($map);
-            $map = $navmap->{NAV_HASH}->{'ids_' . $map};
-            $map = $navmap->getById($map);
+            $map = $navmap->getResourceByUrl($map);
             my $firstResource = $map->map_start();
             my $finishResource = $map->map_finish();
 
@@ -1611,6 +1611,57 @@
         }
     }
     return '';
+}
+
+=pod 
+
+=item * B<getResourceByUrl>(url): Retrieves a resource object by URL of the resource. If passed a resource object, it will simply return it, so it is safe to use this method in code like "$res = $navmap->getResourceByUrl($res)", if you're not sure if $res is already an object, or just a URL. If the resource appears multiple times in the course, only the first instance will be returned. As a result, this is probably useful only for maps.
+
+=item * B<retrieveResources>(map, filterFunc, recursive, bailout): The map is a specification of a map to retreive the resources from, either as a url or as an object. The filterFunc is a reference to a function that takes a resource object as its one argument and returns true if the resource should be included, or false if it should not be. If recursive is true, the map will be recursively examined, otherwise it will not be. If bailout is true, the function will return as soon as it finds a resource, if false it will finish. By default, the map is the top-level map of the course, filterFunc is a function that always returns 1, recursive is true, bailout is false. The resources will be returned in a list reference containing the resource objects for the corresponding resources, with B<no structure information> in the list; regardless of branching, recursion, etc., it will be a flat list. 
+
+Thus, this is suitable for cases where you don't want the structure, just a list of all resources. It is also suitable for finding out how many resources match a given description; for this use, if all you want to know is if I<any> resources match the description, the bailout parameter will allow you to avoid potentially expensive enumeration of all matching resources.
+
+=cut
+
+sub getResourceByUrl {
+    my $self = shift;
+    my $resUrl = shift;
+
+    if (ref($resUrl)) { return $resUrl; }
+
+    $resUrl = &Apache::lonnet::clutter($resUrl);
+    my $resId = $self->{NAV_HASH}->{'ids_' . $resUrl};
+    if ($resId =~ /,/) {
+        $resId = (split (/,/, $resId))[0];
+    }
+    if (!$resId) { return ''; }
+    return $self->getById($resId);
+}
+
+sub retrieveResources {
+    my $self = shift;
+    my $map = shift;
+    my $filterFunc = shift;
+    if (!defined ($filterFunc)) {
+        $filterFunc = sub {return 1;};
+    }
+    my $recursive = shift;
+    if (!defined($recursive)) { $recursive = 1; }
+    my $bailout = shift;
+    if (!defined($bailout)) { $bailout = 0; }
+
+    # Create the necessary iterator.
+    if (!ref($map)) { # assume it's a url of a map.
+        $map = $self->getMapByUrl($map);
+    }
+
+    # Check the map's validity.
+    if (!$map || !$map->is_map()) {
+        # Oh, to throw an exception.... how I'd love that!
+        return ();
+    }
+
+    # UNFINISHED... I was checking in getResourceByUrl
 }
 
 1;