Annotation of loncom/interface/lonnavmaps.pm, revision 1.107
1.72 bowersj2 1:
1.2 www 2: # The LearningOnline Network with CAPA
3: # Navigate Maps Handler
1.1 www 4: #
1.107 ! bowersj2 5: # $Id: lonnavmaps.pm,v 1.106 2002/11/14 21:36:23 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.107 ! bowersj2 1287: # In the CVS repository, documentation of this algorithm is included
! 1288: # in /doc/lonnavdocs, as a PDF and .tex source. Markers like **1**
! 1289: # will reference the same location in the text as the part of the
! 1290: # algorithm is running through.
! 1291:
1.94 bowersj2 1292: sub new {
1293: # magic invocation to create a class instance
1294: my $proto = shift;
1295: my $class = ref($proto) || $proto;
1296: my $self = {};
1297:
1298: $self->{NAV_MAP} = shift;
1299: return undef unless ($self->{NAV_MAP});
1300:
1301: # Handle the parameters
1302: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
1303: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
1304:
1305: # If the given resources are just the ID of the resource, get the
1306: # objects
1307: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
1308: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
1309: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
1310: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
1311:
1312: $self->{FILTER} = shift;
1313:
1314: # A hash, used as a set, of resource already seen
1315: $self->{ALREADY_SEEN} = shift;
1316: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1317: $self->{CONDITION} = shift;
1318:
1319: # Now, we need to pre-process the map, by walking forward and backward
1320: # over the parts of the map we're going to look at.
1.96 bowersj2 1321:
1.97 bowersj2 1322: # The processing steps are exactly the same, except for a few small
1323: # changes, so I bundle those up in the following list of two elements:
1324: # (direction_to_iterate, VAL_name, next_resource_method_to_call,
1325: # first_resource).
1326: # This prevents writing nearly-identical code twice.
1327: my @iterations = ( [FORWARD(), 'TOP_DOWN_VAL', 'getNext',
1328: 'FIRST_RESOURCE'],
1329: [BACKWARD(), 'BOT_UP_VAL', 'getPrevious',
1330: 'FINISH_RESOURCE'] );
1331:
1.98 bowersj2 1332: my $maxDepth = 0; # tracks max depth
1333:
1.107 ! bowersj2 1334: # **1**
! 1335:
1.97 bowersj2 1336: foreach my $pass (@iterations) {
1337: my $direction = $pass->[0];
1338: my $valName = $pass->[1];
1339: my $nextResourceMethod = $pass->[2];
1340: my $firstResourceName = $pass->[3];
1341:
1342: my $iterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP},
1343: $self->{FIRST_RESOURCE},
1344: $self->{FINISH_RESOURCE},
1345: {}, undef, 0, $direction);
1.96 bowersj2 1346:
1.97 bowersj2 1347: # prime the recursion
1348: $self->{$firstResourceName}->{DATA}->{$valName} = 0;
1.98 bowersj2 1349: my $depth = 0;
1.97 bowersj2 1350: $iterator->next();
1351: my $curRes = $iterator->next();
1.98 bowersj2 1352: while ($depth > -1) {
1.97 bowersj2 1353: if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
1354: if ($curRes == $iterator->END_MAP()) { $depth--; }
1.96 bowersj2 1355:
1.97 bowersj2 1356: if (ref($curRes)) {
1357: my $resultingVal = $curRes->{DATA}->{$valName};
1358: my $nextResources = $curRes->$nextResourceMethod();
1359: my $resourceCount = scalar(@{$nextResources});
1.104 bowersj2 1360:
1.107 ! bowersj2 1361: if ($resourceCount == 1) { # **3**
1.97 bowersj2 1362: my $current = $nextResources->[0]->{DATA}->{$valName} || 999999999;
1363: $nextResources->[0]->{DATA}->{$valName} = min($resultingVal, $current);
1364: }
1365:
1.107 ! bowersj2 1366: if ($resourceCount > 1) { # **4**
1.97 bowersj2 1367: foreach my $res (@{$nextResources}) {
1368: my $current = $res->{DATA}->{$valName} || 999999999;
1369: $res->{DATA}->{$valName} = min($current, $resultingVal + 1);
1370: }
1371: }
1372: }
1.96 bowersj2 1373:
1.107 ! bowersj2 1374: # Assign the final val (**2**)
1.97 bowersj2 1375: if (ref($curRes) && $direction == BACKWARD()) {
1.98 bowersj2 1376: my $finalDepth = min($curRes->{DATA}->{TOP_DOWN_VAL},
1377: $curRes->{DATA}->{BOT_UP_VAL});
1378:
1379: $curRes->{DATA}->{DISPLAY_DEPTH} = $finalDepth;
1380: if ($finalDepth > $maxDepth) {$maxDepth = $finalDepth;}
1381: }
1.97 bowersj2 1382: $curRes = $iterator->next();
1.96 bowersj2 1383: }
1384: }
1.94 bowersj2 1385:
1.98 bowersj2 1386: # Set up some bookkeeping information.
1387: $self->{CURRENT_DEPTH} = 0;
1388: $self->{MAX_DEPTH} = $maxDepth;
1389: $self->{STACK} = [];
1390: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1391:
1392: for (my $i = 0; $i <= $self->{MAX_DEPTH}; $i++) {
1393: push @{$self->{STACK}}, [];
1394: }
1395:
1.107 ! bowersj2 1396: # Prime the recursion w/ the first resource **5**
1.98 bowersj2 1397: push @{$self->{STACK}->[0]}, $self->{FIRST_RESOURCE};
1398: $self->{ALREADY_SEEN}->{$self->{FIRST_RESOURCE}->{ID}} = 1;
1399:
1400: bless ($self);
1401:
1402: return $self;
1403: }
1404:
1405: sub next {
1406: my $self = shift;
1407:
1408: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1409: # grab the next from the recursive iterator
1410: my $next = $self->{RECURSIVE_ITERATOR}->next();
1411:
1412: # is it a begin or end map? If so, update the depth
1413: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
1414: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
1415:
1416: # Are we back at depth 0? If so, stop recursing
1417: if ($self->{RECURSIVE_DEPTH} == 0) {
1418: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1419: }
1420:
1421: return $next;
1422: }
1423:
1424: if (defined($self->{FORCE_NEXT})) {
1425: my $tmp = $self->{FORCE_NEXT};
1426: $self->{FORCE_NEXT} = undef;
1427: return $tmp;
1428: }
1429:
1430: # Have we not yet begun? If not, return BEGIN_MAP and
1431: # remember we've started.
1432: if ( !$self->{STARTED} ) {
1433: $self->{STARTED} = 1;
1434: return $self->BEGIN_MAP();
1435: }
1436:
1437: # Here's the guts of the iterator.
1438:
1439: # Find the next resource, if any.
1440: my $found = 0;
1441: my $i = $self->{MAX_DEPTH};
1442: my $newDepth;
1443: my $here;
1444: while ( $i >= 0 && !$found ) {
1.107 ! bowersj2 1445: if ( scalar(@{$self->{STACK}->[$i]}) > 0 ) { # **6**
! 1446: $here = pop @{$self->{STACK}->[$i]}; # **7**
1.98 bowersj2 1447: $found = 1;
1448: $newDepth = $i;
1449: }
1450: $i--;
1451: }
1452:
1453: # If we still didn't find anything, we're done.
1454: if ( !$found ) {
1455: # We need to get back down to the correct branch depth
1456: if ( $self->{CURRENT_DEPTH} > 0 ) {
1457: $self->{CURRENT_DEPTH}--;
1458: return END_BRANCH();
1459: } else {
1460: return END_MAP();
1461: }
1462: }
1463:
1.104 bowersj2 1464: # If this is not a resource, it must be an END_BRANCH marker we want
1465: # to return directly.
1.107 ! bowersj2 1466: if (!ref($here)) { # **8**
1.104 bowersj2 1467: if ($here == END_BRANCH()) { # paranoia, in case of later extension
1468: $self->{CURRENT_DEPTH}--;
1469: return $here;
1470: }
1471: }
1472:
1473: # Otherwise, it is a resource and it's safe to store in $self->{HERE}
1474: $self->{HERE} = $here;
1475:
1.98 bowersj2 1476: # Get to the right level
1477: if ( $self->{CURRENT_DEPTH} > $newDepth ) {
1478: push @{$self->{STACK}->[$newDepth]}, $here;
1479: $self->{CURRENT_DEPTH}--;
1480: return END_BRANCH();
1481: }
1482: if ( $self->{CURRENT_DEPTH} < $newDepth) {
1483: push @{$self->{STACK}->[$newDepth]}, $here;
1484: $self->{CURRENT_DEPTH}++;
1485: return BEGIN_BRANCH();
1486: }
1487:
1488: # If we made it here, we have the next resource, and we're at the
1489: # right branch level. So let's examine the resource for where
1490: # we can get to from here.
1491:
1492: # So we need to look at all the resources we can get to from here,
1493: # categorize them if we haven't seen them, remember if we have a new
1494: my $nextUnfiltered = $here->getNext();
1.104 bowersj2 1495: my $maxDepthAdded = -1;
1496:
1.98 bowersj2 1497: for (@$nextUnfiltered) {
1498: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
1.104 bowersj2 1499: my $depth = $_->{DATA}->{DISPLAY_DEPTH};
1500: push @{$self->{STACK}->[$depth]}, $_;
1.98 bowersj2 1501: $self->{ALREADY_SEEN}->{$_->{ID}} = 1;
1.104 bowersj2 1502: if ($maxDepthAdded < $depth) { $maxDepthAdded = $depth; }
1.98 bowersj2 1503: }
1504: }
1.104 bowersj2 1505:
1506: # Is this the end of a branch? If so, all of the resources examined above
1507: # led to lower levels then the one we are currently at, so we push a END_BRANCH
1508: # marker onto the stack so we don't forget.
1509: # Example: For the usual A(BC)(DE)F case, when the iterator goes down the
1510: # BC branch and gets to C, it will see F as the only next resource, but it's
1511: # one level lower. Thus, this is the end of the branch, since there are no
1512: # more resources added to this level or above.
1513: my $isEndOfBranch = $maxDepthAdded < $self->{CURRENT_DEPTH};
1.107 ! bowersj2 1514: if ($isEndOfBranch) { # **9**
1.104 bowersj2 1515: push @{$self->{STACK}->[$self->{CURRENT_DEPTH}]}, END_BRANCH();
1516: }
1517:
1.98 bowersj2 1518: # That ends the main iterator logic. Now, do we want to recurse
1519: # down this map (if this resource is a map)?
1520: if ($self->{HERE}->is_map() &&
1521: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
1522: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
1523: my $firstResource = $self->{HERE}->map_start();
1524: my $finishResource = $self->{HERE}->map_finish();
1525:
1526: $self->{RECURSIVE_ITERATOR} =
1527: Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
1528: $finishResource, $self->{FILTER},
1529: $self->{ALREADY_SEEN}, $self->{CONDITION});
1530: }
1531:
1532: return $self->{HERE};
1533:
1534: }
1535:
1536: =pod
1537:
1538: 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).
1539:
1540: =cut
1541:
1542: sub getStack {
1543: my $self=shift;
1544:
1545: my @stack;
1546:
1547: $self->populateStack(\@stack);
1548:
1549: return \@stack;
1550: }
1551:
1552: # Private method: Calls the iterators recursively to populate the stack.
1553: sub populateStack {
1554: my $self=shift;
1555: my $stack = shift;
1556:
1557: push @$stack, $self->{HERE} if ($self->{HERE});
1558:
1559: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1560: $self->{RECURSIVE_ITERATOR}->populateStack($stack);
1561: }
1.94 bowersj2 1562: }
1563:
1564: 1;
1565:
1566: package Apache::lonnavmaps::DFSiterator;
1567:
1.100 bowersj2 1568: # Not documented in the perldoc: This is a simple iterator that just walks
1569: # through the nav map and presents the resources in a depth-first search
1570: # fashion, ignorant of conditionals, randomized resources, etc. It presents
1571: # BEGIN_MAP and END_MAP, but does not understand branches at all. It is
1572: # useful for pre-processing of some kind, and is in fact used by the main
1573: # iterator that way, but that's about it.
1574: # One could imagine merging this into the init routine of the main iterator,
1575: # but this might as well be left seperate, since it is possible some other
1576: # use might be found for it. - Jeremy
1.94 bowersj2 1577:
1578: sub BEGIN_MAP { return 1; } # begining of a new map
1579: sub END_MAP { return 2; } # end of the map
1580: sub FORWARD { return 1; } # go forward
1581: sub BACKWARD { return 2; }
1582:
1.100 bowersj2 1583: # Params: Nav map ref, first resource id/ref, finish resource id/ref,
1584: # filter hash ref (or undef), already seen hash or undef, condition
1585: # (as in main iterator), direction FORWARD or BACKWARD (undef->forward).
1.51 bowersj2 1586: sub new {
1587: # magic invocation to create a class instance
1588: my $proto = shift;
1589: my $class = ref($proto) || $proto;
1590: my $self = {};
1591:
1592: $self->{NAV_MAP} = shift;
1593: return undef unless ($self->{NAV_MAP});
1594:
1595: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
1596: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
1597:
1598: # If the given resources are just the ID of the resource, get the
1599: # objects
1600: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
1601: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
1602: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
1603: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
1604:
1605: $self->{FILTER} = shift;
1606:
1607: # A hash, used as a set, of resource already seen
1608: $self->{ALREADY_SEEN} = shift;
1609: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1.63 bowersj2 1610: $self->{CONDITION} = shift;
1.89 bowersj2 1611: $self->{DIRECTION} = shift || FORWARD();
1.51 bowersj2 1612:
1.100 bowersj2 1613: # Flag: Have we started yet?
1.51 bowersj2 1614: $self->{STARTED} = 0;
1615:
1616: # Should we continue calling the recursive iterator, if any?
1617: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1618: # The recursive iterator, if any
1619: $self->{RECURSIVE_ITERATOR} = undef;
1620: # Are we recursing on a map, or a branch?
1621: $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
1622: # And the count of how deep it is, so that this iterator can keep track of
1623: # when to pick back up again.
1624: $self->{RECURSIVE_DEPTH} = 0;
1625:
1626: # For keeping track of our branches, we maintain our own stack
1.100 bowersj2 1627: $self->{STACK} = [];
1.51 bowersj2 1628:
1629: # Start with the first resource
1.89 bowersj2 1630: if ($self->{DIRECTION} == FORWARD) {
1.100 bowersj2 1631: push @{$self->{STACK}}, $self->{FIRST_RESOURCE};
1.89 bowersj2 1632: } else {
1.100 bowersj2 1633: push @{$self->{STACK}}, $self->{FINISH_RESOURCE};
1.89 bowersj2 1634: }
1.51 bowersj2 1635:
1636: bless($self);
1637: return $self;
1638: }
1639:
1640: sub next {
1641: my $self = shift;
1642:
1643: # Are we using a recursive iterator? If so, pull from that and
1644: # watch the depth; we want to resume our level at the correct time.
1.98 bowersj2 1645: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1.51 bowersj2 1646: # grab the next from the recursive iterator
1647: my $next = $self->{RECURSIVE_ITERATOR}->next();
1648:
1649: # is it a begin or end map? Update depth if so
1650: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
1651: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
1652:
1653: # Are we back at depth 0? If so, stop recursing.
1654: if ($self->{RECURSIVE_DEPTH} == 0) {
1655: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1656: }
1657:
1658: return $next;
1659: }
1660:
1661: # Is there a current resource to grab? If not, then return
1.100 bowersj2 1662: # END_MAP, which will end the iterator.
1663: if (scalar(@{$self->{STACK}}) == 0) {
1664: return $self->END_MAP();
1.51 bowersj2 1665: }
1666:
1667: # Have we not yet begun? If not, return BEGIN_MAP and
1668: # remember that we've started.
1669: if ( !$self->{STARTED} ) {
1670: $self->{STARTED} = 1;
1671: return $self->BEGIN_MAP;
1672: }
1673:
1674: # Get the next resource in the branch
1.100 bowersj2 1675: $self->{HERE} = pop @{$self->{STACK}};
1.52 bowersj2 1676:
1.100 bowersj2 1677: # remember that we've seen this, so we don't return it again later
1.51 bowersj2 1678: $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
1679:
1680: # Get the next possible resources
1.90 bowersj2 1681: my $nextUnfiltered;
1.89 bowersj2 1682: if ($self->{DIRECTION} == FORWARD()) {
1.90 bowersj2 1683: $nextUnfiltered = $self->{HERE}->getNext();
1.89 bowersj2 1684: } else {
1.90 bowersj2 1685: $nextUnfiltered = $self->{HERE}->getPrevious();
1.89 bowersj2 1686: }
1.51 bowersj2 1687: my $next = [];
1688:
1689: # filter the next possibilities to remove things we've
1.100 bowersj2 1690: # already seen.
1.51 bowersj2 1691: foreach (@$nextUnfiltered) {
1.52 bowersj2 1692: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
1693: push @$next, $_;
1694: }
1.51 bowersj2 1695: }
1696:
1697: while (@$next) {
1.100 bowersj2 1698: # copy the next possibilities over to the stack
1699: push @{$self->{STACK}}, shift @$next;
1.51 bowersj2 1700: }
1701:
1702: # If this is a map and we want to recurse down it... (not filtered out)
1.70 bowersj2 1703: if ($self->{HERE}->is_map() &&
1.63 bowersj2 1704: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
1.51 bowersj2 1705: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
1706: my $firstResource = $self->{HERE}->map_start();
1707: my $finishResource = $self->{HERE}->map_finish();
1708:
1709: $self->{RECURSIVE_ITERATOR} =
1.94 bowersj2 1710: Apache::lonnavmaps::DFSiterator->new ($self->{NAV_MAP}, $firstResource,
1.63 bowersj2 1711: $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
1.91 bowersj2 1712: $self->{CONDITION}, $self->{DIRECTION});
1.51 bowersj2 1713: }
1714:
1715: return $self->{HERE};
1716: }
1717:
1.1 www 1718: 1;
1.2 www 1719:
1.51 bowersj2 1720: package Apache::lonnavmaps::resource;
1721:
1722: use Apache::lonnet;
1723:
1724: =pod
1725:
1726: =head1 Object: resource
1727:
1728: 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.
1729:
1730: A resource only makes sense in the context of a navmap, as some of the data is stored in the navmap object.
1731:
1732: You will probably never need to instantiate this object directly. Use Apache::lonnavmap::navmap, and use the "start" method to obtain the starting resource.
1733:
1734: =head2 Public Members
1735:
1736: 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.
1737:
1738: =head2 Methods
1739:
1740: =over 4
1741:
1.70 bowersj2 1742: =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.
1743:
1744: =back
1.51 bowersj2 1745:
1746: =cut
1747:
1748: sub new {
1749: # magic invocation to create a class instance
1750: my $proto = shift;
1751: my $class = ref($proto) || $proto;
1752: my $self = {};
1753:
1754: $self->{NAV_MAP} = shift;
1755: $self->{ID} = shift;
1756:
1757: # Store this new resource in the parent nav map's cache.
1758: $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
1.66 bowersj2 1759: $self->{RESOURCE_ERROR} = 0;
1.51 bowersj2 1760:
1761: # A hash that can be used by two-pass algorithms to store data
1762: # about this resource in. Not used by the resource object
1763: # directly.
1764: $self->{DATA} = {};
1765:
1766: bless($self);
1767:
1768: return $self;
1769: }
1770:
1.70 bowersj2 1771: # private function: simplify the NAV_HASH lookups we keep doing
1772: # pass the name, and to automatically append my ID, pass a true val on the
1773: # second param
1774: sub navHash {
1775: my $self = shift;
1776: my $param = shift;
1777: my $id = shift;
1778: return $self->{NAV_MAP}->{NAV_HASH}->{$param . ($id?$self->{ID}:"")};
1779: }
1780:
1.51 bowersj2 1781: =pod
1782:
1.70 bowersj2 1783: B<Metadata Retreival>
1784:
1.101 bowersj2 1785: 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 1786:
1787: =over 4
1788:
1.106 bowersj2 1789: =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").
1790:
1.70 bowersj2 1791: =item * B<ext>: Returns true if the resource is external.
1792:
1793: =item * B<goesto>: Returns the "goesto" value from the compiled nav map. (It is likely you want to use B<getNext> instead.)
1794:
1795: =item * B<kind>: Returns the kind of the resource from the compiled nav map.
1796:
1.101 bowersj2 1797: =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 1798:
1.70 bowersj2 1799: =item * B<randompick>: Returns true for a map if the randompick feature is being used on the map. (?)
1800:
1801: =item * B<src>: Returns the source for the resource.
1802:
1803: =item * B<symb>: Returns the symb for the resource.
1804:
1805: =item * B<title>: Returns the title of the resource.
1806:
1807: =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 1808:
1809: =back
1810:
1811: =cut
1812:
1813: # These info functions can be used directly, as they don't return
1814: # resource information.
1.85 bowersj2 1815: sub comesfrom { my $self=shift; return $self->navHash("comesfrom_", 1); }
1.70 bowersj2 1816: sub ext { my $self=shift; return $self->navHash("ext_", 1) eq 'true:'; }
1.85 bowersj2 1817: sub from { my $self=shift; return $self->navHash("from_", 1); }
1.51 bowersj2 1818: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
1819: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
1.68 bowersj2 1820: sub randomout { my $self=shift; return $self->navHash("randomout_", 1); }
1821: sub randompick {
1822: my $self = shift;
1823: return $self->{NAV_MAP}->{PARM_HASH}->{$self->symb .
1824: '.0.parameter_randompick'};
1825: }
1.51 bowersj2 1826: sub src {
1827: my $self=shift;
1828: return $self->navHash("src_", 1);
1829: }
1830: sub symb {
1831: my $self=shift;
1832: (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
1833: my $symbSrc = &Apache::lonnet::declutter($self->src());
1834: return &Apache::lonnet::declutter(
1835: $self->navHash('map_id_'.$first))
1836: . '___' . $second . '___' . $symbSrc;
1837: }
1.70 bowersj2 1838: sub title { my $self=shift; return $self->navHash("title_", 1); }
1839: sub to { my $self=shift; return $self->navHash("to_", 1); }
1.106 bowersj2 1840: sub compTitle {
1841: my $self = shift;
1842: my $title = $self->title();
1843: if (!$title) {
1844: $title = $self->src();
1845: $title = substr($title, rindex($title, '/') + 1);
1846: }
1847: return $title;
1848: }
1.70 bowersj2 1849: =pod
1850:
1851: B<Predicate Testing the Resource>
1852:
1853: These methods are shortcuts to deciding if a given resource has a given property.
1854:
1855: =over 4
1856:
1857: =item * B<is_map>: Returns true if the resource is a map type.
1858:
1859: =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.)
1860:
1861: =item * B<is_page>: Returns true if the resource is a page.
1862:
1863: =item * B<is_problem>: Returns true if the resource is a problem.
1864:
1.101 bowersj2 1865: =item * B<is_sequence>: Returns true if the resource is a sequence.
1.70 bowersj2 1866:
1867: =back
1868:
1869: =cut
1870:
1871:
1872: sub is_html {
1.51 bowersj2 1873: my $self=shift;
1874: my $src = $self->src();
1.70 bowersj2 1875: return ($src =~ /html$/);
1.51 bowersj2 1876: }
1.70 bowersj2 1877: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
1878: sub is_page {
1.51 bowersj2 1879: my $self=shift;
1880: my $src = $self->src();
1.70 bowersj2 1881: return ($src =~ /page$/);
1.51 bowersj2 1882: }
1.70 bowersj2 1883: sub is_problem {
1.51 bowersj2 1884: my $self=shift;
1885: my $src = $self->src();
1.70 bowersj2 1886: return ($src =~ /problem$/);
1.51 bowersj2 1887: }
1.70 bowersj2 1888: sub is_sequence {
1.51 bowersj2 1889: my $self=shift;
1890: my $src = $self->src();
1.70 bowersj2 1891: return ($src =~ /sequence$/);
1.51 bowersj2 1892: }
1893:
1.101 bowersj2 1894: # Private method: Shells out to the parmval in the nav map, handler parts.
1.51 bowersj2 1895: sub parmval {
1896: my $self = shift;
1897: my $what = shift;
1898: my $part = shift || "0";
1899: return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
1900: }
1901:
1.70 bowersj2 1902: =pod
1903:
1904: B<Map Methods>
1905:
1906: These methods are useful for getting information about the map properties of the resource, if the resource is a map (B<is_map>).
1907:
1908: =over 4
1909:
1910: =item * B<map_finish>: Returns a reference to a resource object corresponding to the finish resource of the map.
1911:
1912: =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.
1913:
1914: =item * B<map_start>: Returns a reference to a resource object corresponding to the start resource of the map.
1915:
1916: =item * B<map_type>: Returns a string with the type of the map in it.
1917:
1918: =back
1.51 bowersj2 1919:
1.70 bowersj2 1920: =cut
1.51 bowersj2 1921:
1922: sub map_finish {
1923: my $self = shift;
1924: my $src = $self->src();
1925: my $res = $self->navHash("map_finish_$src", 0);
1926: $res = $self->{NAV_MAP}->getById($res);
1927: return $res;
1928: }
1.70 bowersj2 1929: sub map_pc {
1930: my $self = shift;
1931: my $src = $self->src();
1932: return $self->navHash("map_pc_$src", 0);
1933: }
1.51 bowersj2 1934: sub map_start {
1935: my $self = shift;
1936: my $src = $self->src();
1937: my $res = $self->navHash("map_start_$src", 0);
1938: $res = $self->{NAV_MAP}->getById($res);
1939: return $res;
1940: }
1941: sub map_type {
1942: my $self = shift;
1943: my $pc = $self->map_pc();
1944: return $self->navHash("map_type_$pc", 0);
1945: }
1946:
1947:
1948:
1949: #####
1950: # Property queries
1951: #####
1952:
1953: # These functions will be responsible for returning the CORRECT
1954: # VALUE for the parameter, no matter what. So while they may look
1955: # like direct calls to parmval, they can be more then that.
1956: # So, for instance, the duedate function should use the "duedatetype"
1957: # information, rather then the resource object user.
1958:
1959: =pod
1960:
1961: =head2 Resource Parameters
1962:
1.70 bowersj2 1963: 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 1964:
1965: 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.)
1966:
1967: =over 4
1968:
1969: =item * B<acc>: Get the Client IP/Name Access Control information.
1970:
1971: =item * B<answerdate>: Get the answer-reveal date for the problem.
1972:
1973: =item * B<duedate>: Get the due date for the problem.
1974:
1975: =item * B<tries>: Get the number of tries the student has used on the problem.
1976:
1977: =item * B<maxtries>: Get the number of max tries allowed.
1978:
1979: =item * B<opendate>: Get the open date for the problem.
1980:
1981: =item * B<sig>: Get the significant figures setting.
1982:
1983: =item * B<tol>: Get the tolerance for the problem.
1984:
1.70 bowersj2 1985: =item * B<tries>: Get the number of tries the user has already used on the problem.
1986:
1.51 bowersj2 1987: =item * B<type>: Get the question type for the problem.
1988:
1989: =item * B<weight>: Get the weight for the problem.
1990:
1991: =back
1992:
1993: =cut
1994:
1995: sub acc {
1996: (my $self, my $part) = @_;
1997: return $self->parmval("acc", $part);
1998: }
1999: sub answerdate {
2000: (my $self, my $part) = @_;
2001: # Handle intervals
2002: if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
2003: return $self->duedate($part) +
2004: $self->parmval("answerdate", $part);
2005: }
2006: return $self->parmval("answerdate", $part);
1.106 bowersj2 2007: }
2008: sub awarded {
2009: (my $self, my $part) = @_;
2010: return $self->parmval("awarded", $part);
1.51 bowersj2 2011: }
2012: sub duedate {
2013: (my $self, my $part) = @_;
2014: return $self->parmval("duedate", $part);
2015: }
2016: sub maxtries {
2017: (my $self, my $part) = @_;
2018: return $self->parmval("maxtries", $part);
2019: }
2020: sub opendate {
2021: (my $self, my $part) = @_;
2022: if ($self->parmval("opendate.type", $part) eq 'date_interval') {
2023: return $self->duedate($part) -
2024: $self->parmval("opendate", $part);
2025: }
2026: return $self->parmval("opendate");
2027: }
2028: sub sig {
2029: (my $self, my $part) = @_;
2030: return $self->parmval("sig", $part);
2031: }
2032: sub tol {
2033: (my $self, my $part) = @_;
2034: return $self->parmval("tol", $part);
2035: }
2036: sub tries {
2037: my $self = shift;
2038: my $part = shift;
2039: $part = '0' if (!defined($part));
2040:
2041: # Make sure return hash is loaded, should error check
2042: $self->getReturnHash();
2043:
2044: my $tries = $self->{RETURN_HASH}->{'resource.'.$part.'.tries'};
2045: if (!defined($tries)) {return '0';}
2046: return $tries;
2047: }
1.70 bowersj2 2048: sub type {
2049: (my $self, my $part) = @_;
2050: return $self->parmval("type", $part);
2051: }
2052: sub weight {
2053: (my $self, my $part) = @_;
2054: return $self->parmval("weight", $part);
2055: }
1.51 bowersj2 2056:
2057: # Multiple things need this
2058: sub getReturnHash {
2059: my $self = shift;
2060:
2061: if (!defined($self->{RETURN_HASH})) {
1.84 bowersj2 2062: my %tmpHash = &Apache::lonnet::restore($self->symb());
1.51 bowersj2 2063: $self->{RETURN_HASH} = \%tmpHash;
2064: }
2065: }
2066:
2067: ######
2068: # Status queries
2069: ######
2070:
2071: # These methods query the status of problems.
2072:
2073: # If we need to count parts, this function determines the number of
2074: # parts from the metadata. When called, it returns a reference to a list
2075: # of strings corresponding to the parts. (Thus, using it in a scalar context
2076: # tells you how many parts you have in the problem:
2077: # $partcount = scalar($resource->countParts());
2078: # Don't use $self->{PARTS} directly because you don't know if it's been
2079: # computed yet.
2080:
2081: =pod
2082:
2083: =head2 Resource misc
2084:
2085: Misc. functions for the resource.
2086:
2087: =over 4
2088:
1.59 bowersj2 2089: =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.
2090:
2091: =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:
2092:
2093: for (split(/\,/, $res->getFeedback())) {
2094: my $link = &Apache::lonnet::escape($_);
2095: ...
2096:
2097: and use the link as appropriate.
2098:
2099: =cut
2100:
2101: sub hasDiscussion {
2102: my $self = shift;
2103: return $self->{NAV_MAP}->hasDiscussion($self->symb());
2104: }
2105:
2106: sub getFeedback {
2107: my $self = shift;
1.85 bowersj2 2108: return $self->{NAV_MAP}->getFeedback($self->src());
1.59 bowersj2 2109: }
2110:
2111: =pod
2112:
1.70 bowersj2 2113: =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 2114:
1.70 bowersj2 2115: =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 2116:
2117: =back
2118:
2119: =cut
2120:
2121: sub parts {
2122: my $self = shift;
2123:
1.67 bowersj2 2124: if ($self->ext) { return ['0']; }
2125:
1.51 bowersj2 2126: $self->extractParts();
2127: return $self->{PARTS};
2128: }
2129:
2130: sub countParts {
2131: my $self = shift;
2132:
2133: my $parts = $self->parts();
1.66 bowersj2 2134:
2135: if ($self->{RESOURCE_ERROR}) {
2136: return 0;
2137: }
2138:
2139: if (scalar(@{$parts}) < 2) { return 1;}
1.51 bowersj2 2140:
2141: return scalar(@{$parts}) - 1;
2142: }
2143:
2144: # Private function: Extracts the parts information and saves it
2145: sub extractParts {
2146: my $self = shift;
2147:
2148: return if ($self->{PARTS});
1.67 bowersj2 2149: return if ($self->ext);
1.51 bowersj2 2150:
2151: $self->{PARTS} = [];
2152:
1.82 bowersj2 2153: # Retrieve part count, if this is a problem
2154: if ($self->is_problem()) {
2155: my $metadata = &Apache::lonnet::metadata($self->src(), 'allpossiblekeys');
2156: if (!$metadata) {
2157: $self->{RESOURCE_ERROR} = 1;
2158: $self->{PARTS} = [];
2159: return;
2160: }
2161:
2162: foreach (split(/\,/,$metadata)) {
2163: if ($_ =~ /^parameter\_(.*)\_opendate$/) {
2164: push @{$self->{PARTS}}, $1;
2165: }
1.51 bowersj2 2166: }
1.82 bowersj2 2167:
2168:
2169: # Is this possible to do in one line? - Jeremy
2170: my @sortedParts = sort @{$self->{PARTS}};
2171: $self->{PARTS} = \@sortedParts;
1.51 bowersj2 2172: }
2173:
2174: return;
2175: }
2176:
2177: =pod
2178:
2179: =head2 Resource Status
2180:
1.101 bowersj2 2181: Problem resources have status information, reflecting their various dates and completion statuses.
1.51 bowersj2 2182:
2183: There are two aspects to the status: the date-related information and the completion information.
2184:
2185: Idiomatic usage of these two methods would probably look something like
2186:
2187: foreach ($resource->parts()) {
2188: my $dateStatus = $resource->getDateStatus($_);
2189: my $completionStatus = $resource->getCompletionStatus($_);
2190:
1.70 bowersj2 2191: or
2192:
2193: my $status = $resource->status($_);
2194:
1.51 bowersj2 2195: ... use it here ...
2196: }
2197:
1.101 bowersj2 2198: 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.
2199:
2200: The symbolic constants shown below can be accessed through the resource object: $res->OPEN.
2201:
1.51 bowersj2 2202: =over 4
2203:
1.70 bowersj2 2204: =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 2205:
2206: =back
2207:
2208: B<Date Codes>
2209:
2210: =over 4
2211:
2212: =item * B<OPEN_LATER>: The problem will be opened later.
2213:
2214: =item * B<OPEN>: Open and not yet due.
2215:
1.59 bowersj2 2216:
1.54 bowersj2 2217: =item * B<PAST_DUE_ANSWER_LATER>: The due date has passed, but the answer date has not yet arrived.
2218:
2219: =item * B<PAST_DUE_NO_ANSWER>: The due date has passed and there is no answer opening date set.
1.51 bowersj2 2220:
2221: =item * B<ANSWER_OPEN>: The answer date is here.
2222:
2223: =item * B<NETWORK_FAILURE>: The information is unknown due to network failure.
2224:
2225: =back
2226:
2227: =cut
2228:
2229: # Apparently the compiler optimizes these into constants automatically
1.54 bowersj2 2230: sub OPEN_LATER { return 0; }
2231: sub OPEN { return 1; }
2232: sub PAST_DUE_NO_ANSWER { return 2; }
2233: sub PAST_DUE_ANSWER_LATER { return 3; }
2234: sub ANSWER_OPEN { return 4; }
2235: sub NOTHING_SET { return 5; }
2236: sub NETWORK_FAILURE { return 100; }
2237:
2238: # getDateStatus gets the date status for a given problem part.
2239: # Because answer date, due date, and open date are fully independent
2240: # (i.e., it is perfectly possible to *only* have an answer date),
2241: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
2242: # (past, future, none given). This function handles this with a decision
2243: # tree. Read the comments to follow the decision tree.
1.51 bowersj2 2244:
2245: sub getDateStatus {
2246: my $self = shift;
2247: my $part = shift;
2248: $part = "0" if (!defined($part));
1.54 bowersj2 2249:
2250: # Always return network failure if there was one.
1.51 bowersj2 2251: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
2252:
2253: my $now = time();
2254:
1.53 bowersj2 2255: my $open = $self->opendate($part);
2256: my $due = $self->duedate($part);
2257: my $answer = $self->answerdate($part);
2258:
2259: if (!$open && !$due && !$answer) {
2260: # no data on the problem at all
2261: # should this be the same as "open later"? think multipart.
2262: return $self->NOTHING_SET;
2263: }
1.59 bowersj2 2264: if (!$open || $now < $open) {return $self->OPEN_LATER}
2265: if (!$due || $now < $due) {return $self->OPEN}
2266: if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
2267: if ($answer) { return $self->ANSWER_OPEN; }
1.54 bowersj2 2268: return PAST_DUE_NO_ANSWER;
1.51 bowersj2 2269: }
2270:
2271: =pod
2272:
2273: B<>
2274:
2275: =over 4
2276:
2277: =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:
2278:
2279: =back
2280:
2281: B<Completion Codes>
2282:
2283: =over 4
2284:
2285: =item * B<NOT_ATTEMPTED>: Has not been attempted at all.
2286:
2287: =item * B<INCORRECT>: Attempted, but wrong by student.
2288:
2289: =item * B<INCORRECT_BY_OVERRIDE>: Attempted, but wrong by instructor override.
2290:
2291: =item * B<CORRECT>: Correct or correct by instructor.
2292:
2293: =item * B<CORRECT_BY_OVERRIDE>: Correct by instructor override.
2294:
2295: =item * B<EXCUSED>: Excused. Not yet implemented.
2296:
2297: =item * B<NETWORK_FAILURE>: Information not available due to network failure.
2298:
1.69 bowersj2 2299: =item * B<ATTEMPTED>: Attempted, and not yet graded.
2300:
1.51 bowersj2 2301: =back
2302:
2303: =cut
2304:
1.53 bowersj2 2305: sub NOT_ATTEMPTED { return 10; }
2306: sub INCORRECT { return 11; }
2307: sub INCORRECT_BY_OVERRIDE { return 12; }
2308: sub CORRECT { return 13; }
2309: sub CORRECT_BY_OVERRIDE { return 14; }
2310: sub EXCUSED { return 15; }
1.69 bowersj2 2311: sub ATTEMPTED { return 16; }
1.51 bowersj2 2312:
2313: sub getCompletionStatus {
2314: my $self = shift;
2315: my $part = shift;
2316: $part = "0" if (!defined($part));
2317: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
2318:
2319: # Make sure return hash exists
2320: $self->getReturnHash();
2321:
2322: my $status = $self->{RETURN_HASH}->{'resource.'.$part.'.solved'};
2323:
2324: # Left as seperate if statements in case we ever do more with this
2325: if ($status eq 'correct_by_student') {return $self->CORRECT;}
2326: if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
2327: if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
2328: if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
2329: if ($status eq 'excused') {return $self->EXCUSED; }
1.69 bowersj2 2330: if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
1.51 bowersj2 2331: return $self->NOT_ATTEMPTED;
2332: }
2333:
2334: =pod
2335:
2336: B<Composite Status>
2337:
1.101 bowersj2 2338: 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 2339:
2340: =over 4
2341:
1.70 bowersj2 2342: =item * B<NETWORK_FAILURE>: The network has failed and the information is not available.
1.51 bowersj2 2343:
1.70 bowersj2 2344: =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 2345:
1.70 bowersj2 2346: =item * B<CORRECT>: For any reason at all, the part is considered correct.
1.51 bowersj2 2347:
1.70 bowersj2 2348: =item * B<EXCUSED>: For any reason at all, the problem is excused.
1.51 bowersj2 2349:
1.70 bowersj2 2350: =item * B<PAST_DUE_NO_ANSWER>: The problem is past due, not considered correct, and no answer date is set.
1.54 bowersj2 2351:
1.70 bowersj2 2352: =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 2353:
1.70 bowersj2 2354: =item * B<ANSWER_OPEN>: The problem is past due, not correct, and the answer is now available.
1.51 bowersj2 2355:
1.70 bowersj2 2356: =item * B<OPEN_LATER>: The problem is not yet open.
1.51 bowersj2 2357:
1.70 bowersj2 2358: =item * B<TRIES_LEFT>: The problem is open, has been tried, is not correct, but there are tries left.
1.51 bowersj2 2359:
1.70 bowersj2 2360: =item * B<INCORRECT>: The problem is open, and all tries have been used without getting the correct answer.
1.51 bowersj2 2361:
1.70 bowersj2 2362: =item * B<OPEN>: The item is open and not yet tried.
1.51 bowersj2 2363:
1.70 bowersj2 2364: =item * B<ATTEMPTED>: The problem has been attempted.
1.69 bowersj2 2365:
1.51 bowersj2 2366: =back
2367:
2368: =cut
2369:
2370: sub TRIES_LEFT { return 10; }
2371:
2372: sub status {
2373: my $self = shift;
2374: my $part = shift;
2375: if (!defined($part)) { $part = "0"; }
2376: my $completionStatus = $self->getCompletionStatus($part);
2377: my $dateStatus = $self->getDateStatus($part);
2378:
2379: # What we have is a two-dimensional matrix with 4 entries on one
2380: # dimension and 5 entries on the other, which we want to colorize,
1.54 bowersj2 2381: # plus network failure and "no date data at all".
1.51 bowersj2 2382:
1.53 bowersj2 2383: if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51 bowersj2 2384:
2385: # There are a few whole rows we can dispose of:
1.53 bowersj2 2386: if ($completionStatus == CORRECT ||
2387: $completionStatus == CORRECT_BY_OVERRIDE ) {
1.69 bowersj2 2388: return CORRECT;
2389: }
2390:
2391: if ($completionStatus == ATTEMPTED) {
2392: return ATTEMPTED;
1.53 bowersj2 2393: }
2394:
2395: # If it's EXCUSED, then return that no matter what
2396: if ($completionStatus == EXCUSED) {
2397: return EXCUSED;
1.51 bowersj2 2398: }
2399:
1.53 bowersj2 2400: if ($dateStatus == NOTHING_SET) {
2401: return NOTHING_SET;
1.51 bowersj2 2402: }
2403:
1.69 bowersj2 2404: # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
2405: # by 4 matrix (date statuses).
1.51 bowersj2 2406:
1.54 bowersj2 2407: if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59 bowersj2 2408: $dateStatus == PAST_DUE_NO_ANSWER ) {
1.54 bowersj2 2409: return $dateStatus;
1.51 bowersj2 2410: }
2411:
1.53 bowersj2 2412: if ($dateStatus == ANSWER_OPEN) {
2413: return ANSWER_OPEN;
1.51 bowersj2 2414: }
2415:
2416: # Now: (incorrect, incorrect_override, not_attempted) x
2417: # (open_later), (open)
2418:
1.53 bowersj2 2419: if ($dateStatus == OPEN_LATER) {
2420: return OPEN_LATER;
1.51 bowersj2 2421: }
2422:
2423: # If it's WRONG...
1.53 bowersj2 2424: if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51 bowersj2 2425: # and there are TRIES LEFT:
1.79 bowersj2 2426: if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
1.53 bowersj2 2427: return TRIES_LEFT;
1.51 bowersj2 2428: }
1.53 bowersj2 2429: return INCORRECT; # otherwise, return orange; student can't fix this
1.51 bowersj2 2430: }
2431:
2432: # Otherwise, it's untried and open
1.53 bowersj2 2433: return OPEN;
1.51 bowersj2 2434: }
2435:
2436: =pod
2437:
2438: =head2 Resource/Nav Map Navigation
2439:
2440: =over 4
2441:
1.101 bowersj2 2442: =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 2443:
1.101 bowersj2 2444: =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 2445:
2446: =cut
2447:
2448: sub getNext {
2449: my $self = shift;
2450: my @branches;
2451: my $to = $self->to();
1.85 bowersj2 2452: foreach my $branch ( split(/,/, $to) ) {
1.51 bowersj2 2453: my $choice = $self->{NAV_MAP}->getById($branch);
2454: my $next = $choice->goesto();
2455: $next = $self->{NAV_MAP}->getById($next);
2456:
1.101 bowersj2 2457: # Don't remember it if the student doesn't have browse priviledges
2458: # future note: this may properly belong in the client of the resource
1.66 bowersj2 2459: my $browsePriv = &Apache::lonnet::allowed('bre', $self->src);
1.102 bowersj2 2460: if (!($browsePriv ne '2' && $browsePriv ne 'F')) {
1.101 bowersj2 2461: push @branches, $next;
2462: }
1.85 bowersj2 2463: }
2464: return \@branches;
2465: }
2466:
2467: sub getPrevious {
2468: my $self = shift;
2469: my @branches;
2470: my $from = $self->from();
2471: foreach my $branch ( split /,/, $from) {
2472: my $choice = $self->{NAV_MAP}->getById($branch);
2473: my $prev = $choice->comesfrom();
2474: $prev = $self->{NAV_MAP}->getById($prev);
2475:
1.101 bowersj2 2476: # Don't remember it if the student doesn't have browse priviledges
2477: # future note: this may properly belong in the client of the resource
1.85 bowersj2 2478: my $browsePriv = &Apache::lonnet::allowed('bre', $self->src);
1.103 bowersj2 2479: if (!($browsePriv ne '2' && $browsePriv ne 'F')) {
1.87 www 2480: push @branches, $prev;
1.85 bowersj2 2481: }
1.51 bowersj2 2482: }
2483: return \@branches;
2484: }
2485:
2486: =pod
1.2 www 2487:
1.51 bowersj2 2488: =back
1.2 www 2489:
1.51 bowersj2 2490: =cut
1.2 www 2491:
1.51 bowersj2 2492: 1;
1.2 www 2493:
1.51 bowersj2 2494: __END__
1.2 www 2495:
2496:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>