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