File:  [LON-CAPA] / loncom / interface / Attic / lonchart.pm
Revision 1.41: download - view: text, annotated - select for diffs
Mon Jun 3 19:28:17 2002 UTC (22 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- reworked some of the cleanup wok fro BUG#480
- should find a section for any real students (usection rework)
- properly handles the results from the name get call

    1: # The LearningOnline Network with CAPA
    2: # (Publication Handler
    3: #
    4: # $Id: lonchart.pm,v 1.41 2002/06/03 19:28:17 albertel Exp $
    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: #
   28: # Homework Performance Chart
   29: #
   30: # (Navigate Maps Handler
   31: #
   32: # (Page Handler
   33: #
   34: # (TeX Content Handler
   35: # YEAR=2000
   36: # 05/29/00,05/30 Gerd Kortemeyer)
   37: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
   38: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16 Gerd Kortemeyer)
   39: # YEAR=2001
   40: # 3/1/1,6/1,17/1,29/1,30/1,31/1 Gerd Kortemeyer)
   41: # 7/10/01 Behrouz Minaei
   42: # 9/8 Gerd Kortemeyer
   43: # 10/1, 10/19, 11/17, 11/22, 11/24, 11/28 12/18 Behrouz Minaei
   44: # YEAR=2002
   45: # 2/1, 2/6, 2/19, 2/28 Behrouz Minaei
   46: #
   47: ###
   48: 
   49: package Apache::lonchart;
   50: 
   51: use strict;
   52: use Apache::Constants qw(:common :http);
   53: use Apache::lonnet();
   54: use Apache::loncommon();
   55: use HTML::TokeParser;
   56: use GDBM_File;
   57: 
   58: # -------------------------------------------------------------- Module Globals
   59: my %hash;
   60: my %CachData;
   61: my @cols;
   62: my @rowlabels;
   63: my @students;
   64: my @PreCol;
   65: my $r;
   66:  
   67: # ------------------------------------------------------------- Find out status
   68: 
   69: sub ExtractStudentData {
   70:     my ($index,$coid)=@_;
   71:     my ($sname,$sdom) = split( /\:/, $students[$index] );
   72:     my %result=&Apache::lonnet::dump($coid,$sdom,$sname);
   73:     my $ResId;
   74:     my $Code;
   75:     my $Tries;
   76:     my $Wrongs;
   77:     my %TempHash;
   78:     my $Version;
   79:     my $ProbNo;
   80:     my $ProbSolved;
   81:     my $ProbTotal;
   82:     my $LatestVersion;                     
   83:     my $Str=substr($students[$index].
   84:             '                                                        ',0,14).' ! '.
   85:             substr($rowlabels[$index].
   86:             '                                                        ',0,45).' ! ';
   87: 
   88:     my($checkForError)=keys (%result);
   89:     if($checkForError =~ /^(con_lost|error|no_such_host)/i) {
   90: 	my $PrTot = sprintf( "%5d", $ProbTotal );
   91: 	my $PrSvd = sprintf( "%5d", $ProbSolved );
   92: 	$Str .= ' '.'<font color="#000088">'.$PrSvd.'  /'.$PrTot.'</font> ';
   93: 	return $Str;
   94:     }
   95: 
   96:     $ProbNo = 0;
   97:     $ProbTotal = 0;
   98:     $ProbSolved = 0;
   99:     my $IterationNo = 0;
  100:     foreach $ResId (@cols) {
  101: 	if ($IterationNo == 0) {$IterationNo++; next;}
  102: 	if (!$ResId) { 
  103: 	    my $PrNo = sprintf( "%3d", $ProbNo );
  104: 	    $Str .= ' '.'<font color="#007700">'.$PrNo.'</font> ';
  105: 	    $ProbSolved += $ProbNo;
  106: 	    $ProbNo=0;
  107: 	    next; 
  108: 	}
  109: 	$ResId=~/(\d+)\.(\d+)/;
  110: 	my $meta=$hash{'src_'.$ResId};
  111: 	my $PartNo = 0;
  112: 	undef %TempHash;
  113: 	foreach (split(/\,/,&Apache::lonnet::metadata($meta,'keys'))) {
  114: 	    if ($_=~/^stores\_(\d+)\_tries$/) {
  115: 		my $Part=&Apache::lonnet::metadata($meta,$_.'.part');
  116: 		if ( $TempHash{"$Part"} eq '' ) { 
  117: 		    $TempHash{"$Part"} = $Part;
  118: 		    $TempHash{$PartNo}=$Part;
  119: 		    $TempHash{"$Part.Code"} = ' ';  
  120: 		    $PartNo++;
  121: 		}
  122: 	    }
  123: 	}
  124: 
  125: 	my $Prob = &Apache::lonnet::symbclean(
  126: 		       &Apache::lonnet::declutter($hash{'map_id_'.$1} ).
  127:                        '___'.$2.'___'.
  128:                        &Apache::lonnet::declutter( $hash{'src_'.$ResId} ));
  129: 	$Code=' ';
  130: 	$Tries = 0;
  131: 	$LatestVersion = $result{"version:$Prob"};
  132: 
  133: 	if ( $LatestVersion ) {
  134: 	    for ( my $Version=1; $Version<=$LatestVersion; $Version++ ) {
  135: 		my $vkeys = $result{"$Version:keys:$Prob"};
  136: 		my @keys = split(/\:/,$vkeys);		  
  137: 
  138: 		foreach my $Key (@keys) {		  
  139: 		    if (($Key=~/\.(\w+)\.solved$/) && ($Key!~/^\d+\:/)) {
  140: 			my $Part = $1;
  141: 			$Tries = $result{"$Version:$Prob:resource.$Part.tries"};
  142: 			$TempHash{"$Part.Tries"}=($Tries) ? $Tries : 0;
  143: 			my $Val = $result{"$Version:$Prob:resource.$Part.solved"};
  144: 			if ($Val eq 'correct_by_student'){$Code='*';} 
  145: 			elsif ($Val eq 'correct_by_override'){$Code = '+';}
  146: 			elsif ($Val eq 'incorrect_attempted'){$Code = '.';} 
  147: 			elsif ($Val eq 'incorrect_by_override'){$Code = '-';}
  148: 			elsif ($Val eq 'excused'){$Code = 'x';}
  149: 			elsif ($Val eq 'ungraded_attempted'){$Code = '#';}
  150: 			else {$Code = ' ';}
  151: 
  152: 			$TempHash{"$Part.Code"} = $Code;
  153: 		    }
  154: 		}
  155: 	    }
  156: # Actually append problem to output (all parts)
  157: 	    $Str.='<a href="/adm/grades?symb='.
  158:                 &Apache::lonnet::escape($Prob).
  159:                 '&student='.$sname.'&domain='.$sdom.'&command=submission">'; 
  160: 	    for ( my $n = 0; $n < $PartNo; $n++ ) {		  
  161: 		my $part = $TempHash{$n};
  162: 		my $Code = $TempHash{"$part.Code"};
  163: 		if ( $Code eq '*') {
  164: 		    $ProbNo++;
  165: 		    if (($TempHash{"$part.Tries"}<10) ||
  166: 			($TempHash{"$part.Tries"} eq '')) {
  167: 			$TempHash{"$part.Code"}=$TempHash{"$part.Tries"};
  168: 		    }
  169: 		}
  170: 		elsif ( $Code eq '+' ) {$ProbNo++;}
  171: 		$Str .= $TempHash{"$part.Code"};
  172: 		if ( $Code ne 'x' ) {$ProbTotal++;}
  173: 	    }
  174: 	    $Str.='</a>';
  175: 	} else {
  176: 	    for(my $n=0; $n<$PartNo; $n++) {
  177: 		$Str.=' ';
  178: 		$ProbTotal++;
  179: 	    }
  180: 	}
  181:     }
  182: 
  183:     my $PrTot = sprintf( "%5d", $ProbTotal );
  184:     my $PrSvd = sprintf( "%5d", $ProbSolved );
  185:     $Str .= ' '.'<font color="#000088">'.$PrSvd.'  /'.$PrTot.'</font> ';
  186: 
  187:     return $Str ;
  188: }
  189: 
  190: 
  191: # ------------------------------------------------------------ Build page table
  192: 
  193: sub tracetable {
  194:     my ($rid,$beenhere)=@_;
  195:     unless ($beenhere=~/\&$rid\&/) {
  196:        $beenhere.=$rid.'&';  
  197: # new ... updating the map according to sequence and page
  198:        if (defined($hash{'is_map_'.$rid})) {
  199: 	   my $cmap=$hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}};
  200:            if ( $cmap eq 'sequence' || $cmap eq 'page' ) { 
  201:                $cols[$#cols+1]=0; 
  202:            }
  203:            if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
  204:                (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
  205:               my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
  206: 
  207:                 &tracetable($hash{'map_start_'.$hash{'src_'.$rid}},
  208:                 '&'.$frid.'&');
  209: 
  210:               if ($hash{'src_'.$frid}) {
  211:                  if ($hash{'src_'.$frid}=~
  212:                                  /\.(problem|exam|quiz|assess|survey|form)$/) {
  213: 		     $cols[$#cols+1]=$frid;
  214:                  }
  215: 	      }
  216: 
  217: 	   }
  218:        } else {
  219:           if ($hash{'src_'.$rid}) {
  220:              if ($hash{'src_'.$rid}=~
  221:                                  /\.(problem|exam|quiz|assess|survey|form)$/) {
  222: 	         $cols[$#cols+1]=$rid;
  223:              }
  224:           }
  225:        }
  226:        if (defined($hash{'to_'.$rid})) {
  227:           foreach (split(/\,/,$hash{'to_'.$rid})){
  228:               &tracetable($hash{'goesto_'.$_},$beenhere);
  229:           }
  230:        }
  231:     }
  232: }
  233: 
  234: sub usection {
  235:     my ($udom,$unam,$courseid,$ActiveFlag)=@_;
  236:     $courseid=~s/\_/\//g;
  237:     $courseid=~s/^(\w)/\/$1/;
  238: 
  239:     my %result=&Apache::lonnet::dump('roles',$udom,$unam);
  240: 
  241:     my($checkForError)=keys (%result);
  242:     if($checkForError =~ /^(con_lost|error|no_such_host)/i) {
  243: 	return -1;
  244:     }
  245:     my $cursection='-1';
  246:     my $oldsection='-1';
  247:     my $status='Expired';
  248:     foreach my $key (keys (%result)) {
  249: 	my $value = $result{$key};
  250:         if ($key=~/^$courseid(?:\/)*(\w+)*\_st$/) {
  251:             my $section=$1;
  252:             if ($key eq $courseid.'_st') { $section=''; }
  253: 	    my ($dummy,$end,$start)=split(/\_/,$value);
  254: 	    my $now=time;
  255: 	    my $notactive=0;
  256: 	    if ($start) { if ($now<$start) { $notactive=1; } }
  257: 	    if ($end) { if ($now>$end) { $notactive=1; } }
  258: 	    if ($notactive == 0) { $status='Active';$cursection=$section;}
  259: 	    if ($notactive == 1) { $oldsection=$section;}
  260:         }
  261:     }
  262:     if ($status eq $ActiveFlag) {
  263:       if ($cursection eq '-1') { return $oldsection; }
  264:       return $cursection;
  265:     }
  266:     if ($ActiveFlag eq 'Any') { 
  267:       if ($cursection eq '-1') { return $oldsection; }
  268:       return $cursection;
  269:     }
  270:     return '-1';
  271: }
  272: 
  273: sub BuildChart {
  274: # ----------------------- Get first and last resource, see if there is anything
  275:     my $firstres=$hash{'map_start_/res/'.$ENV{'request.course.uri'}};
  276:     my $lastres=$hash{'map_finish_/res/'.$ENV{'request.course.uri'}};
  277:     if (($firstres) && ($lastres)) {
  278: # ----------------------------------------------------------------- Render page
  279: 	my $cid=$ENV{'request.course.id'};
  280:         my $chome=$ENV{'course.'.$cid.'.home'};
  281:         my ($cdom,$cnum)=split(/\_/,$cid);
  282: # ---------------------------------------------- Read class list and row labels
  283: 	my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
  284: 
  285: 	my($checkForError)=keys (%classlist);
  286: 	if($checkForError =~ /^(con_lost|error|no_such_host)/i) {
  287: 	    $r->print('<h1>Could not access course data</h1>');
  288: 	} else {
  289: 	    my $now=time;
  290: 	    foreach my $name (sort(keys(%classlist))) {
  291: 		my $value=$classlist{$name};
  292: 		my ($end,$start)=split(/\:/,$value);
  293: 		my $active=1;
  294: 		my $Status=$ENV{'form.status'};
  295: 		$Status = ($Status) ? $Status : 'Active';
  296: 		if ( ( ($end) && $now > $end ) && 
  297: 		     ( ($Status eq 'Active') ) ) { $active=0; }
  298: 		if ( ($Status eq 'Expired') && 
  299: 		     ($end == 0 || $now < $end) ) { $active=0; }
  300: 		if ($active) {
  301: 		    my $thisindex=$#students+1;
  302: 		    $students[$thisindex]=$name;
  303: 		    my ($sname,$sdom)=split(/\:/,$name);
  304: 		    $PreCol[$thisindex]=$sname.':';
  305: 		    my $ssec=&usection($sdom,$sname,$cid,$Status);
  306: 		    if ($ssec==-1) {
  307: 			$rowlabels[$thisindex]=
  308: 			    'Data not available: '.$name;
  309: 		    } else {
  310: 			my %reply=&Apache::lonnet::idrget($sdom,$sname);
  311: 			my %name=&Apache::lonnet::get('environment',
  312: 						       ['lastname','generation'
  313: 						       ,'firstname'
  314: 						       ,'middlename'],
  315: 						       $sdom,$sname);
  316: 			my $name=$name{lastname};
  317: 			if ($name{generation}) {$name.=" $name{generation}";}
  318: 			$name.=',';
  319: 			if ($name{firstname}) {$name.=" $name{firstname}";}
  320: 			if ($name{middlename}) {$name.=" $name{middlename}";}
  321: 			if ($name eq ',') {$name='';}
  322: 			#$ssec=(int($ssec)) ? int($ssec) : $ssec;
  323: 			my $sec=sprintf('%3s',$ssec);
  324: 			$rowlabels[$thisindex]=$sec.' '.$reply{$sname}.' ';
  325: 			$PreCol[$thisindex] .= $name.':'.$sec;
  326: 			$rowlabels[$thisindex].=$name.' ';
  327: 		    }
  328: 		}
  329: 	    }
  330: 	}
  331: 
  332: 	my $allstudents=$#students+1;
  333: 	$r->print('<h3>'.$allstudents.' students</h3>');
  334: 	&CreateForm();
  335: 	$r->rflush();
  336: 
  337: # --------------- Find all assessments and put them into some linear-like order
  338: 	&tracetable($firstres,'&'.$lastres.'&');
  339: # ----------------------------------------------------------------- Start table
  340: 
  341:         $r->print('<p><pre>');
  342:  	my $index;
  343:         for ($index=0;$index<=$#students;$index++) {
  344: 	    my $Str=&ExtractStudentData($index,$cid);
  345: 	    $r->print($Str.'<br>');
  346:             $r->rflush();
  347: 	    $CachData{$PreCol[$index]}=$Str;
  348:         }
  349:         $r->print('</pre>');
  350:     } else {
  351: 	$r->print('<h3>Undefined course sequence</h3>');
  352:     }
  353: }
  354: 
  355: sub CreateForm {
  356:     my $OpSel1='';
  357:     my $OpSel2='';
  358:     my $OpSel3='';
  359:     my $Status = $ENV{'form.status'};
  360:     if ( $Status eq 'Any' ) { $OpSel3='selected'; }
  361:     elsif ($Status eq 'Expired' ) { $OpSel2 = 'selected'; }
  362:     else { $OpSel1 = 'selected'; }
  363: 
  364:     my $Ptr = '<form name=stat method=post action="/adm/chart" >'."\n";
  365:     $Ptr .= '<b> Sort by: &nbsp; </b>'."\n";
  366:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
  367:     $Ptr .= '<input type=submit name=sort value="User Name" />'."\n";
  368:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
  369:     $Ptr .= '<input type=submit name=sort value="Last Name" />'."\n";
  370:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
  371:     $Ptr .= '<input type=submit name=sort value="Section"/>'."\n";
  372:     $Ptr .= '<br><br>';
  373:     $Ptr .= '<b> Student Status: &nbsp; </b>'."\n".
  374:             '<select name="status">'. 
  375:             '<option '.$OpSel1.' >Active</option>'."\n".
  376:             '<option '.$OpSel2.' >Expired</option>'."\n".
  377: 	    '<option '.$OpSel3.' >Any</option> </select> '."\n";
  378:     $Ptr .= '&nbsp;&nbsp;&nbsp;';
  379:     $Ptr .= '<input type=submit name=sort value="Recalculate Chart"/>'."\n";
  380:     $Ptr .= '</form>'."\n";
  381:     $r->print( $Ptr );
  382: }
  383: 
  384: sub CacheChart {
  385:     my %list = ();
  386:     my $count=0;
  387: 
  388:     my $Pos = $ENV{'form.sort'};
  389:     if ( $Pos eq 'Last Name' ) {$Pos=1;}
  390:     elsif ( $Pos eq 'Section' ) {$Pos=2;}
  391:     else {$Pos=0;}
  392: 
  393:     foreach my $key( keys %CachData) { 
  394: 	my @Temp=split(/\:/,$key);
  395: 	my $Use = $Temp[$Pos];
  396: 	$list{$Use.$key}=$key;
  397: 	$count++;
  398:     }
  399: 
  400:     my @order = sort(keys(%list));
  401: 
  402:     $r->print('<h3>'.$count.' students</h3>');
  403:     &CreateForm();
  404:     $r->rflush();
  405:     
  406:     $r->print('<p><pre>');
  407:     for ( my $n; $n < $count; $n++) {
  408: 	$r->print($CachData{$list{$order[$n]}}.'<br>');
  409:     }	
  410:     $r->print('</pre>');
  411: }
  412: 
  413: sub Start {
  414:     undef %hash;
  415:     undef %CachData;
  416:     undef @students;
  417:     undef @cols;
  418:     undef @rowlabels;
  419:     undef @PreCol;
  420: 
  421:     $r->print('<html><head><title>'.
  422:               'LON-CAPA Assessment Chart</title></head>');
  423:     $r->print('<body bgcolor="#FFFFFF">'.
  424:               '<script>window.focus();</script>'.
  425:               '<img align=right src=/adm/lonIcons/lonlogos.gif>'.
  426:               '<h1>Assessment Chart</h1>');
  427: # ---------------------------------------------------------------- Course title
  428:     $r->print('<h1>'.$ENV{'course.'.$ENV{'request.course.id'}.
  429:               '.description'}.'</h1><h3>'.localtime().
  430:               "</h3><p><pre>1..9: correct by student in 1..9 tries\n".
  431:               "   *: correct by student in more than 9 tries\n".
  432: 	      "   +: correct by override\n".
  433:               "   -: incorrect by override\n".
  434: 	      "   .: incorrect attempted\n".
  435: 	      "   #: ungraded attempted\n".
  436:               "    : not attempted\n".
  437: 	      "   x: excused</pre><p>"); 
  438: # ------------------------------- This is going to take a while, produce output
  439:     $r->rflush();
  440: 
  441:     my $cid=$ENV{'request.course.id'};
  442:     my $ChartDB = "/home/httpd/perl/tmp/$ENV{'user.name'}".
  443:                   "_$ENV{'user.domain'}_$cid\_chart.db";
  444: 
  445:     if ((-e "$ChartDB") && ($ENV{'form.sort'} ne 'Recalculate Chart')) {
  446: 	if (tie(%CachData,'GDBM_File',"$ChartDB",&GDBM_READER,0640)) {
  447: 	    &CacheChart();
  448: 	}
  449: 	else {
  450: 	    $r->print("Unable to tie hash to db file");
  451: 	}
  452:     }
  453:     else {
  454: 	if (tie(%CachData,'GDBM_File',$ChartDB,&GDBM_WRCREAT,0640)) {
  455: 	    foreach (keys %CachData) {delete $CachData{$_};}
  456: 	    &BuildChart();
  457: 	}
  458: 	else {
  459: 	    $r->print("Unable to tie hash to db file");
  460: 	}
  461:     }
  462:     untie(%CachData);
  463: }
  464: 
  465: # ================================================================ Main Handler
  466: 
  467: sub handler {
  468:     $r=shift;
  469:     if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
  470: # ------------------------------------------- Set document type for header only
  471: 	if ($r->header_only) {
  472: 	    if ($ENV{'browser.mathml'}) {
  473: 		$r->content_type('text/xml');
  474: 	    } else {
  475: 		$r->content_type('text/html');
  476: 	    }
  477: 	    &Apache::loncommon::no_cache($r);
  478: 	    $r->send_http_header;
  479: 	    return OK;
  480: 	}
  481: 
  482: 	my $requrl=$r->uri;
  483: # ----------------------------------------------------------------- Tie db file
  484: 	if ($ENV{'request.course.fn'}) {
  485: 	    my $fn=$ENV{'request.course.fn'};
  486: 	    if (-e "$fn.db") {
  487: 		if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
  488: # ------------------------------------------------------------------- Hash tied
  489: # ---------------------------------------------------------------- Send headers
  490: 		    $r->content_type('text/html');
  491: 		    $r->send_http_header;
  492: 		    &Start();
  493: 		    $r->print('</body></html>');                 
  494: # ------------------------------------------------------------- End render page
  495: 		} else {
  496: 		    $r->content_type('text/html');
  497: 		    $r->send_http_header;
  498: 		    $r->print('<html><body>Coursemap undefined.</body></html>');
  499: 		}
  500: # ------------------------------------------------------------------ Untie hash
  501: 		unless (untie(%hash)) {
  502: 		    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  503: 			     "Could not untie coursemap $fn (browse).</font>"); 
  504: 		}
  505: 
  506: # -------------------------------------------------------------------- All done
  507: 		return OK;
  508: # ----------------------------------------------- Errors, hash could no be tied
  509: 	    }
  510: 	} else {
  511: 	    $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  512: 	    return HTTP_NOT_ACCEPTABLE; 
  513: 	}
  514:     } else {
  515: 	$ENV{'user.error.msg'}=
  516:         $r->uri.":vgr:0:0:Cannot view grades for complete course";
  517: 	return HTTP_NOT_ACCEPTABLE; 
  518:     }
  519: }
  520: 1;
  521: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>