--- loncom/interface/Attic/lonspreadsheet.pm 2000/12/11 15:33:58 1.11 +++ loncom/interface/Attic/lonspreadsheet.pm 2000/12/18 17:14:49 1.18 @@ -1,7 +1,8 @@ # The LearningOnline Network with CAPA # Spreadsheet/Grades Display Handler # -# 11/11,11/15,11/27,12/04,12/05,12/06,12/07,12/08,12/09,12/11 Gerd Kortemeyer +# 11/11,11/15,11/27,12/04,12/05,12/06,12/07, +# 12/08,12/09,12/11,12/12,12/15,12/16,12/18 Gerd Kortemeyer package Apache::lonspreadsheet; @@ -259,9 +260,15 @@ sub SUMMIN { sub sett { %t=(); + my $pattern=''; + if ($sheettype eq 'assesscalc') { + $pattern='A'; + } else { + $pattern='[A-Z]'; + } map { if ($f{$_}) { - if ($_=~/^A/) { + if ($_=~/^$pattern/) { unless ($f{$_}=~/^\!/) { $t{$_}=$c{$_}; } @@ -272,6 +279,9 @@ sub sett { } } } keys %f; + $t{'A0'}=$f{'A0'}; + $t{'A0'}=~s/\.\.+/\,/g; + $t{'A0'}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$v\{\'$2\'\}/g; } sub calc { @@ -299,7 +309,7 @@ sub calc { return ''; } -sub outrow { +sub outrowassess { my $n=shift; my @cols=(); if ($n) { @@ -318,6 +328,35 @@ sub outrow { return @cols; } +sub outrow { + my $n=shift; + my @cols=(); + if ($n) { + $cols[0]=$rl{$f{'A'.$n}}.' stuff'; + } else { + $cols[0]='Export'; + } + map { + my $fm=$f{$_.$n}; + $fm=~s/[\'\"]/\&\#34;/g; + $cols[$#cols+1]="'$_$n','$fm'".'___eq___'.$v{$_.$n}; + } ('A','B','C','D','E','F','G','H','I','J','K','L','M', + 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', + 'a','b','c','d','e','f','g','h','i','j','k','l','m', + 'n','o','p','q','r','s','t','u','v','w','x','y','z'); + return @cols; +} + +sub exportrowa { + my $rowa=''; + map { + $rowa.=$v{$_.'0'}."___;___"; + } ('A','B','C','D','E','F','G','H','I','J','K','L','M', + 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'); + $rowa=~s/\_\_\_\;\_\_\_$//; + return $rowa; +} + # ------------------------------------------- End of "Inside of the safe space" ENDDEFS $safeeval->reval($code); @@ -406,7 +445,14 @@ sub getfilename { my $safeeval=shift; return $safeeval->reval('$filename'); } - + +# ------------------------------------------------------------- Export of A-row + +sub exportrow { + my $safeeval=shift; + return $safeeval->reval('&exportrowa()'); +} + # ========================================================== End of Spreadsheet # ============================================================================= @@ -418,13 +464,22 @@ sub rown { my $defaultbg=((($n-1)/5)==int(($n-1)/5))?'#E0E0':'#FFFF'; my $rowdata="\n$n"; my $showf=0; + my $proc; + my $maxred; + if (&gettype($safeeval) eq 'assesscalc') { + $proc='&outrowassess'; + $maxred=1; + } else { + $proc='&outrow'; + $maxred=26; + } map { my $bgcolor=$defaultbg.((($showf-1)/5==int(($showf-1)/5))?'99':'DD'); my ($fm,$vl)=split(/\_\_\_eq\_\_\_/,$_); if ($showf==0) { $vl=$_; } - if ($showf<=1) { $bgcolor='#FFDDDD'; } + if ($showf<=$maxred) { $bgcolor='#FFDDDD'; } if (($n==0) && ($showf<=26)) { $bgcolor='#CCCCFF'; } - if ($showf>1) { + if (($showf>$maxred) || ((!$n) && ($showf>0))) { if ($vl eq '') { $vl='#'; } @@ -435,7 +490,7 @@ sub rown { $rowdata.=' '.$vl.' '; } $showf++; - } $safeeval->reval('&outrow('.$n.')'); + } $safeeval->reval($proc.'('.$n.')'); return $rowdata.''; } @@ -443,11 +498,35 @@ sub rown { sub outsheet { my $safeeval=shift; - my $tabledata=''. - ''; + my $maxred; + my $realm; + if (&gettype($safeeval) eq 'assesscalc') { + $maxred=1; + $realm='Assessment'; + } elsif (&gettype($safeeval) eq 'studentcalc') { + $maxred=26; + $realm='User'; + } else { + $maxred=26; + $realm='Course'; + } + my $maxyellow=52-$maxred; + my $tabledata='
 A Import
