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