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