Annotation of loncom/interface/lonnavmaps.pm, revision 1.64
1.2 www 1: # The LearningOnline Network with CAPA
2: # Navigate Maps Handler
1.1 www 3: #
1.59 bowersj2 4: # $Id: lonnavmaps.pm,v 1.58 2002/10/01 07:20:31 albertel Exp $
1.20 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.2 www 28: # (Page Handler
1.1 www 29: #
1.2 www 30: # (TeX Content Handler
1.1 www 31: #
1.2 www 32: # 05/29/00,05/30 Gerd Kortemeyer)
33: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
34: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16 Gerd Kortemeyer)
1.1 www 35: #
1.17 www 36: # 3/1/1,6/1,17/1,29/1,30/1,2/8,9/21,9/24,9/25 Gerd Kortemeyer
1.21 www 37: # YEAR=2002
38: # 1/1 Gerd Kortemeyer
39: #
1.2 www 40:
1.1 www 41: package Apache::lonnavmaps;
42:
43: use strict;
1.2 www 44: use Apache::Constants qw(:common :http);
45: use Apache::lonnet();
1.18 albertel 46: use Apache::loncommon();
1.2 www 47: use HTML::TokeParser;
48: use GDBM_File;
49:
50: # -------------------------------------------------------------- Module Globals
51: my %hash;
52: my @rows;
53:
1.10 www 54: #
55: # These cache hashes need to be independent of user, resource and course
56: # (user and course can/should be in the keys)
57: #
58:
59: my %courserdatas;
60: my %userrdatas;
61:
62: #
63: # These global hashes are dependent on user, course and resource,
64: # and need to be initialized every time when a sheet is calculated
65: #
66: my %courseopt;
67: my %useropt;
68: my %parmhash;
69:
1.47 bowersj2 70: # This parameter keeps track of whether obtaining the user's information
71: # failed, which the colorizer in astatus can use
72: my $networkFailedFlag = 0;
73:
1.2 www 74: # ------------------------------------------------------------------ Euclid gcd
75:
76: sub euclid {
77: my ($e,$f)=@_;
78: my $a; my $b; my $r;
79: if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
80: while ($r!=0) {
1.51 bowersj2 81: $a=$b; $b=$r;
1.2 www 82: $r=$a%$b;
83: }
84: return $b;
85: }
86:
1.10 www 87: # --------------------------------------------------------------------- Parmval
88:
89: # -------------------------------------------- Figure out a cascading parameter
90: #
91: # For this function to work
92: #
93: # * parmhash needs to be tied
94: # * courseopt and useropt need to be initialized for this user and course
95: #
96:
97: sub parmval {
98: my ($what,$symb)=@_;
99: my $cid=$ENV{'request.course.id'};
100: my $csec=$ENV{'request.course.sec'};
101: my $uname=$ENV{'user.name'};
102: my $udom=$ENV{'user.domain'};
103:
104: unless ($symb) { return ''; }
105: my $result='';
106:
107: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
108:
109: # ----------------------------------------------------- Cascading lookup scheme
1.24 albertel 110: my $rwhat=$what;
111: $what=~s/^parameter\_//;
112: $what=~s/\_/\./;
113:
114: my $symbparm=$symb.'.'.$what;
115: my $mapparm=$mapname.'___(all).'.$what;
116: my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
117:
118: my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
119: my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
120: my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
121:
122: my $courselevel= $usercourseprefix.'.'.$what;
123: my $courselevelr=$usercourseprefix.'.'.$symbparm;
124: my $courselevelm=$usercourseprefix.'.'.$mapparm;
125:
126: # ---------------------------------------------------------- first, check user
1.57 albertel 127: if (defined($uname)) {
128: if (defined($useropt{$courselevelr})) { return $useropt{$courselevelr}; }
129: if (defined($useropt{$courselevelm})) { return $useropt{$courselevelm}; }
130: if (defined($useropt{$courselevel})) { return $useropt{$courselevel}; }
1.24 albertel 131: }
1.10 www 132:
1.24 albertel 133: # ------------------------------------------------------- second, check course
1.57 albertel 134: if (defined($csec)) {
135: if (defined($courseopt{$seclevelr})) { return $courseopt{$seclevelr}; }
136: if (defined($courseopt{$seclevelm})) { return $courseopt{$seclevelm}; }
137: if (defined($courseopt{$seclevel})) { return $courseopt{$seclevel}; }
1.24 albertel 138: }
1.10 www 139:
1.57 albertel 140: if (defined($courseopt{$courselevelr})) { return $courseopt{$courselevelr}; }
141: if (defined($courseopt{$courselevelm})) { return $courseopt{$courselevelm}; }
142: if (defined($courseopt{$courselevel})) { return $courseopt{$courselevel}; }
1.10 www 143:
1.24 albertel 144: # ----------------------------------------------------- third, check map parms
1.10 www 145:
1.24 albertel 146: my $thisparm=$parmhash{$symbparm};
1.57 albertel 147: if (defined($thisparm)) { return $thisparm; }
1.10 www 148:
1.24 albertel 149: # ----------------------------------------------------- fourth , check default
1.10 www 150:
1.25 albertel 151: my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
1.57 albertel 152: if (defined($default)) { return $default}
1.25 albertel 153:
154: # --------------------------------------------------- fifth , cascade up parts
155:
156: my ($space,@qualifier)=split(/\./,$rwhat);
157: my $qualifier=join('.',@qualifier);
158: unless ($space eq '0') {
1.51 bowersj2 159: my ($part,$id)=split(/\_/,$space);
160: if ($id) {
161: my $partgeneral=&parmval($part.".$qualifier",$symb);
1.57 albertel 162: if (defined($partgeneral)) { return $partgeneral; }
1.51 bowersj2 163: } else {
164: my $resourcegeneral=&parmval("0.$qualifier",$symb);
1.57 albertel 165: if (defined($resourcegeneral)) { return $resourcegeneral; }
1.51 bowersj2 166: }
1.25 albertel 167: }
168: return '';
1.10 www 169: }
170:
171:
172:
1.9 www 173: # ------------------------------------------------------------- Find out status
1.25 albertel 174: # return codes
175: # tcode (timecode)
176: # 1: will open later
177: # 2: is open and not past due yet
178: # 3: is past due date
179: # 4: due in the next 24 hours
180: #
181: # code (curent solved status)
182: # 1: not attempted
183: # 2: attempted but wrong, or incorrect by instructor
184: # 3: solved or correct by instructor
1.29 albertel 185: # 4: partially correct (one or more parts correct)
186: # "excused" needs to be supported, but is not yet.
1.9 www 187: sub astatus {
188: my $rid=shift;
1.29 albertel 189: my $code=0;
1.9 www 190: my $ctext='';
191: $rid=~/(\d+)\.(\d+)/;
1.10 www 192: my $symb=&Apache::lonnet::declutter($hash{'map_id_'.$1}).'___'.$2.'___'.
1.51 bowersj2 193: &Apache::lonnet::declutter($hash{'src_'.$rid});
1.15 www 194: my %duedate=();
195: my %opendate=();
196: my %answerdate=();
1.25 albertel 197: # need to always check part 0's open/due/answer status
1.51 bowersj2 198: foreach (sort(split(/\,/,&Apache::lonnet::metadata($hash{'src_'.$rid},'allpo\ssiblekeys')))) {
1.15 www 199: if ($_=~/^parameter\_(.*)\_opendate$/) {
1.51 bowersj2 200: my $part=$1;
1.15 www 201: $duedate{$part}=&parmval($part.'.duedate',$symb);
202: $opendate{$part}=&parmval($part.'.opendate',$symb);
203: $answerdate{$part}=&parmval($part.'.answerdate',$symb);
1.49 www 204: if (&parmval($part.'.opendate.type',$symb) eq 'date_interval') {
1.51 bowersj2 205: $opendate{$part}=$duedate{$part}-$opendate{$part};
1.49 www 206: }
1.50 www 207: if (&parmval($part.'.answerdate.type',$symb) eq 'date_interval') {
1.49 www 208: $answerdate{$part}=$duedate{$part}+$answerdate{$part};
209: }
1.15 www 210: }
1.25 albertel 211: }
1.11 www 212: my $now=time;
213: my $tcode=0;
1.16 www 214:
215: my %returnhash=&Apache::lonnet::restore($symb);
216:
1.25 albertel 217: foreach (sort(keys(%opendate))) {
1.51 bowersj2 218: my $duedate=$duedate{$_};
219: my $opendate=$opendate{$_};
220: my $answerdate=$answerdate{$_};
221: my $preface='';
222: unless ($_ eq '0') { $preface=' Part: '.$_.' '; }
223: if ($opendate) {
224: if ($now<$duedate || (!$duedate)) {
225: unless ($tcode==4) { $tcode=2; }
226: if ($duedate) {
227: $ctext.=$preface.'Due: '.localtime($duedate);
228: } else {
229: $ctext.=$preface.'No Due Date';
230: }
231: if ($now<$opendate) {
232: unless ($tcode) { $tcode=1; }
233: $ctext.=$preface.'Open: '.localtime($opendate);
234: }
235: if ($duedate && $duedate-$now<86400) {
236: $tcode=4;
237: $ctext.=$preface.'Due: '.localtime($duedate);
238: }
239: } else {
240: unless (($tcode==4) || ($tcode eq 2)) { $tcode=3; }
241: if ($now<$answerdate) {
242: $ctext.='Answer: '.localtime($duedate);
243: }
244: }
245: } else {
246: unless (($tcode==2) || ($tcode==4)) { $tcode=1; }
247: }
248:
249: my $status=$returnhash{'resource.'.$_.'.solved'};
250:
251: if ($status eq 'correct_by_student') {
252: if ($code==0||$code==3) { $code=3; } else { $code=4; }
253: $ctext.=' solved';
254: } elsif ($status eq 'correct_by_override') {
255: if ($code==0||$code==3) { $code=3; } else { $code=4; }
256: $ctext.=' override';
257: } elsif ($status eq 'incorrect_attempted') {
258: if ($code!=4 && $code!=3) { $code=2; }
259: if ($code==3) { $code=4; }
260: $ctext.=' ('.
261: ($returnhash{'resource.'.$_.'.tries'}?
262: $returnhash{'resource.'.$_.'.tries'}:'0');
263: my $numtries = &parmval($_.'.maxtries',$symb);
264: if ($numtries) { $ctext.='/'.$numtries.' tries'; }
265: $ctext.=')';
266: } elsif ($status eq 'incorrect_by_override') {
267: if ($code!=4 && $code!=3) { $code=2; }
268: if ($code==3) { $code=4; }
269: $ctext.=' override';
270: } elsif ($status eq 'excused') {
271: if ($code==0||$code==3) { $code=3; } else { $code=4; }
272: $ctext.=' excused';
273: } else {
274: if ($code==0) { $code=1; }
275: }
1.25 albertel 276: }
1.16 www 277:
1.11 www 278: return 'p'.$code.$tcode.'"'.$ctext.'"';
1.9 www 279: }
280:
1.2 www 281:
1.31 albertel 282: sub addresource {
1.32 albertel 283: my ($resource,$sofar,$rid,$showtypes,$indent,$linkid)=@_;
1.31 albertel 284: if ($showtypes eq 'problems') {
1.51 bowersj2 285: if ($resource!~/\.(problem|exam|quiz|assess|survey|form)$/) {
286: return;
287: }
1.31 albertel 288: }
289: my $brepriv=&Apache::lonnet::allowed('bre',$resource);
290: if ($hash{'src_'.$rid}) {
1.51 bowersj2 291: if (($brepriv eq '2') || ($brepriv eq 'F')) {
292: my $pprefix='';
293: if ($resource=~/\.(problem|exam|quiz|assess|survey|form)$/) {
294: $pprefix=&astatus($rid);
295: }
296: $$sofar++;
297: if ($indent) { $pprefix='i'.$indent.','.$pprefix; }
298: if ($linkid) { $pprefix='l'.$linkid.','.$pprefix; }
299: if (defined($rows[$$sofar])) {
300: $rows[$$sofar].='&'.$pprefix.$rid;
301: } else {
302: $rows[$$sofar]=$pprefix.$rid;
303: }
304: }
1.31 albertel 305: }
306: }
307:
308: sub followlinks () {
1.32 albertel 309: my ($rid,$sofar,$beenhere,$further,$showtypes,$indent,$linkid)=@_;
1.31 albertel 310: my $mincond=1;
311: my $next='';
312: foreach (split(/\,/,$hash{'to_'.$rid})) {
1.51 bowersj2 313: my $thiscond=
314: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
315: if ($thiscond>=$mincond) {
316: if ($next) {
317: $next.=','.$_.':'.$thiscond;
318: } else {
319: $next=$_.':'.$thiscond;
320: }
321: if ($thiscond>$mincond) { $mincond=$thiscond; }
322: }
1.31 albertel 323: }
1.32 albertel 324: my $col=0;
325: &Apache::lonxml::debug("following links -$next-");
1.31 albertel 326: foreach (split(/\,/,$next)) {
1.51 bowersj2 327: my ($nextlinkid,$condval)=split(/\:/,$_);
328: if ($condval>=$mincond) {
329: my $now=&tracetable($sofar,$hash{'goesto_'.$nextlinkid},
330: $beenhere,$showtypes,$indent,$linkid);
331: if ($now>$further) {
332: if ($col>0) {
333: my $string;
334: for(my $i=0;$i<$col;$i++) { $string.='&'; }
335: for(my $i=$further+1;$now-1>$i;$i++) {
336: $rows[$i]=$string.$rows[$i];
337: }
338: }
339: $further=$now;
340: }
341: }
342: $col++;
1.31 albertel 343: }
344: return $further;
345: }
346: # ------------------------------------------------------------ Build page table
347:
348: sub tracetable {
1.32 albertel 349: my ($sofar,$rid,$beenhere,$showtypes,$indent,$linkid)=@_;
350: my $newshowtypes=$showtypes;
1.31 albertel 351: my $further=$sofar;
1.44 www 352: # $Apache::lonxml::debug=1;
1.32 albertel 353: &Apache::lonxml::debug("$rid ; $linkid ; $sofar ; $beenhere ; ".$hash{'src_'.$rid});
354: if ($beenhere=~/\&$rid\&/) { return $further; }
355: $beenhere.=$rid.'&';
1.31 albertel 356:
357: if (defined($hash{'is_map_'.$rid})) {
1.51 bowersj2 358: $sofar++;
359: my $tprefix='';
360: if ($hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}}
361: eq 'sequence') {
362: $tprefix='h';
363: } elsif ($hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}}
364: eq 'page') {
365: $tprefix='j';
366: if ($indent) { $tprefix='i'.$indent.','.$tprefix; }
367: if ($linkid) { $tprefix='l'.$linkid.','.$tprefix; }
368: $newshowtypes='problems';
369: $indent++;
370: #if in a .page continue to link the encompising .page
371: if (!$linkid) { $linkid=$rid; }
372: }
373: if (defined($rows[$sofar])) {
374: $rows[$sofar].='&'.$tprefix.$rid;
375: } else {
376: $rows[$sofar]=$tprefix.$rid;
377: }
378: if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
379: (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
380: my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
381: $sofar=&tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
382: '&'.$frid.'&',$newshowtypes,$indent,$linkid);
383: &addresource($hash{'src_'.$frid},\$sofar,$frid,$newshowtypes,
384: $indent,$linkid);
385: if ($tprefix =~ /j$/) { $indent--; $linkid=''; }
386: }
1.31 albertel 387: } else {
1.51 bowersj2 388: &addresource($hash{'src_'.$rid},\$sofar,$rid,$showtypes,
389: $indent,$linkid);
1.31 albertel 390: }
391:
392: if (defined($hash{'to_'.$rid})) {
1.51 bowersj2 393: $further=&followlinks($rid,$sofar,$beenhere,$further,$showtypes,
394: $indent,$linkid);
1.2 www 395: }
1.31 albertel 396:
1.2 www 397: return $further;
398: }
399:
400: # ================================================================ Main Handler
1.1 www 401:
402: sub handler {
1.28 albertel 403: my $r=shift;
1.2 www 404:
1.51 bowersj2 405: &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
406:
407: if ($ENV{'form.jtest'} ne "1")
408: {
409: return new_handle($r);
410: }
1.2 www 411:
412: # ------------------------------------------- Set document type for header only
413:
1.28 albertel 414: if ($r->header_only) {
1.51 bowersj2 415: if ($ENV{'browser.mathml'}) {
416: $r->content_type('text/xml');
417: } else {
418: $r->content_type('text/html');
419: }
420: $r->send_http_header;
421: return OK;
1.28 albertel 422: }
423: my $requrl=$r->uri;
424: my $hashtied;
1.2 www 425: # ----------------------------------------------------------------- Tie db file
1.28 albertel 426: my $fn;
427: if ($ENV{'request.course.fn'}) {
1.51 bowersj2 428: $fn=$ENV{'request.course.fn'};
429: if (-e "$fn.db") {
430: if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) &&
431: (tie(%parmhash,'GDBM_File',
432: $ENV{'request.course.fn'}.'_parms.db',
433: &GDBM_READER(),0640))) {
434: $hashtied=1;
435: }
436: }
1.28 albertel 437: }
438: if (!$hashtied) {
1.51 bowersj2 439: $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
440: return HTTP_NOT_ACCEPTABLE;
1.28 albertel 441: }
442:
1.2 www 443: # ------------------------------------------------------------------- Hash tied
1.28 albertel 444:
445: if ($ENV{'browser.mathml'}) {
1.51 bowersj2 446: $r->content_type('text/xml');
1.28 albertel 447: } else {
1.51 bowersj2 448: $r->content_type('text/html');
1.28 albertel 449: }
450: &Apache::loncommon::no_cache($r);
451: $r->send_http_header;
452:
1.42 www 453: my $firstres=$hash{'map_start_'.
454: &Apache::lonnet::clutter($ENV{'request.course.uri'})};
455: my $lastres=$hash{'map_finish_'.
456: &Apache::lonnet::clutter($ENV{'request.course.uri'})};
1.28 albertel 457: if (!(($firstres) && ($lastres))) {
1.51 bowersj2 458: $r->print('<html><body>Coursemap undefined.</body></html>');
1.28 albertel 459: } else {
460:
1.2 www 461: # ----------------------------------------------------------------- Render page
1.10 www 462: # -------------------------------------------------------------- Set parameters
463:
464:
465: # ---------------------------- initialize coursedata and userdata for this user
1.51 bowersj2 466: undef %courseopt;
467: undef %useropt;
1.10 www 468:
1.51 bowersj2 469: my $uname=$ENV{'user.name'};
470: my $udom=$ENV{'user.domain'};
471: my $uhome=$ENV{'user.home'};
472: my $cid=$ENV{'request.course.id'};
473: my $chome=$ENV{'course.'.$cid.'.home'};
474: my ($cdom,$cnum)=split(/\_/,$cid);
475:
476: my $userprefix=$uname.'_'.$udom.'_';
477:
478: unless ($uhome eq 'no_host') {
1.48 bowersj2 479: # ------------------------------------------------- Get coursedata (if present)
1.51 bowersj2 480: unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
481: my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
482: ':resourcedata',$chome);
483: if ($reply!~/^error\:/) {
484: $courserdatas{$cid}=$reply;
485: $courserdatas{$cid.'.last_cache'}=time;
486: }
487: # check to see if network failed
488: elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
489: {
490: $networkFailedFlag = 1;
491: }
492: }
493: foreach (split(/\&/,$courserdatas{$cid})) {
494: my ($name,$value)=split(/\=/,$_);
495: $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
496: &Apache::lonnet::unescape($value);
497: }
1.10 www 498: # --------------------------------------------------- Get userdata (if present)
1.51 bowersj2 499: unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
500: my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
501: if ($reply!~/^error\:/) {
502: $userrdatas{$uname.'___'.$udom}=$reply;
503: $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
504: }
505: }
506: foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
507: my ($name,$value)=split(/\=/,$_);
508: $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
509: &Apache::lonnet::unescape($value);
510: }
511: }
1.28 albertel 512:
1.51 bowersj2 513: @rows=();
1.28 albertel 514:
1.51 bowersj2 515: &tracetable(0,$firstres,'&','',0);
1.2 www 516:
517: # ------------------------------------------------------------------ Page parms
518:
1.51 bowersj2 519: my $j;
520: my $i;
521: my $lcm=1;
522: my $contents=0;
1.2 www 523:
524: # ---------------------------------------------- Go through table to get layout
525:
1.51 bowersj2 526: for ($i=0;$i<=$#rows;$i++) {
527: if ($rows[$i]) {
528: &Apache::lonxml::debug("Row $i is:".$rows[$i]);
529: $contents++;
530: my @colcont=split(/\&/,$rows[$i]);
531: $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
532: }
533: }
1.5 www 534:
1.2 www 535:
1.51 bowersj2 536: unless ($contents) {
537: $r->print('<html><body>Empty Map.</body></html>');
538: } else {
1.10 www 539:
1.2 www 540: # ------------------------------------------------------------------ Build page
541:
1.51 bowersj2 542: my $currenturl=$ENV{'form.postdata'};
543: $currenturl=~s/^http\:\/\///;
544: $currenturl=~s/^[^\/]+//;
1.13 www 545:
1.2 www 546: # ---------------------------------------------------------------- Send headers
547:
1.51 bowersj2 548: my $date=localtime;
549: my $now=time;
1.21 www 550: # ----------------------------------------- Get email status and discussiontime
551:
1.51 bowersj2 552: my %emailstatus=&Apache::lonnet::dump('email_status');
553: my $logouttime=$emailstatus{'logout'};
554: my $courseleave=$emailstatus{'logout_'.$ENV{'request.course.id'}};
555: my $lastcheck=($courseleave>$logouttime?$courseleave:$logouttime);
556:
557: my %discussiontimes=&Apache::lonnet::dump('discussiontimes',
558: $cdom,$cnum);
559:
560: my %feedback=();
561: my %error=();
562: foreach my $msgid (split(/\&/,&Apache::lonnet::reply('keys:'.
563: $ENV{'user.domain'}.':'.
564: $ENV{'user.name'}.':nohist_email',
565: $ENV{'user.home'}))) {
566: $msgid=&Apache::lonnet::unescape($msgid);
567: my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
568: if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
569: my ($what,$url)=($1,$2);
570: my %status=
571: &Apache::lonnet::get('email_status',[$msgid]);
572: if ($status{$msgid}=~/^error\:/) {
573: $status{$msgid}='';
574: }
575:
576: if (($status{$msgid} eq 'new') ||
577: (!$status{$msgid})) {
578: if ($what eq 'Error') {
579: $error{$url}.=','.$msgid;
580: } else {
581: $feedback{$url}.=','.$msgid;
582: }
583: }
584: }
585: }
1.22 www 586: # ----------------------------------------------------------- Start Page Output
1.43 www 587: my $bodytagadd='';
1.51 bowersj2 588: $r->print(
1.43 www 589: '<html><head><title>Navigate Course Map</title></head>');
1.51 bowersj2 590: if (($currenturl=~/^\/res/) &&
591: ($currenturl!~/^\/res\/adm/)) {
592: $bodytagadd='onLoad="window.location.hash='."'curloc'".'"';
593: }
594: $r->print(&Apache::loncommon::bodytag('Navigate Course Map','',
1.43 www 595: $bodytagadd));
1.51 bowersj2 596: $r->print('<script>window.focus();</script>');
597: my $desc=$ENV{'course.'.$ENV{'request.course.id'}.'.description'};
598: if (defined($desc)) { $r->print("<h2>$desc</h2>\n"); }
599: $r->print("<h3>$date</h3>\n");
600: $r->rflush();
601: $r->print('<img src="/adm/lonMisc/chat.gif"> New discussion since '.
602: localtime($lastcheck).
603: '<br><img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>');
604: if (($currenturl=~/^\/res/) &&
605: ($currenturl!~/^\/res\/adm/)) {
606: $r->print('<a href="#curloc">Current Location</a><p>');
607: }
608:
609: # Handle a network error
610:
611: if ($networkFailedFlag)
612: {
613: $r->print('<H2>LON-CAPA network failure.</H2>'."\n");
614: $r->print("<p>LON-CAPA's network is having difficulties, some problem" .
615: " information, such as due dates, will not be available.");
616: }
1.13 www 617: # ----------------------------------------------------- The little content list
1.51 bowersj2 618: for ($i=0;$i<=$#rows;$i++) {
619: if ($rows[$i]) {
620: my @colcont=split(/\&/,$rows[$i]);
621: my $avespan=$lcm/($#colcont+1);
622: for ($j=0;$j<=$#colcont;$j++) {
623: my $rid=$colcont[$j];
624: if ($rid=~/^h(.+)/) {
625: $rid=$1;
626: $r->print(' <a href="#'.
627: $rid.'">'.$hash{'title_'.$rid}.
628: '</a><br>');
629: }
630: }
631: }
632: }
1.2 www 633: # ----------------------------------------------------------------- Start table
1.51 bowersj2 634: $r->print('<hr><table cols="'.$lcm.'" border="0">');
635: for ($i=0;$i<=$#rows;$i++) {
636: if ($rows[$i]) {
637: $r->print("\n<tr>");
638: my @colcont=split(/\&/,$rows[$i]);
639: my $avespan=$lcm/($#colcont+1);
640:
641: # for each item I wish to print on this row...
642: for ($j=0;$j<=$#colcont;$j++) {
643: my $indent;my $indentstr;
644: my $linkid;
645: my $rid=$colcont[$j];
1.36 www 646: $rid=~/(\d+)\.(\d+)$/;
1.51 bowersj2 647: my $src=
648: &Apache::lonnet::declutter($hash{'src_'.$1.'.'.$2});
649: my $symb=
650: &Apache::lonnet::declutter($hash{'map_id_'.$1}).'___'.$2.'___'.$src;
651: my $add='<td>';
652: my $adde='</td>';
653: my $hwk='<font color="#223322">';
654: my $hwke='</font>';
655: if ($rid=~/^l(\d+\.\d+),(.+)/) { $linkid=$1; $rid=$2; }
656: if ($rid=~/^i(\d+),(.+)/) { $indent=$1; $rid=$2; }
657: if ($rid=~/^h(.+)/) {
658: $rid=$1;
659: $add='<th bgcolor="#AAFF55"><a name="'.$rid.'">';
660: $adde='</th>';
1.36 www 661: if (($ENV{'user.adv'}) &&
1.51 bowersj2 662: ($parmhash{$symb.'.0.parameter_randompick'})) {
1.36 www 663: $adde=' (randomly select '.
1.51 bowersj2 664: $parmhash{$symb.'.0.parameter_randompick'}.
1.36 www 665: ')</th>';
666: }
1.51 bowersj2 667: }
668: if ($rid=~/^j(.+)/) { $rid=$1; }
669: if ($rid=~/^p(\d)(\d)\"([\w\: \(\)\/\,]*)\"(.+)/) {
670: # sub astatus describes what code/tcode mean
671: my $code=$1;
672: my $tcode=$2;
673: my $ctext=$3;
674: $rid=$4;
675:
676: # will open later
677: if ($tcode eq '1') {
678: $add='<td bgcolor="#AAAAAA">';
679: }
680:
681: # solved/correct
682: if ($code eq '3') {
683: $add='<td bgcolor="#AAFFAA">';
684: } elsif ($code eq '4') { # partially correct
685: $add='<td bgcolor="#E0FFAA">';
686: } else {
687: # not attempted
688:
689: # we end up here on network failure, so pick a neutral
690: # color if the network failed instead of bright red.
691: if ( $networkFailedFlag )
692: {
693: $add = '<td bgcolor="#AAAAAA">';
694: }
695: else
696: {
697: $add='<td bgcolor="#FFAAAA">';
698: }
699:
700: if ($tcode eq '2') { # open, not past due
701: $add='<td bgcolor="#FFFFAA">';
702: }
703:
704: if ($tcode eq '4') { # due in next 24 hours
705: $add='<td bgcolor="#FFFF33">';
706: $adde='</td>';
707: }
708: }
709: $hwk='<font color="#888811"><b>';
710: $hwke='</b></font>';
711: if ($code eq '1') {
712: $hwke='</b> ('.$ctext.')</font>';
713: }
714: if ($code eq '2' || $code eq '4') {
715: $hwk='<font color="#992222"><b>';
716: $hwke='</b> ('.$ctext.')</font>';
717: }
718: if ($code eq '3') {
719: $hwk='<font color="#229922"><b>';
720: $hwke='</b> ('.$ctext.')</font>';
721: }
722: if ($networkFailedFlag)
723: {
724: $hwke='</b> (status unavailable)</font>';
725: }
726: }
727: if ($rid && $hash{'src_'.$rid} eq $currenturl) {
728: $add=$add.'<a name="curloc"></a>'.
729: '<font color=red size=+2><b>> </b></font>';
730: $adde=
731: '<font color=red size=+2><b> <</b></font>'.$adde;
732: }
733: if ($discussiontimes{$symb}>$lastcheck) {
734: $adde=
735: '<img border=0 src="/adm/lonMisc/chat.gif">'.
736: $adde;
737: }
738: if ($error{$src}) {
739: foreach (split(/\,/,$error{$src})) {
740: if ($_) {
741: $adde=
742: ' <a href="/adm/email?display='.
743: &Apache::lonnet::escape($_).
744: '"><img src="/adm/lonMisc/bomb.gif" border=0></a>'
745: .$adde;
746: }
747: }
748: }
749: if ($feedback{$src}) {
750: foreach (split(/\,/,$feedback{$src})) {
751: if ($_) {
752: $adde=
753: ' <a href="/adm/email?display='.
754: &Apache::lonnet::escape($_).
755: '"><img src="/adm/lonMisc/feedback.gif" border=0></a>'
756: .$adde;
757: }
758: }
759: }
760: if ($indent) {
761: my $is=" ";
762: for(my $i=-1;$i<$indent;$i++) { $indentstr.=$is; }
763: }
764: if (!$linkid) { $linkid=$rid; }
1.38 www 765: if ($hash{'randomout_'.$rid}) {
766: $adde=' <i>(hidden)</i>'.$adde;
767: }
1.51 bowersj2 768: $r->print($add.$indentstr);
769: if ($rid) {
770: $r->print('<a href="'.$hash{'src_'.$linkid}.
1.39 www 771: (($hash{'src_'.$linkid}=~/\?/)?'&':'?').
772: 'symb='.&Apache::lonnet::escape($symb)
1.35 www 773: .'">'.
1.51 bowersj2 774: $hwk.$hash{'title_'.$rid}.$hwke.'</a>');
775: }
776: $r->print($adde);
777: }
778: $r->print('</tr>');
779: }
780: }
781: $r->print("\n</table>");
782: $r->print('</body></html>');
1.2 www 783: # -------------------------------------------------------------------- End page
1.51 bowersj2 784: }
1.2 www 785: # ------------------------------------------------------------- End render page
1.28 albertel 786: }
1.2 www 787: # ------------------------------------------------------------------ Untie hash
1.28 albertel 788: unless (untie(%hash)) {
1.51 bowersj2 789: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
790: "Could not untie coursemap $fn (browse).</font>");
1.28 albertel 791: }
792: unless (untie(%parmhash)) {
1.51 bowersj2 793: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
794: "Could not untie parmhash (browse).</font>");
1.28 albertel 795: }
796: return OK;
1.2 www 797: }
1.1 www 798:
1.51 bowersj2 799: sub new_handle {
800: my $r = shift;
801:
802: # Handle header-only request
803: if ($r->header_only) {
804: if ($ENV{'browser.mathml'}) {
805: $r->content_type('text/xml');
806: } else {
807: $r->content_type('text/html');
808: }
809: $r->send_http_header;
810: return OK;
811: }
812:
813: # Send header, don't cache this page
814: if ($ENV{'browser.mathml'}) {
815: $r->content_type('text/xml');
816: } else {
817: $r->content_type('text/html');
818: }
819: &Apache::loncommon::no_cache($r);
820: $r->send_http_header;
821:
822: # Initialize the nav map
823: my $navmap = Apache::lonnavmaps::navmap->new(
824: $ENV{"request.course.fn"}.".db",
825: $ENV{"request.course.fn"}."_parms.db", 1);
826:
1.55 bowersj2 827:
828: if (!defined($navmap)) {
829: my $requrl = $r->uri;
830: $ENV{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
831: return HTTP_NOT_ACCEPTABLE;
832: }
1.64 ! bowersj2 833:
! 834: # Header
! 835: $r->print(&Apache::loncommon::bodytag('Navigate Course Map','',
! 836: ''));
! 837: $r->print('<script>window.focus();</script>');
! 838: my $desc=$ENV{'course.'.$ENV{'request.course.id'}.'.description'};
! 839: if (defined($desc)) { $r->print("<h2>$desc</h2>\n"); }
! 840: my $date=localtime;
! 841: $r->print("<h3>$date</h3>\n");
! 842: $r->rflush();
! 843: $r->print('<img src="/adm/lonMisc/chat.gif"> New discussion since '.
! 844: timeToHumanString($navmap->{LAST_CHECK}).
! 845: '<br><img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>');
! 846: #if (($currenturl=~/^\/res/) &&
! 847: # ($currenturl!~/^\/res\/adm/)) {
! 848: # $r->print('<a href="#curloc">Current Location</a><p>');
! 849: #}
1.55 bowersj2 850:
851: # Check that it's defined
852: if (!($navmap->courseMapDefined())) {
853: $r->print('<font size="+2" color="red">Coursemap undefined.</font>' .
854: '</body></html>');
855: return OK;
856: }
857:
1.51 bowersj2 858: # Grab a resource object so we have access to the constants; this
859: # is technically not proper, but should be harmless
860: my $res = $navmap->firstResource();
861:
862: # Defines a status->color mapping, null string means don't color
863: my %colormap =
1.54 bowersj2 864: ( $res->NETWORK_FAILURE => '',
1.59 bowersj2 865: $res->CORRECT => '',
1.54 bowersj2 866: $res->EXCUSED => '#BBBBFF',
1.59 bowersj2 867: $res->PAST_DUE_ANSWER_LATER => '',
868: $res->PAST_DUE_NO_ANSWER => '',
869: $res->ANSWER_OPEN => '#CCFFCC',
1.54 bowersj2 870: $res->OPEN_LATER => '',
1.59 bowersj2 871: $res->TRIES_LEFT => '',
872: $res->INCORRECT => '',
873: $res->OPEN => '',
1.54 bowersj2 874: $res->NOTHING_SET => '' );
1.59 bowersj2 875: # And a special case in the nav map; what to do when the assignment
876: # is not yet done and due in less then 24 hours
877: my $hurryUpColor = "#FFCCCC";
878:
879: my %statusIconMap =
880: ( $res->NETWORK_FAILURE => '',
881: $res->NOTHING_SET => '',
882: $res->CORRECT => 'navmap.correct.gif',
883: $res->EXCUSED => 'navmap.correct.gif',
884: $res->PAST_DUE_NO_ANSWER => 'navmap.wrong.gif',
885: $res->PAST_DUE_ANSWER_LATER => 'navmap.wrong.gif',
886: $res->ANSWER_OPEN => 'navmap.wrong.gif',
887: $res->OPEN_LATER => '',
888: $res->TRIES_LEFT => 'navmap.open.gif',
889: $res->INCORRECT => 'navmap.wrong.gif',
890: $res->OPEN => 'navmap.open.gif' );
891:
892: my %condenseStatuses =
893: ( $res->NETWORK_FAILURE => 1,
894: $res->NOTHING_SET => 1,
1.61 bowersj2 895: $res->CORRECT => 1 );
1.51 bowersj2 896:
897: my %filterHash;
898: # Figure out what we're not displaying
899: foreach (split(/\,/, $ENV{"form.filter"})) {
900: if ($_) {
901: $filterHash{$_} = "1";
902: }
903: }
904:
1.63 bowersj2 905: my $currenturl = $ENV{'form.postdata'};
906: $currenturl=~s/^http\:\/\///;
907: $currenturl=~s/^[^\/]+//;
908: my $queryAdd = "postdata=" . &Apache::lonnet::escape($currenturl);
909:
910: $r->print("$currenturl<br/>");
911:
912:
1.51 bowersj2 913: # Begin the HTML table
1.59 bowersj2 914: # four cols: resource + indent, chat+feedback, icon, text string
1.51 bowersj2 915: $r->print('<table cellspacing="3" cellpadding="0" bgcolor="#FFFFFF">' ."\n");
916:
1.63 bowersj2 917: my $mapIterator = $navmap->getIterator(undef, undef, \%filterHash, 0);
1.51 bowersj2 918: my $curRes = $mapIterator->next();
1.53 bowersj2 919: undef $res; # so we don't accidentally use it later
1.51 bowersj2 920: my $indentLevel = -1;
1.61 bowersj2 921: my $indentString = "<img src=\"/adm/lonIcons/whitespace1.gif\" width=\"25\" height=\"1\" alt=\"\" border=\"0\" />";
1.51 bowersj2 922:
923: my $isNewBranch = 0;
1.59 bowersj2 924: my $now = time();
925: my $in24Hours = $now + 24 * 60 * 60;
1.51 bowersj2 926:
927: while ($curRes != $mapIterator->END_NAV_MAP) {
928: if ($curRes == $mapIterator->BEGIN_MAP() ||
1.52 bowersj2 929: $curRes == $mapIterator->BEGIN_BRANCH()) {
1.51 bowersj2 930: $indentLevel++;
931: }
932: if ($curRes == $mapIterator->END_MAP() ||
1.52 bowersj2 933: $curRes == $mapIterator->END_BRANCH()) {
1.51 bowersj2 934: $indentLevel--;
935: }
1.52 bowersj2 936: if ($curRes == $mapIterator->BEGIN_BRANCH()) {
937: $isNewBranch = 1;
1.51 bowersj2 938: }
939:
940: if (ref($curRes) && $curRes->src()) {
941:
1.59 bowersj2 942: # Step one: Decide which parts to show
1.61 bowersj2 943: my @parts = @{$curRes->parts()};
944: my $multipart = scalar(@parts) > 1;
945: my $condensed = 0;
946:
1.51 bowersj2 947: if ($curRes->is_problem()) {
1.59 bowersj2 948: # Is it multipart?
949:
950: if ($multipart) {
951: # If it's multipart, see if part 0 is "open"
952: # if it is, display all parts, if it isn't,
953: # just display first
954: if (!$curRes->opendate("0")) {
955: @parts = ("0"); # just display the zero-th part
956: } else {
957: # Otherwise, only display part 0 if we want to
958: # attach feedback or email information to it
1.61 bowersj2 959: if ($curRes->hasDiscussion() || $curRes->getFeedback()) {
1.59 bowersj2 960: shift @parts;
961: } else {
962: # If there's discussion or feedback, that counts
963: # as a difference, so display the parts.
964:
965: # Now, we decide whether to condense the
966: # parts due to similarity
1.61 bowersj2 967: my $status = $curRes->status($parts[1]);
968: my $due = $curRes->duedate($parts[1]);
1.62 bowersj2 969: my $open = $curRes->opendate($parts[1]);
1.61 bowersj2 970: my $statusAllSame = 1;
971: my $dueAllSame = 1;
972: my $openAllSame = 1;
973: for (my $i = 2; $i < scalar(@parts); $i++) {
1.59 bowersj2 974: if ($curRes->status($parts[$i]) != $status){
1.61 bowersj2 975: $statusAllSame = 0;
976: }
977: if ($curRes->duedate($parts[$i]) != $due ) {
978: $dueAllSame = 0;
979: }
980: if ($curRes->opendate($parts[$i]) != $open) {
981: $openAllSame = 0;
1.59 bowersj2 982: }
983: }
984:
985: # $allSame is true if all the statuses were
986: # the same. Now, if they are all the same and
1.61 bowersj2 987: # match one of the statuses to condense, or they
988: # are all open with the same due date, or they are
989: # all OPEN_LATER with the same open date, display the
990: # status of the first non-zero part (to get the 'correct'
991: # status right, since 0 is never 'correct' or 'open').
992: if (($statusAllSame && defined($condenseStatuses{$status})) ||
993: ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
994: ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
995: @parts = ($parts[1]);
996: $condensed = 1;
1.59 bowersj2 997: }
998: }
999: }
1000: }
1001:
1.51 bowersj2 1002: } else {
1003: @parts[0] = "0"; # this is to get past foreach loop below
1004: # you can consider a non-problem resource as a resource
1005: # with only one part without loss
1006: }
1007:
1.59 bowersj2 1008: # Step two: Show the parts
1.51 bowersj2 1009: foreach my $part (@parts) {
1.59 bowersj2 1010:
1011: my $deltalevel = 0; # for inserting the branch icon
1.51 bowersj2 1012:
1013: # For each thing we're displaying...
1014:
1015: my $stack = $mapIterator->getStack();
1016: my $src = getLinkForResource($stack);
1017:
1018: my $srcHasQuestion = $src =~ /\?/;
1019: my $link = $src.
1020: ($srcHasQuestion?'&':'?') .
1021: 'symb='.&Apache::lonnet::escape($curRes->symb()).
1022: '"';
1023: my $title = $curRes->title();
1024: my $partLabel = "";
1.52 bowersj2 1025: my $newBranchText = "";
1026:
1027: # If this is a new branch, label it so
1028: # (temporary, this should be an icon w/ alt text)
1029: if ($isNewBranch) {
1.59 bowersj2 1030: $newBranchText = "<img src=\"/adm/lonIcons/branch.gif\" border=\"0\">";
1.52 bowersj2 1031: $isNewBranch = 0;
1.59 bowersj2 1032: $deltalevel = 1;
1.52 bowersj2 1033: }
1.51 bowersj2 1034:
1035: # links to open and close the folders
1036: my $linkopen = "<a href=\"$link\">";
1037: my $linkclose = "</a>";
1038:
1.61 bowersj2 1039: my $icon = "<img src=\"/adm/lonIcons/html.gif\" alt=\"\" border=\"0\" />";
1.51 bowersj2 1040: if ($curRes->is_problem()) {
1.61 bowersj2 1041: if ($part eq "0" || $condensed) {
1042: $icon = '<img src="/adm/lonIcons/problem.gif" alt="" border=\"0\" />';
1043: } else {
1044: $icon = $indentString;
1045: }
1.51 bowersj2 1046: }
1047:
1048: # Display the correct icon, link to open or shut map
1049: if ($curRes->is_map()) {
1050: my $mapId = $curRes->map_pc();
1051: my $nowOpen = !defined($filterHash{$mapId});
1052: $icon = $nowOpen ?
1.63 bowersj2 1053: "folder_closed.gif" : "folder_opened.gif";
1.61 bowersj2 1054: $icon = "<img src=\"/adm/lonIcons/$icon\" alt=\"\" border=\"0\" />";
1.52 bowersj2 1055: $linkopen = "<a href=\"/adm/navmaps?filter=";
1.51 bowersj2 1056: $linkopen .= $nowOpen ?
1057: addToFilter(\%filterHash, $mapId) :
1058: removeFromFilter(\%filterHash, $mapId);
1.63 bowersj2 1059: $linkopen .= "&$queryAdd\">";
1.51 bowersj2 1060: $linkclose = "</a>";
1061:
1062: }
1063:
1064: my $colorizer = "";
1065: if ($curRes->is_problem()) {
1066: my $status = $curRes->status($part);
1067: my $color = $colormap{$status};
1068: if ($color ne "") {
1069: $colorizer = "bgcolor=\"$color\"";
1070: }
1071: }
1072:
1.59 bowersj2 1073: # FIRST ROW: The resource indentation, branch icon, and name
1.51 bowersj2 1074: $r->print(" <tr><td align=\"left\" valign=\"top\">\n");
1075:
1076: # print indentation
1.59 bowersj2 1077: for (my $i = 0; $i < $indentLevel - $deltalevel; $i++) {
1.51 bowersj2 1078: $r->print($indentString);
1079: }
1080:
1.61 bowersj2 1081: $r->print(" ${newBranchText}${linkopen}$icon${linkclose}\n");
1.51 bowersj2 1082:
1.61 bowersj2 1083: if ($curRes->is_problem() && $part != "0" && !$condensed) {
1084: $partLabel = " (Part $part)";
1085: $title = "";
1086: }
1.51 bowersj2 1087:
1.59 bowersj2 1088: $r->print(" <a href=\"$link\">$title$partLabel</a></td>");
1089:
1090: my $discussionHTML = ""; my $feedbackHTML = "";
1091:
1092: # SECOND ROW: Is there text or feedback?
1093: if ($curRes->hasDiscussion()) {
1094: $discussionHTML = $linkopen .
1095: '<img border="0" src="/adm/lonMisc/chat.gif" />' .
1096: $linkclose;
1097: }
1098:
1099: if ($curRes->getFeedback()) {
1100: my $feedback = $curRes->getFeedback();
1101: foreach (split(/\,/, $feedback)) {
1102: if ($_) {
1103: $feedbackHTML .= ' <a href="/adm/email?display='
1104: . &Apache::lonnet::escape($_) . '">'
1105: . '<img src="/adm/lonMisc/feedback.gif" '
1106: . 'border="0" /></a>';
1107: }
1108: }
1109: }
1110:
1111: $r->print("<td align=\"left\">$discussionHTML$feedbackHTML</td>");
1112:
1.61 bowersj2 1113: # Is this the first displayed part of a multi-part problem
1114: # that has not been condensed, so we should suppress these two
1115: # columns?
1116: my $firstDisplayed = !$condensed && $multipart && $part eq "0";
1117:
1.59 bowersj2 1118: # THIRD ROW: Problem status icon
1.61 bowersj2 1119: if ($curRes->is_problem() &&
1120: !$firstDisplayed) {
1121: my $icon = $statusIconMap{$curRes->status($part)};
1.59 bowersj2 1122: if ($icon) {
1123: $r->print("<td width=\"50\" align=\"right\">$linkopen<img src=\"/adm/lonIcons/$icon\" border=\"0\" />$linkclose</td>\n");
1124: } else {
1125: $r->print("<td></td>\n");
1126: }
1127: } else { # not problem, no icon
1128: $r->print("<td></td>\n");
1129: }
1130:
1131: # FOURTH ROW: Text description
1132: $r->print("<td $colorizer align=\"right\">\n");
1.51 bowersj2 1133:
1.61 bowersj2 1134: if ($curRes->kind() eq "res" &&
1135: $curRes->is_problem() &&
1136: !$firstDisplayed) {
1.53 bowersj2 1137: $r->print (getDescription($curRes, $part));
1.51 bowersj2 1138: }
1.59 bowersj2 1139:
1140: $r->print("</td></tr>\n");
1.51 bowersj2 1141: }
1142: }
1143: $curRes = $mapIterator->next();
1144: }
1145:
1146: $r->print("</table></body></html>");
1147:
1.59 bowersj2 1148: $navmap->untieHashes();
1149:
1.51 bowersj2 1150: return OK;
1151: }
1152:
1153: # Convenience functions: Returns a string that adds or subtracts
1154: # the second argument from the first hash, appropriate for the
1155: # query string that determines which folders to recurse on
1156: sub addToFilter {
1157: my $hashIn = shift;
1158: my $addition = shift;
1159: my %hash = %$hashIn;
1160: $hash{$addition} = 1;
1161:
1162: return join (",", keys(%hash));
1163: }
1164:
1165: sub removeFromFilter {
1166: my $hashIn = shift;
1167: my $subtraction = shift;
1168: my %hash = %$hashIn;
1169:
1170: delete $hash{$subtraction};
1171: return join(",", keys(%hash));
1172: }
1173:
1174: # Convenience function: Given a stack returned from getStack on the iterator,
1175: # return the correct src() value.
1176: # Later, this should add an anchor when we start putting anchors in pages.
1177: sub getLinkForResource {
1178: my $stack = shift;
1179: my $res;
1180:
1181: # Check to see if there are any pages in the stack
1182: foreach $res (@$stack) {
1183: if (defined($res) && $res->is_page()) {
1184: return $res->src();
1185: }
1186: }
1187:
1188: # Failing that, return the src of the last resource that is defined
1189: # (when we first recurse on a map, it puts an undefined resource
1190: # on the bottom because $self->{HERE} isn't defined yet, and we
1191: # want the src for the map anyhow)
1192: foreach (@$stack) {
1193: if (defined($_)) { $res = $_; }
1194: }
1195:
1196: return $res->src();
1197: }
1198:
1.53 bowersj2 1199: # Convenience function: This seperates the logic of how to create
1200: # the problem text strings ("Due: DATE", "Open: DATE", "Not yet assigned",
1201: # etc.) into a seperate function. It takes a resource object as the
1202: # first parameter, and the part number of the resource as the second.
1203: # It's basically a big switch statement on the status of the resource.
1204:
1205: sub getDescription {
1206: my $res = shift;
1207: my $part = shift;
1208: my $status = $res->getDateStatus();
1209:
1210: if ($status == $res->NETWORK_FAILURE) { return ""; }
1211: if ($status == $res->NOTHING_SET) {
1212: return "Not currently assigned.";
1213: }
1214: if ($status == $res->OPEN_LATER) {
1215: return "Opens: " . timeToHumanString($res->opendate($part));
1216: }
1217: if ($status == $res->OPEN) {
1.59 bowersj2 1218: if ($res->duedate()) {
1219: return "Due: $status " . timeToHumanString($res->duedate($part));
1220: } else {
1221: return "Open, no due date";
1222: }
1.53 bowersj2 1223: }
1.54 bowersj2 1224: if ($status == $res->PAST_DUE_ANSWER_LATER) {
1.59 bowersj2 1225: return "Answer opens: " . timeToHumanString($res->answerdate($part));
1.54 bowersj2 1226: }
1227: if ($status == $res->PAST_DUE_NO_ANSWER) {
1228: return "Was Due: " . timeToHumanString($res->duedate($part));
1.53 bowersj2 1229: }
1230: if ($status == $res->ANSWER_OPEN) {
1231: return "Answer available";
1232: }
1.59 bowersj2 1233: if ($status == $res->EXCUSED) {
1234: return "Excused by instructor";
1235: }
1236: if ($status == $res->TRIES_LEFT) {
1237: my $tries = $res->tries();
1238: my $maxtries = $res->maxtries();
1239: my $triesString = "($tries of $maxtries tries used)";
1240: if ($res->duedate()) {
1241: return "Due: $status " . timeToHumanString($res->duedate($part)) .
1242: " $triesString";
1243: } else {
1244: return "No due date $triesString";
1245: }
1246: }
1.53 bowersj2 1247: }
1248:
1249: # I want to change this into something more human-friendly. For
1250: # now, this is a simple call to localtime. The final function
1251: # probably belongs in loncommon.
1252: sub timeToHumanString {
1.58 albertel 1253: my ($time) = @_;
1254: # zero, '0' and blank are bad times
1255: if ($time) {
1256: return localtime($time);
1257: } else {
1258: return 'Never';
1259: }
1.53 bowersj2 1260: }
1261:
1.51 bowersj2 1262: 1;
1263:
1264: package Apache::lonnavmaps::navmap;
1265:
1266: # LEFT:
1267: # * Actual handler code (multi-part)
1268: # * Branches (aieee!) (and conditionals)
1269:
1270: =pod
1271:
1272: lonnavmaps provides functions and objects for dealing with the compiled course hashes generated when a user enters the course, and also provides the Apache handler for the "Navigation Map" button.
1273:
1274: =head1 navmap object: Encapsulating the compiled nav map
1275:
1276: navmap is an object that encapsulates a compiled course map and provides a reasonable interface to it.
1277:
1278: Most notably it provides a way to navigate the map sensibly and a flexible iterator that makes it easy to write various renderers based on nav maps.
1279:
1280: You must obtain resource objects through the navmap object.
1281:
1282: =head2 Methods
1283:
1284: =over 4
1285:
1.55 bowersj2 1286: =item * B<new>(filename, parmHashFile, genCourseAndUserOptions, genMailDiscussStatus): Binds a new navmap object to the compiled course representation and parmHashFile. genCourseAndUserOptions is a flag saying whether the course options and user options hash should be generated. This is for when you are using the parameters of the resources that require them; see documentation in resource object documentation. genMailDiscussStatus causes the nav map to retreive information about the email and discussion status of resources. Returns the navmap object if this is successful, or B<undef> if not. You must check for undef; errors will occur when you try to use the other methods otherwise.
1.51 bowersj2 1287:
1288: =item * B<getIterator>(first, finish, filter, condition): See iterator documentation below.
1289:
1290: =cut
1291:
1292: use strict;
1293: use GDBM_File;
1294:
1295: sub new {
1296: # magic invocation to create a class instance
1297: my $proto = shift;
1298: my $class = ref($proto) || $proto;
1299: my $self = {};
1300:
1301: $self->{NAV_HASH_FILE} = shift;
1302: $self->{PARM_HASH_FILE} = shift;
1303: $self->{GENERATE_COURSE_USER_OPT} = shift;
1.55 bowersj2 1304: $self->{GENERATE_EMAIL_DISCUSS_STATUS} = shift;
1.51 bowersj2 1305:
1306: # Resource cache stores navmapresource's as we reference them. We generate
1307: # them on-demand so we don't pay for creating resources unless we use them.
1308: $self->{RESOURCE_CACHE} = {};
1309:
1310: # Network failure flag, if we accessed the course or user opt and
1311: # failed
1312: $self->{NETWORK_FAILURE} = 0;
1313:
1314: # tie the nav hash
1315: my %navmaphash;
1316: if (!(tie(%navmaphash, 'GDBM_File', $self->{NAV_HASH_FILE},
1317: &GDBM_READER(), 0640))) {
1318: return undef;
1319: }
1320: $self->{NAV_HASH} = \%navmaphash;
1321:
1322: my %parmhash;
1323: if (!(tie(%parmhash, 'GDBM_File', $self->{PARM_HASH_FILE},
1324: &GDBM_READER(), 0640)))
1325: {
1.60 bowersj2 1326: untie $self->{PARM_HASH};
1.51 bowersj2 1327: return undef;
1328: }
1329: $self->{PARM_HASH} = \%parmhash;
1330: $self->{HASH_TIED} = 1;
1331:
1332: # If the course opt hash and the user opt hash should be generated,
1333: # generate them
1334: if ($self->{GENERATE_COURSE_USER_OPT}) {
1335: my $uname=$ENV{'user.name'};
1336: my $udom=$ENV{'user.domain'};
1337: my $uhome=$ENV{'user.home'};
1338: my $cid=$ENV{'request.course.id'};
1339: my $chome=$ENV{'course.'.$cid.'.home'};
1340: my ($cdom,$cnum)=split(/\_/,$cid);
1341:
1342: my $userprefix=$uname.'_'.$udom.'_';
1343:
1344: my %courserdatas; my %useropt; my %courseopt;
1345: unless ($uhome eq 'no_host') {
1346: # ------------------------------------------------- Get coursedata (if present)
1347: unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
1348: my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
1349: ':resourcedata',$chome);
1350: if ($reply!~/^error\:/) {
1351: $courserdatas{$cid}=$reply;
1352: $courserdatas{$cid.'.last_cache'}=time;
1353: }
1354: # check to see if network failed
1355: elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
1356: {
1357: $self->{NETWORK_FAILURE} = 1;
1358: }
1359: }
1360: foreach (split(/\&/,$courserdatas{$cid})) {
1361: my ($name,$value)=split(/\=/,$_);
1362: $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
1363: &Apache::lonnet::unescape($value);
1364: }
1365: # --------------------------------------------------- Get userdata (if present)
1366: unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
1367: my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
1368: if ($reply!~/^error\:/) {
1369: $userrdatas{$uname.'___'.$udom}=$reply;
1370: $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
1371: }
1372: # check to see if network failed
1373: elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
1374: {
1375: $self->{NETWORK_FAILURE} = 1;
1376: }
1377: }
1378: foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
1379: my ($name,$value)=split(/\=/,$_);
1380: $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
1381: &Apache::lonnet::unescape($value);
1382: }
1.55 bowersj2 1383: $self->{COURSE_OPT} = \%courseopt;
1384: $self->{USER_OPT} = \%useropt;
1.51 bowersj2 1385: }
1386: }
1387:
1.55 bowersj2 1388: if ($self->{GENERATE_EMAIL_DISCUSS_STATUS}) {
1389: my $cid=$ENV{'request.course.id'};
1390: my ($cdom,$cnum)=split(/\_/,$cid);
1391:
1392: my %emailstatus = &Apache::lonnet::dump('email_status');
1.56 bowersj2 1393: my $logoutTime = $emailstatus{'logout'};
1394: my $courseLeaveTime = $emailstatus{'logout_'.$ENV{'request.course.id'}};
1.55 bowersj2 1395: $self->{LAST_CHECK} = ($courseLeaveTime > $logoutTime ?
1396: $courseLeaveTime : $logoutTime);
1.56 bowersj2 1397: my %discussiontime = &Apache::lonnet::dump('discussiontimes',
1.55 bowersj2 1398: $cdom, $cnum);
1399: my %feedback=();
1400: my %error=();
1401: my $keys = &Apache::lonnet::reply('keys:'.
1402: $ENV{'user.domain'}.':'.
1403: $ENV{'user.name'}.':nohist_email',
1404: $ENV{'user.home'});
1405:
1406: foreach my $msgid (split(/\&/, $keys)) {
1407: $msgid=&Apache::lonnet::unescape($msgid);
1408: my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
1409: if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
1410: my ($what,$url)=($1,$2);
1411: my %status=
1412: &Apache::lonnet::get('email_status',[$msgid]);
1413: if ($status{$msgid}=~/^error\:/) {
1414: $status{$msgid}='';
1415: }
1416:
1417: if (($status{$msgid} eq 'new') ||
1418: (!$status{$msgid})) {
1419: if ($what eq 'Error') {
1420: $error{$url}.=','.$msgid;
1421: } else {
1422: $feedback{$url}.=','.$msgid;
1423: }
1424: }
1425: }
1426: }
1427:
1428: $self->{FEEDBACK} = \%feedback;
1429: $self->{ERROR_MSG} = \%error; # what is this? JB
1430: $self->{DISCUSSION_TIME} = \%discussiontime;
1431: $self->{EMAIL_STATUS} = \%emailstatus;
1432:
1433: }
1434:
1.51 bowersj2 1435: bless($self);
1.55 bowersj2 1436:
1437: return $self;
1438: }
1.51 bowersj2 1439:
1.55 bowersj2 1440: # Checks to see if coursemap is defined, matching test in old lonnavmaps
1441: sub courseMapDefined {
1442: my $self = shift;
1443: my $uri = &Apache::lonnet::clutter($ENV{'request.course.uri'});
1444:
1.56 bowersj2 1445: my $firstres = $self->{NAV_HASH}->{"map_start_$uri"};
1446: my $lastres = $self->{NAV_HASH}->{"map_finish_$uri"};
1.55 bowersj2 1447: return $firstres && $lastres;
1.51 bowersj2 1448: }
1449:
1450: sub getIterator {
1451: my $self = shift;
1452: my $iterator = Apache::lonnavmaps::iterator->new($self, shift, shift,
1.63 bowersj2 1453: shift, undef, shift);
1.51 bowersj2 1454: return $iterator;
1455: }
1456:
1457: # unties the hash when done
1458: sub untieHashes {
1459: my $self = shift;
1.60 bowersj2 1460: untie %{$self->{NAV_HASH}} if ($self->{HASH_TIED});
1461: untie %{$self->{PARM_HASH}} if ($self->{HASH_TIED});
1.51 bowersj2 1462: $self->{HASH_TIED} = 0;
1463: }
1464:
1465: # when the object is destroyed, be sure to untie all the hashes we tied.
1466: sub DESTROY {
1467: my $self = shift;
1468: $self->untieHashes();
1469: }
1470:
1.59 bowersj2 1471: # Private function: Does the given resource (as a symb string) have
1472: # current discussion? Returns 0 if chat/mail data not extracted.
1473: sub hasDiscussion {
1474: my $self = shift;
1475: my $symb = shift;
1476: if (!defined($self->{DISCUSSION_TIME})) { return 0; }
1477:
1478: return $self->{DISCUSSION_TIME}->{$symb} >
1479: $self->{LAST_CHECK};
1480: }
1481:
1482: # Private function: Does the given resource (as a symb string) have
1483: # current feedback? Returns the string in the feedback hash, which
1484: # will be false if it does not exist.
1485: sub getFeedback {
1486: my $self = shift;
1487: my $symb = shift;
1488:
1489: if (!defined($self->{FEEDBACK})) { return ""; }
1490:
1491: return $self->{FEEDBACK}->{$symb};
1492: }
1493:
1.51 bowersj2 1494: =pod
1495:
1496: =item * B<getById>(id): Based on the ID of the resource (1.1, 3.2, etc.), get a resource object for that resource. This method, or other methods that use it (as in the resource object) is the only proper way to obtain a resource object.
1497:
1498: =cut
1499:
1500: # The strategy here is to cache the resource objects, and only construct them
1501: # as we use them. The real point is to prevent reading any more from the tied
1502: # hash then we have to, which should hopefully alleviate speed problems.
1503: # Caching is just an incidental detail I throw in because it makes sense.
1504:
1505: sub getById {
1506: my $self = shift;
1507: my $id = shift;
1508:
1509: if (defined ($self->{RESOURCE_CACHE}->{$id}))
1510: {
1511: return $self->{RESOURCE_CACHE}->{$id};
1512: }
1513:
1514: # resource handles inserting itself into cache.
1515: return Apache::lonnavmaps::resource->new($self, $id);
1516: }
1517:
1518: =pod
1519:
1520: =item * B<firstResource>(): Returns a resource object reference corresponding to the first resource in the navmap.
1521:
1522: =cut
1523:
1524: sub firstResource {
1525: my $self = shift;
1526: my $firstResource = $self->{NAV_HASH}->{'map_start_' .
1527: &Apache::lonnet::clutter($ENV{'request.course.uri'})};
1528: return $self->getById($firstResource);
1529: }
1530:
1531: =pod
1532:
1533: =item * B<finishResource>(): Returns a resource object reference corresponding to the last resource in the navmap.
1534:
1535: =cut
1536:
1537: sub finishResource {
1538: my $self = shift;
1539: my $firstResource = $self->{NAV_HASH}->{'map_finish_' .
1540: &Apache::lonnet::clutter($ENV{'request.course.uri'})};
1541: return $self->getById($firstResource);
1542: }
1543:
1544: # -------------------------------------------- Figure out a cascading parameter
1545: #
1546: # For this function to work
1547: #
1548: # * parmhash needs to be tied
1549: # * courseopt and useropt need to be initialized for this user and course
1550: #
1551:
1552: sub parmval {
1553: my $self = shift;
1554: my ($what,$symb)=@_;
1555: my $cid=$ENV{'request.course.id'};
1556: my $csec=$ENV{'request.course.sec'};
1557: my $uname=$ENV{'user.name'};
1558: my $udom=$ENV{'user.domain'};
1559:
1560: unless ($symb) { return ''; }
1561: my $result='';
1562:
1563: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
1564:
1565: # ----------------------------------------------------- Cascading lookup scheme
1566: my $rwhat=$what;
1567: $what=~s/^parameter\_//;
1568: $what=~s/\_/\./;
1569:
1570: my $symbparm=$symb.'.'.$what;
1571: my $mapparm=$mapname.'___(all).'.$what;
1572: my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
1573:
1574: my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
1575: my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
1576: my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
1577:
1578: my $courselevel= $usercourseprefix.'.'.$what;
1579: my $courselevelr=$usercourseprefix.'.'.$symbparm;
1580: my $courselevelm=$usercourseprefix.'.'.$mapparm;
1581:
1582: my $useropt = $self->{USER_OPT};
1583: my $courseopt = $self->{COURSE_OPT};
1584: my $parmhash = $self->{PARM_HASH};
1585:
1586: # ---------------------------------------------------------- first, check user
1587: if ($uname and defined($useropt)) {
1.57 albertel 1588: if (defined($$useropt{$courselevelr})) { return $$useropt{$courselevelr}; }
1589: if (defined($$useropt{$courselevelm})) { return $$useropt{$courselevelm}; }
1590: if (defined($$useropt{$courselevel})) { return $$useropt{$courselevel}; }
1.51 bowersj2 1591: }
1592:
1593: # ------------------------------------------------------- second, check course
1594: if ($csec and defined($courseopt)) {
1.57 albertel 1595: if (defined($$courseopt{$seclevelr})) { return $$courseopt{$seclevelr}; }
1596: if (defined($$courseopt{$seclevelm})) { return $$courseopt{$seclevelm}; }
1597: if (defined($$courseopt{$seclevel})) { return $$courseopt{$seclevel}; }
1.51 bowersj2 1598: }
1599:
1600: if (defined($courseopt)) {
1.57 albertel 1601: if (defined($$courseopt{$courselevelr})) { return $$courseopt{$courselevelr}; }
1602: if (defined($$courseopt{$courselevelm})) { return $$courseopt{$courselevelm}; }
1603: if (defined($$courseopt{$courselevel})) { return $$courseopt{$courselevel}; }
1.51 bowersj2 1604: }
1605:
1606: # ----------------------------------------------------- third, check map parms
1607:
1608: my $thisparm=$$parmhash{$symbparm};
1.57 albertel 1609: if (defined($thisparm)) { return $thisparm; }
1.51 bowersj2 1610:
1611: # ----------------------------------------------------- fourth , check default
1612:
1613: my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
1.57 albertel 1614: if (defined($default)) { return $default}
1.51 bowersj2 1615:
1616: # --------------------------------------------------- fifth , cascade up parts
1617:
1618: my ($space,@qualifier)=split(/\./,$rwhat);
1619: my $qualifier=join('.',@qualifier);
1620: unless ($space eq '0') {
1621: my ($part,$id)=split(/\_/,$space);
1622: if ($id) {
1623: my $partgeneral=$self->parmval($part.".$qualifier",$symb);
1.57 albertel 1624: if (defined($partgeneral)) { return $partgeneral; }
1.51 bowersj2 1625: } else {
1626: my $resourcegeneral=$self->parmval("0.$qualifier",$symb);
1.57 albertel 1627: if (defined($resourcegeneral)) { return $resourcegeneral; }
1.51 bowersj2 1628: }
1629: }
1630: return '';
1631: }
1632:
1633:
1634: 1;
1635:
1636: package Apache::lonnavmaps::iterator;
1637:
1638: # This package must precede "navmap" because "navmap" uses it. In
1639: # order to keep the documentation order straight, the iterator is documented
1640: # after the navmap object.
1641:
1642: =pod
1643:
1644: =back
1645:
1646: =head1 navmap Iterator
1647:
1648: An I<iterator> encapsulates the logic required to traverse a data structure. navmap uses an iterator to traverse the course map according to the criteria you wish to use.
1649:
1650: To obtain an iterator, call the B<getIterator>() function of a B<navmap> object. (Do not instantiate Apache::lonnavmaps::iterator directly.) This will return a reference to the iterator:
1651:
1652: C<my $resourceIterator = $navmap-E<gt>getIterator();>
1653:
1654: To get the next thing from the iterator, call B<next>:
1655:
1656: C<my $nextThing = $resourceIterator-E<gt>next()>
1657:
1658: getIterator behaves as follows:
1659:
1660: =over 4
1661:
1.63 bowersj2 1662: =item B<getIterator>(nav_map, firstResource, finishResource, filterHash, condition): All parameters are optional. firstResource is a resource reference corresponding to where the iterator should start. It defaults to navmap->firstResource() for the corresponding nav map. finishResource corresponds to where you want the iterator to end, defaulting to navmap->finishResource(). It is your responsibility to ensure that the iterator will actually get there. filterHash is a hash used as a set containing strings representing the resource IDs, defaulting to empty. Condition is a 1 or 0 that sets what to do with the filter hash: If a 0, then only resource that exist IN the filterHash will be recursed on. If it is a 1, only resources NOT in the filterHash will be recursed on. Defaults to 0, which is to say, do not recurse unless explicitly asked to.
1.51 bowersj2 1663:
1664: Thus, by default, all resources will be shown. Change the condition to a 1 without changing the hash, and only the top level of the map will be shown. Changing the condition to 1 and including some values in the hash will allow you to selectively examine parts of the navmap, while leaving it on 0 and adding things to the hash will allow you to selectively ignore parts of the nav map. See the handler code for examples.
1665:
1666: The iterator will return either a reference to a resource object, or a token representing something in the map, such as the beginning of a new branch. The possible tokens are:
1667:
1668: =over 4
1669:
1670: =item * some list of tokens here
1671:
1672: =back
1673:
1674: The tokens are retreivable via methods on the iterator object, i.e., $iterator->END_NAV_MAP. (Perl will automatically optimize these into constants.
1675:
1676: =cut
1677:
1678: # Here are the tokens for the iterator:
1679:
1680: sub END_NAV_MAP { return 0; } # Represents end of entire nav map
1681: sub BEGIN_MAP { return 1; } # begining of a new map
1682: sub END_MAP { return 2; } # end of the map
1683: sub BEGIN_BRANCH { return 3; } # beginning of a branch
1684: sub END_BRANCH { return 4; } # end of a branch
1685:
1686: # Params: nav map, start resource, end resource, filter, condition,
1687: # already seen hash ref
1688:
1689: sub new {
1690: # magic invocation to create a class instance
1691: my $proto = shift;
1692: my $class = ref($proto) || $proto;
1693: my $self = {};
1694:
1695: $self->{NAV_MAP} = shift;
1696: return undef unless ($self->{NAV_MAP});
1697:
1698: # Handle the parameters
1699: $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
1700: $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
1701:
1702: # If the given resources are just the ID of the resource, get the
1703: # objects
1704: if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} =
1705: $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
1706: if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} =
1707: $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
1708:
1709: $self->{FILTER} = shift;
1710:
1711: # A hash, used as a set, of resource already seen
1712: $self->{ALREADY_SEEN} = shift;
1713: if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1.63 bowersj2 1714: $self->{CONDITION} = shift;
1.51 bowersj2 1715:
1716: # Flag: Have we started yet? If not, the first action is to return BEGIN_MAP.
1717: $self->{STARTED} = 0;
1718:
1719: # Should we continue calling the recursive iterator, if any?
1720: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1721: # The recursive iterator, if any
1722: $self->{RECURSIVE_ITERATOR} = undef;
1723: # Are we recursing on a map, or a branch?
1724: $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
1725: # And the count of how deep it is, so that this iterator can keep track of
1726: # when to pick back up again.
1727: $self->{RECURSIVE_DEPTH} = 0;
1728:
1729: # For keeping track of our branches, we maintain our own stack
1730: $self->{BRANCH_STACK} = [];
1731: # If the size shrinks, we exhausted a branch
1732: $self->{BRANCH_STACK_SIZE} = 0;
1733: $self->{BRANCH_DEPTH} = 0;
1734:
1735: # For returning two things in a forced sequence
1736: $self->{FORCE_NEXT} = undef;
1737:
1738: # Start with the first resource
1739: push @{$self->{BRANCH_STACK}}, $self->{FIRST_RESOURCE};
1740: $self->{BRANCH_STACK_SIZE} = 1;
1741:
1742: bless($self);
1743: return $self;
1744: }
1745:
1746: # Note... this function is *touchy*. I strongly recommend tracing
1747: # through it with the debugger a few times on a non-trivial map before
1748: # modifying it. Order is *everything*.
1749: sub next {
1750: my $self = shift;
1751:
1752: # Iterator logic goes here
1753:
1754: # Is this return value pre-determined?
1755: if (defined($self->{FORCE_NEXT})) {
1756: my $tmp = $self->{FORCE_NEXT};
1757: $self->{FORCE_NEXT} = undef;
1758: return $tmp;
1759: }
1760:
1761: # Are we using a recursive iterator? If so, pull from that and
1762: # watch the depth; we want to resume our level at the correct time.
1763: if ($self->{RECURSIVE_ITERATOR_FLAG})
1764: {
1765: # grab the next from the recursive iterator
1766: my $next = $self->{RECURSIVE_ITERATOR}->next();
1767:
1768: # is it a begin or end map? Update depth if so
1769: if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
1770: if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
1771:
1772: # Are we back at depth 0? If so, stop recursing.
1773: if ($self->{RECURSIVE_DEPTH} == 0) {
1774: $self->{RECURSIVE_ITERATOR_FLAG} = 0;
1775: }
1776:
1777: return $next;
1778: }
1779:
1780: # Is there a current resource to grab? If not, then return
1781: # END_BRANCH and END_MAP in succession.
1782: if (scalar(@{$self->{BRANCH_STACK}}) == 0) {
1783: if ($self->{BRANCH_DEPTH} > 0) {
1784: $self->{FORCE_NEXT} = $self->END_MAP();
1.52 bowersj2 1785: $self->{BRANCH_DEPTH}--;
1.51 bowersj2 1786: return $self->END_BRANCH();
1787: } else {
1788: return $self->END_MAP();
1789: }
1790: }
1791:
1792: # Have we not yet begun? If not, return BEGIN_MAP and
1793: # remember that we've started.
1794: if ( !$self->{STARTED} ) {
1795: $self->{STARTED} = 1;
1796: return $self->BEGIN_MAP;
1797: }
1798:
1799: # Did the branch stack shrink since last run? If so,
1800: # we exhausted a branch last time, therefore, we're about
1801: # to start a new one. (We know because we already checked to see
1802: # if the stack was empty.)
1803: if ( scalar (@{$self->{BRANCH_STACK}}) < $self->{BRANCH_STACK_SIZE}) {
1.52 bowersj2 1804: $self->{BRANCH_STACK_SIZE} = scalar(@{$self->{BRANCH_STACK}});
1805: $self->{BRANCH_DEPTH}++;
1806: return $self->BEGIN_BRANCH();
1.51 bowersj2 1807: }
1808:
1.52 bowersj2 1809: # Remember the size for comparision next time.
1810: $self->{BRANCH_STACK_SIZE} = scalar(@{$self->{BRANCH_STACK}});
1811:
1812: # If the next resource we mean to return is going to need
1813: # a lower branch level, terminate branches until we get
1814: # there.
1815:
1.51 bowersj2 1816: # Get the next resource in the branch
1817: $self->{HERE} = pop @{$self->{BRANCH_STACK}};
1.52 bowersj2 1818:
1819: # Are we at the right depth? If not, close a branch and return
1820: # the current resource onto the branch stack
1821: if (defined($self->{HERE}->{DATA}->{ITERATOR_DEPTH})
1822: && $self->{HERE}->{DATA}->{ITERATOR_DEPTH} <
1823: $self->{BRANCH_DEPTH} ) {
1824: $self->{BRANCH_DEPTH}--;
1825: # return it so we can pick it up eventually
1826: push @{$self->{BRANCH_STACK}}, $self->{HERE};
1827: return $self->END_BRANCH();
1828: }
1829:
1.51 bowersj2 1830: # We always return it after this point and never before
1831: # (proof: look at just the return statements), so we
1832: # remember that we've seen this.
1833: $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
1834:
1835: # Are we at the utter end? If so, return the END_NAV_MAP marker.
1836: if ($self->{HERE} == $self->{NAV_MAP}->finishResource() ) {
1.52 bowersj2 1837: $self->{FORCE_NEXT} = $self->END_NAV_MAP;
1.51 bowersj2 1838: return $self->{HERE};
1839: }
1840:
1841: # Get the next possible resources
1842: my $nextUnfiltered = $self->{HERE}->getNext();
1843: my $next = [];
1844:
1845: # filter the next possibilities to remove things we've
1.52 bowersj2 1846: # already seen. Also, remember what branch depth they should
1847: # be displayed at, since there's no other reliable way to tell.
1.51 bowersj2 1848: foreach (@$nextUnfiltered) {
1.52 bowersj2 1849: if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
1850: push @$next, $_;
1851: $_->{DATA}->{ITERATOR_DEPTH} =
1852: $self->{BRANCH_DEPTH} + 1;
1853: }
1.51 bowersj2 1854: }
1855:
1856: # Handle branch cases:
1857: # Nothing is available next: BRANCH_END
1858: # 1 thing next: standard non-branch
1859: # 2+ things next: have some branches
1860: my $nextCount = scalar(@$next);
1861: if ($nextCount == 0) {
1.52 bowersj2 1862: # Return this and on the next run, close the branch up if we're
1863: # in a branch
1864: if ($self->{BRANCH_DEPTH} > 0 ) {
1865: $self->{FORCE_NEXT} = $self->END_BRANCH();
1866: $self->{BRANCH_DEPTH}--;
1867: }
1.51 bowersj2 1868: return $self->{HERE};
1869: }
1870:
1871: while (@$next) {
1872: # copy the next possibilities over to the branch stack
1873: # in the right order
1.52 bowersj2 1874: push @{$self->{BRANCH_STACK}}, shift @$next;
1.51 bowersj2 1875: }
1876:
1877: if ($nextCount >= 2) {
1.52 bowersj2 1878: $self->{FORCE_NEXT} = $self->BEGIN_BRANCH();
1879: $self->{BRANCH_DEPTH}++;
1.51 bowersj2 1880: return $self->{HERE};
1881: }
1882:
1883: # If this is a map and we want to recurse down it... (not filtered out)
1884: if ($self->{HERE}->is_map() &&
1.63 bowersj2 1885: (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
1.51 bowersj2 1886: $self->{RECURSIVE_ITERATOR_FLAG} = 1;
1887: my $firstResource = $self->{HERE}->map_start();
1888: my $finishResource = $self->{HERE}->map_finish();
1889:
1890: # Odd perl syntax here; $self->new allows one to create a new iterator
1891: # can't figure out how to ref this package directly correctly
1892: # isn't MAIN::new, __PACKAGE__::new or Apache::lonnavmaps::iterator->new
1893: $self->{RECURSIVE_ITERATOR} =
1894: Apache::lonnavmaps::iterator->new ($self->{NAV_MAP}, $firstResource,
1.63 bowersj2 1895: $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
1896: $self->{CONDITION});
1.51 bowersj2 1897: }
1898:
1899: return $self->{HERE};
1900: }
1901:
1902: =pod
1903:
1904: The other method available on the iterator is B<getStack>, which returns an array populated with the current 'stack' of maps, as references to the resource objects. Example: This is useful when making the navigation map, as we need to check whether we are under a page map to see if we need to link directly to the resource, or to the page. The first elements in the array will correspond to the top of the stack (most inclusive map).
1905:
1906: =cut
1907:
1908: sub getStack {
1909: my $self=shift;
1910:
1911: my @stack;
1912:
1913: $self->populateStack(\@stack);
1914:
1915: return \@stack;
1916: }
1917:
1918: # Private method: Calls the iterators recursively to populate the stack.
1919: sub populateStack {
1920: my $self=shift;
1921: my $stack = shift;
1922:
1923: push @$stack, $self->{HERE};
1924:
1925: if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1926: $self->{RECURSIVE_ITERATOR}->populateStack($stack);
1927: }
1928: }
1929:
1.1 www 1930: 1;
1.2 www 1931:
1.51 bowersj2 1932: package Apache::lonnavmaps::resource;
1933:
1934: use Apache::lonnet;
1935:
1936: =pod
1937:
1938: =head1 Object: resource
1939:
1940: A resource object encapsulates a resource in a resource map, allowing easy manipulation of the resource, querying the properties of the resource (including user properties), and represents a reference that can be used as the canonical representation of the resource by lonnavmap clients like renderers.
1941:
1942: A resource only makes sense in the context of a navmap, as some of the data is stored in the navmap object.
1943:
1944: You will probably never need to instantiate this object directly. Use Apache::lonnavmap::navmap, and use the "start" method to obtain the starting resource.
1945:
1946: =head2 Public Members
1947:
1948: resource objects have a hash called DATA ($resourceRef->{DATA}) that you can store whatever you want in. This allows you to easily do two-pass algorithms without worrying about managing your own resource->data hash.
1949:
1950: =head2 Methods
1951:
1952: =over 4
1953:
1954: =item * B<new>($navmapRef, $idString): The first arg is a reference to the parent navmap object. The second is the idString of the resource itself.
1955:
1956: =cut
1957:
1958: sub new {
1959: # magic invocation to create a class instance
1960: my $proto = shift;
1961: my $class = ref($proto) || $proto;
1962: my $self = {};
1963:
1964: $self->{NAV_MAP} = shift;
1965: $self->{ID} = shift;
1966:
1967: # Store this new resource in the parent nav map's cache.
1968: $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
1969:
1970: # A hash that can be used by two-pass algorithms to store data
1971: # about this resource in. Not used by the resource object
1972: # directly.
1973: $self->{DATA} = {};
1974:
1975: bless($self);
1976:
1977: return $self;
1978: }
1979:
1980: =pod
1981:
1982: =item * B<is_map>: Returns true if this is a map, false otherwise.
1983:
1984: =item * B<title>: Returns title.
1985:
1986: =item * B<type>: Returns the type of the resource, "start", "normal", or "finish".
1987:
1988: =item * B<problem>: Returns true if the resource is a problem type, false otherwise. (Looks at the extension on the src field.)
1989:
1990: =back
1991:
1992: =cut
1993:
1994: # private function: simplify the NAV_HASH lookups we keep doing
1995: # pass the name, and to automatically append my ID, pass a true val on the
1996: # second param
1997: sub navHash {
1998: my $self = shift;
1999: my $param = shift;
2000: my $id = shift;
2001: return $self->{NAV_MAP}->{NAV_HASH}->{$param . ($id?$self->{ID}:"")};
2002: }
2003:
2004: # These info functions can be used directly, as they don't return
2005: # resource information.
2006: sub title { my $self=shift; return $self->navHash("title_", 1); }
2007: sub type { my $self=shift; return $self->navHash("type_", 1); }
2008: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
2009: # "to" can return a comma seperated list for branches
2010: sub to { my $self=shift; return $self->navHash("to_", 1); }
2011: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
2012: sub src {
2013: my $self=shift;
2014: return $self->navHash("src_", 1);
2015: }
2016: sub symb {
2017: my $self=shift;
2018: (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
2019: my $symbSrc = &Apache::lonnet::declutter($self->src());
2020: return &Apache::lonnet::declutter(
2021: $self->navHash('map_id_'.$first))
2022: . '___' . $second . '___' . $symbSrc;
2023: }
2024: sub is_problem {
2025: my $self=shift;
2026: my $src = $self->src();
2027: return ($src =~ /problem$/);
2028: }
2029: sub is_html {
2030: my $self=shift;
2031: my $src = $self->src();
2032: return ($src =~ /html$/);
2033: }
2034: sub is_sequence {
2035: my $self=shift;
2036: my $src = $self->src();
2037: return ($src =~ /sequence$/);
2038: }
2039: sub is_page {
2040: my $self=shift;
2041: my $src = $self->src();
2042: return ($src =~ /page$/);
2043: }
2044:
2045:
2046:
2047:
2048: # Move this into POD: In order to use these correctly, courseopt
2049: # and useropt need to be generated
2050: sub parmval {
2051: my $self = shift;
2052: my $what = shift;
2053: my $part = shift || "0";
2054: return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
2055: }
2056:
2057: #####
2058: # Map Queries
2059: #####
2060:
2061: # These methods relate to whether or not the resource is a map, and the
2062: # attributes of that map.
2063:
2064: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
2065: sub map_finish {
2066: my $self = shift;
2067: my $src = $self->src();
2068: my $res = $self->navHash("map_finish_$src", 0);
2069: $res = $self->{NAV_MAP}->getById($res);
2070: return $res;
2071: }
2072: sub map_start {
2073: my $self = shift;
2074: my $src = $self->src();
2075: my $res = $self->navHash("map_start_$src", 0);
2076: $res = $self->{NAV_MAP}->getById($res);
2077: return $res;
2078: }
2079: sub map_pc {
2080: my $self = shift;
2081: my $src = $self->src();
2082: return $self->navHash("map_pc_$src", 0);
2083: }
2084: sub map_type {
2085: my $self = shift;
2086: my $pc = $self->map_pc();
2087: return $self->navHash("map_type_$pc", 0);
2088: }
2089:
2090:
2091:
2092: #####
2093: # Property queries
2094: #####
2095:
2096: # These functions will be responsible for returning the CORRECT
2097: # VALUE for the parameter, no matter what. So while they may look
2098: # like direct calls to parmval, they can be more then that.
2099: # So, for instance, the duedate function should use the "duedatetype"
2100: # information, rather then the resource object user.
2101:
2102: =pod
2103:
2104: =head2 Resource Parameters
2105:
2106: In order to use the resource parameters correctly, the nav map must have been instantiated with genCourseAndUserOptions set to true, so the courseopt and useropt is read correctly. Then, you can call these functions to get the relevant parameters for the resource. Each function defaults to "part 0", but can be directed to another part by passing the part as the second parameter.
2107:
2108: These methods are responsible for getting the parameter correct, not merely reflecting the contents of the GDBM hashes. As we move towards dates relative to other dates, these methods should be updated to reflect that. (Then, anybody using these methods won't have to update their code.)
2109:
2110: =over 4
2111:
2112: =item * B<acc>: Get the Client IP/Name Access Control information.
2113:
2114: =item * B<answerdate>: Get the answer-reveal date for the problem.
2115:
2116: =item * B<duedate>: Get the due date for the problem.
2117:
2118: =item * B<tries>: Get the number of tries the student has used on the problem.
2119:
2120: =item * B<maxtries>: Get the number of max tries allowed.
2121:
2122: =item * B<opendate>: Get the open date for the problem.
2123:
2124: =item * B<sig>: Get the significant figures setting.
2125:
2126: =item * B<tol>: Get the tolerance for the problem.
2127:
2128: =item * B<type>: Get the question type for the problem.
2129:
2130: =item * B<weight>: Get the weight for the problem.
2131:
2132: =back
2133:
2134: =cut
2135:
2136: sub acc {
2137: (my $self, my $part) = @_;
2138: return $self->parmval("acc", $part);
2139: }
2140: sub answerdate {
2141: (my $self, my $part) = @_;
2142: # Handle intervals
2143: if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
2144: return $self->duedate($part) +
2145: $self->parmval("answerdate", $part);
2146: }
2147: return $self->parmval("answerdate", $part);
2148: }
2149: sub duedate {
2150: (my $self, my $part) = @_;
2151: return $self->parmval("duedate", $part);
2152: }
2153: sub maxtries {
2154: (my $self, my $part) = @_;
2155: return $self->parmval("maxtries", $part);
2156: }
2157: sub opendate {
2158: (my $self, my $part) = @_;
2159: if ($self->parmval("opendate.type", $part) eq 'date_interval') {
2160: return $self->duedate($part) -
2161: $self->parmval("opendate", $part);
2162: }
2163: return $self->parmval("opendate");
2164: }
2165: sub sig {
2166: (my $self, my $part) = @_;
2167: return $self->parmval("sig", $part);
2168: }
2169: sub tol {
2170: (my $self, my $part) = @_;
2171: return $self->parmval("tol", $part);
2172: }
2173: sub type {
2174: (my $self, my $part) = @_;
2175: return $self->parmval("type", $part);
2176: }
2177: sub weight {
2178: (my $self, my $part) = @_;
2179: return $self->parmval("weight", $part);
2180: }
2181: sub tries {
2182: my $self = shift;
2183: my $part = shift;
2184: $part = '0' if (!defined($part));
2185:
2186: # Make sure return hash is loaded, should error check
2187: $self->getReturnHash();
2188:
2189: my $tries = $self->{RETURN_HASH}->{'resource.'.$part.'.tries'};
2190: if (!defined($tries)) {return '0';}
2191: return $tries;
2192: }
2193:
2194: # Multiple things need this
2195: sub getReturnHash {
2196: my $self = shift;
2197:
2198: if (!defined($self->{RETURN_HASH})) {
2199: my %tmpHash = &Apache::lonnet::restore($self->symb());
2200: $self->{RETURN_HASH} = \%tmpHash;
2201: }
2202: }
2203:
2204: ######
2205: # Status queries
2206: ######
2207:
2208: # These methods query the status of problems.
2209:
2210: # If we need to count parts, this function determines the number of
2211: # parts from the metadata. When called, it returns a reference to a list
2212: # of strings corresponding to the parts. (Thus, using it in a scalar context
2213: # tells you how many parts you have in the problem:
2214: # $partcount = scalar($resource->countParts());
2215: # Don't use $self->{PARTS} directly because you don't know if it's been
2216: # computed yet.
2217:
2218: =pod
2219:
2220: =head2 Resource misc
2221:
2222: Misc. functions for the resource.
2223:
2224: =over 4
2225:
1.59 bowersj2 2226: =item * B<hasDiscussion>: Returns a false value if there has been discussion since the user last logged in, true if there has. Always returns false if the discussion data was not extracted when the nav map was constructed.
2227:
2228: =item * B<getFeedback>: Gets the feedback for the resource and returns the raw feedback string for the resource, or the null string if there is no feedback or the email data was not extracted when the nav map was constructed. Usually used like this:
2229:
2230: for (split(/\,/, $res->getFeedback())) {
2231: my $link = &Apache::lonnet::escape($_);
2232: ...
2233:
2234: and use the link as appropriate.
2235:
2236: =cut
2237:
2238: sub hasDiscussion {
2239: my $self = shift;
2240: return $self->{NAV_MAP}->hasDiscussion($self->symb());
2241: }
2242:
2243: sub getFeedback {
2244: my $self = shift;
2245: return $self->{NAV_MAP}->getFeedback($self->symb());
2246: }
2247:
2248: =pod
2249:
1.51 bowersj2 2250: =item * B<parts>(): Returns a list reference containing sorted strings corresponding to each part of the problem. To count the number of parts, use the list in a scalar context, and subtract one if greater then two. (One part problems have a part 0. Multi-parts have a part 0, plus a part for each part. You may or may not wish to filter out part 0.)
2251:
2252: =item * B<countParts>(): Returns the number of parts of the problem a student can answer. Thus, for single part problems, returns 1. For multipart, it returns the number of parts in the problem, not including psuedo-part 0.
2253:
2254: =back
2255:
2256: =cut
2257:
2258: sub parts {
2259: my $self = shift;
2260:
2261: $self->extractParts();
2262: return $self->{PARTS};
2263: }
2264:
2265: sub countParts {
2266: my $self = shift;
2267:
2268: my $parts = $self->parts();
2269:
2270: if (scalar(@{$parts}) == 1) { return 1;}
2271:
2272: return scalar(@{$parts}) - 1;
2273: }
2274:
2275: # Private function: Extracts the parts information and saves it
2276: sub extractParts {
2277: my $self = shift;
2278:
2279: return if ($self->{PARTS});
2280:
2281: $self->{PARTS} = [];
2282:
2283: # Retrieve part count
2284: foreach (split(/\,/,&Apache::lonnet::metadata($self->src(),
2285: 'allpossiblekeys'))) {
2286: if ($_ =~ /^parameter\_(.*)\_opendate$/) {
2287: push @{$self->{PARTS}}, $1;
2288: }
2289: }
2290:
2291: # Is this possible to do in one line? - Jeremy
2292: my @sortedParts = sort @{$self->{PARTS}};
2293: $self->{PARTS} = \@sortedParts;
2294:
2295: return;
2296: }
2297:
2298: =pod
2299:
2300: =head2 Resource Status
2301:
2302: Problem resources have status information, reflecting their various dates and completion statuses. You can obtain this information and import symbolic constants to represent the status.
2303:
2304: There are two aspects to the status: the date-related information and the completion information.
2305:
2306: Idiomatic usage of these two methods would probably look something like
2307:
2308: foreach ($resource->parts()) {
2309: my $dateStatus = $resource->getDateStatus($_);
2310: my $completionStatus = $resource->getCompletionStatus($_);
2311:
2312: ... use it here ...
2313: }
2314:
2315: =over 4
2316:
1.59 bowersj2 2317: =item * B<getDateStatus>($part): ($part defaults to 0). A convenience function that returns a symbolic constant telling you about the date status of the part. You should still check to see if there is a due date at all, if you care about that. The possible return values are:
1.51 bowersj2 2318:
2319: =back
2320:
2321: B<Date Codes>
2322:
2323: =over 4
2324:
2325: =item * B<OPEN_LATER>: The problem will be opened later.
2326:
2327: =item * B<OPEN>: Open and not yet due.
2328:
1.59 bowersj2 2329:
1.54 bowersj2 2330: =item * B<PAST_DUE_ANSWER_LATER>: The due date has passed, but the answer date has not yet arrived.
2331:
2332: =item * B<PAST_DUE_NO_ANSWER>: The due date has passed and there is no answer opening date set.
1.51 bowersj2 2333:
2334: =item * B<ANSWER_OPEN>: The answer date is here.
2335:
2336: =item * B<NETWORK_FAILURE>: The information is unknown due to network failure.
2337:
2338: =back
2339:
2340: =cut
2341:
2342: # Apparently the compiler optimizes these into constants automatically
1.54 bowersj2 2343: sub OPEN_LATER { return 0; }
2344: sub OPEN { return 1; }
2345: sub PAST_DUE_NO_ANSWER { return 2; }
2346: sub PAST_DUE_ANSWER_LATER { return 3; }
2347: sub ANSWER_OPEN { return 4; }
2348: sub NOTHING_SET { return 5; }
2349: sub NETWORK_FAILURE { return 100; }
2350:
2351: # getDateStatus gets the date status for a given problem part.
2352: # Because answer date, due date, and open date are fully independent
2353: # (i.e., it is perfectly possible to *only* have an answer date),
2354: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
2355: # (past, future, none given). This function handles this with a decision
2356: # tree. Read the comments to follow the decision tree.
1.51 bowersj2 2357:
2358: sub getDateStatus {
2359: my $self = shift;
2360: my $part = shift;
2361: $part = "0" if (!defined($part));
1.54 bowersj2 2362:
2363: # Always return network failure if there was one.
1.51 bowersj2 2364: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
2365:
2366: my $now = time();
2367:
1.53 bowersj2 2368: my $open = $self->opendate($part);
2369: my $due = $self->duedate($part);
2370: my $answer = $self->answerdate($part);
2371:
2372: if (!$open && !$due && !$answer) {
2373: # no data on the problem at all
2374: # should this be the same as "open later"? think multipart.
2375: return $self->NOTHING_SET;
2376: }
1.59 bowersj2 2377: if (!$open || $now < $open) {return $self->OPEN_LATER}
2378: if (!$due || $now < $due) {return $self->OPEN}
2379: if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
2380: if ($answer) { return $self->ANSWER_OPEN; }
1.54 bowersj2 2381: return PAST_DUE_NO_ANSWER;
1.51 bowersj2 2382: }
2383:
2384: =pod
2385:
2386: B<>
2387:
2388: =over 4
2389:
2390: =item * B<getCompletionStatus>($part): ($part defaults to 0.) A convenience function that returns a symbolic constant telling you about the completion status of the part, with the following possible results:
2391:
2392: =back
2393:
2394: B<Completion Codes>
2395:
2396: =over 4
2397:
2398: =item * B<NOT_ATTEMPTED>: Has not been attempted at all.
2399:
2400: =item * B<INCORRECT>: Attempted, but wrong by student.
2401:
2402: =item * B<INCORRECT_BY_OVERRIDE>: Attempted, but wrong by instructor override.
2403:
2404: =item * B<CORRECT>: Correct or correct by instructor.
2405:
2406: =item * B<CORRECT_BY_OVERRIDE>: Correct by instructor override.
2407:
2408: =item * B<EXCUSED>: Excused. Not yet implemented.
2409:
2410: =item * B<NETWORK_FAILURE>: Information not available due to network failure.
2411:
2412: =back
2413:
2414: =cut
2415:
1.53 bowersj2 2416: sub NOT_ATTEMPTED { return 10; }
2417: sub INCORRECT { return 11; }
2418: sub INCORRECT_BY_OVERRIDE { return 12; }
2419: sub CORRECT { return 13; }
2420: sub CORRECT_BY_OVERRIDE { return 14; }
2421: sub EXCUSED { return 15; }
1.51 bowersj2 2422:
2423: sub getCompletionStatus {
2424: my $self = shift;
2425: my $part = shift;
2426: $part = "0" if (!defined($part));
2427: return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
2428:
2429: # Make sure return hash exists
2430: $self->getReturnHash();
2431:
2432: my $status = $self->{RETURN_HASH}->{'resource.'.$part.'.solved'};
2433:
2434: # Left as seperate if statements in case we ever do more with this
2435: if ($status eq 'correct_by_student') {return $self->CORRECT;}
2436: if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
2437: if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
2438: if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
2439: if ($status eq 'excused') {return $self->EXCUSED; }
2440: return $self->NOT_ATTEMPTED;
2441: }
2442:
2443: =pod
2444:
2445: B<Composite Status>
2446:
2447: Along with directly returning the date or completion status, the resource object includes a convenience function B<status>() that will combine the two status tidbits into one composite status that can represent the status of the resource as a whole. The precise logic is documented in the comments of the status method. The following results may be returned, all available as methods on the resource object ($res->NETWORK_FAILURE()):
2448:
2449: =over 4
2450:
2451: =item * NETWORK_FAILURE: The network has failed and the information is not available.
2452:
1.59 bowersj2 2453: =item * NOTHING_SET: No dates have been set for this problem (part) at all. (Because only certain parts of a multi-part problem may be assigned, this can not be collapsed into "open later", as we don't know a given part will EVER be opened. For single part, this is the same as "OPEN_LATER".)
1.53 bowersj2 2454:
1.51 bowersj2 2455: =item * CORRECT: For any reason at all, the part is considered correct.
2456:
2457: =item * EXCUSED: For any reason at all, the problem is excused.
2458:
1.54 bowersj2 2459: =item * PAST_DUE_NO_ANSWER: The problem is past due, not considered correct, and no answer date is set.
2460:
2461: =item * PAST_DUE_ANSWER_LATER: The problem is past due, not considered correct, and an answer date in the future is set.
1.51 bowersj2 2462:
2463: =item * ANSWER_OPEN: The problem is past due, not correct, and the answer is now available.
2464:
2465: =item * OPEN_LATER: The problem is not yet open.
2466:
2467: =item * TRIES_LEFT: The problem is open, has been tried, is not correct, but there are tries left.
2468:
2469: =item * INCORRECT: The problem is open, and all tries have been used without getting the correct answer.
2470:
2471: =item * OPEN: The item is open and not yet tried.
2472:
2473: =back
2474:
2475: =cut
2476:
2477: sub TRIES_LEFT { return 10; }
2478:
2479: sub status {
2480: my $self = shift;
2481: my $part = shift;
2482: if (!defined($part)) { $part = "0"; }
2483: my $completionStatus = $self->getCompletionStatus($part);
2484: my $dateStatus = $self->getDateStatus($part);
2485:
2486: # What we have is a two-dimensional matrix with 4 entries on one
2487: # dimension and 5 entries on the other, which we want to colorize,
1.54 bowersj2 2488: # plus network failure and "no date data at all".
1.51 bowersj2 2489:
1.53 bowersj2 2490: if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51 bowersj2 2491:
2492: # There are a few whole rows we can dispose of:
1.53 bowersj2 2493: if ($completionStatus == CORRECT ||
2494: $completionStatus == CORRECT_BY_OVERRIDE ) {
2495: return CORRECT();
2496: }
2497:
2498: # If it's EXCUSED, then return that no matter what
2499: if ($completionStatus == EXCUSED) {
2500: return EXCUSED;
1.51 bowersj2 2501: }
2502:
1.53 bowersj2 2503: if ($dateStatus == NOTHING_SET) {
2504: return NOTHING_SET;
1.51 bowersj2 2505: }
2506:
2507: # Now we're down to a 3 (incorrect, incorrect_override, not_attempted)
2508: # by 4 matrix (date status).
2509:
1.54 bowersj2 2510: if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59 bowersj2 2511: $dateStatus == PAST_DUE_NO_ANSWER ) {
1.54 bowersj2 2512: return $dateStatus;
1.51 bowersj2 2513: }
2514:
1.53 bowersj2 2515: if ($dateStatus == ANSWER_OPEN) {
2516: return ANSWER_OPEN;
1.51 bowersj2 2517: }
2518:
2519: # Now: (incorrect, incorrect_override, not_attempted) x
2520: # (open_later), (open)
2521:
1.53 bowersj2 2522: if ($dateStatus == OPEN_LATER) {
2523: return OPEN_LATER;
1.51 bowersj2 2524: }
2525:
2526: # If it's WRONG...
1.53 bowersj2 2527: if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51 bowersj2 2528: # and there are TRIES LEFT:
2529: if ($self->tries() < $self->maxtries()) {
1.53 bowersj2 2530: return TRIES_LEFT;
1.51 bowersj2 2531: }
1.53 bowersj2 2532: return INCORRECT; # otherwise, return orange; student can't fix this
1.51 bowersj2 2533: }
2534:
2535: # Otherwise, it's untried and open
1.53 bowersj2 2536: return OPEN;
1.51 bowersj2 2537: }
2538:
2539: =pod
2540:
2541: =head2 Resource/Nav Map Navigation
2542:
2543: =over 4
2544:
2545: =item * B<getNext>(): Gets the next resource in the navmap after this one.
2546:
2547: =cut
2548:
2549: # For the simple single-link case, to get from a resource to the next
2550: # resource, you need to look up the "to_" link in the nav hash, then
2551: # follow that with the "goesto_" link.
2552:
2553: sub getNext {
2554: my $self = shift;
2555: my $alreadySeenHash = shift;
2556: my @branches;
2557: my $to = $self->to();
2558: foreach my $branch ( split(/\,/, $to) ) {
2559: my $choice = $self->{NAV_MAP}->getById($branch);
2560: my $next = $choice->goesto();
2561: $next = $self->{NAV_MAP}->getById($next);
2562:
1.61 bowersj2 2563: # Don't remember it if we've already seen it or if
2564: # the student doesn't have browse priviledges
2565: my $browsePriv = &Apache::lonnet::allowed('bre', $self->src);
1.51 bowersj2 2566: if (!defined($alreadySeenHash) ||
1.61 bowersj2 2567: !defined($alreadySeenHash->{$next->{ID}}) ||
2568: ($browsePriv ne '2' && $browsePriv ne 'F')) {
1.51 bowersj2 2569: push @branches, $next;
2570: }
2571: }
2572: return \@branches;
2573: }
2574:
2575: =pod
1.2 www 2576:
1.51 bowersj2 2577: =back
1.2 www 2578:
1.51 bowersj2 2579: =cut
1.2 www 2580:
1.51 bowersj2 2581: 1;
1.2 www 2582:
1.51 bowersj2 2583: __END__
1.2 www 2584:
2585:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>