--- loncom/interface/lonnavmaps.pm 2003/05/14 18:33:28 1.188 +++ loncom/interface/lonnavmaps.pm 2003/06/18 15:49:24 1.208 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Navigate Maps Handler # -# $Id: lonnavmaps.pm,v 1.188 2003/05/14 18:33:28 bowersj2 Exp $ +# $Id: lonnavmaps.pm,v 1.208 2003/06/18 15:49:24 bowersj2 Exp $ # # Copyright Michigan State University Board of Trustees # @@ -47,6 +47,7 @@ use Apache::Constants qw(:common :http); use Apache::loncommon(); use Apache::lonmenu(); use POSIX qw (floor strftime); +use Data::Dumper; # for debugging, not always used # symbolic constants sub SYMB { return 1; } @@ -71,7 +72,7 @@ my %statusIconMap = $resObj->TRIES_LEFT => 'navmap.open.gif', $resObj->INCORRECT => 'navmap.wrong.gif', $resObj->OPEN => 'navmap.open.gif', - $resObj->ATTEMPTED => 'navmap.open.gif', + $resObj->ATTEMPTED => 'navmap.ellipsis.gif', $resObj->ANSWER_SUBMITTED => '' ); my %iconAltTags = @@ -91,7 +92,10 @@ my %colormap = $resObj->TRIES_LEFT => '', $resObj->INCORRECT => '', $resObj->OPEN => '', - $resObj->NOTHING_SET => '' ); + $resObj->NOTHING_SET => '', + $resObj->ATTEMPTED => '', + $resObj->ANSWER_SUBMITTED => '' + ); # 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"; @@ -160,7 +164,6 @@ sub real_handler { # Now that we've displayed some stuff to the user, init the navmap $navmap->init(); - $r->print('
 '); $r->rflush(); # Check that it's defined @@ -200,8 +203,10 @@ sub real_handler { } } + my $jumpToFirstHomework = 0; # Check to see if the student is jumping to next open, do-able problem if ($ENV{QUERY_STRING} eq 'jumpToFirstHomework') { + $jumpToFirstHomework = 1; # Find the next homework problem that they can do. my $iterator = $navmap->getIterator(undef, undef, undef, 1); my $depth = 1; @@ -216,9 +221,7 @@ sub real_handler { if (ref($curRes) && $curRes->is_problem()) { my $status = $curRes->status(); - if (($status == $curRes->OPEN || - $status == $curRes->TRIES_LEFT()) && - $curRes->getCompletionStatus() != $curRes->ATTEMPTED()) { + if ($curRes->completable()) { $problemRes = $curRes; $foundDoableProblem = 1; @@ -243,18 +246,52 @@ sub real_handler { } } else { $r->print("" . - "Go To My First Homework Problem
"); + "Go To My First Homework Problem    "); } - # renderer call - my $render = render({ 'cols' => [0,1,2,3], - 'url' => '/adm/navmaps', - 'navmap' => $navmap, - 'suppressNavmap' => 1, - 'r' => $r}); + my $suppressEmptySequences = 0; + my $filterFunc = undef; + my $resource_no_folder_link = 0; + + # Display only due homework. + my $showOnlyHomework = 0; + if ($ENV{QUERY_STRING} eq 'showOnlyHomework') { + $showOnlyHomework = 1; + $suppressEmptySequences = 1; + $filterFunc = sub { my $res = shift; + return $res->completable() || $res->is_map(); + }; + $r->print("

Uncompleted Homework

"); + $ENV{'form.filter'} = ''; + $ENV{'form.condition'} = 1; + $resource_no_folder_link = 1; + } else { + $r->print("" . + "Show Only Uncompleted Homework    "); + } + # renderer call + my $renderArgs = { 'cols' => [0,1,2,3], + 'url' => '/adm/navmaps', + 'navmap' => $navmap, + 'suppressNavmap' => 1, + 'suppressEmptySequences' => $suppressEmptySequences, + 'filterFunc' => $filterFunc, + 'resource_no_folder_link' => $resource_no_folder_link, + 'r' => $r}; + my $render = render($renderArgs); $navmap->untieHashes(); + # If no resources were printed, print a reassuring message so the + # user knows there was no error. + if ($renderArgs->{'counter'} == 0) { + if ($showOnlyHomework) { + $r->print("