'. + ''. + ''; + my $showf=0; map { - $tabledata.=""; - } ('B','C','D','E','F','G','H','I','J','K','L','M', + $showf++; + if ($showf<=$maxred) { + $tabledata.='"; + } ('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z'); @@ -567,6 +646,9 @@ sub parmval { my ($mapname,$id,$fn)=split(/\_\_\_/,$symb); # ----------------------------------------------------- Cascading lookup scheme + my $rwhat=$what; + $what=~s/^parameter\_//; + $what=~s/\_/\./; my $symbparm=$symb.'.'.$what; my $mapparm=$mapname.'___(all).'.$what; @@ -587,6 +669,7 @@ sub parmval { $ENV{'request.course.id'}.'.'.$symbparm; my $courselevelm= $ENV{'request.course.id'}.'.'.$mapparm; + # ---------------------------------------------------------- fourth, check user if ($uname) { @@ -624,7 +707,7 @@ sub parmval { # -------------------------------------------------------- first, check default - return &Apache::lonnet::metadata($fn,$what.'.default'); + return &Apache::lonnet::metadata($fn,$rwhat.'.default'); } @@ -700,7 +783,7 @@ sub updaterows { if ($_=~/^A(\d+)/) { $maxrow=($1>$maxrow)?$1:$maxrow; $existing{$f{$_}}=1; - unless (defined($current{$f{$_}})) { + unless ((defined($current{$f{$_}})) || (!$1)) { $f{$_}='!!! Obsolete'; $changed=1; } @@ -727,6 +810,40 @@ sub updaterows { } } +# --------------------------------------------------- Load data for one student + +sub rowazstudent { + my $safeeval=shift; + my %c=(); + my %f=&getformulas($safeeval); + map { + if ($_=~/^A(\d+)/) { + my $row=$1; + unless ($f{$_}=~/^\!/) { + my @assessdata=split(/\_\_\_\;\_\_\_/, + &Apache::lonnet::ssi( + '/res/msu/korte/junk.assesscalc',('utarget' => 'export', + 'uname' => $uname, + 'udom' => $udom, + 'usymb' => $f{$_}))); + my $index=0; + map { + if ($assessdata[$index]) { + $c{$_.$row}=$assessdata[$index]; + unless ($_ eq 'A') { + $f{$_.$row}='import'; + } + } + $index++; + } ('A','B','C','D','E','F','G','H','I','J','K','L','M', + 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'); + } + } + } keys %f; + &setformulas($safeeval,%f); + &setconstants($safeeval,%c); +} + # ------------------------------------------------ Load data for one assessment sub rowaassess { @@ -737,7 +854,9 @@ sub rowaassess { # ----------------------------------------------------------- Get stored values my $answer=&Apache::lonnet::reply( - "restore:$udom:$uname:$namespace:$symb",$uhome); + "restore:$udom:$uname:". + &Apache::lonnet::escape($namespace).":". + &Apache::lonnet::escape($symb),$uhome); my %returnhash=(); map { my ($name,$value)=split(/\=/,$_); @@ -758,13 +877,15 @@ sub rowaassess { my $uhome=&Apache::lonnet::homeserver($uname,$udom); unless ($uhome eq 'no_host') { # -------------------------------------------------------------- Get coursedata - unless ($courserdatas{$ENV{'request.course.id'}}) { + unless + ((time-$courserdatas{$ENV{'request.course.id'}.'.last_cache'})<120) { my $reply=&Apache::lonnet::reply('dump:'. $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.':'. $ENV{'course.'.$ENV{'request.course.id'}.'.num'}.':resourcedata', $ENV{'course.'.$ENV{'request.course.id'}.'.home'}); if ($reply!~/^error\:/) { $courserdatas{$ENV{'request.course.id'}}=$reply; + $courserdatas{$ENV{'request.course.id'}.'.last_cache'}=time; } } map { @@ -773,17 +894,19 @@ sub rowaassess { &Apache::lonnet::unescape($value); } split(/\&/,$courserdatas{$ENV{'request.course.id'}}); # --------------------------------------------------- Get userdata (if present) - unless ($userrdatas{$uname.'___'.$udom}) { + unless + ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<120) { my $reply= &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome); if ($reply!~/^error\:/) { $userrdatas{$uname.'___'.$udom}=$reply; + $userrdatas{$uname.'___'.$udom.'.last_cache'}=time; } } map { my ($name,$value)=split(/\=/,$_); $useropt{&Apache::lonnet::unescape($name)}= - &Apache::lonnet::unescape($value); + &Apache::lonnet::unescape($value); } split(/\&/,$userrdatas{$uname.'___'.$udom}); } # -- now courseopt, useropt initialized for this user and course (used parmval) @@ -796,7 +919,10 @@ sub rowaassess { if ($f{$_}=~/^parameter/) { $c{$_}=&parmval($f{$_},$symb); } else { - $c{$_}=$returnhash{$f{$_}}; + my $key=$f{$_}; + $key=~s/^stores\_/resource\./; + $key=~s/\_/\./; + $c{$_}=$returnhash{$key}; } } } @@ -829,7 +955,7 @@ sub selectbox { return $selout.''; } -# ---------------------------------------------------------------- Main handler +# ================================================================ Main handler sub handler { my $r=shift; @@ -850,6 +976,7 @@ sub handler { ($ENV{'request.state'} eq 'construct')) { # --------------------------- Get query string for limited number of parameters + map { my ($name, $value) = split(/=/,$_); $value =~ tr/+/ /; @@ -870,6 +997,10 @@ sub handler { $uname=$ENV{'form.uname'}; $udom=$ENV{'form.udom'}; } +# ----------------------------------------------------------- Change of target? + + my $reroute=($ENV{'form.utarget'} eq 'export'); + # ------------------------------------------------------------------- Open page $r->content_type('text/html'); @@ -877,6 +1008,9 @@ sub handler { $r->header_out('Pragma','no-cache'); $r->send_http_header; +# --------------------------------------------------------------- Screen output + + unless ($reroute) { $r->print('LON-CAPA Spreadsheet'); $r->print(< @@ -899,21 +1033,28 @@ ENDSCRIPT &hiddenfield('usymb',$ENV{'form.usymb'}). &hiddenfield('unewfield',''). &hiddenfield('unewformula','')); + } + +# ---------------------------------------- Read new sheet or modified worksheet + my $sheetone=initsheet(); if ($ENV{'form.unewfield'}) { $r->print('

