Annotation of loncom/interface/lonnavmaps.pm, revision 1.111
1.72 bowersj2 1:
1.2 www 2: # The LearningOnline Network with CAPA
3: # Navigate Maps Handler
1.1 www 4: #
1.111 ! bowersj2 5: # $Id: lonnavmaps.pm,v 1.110 2002/11/15 20:12:11 bowersj2 Exp $
1.20 albertel 6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
27: # http://www.lon-capa.org/
28: #
1.2 www 29: # (Page Handler
1.1 www 30: #
1.2 www 31: # (TeX Content Handler
1.1 www 32: #
1.2 www 33: # 05/29/00,05/30 Gerd Kortemeyer)
34: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
35: # 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 36: #
1.17 www 37: # 3/1/1,6/1,17/1,29/1,30/1,2/8,9/21,9/24,9/25 Gerd Kortemeyer
1.21 www 38: # YEAR=2002
39: # 1/1 Gerd Kortemeyer
1.105 bowersj2 40: # Oct-Nov Jeremy Bowers
1.2 www 41:
1.1 www 42: package Apache::lonnavmaps;
43:
44: use strict;
1.2 www 45: use Apache::Constants qw(:common :http);
1.18 albertel 46: use Apache::loncommon();
1.73 bowersj2 47: use POSIX qw (floor strftime);
1.2 www 48:
1.1 www 49: sub handler {
1.99 bowersj2 50: my $r = shift;
1.2 www 51:
1.51 bowersj2 52: &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
53:
54: # Handle header-only request
55: if ($r->header_only) {
56: if ($ENV{'browser.mathml'}) {
57: $r->content_type('text/xml');
58: } else {
59: $r->content_type('text/html');
60: }
61: $r->send_http_header;
62: return OK;
63: }
64:
65: # Send header, don't cache this page
66: if ($ENV{'browser.mathml'}) {
67: $r->content_type('text/xml');
68: } else {
69: $r->content_type('text/html');
70: }
71: &Apache::loncommon::no_cache($r);
72: $r->send_http_header;
73:
1.106 bowersj2 74: # Create the nav map
1.51 bowersj2 75: my $navmap = Apache::lonnavmaps::navmap->new(
76: $ENV{"request.course.fn"}.".db",
1.80 bowersj2 77: $ENV{"request.course.fn"}."_parms.db", 1, 1);
1.51 bowersj2 78:
1.55 bowersj2 79:
80: if (!defined($navmap)) {
81: my $requrl = $r->uri;
82: $ENV{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
83: return HTTP_NOT_ACCEPTABLE;
84: }
1.64 bowersj2 85:
1.111 ! bowersj2 86: $r->print("<html><head>\n");
! 87: $r->print("<title>Navigate Course Contents</title>");
! 88:
1.64 bowersj2 89: # Header
1.111 ! bowersj2 90: $r->print(&Apache::loncommon::bodytag('Navigate Course Contents','',
1.64 bowersj2 91: ''));
92: $r->print('<script>window.focus();</script>');
1.101 bowersj2 93:
1.95 bowersj2 94: $r->print('<table border="0" cellpadding="2" cellspacing="0">');
1.64 bowersj2 95: my $date=localtime;
1.95 bowersj2 96: $r->print('<tr><td align="right" valign="bottom">Key: </td>');
1.101 bowersj2 97:
98: # Print discussions and feedback header
1.65 bowersj2 99: if ($navmap->{LAST_CHECK}) {
1.95 bowersj2 100: $r->print('<td align="center" valign="bottom"> '.
101: '<img src="/adm/lonMisc/chat.gif"> New discussion since '.
1.73 bowersj2 102: strftime("%A, %b %e at %I:%M %P", localtime($navmap->{LAST_CHECK})).
1.95 bowersj2 103: '</td><td align="center" valign="bottom"> '.
104: '<img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>'.
105: '</td>');
106: } else {
107: $r->print('<td align="center" valign="bottom"> '.
108: '<img src="/adm/lonMisc/chat.gif"> Discussions</td><td align="center" valign="bottom">'.
109: ' <img src="/adm/lonMisc/feedback.gif"> New message (click to open)'.
110: '</td>');
111: }
112: $r->print('</tr></table>');
1.101 bowersj2 113:
1.95 bowersj2 114: my $condition = 0;
115: if ($ENV{'form.condition'}) {
116: $condition = 1;
117: }
118:
1.110 bowersj2 119: my $currenturl = $ENV{'form.postdata'};
120: $currenturl=~s/^http\:\/\///;
121: $currenturl=~s/^[^\/]+//;
122:
123: # alreadyHere allows us to only open the maps necessary to view
124: # the current location once, while at the same time remembering
125: # the current location. Without that check, the user would never
126: # be able to close those maps; the user would close it, and the
127: # currenturl scan would re-open it.
128: my $queryAdd = "postdata=" . &Apache::lonnet::escape($currenturl) .
129: "&alreadyHere=1";
130:
1.95 bowersj2 131: if ($condition) {
1.110 bowersj2 132: $r->print("<a href=\"navmaps?condition=0&filter=&$queryAdd\">Close All Folders</a>");
1.65 bowersj2 133: } else {
1.110 bowersj2 134: $r->print("<a href=\"navmaps?condition=1&filter=&$queryAdd\">Open All Folders</a>");
1.65 bowersj2 135: }
1.55 bowersj2 136:
1.95 bowersj2 137: $r->print('<br> ');
138: $r->rflush();
139:
1.92 bowersj2 140: # Now that we've displayed some stuff to the user, init the navmap
141: $navmap->init();
142:
1.55 bowersj2 143: # Check that it's defined
144: if (!($navmap->courseMapDefined())) {
145: $r->print('<font size="+2" color="red">Coursemap undefined.</font>' .
146: '</body></html>');
147: return OK;
148: }
149:
1.51 bowersj2 150: # Grab a resource object so we have access to the constants; this
151: # is technically not proper, but should be harmless
152: my $res = $navmap->firstResource();
153:
1.101 bowersj2 154: # These are some data tables, which make it easy to change some of
155: # of the specific visualization parameters if desired.
156:
1.51 bowersj2 157: # Defines a status->color mapping, null string means don't color
158: my %colormap =
1.54 bowersj2 159: ( $res->NETWORK_FAILURE => '',
1.59 bowersj2 160: $res->CORRECT => '',
1.54 bowersj2 161: $res->EXCUSED => '#BBBBFF',
1.59 bowersj2 162: $res->PAST_DUE_ANSWER_LATER => '',
163: $res->PAST_DUE_NO_ANSWER => '',
164: $res->ANSWER_OPEN => '#CCFFCC',
1.54 bowersj2 165: $res->OPEN_LATER => '',
1.59 bowersj2 166: $res->TRIES_LEFT => '',
167: $res->INCORRECT => '',
168: $res->OPEN => '',
1.54 bowersj2 169: $res->NOTHING_SET => '' );
1.59 bowersj2 170: # And a special case in the nav map; what to do when the assignment
171: # is not yet done and due in less then 24 hours
1.86 bowersj2 172: my $hurryUpColor = "#FF0000";
1.59 bowersj2 173:
174: my %statusIconMap =
1.66 bowersj2 175: ( $res->NETWORK_FAILURE => '',
176: $res->NOTHING_SET => '',
177: $res->CORRECT => 'navmap.correct.gif',
178: $res->EXCUSED => 'navmap.correct.gif',
179: $res->PAST_DUE_NO_ANSWER => 'navmap.wrong.gif',
180: $res->PAST_DUE_ANSWER_LATER => 'navmap.wrong.gif',
181: $res->ANSWER_OPEN => 'navmap.wrong.gif',
182: $res->OPEN_LATER => '',
183: $res->TRIES_LEFT => 'navmap.open.gif',
184: $res->INCORRECT => 'navmap.wrong.gif',
1.69 bowersj2 185: $res->OPEN => 'navmap.open.gif',
1.85 bowersj2 186: $res->ATTEMPTED => 'navmap.open.gif' );
1.69 bowersj2 187:
188: my %iconAltTags =
189: ( 'navmap.correct.gif' => 'Correct',
190: 'navmap.wrong.gif' => 'Incorrect',
191: 'navmap.open.gif' => 'Open' );
1.59 bowersj2 192:
193: my %condenseStatuses =
1.66 bowersj2 194: ( $res->NETWORK_FAILURE => 1,
195: $res->NOTHING_SET => 1,
196: $res->CORRECT => 1 );
1.51 bowersj2 197:
198: my %filterHash;
199: # Figure out what we're not displaying
200: foreach (split(/\,/, $ENV{"form.filter"})) {
201: if ($_) {
202: $filterHash{$_} = "1";
203: }
204: }
205:
1.88 bowersj2 206: # Is this a new-style course? If so, we want to suppress showing the top-level
207: # maps in their own folders, in favor of "inlining" them.
208: my $topResource = $navmap->getById("0.0");
209: my $inlineTopLevelMaps = $topResource->src() =~ m|^/uploaded/.*default\.sequence$|;
1.63 bowersj2 210:
1.51 bowersj2 211: # Begin the HTML table
1.59 bowersj2 212: # four cols: resource + indent, chat+feedback, icon, text string
1.85 bowersj2 213: $r->print('<table cellspacing="0" cellpadding="3" border="0" bgcolor="#FFFFFF">' ."\n");
1.51 bowersj2 214:
1.74 bowersj2 215: # This needs to be updated to use symbs from the remote,
216: # instead of uris. The changes to this and the main rendering
217: # loop should be obvious.
218: # Here's a simple example of the iterator.
1.88 bowersj2 219: # Preprocess the map: Look for current URL, force inlined maps to display
220:
1.101 bowersj2 221: my $mapIterator = $navmap->getIterator(undef, undef, \%filterHash, 1);
1.88 bowersj2 222: my $found = 0;
223: my $depth = 1;
1.91 bowersj2 224: my $currentUrlIndex = 0; # keeps track of when the current resource is found,
225: # so we can back up a few and put the anchor above the
226: # current resource
1.101 bowersj2 227: my $currentUrlDelta = 5; # change this to change how many resources are displayed
228: # before the current resource when using #current
1.88 bowersj2 229: $mapIterator->next(); # discard the first BEGIN_MAP
230: my $curRes = $mapIterator->next();
1.91 bowersj2 231: my $counter = 0;
1.88 bowersj2 232:
233: while ($depth > 0) {
1.96 bowersj2 234: if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
235: if ($curRes == $mapIterator->END_MAP()) { $depth--; }
1.88 bowersj2 236:
1.91 bowersj2 237: if (ref($curRes)) { $counter++; }
238:
1.88 bowersj2 239: my $mapStack = $mapIterator->getStack();
240: if ($currenturl && !$ENV{'form.alreadyHere'} && ref($curRes) &&
241: $curRes->src() eq $currenturl) {
242: # If this is the correct resource, be sure to
243: # show it by making sure the containing maps
244: # are open.
1.91 bowersj2 245:
1.101 bowersj2 246: # This is why we have to use the main iterator instead of the
247: # potentially faster DFS: The count has to be the same, so
248: # the order has to be the same, which DFS won't give us.
1.91 bowersj2 249: $currentUrlIndex = $counter;
1.88 bowersj2 250:
1.101 bowersj2 251: # Ensure the parent maps are open
1.88 bowersj2 252: for my $map (@{$mapStack}) {
253: if ($condition) {
254: undef $filterHash{$map->map_pc()};
255: } else {
256: $filterHash{$map->map_pc()} = 1;
1.74 bowersj2 257: }
258: }
1.88 bowersj2 259: $ENV{'form.alreadyHere'} = 1;
260: }
261:
262: # Preprocessing: If we're inlining nav maps into the top-level display,
263: # make sure we show this map!
264: if ($inlineTopLevelMaps && ref($curRes) && $curRes->is_map &&
265: scalar(@{$mapStack}) == 1) {
266: if ($condition) {
267: undef $filterHash{$curRes->map_pc()};
268: } else {
269: $filterHash{$curRes->map_pc()} = 1;
270: }
1.74 bowersj2 271: }
1.88 bowersj2 272:
273: $curRes = $mapIterator->next();
1.74 bowersj2 274: }
1.88 bowersj2 275:
1.53 bowersj2 276: undef $res; # so we don't accidentally use it later
1.72 bowersj2 277: my $indentLevel = 0;
1.61 bowersj2 278: my $indentString = "<img src=\"/adm/lonIcons/whitespace1.gif\" width=\"25\" height=\"1\" alt=\"\" border=\"0\" />";
1.51 bowersj2 279:
280: my $isNewBranch = 0;
1.59 bowersj2 281: my $now = time();
282: my $in24Hours = $now + 24 * 60 * 60;
1.78 bowersj2 283: my $displayedHereMarker = 0;
1.88 bowersj2 284:
1.72 bowersj2 285: # We know the first thing is a BEGIN_MAP (see "$self->{STARTED}"
286: # code in iterator->next), so ignore the first one
1.89 bowersj2 287: $mapIterator = $navmap->getIterator(undef, undef, \%filterHash,
1.74 bowersj2 288: $condition);
1.72 bowersj2 289: $mapIterator->next();
1.89 bowersj2 290: $curRes = $mapIterator->next();
1.88 bowersj2 291: my $deltadepth = 0;
1.90 bowersj2 292: $depth = 1;
1.88 bowersj2 293:
294: my @backgroundColors = ("#FFFFFF", "#F6F6F6");
295: my $rowNum = 0;
1.51 bowersj2 296:
1.91 bowersj2 297: $counter = 0;
298:
1.101 bowersj2 299: # Print the 'current' anchor here if it would fall off the top
300: if ($currentUrlIndex - $currentUrlDelta < 0) {
301: $r->print('<a name="current" />');
302: }
303:
1.72 bowersj2 304: while ($depth > 0) {
1.101 bowersj2 305: # If this is an inlined map, cancel the shift to the right,
306: # which has the effect of making the map look inlined
1.88 bowersj2 307: if ($inlineTopLevelMaps && scalar(@{$mapIterator->getStack()}) == 1 &&
308: ref($curRes) && $curRes->is_map()) {
309: $deltadepth = -1;
310: $curRes = $mapIterator->next();
311: next;
312: }
313:
1.51 bowersj2 314: if ($curRes == $mapIterator->BEGIN_MAP() ||
1.52 bowersj2 315: $curRes == $mapIterator->BEGIN_BRANCH()) {
1.51 bowersj2 316: $indentLevel++;
317: }
318: if ($curRes == $mapIterator->END_MAP() ||
1.52 bowersj2 319: $curRes == $mapIterator->END_BRANCH()) {
1.51 bowersj2 320: $indentLevel--;
321: }
1.52 bowersj2 322: if ($curRes == $mapIterator->BEGIN_BRANCH()) {
323: $isNewBranch = 1;
1.72 bowersj2 324: }
1.96 bowersj2 325: if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
326: if ($curRes == $mapIterator->END_MAP()) { $depth--; }
1.51 bowersj2 327:
1.91 bowersj2 328: if (ref($curRes)) { $counter++; }
329:
1.88 bowersj2 330: if ($depth == 1) { $deltadepth = 0; } # we're done shifting, because we're
331: # out of the inlined map
332:
1.101 bowersj2 333: # Is this resource being ignored because it is in a random-out
334: # map and it was not selected?
1.68 bowersj2 335: if (ref($curRes) && !advancedUser() && $curRes->randomout()) {
336: $curRes = $mapIterator->next();
1.101 bowersj2 337: next; # if yes, then just ignore this resource
1.68 bowersj2 338: }
339:
1.51 bowersj2 340: if (ref($curRes) && $curRes->src()) {
341:
1.66 bowersj2 342: # Step one: Decide which parts to show
343: my @parts = @{$curRes->parts()};
344: my $multipart = scalar(@parts) > 1;
345: my $condensed = 0;
346:
1.51 bowersj2 347: if ($curRes->is_problem()) {
1.101 bowersj2 348:
1.66 bowersj2 349: # Is it multipart?
350: if ($multipart) {
351: # If it's multipart, see if part 0 is "open"
352: # if it is, display all parts, if it isn't,
353: # just display first
354: if (!$curRes->opendate("0")) {
1.101 bowersj2 355: # no parts are open, display as one part
356: @parts = ("0");
1.68 bowersj2 357: $condensed = 1;
1.66 bowersj2 358: } else {
359: # Otherwise, only display part 0 if we want to
360: # attach feedback or email information to it
361: if ($curRes->hasDiscussion() || $curRes->getFeedback()) {
362: shift @parts;
363: } else {
364: # Now, we decide whether to condense the
365: # parts due to similarity
366: my $status = $curRes->status($parts[1]);
367: my $due = $curRes->duedate($parts[1]);
368: my $open = $curRes->opendate($parts[1]);
369: my $statusAllSame = 1;
370: my $dueAllSame = 1;
371: my $openAllSame = 1;
372: for (my $i = 2; $i < scalar(@parts); $i++) {
373: if ($curRes->status($parts[$i]) != $status){
374: $statusAllSame = 0;
375: }
376: if ($curRes->duedate($parts[$i]) != $due ) {
377: $dueAllSame = 0;
378: }
379: if ($curRes->opendate($parts[$i]) != $open) {
380: $openAllSame = 0;
381: }
382: }
383:
384: # $allSame is true if all the statuses were
385: # the same. Now, if they are all the same and
386: # match one of the statuses to condense, or they
387: # are all open with the same due date, or they are
388: # all OPEN_LATER with the same open date, display the
389: # status of the first non-zero part (to get the 'correct'
390: # status right, since 0 is never 'correct' or 'open').
391: if (($statusAllSame && defined($condenseStatuses{$status})) ||
392: ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
393: ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
394: @parts = ($parts[1]);
395: $condensed = 1;
396: }
397: }
398: }
399: }
1.59 bowersj2 400:
1.51 bowersj2 401: } else {
1.84 bowersj2 402: $parts[0] = "0"; # this is to get past foreach loop below
1.51 bowersj2 403: # you can consider a non-problem resource as a resource
1.101 bowersj2 404: # with only one part without loss, and it simplifies the looping
1.51 bowersj2 405: }
406:
1.83 bowersj2 407: # Is it a multipart problem with a single part, now in
1.101 bowersj2 408: # @parts with "0" filtered out? If so, 'forget' it's a multi-part
1.83 bowersj2 409: # problem and treat it like a single-part problem.
410: if ( scalar(@parts) == 1 ) {
411: $multipart = 0;
412: }
413:
1.66 bowersj2 414: # Display one part, in event of network error.
415: # If this is a single part, we can at least show the correct
1.101 bowersj2 416: # status, but if it's multipart, we're lost, since we can't
417: # retreive the metadata to count the parts
1.66 bowersj2 418: if ($curRes->{RESOURCE_ERROR}) {
419: @parts = ("0");
420: }
421:
1.101 bowersj2 422: # Step Two: Print the actual data.
423:
424: # For each part we intend to display...
1.51 bowersj2 425: foreach my $part (@parts) {
1.59 bowersj2 426:
1.66 bowersj2 427: my $deltalevel = 0; # for inserting the branch icon
1.68 bowersj2 428: my $nonLinkedText = ""; # unlinked stuff after title
1.51 bowersj2 429:
430: my $stack = $mapIterator->getStack();
431: my $src = getLinkForResource($stack);
432:
1.101 bowersj2 433: # Pass the correct symb on the querystring, so the
434: # remote will figure out where we are if we click a link
1.51 bowersj2 435: my $srcHasQuestion = $src =~ /\?/;
436: my $link = $src.
437: ($srcHasQuestion?'&':'?') .
438: 'symb='.&Apache::lonnet::escape($curRes->symb()).
439: '"';
1.101 bowersj2 440:
1.106 bowersj2 441: my $title = $curRes->compTitle();
1.51 bowersj2 442: my $partLabel = "";
1.52 bowersj2 443: my $newBranchText = "";
444:
445: # If this is a new branch, label it so
446: if ($isNewBranch) {
1.59 bowersj2 447: $newBranchText = "<img src=\"/adm/lonIcons/branch.gif\" border=\"0\">";
1.52 bowersj2 448: $isNewBranch = 0;
1.101 bowersj2 449: $deltalevel = 1; # reserves space for the branch icon
1.52 bowersj2 450: }
1.51 bowersj2 451:
452: # links to open and close the folders
453: my $linkopen = "<a href=\"$link\">";
454: my $linkclose = "</a>";
455:
1.61 bowersj2 456: my $icon = "<img src=\"/adm/lonIcons/html.gif\" alt=\"\" border=\"0\" />";
1.51 bowersj2 457: if ($curRes->is_problem()) {
1.66 bowersj2 458: if ($part eq "0" || $condensed) {
459: $icon = '<img src="/adm/lonIcons/problem.gif" alt="" border=\"0\" />';
460: } else {
461: $icon = $indentString;
462: }
1.51 bowersj2 463: }
464:
465: # Display the correct icon, link to open or shut map
466: if ($curRes->is_map()) {
467: my $mapId = $curRes->map_pc();
1.82 bowersj2 468: my $nowOpen = (!defined($filterHash{$mapId}));
469: if ($condition) {$nowOpen = !$nowOpen;}
1.51 bowersj2 470: $icon = $nowOpen ?
1.77 bowersj2 471: "navmap.folder.closed.gif" : "navmap.folder.open.gif";
1.66 bowersj2 472: $icon = "<img src=\"/adm/lonIcons/$icon\" alt=\"\" border=\"0\" />";
1.52 bowersj2 473: $linkopen = "<a href=\"/adm/navmaps?filter=";
1.66 bowersj2 474: $linkopen .= ($nowOpen xor $condition) ?
1.51 bowersj2 475: addToFilter(\%filterHash, $mapId) :
476: removeFromFilter(\%filterHash, $mapId);
1.66 bowersj2 477: $linkopen .= "&condition=$condition&$queryAdd\">";
1.51 bowersj2 478: $linkclose = "</a>";
1.68 bowersj2 479:
1.51 bowersj2 480: }
481:
482: my $colorizer = "";
1.86 bowersj2 483: my $color;
1.51 bowersj2 484: if ($curRes->is_problem()) {
485: my $status = $curRes->status($part);
1.86 bowersj2 486: $color = $colormap{$status};
1.74 bowersj2 487:
488: # Special case in the navmaps: If in less then
489: # 24 hours, give it a bit of urgency
1.86 bowersj2 490: if (($status == $curRes->OPEN() || $status == $curRes->ATTEMPTED() ||
491: $status == $curRes->TRIES_LEFT())
492: && $curRes->duedate() &&
1.76 bowersj2 493: $curRes->duedate() < time()+(24*60*60) &&
494: $curRes->duedate() > time()) {
1.74 bowersj2 495: $color = $hurryUpColor;
496: }
1.79 bowersj2 497: # Special case: If this is the last try, and there is
1.88 bowersj2 498: # more then one available, and it's not due yet, give a bit of urgency
1.79 bowersj2 499: my $tries = $curRes->tries($part);
500: my $maxtries = $curRes->maxtries($part);
501: if ($tries && $maxtries && $maxtries > 1 &&
1.88 bowersj2 502: $maxtries - $tries == 1 && $curRes->duedate() &&
503: $curRes->duedate() > time()) {
1.79 bowersj2 504: $color = $hurryUpColor;
505: }
1.51 bowersj2 506: if ($color ne "") {
507: $colorizer = "bgcolor=\"$color\"";
508: }
509: }
510:
1.68 bowersj2 511: if ($curRes->randomout()) {
512: $nonLinkedText .= ' <i>(hidden)</i> ';
513: }
514:
1.88 bowersj2 515: $rowNum++;
516: my $backgroundColor = $backgroundColors[$rowNum % scalar(@backgroundColors)];
517:
1.91 bowersj2 518: # FIRST COL: The resource indentation, branch icon, name, and anchor
1.88 bowersj2 519: $r->print(" <tr bgcolor=\"$backgroundColor\"><td align=\"left\" valign=\"center\" width=\"60%\">\n");
1.51 bowersj2 520:
1.101 bowersj2 521: # Print the anchor if necessary
522: if ($counter == $currentUrlIndex - $currentUrlDelta) {
1.91 bowersj2 523: $r->print('<a name="current" />');
524: }
525:
1.51 bowersj2 526: # print indentation
1.88 bowersj2 527: for (my $i = 0; $i < $indentLevel - $deltalevel + $deltadepth; $i++) {
1.51 bowersj2 528: $r->print($indentString);
529: }
530:
1.61 bowersj2 531: $r->print(" ${newBranchText}${linkopen}$icon${linkclose}\n");
1.51 bowersj2 532:
1.74 bowersj2 533: my $curMarkerBegin = "";
534: my $curMarkerEnd = "";
535:
536: # Is this the current resource?
1.78 bowersj2 537: if ($curRes->src() eq $currenturl && !$displayedHereMarker) {
1.74 bowersj2 538: $curMarkerBegin = '<a name="curloc" /><font color="red" size="+2">> </font>';
539: $curMarkerEnd = '<font color="red" size="+2"> <</font>';
1.78 bowersj2 540: $displayedHereMarker = 1;
1.74 bowersj2 541: }
542:
1.69 bowersj2 543: if ($curRes->is_problem() && $part ne "0" && !$condensed) {
1.66 bowersj2 544: $partLabel = " (Part $part)";
545: $title = "";
546: }
1.83 bowersj2 547: if ($multipart && $condensed) {
1.68 bowersj2 548: $nonLinkedText .= ' (' . $curRes->countParts() . ' parts)';
1.66 bowersj2 549: }
1.59 bowersj2 550:
1.74 bowersj2 551: $r->print(" $curMarkerBegin<a href=\"$link\">$title$partLabel</a> $curMarkerEnd $nonLinkedText");
1.66 bowersj2 552:
553: if ($curRes->{RESOURCE_ERROR}) {
554: $r->print(&Apache::loncommon::help_open_topic ("Navmap_Host_Down",
1.84 bowersj2 555: '<font size="-1">Host down</font>'));
1.66 bowersj2 556: }
557:
1.101 bowersj2 558: # SECOND COL: Is there text, feedback, errors??
1.66 bowersj2 559: my $discussionHTML = ""; my $feedbackHTML = "";
560:
561: if ($curRes->hasDiscussion()) {
562: $discussionHTML = $linkopen .
563: '<img border="0" src="/adm/lonMisc/chat.gif" />' .
564: $linkclose;
565: }
566:
567: if ($curRes->getFeedback()) {
568: my $feedback = $curRes->getFeedback();
569: foreach (split(/\,/, $feedback)) {
570: if ($_) {
571: $feedbackHTML .= ' <a href="/adm/email?display='
572: . &Apache::lonnet::escape($_) . '">'
573: . '<img src="/adm/lonMisc/feedback.gif" '
574: . 'border="0" /></a>';
575: }
576: }
577: }
578:
1.84 bowersj2 579: $r->print("<td width=\"75\" align=\"left\" valign=\"center\">$discussionHTML$feedbackHTML </td>");
1.66 bowersj2 580:
581: # Is this the first displayed part of a multi-part problem
582: # that has not been condensed, so we should suppress these two
1.101 bowersj2 583: # columns so we don't display useless status info about part
584: # "0"?
1.66 bowersj2 585: my $firstDisplayed = !$condensed && $multipart && $part eq "0";
586:
1.69 bowersj2 587: # THIRD COL: Problem status icon
1.66 bowersj2 588: if ($curRes->is_problem() &&
589: !$firstDisplayed) {
590: my $icon = $statusIconMap{$curRes->status($part)};
1.69 bowersj2 591: my $alt = $iconAltTags{$icon};
1.66 bowersj2 592: if ($icon) {
1.84 bowersj2 593: $r->print("<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");
1.66 bowersj2 594: } else {
1.84 bowersj2 595: $r->print("<td width=\"30\"> </td>\n");
1.66 bowersj2 596: }
597: } else { # not problem, no icon
1.84 bowersj2 598: $r->print("<td width=\"30\"> </td>\n");
1.66 bowersj2 599: }
600:
1.69 bowersj2 601: # FOURTH COL: Text description
1.86 bowersj2 602: $r->print("<td align=\"right\" valign=\"center\">\n");
1.51 bowersj2 603:
1.61 bowersj2 604: if ($curRes->kind() eq "res" &&
605: $curRes->is_problem() &&
1.66 bowersj2 606: !$firstDisplayed) {
1.86 bowersj2 607: $r->print ("<font color=\"$color\"><b>") if ($color);
1.53 bowersj2 608: $r->print (getDescription($curRes, $part));
1.86 bowersj2 609: $r->print ("</b></font>") if ($color);
1.51 bowersj2 610: }
1.68 bowersj2 611: if ($curRes->is_map() && advancedUser() && $curRes->randompick()) {
612: $r->print('(randomly select ' . $curRes->randompick() .')');
613: }
1.59 bowersj2 614:
1.84 bowersj2 615: $r->print(" </td></tr>\n");
1.51 bowersj2 616: }
617: }
618: $curRes = $mapIterator->next();
619: }
620:
621: $r->print("</table></body></html>");
622:
1.59 bowersj2 623: $navmap->untieHashes();
624:
1.51 bowersj2 625: return OK;
626: }
627:
628: # Convenience functions: Returns a string that adds or subtracts
629: # the second argument from the first hash, appropriate for the
630: # query string that determines which folders to recurse on
631: sub addToFilter {
632: my $hashIn = shift;
633: my $addition = shift;
634: my %hash = %$hashIn;
635: $hash{$addition} = 1;
636:
637: return join (",", keys(%hash));
638: }
639:
640: sub removeFromFilter {
641: my $hashIn = shift;
642: my $subtraction = shift;
643: my %hash = %$hashIn;
644:
645: delete $hash{$subtraction};
646: return join(",", keys(%hash));
647: }
648:
649: # Convenience function: Given a stack returned from getStack on the iterator,
650: # return the correct src() value.
651: # Later, this should add an anchor when we start putting anchors in pages.
652: sub getLinkForResource {
653: my $stack = shift;
654: my $res;
655:
656: # Check to see if there are any pages in the stack
657: foreach $res (@$stack) {
658: if (defined($res) && $res->is_page()) {
659: return $res->src();
660: }
661: }
662:
663: # Failing that, return the src of the last resource that is defined
664: # (when we first recurse on a map, it puts an undefined resource
665: # on the bottom because $self->{HERE} isn't defined yet, and we
666: # want the src for the map anyhow)
667: foreach (@$stack) {
668: if (defined($_)) { $res = $_; }
669: }
670:
671: return $res->src();
672: }
673:
1.53 bowersj2 674: # Convenience function: This seperates the logic of how to create
675: # the problem text strings ("Due: DATE", "Open: DATE", "Not yet assigned",
676: # etc.) into a seperate function. It takes a resource object as the
677: # first parameter, and the part number of the resource as the second.
678: # It's basically a big switch statement on the status of the resource.
679:
680: sub getDescription {
681: my $res = shift;
682: my $part = shift;
1.69 bowersj2 683: my $status = $res->status($part);
1.53 bowersj2 684:
685: if ($status == $res->NETWORK_FAILURE) { return ""; }
686: if ($status == $res->NOTHING_SET) {
687: return "Not currently assigned.";
688: }
689: if ($status == $res->OPEN_LATER) {
1.73 bowersj2 690: return "Open " . timeToHumanString($res->opendate($part));
1.53 bowersj2 691: }
692: if ($status == $res->OPEN) {
1.79 bowersj2 693: if ($res->duedate($part)) {
1.73 bowersj2 694: return "Due " . timeToHumanString($res->duedate($part));
1.66 bowersj2 695: } else {
696: return "Open, no due date";
697: }
1.53 bowersj2 698: }
1.54 bowersj2 699: if ($status == $res->PAST_DUE_ANSWER_LATER) {
1.73 bowersj2 700: return "Answer open " . timeToHumanString($res->answerdate($part));
1.54 bowersj2 701: }
702: if ($status == $res->PAST_DUE_NO_ANSWER) {
1.73 bowersj2 703: return "Was due " . timeToHumanString($res->duedate($part));
1.53 bowersj2 704: }
705: if ($status == $res->ANSWER_OPEN) {
706: return "Answer available";
707: }
1.59 bowersj2 708: if ($status == $res->EXCUSED) {
1.66 bowersj2 709: return "Excused by instructor";
1.59 bowersj2 710: }
1.69 bowersj2 711: if ($status == $res->ATTEMPTED) {
712: return "Not yet graded.";
713: }
1.59 bowersj2 714: if ($status == $res->TRIES_LEFT) {
1.79 bowersj2 715: my $tries = $res->tries($part);
716: my $maxtries = $res->maxtries($part);
717: my $triesString = "";
718: if ($tries && $maxtries) {
719: $triesString = "<font size=\"-1\"><i>($tries of $maxtries tries used)</i></font>";
720: if ($maxtries > 1 && $maxtries - $tries == 1) {
721: $triesString = "<b>$triesString</b>";
722: }
723: }
1.66 bowersj2 724: if ($res->duedate()) {
1.73 bowersj2 725: return "Due " . timeToHumanString($res->duedate($part)) .
1.66 bowersj2 726: " $triesString";
727: } else {
728: return "No due date $triesString";
729: }
1.59 bowersj2 730: }
1.53 bowersj2 731: }
732:
1.101 bowersj2 733: # This puts a human-readable name on the ENV variable.
1.68 bowersj2 734: sub advancedUser {
735: return $ENV{'user.adv'};
736: }
737:
1.73 bowersj2 738:
739: # timeToHumanString takes a time number and converts it to a
740: # human-readable representation, meant to be used in the following
741: # manner:
742: # print "Due $timestring"
743: # print "Open $timestring"
744: # print "Answer available $timestring"
745: # Very, very, very, VERY English-only... goodness help a localizer on
746: # this func...
1.53 bowersj2 747: sub timeToHumanString {
1.58 albertel 748: my ($time) = @_;
749: # zero, '0' and blank are bad times
1.73 bowersj2 750: if (!$time) {
751: return 'never';
752: }
753:
754: my $now = time();
755:
756: my @time = localtime($time);
757: my @now = localtime($now);
758:
759: # Positive = future
760: my $delta = $time - $now;
761:
762: my $minute = 60;
763: my $hour = 60 * $minute;
764: my $day = 24 * $hour;
765: my $week = 7 * $day;
766: my $inPast = 0;
767:
768: # Logic in comments:
769: # Is it now? (extremely unlikely)
770: if ( $delta == 0 ) {
771: return "this instant";
772: }
773:
774: if ($delta < 0) {
775: $inPast = 1;
776: $delta = -$delta;
777: }
778:
779: if ( $delta > 0 ) {
1.101 bowersj2 780:
1.73 bowersj2 781: my $tense = $inPast ? " ago" : "";
782: my $prefix = $inPast ? "" : "in ";
1.101 bowersj2 783:
784: # Less then a minute
1.73 bowersj2 785: if ( $delta < $minute ) {
786: if ($delta == 1) { return "${prefix}1 second$tense"; }
787: return "$prefix$delta seconds$tense";
788: }
789:
1.101 bowersj2 790: # Less then an hour
1.73 bowersj2 791: if ( $delta < $hour ) {
792: # If so, use minutes
793: my $minutes = floor($delta / 60);
794: if ($minutes == 1) { return "${prefix}1 minute$tense"; }
795: return "$prefix$minutes minutes$tense";
796: }
797:
798: # Is it less then 24 hours away? If so,
799: # display hours + minutes
800: if ( $delta < $hour * 24) {
801: my $hours = floor($delta / $hour);
802: my $minutes = floor(($delta % $hour) / $minute);
803: my $hourString = "$hours hours";
804: my $minuteString = ", $minutes minutes";
805: if ($hours == 1) {
806: $hourString = "1 hour";
807: }
808: if ($minutes == 1) {
809: $minuteString = ", 1 minute";
810: }
811: if ($minutes == 0) {
812: $minuteString = "";
813: }
814: return "$prefix$hourString$minuteString$tense";
815: }
816:
817: # Less then 5 days away, display day of the week and
818: # HH:MM
819: if ( $delta < $day * 5 ) {
1.85 bowersj2 820: my $timeStr = strftime("%A, %b %e at %I:%M %P", localtime($time));
1.73 bowersj2 821: $timeStr =~ s/12:00 am/midnight/;
822: $timeStr =~ s/12:00 pm/noon/;
823: return ($inPast ? "last " : "next ") .
824: $timeStr;
825: }
826:
827: # Is it this year?
828: if ( $time[5] == $now[5]) {
829: # Return on Month Day, HH:MM meridian
830: my $timeStr = strftime("on %A, %b %e at %I:%M %P", localtime($time));
831: $timeStr =~ s/12:00 am/midnight/;
832: $timeStr =~ s/12:00 pm/noon/;
833: return $timeStr;
834: }
835:
836: # Not this year, so show the year
837: my $timeStr = strftime("on %A, %b %e %G at %I:%M %P", localtime($time));
838: $timeStr =~ s/12:00 am/midnight/;
839: $timeStr =~ s/12:00 pm/noon/;
840: return $timeStr;
1.58 albertel 841: }
1.53 bowersj2 842: }
843:
1.51 bowersj2 844: 1;
845:
846: package Apache::lonnavmaps::navmap;
847:
848: =pod
849:
850: lonnavmaps provides functions and objects for dealing with the compiled course hashes generated when a user enters the course, and also provides the Apache handler for the "Navigation Map" button.
851:
852: =head1 navmap object: Encapsulating the compiled nav map
853:
854: navmap is an object that encapsulates a compiled course map and provides a reasonable interface to it.
855:
856: Most notably it provides a way to navigate the map sensibly and a flexible iterator that makes it easy to write various renderers based on nav maps.
857:
858: You must obtain resource objects through the navmap object.
859:
860: =head2 Methods
861:
862: =over 4
863:
1.70 bowersj2 864: =item * B<new>(navHashFile, parmHashFile, genCourseAndUserOptions, genMailDiscussStatus): Binds a new navmap object to the compiled nav map hash and parm hash given as filenames. genCourseAndUserOptions is a flag saying whether the course options and user options hash should be generated. This is for when you are using the parameters of the resources that require them; see documentation in resource object documentation. genMailDiscussStatus causes the nav map to retreive information about the email and discussion status of resources. Returns the navmap object if this is successful, or B<undef> if not. You must check for undef; errors will occur when you try to use the other methods otherwise.
1.51 bowersj2 865:
866: =item * B<getIterator>(first, finish, filter, condition): See iterator documentation below.
867:
868: =cut
869:
870: use strict;
871: use GDBM_File;
872:
873: sub new {
874: # magic invocation to create a class instance
875: my $proto = shift;
876: my $class = ref($proto) || $proto;
877: my $self = {};
878:
879: $self->{NAV_HASH_FILE} = shift;
880: $self->{PARM_HASH_FILE} = shift;
881: $self->{GENERATE_COURSE_USER_OPT} = shift;
1.55 bowersj2 882: $self->{GENERATE_EMAIL_DISCUSS_STATUS} = shift;
1.51 bowersj2 883:
1.101 bowersj2 884: # Resource cache stores navmap resources as we reference them. We generate
1.51 bowersj2 885: # them on-demand so we don't pay for creating resources unless we use them.
886: $self->{RESOURCE_CACHE} = {};
887:
888: # Network failure flag, if we accessed the course or user opt and
889: # failed
890: $self->{NETWORK_FAILURE} = 0;
891:
892: # tie the nav hash
893: my %navmaphash;
894: if (!(tie(%navmaphash, 'GDBM_File', $self->{NAV_HASH_FILE},
895: &GDBM_READER(), 0640))) {
896: return undef;
897: }
898: $self->{NAV_HASH} = \%navmaphash;
899:
900: my %parmhash;
901: if (!(tie(%parmhash, 'GDBM_File', $self->{PARM_HASH_FILE},
902: &GDBM_READER(), 0640)))
903: {
1.66 bowersj2 904: untie $self->{PARM_HASH};
1.51 bowersj2 905: return undef;
906: }
907: $self->{PARM_HASH} = \%parmhash;
908: $self->{HASH_TIED} = 1;
909:
1.92 bowersj2 910: bless($self);
911:
912: return $self;
913: }
914:
915: sub init {
1.93 bowersj2 916: my $self = shift;
1.92 bowersj2 917:
1.51 bowersj2 918: # If the course opt hash and the user opt hash should be generated,
919: # generate them
920: if ($self->{GENERATE_COURSE_USER_OPT}) {
921: my $uname=$ENV{'user.name'};
922: my $udom=$ENV{'user.domain'};
923: my $uhome=$ENV{'user.home'};
924: my $cid=$ENV{'request.course.id'};
925: my $chome=$ENV{'course.'.$cid.'.home'};
926: my ($cdom,$cnum)=split(/\_/,$cid);
927:
928: my $userprefix=$uname.'_'.$udom.'_';
929:
1.99 bowersj2 930: my %courserdatas; my %useropt; my %courseopt; my %userrdatas;
1.51 bowersj2 931: unless ($uhome eq 'no_host') {
932: # ------------------------------------------------- Get coursedata (if present)
933: unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
934: my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
935: ':resourcedata',$chome);
936: if ($reply!~/^error\:/) {
937: $courserdatas{$cid}=$reply;
938: $courserdatas{$cid.'.last_cache'}=time;
939: }
940: # check to see if network failed
941: elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
942: {
943: $self->{NETWORK_FAILURE} = 1;
944: }
945: }
946: foreach (split(/\&/,$courserdatas{$cid})) {
947: my ($name,$value)=split(/\=/,$_);
948: $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
949: &Apache::lonnet::unescape($value);
950: }
951: # --------------------------------------------------- Get userdata (if present)
952: unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
953: my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
954: if ($reply!~/^error\:/) {
955: $userrdatas{$uname.'___'.$udom}=$reply;
956: $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
957: }
958: # check to see if network failed
959: elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
960: {
961: $self->{NETWORK_FAILURE} = 1;
962: }
963: }
964: foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
965: my ($name,$value)=split(/\=/,$_);
966: $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
967: &Apache::lonnet::unescape($value);
968: }
1.55 bowersj2 969: $self->{COURSE_OPT} = \%courseopt;
970: $self->{USER_OPT} = \%useropt;
1.51 bowersj2 971: }
972: }
973:
1.55 bowersj2 974: if ($self->{GENERATE_EMAIL_DISCUSS_STATUS}) {
975: my $cid=$ENV{'request.course.id'};
976: my ($cdom,$cnum)=split(/\_/,$cid);
977:
978: my %emailstatus = &Apache::lonnet::dump('email_status');
1.56 bowersj2 979: my $logoutTime = $emailstatus{'logout'};
980: my $courseLeaveTime = $emailstatus{'logout_'.$ENV{'request.course.id'}};
1.55 bowersj2 981: $self->{LAST_CHECK} = ($courseLeaveTime > $logoutTime ?
982: $courseLeaveTime : $logoutTime);
1.56 bowersj2 983: my %discussiontime = &Apache::lonnet::dump('discussiontimes',
1.55 bowersj2 984: $cdom, $cnum);
985: my %feedback=();
986: my %error=();
987: my $keys = &Apache::lonnet::reply('keys:'.
988: $ENV{'user.domain'}.':'.
989: $ENV{'user.name'}.':nohist_email',
990: $ENV{'user.home'});
991:
992: foreach my $msgid (split(/\&/, $keys)) {
993: $msgid=&Apache::lonnet::unescape($msgid);
994: my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
995: if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
996: my ($what,$url)=($1,$2);
997: my %status=
998: &Apache::lonnet::get('email_status',[$msgid]);
999: if ($status{$msgid}=~/^error\:/) {
1000: $status{$msgid}='';
1001: }
1002:
1003: if (($status{$msgid} eq 'new') ||
1004: (!$status{$msgid})) {
1005: if ($what eq 'Error') {
1006: $error{$url}.=','.$msgid;
1007: } else {
1008: $feedback{$url}.=','.$msgid;
1009: }
1010: }
1011: }
1012: }
1013:
1014: $self->{FEEDBACK} = \%feedback;
1015: $self->{ERROR_MSG} = \%error; # what is this? JB
1016: $self->{DISCUSSION_TIME} = \%discussiontime;
1017: $self->{EMAIL_STATUS} = \%emailstatus;
1018:
1019: }
1.84 bowersj2 1020:
1021: $self->{PARM_CACHE} = {};
1.55 bowersj2 1022: }
1.51 bowersj2 1023:
1.55 bowersj2 1024: # Checks to see if coursemap is defined, matching test in old lonnavmaps
1025: sub courseMapDefined {
1026: my $self = shift;
1027: my $uri = &Apache::lonnet::clutter($ENV{'request.course.uri'});
1028:
1.56 bowersj2 1029: my $firstres = $self->{NAV_HASH}->{"map_start_$uri"};
1030: my $lastres = $self->{NAV_HASH}->{"map_finish_$uri"};
1.55 bowersj2 1031: return $firstres && $lastres;
1.51 bowersj2 1032: }
1033:
1034: sub getIterator {
1035: my $self = shift;
1.98 bowersj2 1036: my $iterator = Apache::lonnavmaps::iterator->new($self, shift, shift,
1.101 bowersj2 1037: shift, undef, shift);
1.51 bowersj2 1038: return $iterator;
1039: }
1040:
1041: # unties the hash when done
1042: sub untieHashes {
1043: my $self = shift;
1.60 bowersj2 1044: untie %{$self->{NAV_HASH}} if ($self->{HASH_TIED});
1045: untie %{$self->{PARM_HASH}} if ($self->{HASH_TIED});
1.51 bowersj2 1046: $self->{HASH_TIED} = 0;
1047: }
1048:
1049: # when the object is destroyed, be sure to untie all the hashes we tied.
1050: sub DESTROY {
1051: my $self = shift;
1052: $self->untieHashes();
1053: }
1054:
1.59 bowersj2 1055: # Private function: Does the given resource (as a symb string) have
1056: # current discussion? Returns 0 if chat/mail data not extracted.
1057: sub hasDiscussion {
1058: my $self = shift;
1059: my $symb = shift;
1060: if (!defined($self->{DISCUSSION_TIME})) { return 0; }
1061:
1062: return $self->{DISCUSSION_TIME}->{$symb} >
1.66 bowersj2 1063: $self->{LAST_CHECK};
1.59 bowersj2 1064: }
1065:
1066: # Private function: Does the given resource (as a symb string) have
1067: # current feedback? Returns the string in the feedback hash, which
1068: # will be false if it does not exist.
1069: sub getFeedback {
1070: my $self = shift;
1071: my $symb = shift;
1072:
1073: if (!defined($self->{FEEDBACK})) { return ""; }
1074:
1075: return $self->{FEEDBACK}->{$symb};
1076: }
1077:
1.51 bowersj2 1078: =pod
1079:
1080: =item * B<getById>(id): Based on the ID of the resource (1.1, 3.2, etc.), get a resource object for that resource. This method, or other methods that use it (as in the resource object) is the only proper way to obtain a resource object.
1081:
1082: =cut
1083:
1084: # The strategy here is to cache the resource objects, and only construct them
1085: # as we use them. The real point is to prevent reading any more from the tied
1086: # hash then we have to, which should hopefully alleviate speed problems.
1087: # Caching is just an incidental detail I throw in because it makes sense.
1088:
1089: sub getById {
1090: my $self = shift;
1091: my $id = shift;
1092:
1093: if (defined ($self->{RESOURCE_CACHE}->{$id}))
1094: {
1095: return $self->{RESOURCE_CACHE}->{$id};
1096: }
1097:
1098: # resource handles inserting itself into cache.
1099: return Apache::lonnavmaps::resource->new($self, $id);
1100: }
1101:
1102: =pod
1103:
1104: =item * B<firstResource>(): Returns a resource object reference corresponding to the first resource in the navmap.
1105:
1106: =cut
1107:
1108: sub firstResource {
1109: my $self = shift;
1110: my $firstResource = $self->{NAV_HASH}->{'map_start_' .
1111: &Apache::lonnet::clutter($ENV{'request.course.uri'})};
1112: return $self->getById($firstResource);
1113: }
1114:
1115: =pod
1116:
1117: =item * B<finishResource>(): Returns a resource object reference corresponding to the last resource in the navmap.
1118:
1119: =cut
1120:
1121: sub finishResource {
1122: my $self = shift;
1123: my $firstResource = $self->{NAV_HASH}->{'map_finish_' .
1124: &Apache::lonnet::clutter($ENV{'request.course.uri'})};
1125: return $self->getById($firstResource);
1126: }
1127:
1.101 bowersj2 1128: # Parmval reads the parm hash and cascades the lookups. parmval_real does
1129: # the actual lookup; parmval caches the results.
1.51 bowersj2 1130: sub parmval {
1131: my $self = shift;
1132: my ($what,$symb)=@_;
1.84 bowersj2 1133: my $hashkey = $what."|||".$symb;
1134:
1135: if (defined($self->{PARM_CACHE}->{$hashkey})) {
1136: return $self->{PARM_CACHE}->{$hashkey};
1137: }
1138:
1139: my $result = $self->parmval_real($what, $symb);
1140: $self->{PARM_CACHE}->{$hashkey} = $result;
1141: return $result;
1142: }
1143:
1144: sub parmval_real {
1145: my $self = shift;
1146: my ($what,$symb) = @_;
1147:
1.51 bowersj2 1148: my $cid=$ENV{'request.course.id'};
1149: my $csec=$ENV{'request.course.sec'};
1150: my $uname=$ENV{'user.name'};
1151: my $udom=$ENV{'user.domain'};
1152:
1153: unless ($symb) { return ''; }
1154: my $result='';
1155:
1156: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
1157:
1158: # ----------------------------------------------------- Cascading lookup scheme
1159: my $rwhat=$what;
1160: $what=~s/^parameter\_//;
1161: $what=~s/\_/\./;
1162:
1163: my $symbparm=$symb.'.'.$what;
1164: my $mapparm=$mapname.'___(all).'.$what;
1165: my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
1166:
1167: my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
1168: my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
1169: my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
1170:
1171: my $courselevel= $usercourseprefix.'.'.$what;
1172: my $courselevelr=$usercourseprefix.'.'.$symbparm;
1173: my $courselevelm=$usercourseprefix.'.'.$mapparm;
1174:
1175: my $useropt = $self->{USER_OPT};
1176: my $courseopt = $self->{COURSE_OPT};
1177: my $parmhash = $self->{PARM_HASH};
1178:
1179: # ---------------------------------------------------------- first, check user
1180: if ($uname and defined($useropt)) {
1.57 albertel 1181: if (defined($$useropt{$courselevelr})) { return $$useropt{$courselevelr}; }
1182: if (defined($$useropt{$courselevelm})) { return $$useropt{$courselevelm}; }
1183: if (defined($$useropt{$courselevel})) { return $$useropt{$courselevel}; }
1.51 bowersj2 1184: }
1185:
1186: # ------------------------------------------------------- second, check course
1187: if ($csec and defined($courseopt)) {
1.57 albertel 1188: if (defined($$courseopt{$seclevelr})) { return $$courseopt{$seclevelr}; }
1189: if (defined($$courseopt{$seclevelm})) { return $$courseopt{$seclevelm}; }
1190: if (defined($$courseopt{$seclevel})) { return $$courseopt{$seclevel}; }
1.51 bowersj2 1191: }
1192:
1193: if (defined($courseopt)) {
1.57 albertel 1194: if (defined($$courseopt{$courselevelr})) { return $$courseopt{$courselevelr}; }
1195: if (defined($$courseopt{$courselevelm})) { return $$courseopt{$courselevelm}; }
1196: if (defined($$courseopt{$courselevel})) { return $$courseopt{$courselevel}; }
1.51 bowersj2 1197: }
1198:
1199: # ----------------------------------------------------- third, check map parms
1200:
1201: my $thisparm=$$parmhash{$symbparm};
1.57 albertel 1202: if (defined($thisparm)) { return $thisparm; }
1.51 bowersj2 1203:
1204: # ----------------------------------------------------- fourth , check default
1205:
1206: my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
1.57 albertel 1207: if (defined($default)) { return $default}
1.51 bowersj2 1208:
1209: # --------------------------------------------------- fifth , cascade up parts
1210:
1211: my ($space,@qualifier)=split(/\./,$rwhat);
1212: my $qualifier=join('.',@qualifier);
1213: unless ($space eq '0') {
1214: my ($part,$id)=split(/\_/,$space);
1215: if ($id) {
1216: my $partgeneral=$self->parmval($part.".$qualifier",$symb);
1.57 albertel 1217: if (defined($partgeneral)) { return $partgeneral; }
1.51 bowersj2 1218: } else {
1219: my $resourcegeneral=$self->parmval("0.$qualifier",$symb);
1.57 albertel 1220: if (defined($resourcegeneral)) { return $resourcegeneral; }
1.51 bowersj2 1221: }
1222: }
1223: return '';
1224: }
1225:
1226:
1227: 1;
1228:
1229: package Apache::lonnavmaps::iterator;
1230:
1231: =pod
1232:
1233: =back
1234:
1235: =head1 navmap Iterator
1236:
1237: An I<iterator> encapsulates the logic required to traverse a data structure. navmap uses an iterator to traverse the course map according to the criteria you wish to use.
1238:
1239: To obtain an iterator, call the B<getIterator>() function of a B<navmap> object. (Do not instantiate Apache::lonnavmaps::iterator directly.) This will return a reference to the iterator:
1240:
1241: C<my $resourceIterator = $navmap-E<gt>getIterator();>
1242:
1243: To get the next thing from the iterator, call B<next>:
1244:
1245: C<my $nextThing = $resourceIterator-E<gt>next()>
1246:
1247: getIterator behaves as follows:
1248:
1249: =over 4
1250:
1.101 bowersj2 1251: =item * B<getIterator>(firstResource, finishResource, filterHash, condition): All parameters are optional. firstResource is a resource reference corresponding to where the iterator should start. It defaults to navmap->firstResource() for the corresponding nav map. finishResource corresponds to where you want the iterator to end, defaulting to navmap->finishResource(). filterHash is a hash used as a set containing strings representing the resource IDs, defaulting to empty. Condition is a 1 or 0 that sets what to do with the filter hash: If a 0, then only resource that exist IN the filterHash will be recursed on. If it is a 1, only resources NOT in the filterHash will be recursed on. Defaults to 0.
1.51 bowersj2 1252:
1.101 bowersj2 1253: Thus, by default, only top-level resources will be shown. Change the condition to a 1 without changing the hash, and all resources will be shown. Changing the condition to 1 and including some values in the hash will allow you to selectively suppress parts of the navmap, while leaving it on 0 and adding things to the hash will allow you to selectively add parts of the nav map. See the handler code for examples.
1.51 bowersj2 1254:
1255: The iterator will return either a reference to a resource object, or a token representing something in the map, such as the beginning of a new branch. The possible tokens are:
1256:
1257: =over 4
1258:
1.70 bowersj2 1259: =item * BEGIN_MAP: A new map is being recursed into. This is returned I<after> the map resource itself is returned.
1260:
1261: =item * END_MAP: The map is now done.
1262:
1263: =item * BEGIN_BRANCH: A branch is now starting. The next resource returned will be the first in that branch.
1264:
1265: =item * END_BRANCH: The branch is now done.
1.51 bowersj2 1266:
1267: =back
1268:
1.70 bowersj2 1269: The tokens are retreivable via methods on the iterator object, i.e., $iterator->END_MAP.
1270:
1271: =back
1.51 bowersj2 1272:
1273: =cut
1274:
1275: # Here are the tokens for the iterator:
1276:
1277: sub BEGIN_MAP { return 1; } # begining of a new map
1278: sub END_MAP { return 2; } # end of the map
1279: sub BEGIN_BRANCH { return 3; } # beginning of a branch
1280: sub END_BRANCH { return 4; } # end of a branch
1.89 bowersj2 1281: sub FORWARD { return 1; } # go forward
1282: sub BACKWARD { return 2; }
1.51 bowersj2 1283:
1.96 bowersj2 1284: sub min {
1285: (my $a, my $b) = @_;
1286: if ($a < $b) { return $a; } else { return $b; }
1287: }
1288:
1.107 bowersj2 1289: # In the CVS repository, documentation of this algorithm is included
1290: # in /doc/lonnavdocs, as a PDF and .tex source. Markers like **1**
1291: # will reference the same location in the text as the part of the
1292: # algorithm is running through.
1293:
1.94 bowersj2 1294: sub new {
1295: # magic invocation to create a class instance
1296: my $proto = shift;
1297: my $class = ref($proto) || $proto;
1298: my $self = {};
1299:
1300: $self->{NAV_MAP} = shift;
1301: return undef unless ($self->{NAV_MAP});
1302:
1303: # Handle the parameters
1304: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
1305: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
1306:
1307: # If the given resources are just the ID of the resource, get the
1308: # objects
1309: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
1310: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
1311: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
1312: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
1313:
1314: $self->{FILTER} = shift;
1315:
1316: # A hash, used as a set, of resource already seen
1317: $self->{ALREADY_SEEN} = shift;
1318: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1319: $self->{CONDITION} = shift;
1320:
1321: # Now, we need to pre-process the map, by walking forward and backward
1322: # over the parts of the map we're going to look at.
1.96 bowersj2 1323:
1.97 bowersj2 1324: # The processing steps are exactly the same, except for a few small
1325: # changes, so I bundle those up in the following list of two elements:
1326: # (direction_to_iterate, VAL_name, next_resource_method_to_call,
1327: # first_resource).
1328: # This prevents writing nearly-identical code twice.
1329: my @iterations = ( [FORWARD(), 'TOP_DOWN_VAL', 'getNext',
1330: 'FIRST_RESOURCE'],
1331: [BACKWARD(), 'BOT_UP_VAL', 'getPrevious',
1332: 'FINISH_RESOURCE'] );
1333:
1.98 bowersj2 1334: my $maxDepth = 0; # tracks max depth
1335:
1.107 bowersj2 1336: # **1**
1337:
1.97 bowersj2 1338: foreach my $pass (@iterations) {
1339: my $direction = $pass->[0];
1340: my $valName = $pass->[1];
1341: my $nextResourceMethod = $pass->[2];
1342: my $firstResourceName = $pass->[3];
1343:
1344: my $iterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP},
1345: $self->{FIRST_RESOURCE},
1346: $self->{FINISH_RESOURCE},
1347: {}, undef, 0, $direction);
1.96 bowersj2 1348:
1.97 bowersj2 1349: # prime the recursion
1350: $self->{$firstResourceName}->{DATA}->{$valName} = 0;
1.98 bowersj2 1351: my $depth = 0;
1.97 bowersj2 1352: $iterator->next();
1353: my $curRes = $iterator->next();
1.98 bowersj2 1354: while ($depth > -1) {
1.97 bowersj2 1355: if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
1356: if ($curRes == $iterator->END_MAP()) { $depth--; }
1.96 bowersj2 1357:
1.97 bowersj2 1358: if (ref($curRes)) {
1359: my $resultingVal = $curRes->{DATA}->{$valName};
1360: my $nextResources = $curRes->$nextResourceMethod();
1361: my $resourceCount = scalar(@{$nextResources});
1.104 bowersj2 1362:
1.107 bowersj2 1363: if ($resourceCount == 1) { # **3**
1.97 bowersj2 1364: my $current = $nextResources->[0]->{DATA}->{$valName} || 999999999;
1365: $nextResources->[0]->{DATA}->{$valName} = min($resultingVal, $current);
1366: }
1367:
1.107 bowersj2 1368: if ($resourceCount > 1) { # **4**
1.97 bowersj2 1369: foreach my $res (@{$nextResources}) {
1370: my $current = $res->{DATA}->{$valName} || 999999999;
1371: $res->{DATA}->{$valName} = min($current, $resultingVal + 1);
1372: }
1373: }
1374: }
1.96 bowersj2 1375:
1.107 bowersj2 1376: # Assign the final val (**2**)
1.97 bowersj2 1377: if (ref($curRes) && $direction == BACKWARD()) {
1.98 bowersj2 1378: my $finalDepth = min($curRes->{DATA}->{TOP_DOWN_VAL},
1379: $curRes->{DATA}->{BOT_UP_VAL});
1380:
1381: $curRes->{DATA}->{DISPLAY_DEPTH} = $finalDepth;
1382: if ($finalDepth > $maxDepth) {$maxDepth = $finalDepth;}
1383: }
1.97 bowersj2 1384: $curRes = $iterator->next();
1.96 bowersj2 1385: }
1386: }
1.94 bowersj2 1387:
1.98 bowersj2 1388: # Set up some bookkeeping information.
1389: $self->{CURRENT_DEPTH} = 0;
1390: $self->{MAX_DEPTH} = $maxDepth;
1391: $self->{STACK} = [];
1392: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1393:
1394: for (my $i = 0; $i <= $self->{MAX_DEPTH}; $i++) {
1395: push @{$self->{STACK}}, [];
1396: }
1397:
1.107 bowersj2 1398: # Prime the recursion w/ the first resource **5**
1.98 bowersj2 1399: push @{$self->{STACK}->[0]}, $self->{FIRST_RESOURCE};
1400: $self->{ALREADY_SEEN}->{$self->{FIRST_RESOURCE}->{ID}} = 1;
1401:
1402: bless ($self);
1403:
1404: return $self;
1405: }
1406:
1407: sub next {
1408: my $self = shift;
1409:
1410: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1411: # grab the next from the recursive iterator
1412: my $next = $self->{RECURSIVE_ITERATOR}->next();
1413:
1414: # is it a begin or end map? If so, update the depth
1415: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
1416: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
1417:
1418: # Are we back at depth 0? If so, stop recursing
1419: if ($self->{RECURSIVE_DEPTH} == 0) {
1420: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1421: }
1422:
1423: return $next;
1424: }
1425:
1426: if (defined($self->{FORCE_NEXT})) {
1427: my $tmp = $self->{FORCE_NEXT};
1428: $self->{FORCE_NEXT} = undef;
1429: return $tmp;
1430: }
1431:
1432: # Have we not yet begun? If not, return BEGIN_MAP and
1433: # remember we've started.
1434: if ( !$self->{STARTED} ) {
1435: $self->{STARTED} = 1;
1436: return $self->BEGIN_MAP();
1437: }
1438:
1439: # Here's the guts of the iterator.
1440:
1441: # Find the next resource, if any.
1442: my $found = 0;
1443: my $i = $self->{MAX_DEPTH};
1444: my $newDepth;
1445: my $here;
1446: while ( $i >= 0 && !$found ) {
1.107 bowersj2 1447: if ( scalar(@{$self->{STACK}->[$i]}) > 0 ) { # **6**
1448: $here = pop @{$self->{STACK}->[$i]}; # **7**
1.98 bowersj2 1449: $found = 1;
1450: $newDepth = $i;
1451: }
1452: $i--;
1453: }
1454:
1455: # If we still didn't find anything, we're done.
1456: if ( !$found ) {
1457: # We need to get back down to the correct branch depth
1458: if ( $self->{CURRENT_DEPTH} > 0 ) {
1459: $self->{CURRENT_DEPTH}--;
1460: return END_BRANCH();
1461: } else {
1462: return END_MAP();
1463: }
1464: }
1465:
1.104 bowersj2 1466: # If this is not a resource, it must be an END_BRANCH marker we want
1467: # to return directly.
1.107 bowersj2 1468: if (!ref($here)) { # **8**
1.104 bowersj2 1469: if ($here == END_BRANCH()) { # paranoia, in case of later extension
1470: $self->{CURRENT_DEPTH}--;
1471: return $here;
1472: }
1473: }
1474:
1475: # Otherwise, it is a resource and it's safe to store in $self->{HERE}
1476: $self->{HERE} = $here;
1477:
1.98 bowersj2 1478: # Get to the right level
1479: if ( $self->{CURRENT_DEPTH} > $newDepth ) {
1480: push @{$self->{STACK}->[$newDepth]}, $here;
1481: $self->{CURRENT_DEPTH}--;
1482: return END_BRANCH();
1483: }
1484: if ( $self->{CURRENT_DEPTH} < $newDepth) {
1485: push @{$self->{STACK}->[$newDepth]}, $here;
1486: $self->{CURRENT_DEPTH}++;
1487: return BEGIN_BRANCH();
1488: }
1489:
1490: # If we made it here, we have the next resource, and we're at the
1491: # right branch level. So let's examine the resource for where
1492: # we can get to from here.
1493:
1494: # So we need to look at all the resources we can get to from here,
1495: # categorize them if we haven't seen them, remember if we have a new
1496: my $nextUnfiltered = $here->getNext();
1.104 bowersj2 1497: my $maxDepthAdded = -1;
1498:
1.98 bowersj2 1499: for (@$nextUnfiltered) {
1500: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
1.104 bowersj2 1501: my $depth = $_->{DATA}->{DISPLAY_DEPTH};
1502: push @{$self->{STACK}->[$depth]}, $_;
1.98 bowersj2 1503: $self->{ALREADY_SEEN}->{$_->{ID}} = 1;
1.104 bowersj2 1504: if ($maxDepthAdded < $depth) { $maxDepthAdded = $depth; }
1.98 bowersj2 1505: }
1506: }
1.104 bowersj2 1507:
1508: # Is this the end of a branch? If so, all of the resources examined above
1509: # led to lower levels then the one we are currently at, so we push a END_BRANCH
1510: # marker onto the stack so we don't forget.
1511: # Example: For the usual A(BC)(DE)F case, when the iterator goes down the
1512: # BC branch and gets to C, it will see F as the only next resource, but it's
1513: # one level lower. Thus, this is the end of the branch, since there are no
1514: # more resources added to this level or above.
1.111 ! bowersj2 1515: # We don't do this if the examined resource is the finish resource,
! 1516: # because the condition given above is true, but the "END_MAP" will
! 1517: # take care of things and we should already be at depth 0.
1.104 bowersj2 1518: my $isEndOfBranch = $maxDepthAdded < $self->{CURRENT_DEPTH};
1.111 ! bowersj2 1519: if ($isEndOfBranch && $here != $self->{FINISH_RESOURCE}) { # **9**
1.104 bowersj2 1520: push @{$self->{STACK}->[$self->{CURRENT_DEPTH}]}, END_BRANCH();
1521: }
1522:
1.98 bowersj2 1523: # That ends the main iterator logic. Now, do we want to recurse
1524: # down this map (if this resource is a map)?
1525: if ($self->{HERE}->is_map() &&
1526: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
1527: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
1528: my $firstResource = $self->{HERE}->map_start();
1529: my $finishResource = $self->{HERE}->map_finish();
1530:
1531: $self->{RECURSIVE_ITERATOR} =
1532: Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
1533: $finishResource, $self->{FILTER},
1534: $self->{ALREADY_SEEN}, $self->{CONDITION});
1535: }
1536:
1537: return $self->{HERE};
1538:
1539: }
1540:
1541: =pod
1542:
1543: The other method available on the iterator is B<getStack>, which returns an array populated with the current 'stack' of maps, as references to the resource objects. Example: This is useful when making the navigation map, as we need to check whether we are under a page map to see if we need to link directly to the resource, or to the page. The first elements in the array will correspond to the top of the stack (most inclusive map).
1544:
1545: =cut
1546:
1547: sub getStack {
1548: my $self=shift;
1549:
1550: my @stack;
1551:
1552: $self->populateStack(\@stack);
1553:
1554: return \@stack;
1555: }
1556:
1557: # Private method: Calls the iterators recursively to populate the stack.
1558: sub populateStack {
1559: my $self=shift;
1560: my $stack = shift;
1561:
1562: push @$stack, $self->{HERE} if ($self->{HERE});
1563:
1564: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1565: $self->{RECURSIVE_ITERATOR}->populateStack($stack);
1566: }
1.94 bowersj2 1567: }
1568:
1569: 1;
1570:
1571: package Apache::lonnavmaps::DFSiterator;
1572:
1.100 bowersj2 1573: # Not documented in the perldoc: This is a simple iterator that just walks
1574: # through the nav map and presents the resources in a depth-first search
1575: # fashion, ignorant of conditionals, randomized resources, etc. It presents
1576: # BEGIN_MAP and END_MAP, but does not understand branches at all. It is
1577: # useful for pre-processing of some kind, and is in fact used by the main
1578: # iterator that way, but that's about it.
1579: # One could imagine merging this into the init routine of the main iterator,
1580: # but this might as well be left seperate, since it is possible some other
1581: # use might be found for it. - Jeremy
1.94 bowersj2 1582:
1583: sub BEGIN_MAP { return 1; } # begining of a new map
1584: sub END_MAP { return 2; } # end of the map
1585: sub FORWARD { return 1; } # go forward
1586: sub BACKWARD { return 2; }
1587:
1.100 bowersj2 1588: # Params: Nav map ref, first resource id/ref, finish resource id/ref,
1589: # filter hash ref (or undef), already seen hash or undef, condition
1590: # (as in main iterator), direction FORWARD or BACKWARD (undef->forward).
1.51 bowersj2 1591: sub new {
1592: # magic invocation to create a class instance
1593: my $proto = shift;
1594: my $class = ref($proto) || $proto;
1595: my $self = {};
1596:
1597: $self->{NAV_MAP} = shift;
1598: return undef unless ($self->{NAV_MAP});
1599:
1600: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
1601: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
1602:
1603: # If the given resources are just the ID of the resource, get the
1604: # objects
1605: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
1606: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
1607: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
1608: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
1609:
1610: $self->{FILTER} = shift;
1611:
1612: # A hash, used as a set, of resource already seen
1613: $self->{ALREADY_SEEN} = shift;
1614: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1.63 bowersj2 1615: $self->{CONDITION} = shift;
1.89 bowersj2 1616: $self->{DIRECTION} = shift || FORWARD();
1.51 bowersj2 1617:
1.100 bowersj2 1618: # Flag: Have we started yet?
1.51 bowersj2 1619: $self->{STARTED} = 0;
1620:
1621: # Should we continue calling the recursive iterator, if any?
1622: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1623: # The recursive iterator, if any
1624: $self->{RECURSIVE_ITERATOR} = undef;
1625: # Are we recursing on a map, or a branch?
1626: $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
1627: # And the count of how deep it is, so that this iterator can keep track of
1628: # when to pick back up again.
1629: $self->{RECURSIVE_DEPTH} = 0;
1630:
1631: # For keeping track of our branches, we maintain our own stack
1.100 bowersj2 1632: $self->{STACK} = [];
1.51 bowersj2 1633:
1634: # Start with the first resource
1.89 bowersj2 1635: if ($self->{DIRECTION} == FORWARD) {
1.100 bowersj2 1636: push @{$self->{STACK}}, $self->{FIRST_RESOURCE};
1.89 bowersj2 1637: } else {
1.100 bowersj2 1638: push @{$self->{STACK}}, $self->{FINISH_RESOURCE};
1.89 bowersj2 1639: }
1.51 bowersj2 1640:
1641: bless($self);
1642: return $self;
1643: }
1644:
1645: sub next {
1646: my $self = shift;
1647:
1648: # Are we using a recursive iterator? If so, pull from that and
1649: # watch the depth; we want to resume our level at the correct time.
1.98 bowersj2 1650: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1.51 bowersj2 1651: # grab the next from the recursive iterator
1652: my $next = $self->{RECURSIVE_ITERATOR}->next();
1653:
1654: # is it a begin or end map? Update depth if so
1655: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
1656: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
1657:
1658: # Are we back at depth 0? If so, stop recursing.
1659: if ($self->{RECURSIVE_DEPTH} == 0) {
1660: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1661: }
1662:
1663: return $next;
1664: }
1665:
1666: # Is there a current resource to grab? If not, then return
1.100 bowersj2 1667: # END_MAP, which will end the iterator.
1668: if (scalar(@{$self->{STACK}}) == 0) {
1669: return $self->END_MAP();
1.51 bowersj2 1670: }
1671:
1672: # Have we not yet begun? If not, return BEGIN_MAP and
1673: # remember that we've started.
1674: if ( !$self->{STARTED} ) {
1675: $self->{STARTED} = 1;
1676: return $self->BEGIN_MAP;
1677: }
1678:
1679: # Get the next resource in the branch
1.100 bowersj2 1680: $self->{HERE} = pop @{$self->{STACK}};
1.52 bowersj2 1681:
1.100 bowersj2 1682: # remember that we've seen this, so we don't return it again later
1.51 bowersj2 1683: $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
1684:
1685: # Get the next possible resources
1.90 bowersj2 1686: my $nextUnfiltered;
1.89 bowersj2 1687: if ($self->{DIRECTION} == FORWARD()) {
1.90 bowersj2 1688: $nextUnfiltered = $self->{HERE}->getNext();
1.89 bowersj2 1689: } else {
1.90 bowersj2 1690: $nextUnfiltered = $self->{HERE}->getPrevious();
1.89 bowersj2 1691: }
1.51 bowersj2 1692: my $next = [];
1693:
1694: # filter the next possibilities to remove things we've
1.100 bowersj2 1695: # already seen.
1.51 bowersj2 1696: foreach (@$nextUnfiltered) {
1.52 bowersj2 1697: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
1698: push @$next, $_;
1699: }
1.51 bowersj2 1700: }
1701:
1702: while (@$next) {
1.100 bowersj2 1703: # copy the next possibilities over to the stack
1704: push @{$self->{STACK}}, shift @$next;
1.51 bowersj2 1705: }
1706:
1707: # If this is a map and we want to recurse down it... (not filtered out)
1.70 bowersj2 1708: if ($self->{HERE}->is_map() &&
1.63 bowersj2 1709: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
1.51 bowersj2 1710: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
1711: my $firstResource = $self->{HERE}->map_start();
1712: my $finishResource = $self->{HERE}->map_finish();
1713:
1714: $self->{RECURSIVE_ITERATOR} =
1.94 bowersj2 1715: Apache::lonnavmaps::DFSiterator->new ($self->{NAV_MAP}, $firstResource,
1.63 bowersj2 1716: $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
1.91 bowersj2 1717: $self->{CONDITION}, $self->{DIRECTION});
1.51 bowersj2 1718: }
1719:
1720: return $self->{HERE};
1721: }
1722:
1.1 www 1723: 1;
1.2 www 1724:
1.51 bowersj2 1725: package Apache::lonnavmaps::resource;
1726:
1727: use Apache::lonnet;
1728:
1729: =pod
1730:
1731: =head1 Object: resource
1732:
1733: A resource object encapsulates a resource in a resource map, allowing easy manipulation of the resource, querying the properties of the resource (including user properties), and represents a reference that can be used as the canonical representation of the resource by lonnavmap clients like renderers.
1734:
1735: A resource only makes sense in the context of a navmap, as some of the data is stored in the navmap object.
1736:
1737: You will probably never need to instantiate this object directly. Use Apache::lonnavmap::navmap, and use the "start" method to obtain the starting resource.
1738:
1739: =head2 Public Members
1740:
1741: resource objects have a hash called DATA ($resourceRef->{DATA}) that you can store whatever you want in. This allows you to easily do two-pass algorithms without worrying about managing your own resource->data hash.
1742:
1743: =head2 Methods
1744:
1745: =over 4
1746:
1.70 bowersj2 1747: =item * B<new>($navmapRef, $idString): The first arg is a reference to the parent navmap object. The second is the idString of the resource itself. Very rarely, if ever, called directly. Use the nav map->getByID() method.
1748:
1749: =back
1.51 bowersj2 1750:
1751: =cut
1752:
1753: sub new {
1754: # magic invocation to create a class instance
1755: my $proto = shift;
1756: my $class = ref($proto) || $proto;
1757: my $self = {};
1758:
1759: $self->{NAV_MAP} = shift;
1760: $self->{ID} = shift;
1761:
1762: # Store this new resource in the parent nav map's cache.
1763: $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
1.66 bowersj2 1764: $self->{RESOURCE_ERROR} = 0;
1.51 bowersj2 1765:
1766: # A hash that can be used by two-pass algorithms to store data
1767: # about this resource in. Not used by the resource object
1768: # directly.
1769: $self->{DATA} = {};
1770:
1771: bless($self);
1772:
1773: return $self;
1774: }
1775:
1.70 bowersj2 1776: # private function: simplify the NAV_HASH lookups we keep doing
1777: # pass the name, and to automatically append my ID, pass a true val on the
1778: # second param
1779: sub navHash {
1780: my $self = shift;
1781: my $param = shift;
1782: my $id = shift;
1783: return $self->{NAV_MAP}->{NAV_HASH}->{$param . ($id?$self->{ID}:"")};
1784: }
1785:
1.51 bowersj2 1786: =pod
1787:
1.70 bowersj2 1788: B<Metadata Retreival>
1789:
1.101 bowersj2 1790: These are methods that help you retrieve metadata about the resource: Method names are based on the fields in the compiled course representation.
1.70 bowersj2 1791:
1792: =over 4
1793:
1.106 bowersj2 1794: =item * B<compTitle>: Returns a "composite title", that is equal to $res->title() if the resource has a title, and is otherwise the last part of the URL (e.g., "problem.problem").
1795:
1.70 bowersj2 1796: =item * B<ext>: Returns true if the resource is external.
1797:
1798: =item * B<goesto>: Returns the "goesto" value from the compiled nav map. (It is likely you want to use B<getNext> instead.)
1799:
1800: =item * B<kind>: Returns the kind of the resource from the compiled nav map.
1801:
1.101 bowersj2 1802: =item * B<randomout>: Returns true if this resource was chosen to NOT be shown to the user by the random map selection feature. In other words, this is usually false.
1.51 bowersj2 1803:
1.70 bowersj2 1804: =item * B<randompick>: Returns true for a map if the randompick feature is being used on the map. (?)
1805:
1806: =item * B<src>: Returns the source for the resource.
1807:
1808: =item * B<symb>: Returns the symb for the resource.
1809:
1810: =item * B<title>: Returns the title of the resource.
1811:
1812: =item * B<to>: Returns the "to" value from the compiled nav map. (It is likely you want to use B<getNext> instead.)
1.51 bowersj2 1813:
1814: =back
1815:
1816: =cut
1817:
1818: # These info functions can be used directly, as they don't return
1819: # resource information.
1.85 bowersj2 1820: sub comesfrom { my $self=shift; return $self->navHash("comesfrom_", 1); }
1.70 bowersj2 1821: sub ext { my $self=shift; return $self->navHash("ext_", 1) eq 'true:'; }
1.85 bowersj2 1822: sub from { my $self=shift; return $self->navHash("from_", 1); }
1.51 bowersj2 1823: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
1824: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
1.68 bowersj2 1825: sub randomout { my $self=shift; return $self->navHash("randomout_", 1); }
1826: sub randompick {
1827: my $self = shift;
1828: return $self->{NAV_MAP}->{PARM_HASH}->{$self->symb .
1829: '.0.parameter_randompick'};
1830: }
1.51 bowersj2 1831: sub src {
1832: my $self=shift;
1833: return $self->navHash("src_", 1);
1834: }
1835: sub symb {
1836: my $self=shift;
1837: (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
1838: my $symbSrc = &Apache::lonnet::declutter($self->src());
1839: return &Apache::lonnet::declutter(
1840: $self->navHash('map_id_'.$first))
1841: . '___' . $second . '___' . $symbSrc;
1842: }
1.70 bowersj2 1843: sub title { my $self=shift; return $self->navHash("title_", 1); }
1844: sub to { my $self=shift; return $self->navHash("to_", 1); }
1.106 bowersj2 1845: sub compTitle {
1846: my $self = shift;
1847: my $title = $self->title();
1848: if (!$title) {
1849: $title = $self->src();
1850: $title = substr($title, rindex($title, '/') + 1);
1851: }
1852: return $title;
1853: }
1.70 bowersj2 1854: =pod
1855:
1856: B<Predicate Testing the Resource>
1857:
1858: These methods are shortcuts to deciding if a given resource has a given property.
1859:
1860: =over 4
1861:
1862: =item * B<is_map>: Returns true if the resource is a map type.
1863:
1864: =item * B<is_problem>: Returns true if the resource is a problem type, false otherwise. (Looks at the extension on the src field; might need more to work correctly.)
1865:
1866: =item * B<is_page>: Returns true if the resource is a page.
1867:
1868: =item * B<is_problem>: Returns true if the resource is a problem.
1869:
1.101 bowersj2 1870: =item * B<is_sequence>: Returns true if the resource is a sequence.
1.70 bowersj2 1871:
1872: =back
1873:
1874: =cut
1875:
1876:
1877: sub is_html {
1.51 bowersj2 1878: my $self=shift;
1879: my $src = $self->src();
1.70 bowersj2 1880: return ($src =~ /html$/);
1.51 bowersj2 1881: }
1.70 bowersj2 1882: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
1883: sub is_page {
1.51 bowersj2 1884: my $self=shift;
1885: my $src = $self->src();
1.70 bowersj2 1886: return ($src =~ /page$/);
1.51 bowersj2 1887: }
1.70 bowersj2 1888: sub is_problem {
1.51 bowersj2 1889: my $self=shift;
1890: my $src = $self->src();
1.70 bowersj2 1891: return ($src =~ /problem$/);
1.51 bowersj2 1892: }
1.70 bowersj2 1893: sub is_sequence {
1.51 bowersj2 1894: my $self=shift;
1895: my $src = $self->src();
1.70 bowersj2 1896: return ($src =~ /sequence$/);
1.51 bowersj2 1897: }
1898:
1.101 bowersj2 1899: # Private method: Shells out to the parmval in the nav map, handler parts.
1.51 bowersj2 1900: sub parmval {
1901: my $self = shift;
1902: my $what = shift;
1903: my $part = shift || "0";
1904: return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
1905: }
1906:
1.70 bowersj2 1907: =pod
1908:
1909: B<Map Methods>
1910:
1911: These methods are useful for getting information about the map properties of the resource, if the resource is a map (B<is_map>).
1912:
1913: =over 4
1914:
1915: =item * B<map_finish>: Returns a reference to a resource object corresponding to the finish resource of the map.
1916:
1917: =item * B<map_pc>: Returns the pc value of the map, which is the first number that appears in the resource ID of the resources in the map, and is the number that appears around the middle of the symbs of the resources in that map.
1918:
1919: =item * B<map_start>: Returns a reference to a resource object corresponding to the start resource of the map.
1920:
1921: =item * B<map_type>: Returns a string with the type of the map in it.
1922:
1923: =back
1.51 bowersj2 1924:
1.70 bowersj2 1925: =cut
1.51 bowersj2 1926:
1927: sub map_finish {
1928: my $self = shift;
1929: my $src = $self->src();
1930: my $res = $self->navHash("map_finish_$src", 0);
1931: $res = $self->{NAV_MAP}->getById($res);
1932: return $res;
1933: }
1.70 bowersj2 1934: sub map_pc {
1935: my $self = shift;
1936: my $src = $self->src();
1937: return $self->navHash("map_pc_$src", 0);
1938: }
1.51 bowersj2 1939: sub map_start {
1940: my $self = shift;
1941: my $src = $self->src();
1942: my $res = $self->navHash("map_start_$src", 0);
1943: $res = $self->{NAV_MAP}->getById($res);
1944: return $res;
1945: }
1946: sub map_type {
1947: my $self = shift;
1948: my $pc = $self->map_pc();
1949: return $self->navHash("map_type_$pc", 0);
1950: }
1951:
1952:
1953:
1954: #####
1955: # Property queries
1956: #####
1957:
1958: # These functions will be responsible for returning the CORRECT
1959: # VALUE for the parameter, no matter what. So while they may look
1960: # like direct calls to parmval, they can be more then that.
1961: # So, for instance, the duedate function should use the "duedatetype"
1962: # information, rather then the resource object user.
1963:
1964: =pod
1965:
1966: =head2 Resource Parameters
1967:
1.70 bowersj2 1968: In order to use the resource parameters correctly, the nav map must have been instantiated with genCourseAndUserOptions set to true, so the courseopt and useropt is read correctly. Then, you can call these functions to get the relevant parameters for the resource. Each function defaults to part "0", but can be directed to another part by passing the part as the parameter.
1.51 bowersj2 1969:
1970: These methods are responsible for getting the parameter correct, not merely reflecting the contents of the GDBM hashes. As we move towards dates relative to other dates, these methods should be updated to reflect that. (Then, anybody using these methods won't have to update their code.)
1971:
1972: =over 4
1973:
1974: =item * B<acc>: Get the Client IP/Name Access Control information.
1975:
1976: =item * B<answerdate>: Get the answer-reveal date for the problem.
1977:
1978: =item * B<duedate>: Get the due date for the problem.
1979:
1980: =item * B<tries>: Get the number of tries the student has used on the problem.
1981:
1982: =item * B<maxtries>: Get the number of max tries allowed.
1983:
1984: =item * B<opendate>: Get the open date for the problem.
1985:
1986: =item * B<sig>: Get the significant figures setting.
1987:
1988: =item * B<tol>: Get the tolerance for the problem.
1989:
1.70 bowersj2 1990: =item * B<tries>: Get the number of tries the user has already used on the problem.
1991:
1.51 bowersj2 1992: =item * B<type>: Get the question type for the problem.
1993:
1994: =item * B<weight>: Get the weight for the problem.
1995:
1996: =back
1997:
1998: =cut
1999:
2000: sub acc {
2001: (my $self, my $part) = @_;
2002: return $self->parmval("acc", $part);
2003: }
2004: sub answerdate {
2005: (my $self, my $part) = @_;
2006: # Handle intervals
2007: if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
2008: return $self->duedate($part) +
2009: $self->parmval("answerdate", $part);
2010: }
2011: return $self->parmval("answerdate", $part);
1.106 bowersj2 2012: }
1.108 bowersj2 2013: sub awarded { my $self = shift; return $self->queryRestoreHash('awarded', shift); }
1.51 bowersj2 2014: sub duedate {
2015: (my $self, my $part) = @_;
2016: return $self->parmval("duedate", $part);
2017: }
2018: sub maxtries {
2019: (my $self, my $part) = @_;
2020: return $self->parmval("maxtries", $part);
2021: }
2022: sub opendate {
2023: (my $self, my $part) = @_;
2024: if ($self->parmval("opendate.type", $part) eq 'date_interval') {
2025: return $self->duedate($part) -
2026: $self->parmval("opendate", $part);
2027: }
2028: return $self->parmval("opendate");
2029: }
2030: sub sig {
2031: (my $self, my $part) = @_;
2032: return $self->parmval("sig", $part);
2033: }
2034: sub tol {
2035: (my $self, my $part) = @_;
2036: return $self->parmval("tol", $part);
2037: }
1.108 bowersj2 2038: sub tries {
2039: my $self = shift;
2040: my $tries = $self->queryRestoreHash('tries', shift);
2041: if (!defined($tries)) { return '0';}
1.51 bowersj2 2042: return $tries;
2043: }
1.70 bowersj2 2044: sub type {
2045: (my $self, my $part) = @_;
2046: return $self->parmval("type", $part);
2047: }
1.109 bowersj2 2048: sub weight {
2049: my $self = shift; my $part = shift;
1.70 bowersj2 2050: return $self->parmval("weight", $part);
2051: }
1.51 bowersj2 2052:
2053: # Multiple things need this
2054: sub getReturnHash {
2055: my $self = shift;
2056:
2057: if (!defined($self->{RETURN_HASH})) {
1.84 bowersj2 2058: my %tmpHash = &Apache::lonnet::restore($self->symb());
1.51 bowersj2 2059: $self->{RETURN_HASH} = \%tmpHash;
2060: }
2061: }
2062:
2063: ######
2064: # Status queries
2065: ######
2066:
2067: # These methods query the status of problems.
2068:
2069: # If we need to count parts, this function determines the number of
2070: # parts from the metadata. When called, it returns a reference to a list
2071: # of strings corresponding to the parts. (Thus, using it in a scalar context
2072: # tells you how many parts you have in the problem:
2073: # $partcount = scalar($resource->countParts());
2074: # Don't use $self->{PARTS} directly because you don't know if it's been
2075: # computed yet.
2076:
2077: =pod
2078:
2079: =head2 Resource misc
2080:
2081: Misc. functions for the resource.
2082:
2083: =over 4
2084:
1.59 bowersj2 2085: =item * B<hasDiscussion>: Returns a false value if there has been discussion since the user last logged in, true if there has. Always returns false if the discussion data was not extracted when the nav map was constructed.
2086:
2087: =item * B<getFeedback>: Gets the feedback for the resource and returns the raw feedback string for the resource, or the null string if there is no feedback or the email data was not extracted when the nav map was constructed. Usually used like this:
2088:
2089: for (split(/\,/, $res->getFeedback())) {
2090: my $link = &Apache::lonnet::escape($_);
2091: ...
2092:
2093: and use the link as appropriate.
2094:
2095: =cut
2096:
2097: sub hasDiscussion {
2098: my $self = shift;
2099: return $self->{NAV_MAP}->hasDiscussion($self->symb());
2100: }
2101:
2102: sub getFeedback {
2103: my $self = shift;
1.85 bowersj2 2104: return $self->{NAV_MAP}->getFeedback($self->src());
1.59 bowersj2 2105: }
2106:
2107: =pod
2108:
1.70 bowersj2 2109: =item * B<parts>(): Returns a list reference containing sorted strings corresponding to each part of the problem. To count the number of parts, use the list in a scalar context, and subtract one if greater then two. (One part problems have a part 0. Multi-parts have a part 0, plus a part for each part. Filtering part 0 if you want it is up to you.)
1.51 bowersj2 2110:
1.70 bowersj2 2111: =item * B<countParts>(): Returns the number of parts of the problem a student can answer. Thus, for single part problems, returns 1. For multipart, it returns the number of parts in the problem, not including psuedo-part 0. Thus, B<parts> may return an array with fewer parts in it then countParts might lead you to believe.
1.51 bowersj2 2112:
2113: =back
2114:
2115: =cut
2116:
2117: sub parts {
2118: my $self = shift;
2119:
1.67 bowersj2 2120: if ($self->ext) { return ['0']; }
2121:
1.51 bowersj2 2122: $self->extractParts();
2123: return $self->{PARTS};
2124: }
2125:
2126: sub countParts {
2127: my $self = shift;
2128:
2129: my $parts = $self->parts();
1.66 bowersj2 2130:
2131: if ($self->{RESOURCE_ERROR}) {
2132: return 0;
2133: }
2134:
2135: if (scalar(@{$parts}) < 2) { return 1;}
1.51 bowersj2 2136:
2137: return scalar(@{$parts}) - 1;
2138: }
2139:
2140: # Private function: Extracts the parts information and saves it
2141: sub extractParts {
2142: my $self = shift;
2143:
2144: return if ($self->{PARTS});
1.67 bowersj2 2145: return if ($self->ext);
1.51 bowersj2 2146:
2147: $self->{PARTS} = [];
2148:
1.82 bowersj2 2149: # Retrieve part count, if this is a problem
2150: if ($self->is_problem()) {
2151: my $metadata = &Apache::lonnet::metadata($self->src(), 'allpossiblekeys');
2152: if (!$metadata) {
2153: $self->{RESOURCE_ERROR} = 1;
2154: $self->{PARTS} = [];
2155: return;
2156: }
2157:
2158: foreach (split(/\,/,$metadata)) {
2159: if ($_ =~ /^parameter\_(.*)\_opendate$/) {
2160: push @{$self->{PARTS}}, $1;
2161: }
1.51 bowersj2 2162: }
1.82 bowersj2 2163:
2164:
2165: # Is this possible to do in one line? - Jeremy
2166: my @sortedParts = sort @{$self->{PARTS}};
2167: $self->{PARTS} = \@sortedParts;
1.51 bowersj2 2168: }
2169:
2170: return;
2171: }
2172:
2173: =pod
2174:
2175: =head2 Resource Status
2176:
1.101 bowersj2 2177: Problem resources have status information, reflecting their various dates and completion statuses.
1.51 bowersj2 2178:
2179: There are two aspects to the status: the date-related information and the completion information.
2180:
2181: Idiomatic usage of these two methods would probably look something like
2182:
2183: foreach ($resource->parts()) {
2184: my $dateStatus = $resource->getDateStatus($_);
2185: my $completionStatus = $resource->getCompletionStatus($_);
2186:
1.70 bowersj2 2187: or
2188:
2189: my $status = $resource->status($_);
2190:
1.51 bowersj2 2191: ... use it here ...
2192: }
2193:
1.101 bowersj2 2194: Which you use depends on exactly what you are looking for. The status() function has been optimized for the nav maps display and may not precisely match what you need elsewhere.
2195:
2196: The symbolic constants shown below can be accessed through the resource object: $res->OPEN.
2197:
1.51 bowersj2 2198: =over 4
2199:
1.70 bowersj2 2200: =item * B<getDateStatus>($part): ($part defaults to 0). A convenience function that returns a symbolic constant telling you about the date status of the part. The possible return values are:
1.51 bowersj2 2201:
2202: =back
2203:
2204: B<Date Codes>
2205:
2206: =over 4
2207:
2208: =item * B<OPEN_LATER>: The problem will be opened later.
2209:
2210: =item * B<OPEN>: Open and not yet due.
2211:
1.59 bowersj2 2212:
1.54 bowersj2 2213: =item * B<PAST_DUE_ANSWER_LATER>: The due date has passed, but the answer date has not yet arrived.
2214:
2215: =item * B<PAST_DUE_NO_ANSWER>: The due date has passed and there is no answer opening date set.
1.51 bowersj2 2216:
2217: =item * B<ANSWER_OPEN>: The answer date is here.
2218:
2219: =item * B<NETWORK_FAILURE>: The information is unknown due to network failure.
2220:
2221: =back
2222:
2223: =cut
2224:
2225: # Apparently the compiler optimizes these into constants automatically
1.54 bowersj2 2226: sub OPEN_LATER { return 0; }
2227: sub OPEN { return 1; }
2228: sub PAST_DUE_NO_ANSWER { return 2; }
2229: sub PAST_DUE_ANSWER_LATER { return 3; }
2230: sub ANSWER_OPEN { return 4; }
2231: sub NOTHING_SET { return 5; }
2232: sub NETWORK_FAILURE { return 100; }
2233:
2234: # getDateStatus gets the date status for a given problem part.
2235: # Because answer date, due date, and open date are fully independent
2236: # (i.e., it is perfectly possible to *only* have an answer date),
2237: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
2238: # (past, future, none given). This function handles this with a decision
2239: # tree. Read the comments to follow the decision tree.
1.51 bowersj2 2240:
2241: sub getDateStatus {
2242: my $self = shift;
2243: my $part = shift;
2244: $part = "0" if (!defined($part));
1.54 bowersj2 2245:
2246: # Always return network failure if there was one.
1.51 bowersj2 2247: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
2248:
2249: my $now = time();
2250:
1.53 bowersj2 2251: my $open = $self->opendate($part);
2252: my $due = $self->duedate($part);
2253: my $answer = $self->answerdate($part);
2254:
2255: if (!$open && !$due && !$answer) {
2256: # no data on the problem at all
2257: # should this be the same as "open later"? think multipart.
2258: return $self->NOTHING_SET;
2259: }
1.59 bowersj2 2260: if (!$open || $now < $open) {return $self->OPEN_LATER}
2261: if (!$due || $now < $due) {return $self->OPEN}
2262: if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
2263: if ($answer) { return $self->ANSWER_OPEN; }
1.54 bowersj2 2264: return PAST_DUE_NO_ANSWER;
1.51 bowersj2 2265: }
2266:
2267: =pod
2268:
2269: B<>
2270:
2271: =over 4
2272:
2273: =item * B<getCompletionStatus>($part): ($part defaults to 0.) A convenience function that returns a symbolic constant telling you about the completion status of the part, with the following possible results:
2274:
2275: =back
2276:
2277: B<Completion Codes>
2278:
2279: =over 4
2280:
2281: =item * B<NOT_ATTEMPTED>: Has not been attempted at all.
2282:
2283: =item * B<INCORRECT>: Attempted, but wrong by student.
2284:
2285: =item * B<INCORRECT_BY_OVERRIDE>: Attempted, but wrong by instructor override.
2286:
2287: =item * B<CORRECT>: Correct or correct by instructor.
2288:
2289: =item * B<CORRECT_BY_OVERRIDE>: Correct by instructor override.
2290:
2291: =item * B<EXCUSED>: Excused. Not yet implemented.
2292:
2293: =item * B<NETWORK_FAILURE>: Information not available due to network failure.
2294:
1.69 bowersj2 2295: =item * B<ATTEMPTED>: Attempted, and not yet graded.
2296:
1.51 bowersj2 2297: =back
2298:
2299: =cut
2300:
1.53 bowersj2 2301: sub NOT_ATTEMPTED { return 10; }
2302: sub INCORRECT { return 11; }
2303: sub INCORRECT_BY_OVERRIDE { return 12; }
2304: sub CORRECT { return 13; }
2305: sub CORRECT_BY_OVERRIDE { return 14; }
2306: sub EXCUSED { return 15; }
1.69 bowersj2 2307: sub ATTEMPTED { return 16; }
1.51 bowersj2 2308:
2309: sub getCompletionStatus {
2310: my $self = shift;
2311: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
2312:
1.108 bowersj2 2313: my $status = $self->queryRestoreHash('solved', shift);
1.51 bowersj2 2314:
2315: # Left as seperate if statements in case we ever do more with this
2316: if ($status eq 'correct_by_student') {return $self->CORRECT;}
2317: if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
2318: if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
2319: if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
2320: if ($status eq 'excused') {return $self->EXCUSED; }
1.69 bowersj2 2321: if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
1.51 bowersj2 2322: return $self->NOT_ATTEMPTED;
1.108 bowersj2 2323: }
2324:
2325: sub queryRestoreHash {
2326: my $self = shift;
2327: my $hashentry = shift;
2328: my $part = shift;
2329: $part = "0" if (!defined($part));
2330: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
2331:
2332: $self->getReturnHash();
2333:
2334: return $self->{RETURN_HASH}->{'resource.'.$part.'.'.$hashentry};
1.51 bowersj2 2335: }
2336:
2337: =pod
2338:
2339: B<Composite Status>
2340:
1.101 bowersj2 2341: Along with directly returning the date or completion status, the resource object includes a convenience function B<status>() that will combine the two status tidbits into one composite status that can represent the status of the resource as a whole. The precise logic is documented in the comments of the status method. The following results may be returned, all available as methods on the resource object ($res->NETWORK_FAILURE):
1.51 bowersj2 2342:
2343: =over 4
2344:
1.70 bowersj2 2345: =item * B<NETWORK_FAILURE>: The network has failed and the information is not available.
1.51 bowersj2 2346:
1.70 bowersj2 2347: =item * B<NOTHING_SET>: No dates have been set for this problem (part) at all. (Because only certain parts of a multi-part problem may be assigned, this can not be collapsed into "open later", as we don't know a given part will EVER be opened. For single part, this is the same as "OPEN_LATER".)
1.53 bowersj2 2348:
1.70 bowersj2 2349: =item * B<CORRECT>: For any reason at all, the part is considered correct.
1.51 bowersj2 2350:
1.70 bowersj2 2351: =item * B<EXCUSED>: For any reason at all, the problem is excused.
1.51 bowersj2 2352:
1.70 bowersj2 2353: =item * B<PAST_DUE_NO_ANSWER>: The problem is past due, not considered correct, and no answer date is set.
1.54 bowersj2 2354:
1.70 bowersj2 2355: =item * B<PAST_DUE_ANSWER_LATER>: The problem is past due, not considered correct, and an answer date in the future is set.
1.51 bowersj2 2356:
1.70 bowersj2 2357: =item * B<ANSWER_OPEN>: The problem is past due, not correct, and the answer is now available.
1.51 bowersj2 2358:
1.70 bowersj2 2359: =item * B<OPEN_LATER>: The problem is not yet open.
1.51 bowersj2 2360:
1.70 bowersj2 2361: =item * B<TRIES_LEFT>: The problem is open, has been tried, is not correct, but there are tries left.
1.51 bowersj2 2362:
1.70 bowersj2 2363: =item * B<INCORRECT>: The problem is open, and all tries have been used without getting the correct answer.
1.51 bowersj2 2364:
1.70 bowersj2 2365: =item * B<OPEN>: The item is open and not yet tried.
1.51 bowersj2 2366:
1.70 bowersj2 2367: =item * B<ATTEMPTED>: The problem has been attempted.
1.69 bowersj2 2368:
1.51 bowersj2 2369: =back
2370:
2371: =cut
2372:
2373: sub TRIES_LEFT { return 10; }
2374:
2375: sub status {
2376: my $self = shift;
2377: my $part = shift;
2378: if (!defined($part)) { $part = "0"; }
2379: my $completionStatus = $self->getCompletionStatus($part);
2380: my $dateStatus = $self->getDateStatus($part);
2381:
2382: # What we have is a two-dimensional matrix with 4 entries on one
2383: # dimension and 5 entries on the other, which we want to colorize,
1.54 bowersj2 2384: # plus network failure and "no date data at all".
1.51 bowersj2 2385:
1.53 bowersj2 2386: if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51 bowersj2 2387:
2388: # There are a few whole rows we can dispose of:
1.53 bowersj2 2389: if ($completionStatus == CORRECT ||
2390: $completionStatus == CORRECT_BY_OVERRIDE ) {
1.69 bowersj2 2391: return CORRECT;
2392: }
2393:
2394: if ($completionStatus == ATTEMPTED) {
2395: return ATTEMPTED;
1.53 bowersj2 2396: }
2397:
2398: # If it's EXCUSED, then return that no matter what
2399: if ($completionStatus == EXCUSED) {
2400: return EXCUSED;
1.51 bowersj2 2401: }
2402:
1.53 bowersj2 2403: if ($dateStatus == NOTHING_SET) {
2404: return NOTHING_SET;
1.51 bowersj2 2405: }
2406:
1.69 bowersj2 2407: # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
2408: # by 4 matrix (date statuses).
1.51 bowersj2 2409:
1.54 bowersj2 2410: if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59 bowersj2 2411: $dateStatus == PAST_DUE_NO_ANSWER ) {
1.54 bowersj2 2412: return $dateStatus;
1.51 bowersj2 2413: }
2414:
1.53 bowersj2 2415: if ($dateStatus == ANSWER_OPEN) {
2416: return ANSWER_OPEN;
1.51 bowersj2 2417: }
2418:
2419: # Now: (incorrect, incorrect_override, not_attempted) x
2420: # (open_later), (open)
2421:
1.53 bowersj2 2422: if ($dateStatus == OPEN_LATER) {
2423: return OPEN_LATER;
1.51 bowersj2 2424: }
2425:
2426: # If it's WRONG...
1.53 bowersj2 2427: if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51 bowersj2 2428: # and there are TRIES LEFT:
1.79 bowersj2 2429: if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
1.53 bowersj2 2430: return TRIES_LEFT;
1.51 bowersj2 2431: }
1.53 bowersj2 2432: return INCORRECT; # otherwise, return orange; student can't fix this
1.51 bowersj2 2433: }
2434:
2435: # Otherwise, it's untried and open
1.53 bowersj2 2436: return OPEN;
1.51 bowersj2 2437: }
2438:
2439: =pod
2440:
2441: =head2 Resource/Nav Map Navigation
2442:
2443: =over 4
2444:
1.101 bowersj2 2445: =item * B<getNext>(): Retreive an array of the possible next resources after this one. Always returns an array, even in the one- or zero-element case.
1.85 bowersj2 2446:
1.101 bowersj2 2447: =item * B<getPrevious>(): Retreive an array of the possible previous resources from this one. Always returns an array, even in the one- or zero-element case.
1.51 bowersj2 2448:
2449: =cut
2450:
2451: sub getNext {
2452: my $self = shift;
2453: my @branches;
2454: my $to = $self->to();
1.85 bowersj2 2455: foreach my $branch ( split(/,/, $to) ) {
1.51 bowersj2 2456: my $choice = $self->{NAV_MAP}->getById($branch);
2457: my $next = $choice->goesto();
2458: $next = $self->{NAV_MAP}->getById($next);
2459:
1.101 bowersj2 2460: # Don't remember it if the student doesn't have browse priviledges
2461: # future note: this may properly belong in the client of the resource
1.66 bowersj2 2462: my $browsePriv = &Apache::lonnet::allowed('bre', $self->src);
1.102 bowersj2 2463: if (!($browsePriv ne '2' && $browsePriv ne 'F')) {
1.101 bowersj2 2464: push @branches, $next;
2465: }
1.85 bowersj2 2466: }
2467: return \@branches;
2468: }
2469:
2470: sub getPrevious {
2471: my $self = shift;
2472: my @branches;
2473: my $from = $self->from();
2474: foreach my $branch ( split /,/, $from) {
2475: my $choice = $self->{NAV_MAP}->getById($branch);
2476: my $prev = $choice->comesfrom();
2477: $prev = $self->{NAV_MAP}->getById($prev);
2478:
1.101 bowersj2 2479: # Don't remember it if the student doesn't have browse priviledges
2480: # future note: this may properly belong in the client of the resource
1.85 bowersj2 2481: my $browsePriv = &Apache::lonnet::allowed('bre', $self->src);
1.103 bowersj2 2482: if (!($browsePriv ne '2' && $browsePriv ne 'F')) {
1.87 www 2483: push @branches, $prev;
1.85 bowersj2 2484: }
1.51 bowersj2 2485: }
2486: return \@branches;
2487: }
2488:
2489: =pod
1.2 www 2490:
1.51 bowersj2 2491: =back
1.2 www 2492:
1.51 bowersj2 2493: =cut
1.2 www 2494:
1.51 bowersj2 2495: 1;
1.2 www 2496:
1.51 bowersj2 2497: __END__
1.2 www 2498:
2499:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>