--- loncom/interface/lonnavmaps.pm 2003/02/06 22:58:12 1.139 +++ loncom/interface/lonnavmaps.pm 2003/02/07 21:55:44 1.140 @@ -2,7 +2,7 @@ # The LearningOnline Network with CAPA # Navigate Maps Handler # -# $Id: lonnavmaps.pm,v 1.139 2003/02/06 22:58:12 bowersj2 Exp $ +# $Id: lonnavmaps.pm,v 1.140 2003/02/07 21:55:44 bowersj2 Exp $ # # Copyright Michigan State University Board of Trustees # @@ -56,21 +56,23 @@ sub NOTHING { return 3; } # Some data +my $resObj = "Apache::lonnavmaps::resource"; + # Keep these mappings in sync with lonquickgrades, which uses the colors # instead of the icons. my %statusIconMap = - ( Apache::lonnavmaps::resource->NETWORK_FAILURE => '', - Apache::lonnavmaps::resource->NOTHING_SET => '', - Apache::lonnavmaps::resource->CORRECT => 'navmap.correct.gif', - Apache::lonnavmaps::resource->EXCUSED => 'navmap.correct.gif', - Apache::lonnavmaps::resource->PAST_DUE_NO_ANSWER => 'navmap.wrong.gif', - Apache::lonnavmaps::resource->PAST_DUE_ANSWER_LATER => 'navmap.wrong.gif', - Apache::lonnavmaps::resource->ANSWER_OPEN => 'navmap.wrong.gif', - Apache::lonnavmaps::resource->OPEN_LATER => '', - Apache::lonnavmaps::resource->TRIES_LEFT => 'navmap.open.gif', - Apache::lonnavmaps::resource->INCORRECT => 'navmap.wrong.gif', - Apache::lonnavmaps::resource->OPEN => 'navmap.open.gif', - Apache::lonnavmaps::resource->ATTEMPTED => 'navmap.open.gif' ); + ( $resObj->NETWORK_FAILURE => '', + $resObj->NOTHING_SET => '', + $resObj->CORRECT => 'navmap.correct.gif', + $resObj->EXCUSED => 'navmap.correct.gif', + $resObj->PAST_DUE_NO_ANSWER => 'navmap.wrong.gif', + $resObj->PAST_DUE_ANSWER_LATER => 'navmap.wrong.gif', + $resObj->ANSWER_OPEN => 'navmap.wrong.gif', + $resObj->OPEN_LATER => '', + $resObj->TRIES_LEFT => 'navmap.open.gif', + $resObj->INCORRECT => 'navmap.wrong.gif', + $resObj->OPEN => 'navmap.open.gif', + $resObj->ATTEMPTED => 'navmap.open.gif' ); my %iconAltTags = ( 'navmap.correct.gif' => 'Correct', @@ -79,17 +81,17 @@ my %iconAltTags = # Defines a status->color mapping, null string means don't color my %colormap = - ( Apache::lonnavmaps::resource->NETWORK_FAILURE => '', - Apache::lonnavmaps::resource->CORRECT => '', - Apache::lonnavmaps::resource->EXCUSED => '#3333FF', - Apache::lonnavmaps::resource->PAST_DUE_ANSWER_LATER => '', - Apache::lonnavmaps::resource->PAST_DUE_NO_ANSWER => '', - Apache::lonnavmaps::resource->ANSWER_OPEN => '#006600', - Apache::lonnavmaps::resource->OPEN_LATER => '', - Apache::lonnavmaps::resource->TRIES_LEFT => '', - Apache::lonnavmaps::resource->INCORRECT => '', - Apache::lonnavmaps::resource->OPEN => '', - Apache::lonnavmaps::resource->NOTHING_SET => '' ); + ( $resObj->NETWORK_FAILURE => '', + $resObj->CORRECT => '', + $resObj->EXCUSED => '#3333FF', + $resObj->PAST_DUE_ANSWER_LATER => '', + $resObj->PAST_DUE_NO_ANSWER => '', + $resObj->ANSWER_OPEN => '#006600', + $resObj->OPEN_LATER => '', + $resObj->TRIES_LEFT => '', + $resObj->INCORRECT => '', + $resObj->OPEN => '', + $resObj->NOTHING_SET => '' ); # And a special case in the nav map; what to do when the assignment # is not yet done and due in less then 24 hours my $hurryUpColor = "#FF0000"; @@ -117,8 +119,6 @@ sub handler { sub real_handler { my $r = shift; - &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING}); - # Handle header-only request if ($r->header_only) { if ($ENV{'browser.mathml'}) { @@ -164,83 +164,6 @@ sub real_handler { # Now that we've displayed some stuff to the user, init the navmap $navmap->init(); - $r->print(''); - my $date=localtime; - $r->print(''); - - # Print discussions and feedback header - if ($navmap->{LAST_CHECK}) { - $r->print(''); - } else { - $r->print(''); - } - $r->print('
Key:    '. - ' New discussion since '. - strftime("%A, %b %e at %I:%M %P", localtime($navmap->{LAST_CHECK})). - '  '. - ' New message (click to open)

