Annotation of loncom/interface/lonnavmaps.pm, revision 1.168
1.72 bowersj2 1:
1.2 www 2: # The LearningOnline Network with CAPA
3: # Navigate Maps Handler
1.1 www 4: #
1.168 ! bowersj2 5: # $Id: lonnavmaps.pm,v 1.167 2003/03/25 22:35:58 albertel 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.153 bowersj2 41: # YEAR=2003
42: # Jeremy Bowers ... lots of days
1.2 www 43:
1.1 www 44: package Apache::lonnavmaps;
45:
46: use strict;
1.2 www 47: use Apache::Constants qw(:common :http);
1.18 albertel 48: use Apache::loncommon();
1.150 www 49: use Apache::lonmenu();
1.73 bowersj2 50: use POSIX qw (floor strftime);
1.2 www 51:
1.133 bowersj2 52: # symbolic constants
53: sub SYMB { return 1; }
54: sub URL { return 2; }
55: sub NOTHING { return 3; }
56:
57: # Some data
58:
1.140 bowersj2 59: my $resObj = "Apache::lonnavmaps::resource";
60:
1.135 bowersj2 61: # Keep these mappings in sync with lonquickgrades, which uses the colors
62: # instead of the icons.
63: my %statusIconMap =
1.140 bowersj2 64: ( $resObj->NETWORK_FAILURE => '',
65: $resObj->NOTHING_SET => '',
66: $resObj->CORRECT => 'navmap.correct.gif',
67: $resObj->EXCUSED => 'navmap.correct.gif',
68: $resObj->PAST_DUE_NO_ANSWER => 'navmap.wrong.gif',
69: $resObj->PAST_DUE_ANSWER_LATER => 'navmap.wrong.gif',
70: $resObj->ANSWER_OPEN => 'navmap.wrong.gif',
71: $resObj->OPEN_LATER => '',
72: $resObj->TRIES_LEFT => 'navmap.open.gif',
73: $resObj->INCORRECT => 'navmap.wrong.gif',
74: $resObj->OPEN => 'navmap.open.gif',
75: $resObj->ATTEMPTED => 'navmap.open.gif' );
1.135 bowersj2 76:
77: my %iconAltTags =
78: ( 'navmap.correct.gif' => 'Correct',
79: 'navmap.wrong.gif' => 'Incorrect',
80: 'navmap.open.gif' => 'Open' );
1.133 bowersj2 81:
1.136 bowersj2 82: # Defines a status->color mapping, null string means don't color
83: my %colormap =
1.140 bowersj2 84: ( $resObj->NETWORK_FAILURE => '',
85: $resObj->CORRECT => '',
86: $resObj->EXCUSED => '#3333FF',
87: $resObj->PAST_DUE_ANSWER_LATER => '',
88: $resObj->PAST_DUE_NO_ANSWER => '',
89: $resObj->ANSWER_OPEN => '#006600',
90: $resObj->OPEN_LATER => '',
91: $resObj->TRIES_LEFT => '',
92: $resObj->INCORRECT => '',
93: $resObj->OPEN => '',
94: $resObj->NOTHING_SET => '' );
1.136 bowersj2 95: # And a special case in the nav map; what to do when the assignment
96: # is not yet done and due in less then 24 hours
97: my $hurryUpColor = "#FF0000";
1.130 www 98:
1.1 www 99: sub handler {
1.99 bowersj2 100: my $r = shift;
1.118 bowersj2 101: real_handler($r);
102: }
103:
104: sub real_handler {
105: my $r = shift;
1.2 www 106:
1.51 bowersj2 107: # Handle header-only request
108: if ($r->header_only) {
109: if ($ENV{'browser.mathml'}) {
110: $r->content_type('text/xml');
111: } else {
112: $r->content_type('text/html');
113: }
114: $r->send_http_header;
115: return OK;
116: }
117:
118: # Send header, don't cache this page
119: if ($ENV{'browser.mathml'}) {
120: $r->content_type('text/xml');
121: } else {
122: $r->content_type('text/html');
123: }
124: &Apache::loncommon::no_cache($r);
125: $r->send_http_header;
126:
1.106 bowersj2 127: # Create the nav map
1.168 ! bowersj2 128: my $navmap = Apache::lonnavmaps::navmap->new(
1.51 bowersj2 129: $ENV{"request.course.fn"}.".db",
1.80 bowersj2 130: $ENV{"request.course.fn"}."_parms.db", 1, 1);
1.51 bowersj2 131:
1.55 bowersj2 132:
133: if (!defined($navmap)) {
134: my $requrl = $r->uri;
135: $ENV{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
136: return HTTP_NOT_ACCEPTABLE;
137: }
1.64 bowersj2 138:
1.111 bowersj2 139: $r->print("<html><head>\n");
1.150 www 140: $r->print("<title>Navigate Course Contents</title>");
141: # ------------------------------------------------------------ Get query string
142: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['register']);
1.158 bowersj2 143:
1.150 www 144: # ----------------------------------------------------- Force menu registration
145: my $addentries='';
146: if ($ENV{'form.register'}) {
147: $addentries=' onLoad="'.&Apache::lonmenu::loadevents().
148: '" onUnload="'.&Apache::lonmenu::unloadevents().'"';
149: $r->print(&Apache::lonmenu::registerurl(1));
150: }
1.111 bowersj2 151:
1.64 bowersj2 152: # Header
1.150 www 153: $r->print('</head>'.
154: &Apache::loncommon::bodytag('Navigate Course Contents','',
1.151 www 155: $addentries,'','',$ENV{'form.register'}));
1.64 bowersj2 156: $r->print('<script>window.focus();</script>');
1.101 bowersj2 157:
1.124 bowersj2 158: $r->rflush();
159:
160: # Now that we've displayed some stuff to the user, init the navmap
161: $navmap->init();
162:
1.95 bowersj2 163: $r->print('<br> ');
164: $r->rflush();
165:
1.55 bowersj2 166: # Check that it's defined
167: if (!($navmap->courseMapDefined())) {
168: $r->print('<font size="+2" color="red">Coursemap undefined.</font>' .
169: '</body></html>');
170: return OK;
171: }
172:
1.155 bowersj2 173: # See if there's only one map in the top-level... if so,
174: # automatically display it
175: my $iterator = $navmap->getIterator(undef, undef, undef, 0);
176: my $depth = 1;
177: $iterator->next();
178: my $curRes = $iterator->next();
179: my $sequenceCount = 0;
180: my $sequenceId;
181: while ($depth > 0) {
182: if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
183: if ($curRes == $iterator->END_MAP()) { $depth--; }
184:
185: if (ref($curRes) && $curRes->is_sequence()) {
186: $sequenceCount++;
187: $sequenceId = $curRes->map_pc();
188: }
189:
190: $curRes = $iterator->next();
191: }
192:
193: if ($sequenceCount == 1) {
194: # The automatic iterator creation in the render call
195: # will pick this up.
196: $ENV{'form.filter'} = "$sequenceId";
197: }
198:
1.133 bowersj2 199: # renderer call
1.140 bowersj2 200: my $render = render({ 'cols' => [0,1,2,3],
201: 'url' => '/adm/navmaps',
1.153 bowersj2 202: 'suppressNavmap' => 1,
1.140 bowersj2 203: 'r' => $r});
1.133 bowersj2 204:
1.168 ! bowersj2 205: $navmap->untieHashes();
1.51 bowersj2 206:
1.128 bowersj2 207: $r->print("</body></html>");
1.134 bowersj2 208: $r->rflush();
1.59 bowersj2 209:
1.51 bowersj2 210: return OK;
211: }
212:
213: # Convenience functions: Returns a string that adds or subtracts
214: # the second argument from the first hash, appropriate for the
215: # query string that determines which folders to recurse on
216: sub addToFilter {
217: my $hashIn = shift;
218: my $addition = shift;
219: my %hash = %$hashIn;
220: $hash{$addition} = 1;
221:
222: return join (",", keys(%hash));
223: }
224:
225: sub removeFromFilter {
226: my $hashIn = shift;
227: my $subtraction = shift;
228: my %hash = %$hashIn;
229:
230: delete $hash{$subtraction};
231: return join(",", keys(%hash));
232: }
233:
234: # Convenience function: Given a stack returned from getStack on the iterator,
235: # return the correct src() value.
236: # Later, this should add an anchor when we start putting anchors in pages.
237: sub getLinkForResource {
238: my $stack = shift;
239: my $res;
240:
241: # Check to see if there are any pages in the stack
242: foreach $res (@$stack) {
243: if (defined($res) && $res->is_page()) {
244: return $res->src();
245: }
246: }
247:
248: # Failing that, return the src of the last resource that is defined
249: # (when we first recurse on a map, it puts an undefined resource
250: # on the bottom because $self->{HERE} isn't defined yet, and we
251: # want the src for the map anyhow)
252: foreach (@$stack) {
253: if (defined($_)) { $res = $_; }
254: }
255:
256: return $res->src();
257: }
258:
1.53 bowersj2 259: # Convenience function: This seperates the logic of how to create
260: # the problem text strings ("Due: DATE", "Open: DATE", "Not yet assigned",
261: # etc.) into a seperate function. It takes a resource object as the
262: # first parameter, and the part number of the resource as the second.
263: # It's basically a big switch statement on the status of the resource.
264:
265: sub getDescription {
266: my $res = shift;
267: my $part = shift;
1.69 bowersj2 268: my $status = $res->status($part);
1.53 bowersj2 269:
270: if ($status == $res->NETWORK_FAILURE) { return ""; }
271: if ($status == $res->NOTHING_SET) {
272: return "Not currently assigned.";
273: }
274: if ($status == $res->OPEN_LATER) {
1.73 bowersj2 275: return "Open " . timeToHumanString($res->opendate($part));
1.53 bowersj2 276: }
277: if ($status == $res->OPEN) {
1.79 bowersj2 278: if ($res->duedate($part)) {
1.73 bowersj2 279: return "Due " . timeToHumanString($res->duedate($part));
1.66 bowersj2 280: } else {
281: return "Open, no due date";
282: }
1.53 bowersj2 283: }
1.54 bowersj2 284: if ($status == $res->PAST_DUE_ANSWER_LATER) {
1.73 bowersj2 285: return "Answer open " . timeToHumanString($res->answerdate($part));
1.54 bowersj2 286: }
287: if ($status == $res->PAST_DUE_NO_ANSWER) {
1.73 bowersj2 288: return "Was due " . timeToHumanString($res->duedate($part));
1.53 bowersj2 289: }
290: if ($status == $res->ANSWER_OPEN) {
291: return "Answer available";
292: }
1.59 bowersj2 293: if ($status == $res->EXCUSED) {
1.66 bowersj2 294: return "Excused by instructor";
1.59 bowersj2 295: }
1.69 bowersj2 296: if ($status == $res->ATTEMPTED) {
297: return "Not yet graded.";
298: }
1.59 bowersj2 299: if ($status == $res->TRIES_LEFT) {
1.79 bowersj2 300: my $tries = $res->tries($part);
301: my $maxtries = $res->maxtries($part);
302: my $triesString = "";
303: if ($tries && $maxtries) {
304: $triesString = "<font size=\"-1\"><i>($tries of $maxtries tries used)</i></font>";
305: if ($maxtries > 1 && $maxtries - $tries == 1) {
306: $triesString = "<b>$triesString</b>";
307: }
308: }
1.66 bowersj2 309: if ($res->duedate()) {
1.73 bowersj2 310: return "Due " . timeToHumanString($res->duedate($part)) .
1.66 bowersj2 311: " $triesString";
312: } else {
313: return "No due date $triesString";
314: }
1.59 bowersj2 315: }
1.53 bowersj2 316: }
317:
1.115 bowersj2 318: # Convenience function, so others can use it: Is the problem due in less then
319: # 24 hours, and still can be done?
320:
321: sub dueInLessThen24Hours {
322: my $res = shift;
323: my $part = shift;
324: my $status = $res->status($part);
325:
326: return ($status == $res->OPEN() || $status == $res->ATTEMPTED() ||
327: $status == $res->TRIES_LEFT()) &&
328: $res->duedate() && $res->duedate() < time()+(24*60*60) &&
329: $res->duedate() > time();
330: }
331:
332: # Convenience function, so others can use it: Is there only one try remaining for the
333: # part, with more then one try to begin with, not due yet and still can be done?
334: sub lastTry {
335: my $res = shift;
336: my $part = shift;
337:
338: my $tries = $res->tries($part);
339: my $maxtries = $res->maxtries($part);
340: return $tries && $maxtries && $maxtries > 1 &&
341: $maxtries - $tries == 1 && $res->duedate() &&
342: $res->duedate() > time();
343: }
344:
1.101 bowersj2 345: # This puts a human-readable name on the ENV variable.
1.158 bowersj2 346: # FIXME: This needs better logic: Who gets the advanced view of navmaps?
347: # As of 3-13-03, it's an open question. Guy doesn't want to check
348: # roles directly because it should be a check of capabilities for future
349: # role compatibity. There is no capability that matches this one for
350: # now, so this is done. (A hack for 1.0 might be to simply check roles
351: # anyhow.)
1.68 bowersj2 352: sub advancedUser {
353: return $ENV{'user.adv'};
354: }
355:
1.73 bowersj2 356:
357: # timeToHumanString takes a time number and converts it to a
358: # human-readable representation, meant to be used in the following
359: # manner:
360: # print "Due $timestring"
361: # print "Open $timestring"
362: # print "Answer available $timestring"
363: # Very, very, very, VERY English-only... goodness help a localizer on
364: # this func...
1.53 bowersj2 365: sub timeToHumanString {
1.58 albertel 366: my ($time) = @_;
367: # zero, '0' and blank are bad times
1.73 bowersj2 368: if (!$time) {
369: return 'never';
370: }
371:
372: my $now = time();
373:
374: my @time = localtime($time);
375: my @now = localtime($now);
376:
377: # Positive = future
378: my $delta = $time - $now;
379:
380: my $minute = 60;
381: my $hour = 60 * $minute;
382: my $day = 24 * $hour;
383: my $week = 7 * $day;
384: my $inPast = 0;
385:
386: # Logic in comments:
387: # Is it now? (extremely unlikely)
388: if ( $delta == 0 ) {
389: return "this instant";
390: }
391:
392: if ($delta < 0) {
393: $inPast = 1;
394: $delta = -$delta;
395: }
396:
397: if ( $delta > 0 ) {
1.101 bowersj2 398:
1.73 bowersj2 399: my $tense = $inPast ? " ago" : "";
400: my $prefix = $inPast ? "" : "in ";
1.101 bowersj2 401:
402: # Less then a minute
1.73 bowersj2 403: if ( $delta < $minute ) {
404: if ($delta == 1) { return "${prefix}1 second$tense"; }
405: return "$prefix$delta seconds$tense";
406: }
407:
1.101 bowersj2 408: # Less then an hour
1.73 bowersj2 409: if ( $delta < $hour ) {
410: # If so, use minutes
411: my $minutes = floor($delta / 60);
412: if ($minutes == 1) { return "${prefix}1 minute$tense"; }
413: return "$prefix$minutes minutes$tense";
414: }
415:
416: # Is it less then 24 hours away? If so,
417: # display hours + minutes
418: if ( $delta < $hour * 24) {
419: my $hours = floor($delta / $hour);
420: my $minutes = floor(($delta % $hour) / $minute);
421: my $hourString = "$hours hours";
422: my $minuteString = ", $minutes minutes";
423: if ($hours == 1) {
424: $hourString = "1 hour";
425: }
426: if ($minutes == 1) {
427: $minuteString = ", 1 minute";
428: }
429: if ($minutes == 0) {
430: $minuteString = "";
431: }
432: return "$prefix$hourString$minuteString$tense";
433: }
434:
435: # Less then 5 days away, display day of the week and
436: # HH:MM
437: if ( $delta < $day * 5 ) {
1.85 bowersj2 438: my $timeStr = strftime("%A, %b %e at %I:%M %P", localtime($time));
1.73 bowersj2 439: $timeStr =~ s/12:00 am/midnight/;
440: $timeStr =~ s/12:00 pm/noon/;
441: return ($inPast ? "last " : "next ") .
442: $timeStr;
443: }
444:
445: # Is it this year?
446: if ( $time[5] == $now[5]) {
447: # Return on Month Day, HH:MM meridian
448: my $timeStr = strftime("on %A, %b %e at %I:%M %P", localtime($time));
449: $timeStr =~ s/12:00 am/midnight/;
450: $timeStr =~ s/12:00 pm/noon/;
451: return $timeStr;
452: }
453:
454: # Not this year, so show the year
455: my $timeStr = strftime("on %A, %b %e %G at %I:%M %P", localtime($time));
456: $timeStr =~ s/12:00 am/midnight/;
457: $timeStr =~ s/12:00 pm/noon/;
458: return $timeStr;
1.58 albertel 459: }
1.53 bowersj2 460: }
461:
1.51 bowersj2 462:
1.133 bowersj2 463: =pod
464:
465: =head1 navmap renderer
466:
467: The navmaprenderer package provides a sophisticated rendering of the standard navigation maps interface into HTML. The provided nav map handler is actually just a glorified call to this.
468:
469: Because of the large number of parameters this function presents, instead of passing it arguments as is normal, pass it in an anonymous hash with the given options. This is because there is no obvious order you may wish to override these in and a hash is easier to read and understand then "undef, undef, undef, 1, undef, undef, renderButton, undef, 0" when you mostly want default behaviors.
470:
471: The package provides a function called 'render', called as Apache::lonnavmaps::renderer->render({}).
1.51 bowersj2 472:
1.133 bowersj2 473: =head2 Overview of Columns
1.51 bowersj2 474:
1.133 bowersj2 475: The renderer will build an HTML table for the navmap and return it. The table is consists of several columns, and a row for each resource (or possibly each part). You tell the renderer how many columns to create and what to place in each column, optionally using one or more of the preparent columns, and the renderer will assemble the table.
1.51 bowersj2 476:
1.133 bowersj2 477: Any additional generally useful column types should be placed in the renderer code here, so anybody can use it anywhere else. Any code specific to the current application (such as the addition of <input> elements in a column) should be placed in the code of the thing using the renderer.
1.51 bowersj2 478:
1.133 bowersj2 479: At the core of the renderer is the array reference COLS (see Example section below for how to pass this correctly). The COLS array will consist of entries of one of two types of things: Either an integer representing one of the pre-packaged column types, or a sub reference that takes a resource reference, a part number, and a reference to the argument hash passed to the renderer, and returns a string that will be inserted into the HTML representation as it.
1.51 bowersj2 480:
1.133 bowersj2 481: The pre-packaged column names are refered to by constants in the Apache::lonnavmaps::renderer namespace. The following currently exist:
1.51 bowersj2 482:
1.133 bowersj2 483: =over 4
1.51 bowersj2 484:
1.133 bowersj2 485: =item * B<resource>: The general info about the resource: Link, icon for the type, etc. The first column in the standard nav map display. This column also accepts the following parameter in the renderer hash:
1.51 bowersj2 486:
487: =over 4
488:
1.133 bowersj2 489: =item * B<resource_nolink>: If true, the resource will not be linked. Default: false, resource will have links.
490:
491: =item * B<resource_part_count>: If true (default), the resource will show a part count if the full part list is not displayed. If false, the resource will never show a part count.
492:
1.144 bowersj2 493: =item * B<resource_no_folder_link>: If true, the resource's folder will not be clickable to open or close it. Default is false.
494:
1.133 bowersj2 495: =back
1.51 bowersj2 496:
1.133 bowersj2 497: =item B<communication_status>: Whether there is discussion on the resource, email for the user, or (lumped in here) perl errors in the execution of the problem. This is the second column in the main nav map.
1.51 bowersj2 498:
1.133 bowersj2 499: =item B<quick_status>: An icon for the status of a problem, with four possible states: Correct, incorrect, open, or none (not open yet, not a problem). The third column of the standard navmap.
1.51 bowersj2 500:
1.133 bowersj2 501: =item B<long_status>: A text readout of the details of the current status of the problem, such as "Due in 22 hours". The fourth column of the standard navmap.
1.51 bowersj2 502:
1.133 bowersj2 503: =back
1.51 bowersj2 504:
1.133 bowersj2 505: If you add any others please be sure to document them here.
1.51 bowersj2 506:
1.133 bowersj2 507: An example of a column renderer that will show the ID number of a resource, along with the part name if any:
1.51 bowersj2 508:
1.133 bowersj2 509: sub {
510: my ($resource, $part, $params) = @_;
511: if ($part) { return '<td>' . $resource->{ID} . ' ' . $part . '</td>'; }
512: return '<td>' . $resource->{ID} . '</td>';
513: }
1.51 bowersj2 514:
1.133 bowersj2 515: Note these functions are responsible for the TD tags, which allow them to override vertical and horizontal alignment, etc.
1.130 www 516:
1.133 bowersj2 517: =head2 Parameters
1.115 bowersj2 518:
1.140 bowersj2 519: Most of these parameters are only useful if you are *not* using the folder interface (i.e., the default first column), which is probably the common case. If you are using this interface, then you should be able to get away with just using 'cols' (to specify the columns shown), 'url' (necessary for the folders to link to the current screen correctly), and possibly 'queryString' if your app calls for it. In that case, maintaining the state of the folders will be done automatically.
520:
1.133 bowersj2 521: =over 4
1.51 bowersj2 522:
1.144 bowersj2 523: =item * B<iterator>: A reference to a fresh ::iterator to use from the navmaps. The rendering will reflect the options passed to the iterator, so you can use that to just render a certain part of the course, if you like. If one is not passed, the renderer will attempt to construct one from ENV{'form.filter'} and ENV{'form.condition'} information, plus the 'iterator_map' parameter if any.
524:
525: =item * B<iterator_map>: If you are letting the renderer do the iterator handling, you can instruct the renderer to render only a particular map by passing it the source of the map you want to process, like '/res/103/jerf/navmap.course.sequence'.
1.140 bowersj2 526:
527: =item * B<navmap>: A reference to a navmap, used only if an iterator is not passed in. If this is necessary to make an iterator but it is not passed in, a new one will be constructed based on ENV info. This is useful to do basic error checking before passing it off to render.
1.92 bowersj2 528:
1.164 bowersj2 529: =item * B<r>: The standard Apache response object. This must be passed to the renderer or the course hash will be locked.
530:
1.133 bowersj2 531: =item * B<cols>: An array reference
1.92 bowersj2 532:
1.133 bowersj2 533: =item * B<showParts>: A flag. If yes (default), a line for the resource itself, and a line for each part will be displayed. If not, only one line for each resource will be displayed.
1.51 bowersj2 534:
1.133 bowersj2 535: =item * B<condenseParts>: A flag. If yes (default), if all parts of the problem have the same status and that status is Nothing Set, Correct, or Network Failure, then only one line will be displayed for that resource anyhow. If no, all parts will always be displayed. If showParts is 0, this is ignored.
1.55 bowersj2 536:
1.159 bowersj2 537: =item * B<jumpCount>: A string identifying the URL to place the anchor 'curloc' at. Default to no anchor at all. It is the responsibility of the renderer user to ensure that the #curloc is in the URL. By default, determined through the use of the ENV{} 'jump' information, and should normally "just work" correctly.
1.84 bowersj2 538:
1.158 bowersj2 539: =item * B<here>: A Symb identifying where to place the 'here' marker. Default empty, which means no marker.
1.115 bowersj2 540:
1.133 bowersj2 541: =item * B<indentString>: A string identifying the indentation string to use. By default, this is a 25 pixel whitespace image with no alt text.
1.55 bowersj2 542:
1.133 bowersj2 543: =item * B<queryString>: A string which will be prepended to the query string used when the folders are opened or closed.
1.51 bowersj2 544:
1.133 bowersj2 545: =item * B<url>: The url the folders will link to, which should be the current page. Required if the resource info column is shown.
1.51 bowersj2 546:
1.140 bowersj2 547: =item * B<currentJumpIndex>: Describes the currently-open row number to cause the browser to jump to, because the user just opened that folder. By default, pulled from the Jump information in the ENV{'form.*'}.
1.139 bowersj2 548:
1.140 bowersj2 549: =item * B<printKey>: If true, print the key that appears on the top of the standard navmaps. Default is false.
550:
551: =item * B<printCloseAll>: If true, print the "Close all folders" or "open all folders" links. Default is true.
552:
1.153 bowersj2 553: =item * B<filterFunc>: A function that takes the resource object as its only parameter and returns a true or false value. If true, the resource is displayed. If false, it is simply skipped in the display. By default, all resources are shown.
554:
555: =item * B<suppressNavmaps>: If true, will not display Navigate Content resources. Default to false.
1.143 bowersj2 556:
1.133 bowersj2 557: =back
1.51 bowersj2 558:
1.133 bowersj2 559: =head2 Additional Info
1.51 bowersj2 560:
1.133 bowersj2 561: In addition to the parameters you can pass to the renderer, which will be passed through unchange to the column renderers, the renderer will generate the following information which your renderer may find useful:
1.59 bowersj2 562:
1.145 bowersj2 563: If you want to know how many rows were printed, the 'counter' element of the hash passed into the render function will contain the count. You may want to check whether any resources were printed at all.
564:
1.133 bowersj2 565: =over 4
1.59 bowersj2 566:
1.133 bowersj2 567: =back
1.59 bowersj2 568:
1.133 bowersj2 569: =cut
1.59 bowersj2 570:
1.133 bowersj2 571: sub resource { return 0; }
572: sub communication_status { return 1; }
573: sub quick_status { return 2; }
574: sub long_status { return 3; }
1.124 bowersj2 575:
1.133 bowersj2 576: # Data for render_resource
1.51 bowersj2 577:
1.133 bowersj2 578: sub render_resource {
579: my ($resource, $part, $params) = @_;
1.51 bowersj2 580:
1.133 bowersj2 581: my $nonLinkedText = ''; # stuff after resource title not in link
1.51 bowersj2 582:
1.134 bowersj2 583: my $link = $params->{"resourceLink"};
584: my $src = $resource->src();
585: my $it = $params->{"iterator"};
1.133 bowersj2 586: my $filter = $it->{FILTER};
587:
588: my $title = $resource->compTitle();
589: if ($src =~ /^\/uploaded\//) {
590: $nonLinkedText=$title;
591: $title = '';
592: }
593: my $partLabel = "";
594: my $newBranchText = "";
595:
596: # If this is a new branch, label it so
597: if ($params->{'isNewBranch'}) {
598: $newBranchText = "<img src='/adm/lonIcons/branch.gif' border='0' />";
1.51 bowersj2 599: }
600:
1.133 bowersj2 601: # links to open and close the folder
602: my $linkopen = "<a href='$link'>";
603: my $linkclose = "</a>";
1.51 bowersj2 604:
1.133 bowersj2 605: # Default icon: HTML page
606: my $icon = "<img src='/adm/lonIcons/html.gif' alt='' border='0' />";
607:
608: if ($resource->is_problem()) {
1.156 bowersj2 609: if ($part eq "" || $params->{'condensed'}) {
1.133 bowersj2 610: $icon = '<img src="/adm/lonIcons/problem.gif" alt="" border="0" />';
611: } else {
612: $icon = $params->{'indentString'};
613: }
614: }
1.51 bowersj2 615:
1.133 bowersj2 616: # Display the correct map icon to open or shut map
617: if ($resource->is_map()) {
618: my $mapId = $resource->map_pc();
619: my $nowOpen = !defined($filter->{$mapId});
620: if ($it->{CONDITION}) {
621: $nowOpen = !$nowOpen;
622: }
1.144 bowersj2 623:
624: if (!$params->{'resource_no_folder_link'}) {
625: $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') . '.gif';
626: $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
627:
628: $linkopen = "<a href='" . $params->{'url'} . '?' .
629: $params->{'queryString'} . '&filter=';
630: $linkopen .= ($nowOpen xor $it->{CONDITION}) ?
631: addToFilter($filter, $mapId) :
632: removeFromFilter($filter, $mapId);
633: $linkopen .= "&condition=" . $it->{CONDITION} . '&hereType='
634: . $params->{'hereType'} . '&here=' .
635: &Apache::lonnet::escape($params->{'here'}) .
1.159 bowersj2 636: '&jump=' .
1.144 bowersj2 637: &Apache::lonnet::escape($resource->symb()) .
638: "&folderManip=1'>";
639: } else {
640: # Don't allow users to manipulate folder
641: $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') .
642: '.nomanip.gif';
643: $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
644:
645: $linkopen = "";
646: $linkclose = "";
647: }
1.133 bowersj2 648: }
1.51 bowersj2 649:
1.133 bowersj2 650: if ($resource->randomout()) {
651: $nonLinkedText .= ' <i>(hidden)</i> ';
652: }
653:
654: # We're done preparing and finally ready to start the rendering
655: my $result = "<td align='left' valign='center'>";
1.140 bowersj2 656:
657: my $indentLevel = $params->{'indentLevel'};
658: if ($newBranchText) { $indentLevel--; }
659:
1.133 bowersj2 660: # print indentation
1.140 bowersj2 661: for (my $i = 0; $i < $indentLevel; $i++) {
1.133 bowersj2 662: $result .= $params->{'indentString'};
1.84 bowersj2 663: }
664:
1.133 bowersj2 665: # Decide what to display
666: $result .= "$newBranchText$linkopen$icon$linkclose";
667:
668: my $curMarkerBegin = '';
669: my $curMarkerEnd = '';
1.51 bowersj2 670:
1.133 bowersj2 671: # Is this the current resource?
1.158 bowersj2 672: if (!$params->{'displayedHereMarker'} &&
673: $resource->symb() eq $params->{'here'} ) {
1.133 bowersj2 674: $curMarkerBegin = '<font color="red" size="+2">> </font>';
675: $curMarkerEnd = '<font color="red" size="+2"><</font>';
1.158 bowersj2 676: $params->{'displayedHereMarker'} = 1;
1.51 bowersj2 677: }
678:
1.156 bowersj2 679: if ($resource->is_problem() && $part ne "" &&
1.133 bowersj2 680: !$params->{'condensed'}) {
681: $partLabel = " (Part $part)";
682: $title = "";
1.51 bowersj2 683: }
684:
1.163 bowersj2 685: if ($params->{'condensed'} && $resource->countParts() > 1) {
1.133 bowersj2 686: $nonLinkedText .= ' (' . $resource->countParts() . ' parts)';
1.51 bowersj2 687: }
688:
1.144 bowersj2 689: if (!$params->{'resource_nolink'}) {
690: $result .= " $curMarkerBegin<a href='$link'>$title$partLabel</a>$curMarkerEnd $nonLinkedText</td>";
691: } else {
692: $result .= " $curMarkerBegin$title$partLabel$curMarkerEnd $nonLinkedText</td>";
693: }
1.51 bowersj2 694:
1.133 bowersj2 695: return $result;
696: }
1.51 bowersj2 697:
1.133 bowersj2 698: sub render_communication_status {
699: my ($resource, $part, $params) = @_;
1.134 bowersj2 700: my $discussionHTML = ""; my $feedbackHTML = ""; my $errorHTML = "";
701:
702: my $link = $params->{"resourceLink"};
703: my $linkopen = "<a href='$link'>";
704: my $linkclose = "</a>";
705:
706: if ($resource->hasDiscussion()) {
707: $discussionHTML = $linkopen .
708: '<img border="0" src="/adm/lonMisc/chat.gif" />' .
709: $linkclose;
710: }
711:
712: if ($resource->getFeedback()) {
713: my $feedback = $resource->getFeedback();
714: foreach (split(/\,/, $feedback)) {
715: if ($_) {
716: $feedbackHTML .= ' <a href="/adm/email?display='
717: . &Apache::lonnet::escape($_) . '">'
718: . '<img src="/adm/lonMisc/feedback.gif" '
719: . 'border="0" /></a>';
720: }
721: }
722: }
723:
724: if ($resource->getErrors()) {
725: my $errors = $resource->getErrors();
726: foreach (split(/,/, $errors)) {
727: if ($_) {
728: $errorHTML .= ' <a href="/adm/email?display='
729: . &Apache::lonnet::escape($_) . '">'
730: . '<img src="/adm/lonMisc/bomb.gif" '
731: . 'border="0" /></a>';
732: }
733: }
734: }
735:
736: return "<td width=\"75\" align=\"left\" valign=\"center\">$discussionHTML$feedbackHTML$errorHTML </td>";
737:
1.133 bowersj2 738: }
739: sub render_quick_status {
740: my ($resource, $part, $params) = @_;
1.135 bowersj2 741: my $result = "";
742: my $firstDisplayed = !$params->{'condensed'} &&
743: $params->{'multipart'} && $part eq "0";
744:
745: my $link = $params->{"resourceLink"};
746: my $linkopen = "<a href='$link'>";
747: my $linkclose = "</a>";
748:
749: if ($resource->is_problem() &&
750: !$firstDisplayed) {
751: my $icon = $statusIconMap{$resource->status($part)};
752: my $alt = $iconAltTags{$icon};
753: if ($icon) {
754: $result .= "<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";
755: } else {
756: $result .= "<td width='30'> </td>\n";
757: }
758: } else { # not problem, no icon
759: $result .= "<td width='30'> </td>\n";
760: }
761:
762: return $result;
1.133 bowersj2 763: }
764: sub render_long_status {
765: my ($resource, $part, $params) = @_;
1.136 bowersj2 766: my $result = "<td align='right' valign='center'>\n";
767: my $firstDisplayed = !$params->{'condensed'} &&
768: $params->{'multipart'} && $part eq "0";
769:
770: my $color;
771: if ($resource->is_problem()) {
772: $color = $colormap{$resource->status};
773:
774: if (dueInLessThen24Hours($resource, $part) ||
775: lastTry($resource, $part)) {
776: $color = $hurryUpColor;
777: }
778: }
779:
780: if ($resource->kind() eq "res" &&
781: $resource->is_problem() &&
782: !$firstDisplayed) {
783: if ($color) {$result .= "<font color=\"$color\"><b>"; }
784: $result .= getDescription($resource, $part);
785: if ($color) {$result .= "</b></font>"; }
786: }
787: if ($resource->is_map() && advancedUser() && $resource->randompick()) {
788: $result .= '(randomly select ' . $resource->randompick() .')';
789: }
790:
791: $result .= " </td>\n";
792:
793: return $result;
1.51 bowersj2 794: }
795:
1.133 bowersj2 796: my @preparedColumns = (\&render_resource, \&render_communication_status,
797: \&render_quick_status, \&render_long_status);
1.132 bowersj2 798:
799: sub setDefault {
800: my ($val, $default) = @_;
801: if (!defined($val)) { return $default; }
802: return $val;
803: }
804:
805: sub render {
806: my $args = shift;
1.140 bowersj2 807: &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
808: my $result = '';
809:
1.132 bowersj2 810: # Configure the renderer.
811: my $cols = $args->{'cols'};
812: if (!defined($cols)) {
813: # no columns, no nav maps.
814: return '';
815: }
1.140 bowersj2 816: my $mustCloseNavMap = 0;
817: my $navmap;
818: if (defined($args->{'navmap'})) {
819: $navmap = $args->{'navmap'};
820: }
821:
1.158 bowersj2 822: my $r = $args->{'r'};
1.140 bowersj2 823: my $queryString = $args->{'queryString'};
1.159 bowersj2 824: my $jump = $args->{'jump'};
1.158 bowersj2 825: my $here = $args->{'here'};
1.153 bowersj2 826: my $suppressNavmap = setDefault($args->{'suppressNavmap'}, 0);
1.140 bowersj2 827: my $currentJumpDelta = 2; # change this to change how many resources are displayed
828: # before the current resource when using #current
829:
830: # If we were passed 'here' information, we are not rendering
831: # after a folder manipulation, and we were not passed an
832: # iterator, make sure we open the folders to show the "here"
833: # marker
834: my $filterHash = {};
835: # Figure out what we're not displaying
836: foreach (split(/\,/, $ENV{"form.filter"})) {
837: if ($_) {
838: $filterHash->{$_} = "1";
839: }
840: }
841:
842: my $condition = 0;
843: if ($ENV{'form.condition'}) {
844: $condition = 1;
845: }
846:
847: if (!$ENV{'form.folderManip'} && !defined($args->{'iterator'})) {
848: # Step 1: Check to see if we have a navmap
849: if (!defined($navmap)) {
1.164 bowersj2 850: $navmap = Apache::lonnavmaps::navmap->new($r,
1.140 bowersj2 851: $ENV{"request.course.fn"}.".db",
852: $ENV{"request.course.fn"}."_parms.db", 1, 1);
853: $mustCloseNavMap = 1;
854: }
855: $navmap->init();
856:
857: # Step two: Locate what kind of here marker is necessary
858: # Determine where the "here" marker is and where the screen jumps to.
859:
860: if ($ENV{'form.symb'}) {
861: $here = $jump = $ENV{'form.symb'};
862: } elsif ($ENV{'form.postdata'}) {
863: # couldn't find a symb, is there a URL?
864: my $currenturl = $ENV{'form.postdata'};
1.158 bowersj2 865: #$currenturl=~s/^http\:\/\///;
866: #$currenturl=~s/^[^\/]+//;
1.140 bowersj2 867:
1.158 bowersj2 868: $here = $jump = &Apache::lonnet::symbread($currenturl);
1.140 bowersj2 869: }
1.158 bowersj2 870:
1.140 bowersj2 871: # Step three: Ensure the folders are open
872: my $mapIterator = $navmap->getIterator(undef, undef, undef, 1);
873: my $depth = 1;
874: $mapIterator->next(); # discard the first BEGIN_MAP
875: my $curRes = $mapIterator->next();
876: my $found = 0;
877:
878: # We only need to do this if we need to open the maps to show the
879: # current position. This will change the counter so we can't count
880: # for the jump marker with this loop.
881: while ($depth > 0 && !$found) {
882: if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
883: if ($curRes == $mapIterator->END_MAP()) { $depth--; }
884:
1.158 bowersj2 885: if (ref($curRes) && $curRes->symb() eq $here) {
1.140 bowersj2 886: my $mapStack = $mapIterator->getStack();
887:
888: # Ensure the parent maps are open
889: for my $map (@{$mapStack}) {
890: if ($condition) {
891: undef $filterHash->{$map->map_pc()};
892: } else {
893: $filterHash->{$map->map_pc()} = 1;
894: }
895: }
896: $found = 1;
897: }
898:
899: $curRes = $mapIterator->next();
900: }
1.159 bowersj2 901: }
1.140 bowersj2 902:
903: if ( !defined($args->{'iterator'}) && $ENV{'form.folderManip'} ) { # we came from a user's manipulation of the nav page
904: # If this is a click on a folder or something, we want to preserve the "here"
905: # from the querystring, and get the new "jump" marker
906: $here = $ENV{'form.here'};
907: $jump = $ENV{'form.jump'};
908: }
909:
1.132 bowersj2 910: my $it = $args->{'iterator'};
911: if (!defined($it)) {
1.140 bowersj2 912: # Construct a default iterator based on $ENV{'form.'} information
913:
914: # Step 1: Check to see if we have a navmap
915: if (!defined($navmap)) {
1.164 bowersj2 916: $navmap = Apache::lonnavmaps::navmap->new($r,
1.140 bowersj2 917: $ENV{"request.course.fn"}.".db",
918: $ENV{"request.course.fn"}."_parms.db", 1, 1);
919: $mustCloseNavMap = 1;
920: }
921: # Paranoia: Make sure it's ready
922: $navmap->init();
923:
1.144 bowersj2 924: # See if we're being passed a specific map
925: if ($args->{'iterator_map'}) {
926: my $map = $args->{'iterator_map'};
1.145 bowersj2 927: $map = $navmap->getResourceByUrl($map);
1.144 bowersj2 928: my $firstResource = $map->map_start();
929: my $finishResource = $map->map_finish();
930:
931: $args->{'iterator'} = $it = $navmap->getIterator($firstResource, $finishResource, $filterHash, $condition);
932: } else {
933: $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition);
934: }
1.132 bowersj2 935: }
936:
1.159 bowersj2 937: # (re-)Locate the jump point, if any
938: my $mapIterator = $navmap->getIterator(undef, undef, $filterHash, 0);
939: my $depth = 1;
940: $mapIterator->next();
941: my $curRes = $mapIterator->next();
942: my $foundJump = 0;
943: my $counter = 0;
944:
945: while ($depth > 0 && !$foundJump) {
946: if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
947: if ($curRes == $mapIterator->END_MAP()) { $depth--; }
948: if (ref($curRes)) { $counter++; }
949:
950: if (ref($curRes) && $jump eq $curRes->symb()) {
951:
952: # This is why we have to use the main iterator instead of the
953: # potentially faster DFS: The count has to be the same, so
954: # the order has to be the same, which DFS won't give us.
955: $args->{'currentJumpIndex'} = $counter;
956: $foundJump = 1;
957: }
958:
959: $curRes = $mapIterator->next();
960: }
961:
1.132 bowersj2 962: my $showParts = setDefault($args->{'showParts'}, 1);
963: my $condenseParts = setDefault($args->{'condenseParts'}, 1);
1.139 bowersj2 964: # keeps track of when the current resource is found,
965: # so we can back up a few and put the anchor above the
966: # current resource
1.140 bowersj2 967: my $printKey = $args->{'printKey'};
968: my $printCloseAll = $args->{'printCloseAll'};
969: if (!defined($printCloseAll)) { $printCloseAll = 1; }
1.143 bowersj2 970: my $filterFunc = setDefault($args->{'filterFunc'},
971: sub {return 1;});
1.144 bowersj2 972:
1.140 bowersj2 973: # Print key?
974: if ($printKey) {
975: $result .= '<table border="0" cellpadding="2" cellspacing="0">';
976: my $date=localtime;
977: $result.='<tr><td align="right" valign="bottom">Key: </td>';
978: if ($navmap->{LAST_CHECK}) {
979: $result .=
980: '<img src="/adm/lonMisc/chat.gif"> New discussion since '.
981: strftime("%A, %b %e at %I:%M %P", localtime($navmap->{LAST_CHECK})).
982: '</td><td align="center" valign="bottom"> '.
983: '<img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>'.
984: '</td>';
985: } else {
986: $result .= '<td align="center" valign="bottom"> '.
987: '<img src="/adm/lonMisc/chat.gif"> Discussions</td><td align="center" valign="bottom">'.
988: ' <img src="/adm/lonMisc/feedback.gif"> New message (click to open)'.
989: '</td>';
990: }
991:
992: $result .= '</tr></table>';
993: }
994:
995: if ($printCloseAll) {
996: if ($condition) {
997: $result.="<a href=\"navmaps?condition=0&filter=&$queryString" .
1.158 bowersj2 998: "&here=" . Apache::lonnet::escape($here) .
1.140 bowersj2 999: "\">Close All Folders</a>";
1000: } else {
1001: $result.="<a href=\"navmaps?condition=1&filter=&$queryString" .
1.158 bowersj2 1002: "&here=" . Apache::lonnet::escape($here) .
1.140 bowersj2 1003: "\">Open All Folders</a>";
1004: }
1.143 bowersj2 1005: $result .= "<br /><br />\n";
1.140 bowersj2 1006: }
1007:
1008: if ($r) {
1009: $r->print($result);
1010: $r->rflush();
1011: $result = "";
1012: }
1.132 bowersj2 1013: # End parameter setting
1.133 bowersj2 1014:
1.132 bowersj2 1015: # Data
1.140 bowersj2 1016: $result .= '<table cellspacing="0" cellpadding="3" border="0" bgcolor="#FFFFFF">' ."\n";
1.132 bowersj2 1017: my $res = "Apache::lonnavmaps::resource";
1018: my %condenseStatuses =
1019: ( $res->NETWORK_FAILURE => 1,
1020: $res->NOTHING_SET => 1,
1021: $res->CORRECT => 1 );
1022: my @backgroundColors = ("#FFFFFF", "#F6F6F6");
1.133 bowersj2 1023:
1024: # Shared variables
1025: $args->{'counter'} = 0; # counts the rows
1026: $args->{'indentLevel'} = 0;
1027: $args->{'isNewBranch'} = 0;
1028: $args->{'condensed'} = 0;
1029: $args->{'indentString'} = setDefault($args->{'indentString'}, "<img src='/adm/lonIcons/whitespace1.gif' width='25' height='1' alt='' border='0' />");
1030: $args->{'displayedHereMarker'} = 0;
1.132 bowersj2 1031:
1.133 bowersj2 1032: my $displayedJumpMarker = 0;
1.132 bowersj2 1033: # Set up iteration.
1.159 bowersj2 1034: $depth = 1;
1.132 bowersj2 1035: $it->next(); # discard initial BEGIN_MAP
1.159 bowersj2 1036: $curRes = $it->next();
1.132 bowersj2 1037: my $now = time();
1038: my $in24Hours = $now + 24 * 60 * 60;
1039: my $rownum = 0;
1040:
1.141 bowersj2 1041: # export "here" marker information
1042: $args->{'here'} = $here;
1043:
1.132 bowersj2 1044: while ($depth > 0) {
1045: if ($curRes == $it->BEGIN_MAP()) { $depth++; }
1046: if ($curRes == $it->END_MAP()) { $depth--; }
1047:
1048: # Maintain indentation level.
1049: if ($curRes == $it->BEGIN_MAP() ||
1050: $curRes == $it->BEGIN_BRANCH() ) {
1.133 bowersj2 1051: $args->{'indentLevel'}++;
1.132 bowersj2 1052: }
1053: if ($curRes == $it->END_MAP() ||
1054: $curRes == $it->END_BRANCH() ) {
1.133 bowersj2 1055: $args->{'indentLevel'}--;
1.132 bowersj2 1056: }
1057: # Notice new branches
1058: if ($curRes == $it->BEGIN_BRANCH()) {
1.133 bowersj2 1059: $args->{'isNewBranch'} = 1;
1060: }
1061:
1062: # If this isn't an actual resource, continue on
1063: if (!ref($curRes)) {
1064: next;
1.132 bowersj2 1065: }
1.133 bowersj2 1066:
1067: $args->{'counter'}++;
1.143 bowersj2 1068:
1069: # If this has been filtered out, continue on
1070: if (!(&$filterFunc($curRes))) {
1071: $args->{'isNewBranch'} = 0; # Don't falsely remember this
1072: next;
1073: }
1.132 bowersj2 1074:
1.153 bowersj2 1075: # If we're suppressing navmaps and this is a navmap, continue on
1076: if ($suppressNavmap && $curRes->src() =~ /^\/adm\/navmaps/) {
1077: next;
1078: }
1079:
1.132 bowersj2 1080: # Does it have multiple parts?
1.133 bowersj2 1081: $args->{'multipart'} = 0;
1082: $args->{'condensed'} = 0;
1.132 bowersj2 1083: my @parts;
1084:
1085: # Decide what parts to show.
1.133 bowersj2 1086: if ($curRes->is_problem() && $showParts) {
1.132 bowersj2 1087: @parts = @{$curRes->parts()};
1.133 bowersj2 1088: $args->{'multipart'} = scalar(@parts) > 1;
1.132 bowersj2 1089:
1090: if ($condenseParts) { # do the condensation
1091: if (!$curRes->opendate("0")) {
1.162 bowersj2 1092: @parts = ();
1.133 bowersj2 1093: $args->{'condensed'} = 1;
1.132 bowersj2 1094: }
1.133 bowersj2 1095: if (!$args->{'condensed'}) {
1.132 bowersj2 1096: # Decide whether to condense based on similarity
1097: my $status = $curRes->status($parts[1]);
1098: my $due = $curRes->duedate($parts[1]);
1099: my $open = $curRes->opendate($parts[1]);
1100: my $statusAllSame = 1;
1101: my $dueAllSame = 1;
1102: my $openAllSame = 1;
1103: for (my $i = 2; $i < scalar(@parts); $i++) {
1104: if ($curRes->status($parts[$i]) != $status){
1105: $statusAllSame = 0;
1106: }
1107: if ($curRes->duedate($parts[$i]) != $due ) {
1108: $dueAllSame = 0;
1109: }
1110: if ($curRes->opendate($parts[$i]) != $open) {
1111: $openAllSame = 0;
1112: }
1113: }
1114: # $*allSame is true if all the statuses were
1115: # the same. Now, if they are all the same and
1116: # match one of the statuses to condense, or they
1117: # are all open with the same due date, or they are
1118: # all OPEN_LATER with the same open date, display the
1119: # status of the first non-zero part (to get the 'correct'
1120: # status right, since 0 is never 'correct' or 'open').
1121: if (($statusAllSame && defined($condenseStatuses{$status})) ||
1122: ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
1123: ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
1.161 bowersj2 1124: @parts = ();
1.133 bowersj2 1125: $args->{'condensed'} = 1;
1.132 bowersj2 1126: }
1127:
1128: }
1129: }
1.157 bowersj2 1130: }
1.132 bowersj2 1131:
1132: # If the multipart problem was condensed, "forget" it was multipart
1133: if (scalar(@parts) == 1) {
1.133 bowersj2 1134: $args->{'multipart'} = 0;
1.132 bowersj2 1135: }
1136:
1137: # Now, we've decided what parts to show. Loop through them and
1138: # show them.
1.156 bowersj2 1139: foreach my $part ('', @parts) {
1140: if ($part eq '0') {
1141: next;
1142: }
1.132 bowersj2 1143: $rownum ++;
1144: my $backgroundColor = $backgroundColors[$rownum % scalar(@backgroundColors)];
1145:
1146: $result .= " <tr bgcolor='$backgroundColor'>\n";
1.134 bowersj2 1147:
1148: # Set up some data about the parts that the cols might want
1149: my $filter = $it->{FILTER};
1150: my $stack = $it->getStack();
1151: my $src = getLinkForResource($stack);
1152:
1153: my $srcHasQuestion = $src =~ /\?/;
1154: $args->{"resourceLink"} = $src.
1155: ($srcHasQuestion?'&':'?') .
1.137 bowersj2 1156: 'symb=' . &Apache::lonnet::escape($curRes->symb());
1.132 bowersj2 1157:
1158: # Now, display each column.
1159: foreach my $col (@$cols) {
1.139 bowersj2 1160: my $colHTML = '';
1161: if (ref($col)) {
1162: $colHTML .= &$col($curRes, $part, $args);
1163: } else {
1164: $colHTML .= &{$preparedColumns[$col]}($curRes, $part, $args);
1165: }
1.132 bowersj2 1166:
1.133 bowersj2 1167: # If this is the first column and it's time to print
1168: # the anchor, do so
1169: if ($col == $cols->[0] &&
1170: $args->{'counter'} == $args->{'currentJumpIndex'} -
1.139 bowersj2 1171: $currentJumpDelta) {
1172: # Jam the anchor after the <td> tag;
1173: # necessary for valid HTML (which Mozilla requires)
1174: $colHTML =~ s/\>/\>\<a name="curloc" \/\>/;
1.133 bowersj2 1175: $displayedJumpMarker = 1;
1176: }
1.139 bowersj2 1177: $result .= $colHTML . "\n";
1.132 bowersj2 1178: }
1.139 bowersj2 1179: $result .= " </tr>\n";
1.140 bowersj2 1180: $args->{'isNewBranch'} = 0;
1.139 bowersj2 1181: }
1.153 bowersj2 1182:
1.139 bowersj2 1183: if ($r && $rownum % 20 == 0) {
1184: $r->print($result);
1185: $result = "";
1186: $r->rflush();
1.132 bowersj2 1187: }
1.156 bowersj2 1188: } continue {
1.132 bowersj2 1189: $curRes = $it->next();
1190: }
1191:
1.134 bowersj2 1192: # Print out the part that jumps to #curloc if it exists
1.159 bowersj2 1193: # delay needed because the browser is processing the jump before
1194: # it finishes rendering, so it goes to the wrong place!
1195: # onload might be better, but this routine has no access to that.
1196: # On mozilla, the 0-millisecond timeout seems to prevent this;
1197: # it's quite likely this might fix other browsers, too, and
1198: # certainly won't hurt anything.
1.139 bowersj2 1199: if ($displayedJumpMarker) {
1.159 bowersj2 1200: $result .= "<script>setTimeout(\"location += '#curloc';\", 0)</script>\n";
1.134 bowersj2 1201: }
1202:
1.132 bowersj2 1203: $result .= "</table>";
1.140 bowersj2 1204:
1205: if ($r) {
1206: $r->print($result);
1207: $result = "";
1208: $r->rflush();
1209: }
1210:
1.164 bowersj2 1211: #if ($mustCloseNavMap) { $navmap->untieHashes(); }
1.132 bowersj2 1212:
1213: return $result;
1214: }
1215:
1.133 bowersj2 1216: 1;
1217:
1218: package Apache::lonnavmaps::navmap;
1219:
1220: =pod
1221:
1222: lonnavmaps provides functions and objects for dealing with the compiled course hashes generated when a user enters the course, the Apache handler for the "Navigation Map" button, and a flexible prepared renderer for navigation maps that are easy to use anywhere.
1223:
1224: =head1 navmap object: Encapsulating the compiled nav map
1225:
1226: navmap is an object that encapsulates a compiled course map and provides a reasonable interface to it.
1227:
1228: 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.
1229:
1230: You must obtain resource objects through the navmap object.
1231:
1232: =head2 Methods
1233:
1234: =over 4
1235:
1.168 ! bowersj2 1236: =item * B<new>(navHashFile, parmHashFile, genCourseAndUserOptions,
! 1237: genMailDiscussStatus): Binds a new navmap object to the compiled nav map
! 1238: hash and parm hash given as filenames. genCourseAndUserOptions is a flag
! 1239: saying whether the course options and user options hash should be generated.
! 1240: This is for when you are using the parameters of the resources that require
! 1241: them; see documentation in resource object documentation. genMailDiscussStatus
! 1242: causes the nav map to retreive information about the email and discussion
! 1243: status of resources. Returns the navmap object if this is successful, or
! 1244: B<undef> if not. You must check for undef; errors will occur when you try
! 1245: to use the other methods otherwise.
1.133 bowersj2 1246:
1247: =item * B<getIterator>(first, finish, filter, condition): See iterator documentation below.
1248:
1249: =cut
1250:
1251: use strict;
1252: use GDBM_File;
1253:
1254: sub new {
1255: # magic invocation to create a class instance
1256: my $proto = shift;
1257: my $class = ref($proto) || $proto;
1258: my $self = {};
1259:
1260: $self->{NAV_HASH_FILE} = shift;
1261: $self->{PARM_HASH_FILE} = shift;
1262: $self->{GENERATE_COURSE_USER_OPT} = shift;
1263: $self->{GENERATE_EMAIL_DISCUSS_STATUS} = shift;
1264:
1265: # Resource cache stores navmap resources as we reference them. We generate
1266: # them on-demand so we don't pay for creating resources unless we use them.
1267: $self->{RESOURCE_CACHE} = {};
1268:
1269: # Network failure flag, if we accessed the course or user opt and
1270: # failed
1271: $self->{NETWORK_FAILURE} = 0;
1272:
1273: # tie the nav hash
1274:
1.168 ! bowersj2 1275: my %navmaphash;
! 1276: my %parmhash;
! 1277: if (!(tie(%navmaphash, 'GDBM_File', $self->{NAV_HASH_FILE},
1.133 bowersj2 1278: &GDBM_READER(), 0640))) {
1279: return undef;
1280: }
1281:
1.168 ! bowersj2 1282: if (!(tie(%parmhash, 'GDBM_File', $self->{PARM_HASH_FILE},
1.133 bowersj2 1283: &GDBM_READER(), 0640)))
1284: {
1285: untie $self->{PARM_HASH};
1286: return undef;
1287: }
1288:
1.168 ! bowersj2 1289: $self->{NAV_HASH} = \%navmaphash;
! 1290: $self->{PARM_HASH} = \%parmhash;
1.140 bowersj2 1291: $self->{INITED} = 0;
1.133 bowersj2 1292:
1293: bless($self);
1294:
1295: return $self;
1296: }
1297:
1298: sub init {
1299: my $self = shift;
1.140 bowersj2 1300: if ($self->{INITED}) { return; }
1.133 bowersj2 1301:
1302: # If the course opt hash and the user opt hash should be generated,
1303: # generate them
1304: if ($self->{GENERATE_COURSE_USER_OPT}) {
1305: my $uname=$ENV{'user.name'};
1306: my $udom=$ENV{'user.domain'};
1307: my $uhome=$ENV{'user.home'};
1308: my $cid=$ENV{'request.course.id'};
1309: my $chome=$ENV{'course.'.$cid.'.home'};
1310: my ($cdom,$cnum)=split(/\_/,$cid);
1311:
1312: my $userprefix=$uname.'_'.$udom.'_';
1313:
1314: my %courserdatas; my %useropt; my %courseopt; my %userrdatas;
1315: unless ($uhome eq 'no_host') {
1316: # ------------------------------------------------- Get coursedata (if present)
1317: unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
1318: my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
1319: ':resourcedata',$chome);
1320: if ($reply!~/^error\:/) {
1321: $courserdatas{$cid}=$reply;
1322: $courserdatas{$cid.'.last_cache'}=time;
1323: }
1324: # check to see if network failed
1325: elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
1326: {
1327: $self->{NETWORK_FAILURE} = 1;
1328: }
1329: }
1330: foreach (split(/\&/,$courserdatas{$cid})) {
1331: my ($name,$value)=split(/\=/,$_);
1332: $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
1333: &Apache::lonnet::unescape($value);
1334: }
1335: # --------------------------------------------------- Get userdata (if present)
1336: unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
1337: my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
1338: if ($reply!~/^error\:/) {
1339: $userrdatas{$uname.'___'.$udom}=$reply;
1340: $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
1341: }
1342: # check to see if network failed
1343: elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
1344: {
1345: $self->{NETWORK_FAILURE} = 1;
1346: }
1347: }
1348: foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
1349: my ($name,$value)=split(/\=/,$_);
1350: $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
1351: &Apache::lonnet::unescape($value);
1352: }
1353: $self->{COURSE_OPT} = \%courseopt;
1354: $self->{USER_OPT} = \%useropt;
1355: }
1356: }
1357:
1358: if ($self->{GENERATE_EMAIL_DISCUSS_STATUS}) {
1359: my $cid=$ENV{'request.course.id'};
1360: my ($cdom,$cnum)=split(/\_/,$cid);
1361:
1362: my %emailstatus = &Apache::lonnet::dump('email_status');
1363: my $logoutTime = $emailstatus{'logout'};
1364: my $courseLeaveTime = $emailstatus{'logout_'.$ENV{'request.course.id'}};
1365: $self->{LAST_CHECK} = ($courseLeaveTime < $logoutTime ?
1366: $courseLeaveTime : $logoutTime);
1367: my %discussiontime = &Apache::lonnet::dump('discussiontimes',
1368: $cdom, $cnum);
1369: my %feedback=();
1370: my %error=();
1371: my $keys = &Apache::lonnet::reply('keys:'.
1372: $ENV{'user.domain'}.':'.
1373: $ENV{'user.name'}.':nohist_email',
1374: $ENV{'user.home'});
1375:
1376: foreach my $msgid (split(/\&/, $keys)) {
1377: $msgid=&Apache::lonnet::unescape($msgid);
1378: my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
1379: if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
1380: my ($what,$url)=($1,$2);
1381: my %status=
1382: &Apache::lonnet::get('email_status',[$msgid]);
1383: if ($status{$msgid}=~/^error\:/) {
1384: $status{$msgid}='';
1385: }
1386:
1387: if (($status{$msgid} eq 'new') ||
1388: (!$status{$msgid})) {
1389: if ($what eq 'Error') {
1390: $error{$url}.=','.$msgid;
1391: } else {
1392: $feedback{$url}.=','.$msgid;
1393: }
1394: }
1395: }
1396: }
1397:
1398: $self->{FEEDBACK} = \%feedback;
1399: $self->{ERROR_MSG} = \%error; # what is this? JB
1400: $self->{DISCUSSION_TIME} = \%discussiontime;
1401: $self->{EMAIL_STATUS} = \%emailstatus;
1402:
1403: }
1404:
1405: $self->{PARM_CACHE} = {};
1.140 bowersj2 1406: $self->{INITED} = 1;
1.133 bowersj2 1407: }
1408:
1409: # Internal function: Takes a key to look up in the nav hash and implements internal
1410: # memory caching of that key.
1411: sub navhash {
1412: my $self = shift; my $key = shift;
1413: return $self->{NAV_HASH}->{$key};
1414: }
1415:
1416: # Checks to see if coursemap is defined, matching test in old lonnavmaps
1417: sub courseMapDefined {
1418: my $self = shift;
1419: my $uri = &Apache::lonnet::clutter($ENV{'request.course.uri'});
1420:
1421: my $firstres = $self->navhash("map_start_$uri");
1422: my $lastres = $self->navhash("map_finish_$uri");
1423: return $firstres && $lastres;
1424: }
1425:
1426: sub getIterator {
1427: my $self = shift;
1428: my $iterator = Apache::lonnavmaps::iterator->new($self, shift, shift,
1429: shift, undef, shift);
1430: return $iterator;
1431: }
1432:
1433: # unties the hash when done
1434: sub untieHashes {
1.168 ! bowersj2 1435: my $self = shift;
! 1436: untie $self->{NAV_HASH};
! 1437: untie $self->{PARM_HASH};
1.133 bowersj2 1438: }
1439:
1440: # Private method: Does the given resource (as a symb string) have
1441: # current discussion? Returns 0 if chat/mail data not extracted.
1442: sub hasDiscussion {
1443: my $self = shift;
1444: my $symb = shift;
1445: if (!defined($self->{DISCUSSION_TIME})) { return 0; }
1446:
1447: #return defined($self->{DISCUSSION_TIME}->{$symb});
1448: return $self->{DISCUSSION_TIME}->{$symb} >
1449: $self->{LAST_CHECK};
1450: }
1451:
1452: # Private method: Does the given resource (as a symb string) have
1453: # current feedback? Returns the string in the feedback hash, which
1454: # will be false if it does not exist.
1455: sub getFeedback {
1456: my $self = shift;
1457: my $symb = shift;
1458:
1459: if (!defined($self->{FEEDBACK})) { return ""; }
1460:
1461: return $self->{FEEDBACK}->{$symb};
1462: }
1463:
1464: # Private method: Get the errors for that resource (by source).
1465: sub getErrors {
1466: my $self = shift;
1467: my $src = shift;
1468:
1469: if (!defined($self->{ERROR_MSG})) { return ""; }
1470: return $self->{ERROR_MSG}->{$src};
1471: }
1472:
1473: =pod
1474:
1475: =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.
1476:
1477: =cut
1478:
1479: # The strategy here is to cache the resource objects, and only construct them
1480: # as we use them. The real point is to prevent reading any more from the tied
1481: # hash then we have to, which should hopefully alleviate speed problems.
1482: # Caching is just an incidental detail I throw in because it makes sense.
1483:
1484: sub getById {
1485: my $self = shift;
1486: my $id = shift;
1487:
1488: if (defined ($self->{RESOURCE_CACHE}->{$id}))
1489: {
1490: return $self->{RESOURCE_CACHE}->{$id};
1491: }
1492:
1493: # resource handles inserting itself into cache.
1494: # Not clear why the quotes are necessary, but as of this
1495: # writing it doesn't work without them.
1496: return "Apache::lonnavmaps::resource"->new($self, $id);
1.132 bowersj2 1497: }
1.133 bowersj2 1498:
1499: =pod
1500:
1501: =item * B<firstResource>(): Returns a resource object reference corresponding to the first resource in the navmap.
1502:
1503: =cut
1504:
1505: sub firstResource {
1506: my $self = shift;
1507: my $firstResource = $self->navhash('map_start_' .
1508: &Apache::lonnet::clutter($ENV{'request.course.uri'}));
1509: return $self->getById($firstResource);
1.132 bowersj2 1510: }
1.133 bowersj2 1511:
1512: =pod
1513:
1514: =item * B<finishResource>(): Returns a resource object reference corresponding to the last resource in the navmap.
1515:
1516: =cut
1517:
1518: sub finishResource {
1519: my $self = shift;
1520: my $firstResource = $self->navhash('map_finish_' .
1521: &Apache::lonnet::clutter($ENV{'request.course.uri'}));
1522: return $self->getById($firstResource);
1.132 bowersj2 1523: }
1.133 bowersj2 1524:
1525: # Parmval reads the parm hash and cascades the lookups. parmval_real does
1526: # the actual lookup; parmval caches the results.
1527: sub parmval {
1528: my $self = shift;
1529: my ($what,$symb)=@_;
1530: my $hashkey = $what."|||".$symb;
1531:
1532: if (defined($self->{PARM_CACHE}->{$hashkey})) {
1533: return $self->{PARM_CACHE}->{$hashkey};
1534: }
1535:
1536: my $result = $self->parmval_real($what, $symb);
1537: $self->{PARM_CACHE}->{$hashkey} = $result;
1538: return $result;
1.132 bowersj2 1539: }
1540:
1.133 bowersj2 1541: sub parmval_real {
1542: my $self = shift;
1543: my ($what,$symb) = @_;
1544:
1545: my $cid=$ENV{'request.course.id'};
1546: my $csec=$ENV{'request.course.sec'};
1547: my $uname=$ENV{'user.name'};
1548: my $udom=$ENV{'user.domain'};
1549:
1550: unless ($symb) { return ''; }
1551: my $result='';
1552:
1553: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
1554:
1555: # ----------------------------------------------------- Cascading lookup scheme
1556: my $rwhat=$what;
1557: $what=~s/^parameter\_//;
1558: $what=~s/\_/\./;
1559:
1560: my $symbparm=$symb.'.'.$what;
1561: my $mapparm=$mapname.'___(all).'.$what;
1562: my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
1563:
1564: my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
1565: my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
1566: my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
1567:
1568: my $courselevel= $usercourseprefix.'.'.$what;
1569: my $courselevelr=$usercourseprefix.'.'.$symbparm;
1570: my $courselevelm=$usercourseprefix.'.'.$mapparm;
1571:
1572: my $useropt = $self->{USER_OPT};
1573: my $courseopt = $self->{COURSE_OPT};
1574: my $parmhash = $self->{PARM_HASH};
1575:
1576: # ---------------------------------------------------------- first, check user
1577: if ($uname and defined($useropt)) {
1578: if (defined($$useropt{$courselevelr})) { return $$useropt{$courselevelr}; }
1579: if (defined($$useropt{$courselevelm})) { return $$useropt{$courselevelm}; }
1580: if (defined($$useropt{$courselevel})) { return $$useropt{$courselevel}; }
1581: }
1582:
1583: # ------------------------------------------------------- second, check course
1584: if ($csec and defined($courseopt)) {
1585: if (defined($$courseopt{$seclevelr})) { return $$courseopt{$seclevelr}; }
1586: if (defined($$courseopt{$seclevelm})) { return $$courseopt{$seclevelm}; }
1587: if (defined($$courseopt{$seclevel})) { return $$courseopt{$seclevel}; }
1588: }
1589:
1590: if (defined($courseopt)) {
1591: if (defined($$courseopt{$courselevelr})) { return $$courseopt{$courselevelr}; }
1592: if (defined($$courseopt{$courselevelm})) { return $$courseopt{$courselevelm}; }
1593: if (defined($$courseopt{$courselevel})) { return $$courseopt{$courselevel}; }
1594: }
1595:
1596: # ----------------------------------------------------- third, check map parms
1597:
1598: my $thisparm=$$parmhash{$symbparm};
1599: if (defined($thisparm)) { return $thisparm; }
1600:
1601: # ----------------------------------------------------- fourth , check default
1602:
1603: my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
1604: if (defined($default)) { return $default}
1605:
1606: # --------------------------------------------------- fifth , cascade up parts
1607:
1608: my ($space,@qualifier)=split(/\./,$rwhat);
1609: my $qualifier=join('.',@qualifier);
1610: unless ($space eq '0') {
1.160 albertel 1611: my @parts=split(/_/,$space);
1612: my $id=pop(@parts);
1613: my $part=join('_',@parts);
1614: if ($part eq '') { $part='0'; }
1615: my $partgeneral=$self->parmval($part.".$qualifier",$symb);
1616: if (defined($partgeneral)) { return $partgeneral; }
1.133 bowersj2 1617: }
1618: return '';
1.145 bowersj2 1619: }
1620:
1621: =pod
1622:
1623: =item * B<getResourceByUrl>(url): Retrieves a resource object by URL of the resource. If passed a resource object, it will simply return it, so it is safe to use this method in code like "$res = $navmap->getResourceByUrl($res)", if you're not sure if $res is already an object, or just a URL. If the resource appears multiple times in the course, only the first instance will be returned. As a result, this is probably useful only for maps.
1624:
1625: =item * B<retrieveResources>(map, filterFunc, recursive, bailout): The map is a specification of a map to retreive the resources from, either as a url or as an object. The filterFunc is a reference to a function that takes a resource object as its one argument and returns true if the resource should be included, or false if it should not be. If recursive is true, the map will be recursively examined, otherwise it will not be. If bailout is true, the function will return as soon as it finds a resource, if false it will finish. By default, the map is the top-level map of the course, filterFunc is a function that always returns 1, recursive is true, bailout is false. The resources will be returned in a list reference containing the resource objects for the corresponding resources, with B<no structure information> in the list; regardless of branching, recursion, etc., it will be a flat list.
1626:
1627: Thus, this is suitable for cases where you don't want the structure, just a list of all resources. It is also suitable for finding out how many resources match a given description; for this use, if all you want to know is if I<any> resources match the description, the bailout parameter will allow you to avoid potentially expensive enumeration of all matching resources.
1628:
1.146 bowersj2 1629: =item * B<hasResources>(map, filterFunc, recursive): Convience method for
1630:
1631: scalar(retrieveResources($map, $filterFunc, $recursive, 1)) > 0
1632:
1633: which will tell whether the map has resources matching the description in the filter function.
1634:
1.145 bowersj2 1635: =cut
1636:
1637: sub getResourceByUrl {
1638: my $self = shift;
1639: my $resUrl = shift;
1640:
1641: if (ref($resUrl)) { return $resUrl; }
1642:
1643: $resUrl = &Apache::lonnet::clutter($resUrl);
1644: my $resId = $self->{NAV_HASH}->{'ids_' . $resUrl};
1645: if ($resId =~ /,/) {
1646: $resId = (split (/,/, $resId))[0];
1647: }
1648: if (!$resId) { return ''; }
1649: return $self->getById($resId);
1650: }
1651:
1652: sub retrieveResources {
1653: my $self = shift;
1654: my $map = shift;
1655: my $filterFunc = shift;
1656: if (!defined ($filterFunc)) {
1657: $filterFunc = sub {return 1;};
1658: }
1659: my $recursive = shift;
1660: if (!defined($recursive)) { $recursive = 1; }
1661: my $bailout = shift;
1662: if (!defined($bailout)) { $bailout = 0; }
1663:
1664: # Create the necessary iterator.
1665: if (!ref($map)) { # assume it's a url of a map.
1666: $map = $self->getMapByUrl($map);
1667: }
1668:
1669: # Check the map's validity.
1670: if (!$map || !$map->is_map()) {
1671: # Oh, to throw an exception.... how I'd love that!
1672: return ();
1673: }
1674:
1.146 bowersj2 1675: # Get an iterator.
1676: my $it = $self->getIterator($map->map_start(), $map->map_finish(),
1677: !$recursive);
1678:
1679: my @resources = ();
1680:
1681: # Run down the iterator and collect the resources.
1682: my $depth = 1;
1683: $it->next();
1684: my $curRes = $it->next();
1685:
1686: while ($depth > 0) {
1687: if ($curRes == $it->BEGIN_MAP()) {
1688: $depth++;
1689: }
1690: if ($curRes == $it->END_MAP()) {
1691: $depth--;
1692: }
1693:
1694: if (ref($curRes)) {
1695: if (!&$filterFunc($curRes)) {
1696: next;
1697: }
1698:
1699: push @resources, $curRes;
1700:
1701: if ($bailout) {
1702: return @resources;
1703: }
1704: }
1705:
1706: $curRes = $it->next();
1707: }
1708:
1709: return @resources;
1710: }
1711:
1712: sub hasResource {
1713: my $self = shift;
1714: my $map = shift;
1715: my $filterFunc = shift;
1716: my $recursive = shift;
1717:
1718: return scalar($self->retrieveResources($map, $filterFunc, $recursive, 1)) > 0;
1.133 bowersj2 1719: }
1.51 bowersj2 1720:
1721: 1;
1722:
1723: package Apache::lonnavmaps::iterator;
1724:
1725: =pod
1726:
1727: =back
1728:
1729: =head1 navmap Iterator
1730:
1731: 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.
1732:
1733: 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:
1734:
1735: C<my $resourceIterator = $navmap-E<gt>getIterator();>
1736:
1737: To get the next thing from the iterator, call B<next>:
1738:
1739: C<my $nextThing = $resourceIterator-E<gt>next()>
1740:
1741: getIterator behaves as follows:
1742:
1743: =over 4
1744:
1.162 bowersj2 1745: =item * B<getIterator>(firstResource, finishResource, filterHash, condition, forceTop, returnTopMap): 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. returnTopMap, if true (default false), will cause the iterator to return the top-level map object (resource 0.0) before anything else.
1.51 bowersj2 1746:
1.101 bowersj2 1747: 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 1748:
1749: 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:
1750:
1751: =over 4
1752:
1.70 bowersj2 1753: =item * BEGIN_MAP: A new map is being recursed into. This is returned I<after> the map resource itself is returned.
1754:
1755: =item * END_MAP: The map is now done.
1756:
1757: =item * BEGIN_BRANCH: A branch is now starting. The next resource returned will be the first in that branch.
1758:
1759: =item * END_BRANCH: The branch is now done.
1.51 bowersj2 1760:
1761: =back
1762:
1.70 bowersj2 1763: The tokens are retreivable via methods on the iterator object, i.e., $iterator->END_MAP.
1764:
1.116 bowersj2 1765: 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.
1766:
1.70 bowersj2 1767: =back
1.51 bowersj2 1768:
1769: =cut
1770:
1771: # Here are the tokens for the iterator:
1772:
1773: sub BEGIN_MAP { return 1; } # begining of a new map
1774: sub END_MAP { return 2; } # end of the map
1775: sub BEGIN_BRANCH { return 3; } # beginning of a branch
1776: sub END_BRANCH { return 4; } # end of a branch
1.89 bowersj2 1777: sub FORWARD { return 1; } # go forward
1778: sub BACKWARD { return 2; }
1.51 bowersj2 1779:
1.96 bowersj2 1780: sub min {
1781: (my $a, my $b) = @_;
1782: if ($a < $b) { return $a; } else { return $b; }
1783: }
1784:
1.107 bowersj2 1785: # In the CVS repository, documentation of this algorithm is included
1786: # in /doc/lonnavdocs, as a PDF and .tex source. Markers like **1**
1787: # will reference the same location in the text as the part of the
1788: # algorithm is running through.
1789:
1.94 bowersj2 1790: sub new {
1791: # magic invocation to create a class instance
1792: my $proto = shift;
1793: my $class = ref($proto) || $proto;
1794: my $self = {};
1795:
1796: $self->{NAV_MAP} = shift;
1797: return undef unless ($self->{NAV_MAP});
1798:
1799: # Handle the parameters
1800: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
1801: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
1802:
1803: # If the given resources are just the ID of the resource, get the
1804: # objects
1805: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
1806: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
1807: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
1808: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
1809:
1810: $self->{FILTER} = shift;
1811:
1812: # A hash, used as a set, of resource already seen
1813: $self->{ALREADY_SEEN} = shift;
1814: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1815: $self->{CONDITION} = shift;
1816:
1.116 bowersj2 1817: # Do we want to automatically follow "redirection" maps?
1818: $self->{FORCE_TOP} = shift;
1819:
1.162 bowersj2 1820: # Do we want to return the top-level map object (resource 0.0)?
1821: $self->{RETURN_0} = shift;
1822: # have we done that yet?
1823: $self->{HAVE_RETURNED_0} = 0;
1824:
1.94 bowersj2 1825: # Now, we need to pre-process the map, by walking forward and backward
1826: # over the parts of the map we're going to look at.
1.96 bowersj2 1827:
1.97 bowersj2 1828: # The processing steps are exactly the same, except for a few small
1829: # changes, so I bundle those up in the following list of two elements:
1830: # (direction_to_iterate, VAL_name, next_resource_method_to_call,
1831: # first_resource).
1832: # This prevents writing nearly-identical code twice.
1833: my @iterations = ( [FORWARD(), 'TOP_DOWN_VAL', 'getNext',
1834: 'FIRST_RESOURCE'],
1835: [BACKWARD(), 'BOT_UP_VAL', 'getPrevious',
1836: 'FINISH_RESOURCE'] );
1837:
1.98 bowersj2 1838: my $maxDepth = 0; # tracks max depth
1839:
1.116 bowersj2 1840: # If there is only one resource in this map, and it's a map, we
1841: # want to remember that, so the user can ask for the first map
1842: # that isn't just a redirector.
1843: my $resource; my $resourceCount = 0;
1844:
1.107 bowersj2 1845: # **1**
1846:
1.97 bowersj2 1847: foreach my $pass (@iterations) {
1848: my $direction = $pass->[0];
1849: my $valName = $pass->[1];
1850: my $nextResourceMethod = $pass->[2];
1851: my $firstResourceName = $pass->[3];
1852:
1853: my $iterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP},
1854: $self->{FIRST_RESOURCE},
1855: $self->{FINISH_RESOURCE},
1856: {}, undef, 0, $direction);
1.96 bowersj2 1857:
1.97 bowersj2 1858: # prime the recursion
1859: $self->{$firstResourceName}->{DATA}->{$valName} = 0;
1.98 bowersj2 1860: my $depth = 0;
1.97 bowersj2 1861: $iterator->next();
1862: my $curRes = $iterator->next();
1.98 bowersj2 1863: while ($depth > -1) {
1.97 bowersj2 1864: if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
1865: if ($curRes == $iterator->END_MAP()) { $depth--; }
1.96 bowersj2 1866:
1.97 bowersj2 1867: if (ref($curRes)) {
1.116 bowersj2 1868: # If there's only one resource, this will save it
1.117 bowersj2 1869: # we have to filter empty resources from consideration here,
1870: # or even "empty", redirecting maps have two (start & finish)
1871: # or three (start, finish, plus redirector)
1872: if($direction == FORWARD && $curRes->src()) {
1873: $resource = $curRes; $resourceCount++;
1874: }
1.97 bowersj2 1875: my $resultingVal = $curRes->{DATA}->{$valName};
1876: my $nextResources = $curRes->$nextResourceMethod();
1.116 bowersj2 1877: my $nextCount = scalar(@{$nextResources});
1.104 bowersj2 1878:
1.116 bowersj2 1879: if ($nextCount == 1) { # **3**
1.97 bowersj2 1880: my $current = $nextResources->[0]->{DATA}->{$valName} || 999999999;
1881: $nextResources->[0]->{DATA}->{$valName} = min($resultingVal, $current);
1882: }
1883:
1.116 bowersj2 1884: if ($nextCount > 1) { # **4**
1.97 bowersj2 1885: foreach my $res (@{$nextResources}) {
1886: my $current = $res->{DATA}->{$valName} || 999999999;
1887: $res->{DATA}->{$valName} = min($current, $resultingVal + 1);
1888: }
1889: }
1890: }
1.96 bowersj2 1891:
1.107 bowersj2 1892: # Assign the final val (**2**)
1.97 bowersj2 1893: if (ref($curRes) && $direction == BACKWARD()) {
1.98 bowersj2 1894: my $finalDepth = min($curRes->{DATA}->{TOP_DOWN_VAL},
1895: $curRes->{DATA}->{BOT_UP_VAL});
1896:
1897: $curRes->{DATA}->{DISPLAY_DEPTH} = $finalDepth;
1898: if ($finalDepth > $maxDepth) {$maxDepth = $finalDepth;}
1899: }
1.97 bowersj2 1900: $curRes = $iterator->next();
1.96 bowersj2 1901: }
1902: }
1.94 bowersj2 1903:
1.116 bowersj2 1904: # Check: Was this only one resource, a map?
1905: if ($resourceCount == 1 && $resource->is_map() && !$self->{FORCE_TOP}) {
1906: my $firstResource = $resource->map_start();
1907: my $finishResource = $resource->map_finish();
1908: return
1909: Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
1910: $finishResource, $self->{FILTER},
1911: $self->{ALREADY_SEEN},
1912: $self->{CONDITION}, 0);
1913:
1914: }
1915:
1.98 bowersj2 1916: # Set up some bookkeeping information.
1917: $self->{CURRENT_DEPTH} = 0;
1918: $self->{MAX_DEPTH} = $maxDepth;
1919: $self->{STACK} = [];
1920: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1921:
1922: for (my $i = 0; $i <= $self->{MAX_DEPTH}; $i++) {
1923: push @{$self->{STACK}}, [];
1924: }
1925:
1.107 bowersj2 1926: # Prime the recursion w/ the first resource **5**
1.98 bowersj2 1927: push @{$self->{STACK}->[0]}, $self->{FIRST_RESOURCE};
1928: $self->{ALREADY_SEEN}->{$self->{FIRST_RESOURCE}->{ID}} = 1;
1929:
1930: bless ($self);
1931:
1932: return $self;
1933: }
1934:
1935: sub next {
1936: my $self = shift;
1.162 bowersj2 1937:
1938: # If we want to return the top-level map object, and haven't yet,
1939: # do so.
1940: if ($self->{RETURN_0} && !$self->{HAVE_RETURNED_0}) {
1941: $self->{HAVE_RETURNED_0} = 1;
1942: return $self->{NAV_MAP}->getById('0.0');
1943: }
1.98 bowersj2 1944:
1945: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1946: # grab the next from the recursive iterator
1947: my $next = $self->{RECURSIVE_ITERATOR}->next();
1948:
1949: # is it a begin or end map? If so, update the depth
1950: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
1951: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
1952:
1953: # Are we back at depth 0? If so, stop recursing
1954: if ($self->{RECURSIVE_DEPTH} == 0) {
1955: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1956: }
1957:
1958: return $next;
1959: }
1960:
1961: if (defined($self->{FORCE_NEXT})) {
1962: my $tmp = $self->{FORCE_NEXT};
1963: $self->{FORCE_NEXT} = undef;
1964: return $tmp;
1965: }
1966:
1967: # Have we not yet begun? If not, return BEGIN_MAP and
1968: # remember we've started.
1969: if ( !$self->{STARTED} ) {
1970: $self->{STARTED} = 1;
1971: return $self->BEGIN_MAP();
1972: }
1973:
1974: # Here's the guts of the iterator.
1975:
1976: # Find the next resource, if any.
1977: my $found = 0;
1978: my $i = $self->{MAX_DEPTH};
1979: my $newDepth;
1980: my $here;
1981: while ( $i >= 0 && !$found ) {
1.107 bowersj2 1982: if ( scalar(@{$self->{STACK}->[$i]}) > 0 ) { # **6**
1983: $here = pop @{$self->{STACK}->[$i]}; # **7**
1.98 bowersj2 1984: $found = 1;
1985: $newDepth = $i;
1986: }
1987: $i--;
1988: }
1989:
1990: # If we still didn't find anything, we're done.
1991: if ( !$found ) {
1992: # We need to get back down to the correct branch depth
1993: if ( $self->{CURRENT_DEPTH} > 0 ) {
1994: $self->{CURRENT_DEPTH}--;
1995: return END_BRANCH();
1996: } else {
1997: return END_MAP();
1998: }
1999: }
2000:
1.104 bowersj2 2001: # If this is not a resource, it must be an END_BRANCH marker we want
2002: # to return directly.
1.107 bowersj2 2003: if (!ref($here)) { # **8**
1.104 bowersj2 2004: if ($here == END_BRANCH()) { # paranoia, in case of later extension
2005: $self->{CURRENT_DEPTH}--;
2006: return $here;
2007: }
2008: }
2009:
2010: # Otherwise, it is a resource and it's safe to store in $self->{HERE}
2011: $self->{HERE} = $here;
2012:
1.98 bowersj2 2013: # Get to the right level
2014: if ( $self->{CURRENT_DEPTH} > $newDepth ) {
2015: push @{$self->{STACK}->[$newDepth]}, $here;
2016: $self->{CURRENT_DEPTH}--;
2017: return END_BRANCH();
2018: }
2019: if ( $self->{CURRENT_DEPTH} < $newDepth) {
2020: push @{$self->{STACK}->[$newDepth]}, $here;
2021: $self->{CURRENT_DEPTH}++;
2022: return BEGIN_BRANCH();
2023: }
2024:
2025: # If we made it here, we have the next resource, and we're at the
2026: # right branch level. So let's examine the resource for where
2027: # we can get to from here.
2028:
2029: # So we need to look at all the resources we can get to from here,
2030: # categorize them if we haven't seen them, remember if we have a new
2031: my $nextUnfiltered = $here->getNext();
1.104 bowersj2 2032: my $maxDepthAdded = -1;
2033:
1.98 bowersj2 2034: for (@$nextUnfiltered) {
2035: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
1.104 bowersj2 2036: my $depth = $_->{DATA}->{DISPLAY_DEPTH};
2037: push @{$self->{STACK}->[$depth]}, $_;
1.98 bowersj2 2038: $self->{ALREADY_SEEN}->{$_->{ID}} = 1;
1.104 bowersj2 2039: if ($maxDepthAdded < $depth) { $maxDepthAdded = $depth; }
1.98 bowersj2 2040: }
2041: }
1.104 bowersj2 2042:
2043: # Is this the end of a branch? If so, all of the resources examined above
2044: # led to lower levels then the one we are currently at, so we push a END_BRANCH
2045: # marker onto the stack so we don't forget.
2046: # Example: For the usual A(BC)(DE)F case, when the iterator goes down the
2047: # BC branch and gets to C, it will see F as the only next resource, but it's
2048: # one level lower. Thus, this is the end of the branch, since there are no
2049: # more resources added to this level or above.
1.111 bowersj2 2050: # We don't do this if the examined resource is the finish resource,
2051: # because the condition given above is true, but the "END_MAP" will
2052: # take care of things and we should already be at depth 0.
1.104 bowersj2 2053: my $isEndOfBranch = $maxDepthAdded < $self->{CURRENT_DEPTH};
1.111 bowersj2 2054: if ($isEndOfBranch && $here != $self->{FINISH_RESOURCE}) { # **9**
1.104 bowersj2 2055: push @{$self->{STACK}->[$self->{CURRENT_DEPTH}]}, END_BRANCH();
2056: }
2057:
1.98 bowersj2 2058: # That ends the main iterator logic. Now, do we want to recurse
2059: # down this map (if this resource is a map)?
2060: if ($self->{HERE}->is_map() &&
2061: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
2062: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
2063: my $firstResource = $self->{HERE}->map_start();
2064: my $finishResource = $self->{HERE}->map_finish();
2065:
2066: $self->{RECURSIVE_ITERATOR} =
2067: Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
2068: $finishResource, $self->{FILTER},
2069: $self->{ALREADY_SEEN}, $self->{CONDITION});
2070: }
2071:
1.116 bowersj2 2072: # If this is a blank resource, don't actually return it.
1.117 bowersj2 2073: # Should you ever find you need it, make sure to add an option to the code
2074: # that you can use; other things depend on this behavior.
1.138 bowersj2 2075: my $browsePriv = $self->{HERE}->browsePriv();
2076: if (!$self->{HERE}->src() ||
2077: (!($browsePriv eq 'F') && !($browsePriv eq '2')) ) {
1.116 bowersj2 2078: return $self->next();
2079: }
2080:
1.98 bowersj2 2081: return $self->{HERE};
2082:
2083: }
2084:
2085: =pod
2086:
2087: 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).
2088:
2089: =cut
2090:
2091: sub getStack {
2092: my $self=shift;
2093:
2094: my @stack;
2095:
2096: $self->populateStack(\@stack);
2097:
2098: return \@stack;
2099: }
2100:
2101: # Private method: Calls the iterators recursively to populate the stack.
2102: sub populateStack {
2103: my $self=shift;
2104: my $stack = shift;
2105:
2106: push @$stack, $self->{HERE} if ($self->{HERE});
2107:
2108: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
2109: $self->{RECURSIVE_ITERATOR}->populateStack($stack);
2110: }
1.94 bowersj2 2111: }
2112:
2113: 1;
2114:
2115: package Apache::lonnavmaps::DFSiterator;
2116:
1.100 bowersj2 2117: # Not documented in the perldoc: This is a simple iterator that just walks
2118: # through the nav map and presents the resources in a depth-first search
2119: # fashion, ignorant of conditionals, randomized resources, etc. It presents
2120: # BEGIN_MAP and END_MAP, but does not understand branches at all. It is
2121: # useful for pre-processing of some kind, and is in fact used by the main
2122: # iterator that way, but that's about it.
2123: # One could imagine merging this into the init routine of the main iterator,
2124: # but this might as well be left seperate, since it is possible some other
2125: # use might be found for it. - Jeremy
1.94 bowersj2 2126:
1.117 bowersj2 2127: # Unlike the main iterator, this DOES return all resources, even blank ones.
2128: # The main iterator needs them to correctly preprocess the map.
2129:
1.94 bowersj2 2130: sub BEGIN_MAP { return 1; } # begining of a new map
2131: sub END_MAP { return 2; } # end of the map
2132: sub FORWARD { return 1; } # go forward
2133: sub BACKWARD { return 2; }
2134:
1.100 bowersj2 2135: # Params: Nav map ref, first resource id/ref, finish resource id/ref,
2136: # filter hash ref (or undef), already seen hash or undef, condition
2137: # (as in main iterator), direction FORWARD or BACKWARD (undef->forward).
1.51 bowersj2 2138: sub new {
2139: # magic invocation to create a class instance
2140: my $proto = shift;
2141: my $class = ref($proto) || $proto;
2142: my $self = {};
2143:
2144: $self->{NAV_MAP} = shift;
2145: return undef unless ($self->{NAV_MAP});
2146:
2147: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
2148: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
2149:
2150: # If the given resources are just the ID of the resource, get the
2151: # objects
2152: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
2153: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
2154: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
2155: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
2156:
2157: $self->{FILTER} = shift;
2158:
2159: # A hash, used as a set, of resource already seen
2160: $self->{ALREADY_SEEN} = shift;
1.140 bowersj2 2161: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1.63 bowersj2 2162: $self->{CONDITION} = shift;
1.89 bowersj2 2163: $self->{DIRECTION} = shift || FORWARD();
1.51 bowersj2 2164:
1.100 bowersj2 2165: # Flag: Have we started yet?
1.51 bowersj2 2166: $self->{STARTED} = 0;
2167:
2168: # Should we continue calling the recursive iterator, if any?
2169: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2170: # The recursive iterator, if any
2171: $self->{RECURSIVE_ITERATOR} = undef;
2172: # Are we recursing on a map, or a branch?
2173: $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
2174: # And the count of how deep it is, so that this iterator can keep track of
2175: # when to pick back up again.
2176: $self->{RECURSIVE_DEPTH} = 0;
2177:
2178: # For keeping track of our branches, we maintain our own stack
1.100 bowersj2 2179: $self->{STACK} = [];
1.51 bowersj2 2180:
2181: # Start with the first resource
1.89 bowersj2 2182: if ($self->{DIRECTION} == FORWARD) {
1.100 bowersj2 2183: push @{$self->{STACK}}, $self->{FIRST_RESOURCE};
1.89 bowersj2 2184: } else {
1.100 bowersj2 2185: push @{$self->{STACK}}, $self->{FINISH_RESOURCE};
1.89 bowersj2 2186: }
1.51 bowersj2 2187:
2188: bless($self);
2189: return $self;
2190: }
2191:
2192: sub next {
2193: my $self = shift;
2194:
2195: # Are we using a recursive iterator? If so, pull from that and
2196: # watch the depth; we want to resume our level at the correct time.
1.98 bowersj2 2197: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1.51 bowersj2 2198: # grab the next from the recursive iterator
2199: my $next = $self->{RECURSIVE_ITERATOR}->next();
2200:
2201: # is it a begin or end map? Update depth if so
2202: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
2203: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
2204:
2205: # Are we back at depth 0? If so, stop recursing.
2206: if ($self->{RECURSIVE_DEPTH} == 0) {
2207: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2208: }
2209:
2210: return $next;
2211: }
2212:
2213: # Is there a current resource to grab? If not, then return
1.100 bowersj2 2214: # END_MAP, which will end the iterator.
2215: if (scalar(@{$self->{STACK}}) == 0) {
2216: return $self->END_MAP();
1.51 bowersj2 2217: }
2218:
2219: # Have we not yet begun? If not, return BEGIN_MAP and
2220: # remember that we've started.
2221: if ( !$self->{STARTED} ) {
2222: $self->{STARTED} = 1;
2223: return $self->BEGIN_MAP;
2224: }
2225:
2226: # Get the next resource in the branch
1.100 bowersj2 2227: $self->{HERE} = pop @{$self->{STACK}};
1.52 bowersj2 2228:
1.100 bowersj2 2229: # remember that we've seen this, so we don't return it again later
1.51 bowersj2 2230: $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
2231:
2232: # Get the next possible resources
1.90 bowersj2 2233: my $nextUnfiltered;
1.89 bowersj2 2234: if ($self->{DIRECTION} == FORWARD()) {
1.90 bowersj2 2235: $nextUnfiltered = $self->{HERE}->getNext();
1.89 bowersj2 2236: } else {
1.90 bowersj2 2237: $nextUnfiltered = $self->{HERE}->getPrevious();
1.89 bowersj2 2238: }
1.51 bowersj2 2239: my $next = [];
2240:
2241: # filter the next possibilities to remove things we've
1.100 bowersj2 2242: # already seen.
1.51 bowersj2 2243: foreach (@$nextUnfiltered) {
1.52 bowersj2 2244: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
2245: push @$next, $_;
2246: }
1.51 bowersj2 2247: }
2248:
2249: while (@$next) {
1.100 bowersj2 2250: # copy the next possibilities over to the stack
2251: push @{$self->{STACK}}, shift @$next;
1.51 bowersj2 2252: }
2253:
2254: # If this is a map and we want to recurse down it... (not filtered out)
1.70 bowersj2 2255: if ($self->{HERE}->is_map() &&
1.63 bowersj2 2256: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
1.51 bowersj2 2257: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
2258: my $firstResource = $self->{HERE}->map_start();
2259: my $finishResource = $self->{HERE}->map_finish();
2260:
2261: $self->{RECURSIVE_ITERATOR} =
1.94 bowersj2 2262: Apache::lonnavmaps::DFSiterator->new ($self->{NAV_MAP}, $firstResource,
1.63 bowersj2 2263: $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
1.91 bowersj2 2264: $self->{CONDITION}, $self->{DIRECTION});
1.51 bowersj2 2265: }
2266:
2267: return $self->{HERE};
2268: }
2269:
1.1 www 2270: 1;
1.2 www 2271:
1.51 bowersj2 2272: package Apache::lonnavmaps::resource;
2273:
2274: use Apache::lonnet;
2275:
2276: =pod
2277:
2278: =head1 Object: resource
2279:
2280: 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.
2281:
2282: A resource only makes sense in the context of a navmap, as some of the data is stored in the navmap object.
2283:
1.140 bowersj2 2284: You will probably never need to instantiate this object directly. Use Apache::lonnavmaps::navmap, and use the "start" method to obtain the starting resource.
1.51 bowersj2 2285:
2286: =head2 Public Members
2287:
2288: 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.
2289:
2290: =head2 Methods
2291:
2292: =over 4
2293:
1.70 bowersj2 2294: =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.
2295:
2296: =back
1.51 bowersj2 2297:
2298: =cut
2299:
2300: sub new {
2301: # magic invocation to create a class instance
2302: my $proto = shift;
2303: my $class = ref($proto) || $proto;
2304: my $self = {};
2305:
2306: $self->{NAV_MAP} = shift;
2307: $self->{ID} = shift;
2308:
2309: # Store this new resource in the parent nav map's cache.
2310: $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
1.66 bowersj2 2311: $self->{RESOURCE_ERROR} = 0;
1.51 bowersj2 2312:
2313: # A hash that can be used by two-pass algorithms to store data
2314: # about this resource in. Not used by the resource object
2315: # directly.
2316: $self->{DATA} = {};
2317:
2318: bless($self);
2319:
2320: return $self;
2321: }
2322:
1.70 bowersj2 2323: # private function: simplify the NAV_HASH lookups we keep doing
2324: # pass the name, and to automatically append my ID, pass a true val on the
2325: # second param
2326: sub navHash {
2327: my $self = shift;
2328: my $param = shift;
2329: my $id = shift;
1.115 bowersj2 2330: return $self->{NAV_MAP}->navhash($param . ($id?$self->{ID}:""));
1.70 bowersj2 2331: }
2332:
1.51 bowersj2 2333: =pod
2334:
1.70 bowersj2 2335: B<Metadata Retreival>
2336:
1.101 bowersj2 2337: 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 2338:
2339: =over 4
2340:
1.106 bowersj2 2341: =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").
2342:
1.70 bowersj2 2343: =item * B<ext>: Returns true if the resource is external.
2344:
2345: =item * B<goesto>: Returns the "goesto" value from the compiled nav map. (It is likely you want to use B<getNext> instead.)
2346:
2347: =item * B<kind>: Returns the kind of the resource from the compiled nav map.
2348:
1.101 bowersj2 2349: =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 2350:
1.70 bowersj2 2351: =item * B<randompick>: Returns true for a map if the randompick feature is being used on the map. (?)
2352:
2353: =item * B<src>: Returns the source for the resource.
2354:
2355: =item * B<symb>: Returns the symb for the resource.
2356:
2357: =item * B<title>: Returns the title of the resource.
2358:
2359: =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 2360:
2361: =back
2362:
2363: =cut
2364:
2365: # These info functions can be used directly, as they don't return
2366: # resource information.
1.85 bowersj2 2367: sub comesfrom { my $self=shift; return $self->navHash("comesfrom_", 1); }
1.70 bowersj2 2368: sub ext { my $self=shift; return $self->navHash("ext_", 1) eq 'true:'; }
1.85 bowersj2 2369: sub from { my $self=shift; return $self->navHash("from_", 1); }
1.51 bowersj2 2370: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
2371: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
1.68 bowersj2 2372: sub randomout { my $self=shift; return $self->navHash("randomout_", 1); }
2373: sub randompick {
2374: my $self = shift;
2375: return $self->{NAV_MAP}->{PARM_HASH}->{$self->symb .
2376: '.0.parameter_randompick'};
2377: }
1.51 bowersj2 2378: sub src {
2379: my $self=shift;
2380: return $self->navHash("src_", 1);
2381: }
2382: sub symb {
2383: my $self=shift;
2384: (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
2385: my $symbSrc = &Apache::lonnet::declutter($self->src());
2386: return &Apache::lonnet::declutter(
2387: $self->navHash('map_id_'.$first))
2388: . '___' . $second . '___' . $symbSrc;
2389: }
1.70 bowersj2 2390: sub title { my $self=shift; return $self->navHash("title_", 1); }
2391: sub to { my $self=shift; return $self->navHash("to_", 1); }
1.106 bowersj2 2392: sub compTitle {
2393: my $self = shift;
2394: my $title = $self->title();
2395: if (!$title) {
2396: $title = $self->src();
2397: $title = substr($title, rindex($title, '/') + 1);
2398: }
2399: return $title;
2400: }
1.70 bowersj2 2401: =pod
2402:
2403: B<Predicate Testing the Resource>
2404:
2405: These methods are shortcuts to deciding if a given resource has a given property.
2406:
2407: =over 4
2408:
2409: =item * B<is_map>: Returns true if the resource is a map type.
2410:
2411: =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.)
2412:
2413: =item * B<is_page>: Returns true if the resource is a page.
2414:
1.101 bowersj2 2415: =item * B<is_sequence>: Returns true if the resource is a sequence.
1.70 bowersj2 2416:
2417: =back
2418:
2419: =cut
2420:
2421:
2422: sub is_html {
1.51 bowersj2 2423: my $self=shift;
2424: my $src = $self->src();
1.70 bowersj2 2425: return ($src =~ /html$/);
1.51 bowersj2 2426: }
1.70 bowersj2 2427: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
2428: sub is_page {
1.51 bowersj2 2429: my $self=shift;
2430: my $src = $self->src();
1.70 bowersj2 2431: return ($src =~ /page$/);
1.51 bowersj2 2432: }
1.70 bowersj2 2433: sub is_problem {
1.51 bowersj2 2434: my $self=shift;
2435: my $src = $self->src();
1.70 bowersj2 2436: return ($src =~ /problem$/);
1.51 bowersj2 2437: }
1.70 bowersj2 2438: sub is_sequence {
1.51 bowersj2 2439: my $self=shift;
2440: my $src = $self->src();
1.70 bowersj2 2441: return ($src =~ /sequence$/);
1.51 bowersj2 2442: }
2443:
1.101 bowersj2 2444: # Private method: Shells out to the parmval in the nav map, handler parts.
1.51 bowersj2 2445: sub parmval {
2446: my $self = shift;
2447: my $what = shift;
2448: my $part = shift || "0";
2449: return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
2450: }
2451:
1.70 bowersj2 2452: =pod
2453:
2454: B<Map Methods>
2455:
2456: These methods are useful for getting information about the map properties of the resource, if the resource is a map (B<is_map>).
2457:
2458: =over 4
2459:
2460: =item * B<map_finish>: Returns a reference to a resource object corresponding to the finish resource of the map.
2461:
2462: =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.
2463:
2464: =item * B<map_start>: Returns a reference to a resource object corresponding to the start resource of the map.
2465:
2466: =item * B<map_type>: Returns a string with the type of the map in it.
2467:
2468: =back
1.51 bowersj2 2469:
1.70 bowersj2 2470: =cut
1.51 bowersj2 2471:
2472: sub map_finish {
2473: my $self = shift;
2474: my $src = $self->src();
1.144 bowersj2 2475: $src = Apache::lonnet::clutter($src);
1.51 bowersj2 2476: my $res = $self->navHash("map_finish_$src", 0);
2477: $res = $self->{NAV_MAP}->getById($res);
2478: return $res;
2479: }
1.70 bowersj2 2480: sub map_pc {
2481: my $self = shift;
2482: my $src = $self->src();
2483: return $self->navHash("map_pc_$src", 0);
2484: }
1.51 bowersj2 2485: sub map_start {
2486: my $self = shift;
2487: my $src = $self->src();
1.144 bowersj2 2488: $src = Apache::lonnet::clutter($src);
1.51 bowersj2 2489: my $res = $self->navHash("map_start_$src", 0);
2490: $res = $self->{NAV_MAP}->getById($res);
2491: return $res;
2492: }
2493: sub map_type {
2494: my $self = shift;
2495: my $pc = $self->map_pc();
2496: return $self->navHash("map_type_$pc", 0);
2497: }
2498:
2499:
2500:
2501: #####
2502: # Property queries
2503: #####
2504:
2505: # These functions will be responsible for returning the CORRECT
2506: # VALUE for the parameter, no matter what. So while they may look
2507: # like direct calls to parmval, they can be more then that.
2508: # So, for instance, the duedate function should use the "duedatetype"
2509: # information, rather then the resource object user.
2510:
2511: =pod
2512:
2513: =head2 Resource Parameters
2514:
1.70 bowersj2 2515: 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 2516:
2517: 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.)
2518:
2519: =over 4
2520:
2521: =item * B<acc>: Get the Client IP/Name Access Control information.
2522:
2523: =item * B<answerdate>: Get the answer-reveal date for the problem.
2524:
2525: =item * B<duedate>: Get the due date for the problem.
2526:
2527: =item * B<tries>: Get the number of tries the student has used on the problem.
2528:
2529: =item * B<maxtries>: Get the number of max tries allowed.
2530:
2531: =item * B<opendate>: Get the open date for the problem.
2532:
2533: =item * B<sig>: Get the significant figures setting.
2534:
2535: =item * B<tol>: Get the tolerance for the problem.
2536:
1.70 bowersj2 2537: =item * B<tries>: Get the number of tries the user has already used on the problem.
2538:
1.51 bowersj2 2539: =item * B<type>: Get the question type for the problem.
2540:
2541: =item * B<weight>: Get the weight for the problem.
2542:
2543: =back
2544:
2545: =cut
2546:
2547: sub acc {
2548: (my $self, my $part) = @_;
2549: return $self->parmval("acc", $part);
2550: }
2551: sub answerdate {
2552: (my $self, my $part) = @_;
2553: # Handle intervals
2554: if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
2555: return $self->duedate($part) +
2556: $self->parmval("answerdate", $part);
2557: }
2558: return $self->parmval("answerdate", $part);
1.106 bowersj2 2559: }
1.108 bowersj2 2560: sub awarded { my $self = shift; return $self->queryRestoreHash('awarded', shift); }
1.51 bowersj2 2561: sub duedate {
2562: (my $self, my $part) = @_;
2563: return $self->parmval("duedate", $part);
2564: }
2565: sub maxtries {
2566: (my $self, my $part) = @_;
2567: return $self->parmval("maxtries", $part);
2568: }
2569: sub opendate {
2570: (my $self, my $part) = @_;
2571: if ($self->parmval("opendate.type", $part) eq 'date_interval') {
2572: return $self->duedate($part) -
2573: $self->parmval("opendate", $part);
2574: }
2575: return $self->parmval("opendate");
2576: }
2577: sub sig {
2578: (my $self, my $part) = @_;
2579: return $self->parmval("sig", $part);
2580: }
2581: sub tol {
2582: (my $self, my $part) = @_;
2583: return $self->parmval("tol", $part);
2584: }
1.108 bowersj2 2585: sub tries {
2586: my $self = shift;
2587: my $tries = $self->queryRestoreHash('tries', shift);
2588: if (!defined($tries)) { return '0';}
1.51 bowersj2 2589: return $tries;
2590: }
1.70 bowersj2 2591: sub type {
2592: (my $self, my $part) = @_;
2593: return $self->parmval("type", $part);
2594: }
1.109 bowersj2 2595: sub weight {
2596: my $self = shift; my $part = shift;
1.70 bowersj2 2597: return $self->parmval("weight", $part);
2598: }
1.51 bowersj2 2599:
2600: # Multiple things need this
2601: sub getReturnHash {
2602: my $self = shift;
2603:
2604: if (!defined($self->{RETURN_HASH})) {
1.84 bowersj2 2605: my %tmpHash = &Apache::lonnet::restore($self->symb());
1.51 bowersj2 2606: $self->{RETURN_HASH} = \%tmpHash;
2607: }
2608: }
2609:
2610: ######
2611: # Status queries
2612: ######
2613:
2614: # These methods query the status of problems.
2615:
2616: # If we need to count parts, this function determines the number of
2617: # parts from the metadata. When called, it returns a reference to a list
2618: # of strings corresponding to the parts. (Thus, using it in a scalar context
2619: # tells you how many parts you have in the problem:
2620: # $partcount = scalar($resource->countParts());
2621: # Don't use $self->{PARTS} directly because you don't know if it's been
2622: # computed yet.
2623:
2624: =pod
2625:
2626: =head2 Resource misc
2627:
2628: Misc. functions for the resource.
2629:
2630: =over 4
2631:
1.59 bowersj2 2632: =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.
2633:
2634: =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:
2635:
2636: for (split(/\,/, $res->getFeedback())) {
2637: my $link = &Apache::lonnet::escape($_);
2638: ...
2639:
2640: and use the link as appropriate.
2641:
2642: =cut
2643:
2644: sub hasDiscussion {
2645: my $self = shift;
2646: return $self->{NAV_MAP}->hasDiscussion($self->symb());
2647: }
2648:
2649: sub getFeedback {
2650: my $self = shift;
1.124 bowersj2 2651: my $source = $self->src();
1.125 bowersj2 2652: if ($source =~ /^\/res\//) { $source = substr $source, 5; }
1.124 bowersj2 2653: return $self->{NAV_MAP}->getFeedback($source);
2654: }
2655:
2656: sub getErrors {
2657: my $self = shift;
2658: my $source = $self->src();
2659: if ($source =~ /^\/res\//) { $source = substr $source, 5; }
2660: return $self->{NAV_MAP}->getErrors($source);
1.59 bowersj2 2661: }
2662:
2663: =pod
2664:
1.142 bowersj2 2665: =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 than 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 2666:
1.70 bowersj2 2667: =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 2668:
2669: =back
2670:
2671: =cut
2672:
2673: sub parts {
2674: my $self = shift;
2675:
1.67 bowersj2 2676: if ($self->ext) { return ['0']; }
2677:
1.51 bowersj2 2678: $self->extractParts();
2679: return $self->{PARTS};
2680: }
2681:
2682: sub countParts {
2683: my $self = shift;
2684:
2685: my $parts = $self->parts();
1.163 bowersj2 2686: my $delta = 0;
2687: for my $part (@$parts) {
2688: if ($part eq '0') { $delta--; }
2689: }
1.66 bowersj2 2690:
2691: if ($self->{RESOURCE_ERROR}) {
2692: return 0;
2693: }
2694:
1.163 bowersj2 2695: return scalar(@{$parts}) + $delta;
1.51 bowersj2 2696: }
2697:
2698: # Private function: Extracts the parts information and saves it
2699: sub extractParts {
2700: my $self = shift;
2701:
1.153 bowersj2 2702: return if (defined($self->{PARTS}));
1.67 bowersj2 2703: return if ($self->ext);
1.51 bowersj2 2704:
2705: $self->{PARTS} = [];
2706:
1.82 bowersj2 2707: # Retrieve part count, if this is a problem
2708: if ($self->is_problem()) {
1.152 matthew 2709: my $metadata = &Apache::lonnet::metadata($self->src(), 'packages');
1.82 bowersj2 2710: if (!$metadata) {
2711: $self->{RESOURCE_ERROR} = 1;
2712: $self->{PARTS} = [];
2713: return;
2714: }
2715: foreach (split(/\,/,$metadata)) {
1.152 matthew 2716: if ($_ =~ /^part_(.*)$/) {
1.147 bowersj2 2717: my $part = $1;
2718: # check to see if part is turned off.
1.149 matthew 2719: if (! Apache::loncommon::check_if_partid_hidden($part, $self->symb())) {
1.147 bowersj2 2720: push @{$self->{PARTS}}, $1;
2721: }
1.82 bowersj2 2722: }
1.51 bowersj2 2723: }
1.82 bowersj2 2724:
2725:
2726: my @sortedParts = sort @{$self->{PARTS}};
2727: $self->{PARTS} = \@sortedParts;
1.154 bowersj2 2728: }
2729:
1.51 bowersj2 2730: return;
2731: }
2732:
2733: =pod
2734:
2735: =head2 Resource Status
2736:
1.101 bowersj2 2737: Problem resources have status information, reflecting their various dates and completion statuses.
1.51 bowersj2 2738:
2739: There are two aspects to the status: the date-related information and the completion information.
2740:
2741: Idiomatic usage of these two methods would probably look something like
2742:
2743: foreach ($resource->parts()) {
2744: my $dateStatus = $resource->getDateStatus($_);
2745: my $completionStatus = $resource->getCompletionStatus($_);
2746:
1.70 bowersj2 2747: or
2748:
2749: my $status = $resource->status($_);
2750:
1.51 bowersj2 2751: ... use it here ...
2752: }
2753:
1.101 bowersj2 2754: 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.
2755:
2756: The symbolic constants shown below can be accessed through the resource object: $res->OPEN.
2757:
1.51 bowersj2 2758: =over 4
2759:
1.70 bowersj2 2760: =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 2761:
2762: =back
2763:
2764: B<Date Codes>
2765:
2766: =over 4
2767:
2768: =item * B<OPEN_LATER>: The problem will be opened later.
2769:
2770: =item * B<OPEN>: Open and not yet due.
2771:
1.59 bowersj2 2772:
1.54 bowersj2 2773: =item * B<PAST_DUE_ANSWER_LATER>: The due date has passed, but the answer date has not yet arrived.
2774:
2775: =item * B<PAST_DUE_NO_ANSWER>: The due date has passed and there is no answer opening date set.
1.51 bowersj2 2776:
2777: =item * B<ANSWER_OPEN>: The answer date is here.
2778:
2779: =item * B<NETWORK_FAILURE>: The information is unknown due to network failure.
2780:
2781: =back
2782:
2783: =cut
2784:
2785: # Apparently the compiler optimizes these into constants automatically
1.54 bowersj2 2786: sub OPEN_LATER { return 0; }
2787: sub OPEN { return 1; }
2788: sub PAST_DUE_NO_ANSWER { return 2; }
2789: sub PAST_DUE_ANSWER_LATER { return 3; }
2790: sub ANSWER_OPEN { return 4; }
2791: sub NOTHING_SET { return 5; }
2792: sub NETWORK_FAILURE { return 100; }
2793:
2794: # getDateStatus gets the date status for a given problem part.
2795: # Because answer date, due date, and open date are fully independent
2796: # (i.e., it is perfectly possible to *only* have an answer date),
2797: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
2798: # (past, future, none given). This function handles this with a decision
2799: # tree. Read the comments to follow the decision tree.
1.51 bowersj2 2800:
2801: sub getDateStatus {
2802: my $self = shift;
2803: my $part = shift;
2804: $part = "0" if (!defined($part));
1.54 bowersj2 2805:
2806: # Always return network failure if there was one.
1.51 bowersj2 2807: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
2808:
2809: my $now = time();
2810:
1.53 bowersj2 2811: my $open = $self->opendate($part);
2812: my $due = $self->duedate($part);
2813: my $answer = $self->answerdate($part);
2814:
2815: if (!$open && !$due && !$answer) {
2816: # no data on the problem at all
2817: # should this be the same as "open later"? think multipart.
2818: return $self->NOTHING_SET;
2819: }
1.59 bowersj2 2820: if (!$open || $now < $open) {return $self->OPEN_LATER}
2821: if (!$due || $now < $due) {return $self->OPEN}
2822: if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
2823: if ($answer) { return $self->ANSWER_OPEN; }
1.54 bowersj2 2824: return PAST_DUE_NO_ANSWER;
1.51 bowersj2 2825: }
2826:
2827: =pod
2828:
2829: B<>
2830:
2831: =over 4
2832:
2833: =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:
2834:
2835: =back
2836:
2837: B<Completion Codes>
2838:
2839: =over 4
2840:
2841: =item * B<NOT_ATTEMPTED>: Has not been attempted at all.
2842:
2843: =item * B<INCORRECT>: Attempted, but wrong by student.
2844:
2845: =item * B<INCORRECT_BY_OVERRIDE>: Attempted, but wrong by instructor override.
2846:
2847: =item * B<CORRECT>: Correct or correct by instructor.
2848:
2849: =item * B<CORRECT_BY_OVERRIDE>: Correct by instructor override.
2850:
2851: =item * B<EXCUSED>: Excused. Not yet implemented.
2852:
2853: =item * B<NETWORK_FAILURE>: Information not available due to network failure.
2854:
1.69 bowersj2 2855: =item * B<ATTEMPTED>: Attempted, and not yet graded.
2856:
1.51 bowersj2 2857: =back
2858:
2859: =cut
2860:
1.53 bowersj2 2861: sub NOT_ATTEMPTED { return 10; }
2862: sub INCORRECT { return 11; }
2863: sub INCORRECT_BY_OVERRIDE { return 12; }
2864: sub CORRECT { return 13; }
2865: sub CORRECT_BY_OVERRIDE { return 14; }
2866: sub EXCUSED { return 15; }
1.69 bowersj2 2867: sub ATTEMPTED { return 16; }
1.51 bowersj2 2868:
2869: sub getCompletionStatus {
2870: my $self = shift;
2871: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
2872:
1.108 bowersj2 2873: my $status = $self->queryRestoreHash('solved', shift);
1.51 bowersj2 2874:
2875: # Left as seperate if statements in case we ever do more with this
2876: if ($status eq 'correct_by_student') {return $self->CORRECT;}
2877: if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
2878: if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
2879: if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
2880: if ($status eq 'excused') {return $self->EXCUSED; }
1.69 bowersj2 2881: if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
1.51 bowersj2 2882: return $self->NOT_ATTEMPTED;
1.108 bowersj2 2883: }
2884:
2885: sub queryRestoreHash {
2886: my $self = shift;
2887: my $hashentry = shift;
2888: my $part = shift;
2889: $part = "0" if (!defined($part));
2890: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
2891:
2892: $self->getReturnHash();
2893:
2894: return $self->{RETURN_HASH}->{'resource.'.$part.'.'.$hashentry};
1.51 bowersj2 2895: }
2896:
2897: =pod
2898:
2899: B<Composite Status>
2900:
1.101 bowersj2 2901: 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 2902:
2903: =over 4
2904:
1.70 bowersj2 2905: =item * B<NETWORK_FAILURE>: The network has failed and the information is not available.
1.51 bowersj2 2906:
1.70 bowersj2 2907: =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 2908:
1.70 bowersj2 2909: =item * B<CORRECT>: For any reason at all, the part is considered correct.
1.51 bowersj2 2910:
1.70 bowersj2 2911: =item * B<EXCUSED>: For any reason at all, the problem is excused.
1.51 bowersj2 2912:
1.70 bowersj2 2913: =item * B<PAST_DUE_NO_ANSWER>: The problem is past due, not considered correct, and no answer date is set.
1.54 bowersj2 2914:
1.70 bowersj2 2915: =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 2916:
1.70 bowersj2 2917: =item * B<ANSWER_OPEN>: The problem is past due, not correct, and the answer is now available.
1.51 bowersj2 2918:
1.70 bowersj2 2919: =item * B<OPEN_LATER>: The problem is not yet open.
1.51 bowersj2 2920:
1.70 bowersj2 2921: =item * B<TRIES_LEFT>: The problem is open, has been tried, is not correct, but there are tries left.
1.51 bowersj2 2922:
1.70 bowersj2 2923: =item * B<INCORRECT>: The problem is open, and all tries have been used without getting the correct answer.
1.51 bowersj2 2924:
1.70 bowersj2 2925: =item * B<OPEN>: The item is open and not yet tried.
1.51 bowersj2 2926:
1.70 bowersj2 2927: =item * B<ATTEMPTED>: The problem has been attempted.
1.69 bowersj2 2928:
1.51 bowersj2 2929: =back
2930:
2931: =cut
2932:
2933: sub TRIES_LEFT { return 10; }
2934:
2935: sub status {
2936: my $self = shift;
2937: my $part = shift;
2938: if (!defined($part)) { $part = "0"; }
2939: my $completionStatus = $self->getCompletionStatus($part);
2940: my $dateStatus = $self->getDateStatus($part);
2941:
2942: # What we have is a two-dimensional matrix with 4 entries on one
2943: # dimension and 5 entries on the other, which we want to colorize,
1.54 bowersj2 2944: # plus network failure and "no date data at all".
1.51 bowersj2 2945:
1.53 bowersj2 2946: if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51 bowersj2 2947:
2948: # There are a few whole rows we can dispose of:
1.53 bowersj2 2949: if ($completionStatus == CORRECT ||
2950: $completionStatus == CORRECT_BY_OVERRIDE ) {
1.69 bowersj2 2951: return CORRECT;
2952: }
2953:
2954: if ($completionStatus == ATTEMPTED) {
2955: return ATTEMPTED;
1.53 bowersj2 2956: }
2957:
2958: # If it's EXCUSED, then return that no matter what
2959: if ($completionStatus == EXCUSED) {
2960: return EXCUSED;
1.51 bowersj2 2961: }
2962:
1.53 bowersj2 2963: if ($dateStatus == NOTHING_SET) {
2964: return NOTHING_SET;
1.51 bowersj2 2965: }
2966:
1.69 bowersj2 2967: # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
2968: # by 4 matrix (date statuses).
1.51 bowersj2 2969:
1.54 bowersj2 2970: if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59 bowersj2 2971: $dateStatus == PAST_DUE_NO_ANSWER ) {
1.54 bowersj2 2972: return $dateStatus;
1.51 bowersj2 2973: }
2974:
1.53 bowersj2 2975: if ($dateStatus == ANSWER_OPEN) {
2976: return ANSWER_OPEN;
1.51 bowersj2 2977: }
2978:
2979: # Now: (incorrect, incorrect_override, not_attempted) x
2980: # (open_later), (open)
2981:
1.53 bowersj2 2982: if ($dateStatus == OPEN_LATER) {
2983: return OPEN_LATER;
1.51 bowersj2 2984: }
2985:
2986: # If it's WRONG...
1.53 bowersj2 2987: if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51 bowersj2 2988: # and there are TRIES LEFT:
1.79 bowersj2 2989: if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
1.53 bowersj2 2990: return TRIES_LEFT;
1.51 bowersj2 2991: }
1.53 bowersj2 2992: return INCORRECT; # otherwise, return orange; student can't fix this
1.51 bowersj2 2993: }
2994:
2995: # Otherwise, it's untried and open
1.53 bowersj2 2996: return OPEN;
1.51 bowersj2 2997: }
2998:
2999: =pod
3000:
3001: =head2 Resource/Nav Map Navigation
3002:
3003: =over 4
3004:
1.101 bowersj2 3005: =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 3006:
1.101 bowersj2 3007: =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 3008:
3009: =cut
3010:
3011: sub getNext {
3012: my $self = shift;
3013: my @branches;
3014: my $to = $self->to();
1.85 bowersj2 3015: foreach my $branch ( split(/,/, $to) ) {
1.51 bowersj2 3016: my $choice = $self->{NAV_MAP}->getById($branch);
3017: my $next = $choice->goesto();
3018: $next = $self->{NAV_MAP}->getById($next);
3019:
1.131 bowersj2 3020: push @branches, $next;
1.85 bowersj2 3021: }
3022: return \@branches;
3023: }
3024:
3025: sub getPrevious {
3026: my $self = shift;
3027: my @branches;
3028: my $from = $self->from();
3029: foreach my $branch ( split /,/, $from) {
3030: my $choice = $self->{NAV_MAP}->getById($branch);
3031: my $prev = $choice->comesfrom();
3032: $prev = $self->{NAV_MAP}->getById($prev);
3033:
1.131 bowersj2 3034: push @branches, $prev;
1.51 bowersj2 3035: }
3036: return \@branches;
1.131 bowersj2 3037: }
3038:
3039: sub browsePriv {
3040: my $self = shift;
3041: if (defined($self->{BROWSE_PRIV})) {
3042: return $self->{BROWSE_PRIV};
3043: }
3044:
3045: $self->{BROWSE_PRIV} = &Apache::lonnet::allowed('bre', $self->src());
1.51 bowersj2 3046: }
3047:
3048: =pod
1.2 www 3049:
1.51 bowersj2 3050: =back
1.2 www 3051:
1.51 bowersj2 3052: =cut
1.2 www 3053:
1.51 bowersj2 3054: 1;
1.2 www 3055:
1.51 bowersj2 3056: __END__
1.2 www 3057:
3058:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>