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