'. - '

  '. - ' Discussions'. - '   New message (click to open)'. - '
'); - - my $condition = 0; - if ($ENV{'form.condition'}) { - $condition = 1; - } - - # Determine where the "here" marker is and where the screen jumps to. - my $hereType; # the type of marker, SYMB, URL, or NOTHING - my $here; # the actual URL or SYMB for the here marker - my $jumpType; # The type of the thing we have a jump for, SYMB or URL - my $jump; # the SYMB/URL of the resource we need to jump to - - if ( $ENV{'form.alreadyHere'} ) { # we came from a user's manipulation of the nav page - # If this is a click on a folder or something, we want to preserve the "here" - # from the querystring, and get the new "jump" marker - $hereType = $ENV{'form.hereType'}; - $here = $ENV{'form.here'}; - $jumpType = $ENV{'form.jumpType'} || NOTHING(); - $jump = $ENV{'form.jump'}; - } else { # the user is visiting the nav map from the remote - # We're coming from the remote. We have either a url, a symb, or nothing, - # and we need to figure out what. - # Preference: Symb - - if ($ENV{'form.symb'}) { - $hereType = $jumpType = SYMB(); - $here = $jump = $ENV{'form.symb'}; - } elsif ($ENV{'form.postdata'}) { - # couldn't find a symb, is there a URL? - my $currenturl = $ENV{'form.postdata'}; - $currenturl=~s/^http\:\/\///; - $currenturl=~s/^[^\/]+//; - - $hereType = $jumpType = URL; - $here = $jump = $currenturl; - } else { - # Nothing - $hereType = $jumpType = NOTHING(); - } - } - - - # alreadyHere allows us to only open the maps necessary to view - # the current location once, while at the same time remembering - # the current location. Without that check, the user would never - # be able to close those maps; the user would close it, and the - # currenturl scan would re-open it. - my $queryAdd = "&alreadyHere=1"; - - if ($condition) { - $r->print("Close All Folders"); - } else { - $r->print("Open All Folders"); - } $r->print('
 '); $r->rflush(); @@ -252,101 +175,11 @@ sub real_handler { return OK; } - my $res = "Apache::lonnavmaps::resource"; - - my %filterHash; - # Figure out what we're not displaying - foreach (split(/\,/, $ENV{"form.filter"})) { - if ($_) { - $filterHash{$_} = "1"; - } - } - - # Begin the HTML table - # four cols: resource + indent, chat+feedback, icon, text string - $r->print('' ."\n"); - - # This needs to be updated to use symbs from the remote, - # instead of uris. The changes to this and the main rendering - # loop should be obvious. - # Here's a simple example of the iterator. - # Preprocess the map: Look for current URL, open necessary maps - - my $mapIterator = $navmap->getIterator(undef, undef, undef, 1); - my $found = 0; - my $depth = 1; - my $currentJumpIndex = 0; # keeps track of when the current resource is found, - # so we can back up a few and put the anchor above the - # current resource - my $currentJumpDelta = 2; # change this to change how many resources are displayed - # before the current resource when using #current - $mapIterator->next(); # discard the first BEGIN_MAP - my $curRes = $mapIterator->next(); - my $counter = 0; - my $foundJump = ($jumpType == NOTHING()); # look for jump point if we have one - my $looped = 0; - - # We only need to do this if we need to open the maps to show the - # current position. This will change the counter so we can't count - # for the jump marker with this loop. - while ($depth > 0 && !$ENV{'form.alreadyHere'}) { - if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; } - if ($curRes == $mapIterator->END_MAP()) { $depth--; } - - if (ref($curRes) && !$ENV{'form.alreadyHere'} && - ($hereType == SYMB() && $curRes->symb() eq $here) || - (ref($curRes) && $hereType == URL() && $curRes->src() eq $here)) { - my $mapStack = $mapIterator->getStack(); - - # Ensure the parent maps are open - for my $map (@{$mapStack}) { - if ($condition) { - undef $filterHash{$map->map_pc()}; - } else { - $filterHash{$map->map_pc()} = 1; - } - } - $ENV{'form.alreadyHere'} = 1; - } - $looped = 1; - - $curRes = $mapIterator->next(); - } - - $mapIterator = $navmap->getIterator(undef, undef, \%filterHash, 0); - $depth = 1; - $mapIterator->next(); - $curRes = $mapIterator->next(); - - while ($depth > 0 && !$foundJump) { - if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; } - if ($curRes == $mapIterator->END_MAP()) { $depth--; } - if (ref($curRes)) { $counter++; } - - if (ref($curRes) && - (($jumpType == SYMB() && $curRes->symb() eq $jump) || - ($jumpType == URL() && $curRes->src() eq $jump))) { - # If this is the correct resource, be sure to - # show it by making sure the containing maps - # are open. - - # This is why we have to use the main iterator instead of the - # potentially faster DFS: The count has to be the same, so - # the order has to be the same, which DFS won't give us. - $currentJumpIndex = $counter; - $foundJump = 1; - } - - $curRes = $mapIterator->next(); - } - # renderer call - $mapIterator = $navmap->getIterator(undef, undef, \%filterHash, 0); - my $render = render({ 'cols' => [0,1,2,3], 'iterator' => $mapIterator, - 'url' => '/adm/navmaps', - 'queryString' => 'alreadyHere=1', - 'currentJumpIndex' => $currentJumpIndex}); - $r->print($render); + my $render = render({ 'cols' => [0,1,2,3], + 'url' => '/adm/navmaps', + 'printKey' => 1, + 'r' => $r}); $navmap->untieHashes(); @@ -654,9 +487,13 @@ Note these functions are responsible for =head2 Parameters +Most of these parameters are only useful if you are *not* using the folder interface (i.e., the default first column), which is probably the common case. If you are using this interface, then you should be able to get away with just using 'cols' (to specify the columns shown), 'url' (necessary for the folders to link to the current screen correctly), and possibly 'queryString' if your app calls for it. In that case, maintaining the state of the folders will be done automatically. + =over 4 -=item * B: 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. +=item * B: 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: 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: An array reference @@ -664,11 +501,11 @@ Note these functions are responsible for =item * B: A flag. If yes (default), if all parts of the problem have the same status and that status is Nothing Set, Correct, or Network Failure, then only one line will be displayed for that resource anyhow. If no, all parts will always be displayed. If showParts is 0, this is ignored. -=item * B: A string identifying the URL to place the anchor 'curloc' at. Default to no anchor at all. It is the responsibility of the renderer user to ensure that the #curloc is in the URL. +=item * B: A string identifying the URL to place the anchor 'curloc' at. Default to no anchor at all. It is the responsibility of the renderer user to ensure that the #curloc is in the URL. By default, determined through the use of the ENV{} 'jump' and 'jumpType' information. -=item * B: A URL identifying where to place the 'here' marker. +=item * B: A URL identifying where to place the 'here' marker. By default, will pull this from the ENV{'form.here*'} info. -=item * B: A Symb identifying where to place the 'here' marker. +=item * B: A Symb identifying where to place the 'here' marker. Default same as hereURL. =item * B: A string identifying the indentation string to use. By default, this is a 25 pixel whitespace image with no alt text. @@ -676,10 +513,14 @@ Note these functions are responsible for =item * B: The url the folders will link to, which should be the current page. Required if the resource info column is shown. -=item * B: Describes the currently-open row number to cause the browser to jump to, because the user just opened that folder. +=item * B: Describes the currently-open row number to cause the browser to jump to, because the user just opened that folder. By default, pulled from the Jump information in the ENV{'form.*'}. =item * B: The standard Apache response object. If you pass this to the render, it will use it to flush the table every 20 rows and handle the rendering itself. +=item * B: If true, print the key that appears on the top of the standard navmaps. Default is false. + +=item * B: If true, print the "Close all folders" or "open all folders" links. Default is true. + =back =head2 Additional Info @@ -720,7 +561,6 @@ sub render_resource { # If this is a new branch, label it so if ($params->{'isNewBranch'}) { $newBranchText = ""; - $params->{'isNewBranch'} = 0; } # links to open and close the folder @@ -757,7 +597,8 @@ sub render_resource { . $params->{'hereType'} . '&here=' . &Apache::lonnet::escape($params->{'here'}) . '&jumpType=' . SYMB() . '&jump=' . - &Apache::lonnet::escape($resource->symb()) . "'>"; + &Apache::lonnet::escape($resource->symb()) . + "&folderManip=1'>"; } if ($resource->randomout()) { @@ -766,10 +607,12 @@ sub render_resource { # We're done preparing and finally ready to start the rendering my $result = "
