Annotation of loncom/homework/grades.pm, revision 1.27

1.17      albertel    1: # The LearningOnline Network with CAPA
1.13      albertel    2: # The LON-CAPA Grading handler
1.17      albertel    3: #
1.27    ! albertel    4: # $Id: grades.pm,v 1.26 2002/05/21 02:29:22 albertel Exp $
1.17      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.13      albertel   28: # 2/9,2/13 Guy Albertelli
1.8       www        29: # 6/8 Gerd Kortemeyer
1.13      albertel   30: # 7/26 H.K. Ng
1.14      www        31: # 8/20 Gerd Kortemeyer
1.1       albertel   32: 
                     33: package Apache::grades;
                     34: use strict;
                     35: use Apache::style;
                     36: use Apache::lonxml;
                     37: use Apache::lonnet;
1.3       albertel   38: use Apache::loncommon;
1.1       albertel   39: use Apache::lonhomework;
                     40: use Apache::Constants qw(:common);
                     41: 
1.2       albertel   42: sub moreinfo {
1.13      albertel   43:   my ($request,$reason) = @_;
                     44:   $request->print("Unable to process request: $reason");
                     45:   if ( $Apache::grades::viewgrades eq 'F' ) {
                     46:     $request->print('<form action="/adm/grades" method="post">'."\n");
1.16      albertel   47:     if ($ENV{'form.url'}) {
                     48:       $request->print('<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />'."\n");
                     49:     }
                     50:     if ($ENV{'form.symb'}) {
                     51:       $request->print('<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />'."\n");
                     52:     }
                     53:     $request->print('<input type="hidden" name="command" value="'.$ENV{'form.command'}.'" />'."\n");
                     54:     $request->print("Student:".'<input type="text" name="student" value="'.$ENV{'form.student'}.'" />'."<br />\n");
                     55:     $request->print("Domain:".'<input type="text" name="domain" value="'.$ENV{'user.domain'}.'" />'."<br />\n");
                     56:     $request->print('<input type="submit" name="submit" value="ReSubmit" />'."<br />\n");
1.13      albertel   57:     $request->print('</form>');
                     58:   }
                     59:   return '';
1.2       albertel   60: }
                     61: 
1.23      www        62: sub verifyreceipt {
                     63:     my $request=shift;
                     64:     my $courseid=$ENV{'request.course.id'};
                     65:     my $cdom=$ENV{"course.$courseid.domain"};
                     66:     my $cnum=$ENV{"course.$courseid.num"};
                     67:     my $receipt=unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'}).'-'.
                     68:                 $ENV{'form.receipt'};
                     69:     $receipt=~s/[^\-\d]//g;
                     70:     my $symb=$ENV{'form.symb'};
                     71:     unless ($symb) {
                     72: 	$symb=&Apache::lonnet::symbread($ENV{'form.url'});
                     73:     }
                     74:     if ((&Apache::lonnet::allowed('mgr',$courseid)) && ($symb)) {
                     75:         $request->print('<h1>Verifying Submission Receipt '.$receipt.'</h1>');
                     76:         my $matches=0;
1.24      albertel   77:         my (%classlist) = &getclasslist($cdom,$cnum,'0');
1.23      www        78:         foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                     79:             my ($uname,$udom)=split(/\:/,$student);
                     80:             if ($receipt eq 
                     81:              &Apache::lonnet::ireceipt($uname,$udom,$courseid,$symb)) {
                     82:                $request->print('Matching '.$student.'<br>');
                     83:                $matches++;
                     84: 	   }
                     85:         }
                     86:         $request->print('<p>'.$matches.' match(es)</p>');
                     87:     }
                     88:     return '';
                     89: }
