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