Annotation of loncom/interface/lonnavmaps.pm, revision 1.267.2.8
1.2 www 1: # The LearningOnline Network with CAPA
2: # Navigate Maps Handler
1.1 www 3: #
1.267.2.8! albertel 4: # $Id: lonnavmaps.pm,v 1.267.2.7 2004/09/21 19:28:34 albertel Exp $
1.20 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.245 www 28: ###
1.2 www 29:
1.1 www 30: package Apache::lonnavmaps;
31:
32: use strict;
1.2 www 33: use Apache::Constants qw(:common :http);
1.18 albertel 34: use Apache::loncommon();
1.150 www 35: use Apache::lonmenu();
1.229 www 36: use Apache::lonlocal;
1.73 bowersj2 37: use POSIX qw (floor strftime);
1.192 bowersj2 38: use Data::Dumper; # for debugging, not always used
1.2 www 39:
1.133 bowersj2 40: # symbolic constants
41: sub SYMB { return 1; }
42: sub URL { return 2; }
43: sub NOTHING { return 3; }
44:
45: # Some data
46:
1.140 bowersj2 47: my $resObj = "Apache::lonnavmaps::resource";
48:
1.135 bowersj2 49: # Keep these mappings in sync with lonquickgrades, which uses the colors
50: # instead of the icons.
51: my %statusIconMap =
1.224 bowersj2 52: (
53: $resObj->CLOSED => '',
54: $resObj->OPEN => 'navmap.open.gif',
55: $resObj->CORRECT => 'navmap.correct.gif',
56: $resObj->INCORRECT => 'navmap.wrong.gif',
57: $resObj->ATTEMPTED => 'navmap.ellipsis.gif',
58: $resObj->ERROR => ''
59: );
1.135 bowersj2 60:
61: my %iconAltTags =
62: ( 'navmap.correct.gif' => 'Correct',
63: 'navmap.wrong.gif' => 'Incorrect',
64: 'navmap.open.gif' => 'Open' );
1.133 bowersj2 65:
1.136 bowersj2 66: # Defines a status->color mapping, null string means don't color
67: my %colormap =
1.140 bowersj2 68: ( $resObj->NETWORK_FAILURE => '',
69: $resObj->CORRECT => '',
70: $resObj->EXCUSED => '#3333FF',
71: $resObj->PAST_DUE_ANSWER_LATER => '',
72: $resObj->PAST_DUE_NO_ANSWER => '',
73: $resObj->ANSWER_OPEN => '#006600',
74: $resObj->OPEN_LATER => '',
75: $resObj->TRIES_LEFT => '',
76: $resObj->INCORRECT => '',
77: $resObj->OPEN => '',
1.207 bowersj2 78: $resObj->NOTHING_SET => '',
79: $resObj->ATTEMPTED => '',
80: $resObj->ANSWER_SUBMITTED => ''
81: );
1.136 bowersj2 82: # And a special case in the nav map; what to do when the assignment
83: # is not yet done and due in less then 24 hours
84: my $hurryUpColor = "#FF0000";
1.130 www 85:
1.1 www 86: sub handler {
1.99 bowersj2 87: my $r = shift;
1.118 bowersj2 88: real_handler($r);
89: }
90:
91: sub real_handler {
92: my $r = shift;
1.2 www 93:
1.51 bowersj2 94: # Handle header-only request
95: if ($r->header_only) {
96: if ($ENV{'browser.mathml'}) {
1.232 www 97: &Apache::loncommon::content_type($r,'text/xml');
1.51 bowersj2 98: } else {
1.232 www 99: &Apache::loncommon::content_type($r,'text/html');
1.51 bowersj2 100: }
101: $r->send_http_header;
102: return OK;
103: }
104:
105: # Send header, don't cache this page
106: if ($ENV{'browser.mathml'}) {
1.232 www 107: &Apache::loncommon::content_type($r,'text/xml');
1.51 bowersj2 108: } else {
1.232 www 109: &Apache::loncommon::content_type($r,'text/html');
1.51 bowersj2 110: }
111: &Apache::loncommon::no_cache($r);
112: $r->send_http_header;
113:
1.106 bowersj2 114: # Create the nav map
1.221 bowersj2 115: my $navmap = Apache::lonnavmaps::navmap->new();
1.51 bowersj2 116:
1.55 bowersj2 117: if (!defined($navmap)) {
118: my $requrl = $r->uri;
119: $ENV{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
120: return HTTP_NOT_ACCEPTABLE;
121: }
1.64 bowersj2 122:
1.111 bowersj2 123: $r->print("<html><head>\n");
1.229 www 124: $r->print("<title>".&mt('Navigate Course Contents')."</title>");
1.150 www 125: # ------------------------------------------------------------ Get query string
126: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['register']);
1.158 bowersj2 127:
1.150 www 128: # ----------------------------------------------------- Force menu registration
129: my $addentries='';
130: if ($ENV{'form.register'}) {
131: $addentries=' onLoad="'.&Apache::lonmenu::loadevents().
132: '" onUnload="'.&Apache::lonmenu::unloadevents().'"';
133: $r->print(&Apache::lonmenu::registerurl(1));
134: }
1.111 bowersj2 135:
1.64 bowersj2 136: # Header
1.150 www 137: $r->print('</head>'.
138: &Apache::loncommon::bodytag('Navigate Course Contents','',
1.151 www 139: $addentries,'','',$ENV{'form.register'}));
1.262 www 140: $r->print('<script>window.focus();</script>'.
1.265 albertel 141: &Apache::loncommon::help_open_menu('','Navigation Screen','Navigation_Screen','',undef,'RAT'));
142:
1.124 bowersj2 143: $r->rflush();
144:
1.55 bowersj2 145: # Check that it's defined
146: if (!($navmap->courseMapDefined())) {
147: $r->print('<font size="+2" color="red">Coursemap undefined.</font>' .
148: '</body></html>');
149: return OK;
150: }
151:
1.178 bowersj2 152: # See if there's only one map in the top-level, if we don't
153: # already have a filter... if so, automatically display it
1.222 bowersj2 154: # (older code; should use retrieveResources)
1.179 bowersj2 155: if ($ENV{QUERY_STRING} !~ /filter/) {
1.178 bowersj2 156: my $iterator = $navmap->getIterator(undef, undef, undef, 0);
1.222 bowersj2 157: my $curRes;
1.178 bowersj2 158: my $sequenceCount = 0;
159: my $sequenceId;
1.222 bowersj2 160: while ($curRes = $iterator->next()) {
1.178 bowersj2 161: if (ref($curRes) && $curRes->is_sequence()) {
162: $sequenceCount++;
163: $sequenceId = $curRes->map_pc();
1.179 bowersj2 164: }
1.178 bowersj2 165: }
166:
167: if ($sequenceCount == 1) {
168: # The automatic iterator creation in the render call
169: # will pick this up. We know the condition because
170: # the defined($ENV{'form.filter'}) also ensures this
171: # is a fresh call.
172: $ENV{'form.filter'} = "$sequenceId";
1.155 bowersj2 173: }
174: }
175:
1.191 bowersj2 176: my $jumpToFirstHomework = 0;
1.184 bowersj2 177: # Check to see if the student is jumping to next open, do-able problem
178: if ($ENV{QUERY_STRING} eq 'jumpToFirstHomework') {
1.191 bowersj2 179: $jumpToFirstHomework = 1;
1.184 bowersj2 180: # Find the next homework problem that they can do.
181: my $iterator = $navmap->getIterator(undef, undef, undef, 1);
1.222 bowersj2 182: my $curRes;
1.184 bowersj2 183: my $foundDoableProblem = 0;
184: my $problemRes;
185:
1.222 bowersj2 186: while (($curRes = $iterator->next()) && !$foundDoableProblem) {
1.184 bowersj2 187: if (ref($curRes) && $curRes->is_problem()) {
188: my $status = $curRes->status();
1.191 bowersj2 189: if ($curRes->completable()) {
1.184 bowersj2 190: $problemRes = $curRes;
191: $foundDoableProblem = 1;
192:
193: # Pop open all previous maps
194: my $stack = $iterator->getStack();
195: pop @$stack; # last resource in the stack is the problem
196: # itself, which we don't need in the map stack
197: my @mapPcs = map {$_->map_pc()} @$stack;
198: $ENV{'form.filter'} = join(',', @mapPcs);
199:
200: # Mark as both "here" and "jump"
201: $ENV{'form.postsymb'} = $curRes->symb();
202: }
203: }
204: }
205:
206: # If we found no problems, print a note to that effect.
207: if (!$foundDoableProblem) {
208: $r->print("<font size='+2'>All homework assignments have been completed.</font><br /><br />");
209: }
210: } else {
211: $r->print("<a href='navmaps?jumpToFirstHomework'>" .
1.229 www 212: &mt("Go To My First Homework Problem")."</a> ");
1.184 bowersj2 213: }
214:
1.191 bowersj2 215: my $suppressEmptySequences = 0;
216: my $filterFunc = undef;
1.201 bowersj2 217: my $resource_no_folder_link = 0;
218:
1.191 bowersj2 219: # Display only due homework.
220: my $showOnlyHomework = 0;
221: if ($ENV{QUERY_STRING} eq 'showOnlyHomework') {
222: $showOnlyHomework = 1;
223: $suppressEmptySequences = 1;
224: $filterFunc = sub { my $res = shift;
1.201 bowersj2 225: return $res->completable() || $res->is_map();
1.191 bowersj2 226: };
1.229 www 227: $r->print("<p><font size='+2'>".&mt("Uncompleted Homework")."</font></p>");
1.191 bowersj2 228: $ENV{'form.filter'} = '';
229: $ENV{'form.condition'} = 1;
1.201 bowersj2 230: $resource_no_folder_link = 1;
1.191 bowersj2 231: } else {
232: $r->print("<a href='navmaps?showOnlyHomework'>" .
1.229 www 233: &mt("Show Only Uncompleted Homework")."</a> ");
1.191 bowersj2 234: }
235:
1.133 bowersj2 236: # renderer call
1.191 bowersj2 237: my $renderArgs = { 'cols' => [0,1,2,3],
238: 'url' => '/adm/navmaps',
239: 'navmap' => $navmap,
240: 'suppressNavmap' => 1,
241: 'suppressEmptySequences' => $suppressEmptySequences,
242: 'filterFunc' => $filterFunc,
1.201 bowersj2 243: 'resource_no_folder_link' => $resource_no_folder_link,
1.191 bowersj2 244: 'r' => $r};
245: my $render = render($renderArgs);
246: $navmap->untieHashes();
1.133 bowersj2 247:
1.191 bowersj2 248: # If no resources were printed, print a reassuring message so the
249: # user knows there was no error.
250: if ($renderArgs->{'counter'} == 0) {
251: if ($showOnlyHomework) {
1.229 www 252: $r->print("<p><font size='+1'>".&mt("All homework is currently completed").".</font></p>");
1.191 bowersj2 253: } else { # both jumpToFirstHomework and normal use the same: course must be empty
254: $r->print("<p><font size='+1'>This course is empty.</font></p>");
255: }
256: }
1.51 bowersj2 257:
1.128 bowersj2 258: $r->print("</body></html>");
1.134 bowersj2 259: $r->rflush();
1.59 bowersj2 260:
1.51 bowersj2 261: return OK;
262: }
263:
264: # Convenience functions: Returns a string that adds or subtracts
265: # the second argument from the first hash, appropriate for the
266: # query string that determines which folders to recurse on
267: sub addToFilter {
268: my $hashIn = shift;
269: my $addition = shift;
270: my %hash = %$hashIn;
271: $hash{$addition} = 1;
272:
273: return join (",", keys(%hash));
274: }
275:
276: sub removeFromFilter {
277: my $hashIn = shift;
278: my $subtraction = shift;
279: my %hash = %$hashIn;
280:
281: delete $hash{$subtraction};
282: return join(",", keys(%hash));
283: }
284:
285: # Convenience function: Given a stack returned from getStack on the iterator,
286: # return the correct src() value.
287: # Later, this should add an anchor when we start putting anchors in pages.
288: sub getLinkForResource {
289: my $stack = shift;
290: my $res;
291:
292: # Check to see if there are any pages in the stack
293: foreach $res (@$stack) {
1.248 www 294: if (defined($res)) {
295: if ($res->is_page()) {
296: return $res->src();
297: }
298: # in case folder was skipped over as "only sequence"
299: my ($map,$id,$src)=&Apache::lonnet::decode_symb($res->symb());
300: if ($map=~/\.page$/) {
1.249 www 301: return &Apache::lonnet::clutter($map).'#'.
302: &Apache::lonnet::escape(&Apache::lonnet::declutter($src));
1.248 www 303: }
1.51 bowersj2 304: }
305: }
306:
307: # Failing that, return the src of the last resource that is defined
308: # (when we first recurse on a map, it puts an undefined resource
309: # on the bottom because $self->{HERE} isn't defined yet, and we
310: # want the src for the map anyhow)
311: foreach (@$stack) {
312: if (defined($_)) { $res = $_; }
313: }
314:
315: return $res->src();
316: }
317:
1.251 www 318: # Convenience function: This separates the logic of how to create
1.53 bowersj2 319: # the problem text strings ("Due: DATE", "Open: DATE", "Not yet assigned",
1.251 www 320: # etc.) into a separate function. It takes a resource object as the
1.53 bowersj2 321: # first parameter, and the part number of the resource as the second.
322: # It's basically a big switch statement on the status of the resource.
323:
324: sub getDescription {
325: my $res = shift;
326: my $part = shift;
1.69 bowersj2 327: my $status = $res->status($part);
1.53 bowersj2 328:
1.182 bowersj2 329: if ($status == $res->NETWORK_FAILURE) {
1.229 www 330: return &mt("Having technical difficulties; please check status later");
1.182 bowersj2 331: }
1.53 bowersj2 332: if ($status == $res->NOTHING_SET) {
1.229 www 333: return &mt("Not currently assigned.");
1.53 bowersj2 334: }
335: if ($status == $res->OPEN_LATER) {
1.73 bowersj2 336: return "Open " . timeToHumanString($res->opendate($part));
1.53 bowersj2 337: }
338: if ($status == $res->OPEN) {
1.79 bowersj2 339: if ($res->duedate($part)) {
1.229 www 340: return &mt("Due")." " .timeToHumanString($res->duedate($part));
1.66 bowersj2 341: } else {
1.229 www 342: return &mt("Open, no due date");
1.66 bowersj2 343: }
1.53 bowersj2 344: }
1.54 bowersj2 345: if ($status == $res->PAST_DUE_ANSWER_LATER) {
1.229 www 346: return &mt("Answer open")." " . timeToHumanString($res->answerdate($part));
1.54 bowersj2 347: }
348: if ($status == $res->PAST_DUE_NO_ANSWER) {
1.229 www 349: return &mt("Was due")." " . timeToHumanString($res->duedate($part));
1.53 bowersj2 350: }
351: if ($status == $res->ANSWER_OPEN) {
1.229 www 352: return &mt("Answer available");
1.53 bowersj2 353: }
1.59 bowersj2 354: if ($status == $res->EXCUSED) {
1.229 www 355: return &mt("Excused by instructor");
1.59 bowersj2 356: }
1.69 bowersj2 357: if ($status == $res->ATTEMPTED) {
1.229 www 358: return &mt("Answer submitted, not yet graded");
1.69 bowersj2 359: }
1.59 bowersj2 360: if ($status == $res->TRIES_LEFT) {
1.79 bowersj2 361: my $tries = $res->tries($part);
362: my $maxtries = $res->maxtries($part);
363: my $triesString = "";
364: if ($tries && $maxtries) {
365: $triesString = "<font size=\"-1\"><i>($tries of $maxtries tries used)</i></font>";
366: if ($maxtries > 1 && $maxtries - $tries == 1) {
367: $triesString = "<b>$triesString</b>";
368: }
369: }
1.244 albertel 370: if ($res->duedate($part)) {
1.229 www 371: return &mt("Due")." " . timeToHumanString($res->duedate($part)) .
1.66 bowersj2 372: " $triesString";
373: } else {
1.229 www 374: return &mt("No due date")." $triesString";
1.66 bowersj2 375: }
1.59 bowersj2 376: }
1.185 bowersj2 377: if ($status == $res->ANSWER_SUBMITTED) {
1.229 www 378: return &mt('Answer submitted');
1.185 bowersj2 379: }
1.53 bowersj2 380: }
381:
1.115 bowersj2 382: # Convenience function, so others can use it: Is the problem due in less then
383: # 24 hours, and still can be done?
384:
1.252 matthew 385: sub dueInLessThan24Hours {
1.115 bowersj2 386: my $res = shift;
387: my $part = shift;
388: my $status = $res->status($part);
389:
1.207 bowersj2 390: return ($status == $res->OPEN() ||
1.115 bowersj2 391: $status == $res->TRIES_LEFT()) &&
1.244 albertel 392: $res->duedate($part) && $res->duedate($part) < time()+(24*60*60) &&
393: $res->duedate($part) > time();
1.115 bowersj2 394: }
395:
396: # Convenience function, so others can use it: Is there only one try remaining for the
397: # part, with more then one try to begin with, not due yet and still can be done?
398: sub lastTry {
399: my $res = shift;
400: my $part = shift;
401:
402: my $tries = $res->tries($part);
403: my $maxtries = $res->maxtries($part);
404: return $tries && $maxtries && $maxtries > 1 &&
1.244 albertel 405: $maxtries - $tries == 1 && $res->duedate($part) &&
406: $res->duedate($part) > time();
1.115 bowersj2 407: }
408:
1.101 bowersj2 409: # This puts a human-readable name on the ENV variable.
1.177 www 410:
1.68 bowersj2 411: sub advancedUser {
1.177 www 412: return $ENV{'request.role.adv'};
1.68 bowersj2 413: }
414:
1.73 bowersj2 415:
416: # timeToHumanString takes a time number and converts it to a
417: # human-readable representation, meant to be used in the following
418: # manner:
419: # print "Due $timestring"
420: # print "Open $timestring"
421: # print "Answer available $timestring"
422: # Very, very, very, VERY English-only... goodness help a localizer on
423: # this func...
1.53 bowersj2 424: sub timeToHumanString {
1.58 albertel 425: my ($time) = @_;
426: # zero, '0' and blank are bad times
1.73 bowersj2 427: if (!$time) {
1.229 www 428: return &mt('never');
1.73 bowersj2 429: }
1.231 www 430: unless (&Apache::lonlocal::current_language()=~/^en/) {
1.237 www 431: return &Apache::lonlocal::locallocaltime($time);
1.229 www 432: }
1.73 bowersj2 433: my $now = time();
434:
435: my @time = localtime($time);
436: my @now = localtime($now);
437:
438: # Positive = future
439: my $delta = $time - $now;
440:
441: my $minute = 60;
442: my $hour = 60 * $minute;
443: my $day = 24 * $hour;
444: my $week = 7 * $day;
445: my $inPast = 0;
446:
447: # Logic in comments:
448: # Is it now? (extremely unlikely)
449: if ( $delta == 0 ) {
450: return "this instant";
451: }
452:
453: if ($delta < 0) {
454: $inPast = 1;
455: $delta = -$delta;
456: }
457:
458: if ( $delta > 0 ) {
1.101 bowersj2 459:
1.73 bowersj2 460: my $tense = $inPast ? " ago" : "";
461: my $prefix = $inPast ? "" : "in ";
1.101 bowersj2 462:
463: # Less then a minute
1.73 bowersj2 464: if ( $delta < $minute ) {
465: if ($delta == 1) { return "${prefix}1 second$tense"; }
466: return "$prefix$delta seconds$tense";
467: }
468:
1.101 bowersj2 469: # Less then an hour
1.73 bowersj2 470: if ( $delta < $hour ) {
471: # If so, use minutes
472: my $minutes = floor($delta / 60);
473: if ($minutes == 1) { return "${prefix}1 minute$tense"; }
474: return "$prefix$minutes minutes$tense";
475: }
476:
477: # Is it less then 24 hours away? If so,
478: # display hours + minutes
479: if ( $delta < $hour * 24) {
480: my $hours = floor($delta / $hour);
481: my $minutes = floor(($delta % $hour) / $minute);
482: my $hourString = "$hours hours";
483: my $minuteString = ", $minutes minutes";
484: if ($hours == 1) {
485: $hourString = "1 hour";
486: }
487: if ($minutes == 1) {
488: $minuteString = ", 1 minute";
489: }
490: if ($minutes == 0) {
491: $minuteString = "";
492: }
493: return "$prefix$hourString$minuteString$tense";
494: }
495:
496: # Less then 5 days away, display day of the week and
497: # HH:MM
498: if ( $delta < $day * 5 ) {
1.85 bowersj2 499: my $timeStr = strftime("%A, %b %e at %I:%M %P", localtime($time));
1.238 albertel 500: $timeStr =~ s/12:00 am/00:00/;
1.73 bowersj2 501: $timeStr =~ s/12:00 pm/noon/;
502: return ($inPast ? "last " : "next ") .
503: $timeStr;
504: }
505:
506: # Is it this year?
507: if ( $time[5] == $now[5]) {
508: # Return on Month Day, HH:MM meridian
509: my $timeStr = strftime("on %A, %b %e at %I:%M %P", localtime($time));
1.238 albertel 510: $timeStr =~ s/12:00 am/00:00/;
1.73 bowersj2 511: $timeStr =~ s/12:00 pm/noon/;
512: return $timeStr;
513: }
514:
515: # Not this year, so show the year
1.267.2.3 albertel 516: my $timeStr = strftime("on %A, %b %e %Y at %I:%M %P", localtime($time));
1.238 albertel 517: $timeStr =~ s/12:00 am/00:00/;
1.73 bowersj2 518: $timeStr =~ s/12:00 pm/noon/;
519: return $timeStr;
1.58 albertel 520: }
1.53 bowersj2 521: }
522:
1.51 bowersj2 523:
1.133 bowersj2 524: =pod
525:
1.174 albertel 526: =head1 NAME
527:
1.217 bowersj2 528: Apache::lonnavmap - Subroutines to handle and render the navigation
529: maps
1.174 albertel 530:
531: =head1 SYNOPSIS
532:
533: The main handler generates the navigational listing for the course,
534: the other objects export this information in a usable fashion for
1.205 bowersj2 535: other modules.
1.133 bowersj2 536:
1.216 bowersj2 537: =head1 OVERVIEW
538:
1.217 bowersj2 539: X<lonnavmaps, overview> When a user enters a course, LON-CAPA examines the
1.218 bowersj2 540: course structure and caches it in what is often referred to as the
541: "big hash" X<big hash>. You can see it if you are logged into
542: LON-CAPA, in a course, by going to /adm/test. (You may need to
543: tweak the /home/httpd/lonTabs/htpasswd file to view it.) The
544: content of the hash will be under the heading "Big Hash".
1.216 bowersj2 545:
546: Big Hash contains, among other things, how resources are related
547: to each other (next/previous), what resources are maps, which
548: resources are being chosen to not show to the student (for random
549: selection), and a lot of other things that can take a lot of time
550: to compute due to the amount of data that needs to be collected and
551: processed.
552:
553: Apache::lonnavmaps provides an object model for manipulating this
554: information in a higher-level fashion then directly manipulating
555: the hash. It also provides access to several auxilary functions
556: that aren't necessarily stored in the Big Hash, but are a per-
557: resource sort of value, like whether there is any feedback on
558: a given resource.
559:
560: Apache::lonnavmaps also abstracts away branching, and someday,
561: conditions, for the times where you don't really care about those
562: things.
563:
564: Apache::lonnavmaps also provides fairly powerful routines for
565: rendering navmaps, and last but not least, provides the navmaps
566: view for when the user clicks the NAV button.
567:
568: B<Note>: Apache::lonnavmaps I<only> works for the "currently
569: logged in user"; if you want things like "due dates for another
570: student" lonnavmaps can not directly retrieve information like
571: that. You need the EXT function. This module can still help,
572: because many things, such as the course structure, are constant
573: between users, and Apache::lonnavmaps can help by providing
574: symbs for the EXT call.
575:
576: The rest of this file will cover the provided rendering routines,
577: which can often be used without fiddling with the navmap object at
578: all, then documents the Apache::lonnavmaps::navmap object, which
579: is the key to accessing the Big Hash information, covers the use
580: of the Iterator (which provides the logic for traversing the
581: somewhat-complicated Big Hash data structure), documents the
582: Apache::lonnavmaps::Resource objects that are returned by
583:
1.205 bowersj2 584: =head1 Subroutine: render
1.133 bowersj2 585:
1.174 albertel 586: The navmap renderer package provides a sophisticated rendering of the
587: standard navigation maps interface into HTML. The provided nav map
588: handler is actually just a glorified call to this.
1.133 bowersj2 589:
1.216 bowersj2 590: Because of the large number of parameters this function accepts,
1.174 albertel 591: instead of passing it arguments as is normal, pass it in an anonymous
1.216 bowersj2 592: hash with the desired options.
1.174 albertel 593:
594: The package provides a function called 'render', called as
1.205 bowersj2 595: Apache::lonnavmaps::render({}).
1.51 bowersj2 596:
1.133 bowersj2 597: =head2 Overview of Columns
1.51 bowersj2 598:
1.174 albertel 599: The renderer will build an HTML table for the navmap and return
600: it. The table is consists of several columns, and a row for each
601: resource (or possibly each part). You tell the renderer how many
602: columns to create and what to place in each column, optionally using
1.205 bowersj2 603: one or more of the prepared columns, and the renderer will assemble
1.174 albertel 604: the table.
605:
606: Any additional generally useful column types should be placed in the
607: renderer code here, so anybody can use it anywhere else. Any code
608: specific to the current application (such as the addition of <input>
609: elements in a column) should be placed in the code of the thing using
610: the renderer.
611:
612: At the core of the renderer is the array reference COLS (see Example
613: section below for how to pass this correctly). The COLS array will
614: consist of entries of one of two types of things: Either an integer
615: representing one of the pre-packaged column types, or a sub reference
616: that takes a resource reference, a part number, and a reference to the
617: argument hash passed to the renderer, and returns a string that will
618: be inserted into the HTML representation as it.
619:
1.216 bowersj2 620: All other parameters are ways of either changing how the columns
621: are printing, or which rows are shown.
622:
1.174 albertel 623: The pre-packaged column names are refered to by constants in the
1.205 bowersj2 624: Apache::lonnavmaps namespace. The following currently exist:
1.51 bowersj2 625:
1.174 albertel 626: =over 4
1.51 bowersj2 627:
1.216 bowersj2 628: =item * B<Apache::lonnavmaps::resource>:
1.51 bowersj2 629:
1.174 albertel 630: The general info about the resource: Link, icon for the type, etc. The
1.216 bowersj2 631: first column in the standard nav map display. This column provides the
632: indentation effect seen in the B<NAV> screen. This column also accepts
1.205 bowersj2 633: the following parameters in the renderer hash:
1.51 bowersj2 634:
1.133 bowersj2 635: =over 4
1.51 bowersj2 636:
1.216 bowersj2 637: =item * B<resource_nolink>: default false
1.51 bowersj2 638:
1.216 bowersj2 639: If true, the resource will not be linked. By default, all non-folder
640: resources are linked.
1.174 albertel 641:
1.216 bowersj2 642: =item * B<resource_part_count>: default true
1.51 bowersj2 643:
1.216 bowersj2 644: If true, the resource will show a part count B<if> the full
645: part list is not displayed. (See "condense_parts" later.) If false,
646: the resource will never show a part count.
1.133 bowersj2 647:
1.174 albertel 648: =item * B<resource_no_folder_link>:
1.133 bowersj2 649:
1.174 albertel 650: If true, the resource's folder will not be clickable to open or close
651: it. Default is false. True implies printCloseAll is false, since you
652: can't close or open folders when this is on anyhow.
1.144 bowersj2 653:
1.133 bowersj2 654: =back
1.51 bowersj2 655:
1.225 bowersj2 656: =item * B<Apache::lonnavmaps::communication_status>:
1.174 albertel 657:
658: Whether there is discussion on the resource, email for the user, or
659: (lumped in here) perl errors in the execution of the problem. This is
660: the second column in the main nav map.
661:
1.225 bowersj2 662: =item * B<Apache::lonnavmaps::quick_status>:
1.51 bowersj2 663:
1.216 bowersj2 664: An icon for the status of a problem, with five possible states:
665: Correct, incorrect, open, awaiting grading (for a problem where the
666: computer's grade is suppressed, or the computer can't grade, like
667: essay problem), or none (not open yet, not a problem). The
1.174 albertel 668: third column of the standard navmap.
1.51 bowersj2 669:
1.225 bowersj2 670: =item * B<Apache::lonnavmaps::long_status>:
1.174 albertel 671:
672: A text readout of the details of the current status of the problem,
673: such as "Due in 22 hours". The fourth column of the standard navmap.
1.51 bowersj2 674:
1.225 bowersj2 675: =item * B<Apache::lonnavmaps::part_status_summary>:
676:
677: A text readout summarizing the status of the problem. If it is a
678: single part problem, will display "Correct", "Incorrect",
679: "Not yet open", "Open", "Attempted", or "Error". If there are
680: multiple parts, this will output a string that in HTML will show a
681: status of how many parts are in each status, in color coding, trying
682: to match the colors of the icons within reason.
683:
684: Note this only makes sense if you are I<not> showing parts. If
685: C<showParts> is true (see below), this column will not output
686: anything.
687:
1.133 bowersj2 688: =back
1.51 bowersj2 689:
1.133 bowersj2 690: If you add any others please be sure to document them here.
1.51 bowersj2 691:
1.174 albertel 692: An example of a column renderer that will show the ID number of a
693: resource, along with the part name if any:
1.51 bowersj2 694:
1.133 bowersj2 695: sub {
696: my ($resource, $part, $params) = @_;
697: if ($part) { return '<td>' . $resource->{ID} . ' ' . $part . '</td>'; }
698: return '<td>' . $resource->{ID} . '</td>';
699: }
1.51 bowersj2 700:
1.174 albertel 701: Note these functions are responsible for the TD tags, which allow them
702: to override vertical and horizontal alignment, etc.
1.130 www 703:
1.133 bowersj2 704: =head2 Parameters
1.115 bowersj2 705:
1.217 bowersj2 706: Minimally, you should be
1.174 albertel 707: able to get away with just using 'cols' (to specify the columns
708: shown), 'url' (necessary for the folders to link to the current screen
709: correctly), and possibly 'queryString' if your app calls for it. In
710: that case, maintaining the state of the folders will be done
711: automatically.
1.140 bowersj2 712:
1.133 bowersj2 713: =over 4
1.51 bowersj2 714:
1.216 bowersj2 715: =item * B<iterator>: default: constructs one from %ENV
1.174 albertel 716:
717: A reference to a fresh ::iterator to use from the navmaps. The
718: rendering will reflect the options passed to the iterator, so you can
719: use that to just render a certain part of the course, if you like. If
720: one is not passed, the renderer will attempt to construct one from
721: ENV{'form.filter'} and ENV{'form.condition'} information, plus the
722: 'iterator_map' parameter if any.
723:
1.216 bowersj2 724: =item * B<iterator_map>: default: not used
1.174 albertel 725:
726: If you are letting the renderer do the iterator handling, you can
727: instruct the renderer to render only a particular map by passing it
728: the source of the map you want to process, like
729: '/res/103/jerf/navmap.course.sequence'.
730:
1.216 bowersj2 731: =item * B<navmap>: default: constructs one from %ENV
1.174 albertel 732:
733: A reference to a navmap, used only if an iterator is not passed in. If
734: this is necessary to make an iterator but it is not passed in, a new
735: one will be constructed based on ENV info. This is useful to do basic
736: error checking before passing it off to render.
737:
1.216 bowersj2 738: =item * B<r>: default: must be passed in
1.174 albertel 739:
740: The standard Apache response object. This must be passed to the
741: renderer or the course hash will be locked.
742:
1.216 bowersj2 743: =item * B<cols>: default: empty (useless)
1.174 albertel 744:
745: An array reference
746:
1.216 bowersj2 747: =item * B<showParts>:default true
1.174 albertel 748:
1.216 bowersj2 749: A flag. If true, a line for the resource itself, and a line
1.174 albertel 750: for each part will be displayed. If not, only one line for each
751: resource will be displayed.
752:
1.216 bowersj2 753: =item * B<condenseParts>: default true
1.174 albertel 754:
1.216 bowersj2 755: A flag. If true, if all parts of the problem have the same
1.174 albertel 756: status and that status is Nothing Set, Correct, or Network Failure,
757: then only one line will be displayed for that resource anyhow. If no,
758: all parts will always be displayed. If showParts is 0, this is
759: ignored.
760:
1.216 bowersj2 761: =item * B<jumpCount>: default: determined from %ENV
1.174 albertel 762:
1.216 bowersj2 763: A string identifying the URL to place the anchor 'curloc' at.
764: It is the responsibility of the renderer user to
1.174 albertel 765: ensure that the #curloc is in the URL. By default, determined through
766: the use of the ENV{} 'jump' information, and should normally "just
767: work" correctly.
1.144 bowersj2 768:
1.216 bowersj2 769: =item * B<here>: default: empty string
1.140 bowersj2 770:
1.216 bowersj2 771: A Symb identifying where to place the 'here' marker. The empty
772: string means no marker.
1.92 bowersj2 773:
1.216 bowersj2 774: =item * B<indentString>: default: 25 pixel whitespace image
1.164 bowersj2 775:
1.216 bowersj2 776: A string identifying the indentation string to use.
1.92 bowersj2 777:
1.216 bowersj2 778: =item * B<queryString>: default: empty
1.51 bowersj2 779:
1.174 albertel 780: A string which will be prepended to the query string used when the
1.216 bowersj2 781: folders are opened or closed. You can use this to pass
782: application-specific values.
1.55 bowersj2 783:
1.216 bowersj2 784: =item * B<url>: default: none
1.84 bowersj2 785:
1.174 albertel 786: The url the folders will link to, which should be the current
1.216 bowersj2 787: page. Required if the resource info column is shown, and you
788: are allowing the user to open and close folders.
1.115 bowersj2 789:
1.216 bowersj2 790: =item * B<currentJumpIndex>: default: no jumping
1.55 bowersj2 791:
1.174 albertel 792: Describes the currently-open row number to cause the browser to jump
793: to, because the user just opened that folder. By default, pulled from
794: the Jump information in the ENV{'form.*'}.
1.51 bowersj2 795:
1.216 bowersj2 796: =item * B<printKey>: default: false
1.51 bowersj2 797:
1.174 albertel 798: If true, print the key that appears on the top of the standard
1.216 bowersj2 799: navmaps.
1.139 bowersj2 800:
1.216 bowersj2 801: =item * B<printCloseAll>: default: true
1.140 bowersj2 802:
1.174 albertel 803: If true, print the "Close all folders" or "open all folders"
1.216 bowersj2 804: links.
1.140 bowersj2 805:
1.216 bowersj2 806: =item * B<filterFunc>: default: sub {return 1;} (accept everything)
1.153 bowersj2 807:
1.174 albertel 808: A function that takes the resource object as its only parameter and
809: returns a true or false value. If true, the resource is displayed. If
1.216 bowersj2 810: false, it is simply skipped in the display.
1.174 albertel 811:
1.216 bowersj2 812: =item * B<suppressEmptySequences>: default: false
1.190 bowersj2 813:
814: If you're using a filter function, and displaying sequences to orient
815: the user, then frequently some sequences will be empty. Setting this to
816: true will cause those sequences not to display, so as not to confuse the
817: user into thinking that if the sequence is there there should be things
1.216 bowersj2 818: under it; for example, see the "Show Uncompleted Homework" view on the
819: B<NAV> screen.
1.190 bowersj2 820:
1.216 bowersj2 821: =item * B<suppressNavmaps>: default: false
1.174 albertel 822:
1.216 bowersj2 823: If true, will not display Navigate Content resources.
1.143 bowersj2 824:
1.133 bowersj2 825: =back
1.51 bowersj2 826:
1.133 bowersj2 827: =head2 Additional Info
1.51 bowersj2 828:
1.174 albertel 829: In addition to the parameters you can pass to the renderer, which will
830: be passed through unchange to the column renderers, the renderer will
831: generate the following information which your renderer may find
832: useful:
833:
1.216 bowersj2 834: =over 4
835:
836: =item * B<counter>:
1.145 bowersj2 837:
1.216 bowersj2 838: Contains the number of rows printed. Useful after calling the render
839: function, as you can detect whether anything was printed at all.
840:
841: =item * B<isNewBranch>:
842:
843: Useful for renderers: If this resource is currently the first resource
844: of a new branch, this will be true. The Resource column (leftmost in the
845: navmaps screen) uses this to display the "new branch" icon
1.59 bowersj2 846:
1.133 bowersj2 847: =back
1.59 bowersj2 848:
1.133 bowersj2 849: =cut
1.59 bowersj2 850:
1.133 bowersj2 851: sub resource { return 0; }
852: sub communication_status { return 1; }
853: sub quick_status { return 2; }
854: sub long_status { return 3; }
1.225 bowersj2 855: sub part_status_summary { return 4; }
1.51 bowersj2 856:
1.133 bowersj2 857: sub render_resource {
858: my ($resource, $part, $params) = @_;
1.51 bowersj2 859:
1.133 bowersj2 860: my $nonLinkedText = ''; # stuff after resource title not in link
1.51 bowersj2 861:
1.134 bowersj2 862: my $link = $params->{"resourceLink"};
863: my $src = $resource->src();
864: my $it = $params->{"iterator"};
1.133 bowersj2 865: my $filter = $it->{FILTER};
866:
867: my $title = $resource->compTitle();
1.258 albertel 868:
1.133 bowersj2 869: my $partLabel = "";
870: my $newBranchText = "";
871:
872: # If this is a new branch, label it so
873: if ($params->{'isNewBranch'}) {
874: $newBranchText = "<img src='/adm/lonIcons/branch.gif' border='0' />";
1.51 bowersj2 875: }
876:
1.133 bowersj2 877: # links to open and close the folder
878: my $linkopen = "<a href='$link'>";
879: my $linkclose = "</a>";
1.51 bowersj2 880:
1.204 albertel 881: # Default icon: unknown page
882: my $icon = "<img src='/adm/lonIcons/unknown.gif' alt='' border='0' />";
1.133 bowersj2 883:
884: if ($resource->is_problem()) {
1.192 bowersj2 885: if ($part eq '0' || $params->{'condensed'}) {
1.133 bowersj2 886: $icon = '<img src="/adm/lonIcons/problem.gif" alt="" border="0" />';
887: } else {
888: $icon = $params->{'indentString'};
889: }
1.204 albertel 890: } else {
1.243 albertel 891: $icon = "<img src='".&Apache::loncommon::icon($resource->src).
892: "' alt='' border='0' />";
1.133 bowersj2 893: }
1.51 bowersj2 894:
1.133 bowersj2 895: # Display the correct map icon to open or shut map
896: if ($resource->is_map()) {
897: my $mapId = $resource->map_pc();
898: my $nowOpen = !defined($filter->{$mapId});
899: if ($it->{CONDITION}) {
900: $nowOpen = !$nowOpen;
901: }
1.144 bowersj2 902:
1.205 bowersj2 903: my $folderType = $resource->is_sequence() ? 'folder' : 'page';
904:
1.144 bowersj2 905: if (!$params->{'resource_no_folder_link'}) {
1.205 bowersj2 906: $icon = "navmap.$folderType." . ($nowOpen ? 'closed' : 'open') . '.gif';
1.144 bowersj2 907: $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
908:
909: $linkopen = "<a href='" . $params->{'url'} . '?' .
910: $params->{'queryString'} . '&filter=';
911: $linkopen .= ($nowOpen xor $it->{CONDITION}) ?
912: addToFilter($filter, $mapId) :
913: removeFromFilter($filter, $mapId);
914: $linkopen .= "&condition=" . $it->{CONDITION} . '&hereType='
915: . $params->{'hereType'} . '&here=' .
916: &Apache::lonnet::escape($params->{'here'}) .
1.159 bowersj2 917: '&jump=' .
1.144 bowersj2 918: &Apache::lonnet::escape($resource->symb()) .
919: "&folderManip=1'>";
920: } else {
921: # Don't allow users to manipulate folder
1.205 bowersj2 922: $icon = "navmap.$folderType." . ($nowOpen ? 'closed' : 'open') .
1.144 bowersj2 923: '.nomanip.gif';
924: $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
925:
926: $linkopen = "";
927: $linkclose = "";
928: }
1.133 bowersj2 929: }
1.51 bowersj2 930:
1.133 bowersj2 931: if ($resource->randomout()) {
932: $nonLinkedText .= ' <i>(hidden)</i> ';
933: }
934:
935: # We're done preparing and finally ready to start the rendering
936: my $result = "<td align='left' valign='center'>";
1.140 bowersj2 937:
938: my $indentLevel = $params->{'indentLevel'};
939: if ($newBranchText) { $indentLevel--; }
940:
1.133 bowersj2 941: # print indentation
1.140 bowersj2 942: for (my $i = 0; $i < $indentLevel; $i++) {
1.133 bowersj2 943: $result .= $params->{'indentString'};
1.84 bowersj2 944: }
945:
1.133 bowersj2 946: # Decide what to display
947: $result .= "$newBranchText$linkopen$icon$linkclose";
948:
949: my $curMarkerBegin = '';
950: my $curMarkerEnd = '';
1.51 bowersj2 951:
1.133 bowersj2 952: # Is this the current resource?
1.158 bowersj2 953: if (!$params->{'displayedHereMarker'} &&
954: $resource->symb() eq $params->{'here'} ) {
1.133 bowersj2 955: $curMarkerBegin = '<font color="red" size="+2">> </font>';
956: $curMarkerEnd = '<font color="red" size="+2"><</font>';
1.158 bowersj2 957: $params->{'displayedHereMarker'} = 1;
1.51 bowersj2 958: }
959:
1.192 bowersj2 960: if ($resource->is_problem() && $part ne '0' &&
1.133 bowersj2 961: !$params->{'condensed'}) {
1.245 www 962: my $displaypart=&Apache::lonnet::EXT('resource.'.$part.'.display',
963: $resource->symb());
964: unless ($displaypart) { $displaypart=$part; }
965: $partLabel = " (Part: $displaypart)";
966: $link.='#'.&Apache::lonnet::escape($part);
1.133 bowersj2 967: $title = "";
1.51 bowersj2 968: }
969:
1.163 bowersj2 970: if ($params->{'condensed'} && $resource->countParts() > 1) {
1.133 bowersj2 971: $nonLinkedText .= ' (' . $resource->countParts() . ' parts)';
1.51 bowersj2 972: }
973:
1.266 raeburn 974: if (!$params->{'resource_nolink'} && !$resource->is_sequence() && !$resource->is_empty_sequence) {
1.144 bowersj2 975: $result .= " $curMarkerBegin<a href='$link'>$title$partLabel</a>$curMarkerEnd $nonLinkedText</td>";
976: } else {
977: $result .= " $curMarkerBegin$title$partLabel$curMarkerEnd $nonLinkedText</td>";
978: }
1.51 bowersj2 979:
1.133 bowersj2 980: return $result;
981: }
1.51 bowersj2 982:
1.133 bowersj2 983: sub render_communication_status {
984: my ($resource, $part, $params) = @_;
1.134 bowersj2 985: my $discussionHTML = ""; my $feedbackHTML = ""; my $errorHTML = "";
986:
987: my $link = $params->{"resourceLink"};
988: my $linkopen = "<a href='$link'>";
989: my $linkclose = "</a>";
990:
991: if ($resource->hasDiscussion()) {
992: $discussionHTML = $linkopen .
993: '<img border="0" src="/adm/lonMisc/chat.gif" />' .
994: $linkclose;
995: }
996:
997: if ($resource->getFeedback()) {
998: my $feedback = $resource->getFeedback();
999: foreach (split(/\,/, $feedback)) {
1000: if ($_) {
1001: $feedbackHTML .= ' <a href="/adm/email?display='
1002: . &Apache::lonnet::escape($_) . '">'
1003: . '<img src="/adm/lonMisc/feedback.gif" '
1004: . 'border="0" /></a>';
1005: }
1006: }
1007: }
1008:
1009: if ($resource->getErrors()) {
1010: my $errors = $resource->getErrors();
1.254 matthew 1011: my $errorcount = 0;
1.134 bowersj2 1012: foreach (split(/,/, $errors)) {
1.254 matthew 1013: last if ($errorcount>=10); # Only output 10 bombs maximum
1.134 bowersj2 1014: if ($_) {
1.254 matthew 1015: $errorcount++;
1.134 bowersj2 1016: $errorHTML .= ' <a href="/adm/email?display='
1017: . &Apache::lonnet::escape($_) . '">'
1018: . '<img src="/adm/lonMisc/bomb.gif" '
1019: . 'border="0" /></a>';
1020: }
1021: }
1.197 bowersj2 1022: }
1023:
1024: if ($params->{'multipart'} && $part != '0') {
1025: $discussionHTML = $feedbackHTML = $errorHTML = '';
1.134 bowersj2 1026: }
1027:
1028: return "<td width=\"75\" align=\"left\" valign=\"center\">$discussionHTML$feedbackHTML$errorHTML </td>";
1029:
1.133 bowersj2 1030: }
1031: sub render_quick_status {
1032: my ($resource, $part, $params) = @_;
1.135 bowersj2 1033: my $result = "";
1034: my $firstDisplayed = !$params->{'condensed'} &&
1035: $params->{'multipart'} && $part eq "0";
1036:
1037: my $link = $params->{"resourceLink"};
1038: my $linkopen = "<a href='$link'>";
1039: my $linkclose = "</a>";
1040:
1041: if ($resource->is_problem() &&
1042: !$firstDisplayed) {
1.224 bowersj2 1043:
1044: my $icon = $statusIconMap{$resource->simpleStatus($part)};
1.135 bowersj2 1045: my $alt = $iconAltTags{$icon};
1046: if ($icon) {
1047: $result .= "<td width='30' valign='center' width='50' align='right'>$linkopen<img width='25' height='25' src='/adm/lonIcons/$icon' border='0' alt='$alt' />$linkclose</td>\n";
1048: } else {
1049: $result .= "<td width='30'> </td>\n";
1050: }
1051: } else { # not problem, no icon
1052: $result .= "<td width='30'> </td>\n";
1053: }
1054:
1055: return $result;
1.133 bowersj2 1056: }
1057: sub render_long_status {
1058: my ($resource, $part, $params) = @_;
1.136 bowersj2 1059: my $result = "<td align='right' valign='center'>\n";
1060: my $firstDisplayed = !$params->{'condensed'} &&
1061: $params->{'multipart'} && $part eq "0";
1062:
1063: my $color;
1.212 bowersj2 1064: if ($resource->is_problem()) {
1.136 bowersj2 1065: $color = $colormap{$resource->status};
1066:
1.252 matthew 1067: if (dueInLessThan24Hours($resource, $part) ||
1.136 bowersj2 1068: lastTry($resource, $part)) {
1069: $color = $hurryUpColor;
1070: }
1071: }
1072:
1073: if ($resource->kind() eq "res" &&
1074: $resource->is_problem() &&
1075: !$firstDisplayed) {
1076: if ($color) {$result .= "<font color=\"$color\"><b>"; }
1077: $result .= getDescription($resource, $part);
1078: if ($color) {$result .= "</b></font>"; }
1079: }
1080: if ($resource->is_map() && advancedUser() && $resource->randompick()) {
1081: $result .= '(randomly select ' . $resource->randompick() .')';
1082: }
1.210 bowersj2 1083:
1.213 bowersj2 1084: # Debugging code
1085: #$result .= " " . $resource->awarded($part) . '/' . $resource->weight($part) .
1086: # ' - Part: ' . $part;
1087:
1.210 bowersj2 1088: $result .= "</td>\n";
1.136 bowersj2 1089:
1090: return $result;
1.51 bowersj2 1091: }
1092:
1.227 bowersj2 1093: # Colors obtained by taking the icons, matching the colors, and
1094: # possibly reducing the Value (HSV) of the color, if it's too bright
1095: # for text, generally by one third or so.
1.225 bowersj2 1096: my %statusColors =
1097: (
1098: $resObj->CLOSED => '#000000',
1.227 bowersj2 1099: $resObj->OPEN => '#998b13',
1100: $resObj->CORRECT => '#26933f',
1101: $resObj->INCORRECT => '#c48207',
1102: $resObj->ATTEMPTED => '#a87510',
1.225 bowersj2 1103: $resObj->ERROR => '#000000'
1104: );
1105: my %statusStrings =
1106: (
1107: $resObj->CLOSED => 'Not yet open',
1108: $resObj->OPEN => 'Open',
1109: $resObj->CORRECT => 'Correct',
1110: $resObj->INCORRECT => 'Incorrect',
1111: $resObj->ATTEMPTED => 'Attempted',
1112: $resObj->ERROR => 'Network Error'
1113: );
1114: my @statuses = ($resObj->CORRECT, $resObj->ATTEMPTED, $resObj->INCORRECT, $resObj->OPEN, $resObj->CLOSED, $resObj->ERROR);
1115:
1116: use Data::Dumper;
1117: sub render_parts_summary_status {
1118: my ($resource, $part, $params) = @_;
1.256 albertel 1119: if (!$resource->is_problem() && !$resource->contains_problem) { return '<td></td>'; }
1.225 bowersj2 1120: if ($params->{showParts}) {
1121: return '<td></td>';
1122: }
1123:
1124: my $td = "<td align='right'>\n";
1125: my $endtd = "</td>\n";
1.256 albertel 1126: my @probs;
1.225 bowersj2 1127:
1.256 albertel 1128: if ($resource->contains_problem) {
1129: @probs=$resource->retrieveResources($resource,sub { $_[0]->is_problem() },1,0);
1130: } else {
1131: @probs=($resource);
1132: }
1133: my $return;
1134: my %overallstatus;
1135: my $totalParts;
1136: foreach my $resource (@probs) {
1137: # If there is a single part, just show the simple status
1138: if ($resource->singlepart()) {
1139: my $status = $resource->simpleStatus(${$resource->parts}[0]);
1140: $overallstatus{$status}++;
1141: $totalParts++;
1142: next;
1143: }
1144: # Now we can be sure the $part doesn't really matter.
1145: my $statusCount = $resource->simpleStatusCount();
1146: my @counts;
1147: foreach my $status (@statuses) {
1148: # decouple display order from the simpleStatusCount order
1149: my $slot = Apache::lonnavmaps::resource::statusToSlot($status);
1150: if ($statusCount->[$slot]) {
1151: $overallstatus{$status}+=$statusCount->[$slot];
1152: $totalParts+=$statusCount->[$slot];
1153: }
1154: }
1155: }
1156: $return.= $td . $totalParts . ' parts: ';
1157: foreach my $status (@statuses) {
1158: if ($overallstatus{$status}) {
1159: $return.="<font color='" . $statusColors{$status} .
1160: "'>" . $overallstatus{$status} . ' '
1.225 bowersj2 1161: . $statusStrings{$status} . "</font>";
1162: }
1163: }
1.256 albertel 1164: $return.= $endtd;
1165: return $return;
1.225 bowersj2 1166: }
1167:
1.133 bowersj2 1168: my @preparedColumns = (\&render_resource, \&render_communication_status,
1.225 bowersj2 1169: \&render_quick_status, \&render_long_status,
1170: \&render_parts_summary_status);
1.132 bowersj2 1171:
1172: sub setDefault {
1173: my ($val, $default) = @_;
1174: if (!defined($val)) { return $default; }
1175: return $val;
1176: }
1177:
1178: sub render {
1179: my $args = shift;
1.140 bowersj2 1180: &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
1181: my $result = '';
1182:
1.132 bowersj2 1183: # Configure the renderer.
1184: my $cols = $args->{'cols'};
1185: if (!defined($cols)) {
1186: # no columns, no nav maps.
1187: return '';
1188: }
1.140 bowersj2 1189: my $mustCloseNavMap = 0;
1190: my $navmap;
1191: if (defined($args->{'navmap'})) {
1192: $navmap = $args->{'navmap'};
1193: }
1194:
1.158 bowersj2 1195: my $r = $args->{'r'};
1.140 bowersj2 1196: my $queryString = $args->{'queryString'};
1.159 bowersj2 1197: my $jump = $args->{'jump'};
1.158 bowersj2 1198: my $here = $args->{'here'};
1.153 bowersj2 1199: my $suppressNavmap = setDefault($args->{'suppressNavmap'}, 0);
1.256 albertel 1200: my $closeAllPages = setDefault($args->{'closeAllPages'}, 0);
1.140 bowersj2 1201: my $currentJumpDelta = 2; # change this to change how many resources are displayed
1202: # before the current resource when using #current
1203:
1204: # If we were passed 'here' information, we are not rendering
1205: # after a folder manipulation, and we were not passed an
1206: # iterator, make sure we open the folders to show the "here"
1207: # marker
1208: my $filterHash = {};
1209: # Figure out what we're not displaying
1210: foreach (split(/\,/, $ENV{"form.filter"})) {
1211: if ($_) {
1212: $filterHash->{$_} = "1";
1213: }
1214: }
1215:
1.191 bowersj2 1216: # Filter: Remember filter function and add our own filter: Refuse
1217: # to show hidden resources unless the user can see them.
1218: my $userCanSeeHidden = advancedUser();
1219: my $filterFunc = setDefault($args->{'filterFunc'},
1220: sub {return 1;});
1221: if (!$userCanSeeHidden) {
1222: # Without renaming the filterfunc, the server seems to go into
1223: # an infinite loop
1224: my $oldFilterFunc = $filterFunc;
1225: $filterFunc = sub { my $res = shift; return !$res->randomout() &&
1226: &$oldFilterFunc($res);};
1227: }
1228:
1.140 bowersj2 1229: my $condition = 0;
1230: if ($ENV{'form.condition'}) {
1231: $condition = 1;
1232: }
1233:
1234: if (!$ENV{'form.folderManip'} && !defined($args->{'iterator'})) {
1235: # Step 1: Check to see if we have a navmap
1236: if (!defined($navmap)) {
1.221 bowersj2 1237: $navmap = Apache::lonnavmaps::navmap->new();
1.140 bowersj2 1238: $mustCloseNavMap = 1;
1239: }
1240:
1241: # Step two: Locate what kind of here marker is necessary
1242: # Determine where the "here" marker is and where the screen jumps to.
1243:
1.175 bowersj2 1244: if ($ENV{'form.postsymb'}) {
1245: $here = $jump = $ENV{'form.postsymb'};
1.140 bowersj2 1246: } elsif ($ENV{'form.postdata'}) {
1247: # couldn't find a symb, is there a URL?
1248: my $currenturl = $ENV{'form.postdata'};
1.158 bowersj2 1249: #$currenturl=~s/^http\:\/\///;
1250: #$currenturl=~s/^[^\/]+//;
1.140 bowersj2 1251:
1.158 bowersj2 1252: $here = $jump = &Apache::lonnet::symbread($currenturl);
1.140 bowersj2 1253: }
1.158 bowersj2 1254:
1.140 bowersj2 1255: # Step three: Ensure the folders are open
1256: my $mapIterator = $navmap->getIterator(undef, undef, undef, 1);
1.222 bowersj2 1257: my $curRes;
1.140 bowersj2 1258: my $found = 0;
1259:
1260: # We only need to do this if we need to open the maps to show the
1261: # current position. This will change the counter so we can't count
1262: # for the jump marker with this loop.
1.267.2.5 albertel 1263: while ($here && ($curRes = $mapIterator->next()) && !$found) {
1.158 bowersj2 1264: if (ref($curRes) && $curRes->symb() eq $here) {
1.140 bowersj2 1265: my $mapStack = $mapIterator->getStack();
1266:
1267: # Ensure the parent maps are open
1268: for my $map (@{$mapStack}) {
1269: if ($condition) {
1270: undef $filterHash->{$map->map_pc()};
1271: } else {
1272: $filterHash->{$map->map_pc()} = 1;
1273: }
1274: }
1275: $found = 1;
1276: }
1277: }
1.159 bowersj2 1278: }
1.140 bowersj2 1279:
1280: if ( !defined($args->{'iterator'}) && $ENV{'form.folderManip'} ) { # we came from a user's manipulation of the nav page
1281: # If this is a click on a folder or something, we want to preserve the "here"
1282: # from the querystring, and get the new "jump" marker
1283: $here = $ENV{'form.here'};
1284: $jump = $ENV{'form.jump'};
1285: }
1286:
1.132 bowersj2 1287: my $it = $args->{'iterator'};
1288: if (!defined($it)) {
1.140 bowersj2 1289: # Construct a default iterator based on $ENV{'form.'} information
1290:
1291: # Step 1: Check to see if we have a navmap
1292: if (!defined($navmap)) {
1.221 bowersj2 1293: $navmap = Apache::lonnavmaps::navmap->new();
1.140 bowersj2 1294: $mustCloseNavMap = 1;
1295: }
1296:
1.144 bowersj2 1297: # See if we're being passed a specific map
1298: if ($args->{'iterator_map'}) {
1299: my $map = $args->{'iterator_map'};
1.145 bowersj2 1300: $map = $navmap->getResourceByUrl($map);
1.144 bowersj2 1301: my $firstResource = $map->map_start();
1302: my $finishResource = $map->map_finish();
1303:
1304: $args->{'iterator'} = $it = $navmap->getIterator($firstResource, $finishResource, $filterHash, $condition);
1305: } else {
1306: $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition);
1307: }
1.132 bowersj2 1308: }
1.256 albertel 1309:
1.159 bowersj2 1310: # (re-)Locate the jump point, if any
1.191 bowersj2 1311: # Note this does not take filtering or hidden into account... need
1312: # to be fixed?
1.159 bowersj2 1313: my $mapIterator = $navmap->getIterator(undef, undef, $filterHash, 0);
1.222 bowersj2 1314: my $curRes;
1.159 bowersj2 1315: my $foundJump = 0;
1316: my $counter = 0;
1317:
1.222 bowersj2 1318: while (($curRes = $mapIterator->next()) && !$foundJump) {
1.159 bowersj2 1319: if (ref($curRes)) { $counter++; }
1320:
1321: if (ref($curRes) && $jump eq $curRes->symb()) {
1322:
1323: # This is why we have to use the main iterator instead of the
1324: # potentially faster DFS: The count has to be the same, so
1325: # the order has to be the same, which DFS won't give us.
1326: $args->{'currentJumpIndex'} = $counter;
1327: $foundJump = 1;
1328: }
1329: }
1330:
1.132 bowersj2 1331: my $showParts = setDefault($args->{'showParts'}, 1);
1332: my $condenseParts = setDefault($args->{'condenseParts'}, 1);
1.139 bowersj2 1333: # keeps track of when the current resource is found,
1334: # so we can back up a few and put the anchor above the
1335: # current resource
1.140 bowersj2 1336: my $printKey = $args->{'printKey'};
1337: my $printCloseAll = $args->{'printCloseAll'};
1338: if (!defined($printCloseAll)) { $printCloseAll = 1; }
1.144 bowersj2 1339:
1.140 bowersj2 1340: # Print key?
1341: if ($printKey) {
1342: $result .= '<table border="0" cellpadding="2" cellspacing="0">';
1343: my $date=localtime;
1344: $result.='<tr><td align="right" valign="bottom">Key: </td>';
1345: if ($navmap->{LAST_CHECK}) {
1346: $result .=
1.230 www 1347: '<img src="/adm/lonMisc/chat.gif"> '.&mt('New discussion since').' '.
1.140 bowersj2 1348: strftime("%A, %b %e at %I:%M %P", localtime($navmap->{LAST_CHECK})).
1349: '</td><td align="center" valign="bottom"> '.
1.230 www 1350: '<img src="/adm/lonMisc/feedback.gif"> '.&mt('New message (click to open)').'<p>'.
1.140 bowersj2 1351: '</td>';
1352: } else {
1353: $result .= '<td align="center" valign="bottom"> '.
1.230 www 1354: '<img src="/adm/lonMisc/chat.gif"> '.&mt('Discussions').'</td><td align="center" valign="bottom">'.
1355: ' <img src="/adm/lonMisc/feedback.gif"> '.&mt('New message (click to open)').
1.140 bowersj2 1356: '</td>';
1357: }
1358:
1359: $result .= '</tr></table>';
1360: }
1361:
1.172 bowersj2 1362: if ($printCloseAll && !$args->{'resource_no_folder_link'}) {
1.140 bowersj2 1363: if ($condition) {
1364: $result.="<a href=\"navmaps?condition=0&filter=&$queryString" .
1.158 bowersj2 1365: "&here=" . Apache::lonnet::escape($here) .
1.230 www 1366: "\">".&mt('Close All Folders')."</a>";
1.140 bowersj2 1367: } else {
1368: $result.="<a href=\"navmaps?condition=1&filter=&$queryString" .
1.158 bowersj2 1369: "&here=" . Apache::lonnet::escape($here) .
1.230 www 1370: "\">".&mt('Open All Folders')."</a>";
1.140 bowersj2 1371: }
1.266 raeburn 1372: $result .= "\n";
1373: }
1.140 bowersj2 1374:
1.266 raeburn 1375: # Check for any unread discussions in all resources.
1.267.2.8! albertel 1376: if (!$args->{'resource_no_folder_link'}) {
! 1377: my $time=time;
! 1378: my $mapr = &mt('Mark all posts read');
! 1379: $result .= (<<END);
! 1380: <a href="javascript:document.clearbubbles.submit()">$mapr</a> <a href="javascript:void(open('/adm/help/NavMaps_MarkPosts_Read.hlp', 'Help_for_NavMaps_MarkPosts', 'menubar=0,toolbar=1,scrollbars=1,width=350,height=400,resizable=yes'))" title="Online Help"><image src="/adm/help/gif/smallHelp.gif" border="0" alt="(Help: NavMaps_MarkPostsLink)" /></a>
1.267 albertel 1381: <form name="clearbubbles" method="post" action="/adm/feedback">
1382: <input type="hidden" name="navurl" value="$ENV{'QUERY_STRING'}" />
1.267.2.8! albertel 1383: <input type="hidden" name="navtime" value="$time" />
1.267 albertel 1384: </form>
1.266 raeburn 1385: END
1386: }
1387: $result .= "<br />\n";
1.140 bowersj2 1388: if ($r) {
1389: $r->print($result);
1390: $r->rflush();
1391: $result = "";
1392: }
1.132 bowersj2 1393: # End parameter setting
1.133 bowersj2 1394:
1.132 bowersj2 1395: # Data
1.140 bowersj2 1396: $result .= '<table cellspacing="0" cellpadding="3" border="0" bgcolor="#FFFFFF">' ."\n";
1.132 bowersj2 1397: my $res = "Apache::lonnavmaps::resource";
1398: my %condenseStatuses =
1399: ( $res->NETWORK_FAILURE => 1,
1400: $res->NOTHING_SET => 1,
1401: $res->CORRECT => 1 );
1402: my @backgroundColors = ("#FFFFFF", "#F6F6F6");
1.133 bowersj2 1403:
1404: # Shared variables
1405: $args->{'counter'} = 0; # counts the rows
1406: $args->{'indentLevel'} = 0;
1407: $args->{'isNewBranch'} = 0;
1408: $args->{'condensed'} = 0;
1409: $args->{'indentString'} = setDefault($args->{'indentString'}, "<img src='/adm/lonIcons/whitespace1.gif' width='25' height='1' alt='' border='0' />");
1410: $args->{'displayedHereMarker'} = 0;
1.132 bowersj2 1411:
1.190 bowersj2 1412: # If we're suppressing empty sequences, look for them here. Use DFS for speed,
1413: # since structure actually doesn't matter, except what map has what resources.
1414: if ($args->{'suppressEmptySequences'}) {
1415: my $dfsit = Apache::lonnavmaps::DFSiterator->new($navmap,
1416: $it->{FIRST_RESOURCE},
1417: $it->{FINISH_RESOURCE},
1418: {}, undef, 1);
1.222 bowersj2 1419: my $depth = 0;
1.190 bowersj2 1420: $dfsit->next();
1421: my $curRes = $dfsit->next();
1422: while ($depth > -1) {
1423: if ($curRes == $dfsit->BEGIN_MAP()) { $depth++; }
1424: if ($curRes == $dfsit->END_MAP()) { $depth--; }
1425:
1426: if (ref($curRes)) {
1427: # Parallel pre-processing: Do sequences have non-filtered-out children?
1.201 bowersj2 1428: if ($curRes->is_map()) {
1.190 bowersj2 1429: $curRes->{DATA}->{HAS_VISIBLE_CHILDREN} = 0;
1430: # Sequences themselves do not count as visible children,
1431: # unless those sequences also have visible children.
1432: # This means if a sequence appears, there's a "promise"
1433: # that there's something under it if you open it, somewhere.
1434: } else {
1435: # Not a sequence: if it's filtered, ignore it, otherwise
1436: # rise up the stack and mark the sequences as having children
1437: if (&$filterFunc($curRes)) {
1438: for my $sequence (@{$dfsit->getStack()}) {
1439: $sequence->{DATA}->{HAS_VISIBLE_CHILDREN} = 1;
1440: }
1441: }
1442: }
1443: }
1444: } continue {
1445: $curRes = $dfsit->next();
1446: }
1447: }
1448:
1.133 bowersj2 1449: my $displayedJumpMarker = 0;
1.132 bowersj2 1450: # Set up iteration.
1451: my $now = time();
1452: my $in24Hours = $now + 24 * 60 * 60;
1453: my $rownum = 0;
1454:
1.141 bowersj2 1455: # export "here" marker information
1456: $args->{'here'} = $here;
1457:
1.222 bowersj2 1458: $args->{'indentLevel'} = -1; # first BEGIN_MAP takes this to 0
1.256 albertel 1459: while ($curRes = $it->next($closeAllPages)) {
1.132 bowersj2 1460: # Maintain indentation level.
1461: if ($curRes == $it->BEGIN_MAP() ||
1462: $curRes == $it->BEGIN_BRANCH() ) {
1.133 bowersj2 1463: $args->{'indentLevel'}++;
1.132 bowersj2 1464: }
1465: if ($curRes == $it->END_MAP() ||
1466: $curRes == $it->END_BRANCH() ) {
1.133 bowersj2 1467: $args->{'indentLevel'}--;
1.132 bowersj2 1468: }
1469: # Notice new branches
1470: if ($curRes == $it->BEGIN_BRANCH()) {
1.133 bowersj2 1471: $args->{'isNewBranch'} = 1;
1472: }
1473:
1474: # If this isn't an actual resource, continue on
1475: if (!ref($curRes)) {
1476: next;
1.132 bowersj2 1477: }
1.133 bowersj2 1478:
1.143 bowersj2 1479: # If this has been filtered out, continue on
1480: if (!(&$filterFunc($curRes))) {
1481: $args->{'isNewBranch'} = 0; # Don't falsely remember this
1482: next;
1483: }
1.132 bowersj2 1484:
1.190 bowersj2 1485: # If this is an empty sequence and we're filtering them, continue on
1.201 bowersj2 1486: if ($curRes->is_map() && $args->{'suppressEmptySequences'} &&
1.190 bowersj2 1487: !$curRes->{DATA}->{HAS_VISIBLE_CHILDREN}) {
1488: next;
1489: }
1490:
1.153 bowersj2 1491: # If we're suppressing navmaps and this is a navmap, continue on
1492: if ($suppressNavmap && $curRes->src() =~ /^\/adm\/navmaps/) {
1493: next;
1494: }
1495:
1.191 bowersj2 1496: $args->{'counter'}++;
1497:
1.132 bowersj2 1498: # Does it have multiple parts?
1.133 bowersj2 1499: $args->{'multipart'} = 0;
1500: $args->{'condensed'} = 0;
1.132 bowersj2 1501: my @parts;
1502:
1503: # Decide what parts to show.
1.133 bowersj2 1504: if ($curRes->is_problem() && $showParts) {
1.132 bowersj2 1505: @parts = @{$curRes->parts()};
1.192 bowersj2 1506: $args->{'multipart'} = $curRes->multipart();
1.132 bowersj2 1507:
1508: if ($condenseParts) { # do the condensation
1509: if (!$curRes->opendate("0")) {
1.162 bowersj2 1510: @parts = ();
1.133 bowersj2 1511: $args->{'condensed'} = 1;
1.132 bowersj2 1512: }
1.133 bowersj2 1513: if (!$args->{'condensed'}) {
1.132 bowersj2 1514: # Decide whether to condense based on similarity
1.192 bowersj2 1515: my $status = $curRes->status($parts[0]);
1516: my $due = $curRes->duedate($parts[0]);
1517: my $open = $curRes->opendate($parts[0]);
1.132 bowersj2 1518: my $statusAllSame = 1;
1519: my $dueAllSame = 1;
1520: my $openAllSame = 1;
1.192 bowersj2 1521: for (my $i = 1; $i < scalar(@parts); $i++) {
1.132 bowersj2 1522: if ($curRes->status($parts[$i]) != $status){
1523: $statusAllSame = 0;
1524: }
1525: if ($curRes->duedate($parts[$i]) != $due ) {
1526: $dueAllSame = 0;
1527: }
1528: if ($curRes->opendate($parts[$i]) != $open) {
1529: $openAllSame = 0;
1530: }
1531: }
1532: # $*allSame is true if all the statuses were
1533: # the same. Now, if they are all the same and
1534: # match one of the statuses to condense, or they
1535: # are all open with the same due date, or they are
1536: # all OPEN_LATER with the same open date, display the
1537: # status of the first non-zero part (to get the 'correct'
1538: # status right, since 0 is never 'correct' or 'open').
1539: if (($statusAllSame && defined($condenseStatuses{$status})) ||
1540: ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
1541: ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
1.192 bowersj2 1542: @parts = ($parts[0]);
1.133 bowersj2 1543: $args->{'condensed'} = 1;
1.132 bowersj2 1544: }
1545: }
1.198 bowersj2 1546: # Multipart problem with one part: always "condense" (happens
1547: # to match the desirable behavior)
1548: if ($curRes->countParts() == 1) {
1549: @parts = ($parts[0]);
1550: $args->{'condensed'} = 1;
1551: }
1.132 bowersj2 1552: }
1.157 bowersj2 1553: }
1.132 bowersj2 1554:
1555: # If the multipart problem was condensed, "forget" it was multipart
1556: if (scalar(@parts) == 1) {
1.133 bowersj2 1557: $args->{'multipart'} = 0;
1.192 bowersj2 1558: } else {
1559: # Add part 0 so we display it correctly.
1560: unshift @parts, '0';
1.132 bowersj2 1561: }
1562:
1563: # Now, we've decided what parts to show. Loop through them and
1564: # show them.
1.192 bowersj2 1565: foreach my $part (@parts) {
1.132 bowersj2 1566: $rownum ++;
1567: my $backgroundColor = $backgroundColors[$rownum % scalar(@backgroundColors)];
1568:
1569: $result .= " <tr bgcolor='$backgroundColor'>\n";
1.134 bowersj2 1570:
1571: # Set up some data about the parts that the cols might want
1572: my $filter = $it->{FILTER};
1573: my $stack = $it->getStack();
1574: my $src = getLinkForResource($stack);
1.249 www 1575: my $anchor='';
1576: if ($src=~s/(\#.*$)//) {
1577: $anchor=$1;
1578: }
1.134 bowersj2 1579: my $srcHasQuestion = $src =~ /\?/;
1580: $args->{"resourceLink"} = $src.
1581: ($srcHasQuestion?'&':'?') .
1.249 www 1582: 'symb=' . &Apache::lonnet::escape($curRes->symb()).
1583: $anchor;
1.132 bowersj2 1584:
1585: # Now, display each column.
1586: foreach my $col (@$cols) {
1.139 bowersj2 1587: my $colHTML = '';
1588: if (ref($col)) {
1589: $colHTML .= &$col($curRes, $part, $args);
1590: } else {
1591: $colHTML .= &{$preparedColumns[$col]}($curRes, $part, $args);
1592: }
1.132 bowersj2 1593:
1.133 bowersj2 1594: # If this is the first column and it's time to print
1595: # the anchor, do so
1596: if ($col == $cols->[0] &&
1597: $args->{'counter'} == $args->{'currentJumpIndex'} -
1.139 bowersj2 1598: $currentJumpDelta) {
1599: # Jam the anchor after the <td> tag;
1600: # necessary for valid HTML (which Mozilla requires)
1601: $colHTML =~ s/\>/\>\<a name="curloc" \/\>/;
1.133 bowersj2 1602: $displayedJumpMarker = 1;
1603: }
1.139 bowersj2 1604: $result .= $colHTML . "\n";
1.132 bowersj2 1605: }
1.139 bowersj2 1606: $result .= " </tr>\n";
1.140 bowersj2 1607: $args->{'isNewBranch'} = 0;
1.139 bowersj2 1608: }
1.153 bowersj2 1609:
1.139 bowersj2 1610: if ($r && $rownum % 20 == 0) {
1611: $r->print($result);
1612: $result = "";
1613: $r->rflush();
1.132 bowersj2 1614: }
1.156 bowersj2 1615: } continue {
1.208 bowersj2 1616: if ($r) {
1617: # If we have the connection, make sure the user is still connected
1618: my $c = $r->connection;
1619: if ($c->aborted()) {
1620: # Who cares what we do, nobody will see it anyhow.
1621: return '';
1622: }
1623: }
1.132 bowersj2 1624: }
1625:
1.134 bowersj2 1626: # Print out the part that jumps to #curloc if it exists
1.159 bowersj2 1627: # delay needed because the browser is processing the jump before
1628: # it finishes rendering, so it goes to the wrong place!
1629: # onload might be better, but this routine has no access to that.
1630: # On mozilla, the 0-millisecond timeout seems to prevent this;
1631: # it's quite likely this might fix other browsers, too, and
1632: # certainly won't hurt anything.
1.139 bowersj2 1633: if ($displayedJumpMarker) {
1.247 albertel 1634: $result .= "
1635: <script>
1636: if (location.href.indexOf('#curloc')==-1) {
1637: setTimeout(\"location += '#curloc';\", 0)
1638: }
1639: </script>";
1.134 bowersj2 1640: }
1641:
1.132 bowersj2 1642: $result .= "</table>";
1.140 bowersj2 1643:
1644: if ($r) {
1645: $r->print($result);
1646: $result = "";
1647: $r->rflush();
1648: }
1649:
1.175 bowersj2 1650: if ($mustCloseNavMap) { $navmap->untieHashes(); }
1.132 bowersj2 1651:
1652: return $result;
1653: }
1654:
1.133 bowersj2 1655: 1;
1656:
1657: package Apache::lonnavmaps::navmap;
1658:
1659: =pod
1660:
1.216 bowersj2 1661: =head1 Object: Apache::lonnavmaps::navmap
1.133 bowersj2 1662:
1.217 bowersj2 1663: =head2 Overview
1.133 bowersj2 1664:
1.217 bowersj2 1665: The navmap object's job is to provide access to the resources
1666: in the course as Apache::lonnavmaps::resource objects, and to
1667: query and manage the relationship between those resource objects.
1668:
1669: Generally, you'll use the navmap object in one of three basic ways.
1670: In order of increasing complexity and power:
1671:
1672: =over 4
1673:
1674: =item * C<$navmap-E<gt>getByX>, where X is B<Id>, B<Symb>, B<Url> or B<MapPc>. This provides
1675: various ways to obtain resource objects, based on various identifiers.
1676: Use this when you want to request information about one object or
1677: a handful of resources you already know the identities of, from some
1678: other source. For more about Ids, Symbs, and MapPcs, see the
1679: Resource documentation. Note that Url should be a B<last resort>,
1680: not your first choice; it only works when there is only one
1681: instance of the resource in the course, which only applies to
1682: maps, and even that may change in the future.
1683:
1684: =item * C<my @resources = $navmap-E<gt>retrieveResources(args)>. This
1685: retrieves resources matching some criterion and returns them
1686: in a flat array, with no structure information. Use this when
1687: you are manipulating a series of resources, based on what map
1688: the are in, but do not care about branching, or exactly how
1689: the maps and resources are related. This is the most common case.
1690:
1691: =item * C<$it = $navmap-E<gt>getIterator(args)>. This allows you traverse
1692: the course's navmap in various ways without writing the traversal
1693: code yourself. See iterator documentation below. Use this when
1694: you need to know absolutely everything about the course, including
1695: branches and the precise relationship between maps and resources.
1696:
1697: =back
1698:
1699: =head2 Creation And Destruction
1700:
1701: To create a navmap object, use the following function:
1.133 bowersj2 1702:
1703: =over 4
1704:
1.221 bowersj2 1705: =item * B<Apache::lonnavmaps::navmap-E<gt>new>():
1.133 bowersj2 1706:
1.221 bowersj2 1707: Creates a new navmap object. Returns the navmap object if this is
1708: successful, or B<undef> if not.
1.174 albertel 1709:
1.216 bowersj2 1710: =back
1711:
1.217 bowersj2 1712: When you are done with the $navmap object, you I<must> call
1713: $navmap->untieHashes(), or you'll prevent the current user from using that
1714: course until the web server is restarted. (!)
1715:
1.216 bowersj2 1716: =head2 Methods
1717:
1718: =over 4
1719:
1.174 albertel 1720: =item * B<getIterator>(first, finish, filter, condition):
1721:
1722: See iterator documentation below.
1.133 bowersj2 1723:
1724: =cut
1725:
1726: use strict;
1727: use GDBM_File;
1728:
1729: sub new {
1730: # magic invocation to create a class instance
1731: my $proto = shift;
1732: my $class = ref($proto) || $proto;
1733: my $self = {};
1734:
1735: # Resource cache stores navmap resources as we reference them. We generate
1736: # them on-demand so we don't pay for creating resources unless we use them.
1737: $self->{RESOURCE_CACHE} = {};
1738:
1739: # Network failure flag, if we accessed the course or user opt and
1740: # failed
1741: $self->{NETWORK_FAILURE} = 0;
1742:
1743: # tie the nav hash
1744:
1.168 bowersj2 1745: my %navmaphash;
1746: my %parmhash;
1.220 bowersj2 1747: my $courseFn = $ENV{"request.course.fn"};
1748: if (!(tie(%navmaphash, 'GDBM_File', "${courseFn}.db",
1.133 bowersj2 1749: &GDBM_READER(), 0640))) {
1750: return undef;
1751: }
1752:
1.220 bowersj2 1753: if (!(tie(%parmhash, 'GDBM_File', "${courseFn}_parms.db",
1.133 bowersj2 1754: &GDBM_READER(), 0640)))
1755: {
1.170 matthew 1756: untie %{$self->{PARM_HASH}};
1.133 bowersj2 1757: return undef;
1758: }
1759:
1.199 bowersj2 1760: $self->{NAV_HASH} = \%navmaphash;
1.168 bowersj2 1761: $self->{PARM_HASH} = \%parmhash;
1.221 bowersj2 1762: $self->{PARM_CACHE} = {};
1.133 bowersj2 1763:
1764: bless($self);
1765:
1766: return $self;
1767: }
1768:
1.221 bowersj2 1769: sub generate_course_user_opt {
1.133 bowersj2 1770: my $self = shift;
1.221 bowersj2 1771: if ($self->{COURSE_USER_OPT_GENERATED}) { return; }
1.133 bowersj2 1772:
1.221 bowersj2 1773: my $uname=$ENV{'user.name'};
1774: my $udom=$ENV{'user.domain'};
1775: my $uhome=$ENV{'user.home'};
1776: my $cid=$ENV{'request.course.id'};
1777: my $chome=$ENV{'course.'.$cid.'.home'};
1778: my ($cdom,$cnum)=split(/\_/,$cid);
1779:
1780: my $userprefix=$uname.'_'.$udom.'_';
1781:
1782: my %courserdatas; my %useropt; my %courseopt; my %userrdatas;
1783: unless ($uhome eq 'no_host') {
1.133 bowersj2 1784: # ------------------------------------------------- Get coursedata (if present)
1.221 bowersj2 1785: unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
1786: my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
1787: ':resourcedata',$chome);
1788: # Check for network failure
1789: if ( $reply =~ /no.such.host/i || $reply =~ /con_lost/i) {
1790: $self->{NETWORK_FAILURE} = 1;
1791: } elsif ($reply!~/^error\:/) {
1792: $courserdatas{$cid}=$reply;
1793: $courserdatas{$cid.'.last_cache'}=time;
1794: }
1795: }
1796: foreach (split(/\&/,$courserdatas{$cid})) {
1797: my ($name,$value)=split(/\=/,$_);
1798: $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
1799: &Apache::lonnet::unescape($value);
1800: }
1.133 bowersj2 1801: # --------------------------------------------------- Get userdata (if present)
1.221 bowersj2 1802: unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
1803: my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
1804: if ($reply!~/^error\:/) {
1805: $userrdatas{$uname.'___'.$udom}=$reply;
1806: $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
1807: }
1808: # check to see if network failed
1809: elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
1810: {
1811: $self->{NETWORK_FAILURE} = 1;
1812: }
1813: }
1814: foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
1815: my ($name,$value)=split(/\=/,$_);
1816: $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
1817: &Apache::lonnet::unescape($value);
1818: }
1819: $self->{COURSE_OPT} = \%courseopt;
1820: $self->{USER_OPT} = \%useropt;
1821: }
1822:
1823: $self->{COURSE_USER_OPT_GENERATED} = 1;
1824:
1825: return;
1826: }
1827:
1828: sub generate_email_discuss_status {
1829: my $self = shift;
1.259 raeburn 1830: my $symb = shift;
1.221 bowersj2 1831: if ($self->{EMAIL_DISCUSS_GENERATED}) { return; }
1.133 bowersj2 1832:
1.221 bowersj2 1833: my $cid=$ENV{'request.course.id'};
1834: my ($cdom,$cnum)=split(/\_/,$cid);
1835:
1836: my %emailstatus = &Apache::lonnet::dump('email_status');
1837: my $logoutTime = $emailstatus{'logout'};
1838: my $courseLeaveTime = $emailstatus{'logout_'.$ENV{'request.course.id'}};
1839: $self->{LAST_CHECK} = (($courseLeaveTime > $logoutTime) ?
1840: $courseLeaveTime : $logoutTime);
1841: my %discussiontime = &Apache::lonnet::dump('discussiontimes',
1842: $cdom, $cnum);
1.259 raeburn 1843: my %lastread = &Apache::lonnet::dump('nohist_'.$cid.'_discuss',
1844: $ENV{'user.domain'},$ENV{'user.name'},'lastread');
1845: my %lastreadtime = ();
1846: foreach (keys %lastread) {
1847: my $key = $_;
1848: $key =~ s/_lastread$//;
1849: $lastreadtime{$key} = $lastread{$_};
1850: }
1851:
1.221 bowersj2 1852: my %feedback=();
1853: my %error=();
1854: my $keys = &Apache::lonnet::reply('keys:'.
1855: $ENV{'user.domain'}.':'.
1856: $ENV{'user.name'}.':nohist_email',
1857: $ENV{'user.home'});
1858:
1859: foreach my $msgid (split(/\&/, $keys)) {
1860: $msgid=&Apache::lonnet::unescape($msgid);
1.267.2.7 albertel 1861: if ((!$emailstatus{$msgid}) || ($emailstatus{$msgid} eq 'new')) {
1862: my $plain=
1863: &Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
1864: if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
1865: my ($what,$url)=($1,$2);
1.221 bowersj2 1866: if ($what eq 'Error') {
1867: $error{$url}.=','.$msgid;
1868: } else {
1869: $feedback{$url}.=','.$msgid;
1870: }
1871: }
1872: }
1.211 bowersj2 1873: }
1.221 bowersj2 1874:
1.267.2.7 albertel 1875: #url's of resources that have feedbacks
1.221 bowersj2 1876: $self->{FEEDBACK} = \%feedback;
1.267.2.7 albertel 1877: #or errors
1878: $self->{ERROR_MSG} = \%error;
1.221 bowersj2 1879: $self->{DISCUSSION_TIME} = \%discussiontime;
1880: $self->{EMAIL_STATUS} = \%emailstatus;
1.259 raeburn 1881: $self->{LAST_READ} = \%lastreadtime;
1.221 bowersj2 1882:
1883: $self->{EMAIL_DISCUSS_GENERATED} = 1;
1884: }
1885:
1886: sub get_user_data {
1887: my $self = shift;
1888: if ($self->{RETRIEVED_USER_DATA}) { return; }
1.211 bowersj2 1889:
1.221 bowersj2 1890: # Retrieve performance data on problems
1891: my %student_data = Apache::lonnet::currentdump($ENV{'request.course.id'},
1892: $ENV{'user.domain'},
1893: $ENV{'user.name'});
1894: $self->{STUDENT_DATA} = \%student_data;
1.133 bowersj2 1895:
1.221 bowersj2 1896: $self->{RETRIEVED_USER_DATA} = 1;
1.133 bowersj2 1897: }
1898:
1899: # Internal function: Takes a key to look up in the nav hash and implements internal
1900: # memory caching of that key.
1901: sub navhash {
1902: my $self = shift; my $key = shift;
1903: return $self->{NAV_HASH}->{$key};
1904: }
1905:
1.217 bowersj2 1906: =pod
1907:
1908: =item * B<courseMapDefined>(): Returns true if the course map is defined,
1909: false otherwise. Undefined course maps indicate an error somewhere in
1910: LON-CAPA, and you will not be able to proceed with using the navmap.
1911: See the B<NAV> screen for an example of using this.
1912:
1913: =cut
1914:
1.133 bowersj2 1915: # Checks to see if coursemap is defined, matching test in old lonnavmaps
1916: sub courseMapDefined {
1917: my $self = shift;
1918: my $uri = &Apache::lonnet::clutter($ENV{'request.course.uri'});
1919:
1920: my $firstres = $self->navhash("map_start_$uri");
1921: my $lastres = $self->navhash("map_finish_$uri");
1922: return $firstres && $lastres;
1923: }
1924:
1925: sub getIterator {
1926: my $self = shift;
1927: my $iterator = Apache::lonnavmaps::iterator->new($self, shift, shift,
1928: shift, undef, shift);
1929: return $iterator;
1930: }
1931:
1932: # unties the hash when done
1933: sub untieHashes {
1.168 bowersj2 1934: my $self = shift;
1.170 matthew 1935: untie %{$self->{NAV_HASH}};
1936: untie %{$self->{PARM_HASH}};
1.133 bowersj2 1937: }
1938:
1939: # Private method: Does the given resource (as a symb string) have
1940: # current discussion? Returns 0 if chat/mail data not extracted.
1941: sub hasDiscussion {
1942: my $self = shift;
1943: my $symb = shift;
1.221 bowersj2 1944:
1945: $self->generate_email_discuss_status();
1946:
1.133 bowersj2 1947: if (!defined($self->{DISCUSSION_TIME})) { return 0; }
1948:
1949: #return defined($self->{DISCUSSION_TIME}->{$symb});
1.259 raeburn 1950:
1951: # backward compatibility (bulletin boards used to be 'wrapped')
1952: my $ressymb = $symb;
1953: if ($ressymb =~ m|adm/(\w+)/(\w+)/(\d+)/bulletinboard$|) {
1.266 raeburn 1954: unless ($ressymb =~ m|adm/wrapper/adm|) {
1.259 raeburn 1955: $ressymb = 'bulletin___'.$3.'___adm/wrapper/adm/'.$1.'/'.$2.'/'.$3.'/bulletinboard';
1956: }
1957: }
1958:
1959: if ( defined ( $self->{LAST_READ}->{$ressymb} ) ) {
1960: return $self->{DISCUSSION_TIME}->{$ressymb} > $self->{LAST_READ}->{$ressymb};
1961: } else {
1.266 raeburn 1962: # return $self->{DISCUSSION_TIME}->{$ressymb} > $self->{LAST_CHECK}; # v.1.1 behavior
1963: return $self->{DISCUSSION_TIME}->{$ressymb} > 0; # in 1.2 will display speech bubble icons for all items with posts until marked as read (even if read in v 1.1).
1.259 raeburn 1964: }
1.133 bowersj2 1965: }
1966:
1967: # Private method: Does the given resource (as a symb string) have
1968: # current feedback? Returns the string in the feedback hash, which
1969: # will be false if it does not exist.
1970: sub getFeedback {
1971: my $self = shift;
1972: my $symb = shift;
1973:
1.221 bowersj2 1974: $self->generate_email_discuss_status();
1975:
1.133 bowersj2 1976: if (!defined($self->{FEEDBACK})) { return ""; }
1977:
1978: return $self->{FEEDBACK}->{$symb};
1979: }
1980:
1981: # Private method: Get the errors for that resource (by source).
1982: sub getErrors {
1983: my $self = shift;
1984: my $src = shift;
1.221 bowersj2 1985:
1986: $self->generate_email_discuss_status();
1987:
1.133 bowersj2 1988: if (!defined($self->{ERROR_MSG})) { return ""; }
1989: return $self->{ERROR_MSG}->{$src};
1990: }
1991:
1992: =pod
1993:
1.174 albertel 1994: =item * B<getById>(id):
1995:
1996: Based on the ID of the resource (1.1, 3.2, etc.), get a resource
1997: object for that resource. This method, or other methods that use it
1998: (as in the resource object) is the only proper way to obtain a
1999: resource object.
1.133 bowersj2 2000:
1.194 bowersj2 2001: =item * B<getBySymb>(symb):
2002:
2003: Based on the symb of the resource, get a resource object for that
2004: resource. This is one of the proper ways to get a resource object.
2005:
2006: =item * B<getMapByMapPc>(map_pc):
2007:
2008: Based on the map_pc of the resource, get a resource object for
2009: the given map. This is one of the proper ways to get a resource object.
2010:
1.133 bowersj2 2011: =cut
2012:
2013: # The strategy here is to cache the resource objects, and only construct them
2014: # as we use them. The real point is to prevent reading any more from the tied
2015: # hash then we have to, which should hopefully alleviate speed problems.
2016:
2017: sub getById {
2018: my $self = shift;
2019: my $id = shift;
2020:
2021: if (defined ($self->{RESOURCE_CACHE}->{$id}))
2022: {
2023: return $self->{RESOURCE_CACHE}->{$id};
2024: }
2025:
2026: # resource handles inserting itself into cache.
2027: # Not clear why the quotes are necessary, but as of this
2028: # writing it doesn't work without them.
2029: return "Apache::lonnavmaps::resource"->new($self, $id);
1.132 bowersj2 2030: }
1.133 bowersj2 2031:
1.172 bowersj2 2032: sub getBySymb {
2033: my $self = shift;
2034: my $symb = shift;
1.228 albertel 2035: my ($mapUrl, $id, $filename) = &Apache::lonnet::decode_symb($symb);
1.172 bowersj2 2036: my $map = $self->getResourceByUrl($mapUrl);
2037: return $self->getById($map->map_pc() . '.' . $id);
1.194 bowersj2 2038: }
2039:
2040: sub getByMapPc {
2041: my $self = shift;
2042: my $map_pc = shift;
2043: my $map_id = $self->{NAV_HASH}->{'map_id_' . $map_pc};
2044: $map_id = $self->{NAV_HASH}->{'ids_' . $map_id};
2045: return $self->getById($map_id);
1.172 bowersj2 2046: }
2047:
1.133 bowersj2 2048: =pod
2049:
1.174 albertel 2050: =item * B<firstResource>():
2051:
2052: Returns a resource object reference corresponding to the first
2053: resource in the navmap.
1.133 bowersj2 2054:
2055: =cut
2056:
2057: sub firstResource {
2058: my $self = shift;
2059: my $firstResource = $self->navhash('map_start_' .
2060: &Apache::lonnet::clutter($ENV{'request.course.uri'}));
2061: return $self->getById($firstResource);
1.132 bowersj2 2062: }
1.133 bowersj2 2063:
2064: =pod
2065:
1.174 albertel 2066: =item * B<finishResource>():
2067:
2068: Returns a resource object reference corresponding to the last resource
2069: in the navmap.
1.133 bowersj2 2070:
2071: =cut
2072:
2073: sub finishResource {
2074: my $self = shift;
2075: my $firstResource = $self->navhash('map_finish_' .
2076: &Apache::lonnet::clutter($ENV{'request.course.uri'}));
2077: return $self->getById($firstResource);
1.132 bowersj2 2078: }
1.133 bowersj2 2079:
2080: # Parmval reads the parm hash and cascades the lookups. parmval_real does
2081: # the actual lookup; parmval caches the results.
2082: sub parmval {
2083: my $self = shift;
2084: my ($what,$symb)=@_;
2085: my $hashkey = $what."|||".$symb;
2086:
2087: if (defined($self->{PARM_CACHE}->{$hashkey})) {
2088: return $self->{PARM_CACHE}->{$hashkey};
2089: }
2090:
2091: my $result = $self->parmval_real($what, $symb);
2092: $self->{PARM_CACHE}->{$hashkey} = $result;
2093: return $result;
1.132 bowersj2 2094: }
2095:
1.133 bowersj2 2096: sub parmval_real {
2097: my $self = shift;
1.219 albertel 2098: my ($what,$symb,$recurse) = @_;
1.133 bowersj2 2099:
1.221 bowersj2 2100: # Make sure the {USER_OPT} and {COURSE_OPT} hashes are populated
2101: $self->generate_course_user_opt();
2102:
1.133 bowersj2 2103: my $cid=$ENV{'request.course.id'};
2104: my $csec=$ENV{'request.course.sec'};
2105: my $uname=$ENV{'user.name'};
2106: my $udom=$ENV{'user.domain'};
2107:
2108: unless ($symb) { return ''; }
2109: my $result='';
2110:
1.226 www 2111: my ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($symb);
1.133 bowersj2 2112:
2113: # ----------------------------------------------------- Cascading lookup scheme
2114: my $rwhat=$what;
2115: $what=~s/^parameter\_//;
2116: $what=~s/\_/\./;
2117:
2118: my $symbparm=$symb.'.'.$what;
2119: my $mapparm=$mapname.'___(all).'.$what;
2120: my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
2121:
2122: my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
2123: my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
2124: my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
2125:
2126: my $courselevel= $usercourseprefix.'.'.$what;
2127: my $courselevelr=$usercourseprefix.'.'.$symbparm;
2128: my $courselevelm=$usercourseprefix.'.'.$mapparm;
2129:
2130: my $useropt = $self->{USER_OPT};
2131: my $courseopt = $self->{COURSE_OPT};
2132: my $parmhash = $self->{PARM_HASH};
2133:
2134: # ---------------------------------------------------------- first, check user
2135: if ($uname and defined($useropt)) {
2136: if (defined($$useropt{$courselevelr})) { return $$useropt{$courselevelr}; }
2137: if (defined($$useropt{$courselevelm})) { return $$useropt{$courselevelm}; }
2138: if (defined($$useropt{$courselevel})) { return $$useropt{$courselevel}; }
2139: }
2140:
2141: # ------------------------------------------------------- second, check course
2142: if ($csec and defined($courseopt)) {
2143: if (defined($$courseopt{$seclevelr})) { return $$courseopt{$seclevelr}; }
2144: if (defined($$courseopt{$seclevelm})) { return $$courseopt{$seclevelm}; }
2145: if (defined($$courseopt{$seclevel})) { return $$courseopt{$seclevel}; }
2146: }
2147:
2148: if (defined($courseopt)) {
2149: if (defined($$courseopt{$courselevelr})) { return $$courseopt{$courselevelr}; }
2150: if (defined($$courseopt{$courselevelm})) { return $$courseopt{$courselevelm}; }
2151: if (defined($$courseopt{$courselevel})) { return $$courseopt{$courselevel}; }
2152: }
2153:
2154: # ----------------------------------------------------- third, check map parms
2155:
2156: my $thisparm=$$parmhash{$symbparm};
2157: if (defined($thisparm)) { return $thisparm; }
2158:
2159: # ----------------------------------------------------- fourth , check default
2160:
1.246 albertel 2161: my $meta_rwhat=$rwhat;
2162: $meta_rwhat=~s/\./_/g;
2163: my $default=&Apache::lonnet::metadata($fn,$meta_rwhat);
2164: if (defined($default)) { return $default}
2165: $default=&Apache::lonnet::metadata($fn,'parameter_'.$meta_rwhat);
1.133 bowersj2 2166: if (defined($default)) { return $default}
2167:
2168: # --------------------------------------------------- fifth , cascade up parts
2169:
2170: my ($space,@qualifier)=split(/\./,$rwhat);
2171: my $qualifier=join('.',@qualifier);
2172: unless ($space eq '0') {
1.160 albertel 2173: my @parts=split(/_/,$space);
2174: my $id=pop(@parts);
2175: my $part=join('_',@parts);
2176: if ($part eq '') { $part='0'; }
1.219 albertel 2177: my $partgeneral=$self->parmval($part.".$qualifier",$symb,1);
1.160 albertel 2178: if (defined($partgeneral)) { return $partgeneral; }
1.133 bowersj2 2179: }
1.219 albertel 2180: if ($recurse) { return undef; }
2181: my $pack_def=&Apache::lonnet::packages_tab_default($fn,'resource.'.$what);
2182: if (defined($pack_def)) { return $pack_def; }
1.133 bowersj2 2183: return '';
1.145 bowersj2 2184: }
2185:
1.174 albertel 2186: =pod
1.145 bowersj2 2187:
1.174 albertel 2188: =item * B<getResourceByUrl>(url):
1.145 bowersj2 2189:
1.174 albertel 2190: Retrieves a resource object by URL of the resource. If passed a
2191: resource object, it will simply return it, so it is safe to use this
2192: method in code like "$res = $navmap->getResourceByUrl($res)", if
2193: you're not sure if $res is already an object, or just a URL. If the
2194: resource appears multiple times in the course, only the first instance
2195: will be returned. As a result, this is probably useful only for maps.
2196:
2197: =item * B<retrieveResources>(map, filterFunc, recursive, bailout):
2198:
2199: The map is a specification of a map to retreive the resources from,
2200: either as a url or as an object. The filterFunc is a reference to a
2201: function that takes a resource object as its one argument and returns
2202: true if the resource should be included, or false if it should not
2203: be. If recursive is true, the map will be recursively examined,
2204: otherwise it will not be. If bailout is true, the function will return
2205: as soon as it finds a resource, if false it will finish. By default,
2206: the map is the top-level map of the course, filterFunc is a function
2207: that always returns 1, recursive is true, bailout is false. The
2208: resources will be returned in a list containing the resource objects
2209: for the corresponding resources, with B<no structure information> in
2210: the list; regardless of branching, recursion, etc., it will be a flat
2211: list.
2212:
2213: Thus, this is suitable for cases where you don't want the structure,
2214: just a list of all resources. It is also suitable for finding out how
2215: many resources match a given description; for this use, if all you
2216: want to know is if I<any> resources match the description, the bailout
2217: parameter will allow you to avoid potentially expensive enumeration of
2218: all matching resources.
1.145 bowersj2 2219:
1.256 albertel 2220: =item * B<hasResource>(map, filterFunc, recursive):
1.145 bowersj2 2221:
1.174 albertel 2222: Convience method for
1.146 bowersj2 2223:
2224: scalar(retrieveResources($map, $filterFunc, $recursive, 1)) > 0
2225:
1.174 albertel 2226: which will tell whether the map has resources matching the description
2227: in the filter function.
1.146 bowersj2 2228:
1.145 bowersj2 2229: =cut
2230:
2231: sub getResourceByUrl {
2232: my $self = shift;
2233: my $resUrl = shift;
2234:
2235: if (ref($resUrl)) { return $resUrl; }
2236:
2237: $resUrl = &Apache::lonnet::clutter($resUrl);
2238: my $resId = $self->{NAV_HASH}->{'ids_' . $resUrl};
2239: if ($resId =~ /,/) {
2240: $resId = (split (/,/, $resId))[0];
2241: }
2242: if (!$resId) { return ''; }
2243: return $self->getById($resId);
2244: }
2245:
2246: sub retrieveResources {
2247: my $self = shift;
2248: my $map = shift;
2249: my $filterFunc = shift;
2250: if (!defined ($filterFunc)) {
2251: $filterFunc = sub {return 1;};
2252: }
2253: my $recursive = shift;
2254: if (!defined($recursive)) { $recursive = 1; }
2255: my $bailout = shift;
2256: if (!defined($bailout)) { $bailout = 0; }
2257:
2258: # Create the necessary iterator.
2259: if (!ref($map)) { # assume it's a url of a map.
1.172 bowersj2 2260: $map = $self->getResourceByUrl($map);
1.145 bowersj2 2261: }
2262:
1.213 bowersj2 2263: # If nothing was passed, assume top-level map
2264: if (!$map) {
2265: $map = $self->getById('0.0');
2266: }
2267:
1.145 bowersj2 2268: # Check the map's validity.
1.213 bowersj2 2269: if (!$map->is_map()) {
1.145 bowersj2 2270: # Oh, to throw an exception.... how I'd love that!
2271: return ();
2272: }
2273:
1.146 bowersj2 2274: # Get an iterator.
2275: my $it = $self->getIterator($map->map_start(), $map->map_finish(),
1.215 bowersj2 2276: undef, $recursive);
1.146 bowersj2 2277:
2278: my @resources = ();
2279:
2280: # Run down the iterator and collect the resources.
1.222 bowersj2 2281: my $curRes;
2282:
2283: while ($curRes = $it->next()) {
1.146 bowersj2 2284: if (ref($curRes)) {
2285: if (!&$filterFunc($curRes)) {
2286: next;
2287: }
2288:
2289: push @resources, $curRes;
2290:
2291: if ($bailout) {
2292: return @resources;
2293: }
2294: }
2295:
2296: }
2297:
2298: return @resources;
2299: }
2300:
2301: sub hasResource {
2302: my $self = shift;
2303: my $map = shift;
2304: my $filterFunc = shift;
2305: my $recursive = shift;
2306:
2307: return scalar($self->retrieveResources($map, $filterFunc, $recursive, 1)) > 0;
1.133 bowersj2 2308: }
1.51 bowersj2 2309:
2310: 1;
2311:
2312: package Apache::lonnavmaps::iterator;
2313:
2314: =pod
2315:
2316: =back
2317:
1.174 albertel 2318: =head1 Object: navmap Iterator
1.51 bowersj2 2319:
1.174 albertel 2320: An I<iterator> encapsulates the logic required to traverse a data
2321: structure. navmap uses an iterator to traverse the course map
2322: according to the criteria you wish to use.
2323:
2324: To obtain an iterator, call the B<getIterator>() function of a
2325: B<navmap> object. (Do not instantiate Apache::lonnavmaps::iterator
2326: directly.) This will return a reference to the iterator:
1.51 bowersj2 2327:
2328: C<my $resourceIterator = $navmap-E<gt>getIterator();>
2329:
2330: To get the next thing from the iterator, call B<next>:
2331:
2332: C<my $nextThing = $resourceIterator-E<gt>next()>
2333:
2334: getIterator behaves as follows:
2335:
2336: =over 4
2337:
1.174 albertel 2338: =item * B<getIterator>(firstResource, finishResource, filterHash, condition, forceTop, returnTopMap):
2339:
2340: All parameters are optional. firstResource is a resource reference
2341: corresponding to where the iterator should start. It defaults to
2342: navmap->firstResource() for the corresponding nav map. finishResource
2343: corresponds to where you want the iterator to end, defaulting to
2344: navmap->finishResource(). filterHash is a hash used as a set
2345: containing strings representing the resource IDs, defaulting to
2346: empty. Condition is a 1 or 0 that sets what to do with the filter
1.205 bowersj2 2347: hash: If a 0, then only resources that exist IN the filterHash will be
1.174 albertel 2348: recursed on. If it is a 1, only resources NOT in the filterHash will
2349: be recursed on. Defaults to 0. forceTop is a boolean value. If it is
2350: false (default), the iterator will only return the first level of map
2351: that is not just a single, 'redirecting' map. If true, the iterator
2352: will return all information, starting with the top-level map,
2353: regardless of content. returnTopMap, if true (default false), will
2354: cause the iterator to return the top-level map object (resource 0.0)
2355: before anything else.
2356:
2357: Thus, by default, only top-level resources will be shown. Change the
2358: condition to a 1 without changing the hash, and all resources will be
2359: shown. Changing the condition to 1 and including some values in the
2360: hash will allow you to selectively suppress parts of the navmap, while
2361: leaving it on 0 and adding things to the hash will allow you to
2362: selectively add parts of the nav map. See the handler code for
2363: examples.
2364:
2365: The iterator will return either a reference to a resource object, or a
2366: token representing something in the map, such as the beginning of a
2367: new branch. The possible tokens are:
2368:
2369: =over 4
1.51 bowersj2 2370:
1.222 bowersj2 2371: =item * B<END_ITERATOR>:
2372:
2373: The iterator has returned all that it's going to. Further calls to the
2374: iterator will just produce more of these. This is a "false" value, and
2375: is the only false value the iterator which will be returned, so it can
2376: be used as a loop sentinel.
2377:
1.217 bowersj2 2378: =item * B<BEGIN_MAP>:
1.51 bowersj2 2379:
1.174 albertel 2380: A new map is being recursed into. This is returned I<after> the map
2381: resource itself is returned.
1.51 bowersj2 2382:
1.217 bowersj2 2383: =item * B<END_MAP>:
1.174 albertel 2384:
2385: The map is now done.
1.51 bowersj2 2386:
1.217 bowersj2 2387: =item * B<BEGIN_BRANCH>:
1.70 bowersj2 2388:
1.174 albertel 2389: A branch is now starting. The next resource returned will be the first
2390: in that branch.
1.70 bowersj2 2391:
1.217 bowersj2 2392: =item * B<END_BRANCH>:
1.70 bowersj2 2393:
1.174 albertel 2394: The branch is now done.
1.51 bowersj2 2395:
2396: =back
2397:
1.174 albertel 2398: The tokens are retreivable via methods on the iterator object, i.e.,
2399: $iterator->END_MAP.
1.70 bowersj2 2400:
1.174 albertel 2401: Maps can contain empty resources. The iterator will automatically skip
2402: over such resources, but will still treat the structure
2403: correctly. Thus, a complicated map with several branches, but
2404: consisting entirely of empty resources except for one beginning or
2405: ending resource, will cause a lot of BRANCH_STARTs and BRANCH_ENDs,
2406: but only one resource will be returned.
1.116 bowersj2 2407:
1.242 matthew 2408: =back
2409:
1.222 bowersj2 2410: =head2 Normal Usage
2411:
2412: Normal usage of the iterator object is to do the following:
2413:
2414: my $it = $navmap->getIterator([your params here]);
2415: my $curRes;
2416: while ($curRes = $it->next()) {
2417: [your logic here]
2418: }
2419:
2420: Note that inside of the loop, it's frequently useful to check if
2421: "$curRes" is a reference or not with the reference function; only
2422: resource objects will be references, and any non-references will
2423: be the tokens described above.
2424:
2425: Also note there is some old code floating around that trys to track
2426: the depth of the iterator to see when it's done; do not copy that
2427: code. It is difficult to get right and harder to understand then
2428: this. They should be migrated to this new style.
1.51 bowersj2 2429:
2430: =cut
2431:
2432: # Here are the tokens for the iterator:
2433:
1.222 bowersj2 2434: sub END_ITERATOR { return 0; }
1.51 bowersj2 2435: sub BEGIN_MAP { return 1; } # begining of a new map
2436: sub END_MAP { return 2; } # end of the map
2437: sub BEGIN_BRANCH { return 3; } # beginning of a branch
2438: sub END_BRANCH { return 4; } # end of a branch
1.89 bowersj2 2439: sub FORWARD { return 1; } # go forward
2440: sub BACKWARD { return 2; }
1.51 bowersj2 2441:
1.96 bowersj2 2442: sub min {
2443: (my $a, my $b) = @_;
2444: if ($a < $b) { return $a; } else { return $b; }
2445: }
2446:
1.94 bowersj2 2447: sub new {
2448: # magic invocation to create a class instance
2449: my $proto = shift;
2450: my $class = ref($proto) || $proto;
2451: my $self = {};
2452:
2453: $self->{NAV_MAP} = shift;
2454: return undef unless ($self->{NAV_MAP});
2455:
2456: # Handle the parameters
2457: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
2458: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
2459:
2460: # If the given resources are just the ID of the resource, get the
2461: # objects
2462: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
2463: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
2464: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
2465: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
2466:
2467: $self->{FILTER} = shift;
2468:
2469: # A hash, used as a set, of resource already seen
2470: $self->{ALREADY_SEEN} = shift;
2471: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
2472: $self->{CONDITION} = shift;
2473:
1.116 bowersj2 2474: # Do we want to automatically follow "redirection" maps?
2475: $self->{FORCE_TOP} = shift;
2476:
1.162 bowersj2 2477: # Do we want to return the top-level map object (resource 0.0)?
2478: $self->{RETURN_0} = shift;
2479: # have we done that yet?
2480: $self->{HAVE_RETURNED_0} = 0;
2481:
1.94 bowersj2 2482: # Now, we need to pre-process the map, by walking forward and backward
2483: # over the parts of the map we're going to look at.
1.96 bowersj2 2484:
1.97 bowersj2 2485: # The processing steps are exactly the same, except for a few small
2486: # changes, so I bundle those up in the following list of two elements:
2487: # (direction_to_iterate, VAL_name, next_resource_method_to_call,
2488: # first_resource).
2489: # This prevents writing nearly-identical code twice.
2490: my @iterations = ( [FORWARD(), 'TOP_DOWN_VAL', 'getNext',
2491: 'FIRST_RESOURCE'],
2492: [BACKWARD(), 'BOT_UP_VAL', 'getPrevious',
2493: 'FINISH_RESOURCE'] );
2494:
1.98 bowersj2 2495: my $maxDepth = 0; # tracks max depth
2496:
1.116 bowersj2 2497: # If there is only one resource in this map, and it's a map, we
2498: # want to remember that, so the user can ask for the first map
2499: # that isn't just a redirector.
2500: my $resource; my $resourceCount = 0;
2501:
1.209 bowersj2 2502: # Documentation on this algorithm can be found in the CVS repository at
2503: # /docs/lonnavdocs; these "**#**" markers correspond to documentation
2504: # in that file.
1.107 bowersj2 2505: # **1**
2506:
1.97 bowersj2 2507: foreach my $pass (@iterations) {
2508: my $direction = $pass->[0];
2509: my $valName = $pass->[1];
2510: my $nextResourceMethod = $pass->[2];
2511: my $firstResourceName = $pass->[3];
2512:
2513: my $iterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP},
2514: $self->{FIRST_RESOURCE},
2515: $self->{FINISH_RESOURCE},
2516: {}, undef, 0, $direction);
1.96 bowersj2 2517:
1.97 bowersj2 2518: # prime the recursion
2519: $self->{$firstResourceName}->{DATA}->{$valName} = 0;
1.222 bowersj2 2520: $iterator->next();
1.97 bowersj2 2521: my $curRes = $iterator->next();
1.222 bowersj2 2522: my $depth = 1;
2523: while ($depth > 0) {
2524: if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
2525: if ($curRes == $iterator->END_MAP()) { $depth--; }
2526:
1.97 bowersj2 2527: if (ref($curRes)) {
1.116 bowersj2 2528: # If there's only one resource, this will save it
1.117 bowersj2 2529: # we have to filter empty resources from consideration here,
2530: # or even "empty", redirecting maps have two (start & finish)
2531: # or three (start, finish, plus redirector)
2532: if($direction == FORWARD && $curRes->src()) {
2533: $resource = $curRes; $resourceCount++;
2534: }
1.97 bowersj2 2535: my $resultingVal = $curRes->{DATA}->{$valName};
2536: my $nextResources = $curRes->$nextResourceMethod();
1.116 bowersj2 2537: my $nextCount = scalar(@{$nextResources});
1.104 bowersj2 2538:
1.116 bowersj2 2539: if ($nextCount == 1) { # **3**
1.97 bowersj2 2540: my $current = $nextResources->[0]->{DATA}->{$valName} || 999999999;
2541: $nextResources->[0]->{DATA}->{$valName} = min($resultingVal, $current);
2542: }
2543:
1.116 bowersj2 2544: if ($nextCount > 1) { # **4**
1.97 bowersj2 2545: foreach my $res (@{$nextResources}) {
2546: my $current = $res->{DATA}->{$valName} || 999999999;
2547: $res->{DATA}->{$valName} = min($current, $resultingVal + 1);
2548: }
2549: }
2550: }
1.96 bowersj2 2551:
1.107 bowersj2 2552: # Assign the final val (**2**)
1.97 bowersj2 2553: if (ref($curRes) && $direction == BACKWARD()) {
1.98 bowersj2 2554: my $finalDepth = min($curRes->{DATA}->{TOP_DOWN_VAL},
2555: $curRes->{DATA}->{BOT_UP_VAL});
2556:
2557: $curRes->{DATA}->{DISPLAY_DEPTH} = $finalDepth;
2558: if ($finalDepth > $maxDepth) {$maxDepth = $finalDepth;}
1.190 bowersj2 2559: }
1.222 bowersj2 2560:
2561: $curRes = $iterator->next();
1.96 bowersj2 2562: }
2563: }
1.94 bowersj2 2564:
1.116 bowersj2 2565: # Check: Was this only one resource, a map?
1.255 albertel 2566: if ($resourceCount == 1 && $resource->is_sequence() && !$self->{FORCE_TOP}) {
1.116 bowersj2 2567: my $firstResource = $resource->map_start();
2568: my $finishResource = $resource->map_finish();
2569: return
2570: Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
2571: $finishResource, $self->{FILTER},
2572: $self->{ALREADY_SEEN},
2573: $self->{CONDITION}, 0);
2574:
2575: }
2576:
1.98 bowersj2 2577: # Set up some bookkeeping information.
2578: $self->{CURRENT_DEPTH} = 0;
2579: $self->{MAX_DEPTH} = $maxDepth;
2580: $self->{STACK} = [];
2581: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1.222 bowersj2 2582: $self->{FINISHED} = 0; # When true, the iterator has finished
1.98 bowersj2 2583:
2584: for (my $i = 0; $i <= $self->{MAX_DEPTH}; $i++) {
2585: push @{$self->{STACK}}, [];
2586: }
2587:
1.107 bowersj2 2588: # Prime the recursion w/ the first resource **5**
1.98 bowersj2 2589: push @{$self->{STACK}->[0]}, $self->{FIRST_RESOURCE};
2590: $self->{ALREADY_SEEN}->{$self->{FIRST_RESOURCE}->{ID}} = 1;
2591:
2592: bless ($self);
2593:
2594: return $self;
2595: }
2596:
2597: sub next {
2598: my $self = shift;
1.256 albertel 2599: my $closeAllPages=shift;
1.222 bowersj2 2600: if ($self->{FINISHED}) {
2601: return END_ITERATOR();
2602: }
2603:
1.162 bowersj2 2604: # If we want to return the top-level map object, and haven't yet,
2605: # do so.
2606: if ($self->{RETURN_0} && !$self->{HAVE_RETURNED_0}) {
2607: $self->{HAVE_RETURNED_0} = 1;
2608: return $self->{NAV_MAP}->getById('0.0');
2609: }
1.98 bowersj2 2610:
2611: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
2612: # grab the next from the recursive iterator
1.256 albertel 2613: my $next = $self->{RECURSIVE_ITERATOR}->next($closeAllPages);
1.98 bowersj2 2614:
2615: # is it a begin or end map? If so, update the depth
2616: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
2617: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
2618:
2619: # Are we back at depth 0? If so, stop recursing
2620: if ($self->{RECURSIVE_DEPTH} == 0) {
2621: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2622: }
2623:
2624: return $next;
2625: }
2626:
2627: if (defined($self->{FORCE_NEXT})) {
2628: my $tmp = $self->{FORCE_NEXT};
2629: $self->{FORCE_NEXT} = undef;
2630: return $tmp;
2631: }
2632:
2633: # Have we not yet begun? If not, return BEGIN_MAP and
2634: # remember we've started.
2635: if ( !$self->{STARTED} ) {
2636: $self->{STARTED} = 1;
2637: return $self->BEGIN_MAP();
2638: }
2639:
2640: # Here's the guts of the iterator.
2641:
2642: # Find the next resource, if any.
2643: my $found = 0;
2644: my $i = $self->{MAX_DEPTH};
2645: my $newDepth;
2646: my $here;
2647: while ( $i >= 0 && !$found ) {
1.107 bowersj2 2648: if ( scalar(@{$self->{STACK}->[$i]}) > 0 ) { # **6**
2649: $here = pop @{$self->{STACK}->[$i]}; # **7**
1.98 bowersj2 2650: $found = 1;
2651: $newDepth = $i;
2652: }
2653: $i--;
2654: }
2655:
2656: # If we still didn't find anything, we're done.
2657: if ( !$found ) {
2658: # We need to get back down to the correct branch depth
2659: if ( $self->{CURRENT_DEPTH} > 0 ) {
2660: $self->{CURRENT_DEPTH}--;
2661: return END_BRANCH();
2662: } else {
1.222 bowersj2 2663: $self->{FINISHED} = 1;
1.98 bowersj2 2664: return END_MAP();
2665: }
2666: }
2667:
1.104 bowersj2 2668: # If this is not a resource, it must be an END_BRANCH marker we want
2669: # to return directly.
1.107 bowersj2 2670: if (!ref($here)) { # **8**
1.104 bowersj2 2671: if ($here == END_BRANCH()) { # paranoia, in case of later extension
2672: $self->{CURRENT_DEPTH}--;
2673: return $here;
2674: }
2675: }
2676:
2677: # Otherwise, it is a resource and it's safe to store in $self->{HERE}
2678: $self->{HERE} = $here;
2679:
1.98 bowersj2 2680: # Get to the right level
2681: if ( $self->{CURRENT_DEPTH} > $newDepth ) {
2682: push @{$self->{STACK}->[$newDepth]}, $here;
2683: $self->{CURRENT_DEPTH}--;
2684: return END_BRANCH();
2685: }
2686: if ( $self->{CURRENT_DEPTH} < $newDepth) {
2687: push @{$self->{STACK}->[$newDepth]}, $here;
2688: $self->{CURRENT_DEPTH}++;
2689: return BEGIN_BRANCH();
2690: }
2691:
2692: # If we made it here, we have the next resource, and we're at the
2693: # right branch level. So let's examine the resource for where
2694: # we can get to from here.
2695:
2696: # So we need to look at all the resources we can get to from here,
2697: # categorize them if we haven't seen them, remember if we have a new
2698: my $nextUnfiltered = $here->getNext();
1.104 bowersj2 2699: my $maxDepthAdded = -1;
2700:
1.98 bowersj2 2701: for (@$nextUnfiltered) {
2702: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
1.104 bowersj2 2703: my $depth = $_->{DATA}->{DISPLAY_DEPTH};
2704: push @{$self->{STACK}->[$depth]}, $_;
1.98 bowersj2 2705: $self->{ALREADY_SEEN}->{$_->{ID}} = 1;
1.104 bowersj2 2706: if ($maxDepthAdded < $depth) { $maxDepthAdded = $depth; }
1.98 bowersj2 2707: }
2708: }
1.104 bowersj2 2709:
2710: # Is this the end of a branch? If so, all of the resources examined above
2711: # led to lower levels then the one we are currently at, so we push a END_BRANCH
2712: # marker onto the stack so we don't forget.
2713: # Example: For the usual A(BC)(DE)F case, when the iterator goes down the
2714: # BC branch and gets to C, it will see F as the only next resource, but it's
2715: # one level lower. Thus, this is the end of the branch, since there are no
2716: # more resources added to this level or above.
1.111 bowersj2 2717: # We don't do this if the examined resource is the finish resource,
2718: # because the condition given above is true, but the "END_MAP" will
2719: # take care of things and we should already be at depth 0.
1.104 bowersj2 2720: my $isEndOfBranch = $maxDepthAdded < $self->{CURRENT_DEPTH};
1.111 bowersj2 2721: if ($isEndOfBranch && $here != $self->{FINISH_RESOURCE}) { # **9**
1.104 bowersj2 2722: push @{$self->{STACK}->[$self->{CURRENT_DEPTH}]}, END_BRANCH();
2723: }
2724:
1.98 bowersj2 2725: # That ends the main iterator logic. Now, do we want to recurse
2726: # down this map (if this resource is a map)?
1.256 albertel 2727: if ( ($self->{HERE}->is_sequence() || (!$closeAllPages && $self->{HERE}->is_page())) &&
1.98 bowersj2 2728: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
2729: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
2730: my $firstResource = $self->{HERE}->map_start();
2731: my $finishResource = $self->{HERE}->map_finish();
2732:
2733: $self->{RECURSIVE_ITERATOR} =
2734: Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
2735: $finishResource, $self->{FILTER},
2736: $self->{ALREADY_SEEN}, $self->{CONDITION});
2737: }
2738:
1.116 bowersj2 2739: # If this is a blank resource, don't actually return it.
1.117 bowersj2 2740: # Should you ever find you need it, make sure to add an option to the code
2741: # that you can use; other things depend on this behavior.
1.138 bowersj2 2742: my $browsePriv = $self->{HERE}->browsePriv();
2743: if (!$self->{HERE}->src() ||
2744: (!($browsePriv eq 'F') && !($browsePriv eq '2')) ) {
1.256 albertel 2745: return $self->next($closeAllPages);
1.116 bowersj2 2746: }
2747:
1.98 bowersj2 2748: return $self->{HERE};
2749:
2750: }
2751:
2752: =pod
2753:
1.174 albertel 2754: The other method available on the iterator is B<getStack>, which
2755: returns an array populated with the current 'stack' of maps, as
2756: references to the resource objects. Example: This is useful when
2757: making the navigation map, as we need to check whether we are under a
2758: page map to see if we need to link directly to the resource, or to the
2759: page. The first elements in the array will correspond to the top of
2760: the stack (most inclusive map).
1.98 bowersj2 2761:
2762: =cut
2763:
2764: sub getStack {
2765: my $self=shift;
2766:
2767: my @stack;
2768:
2769: $self->populateStack(\@stack);
2770:
2771: return \@stack;
2772: }
2773:
2774: # Private method: Calls the iterators recursively to populate the stack.
2775: sub populateStack {
2776: my $self=shift;
2777: my $stack = shift;
2778:
2779: push @$stack, $self->{HERE} if ($self->{HERE});
2780:
2781: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
2782: $self->{RECURSIVE_ITERATOR}->populateStack($stack);
2783: }
1.94 bowersj2 2784: }
2785:
2786: 1;
2787:
2788: package Apache::lonnavmaps::DFSiterator;
2789:
1.100 bowersj2 2790: # Not documented in the perldoc: This is a simple iterator that just walks
2791: # through the nav map and presents the resources in a depth-first search
2792: # fashion, ignorant of conditionals, randomized resources, etc. It presents
2793: # BEGIN_MAP and END_MAP, but does not understand branches at all. It is
2794: # useful for pre-processing of some kind, and is in fact used by the main
2795: # iterator that way, but that's about it.
2796: # One could imagine merging this into the init routine of the main iterator,
1.251 www 2797: # but this might as well be left separate, since it is possible some other
1.100 bowersj2 2798: # use might be found for it. - Jeremy
1.94 bowersj2 2799:
1.117 bowersj2 2800: # Unlike the main iterator, this DOES return all resources, even blank ones.
2801: # The main iterator needs them to correctly preprocess the map.
2802:
1.94 bowersj2 2803: sub BEGIN_MAP { return 1; } # begining of a new map
2804: sub END_MAP { return 2; } # end of the map
2805: sub FORWARD { return 1; } # go forward
2806: sub BACKWARD { return 2; }
2807:
1.100 bowersj2 2808: # Params: Nav map ref, first resource id/ref, finish resource id/ref,
2809: # filter hash ref (or undef), already seen hash or undef, condition
2810: # (as in main iterator), direction FORWARD or BACKWARD (undef->forward).
1.51 bowersj2 2811: sub new {
2812: # magic invocation to create a class instance
2813: my $proto = shift;
2814: my $class = ref($proto) || $proto;
2815: my $self = {};
2816:
2817: $self->{NAV_MAP} = shift;
2818: return undef unless ($self->{NAV_MAP});
2819:
2820: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
2821: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
2822:
2823: # If the given resources are just the ID of the resource, get the
2824: # objects
2825: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
2826: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
2827: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
2828: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
2829:
2830: $self->{FILTER} = shift;
2831:
2832: # A hash, used as a set, of resource already seen
2833: $self->{ALREADY_SEEN} = shift;
1.140 bowersj2 2834: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1.63 bowersj2 2835: $self->{CONDITION} = shift;
1.89 bowersj2 2836: $self->{DIRECTION} = shift || FORWARD();
1.51 bowersj2 2837:
1.100 bowersj2 2838: # Flag: Have we started yet?
1.51 bowersj2 2839: $self->{STARTED} = 0;
2840:
2841: # Should we continue calling the recursive iterator, if any?
2842: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2843: # The recursive iterator, if any
2844: $self->{RECURSIVE_ITERATOR} = undef;
2845: # Are we recursing on a map, or a branch?
2846: $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
2847: # And the count of how deep it is, so that this iterator can keep track of
2848: # when to pick back up again.
2849: $self->{RECURSIVE_DEPTH} = 0;
2850:
2851: # For keeping track of our branches, we maintain our own stack
1.100 bowersj2 2852: $self->{STACK} = [];
1.51 bowersj2 2853:
2854: # Start with the first resource
1.89 bowersj2 2855: if ($self->{DIRECTION} == FORWARD) {
1.100 bowersj2 2856: push @{$self->{STACK}}, $self->{FIRST_RESOURCE};
1.89 bowersj2 2857: } else {
1.100 bowersj2 2858: push @{$self->{STACK}}, $self->{FINISH_RESOURCE};
1.89 bowersj2 2859: }
1.51 bowersj2 2860:
2861: bless($self);
2862: return $self;
2863: }
2864:
2865: sub next {
2866: my $self = shift;
2867:
2868: # Are we using a recursive iterator? If so, pull from that and
2869: # watch the depth; we want to resume our level at the correct time.
1.98 bowersj2 2870: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1.51 bowersj2 2871: # grab the next from the recursive iterator
2872: my $next = $self->{RECURSIVE_ITERATOR}->next();
2873:
2874: # is it a begin or end map? Update depth if so
2875: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
2876: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
2877:
2878: # Are we back at depth 0? If so, stop recursing.
2879: if ($self->{RECURSIVE_DEPTH} == 0) {
2880: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2881: }
2882:
2883: return $next;
2884: }
2885:
2886: # Is there a current resource to grab? If not, then return
1.100 bowersj2 2887: # END_MAP, which will end the iterator.
2888: if (scalar(@{$self->{STACK}}) == 0) {
2889: return $self->END_MAP();
1.51 bowersj2 2890: }
2891:
2892: # Have we not yet begun? If not, return BEGIN_MAP and
2893: # remember that we've started.
2894: if ( !$self->{STARTED} ) {
2895: $self->{STARTED} = 1;
2896: return $self->BEGIN_MAP;
2897: }
2898:
2899: # Get the next resource in the branch
1.100 bowersj2 2900: $self->{HERE} = pop @{$self->{STACK}};
1.52 bowersj2 2901:
1.100 bowersj2 2902: # remember that we've seen this, so we don't return it again later
1.51 bowersj2 2903: $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
2904:
2905: # Get the next possible resources
1.90 bowersj2 2906: my $nextUnfiltered;
1.89 bowersj2 2907: if ($self->{DIRECTION} == FORWARD()) {
1.90 bowersj2 2908: $nextUnfiltered = $self->{HERE}->getNext();
1.89 bowersj2 2909: } else {
1.90 bowersj2 2910: $nextUnfiltered = $self->{HERE}->getPrevious();
1.89 bowersj2 2911: }
1.51 bowersj2 2912: my $next = [];
2913:
2914: # filter the next possibilities to remove things we've
1.100 bowersj2 2915: # already seen.
1.51 bowersj2 2916: foreach (@$nextUnfiltered) {
1.52 bowersj2 2917: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
2918: push @$next, $_;
2919: }
1.51 bowersj2 2920: }
2921:
2922: while (@$next) {
1.100 bowersj2 2923: # copy the next possibilities over to the stack
2924: push @{$self->{STACK}}, shift @$next;
1.51 bowersj2 2925: }
2926:
2927: # If this is a map and we want to recurse down it... (not filtered out)
1.70 bowersj2 2928: if ($self->{HERE}->is_map() &&
1.63 bowersj2 2929: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
1.51 bowersj2 2930: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
2931: my $firstResource = $self->{HERE}->map_start();
2932: my $finishResource = $self->{HERE}->map_finish();
2933:
2934: $self->{RECURSIVE_ITERATOR} =
1.94 bowersj2 2935: Apache::lonnavmaps::DFSiterator->new ($self->{NAV_MAP}, $firstResource,
1.63 bowersj2 2936: $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
1.91 bowersj2 2937: $self->{CONDITION}, $self->{DIRECTION});
1.51 bowersj2 2938: }
2939:
2940: return $self->{HERE};
1.190 bowersj2 2941: }
2942:
2943: # Identical to the full iterator methods of the same name. Hate to copy/paste
2944: # but I also hate to "inherit" either iterator from the other.
2945:
2946: sub getStack {
2947: my $self=shift;
2948:
2949: my @stack;
2950:
2951: $self->populateStack(\@stack);
2952:
2953: return \@stack;
2954: }
2955:
2956: # Private method: Calls the iterators recursively to populate the stack.
2957: sub populateStack {
2958: my $self=shift;
2959: my $stack = shift;
2960:
2961: push @$stack, $self->{HERE} if ($self->{HERE});
2962:
2963: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
2964: $self->{RECURSIVE_ITERATOR}->populateStack($stack);
2965: }
1.51 bowersj2 2966: }
2967:
1.1 www 2968: 1;
1.2 www 2969:
1.51 bowersj2 2970: package Apache::lonnavmaps::resource;
2971:
2972: use Apache::lonnet;
2973:
2974: =pod
2975:
1.217 bowersj2 2976: =head1 Object: resource
1.51 bowersj2 2977:
1.217 bowersj2 2978: X<resource, navmap object>
1.174 albertel 2979: A resource object encapsulates a resource in a resource map, allowing
2980: easy manipulation of the resource, querying the properties of the
2981: resource (including user properties), and represents a reference that
2982: can be used as the canonical representation of the resource by
2983: lonnavmap clients like renderers.
2984:
2985: A resource only makes sense in the context of a navmap, as some of the
2986: data is stored in the navmap object.
2987:
2988: You will probably never need to instantiate this object directly. Use
2989: Apache::lonnavmaps::navmap, and use the "start" method to obtain the
2990: starting resource.
1.51 bowersj2 2991:
1.188 bowersj2 2992: Resource objects respect the parameter_hiddenparts, which suppresses
2993: various parts according to the wishes of the map author. As of this
2994: writing, there is no way to override this parameter, and suppressed
2995: parts will never be returned, nor will their response types or ids be
2996: stored.
2997:
1.217 bowersj2 2998: =head2 Overview
1.51 bowersj2 2999:
1.217 bowersj2 3000: A B<Resource> is the most granular type of object in LON-CAPA that can
3001: be included in a course. It can either be a particular resource, like
3002: an HTML page, external resource, problem, etc., or it can be a
3003: container sequence, such as a "page" or a "map".
3004:
3005: To see a sequence from the user's point of view, please see the
3006: B<Creating a Course: Maps and Sequences> chapter of the Author's
3007: Manual.
3008:
3009: A Resource Object, once obtained from a navmap object via a B<getBy*>
3010: method of the navmap, or from an iterator, allows you to query
3011: information about that resource.
3012:
3013: Generally, you do not ever want to create a resource object yourself,
3014: so creation has been left undocumented. Always retrieve resources
3015: from navmap objects.
3016:
3017: =head3 Identifying Resources
3018:
3019: X<big hash>Every resource is identified by a Resource ID in the big hash that is
3020: unique to that resource for a given course. X<resource ID, in big hash>
3021: The Resource ID has the form #.#, where the first number is the same
3022: for every resource in a map, and the second is unique. For instance,
3023: for a course laid out like this:
3024:
3025: * Problem 1
3026: * Map
3027: * Resource 2
3028: * Resource 3
3029:
3030: C<Problem 1> and C<Map> will share a first number, and C<Resource 2>
3031: C<Resource 3> will share a first number. The second number may end up
3032: re-used between the two groups.
3033:
3034: The resource ID is only used in the big hash, but can be used in the
3035: context of a course to identify a resource easily. (For instance, the
3036: printing system uses it to record which resources from a sequence you
3037: wish to print.)
3038:
3039: X<symb> X<resource, symb>
3040: All resources also have B<symb>s, which uniquely identify a resource
3041: in a course. Many internal LON-CAPA functions expect a symb. A symb
3042: carries along with it the URL of the resource, and the map it appears
3043: in. Symbs are much larger then resource IDs.
1.51 bowersj2 3044:
3045: =cut
3046:
3047: sub new {
3048: # magic invocation to create a class instance
3049: my $proto = shift;
3050: my $class = ref($proto) || $proto;
3051: my $self = {};
3052:
3053: $self->{NAV_MAP} = shift;
3054: $self->{ID} = shift;
3055:
3056: # Store this new resource in the parent nav map's cache.
3057: $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
1.66 bowersj2 3058: $self->{RESOURCE_ERROR} = 0;
1.51 bowersj2 3059:
3060: # A hash that can be used by two-pass algorithms to store data
3061: # about this resource in. Not used by the resource object
3062: # directly.
3063: $self->{DATA} = {};
3064:
3065: bless($self);
3066:
3067: return $self;
3068: }
3069:
1.70 bowersj2 3070: # private function: simplify the NAV_HASH lookups we keep doing
3071: # pass the name, and to automatically append my ID, pass a true val on the
3072: # second param
3073: sub navHash {
3074: my $self = shift;
3075: my $param = shift;
3076: my $id = shift;
1.115 bowersj2 3077: return $self->{NAV_MAP}->navhash($param . ($id?$self->{ID}:""));
1.70 bowersj2 3078: }
3079:
1.51 bowersj2 3080: =pod
3081:
1.217 bowersj2 3082: =head2 Methods
1.70 bowersj2 3083:
1.217 bowersj2 3084: Once you have a resource object, here's what you can do with it:
3085:
3086: =head3 Attribute Retrieval
3087:
3088: Every resource has certain attributes that can be retrieved and used:
1.70 bowersj2 3089:
3090: =over 4
3091:
1.217 bowersj2 3092: =item * B<ID>: Every resource has an ID that is unique for that
3093: resource in the course it is in. The ID is actually in the hash
3094: representing the resource, so for a resource object $res, obtain
3095: it via C<$res->{ID}).
3096:
1.174 albertel 3097: =item * B<compTitle>:
3098:
3099: Returns a "composite title", that is equal to $res->title() if the
3100: resource has a title, and is otherwise the last part of the URL (e.g.,
3101: "problem.problem").
3102:
3103: =item * B<ext>:
3104:
3105: Returns true if the resource is external.
3106:
3107: =item * B<kind>:
3108:
3109: Returns the kind of the resource from the compiled nav map.
3110:
3111: =item * B<randomout>:
1.106 bowersj2 3112:
1.174 albertel 3113: Returns true if this resource was chosen to NOT be shown to the user
3114: by the random map selection feature. In other words, this is usually
3115: false.
1.70 bowersj2 3116:
1.174 albertel 3117: =item * B<randompick>:
1.70 bowersj2 3118:
1.174 albertel 3119: Returns true for a map if the randompick feature is being used on the
3120: map. (?)
1.70 bowersj2 3121:
1.174 albertel 3122: =item * B<src>:
1.51 bowersj2 3123:
1.174 albertel 3124: Returns the source for the resource.
1.70 bowersj2 3125:
1.174 albertel 3126: =item * B<symb>:
1.70 bowersj2 3127:
1.174 albertel 3128: Returns the symb for the resource.
1.70 bowersj2 3129:
1.174 albertel 3130: =item * B<title>:
1.70 bowersj2 3131:
1.174 albertel 3132: Returns the title of the resource.
3133:
1.51 bowersj2 3134: =back
3135:
3136: =cut
3137:
3138: # These info functions can be used directly, as they don't return
3139: # resource information.
1.85 bowersj2 3140: sub comesfrom { my $self=shift; return $self->navHash("comesfrom_", 1); }
1.70 bowersj2 3141: sub ext { my $self=shift; return $self->navHash("ext_", 1) eq 'true:'; }
1.85 bowersj2 3142: sub from { my $self=shift; return $self->navHash("from_", 1); }
1.217 bowersj2 3143: # considered private and undocumented
1.51 bowersj2 3144: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
3145: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
1.68 bowersj2 3146: sub randomout { my $self=shift; return $self->navHash("randomout_", 1); }
3147: sub randompick {
3148: my $self = shift;
3149: return $self->{NAV_MAP}->{PARM_HASH}->{$self->symb .
3150: '.0.parameter_randompick'};
3151: }
1.51 bowersj2 3152: sub src {
3153: my $self=shift;
3154: return $self->navHash("src_", 1);
3155: }
3156: sub symb {
3157: my $self=shift;
3158: (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
3159: my $symbSrc = &Apache::lonnet::declutter($self->src());
1.223 albertel 3160: my $symb = &Apache::lonnet::declutter($self->navHash('map_id_'.$first))
1.51 bowersj2 3161: . '___' . $second . '___' . $symbSrc;
1.223 albertel 3162: return &Apache::lonnet::symbclean($symb);
1.51 bowersj2 3163: }
1.213 bowersj2 3164: sub title {
3165: my $self=shift;
3166: if ($self->{ID} eq '0.0') {
3167: # If this is the top-level map, return the title of the course
3168: # since this map can not be titled otherwise.
3169: return $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
3170: }
3171: return $self->navHash("title_", 1); }
1.217 bowersj2 3172: # considered private and undocumented
1.70 bowersj2 3173: sub to { my $self=shift; return $self->navHash("to_", 1); }
1.106 bowersj2 3174: sub compTitle {
3175: my $self = shift;
3176: my $title = $self->title();
1.176 www 3177: $title=~s/\&colon\;/\:/gs;
1.106 bowersj2 3178: if (!$title) {
3179: $title = $self->src();
3180: $title = substr($title, rindex($title, '/') + 1);
3181: }
3182: return $title;
3183: }
1.70 bowersj2 3184: =pod
3185:
3186: B<Predicate Testing the Resource>
3187:
3188: These methods are shortcuts to deciding if a given resource has a given property.
3189:
3190: =over 4
3191:
1.174 albertel 3192: =item * B<is_map>:
3193:
3194: Returns true if the resource is a map type.
3195:
3196: =item * B<is_problem>:
1.70 bowersj2 3197:
1.174 albertel 3198: Returns true if the resource is a problem type, false
3199: otherwise. (Looks at the extension on the src field; might need more
3200: to work correctly.)
1.70 bowersj2 3201:
1.174 albertel 3202: =item * B<is_page>:
1.70 bowersj2 3203:
1.174 albertel 3204: Returns true if the resource is a page.
3205:
3206: =item * B<is_sequence>:
3207:
3208: Returns true if the resource is a sequence.
1.70 bowersj2 3209:
3210: =back
3211:
3212: =cut
3213:
1.256 albertel 3214: sub hasResource {
3215: my $self = shift;
3216: return $self->{NAV_MAP}->hasResource(@_);
3217: }
3218:
3219: sub retrieveResources {
3220: my $self = shift;
3221: return $self->{NAV_MAP}->retrieveResources(@_);
3222: }
1.70 bowersj2 3223:
3224: sub is_html {
1.51 bowersj2 3225: my $self=shift;
3226: my $src = $self->src();
1.70 bowersj2 3227: return ($src =~ /html$/);
1.51 bowersj2 3228: }
1.70 bowersj2 3229: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
3230: sub is_page {
1.51 bowersj2 3231: my $self=shift;
3232: my $src = $self->src();
1.205 bowersj2 3233: return $self->navHash("is_map_", 1) &&
3234: $self->navHash("map_type_" . $self->map_pc()) eq 'page';
1.51 bowersj2 3235: }
1.70 bowersj2 3236: sub is_problem {
1.51 bowersj2 3237: my $self=shift;
3238: my $src = $self->src();
1.256 albertel 3239: return ($src =~ /\.(problem|exam|quiz|assess|survey|form|library)$/)
3240: }
3241: sub contains_problem {
3242: my $self=shift;
3243: if ($self->is_page()) {
3244: my $hasProblem=$self->hasResource($self,sub { $_[0]->is_problem() },1);
3245: return $hasProblem;
3246: }
3247: return 0;
1.51 bowersj2 3248: }
1.70 bowersj2 3249: sub is_sequence {
1.51 bowersj2 3250: my $self=shift;
3251: my $src = $self->src();
1.205 bowersj2 3252: return $self->navHash("is_map_", 1) &&
3253: $self->navHash("map_type_" . $self->map_pc()) eq 'sequence';
1.51 bowersj2 3254: }
1.261 matthew 3255: sub is_survey {
3256: my $self = shift();
3257: my $part = shift();
1.263 albertel 3258: if ($self->parmval('type',$part) eq 'survey') {
1.261 matthew 3259: return 1;
3260: }
1.263 albertel 3261: if ($self->src() =~ /\.(survey)$/) {
1.261 matthew 3262: return 1;
3263: }
3264: return 0;
3265: }
1.51 bowersj2 3266:
1.266 raeburn 3267: sub is_empty_sequence {
3268: my $self=shift;
3269: my $src = $self->src();
3270: return !$self->is_page() && $self->navHash("is_map_", 1) && !$self->navHash("map_type_" . $self->map_pc());
3271: }
3272:
1.101 bowersj2 3273: # Private method: Shells out to the parmval in the nav map, handler parts.
1.51 bowersj2 3274: sub parmval {
3275: my $self = shift;
3276: my $what = shift;
1.185 bowersj2 3277: my $part = shift;
3278: if (!defined($part)) {
3279: $part = '0';
3280: }
1.51 bowersj2 3281: return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
3282: }
3283:
1.70 bowersj2 3284: =pod
3285:
3286: B<Map Methods>
3287:
1.174 albertel 3288: These methods are useful for getting information about the map
3289: properties of the resource, if the resource is a map (B<is_map>).
1.70 bowersj2 3290:
3291: =over 4
3292:
1.174 albertel 3293: =item * B<map_finish>:
3294:
3295: Returns a reference to a resource object corresponding to the finish
3296: resource of the map.
1.70 bowersj2 3297:
1.174 albertel 3298: =item * B<map_pc>:
1.70 bowersj2 3299:
1.174 albertel 3300: Returns the pc value of the map, which is the first number that
3301: appears in the resource ID of the resources in the map, and is the
3302: number that appears around the middle of the symbs of the resources in
3303: that map.
1.70 bowersj2 3304:
1.174 albertel 3305: =item * B<map_start>:
3306:
3307: Returns a reference to a resource object corresponding to the start
3308: resource of the map.
3309:
3310: =item * B<map_type>:
3311:
3312: Returns a string with the type of the map in it.
1.70 bowersj2 3313:
3314: =back
1.51 bowersj2 3315:
1.70 bowersj2 3316: =cut
1.51 bowersj2 3317:
3318: sub map_finish {
3319: my $self = shift;
3320: my $src = $self->src();
1.144 bowersj2 3321: $src = Apache::lonnet::clutter($src);
1.51 bowersj2 3322: my $res = $self->navHash("map_finish_$src", 0);
3323: $res = $self->{NAV_MAP}->getById($res);
3324: return $res;
3325: }
1.70 bowersj2 3326: sub map_pc {
3327: my $self = shift;
3328: my $src = $self->src();
3329: return $self->navHash("map_pc_$src", 0);
3330: }
1.51 bowersj2 3331: sub map_start {
3332: my $self = shift;
3333: my $src = $self->src();
1.144 bowersj2 3334: $src = Apache::lonnet::clutter($src);
1.51 bowersj2 3335: my $res = $self->navHash("map_start_$src", 0);
3336: $res = $self->{NAV_MAP}->getById($res);
3337: return $res;
3338: }
3339: sub map_type {
3340: my $self = shift;
3341: my $pc = $self->map_pc();
3342: return $self->navHash("map_type_$pc", 0);
3343: }
3344:
3345: #####
3346: # Property queries
3347: #####
3348:
3349: # These functions will be responsible for returning the CORRECT
3350: # VALUE for the parameter, no matter what. So while they may look
3351: # like direct calls to parmval, they can be more then that.
3352: # So, for instance, the duedate function should use the "duedatetype"
3353: # information, rather then the resource object user.
3354:
3355: =pod
3356:
3357: =head2 Resource Parameters
3358:
1.174 albertel 3359: In order to use the resource parameters correctly, the nav map must
3360: have been instantiated with genCourseAndUserOptions set to true, so
3361: the courseopt and useropt is read correctly. Then, you can call these
3362: functions to get the relevant parameters for the resource. Each
3363: function defaults to part "0", but can be directed to another part by
3364: passing the part as the parameter.
3365:
3366: These methods are responsible for getting the parameter correct, not
3367: merely reflecting the contents of the GDBM hashes. As we move towards
3368: dates relative to other dates, these methods should be updated to
3369: reflect that. (Then, anybody using these methods will not have to update
3370: their code.)
3371:
3372: =over 4
3373:
3374: =item * B<acc>:
3375:
3376: Get the Client IP/Name Access Control information.
1.51 bowersj2 3377:
1.174 albertel 3378: =item * B<answerdate>:
1.51 bowersj2 3379:
1.174 albertel 3380: Get the answer-reveal date for the problem.
3381:
1.211 bowersj2 3382: =item * B<awarded>:
3383:
3384: Gets the awarded value for the problem part. Requires genUserData set to
3385: true when the navmap object was created.
3386:
1.174 albertel 3387: =item * B<duedate>:
3388:
3389: Get the due date for the problem.
3390:
3391: =item * B<tries>:
3392:
3393: Get the number of tries the student has used on the problem.
3394:
3395: =item * B<maxtries>:
3396:
3397: Get the number of max tries allowed.
3398:
3399: =item * B<opendate>:
1.51 bowersj2 3400:
1.174 albertel 3401: Get the open date for the problem.
1.51 bowersj2 3402:
1.174 albertel 3403: =item * B<sig>:
1.51 bowersj2 3404:
1.174 albertel 3405: Get the significant figures setting.
1.51 bowersj2 3406:
1.174 albertel 3407: =item * B<tol>:
1.51 bowersj2 3408:
1.174 albertel 3409: Get the tolerance for the problem.
1.51 bowersj2 3410:
1.174 albertel 3411: =item * B<tries>:
1.51 bowersj2 3412:
1.174 albertel 3413: Get the number of tries the user has already used on the problem.
1.51 bowersj2 3414:
1.174 albertel 3415: =item * B<type>:
1.51 bowersj2 3416:
1.174 albertel 3417: Get the question type for the problem.
1.70 bowersj2 3418:
1.174 albertel 3419: =item * B<weight>:
1.51 bowersj2 3420:
1.174 albertel 3421: Get the weight for the problem.
1.51 bowersj2 3422:
3423: =back
3424:
3425: =cut
3426:
3427: sub acc {
3428: (my $self, my $part) = @_;
3429: return $self->parmval("acc", $part);
3430: }
3431: sub answerdate {
3432: (my $self, my $part) = @_;
3433: # Handle intervals
3434: if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
3435: return $self->duedate($part) +
3436: $self->parmval("answerdate", $part);
3437: }
3438: return $self->parmval("answerdate", $part);
1.106 bowersj2 3439: }
1.211 bowersj2 3440: sub awarded {
3441: my $self = shift; my $part = shift;
1.221 bowersj2 3442: $self->{NAV_MAP}->get_user_data();
1.211 bowersj2 3443: if (!defined($part)) { $part = '0'; }
3444: return $self->{NAV_MAP}->{STUDENT_DATA}->{$self->symb()}->{'resource.'.$part.'.awarded'};
3445: }
1.51 bowersj2 3446: sub duedate {
3447: (my $self, my $part) = @_;
1.260 albertel 3448: my $interval=$self->parmval("interval", $part);
3449: if ($interval) {
3450: my $first_access=&Apache::lonnet::get_first_access('map',$self->symb);
3451: if ($first_access) { return ($first_access+$interval); }
3452: }
1.51 bowersj2 3453: return $self->parmval("duedate", $part);
3454: }
3455: sub maxtries {
3456: (my $self, my $part) = @_;
3457: return $self->parmval("maxtries", $part);
3458: }
3459: sub opendate {
3460: (my $self, my $part) = @_;
3461: if ($self->parmval("opendate.type", $part) eq 'date_interval') {
3462: return $self->duedate($part) -
3463: $self->parmval("opendate", $part);
3464: }
3465: return $self->parmval("opendate");
3466: }
1.185 bowersj2 3467: sub problemstatus {
3468: (my $self, my $part) = @_;
1.236 bowersj2 3469: return lc $self->parmval("problemstatus", $part);
1.185 bowersj2 3470: }
1.51 bowersj2 3471: sub sig {
3472: (my $self, my $part) = @_;
3473: return $self->parmval("sig", $part);
3474: }
3475: sub tol {
3476: (my $self, my $part) = @_;
3477: return $self->parmval("tol", $part);
3478: }
1.108 bowersj2 3479: sub tries {
3480: my $self = shift;
3481: my $tries = $self->queryRestoreHash('tries', shift);
3482: if (!defined($tries)) { return '0';}
1.51 bowersj2 3483: return $tries;
3484: }
1.70 bowersj2 3485: sub type {
3486: (my $self, my $part) = @_;
3487: return $self->parmval("type", $part);
3488: }
1.109 bowersj2 3489: sub weight {
3490: my $self = shift; my $part = shift;
1.211 bowersj2 3491: if (!defined($part)) { $part = '0'; }
3492: return &Apache::lonnet::EXT('resource.'.$part.'.weight',
3493: $self->symb(), $ENV{'user.domain'},
3494: $ENV{'user.name'},
3495: $ENV{'request.course.sec'});
3496:
1.70 bowersj2 3497: }
1.51 bowersj2 3498:
3499: # Multiple things need this
3500: sub getReturnHash {
3501: my $self = shift;
3502:
3503: if (!defined($self->{RETURN_HASH})) {
1.84 bowersj2 3504: my %tmpHash = &Apache::lonnet::restore($self->symb());
1.51 bowersj2 3505: $self->{RETURN_HASH} = \%tmpHash;
3506: }
3507: }
3508:
3509: ######
3510: # Status queries
3511: ######
3512:
3513: # These methods query the status of problems.
3514:
3515: # If we need to count parts, this function determines the number of
3516: # parts from the metadata. When called, it returns a reference to a list
3517: # of strings corresponding to the parts. (Thus, using it in a scalar context
3518: # tells you how many parts you have in the problem:
3519: # $partcount = scalar($resource->countParts());
3520: # Don't use $self->{PARTS} directly because you don't know if it's been
3521: # computed yet.
3522:
3523: =pod
3524:
3525: =head2 Resource misc
3526:
3527: Misc. functions for the resource.
3528:
3529: =over 4
3530:
1.174 albertel 3531: =item * B<hasDiscussion>:
1.59 bowersj2 3532:
1.174 albertel 3533: Returns a false value if there has been discussion since the user last
3534: logged in, true if there has. Always returns false if the discussion
3535: data was not extracted when the nav map was constructed.
3536:
3537: =item * B<getFeedback>:
3538:
3539: Gets the feedback for the resource and returns the raw feedback string
3540: for the resource, or the null string if there is no feedback or the
3541: email data was not extracted when the nav map was constructed. Usually
3542: used like this:
1.59 bowersj2 3543:
3544: for (split(/\,/, $res->getFeedback())) {
3545: my $link = &Apache::lonnet::escape($_);
3546: ...
3547:
3548: and use the link as appropriate.
3549:
3550: =cut
3551:
3552: sub hasDiscussion {
3553: my $self = shift;
3554: return $self->{NAV_MAP}->hasDiscussion($self->symb());
3555: }
3556:
3557: sub getFeedback {
3558: my $self = shift;
1.124 bowersj2 3559: my $source = $self->src();
1.125 bowersj2 3560: if ($source =~ /^\/res\//) { $source = substr $source, 5; }
1.124 bowersj2 3561: return $self->{NAV_MAP}->getFeedback($source);
3562: }
3563:
3564: sub getErrors {
3565: my $self = shift;
3566: my $source = $self->src();
3567: if ($source =~ /^\/res\//) { $source = substr $source, 5; }
3568: return $self->{NAV_MAP}->getErrors($source);
1.59 bowersj2 3569: }
3570:
3571: =pod
3572:
1.174 albertel 3573: =item * B<parts>():
1.51 bowersj2 3574:
1.174 albertel 3575: Returns a list reference containing sorted strings corresponding to
1.193 bowersj2 3576: each part of the problem. Single part problems have only a part '0'.
3577: Multipart problems do not return their part '0', since they typically
3578: do not really matter.
1.174 albertel 3579:
3580: =item * B<countParts>():
3581:
3582: Returns the number of parts of the problem a student can answer. Thus,
3583: for single part problems, returns 1. For multipart, it returns the
1.193 bowersj2 3584: number of parts in the problem, not including psuedo-part 0.
1.51 bowersj2 3585:
1.192 bowersj2 3586: =item * B<multipart>():
3587:
1.193 bowersj2 3588: Returns true if the problem is multipart, false otherwise. Use this instead
3589: of countParts if all you want is multipart/not multipart.
1.192 bowersj2 3590:
1.187 bowersj2 3591: =item * B<responseType>($part):
3592:
3593: Returns the response type of the part, without the word "response" on the
3594: end. Example return values: 'string', 'essay', 'numeric', etc.
3595:
1.193 bowersj2 3596: =item * B<responseIds>($part):
1.187 bowersj2 3597:
1.193 bowersj2 3598: Retreives the response IDs for the given part as an array reference containing
3599: strings naming the response IDs. This may be empty.
1.187 bowersj2 3600:
1.51 bowersj2 3601: =back
3602:
3603: =cut
3604:
3605: sub parts {
3606: my $self = shift;
3607:
1.192 bowersj2 3608: if ($self->ext) { return []; }
1.67 bowersj2 3609:
1.51 bowersj2 3610: $self->extractParts();
3611: return $self->{PARTS};
3612: }
3613:
3614: sub countParts {
3615: my $self = shift;
3616:
3617: my $parts = $self->parts();
1.191 bowersj2 3618:
3619: # If I left this here, then it's not necessary.
3620: #my $delta = 0;
3621: #for my $part (@$parts) {
3622: # if ($part eq '0') { $delta--; }
3623: #}
1.66 bowersj2 3624:
3625: if ($self->{RESOURCE_ERROR}) {
3626: return 0;
3627: }
3628:
1.191 bowersj2 3629: return scalar(@{$parts}); # + $delta;
1.192 bowersj2 3630: }
3631:
3632: sub multipart {
3633: my $self = shift;
3634: return $self->countParts() > 1;
1.51 bowersj2 3635: }
3636:
1.225 bowersj2 3637: sub singlepart {
3638: my $self = shift;
3639: return $self->countParts() == 1;
3640: }
3641:
1.187 bowersj2 3642: sub responseType {
1.184 bowersj2 3643: my $self = shift;
3644: my $part = shift;
3645:
3646: $self->extractParts();
1.235 albertel 3647: if (defined($self->{RESPONSE_TYPES}->{$part})) {
3648: return @{$self->{RESPONSE_TYPES}->{$part}};
3649: } else {
3650: return undef;
3651: }
1.187 bowersj2 3652: }
3653:
1.193 bowersj2 3654: sub responseIds {
1.187 bowersj2 3655: my $self = shift;
3656: my $part = shift;
3657:
3658: $self->extractParts();
1.235 albertel 3659: if (defined($self->{RESPONSE_IDS}->{$part})) {
3660: return @{$self->{RESPONSE_IDS}->{$part}};
3661: } else {
3662: return undef;
3663: }
1.184 bowersj2 3664: }
3665:
3666: # Private function: Extracts the parts information, both part names and
1.187 bowersj2 3667: # part types, and saves it.
1.51 bowersj2 3668: sub extractParts {
3669: my $self = shift;
3670:
1.153 bowersj2 3671: return if (defined($self->{PARTS}));
1.67 bowersj2 3672: return if ($self->ext);
1.51 bowersj2 3673:
3674: $self->{PARTS} = [];
3675:
1.181 bowersj2 3676: my %parts;
3677:
1.82 bowersj2 3678: # Retrieve part count, if this is a problem
3679: if ($self->is_problem()) {
1.240 albertel 3680: my $partorder = &Apache::lonnet::metadata($self->src(), 'partorder');
1.152 matthew 3681: my $metadata = &Apache::lonnet::metadata($self->src(), 'packages');
1.181 bowersj2 3682:
1.239 bowersj2 3683: if ($partorder) {
3684: my @parts;
3685: for my $part (split (/,/,$partorder)) {
3686: if (!Apache::loncommon::check_if_partid_hidden($part, $self->symb())) {
3687: push @parts, $part;
1.241 albertel 3688: $parts{$part} = 1;
1.239 bowersj2 3689: }
3690: }
3691: $self->{PARTS} = \@parts;
3692: } else {
3693: if (!$metadata) {
3694: $self->{RESOURCE_ERROR} = 1;
3695: $self->{PARTS} = [];
3696: $self->{PART_TYPE} = {};
3697: return;
3698: }
3699: foreach (split(/\,/,$metadata)) {
3700: if ($_ =~ /^part_(.*)$/) {
3701: my $part = $1;
3702: # This floods the logs if it blows up
3703: if (defined($parts{$part})) {
1.241 albertel 3704: &Apache::lonnet::logthis("$part multiply defined in metadata for " . $self->symb());
3705: }
1.239 bowersj2 3706:
3707: # check to see if part is turned off.
3708:
3709: if (!Apache::loncommon::check_if_partid_hidden($part, $self->symb())) {
3710: $parts{$part} = 1;
3711: }
3712: }
3713: }
3714: my @sortedParts = sort keys %parts;
3715: $self->{PARTS} = \@sortedParts;
1.51 bowersj2 3716: }
1.82 bowersj2 3717:
1.187 bowersj2 3718:
3719: my %responseIdHash;
3720: my %responseTypeHash;
3721:
1.189 bowersj2 3722:
3723: # Init the responseIdHash
3724: foreach (@{$self->{PARTS}}) {
3725: $responseIdHash{$_} = [];
3726: }
3727:
1.187 bowersj2 3728: # Now, the unfortunate thing about this is that parts, part name, and
1.239 bowersj2 3729: # response id are delimited by underscores, but both the part
1.187 bowersj2 3730: # name and response id can themselves have underscores in them.
3731: # So we have to use our knowlege of part names to figure out
3732: # where the part names begin and end, and even then, it is possible
3733: # to construct ambiguous situations.
3734: foreach (split /,/, $metadata) {
3735: if ($_ =~ /^([a-zA-Z]+)response_(.*)/) {
3736: my $responseType = $1;
3737: my $partStuff = $2;
3738: my $partIdSoFar = '';
3739: my @partChunks = split /_/, $partStuff;
3740: my $i = 0;
3741: for ($i = 0; $i < scalar(@partChunks); $i++) {
3742: if ($partIdSoFar) { $partIdSoFar .= '_'; }
3743: $partIdSoFar .= $partChunks[$i];
3744: if ($parts{$partIdSoFar}) {
3745: my @otherChunks = @partChunks[$i+1..$#partChunks];
3746: my $responseId = join('_', @otherChunks);
1.188 bowersj2 3747: push @{$responseIdHash{$partIdSoFar}}, $responseId;
1.233 matthew 3748: push @{$responseTypeHash{$partIdSoFar}}, $responseType;
1.187 bowersj2 3749: }
3750: }
3751: }
3752: }
1.264 albertel 3753: my $resorder = &Apache::lonnet::metadata($self->src(),'responseorder');
3754: if ($resorder) {
3755: my @resorder=split(/,/,$resorder);
3756: foreach my $part (keys(%responseIdHash)) {
3757: my %resids = map { ($_,1) } @{ $responseIdHash{$part} };
3758: my @neworder;
3759: foreach my $possibleid (@resorder) {
3760: if (exists($resids{$possibleid})) {
3761: push(@neworder,$possibleid);
3762: }
3763: }
3764: $responseIdHash{$part}=\@neworder;
3765: }
3766: }
1.187 bowersj2 3767: $self->{RESPONSE_IDS} = \%responseIdHash;
3768: $self->{RESPONSE_TYPES} = \%responseTypeHash;
1.154 bowersj2 3769: }
3770:
1.51 bowersj2 3771: return;
3772: }
3773:
3774: =pod
3775:
3776: =head2 Resource Status
3777:
1.174 albertel 3778: Problem resources have status information, reflecting their various
3779: dates and completion statuses.
1.51 bowersj2 3780:
1.174 albertel 3781: There are two aspects to the status: the date-related information and
3782: the completion information.
1.51 bowersj2 3783:
1.174 albertel 3784: Idiomatic usage of these two methods would probably look something
3785: like
1.51 bowersj2 3786:
3787: foreach ($resource->parts()) {
3788: my $dateStatus = $resource->getDateStatus($_);
3789: my $completionStatus = $resource->getCompletionStatus($_);
3790:
1.70 bowersj2 3791: or
3792:
3793: my $status = $resource->status($_);
3794:
1.51 bowersj2 3795: ... use it here ...
3796: }
3797:
1.174 albertel 3798: Which you use depends on exactly what you are looking for. The
3799: status() function has been optimized for the nav maps display and may
3800: not precisely match what you need elsewhere.
1.101 bowersj2 3801:
1.174 albertel 3802: The symbolic constants shown below can be accessed through the
3803: resource object: C<$res->OPEN>.
1.101 bowersj2 3804:
1.51 bowersj2 3805: =over 4
3806:
1.174 albertel 3807: =item * B<getDateStatus>($part):
3808:
3809: ($part defaults to 0). A convenience function that returns a symbolic
3810: constant telling you about the date status of the part. The possible
3811: return values are:
1.51 bowersj2 3812:
3813: =back
3814:
3815: B<Date Codes>
3816:
3817: =over 4
3818:
1.174 albertel 3819: =item * B<OPEN_LATER>:
3820:
3821: The problem will be opened later.
3822:
3823: =item * B<OPEN>:
3824:
3825: Open and not yet due.
3826:
1.51 bowersj2 3827:
1.174 albertel 3828: =item * B<PAST_DUE_ANSWER_LATER>:
1.51 bowersj2 3829:
1.174 albertel 3830: The due date has passed, but the answer date has not yet arrived.
1.59 bowersj2 3831:
1.174 albertel 3832: =item * B<PAST_DUE_NO_ANSWER>:
1.54 bowersj2 3833:
1.174 albertel 3834: The due date has passed and there is no answer opening date set.
1.51 bowersj2 3835:
1.174 albertel 3836: =item * B<ANSWER_OPEN>:
1.51 bowersj2 3837:
1.174 albertel 3838: The answer date is here.
3839:
3840: =item * B<NETWORK_FAILURE>:
3841:
3842: The information is unknown due to network failure.
1.51 bowersj2 3843:
3844: =back
3845:
3846: =cut
3847:
3848: # Apparently the compiler optimizes these into constants automatically
1.54 bowersj2 3849: sub OPEN_LATER { return 0; }
3850: sub OPEN { return 1; }
3851: sub PAST_DUE_NO_ANSWER { return 2; }
3852: sub PAST_DUE_ANSWER_LATER { return 3; }
3853: sub ANSWER_OPEN { return 4; }
3854: sub NOTHING_SET { return 5; }
3855: sub NETWORK_FAILURE { return 100; }
3856:
3857: # getDateStatus gets the date status for a given problem part.
3858: # Because answer date, due date, and open date are fully independent
3859: # (i.e., it is perfectly possible to *only* have an answer date),
3860: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
3861: # (past, future, none given). This function handles this with a decision
3862: # tree. Read the comments to follow the decision tree.
1.51 bowersj2 3863:
3864: sub getDateStatus {
3865: my $self = shift;
3866: my $part = shift;
3867: $part = "0" if (!defined($part));
1.54 bowersj2 3868:
3869: # Always return network failure if there was one.
1.51 bowersj2 3870: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
3871:
3872: my $now = time();
3873:
1.53 bowersj2 3874: my $open = $self->opendate($part);
3875: my $due = $self->duedate($part);
3876: my $answer = $self->answerdate($part);
3877:
3878: if (!$open && !$due && !$answer) {
3879: # no data on the problem at all
3880: # should this be the same as "open later"? think multipart.
3881: return $self->NOTHING_SET;
3882: }
1.59 bowersj2 3883: if (!$open || $now < $open) {return $self->OPEN_LATER}
3884: if (!$due || $now < $due) {return $self->OPEN}
3885: if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
3886: if ($answer) { return $self->ANSWER_OPEN; }
1.54 bowersj2 3887: return PAST_DUE_NO_ANSWER;
1.51 bowersj2 3888: }
3889:
3890: =pod
3891:
3892: B<>
3893:
3894: =over 4
3895:
1.174 albertel 3896: =item * B<getCompletionStatus>($part):
1.51 bowersj2 3897:
1.174 albertel 3898: ($part defaults to 0.) A convenience function that returns a symbolic
3899: constant telling you about the completion status of the part, with the
3900: following possible results:
3901:
3902: =back
1.51 bowersj2 3903:
3904: B<Completion Codes>
3905:
3906: =over 4
3907:
1.174 albertel 3908: =item * B<NOT_ATTEMPTED>:
3909:
3910: Has not been attempted at all.
3911:
3912: =item * B<INCORRECT>:
3913:
3914: Attempted, but wrong by student.
1.51 bowersj2 3915:
1.174 albertel 3916: =item * B<INCORRECT_BY_OVERRIDE>:
1.51 bowersj2 3917:
1.174 albertel 3918: Attempted, but wrong by instructor override.
1.51 bowersj2 3919:
1.174 albertel 3920: =item * B<CORRECT>:
1.51 bowersj2 3921:
1.174 albertel 3922: Correct or correct by instructor.
1.51 bowersj2 3923:
1.174 albertel 3924: =item * B<CORRECT_BY_OVERRIDE>:
1.51 bowersj2 3925:
1.174 albertel 3926: Correct by instructor override.
1.51 bowersj2 3927:
1.174 albertel 3928: =item * B<EXCUSED>:
3929:
3930: Excused. Not yet implemented.
3931:
3932: =item * B<NETWORK_FAILURE>:
3933:
3934: Information not available due to network failure.
3935:
3936: =item * B<ATTEMPTED>:
3937:
3938: Attempted, and not yet graded.
1.69 bowersj2 3939:
1.51 bowersj2 3940: =back
3941:
3942: =cut
3943:
1.53 bowersj2 3944: sub NOT_ATTEMPTED { return 10; }
3945: sub INCORRECT { return 11; }
3946: sub INCORRECT_BY_OVERRIDE { return 12; }
3947: sub CORRECT { return 13; }
3948: sub CORRECT_BY_OVERRIDE { return 14; }
3949: sub EXCUSED { return 15; }
1.69 bowersj2 3950: sub ATTEMPTED { return 16; }
1.51 bowersj2 3951:
3952: sub getCompletionStatus {
3953: my $self = shift;
3954: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
3955:
1.108 bowersj2 3956: my $status = $self->queryRestoreHash('solved', shift);
1.51 bowersj2 3957:
1.251 www 3958: # Left as separate if statements in case we ever do more with this
1.51 bowersj2 3959: if ($status eq 'correct_by_student') {return $self->CORRECT;}
1.267.2.4 albertel 3960: if ($status eq 'correct_by_scantron') {return $self->CORRECT;}
1.51 bowersj2 3961: if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
3962: if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
3963: if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
3964: if ($status eq 'excused') {return $self->EXCUSED; }
1.69 bowersj2 3965: if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
1.51 bowersj2 3966: return $self->NOT_ATTEMPTED;
1.108 bowersj2 3967: }
3968:
3969: sub queryRestoreHash {
3970: my $self = shift;
3971: my $hashentry = shift;
3972: my $part = shift;
1.185 bowersj2 3973: $part = "0" if (!defined($part) || $part eq '');
1.108 bowersj2 3974: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
3975:
3976: $self->getReturnHash();
3977:
3978: return $self->{RETURN_HASH}->{'resource.'.$part.'.'.$hashentry};
1.51 bowersj2 3979: }
3980:
3981: =pod
3982:
3983: B<Composite Status>
3984:
1.174 albertel 3985: Along with directly returning the date or completion status, the
3986: resource object includes a convenience function B<status>() that will
3987: combine the two status tidbits into one composite status that can
1.191 bowersj2 3988: represent the status of the resource as a whole. This method represents
3989: the concept of the thing we want to display to the user on the nav maps
3990: screen, which is a combination of completion and open status. The precise logic is
1.174 albertel 3991: documented in the comments of the status method. The following results
3992: may be returned, all available as methods on the resource object
1.185 bowersj2 3993: ($res->NETWORK_FAILURE): In addition to the return values that match
3994: the date or completion status, this function can return "ANSWER_SUBMITTED"
3995: if that problemstatus parameter value is set to No, suppressing the
3996: incorrect/correct feedback.
1.51 bowersj2 3997:
3998: =over 4
3999:
1.174 albertel 4000: =item * B<NETWORK_FAILURE>:
4001:
4002: The network has failed and the information is not available.
1.51 bowersj2 4003:
1.174 albertel 4004: =item * B<NOTHING_SET>:
1.53 bowersj2 4005:
1.174 albertel 4006: No dates have been set for this problem (part) at all. (Because only
4007: certain parts of a multi-part problem may be assigned, this can not be
4008: collapsed into "open later", as we do not know a given part will EVER
4009: be opened. For single part, this is the same as "OPEN_LATER".)
1.51 bowersj2 4010:
1.174 albertel 4011: =item * B<CORRECT>:
1.51 bowersj2 4012:
1.174 albertel 4013: For any reason at all, the part is considered correct.
1.54 bowersj2 4014:
1.174 albertel 4015: =item * B<EXCUSED>:
1.51 bowersj2 4016:
1.174 albertel 4017: For any reason at all, the problem is excused.
1.51 bowersj2 4018:
1.174 albertel 4019: =item * B<PAST_DUE_NO_ANSWER>:
1.51 bowersj2 4020:
1.174 albertel 4021: The problem is past due, not considered correct, and no answer date is
4022: set.
1.51 bowersj2 4023:
1.174 albertel 4024: =item * B<PAST_DUE_ANSWER_LATER>:
1.51 bowersj2 4025:
1.174 albertel 4026: The problem is past due, not considered correct, and an answer date in
4027: the future is set.
1.51 bowersj2 4028:
1.174 albertel 4029: =item * B<ANSWER_OPEN>:
4030:
4031: The problem is past due, not correct, and the answer is now available.
4032:
4033: =item * B<OPEN_LATER>:
4034:
4035: The problem is not yet open.
4036:
4037: =item * B<TRIES_LEFT>:
4038:
4039: The problem is open, has been tried, is not correct, but there are
4040: tries left.
4041:
4042: =item * B<INCORRECT>:
4043:
4044: The problem is open, and all tries have been used without getting the
4045: correct answer.
4046:
4047: =item * B<OPEN>:
4048:
4049: The item is open and not yet tried.
4050:
4051: =item * B<ATTEMPTED>:
4052:
4053: The problem has been attempted.
1.69 bowersj2 4054:
1.185 bowersj2 4055: =item * B<ANSWER_SUBMITTED>:
4056:
4057: An answer has been submitted, but the student should not see it.
4058:
1.51 bowersj2 4059: =back
4060:
4061: =cut
4062:
1.185 bowersj2 4063: sub TRIES_LEFT { return 20; }
4064: sub ANSWER_SUBMITTED { return 21; }
1.51 bowersj2 4065:
4066: sub status {
4067: my $self = shift;
4068: my $part = shift;
4069: if (!defined($part)) { $part = "0"; }
4070: my $completionStatus = $self->getCompletionStatus($part);
4071: my $dateStatus = $self->getDateStatus($part);
4072:
4073: # What we have is a two-dimensional matrix with 4 entries on one
4074: # dimension and 5 entries on the other, which we want to colorize,
1.54 bowersj2 4075: # plus network failure and "no date data at all".
1.51 bowersj2 4076:
1.222 bowersj2 4077: #if ($self->{RESOURCE_ERROR}) { return NETWORK_FAILURE; }
1.53 bowersj2 4078: if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51 bowersj2 4079:
1.236 bowersj2 4080: my $suppressFeedback = $self->problemstatus($part) eq 'no';
4081: # If there's an answer date and we're past it, don't
4082: # suppress the feedback; student should know
4083: if ($self->answerdate($part) && $self->answerdate($part) < time()) {
4084: $suppressFeedback = 0;
4085: }
1.185 bowersj2 4086:
1.51 bowersj2 4087: # There are a few whole rows we can dispose of:
1.53 bowersj2 4088: if ($completionStatus == CORRECT ||
4089: $completionStatus == CORRECT_BY_OVERRIDE ) {
1.185 bowersj2 4090: return $suppressFeedback? ANSWER_SUBMITTED : CORRECT;
1.69 bowersj2 4091: }
4092:
4093: if ($completionStatus == ATTEMPTED) {
4094: return ATTEMPTED;
1.53 bowersj2 4095: }
4096:
4097: # If it's EXCUSED, then return that no matter what
4098: if ($completionStatus == EXCUSED) {
4099: return EXCUSED;
1.51 bowersj2 4100: }
4101:
1.53 bowersj2 4102: if ($dateStatus == NOTHING_SET) {
4103: return NOTHING_SET;
1.51 bowersj2 4104: }
4105:
1.69 bowersj2 4106: # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
4107: # by 4 matrix (date statuses).
1.51 bowersj2 4108:
1.54 bowersj2 4109: if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59 bowersj2 4110: $dateStatus == PAST_DUE_NO_ANSWER ) {
1.267.2.2 albertel 4111: return $suppressFeedback ? ANSWER_SUBMITTED : $dateStatus;
1.51 bowersj2 4112: }
4113:
1.53 bowersj2 4114: if ($dateStatus == ANSWER_OPEN) {
4115: return ANSWER_OPEN;
1.51 bowersj2 4116: }
4117:
4118: # Now: (incorrect, incorrect_override, not_attempted) x
4119: # (open_later), (open)
4120:
1.53 bowersj2 4121: if ($dateStatus == OPEN_LATER) {
4122: return OPEN_LATER;
1.51 bowersj2 4123: }
4124:
4125: # If it's WRONG...
1.53 bowersj2 4126: if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51 bowersj2 4127: # and there are TRIES LEFT:
1.79 bowersj2 4128: if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
1.222 bowersj2 4129: return $suppressFeedback ? ANSWER_SUBMITTED : TRIES_LEFT;
1.51 bowersj2 4130: }
1.185 bowersj2 4131: return $suppressFeedback ? ANSWER_SUBMITTED : INCORRECT; # otherwise, return orange; student can't fix this
1.51 bowersj2 4132: }
4133:
4134: # Otherwise, it's untried and open
1.53 bowersj2 4135: return OPEN;
1.224 bowersj2 4136: }
4137:
4138: sub CLOSED { return 23; }
4139: sub ERROR { return 24; }
4140:
4141: =pod
4142:
4143: B<Simple Status>
4144:
4145: Convenience method B<simpleStatus> provides a "simple status" for the resource.
4146: "Simple status" corresponds to "which icon is shown on the
4147: Navmaps". There are six "simple" statuses:
4148:
4149: =over 4
4150:
4151: =item * B<CLOSED>: The problem is currently closed. (No icon shown.)
4152:
4153: =item * B<OPEN>: The problem is open and unattempted.
4154:
4155: =item * B<CORRECT>: The problem is correct for any reason.
4156:
4157: =item * B<INCORRECT>: The problem is incorrect and can still be
4158: completed successfully.
4159:
4160: =item * B<ATTEMPTED>: The problem has been attempted, but the student
4161: does not know if they are correct. (The ellipsis icon.)
4162:
4163: =item * B<ERROR>: There is an error retrieving information about this
4164: problem.
4165:
4166: =back
4167:
4168: =cut
4169:
4170: # This hash maps the composite status to this simple status, and
4171: # can be used directly, if you like
4172: my %compositeToSimple =
4173: (
4174: NETWORK_FAILURE() => ERROR,
4175: NOTHING_SET() => CLOSED,
4176: CORRECT() => CORRECT,
4177: EXCUSED() => CORRECT,
4178: PAST_DUE_NO_ANSWER() => INCORRECT,
4179: PAST_DUE_ANSWER_LATER() => INCORRECT,
4180: ANSWER_OPEN() => INCORRECT,
4181: OPEN_LATER() => CLOSED,
4182: TRIES_LEFT() => OPEN,
4183: INCORRECT() => INCORRECT,
4184: OPEN() => OPEN,
4185: ATTEMPTED() => ATTEMPTED,
4186: ANSWER_SUBMITTED() => ATTEMPTED
4187: );
4188:
4189: sub simpleStatus {
4190: my $self = shift;
4191: my $part = shift;
4192: my $status = $self->status($part);
4193: return $compositeToSimple{$status};
1.225 bowersj2 4194: }
4195:
4196: =pod
4197:
4198: B<simpleStatusCount> will return an array reference containing, in
4199: this order, the number of OPEN, CLOSED, CORRECT, INCORRECT, ATTEMPTED,
4200: and ERROR parts the given problem has.
4201:
4202: =cut
4203:
4204: # This maps the status to the slot we want to increment
4205: my %statusToSlotMap =
4206: (
4207: OPEN() => 0,
4208: CLOSED() => 1,
4209: CORRECT() => 2,
4210: INCORRECT() => 3,
4211: ATTEMPTED() => 4,
4212: ERROR() => 5
4213: );
4214:
4215: sub statusToSlot { return $statusToSlotMap{shift()}; }
4216:
4217: sub simpleStatusCount {
4218: my $self = shift;
4219:
4220: my @counts = (0, 0, 0, 0, 0, 0, 0);
4221: foreach my $part (@{$self->parts()}) {
4222: $counts[$statusToSlotMap{$self->simpleStatus($part)}]++;
4223: }
4224:
4225: return \@counts;
1.191 bowersj2 4226: }
4227:
4228: =pod
4229:
4230: B<Completable>
4231:
4232: The completable method represents the concept of I<whether the student can
4233: currently do the problem>. If the student can do the problem, which means
4234: that it is open, there are tries left, and if the problem is manually graded
4235: or the grade is suppressed via problemstatus, the student has not tried it
4236: yet, then the method returns 1. Otherwise, it returns 0, to indicate that
4237: either the student has tried it and there is no feedback, or that for
4238: some reason it is no longer completable (not open yet, successfully completed,
4239: out of tries, etc.). As an example, this is used as the filter for the
4240: "Uncompleted Homework" option for the nav maps.
4241:
4242: If this does not quite meet your needs, do not fiddle with it (unless you are
4243: fixing it to better match the student's conception of "completable" because
4244: it's broken somehow)... make a new method.
4245:
4246: =cut
4247:
4248: sub completable {
4249: my $self = shift;
4250: if (!$self->is_problem()) { return 0; }
4251: my $partCount = $self->countParts();
4252:
4253: foreach my $part (@{$self->parts()}) {
4254: if ($part eq '0' && $partCount != 1) { next; }
4255: my $status = $self->status($part);
4256: # "If any of the parts are open, or have tries left (implies open),
4257: # and it is not "attempted" (manually graded problem), it is
4258: # not "complete"
1.216 bowersj2 4259: if ($self->getCompletionStatus($part) == ATTEMPTED() ||
4260: $status == ANSWER_SUBMITTED() ) {
4261: # did this part already, as well as we can
4262: next;
4263: }
4264: if ($status == OPEN() || $status == TRIES_LEFT()) {
4265: return 1;
4266: }
1.191 bowersj2 4267: }
4268:
4269: # If all the parts were complete, so was this problem.
1.216 bowersj2 4270: return 0;
1.51 bowersj2 4271: }
4272:
4273: =pod
4274:
4275: =head2 Resource/Nav Map Navigation
4276:
4277: =over 4
4278:
1.174 albertel 4279: =item * B<getNext>():
4280:
4281: Retreive an array of the possible next resources after this
4282: one. Always returns an array, even in the one- or zero-element case.
4283:
4284: =item * B<getPrevious>():
1.85 bowersj2 4285:
1.174 albertel 4286: Retreive an array of the possible previous resources from this
4287: one. Always returns an array, even in the one- or zero-element case.
1.51 bowersj2 4288:
4289: =cut
4290:
4291: sub getNext {
4292: my $self = shift;
4293: my @branches;
4294: my $to = $self->to();
1.85 bowersj2 4295: foreach my $branch ( split(/,/, $to) ) {
1.51 bowersj2 4296: my $choice = $self->{NAV_MAP}->getById($branch);
4297: my $next = $choice->goesto();
4298: $next = $self->{NAV_MAP}->getById($next);
4299:
1.131 bowersj2 4300: push @branches, $next;
1.85 bowersj2 4301: }
4302: return \@branches;
4303: }
4304:
4305: sub getPrevious {
4306: my $self = shift;
4307: my @branches;
4308: my $from = $self->from();
4309: foreach my $branch ( split /,/, $from) {
4310: my $choice = $self->{NAV_MAP}->getById($branch);
4311: my $prev = $choice->comesfrom();
4312: $prev = $self->{NAV_MAP}->getById($prev);
4313:
1.131 bowersj2 4314: push @branches, $prev;
1.51 bowersj2 4315: }
4316: return \@branches;
1.131 bowersj2 4317: }
4318:
4319: sub browsePriv {
4320: my $self = shift;
4321: if (defined($self->{BROWSE_PRIV})) {
4322: return $self->{BROWSE_PRIV};
4323: }
4324:
4325: $self->{BROWSE_PRIV} = &Apache::lonnet::allowed('bre', $self->src());
1.51 bowersj2 4326: }
4327:
4328: =pod
1.2 www 4329:
1.51 bowersj2 4330: =back
1.2 www 4331:
1.51 bowersj2 4332: =cut
1.2 www 4333:
1.51 bowersj2 4334: 1;
1.2 www 4335:
1.51 bowersj2 4336: __END__
1.2 www 4337:
4338:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>