1.13      albertel   90: 
1.10      ng         91: sub listStudents {
1.13      albertel   92:   my ($request) = shift;
1.23      www        93:   my $cdom=$ENV{"course.$ENV{'request.course.id'}.domain"};
                     94:   my $cnum=$ENV{"course.$ENV{'request.course.id'}.num"};
                     95:   my $hostver=unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'});
                     96:   $request->print(<<ENDHEADER);
                     97: <h1>Verify a Submission Receipt Issued by this Server</h1>
                     98: <form action="/adm/grades" method="post">
                     99: <tt>$hostver-<input type="text" name="receipt" size="4"></tt>
                    100: <input type="submit" name="submit" value="Verify">
                    101: <input type="hidden" name="command" value="verify">
                    102: ENDHEADER
                    103: 	  if ($ENV{'form.url'}) {
                    104: 	    $request->print(
                    105:     '<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />');
                    106: 	  }
                    107: 	  if ($ENV{'form.symb'}) {
                    108: 	    $request->print(
                    109:     '<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />');
                    110: 	  }
                    111:   $request->print(<<ENDTABLEST);
                    112: </form>
                    113: <h1>Show Student Submissions on Assessment</h1>
                    114: <table border="1">
                    115: <tr><th>Username</th><th>Domain</th><th>Name</th><th>&nbsp;</th></tr>
                    116: ENDTABLEST
1.24      albertel  117:   my (%classlist) = &getclasslist($cdom,$cnum,'0');
1.13      albertel  118:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                    119:       my ($sname,$sdom) = split(/:/,$student);
                    120: 
1.25      albertel  121:       my %name=&Apache::lonnet::get('environment', ['lastname','generation',
                    122: 						    'firstname','middlename'],
                    123: 				    $sdom,$sname);
                    124:       my $fullname;
                    125:       my ($tmp) = keys(%name);
                    126:       if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                    127: 	$fullname=$name{'lastname'}.$name{'generation'};
                    128: 	if ($fullname =~ /[^\s]+/) { $fullname.=','; }
                    129: 	$fullname.=$name{'firstname'}.' '.$name{'middlename'};
                    130:       }
1.13      albertel  131:       if ( $Apache::grades::viewgrades eq 'F' ) {
1.25      albertel  132: 	  $request->print("\n".'<tr>'."<td>$sname</td><td>$sdom</td><td>$fullname</td><td>".
1.21      albertel  133: 			  '<form action="/adm/grades" method="post">');
1.16      albertel  134: 	  if ($ENV{'form.url'}) {
1.19      www       135: 	    $request->print(
                    136:     '<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />');
1.16      albertel  137: 	  }
                    138: 	  if ($ENV{'form.symb'}) {
1.19      www       139: 	    $request->print(
                    140:     '<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />');
1.16      albertel  141: 	  }
1.19      www       142: 	  $request->print(
                    143:     '<input type="hidden" name="command" value="'.$ENV{'form.command'}.'" />');
                    144: 	  $request->print(
                    145:              '<input type="hidden" name="student" value="'.$sname.'" />');
                    146: 	  $request->print(
                    147:              '<input type="hidden" name="domain" value="'.$sdom.'" />');
                    148: 	  $request->print(
                    149:                          '<input type="submit" name="submit" value="View" />');
1.21      albertel  150: 	  $request->print('</form></td></tr>');
1.13      albertel  151:       }
                    152:   }
1.19      www       153:   $request->print('</table>');
1.10      ng        154: }
                    155: 
1.13      albertel  156: 
1.7       albertel  157: #FIXME - needs to handle multiple matches
1.2       albertel  158: sub finduser {
1.13      albertel  159:   my ($name) = @_;
                    160:   my $domain = '';
                    161: 
                    162:   if ( $Apache::grades::viewgrades eq 'F' ) {
                    163:     #get classlist
                    164:     my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
1.24      albertel  165:     #print "Found $cdom:$cnum<br />";
                    166:     my (%classlist) = &getclasslist($cdom,$cnum,'0');
1.13      albertel  167:     foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                    168:       my ($posname,$posdomain) = split(/:/,$student);
                    169:       if ($posname =~ $name) { $name=$posname; $domain=$posdomain; last; }
1.7       albertel  170:     }
1.13      albertel  171:     return ($name,$domain);
                    172:   } else {
                    173:     return ($ENV{'user.name'},$ENV{'user.domain'});
                    174:   }
