Annotation of loncom/interface/lonnavmaps.pm, revision 1.198
1.2 www 1: # The LearningOnline Network with CAPA
2: # Navigate Maps Handler
1.1 www 3: #
1.197 bowersj2 4: # $Id: lonnavmaps.pm,v 1.196 2003/06/10 18:42:18 bowersj2 Exp $
1.20 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.2 www 28: # (Page Handler
1.1 www 29: #
1.2 www 30: # (TeX Content Handler
1.1 www 31: #
1.2 www 32: # 05/29/00,05/30 Gerd Kortemeyer)
33: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
34: # 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 35: #
1.17 www 36: # 3/1/1,6/1,17/1,29/1,30/1,2/8,9/21,9/24,9/25 Gerd Kortemeyer
1.21 www 37: # YEAR=2002
38: # 1/1 Gerd Kortemeyer
1.105 bowersj2 39: # Oct-Nov Jeremy Bowers
1.153 bowersj2 40: # YEAR=2003
41: # Jeremy Bowers ... lots of days
1.2 www 42:
1.1 www 43: package Apache::lonnavmaps;
44:
45: use strict;
1.2 www 46: use Apache::Constants qw(:common :http);
1.18 albertel 47: use Apache::loncommon();
1.150 www 48: use Apache::lonmenu();
1.73 bowersj2 49: use POSIX qw (floor strftime);
1.192 bowersj2 50: use Data::Dumper; # for debugging, not always used
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',
1.185 bowersj2 75: $resObj->ATTEMPTED => 'navmap.open.gif',
76: $resObj->ANSWER_SUBMITTED => '' );
1.135 bowersj2 77:
78: my %iconAltTags =
79: ( 'navmap.correct.gif' => 'Correct',
80: 'navmap.wrong.gif' => 'Incorrect',
81: 'navmap.open.gif' => 'Open' );
1.133 bowersj2 82:
1.136 bowersj2 83: # Defines a status->color mapping, null string means don't color
84: my %colormap =
1.140 bowersj2 85: ( $resObj->NETWORK_FAILURE => '',
86: $resObj->CORRECT => '',
87: $resObj->EXCUSED => '#3333FF',
88: $resObj->PAST_DUE_ANSWER_LATER => '',
89: $resObj->PAST_DUE_NO_ANSWER => '',
90: $resObj->ANSWER_OPEN => '#006600',
91: $resObj->OPEN_LATER => '',
92: $resObj->TRIES_LEFT => '',
93: $resObj->INCORRECT => '',
94: $resObj->OPEN => '',
95: $resObj->NOTHING_SET => '' );
1.136 bowersj2 96: # And a special case in the nav map; what to do when the assignment
97: # is not yet done and due in less then 24 hours
98: my $hurryUpColor = "#FF0000";
1.130 www 99:
1.1 www 100: sub handler {
1.99 bowersj2 101: my $r = shift;
1.118 bowersj2 102: real_handler($r);
103: }
104:
105: sub real_handler {
106: my $r = shift;
1.2 www 107:
1.51 bowersj2 108: # Handle header-only request
109: if ($r->header_only) {
110: if ($ENV{'browser.mathml'}) {
111: $r->content_type('text/xml');
112: } else {
113: $r->content_type('text/html');
114: }
115: $r->send_http_header;
116: return OK;
117: }
118:
119: # Send header, don't cache this page
120: if ($ENV{'browser.mathml'}) {
121: $r->content_type('text/xml');
122: } else {
123: $r->content_type('text/html');
124: }
125: &Apache::loncommon::no_cache($r);
126: $r->send_http_header;
127:
1.106 bowersj2 128: # Create the nav map
1.168 bowersj2 129: my $navmap = Apache::lonnavmaps::navmap->new(
1.51 bowersj2 130: $ENV{"request.course.fn"}.".db",
1.80 bowersj2 131: $ENV{"request.course.fn"}."_parms.db", 1, 1);
1.51 bowersj2 132:
1.55 bowersj2 133:
134: if (!defined($navmap)) {
135: my $requrl = $r->uri;
136: $ENV{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
137: return HTTP_NOT_ACCEPTABLE;
138: }
1.64 bowersj2 139:
1.111 bowersj2 140: $r->print("<html><head>\n");
1.150 www 141: $r->print("<title>Navigate Course Contents</title>");
142: # ------------------------------------------------------------ Get query string
143: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['register']);
1.158 bowersj2 144:
1.150 www 145: # ----------------------------------------------------- Force menu registration
146: my $addentries='';
147: if ($ENV{'form.register'}) {
148: $addentries=' onLoad="'.&Apache::lonmenu::loadevents().
149: '" onUnload="'.&Apache::lonmenu::unloadevents().'"';
150: $r->print(&Apache::lonmenu::registerurl(1));
151: }
1.111 bowersj2 152:
1.64 bowersj2 153: # Header
1.150 www 154: $r->print('</head>'.
155: &Apache::loncommon::bodytag('Navigate Course Contents','',
1.151 www 156: $addentries,'','',$ENV{'form.register'}));
1.64 bowersj2 157: $r->print('<script>window.focus();</script>');
1.101 bowersj2 158:
1.124 bowersj2 159: $r->rflush();
160:
161: # Now that we've displayed some stuff to the user, init the navmap
162: $navmap->init();
163:
1.95 bowersj2 164: $r->print('<br> ');
165: $r->rflush();
166:
1.55 bowersj2 167: # Check that it's defined
168: if (!($navmap->courseMapDefined())) {
169: $r->print('<font size="+2" color="red">Coursemap undefined.</font>' .
170: '</body></html>');
171: return OK;
172: }
173:
1.178 bowersj2 174: # See if there's only one map in the top-level, if we don't
175: # already have a filter... if so, automatically display it
1.179 bowersj2 176: if ($ENV{QUERY_STRING} !~ /filter/) {
1.178 bowersj2 177: my $iterator = $navmap->getIterator(undef, undef, undef, 0);
178: my $depth = 1;
179: $iterator->next();
180: my $curRes = $iterator->next();
181: my $sequenceCount = 0;
182: my $sequenceId;
183: while ($depth > 0) {
184: if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
185: if ($curRes == $iterator->END_MAP()) { $depth--; }
186:
187: if (ref($curRes) && $curRes->is_sequence()) {
188: $sequenceCount++;
189: $sequenceId = $curRes->map_pc();
1.179 bowersj2 190: }
1.178 bowersj2 191:
192: $curRes = $iterator->next();
193: }
194:
195: if ($sequenceCount == 1) {
196: # The automatic iterator creation in the render call
197: # will pick this up. We know the condition because
198: # the defined($ENV{'form.filter'}) also ensures this
199: # is a fresh call.
200: $ENV{'form.filter'} = "$sequenceId";
1.155 bowersj2 201: }
202: }
203:
1.191 bowersj2 204: my $jumpToFirstHomework = 0;
1.184 bowersj2 205: # Check to see if the student is jumping to next open, do-able problem
206: if ($ENV{QUERY_STRING} eq 'jumpToFirstHomework') {
1.191 bowersj2 207: $jumpToFirstHomework = 1;
1.184 bowersj2 208: # Find the next homework problem that they can do.
209: my $iterator = $navmap->getIterator(undef, undef, undef, 1);
210: my $depth = 1;
211: $iterator->next();
212: my $curRes = $iterator->next();
213: my $foundDoableProblem = 0;
214: my $problemRes;
215:
216: while ($depth > 0 && !$foundDoableProblem) {
217: if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
218: if ($curRes == $iterator->END_MAP()) { $depth--; }
219:
220: if (ref($curRes) && $curRes->is_problem()) {
221: my $status = $curRes->status();
1.191 bowersj2 222: if ($curRes->completable()) {
1.184 bowersj2 223: $problemRes = $curRes;
224: $foundDoableProblem = 1;
225:
226: # Pop open all previous maps
227: my $stack = $iterator->getStack();
228: pop @$stack; # last resource in the stack is the problem
229: # itself, which we don't need in the map stack
230: my @mapPcs = map {$_->map_pc()} @$stack;
231: $ENV{'form.filter'} = join(',', @mapPcs);
232:
233: # Mark as both "here" and "jump"
234: $ENV{'form.postsymb'} = $curRes->symb();
235: }
236: }
237: } continue {
238: $curRes = $iterator->next();
239: }
240:
241: # If we found no problems, print a note to that effect.
242: if (!$foundDoableProblem) {
243: $r->print("<font size='+2'>All homework assignments have been completed.</font><br /><br />");
244: }
245: } else {
246: $r->print("<a href='navmaps?jumpToFirstHomework'>" .
247: "Go To My First Homework Problem</a><br />");
248: }
249:
1.191 bowersj2 250: my $suppressEmptySequences = 0;
251: my $filterFunc = undef;
252: # Display only due homework.
253: my $showOnlyHomework = 0;
254: if ($ENV{QUERY_STRING} eq 'showOnlyHomework') {
255: $showOnlyHomework = 1;
256: $suppressEmptySequences = 1;
257: $filterFunc = sub { my $res = shift;
258: return $res->completable() || $res->is_sequence();
259: };
260: $r->print("<p><font size='+2'>Uncompleted Homework</font></p>");
261: $ENV{'form.filter'} = '';
262: $ENV{'form.condition'} = 1;
263: } else {
264: $r->print("<a href='navmaps?showOnlyHomework'>" .
265: "Show Only Uncompleted Homework</a><br />");
266: }
267:
1.133 bowersj2 268: # renderer call
1.191 bowersj2 269: my $renderArgs = { 'cols' => [0,1,2,3],
270: 'url' => '/adm/navmaps',
271: 'navmap' => $navmap,
272: 'suppressNavmap' => 1,
273: 'suppressEmptySequences' => $suppressEmptySequences,
274: 'filterFunc' => $filterFunc,
275: 'r' => $r};
276: my $render = render($renderArgs);
277: $navmap->untieHashes();
1.133 bowersj2 278:
1.191 bowersj2 279: # If no resources were printed, print a reassuring message so the
280: # user knows there was no error.
281: if ($renderArgs->{'counter'} == 0) {
282: if ($showOnlyHomework) {
283: $r->print("<p><font size='+1'>All homework is currently completed.</font></p>");
284: } else { # both jumpToFirstHomework and normal use the same: course must be empty
285: $r->print("<p><font size='+1'>This course is empty.</font></p>");
286: }
287: }
1.51 bowersj2 288:
1.128 bowersj2 289: $r->print("</body></html>");
1.134 bowersj2 290: $r->rflush();
1.59 bowersj2 291:
1.51 bowersj2 292: return OK;
293: }
294:
295: # Convenience functions: Returns a string that adds or subtracts
296: # the second argument from the first hash, appropriate for the
297: # query string that determines which folders to recurse on
298: sub addToFilter {
299: my $hashIn = shift;
300: my $addition = shift;
301: my %hash = %$hashIn;
302: $hash{$addition} = 1;
303:
304: return join (",", keys(%hash));
305: }
306:
307: sub removeFromFilter {
308: my $hashIn = shift;
309: my $subtraction = shift;
310: my %hash = %$hashIn;
311:
312: delete $hash{$subtraction};
313: return join(",", keys(%hash));
314: }
315:
316: # Convenience function: Given a stack returned from getStack on the iterator,
317: # return the correct src() value.
318: # Later, this should add an anchor when we start putting anchors in pages.
319: sub getLinkForResource {
320: my $stack = shift;
321: my $res;
322:
323: # Check to see if there are any pages in the stack
324: foreach $res (@$stack) {
325: if (defined($res) && $res->is_page()) {
326: return $res->src();
327: }
328: }
329:
330: # Failing that, return the src of the last resource that is defined
331: # (when we first recurse on a map, it puts an undefined resource
332: # on the bottom because $self->{HERE} isn't defined yet, and we
333: # want the src for the map anyhow)
334: foreach (@$stack) {
335: if (defined($_)) { $res = $_; }
336: }
337:
338: return $res->src();
339: }
340:
1.53 bowersj2 341: # Convenience function: This seperates the logic of how to create
342: # the problem text strings ("Due: DATE", "Open: DATE", "Not yet assigned",
343: # etc.) into a seperate function. It takes a resource object as the
344: # first parameter, and the part number of the resource as the second.
345: # It's basically a big switch statement on the status of the resource.
346:
347: sub getDescription {
348: my $res = shift;
349: my $part = shift;
1.69 bowersj2 350: my $status = $res->status($part);
1.53 bowersj2 351:
1.182 bowersj2 352: if ($status == $res->NETWORK_FAILURE) {
353: return "Having technical difficulties; please check status later";
354: }
1.53 bowersj2 355: if ($status == $res->NOTHING_SET) {
356: return "Not currently assigned.";
357: }
358: if ($status == $res->OPEN_LATER) {
1.73 bowersj2 359: return "Open " . timeToHumanString($res->opendate($part));
1.53 bowersj2 360: }
361: if ($status == $res->OPEN) {
1.79 bowersj2 362: if ($res->duedate($part)) {
1.73 bowersj2 363: return "Due " . timeToHumanString($res->duedate($part));
1.66 bowersj2 364: } else {
365: return "Open, no due date";
366: }
1.53 bowersj2 367: }
1.54 bowersj2 368: if ($status == $res->PAST_DUE_ANSWER_LATER) {
1.73 bowersj2 369: return "Answer open " . timeToHumanString($res->answerdate($part));
1.54 bowersj2 370: }
371: if ($status == $res->PAST_DUE_NO_ANSWER) {
1.73 bowersj2 372: return "Was due " . timeToHumanString($res->duedate($part));
1.53 bowersj2 373: }
374: if ($status == $res->ANSWER_OPEN) {
375: return "Answer available";
376: }
1.59 bowersj2 377: if ($status == $res->EXCUSED) {
1.66 bowersj2 378: return "Excused by instructor";
1.59 bowersj2 379: }
1.69 bowersj2 380: if ($status == $res->ATTEMPTED) {
381: return "Not yet graded.";
382: }
1.59 bowersj2 383: if ($status == $res->TRIES_LEFT) {
1.79 bowersj2 384: my $tries = $res->tries($part);
385: my $maxtries = $res->maxtries($part);
386: my $triesString = "";
387: if ($tries && $maxtries) {
388: $triesString = "<font size=\"-1\"><i>($tries of $maxtries tries used)</i></font>";
389: if ($maxtries > 1 && $maxtries - $tries == 1) {
390: $triesString = "<b>$triesString</b>";
391: }
392: }
1.66 bowersj2 393: if ($res->duedate()) {
1.73 bowersj2 394: return "Due " . timeToHumanString($res->duedate($part)) .
1.66 bowersj2 395: " $triesString";
396: } else {
397: return "No due date $triesString";
398: }
1.59 bowersj2 399: }
1.185 bowersj2 400: if ($status == $res->ANSWER_SUBMITTED) {
401: return 'Answer submitted';
402: }
1.53 bowersj2 403: }
404:
1.115 bowersj2 405: # Convenience function, so others can use it: Is the problem due in less then
406: # 24 hours, and still can be done?
407:
408: sub dueInLessThen24Hours {
409: my $res = shift;
410: my $part = shift;
411: my $status = $res->status($part);
412:
413: return ($status == $res->OPEN() || $status == $res->ATTEMPTED() ||
414: $status == $res->TRIES_LEFT()) &&
415: $res->duedate() && $res->duedate() < time()+(24*60*60) &&
416: $res->duedate() > time();
417: }
418:
419: # Convenience function, so others can use it: Is there only one try remaining for the
420: # part, with more then one try to begin with, not due yet and still can be done?
421: sub lastTry {
422: my $res = shift;
423: my $part = shift;
424:
425: my $tries = $res->tries($part);
426: my $maxtries = $res->maxtries($part);
427: return $tries && $maxtries && $maxtries > 1 &&
428: $maxtries - $tries == 1 && $res->duedate() &&
429: $res->duedate() > time();
430: }
431:
1.101 bowersj2 432: # This puts a human-readable name on the ENV variable.
1.177 www 433:
1.68 bowersj2 434: sub advancedUser {
1.177 www 435: return $ENV{'request.role.adv'};
1.68 bowersj2 436: }
437:
1.73 bowersj2 438:
439: # timeToHumanString takes a time number and converts it to a
440: # human-readable representation, meant to be used in the following
441: # manner:
442: # print "Due $timestring"
443: # print "Open $timestring"
444: # print "Answer available $timestring"
445: # Very, very, very, VERY English-only... goodness help a localizer on
446: # this func...
1.53 bowersj2 447: sub timeToHumanString {
1.58 albertel 448: my ($time) = @_;
449: # zero, '0' and blank are bad times
1.73 bowersj2 450: if (!$time) {
451: return 'never';
452: }
453:
454: my $now = time();
455:
456: my @time = localtime($time);
457: my @now = localtime($now);
458:
459: # Positive = future
460: my $delta = $time - $now;
461:
462: my $minute = 60;
463: my $hour = 60 * $minute;
464: my $day = 24 * $hour;
465: my $week = 7 * $day;
466: my $inPast = 0;
467:
468: # Logic in comments:
469: # Is it now? (extremely unlikely)
470: if ( $delta == 0 ) {
471: return "this instant";
472: }
473:
474: if ($delta < 0) {
475: $inPast = 1;
476: $delta = -$delta;
477: }
478:
479: if ( $delta > 0 ) {
1.101 bowersj2 480:
1.73 bowersj2 481: my $tense = $inPast ? " ago" : "";
482: my $prefix = $inPast ? "" : "in ";
1.101 bowersj2 483:
484: # Less then a minute
1.73 bowersj2 485: if ( $delta < $minute ) {
486: if ($delta == 1) { return "${prefix}1 second$tense"; }
487: return "$prefix$delta seconds$tense";
488: }
489:
1.101 bowersj2 490: # Less then an hour
1.73 bowersj2 491: if ( $delta < $hour ) {
492: # If so, use minutes
493: my $minutes = floor($delta / 60);
494: if ($minutes == 1) { return "${prefix}1 minute$tense"; }
495: return "$prefix$minutes minutes$tense";
496: }
497:
498: # Is it less then 24 hours away? If so,
499: # display hours + minutes
500: if ( $delta < $hour * 24) {
501: my $hours = floor($delta / $hour);
502: my $minutes = floor(($delta % $hour) / $minute);
503: my $hourString = "$hours hours";
504: my $minuteString = ", $minutes minutes";
505: if ($hours == 1) {
506: $hourString = "1 hour";
507: }
508: if ($minutes == 1) {
509: $minuteString = ", 1 minute";
510: }
511: if ($minutes == 0) {
512: $minuteString = "";
513: }
514: return "$prefix$hourString$minuteString$tense";
515: }
516:
517: # Less then 5 days away, display day of the week and
518: # HH:MM
519: if ( $delta < $day * 5 ) {
1.85 bowersj2 520: my $timeStr = strftime("%A, %b %e at %I:%M %P", localtime($time));
1.73 bowersj2 521: $timeStr =~ s/12:00 am/midnight/;
522: $timeStr =~ s/12:00 pm/noon/;
523: return ($inPast ? "last " : "next ") .
524: $timeStr;
525: }
526:
527: # Is it this year?
528: if ( $time[5] == $now[5]) {
529: # Return on Month Day, HH:MM meridian
530: my $timeStr = strftime("on %A, %b %e at %I:%M %P", localtime($time));
531: $timeStr =~ s/12:00 am/midnight/;
532: $timeStr =~ s/12:00 pm/noon/;
533: return $timeStr;
534: }
535:
536: # Not this year, so show the year
537: my $timeStr = strftime("on %A, %b %e %G at %I:%M %P", localtime($time));
538: $timeStr =~ s/12:00 am/midnight/;
539: $timeStr =~ s/12:00 pm/noon/;
540: return $timeStr;
1.58 albertel 541: }
1.53 bowersj2 542: }
543:
1.51 bowersj2 544:
1.133 bowersj2 545: =pod
546:
1.174 albertel 547: =head1 NAME
548:
549: Apache::lonnavmap - Subroutines to handle and render the navigation maps
550:
551: =head1 SYNOPSIS
552:
553: The main handler generates the navigational listing for the course,
554: the other objects export this information in a usable fashion for
555: other modules
1.133 bowersj2 556:
1.174 albertel 557: =head1 Object: render
1.133 bowersj2 558:
1.174 albertel 559: The navmap renderer package provides a sophisticated rendering of the
560: standard navigation maps interface into HTML. The provided nav map
561: handler is actually just a glorified call to this.
1.133 bowersj2 562:
1.174 albertel 563: Because of the large number of parameters this function presents,
564: instead of passing it arguments as is normal, pass it in an anonymous
565: hash with the given options. This is because there is no obvious order
566: you may wish to override these in and a hash is easier to read and
567: understand then "undef, undef, undef, 1, undef, undef, renderButton,
568: undef, 0" when you mostly want default behaviors.
569:
570: The package provides a function called 'render', called as
571: Apache::lonnavmaps::renderer->render({}).
1.51 bowersj2 572:
1.133 bowersj2 573: =head2 Overview of Columns
1.51 bowersj2 574:
1.174 albertel 575: The renderer will build an HTML table for the navmap and return
576: it. The table is consists of several columns, and a row for each
577: resource (or possibly each part). You tell the renderer how many
578: columns to create and what to place in each column, optionally using
579: one or more of the preparent columns, and the renderer will assemble
580: the table.
581:
582: Any additional generally useful column types should be placed in the
583: renderer code here, so anybody can use it anywhere else. Any code
584: specific to the current application (such as the addition of <input>
585: elements in a column) should be placed in the code of the thing using
586: the renderer.
587:
588: At the core of the renderer is the array reference COLS (see Example
589: section below for how to pass this correctly). The COLS array will
590: consist of entries of one of two types of things: Either an integer
591: representing one of the pre-packaged column types, or a sub reference
592: that takes a resource reference, a part number, and a reference to the
593: argument hash passed to the renderer, and returns a string that will
594: be inserted into the HTML representation as it.
595:
596: The pre-packaged column names are refered to by constants in the
597: Apache::lonnavmaps::renderer namespace. The following currently exist:
1.51 bowersj2 598:
1.174 albertel 599: =over 4
1.51 bowersj2 600:
1.174 albertel 601: =item * B<resource>:
1.51 bowersj2 602:
1.174 albertel 603: The general info about the resource: Link, icon for the type, etc. The
604: first column in the standard nav map display. This column also accepts
605: the following parameter in the renderer hash:
1.51 bowersj2 606:
1.133 bowersj2 607: =over 4
1.51 bowersj2 608:
1.174 albertel 609: =item * B<resource_nolink>:
1.51 bowersj2 610:
1.174 albertel 611: If true, the resource will not be linked. Default: false, resource
612: will have links.
613:
614: =item * B<resource_part_count>:
1.51 bowersj2 615:
1.174 albertel 616: If true (default), the resource will show a part count if the full
617: part list is not displayed. If false, the resource will never show a
618: part count.
1.133 bowersj2 619:
1.174 albertel 620: =item * B<resource_no_folder_link>:
1.133 bowersj2 621:
1.174 albertel 622: If true, the resource's folder will not be clickable to open or close
623: it. Default is false. True implies printCloseAll is false, since you
624: can't close or open folders when this is on anyhow.
1.144 bowersj2 625:
1.133 bowersj2 626: =back
1.51 bowersj2 627:
1.174 albertel 628: =item B<communication_status>:
629:
630: Whether there is discussion on the resource, email for the user, or
631: (lumped in here) perl errors in the execution of the problem. This is
632: the second column in the main nav map.
633:
634: =item B<quick_status>:
1.51 bowersj2 635:
1.174 albertel 636: An icon for the status of a problem, with four possible states:
637: Correct, incorrect, open, or none (not open yet, not a problem). The
638: third column of the standard navmap.
1.51 bowersj2 639:
1.174 albertel 640: =item B<long_status>:
641:
642: A text readout of the details of the current status of the problem,
643: such as "Due in 22 hours". The fourth column of the standard navmap.
1.51 bowersj2 644:
1.133 bowersj2 645: =back
1.51 bowersj2 646:
1.133 bowersj2 647: If you add any others please be sure to document them here.
1.51 bowersj2 648:
1.174 albertel 649: An example of a column renderer that will show the ID number of a
650: resource, along with the part name if any:
1.51 bowersj2 651:
1.133 bowersj2 652: sub {
653: my ($resource, $part, $params) = @_;
654: if ($part) { return '<td>' . $resource->{ID} . ' ' . $part . '</td>'; }
655: return '<td>' . $resource->{ID} . '</td>';
656: }
1.51 bowersj2 657:
1.174 albertel 658: Note these functions are responsible for the TD tags, which allow them
659: to override vertical and horizontal alignment, etc.
1.130 www 660:
1.133 bowersj2 661: =head2 Parameters
1.115 bowersj2 662:
1.174 albertel 663: Most of these parameters are only useful if you are *not* using the
664: folder interface (i.e., the default first column), which is probably
665: the common case. If you are using this interface, then you should be
666: able to get away with just using 'cols' (to specify the columns
667: shown), 'url' (necessary for the folders to link to the current screen
668: correctly), and possibly 'queryString' if your app calls for it. In
669: that case, maintaining the state of the folders will be done
670: automatically.
1.140 bowersj2 671:
1.133 bowersj2 672: =over 4
1.51 bowersj2 673:
1.174 albertel 674: =item * B<iterator>:
675:
676: A reference to a fresh ::iterator to use from the navmaps. The
677: rendering will reflect the options passed to the iterator, so you can
678: use that to just render a certain part of the course, if you like. If
679: one is not passed, the renderer will attempt to construct one from
680: ENV{'form.filter'} and ENV{'form.condition'} information, plus the
681: 'iterator_map' parameter if any.
682:
683: =item * B<iterator_map>:
684:
685: If you are letting the renderer do the iterator handling, you can
686: instruct the renderer to render only a particular map by passing it
687: the source of the map you want to process, like
688: '/res/103/jerf/navmap.course.sequence'.
689:
690: =item * B<navmap>:
691:
692: A reference to a navmap, used only if an iterator is not passed in. If
693: this is necessary to make an iterator but it is not passed in, a new
694: one will be constructed based on ENV info. This is useful to do basic
695: error checking before passing it off to render.
696:
697: =item * B<r>:
698:
699: The standard Apache response object. This must be passed to the
700: renderer or the course hash will be locked.
701:
702: =item * B<cols>:
703:
704: An array reference
705:
706: =item * B<showParts>:
707:
708: A flag. If yes (default), a line for the resource itself, and a line
709: for each part will be displayed. If not, only one line for each
710: resource will be displayed.
711:
712: =item * B<condenseParts>:
713:
714: A flag. If yes (default), if all parts of the problem have the same
715: status and that status is Nothing Set, Correct, or Network Failure,
716: then only one line will be displayed for that resource anyhow. If no,
717: all parts will always be displayed. If showParts is 0, this is
718: ignored.
719:
720: =item * B<jumpCount>:
721:
722: A string identifying the URL to place the anchor 'curloc' at. Default
723: to no anchor at all. It is the responsibility of the renderer user to
724: ensure that the #curloc is in the URL. By default, determined through
725: the use of the ENV{} 'jump' information, and should normally "just
726: work" correctly.
1.144 bowersj2 727:
1.174 albertel 728: =item * B<here>:
1.140 bowersj2 729:
1.174 albertel 730: A Symb identifying where to place the 'here' marker. Default empty,
731: which means no marker.
1.92 bowersj2 732:
1.174 albertel 733: =item * B<indentString>:
1.164 bowersj2 734:
1.174 albertel 735: A string identifying the indentation string to use. By default, this
736: is a 25 pixel whitespace image with no alt text.
1.92 bowersj2 737:
1.174 albertel 738: =item * B<queryString>:
1.51 bowersj2 739:
1.174 albertel 740: A string which will be prepended to the query string used when the
741: folders are opened or closed.
1.55 bowersj2 742:
1.174 albertel 743: =item * B<url>:
1.84 bowersj2 744:
1.174 albertel 745: The url the folders will link to, which should be the current
746: page. Required if the resource info column is shown.
1.115 bowersj2 747:
1.174 albertel 748: =item * B<currentJumpIndex>:
1.55 bowersj2 749:
1.174 albertel 750: Describes the currently-open row number to cause the browser to jump
751: to, because the user just opened that folder. By default, pulled from
752: the Jump information in the ENV{'form.*'}.
1.51 bowersj2 753:
1.174 albertel 754: =item * B<printKey>:
1.51 bowersj2 755:
1.174 albertel 756: If true, print the key that appears on the top of the standard
757: navmaps. Default is false.
1.139 bowersj2 758:
1.174 albertel 759: =item * B<printCloseAll>:
1.140 bowersj2 760:
1.174 albertel 761: If true, print the "Close all folders" or "open all folders"
762: links. Default is true.
1.140 bowersj2 763:
1.174 albertel 764: =item * B<filterFunc>:
1.153 bowersj2 765:
1.174 albertel 766: A function that takes the resource object as its only parameter and
767: returns a true or false value. If true, the resource is displayed. If
768: false, it is simply skipped in the display. By default, all resources
769: are shown.
770:
1.190 bowersj2 771: =item * B<suppressEmptySequences>:
772:
773: If you're using a filter function, and displaying sequences to orient
774: the user, then frequently some sequences will be empty. Setting this to
775: true will cause those sequences not to display, so as not to confuse the
776: user into thinking that if the sequence is there there should be things
777: under it.
778:
1.174 albertel 779: =item * B<suppressNavmaps>:
780:
781: If true, will not display Navigate Content resources. Default to
782: false.
1.143 bowersj2 783:
1.133 bowersj2 784: =back
1.51 bowersj2 785:
1.133 bowersj2 786: =head2 Additional Info
1.51 bowersj2 787:
1.174 albertel 788: In addition to the parameters you can pass to the renderer, which will
789: be passed through unchange to the column renderers, the renderer will
790: generate the following information which your renderer may find
791: useful:
792:
793: If you want to know how many rows were printed, the 'counter' element
794: of the hash passed into the render function will contain the
795: count. You may want to check whether any resources were printed at
796: all.
1.145 bowersj2 797:
1.133 bowersj2 798: =over 4
1.59 bowersj2 799:
1.133 bowersj2 800: =back
1.59 bowersj2 801:
1.133 bowersj2 802: =cut
1.59 bowersj2 803:
1.133 bowersj2 804: sub resource { return 0; }
805: sub communication_status { return 1; }
806: sub quick_status { return 2; }
807: sub long_status { return 3; }
1.124 bowersj2 808:
1.133 bowersj2 809: # Data for render_resource
1.51 bowersj2 810:
1.133 bowersj2 811: sub render_resource {
812: my ($resource, $part, $params) = @_;
1.51 bowersj2 813:
1.133 bowersj2 814: my $nonLinkedText = ''; # stuff after resource title not in link
1.51 bowersj2 815:
1.134 bowersj2 816: my $link = $params->{"resourceLink"};
817: my $src = $resource->src();
818: my $it = $params->{"iterator"};
1.133 bowersj2 819: my $filter = $it->{FILTER};
820:
821: my $title = $resource->compTitle();
822: if ($src =~ /^\/uploaded\//) {
823: $nonLinkedText=$title;
824: $title = '';
825: }
826: my $partLabel = "";
827: my $newBranchText = "";
828:
829: # If this is a new branch, label it so
830: if ($params->{'isNewBranch'}) {
831: $newBranchText = "<img src='/adm/lonIcons/branch.gif' border='0' />";
1.51 bowersj2 832: }
833:
1.133 bowersj2 834: # links to open and close the folder
835: my $linkopen = "<a href='$link'>";
836: my $linkclose = "</a>";
1.51 bowersj2 837:
1.133 bowersj2 838: # Default icon: HTML page
839: my $icon = "<img src='/adm/lonIcons/html.gif' alt='' border='0' />";
840:
841: if ($resource->is_problem()) {
1.192 bowersj2 842: if ($part eq '0' || $params->{'condensed'}) {
1.133 bowersj2 843: $icon = '<img src="/adm/lonIcons/problem.gif" alt="" border="0" />';
844: } else {
845: $icon = $params->{'indentString'};
846: }
847: }
1.51 bowersj2 848:
1.133 bowersj2 849: # Display the correct map icon to open or shut map
850: if ($resource->is_map()) {
851: my $mapId = $resource->map_pc();
852: my $nowOpen = !defined($filter->{$mapId});
853: if ($it->{CONDITION}) {
854: $nowOpen = !$nowOpen;
855: }
1.144 bowersj2 856:
857: if (!$params->{'resource_no_folder_link'}) {
858: $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') . '.gif';
859: $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
860:
861: $linkopen = "<a href='" . $params->{'url'} . '?' .
862: $params->{'queryString'} . '&filter=';
863: $linkopen .= ($nowOpen xor $it->{CONDITION}) ?
864: addToFilter($filter, $mapId) :
865: removeFromFilter($filter, $mapId);
866: $linkopen .= "&condition=" . $it->{CONDITION} . '&hereType='
867: . $params->{'hereType'} . '&here=' .
868: &Apache::lonnet::escape($params->{'here'}) .
1.159 bowersj2 869: '&jump=' .
1.144 bowersj2 870: &Apache::lonnet::escape($resource->symb()) .
871: "&folderManip=1'>";
872: } else {
873: # Don't allow users to manipulate folder
874: $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') .
875: '.nomanip.gif';
876: $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
877:
878: $linkopen = "";
879: $linkclose = "";
880: }
1.133 bowersj2 881: }
1.51 bowersj2 882:
1.133 bowersj2 883: if ($resource->randomout()) {
884: $nonLinkedText .= ' <i>(hidden)</i> ';
885: }
886:
887: # We're done preparing and finally ready to start the rendering
888: my $result = "<td align='left' valign='center'>";
1.140 bowersj2 889:
890: my $indentLevel = $params->{'indentLevel'};
891: if ($newBranchText) { $indentLevel--; }
892:
1.133 bowersj2 893: # print indentation
1.140 bowersj2 894: for (my $i = 0; $i < $indentLevel; $i++) {
1.133 bowersj2 895: $result .= $params->{'indentString'};
1.84 bowersj2 896: }
897:
1.133 bowersj2 898: # Decide what to display
899: $result .= "$newBranchText$linkopen$icon$linkclose";
900:
901: my $curMarkerBegin = '';
902: my $curMarkerEnd = '';
1.51 bowersj2 903:
1.133 bowersj2 904: # Is this the current resource?
1.158 bowersj2 905: if (!$params->{'displayedHereMarker'} &&
906: $resource->symb() eq $params->{'here'} ) {
1.133 bowersj2 907: $curMarkerBegin = '<font color="red" size="+2">> </font>';
908: $curMarkerEnd = '<font color="red" size="+2"><</font>';
1.158 bowersj2 909: $params->{'displayedHereMarker'} = 1;
1.51 bowersj2 910: }
911:
1.192 bowersj2 912: if ($resource->is_problem() && $part ne '0' &&
1.133 bowersj2 913: !$params->{'condensed'}) {
914: $partLabel = " (Part $part)";
915: $title = "";
1.51 bowersj2 916: }
917:
1.163 bowersj2 918: if ($params->{'condensed'} && $resource->countParts() > 1) {
1.133 bowersj2 919: $nonLinkedText .= ' (' . $resource->countParts() . ' parts)';
1.51 bowersj2 920: }
921:
1.144 bowersj2 922: if (!$params->{'resource_nolink'}) {
923: $result .= " $curMarkerBegin<a href='$link'>$title$partLabel</a>$curMarkerEnd $nonLinkedText</td>";
924: } else {
925: $result .= " $curMarkerBegin$title$partLabel$curMarkerEnd $nonLinkedText</td>";
926: }
1.51 bowersj2 927:
1.133 bowersj2 928: return $result;
929: }
1.51 bowersj2 930:
1.133 bowersj2 931: sub render_communication_status {
932: my ($resource, $part, $params) = @_;
1.134 bowersj2 933: my $discussionHTML = ""; my $feedbackHTML = ""; my $errorHTML = "";
934:
935: my $link = $params->{"resourceLink"};
936: my $linkopen = "<a href='$link'>";
937: my $linkclose = "</a>";
938:
939: if ($resource->hasDiscussion()) {
940: $discussionHTML = $linkopen .
941: '<img border="0" src="/adm/lonMisc/chat.gif" />' .
942: $linkclose;
943: }
944:
945: if ($resource->getFeedback()) {
946: my $feedback = $resource->getFeedback();
947: foreach (split(/\,/, $feedback)) {
948: if ($_) {
949: $feedbackHTML .= ' <a href="/adm/email?display='
950: . &Apache::lonnet::escape($_) . '">'
951: . '<img src="/adm/lonMisc/feedback.gif" '
952: . 'border="0" /></a>';
953: }
954: }
955: }
956:
957: if ($resource->getErrors()) {
958: my $errors = $resource->getErrors();
959: foreach (split(/,/, $errors)) {
960: if ($_) {
961: $errorHTML .= ' <a href="/adm/email?display='
962: . &Apache::lonnet::escape($_) . '">'
963: . '<img src="/adm/lonMisc/bomb.gif" '
964: . 'border="0" /></a>';
965: }
966: }
1.197 bowersj2 967: }
968:
969: if ($params->{'multipart'} && $part != '0') {
970: $discussionHTML = $feedbackHTML = $errorHTML = '';
1.134 bowersj2 971: }
972:
973: return "<td width=\"75\" align=\"left\" valign=\"center\">$discussionHTML$feedbackHTML$errorHTML </td>";
974:
1.133 bowersj2 975: }
976: sub render_quick_status {
977: my ($resource, $part, $params) = @_;
1.135 bowersj2 978: my $result = "";
979: my $firstDisplayed = !$params->{'condensed'} &&
980: $params->{'multipart'} && $part eq "0";
981:
982: my $link = $params->{"resourceLink"};
983: my $linkopen = "<a href='$link'>";
984: my $linkclose = "</a>";
985:
986: if ($resource->is_problem() &&
987: !$firstDisplayed) {
988: my $icon = $statusIconMap{$resource->status($part)};
989: my $alt = $iconAltTags{$icon};
990: if ($icon) {
991: $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";
992: } else {
993: $result .= "<td width='30'> </td>\n";
994: }
995: } else { # not problem, no icon
996: $result .= "<td width='30'> </td>\n";
997: }
998:
999: return $result;
1.133 bowersj2 1000: }
1001: sub render_long_status {
1002: my ($resource, $part, $params) = @_;
1.136 bowersj2 1003: my $result = "<td align='right' valign='center'>\n";
1004: my $firstDisplayed = !$params->{'condensed'} &&
1005: $params->{'multipart'} && $part eq "0";
1006:
1007: my $color;
1.192 bowersj2 1008: if ($resource->is_problem() && ($resource->countParts() <= 1 || $part ne '') ) {
1.136 bowersj2 1009: $color = $colormap{$resource->status};
1010:
1011: if (dueInLessThen24Hours($resource, $part) ||
1012: lastTry($resource, $part)) {
1013: $color = $hurryUpColor;
1014: }
1015: }
1016:
1017: if ($resource->kind() eq "res" &&
1018: $resource->is_problem() &&
1019: !$firstDisplayed) {
1020: if ($color) {$result .= "<font color=\"$color\"><b>"; }
1021: $result .= getDescription($resource, $part);
1022: if ($color) {$result .= "</b></font>"; }
1023: }
1024: if ($resource->is_map() && advancedUser() && $resource->randompick()) {
1025: $result .= '(randomly select ' . $resource->randompick() .')';
1026: }
1027:
1028: return $result;
1.51 bowersj2 1029: }
1030:
1.133 bowersj2 1031: my @preparedColumns = (\&render_resource, \&render_communication_status,
1032: \&render_quick_status, \&render_long_status);
1.132 bowersj2 1033:
1034: sub setDefault {
1035: my ($val, $default) = @_;
1036: if (!defined($val)) { return $default; }
1037: return $val;
1038: }
1039:
1040: sub render {
1041: my $args = shift;
1.140 bowersj2 1042: &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
1043: my $result = '';
1044:
1.132 bowersj2 1045: # Configure the renderer.
1046: my $cols = $args->{'cols'};
1047: if (!defined($cols)) {
1048: # no columns, no nav maps.
1049: return '';
1050: }
1.140 bowersj2 1051: my $mustCloseNavMap = 0;
1052: my $navmap;
1053: if (defined($args->{'navmap'})) {
1054: $navmap = $args->{'navmap'};
1055: }
1056:
1.158 bowersj2 1057: my $r = $args->{'r'};
1.140 bowersj2 1058: my $queryString = $args->{'queryString'};
1.159 bowersj2 1059: my $jump = $args->{'jump'};
1.158 bowersj2 1060: my $here = $args->{'here'};
1.153 bowersj2 1061: my $suppressNavmap = setDefault($args->{'suppressNavmap'}, 0);
1.140 bowersj2 1062: my $currentJumpDelta = 2; # change this to change how many resources are displayed
1063: # before the current resource when using #current
1064:
1065: # If we were passed 'here' information, we are not rendering
1066: # after a folder manipulation, and we were not passed an
1067: # iterator, make sure we open the folders to show the "here"
1068: # marker
1069: my $filterHash = {};
1070: # Figure out what we're not displaying
1071: foreach (split(/\,/, $ENV{"form.filter"})) {
1072: if ($_) {
1073: $filterHash->{$_} = "1";
1074: }
1075: }
1076:
1.191 bowersj2 1077: # Filter: Remember filter function and add our own filter: Refuse
1078: # to show hidden resources unless the user can see them.
1079: my $userCanSeeHidden = advancedUser();
1080: my $filterFunc = setDefault($args->{'filterFunc'},
1081: sub {return 1;});
1082: if (!$userCanSeeHidden) {
1083: # Without renaming the filterfunc, the server seems to go into
1084: # an infinite loop
1085: my $oldFilterFunc = $filterFunc;
1086: $filterFunc = sub { my $res = shift; return !$res->randomout() &&
1087: &$oldFilterFunc($res);};
1088: }
1089:
1.140 bowersj2 1090: my $condition = 0;
1091: if ($ENV{'form.condition'}) {
1092: $condition = 1;
1093: }
1094:
1095: if (!$ENV{'form.folderManip'} && !defined($args->{'iterator'})) {
1096: # Step 1: Check to see if we have a navmap
1097: if (!defined($navmap)) {
1.171 bowersj2 1098: $navmap = Apache::lonnavmaps::navmap->new(
1.140 bowersj2 1099: $ENV{"request.course.fn"}.".db",
1100: $ENV{"request.course.fn"}."_parms.db", 1, 1);
1101: $mustCloseNavMap = 1;
1102: }
1103: $navmap->init();
1104:
1105: # Step two: Locate what kind of here marker is necessary
1106: # Determine where the "here" marker is and where the screen jumps to.
1107:
1.175 bowersj2 1108: if ($ENV{'form.postsymb'}) {
1109: $here = $jump = $ENV{'form.postsymb'};
1.140 bowersj2 1110: } elsif ($ENV{'form.postdata'}) {
1111: # couldn't find a symb, is there a URL?
1112: my $currenturl = $ENV{'form.postdata'};
1.158 bowersj2 1113: #$currenturl=~s/^http\:\/\///;
1114: #$currenturl=~s/^[^\/]+//;
1.140 bowersj2 1115:
1.158 bowersj2 1116: $here = $jump = &Apache::lonnet::symbread($currenturl);
1.140 bowersj2 1117: }
1.158 bowersj2 1118:
1.140 bowersj2 1119: # Step three: Ensure the folders are open
1120: my $mapIterator = $navmap->getIterator(undef, undef, undef, 1);
1121: my $depth = 1;
1122: $mapIterator->next(); # discard the first BEGIN_MAP
1123: my $curRes = $mapIterator->next();
1124: my $found = 0;
1125:
1126: # We only need to do this if we need to open the maps to show the
1127: # current position. This will change the counter so we can't count
1128: # for the jump marker with this loop.
1129: while ($depth > 0 && !$found) {
1130: if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
1131: if ($curRes == $mapIterator->END_MAP()) { $depth--; }
1132:
1.158 bowersj2 1133: if (ref($curRes) && $curRes->symb() eq $here) {
1.140 bowersj2 1134: my $mapStack = $mapIterator->getStack();
1135:
1136: # Ensure the parent maps are open
1137: for my $map (@{$mapStack}) {
1138: if ($condition) {
1139: undef $filterHash->{$map->map_pc()};
1140: } else {
1141: $filterHash->{$map->map_pc()} = 1;
1142: }
1143: }
1144: $found = 1;
1145: }
1146:
1147: $curRes = $mapIterator->next();
1148: }
1.159 bowersj2 1149: }
1.140 bowersj2 1150:
1151: if ( !defined($args->{'iterator'}) && $ENV{'form.folderManip'} ) { # we came from a user's manipulation of the nav page
1152: # If this is a click on a folder or something, we want to preserve the "here"
1153: # from the querystring, and get the new "jump" marker
1154: $here = $ENV{'form.here'};
1155: $jump = $ENV{'form.jump'};
1156: }
1157:
1.132 bowersj2 1158: my $it = $args->{'iterator'};
1159: if (!defined($it)) {
1.140 bowersj2 1160: # Construct a default iterator based on $ENV{'form.'} information
1161:
1162: # Step 1: Check to see if we have a navmap
1163: if (!defined($navmap)) {
1.164 bowersj2 1164: $navmap = Apache::lonnavmaps::navmap->new($r,
1.140 bowersj2 1165: $ENV{"request.course.fn"}.".db",
1166: $ENV{"request.course.fn"}."_parms.db", 1, 1);
1167: $mustCloseNavMap = 1;
1168: }
1169: # Paranoia: Make sure it's ready
1170: $navmap->init();
1171:
1.144 bowersj2 1172: # See if we're being passed a specific map
1173: if ($args->{'iterator_map'}) {
1174: my $map = $args->{'iterator_map'};
1.145 bowersj2 1175: $map = $navmap->getResourceByUrl($map);
1.144 bowersj2 1176: my $firstResource = $map->map_start();
1177: my $finishResource = $map->map_finish();
1178:
1179: $args->{'iterator'} = $it = $navmap->getIterator($firstResource, $finishResource, $filterHash, $condition);
1180: } else {
1181: $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition);
1182: }
1.132 bowersj2 1183: }
1184:
1.159 bowersj2 1185: # (re-)Locate the jump point, if any
1.191 bowersj2 1186: # Note this does not take filtering or hidden into account... need
1187: # to be fixed?
1.159 bowersj2 1188: my $mapIterator = $navmap->getIterator(undef, undef, $filterHash, 0);
1189: my $depth = 1;
1190: $mapIterator->next();
1191: my $curRes = $mapIterator->next();
1192: my $foundJump = 0;
1193: my $counter = 0;
1194:
1195: while ($depth > 0 && !$foundJump) {
1196: if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
1197: if ($curRes == $mapIterator->END_MAP()) { $depth--; }
1198: if (ref($curRes)) { $counter++; }
1199:
1200: if (ref($curRes) && $jump eq $curRes->symb()) {
1201:
1202: # This is why we have to use the main iterator instead of the
1203: # potentially faster DFS: The count has to be the same, so
1204: # the order has to be the same, which DFS won't give us.
1205: $args->{'currentJumpIndex'} = $counter;
1206: $foundJump = 1;
1207: }
1208:
1209: $curRes = $mapIterator->next();
1210: }
1211:
1.132 bowersj2 1212: my $showParts = setDefault($args->{'showParts'}, 1);
1213: my $condenseParts = setDefault($args->{'condenseParts'}, 1);
1.139 bowersj2 1214: # keeps track of when the current resource is found,
1215: # so we can back up a few and put the anchor above the
1216: # current resource
1.140 bowersj2 1217: my $printKey = $args->{'printKey'};
1218: my $printCloseAll = $args->{'printCloseAll'};
1219: if (!defined($printCloseAll)) { $printCloseAll = 1; }
1.144 bowersj2 1220:
1.140 bowersj2 1221: # Print key?
1222: if ($printKey) {
1223: $result .= '<table border="0" cellpadding="2" cellspacing="0">';
1224: my $date=localtime;
1225: $result.='<tr><td align="right" valign="bottom">Key: </td>';
1226: if ($navmap->{LAST_CHECK}) {
1227: $result .=
1228: '<img src="/adm/lonMisc/chat.gif"> New discussion since '.
1229: strftime("%A, %b %e at %I:%M %P", localtime($navmap->{LAST_CHECK})).
1230: '</td><td align="center" valign="bottom"> '.
1231: '<img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>'.
1232: '</td>';
1233: } else {
1234: $result .= '<td align="center" valign="bottom"> '.
1235: '<img src="/adm/lonMisc/chat.gif"> Discussions</td><td align="center" valign="bottom">'.
1236: ' <img src="/adm/lonMisc/feedback.gif"> New message (click to open)'.
1237: '</td>';
1238: }
1239:
1240: $result .= '</tr></table>';
1241: }
1242:
1.172 bowersj2 1243: if ($printCloseAll && !$args->{'resource_no_folder_link'}) {
1.140 bowersj2 1244: if ($condition) {
1245: $result.="<a href=\"navmaps?condition=0&filter=&$queryString" .
1.158 bowersj2 1246: "&here=" . Apache::lonnet::escape($here) .
1.140 bowersj2 1247: "\">Close All Folders</a>";
1248: } else {
1249: $result.="<a href=\"navmaps?condition=1&filter=&$queryString" .
1.158 bowersj2 1250: "&here=" . Apache::lonnet::escape($here) .
1.140 bowersj2 1251: "\">Open All Folders</a>";
1252: }
1.143 bowersj2 1253: $result .= "<br /><br />\n";
1.140 bowersj2 1254: }
1255:
1256: if ($r) {
1257: $r->print($result);
1258: $r->rflush();
1259: $result = "";
1260: }
1.132 bowersj2 1261: # End parameter setting
1.133 bowersj2 1262:
1.132 bowersj2 1263: # Data
1.140 bowersj2 1264: $result .= '<table cellspacing="0" cellpadding="3" border="0" bgcolor="#FFFFFF">' ."\n";
1.132 bowersj2 1265: my $res = "Apache::lonnavmaps::resource";
1266: my %condenseStatuses =
1267: ( $res->NETWORK_FAILURE => 1,
1268: $res->NOTHING_SET => 1,
1269: $res->CORRECT => 1 );
1270: my @backgroundColors = ("#FFFFFF", "#F6F6F6");
1.133 bowersj2 1271:
1272: # Shared variables
1273: $args->{'counter'} = 0; # counts the rows
1274: $args->{'indentLevel'} = 0;
1275: $args->{'isNewBranch'} = 0;
1276: $args->{'condensed'} = 0;
1277: $args->{'indentString'} = setDefault($args->{'indentString'}, "<img src='/adm/lonIcons/whitespace1.gif' width='25' height='1' alt='' border='0' />");
1278: $args->{'displayedHereMarker'} = 0;
1.132 bowersj2 1279:
1.190 bowersj2 1280: # If we're suppressing empty sequences, look for them here. Use DFS for speed,
1281: # since structure actually doesn't matter, except what map has what resources.
1282: if ($args->{'suppressEmptySequences'}) {
1283: my $dfsit = Apache::lonnavmaps::DFSiterator->new($navmap,
1284: $it->{FIRST_RESOURCE},
1285: $it->{FINISH_RESOURCE},
1286: {}, undef, 1);
1287: $depth = 0;
1288: $dfsit->next();
1289: my $curRes = $dfsit->next();
1290: while ($depth > -1) {
1291: if ($curRes == $dfsit->BEGIN_MAP()) { $depth++; }
1292: if ($curRes == $dfsit->END_MAP()) { $depth--; }
1293:
1294: if (ref($curRes)) {
1295: # Parallel pre-processing: Do sequences have non-filtered-out children?
1296: if ($curRes->is_sequence()) {
1297: $curRes->{DATA}->{HAS_VISIBLE_CHILDREN} = 0;
1298: # Sequences themselves do not count as visible children,
1299: # unless those sequences also have visible children.
1300: # This means if a sequence appears, there's a "promise"
1301: # that there's something under it if you open it, somewhere.
1302: } else {
1303: # Not a sequence: if it's filtered, ignore it, otherwise
1304: # rise up the stack and mark the sequences as having children
1305: if (&$filterFunc($curRes)) {
1306: for my $sequence (@{$dfsit->getStack()}) {
1307: $sequence->{DATA}->{HAS_VISIBLE_CHILDREN} = 1;
1308: }
1309: }
1310: }
1311: }
1312: } continue {
1313: $curRes = $dfsit->next();
1314: }
1315: }
1316:
1.133 bowersj2 1317: my $displayedJumpMarker = 0;
1.132 bowersj2 1318: # Set up iteration.
1.159 bowersj2 1319: $depth = 1;
1.132 bowersj2 1320: $it->next(); # discard initial BEGIN_MAP
1.159 bowersj2 1321: $curRes = $it->next();
1.132 bowersj2 1322: my $now = time();
1323: my $in24Hours = $now + 24 * 60 * 60;
1324: my $rownum = 0;
1325:
1.141 bowersj2 1326: # export "here" marker information
1327: $args->{'here'} = $here;
1328:
1.132 bowersj2 1329: while ($depth > 0) {
1330: if ($curRes == $it->BEGIN_MAP()) { $depth++; }
1331: if ($curRes == $it->END_MAP()) { $depth--; }
1332:
1333: # Maintain indentation level.
1334: if ($curRes == $it->BEGIN_MAP() ||
1335: $curRes == $it->BEGIN_BRANCH() ) {
1.133 bowersj2 1336: $args->{'indentLevel'}++;
1.132 bowersj2 1337: }
1338: if ($curRes == $it->END_MAP() ||
1339: $curRes == $it->END_BRANCH() ) {
1.133 bowersj2 1340: $args->{'indentLevel'}--;
1.132 bowersj2 1341: }
1342: # Notice new branches
1343: if ($curRes == $it->BEGIN_BRANCH()) {
1.133 bowersj2 1344: $args->{'isNewBranch'} = 1;
1345: }
1346:
1347: # If this isn't an actual resource, continue on
1348: if (!ref($curRes)) {
1349: next;
1.132 bowersj2 1350: }
1.133 bowersj2 1351:
1.143 bowersj2 1352: # If this has been filtered out, continue on
1353: if (!(&$filterFunc($curRes))) {
1354: $args->{'isNewBranch'} = 0; # Don't falsely remember this
1355: next;
1356: }
1.132 bowersj2 1357:
1.190 bowersj2 1358: # If this is an empty sequence and we're filtering them, continue on
1359: if ($curRes->is_sequence() && $args->{'suppressEmptySequences'} &&
1360: !$curRes->{DATA}->{HAS_VISIBLE_CHILDREN}) {
1361: next;
1362: }
1363:
1.153 bowersj2 1364: # If we're suppressing navmaps and this is a navmap, continue on
1365: if ($suppressNavmap && $curRes->src() =~ /^\/adm\/navmaps/) {
1366: next;
1367: }
1368:
1.191 bowersj2 1369: $args->{'counter'}++;
1370:
1.132 bowersj2 1371: # Does it have multiple parts?
1.133 bowersj2 1372: $args->{'multipart'} = 0;
1373: $args->{'condensed'} = 0;
1.132 bowersj2 1374: my @parts;
1375:
1376: # Decide what parts to show.
1.133 bowersj2 1377: if ($curRes->is_problem() && $showParts) {
1.132 bowersj2 1378: @parts = @{$curRes->parts()};
1.192 bowersj2 1379: $args->{'multipart'} = $curRes->multipart();
1.132 bowersj2 1380:
1381: if ($condenseParts) { # do the condensation
1382: if (!$curRes->opendate("0")) {
1.162 bowersj2 1383: @parts = ();
1.133 bowersj2 1384: $args->{'condensed'} = 1;
1.132 bowersj2 1385: }
1.133 bowersj2 1386: if (!$args->{'condensed'}) {
1.132 bowersj2 1387: # Decide whether to condense based on similarity
1.192 bowersj2 1388: my $status = $curRes->status($parts[0]);
1389: my $due = $curRes->duedate($parts[0]);
1390: my $open = $curRes->opendate($parts[0]);
1.132 bowersj2 1391: my $statusAllSame = 1;
1392: my $dueAllSame = 1;
1393: my $openAllSame = 1;
1.192 bowersj2 1394: for (my $i = 1; $i < scalar(@parts); $i++) {
1.132 bowersj2 1395: if ($curRes->status($parts[$i]) != $status){
1396: $statusAllSame = 0;
1397: }
1398: if ($curRes->duedate($parts[$i]) != $due ) {
1399: $dueAllSame = 0;
1400: }
1401: if ($curRes->opendate($parts[$i]) != $open) {
1402: $openAllSame = 0;
1403: }
1404: }
1405: # $*allSame is true if all the statuses were
1406: # the same. Now, if they are all the same and
1407: # match one of the statuses to condense, or they
1408: # are all open with the same due date, or they are
1409: # all OPEN_LATER with the same open date, display the
1410: # status of the first non-zero part (to get the 'correct'
1411: # status right, since 0 is never 'correct' or 'open').
1412: if (($statusAllSame && defined($condenseStatuses{$status})) ||
1413: ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
1414: ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
1.192 bowersj2 1415: @parts = ($parts[0]);
1.133 bowersj2 1416: $args->{'condensed'} = 1;
1.132 bowersj2 1417: }
1418: }
1.198 ! bowersj2 1419: # Multipart problem with one part: always "condense" (happens
! 1420: # to match the desirable behavior)
! 1421: if ($curRes->countParts() == 1) {
! 1422: @parts = ($parts[0]);
! 1423: $args->{'condensed'} = 1;
! 1424: }
1.132 bowersj2 1425: }
1.157 bowersj2 1426: }
1.132 bowersj2 1427:
1428: # If the multipart problem was condensed, "forget" it was multipart
1429: if (scalar(@parts) == 1) {
1.133 bowersj2 1430: $args->{'multipart'} = 0;
1.192 bowersj2 1431: } else {
1432: # Add part 0 so we display it correctly.
1433: unshift @parts, '0';
1.132 bowersj2 1434: }
1435:
1436: # Now, we've decided what parts to show. Loop through them and
1437: # show them.
1.192 bowersj2 1438: foreach my $part (@parts) {
1.132 bowersj2 1439: $rownum ++;
1440: my $backgroundColor = $backgroundColors[$rownum % scalar(@backgroundColors)];
1441:
1442: $result .= " <tr bgcolor='$backgroundColor'>\n";
1.134 bowersj2 1443:
1444: # Set up some data about the parts that the cols might want
1445: my $filter = $it->{FILTER};
1446: my $stack = $it->getStack();
1447: my $src = getLinkForResource($stack);
1448:
1449: my $srcHasQuestion = $src =~ /\?/;
1450: $args->{"resourceLink"} = $src.
1451: ($srcHasQuestion?'&':'?') .
1.137 bowersj2 1452: 'symb=' . &Apache::lonnet::escape($curRes->symb());
1.132 bowersj2 1453:
1454: # Now, display each column.
1455: foreach my $col (@$cols) {
1.139 bowersj2 1456: my $colHTML = '';
1457: if (ref($col)) {
1458: $colHTML .= &$col($curRes, $part, $args);
1459: } else {
1460: $colHTML .= &{$preparedColumns[$col]}($curRes, $part, $args);
1461: }
1.132 bowersj2 1462:
1.133 bowersj2 1463: # If this is the first column and it's time to print
1464: # the anchor, do so
1465: if ($col == $cols->[0] &&
1466: $args->{'counter'} == $args->{'currentJumpIndex'} -
1.139 bowersj2 1467: $currentJumpDelta) {
1468: # Jam the anchor after the <td> tag;
1469: # necessary for valid HTML (which Mozilla requires)
1470: $colHTML =~ s/\>/\>\<a name="curloc" \/\>/;
1.133 bowersj2 1471: $displayedJumpMarker = 1;
1472: }
1.139 bowersj2 1473: $result .= $colHTML . "\n";
1.132 bowersj2 1474: }
1.139 bowersj2 1475: $result .= " </tr>\n";
1.140 bowersj2 1476: $args->{'isNewBranch'} = 0;
1.139 bowersj2 1477: }
1.153 bowersj2 1478:
1.139 bowersj2 1479: if ($r && $rownum % 20 == 0) {
1480: $r->print($result);
1481: $result = "";
1482: $r->rflush();
1.132 bowersj2 1483: }
1.156 bowersj2 1484: } continue {
1.132 bowersj2 1485: $curRes = $it->next();
1486: }
1487:
1.134 bowersj2 1488: # Print out the part that jumps to #curloc if it exists
1.159 bowersj2 1489: # delay needed because the browser is processing the jump before
1490: # it finishes rendering, so it goes to the wrong place!
1491: # onload might be better, but this routine has no access to that.
1492: # On mozilla, the 0-millisecond timeout seems to prevent this;
1493: # it's quite likely this might fix other browsers, too, and
1494: # certainly won't hurt anything.
1.139 bowersj2 1495: if ($displayedJumpMarker) {
1.159 bowersj2 1496: $result .= "<script>setTimeout(\"location += '#curloc';\", 0)</script>\n";
1.134 bowersj2 1497: }
1498:
1.132 bowersj2 1499: $result .= "</table>";
1.140 bowersj2 1500:
1501: if ($r) {
1502: $r->print($result);
1503: $result = "";
1504: $r->rflush();
1505: }
1506:
1.175 bowersj2 1507: if ($mustCloseNavMap) { $navmap->untieHashes(); }
1.132 bowersj2 1508:
1509: return $result;
1510: }
1511:
1.133 bowersj2 1512: 1;
1513:
1514: package Apache::lonnavmaps::navmap;
1515:
1516: =pod
1517:
1.174 albertel 1518: lonnavmaps provides functions and objects for dealing with the
1519: compiled course hashes generated when a user enters the course, the
1520: Apache handler for the "Navigation Map" button, and a flexible
1521: prepared renderer for navigation maps that are easy to use anywhere.
1522:
1523: =head1 Object: navmap
1.133 bowersj2 1524:
1.174 albertel 1525: Encapsulating the compiled nav map
1.133 bowersj2 1526:
1.174 albertel 1527: navmap is an object that encapsulates a compiled course map and
1528: provides a reasonable interface to it.
1.133 bowersj2 1529:
1.174 albertel 1530: Most notably it provides a way to navigate the map sensibly and a
1531: flexible iterator that makes it easy to write various renderers based
1532: on nav maps.
1.133 bowersj2 1533:
1534: You must obtain resource objects through the navmap object.
1535:
1536: =head2 Methods
1537:
1538: =over 4
1539:
1.174 albertel 1540: =item * B<new>(navHashFile, parmHashFile, genCourseAndUserOptions,
1541: genMailDiscussStatus):
1.133 bowersj2 1542:
1.174 albertel 1543: Binds a new navmap object to the compiled nav map hash and parm hash
1544: given as filenames. genCourseAndUserOptions is a flag saying whether
1545: the course options and user options hash should be generated. This is
1546: for when you are using the parameters of the resources that require
1547: them; see documentation in resource object
1548: documentation. genMailDiscussStatus causes the nav map to retreive
1549: information about the email and discussion status of
1550: resources. Returns the navmap object if this is successful, or
1551: B<undef> if not. You must check for undef; errors will occur when you
1552: try to use the other methods otherwise.
1553:
1554: =item * B<getIterator>(first, finish, filter, condition):
1555:
1556: See iterator documentation below.
1.133 bowersj2 1557:
1558: =cut
1559:
1560: use strict;
1561: use GDBM_File;
1562:
1563: sub new {
1564: # magic invocation to create a class instance
1565: my $proto = shift;
1566: my $class = ref($proto) || $proto;
1567: my $self = {};
1568:
1569: $self->{NAV_HASH_FILE} = shift;
1570: $self->{PARM_HASH_FILE} = shift;
1571: $self->{GENERATE_COURSE_USER_OPT} = shift;
1572: $self->{GENERATE_EMAIL_DISCUSS_STATUS} = shift;
1573:
1574: # Resource cache stores navmap resources as we reference them. We generate
1575: # them on-demand so we don't pay for creating resources unless we use them.
1576: $self->{RESOURCE_CACHE} = {};
1577:
1578: # Network failure flag, if we accessed the course or user opt and
1579: # failed
1580: $self->{NETWORK_FAILURE} = 0;
1581:
1582: # tie the nav hash
1583:
1.168 bowersj2 1584: my %navmaphash;
1585: my %parmhash;
1586: if (!(tie(%navmaphash, 'GDBM_File', $self->{NAV_HASH_FILE},
1.133 bowersj2 1587: &GDBM_READER(), 0640))) {
1588: return undef;
1589: }
1590:
1.168 bowersj2 1591: if (!(tie(%parmhash, 'GDBM_File', $self->{PARM_HASH_FILE},
1.133 bowersj2 1592: &GDBM_READER(), 0640)))
1593: {
1.170 matthew 1594: untie %{$self->{PARM_HASH}};
1.133 bowersj2 1595: return undef;
1596: }
1597:
1.195 bowersj2 1598: # try copying into memory
1599: my %tmpnavhash;
1600: while (my ($k, $v) = each(%navmaphash)) {
1601: $tmpnavhash{$k} = $v;
1602: }
1603: untie %navmaphash;
1604:
1605: $self->{NAV_HASH} = \%tmpnavhash;
1.168 bowersj2 1606: $self->{PARM_HASH} = \%parmhash;
1.140 bowersj2 1607: $self->{INITED} = 0;
1.133 bowersj2 1608:
1609: bless($self);
1610:
1611: return $self;
1612: }
1613:
1614: sub init {
1615: my $self = shift;
1.140 bowersj2 1616: if ($self->{INITED}) { return; }
1.133 bowersj2 1617:
1618: # If the course opt hash and the user opt hash should be generated,
1619: # generate them
1620: if ($self->{GENERATE_COURSE_USER_OPT}) {
1621: my $uname=$ENV{'user.name'};
1622: my $udom=$ENV{'user.domain'};
1623: my $uhome=$ENV{'user.home'};
1624: my $cid=$ENV{'request.course.id'};
1625: my $chome=$ENV{'course.'.$cid.'.home'};
1626: my ($cdom,$cnum)=split(/\_/,$cid);
1627:
1628: my $userprefix=$uname.'_'.$udom.'_';
1629:
1630: my %courserdatas; my %useropt; my %courseopt; my %userrdatas;
1631: unless ($uhome eq 'no_host') {
1632: # ------------------------------------------------- Get coursedata (if present)
1633: unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
1634: my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
1635: ':resourcedata',$chome);
1.182 bowersj2 1636: # Check for network failure
1637: if ( $reply =~ /no.such.host/i || $reply =~ /con_lost/i) {
1638: $self->{NETWORK_FAILURE} = 1;
1639: } elsif ($reply!~/^error\:/) {
1.133 bowersj2 1640: $courserdatas{$cid}=$reply;
1641: $courserdatas{$cid.'.last_cache'}=time;
1642: }
1643: }
1644: foreach (split(/\&/,$courserdatas{$cid})) {
1645: my ($name,$value)=split(/\=/,$_);
1646: $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
1647: &Apache::lonnet::unescape($value);
1648: }
1649: # --------------------------------------------------- Get userdata (if present)
1650: unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
1651: my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
1652: if ($reply!~/^error\:/) {
1653: $userrdatas{$uname.'___'.$udom}=$reply;
1654: $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
1655: }
1656: # check to see if network failed
1657: elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
1658: {
1659: $self->{NETWORK_FAILURE} = 1;
1660: }
1661: }
1662: foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
1663: my ($name,$value)=split(/\=/,$_);
1664: $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
1665: &Apache::lonnet::unescape($value);
1666: }
1667: $self->{COURSE_OPT} = \%courseopt;
1668: $self->{USER_OPT} = \%useropt;
1669: }
1670: }
1671:
1672: if ($self->{GENERATE_EMAIL_DISCUSS_STATUS}) {
1673: my $cid=$ENV{'request.course.id'};
1674: my ($cdom,$cnum)=split(/\_/,$cid);
1675:
1676: my %emailstatus = &Apache::lonnet::dump('email_status');
1677: my $logoutTime = $emailstatus{'logout'};
1678: my $courseLeaveTime = $emailstatus{'logout_'.$ENV{'request.course.id'}};
1.193 bowersj2 1679: $self->{LAST_CHECK} = (($courseLeaveTime > $logoutTime) ?
1.133 bowersj2 1680: $courseLeaveTime : $logoutTime);
1681: my %discussiontime = &Apache::lonnet::dump('discussiontimes',
1682: $cdom, $cnum);
1683: my %feedback=();
1684: my %error=();
1685: my $keys = &Apache::lonnet::reply('keys:'.
1686: $ENV{'user.domain'}.':'.
1687: $ENV{'user.name'}.':nohist_email',
1688: $ENV{'user.home'});
1689:
1690: foreach my $msgid (split(/\&/, $keys)) {
1691: $msgid=&Apache::lonnet::unescape($msgid);
1692: my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
1693: if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
1694: my ($what,$url)=($1,$2);
1695: my %status=
1696: &Apache::lonnet::get('email_status',[$msgid]);
1697: if ($status{$msgid}=~/^error\:/) {
1698: $status{$msgid}='';
1699: }
1700:
1701: if (($status{$msgid} eq 'new') ||
1702: (!$status{$msgid})) {
1703: if ($what eq 'Error') {
1704: $error{$url}.=','.$msgid;
1705: } else {
1706: $feedback{$url}.=','.$msgid;
1707: }
1708: }
1709: }
1710: }
1711:
1712: $self->{FEEDBACK} = \%feedback;
1713: $self->{ERROR_MSG} = \%error; # what is this? JB
1714: $self->{DISCUSSION_TIME} = \%discussiontime;
1715: $self->{EMAIL_STATUS} = \%emailstatus;
1716:
1717: }
1718:
1719: $self->{PARM_CACHE} = {};
1.140 bowersj2 1720: $self->{INITED} = 1;
1.133 bowersj2 1721: }
1722:
1723: # Internal function: Takes a key to look up in the nav hash and implements internal
1724: # memory caching of that key.
1725: sub navhash {
1726: my $self = shift; my $key = shift;
1727: return $self->{NAV_HASH}->{$key};
1728: }
1729:
1730: # Checks to see if coursemap is defined, matching test in old lonnavmaps
1731: sub courseMapDefined {
1732: my $self = shift;
1733: my $uri = &Apache::lonnet::clutter($ENV{'request.course.uri'});
1734:
1735: my $firstres = $self->navhash("map_start_$uri");
1736: my $lastres = $self->navhash("map_finish_$uri");
1737: return $firstres && $lastres;
1738: }
1739:
1740: sub getIterator {
1741: my $self = shift;
1742: my $iterator = Apache::lonnavmaps::iterator->new($self, shift, shift,
1743: shift, undef, shift);
1744: return $iterator;
1745: }
1746:
1747: # unties the hash when done
1748: sub untieHashes {
1.168 bowersj2 1749: my $self = shift;
1.170 matthew 1750: untie %{$self->{NAV_HASH}};
1751: untie %{$self->{PARM_HASH}};
1.133 bowersj2 1752: }
1753:
1754: # Private method: Does the given resource (as a symb string) have
1755: # current discussion? Returns 0 if chat/mail data not extracted.
1756: sub hasDiscussion {
1757: my $self = shift;
1758: my $symb = shift;
1759: if (!defined($self->{DISCUSSION_TIME})) { return 0; }
1760:
1761: #return defined($self->{DISCUSSION_TIME}->{$symb});
1762: return $self->{DISCUSSION_TIME}->{$symb} >
1763: $self->{LAST_CHECK};
1764: }
1765:
1766: # Private method: Does the given resource (as a symb string) have
1767: # current feedback? Returns the string in the feedback hash, which
1768: # will be false if it does not exist.
1769: sub getFeedback {
1770: my $self = shift;
1771: my $symb = shift;
1772:
1773: if (!defined($self->{FEEDBACK})) { return ""; }
1774:
1775: return $self->{FEEDBACK}->{$symb};
1776: }
1777:
1778: # Private method: Get the errors for that resource (by source).
1779: sub getErrors {
1780: my $self = shift;
1781: my $src = shift;
1782:
1783: if (!defined($self->{ERROR_MSG})) { return ""; }
1784: return $self->{ERROR_MSG}->{$src};
1785: }
1786:
1787: =pod
1788:
1.174 albertel 1789: =item * B<getById>(id):
1790:
1791: Based on the ID of the resource (1.1, 3.2, etc.), get a resource
1792: object for that resource. This method, or other methods that use it
1793: (as in the resource object) is the only proper way to obtain a
1794: resource object.
1.133 bowersj2 1795:
1.194 bowersj2 1796: =item * B<getBySymb>(symb):
1797:
1798: Based on the symb of the resource, get a resource object for that
1799: resource. This is one of the proper ways to get a resource object.
1800:
1801: =item * B<getMapByMapPc>(map_pc):
1802:
1803: Based on the map_pc of the resource, get a resource object for
1804: the given map. This is one of the proper ways to get a resource object.
1805:
1.133 bowersj2 1806: =cut
1807:
1808: # The strategy here is to cache the resource objects, and only construct them
1809: # as we use them. The real point is to prevent reading any more from the tied
1810: # hash then we have to, which should hopefully alleviate speed problems.
1811: # Caching is just an incidental detail I throw in because it makes sense.
1812:
1813: sub getById {
1814: my $self = shift;
1815: my $id = shift;
1816:
1817: if (defined ($self->{RESOURCE_CACHE}->{$id}))
1818: {
1819: return $self->{RESOURCE_CACHE}->{$id};
1820: }
1821:
1822: # resource handles inserting itself into cache.
1823: # Not clear why the quotes are necessary, but as of this
1824: # writing it doesn't work without them.
1825: return "Apache::lonnavmaps::resource"->new($self, $id);
1.132 bowersj2 1826: }
1.133 bowersj2 1827:
1.172 bowersj2 1828: sub getBySymb {
1829: my $self = shift;
1830: my $symb = shift;
1831: my ($mapUrl, $id, $filename) = split (/___/, $symb);
1832: my $map = $self->getResourceByUrl($mapUrl);
1833: return $self->getById($map->map_pc() . '.' . $id);
1.194 bowersj2 1834: }
1835:
1836: sub getByMapPc {
1837: my $self = shift;
1838: my $map_pc = shift;
1839: my $map_id = $self->{NAV_HASH}->{'map_id_' . $map_pc};
1840: $map_id = $self->{NAV_HASH}->{'ids_' . $map_id};
1841: return $self->getById($map_id);
1.172 bowersj2 1842: }
1843:
1.133 bowersj2 1844: =pod
1845:
1.174 albertel 1846: =item * B<firstResource>():
1847:
1848: Returns a resource object reference corresponding to the first
1849: resource in the navmap.
1.133 bowersj2 1850:
1851: =cut
1852:
1853: sub firstResource {
1854: my $self = shift;
1855: my $firstResource = $self->navhash('map_start_' .
1856: &Apache::lonnet::clutter($ENV{'request.course.uri'}));
1857: return $self->getById($firstResource);
1.132 bowersj2 1858: }
1.133 bowersj2 1859:
1860: =pod
1861:
1.174 albertel 1862: =item * B<finishResource>():
1863:
1864: Returns a resource object reference corresponding to the last resource
1865: in the navmap.
1.133 bowersj2 1866:
1867: =cut
1868:
1869: sub finishResource {
1870: my $self = shift;
1871: my $firstResource = $self->navhash('map_finish_' .
1872: &Apache::lonnet::clutter($ENV{'request.course.uri'}));
1873: return $self->getById($firstResource);
1.132 bowersj2 1874: }
1.133 bowersj2 1875:
1876: # Parmval reads the parm hash and cascades the lookups. parmval_real does
1877: # the actual lookup; parmval caches the results.
1878: sub parmval {
1879: my $self = shift;
1880: my ($what,$symb)=@_;
1881: my $hashkey = $what."|||".$symb;
1882:
1883: if (defined($self->{PARM_CACHE}->{$hashkey})) {
1884: return $self->{PARM_CACHE}->{$hashkey};
1885: }
1886:
1887: my $result = $self->parmval_real($what, $symb);
1888: $self->{PARM_CACHE}->{$hashkey} = $result;
1889: return $result;
1.132 bowersj2 1890: }
1891:
1.133 bowersj2 1892: sub parmval_real {
1893: my $self = shift;
1894: my ($what,$symb) = @_;
1895:
1896: my $cid=$ENV{'request.course.id'};
1897: my $csec=$ENV{'request.course.sec'};
1898: my $uname=$ENV{'user.name'};
1899: my $udom=$ENV{'user.domain'};
1900:
1901: unless ($symb) { return ''; }
1902: my $result='';
1903:
1904: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
1905:
1906: # ----------------------------------------------------- Cascading lookup scheme
1907: my $rwhat=$what;
1908: $what=~s/^parameter\_//;
1909: $what=~s/\_/\./;
1910:
1911: my $symbparm=$symb.'.'.$what;
1912: my $mapparm=$mapname.'___(all).'.$what;
1913: my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
1914:
1915: my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
1916: my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
1917: my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
1918:
1919: my $courselevel= $usercourseprefix.'.'.$what;
1920: my $courselevelr=$usercourseprefix.'.'.$symbparm;
1921: my $courselevelm=$usercourseprefix.'.'.$mapparm;
1922:
1923: my $useropt = $self->{USER_OPT};
1924: my $courseopt = $self->{COURSE_OPT};
1925: my $parmhash = $self->{PARM_HASH};
1926:
1927: # ---------------------------------------------------------- first, check user
1928: if ($uname and defined($useropt)) {
1929: if (defined($$useropt{$courselevelr})) { return $$useropt{$courselevelr}; }
1930: if (defined($$useropt{$courselevelm})) { return $$useropt{$courselevelm}; }
1931: if (defined($$useropt{$courselevel})) { return $$useropt{$courselevel}; }
1932: }
1933:
1934: # ------------------------------------------------------- second, check course
1935: if ($csec and defined($courseopt)) {
1936: if (defined($$courseopt{$seclevelr})) { return $$courseopt{$seclevelr}; }
1937: if (defined($$courseopt{$seclevelm})) { return $$courseopt{$seclevelm}; }
1938: if (defined($$courseopt{$seclevel})) { return $$courseopt{$seclevel}; }
1939: }
1940:
1941: if (defined($courseopt)) {
1942: if (defined($$courseopt{$courselevelr})) { return $$courseopt{$courselevelr}; }
1943: if (defined($$courseopt{$courselevelm})) { return $$courseopt{$courselevelm}; }
1944: if (defined($$courseopt{$courselevel})) { return $$courseopt{$courselevel}; }
1945: }
1946:
1947: # ----------------------------------------------------- third, check map parms
1948:
1949: my $thisparm=$$parmhash{$symbparm};
1950: if (defined($thisparm)) { return $thisparm; }
1951:
1952: # ----------------------------------------------------- fourth , check default
1953:
1954: my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
1955: if (defined($default)) { return $default}
1956:
1957: # --------------------------------------------------- fifth , cascade up parts
1958:
1959: my ($space,@qualifier)=split(/\./,$rwhat);
1960: my $qualifier=join('.',@qualifier);
1961: unless ($space eq '0') {
1.160 albertel 1962: my @parts=split(/_/,$space);
1963: my $id=pop(@parts);
1964: my $part=join('_',@parts);
1965: if ($part eq '') { $part='0'; }
1966: my $partgeneral=$self->parmval($part.".$qualifier",$symb);
1967: if (defined($partgeneral)) { return $partgeneral; }
1.133 bowersj2 1968: }
1969: return '';
1.145 bowersj2 1970: }
1971:
1.174 albertel 1972: =pod
1.145 bowersj2 1973:
1.174 albertel 1974: =item * B<getResourceByUrl>(url):
1.145 bowersj2 1975:
1.174 albertel 1976: Retrieves a resource object by URL of the resource. If passed a
1977: resource object, it will simply return it, so it is safe to use this
1978: method in code like "$res = $navmap->getResourceByUrl($res)", if
1979: you're not sure if $res is already an object, or just a URL. If the
1980: resource appears multiple times in the course, only the first instance
1981: will be returned. As a result, this is probably useful only for maps.
1982:
1983: =item * B<retrieveResources>(map, filterFunc, recursive, bailout):
1984:
1985: The map is a specification of a map to retreive the resources from,
1986: either as a url or as an object. The filterFunc is a reference to a
1987: function that takes a resource object as its one argument and returns
1988: true if the resource should be included, or false if it should not
1989: be. If recursive is true, the map will be recursively examined,
1990: otherwise it will not be. If bailout is true, the function will return
1991: as soon as it finds a resource, if false it will finish. By default,
1992: the map is the top-level map of the course, filterFunc is a function
1993: that always returns 1, recursive is true, bailout is false. The
1994: resources will be returned in a list containing the resource objects
1995: for the corresponding resources, with B<no structure information> in
1996: the list; regardless of branching, recursion, etc., it will be a flat
1997: list.
1998:
1999: Thus, this is suitable for cases where you don't want the structure,
2000: just a list of all resources. It is also suitable for finding out how
2001: many resources match a given description; for this use, if all you
2002: want to know is if I<any> resources match the description, the bailout
2003: parameter will allow you to avoid potentially expensive enumeration of
2004: all matching resources.
1.145 bowersj2 2005:
1.174 albertel 2006: =item * B<hasResources>(map, filterFunc, recursive):
1.145 bowersj2 2007:
1.174 albertel 2008: Convience method for
1.146 bowersj2 2009:
2010: scalar(retrieveResources($map, $filterFunc, $recursive, 1)) > 0
2011:
1.174 albertel 2012: which will tell whether the map has resources matching the description
2013: in the filter function.
1.146 bowersj2 2014:
1.145 bowersj2 2015: =cut
2016:
2017: sub getResourceByUrl {
2018: my $self = shift;
2019: my $resUrl = shift;
2020:
2021: if (ref($resUrl)) { return $resUrl; }
2022:
2023: $resUrl = &Apache::lonnet::clutter($resUrl);
2024: my $resId = $self->{NAV_HASH}->{'ids_' . $resUrl};
2025: if ($resId =~ /,/) {
2026: $resId = (split (/,/, $resId))[0];
2027: }
2028: if (!$resId) { return ''; }
2029: return $self->getById($resId);
2030: }
2031:
2032: sub retrieveResources {
2033: my $self = shift;
2034: my $map = shift;
2035: my $filterFunc = shift;
2036: if (!defined ($filterFunc)) {
2037: $filterFunc = sub {return 1;};
2038: }
2039: my $recursive = shift;
2040: if (!defined($recursive)) { $recursive = 1; }
2041: my $bailout = shift;
2042: if (!defined($bailout)) { $bailout = 0; }
2043:
2044: # Create the necessary iterator.
2045: if (!ref($map)) { # assume it's a url of a map.
1.172 bowersj2 2046: $map = $self->getResourceByUrl($map);
1.145 bowersj2 2047: }
2048:
2049: # Check the map's validity.
2050: if (!$map || !$map->is_map()) {
2051: # Oh, to throw an exception.... how I'd love that!
2052: return ();
2053: }
2054:
1.146 bowersj2 2055: # Get an iterator.
2056: my $it = $self->getIterator($map->map_start(), $map->map_finish(),
2057: !$recursive);
2058:
2059: my @resources = ();
2060:
2061: # Run down the iterator and collect the resources.
2062: my $depth = 1;
2063: $it->next();
2064: my $curRes = $it->next();
2065:
2066: while ($depth > 0) {
2067: if ($curRes == $it->BEGIN_MAP()) {
2068: $depth++;
2069: }
2070: if ($curRes == $it->END_MAP()) {
2071: $depth--;
2072: }
2073:
2074: if (ref($curRes)) {
2075: if (!&$filterFunc($curRes)) {
2076: next;
2077: }
2078:
2079: push @resources, $curRes;
2080:
2081: if ($bailout) {
2082: return @resources;
2083: }
2084: }
2085:
2086: $curRes = $it->next();
2087: }
2088:
2089: return @resources;
2090: }
2091:
2092: sub hasResource {
2093: my $self = shift;
2094: my $map = shift;
2095: my $filterFunc = shift;
2096: my $recursive = shift;
2097:
2098: return scalar($self->retrieveResources($map, $filterFunc, $recursive, 1)) > 0;
1.133 bowersj2 2099: }
1.51 bowersj2 2100:
2101: 1;
2102:
2103: package Apache::lonnavmaps::iterator;
2104:
2105: =pod
2106:
2107: =back
2108:
1.174 albertel 2109: =head1 Object: navmap Iterator
1.51 bowersj2 2110:
1.174 albertel 2111: An I<iterator> encapsulates the logic required to traverse a data
2112: structure. navmap uses an iterator to traverse the course map
2113: according to the criteria you wish to use.
2114:
2115: To obtain an iterator, call the B<getIterator>() function of a
2116: B<navmap> object. (Do not instantiate Apache::lonnavmaps::iterator
2117: directly.) This will return a reference to the iterator:
1.51 bowersj2 2118:
2119: C<my $resourceIterator = $navmap-E<gt>getIterator();>
2120:
2121: To get the next thing from the iterator, call B<next>:
2122:
2123: C<my $nextThing = $resourceIterator-E<gt>next()>
2124:
2125: getIterator behaves as follows:
2126:
2127: =over 4
2128:
1.174 albertel 2129: =item * B<getIterator>(firstResource, finishResource, filterHash, condition, forceTop, returnTopMap):
2130:
2131: All parameters are optional. firstResource is a resource reference
2132: corresponding to where the iterator should start. It defaults to
2133: navmap->firstResource() for the corresponding nav map. finishResource
2134: corresponds to where you want the iterator to end, defaulting to
2135: navmap->finishResource(). filterHash is a hash used as a set
2136: containing strings representing the resource IDs, defaulting to
2137: empty. Condition is a 1 or 0 that sets what to do with the filter
2138: hash: If a 0, then only resource that exist IN the filterHash will be
2139: recursed on. If it is a 1, only resources NOT in the filterHash will
2140: be recursed on. Defaults to 0. forceTop is a boolean value. If it is
2141: false (default), the iterator will only return the first level of map
2142: that is not just a single, 'redirecting' map. If true, the iterator
2143: will return all information, starting with the top-level map,
2144: regardless of content. returnTopMap, if true (default false), will
2145: cause the iterator to return the top-level map object (resource 0.0)
2146: before anything else.
2147:
2148: Thus, by default, only top-level resources will be shown. Change the
2149: condition to a 1 without changing the hash, and all resources will be
2150: shown. Changing the condition to 1 and including some values in the
2151: hash will allow you to selectively suppress parts of the navmap, while
2152: leaving it on 0 and adding things to the hash will allow you to
2153: selectively add parts of the nav map. See the handler code for
2154: examples.
2155:
2156: The iterator will return either a reference to a resource object, or a
2157: token representing something in the map, such as the beginning of a
2158: new branch. The possible tokens are:
2159:
2160: =over 4
1.51 bowersj2 2161:
1.174 albertel 2162: =item * BEGIN_MAP:
1.51 bowersj2 2163:
1.174 albertel 2164: A new map is being recursed into. This is returned I<after> the map
2165: resource itself is returned.
1.51 bowersj2 2166:
1.174 albertel 2167: =item * END_MAP:
2168:
2169: The map is now done.
1.51 bowersj2 2170:
1.174 albertel 2171: =item * BEGIN_BRANCH:
1.70 bowersj2 2172:
1.174 albertel 2173: A branch is now starting. The next resource returned will be the first
2174: in that branch.
1.70 bowersj2 2175:
1.174 albertel 2176: =item * END_BRANCH:
1.70 bowersj2 2177:
1.174 albertel 2178: The branch is now done.
1.51 bowersj2 2179:
2180: =back
2181:
1.174 albertel 2182: The tokens are retreivable via methods on the iterator object, i.e.,
2183: $iterator->END_MAP.
1.70 bowersj2 2184:
1.174 albertel 2185: Maps can contain empty resources. The iterator will automatically skip
2186: over such resources, but will still treat the structure
2187: correctly. Thus, a complicated map with several branches, but
2188: consisting entirely of empty resources except for one beginning or
2189: ending resource, will cause a lot of BRANCH_STARTs and BRANCH_ENDs,
2190: but only one resource will be returned.
1.116 bowersj2 2191:
1.70 bowersj2 2192: =back
1.51 bowersj2 2193:
2194: =cut
2195:
2196: # Here are the tokens for the iterator:
2197:
2198: sub BEGIN_MAP { return 1; } # begining of a new map
2199: sub END_MAP { return 2; } # end of the map
2200: sub BEGIN_BRANCH { return 3; } # beginning of a branch
2201: sub END_BRANCH { return 4; } # end of a branch
1.89 bowersj2 2202: sub FORWARD { return 1; } # go forward
2203: sub BACKWARD { return 2; }
1.51 bowersj2 2204:
1.96 bowersj2 2205: sub min {
2206: (my $a, my $b) = @_;
2207: if ($a < $b) { return $a; } else { return $b; }
2208: }
2209:
1.107 bowersj2 2210: # In the CVS repository, documentation of this algorithm is included
2211: # in /doc/lonnavdocs, as a PDF and .tex source. Markers like **1**
2212: # will reference the same location in the text as the part of the
2213: # algorithm is running through.
2214:
1.94 bowersj2 2215: sub new {
2216: # magic invocation to create a class instance
2217: my $proto = shift;
2218: my $class = ref($proto) || $proto;
2219: my $self = {};
2220:
2221: $self->{NAV_MAP} = shift;
2222: return undef unless ($self->{NAV_MAP});
2223:
2224: # Handle the parameters
2225: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
2226: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
2227:
2228: # If the given resources are just the ID of the resource, get the
2229: # objects
2230: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
2231: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
2232: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
2233: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
2234:
2235: $self->{FILTER} = shift;
2236:
2237: # A hash, used as a set, of resource already seen
2238: $self->{ALREADY_SEEN} = shift;
2239: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
2240: $self->{CONDITION} = shift;
2241:
1.116 bowersj2 2242: # Do we want to automatically follow "redirection" maps?
2243: $self->{FORCE_TOP} = shift;
2244:
1.162 bowersj2 2245: # Do we want to return the top-level map object (resource 0.0)?
2246: $self->{RETURN_0} = shift;
2247: # have we done that yet?
2248: $self->{HAVE_RETURNED_0} = 0;
2249:
1.94 bowersj2 2250: # Now, we need to pre-process the map, by walking forward and backward
2251: # over the parts of the map we're going to look at.
1.96 bowersj2 2252:
1.97 bowersj2 2253: # The processing steps are exactly the same, except for a few small
2254: # changes, so I bundle those up in the following list of two elements:
2255: # (direction_to_iterate, VAL_name, next_resource_method_to_call,
2256: # first_resource).
2257: # This prevents writing nearly-identical code twice.
2258: my @iterations = ( [FORWARD(), 'TOP_DOWN_VAL', 'getNext',
2259: 'FIRST_RESOURCE'],
2260: [BACKWARD(), 'BOT_UP_VAL', 'getPrevious',
2261: 'FINISH_RESOURCE'] );
2262:
1.98 bowersj2 2263: my $maxDepth = 0; # tracks max depth
2264:
1.116 bowersj2 2265: # If there is only one resource in this map, and it's a map, we
2266: # want to remember that, so the user can ask for the first map
2267: # that isn't just a redirector.
2268: my $resource; my $resourceCount = 0;
2269:
1.107 bowersj2 2270: # **1**
2271:
1.97 bowersj2 2272: foreach my $pass (@iterations) {
2273: my $direction = $pass->[0];
2274: my $valName = $pass->[1];
2275: my $nextResourceMethod = $pass->[2];
2276: my $firstResourceName = $pass->[3];
2277:
2278: my $iterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP},
2279: $self->{FIRST_RESOURCE},
2280: $self->{FINISH_RESOURCE},
2281: {}, undef, 0, $direction);
1.96 bowersj2 2282:
1.97 bowersj2 2283: # prime the recursion
2284: $self->{$firstResourceName}->{DATA}->{$valName} = 0;
1.98 bowersj2 2285: my $depth = 0;
1.97 bowersj2 2286: $iterator->next();
2287: my $curRes = $iterator->next();
1.98 bowersj2 2288: while ($depth > -1) {
1.97 bowersj2 2289: if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
2290: if ($curRes == $iterator->END_MAP()) { $depth--; }
1.96 bowersj2 2291:
1.97 bowersj2 2292: if (ref($curRes)) {
1.116 bowersj2 2293: # If there's only one resource, this will save it
1.117 bowersj2 2294: # we have to filter empty resources from consideration here,
2295: # or even "empty", redirecting maps have two (start & finish)
2296: # or three (start, finish, plus redirector)
2297: if($direction == FORWARD && $curRes->src()) {
2298: $resource = $curRes; $resourceCount++;
2299: }
1.97 bowersj2 2300: my $resultingVal = $curRes->{DATA}->{$valName};
2301: my $nextResources = $curRes->$nextResourceMethod();
1.116 bowersj2 2302: my $nextCount = scalar(@{$nextResources});
1.104 bowersj2 2303:
1.116 bowersj2 2304: if ($nextCount == 1) { # **3**
1.97 bowersj2 2305: my $current = $nextResources->[0]->{DATA}->{$valName} || 999999999;
2306: $nextResources->[0]->{DATA}->{$valName} = min($resultingVal, $current);
2307: }
2308:
1.116 bowersj2 2309: if ($nextCount > 1) { # **4**
1.97 bowersj2 2310: foreach my $res (@{$nextResources}) {
2311: my $current = $res->{DATA}->{$valName} || 999999999;
2312: $res->{DATA}->{$valName} = min($current, $resultingVal + 1);
2313: }
2314: }
2315: }
1.96 bowersj2 2316:
1.107 bowersj2 2317: # Assign the final val (**2**)
1.97 bowersj2 2318: if (ref($curRes) && $direction == BACKWARD()) {
1.98 bowersj2 2319: my $finalDepth = min($curRes->{DATA}->{TOP_DOWN_VAL},
2320: $curRes->{DATA}->{BOT_UP_VAL});
2321:
2322: $curRes->{DATA}->{DISPLAY_DEPTH} = $finalDepth;
2323: if ($finalDepth > $maxDepth) {$maxDepth = $finalDepth;}
1.190 bowersj2 2324: }
2325: } continue {
1.97 bowersj2 2326: $curRes = $iterator->next();
1.96 bowersj2 2327: }
2328: }
1.94 bowersj2 2329:
1.116 bowersj2 2330: # Check: Was this only one resource, a map?
2331: if ($resourceCount == 1 && $resource->is_map() && !$self->{FORCE_TOP}) {
2332: my $firstResource = $resource->map_start();
2333: my $finishResource = $resource->map_finish();
2334: return
2335: Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
2336: $finishResource, $self->{FILTER},
2337: $self->{ALREADY_SEEN},
2338: $self->{CONDITION}, 0);
2339:
2340: }
2341:
1.98 bowersj2 2342: # Set up some bookkeeping information.
2343: $self->{CURRENT_DEPTH} = 0;
2344: $self->{MAX_DEPTH} = $maxDepth;
2345: $self->{STACK} = [];
2346: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2347:
2348: for (my $i = 0; $i <= $self->{MAX_DEPTH}; $i++) {
2349: push @{$self->{STACK}}, [];
2350: }
2351:
1.107 bowersj2 2352: # Prime the recursion w/ the first resource **5**
1.98 bowersj2 2353: push @{$self->{STACK}->[0]}, $self->{FIRST_RESOURCE};
2354: $self->{ALREADY_SEEN}->{$self->{FIRST_RESOURCE}->{ID}} = 1;
2355:
2356: bless ($self);
2357:
2358: return $self;
2359: }
2360:
2361: sub next {
2362: my $self = shift;
1.162 bowersj2 2363:
2364: # If we want to return the top-level map object, and haven't yet,
2365: # do so.
2366: if ($self->{RETURN_0} && !$self->{HAVE_RETURNED_0}) {
2367: $self->{HAVE_RETURNED_0} = 1;
2368: return $self->{NAV_MAP}->getById('0.0');
2369: }
1.98 bowersj2 2370:
2371: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
2372: # grab the next from the recursive iterator
2373: my $next = $self->{RECURSIVE_ITERATOR}->next();
2374:
2375: # is it a begin or end map? If so, update the depth
2376: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
2377: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
2378:
2379: # Are we back at depth 0? If so, stop recursing
2380: if ($self->{RECURSIVE_DEPTH} == 0) {
2381: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2382: }
2383:
2384: return $next;
2385: }
2386:
2387: if (defined($self->{FORCE_NEXT})) {
2388: my $tmp = $self->{FORCE_NEXT};
2389: $self->{FORCE_NEXT} = undef;
2390: return $tmp;
2391: }
2392:
2393: # Have we not yet begun? If not, return BEGIN_MAP and
2394: # remember we've started.
2395: if ( !$self->{STARTED} ) {
2396: $self->{STARTED} = 1;
2397: return $self->BEGIN_MAP();
2398: }
2399:
2400: # Here's the guts of the iterator.
2401:
2402: # Find the next resource, if any.
2403: my $found = 0;
2404: my $i = $self->{MAX_DEPTH};
2405: my $newDepth;
2406: my $here;
2407: while ( $i >= 0 && !$found ) {
1.107 bowersj2 2408: if ( scalar(@{$self->{STACK}->[$i]}) > 0 ) { # **6**
2409: $here = pop @{$self->{STACK}->[$i]}; # **7**
1.98 bowersj2 2410: $found = 1;
2411: $newDepth = $i;
2412: }
2413: $i--;
2414: }
2415:
2416: # If we still didn't find anything, we're done.
2417: if ( !$found ) {
2418: # We need to get back down to the correct branch depth
2419: if ( $self->{CURRENT_DEPTH} > 0 ) {
2420: $self->{CURRENT_DEPTH}--;
2421: return END_BRANCH();
2422: } else {
2423: return END_MAP();
2424: }
2425: }
2426:
1.104 bowersj2 2427: # If this is not a resource, it must be an END_BRANCH marker we want
2428: # to return directly.
1.107 bowersj2 2429: if (!ref($here)) { # **8**
1.104 bowersj2 2430: if ($here == END_BRANCH()) { # paranoia, in case of later extension
2431: $self->{CURRENT_DEPTH}--;
2432: return $here;
2433: }
2434: }
2435:
2436: # Otherwise, it is a resource and it's safe to store in $self->{HERE}
2437: $self->{HERE} = $here;
2438:
1.98 bowersj2 2439: # Get to the right level
2440: if ( $self->{CURRENT_DEPTH} > $newDepth ) {
2441: push @{$self->{STACK}->[$newDepth]}, $here;
2442: $self->{CURRENT_DEPTH}--;
2443: return END_BRANCH();
2444: }
2445: if ( $self->{CURRENT_DEPTH} < $newDepth) {
2446: push @{$self->{STACK}->[$newDepth]}, $here;
2447: $self->{CURRENT_DEPTH}++;
2448: return BEGIN_BRANCH();
2449: }
2450:
2451: # If we made it here, we have the next resource, and we're at the
2452: # right branch level. So let's examine the resource for where
2453: # we can get to from here.
2454:
2455: # So we need to look at all the resources we can get to from here,
2456: # categorize them if we haven't seen them, remember if we have a new
2457: my $nextUnfiltered = $here->getNext();
1.104 bowersj2 2458: my $maxDepthAdded = -1;
2459:
1.98 bowersj2 2460: for (@$nextUnfiltered) {
2461: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
1.104 bowersj2 2462: my $depth = $_->{DATA}->{DISPLAY_DEPTH};
2463: push @{$self->{STACK}->[$depth]}, $_;
1.98 bowersj2 2464: $self->{ALREADY_SEEN}->{$_->{ID}} = 1;
1.104 bowersj2 2465: if ($maxDepthAdded < $depth) { $maxDepthAdded = $depth; }
1.98 bowersj2 2466: }
2467: }
1.104 bowersj2 2468:
2469: # Is this the end of a branch? If so, all of the resources examined above
2470: # led to lower levels then the one we are currently at, so we push a END_BRANCH
2471: # marker onto the stack so we don't forget.
2472: # Example: For the usual A(BC)(DE)F case, when the iterator goes down the
2473: # BC branch and gets to C, it will see F as the only next resource, but it's
2474: # one level lower. Thus, this is the end of the branch, since there are no
2475: # more resources added to this level or above.
1.111 bowersj2 2476: # We don't do this if the examined resource is the finish resource,
2477: # because the condition given above is true, but the "END_MAP" will
2478: # take care of things and we should already be at depth 0.
1.104 bowersj2 2479: my $isEndOfBranch = $maxDepthAdded < $self->{CURRENT_DEPTH};
1.111 bowersj2 2480: if ($isEndOfBranch && $here != $self->{FINISH_RESOURCE}) { # **9**
1.104 bowersj2 2481: push @{$self->{STACK}->[$self->{CURRENT_DEPTH}]}, END_BRANCH();
2482: }
2483:
1.98 bowersj2 2484: # That ends the main iterator logic. Now, do we want to recurse
2485: # down this map (if this resource is a map)?
2486: if ($self->{HERE}->is_map() &&
2487: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
2488: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
2489: my $firstResource = $self->{HERE}->map_start();
2490: my $finishResource = $self->{HERE}->map_finish();
2491:
2492: $self->{RECURSIVE_ITERATOR} =
2493: Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
2494: $finishResource, $self->{FILTER},
2495: $self->{ALREADY_SEEN}, $self->{CONDITION});
2496: }
2497:
1.116 bowersj2 2498: # If this is a blank resource, don't actually return it.
1.117 bowersj2 2499: # Should you ever find you need it, make sure to add an option to the code
2500: # that you can use; other things depend on this behavior.
1.138 bowersj2 2501: my $browsePriv = $self->{HERE}->browsePriv();
2502: if (!$self->{HERE}->src() ||
2503: (!($browsePriv eq 'F') && !($browsePriv eq '2')) ) {
1.116 bowersj2 2504: return $self->next();
2505: }
2506:
1.98 bowersj2 2507: return $self->{HERE};
2508:
2509: }
2510:
2511: =pod
2512:
1.174 albertel 2513: The other method available on the iterator is B<getStack>, which
2514: returns an array populated with the current 'stack' of maps, as
2515: references to the resource objects. Example: This is useful when
2516: making the navigation map, as we need to check whether we are under a
2517: page map to see if we need to link directly to the resource, or to the
2518: page. The first elements in the array will correspond to the top of
2519: the stack (most inclusive map).
1.98 bowersj2 2520:
2521: =cut
2522:
2523: sub getStack {
2524: my $self=shift;
2525:
2526: my @stack;
2527:
2528: $self->populateStack(\@stack);
2529:
2530: return \@stack;
2531: }
2532:
2533: # Private method: Calls the iterators recursively to populate the stack.
2534: sub populateStack {
2535: my $self=shift;
2536: my $stack = shift;
2537:
2538: push @$stack, $self->{HERE} if ($self->{HERE});
2539:
2540: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
2541: $self->{RECURSIVE_ITERATOR}->populateStack($stack);
2542: }
1.94 bowersj2 2543: }
2544:
2545: 1;
2546:
2547: package Apache::lonnavmaps::DFSiterator;
2548:
1.100 bowersj2 2549: # Not documented in the perldoc: This is a simple iterator that just walks
2550: # through the nav map and presents the resources in a depth-first search
2551: # fashion, ignorant of conditionals, randomized resources, etc. It presents
2552: # BEGIN_MAP and END_MAP, but does not understand branches at all. It is
2553: # useful for pre-processing of some kind, and is in fact used by the main
2554: # iterator that way, but that's about it.
2555: # One could imagine merging this into the init routine of the main iterator,
2556: # but this might as well be left seperate, since it is possible some other
2557: # use might be found for it. - Jeremy
1.94 bowersj2 2558:
1.117 bowersj2 2559: # Unlike the main iterator, this DOES return all resources, even blank ones.
2560: # The main iterator needs them to correctly preprocess the map.
2561:
1.94 bowersj2 2562: sub BEGIN_MAP { return 1; } # begining of a new map
2563: sub END_MAP { return 2; } # end of the map
2564: sub FORWARD { return 1; } # go forward
2565: sub BACKWARD { return 2; }
2566:
1.100 bowersj2 2567: # Params: Nav map ref, first resource id/ref, finish resource id/ref,
2568: # filter hash ref (or undef), already seen hash or undef, condition
2569: # (as in main iterator), direction FORWARD or BACKWARD (undef->forward).
1.51 bowersj2 2570: sub new {
2571: # magic invocation to create a class instance
2572: my $proto = shift;
2573: my $class = ref($proto) || $proto;
2574: my $self = {};
2575:
2576: $self->{NAV_MAP} = shift;
2577: return undef unless ($self->{NAV_MAP});
2578:
2579: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
2580: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
2581:
2582: # If the given resources are just the ID of the resource, get the
2583: # objects
2584: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
2585: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
2586: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
2587: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
2588:
2589: $self->{FILTER} = shift;
2590:
2591: # A hash, used as a set, of resource already seen
2592: $self->{ALREADY_SEEN} = shift;
1.140 bowersj2 2593: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1.63 bowersj2 2594: $self->{CONDITION} = shift;
1.89 bowersj2 2595: $self->{DIRECTION} = shift || FORWARD();
1.51 bowersj2 2596:
1.100 bowersj2 2597: # Flag: Have we started yet?
1.51 bowersj2 2598: $self->{STARTED} = 0;
2599:
2600: # Should we continue calling the recursive iterator, if any?
2601: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2602: # The recursive iterator, if any
2603: $self->{RECURSIVE_ITERATOR} = undef;
2604: # Are we recursing on a map, or a branch?
2605: $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
2606: # And the count of how deep it is, so that this iterator can keep track of
2607: # when to pick back up again.
2608: $self->{RECURSIVE_DEPTH} = 0;
2609:
2610: # For keeping track of our branches, we maintain our own stack
1.100 bowersj2 2611: $self->{STACK} = [];
1.51 bowersj2 2612:
2613: # Start with the first resource
1.89 bowersj2 2614: if ($self->{DIRECTION} == FORWARD) {
1.100 bowersj2 2615: push @{$self->{STACK}}, $self->{FIRST_RESOURCE};
1.89 bowersj2 2616: } else {
1.100 bowersj2 2617: push @{$self->{STACK}}, $self->{FINISH_RESOURCE};
1.89 bowersj2 2618: }
1.51 bowersj2 2619:
2620: bless($self);
2621: return $self;
2622: }
2623:
2624: sub next {
2625: my $self = shift;
2626:
2627: # Are we using a recursive iterator? If so, pull from that and
2628: # watch the depth; we want to resume our level at the correct time.
1.98 bowersj2 2629: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1.51 bowersj2 2630: # grab the next from the recursive iterator
2631: my $next = $self->{RECURSIVE_ITERATOR}->next();
2632:
2633: # is it a begin or end map? Update depth if so
2634: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
2635: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
2636:
2637: # Are we back at depth 0? If so, stop recursing.
2638: if ($self->{RECURSIVE_DEPTH} == 0) {
2639: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2640: }
2641:
2642: return $next;
2643: }
2644:
2645: # Is there a current resource to grab? If not, then return
1.100 bowersj2 2646: # END_MAP, which will end the iterator.
2647: if (scalar(@{$self->{STACK}}) == 0) {
2648: return $self->END_MAP();
1.51 bowersj2 2649: }
2650:
2651: # Have we not yet begun? If not, return BEGIN_MAP and
2652: # remember that we've started.
2653: if ( !$self->{STARTED} ) {
2654: $self->{STARTED} = 1;
2655: return $self->BEGIN_MAP;
2656: }
2657:
2658: # Get the next resource in the branch
1.100 bowersj2 2659: $self->{HERE} = pop @{$self->{STACK}};
1.52 bowersj2 2660:
1.100 bowersj2 2661: # remember that we've seen this, so we don't return it again later
1.51 bowersj2 2662: $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
2663:
2664: # Get the next possible resources
1.90 bowersj2 2665: my $nextUnfiltered;
1.89 bowersj2 2666: if ($self->{DIRECTION} == FORWARD()) {
1.90 bowersj2 2667: $nextUnfiltered = $self->{HERE}->getNext();
1.89 bowersj2 2668: } else {
1.90 bowersj2 2669: $nextUnfiltered = $self->{HERE}->getPrevious();
1.89 bowersj2 2670: }
1.51 bowersj2 2671: my $next = [];
2672:
2673: # filter the next possibilities to remove things we've
1.100 bowersj2 2674: # already seen.
1.51 bowersj2 2675: foreach (@$nextUnfiltered) {
1.52 bowersj2 2676: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
2677: push @$next, $_;
2678: }
1.51 bowersj2 2679: }
2680:
2681: while (@$next) {
1.100 bowersj2 2682: # copy the next possibilities over to the stack
2683: push @{$self->{STACK}}, shift @$next;
1.51 bowersj2 2684: }
2685:
2686: # If this is a map and we want to recurse down it... (not filtered out)
1.70 bowersj2 2687: if ($self->{HERE}->is_map() &&
1.63 bowersj2 2688: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
1.51 bowersj2 2689: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
2690: my $firstResource = $self->{HERE}->map_start();
2691: my $finishResource = $self->{HERE}->map_finish();
2692:
2693: $self->{RECURSIVE_ITERATOR} =
1.94 bowersj2 2694: Apache::lonnavmaps::DFSiterator->new ($self->{NAV_MAP}, $firstResource,
1.63 bowersj2 2695: $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
1.91 bowersj2 2696: $self->{CONDITION}, $self->{DIRECTION});
1.51 bowersj2 2697: }
2698:
2699: return $self->{HERE};
1.190 bowersj2 2700: }
2701:
2702: # Identical to the full iterator methods of the same name. Hate to copy/paste
2703: # but I also hate to "inherit" either iterator from the other.
2704:
2705: sub getStack {
2706: my $self=shift;
2707:
2708: my @stack;
2709:
2710: $self->populateStack(\@stack);
2711:
2712: return \@stack;
2713: }
2714:
2715: # Private method: Calls the iterators recursively to populate the stack.
2716: sub populateStack {
2717: my $self=shift;
2718: my $stack = shift;
2719:
2720: push @$stack, $self->{HERE} if ($self->{HERE});
2721:
2722: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
2723: $self->{RECURSIVE_ITERATOR}->populateStack($stack);
2724: }
1.51 bowersj2 2725: }
2726:
1.1 www 2727: 1;
1.2 www 2728:
1.51 bowersj2 2729: package Apache::lonnavmaps::resource;
2730:
2731: use Apache::lonnet;
2732:
2733: =pod
2734:
2735: =head1 Object: resource
2736:
1.174 albertel 2737: A resource object encapsulates a resource in a resource map, allowing
2738: easy manipulation of the resource, querying the properties of the
2739: resource (including user properties), and represents a reference that
2740: can be used as the canonical representation of the resource by
2741: lonnavmap clients like renderers.
2742:
2743: A resource only makes sense in the context of a navmap, as some of the
2744: data is stored in the navmap object.
2745:
2746: You will probably never need to instantiate this object directly. Use
2747: Apache::lonnavmaps::navmap, and use the "start" method to obtain the
2748: starting resource.
1.51 bowersj2 2749:
1.188 bowersj2 2750: Resource objects respect the parameter_hiddenparts, which suppresses
2751: various parts according to the wishes of the map author. As of this
2752: writing, there is no way to override this parameter, and suppressed
2753: parts will never be returned, nor will their response types or ids be
2754: stored.
2755:
1.51 bowersj2 2756: =head2 Public Members
2757:
1.174 albertel 2758: resource objects have a hash called DATA ($resourceRef->{DATA}) that
2759: you can store whatever you want in. This allows you to easily do
2760: two-pass algorithms without worrying about managing your own
2761: resource->data hash.
1.51 bowersj2 2762:
2763: =head2 Methods
2764:
2765: =over 4
2766:
1.174 albertel 2767: =item * B<new>($navmapRef, $idString):
2768:
2769: The first arg is a reference to the parent navmap object. The second
2770: is the idString of the resource itself. Very rarely, if ever, called
2771: directly. Use the nav map->getByID() method.
1.70 bowersj2 2772:
2773: =back
1.51 bowersj2 2774:
2775: =cut
2776:
2777: sub new {
2778: # magic invocation to create a class instance
2779: my $proto = shift;
2780: my $class = ref($proto) || $proto;
2781: my $self = {};
2782:
2783: $self->{NAV_MAP} = shift;
2784: $self->{ID} = shift;
2785:
2786: # Store this new resource in the parent nav map's cache.
2787: $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
1.66 bowersj2 2788: $self->{RESOURCE_ERROR} = 0;
1.51 bowersj2 2789:
2790: # A hash that can be used by two-pass algorithms to store data
2791: # about this resource in. Not used by the resource object
2792: # directly.
2793: $self->{DATA} = {};
2794:
2795: bless($self);
2796:
2797: return $self;
2798: }
2799:
1.70 bowersj2 2800: # private function: simplify the NAV_HASH lookups we keep doing
2801: # pass the name, and to automatically append my ID, pass a true val on the
2802: # second param
2803: sub navHash {
2804: my $self = shift;
2805: my $param = shift;
2806: my $id = shift;
1.115 bowersj2 2807: return $self->{NAV_MAP}->navhash($param . ($id?$self->{ID}:""));
1.70 bowersj2 2808: }
2809:
1.51 bowersj2 2810: =pod
2811:
1.70 bowersj2 2812: B<Metadata Retreival>
2813:
1.174 albertel 2814: These are methods that help you retrieve metadata about the resource:
2815: Method names are based on the fields in the compiled course
2816: representation.
1.70 bowersj2 2817:
2818: =over 4
2819:
1.174 albertel 2820: =item * B<compTitle>:
2821:
2822: Returns a "composite title", that is equal to $res->title() if the
2823: resource has a title, and is otherwise the last part of the URL (e.g.,
2824: "problem.problem").
2825:
2826: =item * B<ext>:
2827:
2828: Returns true if the resource is external.
2829:
2830: =item * B<goesto>:
2831:
2832: Returns the "goesto" value from the compiled nav map. (It is likely
2833: you want to use B<getNext> instead.)
2834:
2835: =item * B<kind>:
2836:
2837: Returns the kind of the resource from the compiled nav map.
2838:
2839: =item * B<randomout>:
1.106 bowersj2 2840:
1.174 albertel 2841: Returns true if this resource was chosen to NOT be shown to the user
2842: by the random map selection feature. In other words, this is usually
2843: false.
1.70 bowersj2 2844:
1.174 albertel 2845: =item * B<randompick>:
1.70 bowersj2 2846:
1.174 albertel 2847: Returns true for a map if the randompick feature is being used on the
2848: map. (?)
1.70 bowersj2 2849:
1.174 albertel 2850: =item * B<src>:
1.51 bowersj2 2851:
1.174 albertel 2852: Returns the source for the resource.
1.70 bowersj2 2853:
1.174 albertel 2854: =item * B<symb>:
1.70 bowersj2 2855:
1.174 albertel 2856: Returns the symb for the resource.
1.70 bowersj2 2857:
1.174 albertel 2858: =item * B<title>:
1.70 bowersj2 2859:
1.174 albertel 2860: Returns the title of the resource.
2861:
2862: =item * B<to>:
2863:
2864: Returns the "to" value from the compiled nav map. (It is likely you
2865: want to use B<getNext> instead.)
1.51 bowersj2 2866:
2867: =back
2868:
2869: =cut
2870:
2871: # These info functions can be used directly, as they don't return
2872: # resource information.
1.85 bowersj2 2873: sub comesfrom { my $self=shift; return $self->navHash("comesfrom_", 1); }
1.70 bowersj2 2874: sub ext { my $self=shift; return $self->navHash("ext_", 1) eq 'true:'; }
1.85 bowersj2 2875: sub from { my $self=shift; return $self->navHash("from_", 1); }
1.51 bowersj2 2876: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
2877: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
1.68 bowersj2 2878: sub randomout { my $self=shift; return $self->navHash("randomout_", 1); }
2879: sub randompick {
2880: my $self = shift;
2881: return $self->{NAV_MAP}->{PARM_HASH}->{$self->symb .
2882: '.0.parameter_randompick'};
2883: }
1.51 bowersj2 2884: sub src {
2885: my $self=shift;
2886: return $self->navHash("src_", 1);
2887: }
2888: sub symb {
2889: my $self=shift;
2890: (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
2891: my $symbSrc = &Apache::lonnet::declutter($self->src());
2892: return &Apache::lonnet::declutter(
2893: $self->navHash('map_id_'.$first))
2894: . '___' . $second . '___' . $symbSrc;
2895: }
1.70 bowersj2 2896: sub title { my $self=shift; return $self->navHash("title_", 1); }
2897: sub to { my $self=shift; return $self->navHash("to_", 1); }
1.106 bowersj2 2898: sub compTitle {
2899: my $self = shift;
2900: my $title = $self->title();
1.176 www 2901: $title=~s/\&colon\;/\:/gs;
1.106 bowersj2 2902: if (!$title) {
2903: $title = $self->src();
2904: $title = substr($title, rindex($title, '/') + 1);
2905: }
2906: return $title;
2907: }
1.70 bowersj2 2908: =pod
2909:
2910: B<Predicate Testing the Resource>
2911:
2912: These methods are shortcuts to deciding if a given resource has a given property.
2913:
2914: =over 4
2915:
1.174 albertel 2916: =item * B<is_map>:
2917:
2918: Returns true if the resource is a map type.
2919:
2920: =item * B<is_problem>:
1.70 bowersj2 2921:
1.174 albertel 2922: Returns true if the resource is a problem type, false
2923: otherwise. (Looks at the extension on the src field; might need more
2924: to work correctly.)
1.70 bowersj2 2925:
1.174 albertel 2926: =item * B<is_page>:
1.70 bowersj2 2927:
1.174 albertel 2928: Returns true if the resource is a page.
2929:
2930: =item * B<is_sequence>:
2931:
2932: Returns true if the resource is a sequence.
1.70 bowersj2 2933:
2934: =back
2935:
2936: =cut
2937:
2938:
2939: sub is_html {
1.51 bowersj2 2940: my $self=shift;
2941: my $src = $self->src();
1.70 bowersj2 2942: return ($src =~ /html$/);
1.51 bowersj2 2943: }
1.70 bowersj2 2944: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
2945: sub is_page {
1.51 bowersj2 2946: my $self=shift;
2947: my $src = $self->src();
1.70 bowersj2 2948: return ($src =~ /page$/);
1.51 bowersj2 2949: }
1.70 bowersj2 2950: sub is_problem {
1.51 bowersj2 2951: my $self=shift;
2952: my $src = $self->src();
1.70 bowersj2 2953: return ($src =~ /problem$/);
1.51 bowersj2 2954: }
1.70 bowersj2 2955: sub is_sequence {
1.51 bowersj2 2956: my $self=shift;
2957: my $src = $self->src();
1.70 bowersj2 2958: return ($src =~ /sequence$/);
1.51 bowersj2 2959: }
2960:
1.101 bowersj2 2961: # Private method: Shells out to the parmval in the nav map, handler parts.
1.51 bowersj2 2962: sub parmval {
2963: my $self = shift;
2964: my $what = shift;
1.185 bowersj2 2965: my $part = shift;
2966: if (!defined($part)) {
2967: $part = '0';
2968: }
1.51 bowersj2 2969: return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
2970: }
2971:
1.70 bowersj2 2972: =pod
2973:
2974: B<Map Methods>
2975:
1.174 albertel 2976: These methods are useful for getting information about the map
2977: properties of the resource, if the resource is a map (B<is_map>).
1.70 bowersj2 2978:
2979: =over 4
2980:
1.174 albertel 2981: =item * B<map_finish>:
2982:
2983: Returns a reference to a resource object corresponding to the finish
2984: resource of the map.
1.70 bowersj2 2985:
1.174 albertel 2986: =item * B<map_pc>:
1.70 bowersj2 2987:
1.174 albertel 2988: Returns the pc value of the map, which is the first number that
2989: appears in the resource ID of the resources in the map, and is the
2990: number that appears around the middle of the symbs of the resources in
2991: that map.
1.70 bowersj2 2992:
1.174 albertel 2993: =item * B<map_start>:
2994:
2995: Returns a reference to a resource object corresponding to the start
2996: resource of the map.
2997:
2998: =item * B<map_type>:
2999:
3000: Returns a string with the type of the map in it.
1.70 bowersj2 3001:
3002: =back
1.51 bowersj2 3003:
1.70 bowersj2 3004: =cut
1.51 bowersj2 3005:
3006: sub map_finish {
3007: my $self = shift;
3008: my $src = $self->src();
1.144 bowersj2 3009: $src = Apache::lonnet::clutter($src);
1.51 bowersj2 3010: my $res = $self->navHash("map_finish_$src", 0);
3011: $res = $self->{NAV_MAP}->getById($res);
3012: return $res;
3013: }
1.70 bowersj2 3014: sub map_pc {
3015: my $self = shift;
3016: my $src = $self->src();
3017: return $self->navHash("map_pc_$src", 0);
3018: }
1.51 bowersj2 3019: sub map_start {
3020: my $self = shift;
3021: my $src = $self->src();
1.144 bowersj2 3022: $src = Apache::lonnet::clutter($src);
1.51 bowersj2 3023: my $res = $self->navHash("map_start_$src", 0);
3024: $res = $self->{NAV_MAP}->getById($res);
3025: return $res;
3026: }
3027: sub map_type {
3028: my $self = shift;
3029: my $pc = $self->map_pc();
3030: return $self->navHash("map_type_$pc", 0);
3031: }
3032:
3033:
3034:
3035: #####
3036: # Property queries
3037: #####
3038:
3039: # These functions will be responsible for returning the CORRECT
3040: # VALUE for the parameter, no matter what. So while they may look
3041: # like direct calls to parmval, they can be more then that.
3042: # So, for instance, the duedate function should use the "duedatetype"
3043: # information, rather then the resource object user.
3044:
3045: =pod
3046:
3047: =head2 Resource Parameters
3048:
1.174 albertel 3049: In order to use the resource parameters correctly, the nav map must
3050: have been instantiated with genCourseAndUserOptions set to true, so
3051: the courseopt and useropt is read correctly. Then, you can call these
3052: functions to get the relevant parameters for the resource. Each
3053: function defaults to part "0", but can be directed to another part by
3054: passing the part as the parameter.
3055:
3056: These methods are responsible for getting the parameter correct, not
3057: merely reflecting the contents of the GDBM hashes. As we move towards
3058: dates relative to other dates, these methods should be updated to
3059: reflect that. (Then, anybody using these methods will not have to update
3060: their code.)
3061:
3062: =over 4
3063:
3064: =item * B<acc>:
3065:
3066: Get the Client IP/Name Access Control information.
1.51 bowersj2 3067:
1.174 albertel 3068: =item * B<answerdate>:
1.51 bowersj2 3069:
1.174 albertel 3070: Get the answer-reveal date for the problem.
3071:
3072: =item * B<duedate>:
3073:
3074: Get the due date for the problem.
3075:
3076: =item * B<tries>:
3077:
3078: Get the number of tries the student has used on the problem.
3079:
3080: =item * B<maxtries>:
3081:
3082: Get the number of max tries allowed.
3083:
3084: =item * B<opendate>:
1.51 bowersj2 3085:
1.174 albertel 3086: Get the open date for the problem.
1.51 bowersj2 3087:
1.174 albertel 3088: =item * B<sig>:
1.51 bowersj2 3089:
1.174 albertel 3090: Get the significant figures setting.
1.51 bowersj2 3091:
1.174 albertel 3092: =item * B<tol>:
1.51 bowersj2 3093:
1.174 albertel 3094: Get the tolerance for the problem.
1.51 bowersj2 3095:
1.174 albertel 3096: =item * B<tries>:
1.51 bowersj2 3097:
1.174 albertel 3098: Get the number of tries the user has already used on the problem.
1.51 bowersj2 3099:
1.174 albertel 3100: =item * B<type>:
1.51 bowersj2 3101:
1.174 albertel 3102: Get the question type for the problem.
1.70 bowersj2 3103:
1.174 albertel 3104: =item * B<weight>:
1.51 bowersj2 3105:
1.174 albertel 3106: Get the weight for the problem.
1.51 bowersj2 3107:
3108: =back
3109:
3110: =cut
3111:
3112: sub acc {
3113: (my $self, my $part) = @_;
3114: return $self->parmval("acc", $part);
3115: }
3116: sub answerdate {
3117: (my $self, my $part) = @_;
3118: # Handle intervals
3119: if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
3120: return $self->duedate($part) +
3121: $self->parmval("answerdate", $part);
3122: }
3123: return $self->parmval("answerdate", $part);
1.106 bowersj2 3124: }
1.108 bowersj2 3125: sub awarded { my $self = shift; return $self->queryRestoreHash('awarded', shift); }
1.51 bowersj2 3126: sub duedate {
3127: (my $self, my $part) = @_;
3128: return $self->parmval("duedate", $part);
3129: }
3130: sub maxtries {
3131: (my $self, my $part) = @_;
3132: return $self->parmval("maxtries", $part);
3133: }
3134: sub opendate {
3135: (my $self, my $part) = @_;
3136: if ($self->parmval("opendate.type", $part) eq 'date_interval') {
3137: return $self->duedate($part) -
3138: $self->parmval("opendate", $part);
3139: }
3140: return $self->parmval("opendate");
3141: }
1.185 bowersj2 3142: sub problemstatus {
3143: (my $self, my $part) = @_;
3144: return $self->parmval("problemstatus", $part);
3145: }
1.51 bowersj2 3146: sub sig {
3147: (my $self, my $part) = @_;
3148: return $self->parmval("sig", $part);
3149: }
3150: sub tol {
3151: (my $self, my $part) = @_;
3152: return $self->parmval("tol", $part);
3153: }
1.108 bowersj2 3154: sub tries {
3155: my $self = shift;
3156: my $tries = $self->queryRestoreHash('tries', shift);
3157: if (!defined($tries)) { return '0';}
1.51 bowersj2 3158: return $tries;
3159: }
1.70 bowersj2 3160: sub type {
3161: (my $self, my $part) = @_;
3162: return $self->parmval("type", $part);
3163: }
1.109 bowersj2 3164: sub weight {
3165: my $self = shift; my $part = shift;
1.70 bowersj2 3166: return $self->parmval("weight", $part);
3167: }
1.51 bowersj2 3168:
3169: # Multiple things need this
3170: sub getReturnHash {
3171: my $self = shift;
3172:
3173: if (!defined($self->{RETURN_HASH})) {
1.84 bowersj2 3174: my %tmpHash = &Apache::lonnet::restore($self->symb());
1.51 bowersj2 3175: $self->{RETURN_HASH} = \%tmpHash;
3176: }
3177: }
3178:
3179: ######
3180: # Status queries
3181: ######
3182:
3183: # These methods query the status of problems.
3184:
3185: # If we need to count parts, this function determines the number of
3186: # parts from the metadata. When called, it returns a reference to a list
3187: # of strings corresponding to the parts. (Thus, using it in a scalar context
3188: # tells you how many parts you have in the problem:
3189: # $partcount = scalar($resource->countParts());
3190: # Don't use $self->{PARTS} directly because you don't know if it's been
3191: # computed yet.
3192:
3193: =pod
3194:
3195: =head2 Resource misc
3196:
3197: Misc. functions for the resource.
3198:
3199: =over 4
3200:
1.174 albertel 3201: =item * B<hasDiscussion>:
1.59 bowersj2 3202:
1.174 albertel 3203: Returns a false value if there has been discussion since the user last
3204: logged in, true if there has. Always returns false if the discussion
3205: data was not extracted when the nav map was constructed.
3206:
3207: =item * B<getFeedback>:
3208:
3209: Gets the feedback for the resource and returns the raw feedback string
3210: for the resource, or the null string if there is no feedback or the
3211: email data was not extracted when the nav map was constructed. Usually
3212: used like this:
1.59 bowersj2 3213:
3214: for (split(/\,/, $res->getFeedback())) {
3215: my $link = &Apache::lonnet::escape($_);
3216: ...
3217:
3218: and use the link as appropriate.
3219:
3220: =cut
3221:
3222: sub hasDiscussion {
3223: my $self = shift;
3224: return $self->{NAV_MAP}->hasDiscussion($self->symb());
3225: }
3226:
3227: sub getFeedback {
3228: my $self = shift;
1.124 bowersj2 3229: my $source = $self->src();
1.125 bowersj2 3230: if ($source =~ /^\/res\//) { $source = substr $source, 5; }
1.124 bowersj2 3231: return $self->{NAV_MAP}->getFeedback($source);
3232: }
3233:
3234: sub getErrors {
3235: my $self = shift;
3236: my $source = $self->src();
3237: if ($source =~ /^\/res\//) { $source = substr $source, 5; }
3238: return $self->{NAV_MAP}->getErrors($source);
1.59 bowersj2 3239: }
3240:
3241: =pod
3242:
1.174 albertel 3243: =item * B<parts>():
1.51 bowersj2 3244:
1.174 albertel 3245: Returns a list reference containing sorted strings corresponding to
1.193 bowersj2 3246: each part of the problem. Single part problems have only a part '0'.
3247: Multipart problems do not return their part '0', since they typically
3248: do not really matter.
1.174 albertel 3249:
3250: =item * B<countParts>():
3251:
3252: Returns the number of parts of the problem a student can answer. Thus,
3253: for single part problems, returns 1. For multipart, it returns the
1.193 bowersj2 3254: number of parts in the problem, not including psuedo-part 0.
1.51 bowersj2 3255:
1.192 bowersj2 3256: =item * B<multipart>():
3257:
1.193 bowersj2 3258: Returns true if the problem is multipart, false otherwise. Use this instead
3259: of countParts if all you want is multipart/not multipart.
1.192 bowersj2 3260:
1.187 bowersj2 3261: =item * B<responseType>($part):
3262:
3263: Returns the response type of the part, without the word "response" on the
3264: end. Example return values: 'string', 'essay', 'numeric', etc.
3265:
1.193 bowersj2 3266: =item * B<responseIds>($part):
1.187 bowersj2 3267:
1.193 bowersj2 3268: Retreives the response IDs for the given part as an array reference containing
3269: strings naming the response IDs. This may be empty.
1.187 bowersj2 3270:
1.51 bowersj2 3271: =back
3272:
3273: =cut
3274:
3275: sub parts {
3276: my $self = shift;
3277:
1.192 bowersj2 3278: if ($self->ext) { return []; }
1.67 bowersj2 3279:
1.51 bowersj2 3280: $self->extractParts();
3281: return $self->{PARTS};
3282: }
3283:
3284: sub countParts {
3285: my $self = shift;
3286:
3287: my $parts = $self->parts();
1.191 bowersj2 3288:
3289: # If I left this here, then it's not necessary.
3290: #my $delta = 0;
3291: #for my $part (@$parts) {
3292: # if ($part eq '0') { $delta--; }
3293: #}
1.66 bowersj2 3294:
3295: if ($self->{RESOURCE_ERROR}) {
3296: return 0;
3297: }
3298:
1.191 bowersj2 3299: return scalar(@{$parts}); # + $delta;
1.192 bowersj2 3300: }
3301:
3302: sub multipart {
3303: my $self = shift;
3304: return $self->countParts() > 1;
1.51 bowersj2 3305: }
3306:
1.187 bowersj2 3307: sub responseType {
1.184 bowersj2 3308: my $self = shift;
3309: my $part = shift;
3310:
3311: $self->extractParts();
1.187 bowersj2 3312: return $self->{RESPONSE_TYPE}->{$part};
3313: }
3314:
1.193 bowersj2 3315: sub responseIds {
1.187 bowersj2 3316: my $self = shift;
3317: my $part = shift;
3318:
3319: $self->extractParts();
3320: return $self->{RESPONSE_IDS}->{$part};
1.184 bowersj2 3321: }
3322:
3323: # Private function: Extracts the parts information, both part names and
1.187 bowersj2 3324: # part types, and saves it.
1.51 bowersj2 3325: sub extractParts {
3326: my $self = shift;
3327:
1.153 bowersj2 3328: return if (defined($self->{PARTS}));
1.67 bowersj2 3329: return if ($self->ext);
1.51 bowersj2 3330:
3331: $self->{PARTS} = [];
3332:
1.181 bowersj2 3333: my %parts;
3334:
1.82 bowersj2 3335: # Retrieve part count, if this is a problem
3336: if ($self->is_problem()) {
1.152 matthew 3337: my $metadata = &Apache::lonnet::metadata($self->src(), 'packages');
1.82 bowersj2 3338: if (!$metadata) {
3339: $self->{RESOURCE_ERROR} = 1;
3340: $self->{PARTS} = [];
1.184 bowersj2 3341: $self->{PART_TYPE} = {};
1.82 bowersj2 3342: return;
3343: }
3344: foreach (split(/\,/,$metadata)) {
1.152 matthew 3345: if ($_ =~ /^part_(.*)$/) {
1.147 bowersj2 3346: my $part = $1;
1.183 bowersj2 3347: # This floods the logs if it blows up
3348: if (defined($parts{$part})) {
3349: Apache::lonnet::logthis("$part multiply defined in metadata for " . $self->symb());
3350: }
1.181 bowersj2 3351:
1.147 bowersj2 3352: # check to see if part is turned off.
1.181 bowersj2 3353:
3354: if (!Apache::loncommon::check_if_partid_hidden($part, $self->symb())) {
3355: $parts{$part} = 1;
1.147 bowersj2 3356: }
1.82 bowersj2 3357: }
1.51 bowersj2 3358: }
1.82 bowersj2 3359:
3360:
1.181 bowersj2 3361: my @sortedParts = sort keys %parts;
1.82 bowersj2 3362: $self->{PARTS} = \@sortedParts;
1.187 bowersj2 3363:
3364: my %responseIdHash;
3365: my %responseTypeHash;
3366:
1.189 bowersj2 3367:
3368: # Init the responseIdHash
3369: foreach (@{$self->{PARTS}}) {
3370: $responseIdHash{$_} = [];
3371: }
3372:
1.187 bowersj2 3373: # Now, the unfortunate thing about this is that parts, part name, and
3374: # response if are delimited by underscores, but both the part
3375: # name and response id can themselves have underscores in them.
3376: # So we have to use our knowlege of part names to figure out
3377: # where the part names begin and end, and even then, it is possible
3378: # to construct ambiguous situations.
3379: foreach (split /,/, $metadata) {
3380: if ($_ =~ /^([a-zA-Z]+)response_(.*)/) {
3381: my $responseType = $1;
3382: my $partStuff = $2;
3383: my $partIdSoFar = '';
3384: my @partChunks = split /_/, $partStuff;
3385: my $i = 0;
3386:
3387: for ($i = 0; $i < scalar(@partChunks); $i++) {
3388: if ($partIdSoFar) { $partIdSoFar .= '_'; }
3389: $partIdSoFar .= $partChunks[$i];
3390: if ($parts{$partIdSoFar}) {
3391: my @otherChunks = @partChunks[$i+1..$#partChunks];
3392: my $responseId = join('_', @otherChunks);
1.188 bowersj2 3393: push @{$responseIdHash{$partIdSoFar}}, $responseId;
1.187 bowersj2 3394: $responseTypeHash{$partIdSoFar} = $responseType;
3395: last;
3396: }
3397: }
3398: }
3399: }
3400:
3401: $self->{RESPONSE_IDS} = \%responseIdHash;
3402: $self->{RESPONSE_TYPES} = \%responseTypeHash;
1.154 bowersj2 3403: }
3404:
1.51 bowersj2 3405: return;
3406: }
3407:
3408: =pod
3409:
3410: =head2 Resource Status
3411:
1.174 albertel 3412: Problem resources have status information, reflecting their various
3413: dates and completion statuses.
1.51 bowersj2 3414:
1.174 albertel 3415: There are two aspects to the status: the date-related information and
3416: the completion information.
1.51 bowersj2 3417:
1.174 albertel 3418: Idiomatic usage of these two methods would probably look something
3419: like
1.51 bowersj2 3420:
3421: foreach ($resource->parts()) {
3422: my $dateStatus = $resource->getDateStatus($_);
3423: my $completionStatus = $resource->getCompletionStatus($_);
3424:
1.70 bowersj2 3425: or
3426:
3427: my $status = $resource->status($_);
3428:
1.51 bowersj2 3429: ... use it here ...
3430: }
3431:
1.174 albertel 3432: Which you use depends on exactly what you are looking for. The
3433: status() function has been optimized for the nav maps display and may
3434: not precisely match what you need elsewhere.
1.101 bowersj2 3435:
1.174 albertel 3436: The symbolic constants shown below can be accessed through the
3437: resource object: C<$res->OPEN>.
1.101 bowersj2 3438:
1.51 bowersj2 3439: =over 4
3440:
1.174 albertel 3441: =item * B<getDateStatus>($part):
3442:
3443: ($part defaults to 0). A convenience function that returns a symbolic
3444: constant telling you about the date status of the part. The possible
3445: return values are:
1.51 bowersj2 3446:
3447: =back
3448:
3449: B<Date Codes>
3450:
3451: =over 4
3452:
1.174 albertel 3453: =item * B<OPEN_LATER>:
3454:
3455: The problem will be opened later.
3456:
3457: =item * B<OPEN>:
3458:
3459: Open and not yet due.
3460:
1.51 bowersj2 3461:
1.174 albertel 3462: =item * B<PAST_DUE_ANSWER_LATER>:
1.51 bowersj2 3463:
1.174 albertel 3464: The due date has passed, but the answer date has not yet arrived.
1.59 bowersj2 3465:
1.174 albertel 3466: =item * B<PAST_DUE_NO_ANSWER>:
1.54 bowersj2 3467:
1.174 albertel 3468: The due date has passed and there is no answer opening date set.
1.51 bowersj2 3469:
1.174 albertel 3470: =item * B<ANSWER_OPEN>:
1.51 bowersj2 3471:
1.174 albertel 3472: The answer date is here.
3473:
3474: =item * B<NETWORK_FAILURE>:
3475:
3476: The information is unknown due to network failure.
1.51 bowersj2 3477:
3478: =back
3479:
3480: =cut
3481:
3482: # Apparently the compiler optimizes these into constants automatically
1.54 bowersj2 3483: sub OPEN_LATER { return 0; }
3484: sub OPEN { return 1; }
3485: sub PAST_DUE_NO_ANSWER { return 2; }
3486: sub PAST_DUE_ANSWER_LATER { return 3; }
3487: sub ANSWER_OPEN { return 4; }
3488: sub NOTHING_SET { return 5; }
3489: sub NETWORK_FAILURE { return 100; }
3490:
3491: # getDateStatus gets the date status for a given problem part.
3492: # Because answer date, due date, and open date are fully independent
3493: # (i.e., it is perfectly possible to *only* have an answer date),
3494: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
3495: # (past, future, none given). This function handles this with a decision
3496: # tree. Read the comments to follow the decision tree.
1.51 bowersj2 3497:
3498: sub getDateStatus {
3499: my $self = shift;
3500: my $part = shift;
3501: $part = "0" if (!defined($part));
1.54 bowersj2 3502:
3503: # Always return network failure if there was one.
1.51 bowersj2 3504: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
3505:
3506: my $now = time();
3507:
1.53 bowersj2 3508: my $open = $self->opendate($part);
3509: my $due = $self->duedate($part);
3510: my $answer = $self->answerdate($part);
3511:
3512: if (!$open && !$due && !$answer) {
3513: # no data on the problem at all
3514: # should this be the same as "open later"? think multipart.
3515: return $self->NOTHING_SET;
3516: }
1.59 bowersj2 3517: if (!$open || $now < $open) {return $self->OPEN_LATER}
3518: if (!$due || $now < $due) {return $self->OPEN}
3519: if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
3520: if ($answer) { return $self->ANSWER_OPEN; }
1.54 bowersj2 3521: return PAST_DUE_NO_ANSWER;
1.51 bowersj2 3522: }
3523:
3524: =pod
3525:
3526: B<>
3527:
3528: =over 4
3529:
1.174 albertel 3530: =item * B<getCompletionStatus>($part):
1.51 bowersj2 3531:
1.174 albertel 3532: ($part defaults to 0.) A convenience function that returns a symbolic
3533: constant telling you about the completion status of the part, with the
3534: following possible results:
3535:
3536: =back
1.51 bowersj2 3537:
3538: B<Completion Codes>
3539:
3540: =over 4
3541:
1.174 albertel 3542: =item * B<NOT_ATTEMPTED>:
3543:
3544: Has not been attempted at all.
3545:
3546: =item * B<INCORRECT>:
3547:
3548: Attempted, but wrong by student.
1.51 bowersj2 3549:
1.174 albertel 3550: =item * B<INCORRECT_BY_OVERRIDE>:
1.51 bowersj2 3551:
1.174 albertel 3552: Attempted, but wrong by instructor override.
1.51 bowersj2 3553:
1.174 albertel 3554: =item * B<CORRECT>:
1.51 bowersj2 3555:
1.174 albertel 3556: Correct or correct by instructor.
1.51 bowersj2 3557:
1.174 albertel 3558: =item * B<CORRECT_BY_OVERRIDE>:
1.51 bowersj2 3559:
1.174 albertel 3560: Correct by instructor override.
1.51 bowersj2 3561:
1.174 albertel 3562: =item * B<EXCUSED>:
3563:
3564: Excused. Not yet implemented.
3565:
3566: =item * B<NETWORK_FAILURE>:
3567:
3568: Information not available due to network failure.
3569:
3570: =item * B<ATTEMPTED>:
3571:
3572: Attempted, and not yet graded.
1.69 bowersj2 3573:
1.51 bowersj2 3574: =back
3575:
3576: =cut
3577:
1.53 bowersj2 3578: sub NOT_ATTEMPTED { return 10; }
3579: sub INCORRECT { return 11; }
3580: sub INCORRECT_BY_OVERRIDE { return 12; }
3581: sub CORRECT { return 13; }
3582: sub CORRECT_BY_OVERRIDE { return 14; }
3583: sub EXCUSED { return 15; }
1.69 bowersj2 3584: sub ATTEMPTED { return 16; }
1.51 bowersj2 3585:
3586: sub getCompletionStatus {
3587: my $self = shift;
3588: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
3589:
1.108 bowersj2 3590: my $status = $self->queryRestoreHash('solved', shift);
1.51 bowersj2 3591:
3592: # Left as seperate if statements in case we ever do more with this
3593: if ($status eq 'correct_by_student') {return $self->CORRECT;}
3594: if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
3595: if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
3596: if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
3597: if ($status eq 'excused') {return $self->EXCUSED; }
1.69 bowersj2 3598: if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
1.51 bowersj2 3599: return $self->NOT_ATTEMPTED;
1.108 bowersj2 3600: }
3601:
3602: sub queryRestoreHash {
3603: my $self = shift;
3604: my $hashentry = shift;
3605: my $part = shift;
1.185 bowersj2 3606: $part = "0" if (!defined($part) || $part eq '');
1.108 bowersj2 3607: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
3608:
3609: $self->getReturnHash();
3610:
3611: return $self->{RETURN_HASH}->{'resource.'.$part.'.'.$hashentry};
1.51 bowersj2 3612: }
3613:
3614: =pod
3615:
3616: B<Composite Status>
3617:
1.174 albertel 3618: Along with directly returning the date or completion status, the
3619: resource object includes a convenience function B<status>() that will
3620: combine the two status tidbits into one composite status that can
1.191 bowersj2 3621: represent the status of the resource as a whole. This method represents
3622: the concept of the thing we want to display to the user on the nav maps
3623: screen, which is a combination of completion and open status. The precise logic is
1.174 albertel 3624: documented in the comments of the status method. The following results
3625: may be returned, all available as methods on the resource object
1.185 bowersj2 3626: ($res->NETWORK_FAILURE): In addition to the return values that match
3627: the date or completion status, this function can return "ANSWER_SUBMITTED"
3628: if that problemstatus parameter value is set to No, suppressing the
3629: incorrect/correct feedback.
1.51 bowersj2 3630:
3631: =over 4
3632:
1.174 albertel 3633: =item * B<NETWORK_FAILURE>:
3634:
3635: The network has failed and the information is not available.
1.51 bowersj2 3636:
1.174 albertel 3637: =item * B<NOTHING_SET>:
1.53 bowersj2 3638:
1.174 albertel 3639: No dates have been set for this problem (part) at all. (Because only
3640: certain parts of a multi-part problem may be assigned, this can not be
3641: collapsed into "open later", as we do not know a given part will EVER
3642: be opened. For single part, this is the same as "OPEN_LATER".)
1.51 bowersj2 3643:
1.174 albertel 3644: =item * B<CORRECT>:
1.51 bowersj2 3645:
1.174 albertel 3646: For any reason at all, the part is considered correct.
1.54 bowersj2 3647:
1.174 albertel 3648: =item * B<EXCUSED>:
1.51 bowersj2 3649:
1.174 albertel 3650: For any reason at all, the problem is excused.
1.51 bowersj2 3651:
1.174 albertel 3652: =item * B<PAST_DUE_NO_ANSWER>:
1.51 bowersj2 3653:
1.174 albertel 3654: The problem is past due, not considered correct, and no answer date is
3655: set.
1.51 bowersj2 3656:
1.174 albertel 3657: =item * B<PAST_DUE_ANSWER_LATER>:
1.51 bowersj2 3658:
1.174 albertel 3659: The problem is past due, not considered correct, and an answer date in
3660: the future is set.
1.51 bowersj2 3661:
1.174 albertel 3662: =item * B<ANSWER_OPEN>:
3663:
3664: The problem is past due, not correct, and the answer is now available.
3665:
3666: =item * B<OPEN_LATER>:
3667:
3668: The problem is not yet open.
3669:
3670: =item * B<TRIES_LEFT>:
3671:
3672: The problem is open, has been tried, is not correct, but there are
3673: tries left.
3674:
3675: =item * B<INCORRECT>:
3676:
3677: The problem is open, and all tries have been used without getting the
3678: correct answer.
3679:
3680: =item * B<OPEN>:
3681:
3682: The item is open and not yet tried.
3683:
3684: =item * B<ATTEMPTED>:
3685:
3686: The problem has been attempted.
1.69 bowersj2 3687:
1.185 bowersj2 3688: =item * B<ANSWER_SUBMITTED>:
3689:
3690: An answer has been submitted, but the student should not see it.
3691:
1.51 bowersj2 3692: =back
3693:
3694: =cut
3695:
1.185 bowersj2 3696: sub TRIES_LEFT { return 20; }
3697: sub ANSWER_SUBMITTED { return 21; }
1.51 bowersj2 3698:
3699: sub status {
3700: my $self = shift;
3701: my $part = shift;
3702: if (!defined($part)) { $part = "0"; }
3703: my $completionStatus = $self->getCompletionStatus($part);
3704: my $dateStatus = $self->getDateStatus($part);
3705:
3706: # What we have is a two-dimensional matrix with 4 entries on one
3707: # dimension and 5 entries on the other, which we want to colorize,
1.54 bowersj2 3708: # plus network failure and "no date data at all".
1.51 bowersj2 3709:
1.53 bowersj2 3710: if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51 bowersj2 3711:
1.186 bowersj2 3712: my $suppressFeedback = lc($self->parmval("problemstatus", $part)) eq 'no';
1.185 bowersj2 3713:
1.51 bowersj2 3714: # There are a few whole rows we can dispose of:
1.53 bowersj2 3715: if ($completionStatus == CORRECT ||
3716: $completionStatus == CORRECT_BY_OVERRIDE ) {
1.185 bowersj2 3717: return $suppressFeedback? ANSWER_SUBMITTED : CORRECT;
1.69 bowersj2 3718: }
3719:
3720: if ($completionStatus == ATTEMPTED) {
3721: return ATTEMPTED;
1.53 bowersj2 3722: }
3723:
3724: # If it's EXCUSED, then return that no matter what
3725: if ($completionStatus == EXCUSED) {
3726: return EXCUSED;
1.51 bowersj2 3727: }
3728:
1.53 bowersj2 3729: if ($dateStatus == NOTHING_SET) {
3730: return NOTHING_SET;
1.51 bowersj2 3731: }
3732:
1.69 bowersj2 3733: # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
3734: # by 4 matrix (date statuses).
1.51 bowersj2 3735:
1.54 bowersj2 3736: if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59 bowersj2 3737: $dateStatus == PAST_DUE_NO_ANSWER ) {
1.54 bowersj2 3738: return $dateStatus;
1.51 bowersj2 3739: }
3740:
1.53 bowersj2 3741: if ($dateStatus == ANSWER_OPEN) {
3742: return ANSWER_OPEN;
1.51 bowersj2 3743: }
3744:
3745: # Now: (incorrect, incorrect_override, not_attempted) x
3746: # (open_later), (open)
3747:
1.53 bowersj2 3748: if ($dateStatus == OPEN_LATER) {
3749: return OPEN_LATER;
1.51 bowersj2 3750: }
3751:
3752: # If it's WRONG...
1.53 bowersj2 3753: if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51 bowersj2 3754: # and there are TRIES LEFT:
1.79 bowersj2 3755: if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
1.53 bowersj2 3756: return TRIES_LEFT;
1.51 bowersj2 3757: }
1.185 bowersj2 3758: return $suppressFeedback ? ANSWER_SUBMITTED : INCORRECT; # otherwise, return orange; student can't fix this
1.51 bowersj2 3759: }
3760:
3761: # Otherwise, it's untried and open
1.53 bowersj2 3762: return OPEN;
1.191 bowersj2 3763: }
3764:
3765: =pod
3766:
3767: B<Completable>
3768:
3769: The completable method represents the concept of I<whether the student can
3770: currently do the problem>. If the student can do the problem, which means
3771: that it is open, there are tries left, and if the problem is manually graded
3772: or the grade is suppressed via problemstatus, the student has not tried it
3773: yet, then the method returns 1. Otherwise, it returns 0, to indicate that
3774: either the student has tried it and there is no feedback, or that for
3775: some reason it is no longer completable (not open yet, successfully completed,
3776: out of tries, etc.). As an example, this is used as the filter for the
3777: "Uncompleted Homework" option for the nav maps.
3778:
3779: If this does not quite meet your needs, do not fiddle with it (unless you are
3780: fixing it to better match the student's conception of "completable" because
3781: it's broken somehow)... make a new method.
3782:
3783: =cut
3784:
3785: sub completable {
3786: my $self = shift;
3787: if (!$self->is_problem()) { return 0; }
3788: my $partCount = $self->countParts();
3789:
3790: foreach my $part (@{$self->parts()}) {
3791: if ($part eq '0' && $partCount != 1) { next; }
3792: my $status = $self->status($part);
3793: # "If any of the parts are open, or have tries left (implies open),
3794: # and it is not "attempted" (manually graded problem), it is
3795: # not "complete"
3796: if (!(($status == OPEN() || $status == TRIES_LEFT())
3797: && $self->getCompletionStatus($part) != ATTEMPTED()
3798: && $status != ANSWER_SUBMITTED())) {
3799: return 0;
3800: }
3801: }
3802:
3803: # If all the parts were complete, so was this problem.
3804: return 1;
1.51 bowersj2 3805: }
3806:
3807: =pod
3808:
3809: =head2 Resource/Nav Map Navigation
3810:
3811: =over 4
3812:
1.174 albertel 3813: =item * B<getNext>():
3814:
3815: Retreive an array of the possible next resources after this
3816: one. Always returns an array, even in the one- or zero-element case.
3817:
3818: =item * B<getPrevious>():
1.85 bowersj2 3819:
1.174 albertel 3820: Retreive an array of the possible previous resources from this
3821: one. Always returns an array, even in the one- or zero-element case.
1.51 bowersj2 3822:
3823: =cut
3824:
3825: sub getNext {
3826: my $self = shift;
3827: my @branches;
3828: my $to = $self->to();
1.85 bowersj2 3829: foreach my $branch ( split(/,/, $to) ) {
1.51 bowersj2 3830: my $choice = $self->{NAV_MAP}->getById($branch);
3831: my $next = $choice->goesto();
3832: $next = $self->{NAV_MAP}->getById($next);
3833:
1.131 bowersj2 3834: push @branches, $next;
1.85 bowersj2 3835: }
3836: return \@branches;
3837: }
3838:
3839: sub getPrevious {
3840: my $self = shift;
3841: my @branches;
3842: my $from = $self->from();
3843: foreach my $branch ( split /,/, $from) {
3844: my $choice = $self->{NAV_MAP}->getById($branch);
3845: my $prev = $choice->comesfrom();
3846: $prev = $self->{NAV_MAP}->getById($prev);
3847:
1.131 bowersj2 3848: push @branches, $prev;
1.51 bowersj2 3849: }
3850: return \@branches;
1.131 bowersj2 3851: }
3852:
3853: sub browsePriv {
3854: my $self = shift;
3855: if (defined($self->{BROWSE_PRIV})) {
3856: return $self->{BROWSE_PRIV};
3857: }
3858:
3859: $self->{BROWSE_PRIV} = &Apache::lonnet::allowed('bre', $self->src());
1.51 bowersj2 3860: }
3861:
3862: =pod
1.2 www 3863:
1.51 bowersj2 3864: =back
1.2 www 3865:
1.51 bowersj2 3866: =cut
1.2 www 3867:
1.51 bowersj2 3868: 1;
1.2 www 3869:
1.51 bowersj2 3870: __END__
1.2 www 3871:
3872:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>