Diff for /loncom/interface/lonnavmaps.pm between versions 1.142 and 1.155

version 1.142, 2003/02/07 22:17:51 version 1.155, 2003/03/08 21:16:38
Line 38 Line 38
 # YEAR=2002  # YEAR=2002
 # 1/1 Gerd Kortemeyer  # 1/1 Gerd Kortemeyer
 # Oct-Nov Jeremy Bowers  # Oct-Nov Jeremy Bowers
   # YEAR=2003
   # Jeremy Bowers ... lots of days
   
 package Apache::lonnavmaps;  package Apache::lonnavmaps;
   
 use strict;  use strict;
 use Apache::Constants qw(:common :http);  use Apache::Constants qw(:common :http);
 use Apache::loncommon();  use Apache::loncommon();
   use Apache::lonmenu();
 use POSIX qw (floor strftime);  use POSIX qw (floor strftime);
   
 my %navmaphash;  my %navmaphash;
Line 152  sub real_handler { Line 155  sub real_handler {
     }      }
   
     $r->print("<html><head>\n");      $r->print("<html><head>\n");
     $r->print("<title>Navigate Course Contents</title></head>");      $r->print("<title>Navigate Course Contents</title>");
   # ------------------------------------------------------------ Get query string
       &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['register']);
   # ----------------------------------------------------- Force menu registration
       my $addentries='';
       if ($ENV{'form.register'}) {
          $addentries=' onLoad="'.&Apache::lonmenu::loadevents().
      '" onUnload="'.&Apache::lonmenu::unloadevents().'"';
          $r->print(&Apache::lonmenu::registerurl(1));
       }
   
     # Header      # Header
     $r->print(&Apache::loncommon::bodytag('Navigate Course Contents','',      $r->print('</head>'.
                                           ''));                &Apache::loncommon::bodytag('Navigate Course Contents','',
                                       $addentries,'','',$ENV{'form.register'}));
     $r->print('<script>window.focus();</script>');      $r->print('<script>window.focus();</script>');
   
     $r->rflush();      $r->rflush();
Line 175  sub real_handler { Line 188  sub real_handler {
         return OK;          return OK;
     }      }
   
       # See if there's only one map in the top-level... if so,
       # automatically display it
       my $iterator = $navmap->getIterator(undef, undef, undef, 0);
       my $depth = 1;
       $iterator->next();
       my $curRes = $iterator->next();
       my $sequenceCount = 0;
       my $sequenceId;
       while ($depth > 0) {
           if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
           if ($curRes == $iterator->END_MAP()) { $depth--; }
   
           if (ref($curRes) && $curRes->is_sequence()) {
               $sequenceCount++;
               $sequenceId = $curRes->map_pc();
           }
   
           $curRes = $iterator->next();
       }
   
       if ($sequenceCount == 1) {
           # The automatic iterator creation in the render call 
           # will pick this up.
           $ENV{'form.filter'} = "$sequenceId";
       }
   
     # renderer call      # renderer call
     my $render = render({ 'cols' => [0,1,2,3],      my $render = render({ 'cols' => [0,1,2,3],
                           'url' => '/adm/navmaps',                            'url' => '/adm/navmaps',
                           'printKey' => 1,                            'suppressNavmap' => 1,
                           'r' => $r});                            'r' => $r});
   
     $navmap->untieHashes();      $navmap->untieHashes();
Line 463  The pre-packaged column names are refere Line 502  The pre-packaged column names are refere
   
 =item * B<resource_part_count>: If true (default), the resource will show a part count if the full part list is not displayed. If false, the resource will never show a part count.  =item * B<resource_part_count>: If true (default), the resource will show a part count if the full part list is not displayed. If false, the resource will never show a part count.
   
   =item * B<resource_no_folder_link>: If true, the resource's folder will not be clickable to open or close it. Default is false.
   
 =back  =back
   
 =item B<communication_status>: Whether there is discussion on the resource, email for the user, or (lumped in here) perl errors in the execution of the problem. This is the second column in the main nav map.  =item B<communication_status>: Whether there is discussion on the resource, email for the user, or (lumped in here) perl errors in the execution of the problem. This is the second column in the main nav map.
