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