1.5       albertel  175: }
                    176: 
                    177: sub getclasslist {
1.24      albertel  178:   my ($coursedomain,$coursenum,$hideexpired) = @_;
                    179:   my %classlist=&Apache::lonnet::dump('classlist',$coursedomain,$coursenum);
1.13      albertel  180:   my $now = time;
1.24      albertel  181:   foreach my $student (keys(%classlist)) {
                    182:     my ($end,$start)=split(/:/,$classlist{$student});
1.13      albertel  183:     # still a student?
                    184:     if (($hideexpired) && ($end) && ($end < $now)) {
1.15      albertel  185:       #print "Skipping:$name:$end:$now<br />\n";
1.13      albertel  186:       next;
                    187:     }
1.15      albertel  188:     #print "record=$record<br>";
1.24      albertel  189:     push( @{ $classlist{'allids'} }, $student);
1.13      albertel  190:   }
                    191:   return (%classlist);
1.5       albertel  192: }
                    193: 
                    194: sub getpartlist {
1.13      albertel  195:   my ($url) = @_;
                    196:   my @parts =();
                    197:   my (@metakeys) = split(/,/,&Apache::lonnet::metadata($url,'keys'));
                    198:   foreach my $key (@metakeys) {
                    199:     if ( $key =~ m/stores_([0-9]+)_.*/ ) {
                    200:       push(@parts,$key);
1.6       albertel  201:     }
1.13      albertel  202:   }
                    203:   return @parts;
1.5       albertel  204: }
                    205: 
                    206: sub viewstudentgrade {
1.13      albertel  207:   my ($url,$symb,$courseid,$student,@parts) = @_;
                    208:   my $result ='';
                    209:   my $cellclr = '"#ffffdd"';
                    210:   my ($stuname,$domain) = split(/:/,$student);
                    211: 
                    212:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
                    213: 
                    214:   $result.="<tr><td bgcolor=$cellclr>$stuname</td><td bgcolor=$cellclr align=\"middle\">$domain</td>\n";
                    215:   foreach my $part (@parts) {
                    216:     my ($temp,$part,$type)=split(/_/,$part);
                    217:     #print "resource.$part.$type = ".$record{"resource.$part.$type"}." <br />\n";
                    218:     if ($type eq 'awarded') {
                    219:       my $score=$record{"resource.$part.$type"};
                    220:       $result.="<td bgcolor=$cellclr align=\"middle\"><input type=\"text\" name=\"GRADE.$student.$part.$type\" value=\"$score\" size=\"4\" /></td>\n";
                    221:     } elsif ($type eq 'tries') {
                    222:       my $score=$record{"resource.$part.$type"};
                    223:       $result.="<td bgcolor=$cellclr align=\"middle\"><input type=\"text\" name=\"GRADE.$student.$part.$type\" value=\"$score\" size=\"4\" /></td>\n"
                    224:     } elsif ($type eq 'solved') {
                    225:       my $score=$record{"resource.$part.$type"};
                    226:       $result.="<td bgcolor=$cellclr align=\"middle\"><select name=\"GRADE.$student.$part.$type\">\n";
                    227:       if ($score =~ /^correct/) {
                    228: 	$result.="<option selected=\"on\">correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
                    229:       } elsif ($score =~ /^incorrect/) {
                    230: 	$result.="<option>correct</option>\n<option selected=\"on\">incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
                    231:       } elsif ($score eq '') {
                    232: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option>ungraded</option>\n<option selected=\"on\">nothing</option>\n";
                    233:       } elsif ($score =~ /^excused/) {
                    234: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option selected=\"on\">excused</option>\n<option>ungraded</option>\n<option>nothing</option>\n";
                    235:       } elsif ($score =~ /^ungraded/) {
                    236: 	$result.="<option>correct</option>\n<option>incorrect</option>\n<option>excused</option>\n<option selected=\"on\">ungraded</option>\n<option>nothing</option>\n";
                    237:       }
                    238:       $result.="</select></td>\n";
                    239:     }
                    240:   }
                    241:   $result.='</tr>';
                    242:   return $result;
1.5       albertel  243: }
1.13      albertel  244: #FIXME need to look at the meatdata <stores> spec on what type of data to accept and provide an
1.6       albertel  245: #interface based on that, also do that to above function.
1.5       albertel  246: sub setstudentgrade {
1.13      albertel  247:   my ($url,$symb,$courseid,$student,@parts) = @_;
                    248: 
                    249:   my $result ='';
                    250: 
                    251:   my ($stuname,$domain) = split(/:/,$student);
                    252: 
                    253:   my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$stuname);
                    254: 
                    255:   my %newrecord;
                    256: 
                    257:   foreach my $part (@parts) {
                    258:     my ($temp,$part,$type)=split(/_/,$part);
                    259:     my $oldscore=$record{"resource.$part.$type"};
                    260:     my $newscore=$ENV{"form.GRADE.$student.$part.$type"};
                    261:     if ($type eq 'solved') {
                    262:       my $update=0;
                    263:       if ($newscore eq 'nothing' ) {
                    264: 	if ($oldscore ne '') {
                    265: 	  $update=1;
                    266: 	  $newscore = '';
1.6       albertel  267: 	}
1.13      albertel  268:       } elsif ($oldscore !~ m/^$newscore/) {
                    269: 	$update=1;
                    270: 	$result.="Updating $stuname to $newscore<br />\n";
                    271: 	if ($newscore eq 'correct') { $newscore = 'correct_by_override'; }
                    272: 	if ($newscore eq 'incorrect') { $newscore = 'incorrect_by_override'; }
                    273: 	if ($newscore eq 'excused') { $newscore = 'excused'; }
                    274: 	if ($newscore eq 'ungraded') { $newscore = 'ungraded_attempted'; }
                    275:       } else {
                    276: 	#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
                    277:       }
                    278:       if ($update) { $newrecord{"resource.$part.$type"}=$newscore; }
                    279:     } else {
                    280:       if ($oldscore ne $newscore) {
                    281: 	$newrecord{"resource.$part.$type"}=$newscore;
                    282: 	$result.="Updating $student"."'s status for $part.$type to $newscore<br />\n";
                    283:       } else {
                    284: 	#$result.="$stuname:$part:$type:unchanged  $oldscore to $newscore:<br />\n";
                    285:       }
                    286:     }
                    287:   }
                    288:   if ( scalar(keys(%newrecord)) > 0 ) {
                    289:     $newrecord{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
                    290:     &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$stuname);
                    291: 
                    292:     $result.="Stored away ".scalar(keys(%newrecord))." elements.<br />\n";
                    293:   }
                    294:   return $result;