All homework is currently completed.

"); + } else { # both jumpToFirstHomework and normal use the same: course must be empty + $r->print("

This course is empty.

"); + } + } + $r->print(""); $r->rflush(); @@ -347,7 +384,7 @@ sub getDescription { return "Excused by instructor"; } if ($status == $res->ATTEMPTED) { - return "Not yet graded."; + return "Answer submitted, not yet graded."; } if ($status == $res->TRIES_LEFT) { my $tries = $res->tries($part); @@ -379,7 +416,7 @@ sub dueInLessThen24Hours { my $part = shift; my $status = $res->status($part); - return ($status == $res->OPEN() || $status == $res->ATTEMPTED() || + return ($status == $res->OPEN() || $status == $res->TRIES_LEFT()) && $res->duedate() && $res->duedate() < time()+(24*60*60) && $res->duedate() > time(); @@ -521,9 +558,9 @@ Apache::lonnavmap - Subroutines to handl The main handler generates the navigational listing for the course, the other objects export this information in a usable fashion for -other modules +other modules. -=head1 Object: render +=head1 Subroutine: render The navmap renderer package provides a sophisticated rendering of the standard navigation maps interface into HTML. The provided nav map @@ -537,7 +574,7 @@ understand then "undef, undef, undef, 1, undef, 0" when you mostly want default behaviors. The package provides a function called 'render', called as -Apache::lonnavmaps::renderer->render({}). +Apache::lonnavmaps::render({}). =head2 Overview of Columns @@ -545,7 +582,7 @@ The renderer will build an HTML table fo it. The table is consists of several columns, and a row for each resource (or possibly each part). You tell the renderer how many columns to create and what to place in each column, optionally using -one or more of the preparent columns, and the renderer will assemble +one or more of the prepared columns, and the renderer will assemble the table. Any additional generally useful column types should be placed in the @@ -563,7 +600,7 @@ argument hash passed to the renderer, an be inserted into the HTML representation as it. The pre-packaged column names are refered to by constants in the -Apache::lonnavmaps::renderer namespace. The following currently exist: +Apache::lonnavmaps namespace. The following currently exist: =over 4 @@ -571,7 +608,7 @@ Apache::lonnavmaps::renderer namespace. The general info about the resource: Link, icon for the type, etc. The first column in the standard nav map display. This column also accepts -the following parameter in the renderer hash: +the following parameters in the renderer hash: =over 4 @@ -737,6 +774,14 @@ returns a true or false value. If true, false, it is simply skipped in the display. By default, all resources are shown. +=item * B: + +If you're using a filter function, and displaying sequences to orient +the user, then frequently some sequences will be empty. Setting this to +true will cause those sequences not to display, so as not to confuse the +user into thinking that if the sequence is there there should be things +under it. + =item * B: If true, will not display Navigate Content resources. Default to @@ -796,15 +841,22 @@ sub render_resource { my $linkopen = ""; my $linkclose = ""; - # Default icon: HTML page - my $icon = ""; + # Default icon: unknown page + my $icon = ""; if ($resource->is_problem()) { - if ($part eq "" || $params->{'condensed'}) { + if ($part eq '0' || $params->{'condensed'}) { $icon = ''; } else { $icon = $params->{'indentString'}; } + } else { + my $curfext= (split (/\./,$resource->src))[-1]; + my $embstyle = &Apache::loncommon::fileembstyle($curfext); + # The unless conditional that follows is a bit of overkill + if (!(!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn')) { + $icon = ""; + } } # Display the correct map icon to open or shut map @@ -815,8 +867,10 @@ sub render_resource { $nowOpen = !$nowOpen; } + my $folderType = $resource->is_sequence() ? 'folder' : 'page'; + if (!$params->{'resource_no_folder_link'}) { - $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') . '.gif'; + $icon = "navmap.$folderType." . ($nowOpen ? 'closed' : 'open') . '.gif'; $icon = ""; $linkopen = ""; } else { # Don't allow users to manipulate folder - $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') . + $icon = "navmap.$folderType." . ($nowOpen ? 'closed' : 'open') . '.nomanip.gif'; $icon = ""; @@ -870,7 +924,7 @@ sub render_resource { $params->{'displayedHereMarker'} = 1; } - if ($resource->is_problem() && $part ne "" && + if ($resource->is_problem() && $part ne '0' && !$params->{'condensed'}) { $partLabel = " (Part $part)"; $title = ""; @@ -880,7 +934,8 @@ sub render_resource { $nonLinkedText .= ' (' . $resource->countParts() . ' parts)'; } - if (!$params->{'resource_nolink'}) { + if (!$params->{'resource_nolink'} && $src !~ /^\/uploaded\// && + !$resource->is_sequence()) { $result .= " $curMarkerBegin$title$partLabel$curMarkerEnd $nonLinkedText"; } else { $result .= " $curMarkerBegin$title$partLabel$curMarkerEnd $nonLinkedText"; @@ -927,6 +982,10 @@ sub render_communication_status { } } + if ($params->{'multipart'} && $part != '0') { + $discussionHTML = $feedbackHTML = $errorHTML = ''; + } + return "$discussionHTML$feedbackHTML$errorHTML "; } @@ -962,7 +1021,7 @@ sub render_long_status { $params->{'multipart'} && $part eq "0"; my $color; - if ($resource->is_problem()) { + if ($resource->is_problem() && ($resource->countParts() <= 1) ) { $color = $colormap{$resource->status}; if (dueInLessThen24Hours($resource, $part) || @@ -982,8 +1041,6 @@ sub render_long_status { $result .= '(randomly select ' . $resource->randompick() .')'; } - $result .= " \n"; - return $result; } @@ -1033,6 +1090,19 @@ sub render { } } + # Filter: Remember filter function and add our own filter: Refuse + # to show hidden resources unless the user can see them. + my $userCanSeeHidden = advancedUser(); + my $filterFunc = setDefault($args->{'filterFunc'}, + sub {return 1;}); + if (!$userCanSeeHidden) { + # Without renaming the filterfunc, the server seems to go into + # an infinite loop + my $oldFilterFunc = $filterFunc; + $filterFunc = sub { my $res = shift; return !$res->randomout() && + &$oldFilterFunc($res);}; + } + my $condition = 0; if ($ENV{'form.condition'}) { $condition = 1; @@ -1129,6 +1199,8 @@ sub render { } # (re-)Locate the jump point, if any + # Note this does not take filtering or hidden into account... need + # to be fixed? my $mapIterator = $navmap->getIterator(undef, undef, $filterHash, 0); my $depth = 1; $mapIterator->next(); @@ -1161,8 +1233,6 @@ sub render { my $printKey = $args->{'printKey'}; my $printCloseAll = $args->{'printCloseAll'}; if (!defined($printCloseAll)) { $printCloseAll = 1; } - my $filterFunc = setDefault($args->{'filterFunc'}, - sub {return 1;}); # Print key? if ($printKey) { @@ -1223,6 +1293,43 @@ sub render { $args->{'indentString'} = setDefault($args->{'indentString'}, ""); $args->{'displayedHereMarker'} = 0; + # If we're suppressing empty sequences, look for them here. Use DFS for speed, + # since structure actually doesn't matter, except what map has what resources. + if ($args->{'suppressEmptySequences'}) { + my $dfsit = Apache::lonnavmaps::DFSiterator->new($navmap, + $it->{FIRST_RESOURCE}, + $it->{FINISH_RESOURCE}, + {}, undef, 1); + $depth = 0; + $dfsit->next(); + my $curRes = $dfsit->next(); + while ($depth > -1) { + if ($curRes == $dfsit->BEGIN_MAP()) { $depth++; } + if ($curRes == $dfsit->END_MAP()) { $depth--; } + + if (ref($curRes)) { + # Parallel pre-processing: Do sequences have non-filtered-out children? + if ($curRes->is_map()) { + $curRes->{DATA}->{HAS_VISIBLE_CHILDREN} = 0; + # Sequences themselves do not count as visible children, + # unless those sequences also have visible children. + # This means if a sequence appears, there's a "promise" + # that there's something under it if you open it, somewhere. + } else { + # Not a sequence: if it's filtered, ignore it, otherwise + # rise up the stack and mark the sequences as having children + if (&$filterFunc($curRes)) { + for my $sequence (@{$dfsit->getStack()}) { + $sequence->{DATA}->{HAS_VISIBLE_CHILDREN} = 1; + } + } + } + } + } continue { + $curRes = $dfsit->next(); + } + } + my $displayedJumpMarker = 0; # Set up iteration. $depth = 1; @@ -1258,19 +1365,25 @@ sub render { next; } - $args->{'counter'}++; - # If this has been filtered out, continue on if (!(&$filterFunc($curRes))) { $args->{'isNewBranch'} = 0; # Don't falsely remember this next; } + # If this is an empty sequence and we're filtering them, continue on + if ($curRes->is_map() && $args->{'suppressEmptySequences'} && + !$curRes->{DATA}->{HAS_VISIBLE_CHILDREN}) { + next; + } + # If we're suppressing navmaps and this is a navmap, continue on if ($suppressNavmap && $curRes->src() =~ /^\/adm\/navmaps/) { next; } + $args->{'counter'}++; + # Does it have multiple parts? $args->{'multipart'} = 0; $args->{'condensed'} = 0; @@ -1279,7 +1392,7 @@ sub render { # Decide what parts to show. if ($curRes->is_problem() && $showParts) { @parts = @{$curRes->parts()}; - $args->{'multipart'} = scalar(@parts) > 1; + $args->{'multipart'} = $curRes->multipart(); if ($condenseParts) { # do the condensation if (!$curRes->opendate("0")) { @@ -1288,13 +1401,13 @@ sub render { } if (!$args->{'condensed'}) { # Decide whether to condense based on similarity - my $status = $curRes->status($parts[1]); - my $due = $curRes->duedate($parts[1]); - my $open = $curRes->opendate($parts[1]); + my $status = $curRes->status($parts[0]); + my $due = $curRes->duedate($parts[0]); + my $open = $curRes->opendate($parts[0]); my $statusAllSame = 1; my $dueAllSame = 1; my $openAllSame = 1; - for (my $i = 2; $i < scalar(@parts); $i++) { + for (my $i = 1; $i < scalar(@parts); $i++) { if ($curRes->status($parts[$i]) != $status){ $statusAllSame = 0; } @@ -1315,25 +1428,30 @@ sub render { if (($statusAllSame && defined($condenseStatuses{$status})) || ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)|| ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){ - @parts = (); + @parts = ($parts[0]); $args->{'condensed'} = 1; } - } + # Multipart problem with one part: always "condense" (happens + # to match the desirable behavior) + if ($curRes->countParts() == 1) { + @parts = ($parts[0]); + $args->{'condensed'} = 1; + } } } # If the multipart problem was condensed, "forget" it was multipart if (scalar(@parts) == 1) { $args->{'multipart'} = 0; + } else { + # Add part 0 so we display it correctly. + unshift @parts, '0'; } # Now, we've decided what parts to show. Loop through them and # show them. - foreach my $part ('', @parts) { - if ($part eq '0') { - next; - } + foreach my $part (@parts) { $rownum ++; my $backgroundColor = $backgroundColors[$rownum % scalar(@backgroundColors)]; @@ -1381,6 +1499,16 @@ sub render { } } continue { $curRes = $it->next(); + + if ($r) { + # If we have the connection, make sure the user is still connected + my $c = $r->connection; + if ($c->aborted()) { + Apache::lonnet::logthis("navmaps aborted"); + # Who cares what we do, nobody will see it anyhow. + return ''; + } + } } # Print out the part that jumps to #curloc if it exists @@ -1567,7 +1695,7 @@ sub init { my %emailstatus = &Apache::lonnet::dump('email_status'); my $logoutTime = $emailstatus{'logout'}; my $courseLeaveTime = $emailstatus{'logout_'.$ENV{'request.course.id'}}; - $self->{LAST_CHECK} = ($courseLeaveTime < $logoutTime ? + $self->{LAST_CHECK} = (($courseLeaveTime > $logoutTime) ? $courseLeaveTime : $logoutTime); my %discussiontime = &Apache::lonnet::dump('discussiontimes', $cdom, $cnum); @@ -1684,6 +1812,16 @@ object for that resource. This method, o (as in the resource object) is the only proper way to obtain a resource object. +=item * B(symb): + +Based on the symb of the resource, get a resource object for that +resource. This is one of the proper ways to get a resource object. + +=item * B(map_pc): + +Based on the map_pc of the resource, get a resource object for +the given map. This is one of the proper ways to get a resource object. + =cut # The strategy here is to cache the resource objects, and only construct them @@ -1714,6 +1852,14 @@ sub getBySymb { return $self->getById($map->map_pc() . '.' . $id); } +sub getByMapPc { + my $self = shift; + my $map_pc = shift; + my $map_id = $self->{NAV_HASH}->{'map_id_' . $map_pc}; + $map_id = $self->{NAV_HASH}->{'ids_' . $map_id}; + return $self->getById($map_id); +} + =pod =item * B(): @@ -2008,7 +2154,7 @@ corresponds to where you want the iterat navmap->finishResource(). filterHash is a hash used as a set containing strings representing the resource IDs, defaulting to empty. Condition is a 1 or 0 that sets what to do with the filter -hash: If a 0, then only resource that exist IN the filterHash will be +hash: If a 0, then only resources that exist IN the filterHash will be recursed on. If it is a 1, only resources NOT in the filterHash will be recursed on. Defaults to 0. forceTop is a boolean value. If it is false (default), the iterator will only return the first level of map @@ -2194,7 +2340,8 @@ sub new { $curRes->{DATA}->{DISPLAY_DEPTH} = $finalDepth; if ($finalDepth > $maxDepth) {$maxDepth = $finalDepth;} - } + } + } continue { $curRes = $iterator->next(); } } @@ -2571,6 +2718,31 @@ sub next { return $self->{HERE}; } +# Identical to the full iterator methods of the same name. Hate to copy/paste +# but I also hate to "inherit" either iterator from the other. + +sub getStack { + my $self=shift; + + my @stack; + + $self->populateStack(\@stack); + + return \@stack; +} + +# Private method: Calls the iterators recursively to populate the stack. +sub populateStack { + my $self=shift; + my $stack = shift; + + push @$stack, $self->{HERE} if ($self->{HERE}); + + if ($self->{RECURSIVE_ITERATOR_FLAG}) { + $self->{RECURSIVE_ITERATOR}->populateStack($stack); + } +} + 1; package Apache::lonnavmaps::resource; @@ -2792,7 +2964,8 @@ sub is_map { my $self=shift; return defi sub is_page { my $self=shift; my $src = $self->src(); - return ($src =~ /page$/); + return $self->navHash("is_map_", 1) && + $self->navHash("map_type_" . $self->map_pc()) eq 'page'; } sub is_problem { my $self=shift; @@ -2802,7 +2975,8 @@ sub is_problem { sub is_sequence { my $self=shift; my $src = $self->src(); - return ($src =~ /sequence$/); + return $self->navHash("is_map_", 1) && + $self->navHash("map_type_" . $self->map_pc()) eq 'sequence'; } # Private method: Shells out to the parmval in the nav map, handler parts. @@ -2877,8 +3051,6 @@ sub map_type { return $self->navHash("map_type_$pc", 0); } - - ##### # Property queries ##### @@ -3090,27 +3262,30 @@ sub getErrors { =item * B(): Returns a list reference containing sorted strings corresponding to -each part of the problem. To count the number of parts, use the list -in a scalar context, and subtract one if greater than two. (One part -problems have a part 0. Multi-parts have a part 0, plus a part for -each part. Filtering part 0 if you want it is up to you.) +each part of the problem. Single part problems have only a part '0'. +Multipart problems do not return their part '0', since they typically +do not really matter. =item * B(): Returns the number of parts of the problem a student can answer. Thus, for single part problems, returns 1. For multipart, it returns the -number of parts in the problem, not including psuedo-part 0. Thus, -B may return an array with fewer parts in it then countParts -might lead you to believe. +number of parts in the problem, not including psuedo-part 0. + +=item * B(): + +Returns true if the problem is multipart, false otherwise. Use this instead +of countParts if all you want is multipart/not multipart. =item * B($part): Returns the response type of the part, without the word "response" on the end. Example return values: 'string', 'essay', 'numeric', etc. -=item * B($part): +=item * B($part): -Retreives the response ID for the given part, which may be an empty string. +Retreives the response IDs for the given part as an array reference containing +strings naming the response IDs. This may be empty. =back @@ -3119,7 +3294,7 @@ Retreives the response ID for the given sub parts { my $self = shift; - if ($self->ext) { return ['0']; } + if ($self->ext) { return []; } $self->extractParts(); return $self->{PARTS}; @@ -3129,16 +3304,23 @@ sub countParts { my $self = shift; my $parts = $self->parts(); - my $delta = 0; - for my $part (@$parts) { - if ($part eq '0') { $delta--; } - } + + # If I left this here, then it's not necessary. + #my $delta = 0; + #for my $part (@$parts) { + # if ($part eq '0') { $delta--; } + #} if ($self->{RESOURCE_ERROR}) { return 0; } - return scalar(@{$parts}) + $delta; + return scalar(@{$parts}); # + $delta; +} + +sub multipart { + my $self = shift; + return $self->countParts() > 1; } sub responseType { @@ -3149,7 +3331,7 @@ sub responseType { return $self->{RESPONSE_TYPE}->{$part}; } -sub responseId { +sub responseIds { my $self = shift; my $part = shift; @@ -3201,6 +3383,12 @@ sub extractParts { my %responseIdHash; my %responseTypeHash; + + # Init the responseIdHash + foreach (@{$self->{PARTS}}) { + $responseIdHash{$_} = []; + } + # Now, the unfortunate thing about this is that parts, part name, and # response if are delimited by underscores, but both the part # name and response id can themselves have underscores in them. @@ -3221,9 +3409,6 @@ sub extractParts { if ($parts{$partIdSoFar}) { my @otherChunks = @partChunks[$i+1..$#partChunks]; my $responseId = join('_', @otherChunks); - if (!defined($responseIdHash{$partIdSoFar})) { - $responseIdHash{$partIdSoFar} = []; - } push @{$responseIdHash{$partIdSoFar}}, $responseId; $responseTypeHash{$partIdSoFar} = $responseType; last; @@ -3452,7 +3637,9 @@ B Along with directly returning the date or completion status, the resource object includes a convenience function B() that will combine the two status tidbits into one composite status that can -represent the status of the resource as a whole. The precise logic is +represent the status of the resource as a whole. This method represents +the concept of the thing we want to display to the user on the nav maps +screen, which is a combination of completion and open status. The precise logic is documented in the comments of the status method. The following results may be returned, all available as methods on the resource object ($res->NETWORK_FAILURE): In addition to the return values that match @@ -3595,6 +3782,48 @@ sub status { } =pod + +B + +The completable method represents the concept of I. If the student can do the problem, which means +that it is open, there are tries left, and if the problem is manually graded +or the grade is suppressed via problemstatus, the student has not tried it +yet, then the method returns 1. Otherwise, it returns 0, to indicate that +either the student has tried it and there is no feedback, or that for +some reason it is no longer completable (not open yet, successfully completed, +out of tries, etc.). As an example, this is used as the filter for the +"Uncompleted Homework" option for the nav maps. + +If this does not quite meet your needs, do not fiddle with it (unless you are +fixing it to better match the student's conception of "completable" because +it's broken somehow)... make a new method. + +=cut + +sub completable { + my $self = shift; + if (!$self->is_problem()) { return 0; } + my $partCount = $self->countParts(); + + foreach my $part (@{$self->parts()}) { + if ($part eq '0' && $partCount != 1) { next; } + my $status = $self->status($part); + # "If any of the parts are open, or have tries left (implies open), + # and it is not "attempted" (manually graded problem), it is + # not "complete" + if (!(($status == OPEN() || $status == TRIES_LEFT()) + && $self->getCompletionStatus($part) != ATTEMPTED() + && $status != ANSWER_SUBMITTED())) { + return 0; + } + } + + # If all the parts were complete, so was this problem. + return 1; +} + +=pod =head2 Resource/Nav Map Navigation