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

bowersj2 lon-capa-cvs@mail.lon-capa.org
Fri, 21 Feb 2003 21:40:07 -0000


bowersj2		Fri Feb 21 16:40:07 2003 EDT

  Modified files:              
    /loncom/interface	lonnavmaps.pm 
  Log:
  Functions to automatically retrieve a list of resource meeting some
  criterion, and a convience function to ask if *any* resources meet that 
  criterion.
  
  Intended first use: Filtering out things in the print wizard that
  will present choices that have no possible selections. For instance, you 
  can print "selected problems" from the given sequence, but the sequence
  may have no problems. It is better to tell the user that before they
  ask for "selected problems", so they are not annoyed at the program.
  
  
  
Index: loncom/interface/lonnavmaps.pm
diff -u loncom/interface/lonnavmaps.pm:1.145 loncom/interface/lonnavmaps.pm:1.146
--- loncom/interface/lonnavmaps.pm:1.145	Fri Feb 21 15:05:00 2003
+++ loncom/interface/lonnavmaps.pm	Fri Feb 21 16:40:07 2003
@@ -2,7 +2,7 @@
 # The LearningOnline Network with CAPA
 # Navigate Maps Handler
 #
-# $Id: lonnavmaps.pm,v 1.145 2003/02/21 20:05:00 bowersj2 Exp $
+# $Id: lonnavmaps.pm,v 1.146 2003/02/21 21:40:07 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1621,6 +1621,12 @@
 
 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.
 
+=item * B<hasResources>(map, filterFunc, recursive): Convience method for 
+
+ scalar(retrieveResources($map, $filterFunc, $recursive, 1)) > 0
+
+which will tell whether the map has resources matching the description in the filter function.
+
 =cut
 
 sub getResourceByUrl {
@@ -1661,7 +1667,50 @@
         return ();
     }
 
-    # UNFINISHED... I was checking in getResourceByUrl
+    # Get an iterator.
+    my $it = $self->getIterator($map->map_start(), $map->map_finish(),
+                                !$recursive);
+
+    my @resources = ();
+
+    # Run down the iterator and collect the resources.
+    my $depth = 1;
+    $it->next();
+    my $curRes = $it->next();
+
+    while ($depth > 0) {
+        if ($curRes == $it->BEGIN_MAP()) {
+            $depth++;
+        }
+        if ($curRes == $it->END_MAP()) {
+            $depth--;
+        }
+        
+        if (ref($curRes)) {
+            if (!&$filterFunc($curRes)) {
+                next;
+            }
+
+            push @resources, $curRes;
+
+            if ($bailout) {
+                return @resources;
+            }
+        }
+
+        $curRes = $it->next();
+    }
+
+    return @resources;
+}
+
+sub hasResource {
+    my $self = shift;
+    my $map = shift;
+    my $filterFunc = shift;
+    my $recursive = shift;
+    
+    return scalar($self->retrieveResources($map, $filterFunc, $recursive, 1)) > 0;
 }
 
 1;