Annotation of loncom/interface/lonnavmaps.pm, revision 1.195
1.2 www 1: # The LearningOnline Network with CAPA
2: # Navigate Maps Handler
1.1 www 3: #
1.195 ! bowersj2 4: # $Id: lonnavmaps.pm,v 1.194 2003/05/29 17:39:41 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: }
967: }
968:
969: return "<td width=\"75\" align=\"left\" valign=\"center\">$discussionHTML$feedbackHTML$errorHTML </td>";
970:
1.133 bowersj2 971: }
972: sub render_quick_status {
973: my ($resource, $part, $params) = @_;
1.135 bowersj2 974: my $result = "";
975: my $firstDisplayed = !$params->{'condensed'} &&
976: $params->{'multipart'} && $part eq "0";
977:
978: my $link = $params->{"resourceLink"};
979: my $linkopen = "<a href='$link'>";
980: my $linkclose = "</a>";
981:
982: if ($resource->is_problem() &&
983: !$firstDisplayed) {
984: my $icon = $statusIconMap{$resource->status($part)};
985: my $alt = $iconAltTags{$icon};
986: if ($icon) {
987: $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";
988: } else {
989: $result .= "<td width='30'> </td>\n";
990: }
991: } else { # not problem, no icon
992: $result .= "<td width='30'> </td>\n";
993: }
994:
995: return $result;
1.133 bowersj2 996: }
997: sub render_long_status {
998: my ($resource, $part, $params) = @_;
1.136 bowersj2 999: my $result = "<td align='right' valign='center'>\n";
1000: my $firstDisplayed = !$params->{'condensed'} &&
1001: $params->{'multipart'} && $part eq "0";
1002:
1003: my $color;
1.192 bowersj2 1004: if ($resource->is_problem() && ($resource->countParts() <= 1 || $part ne '') ) {
1.136 bowersj2 1005: $color = $colormap{$resource->status};
1006:
1007: if (dueInLessThen24Hours($resource, $part) ||
1008: lastTry($resource, $part)) {
1009: $color = $hurryUpColor;
1010: }
1011: }
1012:
1013: if ($resource->kind() eq "res" &&
1014: $resource->is_problem() &&
1015: !$firstDisplayed) {
1016: if ($color) {$result .= "<font color=\"$color\"><b>"; }
1017: $result .= getDescription($resource, $part);
1018: if ($color) {$result .= "</b></font>"; }
1019: }
1020: if ($resource->is_map() && advancedUser() && $resource->randompick()) {
1021: $result .= '(randomly select ' . $resource->randompick() .')';
1022: }
1023:
1.195 ! bowersj2 1024: $result .= " - $part </td>\n";
1.136 bowersj2 1025:
1026: return $result;
1.51 bowersj2 1027: }
1028:
1.133 bowersj2 1029: my @preparedColumns = (\&render_resource, \&render_communication_status,
1030: \&render_quick_status, \&render_long_status);
1.132 bowersj2 1031:
1032: sub setDefault {
1033: my ($val, $default) = @_;
1034: if (!defined($val)) { return $default; }
1035: return $val;
1036: }
1037:
1038: sub render {
1039: my $args = shift;
1.140 bowersj2 1040: &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
1041: my $result = '';
1042:
1.132 bowersj2 1043: # Configure the renderer.
1044: my $cols = $args->{'cols'};
1045: if (!defined($cols)) {
1046: # no columns, no nav maps.
1047: return '';
1048: }
1.140 bowersj2 1049: my $mustCloseNavMap = 0;
1050: my $navmap;
1051: if (defined($args->{'navmap'})) {
1052: $navmap = $args->{'navmap'};
1053: }
1054:
1.158 bowersj2 1055: my $r = $args->{'r'};
1.140 bowersj2 1056: my $queryString = $args->{'queryString'};
1.159 bowersj2 1057: my $jump = $args->{'jump'};
1.158 bowersj2 1058: my $here = $args->{'here'};
1.153 bowersj2 1059: my $suppressNavmap = setDefault($args->{'suppressNavmap'}, 0);
1.140 bowersj2 1060: my $currentJumpDelta = 2; # change this to change how many resources are displayed
1061: # before the current resource when using #current
1062:
1063: # If we were passed 'here' information, we are not rendering
1064: # after a folder manipulation, and we were not passed an
1065: # iterator, make sure we open the folders to show the "here"
1066: # marker
1067: my $filterHash = {};
1068: # Figure out what we're not displaying
1069: foreach (split(/\,/, $ENV{"form.filter"})) {
1070: if ($_) {
1071: $filterHash->{$_} = "1";
1072: }
1073: }
1074:
1.191 bowersj2 1075: # Filter: Remember filter function and add our own filter: Refuse
1076: # to show hidden resources unless the user can see them.
1077: my $userCanSeeHidden = advancedUser();
1078: my $filterFunc = setDefault($args->{'filterFunc'},
1079: sub {return 1;});
1080: if (!$userCanSeeHidden) {
1081: # Without renaming the filterfunc, the server seems to go into
1082: # an infinite loop
1083: my $oldFilterFunc = $filterFunc;
1084: $filterFunc = sub { my $res = shift; return !$res->randomout() &&
1085: &$oldFilterFunc($res);};
1086: }
1087:
1.140 bowersj2 1088: my $condition = 0;
1089: if ($ENV{'form.condition'}) {
1090: $condition = 1;
1091: }
1092:
1093: if (!$ENV{'form.folderManip'} && !defined($args->{'iterator'})) {
1094: # Step 1: Check to see if we have a navmap
1095: if (!defined($navmap)) {
1.171 bowersj2 1096: $navmap = Apache::lonnavmaps::navmap->new(
1.140 bowersj2 1097: $ENV{"request.course.fn"}.".db",
1098: $ENV{"request.course.fn"}."_parms.db", 1, 1);
1099: $mustCloseNavMap = 1;
1100: }
1101: $navmap->init();
1102:
1103: # Step two: Locate what kind of here marker is necessary
1104: # Determine where the "here" marker is and where the screen jumps to.
1105:
1.175 bowersj2 1106: if ($ENV{'form.postsymb'}) {
1107: $here = $jump = $ENV{'form.postsymb'};
1.140 bowersj2 1108: } elsif ($ENV{'form.postdata'}) {
1109: # couldn't find a symb, is there a URL?
1110: my $currenturl = $ENV{'form.postdata'};
1.158 bowersj2 1111: #$currenturl=~s/^http\:\/\///;
1112: #$currenturl=~s/^[^\/]+//;
1.140 bowersj2 1113:
1.158 bowersj2 1114: $here = $jump = &Apache::lonnet::symbread($currenturl);
1.140 bowersj2 1115: }
1.158 bowersj2 1116:
1.140 bowersj2 1117: # Step three: Ensure the folders are open
1118: my $mapIterator = $navmap->getIterator(undef, undef, undef, 1);
1119: my $depth = 1;
1120: $mapIterator->next(); # discard the first BEGIN_MAP
1121: my $curRes = $mapIterator->next();
1122: my $found = 0;
1123:
1124: # We only need to do this if we need to open the maps to show the
1125: # current position. This will change the counter so we can't count
1126: # for the jump marker with this loop.
1127: while ($depth > 0 && !$found) {
1128: if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
1129: if ($curRes == $mapIterator->END_MAP()) { $depth--; }
1130:
1.158 bowersj2 1131: if (ref($curRes) && $curRes->symb() eq $here) {
1.140 bowersj2 1132: my $mapStack = $mapIterator->getStack();
1133:
1134: # Ensure the parent maps are open
1135: for my $map (@{$mapStack}) {
1136: if ($condition) {
1137: undef $filterHash->{$map->map_pc()};
1138: } else {
1139: $filterHash->{$map->map_pc()} = 1;
1140: }
1141: }
1142: $found = 1;
1143: }
1144:
1145: $curRes = $mapIterator->next();
1146: }
1.159 bowersj2 1147: }
1.140 bowersj2 1148:
1149: if ( !defined($args->{'iterator'}) && $ENV{'form.folderManip'} ) { # we came from a user's manipulation of the nav page
1150: # If this is a click on a folder or something, we want to preserve the "here"
1151: # from the querystring, and get the new "jump" marker
1152: $here = $ENV{'form.here'};
1153: $jump = $ENV{'form.jump'};
1154: }
1155:
1.132 bowersj2 1156: my $it = $args->{'iterator'};
1157: if (!defined($it)) {
1.140 bowersj2 1158: # Construct a default iterator based on $ENV{'form.'} information
1159:
1160: # Step 1: Check to see if we have a navmap
1161: if (!defined($navmap)) {
1.164 bowersj2 1162: $navmap = Apache::lonnavmaps::navmap->new($r,
1.140 bowersj2 1163: $ENV{"request.course.fn"}.".db",
1164: $ENV{"request.course.fn"}."_parms.db", 1, 1);
1165: $mustCloseNavMap = 1;
1166: }
1167: # Paranoia: Make sure it's ready
1168: $navmap->init();
1169:
1.144 bowersj2 1170: # See if we're being passed a specific map
1171: if ($args->{'iterator_map'}) {
1172: my $map = $args->{'iterator_map'};
1.145 bowersj2 1173: $map = $navmap->getResourceByUrl($map);
1.144 bowersj2 1174: my $firstResource = $map->map_start();
1175: my $finishResource = $map->map_finish();
1176:
1177: $args->{'iterator'} = $it = $navmap->getIterator($firstResource, $finishResource, $filterHash, $condition);
1178: } else {
1179: $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition);
1180: }
1.132 bowersj2 1181: }
1182:
1.159 bowersj2 1183: # (re-)Locate the jump point, if any
1.191 bowersj2 1184: # Note this does not take filtering or hidden into account... need
1185: # to be fixed?
1.159 bowersj2 1186: my $mapIterator = $navmap->getIterator(undef, undef, $filterHash, 0);
1187: my $depth = 1;
1188: $mapIterator->next();
1189: my $curRes = $mapIterator->next();
1190: my $foundJump = 0;
1191: my $counter = 0;
1192:
1193: while ($depth > 0 && !$foundJump) {
1194: if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
1195: if ($curRes == $mapIterator->END_MAP()) { $depth--; }
1196: if (ref($curRes)) { $counter++; }
1197:
1198: if (ref($curRes) && $jump eq $curRes->symb()) {
1199:
1200: # This is why we have to use the main iterator instead of the
1201: # potentially faster DFS: The count has to be the same, so
1202: # the order has to be the same, which DFS won't give us.
1203: $args->{'currentJumpIndex'} = $counter;
1204: $foundJump = 1;
1205: }
1206:
1207: $curRes = $mapIterator->next();
1208: }
1209:
1.132 bowersj2 1210: my $showParts = setDefault($args->{'showParts'}, 1);
1211: my $condenseParts = setDefault($args->{'condenseParts'}, 1);
1.139 bowersj2 1212: # keeps track of when the current resource is found,
1213: # so we can back up a few and put the anchor above the
1214: # current resource
1.140 bowersj2 1215: my $printKey = $args->{'printKey'};
1216: my $printCloseAll = $args->{'printCloseAll'};
1217: if (!defined($printCloseAll)) { $printCloseAll = 1; }
1.144 bowersj2 1218:
1.140 bowersj2 1219: # Print key?
1220: if ($printKey) {
1221: $result .= '<table border="0" cellpadding="2" cellspacing="0">';
1222: my $date=localtime;
1223: $result.='<tr><td align="right" valign="bottom">Key: </td>';
1224: if ($navmap->{LAST_CHECK}) {
1225: $result .=
1226: '<img src="/adm/lonMisc/chat.gif"> New discussion since '.
1227: strftime("%A, %b %e at %I:%M %P", localtime($navmap->{LAST_CHECK})).
1228: '</td><td align="center" valign="bottom"> '.
1229: '<img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>'.
1230: '</td>';
1231: } else {
1232: $result .= '<td align="center" valign="bottom"> '.
1233: '<img src="/adm/lonMisc/chat.gif"> Discussions</td><td align="center" valign="bottom">'.
1234: ' <img src="/adm/lonMisc/feedback.gif"> New message (click to open)'.
1235: '</td>';
1236: }
1237:
1238: $result .= '</tr></table>';
1239: }
1240:
1.172 bowersj2 1241: if ($printCloseAll && !$args->{'resource_no_folder_link'}) {
1.140 bowersj2 1242: if ($condition) {
1243: $result.="<a href=\"navmaps?condition=0&filter=&$queryString" .
1.158 bowersj2 1244: "&here=" . Apache::lonnet::escape($here) .
1.140 bowersj2 1245: "\">Close All Folders</a>";
1246: } else {
1247: $result.="<a href=\"navmaps?condition=1&filter=&$queryString" .
1.158 bowersj2 1248: "&here=" . Apache::lonnet::escape($here) .
1.140 bowersj2 1249: "\">Open All Folders</a>";
1250: }
1.143 bowersj2 1251: $result .= "<br /><br />\n";
1.140 bowersj2 1252: }
1253:
1254: if ($r) {
1255: $r->print($result);
1256: $r->rflush();
1257: $result = "";
1258: }
1.132 bowersj2 1259: # End parameter setting
1.133 bowersj2 1260:
1.132 bowersj2 1261: # Data
1.140 bowersj2 1262: $result .= '<table cellspacing="0" cellpadding="3" border="0" bgcolor="#FFFFFF">' ."\n";
1.132 bowersj2 1263: my $res = "Apache::lonnavmaps::resource";
1264: my %condenseStatuses =
1265: ( $res->NETWORK_FAILURE => 1,
1266: $res->NOTHING_SET => 1,
1267: $res->CORRECT => 1 );
1268: my @backgroundColors = ("#FFFFFF", "#F6F6F6");
1.133 bowersj2 1269:
1270: # Shared variables
1271: $args->{'counter'} = 0; # counts the rows
1272: $args->{'indentLevel'} = 0;
1273: $args->{'isNewBranch'} = 0;
1274: $args->{'condensed'} = 0;
1275: $args->{'indentString'} = setDefault($args->{'indentString'}, "<img src='/adm/lonIcons/whitespace1.gif' width='25' height='1' alt='' border='0' />");
1276: $args->{'displayedHereMarker'} = 0;
1.132 bowersj2 1277:
1.190 bowersj2 1278: # If we're suppressing empty sequences, look for them here. Use DFS for speed,
1279: # since structure actually doesn't matter, except what map has what resources.
1280: if ($args->{'suppressEmptySequences'}) {
1281: my $dfsit = Apache::lonnavmaps::DFSiterator->new($navmap,
1282: $it->{FIRST_RESOURCE},
1283: $it->{FINISH_RESOURCE},
1284: {}, undef, 1);
1285: $depth = 0;
1286: $dfsit->next();
1287: my $curRes = $dfsit->next();
1288: while ($depth > -1) {
1289: if ($curRes == $dfsit->BEGIN_MAP()) { $depth++; }
1290: if ($curRes == $dfsit->END_MAP()) { $depth--; }
1291:
1292: if (ref($curRes)) {
1293: # Parallel pre-processing: Do sequences have non-filtered-out children?
1294: if ($curRes->is_sequence()) {
1295: $curRes->{DATA}->{HAS_VISIBLE_CHILDREN} = 0;
1296: # Sequences themselves do not count as visible children,
1297: # unless those sequences also have visible children.
1298: # This means if a sequence appears, there's a "promise"
1299: # that there's something under it if you open it, somewhere.
1300: } else {
1301: # Not a sequence: if it's filtered, ignore it, otherwise
1302: # rise up the stack and mark the sequences as having children
1303: if (&$filterFunc($curRes)) {
1304: for my $sequence (@{$dfsit->getStack()}) {
1305: $sequence->{DATA}->{HAS_VISIBLE_CHILDREN} = 1;
1306: }
1307: }
1308: }
1309: }
1310: } continue {
1311: $curRes = $dfsit->next();
1312: }
1313: }
1314:
1.133 bowersj2 1315: my $displayedJumpMarker = 0;
1.132 bowersj2 1316: # Set up iteration.
1.159 bowersj2 1317: $depth = 1;
1.132 bowersj2 1318: $it->next(); # discard initial BEGIN_MAP
1.159 bowersj2 1319: $curRes = $it->next();
1.132 bowersj2 1320: my $now = time();
1321: my $in24Hours = $now + 24 * 60 * 60;
1322: my $rownum = 0;
1323:
1.141 bowersj2 1324: # export "here" marker information
1325: $args->{'here'} = $here;
1326:
1.132 bowersj2 1327: while ($depth > 0) {
1328: if ($curRes == $it->BEGIN_MAP()) { $depth++; }
1329: if ($curRes == $it->END_MAP()) { $depth--; }
1330:
1331: # Maintain indentation level.
1332: if ($curRes == $it->BEGIN_MAP() ||
1333: $curRes == $it->BEGIN_BRANCH() ) {
1.133 bowersj2 1334: $args->{'indentLevel'}++;
1.132 bowersj2 1335: }
1336: if ($curRes == $it->END_MAP() ||
1337: $curRes == $it->END_BRANCH() ) {
1.133 bowersj2 1338: $args->{'indentLevel'}--;
1.132 bowersj2 1339: }
1340: # Notice new branches
1341: if ($curRes == $it->BEGIN_BRANCH()) {
1.133 bowersj2 1342: $args->{'isNewBranch'} = 1;
1343: }
1344:
1345: # If this isn't an actual resource, continue on
1346: if (!ref($curRes)) {
1347: next;
1.132 bowersj2 1348: }
1.133 bowersj2 1349:
1.143 bowersj2 1350: # If this has been filtered out, continue on
1351: if (!(&$filterFunc($curRes))) {
1352: $args->{'isNewBranch'} = 0; # Don't falsely remember this
1353: next;
1354: }
1.132 bowersj2 1355:
1.190 bowersj2 1356: # If this is an empty sequence and we're filtering them, continue on
1357: if ($curRes->is_sequence() && $args->{'suppressEmptySequences'} &&
1358: !$curRes->{DATA}->{HAS_VISIBLE_CHILDREN}) {
1359: next;
1360: }
1361:
1.153 bowersj2 1362: # If we're suppressing navmaps and this is a navmap, continue on
1363: if ($suppressNavmap && $curRes->src() =~ /^\/adm\/navmaps/) {
1364: next;
1365: }
1366:
1.191 bowersj2 1367: $args->{'counter'}++;
1368:
1.132 bowersj2 1369: # Does it have multiple parts?
1.133 bowersj2 1370: $args->{'multipart'} = 0;
1371: $args->{'condensed'} = 0;
1.132 bowersj2 1372: my @parts;
1373:
1374: # Decide what parts to show.
1.133 bowersj2 1375: if ($curRes->is_problem() && $showParts) {
1.132 bowersj2 1376: @parts = @{$curRes->parts()};
1.192 bowersj2 1377: $args->{'multipart'} = $curRes->multipart();
1.132 bowersj2 1378:
1379: if ($condenseParts) { # do the condensation
1380: if (!$curRes->opendate("0")) {
1.162 bowersj2 1381: @parts = ();
1.133 bowersj2 1382: $args->{'condensed'} = 1;
1.132 bowersj2 1383: }
1.133 bowersj2 1384: if (!$args->{'condensed'}) {
1.132 bowersj2 1385: # Decide whether to condense based on similarity
1.192 bowersj2 1386: my $status = $curRes->status($parts[0]);
1387: my $due = $curRes->duedate($parts[0]);
1388: my $open = $curRes->opendate($parts[0]);
1.132 bowersj2 1389: my $statusAllSame = 1;
1390: my $dueAllSame = 1;
1391: my $openAllSame = 1;
1.192 bowersj2 1392: for (my $i = 1; $i < scalar(@parts); $i++) {
1.132 bowersj2 1393: if ($curRes->status($parts[$i]) != $status){
1394: $statusAllSame = 0;
1395: }
1396: if ($curRes->duedate($parts[$i]) != $due ) {
1397: $dueAllSame = 0;
1398: }
1399: if ($curRes->opendate($parts[$i]) != $open) {
1400: $openAllSame = 0;
1401: }
1402: }
1403: # $*allSame is true if all the statuses were
1404: # the same. Now, if they are all the same and
1405: # match one of the statuses to condense, or they
1406: # are all open with the same due date, or they are
1407: # all OPEN_LATER with the same open date, display the
1408: # status of the first non-zero part (to get the 'correct'
1409: # status right, since 0 is never 'correct' or 'open').
1410: if (($statusAllSame && defined($condenseStatuses{$status})) ||
1411: ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
1412: ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
1.192 bowersj2 1413: @parts = ($parts[0]);
1.133 bowersj2 1414: $args->{'condensed'} = 1;
1.132 bowersj2 1415: }
1416:
1417: }
1418: }
1.157 bowersj2 1419: }
1.132 bowersj2 1420:
1421: # If the multipart problem was condensed, "forget" it was multipart
1422: if (scalar(@parts) == 1) {
1.133 bowersj2 1423: $args->{'multipart'} = 0;
1.192 bowersj2 1424: } else {
1425: # Add part 0 so we display it correctly.
1426: unshift @parts, '0';
1.132 bowersj2 1427: }
1428:
1429: # Now, we've decided what parts to show. Loop through them and
1430: # show them.
1.192 bowersj2 1431: foreach my $part (@parts) {
1.132 bowersj2 1432: $rownum ++;
1433: my $backgroundColor = $backgroundColors[$rownum % scalar(@backgroundColors)];
1434:
1435: $result .= " <tr bgcolor='$backgroundColor'>\n";
1.134 bowersj2 1436:
1437: # Set up some data about the parts that the cols might want
1438: my $filter = $it->{FILTER};
1439: my $stack = $it->getStack();
1440: my $src = getLinkForResource($stack);
1441:
1442: my $srcHasQuestion = $src =~ /\?/;
1443: $args->{"resourceLink"} = $src.
1444: ($srcHasQuestion?'&':'?') .
1.137 bowersj2 1445: 'symb=' . &Apache::lonnet::escape($curRes->symb());
1.132 bowersj2 1446:
1447: # Now, display each column.
1448: foreach my $col (@$cols) {
1.139 bowersj2 1449: my $colHTML = '';
1450: if (ref($col)) {
1451: $colHTML .= &$col($curRes, $part, $args);
1452: } else {
1453: $colHTML .= &{$preparedColumns[$col]}($curRes, $part, $args);
1454: }
1.132 bowersj2 1455:
1.133 bowersj2 1456: # If this is the first column and it's time to print
1457: # the anchor, do so
1458: if ($col == $cols->[0] &&
1459: $args->{'counter'} == $args->{'currentJumpIndex'} -
1.139 bowersj2 1460: $currentJumpDelta) {
1461: # Jam the anchor after the <td> tag;
1462: # necessary for valid HTML (which Mozilla requires)
1463: $colHTML =~ s/\>/\>\<a name="curloc" \/\>/;
1.133 bowersj2 1464: $displayedJumpMarker = 1;
1465: }
1.139 bowersj2 1466: $result .= $colHTML . "\n";
1.132 bowersj2 1467: }
1.139 bowersj2 1468: $result .= " </tr>\n";
1.140 bowersj2 1469: $args->{'isNewBranch'} = 0;
1.139 bowersj2 1470: }
1.153 bowersj2 1471:
1.139 bowersj2 1472: if ($r && $rownum % 20 == 0) {
1473: $r->print($result);
1474: $result = "";
1475: $r->rflush();
1.132 bowersj2 1476: }
1.156 bowersj2 1477: } continue {
1.132 bowersj2 1478: $curRes = $it->next();
1479: }
1480:
1.134 bowersj2 1481: # Print out the part that jumps to #curloc if it exists
1.159 bowersj2 1482: # delay needed because the browser is processing the jump before
1483: # it finishes rendering, so it goes to the wrong place!
1484: # onload might be better, but this routine has no access to that.
1485: # On mozilla, the 0-millisecond timeout seems to prevent this;
1486: # it's quite likely this might fix other browsers, too, and
1487: # certainly won't hurt anything.
1.139 bowersj2 1488: if ($displayedJumpMarker) {
1.159 bowersj2 1489: $result .= "<script>setTimeout(\"location += '#curloc';\", 0)</script>\n";
1.134 bowersj2 1490: }
1491:
1.132 bowersj2 1492: $result .= "</table>";
1.140 bowersj2 1493:
1494: if ($r) {
1495: $r->print($result);
1496: $result = "";
1497: $r->rflush();
1498: }
1499:
1.175 bowersj2 1500: if ($mustCloseNavMap) { $navmap->untieHashes(); }
1.132 bowersj2 1501:
1502: return $result;
1503: }
1504:
1.133 bowersj2 1505: 1;
1506:
1507: package Apache::lonnavmaps::navmap;
1508:
1509: =pod
1510:
1.174 albertel 1511: lonnavmaps provides functions and objects for dealing with the
1512: compiled course hashes generated when a user enters the course, the
1513: Apache handler for the "Navigation Map" button, and a flexible
1514: prepared renderer for navigation maps that are easy to use anywhere.
1515:
1516: =head1 Object: navmap
1.133 bowersj2 1517:
1.174 albertel 1518: Encapsulating the compiled nav map
1.133 bowersj2 1519:
1.174 albertel 1520: navmap is an object that encapsulates a compiled course map and
1521: provides a reasonable interface to it.
1.133 bowersj2 1522:
1.174 albertel 1523: Most notably it provides a way to navigate the map sensibly and a
1524: flexible iterator that makes it easy to write various renderers based
1525: on nav maps.
1.133 bowersj2 1526:
1527: You must obtain resource objects through the navmap object.
1528:
1529: =head2 Methods
1530:
1531: =over 4
1532:
1.174 albertel 1533: =item * B<new>(navHashFile, parmHashFile, genCourseAndUserOptions,
1534: genMailDiscussStatus):
1.133 bowersj2 1535:
1.174 albertel 1536: Binds a new navmap object to the compiled nav map hash and parm hash
1537: given as filenames. genCourseAndUserOptions is a flag saying whether
1538: the course options and user options hash should be generated. This is
1539: for when you are using the parameters of the resources that require
1540: them; see documentation in resource object
1541: documentation. genMailDiscussStatus causes the nav map to retreive
1542: information about the email and discussion status of
1543: resources. Returns the navmap object if this is successful, or
1544: B<undef> if not. You must check for undef; errors will occur when you
1545: try to use the other methods otherwise.
1546:
1547: =item * B<getIterator>(first, finish, filter, condition):
1548:
1549: See iterator documentation below.
1.133 bowersj2 1550:
1551: =cut
1552:
1553: use strict;
1554: use GDBM_File;
1555:
1556: sub new {
1557: # magic invocation to create a class instance
1558: my $proto = shift;
1559: my $class = ref($proto) || $proto;
1560: my $self = {};
1561:
1562: $self->{NAV_HASH_FILE} = shift;
1563: $self->{PARM_HASH_FILE} = shift;
1564: $self->{GENERATE_COURSE_USER_OPT} = shift;
1565: $self->{GENERATE_EMAIL_DISCUSS_STATUS} = shift;
1566:
1567: # Resource cache stores navmap resources as we reference them. We generate
1568: # them on-demand so we don't pay for creating resources unless we use them.
1569: $self->{RESOURCE_CACHE} = {};
1570:
1571: # Network failure flag, if we accessed the course or user opt and
1572: # failed
1573: $self->{NETWORK_FAILURE} = 0;
1574:
1575: # tie the nav hash
1576:
1.168 bowersj2 1577: my %navmaphash;
1578: my %parmhash;
1579: if (!(tie(%navmaphash, 'GDBM_File', $self->{NAV_HASH_FILE},
1.133 bowersj2 1580: &GDBM_READER(), 0640))) {
1581: return undef;
1582: }
1583:
1.168 bowersj2 1584: if (!(tie(%parmhash, 'GDBM_File', $self->{PARM_HASH_FILE},
1.133 bowersj2 1585: &GDBM_READER(), 0640)))
1586: {
1.170 matthew 1587: untie %{$self->{PARM_HASH}};
1.133 bowersj2 1588: return undef;
1589: }
1590:
1.195 ! bowersj2 1591: # try copying into memory
! 1592: my %tmpnavhash;
! 1593: while (my ($k, $v) = each(%navmaphash)) {
! 1594: $tmpnavhash{$k} = $v;
! 1595: }
! 1596: untie %navmaphash;
! 1597:
! 1598: $self->{NAV_HASH} = \%tmpnavhash;
1.168 bowersj2 1599: $self->{PARM_HASH} = \%parmhash;
1.140 bowersj2 1600: $self->{INITED} = 0;
1.133 bowersj2 1601:
1602: bless($self);
1603:
1604: return $self;
1605: }
1606:
1607: sub init {
1608: my $self = shift;
1.140 bowersj2 1609: if ($self->{INITED}) { return; }
1.133 bowersj2 1610:
1611: # If the course opt hash and the user opt hash should be generated,
1612: # generate them
1613: if ($self->{GENERATE_COURSE_USER_OPT}) {
1614: my $uname=$ENV{'user.name'};
1615: my $udom=$ENV{'user.domain'};
1616: my $uhome=$ENV{'user.home'};
1617: my $cid=$ENV{'request.course.id'};
1618: my $chome=$ENV{'course.'.$cid.'.home'};
1619: my ($cdom,$cnum)=split(/\_/,$cid);
1620:
1621: my $userprefix=$uname.'_'.$udom.'_';
1622:
1623: my %courserdatas; my %useropt; my %courseopt; my %userrdatas;
1624: unless ($uhome eq 'no_host') {
1625: # ------------------------------------------------- Get coursedata (if present)
1626: unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
1627: my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
1628: ':resourcedata',$chome);
1.182 bowersj2 1629: # Check for network failure
1630: if ( $reply =~ /no.such.host/i || $reply =~ /con_lost/i) {
1631: $self->{NETWORK_FAILURE} = 1;
1632: } elsif ($reply!~/^error\:/) {
1.133 bowersj2 1633: $courserdatas{$cid}=$reply;
1634: $courserdatas{$cid.'.last_cache'}=time;
1635: }
1636: }
1637: foreach (split(/\&/,$courserdatas{$cid})) {
1638: my ($name,$value)=split(/\=/,$_);
1639: $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
1640: &Apache::lonnet::unescape($value);
1641: }
1642: # --------------------------------------------------- Get userdata (if present)
1643: unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
1644: my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
1645: if ($reply!~/^error\:/) {
1646: $userrdatas{$uname.'___'.$udom}=$reply;
1647: $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
1648: }
1649: # check to see if network failed
1650: elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
1651: {
1652: $self->{NETWORK_FAILURE} = 1;
1653: }
1654: }
1655: foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
1656: my ($name,$value)=split(/\=/,$_);
1657: $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
1658: &Apache::lonnet::unescape($value);
1659: }
1660: $self->{COURSE_OPT} = \%courseopt;
1661: $self->{USER_OPT} = \%useropt;
1662: }
1663: }
1664:
1665: if ($self->{GENERATE_EMAIL_DISCUSS_STATUS}) {
1666: my $cid=$ENV{'request.course.id'};
1667: my ($cdom,$cnum)=split(/\_/,$cid);
1668:
1669: my %emailstatus = &Apache::lonnet::dump('email_status');
1670: my $logoutTime = $emailstatus{'logout'};
1671: my $courseLeaveTime = $emailstatus{'logout_'.$ENV{'request.course.id'}};
1.193 bowersj2 1672: $self->{LAST_CHECK} = (($courseLeaveTime > $logoutTime) ?
1.133 bowersj2 1673: $courseLeaveTime : $logoutTime);
1674: my %discussiontime = &Apache::lonnet::dump('discussiontimes',
1675: $cdom, $cnum);
1676: my %feedback=();
1677: my %error=();
1678: my $keys = &Apache::lonnet::reply('keys:'.
1679: $ENV{'user.domain'}.':'.
1680: $ENV{'user.name'}.':nohist_email',
1681: $ENV{'user.home'});
1682:
1683: foreach my $msgid (split(/\&/, $keys)) {
1684: $msgid=&Apache::lonnet::unescape($msgid);
1685: my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
1686: if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
1687: my ($what,$url)=($1,$2);
1688: my %status=
1689: &Apache::lonnet::get('email_status',[$msgid]);
1690: if ($status{$msgid}=~/^error\:/) {
1691: $status{$msgid}='';
1692: }
1693:
1694: if (($status{$msgid} eq 'new') ||
1695: (!$status{$msgid})) {
1696: if ($what eq 'Error') {
1697: $error{$url}.=','.$msgid;
1698: } else {
1699: $feedback{$url}.=','.$msgid;
1700: }
1701: }
1702: }
1703: }
1704:
1705: $self->{FEEDBACK} = \%feedback;
1706: $self->{ERROR_MSG} = \%error; # what is this? JB
1707: $self->{DISCUSSION_TIME} = \%discussiontime;
1708: $self->{EMAIL_STATUS} = \%emailstatus;
1709:
1710: }
1711:
1712: $self->{PARM_CACHE} = {};
1.140 bowersj2 1713: $self->{INITED} = 1;
1.133 bowersj2 1714: }
1715:
1716: # Internal function: Takes a key to look up in the nav hash and implements internal
1717: # memory caching of that key.
1718: sub navhash {
1719: my $self = shift; my $key = shift;
1720: return $self->{NAV_HASH}->{$key};
1721: }
1722:
1723: # Checks to see if coursemap is defined, matching test in old lonnavmaps
1724: sub courseMapDefined {
1725: my $self = shift;
1726: my $uri = &Apache::lonnet::clutter($ENV{'request.course.uri'});
1727:
1728: my $firstres = $self->navhash("map_start_$uri");
1729: my $lastres = $self->navhash("map_finish_$uri");
1730: return $firstres && $lastres;
1731: }
1732:
1733: sub getIterator {
1734: my $self = shift;
1735: my $iterator = Apache::lonnavmaps::iterator->new($self, shift, shift,
1736: shift, undef, shift);
1737: return $iterator;
1738: }
1739:
1740: # unties the hash when done
1741: sub untieHashes {
1.168 bowersj2 1742: my $self = shift;
1.170 matthew 1743: untie %{$self->{NAV_HASH}};
1744: untie %{$self->{PARM_HASH}};
1.133 bowersj2 1745: }
1746:
1747: # Private method: Does the given resource (as a symb string) have
1748: # current discussion? Returns 0 if chat/mail data not extracted.
1749: sub hasDiscussion {
1750: my $self = shift;
1751: my $symb = shift;
1752: if (!defined($self->{DISCUSSION_TIME})) { return 0; }
1753:
1754: #return defined($self->{DISCUSSION_TIME}->{$symb});
1755: return $self->{DISCUSSION_TIME}->{$symb} >
1756: $self->{LAST_CHECK};
1757: }
1758:
1759: # Private method: Does the given resource (as a symb string) have
1760: # current feedback? Returns the string in the feedback hash, which
1761: # will be false if it does not exist.
1762: sub getFeedback {
1763: my $self = shift;
1764: my $symb = shift;
1765:
1766: if (!defined($self->{FEEDBACK})) { return ""; }
1767:
1768: return $self->{FEEDBACK}->{$symb};
1769: }
1770:
1771: # Private method: Get the errors for that resource (by source).
1772: sub getErrors {
1773: my $self = shift;
1774: my $src = shift;
1775:
1776: if (!defined($self->{ERROR_MSG})) { return ""; }
1777: return $self->{ERROR_MSG}->{$src};
1778: }
1779:
1780: =pod
1781:
1.174 albertel 1782: =item * B<getById>(id):
1783:
1784: Based on the ID of the resource (1.1, 3.2, etc.), get a resource
1785: object for that resource. This method, or other methods that use it
1786: (as in the resource object) is the only proper way to obtain a
1787: resource object.
1.133 bowersj2 1788:
1.194 bowersj2 1789: =item * B<getBySymb>(symb):
1790:
1791: Based on the symb of the resource, get a resource object for that
1792: resource. This is one of the proper ways to get a resource object.
1793:
1794: =item * B<getMapByMapPc>(map_pc):
1795:
1796: Based on the map_pc of the resource, get a resource object for
1797: the given map. This is one of the proper ways to get a resource object.
1798:
1.133 bowersj2 1799: =cut
1800:
1801: # The strategy here is to cache the resource objects, and only construct them
1802: # as we use them. The real point is to prevent reading any more from the tied
1803: # hash then we have to, which should hopefully alleviate speed problems.
1804: # Caching is just an incidental detail I throw in because it makes sense.
1805:
1806: sub getById {
1807: my $self = shift;
1808: my $id = shift;
1809:
1810: if (defined ($self->{RESOURCE_CACHE}->{$id}))
1811: {
1812: return $self->{RESOURCE_CACHE}->{$id};
1813: }
1814:
1815: # resource handles inserting itself into cache.
1816: # Not clear why the quotes are necessary, but as of this
1817: # writing it doesn't work without them.
1818: return "Apache::lonnavmaps::resource"->new($self, $id);
1.132 bowersj2 1819: }
1.133 bowersj2 1820:
1.172 bowersj2 1821: sub getBySymb {
1822: my $self = shift;
1823: my $symb = shift;
1824: my ($mapUrl, $id, $filename) = split (/___/, $symb);
1825: my $map = $self->getResourceByUrl($mapUrl);
1826: return $self->getById($map->map_pc() . '.' . $id);
1.194 bowersj2 1827: }
1828:
1829: sub getByMapPc {
1830: my $self = shift;
1831: my $map_pc = shift;
1832: my $map_id = $self->{NAV_HASH}->{'map_id_' . $map_pc};
1833: $map_id = $self->{NAV_HASH}->{'ids_' . $map_id};
1834: return $self->getById($map_id);
1.172 bowersj2 1835: }
1836:
1.133 bowersj2 1837: =pod
1838:
1.174 albertel 1839: =item * B<firstResource>():
1840:
1841: Returns a resource object reference corresponding to the first
1842: resource in the navmap.
1.133 bowersj2 1843:
1844: =cut
1845:
1846: sub firstResource {
1847: my $self = shift;
1848: my $firstResource = $self->navhash('map_start_' .
1849: &Apache::lonnet::clutter($ENV{'request.course.uri'}));
1850: return $self->getById($firstResource);
1.132 bowersj2 1851: }
1.133 bowersj2 1852:
1853: =pod
1854:
1.174 albertel 1855: =item * B<finishResource>():
1856:
1857: Returns a resource object reference corresponding to the last resource
1858: in the navmap.
1.133 bowersj2 1859:
1860: =cut
1861:
1862: sub finishResource {
1863: my $self = shift;
1864: my $firstResource = $self->navhash('map_finish_' .
1865: &Apache::lonnet::clutter($ENV{'request.course.uri'}));
1866: return $self->getById($firstResource);
1.132 bowersj2 1867: }
1.133 bowersj2 1868:
1869: # Parmval reads the parm hash and cascades the lookups. parmval_real does
1870: # the actual lookup; parmval caches the results.
1871: sub parmval {
1872: my $self = shift;
1873: my ($what,$symb)=@_;
1874: my $hashkey = $what."|||".$symb;
1875:
1876: if (defined($self->{PARM_CACHE}->{$hashkey})) {
1877: return $self->{PARM_CACHE}->{$hashkey};
1878: }
1879:
1880: my $result = $self->parmval_real($what, $symb);
1881: $self->{PARM_CACHE}->{$hashkey} = $result;
1882: return $result;
1.132 bowersj2 1883: }
1884:
1.133 bowersj2 1885: sub parmval_real {
1886: my $self = shift;
1887: my ($what,$symb) = @_;
1888:
1889: my $cid=$ENV{'request.course.id'};
1890: my $csec=$ENV{'request.course.sec'};
1891: my $uname=$ENV{'user.name'};
1892: my $udom=$ENV{'user.domain'};
1893:
1894: unless ($symb) { return ''; }
1895: my $result='';
1896:
1897: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
1898:
1899: # ----------------------------------------------------- Cascading lookup scheme
1900: my $rwhat=$what;
1901: $what=~s/^parameter\_//;
1902: $what=~s/\_/\./;
1903:
1904: my $symbparm=$symb.'.'.$what;
1905: my $mapparm=$mapname.'___(all).'.$what;
1906: my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
1907:
1908: my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
1909: my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
1910: my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
1911:
1912: my $courselevel= $usercourseprefix.'.'.$what;
1913: my $courselevelr=$usercourseprefix.'.'.$symbparm;
1914: my $courselevelm=$usercourseprefix.'.'.$mapparm;
1915:
1916: my $useropt = $self->{USER_OPT};
1917: my $courseopt = $self->{COURSE_OPT};
1918: my $parmhash = $self->{PARM_HASH};
1919:
1920: # ---------------------------------------------------------- first, check user
1921: if ($uname and defined($useropt)) {
1922: if (defined($$useropt{$courselevelr})) { return $$useropt{$courselevelr}; }
1923: if (defined($$useropt{$courselevelm})) { return $$useropt{$courselevelm}; }
1924: if (defined($$useropt{$courselevel})) { return $$useropt{$courselevel}; }
1925: }
1926:
1927: # ------------------------------------------------------- second, check course
1928: if ($csec and defined($courseopt)) {
1929: if (defined($$courseopt{$seclevelr})) { return $$courseopt{$seclevelr}; }
1930: if (defined($$courseopt{$seclevelm})) { return $$courseopt{$seclevelm}; }
1931: if (defined($$courseopt{$seclevel})) { return $$courseopt{$seclevel}; }
1932: }
1933:
1934: if (defined($courseopt)) {
1935: if (defined($$courseopt{$courselevelr})) { return $$courseopt{$courselevelr}; }
1936: if (defined($$courseopt{$courselevelm})) { return $$courseopt{$courselevelm}; }
1937: if (defined($$courseopt{$courselevel})) { return $$courseopt{$courselevel}; }
1938: }
1939:
1940: # ----------------------------------------------------- third, check map parms
1941:
1942: my $thisparm=$$parmhash{$symbparm};
1943: if (defined($thisparm)) { return $thisparm; }
1944:
1945: # ----------------------------------------------------- fourth , check default
1946:
1947: my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
1948: if (defined($default)) { return $default}
1949:
1950: # --------------------------------------------------- fifth , cascade up parts
1951:
1952: my ($space,@qualifier)=split(/\./,$rwhat);
1953: my $qualifier=join('.',@qualifier);
1954: unless ($space eq '0') {
1.160 albertel 1955: my @parts=split(/_/,$space);
1956: my $id=pop(@parts);
1957: my $part=join('_',@parts);
1958: if ($part eq '') { $part='0'; }
1959: my $partgeneral=$self->parmval($part.".$qualifier",$symb);
1960: if (defined($partgeneral)) { return $partgeneral; }
1.133 bowersj2 1961: }
1962: return '';
1.145 bowersj2 1963: }
1964:
1.174 albertel 1965: =pod
1.145 bowersj2 1966:
1.174 albertel 1967: =item * B<getResourceByUrl>(url):
1.145 bowersj2 1968:
1.174 albertel 1969: Retrieves a resource object by URL of the resource. If passed a
1970: resource object, it will simply return it, so it is safe to use this
1971: method in code like "$res = $navmap->getResourceByUrl($res)", if
1972: you're not sure if $res is already an object, or just a URL. If the
1973: resource appears multiple times in the course, only the first instance
1974: will be returned. As a result, this is probably useful only for maps.
1975:
1976: =item * B<retrieveResources>(map, filterFunc, recursive, bailout):
1977:
1978: The map is a specification of a map to retreive the resources from,
1979: either as a url or as an object. The filterFunc is a reference to a
1980: function that takes a resource object as its one argument and returns
1981: true if the resource should be included, or false if it should not
1982: be. If recursive is true, the map will be recursively examined,
1983: otherwise it will not be. If bailout is true, the function will return
1984: as soon as it finds a resource, if false it will finish. By default,
1985: the map is the top-level map of the course, filterFunc is a function
1986: that always returns 1, recursive is true, bailout is false. The
1987: resources will be returned in a list containing the resource objects
1988: for the corresponding resources, with B<no structure information> in
1989: the list; regardless of branching, recursion, etc., it will be a flat
1990: list.
1991:
1992: Thus, this is suitable for cases where you don't want the structure,
1993: just a list of all resources. It is also suitable for finding out how
1994: many resources match a given description; for this use, if all you
1995: want to know is if I<any> resources match the description, the bailout
1996: parameter will allow you to avoid potentially expensive enumeration of
1997: all matching resources.
1.145 bowersj2 1998:
1.174 albertel 1999: =item * B<hasResources>(map, filterFunc, recursive):
1.145 bowersj2 2000:
1.174 albertel 2001: Convience method for
1.146 bowersj2 2002:
2003: scalar(retrieveResources($map, $filterFunc, $recursive, 1)) > 0
2004:
1.174 albertel 2005: which will tell whether the map has resources matching the description
2006: in the filter function.
1.146 bowersj2 2007:
1.145 bowersj2 2008: =cut
2009:
2010: sub getResourceByUrl {
2011: my $self = shift;
2012: my $resUrl = shift;
2013:
2014: if (ref($resUrl)) { return $resUrl; }
2015:
2016: $resUrl = &Apache::lonnet::clutter($resUrl);
2017: my $resId = $self->{NAV_HASH}->{'ids_' . $resUrl};
2018: if ($resId =~ /,/) {
2019: $resId = (split (/,/, $resId))[0];
2020: }
2021: if (!$resId) { return ''; }
2022: return $self->getById($resId);
2023: }
2024:
2025: sub retrieveResources {
2026: my $self = shift;
2027: my $map = shift;
2028: my $filterFunc = shift;
2029: if (!defined ($filterFunc)) {
2030: $filterFunc = sub {return 1;};
2031: }
2032: my $recursive = shift;
2033: if (!defined($recursive)) { $recursive = 1; }
2034: my $bailout = shift;
2035: if (!defined($bailout)) { $bailout = 0; }
2036:
2037: # Create the necessary iterator.
2038: if (!ref($map)) { # assume it's a url of a map.
1.172 bowersj2 2039: $map = $self->getResourceByUrl($map);
1.145 bowersj2 2040: }
2041:
2042: # Check the map's validity.
2043: if (!$map || !$map->is_map()) {
2044: # Oh, to throw an exception.... how I'd love that!
2045: return ();
2046: }
2047:
1.146 bowersj2 2048: # Get an iterator.
2049: my $it = $self->getIterator($map->map_start(), $map->map_finish(),
2050: !$recursive);
2051:
2052: my @resources = ();
2053:
2054: # Run down the iterator and collect the resources.
2055: my $depth = 1;
2056: $it->next();
2057: my $curRes = $it->next();
2058:
2059: while ($depth > 0) {
2060: if ($curRes == $it->BEGIN_MAP()) {
2061: $depth++;
2062: }
2063: if ($curRes == $it->END_MAP()) {
2064: $depth--;
2065: }
2066:
2067: if (ref($curRes)) {
2068: if (!&$filterFunc($curRes)) {
2069: next;
2070: }
2071:
2072: push @resources, $curRes;
2073:
2074: if ($bailout) {
2075: return @resources;
2076: }
2077: }
2078:
2079: $curRes = $it->next();
2080: }
2081:
2082: return @resources;
2083: }
2084:
2085: sub hasResource {
2086: my $self = shift;
2087: my $map = shift;
2088: my $filterFunc = shift;
2089: my $recursive = shift;
2090:
2091: return scalar($self->retrieveResources($map, $filterFunc, $recursive, 1)) > 0;
1.133 bowersj2 2092: }
1.51 bowersj2 2093:
2094: 1;
2095:
2096: package Apache::lonnavmaps::iterator;
2097:
2098: =pod
2099:
2100: =back
2101:
1.174 albertel 2102: =head1 Object: navmap Iterator
1.51 bowersj2 2103:
1.174 albertel 2104: An I<iterator> encapsulates the logic required to traverse a data
2105: structure. navmap uses an iterator to traverse the course map
2106: according to the criteria you wish to use.
2107:
2108: To obtain an iterator, call the B<getIterator>() function of a
2109: B<navmap> object. (Do not instantiate Apache::lonnavmaps::iterator
2110: directly.) This will return a reference to the iterator:
1.51 bowersj2 2111:
2112: C<my $resourceIterator = $navmap-E<gt>getIterator();>
2113:
2114: To get the next thing from the iterator, call B<next>:
2115:
2116: C<my $nextThing = $resourceIterator-E<gt>next()>
2117:
2118: getIterator behaves as follows:
2119:
2120: =over 4
2121:
1.174 albertel 2122: =item * B<getIterator>(firstResource, finishResource, filterHash, condition, forceTop, returnTopMap):
2123:
2124: All parameters are optional. firstResource is a resource reference
2125: corresponding to where the iterator should start. It defaults to
2126: navmap->firstResource() for the corresponding nav map. finishResource
2127: corresponds to where you want the iterator to end, defaulting to
2128: navmap->finishResource(). filterHash is a hash used as a set
2129: containing strings representing the resource IDs, defaulting to
2130: empty. Condition is a 1 or 0 that sets what to do with the filter
2131: hash: If a 0, then only resource that exist IN the filterHash will be
2132: recursed on. If it is a 1, only resources NOT in the filterHash will
2133: be recursed on. Defaults to 0. forceTop is a boolean value. If it is
2134: false (default), the iterator will only return the first level of map
2135: that is not just a single, 'redirecting' map. If true, the iterator
2136: will return all information, starting with the top-level map,
2137: regardless of content. returnTopMap, if true (default false), will
2138: cause the iterator to return the top-level map object (resource 0.0)
2139: before anything else.
2140:
2141: Thus, by default, only top-level resources will be shown. Change the
2142: condition to a 1 without changing the hash, and all resources will be
2143: shown. Changing the condition to 1 and including some values in the
2144: hash will allow you to selectively suppress parts of the navmap, while
2145: leaving it on 0 and adding things to the hash will allow you to
2146: selectively add parts of the nav map. See the handler code for
2147: examples.
2148:
2149: The iterator will return either a reference to a resource object, or a
2150: token representing something in the map, such as the beginning of a
2151: new branch. The possible tokens are:
2152:
2153: =over 4
1.51 bowersj2 2154:
1.174 albertel 2155: =item * BEGIN_MAP:
1.51 bowersj2 2156:
1.174 albertel 2157: A new map is being recursed into. This is returned I<after> the map
2158: resource itself is returned.
1.51 bowersj2 2159:
1.174 albertel 2160: =item * END_MAP:
2161:
2162: The map is now done.
1.51 bowersj2 2163:
1.174 albertel 2164: =item * BEGIN_BRANCH:
1.70 bowersj2 2165:
1.174 albertel 2166: A branch is now starting. The next resource returned will be the first
2167: in that branch.
1.70 bowersj2 2168:
1.174 albertel 2169: =item * END_BRANCH:
1.70 bowersj2 2170:
1.174 albertel 2171: The branch is now done.
1.51 bowersj2 2172:
2173: =back
2174:
1.174 albertel 2175: The tokens are retreivable via methods on the iterator object, i.e.,
2176: $iterator->END_MAP.
1.70 bowersj2 2177:
1.174 albertel 2178: Maps can contain empty resources. The iterator will automatically skip
2179: over such resources, but will still treat the structure
2180: correctly. Thus, a complicated map with several branches, but
2181: consisting entirely of empty resources except for one beginning or
2182: ending resource, will cause a lot of BRANCH_STARTs and BRANCH_ENDs,
2183: but only one resource will be returned.
1.116 bowersj2 2184:
1.70 bowersj2 2185: =back
1.51 bowersj2 2186:
2187: =cut
2188:
2189: # Here are the tokens for the iterator:
2190:
2191: sub BEGIN_MAP { return 1; } # begining of a new map
2192: sub END_MAP { return 2; } # end of the map
2193: sub BEGIN_BRANCH { return 3; } # beginning of a branch
2194: sub END_BRANCH { return 4; } # end of a branch
1.89 bowersj2 2195: sub FORWARD { return 1; } # go forward
2196: sub BACKWARD { return 2; }
1.51 bowersj2 2197:
1.96 bowersj2 2198: sub min {
2199: (my $a, my $b) = @_;
2200: if ($a < $b) { return $a; } else { return $b; }
2201: }
2202:
1.107 bowersj2 2203: # In the CVS repository, documentation of this algorithm is included
2204: # in /doc/lonnavdocs, as a PDF and .tex source. Markers like **1**
2205: # will reference the same location in the text as the part of the
2206: # algorithm is running through.
2207:
1.94 bowersj2 2208: sub new {
2209: # magic invocation to create a class instance
2210: my $proto = shift;
2211: my $class = ref($proto) || $proto;
2212: my $self = {};
2213:
2214: $self->{NAV_MAP} = shift;
2215: return undef unless ($self->{NAV_MAP});
2216:
2217: # Handle the parameters
2218: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
2219: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
2220:
2221: # If the given resources are just the ID of the resource, get the
2222: # objects
2223: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
2224: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
2225: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
2226: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
2227:
2228: $self->{FILTER} = shift;
2229:
2230: # A hash, used as a set, of resource already seen
2231: $self->{ALREADY_SEEN} = shift;
2232: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
2233: $self->{CONDITION} = shift;
2234:
1.116 bowersj2 2235: # Do we want to automatically follow "redirection" maps?
2236: $self->{FORCE_TOP} = shift;
2237:
1.162 bowersj2 2238: # Do we want to return the top-level map object (resource 0.0)?
2239: $self->{RETURN_0} = shift;
2240: # have we done that yet?
2241: $self->{HAVE_RETURNED_0} = 0;
2242:
1.94 bowersj2 2243: # Now, we need to pre-process the map, by walking forward and backward
2244: # over the parts of the map we're going to look at.
1.96 bowersj2 2245:
1.97 bowersj2 2246: # The processing steps are exactly the same, except for a few small
2247: # changes, so I bundle those up in the following list of two elements:
2248: # (direction_to_iterate, VAL_name, next_resource_method_to_call,
2249: # first_resource).
2250: # This prevents writing nearly-identical code twice.
2251: my @iterations = ( [FORWARD(), 'TOP_DOWN_VAL', 'getNext',
2252: 'FIRST_RESOURCE'],
2253: [BACKWARD(), 'BOT_UP_VAL', 'getPrevious',
2254: 'FINISH_RESOURCE'] );
2255:
1.98 bowersj2 2256: my $maxDepth = 0; # tracks max depth
2257:
1.116 bowersj2 2258: # If there is only one resource in this map, and it's a map, we
2259: # want to remember that, so the user can ask for the first map
2260: # that isn't just a redirector.
2261: my $resource; my $resourceCount = 0;
2262:
1.107 bowersj2 2263: # **1**
2264:
1.97 bowersj2 2265: foreach my $pass (@iterations) {
2266: my $direction = $pass->[0];
2267: my $valName = $pass->[1];
2268: my $nextResourceMethod = $pass->[2];
2269: my $firstResourceName = $pass->[3];
2270:
2271: my $iterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP},
2272: $self->{FIRST_RESOURCE},
2273: $self->{FINISH_RESOURCE},
2274: {}, undef, 0, $direction);
1.96 bowersj2 2275:
1.97 bowersj2 2276: # prime the recursion
2277: $self->{$firstResourceName}->{DATA}->{$valName} = 0;
1.98 bowersj2 2278: my $depth = 0;
1.97 bowersj2 2279: $iterator->next();
2280: my $curRes = $iterator->next();
1.98 bowersj2 2281: while ($depth > -1) {
1.97 bowersj2 2282: if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
2283: if ($curRes == $iterator->END_MAP()) { $depth--; }
1.96 bowersj2 2284:
1.97 bowersj2 2285: if (ref($curRes)) {
1.116 bowersj2 2286: # If there's only one resource, this will save it
1.117 bowersj2 2287: # we have to filter empty resources from consideration here,
2288: # or even "empty", redirecting maps have two (start & finish)
2289: # or three (start, finish, plus redirector)
2290: if($direction == FORWARD && $curRes->src()) {
2291: $resource = $curRes; $resourceCount++;
2292: }
1.97 bowersj2 2293: my $resultingVal = $curRes->{DATA}->{$valName};
2294: my $nextResources = $curRes->$nextResourceMethod();
1.116 bowersj2 2295: my $nextCount = scalar(@{$nextResources});
1.104 bowersj2 2296:
1.116 bowersj2 2297: if ($nextCount == 1) { # **3**
1.97 bowersj2 2298: my $current = $nextResources->[0]->{DATA}->{$valName} || 999999999;
2299: $nextResources->[0]->{DATA}->{$valName} = min($resultingVal, $current);
2300: }
2301:
1.116 bowersj2 2302: if ($nextCount > 1) { # **4**
1.97 bowersj2 2303: foreach my $res (@{$nextResources}) {
2304: my $current = $res->{DATA}->{$valName} || 999999999;
2305: $res->{DATA}->{$valName} = min($current, $resultingVal + 1);
2306: }
2307: }
2308: }
1.96 bowersj2 2309:
1.107 bowersj2 2310: # Assign the final val (**2**)
1.97 bowersj2 2311: if (ref($curRes) && $direction == BACKWARD()) {
1.98 bowersj2 2312: my $finalDepth = min($curRes->{DATA}->{TOP_DOWN_VAL},
2313: $curRes->{DATA}->{BOT_UP_VAL});
2314:
2315: $curRes->{DATA}->{DISPLAY_DEPTH} = $finalDepth;
2316: if ($finalDepth > $maxDepth) {$maxDepth = $finalDepth;}
1.190 bowersj2 2317: }
2318: } continue {
1.97 bowersj2 2319: $curRes = $iterator->next();
1.96 bowersj2 2320: }
2321: }
1.94 bowersj2 2322:
1.116 bowersj2 2323: # Check: Was this only one resource, a map?
2324: if ($resourceCount == 1 && $resource->is_map() && !$self->{FORCE_TOP}) {
2325: my $firstResource = $resource->map_start();
2326: my $finishResource = $resource->map_finish();
2327: return
2328: Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
2329: $finishResource, $self->{FILTER},
2330: $self->{ALREADY_SEEN},
2331: $self->{CONDITION}, 0);
2332:
2333: }
2334:
1.98 bowersj2 2335: # Set up some bookkeeping information.
2336: $self->{CURRENT_DEPTH} = 0;
2337: $self->{MAX_DEPTH} = $maxDepth;
2338: $self->{STACK} = [];
2339: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2340:
2341: for (my $i = 0; $i <= $self->{MAX_DEPTH}; $i++) {
2342: push @{$self->{STACK}}, [];
2343: }
2344:
1.107 bowersj2 2345: # Prime the recursion w/ the first resource **5**
1.98 bowersj2 2346: push @{$self->{STACK}->[0]}, $self->{FIRST_RESOURCE};
2347: $self->{ALREADY_SEEN}->{$self->{FIRST_RESOURCE}->{ID}} = 1;
2348:
2349: bless ($self);
2350:
2351: return $self;
2352: }
2353:
2354: sub next {
2355: my $self = shift;
1.162 bowersj2 2356:
2357: # If we want to return the top-level map object, and haven't yet,
2358: # do so.
2359: if ($self->{RETURN_0} && !$self->{HAVE_RETURNED_0}) {
2360: $self->{HAVE_RETURNED_0} = 1;
2361: return $self->{NAV_MAP}->getById('0.0');
2362: }
1.98 bowersj2 2363:
2364: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
2365: # grab the next from the recursive iterator
2366: my $next = $self->{RECURSIVE_ITERATOR}->next();
2367:
2368: # is it a begin or end map? If so, update the depth
2369: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
2370: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
2371:
2372: # Are we back at depth 0? If so, stop recursing
2373: if ($self->{RECURSIVE_DEPTH} == 0) {
2374: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2375: }
2376:
2377: return $next;
2378: }
2379:
2380: if (defined($self->{FORCE_NEXT})) {
2381: my $tmp = $self->{FORCE_NEXT};
2382: $self->{FORCE_NEXT} = undef;
2383: return $tmp;
2384: }
2385:
2386: # Have we not yet begun? If not, return BEGIN_MAP and
2387: # remember we've started.
2388: if ( !$self->{STARTED} ) {
2389: $self->{STARTED} = 1;
2390: return $self->BEGIN_MAP();
2391: }
2392:
2393: # Here's the guts of the iterator.
2394:
2395: # Find the next resource, if any.
2396: my $found = 0;
2397: my $i = $self->{MAX_DEPTH};
2398: my $newDepth;
2399: my $here;
2400: while ( $i >= 0 && !$found ) {
1.107 bowersj2 2401: if ( scalar(@{$self->{STACK}->[$i]}) > 0 ) { # **6**
2402: $here = pop @{$self->{STACK}->[$i]}; # **7**
1.98 bowersj2 2403: $found = 1;
2404: $newDepth = $i;
2405: }
2406: $i--;
2407: }
2408:
2409: # If we still didn't find anything, we're done.
2410: if ( !$found ) {
2411: # We need to get back down to the correct branch depth
2412: if ( $self->{CURRENT_DEPTH} > 0 ) {
2413: $self->{CURRENT_DEPTH}--;
2414: return END_BRANCH();
2415: } else {
2416: return END_MAP();
2417: }
2418: }
2419:
1.104 bowersj2 2420: # If this is not a resource, it must be an END_BRANCH marker we want
2421: # to return directly.
1.107 bowersj2 2422: if (!ref($here)) { # **8**
1.104 bowersj2 2423: if ($here == END_BRANCH()) { # paranoia, in case of later extension
2424: $self->{CURRENT_DEPTH}--;
2425: return $here;
2426: }
2427: }
2428:
2429: # Otherwise, it is a resource and it's safe to store in $self->{HERE}
2430: $self->{HERE} = $here;
2431:
1.98 bowersj2 2432: # Get to the right level
2433: if ( $self->{CURRENT_DEPTH} > $newDepth ) {
2434: push @{$self->{STACK}->[$newDepth]}, $here;
2435: $self->{CURRENT_DEPTH}--;
2436: return END_BRANCH();
2437: }
2438: if ( $self->{CURRENT_DEPTH} < $newDepth) {
2439: push @{$self->{STACK}->[$newDepth]}, $here;
2440: $self->{CURRENT_DEPTH}++;
2441: return BEGIN_BRANCH();
2442: }
2443:
2444: # If we made it here, we have the next resource, and we're at the
2445: # right branch level. So let's examine the resource for where
2446: # we can get to from here.
2447:
2448: # So we need to look at all the resources we can get to from here,
2449: # categorize them if we haven't seen them, remember if we have a new
2450: my $nextUnfiltered = $here->getNext();
1.104 bowersj2 2451: my $maxDepthAdded = -1;
2452:
1.98 bowersj2 2453: for (@$nextUnfiltered) {
2454: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
1.104 bowersj2 2455: my $depth = $_->{DATA}->{DISPLAY_DEPTH};
2456: push @{$self->{STACK}->[$depth]}, $_;
1.98 bowersj2 2457: $self->{ALREADY_SEEN}->{$_->{ID}} = 1;
1.104 bowersj2 2458: if ($maxDepthAdded < $depth) { $maxDepthAdded = $depth; }
1.98 bowersj2 2459: }
2460: }
1.104 bowersj2 2461:
2462: # Is this the end of a branch? If so, all of the resources examined above
2463: # led to lower levels then the one we are currently at, so we push a END_BRANCH
2464: # marker onto the stack so we don't forget.
2465: # Example: For the usual A(BC)(DE)F case, when the iterator goes down the
2466: # BC branch and gets to C, it will see F as the only next resource, but it's
2467: # one level lower. Thus, this is the end of the branch, since there are no
2468: # more resources added to this level or above.
1.111 bowersj2 2469: # We don't do this if the examined resource is the finish resource,
2470: # because the condition given above is true, but the "END_MAP" will
2471: # take care of things and we should already be at depth 0.
1.104 bowersj2 2472: my $isEndOfBranch = $maxDepthAdded < $self->{CURRENT_DEPTH};
1.111 bowersj2 2473: if ($isEndOfBranch && $here != $self->{FINISH_RESOURCE}) { # **9**
1.104 bowersj2 2474: push @{$self->{STACK}->[$self->{CURRENT_DEPTH}]}, END_BRANCH();
2475: }
2476:
1.98 bowersj2 2477: # That ends the main iterator logic. Now, do we want to recurse
2478: # down this map (if this resource is a map)?
2479: if ($self->{HERE}->is_map() &&
2480: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
2481: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
2482: my $firstResource = $self->{HERE}->map_start();
2483: my $finishResource = $self->{HERE}->map_finish();
2484:
2485: $self->{RECURSIVE_ITERATOR} =
2486: Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
2487: $finishResource, $self->{FILTER},
2488: $self->{ALREADY_SEEN}, $self->{CONDITION});
2489: }
2490:
1.116 bowersj2 2491: # If this is a blank resource, don't actually return it.
1.117 bowersj2 2492: # Should you ever find you need it, make sure to add an option to the code
2493: # that you can use; other things depend on this behavior.
1.138 bowersj2 2494: my $browsePriv = $self->{HERE}->browsePriv();
2495: if (!$self->{HERE}->src() ||
2496: (!($browsePriv eq 'F') && !($browsePriv eq '2')) ) {
1.116 bowersj2 2497: return $self->next();
2498: }
2499:
1.98 bowersj2 2500: return $self->{HERE};
2501:
2502: }
2503:
2504: =pod
2505:
1.174 albertel 2506: The other method available on the iterator is B<getStack>, which
2507: returns an array populated with the current 'stack' of maps, as
2508: references to the resource objects. Example: This is useful when
2509: making the navigation map, as we need to check whether we are under a
2510: page map to see if we need to link directly to the resource, or to the
2511: page. The first elements in the array will correspond to the top of
2512: the stack (most inclusive map).
1.98 bowersj2 2513:
2514: =cut
2515:
2516: sub getStack {
2517: my $self=shift;
2518:
2519: my @stack;
2520:
2521: $self->populateStack(\@stack);
2522:
2523: return \@stack;
2524: }
2525:
2526: # Private method: Calls the iterators recursively to populate the stack.
2527: sub populateStack {
2528: my $self=shift;
2529: my $stack = shift;
2530:
2531: push @$stack, $self->{HERE} if ($self->{HERE});
2532:
2533: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
2534: $self->{RECURSIVE_ITERATOR}->populateStack($stack);
2535: }
1.94 bowersj2 2536: }
2537:
2538: 1;
2539:
2540: package Apache::lonnavmaps::DFSiterator;
2541:
1.100 bowersj2 2542: # Not documented in the perldoc: This is a simple iterator that just walks
2543: # through the nav map and presents the resources in a depth-first search
2544: # fashion, ignorant of conditionals, randomized resources, etc. It presents
2545: # BEGIN_MAP and END_MAP, but does not understand branches at all. It is
2546: # useful for pre-processing of some kind, and is in fact used by the main
2547: # iterator that way, but that's about it.
2548: # One could imagine merging this into the init routine of the main iterator,
2549: # but this might as well be left seperate, since it is possible some other
2550: # use might be found for it. - Jeremy
1.94 bowersj2 2551:
1.117 bowersj2 2552: # Unlike the main iterator, this DOES return all resources, even blank ones.
2553: # The main iterator needs them to correctly preprocess the map.
2554:
1.94 bowersj2 2555: sub BEGIN_MAP { return 1; } # begining of a new map
2556: sub END_MAP { return 2; } # end of the map
2557: sub FORWARD { return 1; } # go forward
2558: sub BACKWARD { return 2; }
2559:
1.100 bowersj2 2560: # Params: Nav map ref, first resource id/ref, finish resource id/ref,
2561: # filter hash ref (or undef), already seen hash or undef, condition
2562: # (as in main iterator), direction FORWARD or BACKWARD (undef->forward).
1.51 bowersj2 2563: sub new {
2564: # magic invocation to create a class instance
2565: my $proto = shift;
2566: my $class = ref($proto) || $proto;
2567: my $self = {};
2568:
2569: $self->{NAV_MAP} = shift;
2570: return undef unless ($self->{NAV_MAP});
2571:
2572: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
2573: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
2574:
2575: # If the given resources are just the ID of the resource, get the
2576: # objects
2577: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
2578: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
2579: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
2580: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
2581:
2582: $self->{FILTER} = shift;
2583:
2584: # A hash, used as a set, of resource already seen
2585: $self->{ALREADY_SEEN} = shift;
1.140 bowersj2 2586: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1.63 bowersj2 2587: $self->{CONDITION} = shift;
1.89 bowersj2 2588: $self->{DIRECTION} = shift || FORWARD();
1.51 bowersj2 2589:
1.100 bowersj2 2590: # Flag: Have we started yet?
1.51 bowersj2 2591: $self->{STARTED} = 0;
2592:
2593: # Should we continue calling the recursive iterator, if any?
2594: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2595: # The recursive iterator, if any
2596: $self->{RECURSIVE_ITERATOR} = undef;
2597: # Are we recursing on a map, or a branch?
2598: $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
2599: # And the count of how deep it is, so that this iterator can keep track of
2600: # when to pick back up again.
2601: $self->{RECURSIVE_DEPTH} = 0;
2602:
2603: # For keeping track of our branches, we maintain our own stack
1.100 bowersj2 2604: $self->{STACK} = [];
1.51 bowersj2 2605:
2606: # Start with the first resource
1.89 bowersj2 2607: if ($self->{DIRECTION} == FORWARD) {
1.100 bowersj2 2608: push @{$self->{STACK}}, $self->{FIRST_RESOURCE};
1.89 bowersj2 2609: } else {
1.100 bowersj2 2610: push @{$self->{STACK}}, $self->{FINISH_RESOURCE};
1.89 bowersj2 2611: }
1.51 bowersj2 2612:
2613: bless($self);
2614: return $self;
2615: }
2616:
2617: sub next {
2618: my $self = shift;
2619:
2620: # Are we using a recursive iterator? If so, pull from that and
2621: # watch the depth; we want to resume our level at the correct time.
1.98 bowersj2 2622: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1.51 bowersj2 2623: # grab the next from the recursive iterator
2624: my $next = $self->{RECURSIVE_ITERATOR}->next();
2625:
2626: # is it a begin or end map? Update depth if so
2627: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
2628: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
2629:
2630: # Are we back at depth 0? If so, stop recursing.
2631: if ($self->{RECURSIVE_DEPTH} == 0) {
2632: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
2633: }
2634:
2635: return $next;
2636: }
2637:
2638: # Is there a current resource to grab? If not, then return
1.100 bowersj2 2639: # END_MAP, which will end the iterator.
2640: if (scalar(@{$self->{STACK}}) == 0) {
2641: return $self->END_MAP();
1.51 bowersj2 2642: }
2643:
2644: # Have we not yet begun? If not, return BEGIN_MAP and
2645: # remember that we've started.
2646: if ( !$self->{STARTED} ) {
2647: $self->{STARTED} = 1;
2648: return $self->BEGIN_MAP;
2649: }
2650:
2651: # Get the next resource in the branch
1.100 bowersj2 2652: $self->{HERE} = pop @{$self->{STACK}};
1.52 bowersj2 2653:
1.100 bowersj2 2654: # remember that we've seen this, so we don't return it again later
1.51 bowersj2 2655: $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
2656:
2657: # Get the next possible resources
1.90 bowersj2 2658: my $nextUnfiltered;
1.89 bowersj2 2659: if ($self->{DIRECTION} == FORWARD()) {
1.90 bowersj2 2660: $nextUnfiltered = $self->{HERE}->getNext();
1.89 bowersj2 2661: } else {
1.90 bowersj2 2662: $nextUnfiltered = $self->{HERE}->getPrevious();
1.89 bowersj2 2663: }
1.51 bowersj2 2664: my $next = [];
2665:
2666: # filter the next possibilities to remove things we've
1.100 bowersj2 2667: # already seen.
1.51 bowersj2 2668: foreach (@$nextUnfiltered) {
1.52 bowersj2 2669: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
2670: push @$next, $_;
2671: }
1.51 bowersj2 2672: }
2673:
2674: while (@$next) {
1.100 bowersj2 2675: # copy the next possibilities over to the stack
2676: push @{$self->{STACK}}, shift @$next;
1.51 bowersj2 2677: }
2678:
2679: # If this is a map and we want to recurse down it... (not filtered out)
1.70 bowersj2 2680: if ($self->{HERE}->is_map() &&
1.63 bowersj2 2681: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
1.51 bowersj2 2682: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
2683: my $firstResource = $self->{HERE}->map_start();
2684: my $finishResource = $self->{HERE}->map_finish();
2685:
2686: $self->{RECURSIVE_ITERATOR} =
1.94 bowersj2 2687: Apache::lonnavmaps::DFSiterator->new ($self->{NAV_MAP}, $firstResource,
1.63 bowersj2 2688: $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
1.91 bowersj2 2689: $self->{CONDITION}, $self->{DIRECTION});
1.51 bowersj2 2690: }
2691:
2692: return $self->{HERE};
1.190 bowersj2 2693: }
2694:
2695: # Identical to the full iterator methods of the same name. Hate to copy/paste
2696: # but I also hate to "inherit" either iterator from the other.
2697:
2698: sub getStack {
2699: my $self=shift;
2700:
2701: my @stack;
2702:
2703: $self->populateStack(\@stack);
2704:
2705: return \@stack;
2706: }
2707:
2708: # Private method: Calls the iterators recursively to populate the stack.
2709: sub populateStack {
2710: my $self=shift;
2711: my $stack = shift;
2712:
2713: push @$stack, $self->{HERE} if ($self->{HERE});
2714:
2715: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
2716: $self->{RECURSIVE_ITERATOR}->populateStack($stack);
2717: }
1.51 bowersj2 2718: }
2719:
1.1 www 2720: 1;
1.2 www 2721:
1.51 bowersj2 2722: package Apache::lonnavmaps::resource;
2723:
2724: use Apache::lonnet;
2725:
2726: =pod
2727:
2728: =head1 Object: resource
2729:
1.174 albertel 2730: A resource object encapsulates a resource in a resource map, allowing
2731: easy manipulation of the resource, querying the properties of the
2732: resource (including user properties), and represents a reference that
2733: can be used as the canonical representation of the resource by
2734: lonnavmap clients like renderers.
2735:
2736: A resource only makes sense in the context of a navmap, as some of the
2737: data is stored in the navmap object.
2738:
2739: You will probably never need to instantiate this object directly. Use
2740: Apache::lonnavmaps::navmap, and use the "start" method to obtain the
2741: starting resource.
1.51 bowersj2 2742:
1.188 bowersj2 2743: Resource objects respect the parameter_hiddenparts, which suppresses
2744: various parts according to the wishes of the map author. As of this
2745: writing, there is no way to override this parameter, and suppressed
2746: parts will never be returned, nor will their response types or ids be
2747: stored.
2748:
1.51 bowersj2 2749: =head2 Public Members
2750:
1.174 albertel 2751: resource objects have a hash called DATA ($resourceRef->{DATA}) that
2752: you can store whatever you want in. This allows you to easily do
2753: two-pass algorithms without worrying about managing your own
2754: resource->data hash.
1.51 bowersj2 2755:
2756: =head2 Methods
2757:
2758: =over 4
2759:
1.174 albertel 2760: =item * B<new>($navmapRef, $idString):
2761:
2762: The first arg is a reference to the parent navmap object. The second
2763: is the idString of the resource itself. Very rarely, if ever, called
2764: directly. Use the nav map->getByID() method.
1.70 bowersj2 2765:
2766: =back
1.51 bowersj2 2767:
2768: =cut
2769:
2770: sub new {
2771: # magic invocation to create a class instance
2772: my $proto = shift;
2773: my $class = ref($proto) || $proto;
2774: my $self = {};
2775:
2776: $self->{NAV_MAP} = shift;
2777: $self->{ID} = shift;
2778:
2779: # Store this new resource in the parent nav map's cache.
2780: $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
1.66 bowersj2 2781: $self->{RESOURCE_ERROR} = 0;
1.51 bowersj2 2782:
2783: # A hash that can be used by two-pass algorithms to store data
2784: # about this resource in. Not used by the resource object
2785: # directly.
2786: $self->{DATA} = {};
2787:
2788: bless($self);
2789:
2790: return $self;
2791: }
2792:
1.70 bowersj2 2793: # private function: simplify the NAV_HASH lookups we keep doing
2794: # pass the name, and to automatically append my ID, pass a true val on the
2795: # second param
2796: sub navHash {
2797: my $self = shift;
2798: my $param = shift;
2799: my $id = shift;
1.115 bowersj2 2800: return $self->{NAV_MAP}->navhash($param . ($id?$self->{ID}:""));
1.70 bowersj2 2801: }
2802:
1.51 bowersj2 2803: =pod
2804:
1.70 bowersj2 2805: B<Metadata Retreival>
2806:
1.174 albertel 2807: These are methods that help you retrieve metadata about the resource:
2808: Method names are based on the fields in the compiled course
2809: representation.
1.70 bowersj2 2810:
2811: =over 4
2812:
1.174 albertel 2813: =item * B<compTitle>:
2814:
2815: Returns a "composite title", that is equal to $res->title() if the
2816: resource has a title, and is otherwise the last part of the URL (e.g.,
2817: "problem.problem").
2818:
2819: =item * B<ext>:
2820:
2821: Returns true if the resource is external.
2822:
2823: =item * B<goesto>:
2824:
2825: Returns the "goesto" value from the compiled nav map. (It is likely
2826: you want to use B<getNext> instead.)
2827:
2828: =item * B<kind>:
2829:
2830: Returns the kind of the resource from the compiled nav map.
2831:
2832: =item * B<randomout>:
1.106 bowersj2 2833:
1.174 albertel 2834: Returns true if this resource was chosen to NOT be shown to the user
2835: by the random map selection feature. In other words, this is usually
2836: false.
1.70 bowersj2 2837:
1.174 albertel 2838: =item * B<randompick>:
1.70 bowersj2 2839:
1.174 albertel 2840: Returns true for a map if the randompick feature is being used on the
2841: map. (?)
1.70 bowersj2 2842:
1.174 albertel 2843: =item * B<src>:
1.51 bowersj2 2844:
1.174 albertel 2845: Returns the source for the resource.
1.70 bowersj2 2846:
1.174 albertel 2847: =item * B<symb>:
1.70 bowersj2 2848:
1.174 albertel 2849: Returns the symb for the resource.
1.70 bowersj2 2850:
1.174 albertel 2851: =item * B<title>:
1.70 bowersj2 2852:
1.174 albertel 2853: Returns the title of the resource.
2854:
2855: =item * B<to>:
2856:
2857: Returns the "to" value from the compiled nav map. (It is likely you
2858: want to use B<getNext> instead.)
1.51 bowersj2 2859:
2860: =back
2861:
2862: =cut
2863:
2864: # These info functions can be used directly, as they don't return
2865: # resource information.
1.85 bowersj2 2866: sub comesfrom { my $self=shift; return $self->navHash("comesfrom_", 1); }
1.70 bowersj2 2867: sub ext { my $self=shift; return $self->navHash("ext_", 1) eq 'true:'; }
1.85 bowersj2 2868: sub from { my $self=shift; return $self->navHash("from_", 1); }
1.51 bowersj2 2869: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
2870: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
1.68 bowersj2 2871: sub randomout { my $self=shift; return $self->navHash("randomout_", 1); }
2872: sub randompick {
2873: my $self = shift;
2874: return $self->{NAV_MAP}->{PARM_HASH}->{$self->symb .
2875: '.0.parameter_randompick'};
2876: }
1.51 bowersj2 2877: sub src {
2878: my $self=shift;
2879: return $self->navHash("src_", 1);
2880: }
2881: sub symb {
2882: my $self=shift;
2883: (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
2884: my $symbSrc = &Apache::lonnet::declutter($self->src());
2885: return &Apache::lonnet::declutter(
2886: $self->navHash('map_id_'.$first))
2887: . '___' . $second . '___' . $symbSrc;
2888: }
1.70 bowersj2 2889: sub title { my $self=shift; return $self->navHash("title_", 1); }
2890: sub to { my $self=shift; return $self->navHash("to_", 1); }
1.106 bowersj2 2891: sub compTitle {
2892: my $self = shift;
2893: my $title = $self->title();
1.176 www 2894: $title=~s/\&colon\;/\:/gs;
1.106 bowersj2 2895: if (!$title) {
2896: $title = $self->src();
2897: $title = substr($title, rindex($title, '/') + 1);
2898: }
2899: return $title;
2900: }
1.70 bowersj2 2901: =pod
2902:
2903: B<Predicate Testing the Resource>
2904:
2905: These methods are shortcuts to deciding if a given resource has a given property.
2906:
2907: =over 4
2908:
1.174 albertel 2909: =item * B<is_map>:
2910:
2911: Returns true if the resource is a map type.
2912:
2913: =item * B<is_problem>:
1.70 bowersj2 2914:
1.174 albertel 2915: Returns true if the resource is a problem type, false
2916: otherwise. (Looks at the extension on the src field; might need more
2917: to work correctly.)
1.70 bowersj2 2918:
1.174 albertel 2919: =item * B<is_page>:
1.70 bowersj2 2920:
1.174 albertel 2921: Returns true if the resource is a page.
2922:
2923: =item * B<is_sequence>:
2924:
2925: Returns true if the resource is a sequence.
1.70 bowersj2 2926:
2927: =back
2928:
2929: =cut
2930:
2931:
2932: sub is_html {
1.51 bowersj2 2933: my $self=shift;
2934: my $src = $self->src();
1.70 bowersj2 2935: return ($src =~ /html$/);
1.51 bowersj2 2936: }
1.70 bowersj2 2937: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
2938: sub is_page {
1.51 bowersj2 2939: my $self=shift;
2940: my $src = $self->src();
1.70 bowersj2 2941: return ($src =~ /page$/);
1.51 bowersj2 2942: }
1.70 bowersj2 2943: sub is_problem {
1.51 bowersj2 2944: my $self=shift;
2945: my $src = $self->src();
1.70 bowersj2 2946: return ($src =~ /problem$/);
1.51 bowersj2 2947: }
1.70 bowersj2 2948: sub is_sequence {
1.51 bowersj2 2949: my $self=shift;
2950: my $src = $self->src();
1.70 bowersj2 2951: return ($src =~ /sequence$/);
1.51 bowersj2 2952: }
2953:
1.101 bowersj2 2954: # Private method: Shells out to the parmval in the nav map, handler parts.
1.51 bowersj2 2955: sub parmval {
2956: my $self = shift;
2957: my $what = shift;
1.185 bowersj2 2958: my $part = shift;
2959: if (!defined($part)) {
2960: $part = '0';
2961: }
1.51 bowersj2 2962: return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
2963: }
2964:
1.70 bowersj2 2965: =pod
2966:
2967: B<Map Methods>
2968:
1.174 albertel 2969: These methods are useful for getting information about the map
2970: properties of the resource, if the resource is a map (B<is_map>).
1.70 bowersj2 2971:
2972: =over 4
2973:
1.174 albertel 2974: =item * B<map_finish>:
2975:
2976: Returns a reference to a resource object corresponding to the finish
2977: resource of the map.
1.70 bowersj2 2978:
1.174 albertel 2979: =item * B<map_pc>:
1.70 bowersj2 2980:
1.174 albertel 2981: Returns the pc value of the map, which is the first number that
2982: appears in the resource ID of the resources in the map, and is the
2983: number that appears around the middle of the symbs of the resources in
2984: that map.
1.70 bowersj2 2985:
1.174 albertel 2986: =item * B<map_start>:
2987:
2988: Returns a reference to a resource object corresponding to the start
2989: resource of the map.
2990:
2991: =item * B<map_type>:
2992:
2993: Returns a string with the type of the map in it.
1.70 bowersj2 2994:
2995: =back
1.51 bowersj2 2996:
1.70 bowersj2 2997: =cut
1.51 bowersj2 2998:
2999: sub map_finish {
3000: my $self = shift;
3001: my $src = $self->src();
1.144 bowersj2 3002: $src = Apache::lonnet::clutter($src);
1.51 bowersj2 3003: my $res = $self->navHash("map_finish_$src", 0);
3004: $res = $self->{NAV_MAP}->getById($res);
3005: return $res;
3006: }
1.70 bowersj2 3007: sub map_pc {
3008: my $self = shift;
3009: my $src = $self->src();
3010: return $self->navHash("map_pc_$src", 0);
3011: }
1.51 bowersj2 3012: sub map_start {
3013: my $self = shift;
3014: my $src = $self->src();
1.144 bowersj2 3015: $src = Apache::lonnet::clutter($src);
1.51 bowersj2 3016: my $res = $self->navHash("map_start_$src", 0);
3017: $res = $self->{NAV_MAP}->getById($res);
3018: return $res;
3019: }
3020: sub map_type {
3021: my $self = shift;
3022: my $pc = $self->map_pc();
3023: return $self->navHash("map_type_$pc", 0);
3024: }
3025:
3026:
3027:
3028: #####
3029: # Property queries
3030: #####
3031:
3032: # These functions will be responsible for returning the CORRECT
3033: # VALUE for the parameter, no matter what. So while they may look
3034: # like direct calls to parmval, they can be more then that.
3035: # So, for instance, the duedate function should use the "duedatetype"
3036: # information, rather then the resource object user.
3037:
3038: =pod
3039:
3040: =head2 Resource Parameters
3041:
1.174 albertel 3042: In order to use the resource parameters correctly, the nav map must
3043: have been instantiated with genCourseAndUserOptions set to true, so
3044: the courseopt and useropt is read correctly. Then, you can call these
3045: functions to get the relevant parameters for the resource. Each
3046: function defaults to part "0", but can be directed to another part by
3047: passing the part as the parameter.
3048:
3049: These methods are responsible for getting the parameter correct, not
3050: merely reflecting the contents of the GDBM hashes. As we move towards
3051: dates relative to other dates, these methods should be updated to
3052: reflect that. (Then, anybody using these methods will not have to update
3053: their code.)
3054:
3055: =over 4
3056:
3057: =item * B<acc>:
3058:
3059: Get the Client IP/Name Access Control information.
1.51 bowersj2 3060:
1.174 albertel 3061: =item * B<answerdate>:
1.51 bowersj2 3062:
1.174 albertel 3063: Get the answer-reveal date for the problem.
3064:
3065: =item * B<duedate>:
3066:
3067: Get the due date for the problem.
3068:
3069: =item * B<tries>:
3070:
3071: Get the number of tries the student has used on the problem.
3072:
3073: =item * B<maxtries>:
3074:
3075: Get the number of max tries allowed.
3076:
3077: =item * B<opendate>:
1.51 bowersj2 3078:
1.174 albertel 3079: Get the open date for the problem.
1.51 bowersj2 3080:
1.174 albertel 3081: =item * B<sig>:
1.51 bowersj2 3082:
1.174 albertel 3083: Get the significant figures setting.
1.51 bowersj2 3084:
1.174 albertel 3085: =item * B<tol>:
1.51 bowersj2 3086:
1.174 albertel 3087: Get the tolerance for the problem.
1.51 bowersj2 3088:
1.174 albertel 3089: =item * B<tries>:
1.51 bowersj2 3090:
1.174 albertel 3091: Get the number of tries the user has already used on the problem.
1.51 bowersj2 3092:
1.174 albertel 3093: =item * B<type>:
1.51 bowersj2 3094:
1.174 albertel 3095: Get the question type for the problem.
1.70 bowersj2 3096:
1.174 albertel 3097: =item * B<weight>:
1.51 bowersj2 3098:
1.174 albertel 3099: Get the weight for the problem.
1.51 bowersj2 3100:
3101: =back
3102:
3103: =cut
3104:
3105: sub acc {
3106: (my $self, my $part) = @_;
3107: return $self->parmval("acc", $part);
3108: }
3109: sub answerdate {
3110: (my $self, my $part) = @_;
3111: # Handle intervals
3112: if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
3113: return $self->duedate($part) +
3114: $self->parmval("answerdate", $part);
3115: }
3116: return $self->parmval("answerdate", $part);
1.106 bowersj2 3117: }
1.108 bowersj2 3118: sub awarded { my $self = shift; return $self->queryRestoreHash('awarded', shift); }
1.51 bowersj2 3119: sub duedate {
3120: (my $self, my $part) = @_;
3121: return $self->parmval("duedate", $part);
3122: }
3123: sub maxtries {
3124: (my $self, my $part) = @_;
3125: return $self->parmval("maxtries", $part);
3126: }
3127: sub opendate {
3128: (my $self, my $part) = @_;
3129: if ($self->parmval("opendate.type", $part) eq 'date_interval') {
3130: return $self->duedate($part) -
3131: $self->parmval("opendate", $part);
3132: }
3133: return $self->parmval("opendate");
3134: }
1.185 bowersj2 3135: sub problemstatus {
3136: (my $self, my $part) = @_;
3137: return $self->parmval("problemstatus", $part);
3138: }
1.51 bowersj2 3139: sub sig {
3140: (my $self, my $part) = @_;
3141: return $self->parmval("sig", $part);
3142: }
3143: sub tol {
3144: (my $self, my $part) = @_;
3145: return $self->parmval("tol", $part);
3146: }
1.108 bowersj2 3147: sub tries {
3148: my $self = shift;
3149: my $tries = $self->queryRestoreHash('tries', shift);
3150: if (!defined($tries)) { return '0';}
1.51 bowersj2 3151: return $tries;
3152: }
1.70 bowersj2 3153: sub type {
3154: (my $self, my $part) = @_;
3155: return $self->parmval("type", $part);
3156: }
1.109 bowersj2 3157: sub weight {
3158: my $self = shift; my $part = shift;
1.70 bowersj2 3159: return $self->parmval("weight", $part);
3160: }
1.51 bowersj2 3161:
3162: # Multiple things need this
3163: sub getReturnHash {
3164: my $self = shift;
3165:
3166: if (!defined($self->{RETURN_HASH})) {
1.84 bowersj2 3167: my %tmpHash = &Apache::lonnet::restore($self->symb());
1.51 bowersj2 3168: $self->{RETURN_HASH} = \%tmpHash;
3169: }
3170: }
3171:
3172: ######
3173: # Status queries
3174: ######
3175:
3176: # These methods query the status of problems.
3177:
3178: # If we need to count parts, this function determines the number of
3179: # parts from the metadata. When called, it returns a reference to a list
3180: # of strings corresponding to the parts. (Thus, using it in a scalar context
3181: # tells you how many parts you have in the problem:
3182: # $partcount = scalar($resource->countParts());
3183: # Don't use $self->{PARTS} directly because you don't know if it's been
3184: # computed yet.
3185:
3186: =pod
3187:
3188: =head2 Resource misc
3189:
3190: Misc. functions for the resource.
3191:
3192: =over 4
3193:
1.174 albertel 3194: =item * B<hasDiscussion>:
1.59 bowersj2 3195:
1.174 albertel 3196: Returns a false value if there has been discussion since the user last
3197: logged in, true if there has. Always returns false if the discussion
3198: data was not extracted when the nav map was constructed.
3199:
3200: =item * B<getFeedback>:
3201:
3202: Gets the feedback for the resource and returns the raw feedback string
3203: for the resource, or the null string if there is no feedback or the
3204: email data was not extracted when the nav map was constructed. Usually
3205: used like this:
1.59 bowersj2 3206:
3207: for (split(/\,/, $res->getFeedback())) {
3208: my $link = &Apache::lonnet::escape($_);
3209: ...
3210:
3211: and use the link as appropriate.
3212:
3213: =cut
3214:
3215: sub hasDiscussion {
3216: my $self = shift;
3217: return $self->{NAV_MAP}->hasDiscussion($self->symb());
3218: }
3219:
3220: sub getFeedback {
3221: my $self = shift;
1.124 bowersj2 3222: my $source = $self->src();
1.125 bowersj2 3223: if ($source =~ /^\/res\//) { $source = substr $source, 5; }
1.124 bowersj2 3224: return $self->{NAV_MAP}->getFeedback($source);
3225: }
3226:
3227: sub getErrors {
3228: my $self = shift;
3229: my $source = $self->src();
3230: if ($source =~ /^\/res\//) { $source = substr $source, 5; }
3231: return $self->{NAV_MAP}->getErrors($source);
1.59 bowersj2 3232: }
3233:
3234: =pod
3235:
1.174 albertel 3236: =item * B<parts>():
1.51 bowersj2 3237:
1.174 albertel 3238: Returns a list reference containing sorted strings corresponding to
1.193 bowersj2 3239: each part of the problem. Single part problems have only a part '0'.
3240: Multipart problems do not return their part '0', since they typically
3241: do not really matter.
1.174 albertel 3242:
3243: =item * B<countParts>():
3244:
3245: Returns the number of parts of the problem a student can answer. Thus,
3246: for single part problems, returns 1. For multipart, it returns the
1.193 bowersj2 3247: number of parts in the problem, not including psuedo-part 0.
1.51 bowersj2 3248:
1.192 bowersj2 3249: =item * B<multipart>():
3250:
1.193 bowersj2 3251: Returns true if the problem is multipart, false otherwise. Use this instead
3252: of countParts if all you want is multipart/not multipart.
1.192 bowersj2 3253:
1.187 bowersj2 3254: =item * B<responseType>($part):
3255:
3256: Returns the response type of the part, without the word "response" on the
3257: end. Example return values: 'string', 'essay', 'numeric', etc.
3258:
1.193 bowersj2 3259: =item * B<responseIds>($part):
1.187 bowersj2 3260:
1.193 bowersj2 3261: Retreives the response IDs for the given part as an array reference containing
3262: strings naming the response IDs. This may be empty.
1.187 bowersj2 3263:
1.51 bowersj2 3264: =back
3265:
3266: =cut
3267:
3268: sub parts {
3269: my $self = shift;
3270:
1.192 bowersj2 3271: if ($self->ext) { return []; }
1.67 bowersj2 3272:
1.51 bowersj2 3273: $self->extractParts();
3274: return $self->{PARTS};
3275: }
3276:
3277: sub countParts {
3278: my $self = shift;
3279:
3280: my $parts = $self->parts();
1.191 bowersj2 3281:
3282: # If I left this here, then it's not necessary.
3283: #my $delta = 0;
3284: #for my $part (@$parts) {
3285: # if ($part eq '0') { $delta--; }
3286: #}
1.66 bowersj2 3287:
3288: if ($self->{RESOURCE_ERROR}) {
3289: return 0;
3290: }
3291:
1.191 bowersj2 3292: return scalar(@{$parts}); # + $delta;
1.192 bowersj2 3293: }
3294:
3295: sub multipart {
3296: my $self = shift;
3297: return $self->countParts() > 1;
1.51 bowersj2 3298: }
3299:
1.187 bowersj2 3300: sub responseType {
1.184 bowersj2 3301: my $self = shift;
3302: my $part = shift;
3303:
3304: $self->extractParts();
1.187 bowersj2 3305: return $self->{RESPONSE_TYPE}->{$part};
3306: }
3307:
1.193 bowersj2 3308: sub responseIds {
1.187 bowersj2 3309: my $self = shift;
3310: my $part = shift;
3311:
3312: $self->extractParts();
3313: return $self->{RESPONSE_IDS}->{$part};
1.184 bowersj2 3314: }
3315:
3316: # Private function: Extracts the parts information, both part names and
1.187 bowersj2 3317: # part types, and saves it.
1.51 bowersj2 3318: sub extractParts {
3319: my $self = shift;
3320:
1.153 bowersj2 3321: return if (defined($self->{PARTS}));
1.67 bowersj2 3322: return if ($self->ext);
1.51 bowersj2 3323:
3324: $self->{PARTS} = [];
3325:
1.181 bowersj2 3326: my %parts;
3327:
1.82 bowersj2 3328: # Retrieve part count, if this is a problem
3329: if ($self->is_problem()) {
1.152 matthew 3330: my $metadata = &Apache::lonnet::metadata($self->src(), 'packages');
1.82 bowersj2 3331: if (!$metadata) {
3332: $self->{RESOURCE_ERROR} = 1;
3333: $self->{PARTS} = [];
1.184 bowersj2 3334: $self->{PART_TYPE} = {};
1.82 bowersj2 3335: return;
3336: }
3337: foreach (split(/\,/,$metadata)) {
1.152 matthew 3338: if ($_ =~ /^part_(.*)$/) {
1.147 bowersj2 3339: my $part = $1;
1.183 bowersj2 3340: # This floods the logs if it blows up
3341: if (defined($parts{$part})) {
3342: Apache::lonnet::logthis("$part multiply defined in metadata for " . $self->symb());
3343: }
1.181 bowersj2 3344:
1.147 bowersj2 3345: # check to see if part is turned off.
1.181 bowersj2 3346:
3347: if (!Apache::loncommon::check_if_partid_hidden($part, $self->symb())) {
3348: $parts{$part} = 1;
1.147 bowersj2 3349: }
1.82 bowersj2 3350: }
1.51 bowersj2 3351: }
1.82 bowersj2 3352:
3353:
1.181 bowersj2 3354: my @sortedParts = sort keys %parts;
1.82 bowersj2 3355: $self->{PARTS} = \@sortedParts;
1.187 bowersj2 3356:
3357: my %responseIdHash;
3358: my %responseTypeHash;
3359:
1.189 bowersj2 3360:
3361: # Init the responseIdHash
3362: foreach (@{$self->{PARTS}}) {
3363: $responseIdHash{$_} = [];
3364: }
3365:
1.187 bowersj2 3366: # Now, the unfortunate thing about this is that parts, part name, and
3367: # response if are delimited by underscores, but both the part
3368: # name and response id can themselves have underscores in them.
3369: # So we have to use our knowlege of part names to figure out
3370: # where the part names begin and end, and even then, it is possible
3371: # to construct ambiguous situations.
3372: foreach (split /,/, $metadata) {
3373: if ($_ =~ /^([a-zA-Z]+)response_(.*)/) {
3374: my $responseType = $1;
3375: my $partStuff = $2;
3376: my $partIdSoFar = '';
3377: my @partChunks = split /_/, $partStuff;
3378: my $i = 0;
3379:
3380: for ($i = 0; $i < scalar(@partChunks); $i++) {
3381: if ($partIdSoFar) { $partIdSoFar .= '_'; }
3382: $partIdSoFar .= $partChunks[$i];
3383: if ($parts{$partIdSoFar}) {
3384: my @otherChunks = @partChunks[$i+1..$#partChunks];
3385: my $responseId = join('_', @otherChunks);
1.188 bowersj2 3386: push @{$responseIdHash{$partIdSoFar}}, $responseId;
1.187 bowersj2 3387: $responseTypeHash{$partIdSoFar} = $responseType;
3388: last;
3389: }
3390: }
3391: }
3392: }
3393:
3394: $self->{RESPONSE_IDS} = \%responseIdHash;
3395: $self->{RESPONSE_TYPES} = \%responseTypeHash;
1.154 bowersj2 3396: }
3397:
1.51 bowersj2 3398: return;
3399: }
3400:
3401: =pod
3402:
3403: =head2 Resource Status
3404:
1.174 albertel 3405: Problem resources have status information, reflecting their various
3406: dates and completion statuses.
1.51 bowersj2 3407:
1.174 albertel 3408: There are two aspects to the status: the date-related information and
3409: the completion information.
1.51 bowersj2 3410:
1.174 albertel 3411: Idiomatic usage of these two methods would probably look something
3412: like
1.51 bowersj2 3413:
3414: foreach ($resource->parts()) {
3415: my $dateStatus = $resource->getDateStatus($_);
3416: my $completionStatus = $resource->getCompletionStatus($_);
3417:
1.70 bowersj2 3418: or
3419:
3420: my $status = $resource->status($_);
3421:
1.51 bowersj2 3422: ... use it here ...
3423: }
3424:
1.174 albertel 3425: Which you use depends on exactly what you are looking for. The
3426: status() function has been optimized for the nav maps display and may
3427: not precisely match what you need elsewhere.
1.101 bowersj2 3428:
1.174 albertel 3429: The symbolic constants shown below can be accessed through the
3430: resource object: C<$res->OPEN>.
1.101 bowersj2 3431:
1.51 bowersj2 3432: =over 4
3433:
1.174 albertel 3434: =item * B<getDateStatus>($part):
3435:
3436: ($part defaults to 0). A convenience function that returns a symbolic
3437: constant telling you about the date status of the part. The possible
3438: return values are:
1.51 bowersj2 3439:
3440: =back
3441:
3442: B<Date Codes>
3443:
3444: =over 4
3445:
1.174 albertel 3446: =item * B<OPEN_LATER>:
3447:
3448: The problem will be opened later.
3449:
3450: =item * B<OPEN>:
3451:
3452: Open and not yet due.
3453:
1.51 bowersj2 3454:
1.174 albertel 3455: =item * B<PAST_DUE_ANSWER_LATER>:
1.51 bowersj2 3456:
1.174 albertel 3457: The due date has passed, but the answer date has not yet arrived.
1.59 bowersj2 3458:
1.174 albertel 3459: =item * B<PAST_DUE_NO_ANSWER>:
1.54 bowersj2 3460:
1.174 albertel 3461: The due date has passed and there is no answer opening date set.
1.51 bowersj2 3462:
1.174 albertel 3463: =item * B<ANSWER_OPEN>:
1.51 bowersj2 3464:
1.174 albertel 3465: The answer date is here.
3466:
3467: =item * B<NETWORK_FAILURE>:
3468:
3469: The information is unknown due to network failure.
1.51 bowersj2 3470:
3471: =back
3472:
3473: =cut
3474:
3475: # Apparently the compiler optimizes these into constants automatically
1.54 bowersj2 3476: sub OPEN_LATER { return 0; }
3477: sub OPEN { return 1; }
3478: sub PAST_DUE_NO_ANSWER { return 2; }
3479: sub PAST_DUE_ANSWER_LATER { return 3; }
3480: sub ANSWER_OPEN { return 4; }
3481: sub NOTHING_SET { return 5; }
3482: sub NETWORK_FAILURE { return 100; }
3483:
3484: # getDateStatus gets the date status for a given problem part.
3485: # Because answer date, due date, and open date are fully independent
3486: # (i.e., it is perfectly possible to *only* have an answer date),
3487: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
3488: # (past, future, none given). This function handles this with a decision
3489: # tree. Read the comments to follow the decision tree.
1.51 bowersj2 3490:
3491: sub getDateStatus {
3492: my $self = shift;
3493: my $part = shift;
3494: $part = "0" if (!defined($part));
1.54 bowersj2 3495:
3496: # Always return network failure if there was one.
1.51 bowersj2 3497: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
3498:
3499: my $now = time();
3500:
1.53 bowersj2 3501: my $open = $self->opendate($part);
3502: my $due = $self->duedate($part);
3503: my $answer = $self->answerdate($part);
3504:
3505: if (!$open && !$due && !$answer) {
3506: # no data on the problem at all
3507: # should this be the same as "open later"? think multipart.
3508: return $self->NOTHING_SET;
3509: }
1.59 bowersj2 3510: if (!$open || $now < $open) {return $self->OPEN_LATER}
3511: if (!$due || $now < $due) {return $self->OPEN}
3512: if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
3513: if ($answer) { return $self->ANSWER_OPEN; }
1.54 bowersj2 3514: return PAST_DUE_NO_ANSWER;
1.51 bowersj2 3515: }
3516:
3517: =pod
3518:
3519: B<>
3520:
3521: =over 4
3522:
1.174 albertel 3523: =item * B<getCompletionStatus>($part):
1.51 bowersj2 3524:
1.174 albertel 3525: ($part defaults to 0.) A convenience function that returns a symbolic
3526: constant telling you about the completion status of the part, with the
3527: following possible results:
3528:
3529: =back
1.51 bowersj2 3530:
3531: B<Completion Codes>
3532:
3533: =over 4
3534:
1.174 albertel 3535: =item * B<NOT_ATTEMPTED>:
3536:
3537: Has not been attempted at all.
3538:
3539: =item * B<INCORRECT>:
3540:
3541: Attempted, but wrong by student.
1.51 bowersj2 3542:
1.174 albertel 3543: =item * B<INCORRECT_BY_OVERRIDE>:
1.51 bowersj2 3544:
1.174 albertel 3545: Attempted, but wrong by instructor override.
1.51 bowersj2 3546:
1.174 albertel 3547: =item * B<CORRECT>:
1.51 bowersj2 3548:
1.174 albertel 3549: Correct or correct by instructor.
1.51 bowersj2 3550:
1.174 albertel 3551: =item * B<CORRECT_BY_OVERRIDE>:
1.51 bowersj2 3552:
1.174 albertel 3553: Correct by instructor override.
1.51 bowersj2 3554:
1.174 albertel 3555: =item * B<EXCUSED>:
3556:
3557: Excused. Not yet implemented.
3558:
3559: =item * B<NETWORK_FAILURE>:
3560:
3561: Information not available due to network failure.
3562:
3563: =item * B<ATTEMPTED>:
3564:
3565: Attempted, and not yet graded.
1.69 bowersj2 3566:
1.51 bowersj2 3567: =back
3568:
3569: =cut
3570:
1.53 bowersj2 3571: sub NOT_ATTEMPTED { return 10; }
3572: sub INCORRECT { return 11; }
3573: sub INCORRECT_BY_OVERRIDE { return 12; }
3574: sub CORRECT { return 13; }
3575: sub CORRECT_BY_OVERRIDE { return 14; }
3576: sub EXCUSED { return 15; }
1.69 bowersj2 3577: sub ATTEMPTED { return 16; }
1.51 bowersj2 3578:
3579: sub getCompletionStatus {
3580: my $self = shift;
3581: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
3582:
1.108 bowersj2 3583: my $status = $self->queryRestoreHash('solved', shift);
1.51 bowersj2 3584:
3585: # Left as seperate if statements in case we ever do more with this
3586: if ($status eq 'correct_by_student') {return $self->CORRECT;}
3587: if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
3588: if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
3589: if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
3590: if ($status eq 'excused') {return $self->EXCUSED; }
1.69 bowersj2 3591: if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
1.51 bowersj2 3592: return $self->NOT_ATTEMPTED;
1.108 bowersj2 3593: }
3594:
3595: sub queryRestoreHash {
3596: my $self = shift;
3597: my $hashentry = shift;
3598: my $part = shift;
1.185 bowersj2 3599: $part = "0" if (!defined($part) || $part eq '');
1.108 bowersj2 3600: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
3601:
3602: $self->getReturnHash();
3603:
3604: return $self->{RETURN_HASH}->{'resource.'.$part.'.'.$hashentry};
1.51 bowersj2 3605: }
3606:
3607: =pod
3608:
3609: B<Composite Status>
3610:
1.174 albertel 3611: Along with directly returning the date or completion status, the
3612: resource object includes a convenience function B<status>() that will
3613: combine the two status tidbits into one composite status that can
1.191 bowersj2 3614: represent the status of the resource as a whole. This method represents
3615: the concept of the thing we want to display to the user on the nav maps
3616: screen, which is a combination of completion and open status. The precise logic is
1.174 albertel 3617: documented in the comments of the status method. The following results
3618: may be returned, all available as methods on the resource object
1.185 bowersj2 3619: ($res->NETWORK_FAILURE): In addition to the return values that match
3620: the date or completion status, this function can return "ANSWER_SUBMITTED"
3621: if that problemstatus parameter value is set to No, suppressing the
3622: incorrect/correct feedback.
1.51 bowersj2 3623:
3624: =over 4
3625:
1.174 albertel 3626: =item * B<NETWORK_FAILURE>:
3627:
3628: The network has failed and the information is not available.
1.51 bowersj2 3629:
1.174 albertel 3630: =item * B<NOTHING_SET>:
1.53 bowersj2 3631:
1.174 albertel 3632: No dates have been set for this problem (part) at all. (Because only
3633: certain parts of a multi-part problem may be assigned, this can not be
3634: collapsed into "open later", as we do not know a given part will EVER
3635: be opened. For single part, this is the same as "OPEN_LATER".)
1.51 bowersj2 3636:
1.174 albertel 3637: =item * B<CORRECT>:
1.51 bowersj2 3638:
1.174 albertel 3639: For any reason at all, the part is considered correct.
1.54 bowersj2 3640:
1.174 albertel 3641: =item * B<EXCUSED>:
1.51 bowersj2 3642:
1.174 albertel 3643: For any reason at all, the problem is excused.
1.51 bowersj2 3644:
1.174 albertel 3645: =item * B<PAST_DUE_NO_ANSWER>:
1.51 bowersj2 3646:
1.174 albertel 3647: The problem is past due, not considered correct, and no answer date is
3648: set.
1.51 bowersj2 3649:
1.174 albertel 3650: =item * B<PAST_DUE_ANSWER_LATER>:
1.51 bowersj2 3651:
1.174 albertel 3652: The problem is past due, not considered correct, and an answer date in
3653: the future is set.
1.51 bowersj2 3654:
1.174 albertel 3655: =item * B<ANSWER_OPEN>:
3656:
3657: The problem is past due, not correct, and the answer is now available.
3658:
3659: =item * B<OPEN_LATER>:
3660:
3661: The problem is not yet open.
3662:
3663: =item * B<TRIES_LEFT>:
3664:
3665: The problem is open, has been tried, is not correct, but there are
3666: tries left.
3667:
3668: =item * B<INCORRECT>:
3669:
3670: The problem is open, and all tries have been used without getting the
3671: correct answer.
3672:
3673: =item * B<OPEN>:
3674:
3675: The item is open and not yet tried.
3676:
3677: =item * B<ATTEMPTED>:
3678:
3679: The problem has been attempted.
1.69 bowersj2 3680:
1.185 bowersj2 3681: =item * B<ANSWER_SUBMITTED>:
3682:
3683: An answer has been submitted, but the student should not see it.
3684:
1.51 bowersj2 3685: =back
3686:
3687: =cut
3688:
1.185 bowersj2 3689: sub TRIES_LEFT { return 20; }
3690: sub ANSWER_SUBMITTED { return 21; }
1.51 bowersj2 3691:
3692: sub status {
3693: my $self = shift;
3694: my $part = shift;
3695: if (!defined($part)) { $part = "0"; }
3696: my $completionStatus = $self->getCompletionStatus($part);
3697: my $dateStatus = $self->getDateStatus($part);
3698:
3699: # What we have is a two-dimensional matrix with 4 entries on one
3700: # dimension and 5 entries on the other, which we want to colorize,
1.54 bowersj2 3701: # plus network failure and "no date data at all".
1.51 bowersj2 3702:
1.53 bowersj2 3703: if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51 bowersj2 3704:
1.186 bowersj2 3705: my $suppressFeedback = lc($self->parmval("problemstatus", $part)) eq 'no';
1.185 bowersj2 3706:
1.51 bowersj2 3707: # There are a few whole rows we can dispose of:
1.53 bowersj2 3708: if ($completionStatus == CORRECT ||
3709: $completionStatus == CORRECT_BY_OVERRIDE ) {
1.185 bowersj2 3710: return $suppressFeedback? ANSWER_SUBMITTED : CORRECT;
1.69 bowersj2 3711: }
3712:
3713: if ($completionStatus == ATTEMPTED) {
3714: return ATTEMPTED;
1.53 bowersj2 3715: }
3716:
3717: # If it's EXCUSED, then return that no matter what
3718: if ($completionStatus == EXCUSED) {
3719: return EXCUSED;
1.51 bowersj2 3720: }
3721:
1.53 bowersj2 3722: if ($dateStatus == NOTHING_SET) {
3723: return NOTHING_SET;
1.51 bowersj2 3724: }
3725:
1.69 bowersj2 3726: # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
3727: # by 4 matrix (date statuses).
1.51 bowersj2 3728:
1.54 bowersj2 3729: if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59 bowersj2 3730: $dateStatus == PAST_DUE_NO_ANSWER ) {
1.54 bowersj2 3731: return $dateStatus;
1.51 bowersj2 3732: }
3733:
1.53 bowersj2 3734: if ($dateStatus == ANSWER_OPEN) {
3735: return ANSWER_OPEN;
1.51 bowersj2 3736: }
3737:
3738: # Now: (incorrect, incorrect_override, not_attempted) x
3739: # (open_later), (open)
3740:
1.53 bowersj2 3741: if ($dateStatus == OPEN_LATER) {
3742: return OPEN_LATER;
1.51 bowersj2 3743: }
3744:
3745: # If it's WRONG...
1.53 bowersj2 3746: if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51 bowersj2 3747: # and there are TRIES LEFT:
1.79 bowersj2 3748: if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
1.53 bowersj2 3749: return TRIES_LEFT;
1.51 bowersj2 3750: }
1.185 bowersj2 3751: return $suppressFeedback ? ANSWER_SUBMITTED : INCORRECT; # otherwise, return orange; student can't fix this
1.51 bowersj2 3752: }
3753:
3754: # Otherwise, it's untried and open
1.53 bowersj2 3755: return OPEN;
1.191 bowersj2 3756: }
3757:
3758: =pod
3759:
3760: B<Completable>
3761:
3762: The completable method represents the concept of I<whether the student can
3763: currently do the problem>. If the student can do the problem, which means
3764: that it is open, there are tries left, and if the problem is manually graded
3765: or the grade is suppressed via problemstatus, the student has not tried it
3766: yet, then the method returns 1. Otherwise, it returns 0, to indicate that
3767: either the student has tried it and there is no feedback, or that for
3768: some reason it is no longer completable (not open yet, successfully completed,
3769: out of tries, etc.). As an example, this is used as the filter for the
3770: "Uncompleted Homework" option for the nav maps.
3771:
3772: If this does not quite meet your needs, do not fiddle with it (unless you are
3773: fixing it to better match the student's conception of "completable" because
3774: it's broken somehow)... make a new method.
3775:
3776: =cut
3777:
3778: sub completable {
3779: my $self = shift;
3780: if (!$self->is_problem()) { return 0; }
3781: my $partCount = $self->countParts();
3782:
3783: foreach my $part (@{$self->parts()}) {
3784: if ($part eq '0' && $partCount != 1) { next; }
3785: my $status = $self->status($part);
3786: # "If any of the parts are open, or have tries left (implies open),
3787: # and it is not "attempted" (manually graded problem), it is
3788: # not "complete"
3789: if (!(($status == OPEN() || $status == TRIES_LEFT())
3790: && $self->getCompletionStatus($part) != ATTEMPTED()
3791: && $status != ANSWER_SUBMITTED())) {
3792: return 0;
3793: }
3794: }
3795:
3796: # If all the parts were complete, so was this problem.
3797: return 1;
1.51 bowersj2 3798: }
3799:
3800: =pod
3801:
3802: =head2 Resource/Nav Map Navigation
3803:
3804: =over 4
3805:
1.174 albertel 3806: =item * B<getNext>():
3807:
3808: Retreive an array of the possible next resources after this
3809: one. Always returns an array, even in the one- or zero-element case.
3810:
3811: =item * B<getPrevious>():
1.85 bowersj2 3812:
1.174 albertel 3813: Retreive an array of the possible previous resources from this
3814: one. Always returns an array, even in the one- or zero-element case.
1.51 bowersj2 3815:
3816: =cut
3817:
3818: sub getNext {
3819: my $self = shift;
3820: my @branches;
3821: my $to = $self->to();
1.85 bowersj2 3822: foreach my $branch ( split(/,/, $to) ) {
1.51 bowersj2 3823: my $choice = $self->{NAV_MAP}->getById($branch);
3824: my $next = $choice->goesto();
3825: $next = $self->{NAV_MAP}->getById($next);
3826:
1.131 bowersj2 3827: push @branches, $next;
1.85 bowersj2 3828: }
3829: return \@branches;
3830: }
3831:
3832: sub getPrevious {
3833: my $self = shift;
3834: my @branches;
3835: my $from = $self->from();
3836: foreach my $branch ( split /,/, $from) {
3837: my $choice = $self->{NAV_MAP}->getById($branch);
3838: my $prev = $choice->comesfrom();
3839: $prev = $self->{NAV_MAP}->getById($prev);
3840:
1.131 bowersj2 3841: push @branches, $prev;
1.51 bowersj2 3842: }
3843: return \@branches;
1.131 bowersj2 3844: }
3845:
3846: sub browsePriv {
3847: my $self = shift;
3848: if (defined($self->{BROWSE_PRIV})) {
3849: return $self->{BROWSE_PRIV};
3850: }
3851:
3852: $self->{BROWSE_PRIV} = &Apache::lonnet::allowed('bre', $self->src());
1.51 bowersj2 3853: }
3854:
3855: =pod
1.2 www 3856:
1.51 bowersj2 3857: =back
1.2 www 3858:
1.51 bowersj2 3859: =cut
1.2 www 3860:
1.51 bowersj2 3861: 1;
1.2 www 3862:
1.51 bowersj2 3863: __END__
1.2 www 3864:
3865:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>