Line 491  Most of these parameters are only useful Line 532  Most of these parameters are only useful
   
 =over 4  =over 4
   
 =item * B<iterator>: A reference to a fresh ::iterator to use from the navmaps. The rendering will reflect the options passed to the iterator, so you can use that to just render a certain part of the course, if you like. If one is not passed, the renderer will attempt to construct one from ENV{'form.filter'} and ENV{'form.condition'} information.  =item * B<iterator>: A reference to a fresh ::iterator to use from the navmaps. The rendering will reflect the options passed to the iterator, so you can use that to just render a certain part of the course, if you like. If one is not passed, the renderer will attempt to construct one from ENV{'form.filter'} and ENV{'form.condition'} information, plus the 'iterator_map' parameter if any.
   
   =item * B<iterator_map>: If you are letting the renderer do the iterator handling, you can instruct the renderer to render only a particular map by passing it the source of the map you want to process, like '/res/103/jerf/navmap.course.sequence'.
   
 =item * B<navmap>: A reference to a navmap, used only if an iterator is not passed in. If this is necessary to make an iterator but it is not passed in, a new one will be constructed based on ENV info. This is useful to do basic error checking before passing it off to render.  =item * B<navmap>: A reference to a navmap, used only if an iterator is not passed in. If this is necessary to make an iterator but it is not passed in, a new one will be constructed based on ENV info. This is useful to do basic error checking before passing it off to render.
   
Line 521  Most of these parameters are only useful Line 564  Most of these parameters are only useful
   
 =item * B<printCloseAll>: If true, print the "Close all folders" or "open all folders" links. Default is true.  =item * B<printCloseAll>: If true, print the "Close all folders" or "open all folders" links. Default is true.
   
   =item * B<filterFunc>: A function that takes the resource object as its only parameter and returns a true or false value. If true, the resource is displayed. If false, it is simply skipped in the display. By default, all resources are shown.
   
   =item * B<suppressNavmaps>: If true, will not display Navigate Content resources. Default to false.
   
 =back  =back
   
 =head2 Additional Info  =head2 Additional Info
   
 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:  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  =over 4
   
 =back  =back
Line 585  sub render_resource { Line 634  sub render_resource {
         if ($it->{CONDITION}) {          if ($it->{CONDITION}) {
             $nowOpen = !$nowOpen;              $nowOpen = !$nowOpen;
         }          }
         $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') . '.gif';  
         $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";          if (!$params->{'resource_no_folder_link'}) {
                       $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') . '.gif';
         $linkopen = "<a href='" . $params->{'url'} . '?' .               $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
             $params->{'queryString'} . '&filter=';  
         $linkopen .= ($nowOpen xor $it->{CONDITION}) ?              $linkopen = "<a href='" . $params->{'url'} . '?' . 
             addToFilter($filter, $mapId) :                  $params->{'queryString'} . '&filter=';
             removeFromFilter($filter, $mapId);              $linkopen .= ($nowOpen xor $it->{CONDITION}) ?
         $linkopen .= "&condition=" . $it->{CONDITION} . '&hereType='                  addToFilter($filter, $mapId) :
             . $params->{'hereType'} . '&here=' .                  removeFromFilter($filter, $mapId);
             &Apache::lonnet::escape($params->{'here'}) .               $linkopen .= "&condition=" . $it->{CONDITION} . '&hereType='
             '&jumpType=' . SYMB() . '&jump=' .                  . $params->{'hereType'} . '&here=' .
             &Apache::lonnet::escape($resource->symb()) .                   &Apache::lonnet::escape($params->{'here'}) . 
             "&folderManip=1'>";                  '&jumpType=' . SYMB() . '&jump=' .
                   &Apache::lonnet::escape($resource->symb()) . 
                   "&folderManip=1'>";
           } else {
               # Don't allow users to manipulate folder
               $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') .
                   '.nomanip.gif';
               $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
   
               $linkopen = "";
               $linkclose = "";
           }
     }      }
   
     if ($resource->randomout()) {      if ($resource->randomout()) {
Line 642  sub render_resource { Line 702  sub render_resource {
         $nonLinkedText .= ' (' . $resource->countParts() . ' parts)';          $nonLinkedText .= ' (' . $resource->countParts() . ' parts)';
     }      }
   
     $result .= "  $curMarkerBegin<a href='$link'>$title$partLabel</a>$curMarkerEnd $nonLinkedText</td>";      if (!$params->{'resource_nolink'}) {
           $result .= "  $curMarkerBegin<a href='$link'>$title$partLabel</a>$curMarkerEnd $nonLinkedText</td>";
       } else {
           $result .= "  $curMarkerBegin$title$partLabel$curMarkerEnd $nonLinkedText</td>";
       }
   
     return $result;      return $result;
 }  }
