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