Modified Workcopy

'); $ENV{'form.unewformula'}=~s/\'/\"/g; $r->print('New formula: '.$ENV{'form.unewfield'}.'='. $ENV{'form.unewformula'}.'
'); - &tmpread($sheetone,$r->dir_config('lonDaemons').'/tmp/', - $ENV{'form.usymb'}, - $ENV{'form.unewfield'},$ENV{'form.unewformula'}); &setfilename($sheetone,$r->filename); $r->filename=~/\.(\w+)/; &settype($sheetone,$1); + &tmpread($sheetone,$r->dir_config('lonDaemons').'/tmp/', + $ENV{'form.usymb'}, + $ENV{'form.unewfield'},$ENV{'form.unewformula'}); } else { &readsheet($sheetone,$r->filename); } + +# --------------------------------------------- See if all import rows uptodate + if (tie(%parmhash,'GDBM_File', $ENV{'request.course.fn'}.'_parms.db',&GDBM_READER,0640)) { $csec=&Apache::lonnet::usection($udom,$uname,$ENV{'request.course.id'}); @@ -927,22 +1068,42 @@ ENDSCRIPT $r->print('

'. 'Could not initialize import fields (not in a course)

'); } + +# ------------------------------------------------ Write the modified worksheet + &tmpwrite($sheetone,$r->dir_config('lonDaemons').'/tmp/', $ENV{'form.usymb'}); + +# ----------------------------------------------------- Print user, course, etc + unless ($reroute) { $r->print("User '$uname' at domain '$udom' for '". $ENV{'course.'.$ENV{'request.course.id'}.'.description'}."'"); if ($csec) { $r->print(", group/section '$csec'"); } $r->print("\n"); + } +# -------------------------------------------------------- Import and calculate + if (&gettype($sheetone) eq 'assesscalc') { &rowaassess($sheetone,$ENV{'form.usymb'}); + } elsif (&gettype($sheetone) eq 'studentcalc') { + &rowazstudent($sheetone); + } + my $calcoutput=&calcsheet($sheetone); + unless ($reroute) { + $r->print('

'.$calcoutput.'

'); } - &calcsheet($sheetone); + +# ------------------------------------------------------- Print or export sheet + unless ($reroute) { $r->print(&outsheet($sheetone)); $r->print(''); - + } else { + $r->print(&exportrow($sheetone)); + } +# ------------------------------------------------------------------------ Done } else { # ----------------------------- Not in a course, or not allowed to modify parms $ENV{'user.error.msg'}=
'. + $realm.'ImportCalculations
$_'; + } else { + $tabledata.=''; + } + $tabledata.="$_