Line 781  sub render { Line 845  sub render {
     my $here;      my $here;
     my $jump;      my $jump;
     my $currentJumpIndex = setDefault($args->{'currentJumpIndex'}, 0);          my $currentJumpIndex = setDefault($args->{'currentJumpIndex'}, 0);    
       my $suppressNavmap = setDefault($args->{'suppressNavmap'}, 0);
     my $currentJumpDelta = 2; # change this to change how many resources are displayed      my $currentJumpDelta = 2; # change this to change how many resources are displayed
                              # before the current resource when using #current                               # before the current resource when using #current
   
Line 917  sub render { Line 982  sub render {
         # Paranoia: Make sure it's ready          # Paranoia: Make sure it's ready
         $navmap->init();          $navmap->init();
   
         $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition);          # See if we're being passed a specific map
           if ($args->{'iterator_map'}) {
               my $map = $args->{'iterator_map'};
               $map = $navmap->getResourceByUrl($map);
               my $firstResource = $map->map_start();
               my $finishResource = $map->map_finish();
   
               $args->{'iterator'} = $it = $navmap->getIterator($firstResource, $finishResource, $filterHash, $condition);
           } else {
               $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition);
           }
     }      }
           
     my $showParts = setDefault($args->{'showParts'}, 1);      my $showParts = setDefault($args->{'showParts'}, 1);
