--- loncom/interface/lonnavmaps.pm 2006/12/20 23:02:33 1.393 +++ loncom/interface/lonnavmaps.pm 2007/09/01 00:41:42 1.401 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Navigate Maps Handler # -# $Id: lonnavmaps.pm,v 1.393 2006/12/20 23:02:33 raeburn Exp $ +# $Id: lonnavmaps.pm,v 1.401 2007/09/01 00:41:42 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -38,7 +38,6 @@ use Apache::lonnet; use POSIX qw (floor strftime); use Data::Dumper; # for debugging, not always use Time::HiRes qw( gettimeofday tv_interval ); -use lib '/home/httpd/lib/perl/'; use LONCAPA; # symbolic constants @@ -86,7 +85,7 @@ my %colormap = $resObj->PARTIALLY_CORRECT => '#006600' ); # 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 +# is not yet done and due in less than 24 hours my $hurryUpColor = "#FF0000"; sub close { @@ -149,7 +148,7 @@ sub getLinkForResource { if (defined($res)) { my $anchor; if ($res->is_page()) { - foreach (@$stack) { if (defined($_)) { $anchor = $_; } } + foreach my $item (@$stack) { if (defined($item)) { $anchor = $item; } } $anchor=&escape($anchor->shown_symb()); return ($res->link(),$res->shown_symb(),$anchor); } @@ -167,11 +166,14 @@ sub getLinkForResource { # (when we first recurse on a map, it puts an undefined resource # on the bottom because $self->{HERE} isn't defined yet, and we # want the src for the map anyhow) - foreach (@$stack) { - if (defined($_)) { $res = $_; } + foreach my $item (@$stack) { + if (defined($item)) { $res = $item; } } - return ($res->link(),$res->shown_symb()); + if ($res) { + return ($res->link(),$res->shown_symb()); + } + return; } # Convenience function: This separates the logic of how to create @@ -239,7 +241,7 @@ sub getDescription { } } -# Convenience function, so others can use it: Is the problem due in less then +# Convenience function, so others can use it: Is the problem due in less than # 24 hours, and still can be done? sub dueInLessThan24Hours { @@ -254,7 +256,7 @@ sub dueInLessThan24Hours { } # Convenience function, so others can use it: Is there only one try remaining for the -# part, with more then one try to begin with, not due yet and still can be done? +# part, with more than one try to begin with, not due yet and still can be done? sub lastTry { my $res = shift; my $part = shift; @@ -323,13 +325,13 @@ sub timeToHumanString { my $tense = $inPast ? " ago" : ""; my $prefix = $inPast ? "" : "in "; - # Less then a minute + # Less than a minute if ( $delta < $minute ) { if ($delta == 1) { return "${prefix}1 second$tense"; } return "$prefix$delta seconds$tense"; } - # Less then an hour + # Less than an hour if ( $delta < $hour ) { # If so, use minutes my $minutes = floor($delta / 60); @@ -337,7 +339,7 @@ sub timeToHumanString { return "$prefix$minutes minutes$tense"; } - # Is it less then 24 hours away? If so, + # Is it less than 24 hours away? If so, # display hours + minutes if ( $delta < $hour * 24) { my $hours = floor($delta / $hour); @@ -363,7 +365,7 @@ sub timeToHumanString { return $timeStr.&Apache::lonlocal::gettimezone($time); } - # Less then 5 days away, display day of the week and + # Less than 5 days away, display day of the week and # HH:MM if ( $delta < $day * 5 ) { @@ -428,7 +430,7 @@ to compute due to the amount of data tha processed. Apache::lonnavmaps provides an object model for manipulating this -information in a higher-level fashion then directly manipulating +information in a higher-level fashion than directly manipulating the hash. It also provides access to several auxilary functions that aren't necessarily stored in the Big Hash, but are a per- resource sort of value, like whether there is any feedback on @@ -474,7 +476,7 @@ Apache::lonnavmaps::render({}). =head2 Overview of Columns The renderer will build an HTML table for the navmap and return -it. The table is consists of several columns, and a row for each +it. The table 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 prepared columns, and the renderer will assemble @@ -605,6 +607,11 @@ instruct the renderer to render only a p the source of the map you want to process, like '/res/103/jerf/navmap.course.sequence'. +=item * B: default: false + +If you need to include the top level map (meaning the course) in the +rendered output set this to true + =item * B: default: constructs one from %env A reference to a navmap, used only if an iterator is not passed in. If @@ -904,10 +911,10 @@ sub render_communication_status { if ($resource->getFeedback()) { my $feedback = $resource->getFeedback(); - foreach (split(/\,/, $feedback)) { - if ($_) { + foreach my $msgid (split(/\,/, $feedback)) { + if ($msgid) { $feedbackHTML .= ' ' + . &escape($msgid) . '">' . ''.&mt('New Email').''; } @@ -917,12 +924,12 @@ sub render_communication_status { if ($resource->getErrors()) { my $errors = $resource->getErrors(); my $errorcount = 0; - foreach (split(/,/, $errors)) { + foreach my $msgid (split(/,/, $errors)) { last if ($errorcount>=10); # Only output 10 bombs maximum - if ($_) { + if ($msgid) { $errorcount++; $errorHTML .= ' ' + . &escape($msgid) . '">' . ''.&mt('New Error').''; } @@ -992,7 +999,10 @@ sub render_long_status { if ($color) {$result .= ""; } } if ($resource->is_map() && advancedUser() && $resource->randompick()) { - $result .= '(randomly select ' . $resource->randompick() .')'; + $result .= &mt('(randomly select [_1])', $resource->randompick()); + } + if ($resource->is_map() && &advancedUser() && $resource->randomorder()) { + $result .= &mt('(randomly ordered)'); } # Debugging code @@ -1126,9 +1136,9 @@ sub render { # marker my $filterHash = {}; # Figure out what we're not displaying - foreach (split(/\,/, $env{"form.filter"})) { - if ($_) { - $filterHash->{$_} = "1"; + foreach my $item (split(/\,/, $env{"form.filter"})) { + if ($item) { + $filterHash->{$item} = "1"; } } @@ -1234,7 +1244,7 @@ sub render { $args->{'iterator'} = $it = $navmap->getIterator($firstResource, $finishResource, $filterHash, $condition); } else { - $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition); + $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition,undef,$args->{'include_top_level_map'}); } } @@ -1802,6 +1812,7 @@ See iterator documentation below. use strict; use GDBM_File; use Apache::lonnet; +use LONCAPA; sub new { # magic invocation to create a class instance @@ -1901,10 +1912,10 @@ sub generate_email_discuss_status { my %lastread = &Apache::lonnet::dump('nohist_'.$cid.'_discuss', $env{'user.domain'},$env{'user.name'},'lastread'); my %lastreadtime = (); - foreach (keys %lastread) { - my $key = $_; - $key =~ s/_lastread$//; - $lastreadtime{$key} = $lastread{$_}; + foreach my $key (keys %lastread) { + my $shortkey = $key; + $shortkey =~ s/_lastread$//; + $lastreadtime{$shortkey} = $lastread{$key}; } my %feedback=(); @@ -1914,22 +1925,36 @@ sub generate_email_discuss_status { foreach my $msgid (@keys) { if ((!$emailstatus{$msgid}) || ($emailstatus{$msgid} eq 'new')) { - my $plain= - &LONCAPA::unescape(&LONCAPA::unescape($msgid)); - if ($plain=~/ \[([^\]]+)\]\:/) { - my $url=$1; - if ($plain=~/\:Error \[/) { - $error{$url}.=','.$msgid; - } else { - $feedback{$url}.=','.$msgid; - } - } + my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid, + $symb,$error) = &Apache::lonmsg::unpackmsgid($msgid); + &Apache::lonenc::check_decrypt(\$symb); + if (($fromcid ne '') && ($fromcid ne $cid)) { + next; + } + if (defined($symb)) { + if (defined($error) && $error == 1) { + $error{$symb}.=','.$msgid; + } else { + $feedback{$symb}.=','.$msgid; + } + } else { + my $plain= + &LONCAPA::unescape(&LONCAPA::unescape($msgid)); + if ($plain=~/ \[([^\]]+)\]\:/) { + my $url=$1; + if ($plain=~/\:Error \[/) { + $error{$url}.=','.$msgid; + } else { + $feedback{$url}.=','.$msgid; + } + } + } } } - #url's of resources that have feedbacks + #symbs of resources that have feedbacks (will be urls pre-2.3) $self->{FEEDBACK} = \%feedback; - #or errors + #or errors (will be urls pre 2.3) $self->{ERROR_MSG} = \%error; $self->{DISCUSSION_TIME} = \%discussiontime; $self->{EMAIL_STATUS} = \%emailstatus; @@ -2043,7 +2068,7 @@ sub discussion_info { my $ressymb = $self->wrap_symb($symb); # keys used to store bulletinboard postings use 'unwrapped' symb. - my $discsymb = $self->unwrap_symb($ressymb); + my $discsymb = &escape($self->unwrap_symb($ressymb)); my $version = $self->{DISCUSSION_DATA}{'version:'.$discsymb}; if (!$version) { return; } @@ -2116,23 +2141,48 @@ sub unwrap_symb { sub getFeedback { my $self = shift; my $symb = shift; + my $source = shift; $self->generate_email_discuss_status(); if (!defined($self->{FEEDBACK})) { return ""; } - return $self->{FEEDBACK}->{$symb}; + my $feedback; + if ($self->{FEEDBACK}->{$symb}) { + $feedback = $self->{FEEDBACK}->{$symb}; + if ($self->{FEEDBACK}->{$source}) { + $feedback .= ','.$self->{FEEDBACK}->{$source}; + } + } else { + if ($self->{FEEDBACK}->{$source}) { + $feedback = $self->{FEEDBACK}->{$source}; + } + } + return $feedback; } # Private method: Get the errors for that resource (by source). sub getErrors { my $self = shift; + my $symb = shift; my $src = shift; $self->generate_email_discuss_status(); if (!defined($self->{ERROR_MSG})) { return ""; } - return $self->{ERROR_MSG}->{$src}; + + my $errors; + if ($self->{ERROR_MSG}->{$symb}) { + $errors = $self->{ERROR_MSG}->{$symb}; + if ($self->{ERROR_MSG}->{$src}) { + $errors .= ','.$self->{ERROR_MSG}->{$src}; + } + } else { + if ($self->{ERROR_MSG}->{$src}) { + $errors = $self->{ERROR_MSG}->{$src}; + } + } + return $errors; } =pod @@ -2158,7 +2208,7 @@ the given map. This is one of the proper # The strategy here is to cache the resource objects, and only construct them # as we use them. The real point is to prevent reading any more from the tied -# hash then we have to, which should hopefully alleviate speed problems. +# hash than we have to, which should hopefully alleviate speed problems. sub getById { my $self = shift; @@ -2232,14 +2282,14 @@ sub finishResource { # the actual lookup; parmval caches the results. sub parmval { my $self = shift; - my ($what,$symb)=@_; + my ($what,$symb,$recurse)=@_; my $hashkey = $what."|||".$symb; if (defined($self->{PARM_CACHE}->{$hashkey})) { return $self->{PARM_CACHE}->{$hashkey}; } - my $result = $self->parmval_real($what, $symb); + my $result = $self->parmval_real($what, $symb, $recurse); $self->{PARM_CACHE}->{$hashkey} = $result; return $result; } @@ -2349,7 +2399,7 @@ sub parmval_real { if (defined($partgeneral)) { return $partgeneral; } } if ($recurse) { return undef; } - my $pack_def=&Apache::lonnet::packages_tab_default($fn,'resource.'.$what); + my $pack_def=&Apache::lonnet::packages_tab_default($fn,'resource.'.$rwhat); if (defined($pack_def)) { return $pack_def; } return ''; } @@ -2359,7 +2409,7 @@ sub parmval_real { =item * B(url,multiple): Retrieves a resource object by URL of the resource, unless the optional -multiple parameter is included in wahich caes an array of resource +multiple parameter is included in which case an array of resource objects is returned. 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)" @@ -2394,7 +2444,7 @@ all matching resources. =item * B(map, filterFunc, recursive, showall): -Convience method for +Convenience method for scalar(retrieveResources($map, $filterFunc, $recursive, 1, $showall)) > 0 @@ -2472,6 +2522,10 @@ sub retrieveResources { my @resources = (); + if (&$filterFunc($map)) { + push(@resources, $map); + } + # Run down the iterator and collect the resources. my $curRes; @@ -2481,7 +2535,7 @@ sub retrieveResources { next; } - push @resources, $curRes; + push(@resources, $curRes); if ($bailout) { return @resources; @@ -2628,7 +2682,7 @@ be the tokens described above. Also note there is some old code floating around that trys to track the depth of the iterator to see when it's done; do not copy that -code. It is difficult to get right and harder to understand then +code. It is difficult to get right and harder to understand than this. They should be migrated to this new style. =cut @@ -2812,6 +2866,10 @@ sub next { $self->{HAVE_RETURNED_0} = 1; return $self->{NAV_MAP}->getById('0.0'); } + if ($self->{RETURN_0} && !$self->{HAVE_RETURNED_0_BEGIN_MAP}) { + $self->{HAVE_RETURNED_0_BEGIN_MAP} = 1; + return $self->BEGIN_MAP(); + } if ($self->{RECURSIVE_ITERATOR_FLAG}) { # grab the next from the recursive iterator @@ -2913,7 +2971,7 @@ sub next { } # Is this the end of a branch? If so, all of the resources examined above - # led to lower levels then the one we are currently at, so we push a END_BRANCH + # led to lower levels than the one we are currently at, so we push a END_BRANCH # marker onto the stack so we don't forget. # Example: For the usual A(BC)(DE)F case, when the iterator goes down the # BC branch and gets to C, it will see F as the only next resource, but it's @@ -3122,9 +3180,9 @@ sub next { # filter the next possibilities to remove things we've # already seen. - foreach (@$nextUnfiltered) { - if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) { - push @$next, $_; + foreach my $item (@$nextUnfiltered) { + if (!defined($self->{ALREADY_SEEN}->{$item->{ID}})) { + push @$next, $item; } } @@ -3249,7 +3307,7 @@ X X All resources also have Bs, which uniquely identify a resource in a course. Many internal LON-CAPA functions expect a symb. A symb carries along with it the URL of the resource, and the map it appears -in. Symbs are much larger then resource IDs. +in. Symbs are much larger than resource IDs. =cut @@ -3325,8 +3383,13 @@ false. =item * B: -Returns true for a map if the randompick feature is being used on the -map. (?) +Returns the number of randomly picked items for a map if the randompick +feature is being used on the map. + +=item * B: + +Returns true for a map if the randomorder feature is being used on the +map. =item * B: @@ -3358,6 +3421,10 @@ sub randompick { my $self = shift; return $self->parmval('randompick'); } +sub randomorder { + my $self = shift; + return ($self->parmval('randomorder') =~ /^yes$/i); +} sub link { my $self=shift; if ($self->encrypted()) { return &Apache::lonenc::encrypted($self->src); } @@ -3434,6 +3501,7 @@ sub compTitle { } return $title; } + =pod B @@ -3520,6 +3588,15 @@ sub contains_problem { } return 0; } +sub map_contains_problem { + my $self=shift; + if ($self->is_map()) { + my $has_problem= + $self->hasResource($self,sub { $_[0]->is_problem() },1); + return $has_problem; + } + return 0; +} sub is_sequence { my $self=shift; return $self->navHash("is_map_", 1) && @@ -3626,9 +3703,9 @@ sub map_type { # These functions will be responsible for returning the CORRECT # VALUE for the parameter, no matter what. So while they may look -# like direct calls to parmval, they can be more then that. +# like direct calls to parmval, they can be more than that. # So, for instance, the duedate function should use the "duedatetype" -# information, rather then the resource object user. +# information, rather than the resource object user. =pod @@ -3742,6 +3819,15 @@ sub duedate { } sub handgrade { (my $self, my $part) = @_; + my @response_ids = $self->responseIds($part); + if (@response_ids) { + foreach my $response_id (@response_ids) { + if (lc($self->parmval("handgrade",$part.'_'.$response_id)) + eq 'yes') { + return 'yes'; + } + } + } return $self->parmval("handgrade", $part); } sub maxtries { @@ -3857,8 +3943,8 @@ for the resource, or the null string if email data was not extracted when the nav map was constructed. Usually used like this: - for (split(/\,/, $res->getFeedback())) { - my $link = &escape($_); + for my $url (split(/\,/, $res->getFeedback())) { + my $link = &escape($url); ... and use the link as appropriate. @@ -3883,15 +3969,17 @@ sub discussion_info { sub getFeedback { my $self = shift; my $source = $self->src(); + my $symb = $self->symb(); if ($source =~ /^\/res\//) { $source = substr $source, 5; } - return $self->{NAV_MAP}->getFeedback($source); + return $self->{NAV_MAP}->getFeedback($symb,$source); } sub getErrors { my $self = shift; my $source = $self->src(); + my $symb = $self->symb(); if ($source =~ /^\/res\//) { $source = substr $source, 5; } - return $self->{NAV_MAP}->getErrors($source); + return $self->{NAV_MAP}->getErrors($symb,$source); } =pod @@ -4052,8 +4140,8 @@ sub extractParts { $self->{PART_TYPE} = {}; return; } - foreach (split(/\,/,$metadata)) { - if ($_ =~ /^(?:part|Task)_(.*)$/) { + foreach my $entry (split(/\,/,$metadata)) { + if ($entry =~ /^(?:part|Task)_(.*)$/) { my $part = $1; # This floods the logs if it blows up if (defined($parts{$part})) { @@ -4078,8 +4166,8 @@ sub extractParts { # Init the responseIdHash - foreach (@{$self->{PARTS}}) { - $responseIdHash{$_} = []; + foreach my $part (@{$self->{PARTS}}) { + $responseIdHash{$part} = []; } # Now, the unfortunate thing about this is that parts, part name, and @@ -4159,13 +4247,13 @@ the completion information. Idiomatic usage of these two methods would probably look something like - foreach ($resource->parts()) { - my $dateStatus = $resource->getDateStatus($_); - my $completionStatus = $resource->getCompletionStatus($_); + foreach my $part ($resource->parts()) { + my $dateStatus = $resource->getDateStatus($part); + my $completionStatus = $resource->getCompletionStatus($part); or - my $status = $resource->status($_); + my $status = $resource->status($part); ... use it here ... }