1.2       albertel  295: }
                    296: 
                    297: sub submission {
1.13      albertel  298:   my ($request) = @_;
                    299:   my $url=$ENV{'form.url'};
                    300:   $url=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
                    301:   if ($ENV{'form.student'} eq '') { &moreinfo($request,"Need student login id"); return ''; }
1.10      ng        302: #  if ($ENV{'form.student'} eq '') { &listStudents($request); return ''; }
1.13      albertel  303:   my ($uname,$udom) = &finduser($ENV{'form.student'});
                    304:   if ($uname eq '') { &moreinfo($request,"Unable to find student"); return ''; }
1.16      albertel  305:   my $symb;
                    306:   if ($ENV{'form.symb'}) {
                    307:     $symb=$ENV{'form.symb'};
                    308:   } else {
                    309:     $symb=&Apache::lonnet::symbread($url);
                    310:   }
1.13      albertel  311:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
                    312:   my $answer=&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
                    313: 						      $ENV{'request.course.id'});
                    314:   my $result="<h2> Submission Record </h2>  $uname:$udom for $url <br />".$answer;
1.17      albertel  315:   my $rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
                    316: 						   $ENV{'request.course.id'});
1.22      albertel  317:   $result.="Student's view of the problem:<br /> $rendered <br /> Correct answer:<br />";
1.18      albertel  318: 
1.22      albertel  319:   $answer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
1.18      albertel  320: 						  $ENV{'request.course.id'});
                    321:   $result.=$answer;
1.13      albertel  322:   return $result;
1.2       albertel  323: }
                    324: 
1.26      albertel  325: sub get_symb_and_url {
                    326:  my ($request) = @_;
1.13      albertel  327:   my $url=$ENV{'form.url'};
                    328:   $url=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
                    329:   my $symb=$ENV{'form.symb'};
                    330:   if (!$symb) { $symb=&Apache::lonnet::symbread($url); }
                    331:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
1.26      albertel  332:  return ($symb,$url);
                    333: }
1.13      albertel  334: 
1.26      albertel  335: sub gradingmenu {
                    336:   my ($request) = @_;
                    337:   my ($symb,$url)=&get_symb_and_url($request);
                    338:   if (!$symb) {return '';}
                    339:   my $result='<h2>Select a grading method</h2><br />';
                    340:   $result.='<form action="/adm/grades" method="post">'."\n".
                    341:      '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    342:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                    343: 	'<input type="hidden" name="command" value="viewgrades" />'."\n".
                    344: 	  '<input type="submit" name="submit" value="View/Edit Entire Class" />'."\n".
                    345: 	    '</form>';
                    346:   $result.='<form action="/adm/grades" method="post">'."\n".
                    347:      '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    348:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                    349: 	'<input type="hidden" name="command" value="csvupload" />'."\n".
                    350: 	  '<input type="submit" name="submit" value="Upload Scores" />'."\n".
                    351: 	    '</form>';
                    352:   $result.='<form action="/adm/grades" method="post">'."\n".
                    353:      '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    354:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                    355: 	'<input type="hidden" name="command" value="submission" />'."\n".
                    356: 	  '<input type="submit" name="submit" value="View/Edit Student" />'."\n".
                    357: 	    '</form>';
                    358:   return $result;
                    359: }
                    360: 
                    361: sub viewgrades {
                    362:   my ($request) = @_;
                    363:   my $result='';
                    364: 
                    365:   #get resource reference
                    366:   my ($symb,$url)=&get_symb_and_url($request);
                    367:   if (!$symb) {return '';}
1.13      albertel  368:   #get classlist
                    369:   my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
1.24      albertel  370:   #print "Found $cdom:$cnum<br />";
                    371:   my (%classlist) = &getclasslist($cdom,$cnum,'0');
1.13      albertel  372:   my $headerclr = '"#ccffff"';
                    373:   my $cellclr = '"#ffffcc"';
                    374: 
                    375:   #get list of parts for this problem
                    376:   my (@parts) = &getpartlist($url);
                    377: 
                    378:   $request->print ("<h2><font color=\"#339966\">Manual Grading</font></h2>");
                    379: 
                    380:   #start the form
                    381:   $result = '<form action="/adm/grades" method="post">'."\n".
1.16      albertel  382:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    383:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
1.13      albertel  384: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
                    385: 	  '<input type="submit" name="submit" value="Submit Changes" />'."\n".
                    386: 	    '<table border=0><tr><td bgcolor="#999999">'."\n".
                    387: 	     '<table border=0>'."\n".
                    388: 	      '<tr><td bgcolor='.$headerclr.'>UserId</td><td bgcolor='.$headerclr.'>Domain</td>'."\n";
1.27    ! albertel  389:   foreach my $part (sort(@parts)) {
1.13      albertel  390:      my $display=&Apache::lonnet::metadata($url,$part.'.display');
                    391:      if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
                    392:      $result.='<td bgcolor='.$headerclr.'>'.$display.'</td>'."\n";
                    393:    }
                    394:   $result.="</tr>";
                    395:   #get info for each student
                    396:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                    397:     $result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
                    398:   }
                    399:   $result.='</table></td></tr></table><input type="submit" name="submit" value="Submit Changes" /></form>';
