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