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