1.5       albertel  400: 
1.13      albertel  401:   return $result;
1.5       albertel  402: }
                    403: 
                    404: sub editgrades {
1.13      albertel  405:   my ($request) = @_;
                    406:   my $result='';
1.5       albertel  407: 
1.13      albertel  408:   my $symb=$ENV{'form.symb'};
                    409:   if ($symb eq '') { $request->print("Unable to handle ambiguous references:$symb:$ENV{'form.url'}"); return ''; }
                    410:   my $url=$ENV{'form.url'};
                    411:   #get classlist
                    412:   my ($cdom,$cnum) = split(/_/,$ENV{'request.course.id'});
1.24      albertel  413:   #print "Found $cdom:$cnum<br />";
                    414:   my (%classlist) = &getclasslist($cdom,$cnum,'0');
1.13      albertel  415: 
                    416:   #get list of parts for this problem
                    417:   my (@parts) = &getpartlist($url);
                    418: 
                    419:   $result.='<form action="/adm/grades" method="post">'."\n".
                    420:     '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                    421:       '<input type="hidden" name="url" value="'.$url.'" />'."\n".
                    422: 	'<input type="hidden" name="command" value="viewgrades" />'."\n".
                    423: 	  '<input type="submit" name="submit" value="See Grades" /> <br />'."\n";
                    424: 
                    425:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
                    426:     $result.=&setstudentgrade($url,$symb,$ENV{'request.course.id'},$student,@parts);
                    427:   }
1.5       albertel  428: 
1.13      albertel  429:   $result.='<input type="submit" name="submit" value="See Grades" /></table></form>';
                    430:   return $result;
1.5       albertel  431: }
                    432: 
1.26      albertel  433: sub csvupload {
                    434:   my ($request)= @_;
                    435:   my $result;
                    436:   my ($symb,$url)=&get_symb_and_url($request);
                    437:   if (!$symb) {return '';}
                    438:   my $upfile_select=&Apache::loncommon::upfile_select_html();
                    439:   $result.=<<ENDUPFORM;
                    440: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                    441: <input type="hidden" name="symb" value="$symb" />
                    442: <input type="hidden" name="url" value="$url" />
                    443: <input type="hidden" name="command" value="csvuploadmap" />
                    444: <hr />
                    445: <h3>Specify a file containing the class grades for resource $url</h3>
                    446: $upfile_select
                    447: <p><input type="submit" name="submit" value="Upload Grades" />
                    448: ENDUPFORM
                    449:   return $result;
                    450: }
                    451: 
