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