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