1.27    ! albertel  452: sub csvupload_javascript_reverse_associate {
        !           453:   return(<<ENDPICK);
        !           454:   function verify(vf) {
        !           455:     var foundsomething=0;
        !           456:     var founduname=0;
        !           457:     var founddomain=0;
        !           458:     for (i=0;i<=vf.nfields.value;i++) {
        !           459:       tw=eval('vf.f'+i+'.selectedIndex');
        !           460:       if (i==0 && tw!=0) { founduname=1; }
        !           461:       if (i==1 && tw!=0) { founddomain=1; }
        !           462:       if (i!=0 && i!=1 && tw!=0) { foundsomething=1; }
        !           463:     }
        !           464:     if (founduname==0 || founddomain==0) {
        !           465:       alert('You need to specify at both the username and domain');
        !           466:       return;
        !           467:     }
        !           468:     if (foundsomething==0) {
        !           469:       alert('You need to specify at least one grading field');
        !           470:       return;
        !           471:     }
        !           472:     vf.submit();
        !           473:   }
        !           474:   function flip(vf,tf) {
        !           475:     var nw=eval('vf.f'+tf+'.selectedIndex');
        !           476:     var i;
        !           477:     for (i=0;i<=vf.nfields.value;i++) {
        !           478:       //can not pick the same destination field for both name and domain
        !           479:       if (((i ==0)||(i ==1)) && 
        !           480:           ((tf==0)||(tf==1)) && 
        !           481:           (i!=tf) &&
        !           482:           (eval('vf.f'+i+'.selectedIndex')==nw)) {
        !           483:         eval('vf.f'+i+'.selectedIndex=0;')
        !           484:       }
        !           485:     }
        !           486:   }
        !           487: ENDPICK
        !           488: }
        !           489: 
        !           490: sub csvupload_javascript_forward_associate {
        !           491:   return(<<ENDPICK);
        !           492:   function verify(vf) {
        !           493:     var foundsomething=0;
        !           494:     var founduname=0;
        !           495:     var founddomain=0;
        !           496:     for (i=0;i<=vf.nfields.value;i++) {
        !           497:       tw=eval('vf.f'+i+'.selectedIndex');
        !           498:       if (tw==1) { founduname=1; }
        !           499:       if (tw==2) { founddomain=1; }
        !           500:       if (tw>2) { foundsomething=1; }
        !           501:     }
        !           502:     if (founduname==0 || founddomain==0) {
        !           503:       alert('You need to specify at both the username and domain');
        !           504:       return;
        !           505:     }
        !           506:     if (foundsomething==0) {
        !           507:       alert('You need to specify at least one grading field');
        !           508:       return;
        !           509:     }
        !           510:     vf.submit();
        !           511:   }
        !           512:   function flip(vf,tf) {
        !           513:     var nw=eval('vf.f'+tf+'.selectedIndex');
        !           514:     var i;
        !           515:     //can not pick the same destination field twice
        !           516:     for (i=0;i<=vf.nfields.value;i++) {
        !           517:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
        !           518:         eval('vf.f'+i+'.selectedIndex=0;')
        !           519:       }
        !           520:     }
        !           521:   }
        !           522: ENDPICK
        !           523: }
        !           524: 