Line 929  sub render { Line 1004  sub render {
     my $printKey = $args->{'printKey'};      my $printKey = $args->{'printKey'};
     my $printCloseAll = $args->{'printCloseAll'};      my $printCloseAll = $args->{'printCloseAll'};
     if (!defined($printCloseAll)) { $printCloseAll = 1; }      if (!defined($printCloseAll)) { $printCloseAll = 1; }
       my $filterFunc = setDefault($args->{'filterFunc'},
                                   sub {return 1;});
       
     # Print key?      # Print key?
     if ($printKey) {      if ($printKey) {
         $result .= '<table border="0" cellpadding="2" cellspacing="0">';          $result .= '<table border="0" cellpadding="2" cellspacing="0">';
Line 962  sub render { Line 1039  sub render {
                 "&hereType=$hereType&here=" . Apache::lonnet::escape($here) .                   "&hereType=$hereType&here=" . Apache::lonnet::escape($here) . 
                 "\">Open All Folders</a>";                  "\">Open All Folders</a>";
         }          }
           $result .= "<br /><br />\n";
     }          }    
   
     if ($r) {      if ($r) {
Line 1027  sub render { Line 1105  sub render {
   
         $args->{'counter'}++;          $args->{'counter'}++;
   
           # If this has been filtered out, continue on
           if (!(&$filterFunc($curRes))) {
               $curRes = $it->next();
               $args->{'isNewBranch'} = 0; # Don't falsely remember this
               next;
           } 
   
           # If we're suppressing navmaps and this is a navmap, continue on
           if ($suppressNavmap && $curRes->src() =~ /^\/adm\/navmaps/) {
               $curRes = $it->next();
               next;
           }
   
         # Does it have multiple parts?          # Does it have multiple parts?
         $args->{'multipart'} = 0;          $args->{'multipart'} = 0;
         $args->{'condensed'} = 0;          $args->{'condensed'} = 0;
Line 1138  sub render { Line 1229  sub render {
             $result .= "    </tr>\n";              $result .= "    </tr>\n";
             $args->{'isNewBranch'} = 0;              $args->{'isNewBranch'} = 0;
         }          }
           
         if ($r && $rownum % 20 == 0) {          if ($r && $rownum % 20 == 0) {
             $r->print($result);              $r->print($result);
             $result = "";              $result = "";
Line 1570  sub parmval_real { Line 1661  sub parmval_real {
     return '';      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.
   
   =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 {
       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 ();
       }
   
       # 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;  1;
   
 package Apache::lonnavmaps::iterator;  package Apache::lonnavmaps::iterator;
Line 2312  These methods are useful for getting inf Line 2503  These methods are useful for getting inf
 sub map_finish {  sub map_finish {
     my $self = shift;      my $self = shift;
     my $src = $self->src();      my $src = $self->src();
       $src = Apache::lonnet::clutter($src);
     my $res = $self->navHash("map_finish_$src", 0);      my $res = $self->navHash("map_finish_$src", 0);
     $res = $self->{NAV_MAP}->getById($res);      $res = $self->{NAV_MAP}->getById($res);
     return $res;      return $res;
Line 2324  sub map_pc { Line 2516  sub map_pc {
 sub map_start {  sub map_start {
     my $self = shift;      my $self = shift;
     my $src = $self->src();      my $src = $self->src();
       $src = Apache::lonnet::clutter($src);
     my $res = $self->navHash("map_start_$src", 0);      my $res = $self->navHash("map_start_$src", 0);
     $res = $self->{NAV_MAP}->getById($res);      $res = $self->{NAV_MAP}->getById($res);
     return $res;      return $res;
Line 2535  sub countParts { Line 2728  sub countParts {
 sub extractParts {   sub extractParts { 
     my $self = shift;      my $self = shift;
           
     return if ($self->{PARTS});      return if (defined($self->{PARTS}));
     return if ($self->ext);      return if ($self->ext);
   
     $self->{PARTS} = [];      $self->{PARTS} = [];
   
     # Retrieve part count, if this is a problem      # Retrieve part count, if this is a problem
     if ($self->is_problem()) {      if ($self->is_problem()) {
         my $metadata = &Apache::lonnet::metadata($self->src(), 'allpossiblekeys');          my $metadata = &Apache::lonnet::metadata($self->src(), 'packages');
         if (!$metadata) {          if (!$metadata) {
             $self->{RESOURCE_ERROR} = 1;              $self->{RESOURCE_ERROR} = 1;
             $self->{PARTS} = [];              $self->{PARTS} = [];
             return;              return;
         }          }
           
         foreach (split(/\,/,$metadata)) {          foreach (split(/\,/,$metadata)) {
             if ($_ =~ /^parameter\_(.*)\_opendate$/) {              if ($_ =~ /^part_(.*)$/) {
                 push @{$self->{PARTS}}, $1;                  my $part = $1;
                   # check to see if part is turned off.
                   if (! Apache::loncommon::check_if_partid_hidden($part, $self->symb())) {
                       push @{$self->{PARTS}}, $1;
                   }
             }              }
         }          }
                   
                   
         # Is this possible to do in one line? - Jeremy  
         my @sortedParts = sort @{$self->{PARTS}};          my @sortedParts = sort @{$self->{PARTS}};
         $self->{PARTS} = \@sortedParts;          $self->{PARTS} = \@sortedParts;
     }      }
   
       # Ensure part 0 is included at the beginning.
       if ($self->{PARTS}->[0] ne '0') {
           unshift @{$self->{PARTS}}, '0';
       }
   
     return;      return;
 }  }
   

Removed from v.1.142  
changed lines
  Added in v.1.155


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>