"; - + + my $indentLevel = $params->{'indentLevel'}; + if ($newBranchText) { $indentLevel--; } + # print indentation - for (my $i = 0; $i < $params->{'indentLevel'} - - $params->{'deltaLevel'}; $i++) { + for (my $i = 0; $i < $indentLevel; $i++) { $result .= $params->{'indentString'}; } @@ -913,39 +756,223 @@ sub setDefault { sub render { my $args = shift; - + &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING}); + my $result = ''; + # Configure the renderer. my $cols = $args->{'cols'}; if (!defined($cols)) { # no columns, no nav maps. return ''; } - my $it = $args->{'iterator'}; - if (!defined($it)) { - # no iterator, no nav map. - return ''; + my $mustCloseNavMap = 0; + my $navmap; + if (defined($args->{'navmap'})) { + $navmap = $args->{'navmap'}; } - - my $showParts = setDefault($args->{'showParts'}, 1); - my $condenseParts = setDefault($args->{'condenseParts'}, 1); + + my $queryString = $args->{'queryString'}; my $jumpToURL = $args->{'jumpToURL'}; my $jumpToSymb = $args->{'jumpToSymb'}; + my $jumpType; my $hereURL = $args->{'hereURL'}; my $hereSymb = $args->{'hereSymb'}; - # keeps track of when the current resource is found, - # so we can back up a few and put the anchor above the - # current resource + my $hereType; + my $here; + my $jump; my $currentJumpIndex = setDefault($args->{'currentJumpIndex'}, 0); my $currentJumpDelta = 2; # change this to change how many resources are displayed # before the current resource when using #current - my $r = $args->{'r'}; + + # If we were passed 'here' information, we are not rendering + # after a folder manipulation, and we were not passed an + # iterator, make sure we open the folders to show the "here" + # marker + my $filterHash = {}; + # Figure out what we're not displaying + foreach (split(/\,/, $ENV{"form.filter"})) { + if ($_) { + $filterHash->{$_} = "1"; + } + } + + my $condition = 0; + if ($ENV{'form.condition'}) { + $condition = 1; + } + + if (!$ENV{'form.folderManip'} && !defined($args->{'iterator'})) { + # Step 1: Check to see if we have a navmap + if (!defined($navmap)) { + $navmap = Apache::lonnavmaps::navmap->new( + $ENV{"request.course.fn"}.".db", + $ENV{"request.course.fn"}."_parms.db", 1, 1); + $mustCloseNavMap = 1; + } + $navmap->init(); + + # Step two: Locate what kind of here marker is necessary + # Determine where the "here" marker is and where the screen jumps to. + + # We're coming from the remote. We have either a url, a symb, or nothing, + # and we need to figure out what. + # Preference: Symb + + if ($ENV{'form.symb'}) { + $hereType = $jumpType = SYMB(); + $here = $jump = $ENV{'form.symb'}; + } elsif ($ENV{'form.postdata'}) { + # couldn't find a symb, is there a URL? + my $currenturl = $ENV{'form.postdata'}; + $currenturl=~s/^http\:\/\///; + $currenturl=~s/^[^\/]+//; + + $hereType = $jumpType = URL; + $here = $jump = $currenturl; + } else { + # Nothing + $hereType = $jumpType = NOTHING(); + } + # Step three: Ensure the folders are open + my $mapIterator = $navmap->getIterator(undef, undef, undef, 1); + my $depth = 1; + $mapIterator->next(); # discard the first BEGIN_MAP + my $curRes = $mapIterator->next(); + my $counter = 0; + my $found = 0; + + # We only need to do this if we need to open the maps to show the + # current position. This will change the counter so we can't count + # for the jump marker with this loop. + while ($depth > 0 && !$found) { + if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; } + if ($curRes == $mapIterator->END_MAP()) { $depth--; } + + if (ref($curRes) && + ($hereType == SYMB() && $curRes->symb() eq $here) || + (ref($curRes) && $hereType == URL() && $curRes->src() eq $here)) { + my $mapStack = $mapIterator->getStack(); + + # Ensure the parent maps are open + for my $map (@{$mapStack}) { + if ($condition) { + undef $filterHash->{$map->map_pc()}; + } else { + $filterHash->{$map->map_pc()} = 1; + } + } + $found = 1; + } + + $curRes = $mapIterator->next(); + } + + # Since we changed the folders, (re-)locate the jump point, if any + $mapIterator = $navmap->getIterator(undef, undef, $filterHash, 0); + $depth = 1; + $mapIterator->next(); + $curRes = $mapIterator->next(); + my $foundJump = 0; + + while ($depth > 0 && !$foundJump) { + if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; } + if ($curRes == $mapIterator->END_MAP()) { $depth--; } + if (ref($curRes)) { $counter++; } + + if (ref($curRes) && + (($jumpType == SYMB() && $curRes->symb() eq $jump) || + ($jumpType == URL() && $curRes->src() eq $jump))) { + + # This is why we have to use the main iterator instead of the + # potentially faster DFS: The count has to be the same, so + # the order has to be the same, which DFS won't give us. + $currentJumpIndex = $counter; + $foundJump = 1; + } + + $curRes = $mapIterator->next(); + } + } + if ( !defined($args->{'iterator'}) && $ENV{'form.folderManip'} ) { # we came from a user's manipulation of the nav page + # If this is a click on a folder or something, we want to preserve the "here" + # from the querystring, and get the new "jump" marker + $hereType = $ENV{'form.hereType'}; + $here = $ENV{'form.here'}; + $jumpType = $ENV{'form.jumpType'} || NOTHING(); + $jump = $ENV{'form.jump'}; + } - + my $it = $args->{'iterator'}; + if (!defined($it)) { + # Construct a default iterator based on $ENV{'form.'} information + + # Step 1: Check to see if we have a navmap + if (!defined($navmap)) { + $navmap = Apache::lonnavmaps::navmap->new( + $ENV{"request.course.fn"}.".db", + $ENV{"request.course.fn"}."_parms.db", 1, 1); + $mustCloseNavMap = 1; + } + # Paranoia: Make sure it's ready + $navmap->init(); + + $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition); + } + + my $showParts = setDefault($args->{'showParts'}, 1); + my $condenseParts = setDefault($args->{'condenseParts'}, 1); + # keeps track of when the current resource is found, + # so we can back up a few and put the anchor above the + # current resource + my $r = $args->{'r'}; + my $printKey = $args->{'printKey'}; + my $printCloseAll = $args->{'printCloseAll'}; + if (!defined($printCloseAll)) { $printCloseAll = 1; } + + # Print key? + if ($printKey) { + $result .= ''; + my $date=localtime; + $result.=''; + if ($navmap->{LAST_CHECK}) { + $result .= + ' New discussion since '. + strftime("%A, %b %e at %I:%M %P", localtime($navmap->{LAST_CHECK})). + ''; + } else { + $result .= ''; + } + + $result .= '
Key:    '. + ' New message (click to open)