1.26      albertel  525: sub csvuploadmap_header {
                    526:   my ($request,$symb,$url,$datatoken,$distotal)= @_;
                    527:   my $result;
                    528:   my $javascript;
                    529:   if ($ENV{'form.upfile_associate'} eq 'reverse') {
1.27    ! albertel  530:     $javascript=&csvupload_javascript_reverse_associate();
1.26      albertel  531:   } else {
1.27    ! albertel  532:     $javascript=&csvupload_javascript_forward_associate();
1.26      albertel  533:   }
                    534:   $request->print(<<ENDPICK);
                    535: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                    536: <h3>Uploading Class Grades for resource $url</h3>
                    537: <hr>
                    538: <h3>Identify fields</h3>
                    539: Total number of records found in file: $distotal <hr />
                    540: Enter as many fields as you can. The system will inform you and bring you back
                    541: to this page if the data selected is insufficient to run your class.<hr />
                    542: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
                    543: <input type="hidden" name="associate"  value="" />
                    544: <input type="hidden" name="phase"      value="three" />
                    545: <input type="hidden" name="datatoken"  value="$datatoken" />
                    546: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
                    547: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
                    548: <input type="hidden" name="upfile_associate" 
                    549:                                        value="$ENV{'form.upfile_associate'}" />
                    550: <input type="hidden" name="symb"       value="$symb" />
                    551: <input type="hidden" name="url"        value="$url" />
                    552: <input type="hidden" name="command"    value="csvuploadassign" />
                    553: <hr />
                    554: <script type="text/javascript" language="Javascript">
                    555: $javascript
                    556: </script>
                    557: ENDPICK
                    558:   return '';
                    559: 
                    560: }
                    561: 
                    562: sub csvupload_fields {
                    563:   my ($url) = @_;
                    564:   my (@parts) = &getpartlist($url);
1.27    ! albertel  565:   my @fields=(['username','Student Username'],['domain','Student Domain']);
        !           566:   foreach my $part (sort(@parts)) {
1.26      albertel  567:     my @datum;
                    568:     my $display=&Apache::lonnet::metadata($url,$part.'.display');
1.27    ! albertel  569:     my $name=$part;
1.26      albertel  570:     if  (!$display) { $display = $name; }
                    571:     @datum=($name,$display);
                    572:     push(@fields,\@datum);
                    573:   }
                    574:   return (@fields);
                    575: }
                    576: 
                    577: sub csvuploadmap_footer {
                    578:   my ($request,$i,$keyfields) =@_;
                    579:   $request->print(<<ENDPICK);
                    580: </table>
                    581: <input type="hidden" name="nfields" value="$i" />
                    582: <input type="hidden" name="keyfields" value="$keyfields" />
                    583: <input type="button" onClick="javascript:verify(this.form)" value="Assign Grades" /><br />
                    584: </form>
                    585: ENDPICK
                    586: }
                    587: 
                    588: sub csvuploadmap {
                    589:   my ($request)= @_;
                    590:   my ($symb,$url)=&get_symb_and_url($request);
                    591:   if (!$symb) {return '';}
                    592:   my $datatoken;
                    593:   if (!$ENV{'form.datatoken'}) {
                    594:     $datatoken=&Apache::loncommon::upfile_store($request);
                    595:   } else {
                    596:     $datatoken=$ENV{'form.datatoken'};
                    597:     &Apache::loncommon::load_tmp_file($request);
                    598:   }
                    599:   my @records=&Apache::loncommon::upfile_record_sep();
                    600:   &csvuploadmap_header($request,$symb,$url,$datatoken,$#records+1);
                    601:   my $i;
                    602:   my $keyfields;
                    603:   if (@records) {
                    604:     my @fields=&csvupload_fields($url);
                    605:     if ($ENV{'form.upfile_associate'} eq 'reverse') {	
                    606:       &Apache::loncommon::csv_print_samples($request,\@records);
                    607:       $i=&Apache::loncommon::csv_print_select_table($request,\@records,
                    608: 						    \@fields);
                    609:       foreach (@fields) { $keyfields.=$_->[0].','; }
                    610:       chop($keyfields);
                    611:     } else {
                    612:       unshift(@fields,['none','']);
                    613:       $i=&Apache::loncommon::csv_samples_select_table($request,\@records,
                    614: 						      \@fields);
                    615:       my %sone=&Apache::loncommon::record_sep($records[0]);
                    616:       $keyfields=join(',',sort(keys(%sone)));
                    617:     }
                    618:   }
                    619:   &csvuploadmap_footer($request,$i,$keyfields);
                    620:   return '';
1.27    ! albertel  621: }
        !           622: 
        !           623: sub csvuploadassign {
        !           624:   my ($request)= @_;
        !           625:   my ($symb,$url)=&get_symb_and_url($request);
        !           626:   if (!$symb) {return '';}
        !           627:   &Apache::loncommon::load_tmp_file($request);
        !           628:   my @gradedata=&Apache::loncommon::upfile_record_sep();
        !           629:   my @keyfields = split(/\,/,$ENV{'form.keyfields'});
        !           630:   my %fields=();
        !           631:   for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
        !           632:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
        !           633:       if ($ENV{'form.f'.$i} ne 'none') {
        !           634: 	$fields{$keyfields[$i]}=$ENV{'form.f'.$i};
        !           635:       }
        !           636:     } else {
        !           637:       if ($ENV{'form.f'.$i} ne 'none') {
        !           638: 	$fields{$ENV{'form.f'.$i}}=$keyfields[$i];
        !           639:       }
        !           640:     }
        !           641:   }
        !           642:   $request->print('<h3>Assigning Grades</h3>');
        !           643:   &Apache::lonhomework::showhash(('1'=>\@keyfields));
        !           644:   &Apache::lonhomework::showhash(%fields);
        !           645:   my $courseid=$ENV{'request.course.id'};
        !           646:   my $cdom=$ENV{"course.$courseid.domain"};
        !           647:   my $cnum=$ENV{"course.$courseid.num"};
        !           648:   my (%classlist) = &getclasslist($cdom,$cnum,'1');
        !           649:   foreach my $student ( sort(@{ $classlist{'allids'} }) ) {
        !           650:     my %newhash;
        !           651:     foreach my $grade (@gradedata) {
        !           652:       my %entries=&Apache::loncommon::record_sep($grade);
        !           653:       foreach my $dest (keys(%fields)) {
        !           654: 	
        !           655:       }
        !           656:     }
        !           657:   }
1.26      albertel  658: }
                    659: 
1.2       albertel  660: sub send_header {
1.13      albertel  661:   my ($request)= @_;
                    662:   $request->print(&Apache::lontexconvert::header());
1.6       albertel  663: #  $request->print("
                    664: #<script>
                    665: #remotewindow=open('','homeworkremote');
                    666: #remotewindow.close();
                    667: #</script>"); 
1.13      albertel  668:   $request->print('<body bgcolor="#FFFFFF">');
1.2       albertel  669: }
                    670: 
                    671: sub send_footer {
1.13      albertel  672:   my ($request)= @_;
1.2       albertel  673:   $request->print('</body>');
                    674:   $request->print(&Apache::lontexconvert::footer());
                    675: }
                    676: 
1.1       albertel  677: sub handler {
1.13      albertel  678:   my $request=$_[0];
                    679: 
                    680:   if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;} else {$Apache::lonxml::debug=0;}
                    681:   
                    682:   if ($ENV{'browser.mathml'}) {
                    683:     $request->content_type('text/xml');
                    684:   } else {
                    685:     $request->content_type('text/html');
                    686:   }
                    687:   $request->send_http_header;
                    688:   return OK if $request->header_only;
1.16      albertel  689:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.13      albertel  690:   my $url=$ENV{'form.url'};
                    691:   my $symb=$ENV{'form.symb'};
                    692:   my $command=$ENV{'form.command'};
1.16      albertel  693:   if (!$url) {
                    694:     my ($temp1,$temp2);
                    695:     ($temp1,$temp2,$ENV{'form.url'})=split(/___/,$symb);
                    696:     $url = $ENV{'form.url'};
                    697:   }
1.13      albertel  698:   &send_header($request);
                    699:   if ($url eq '' && $symb eq '') {
1.14      www       700:      if ($ENV{'user.adv'}) {
                    701:          if (($ENV{'form.codeone'}) && ($ENV{'form.codetwo'}) &&
                    702:              ($ENV{'form.codethree'})) {
                    703:              my $token=$ENV{'form.codeone'}.'*'.$ENV{'form.codetwo'}.'*'.
                    704: 		        $ENV{'form.codethree'};
                    705:              my ($tsymb,$tuname,$tudom,$tcrsid)=
                    706: 		 &Apache::lonnet::checkin($token);
                    707:              if ($tsymb) {
                    708:                 my ($map,$id,$url)=split(/\_\_\_/,$tsymb);
                    709:                 if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
                    710:                    $request->print(
                    711:                      &Apache::lonnet::ssi('/res/'.$url,
                    712:                         ('grade_username' => $tuname,
                    713:                          'grade_domain' => $tudom,
                    714:                          'grade_courseid' => $tcrsid,
                    715:                          'grade_symb' => $tsymb)));
                    716:                 } else {
                    717:                    $request->print('<h1>Not authorized: '.$token.'</h1>');
                    718:                 }           
                    719: 	    } else {
                    720:                 $request->print('<h1>Not a valid DocID: '.$token.'</h1>');
                    721:             }
                    722: 	 } else {
                    723:              $request->print(&Apache::lonxml::tokeninputfield());
                    724:          }
                    725:      }
1.13      albertel  726:   } else {
1.26      albertel  727:     &Apache::lonhomework::showhashsubset(\%ENV,'^form');
1.13      albertel  728:     $Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    729:     if ($command eq 'submission') {
1.20      albertel  730:       &listStudents($request) if ($ENV{'form.student'} eq '');
1.13      albertel  731:       $request->print(&submission($request)) if ($ENV{'form.student'} ne '');
1.26      albertel  732:     } elsif ($command eq 'gradingmenu') {
                    733:       $request->print(&gradingmenu($request));
1.13      albertel  734:     } elsif ($command eq 'viewgrades') {
                    735:       $request->print(&viewgrades($request));
                    736:     } elsif ($command eq 'editgrades') {
                    737:       $request->print(&editgrades($request));
1.23      www       738:     } elsif ($command eq 'verify') {
                    739:       $request->print(&verifyreceipt($request));
1.26      albertel  740:     } elsif ($command eq 'csvupload') {
                    741:       $request->print(&csvupload($request));
                    742:     } elsif ($command eq 'csvuploadmap') {
                    743:       $request->print(&csvuploadmap($request));
                    744:     } elsif ($command eq 'csvuploadassign') {
                    745:       if ($ENV{'form.associate'} ne 'Reverse Association') {
                    746: 	$request->print(&csvuploadassign($request));
                    747:       } else {
                    748: 	if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
                    749: 	  $ENV{'form.upfile_associate'} = 'reverse';
                    750: 	} else {
                    751: 	  $ENV{'form.upfile_associate'} = 'forward';
                    752: 	}
                    753: 	$request->print(&csvuploadmap($request));
                    754:       }
1.12      harris41  755:     } else {
1.23      www       756:       $request->print("Unknown action: $command:");
1.2       albertel  757:     }
1.13      albertel  758:   }
                    759:   &send_footer($request);
                    760:   return OK;
1.1       albertel  761: }
                    762: 
                    763: 1;
                    764: 
1.13      albertel  765: __END__;

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