'. + '

  '. + ' Discussions'. + '   New message (click to open)'. + '
'; + } + + if ($printCloseAll) { + if ($condition) { + $result.="Close All Folders"; + } else { + $result.="Open All Folders"; + } + } + + if ($r) { + $r->print($result); + $r->rflush(); + $result = ""; + } # End parameter setting # Data - my $result .= '' ."\n"; + $result .= '
' ."\n"; my $res = "Apache::lonnavmaps::resource"; my %condenseStatuses = ( $res->NETWORK_FAILURE => 1, @@ -995,15 +1022,6 @@ sub render { } $args->{'counter'}++; - # reserve space for branch symbol - $args->{'deltalevel'} = $args->{'isNewBranch'}? 1 : 0; - if ($args->{'indentLevel'} - $args->{'deltalevel'} < 0) { - # If this would be at a negative depth (top-level maps in - # new-style courses, we want to suppress their title display) - # then ignore it. - $curRes = $it->next(); - next; - } # Does it have multiple parts? $args->{'multipart'} = 0; @@ -1114,6 +1132,7 @@ sub render { $result .= $colHTML . "\n"; } $result .= " \n"; + $args->{'isNewBranch'} = 0; } if ($r && $rownum % 20 == 0) { @@ -1131,6 +1150,14 @@ sub render { } $result .= "
"; + + if ($r) { + $r->print($result); + $result = ""; + $r->rflush(); + } + + if ($mustCloseNavMap) { $navmap->untieHashes(); } return $result; } @@ -1200,6 +1227,7 @@ sub new { $self->{HASH_TIED} = 1; $self->{NAV_HASH} = \%navmaphash; $self->{PARM_HASH} = \%parmhash; + $self->{INITED} = 0; bless($self); @@ -1208,6 +1236,7 @@ sub new { sub init { my $self = shift; + if ($self->{INITED}) { return; } # If the course opt hash and the user opt hash should be generated, # generate them @@ -1313,6 +1342,7 @@ sub init { } $self->{PARM_CACHE} = {}; + $self->{INITED} = 1; } # Internal function: Takes a key to look up in the nav hash and implements internal @@ -1964,7 +1994,7 @@ sub new { # A hash, used as a set, of resource already seen $self->{ALREADY_SEEN} = shift; - if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} }; + if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} }; $self->{CONDITION} = shift; $self->{DIRECTION} = shift || FORWARD(); @@ -2087,7 +2117,7 @@ A resource object encapsulates a resourc A resource only makes sense in the context of a navmap, as some of the data is stored in the navmap object. -You will probably never need to instantiate this object directly. Use Apache::lonnavmap::navmap, and use the "start" method to obtain the starting resource. +You will probably never need to instantiate this object directly. Use Apache::lonnavmaps::navmap, and use the "start" method to obtain the starting resource. =head2 Public Members @@ -2218,8 +2248,6 @@ These methods are shortcuts to deciding =item * B: Returns true if the resource is a page. -=item * B: Returns true if the resource is a problem. - =item * B: Returns true if the resource is a sequence. =back