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

1.17      albertel    1: # The LearningOnline Network with CAPA
1.13      albertel    2: # The LON-CAPA Grading handler
1.17      albertel    3: #
1.56    ! matthew     4: # $Id: grades.pm,v 1.55 2002/10/16 19:13:57 matthew 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.30      ng         32: # Year 2002
1.44      ng         33: # June-August H.K. Ng
1.30      ng         34: #
1.1       albertel   35: 
                     36: package Apache::grades;
                     37: use strict;
                     38: use Apache::style;
                     39: use Apache::lonxml;
                     40: use Apache::lonnet;
1.3       albertel   41: use Apache::loncommon;
1.1       albertel   42: use Apache::lonhomework;
1.55      matthew    43: use Apache::loncoursedata;
1.38      ng         44: use Apache::lonmsg qw(:user_normal_msg);
1.1       albertel   45: use Apache::Constants qw(:common);
                     46: 
1.44      ng         47: # ----- These first few routines are general use routines.-----
                     48: #
                     49: # --- Retrieve the parts that matches stores_\d+ from the metadata file.---
                     50: sub getpartlist {
                     51:     my ($url) = @_;
                     52:     my @parts =();
                     53:     my (@metakeys) = split(/,/,&Apache::lonnet::metadata($url,'keys'));
                     54:     foreach my $key (@metakeys) {
1.54      albertel   55: 	if ( $key =~ m/stores_(\w+)_.*/) {
1.44      ng         56: 	    push(@parts,$key);
1.41      ng         57: 	}
1.16      albertel   58:     }
1.44      ng         59:     return @parts;
1.2       albertel   60: }
                     61: 
1.44      ng         62: # --- Get the symbolic name of a problem and the url
                     63: sub get_symb_and_url {
                     64:     my ($request) = @_;
                     65:     (my $url=$ENV{'form.url'}) =~ s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
1.41      ng         66:     my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
1.44      ng         67:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
                     68:     return ($symb,$url);
1.32      ng         69: }
                     70: 
1.44      ng         71: # --- Retrieve the fullname for a user. Return lastname, first middle ---
                     72: # --- Generation is attached next to the lastname if it exists. ---
1.34      ng         73: sub get_fullname {
1.39      ng         74:     my ($uname,$udom) = @_;
1.34      ng         75:     my %name=&Apache::lonnet::get('environment', ['lastname','generation',
1.55      matthew    76: 						  'firstname','middlename'],
                     77:                                   $udom,$uname);
1.34      ng         78:     my $fullname;
                     79:     my ($tmp) = keys(%name);
                     80:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.55      matthew    81:         $fullname = &Apache::loncoursedata::ProcessFullName
                     82:             (@name{qw/lastname generation firstname middlename/});
                     83:     } else {
                     84:         &Apache::lonnet::logthis('grades.pm: no name data for '.$uname.
                     85:                                  '@'.$udom.':'.$tmp);
1.34      ng         86:     }
                     87:     return $fullname;
                     88: }
                     89: 
1.44      ng         90: #--- Get the partlist and the response type for a given problem. ---
                     91: #--- Indicate if a response type is coded handgraded or not. ---
1.39      ng         92: sub response_type {
1.41      ng         93:     my ($url) = shift;
                     94:     my $allkeys = &Apache::lonnet::metadata($url,'keys');
                     95:     my %seen = ();
                     96:     my (@partlist,%handgrade);
                     97:     foreach (split(/,/,&Apache::lonnet::metadata($url,'packages'))) {
1.54      albertel   98: 	if (/^\w+response_\w+.*/) {
1.41      ng         99: 	    my ($responsetype,$part) = split(/_/,$_,2);
                    100: 	    my ($partid,$respid) = split(/_/,$part);
                    101: 	    $handgrade{$part} = $responsetype.':'.($allkeys =~ /parameter_$part\_handgrade/ ? 'yes' : 'no');
                    102: 	    next if ($seen{$partid} > 0);
                    103: 	    $seen{$partid}++;
                    104: 	    push @partlist,$partid;
                    105: 	}
                    106:     }
                    107:     return \@partlist,\%handgrade;
1.39      ng        108: }
                    109: 
1.44      ng        110: #--- Dumps the class list with usernames,list of sections,
                    111: #--- section, ids and fullnames for each user.
                    112: sub getclasslist {
                    113:     my ($getsec,$hideexpired) = @_;
1.56    ! matthew   114:     my $classlist=&Apache::loncoursedata::get_classlist();
1.49      albertel  115:     # Bail out if we were unable to get the classlist
1.56    ! matthew   116:     return if (! defined($classlist));
        !           117:     #
        !           118:     my %sections;
        !           119:     my %fullnames;
        !           120:     foreach (keys(%$classlist)) {
        !           121:         # the following undefs are for 'domain', and 'username' respectively.
        !           122: 	my (undef,undef,$end,$start,$id,$section,$fullname,$status)=
        !           123:             @{$classlist->{$_}};
1.44      ng        124: 	# still a student?
1.56    ! matthew   125: 	if (($hideexpired) && ($status ne 'Active')) {
        !           126:             delete ($classlist->{$_});
        !           127:             next;
        !           128:         }
1.44      ng        129: 	$section = ($section ne '' ? $section : 'no');
                    130: 	if ($getsec eq 'all' || $getsec eq $section) {
1.56    ! matthew   131:             $sections{$section}++;
        !           132:             $fullnames{$_}=$fullname;
        !           133:         } else {
        !           134:             delete($classlist->{$_});
        !           135:         }
1.44      ng        136:     }
                    137:     my %seen = ();
1.56    ! matthew   138:     my @sections = sort(keys(%sections));
        !           139:     return ($classlist,\@sections,\%fullnames);
1.44      ng        140: }
                    141: 
                    142: #find user domain
                    143: sub finduser {
                    144:     my ($name) = @_;
                    145:     my $domain = '';
                    146:     if ( $Apache::grades::viewgrades eq 'F' ) {
                    147: 	my %classlist=&Apache::lonnet::dump('classlist',
                    148: 					    $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    149: 					    $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    150: 	my (@fields) = grep /^$name:/, keys %classlist;
                    151: 	($name, $domain) = split(/:/,$fields[0]);
                    152: 	return ($name,$domain);
                    153:     } else {
                    154: 	return ($ENV{'user.name'},$ENV{'user.domain'});
                    155:     }
                    156: }
                    157: 
                    158: #--- Prompts a user to enter a username.
                    159: sub moreinfo {
                    160:     my ($request,$reason) = @_;
                    161:     $request->print("Unable to process request: $reason");
                    162:     if ( $Apache::grades::viewgrades eq 'F' ) {
                    163: 	$request->print('<form action="/adm/grades" method="post">'."\n");
                    164: 	if ($ENV{'form.url'}) {
                    165: 	    $request->print('<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />'."\n");
                    166: 	}
                    167: 	if ($ENV{'form.symb'}) {
                    168: 	    $request->print('<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />'."\n");
                    169: 	}
                    170: 	$request->print('<input type="hidden" name="command" value="'.$ENV{'form.command'}.'" />'."\n");
                    171: 	$request->print("Student:".'<input type="text" name="student" value="'.$ENV{'form.student'}.'" />'."<br />\n");
                    172: 	$request->print("Domain:".'<input type="text" name="domain" value="'.$ENV{'user.domain'}.'" />'."<br />\n");
                    173: 	$request->print('<input type="submit" name="submit" value="ReSubmit" />'."<br />\n");
                    174: 	$request->print('</form>');
                    175:     }
                    176:     return '';
                    177: }
                    178: 
                    179: #--- Retrieve the grade status of a student for all the parts
                    180: sub student_gradeStatus {
                    181:     my ($url,$symb,$udom,$uname,$partlist) = @_;
                    182:     my %record     = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
                    183:     my %partstatus = ();
                    184:     foreach (@$partlist) {
                    185: 	my ($status,$foo)    = split(/_/,$record{"resource.$_.solved"},2);
                    186: 	$status              = 'nothing' if ($status eq '');
                    187: 	$partstatus{$_}      = $status;
                    188: 	my $subkey           = "resource.$_.submitted_by";
                    189: 	$partstatus{$subkey} = $record{$subkey} if ($record{$subkey} ne '');
                    190:     }
                    191:     return %partstatus;
                    192: }
                    193: 
1.45      ng        194: # hidden form and javascript that calls the form
                    195: # Use by verifyscript and viewgrades
                    196: # Shows a student's view of problem and submission
                    197: sub jscriptNform {
                    198:     my ($url,$symb) = @_;
                    199:     my $jscript='<script type="text/javascript" language="javascript">'."\n".
                    200: 	'    function viewOneStudent(user,domain) {'."\n".
                    201: 	'	document.onestudent.student.value = user;'."\n".
                    202: 	'	document.onestudent.userdom.value = domain;'."\n".
                    203: 	'	document.onestudent.submit();'."\n".
                    204: 	'    }'."\n".
                    205: 	'</script>'."\n";
                    206:     $jscript.= '<form action="/adm/grades" method="post" name="onestudent">'."\n".
                    207: 	'<input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
                    208: 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
                    209: 	'<input type="hidden" name="command" value="submission" />'."\n".
                    210: 	'<input type="hidden" name="student" value="" />'."\n".
                    211: 	'<input type="hidden" name="userdom" value="" />'."\n".
                    212: 	'</form>'."\n";
                    213:     return $jscript;
                    214: }
1.39      ng        215: 
1.44      ng        216: #------------------ End of general use routines --------------------
                    217: #-------------------------------------------------------------------
                    218: 
                    219: #------------------------------------ Receipt Verification Routines
1.45      ng        220: #
1.44      ng        221: #--- Check whether a receipt number is valid.---
                    222: sub verifyreceipt {
                    223:     my $request  = shift;
                    224: 
                    225:     my $courseid = $ENV{'request.course.id'};
                    226:     my $receipt  = unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'}).'-'.
                    227: 	$ENV{'form.receipt'};
                    228:     $receipt     =~ s/[^\-\d]//g;
                    229:     my $url      = $ENV{'form.url'};
                    230:     my $symb     = $ENV{'form.symb'};
                    231:     unless ($symb) {
                    232: 	$symb    = &Apache::lonnet::symbread($url);
                    233:     }
                    234: 
1.45      ng        235:     my $title.='<h3><font color="#339933">Verifying Submission Receipt '.
                    236: 	$receipt.'</h3></font>'."\n".
1.44      ng        237: 	'<font size=+1><b>Resource: </b>'.$ENV{'form.url'}.'</font><br><br>'."\n";
                    238: 
                    239:     my ($string,$contents,$matches) = ('','',0);
1.56    ! matthew   240:     my (undef,undef,$fullname) = &getclasslist('all','0');
        !           241: 
1.53      albertel  242:     foreach (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
1.44      ng        243: 	my ($uname,$udom)=split(/\:/);
                    244: 	if ($receipt eq 
                    245: 	    &Apache::lonnet::ireceipt($uname,$udom,$courseid,$symb)) {
                    246: 	    $contents.='<tr bgcolor="#ffffe6"><td>&nbsp;'."\n".
                    247: 		'<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
                    248: 		'\')"; TARGET=_self>'.$$fullname{$_}.'</a>&nbsp;</td>'."\n".
                    249: 		'<td>&nbsp;'.$uname.'&nbsp;</td>'.
                    250: 		'<td>&nbsp;'.$udom.'&nbsp;</td></tr>'."\n";
                    251: 	    
                    252: 	    $matches++;
                    253: 	}
                    254:     }
                    255:     if ($matches == 0) {
                    256: 	$string = $title.'No match found for the above receipt.';
                    257:     } else {
1.45      ng        258: 	$string = &jscriptNform($url,$symb).$title.
1.44      ng        259: 	    'The above receipt matches the following student'.
                    260: 	    ($matches <= 1 ? '.' : 's.')."\n".
                    261: 	    '<table border="0"><tr><td bgcolor="#777777">'."\n".
                    262: 	    '<table border="0"><tr bgcolor="#e6ffff">'."\n".
                    263: 	    '<td><b>&nbsp;Fullname&nbsp;</b></td>'."\n".
                    264: 	    '<td><b>&nbsp;Username&nbsp;</b></td>'."\n".
                    265: 	    '<td><b>&nbsp;Domain&nbsp;</b></td></tr>'."\n".
                    266: 	    $contents.
                    267: 	    '</table></td></tr></table>'."\n";
                    268:     }
1.50      albertel  269:     return $string.&show_grading_menu_form($symb,$url);
1.44      ng        270: }
                    271: 
                    272: #--- This is called by a number of programs.
                    273: #--- Called from the Grading Menu - View/Grade an individual student
                    274: #--- Also called directly when one clicks on the subm button 
                    275: #    on the problem page.
1.30      ng        276: sub listStudents {
1.41      ng        277:     my ($request) = shift;
1.49      albertel  278: 
                    279:     my ($symb,$url) = &get_symb_and_url();
                    280:     my $cdom      = $ENV{"course.$ENV{'request.course.id'}.domain"};
                    281:     my $cnum      = $ENV{"course.$ENV{'request.course.id'}.num"};
                    282:     my $getsec    = $ENV{'form.section'} eq '' ? 'all' : $ENV{'form.section'};
                    283:     my $submitonly= $ENV{'form.submitonly'} eq '' ? 'all' : $ENV{'form.submitonly'};
                    284: 
                    285:     my $result;
                    286:     my ($partlist,$handgrade) = &response_type($url);
                    287:     for (sort keys(%$handgrade)) {
                    288: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
                    289: 	$ENV{'form.handgrade'} = 'yes' if ($handgrade eq 'yes');
                    290: 	$result.='<tr><td><b>Part </b>'.(split(/_/))[0].'</td>'.
                    291: 	    '<td><b>Type: </b>'.$responsetype.'</td>'.
                    292: 	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
                    293:     }
                    294:     $result.='</table>';
                    295: 
                    296:     my $viewgrade;
                    297:     if ($ENV{'form.handgrade'} eq 'yes') {
                    298: 	$viewgrade = 'View/Grade';
                    299:     } else {
                    300: 	$viewgrade = 'View';
                    301:     }
                    302: 
                    303:     $result='<h3><font color="#339933">&nbsp;'.
                    304: 	$viewgrade.
                    305: 	    ' Submissions for a Student or a Group of Students</font></h3>'.
                    306: 		'<table border="0"><tr><td colspan=3><font size=+1>'.
                    307: 		    '<b>Resource: </b>'.$url.'</font></td></tr>'.$result;
                    308: 
1.45      ng        309:     $request->print(<<LISTJAVASCRIPT);
                    310: <script type="text/javascript" language="javascript">
                    311:   function checkSelect(checkBox) {
                    312:     var ctr=0;
1.46      ng        313:     var sense="";
1.45      ng        314:     if (checkBox.length > 1) {
                    315:        for (var i=0; i<checkBox.length; i++) {
                    316: 	  if (checkBox[i].checked) {
                    317: 	     ctr++;
                    318: 	  }
                    319:        }
1.46      ng        320:        sense = "a student or group of students";
1.45      ng        321:     } else {
                    322:        if (checkBox.checked) {
                    323: 	   ctr = 1;
                    324:        }
1.46      ng        325:        sense = "the student";
1.45      ng        326:     }
                    327:     if (ctr == 0) {
1.49      albertel  328:        alert("Please select "+sense+" before clicking on the $viewgrade button.");
1.45      ng        329:        return false;
                    330:     }
                    331:     document.gradesub.submit();
                    332:   }
                    333: </script>
                    334: LISTJAVASCRIPT
                    335: 
1.41      ng        336:     $request->print($result);
1.39      ng        337: 
1.45      ng        338:     my $checkhdgrade = $ENV{'form.handgrade'} eq 'yes' ? 'checked' : '';
                    339:     my $checklastsub = $ENV{'form.handgrade'} eq 'yes' ? '' : 'checked';
1.44      ng        340: 
1.45      ng        341:     my $gradeTable='<form action="/adm/grades" method="post" name="gradesub">'."\n".
                    342: 	'&nbsp;<b>View Problem: </b><input type="radio" name="vProb" value="no" checked> no '."\n".
                    343: 	'<input type="radio" name="vProb" value="yes"> yes <br />'."\n".
1.49      albertel  344: 	'&nbsp;<b>Submissions: </b>'."\n";
                    345:     if ($ENV{'form.handgrade'} eq 'yes') {
                    346: 	$gradeTable.='<input type="radio" name="lastSub" value="hdgrade" '.$checkhdgrade.' /> handgrade only'."\n";
                    347:     }
                    348:     $gradeTable.='<input type="radio" name="lastSub" value="lastonly" '.$checklastsub.' /> last sub only'."\n".
1.45      ng        349: 	'<input type="radio" name="lastSub" value="last" /> last sub & parts info'."\n".
                    350: 	'<input type="radio" name="lastSub" value="all" /> all details'."\n".
                    351: 	'<input type="hidden" name="section"     value="'.$getsec.'" />'."\n".
                    352: 	'<input type="hidden" name="submitonly"  value="'.$submitonly.'" />'."\n".
                    353: 	'<input type="hidden" name="response"    value="'.$ENV{'form.response'}.'" />'."\n".
                    354: 	'<input type="hidden" name="handgrade"   value="'.$ENV{'form.handgrade'}.'" /><br />'."\n".
                    355: 	'<input type="hidden" name="showgrading" value="'.$ENV{'form.showgrading'}.'" /><br />'."\n".
1.48      albertel  356: 	'<input type="hidden" name="url"  value="'.$url.'" />'."\n".
                    357: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
1.49      albertel  358: 	'To '.lc($viewgrade).' a submission, click on the check box next to the student\'s name. Then '."\n".
                    359: 	'click on the '.$viewgrade.' button. To view the submissions for a group of students, click'."\n".
1.45      ng        360: 	' on the check boxes for the group of students.<br />'."\n".
                    361: 	'<input type="hidden" name="command" value="processGroup" />'."\n".
                    362: 	'<input type="button" '."\n".
                    363: 	'onClick="javascript:checkSelect(this.form.stuinfo);" '."\n".
1.49      albertel  364: 	'value="'.$viewgrade.'" />'."\n";
1.45      ng        365:  
1.56    ! matthew   366:     my (undef,undef,$fullname) = &getclasslist($getsec,'0');
1.41      ng        367:     
1.45      ng        368:     $gradeTable.='<table border="0"><tr><td bgcolor="#777777">'.
1.41      ng        369: 	'<table border="0"><tr bgcolor="#e6ffff">'.
1.44      ng        370: 	'<td><b>&nbsp;Select&nbsp;</b></td><td><b>&nbsp;Fullname&nbsp;</b></td>'.
                    371: 	'<td><b>&nbsp;Username&nbsp;</b></td><td><b>&nbsp;Domain&nbsp;</b></td>';
1.41      ng        372:     foreach (sort(@$partlist)) {
1.45      ng        373: 	$gradeTable.='<td><b>&nbsp;Part '.(split(/_/))[0].' Status&nbsp;</b></td>';
1.41      ng        374:     }
1.45      ng        375:     $gradeTable.='</tr>'."\n";
1.41      ng        376: 
1.45      ng        377:     my $ctr = 0;
1.53      albertel  378:     foreach my $student (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
1.41      ng        379: 	my ($uname,$udom) = split(/:/,$student);
1.48      albertel  380: 	my (%status) =&student_gradeStatus($url,$symb,$udom,$uname,$partlist);
1.41      ng        381: 	my $statusflg = '';
                    382: 	foreach (keys(%status)) {
                    383: 	    $statusflg = 1 if ($status{$_} ne 'nothing');
1.43      ng        384: 	    my ($foo,$partid,$foo1) = split(/\./,$_);
1.41      ng        385: 	    if ($status{'resource.'.$partid.'.submitted_by'} ne '') {
                    386: 		$statusflg = '';
1.45      ng        387: 		$gradeTable.='<input type="hidden" name="'.
                    388: 		    $student.':submitted_by" value="'.
                    389: 		    $status{'resource.'.$partid.'.submitted_by'}.'" />';
1.41      ng        390: 	    }
                    391: 	}
                    392: 	next if ($statusflg eq '' && $submitonly eq 'yes');
1.34      ng        393: 
1.45      ng        394: 	$ctr++;
1.41      ng        395: 	if ( $Apache::grades::viewgrades eq 'F' ) {
1.45      ng        396: 	    $gradeTable.='<tr bgcolor="#ffffe6">'.
1.41      ng        397: 		'<td align="center"><input type=checkbox name="stuinfo" value="'.
                    398: 		$student.':'.$$fullname{$student}.'"></td>'."\n".
1.44      ng        399: 		'<td>&nbsp;'.$$fullname{$student}.'&nbsp;</td>'."\n".
1.41      ng        400: 		'<td>&nbsp;'.$uname.'&nbsp;</td>'."\n".
                    401: 		'<td align="middle">&nbsp;'.$udom.'&nbsp;</td>'."\n";
                    402: 	    
                    403: 	    foreach (sort keys(%status)) {
                    404: 		next if (/^resource.*?submitted_by$/);
1.45      ng        405: 		$gradeTable.='<td align="middle">&nbsp;'.$status{$_}.'&nbsp;</td>'."\n";
1.41      ng        406: 	    }
1.45      ng        407: 	    $gradeTable.='</tr>'."\n";
1.41      ng        408: 	}
                    409:     }
1.45      ng        410:     $gradeTable.='</table></td></tr></table>'.
                    411: 	'<input type="button" '.
                    412: 	'onClick="javascript:checkSelect(this.form.stuinfo);" '.
1.50      albertel  413: 	'value="'.$viewgrade.'" /></form>'."\n";
1.45      ng        414:     if ($ctr == 0) {
                    415: 	$gradeTable='<br />&nbsp;<font color="red">'.
                    416: 	    'No submission found for this resource.</font><br />';
1.46      ng        417:     } elsif ($ctr == 1) {
                    418: 	$gradeTable =~ s/type=checkbox/type=checkbox checked/;
1.45      ng        419:     }
1.50      albertel  420:     $gradeTable.=&show_grading_menu_form($symb,$url);
1.45      ng        421:     $request->print($gradeTable);
1.44      ng        422:     return '';
1.10      ng        423: }
                    424: 
1.44      ng        425: #---- Called from the listStudents routine
                    426: #     Displays the submissions for one student or a group of students
1.34      ng        427: sub processGroup {
1.41      ng        428:     my ($request)  = shift;
                    429:     my $ctr        = 0;
                    430:     my @stuchecked = (ref($ENV{'form.stuinfo'}) ? @{$ENV{'form.stuinfo'}}
                    431: 		      : ($ENV{'form.stuinfo'}));
                    432:     my $total      = scalar(@stuchecked)-1;
1.45      ng        433: 
1.41      ng        434:     foreach (@stuchecked) {
                    435: 	my ($uname,$udom,$fullname) = split(/:/);
1.44      ng        436: 	$ENV{'form.student'}        = $uname;
                    437: 	$ENV{'form.userdom'}        = $udom;
                    438: 	$ENV{'form.fullname'}       = $fullname;
1.41      ng        439: 	&submission($request,$ctr,$total);
                    440: 	$ctr++;
                    441:     }
                    442:     return '';
1.35      ng        443: }
1.34      ng        444: 
1.44      ng        445: #------------------------------------------------------------------------------------
                    446: #
                    447: #-------------------------- Next few routines handles grading by student, essentially
                    448: #                           handles essay response type problem/part
                    449: #
                    450: #--- Javascript to handle the submission page functionality ---
                    451: sub sub_page_js {
                    452:     my $request = shift;
                    453:     $request->print(<<SUBJAVASCRIPT);
                    454: <script type="text/javascript" language="javascript">
                    455:   function updateRadio(radioButton,formtextbox,formsel,scores,weight) {
                    456:      var pts = formtextbox.value;
                    457:      var resetbox =false;
                    458:      if (isNaN(pts) || pts < 0) {
                    459:         alert("A number equal or greater than 0 is expected. Entered value = "+pts);
                    460:         for (var i=0; i<radioButton.length; i++) {
                    461:            if (radioButton[i].checked) {
                    462: 	      formtextbox.value = i;
                    463: 	      resetbox = true;
                    464: 	   }
                    465: 	}
                    466: 	if (!resetbox) {
                    467: 	   formtextbox.value = "";
                    468: 	}
                    469: 	return;
                    470:      }
1.13      albertel  471: 
1.44      ng        472:      if (pts > weight) {
                    473:         var resp = confirm("You entered a value ("+pts+
                    474:       		     ") greater than the weight for the part. Accept?");
                    475:         if (resp == false) {
                    476:            formtextbox.value = "";
                    477:            return;
                    478:        }
1.41      ng        479:     }
1.5       albertel  480: 
1.44      ng        481:     for (var i=0; i<radioButton.length; i++) {
                    482: 	radioButton[i].checked=false;
                    483: 	if (pts == i) {
                    484: 	   radioButton[i].checked=true;
1.41      ng        485: 	}
                    486:     }
1.44      ng        487:     updateSelect(formsel);
                    488:     scores.value = "0";
                    489:   }
                    490: 
                    491:   function writeBox(formrad,formsel,pts,scores) {
                    492:     formrad.value = pts;
                    493:     scores.value = "0";
                    494:     updateSelect(formsel,pts);
                    495:     return;
                    496:   }
1.5       albertel  497: 
1.44      ng        498:   function clearRadBox(radioButton,formbox,formsel,scores) {
                    499:     for (var i=0; i<formsel.length; i++) {
                    500: 	if (formsel[i].selected) {
                    501: 	    var selectx=i;
1.41      ng        502: 	}
1.13      albertel  503:     }
1.44      ng        504:     if (selectx == scores.value) { return };
                    505:     formbox.value = "";
                    506:     for (var i=0; i<radioButton.length; i++) {
                    507: 	radioButton[i].checked=false;
1.41      ng        508:     }
1.44      ng        509:     scores.value = selectx;
                    510:   }
1.33      ng        511: 
1.44      ng        512:   function updateSelect(formsel) {
                    513:     formsel[0].selected = true;
                    514:     return;
                    515:   }
                    516: 
1.45      ng        517: //=================== Check that a point is assigned for all the parts  ==============
                    518:   function checksubmit(val,total,parttot) {
                    519:      document.SCORE.gradeOpt.value = val;
                    520:      if (val == "Save & Next") {
                    521: 	for (i=0;i<=total;i++) {
                    522: 	   for (j=0;j<parttot;j++) {
                    523: 	      var partid = eval("document.SCORE.partid"+i+"_"+j+".value");
                    524: 	      var selopt = eval("document.SCORE.GD_SEL"+i+"_"+partid);
                    525: 	      if (selopt[0].selected) {
                    526: 		 var points = eval("document.SCORE.GD_BOX"+i+"_"+partid+".value");
                    527: 		 if (points == "") {
                    528: 		     var name = eval("document.SCORE.name"+i+".value");
1.46      ng        529: 		     alert("Please assign a score for "+name+", part "+partid+".");
1.45      ng        530: 		     return false;
                    531: 		 }
                    532: 	      }
                    533: 
                    534: 	  }
                    535:        }
                    536: 
                    537:      }
                    538:      document.SCORE.submit();
                    539:  }
                    540: 
1.44      ng        541: //===================== Show list of keywords ====================
                    542:   function keywords(keyform) {
                    543:     var keywds = keyform.value;
                    544:     var nret = prompt("Keywords list, separated by a space. Add/delete to list if desired.",keywds);
                    545:     if (nret==null) return;
                    546:     keyform.value = nret;
                    547: 
                    548:     document.SCORE.refresh.value = "on";
                    549:     if (document.SCORE.keywords.value != "") {
                    550: 	document.SCORE.submit();
                    551:     }
                    552:     return;
                    553:   }
                    554: 
                    555: //===================== Script to view submitted by ==================
                    556:   function viewSubmitter(submitter) {
                    557:     document.SCORE.refresh.value = "on";
                    558:     document.SCORE.NCT.value = "1";
                    559:     document.SCORE.unamedom0.value = submitter;
                    560:     document.SCORE.submit();
                    561:     return;
                    562:   }
                    563: 
                    564: //===================== Script to add keyword(s) ==================
                    565:   function getSel() {
                    566:     if (document.getSelection) txt = document.getSelection();
                    567:     else if (document.selection) txt = document.selection.createRange().text;
                    568:     else return;
                    569:     var cleantxt = txt.replace(new RegExp('([\\f\\n\\r\\t\\v ])+', 'g')," ");
                    570:     if (cleantxt=="") {
1.46      ng        571: 	alert("Please select a word or group of words from document and then click this link.");
1.44      ng        572: 	return;
                    573:     }
                    574:     var nret = prompt("Add selection to keyword list? Edit if desired.",cleantxt);
                    575:     if (nret==null) return;
                    576:     var curlist = document.SCORE.keywords.value;
                    577:     document.SCORE.keywords.value = curlist+" "+nret;
                    578:     document.SCORE.refresh.value = "on";
                    579:     if (document.SCORE.keywords.value != "") {
                    580: 	document.SCORE.submit();
                    581:     }
                    582:     return;
                    583:   }
                    584: 
                    585: //====================== Script for composing message ==============
                    586:   function msgCenter(msgform,usrctr,fullname) {
                    587:     var Nmsg  = msgform.savemsgN.value;
                    588:     savedMsgHeader(Nmsg,usrctr,fullname);
                    589:     var subject = msgform.msgsub.value;
                    590:     var rtrchk  = eval("document.SCORE.includemsg"+usrctr);
                    591:     var msgchk = rtrchk.value;
                    592:     re = /msgsub/;
                    593:     var shwsel = "";
                    594:     if (re.test(msgchk)) { shwsel = "checked" }
                    595:     displaySubject(subject,shwsel);
                    596:     for (var i=1; i<=Nmsg; i++) {
                    597: 	var testpt = "savemsg"+i+",";
                    598: 	re = /testpt/;
                    599: 	shwsel = "";
                    600: 	if (re.test(msgchk)) { shwsel = "checked" }
                    601: 	var message = eval("document.SCORE.savemsg"+i+".value");
                    602: 	displaySavedMsg(i,message,shwsel);
                    603:     }
                    604:     newmsg = eval("document.SCORE.newmsg"+usrctr+".value");
                    605:     shwsel = "";
                    606:     re = /newmsg/;
                    607:     if (re.test(msgchk)) { shwsel = "checked" }
                    608:     newMsg(newmsg,shwsel);
                    609:     msgTail(); 
                    610:     return;
                    611:   }
                    612: 
                    613:   function savedMsgHeader(Nmsg,usrctr,fullname) {
                    614:     var height = 30*Nmsg+250;
                    615:     var scrollbar = "no";
                    616:     if (height > 600) {
                    617: 	height = 600;
                    618: 	scrollbar = "yes";
                    619:     }
                    620: /*    if (window.pWin)
                    621: 	window.pWin.close(); */
                    622:     pWin = window.open('', 'MessageCenter', 'toolbar=no,location=no,scrollbars='+scrollbar+',screenx=70,screeny=75,width=600,height='+height);
                    623:     pWin.document.write("<html><head>");
                    624:     pWin.document.write("<title>Message Central</title>");
                    625: 
                    626:     pWin.document.write("<script language=javascript>");
                    627:     pWin.document.write("function checkInput() {");
                    628:     pWin.document.write("  opener.document.SCORE.msgsub.value = document.msgcenter.msgsub.value;");
                    629:     pWin.document.write("  var nmsg   = opener.document.SCORE.savemsgN.value;");
                    630:     pWin.document.write("  var usrctr = document.msgcenter.usrctr.value;");
                    631:     pWin.document.write("  var newval = eval(\\"opener.document.SCORE.newmsg\\"+usrctr);");
                    632:     pWin.document.write("  newval.value = document.msgcenter.newmsg.value;");
                    633: 
                    634:     pWin.document.write("  var msgchk = \\"\\";");
                    635:     pWin.document.write("  if (document.msgcenter.subchk.checked) {");
                    636:     pWin.document.write("     msgchk = \\"msgsub,\\";");
                    637:     pWin.document.write("  }");
                    638:     pWin.document.write(   "for (var i=1; i<=nmsg; i++) {");
                    639:     pWin.document.write("      var opnmsg = eval(\\"opener.document.SCORE.savemsg\\"+i);");
                    640:     pWin.document.write("      var frmmsg = eval(\\"document.msgcenter.msg\\"+i);");
                    641:     pWin.document.write("      opnmsg.value = frmmsg.value;");
                    642:     pWin.document.write("      var chkbox = eval(\\"document.msgcenter.msgn\\"+i);");
                    643:     pWin.document.write("      if (chkbox.checked) {");
                    644:     pWin.document.write("         msgchk += \\"savemsg\\"+i+\\",\\";");
                    645:     pWin.document.write("      }");
                    646:     pWin.document.write("  }");
                    647:     pWin.document.write("  if (document.msgcenter.newmsgchk.checked) {");
                    648:     pWin.document.write("     msgchk += \\"newmsg\\"+usrctr;");
                    649:     pWin.document.write("  }");
                    650:     pWin.document.write("  var includemsg = eval(\\"opener.document.SCORE.includemsg\\"+usrctr);");
                    651:     pWin.document.write("  includemsg.value = msgchk;");
                    652: 
                    653:     pWin.document.write("  self.close()");
                    654: 
                    655:     pWin.document.write("}");
                    656: 
                    657:     pWin.document.write("<");
                    658:     pWin.document.write("/script>");
                    659: 
                    660:     pWin.document.write("</head><body bgcolor=white>");
                    661: 
                    662:     pWin.document.write("<form action=\\"inactive\\" name=\\"msgcenter\\">");
                    663:     pWin.document.write("<input value=\\""+usrctr+"\\" name=\\"usrctr\\" type=\\"hidden\\">");
                    664:     pWin.document.write("<font color=\\"green\\" size=+1>&nbsp;Compose Message for \"+fullname+\"</font><br><br>");
                    665: 
                    666:     pWin.document.write("<table border=0 width=100%><tr><td bgcolor=\\"#777777\\">");
                    667:     pWin.document.write("<table border=0 width=100%><tr bgcolor=\\"#ddffff\\">");
                    668:     pWin.document.write("<td><b>Type</b></td><td><b>Include</b></td><td><b>Message</td></tr>");
                    669: }
                    670:     function displaySubject(msg,shwsel) {
                    671:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                    672:     pWin.document.write("<td>Subject</td>");
                    673:     pWin.document.write("<td align=\\"center\\"><input name=\\"subchk\\" type=\\"checkbox\\"" +shwsel+"></td>");
                    674:     pWin.document.write("<td><input name=\\"msgsub\\" type=\\"text\\" value=\\""+msg+" \\"size=\\"60\\" maxlength=\\"80\\"></td></tr>");
                    675: }
                    676: 
                    677: function displaySavedMsg(ctr,msg,shwsel) {
                    678:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                    679:     pWin.document.write("<td align=\\"center\\">"+ctr+"</td>");
                    680:     pWin.document.write("<td align=\\"center\\"><input name=\\"msgn"+ctr+"\\" type=\\"checkbox\\"" +shwsel+"></td>");
                    681:     pWin.document.write("<td><input name=\\"msg"+ctr+"\\" type=\\"text\\" value=\\""+msg+" \\" size=\\"60\\" maxlength=\\"80\\"></td></tr>");
                    682: }
                    683: 
                    684:   function newMsg(newmsg,shwsel) {
                    685:     pWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                    686:     pWin.document.write("<td align=\\"center\\">New</td>");
                    687:     pWin.document.write("<td align=\\"center\\"><input name=\\"newmsgchk\\" type=\\"checkbox\\"" +shwsel+"></td>");
                    688:     pWin.document.write("<td><input name=\\"newmsg\\" type=\\"text\\" onchange=\\"javascript:this.form.newmsgchk.checked=true\\" value=\\""+newmsg+" \\" size=\\"60\\" maxlength=\\"80\\"></td></tr>");
                    689: }
                    690: 
                    691:   function msgTail() {
                    692:     pWin.document.write("</table>");
                    693:     pWin.document.write("</td></tr></table>&nbsp;");
                    694:     pWin.document.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:checkInput()\\">&nbsp;&nbsp;");
                    695:     pWin.document.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br><br>");
                    696:     pWin.document.write("</form>");
                    697:     pWin.document.write("</body></html>");
                    698: }
                    699: 
                    700: //====================== Script for keyword highlight options ==============
                    701:   function kwhighlight() {
                    702:     var kwclr    = document.SCORE.kwclr.value;
                    703:     var kwsize   = document.SCORE.kwsize.value;
                    704:     var kwstyle  = document.SCORE.kwstyle.value;
                    705:     var redsel = "";
                    706:     var grnsel = "";
                    707:     var blusel = "";
                    708:     if (kwclr=="red")   {var redsel="checked"};
                    709:     if (kwclr=="green") {var grnsel="checked"};
                    710:     if (kwclr=="blue")  {var blusel="checked"};
                    711:     var sznsel = "";
                    712:     var sz1sel = "";
                    713:     var sz2sel = "";
                    714:     if (kwsize=="0")  {var sznsel="checked"};
                    715:     if (kwsize=="+1") {var sz1sel="checked"};
                    716:     if (kwsize=="+2") {var sz2sel="checked"};
                    717:     var synsel = "";
                    718:     var syisel = "";
                    719:     var sybsel = "";
                    720:     if (kwstyle=="")    {var synsel="checked"};
                    721:     if (kwstyle=="<i>") {var syisel="checked"};
                    722:     if (kwstyle=="<b>") {var sybsel="checked"};
                    723:     highlightCentral();
                    724:     highlightbody('red','red',redsel,'0','normal',sznsel,'','normal',synsel);
                    725:     highlightbody('green','green',grnsel,'+1','+1',sz1sel,'<i>','italic',syisel);
                    726:     highlightbody('blue','blue',blusel,'+2','+2',sz2sel,'<b>','bold',sybsel);
                    727:     highlightend();
                    728:     return;
                    729:   }
                    730: 
                    731: 
                    732:   function highlightCentral() {
                    733:     hwdWin = window.open('', 'KeywordHighlightCentral', 'toolbar=no,location=no,scrollbars=no,width=400,height=300,screenx=100,screeny=75');
                    734:     hwdWin.document.write("<html><head>");
                    735:     hwdWin.document.write("<title>Highlight Central</title>");
                    736: 
                    737:     hwdWin.document.write("<script language=javascript>");
                    738:     hwdWin.document.write("function updateChoice(flag) {");
                    739:     hwdWin.document.write("  opener.document.SCORE.kwclr.value = radioSelection(document.hlCenter.kwdclr);");
                    740:     hwdWin.document.write("  opener.document.SCORE.kwsize.value = radioSelection(document.hlCenter.kwdsize);");
                    741:     hwdWin.document.write("  opener.document.SCORE.kwstyle.value = radioSelection(document.hlCenter.kwdstyle);");
                    742:     hwdWin.document.write("  opener.document.SCORE.refresh.value = \\"on\\";");
                    743:     hwdWin.document.write("  if (opener.document.SCORE.keywords.value!=\\"\\"){");
                    744:     hwdWin.document.write("     opener.document.SCORE.submit();");
                    745:     hwdWin.document.write("  }");
                    746:     hwdWin.document.write("  self.close()");
                    747:     hwdWin.document.write("}");
                    748: 
                    749:     hwdWin.document.write("function radioSelection(radioButton) {");
                    750:     hwdWin.document.write("    var selection=null;");
                    751:     hwdWin.document.write("    for (var i=0; i<radioButton.length; i++) {");
                    752:     hwdWin.document.write("        if (radioButton[i].checked) {");
                    753:     hwdWin.document.write("            selection=radioButton[i].value;");
                    754:     hwdWin.document.write("            return selection;");
                    755:     hwdWin.document.write("        }");
                    756:     hwdWin.document.write("    }");
                    757:     hwdWin.document.write("}");
                    758: 
                    759:     hwdWin.document.write("<");
                    760:     hwdWin.document.write("/script>");
                    761: 
                    762:     hwdWin.document.write("</head><body bgcolor=white>");
                    763: 
                    764:     hwdWin.document.write("<form action=\\"inactive\\" name=\\"hlCenter\\">");
                    765:     hwdWin.document.write("<font color=\\"green\\" size=+1>&nbsp;Keyword Highlight Options</font><br><br>");
                    766: 
                    767:     hwdWin.document.write("<table border=0 width=100%><tr><td bgcolor=\\"#777777\\">");
                    768:     hwdWin.document.write("<table border=0 width=100%><tr bgcolor=\\"#ddffff\\">");
                    769:     hwdWin.document.write("<td><b>Text Color</b></td><td><b>Font Size</b></td><td><b>Font Style</td></tr>");
                    770:   }
                    771: 
                    772:   function highlightbody(clrval,clrtxt,clrsel,szval,sztxt,szsel,syval,sytxt,sysel) { 
                    773:     hwdWin.document.write("<tr bgcolor=\\"#ffffdd\\">");
                    774:     hwdWin.document.write("<td align=\\"left\\">");
                    775:     hwdWin.document.write("<input name=\\"kwdclr\\" type=\\"radio\\" value=\\""+clrval+"\\" "+clrsel+">&nbsp;"+clrtxt+"</td>");
                    776:     hwdWin.document.write("<td align=\\"left\\">");
                    777:     hwdWin.document.write("<input name=\\"kwdsize\\" type=\\"radio\\" value=\\""+szval+"\\" "+szsel+">&nbsp;"+sztxt+"</td>");
                    778:     hwdWin.document.write("<td align=\\"left\\">");
                    779:     hwdWin.document.write("<input name=\\"kwdstyle\\" type=\\"radio\\" value=\\""+syval+"\\" "+sysel+">&nbsp;"+sytxt+"</td>");
                    780:     hwdWin.document.write("</tr>");
                    781:   }
                    782: 
                    783:   function highlightend() { 
                    784:     hwdWin.document.write("</table>");
                    785:     hwdWin.document.write("</td></tr></table>&nbsp;");
                    786: //    hwdWin.document.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:updateChoice(0)\\">&nbsp;&nbsp;");
                    787:     hwdWin.document.write("<input type=\\"button\\" value=\\"Save\\" onClick=\\"javascript:updateChoice(1)\\">&nbsp;&nbsp;");
                    788:     hwdWin.document.write("<input type=\\"button\\" value=\\"Cancel\\" onClick=\\"self.close()\\"><br><br>");
                    789:     hwdWin.document.write("</form>");
                    790:     hwdWin.document.write("</body></html>");
                    791:   }
                    792: 
                    793: </script>
                    794: SUBJAVASCRIPT
                    795: }
                    796: 
                    797: 
                    798: # --------------------------- show submissions of a student, option to grade 
                    799: sub submission {
                    800:     my ($request,$counter,$total) = @_;
                    801: 
                    802:     (my $url=$ENV{'form.url'})=~s-^http://($ENV{'SERVER_NAME'}|$ENV{'HTTP_HOST'})--;
                    803: #    if ($ENV{'form.student'} eq '') { &moreinfo($request,'Need student login id'); return ''; }
                    804:     my ($uname,$udom)     = ($ENV{'form.student'},$ENV{'form.userdom'});
                    805:     ($uname,$udom)        = &finduser($uname) if $udom eq '';
                    806:     $ENV{'form.fullname'} = &get_fullname ($uname,$udom) if $ENV{'form.fullname'} eq '';
                    807: #    if ($uname eq '') { &moreinfo($request,'Unable to find student'); return ''; }
1.41      ng        808: 
                    809:     my $symb=($ENV{'form.symb'} ne '' ? $ENV{'form.symb'} : (&Apache::lonnet::symbread($url)));
                    810:     if ($symb eq '') { $request->print("Unable to handle ambiguous references:$url:."); return ''; }
                    811:     my $last = ($ENV{'form.lastSub'} eq 'last' ? 'last' : '');
                    812:     $ENV{'form.vProb'} = $ENV{'form.vProb'} ne '' ? $ENV{'form.vProb'} : 'yes';
                    813: 
                    814:     # header info
                    815:     if ($counter == 0) {
                    816: 	&sub_page_js($request);
1.45      ng        817: 	$request->print('<h3>&nbsp;<font color="#339933">Submission Record</font></h3>'."\n".
                    818: 			'<font size=+1>&nbsp;<b>Resource: </b>'.$url.'</font>'."\n");
1.41      ng        819: 
1.44      ng        820: 	# option to display problem, only once else it cause problems 
                    821:         # with the form later since the problem has a form.
1.41      ng        822: 	if ($ENV{'form.vProb'} eq 'yes') {
                    823: 	    my $rendered=&Apache::loncommon::get_student_view($symb,$uname,$udom,
                    824: 							      $ENV{'request.course.id'});
                    825: 	    my $companswer=&Apache::loncommon::get_student_answers($symb,$uname,$udom,
                    826: 								   $ENV{'request.course.id'});
                    827: 	    my $result.='<table border="0" width="100%"><tr><td bgcolor="#777777">';
                    828: 	    $result.='<table border="0" width="100%"><tr><td bgcolor="#e6ffff">';
1.45      ng        829: 	    $result.='<b> View of the problem - '.$ENV{'form.fullname'}.
1.44      ng        830: 		'</b></td></tr><tr><td bgcolor="#ffffff">'.$rendered.'<br />';
1.41      ng        831: 	    $result.='<b>Correct answer:</b><br />'.$companswer;
                    832: 	    $result.='</td></tr></table>';
                    833: 	    $result.='</td></tr></table><br />';
                    834: 	    $request->print($result);
                    835: 	}
                    836: 	
1.44      ng        837: 	# kwclr is the only variable that is guaranteed to be non blank 
                    838:         # if this subroutine has been called once.
1.41      ng        839: 	my %keyhash = ();
                    840: 	if ($ENV{'form.kwclr'} eq '') {
                    841: 	    %keyhash = &Apache::lonnet::dump('nohist_handgrade',
                    842: 					     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    843: 					     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    844: 
                    845: 	    my $loginuser = $ENV{'user.name'}.':'.$ENV{'user.domain'};
                    846: 	    $ENV{'form.keywords'} = $keyhash{$symb.'_keywords'} ne '' ? $keyhash{$symb.'_keywords'} : '';
                    847: 	    $ENV{'form.kwclr'}    = $keyhash{$loginuser.'_kwclr'} ne '' ? $keyhash{$loginuser.'_kwclr'} : 'red';
                    848: 	    $ENV{'form.kwsize'}   = $keyhash{$loginuser.'_kwsize'} ne '' ? $keyhash{$loginuser.'_kwsize'} : '0';
                    849: 	    $ENV{'form.kwstyle'}  = $keyhash{$loginuser.'_kwstyle'} ne '' ? $keyhash{$loginuser.'_kwstyle'} : '';
                    850: 	    $ENV{'form.msgsub'}   = $keyhash{$symb.'_subject'} ne '' ? 
                    851: 		$keyhash{$symb.'_subject'} : &Apache::lonnet::metadata($url,'title');
                    852: 	    $ENV{'form.savemsgN'} = $keyhash{$symb.'_savemsgN'} ne '' ? $keyhash{$symb.'_savemsgN'} : '0';
1.38      ng        853: 
1.41      ng        854: 	}
1.44      ng        855: 
1.41      ng        856: 	$request->print('<form action="/adm/grades" method="post" name="SCORE">'."\n".
                    857: 			'<input type="hidden" name="command"    value="handgrade" />'."\n".
                    858: 			'<input type="hidden" name="refresh"    value="off" />'."\n".
                    859: 			'<input type="hidden" name="symb"       value="'.$symb.'" />'."\n".
                    860: 			'<input type="hidden" name="url"        value="'.$url.'" />'."\n".
                    861: 			'<input type="hidden" name="showgrading" value="'.$ENV{'form.showgrading'}.'" />'."\n".
                    862: 			'<input type="hidden" name="vProb"      value="'.$ENV{'form.vProb'}.'" />'."\n".
                    863: 			'<input type="hidden" name="lastSub"    value="'.$ENV{'form.lastSub'}.'" />'."\n".
                    864: 			'<input type="hidden" name="section"    value="'.$ENV{'form.section'}.'">'."\n".
                    865: 			'<input type="hidden" name="submitonly" value="'.$ENV{'form.submitonly'}.'">'."\n".
                    866: 			'<input type="hidden" name="response"   value="'.$ENV{'form.response'}.'">'."\n".
                    867: 			'<input type="hidden" name="handgrade"  value="'.$ENV{'form.handgrade'}.'">'."\n".
                    868: 			'<input type="hidden" name="keywords"   value="'.$ENV{'form.keywords'}.'" />'."\n".
                    869: 			'<input type="hidden" name="kwclr"      value="'.$ENV{'form.kwclr'}.'" />'."\n".
                    870: 			'<input type="hidden" name="kwsize"     value="'.$ENV{'form.kwsize'}.'" />'."\n".
                    871: 			'<input type="hidden" name="kwstyle"    value="'.$ENV{'form.kwstyle'}.'" />'."\n".
                    872: 			'<input type="hidden" name="msgsub"     value="'.$ENV{'form.msgsub'}.'" />'."\n".
                    873: 			'<input type="hidden" name="savemsgN"   value="'.$ENV{'form.savemsgN'}.'" />'."\n".
                    874: 			'<input type="hidden" name="NCT"'.
                    875: 			' value="'.($ENV{'form.NTSTU'} ne '' ? $ENV{'form.NTSTU'} : $total+1).'" />'."\n");
                    876: 	
                    877: 	my ($cts,$prnmsg) = (1,'');
                    878: 	while ($cts <= $ENV{'form.savemsgN'}) {
                    879: 	    $prnmsg.='<input type="hidden" name="savemsg'.$cts.'" value="'.
                    880: 		($keyhash{$symb.'_savemsg'.$cts} eq '' ? $ENV{'form.savemsg'.$cts} : $keyhash{$symb.'_savemsg'.$cts}).
                    881: 		'" />'."\n";
                    882: 	    $cts++;
                    883: 	}
                    884: 	$request->print($prnmsg);
1.32      ng        885: 
1.41      ng        886: 	if ($ENV{'form.handgrade'} eq 'yes' && $ENV{'form.showgrading'} eq 'yes') {
                    887: 	    $request->print(<<KEYWORDS);
1.38      ng        888: &nbsp;<b>Keyword Options:</b>&nbsp;
                    889: <a href="javascript:keywords(document.SCORE.keywords)"; TARGET=_self>List</a>&nbsp; &nbsp;
                    890: <a href="#" onMouseDown="javascript:getSel(); return false"
                    891:  CLASS="page">Paste Selection to List</a>&nbsp; &nbsp;
                    892: <a href="javascript:kwhighlight()"; TARGET=_self>Highlight Attribute</a><br /><br />
                    893: KEYWORDS
1.41      ng        894:         }
                    895:     }
1.44      ng        896: 
1.41      ng        897:     my %record = &Apache::lonnet::restore($symb,$ENV{'request.course.id'},$udom,$uname);
                    898:     my ($partlist,$handgrade) = &response_type($url);
                    899: 
1.44      ng        900:     # Display student info
1.41      ng        901:     $request->print(($counter == 0 ? '' : '<br />'));
1.45      ng        902:     my $result='<table border="0" width=100%><tr><td bgcolor="#777777">'."\n".
                    903: 	'<table border="0" width=100%><tr bgcolor="#edffff"><td>'."\n";
1.44      ng        904: 
                    905:     $result.='<b>Fullname: </b>'.$ENV{'form.fullname'}.
                    906: 	'<font color="#999999">&nbsp; &nbsp;Username: '.$uname.'</font>'.
1.45      ng        907: 	'<font color="#999999">&nbsp; &nbsp;Domain: '.$udom.'</font><br />'."\n";
                    908:     $result.='<input type="hidden" name="name'.$counter.
                    909: 	'" value="'.$ENV{'form.fullname'}.'" />'."\n";
1.41      ng        910: 
1.44      ng        911:     # If this is handgraded, then check for collaborators
1.45      ng        912:     my @col_fullnames;
1.56    ! matthew   913:     my ($classlist,$fullname);
1.41      ng        914:     if ($ENV{'form.handgrade'} eq 'yes') {
                    915: 	my @col_list;
1.56    ! matthew   916: 	($classlist,undef,$fullname) = &getclasslist('all','0');
1.41      ng        917: 	for (keys (%$handgrade)) {
1.44      ng        918: 	    my $ncol = &Apache::lonnet::EXT('resource.'.$_.
                    919: 					    '.maxcollaborators',$symb,$udom,$uname);
1.41      ng        920: 	    if ($ncol > 0) {
                    921: 		s/\_/\./g;
                    922: 		if ($record{'resource.'.$_.'.collaborators'} ne '') {
1.44      ng        923: 		    my (@collaborators) = split(/,?\s+/,
                    924: 						$record{'resource.'.$_.'.collaborators'});
1.41      ng        925: 		    my (@badcollaborators);
                    926: 		    if (scalar(@collaborators) != 0) {
1.44      ng        927: 			$result.='<b>Collaborators: </b>';
1.41      ng        928: 			foreach my $collaborator (@collaborators) {
                    929: 			    $collaborator = $collaborator =~ /\@|:/ ? 
                    930: 				(split(/@|:/,$collaborator))[0] : $collaborator;
                    931: 			    next if ($collaborator eq $uname);
                    932: 			    if (!grep /^$collaborator:/i,keys %$classlist) {
                    933: 				push @badcollaborators,$collaborator;
                    934: 				next;
                    935: 			    }
                    936: 			    push @col_list, $collaborator;
1.45      ng        937: 			    my ($lastname,$givenn) = split(/,/,$$fullname{$collaborator.':'.$udom});
                    938: 			    push @col_fullnames, $givenn.' '.$lastname;
1.44      ng        939: 			    $result.=$$fullname{$collaborator.':'.$udom}.'&nbsp; &nbsp; &nbsp;';
1.41      ng        940: 			}
1.44      ng        941: 			$result.='<br />'."\n";
                    942: 			$result.='<table border="0"><tr bgcolor="#ffbbbb"><td>'.
                    943: 			    'This student has submitted '.
                    944: 			    (scalar (@badcollaborators) > 1 ? '' : 'an').
1.41      ng        945: 			    ' invalid collaborator'.(scalar (@badcollaborators) > 1 ? 's. ' : '. ').
1.44      ng        946: 			    (join ', ',@badcollaborators).'</td></tr></table>' 
1.41      ng        947: 			    if (scalar(@badcollaborators) > 0);
                    948: 
1.44      ng        949: 			$result.='<table border="0"><tr bgcolor="#ffbbbb"><td>'.
1.41      ng        950: 			    'This student has submitted too many collaborators. Maximum is '.
1.44      ng        951: 			    $ncol.'.</td></tr></table>' if (scalar(@collaborators) > $ncol);
1.41      ng        952: 			$result.='<input type="hidden" name="collaborator'.$counter.
                    953: 			    '" value="'.(join ':',@col_list).'" />'."\n";
                    954: 		    }
                    955: 		}
                    956: 	    }
                    957: 	}
                    958:     }
1.44      ng        959:     $request->print($result."\n");
1.33      ng        960: 
1.44      ng        961:     # print student answer/submission
                    962:     # Options are (1) Handgaded submission only
                    963:     #             (2) Last submission, includes submission that is not handgraded 
                    964:     #                  (for multi-response type part)
                    965:     #             (3) Last submission plus the parts info
                    966:     #             (4) The whole record for this student
1.41      ng        967:     if ($ENV{'form.lastSub'} =~ /^(lastonly|hdgrade)$/) {
                    968: 	if ($ENV{'form.'.$uname.':'.$udom.':submitted_by'}) {
1.44      ng        969: 	    my $submitby=''.
1.41      ng        970: 		'<b>Collaborative submission by: </b>'.
1.44      ng        971: 		'<a href="javascript:viewSubmitter(\''.
                    972: 		$ENV{'form.'.$uname.':'.$udom.':submitted_by'}.
1.41      ng        973: 		'\')"; TARGET=_self>'.
                    974: 		$$fullname{$ENV{'form.'.$uname.':'.$udom.':submitted_by'}}.'</a>';
                    975: 	    $request->print($submitby);
                    976: 	} else {
1.44      ng        977: 	    my ($string,$timestamp)=
1.46      ng        978: 		&get_last_submission (%record);
1.44      ng        979: 	    my $lastsubonly.=''.
                    980: 		($$timestamp eq '' ? '' : '<b>Date Submitted:</b> '.
                    981: 		 $$timestamp).'';
1.41      ng        982: 	    if ($$timestamp eq '') {
1.45      ng        983: 		$lastsubonly.='<tr><td bgcolor="#ffffe6">'.$$string[0].'</td></tr>'."\n";
1.41      ng        984: 	    } else {
                    985: 		for my $part (sort keys(%$handgrade)) {
                    986: 		    foreach (@$string) {
                    987: 			my ($partid,$respid) = /^resource\.(\d+)\.(\d+)\.submission/;
                    988: 			if ($part eq ($partid.'_'.$respid)) {
                    989: 			    my ($ressub,$subval) = split(/:/,$_,2);
1.44      ng        990: 			    $lastsubonly.='<tr><td bgcolor="#ffffe6"><b>Part '.
                    991: 				$partid.'</b> <font color="#999999">( ID '.$respid.
                    992: 				' )</font>&nbsp; &nbsp;<b>Answer: </b>'.
1.45      ng        993: 				&keywords_highlight($subval).'</td></tr>'."\n"
1.41      ng        994: 				if ($ENV{'form.lastSub'} eq 'lastonly' || 
1.44      ng        995: 				    ($ENV{'form.lastSub'} eq 'hdgrade' && 
                    996: 				     $$handgrade{$part} =~ /:yes$/));
1.41      ng        997: 			}
                    998: 		    }
                    999: 		}
                   1000: 	    }
1.45      ng       1001: 	    $lastsubonly.='</td></tr>'."\n";
1.41      ng       1002: 	    $request->print($lastsubonly);
                   1003: 	}
                   1004:     } else {
                   1005: 	$request->print(&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
1.44      ng       1006: 								 $ENV{'request.course.id'},
                   1007: 								 $last,'.submission',
                   1008: 								 'Apache::grades::keywords_highlight'));
1.41      ng       1009:     }
                   1010:     
1.44      ng       1011:     # return if view submission with no grading option
1.41      ng       1012:     if ($ENV{'form.showgrading'} eq '') {
1.45      ng       1013: 	$request->print('</td></tr></table></td></tr></table></form>'."\n");
1.41      ng       1014: 	return;
                   1015:     }
1.33      ng       1016: 
1.44      ng       1017:     # Grading options
1.41      ng       1018:     $result='<input type="hidden" name="newmsg'.$counter.'" value="" />'."\n".
                   1019: 	'<input type="hidden" name="includemsg'.$counter.'" value="" />'."\n".
1.45      ng       1020: 	'<input type="hidden" name="unamedom'.$counter.'" value="'.$uname.':'
                   1021: 	.$udom.'" />'."\n";
                   1022:     my ($lastname,$givenn) = split(/,/,$ENV{'form.fullname'});
                   1023:     my $msgfor = $givenn.' '.$lastname;
                   1024:     if (scalar(@col_fullnames) > 0) {
                   1025: 	my $lastone = pop @col_fullnames;
                   1026: 	$msgfor .= ', '.(join ', ',@col_fullnames).' and '.$lastone.'.';
                   1027:     }
                   1028:     $result.='<tr><td bgcolor="#ffffff">'."\n".
                   1029: 	'&nbsp;<a href="javascript:msgCenter(document.SCORE,'.$counter.
                   1030: 	',\''.$msgfor.'\')"; TARGET=_self>'.
                   1031: 	'Compose Message to student'.(scalar(@col_fullnames) >= 1 ? 's' : '').'</a>'.
1.44      ng       1032: 	'<br />&nbsp;(Message will be sent when you click on Save & Next below.)'."\n" 
                   1033: 	if ($ENV{'form.handgrade'} eq 'yes');
1.41      ng       1034:     $request->print($result);
                   1035: 
                   1036:     my %seen = ();
                   1037:     my @partlist;
                   1038:     for (sort keys(%$handgrade)) {
                   1039: 	my ($partid,$respid) = split(/_/);
                   1040: 	next if ($seen{$partid} > 0);
                   1041: 	$seen{$partid}++;
                   1042: 	next if ($$handgrade{$_} =~ /:no$/);
                   1043: 	push @partlist,$partid;
                   1044: 	my $wgt    = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb,$udom,$uname);
1.44      ng       1045: 	my $wgtmsg = ($wgt > 0 ? '(problem weight)' : 
                   1046: 		      '<font color="red">problem weight assigned by computer</font>');
1.41      ng       1047: 	$wgt       = ($wgt > 0 ? $wgt : '1');
                   1048: 	my $score  = ($record{'resource.'.$partid.'.awarded'} eq '' ?
                   1049: 		      '' : $record{'resource.'.$partid.'.awarded'}*$wgt);
                   1050: 	$result='<input type="hidden" name="WGT'.$counter.'_'.$partid.'" value="'.$wgt.'" />';
1.44      ng       1051: 	$result.='<table border="0"><tr><td><b>Part </b>'.$partid.' <b>Points: </b></td><td>';
1.41      ng       1052: 
                   1053: 	my $ctr = 0;
                   1054: 	$result.='<table border="0"><tr>';  # display radio buttons in a nice table 10 across
                   1055: 	while ($ctr<=$wgt) {
                   1056: 	    $result.= '<td><input type="radio" name="RADVAL'.$counter.'_'.$partid.'" '.
1.43      ng       1057: 		'onclick="javascript:writeBox(this.form.GD_BOX'.$counter.'_'.$partid.
                   1058: 		',this.form.GD_SEL'.$counter.'_'.$partid.','.$ctr.
1.41      ng       1059: 		',this.form.stores'.$counter.'_'.$partid.')" '.
                   1060: 		($score eq $ctr ? 'checked':'').' /> '.$ctr."</td>\n";
                   1061: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
                   1062: 	    $ctr++;
                   1063: 	}
                   1064: 	$result.='</tr></table>';
1.39      ng       1065: 
1.41      ng       1066: 	$result.='</td><td>&nbsp;<b>or</b>&nbsp;</td>';
1.43      ng       1067: 	$result.='<td><input type="text" name="GD_BOX'.$counter.'_'.$partid.'"'.
1.41      ng       1068: 	    ($score ne ''? ' value = "'.$score.'"':'').' size="4" '.
                   1069: 	    'onChange="javascript:updateRadio(this.form.RADVAL'.$counter.'_'.$partid.
1.43      ng       1070: 	    ',this.form.GD_BOX'.$counter.'_'.$partid.
                   1071: 	    ',this.form.GD_SEL'.$counter.'_'.$partid.
1.44      ng       1072: 	    ',this.form.stores'.$counter.'_'.$partid.
                   1073: 	    ','.$wgt.')" /></td>'."\n";
1.41      ng       1074: 	$result.='<td>/'.$wgt.' '.$wgtmsg.' </td><td>';
                   1075: 
1.43      ng       1076: 	$result.='<select name="GD_SEL'.$counter.'_'.$partid.'" '.
1.41      ng       1077: 	    'onChange="javascript:clearRadBox(this.form.RADVAL'.$counter.'_'.$partid.
1.43      ng       1078: 	    ',this.form.GD_BOX'.$counter.'_'.$partid.
                   1079: 	    ',this.form.GD_SEL'.$counter.'_'.$partid.
1.41      ng       1080: 	    ',this.form.stores'.$counter.'_'.$partid.')" />'."\n".
                   1081: 	    '<option selected="on"> </option>'.
                   1082: 	    '<option>excused</option></select>'."&nbsp&nbsp\n";
                   1083: 	$result.='<input type="hidden" name="stores'.$counter.'_'.$partid.'" value="0" />';
1.45      ng       1084: 	$result.='</td></tr></table>'."\n";
1.41      ng       1085: 	$request->print($result);
                   1086:     }
1.45      ng       1087:     $result='<input type="hidden" name="partlist'.$counter.
                   1088: 	'" value="'.(join ":",@partlist).'" />'."\n";
                   1089:     my $ctr = 0;
                   1090:     while ($ctr < scalar(@partlist)) {
                   1091: 	$result.='<input type="hidden" name="partid'.$counter.'_'.$ctr.'" value="'.
                   1092: 	    $partlist[$ctr].'" />'."\n";
                   1093: 	$ctr++;
                   1094:     }
                   1095:     $request->print($result.'</td></tr></table></td></tr></table>'."\n");
1.41      ng       1096: 
                   1097:     # print end of form
                   1098:     if ($counter == $total) {
1.45      ng       1099: 	my $endform='<table border="0"><tr><td>'.
                   1100: 	    '<input type="hidden" name="gradeOpt" value="" />'."\n";
                   1101: 	if ($ENV{'form.handgrade'} eq 'yes') {
                   1102: 	    $endform.='<input type="button" value="Save & Next" '.
                   1103: 		'onClick="javascript:checksubmit(\'Save & Next\','.
                   1104: 		$total.','.scalar(@partlist).');" TARGET=_self> &nbsp;'."\n";
                   1105: 	    my $ntstu ='<select name="NTSTU">'.
                   1106: 		'<option>1</option><option>2</option>'.
                   1107: 		'<option>3</option><option>5</option>'.
                   1108: 		'<option>7</option><option>10</option></select>'."\n";
                   1109: 	    my $nsel = ($ENV{'form.NTSTU'} ne '' ? $ENV{'form.NTSTU'} : '1');
                   1110: 	    $ntstu =~ s/<option>$nsel</<option selected="on">$nsel</;
                   1111: 	    $endform.=$ntstu.'student(s) &nbsp;&nbsp;';
                   1112: 	} else {
                   1113: 	    $endform.='<input type="hidden" name="NTSTU" value="1" />'."\n";
                   1114: 	}
                   1115: 	$endform.='<input type="button" value="Next" '.
                   1116: 	    'onClick="javascript:checksubmit(\'Next\');" TARGET=_self> &nbsp;'."\n".
                   1117: 	    '<input type="button" value="Previous" '.
                   1118: 	    'onClick="javascript:checksubmit(\'Previous\');" TARGET=_self> &nbsp;';
                   1119: 	$endform.='(Next and Previous do not save the scores.)'."\n" 
                   1120: 	    if ($ENV{'form.handgrade'} eq 'yes');
                   1121: 	$endform.='</td><tr></table></form>';
1.50      albertel 1122: 	$endform.=&show_grading_menu_form($symb,$url);
1.41      ng       1123: 	$request->print($endform);
                   1124:     }
                   1125:     return '';
1.38      ng       1126: }
                   1127: 
1.44      ng       1128: #--- Retrieve the last submission for all the parts
1.38      ng       1129: sub get_last_submission {
1.46      ng       1130:     my (%returnhash)=@_;
                   1131:     my (@string,$timestamp);
                   1132:     if ($returnhash{'version'}) {
                   1133: 	my %lasthash=();
                   1134: 	my ($version);
                   1135: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
                   1136: 	    foreach (sort(split(/\:/,$returnhash{$version.':keys'}))) {
                   1137: 		$lasthash{$_}=$returnhash{$version.':'.$_};
                   1138: 		if ($returnhash{$version.':'.$_} =~ /(SUBMITTED|DRAFT)$/) {
                   1139: 		   $timestamp = scalar(localtime($returnhash{$version.':timestamp'}));
                   1140: 	       } 
                   1141: 	    }
                   1142: 	}
                   1143: 	foreach ((keys %lasthash)) {
                   1144: 	    if ($_ =~ /\.submission$/) {
                   1145: 		my ($partid,$foo) = split(/submission$/,$_);
                   1146: 		my $draft  = $lasthash{$partid.'awarddetail'} eq 'DRAFT' ?
                   1147: 		    '<font color="red">Draft Copy</font> ' : '';
                   1148: 		push @string, (join(':',$_,$draft.$lasthash{$_}));
1.41      ng       1149: 	    }
                   1150: 	}
                   1151:     }
1.46      ng       1152:     @string = $string[0] eq '' ? 'Nothing submitted - no attempts.' : @string;
                   1153:     return \@string,\$timestamp;
1.38      ng       1154: }
1.35      ng       1155: 
1.44      ng       1156: #--- High light keywords, with style choosen by user.
1.38      ng       1157: sub keywords_highlight {
1.44      ng       1158:     my $string    = shift;
                   1159:     my $size      = $ENV{'form.kwsize'} eq '0' ? '' : 'size='.$ENV{'form.kwsize'};
                   1160:     my $styleon   = $ENV{'form.kwstyle'} eq ''  ? '' : $ENV{'form.kwstyle'};
1.41      ng       1161:     (my $styleoff = $styleon) =~ s/\</\<\//;
1.44      ng       1162:     my @keylist   = split(/[,\s+]/,$ENV{'form.keywords'});
1.41      ng       1163:     foreach (@keylist) {
                   1164: 	$string =~ s/\b$_(\b|\.)/\<font color\=$ENV{'form.kwclr'} $size\>$styleon$_$styleoff\<\/font\>/gi;
                   1165:     }
                   1166:     return $string;
1.38      ng       1167: }
1.36      ng       1168: 
1.44      ng       1169: #--- Called from submission routine
1.38      ng       1170: sub processHandGrade {
1.41      ng       1171:     my ($request) = shift;
                   1172:     my $url    = $ENV{'form.url'};
                   1173:     my $symb   = $ENV{'form.symb'};
                   1174:     my $button = $ENV{'form.gradeOpt'};
                   1175:     my $ngrade = $ENV{'form.NCT'};
                   1176:     my $ntstu  = $ENV{'form.NTSTU'};
                   1177: 
1.44      ng       1178:     if ($button eq 'Save & Next') {
                   1179: 	my $ctr = 0;
                   1180: 	while ($ctr < $ngrade) {
                   1181: 	    my ($uname,$udom) = split(/:/,$ENV{'form.unamedom'.$ctr});
1.45      ng       1182: 	    my ($errorflag) = &saveHandGrade($request,$url,$symb,$uname,$udom,$ctr);
1.44      ng       1183: 
                   1184: 	    my $includemsg = $ENV{'form.includemsg'.$ctr};
                   1185: 	    my ($subject,$message,$msgstatus) = ('','','');
                   1186: 	    if ($includemsg =~ /savemsg|new$ctr/) {
                   1187: 		$subject = $ENV{'form.msgsub'} if ($includemsg =~ /^msgsub/);
                   1188: 		my (@msgnum) = split(/,/,$includemsg);
                   1189: 		foreach (@msgnum) {
                   1190: 		    $message.=$ENV{'form.'.$_} if ($_ =~ /savemsg|newmsg/ && $_ ne '');
                   1191: 		}
                   1192: 		$message =~ s/\s+/ /g;
                   1193: 		$msgstatus = &Apache::lonmsg::user_normal_msg ($uname,$udom,
                   1194: 							       $ENV{'form.msgsub'},$message);
                   1195: 	    }
                   1196: 	    if ($ENV{'form.collaborator'.$ctr}) {
                   1197: 		my (@collaborators) = split(/:/,$ENV{'form.collaborator'.$ctr});
                   1198: 		foreach (@collaborators) {
                   1199: 		    &saveHandGrade($request,$url,$symb,$_,$udom,$ctr,
                   1200: 				   $ENV{'form.unamedom'.$ctr});
                   1201: 		    if ($message ne '') {
                   1202: 			$msgstatus = &Apache::lonmsg::user_normal_msg ($_,$udom,
                   1203: 								       $ENV{'form.msgsub'},
                   1204: 								       $message);
                   1205: 		    }
                   1206: 		}
                   1207: 	    }
                   1208: 	    $ctr++;
                   1209: 	}
                   1210:     }
                   1211: 
                   1212:     # Keywords sorted in alphabatical order
1.41      ng       1213:     my $loginuser = $ENV{'user.name'}.':'.$ENV{'user.domain'};
                   1214:     my %keyhash = ();
                   1215:     $ENV{'form.keywords'}           =~ s/,\s{0,}|\s+/ /g;
                   1216:     $ENV{'form.keywords'}           =~ s/^\s+|\s+$//;
1.44      ng       1217:     my (@keywords) = sort(split(/\s+/,$ENV{'form.keywords'}));
                   1218:     $ENV{'form.keywords'} = join(' ',@keywords);
1.41      ng       1219:     $keyhash{$symb.'_keywords'}     = $ENV{'form.keywords'};
                   1220:     $keyhash{$symb.'_subject'}      = $ENV{'form.msgsub'};
                   1221:     $keyhash{$loginuser.'_kwclr'}   = $ENV{'form.kwclr'};
                   1222:     $keyhash{$loginuser.'_kwsize'}  = $ENV{'form.kwsize'};
                   1223:     $keyhash{$loginuser.'_kwstyle'} = $ENV{'form.kwstyle'};
                   1224: 
1.44      ng       1225:     # message center - Order of message gets changed. Blank line is eliminated.
                   1226:     # New messages are saved in ENV for the next student.
                   1227:     # All messages are saved in nohist_handgrade.db
1.41      ng       1228:     my ($ctr,$idx) = (1,1);
                   1229:     while ($ctr <= $ENV{'form.savemsgN'}) {
                   1230: 	if ($ENV{'form.savemsg'.$ctr} ne '') {
                   1231: 	    $keyhash{$symb.'_savemsg'.$idx} = $ENV{'form.savemsg'.$ctr};
                   1232: 	    $idx++;
                   1233: 	}
                   1234: 	$ctr++;
                   1235:     }
                   1236:     $ctr = 0;
                   1237:     while ($ctr < $ngrade) {
                   1238: 	if ($ENV{'form.newmsg'.$ctr} ne '') {
                   1239: 	    $keyhash{$symb.'_savemsg'.$idx} = $ENV{'form.newmsg'.$ctr};
                   1240: 	    $ENV{'form.savemsg'.$idx} = $ENV{'form.newmsg'.$ctr};
                   1241: 	    $idx++;
                   1242: 	}
                   1243: 	$ctr++;
                   1244:     }
                   1245:     $ENV{'form.savemsgN'} = --$idx;
                   1246:     $keyhash{$symb.'_savemsgN'} = $ENV{'form.savemsgN'};
                   1247:     my $putresult = &Apache::lonnet::put
                   1248: 	('nohist_handgrade',\%keyhash,
                   1249: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1250: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                   1251: 
1.44      ng       1252:     # Called by Save & Refresh from Highlight Attribute Window
1.41      ng       1253:     if ($ENV{'form.refresh'} eq 'on') {
                   1254: 	my $ctr = 0;
                   1255: 	$ENV{'form.NTSTU'}=$ngrade;
                   1256: 	while ($ctr < $ngrade) {
1.44      ng       1257: 	    ($ENV{'form.student'},$ENV{'form.userdom'}) = split(/:/,$ENV{'form.unamedom'.$ctr});
1.41      ng       1258: 	    &submission($request,$ctr,$ngrade-1);
                   1259: 	    $ctr++;
                   1260: 	}
                   1261: 	return '';
                   1262:     }
1.36      ng       1263: 
1.44      ng       1264:     # Get the next/previous one or group of students
1.41      ng       1265:     my $firststu = $ENV{'form.unamedom0'};
                   1266:     my $laststu = $ENV{'form.unamedom'.($ngrade-1)};
                   1267:     $ctr = 2;
                   1268:     while ($laststu eq '') {
                   1269: 	$laststu  = $ENV{'form.unamedom'.($ngrade-$ctr)};
                   1270: 	$ctr++;
                   1271: 	$laststu = $firststu if ($ctr > $ngrade);
                   1272:     }
1.44      ng       1273: 
1.56    ! matthew  1274:     my (undef,undef,$fullname) = &getclasslist($ENV{'form.section'},'0');
1.41      ng       1275:     my (@parsedlist,@nextlist);
                   1276:     my ($nextflg) = 0;
1.53      albertel 1277:     foreach (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
1.41      ng       1278: 	if ($nextflg == 1 && $button =~ /Next$/) {
                   1279: 	    push @parsedlist,$_;
                   1280: 	}
                   1281: 	$nextflg = 1 if ($_ eq $laststu);
                   1282: 	if ($button eq 'Previous') {
                   1283: 	    last if ($_ eq $firststu);
                   1284: 	    push @parsedlist,$_;
                   1285: 	}
                   1286:     }
                   1287:     $ctr = 0;
                   1288:     my ($partlist,$handgrade) = &response_type($ENV{'form.url'});
                   1289:     @parsedlist = reverse @parsedlist if ($button eq 'Previous');
                   1290:     foreach my $student (@parsedlist) {
                   1291: 	my ($uname,$udom) = split(/:/,$student);
                   1292: 	if ($ENV{'form.submitonly'} eq 'yes') {
1.44      ng       1293: 	    my (%status) = &student_gradeStatus($ENV{'form.url'},$symb,$udom,$uname,$partlist) ;
1.41      ng       1294: 	    my $statusflg = '';
                   1295: 	    foreach (keys(%status)) {
                   1296: 		$statusflg = 1 if ($status{$_} ne 'nothing');
1.44      ng       1297: 		my ($foo,$partid,$foo1) = split(/\./);
1.41      ng       1298: 		$statusflg = '' if ($status{'resource.'.$partid.'.submitted_by'} ne '');
                   1299: 	    }
                   1300: 	    next if ($statusflg eq '');
                   1301: 	}
                   1302: 	push @nextlist,$student if ($ctr < $ntstu);
                   1303: 	$ctr++;
                   1304:     }
1.36      ng       1305: 
1.41      ng       1306:     $ctr = 0;
                   1307:     my $total = scalar(@nextlist)-1;
1.39      ng       1308: 
1.41      ng       1309:     foreach (sort @nextlist) {
                   1310: 	my ($uname,$udom,$submitter) = split(/:/);
1.44      ng       1311: 	$ENV{'form.student'}  = $uname;
                   1312: 	$ENV{'form.userdom'}  = $udom;
1.41      ng       1313: 	$ENV{'form.fullname'} = $$fullname{$_};
                   1314: #	$ENV{'form.'.$_.':submitted_by'} = $submitter;
                   1315: #	print "submitter=$ENV{'form.'.$_.':submitted_by'}= $submitter:<br>";
                   1316: 	&submission($request,$ctr,$total);
                   1317: 	$ctr++;
                   1318:     }
                   1319:     if ($total < 0) {
                   1320: 	my $the_end = '<h3><font color="red">LON-CAPA User Message</font></h3><br />'."\n";
                   1321: 	$the_end.='<b>Message: </b> No more students for this section or class.<br /><br />'."\n";
                   1322: 	$the_end.='Click on the button below to return to the grading menu.<br /><br />'."\n";
                   1323: 	$the_end.=&show_grading_menu_form ($symb,$url);
                   1324: 	$request->print($the_end);
                   1325:     }
                   1326:     return '';
1.38      ng       1327: }
1.36      ng       1328: 
1.44      ng       1329: #---- Save the score and award for each student, if changed
1.38      ng       1330: sub saveHandGrade {
1.41      ng       1331:     my ($request,$url,$symb,$stuname,$domain,$newflg,$submitter) = @_;
1.44      ng       1332:     my %record=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},$domain,$stuname);
1.41      ng       1333:     my %newrecord;
                   1334:     foreach (split(/:/,$ENV{'form.partlist'.$newflg})) {
1.43      ng       1335: 	if ($ENV{'form.GD_SEL'.$newflg.'_'.$_} eq 'excused') {
1.44      ng       1336: 	    $newrecord{'resource.'.$_.'.solved'} = 'excused' 
                   1337: 		if ($record{'resource.'.$_.'.solved'} ne 'excused');
1.41      ng       1338: 	} else {
1.44      ng       1339: 	    my $pts = ($ENV{'form.GD_BOX'.$newflg.'_'.$_} ne '' ? 
                   1340: 		       $ENV{'form.GD_BOX'.$newflg.'_'.$_} : 
                   1341: 		       $ENV{'form.RADVAL'.$newflg.'_'.$_});
                   1342: 	    my $wgt = $ENV{'form.WGT'.$newflg.'_'.$_} eq '' ? 1 : 
                   1343: 		$ENV{'form.WGT'.$newflg.'_'.$_};
1.41      ng       1344: 	    my $partial= $pts/$wgt;
1.44      ng       1345: 	    $newrecord{'resource.'.$_.'.awarded'}  = $partial 
                   1346: 		if ($record{'resource.'.$_.'.awarded'} ne $partial);
                   1347: 	    my $reckey = 'resource.'.$_.'.solved';
1.41      ng       1348: 	    if ($partial == 0) {
1.44      ng       1349: 		$newrecord{$reckey} = 'incorrect_by_override' 
                   1350: 		    if ($record{$reckey} ne 'incorrect_by_override');
1.41      ng       1351: 	    } else {
1.44      ng       1352: 		$newrecord{$reckey} = 'correct_by_override' 
                   1353: 		    if ($record{$reckey} ne 'correct_by_override');
1.41      ng       1354: 	    }
1.44      ng       1355: 	    $newrecord{'resource.'.$_.'.submitted_by'} = $submitter 
                   1356: 		if ($submitter && ($record{'resource.'.$_.'.submitted_by'} ne $submitter));
1.41      ng       1357: 	}
                   1358:     }
1.44      ng       1359: 
                   1360:     if (scalar(keys(%newrecord)) > 0) {
1.41      ng       1361: 	$newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
1.44      ng       1362: 	&Apache::lonnet::cstore(\%newrecord,$symb,
                   1363: 				$ENV{'request.course.id'},$domain,$stuname);
1.41      ng       1364:     }
                   1365:     return '';
1.36      ng       1366: }
1.38      ng       1367: 
1.44      ng       1368: #--------------------------------------------------------------------------------------
                   1369: #
                   1370: #-------------------------- Next few routines handles grading by section or whole class
                   1371: #
                   1372: #--- Javascript to handle grading by section or whole class
1.42      ng       1373: sub viewgrades_js {
                   1374:     my ($request) = shift;
                   1375: 
1.41      ng       1376:     $request->print(<<VIEWJAVASCRIPT);
                   1377: <script type="text/javascript" language="javascript">
1.45      ng       1378:    function writePoint(partid,weight,point) {
1.42      ng       1379: 	var radioButton = eval("document.classgrade.RADVAL_"+partid);
                   1380: 	var textbox = eval("document.classgrade.TEXTVAL_"+partid);
                   1381: 	if (point == "textval") {
                   1382: 	    var point = eval("document.classgrade.TEXTVAL_"+partid+".value");
                   1383: 	    if (isNaN(point) || point < 0) {
                   1384: 		alert("A number equal or greater than 0 is expected. Entered value = "+point);
                   1385: 		var resetbox = false;
                   1386: 		for (var i=0; i<radioButton.length; i++) {
                   1387: 		    if (radioButton[i].checked) {
                   1388: 			textbox.value = i;
                   1389: 			resetbox = true;
                   1390: 		    }
                   1391: 		}
                   1392: 		if (!resetbox) {
                   1393: 		    textbox.value = "";
                   1394: 		}
                   1395: 		return;
                   1396: 	    }
1.44      ng       1397: 	    if (point > weight) {
                   1398: 		var resp = confirm("You entered a value ("+point+
                   1399: 				   ") greater than the weight for the part. Accept?");
                   1400: 		if (resp == false) {
                   1401: 		    textbox.value = "";
                   1402: 		    return;
                   1403: 		}
                   1404: 	    }
1.42      ng       1405: 	    for (var i=0; i<radioButton.length; i++) {
                   1406: 		radioButton[i].checked=false;
                   1407: 		if (point == i) {
                   1408: 		    radioButton[i].checked=true;
                   1409: 		}
                   1410: 	    }
1.41      ng       1411: 
1.42      ng       1412: 	} else {
                   1413: 	    textbox.value = point;
                   1414: 	}
1.41      ng       1415: 	for (i=0;i<document.classgrade.total.value;i++) {
1.43      ng       1416: 	    var user = eval("document.classgrade.ctr"+i+".value");
                   1417: 	    var scorename = eval("document.classgrade.GD_"+user+
1.54      albertel 1418: 				 "_"+partid+"_awarded");
1.43      ng       1419: 	    var saveval   = eval("document.classgrade.GD_"+user+
1.54      albertel 1420: 				 "_"+partid+"_solved_s.value");
                   1421: 	    var selname   = eval("document.classgrade.GD_"+user+"_"+partid+"_solved");
1.42      ng       1422: 	    if (saveval != "correct") {
                   1423: 		scorename.value = point;
1.43      ng       1424: 		if (selname[0].selected != true) {
                   1425: 		    selname[0].selected = true;
                   1426: 		}
1.42      ng       1427: 	    }
                   1428: 	}
                   1429: 	var selval   = eval("document.classgrade.SELVAL_"+partid);
                   1430: 	selval[0].selected = true;
                   1431:     }
                   1432: 
                   1433:     function writeRadText(partid,weight) {
                   1434: 	var selval   = eval("document.classgrade.SELVAL_"+partid);
1.43      ng       1435: 	var radioButton = eval("document.classgrade.RADVAL_"+partid);
                   1436: 	var textbox = eval("document.classgrade.TEXTVAL_"+partid);
1.42      ng       1437: 	if (selval[1].selected) {
                   1438: 	    for (var i=0; i<radioButton.length; i++) {
                   1439: 		radioButton[i].checked=false;
                   1440: 
                   1441: 	    }
                   1442: 	    textbox.value = "";
                   1443: 
                   1444: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.43      ng       1445: 		var user = eval("document.classgrade.ctr"+i+".value");
                   1446: 		var scorename = eval("document.classgrade.GD_"+user+
1.54      albertel 1447: 				     "_"+partid+"_awarded");
1.43      ng       1448: 		var saveval   = eval("document.classgrade.GD_"+user+
1.54      albertel 1449: 				     "_"+partid+"_solved_s.value");
1.43      ng       1450: 		var selname   = eval("document.classgrade.GD_"+user+
1.54      albertel 1451: 				     "_"+partid+"_solved");
1.42      ng       1452: 		if (saveval != "correct") {
                   1453: 		    scorename.value = "";
                   1454: 		    selname[1].selected = true;
                   1455: 		}
                   1456: 	    }
1.43      ng       1457: 	} else {
                   1458: 	    for (i=0;i<document.classgrade.total.value;i++) {
                   1459: 		var user = eval("document.classgrade.ctr"+i+".value");
                   1460: 		var scorename = eval("document.classgrade.GD_"+user+
1.54      albertel 1461: 				     "_"+partid+"_awarded");
1.43      ng       1462: 		var saveval   = eval("document.classgrade.GD_"+user+
1.54      albertel 1463: 				     "_"+partid+"_solved_s.value");
1.43      ng       1464: 		var selname   = eval("document.classgrade.GD_"+user+
1.54      albertel 1465: 				     "_"+partid+"_solved");
1.43      ng       1466: 		if (saveval != "correct") {
                   1467: 		    scorename.value = eval("document.classgrade.GD_"+user+
1.54      albertel 1468: 				     "_"+partid+"_awarded_s.value");;
1.43      ng       1469: 		    selname[0].selected = true;
                   1470: 		}
                   1471: 	    }
                   1472: 	}	    
1.42      ng       1473:     }
                   1474: 
                   1475:     function changeSelect(partid,user) {
1.54      albertel 1476: 	var selval = eval("document.classgrade.GD_"+user+'_'+partid+"_solved");
                   1477: 	var textbox = eval("document.classgrade.GD_"+user+'_'+partid+"_awarded");
1.44      ng       1478: 	var point  = textbox.value;
                   1479: 	var weight = eval("document.classgrade.weight_"+partid+".value");
                   1480: 
                   1481: 	if (isNaN(point) || point < 0) {
                   1482: 	    alert("A number equal or greater than 0 is expected. Entered value = "+point);
                   1483: 	    textbox.value = "";
                   1484: 	    return;
                   1485: 	}
                   1486: 	if (point > weight) {
                   1487: 	    var resp = confirm("You entered a value ("+point+
                   1488: 			       ") greater than the weight of the part. Accept?");
                   1489: 	    if (resp == false) {
                   1490: 		textbox.value = "";
                   1491: 		return;
                   1492: 	    }
                   1493: 	}
1.42      ng       1494: 	selval[0].selected = true;
                   1495:     }
                   1496: 
                   1497:     function changeOneScore(partid,user) {
1.54      albertel 1498: 	var selval = eval("document.classgrade.GD_"+user+'_'+partid+"_solved");
1.42      ng       1499: 	if (selval[1].selected) {
1.54      albertel 1500: 	    var boxval = eval("document.classgrade.GD_"+user+'_'+partid+"_awarded");
1.42      ng       1501: 	    boxval.value = "";
                   1502: 	}
                   1503:     }
                   1504: 
                   1505:     function resetEntry(numpart) {
                   1506: 	for (ctpart=0;ctpart<numpart;ctpart++) {
                   1507: 	    var partid = eval("document.classgrade.partid_"+ctpart+".value");
                   1508: 	    var radioButton = eval("document.classgrade.RADVAL_"+partid);
                   1509: 	    var textbox = eval("document.classgrade.TEXTVAL_"+partid);
                   1510: 	    var selval  = eval("document.classgrade.SELVAL_"+partid);
                   1511: 	    for (var i=0; i<radioButton.length; i++) {
                   1512: 		radioButton[i].checked=false;
                   1513: 
                   1514: 	    }
                   1515: 	    textbox.value = "";
                   1516: 	    selval[0].selected = true;
                   1517: 
                   1518: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.43      ng       1519: 		var user = eval("document.classgrade.ctr"+i+".value");
                   1520: 		var resetscore = eval("document.classgrade.GD_"+user+
1.54      albertel 1521: 				      "_"+partid+"_awarded");
1.43      ng       1522: 		resetscore.value = eval("document.classgrade.GD_"+user+
1.54      albertel 1523: 					"_"+partid+"_awarded_s.value");
1.42      ng       1524: 
1.43      ng       1525: 		var saveselval   = eval("document.classgrade.GD_"+user+
1.54      albertel 1526: 				     "_"+partid+"_solved_s.value");
1.42      ng       1527: 
1.54      albertel 1528: 		var selname   = eval("document.classgrade.GD_"+user+"_"+partid+"_solved");
1.42      ng       1529: 		if (saveselval == "excused") {
1.43      ng       1530: 		    if (selname[1].selected == false) { selname[1].selected = true;}
1.42      ng       1531: 		} else {
1.43      ng       1532: 		    if (selname[0].selected == false) {selname[0].selected = true};
1.42      ng       1533: 		}
                   1534: 	    }
1.41      ng       1535: 	}
1.42      ng       1536:     }
                   1537: 
1.41      ng       1538: </script>
                   1539: VIEWJAVASCRIPT
1.42      ng       1540: }
                   1541: 
1.44      ng       1542: #--- show scores for a section or whole class w/ option to change/update a score
1.42      ng       1543: sub viewgrades {
                   1544:     my ($request) = shift;
                   1545:     &viewgrades_js($request);
1.41      ng       1546: 
                   1547:     my ($symb,$url) = ($ENV{'form.symb'},$ENV{'form.url'}); 
1.45      ng       1548:     my $result='<h3><font color="#339933">Manual Grading</font></h3>';
1.38      ng       1549: 
1.43      ng       1550:     $result.='<font size=+1><b>Resource: </b>'.$ENV{'form.url'}.'</font>'."\n";
1.41      ng       1551: 
                   1552:     #view individual student submission form - called using Javascript viewOneStudent
1.45      ng       1553:     $result.=&jscriptNform($url,$symb);
1.41      ng       1554: 
1.44      ng       1555:     #beginning of class grading form
1.41      ng       1556:     $result.= '<form action="/adm/grades" method="post" name="classgrade">'."\n".
                   1557: 	'<input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
                   1558: 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
1.38      ng       1559: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
1.41      ng       1560: 	'<input type="hidden" name="section" value="'.$ENV{'form.section'}.'" />'."\n";
1.52      albertel 1561:     $result.='<h3>Assign Common Grade To ';
                   1562:     if ($ENV{'form.section'} eq 'all') {
                   1563: 	$result.='Class </h3>';
                   1564:     } elsif ($ENV{'form.section'} eq 'no') {
                   1565: 	$result.='Students in no Section </h3>';
                   1566:     } else {
                   1567: 	$result.='Students in Section '.$ENV{'form.section'}.'</h3>';
                   1568:     }
                   1569:     $result.= '<table border=0><tr><td bgcolor="#777777">'."\n".
                   1570: 	'<table border=0><tr bgcolor="#ffffdd"><td>';
                   1571: #    $result.='To assign the same score for all the students use the radio buttons or '.
                   1572: #	'text box below. To assign scores individually fill in the score boxes for '.
                   1573: #	'each student in the table below. <font color="red">A part that has already '.
                   1574: #	'been graded does not get changed using the radio buttons or text box. '.
                   1575: #	'If needed, it has to be changed individually.</font>';
                   1576: #    $result.='</td></tr><tr><td>';
1.44      ng       1577:     #radio buttons/text box for assigning points for a section or class.
                   1578:     #handles different parts of a problem
1.42      ng       1579:     my ($partlist,$handgrade) = &response_type($ENV{'form.url'});
                   1580:     my %weight = ();
                   1581:     my $ctsparts = 0;
1.41      ng       1582:     $result.='<table border="0">';
1.45      ng       1583:     my %seen = ();
1.42      ng       1584:     for (sort keys(%$handgrade)) {
1.54      albertel 1585: 	my ($partid,$respid) = split (/_/,$_,2);
1.45      ng       1586: 	next if $seen{$partid};
                   1587: 	$seen{$partid}++;
1.42      ng       1588: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
                   1589: 	my $wgt = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb);
                   1590: 	$weight{$partid} = $wgt eq '' ? '1' : $wgt;
                   1591: 
1.44      ng       1592: 	$result.='<input type="hidden" name="partid_'.
                   1593: 	    $ctsparts.'" value="'.$partid.'" />'."\n";
                   1594: 	$result.='<input type="hidden" name="weight_'.
                   1595: 	    $partid.'" value="'.$weight{$partid}.'" />'."\n";
                   1596: 	$result.='<tr><td><b>Part  '.$partid.'&nbsp; &nbsp;Point:</b> </td><td>';
1.42      ng       1597: 	$result.='<table border="0"><tr>';  
1.41      ng       1598: 	my $ctr = 0;
1.42      ng       1599: 	while ($ctr<=$weight{$partid}) { # display radio buttons in a nice table 10 across
                   1600: 	    $result.= '<td><input type="radio" name="RADVAL_'.$partid.'" '.
1.54      albertel 1601: 		'onclick="javascript:writePoint(\''.$partid.'\','.$weight{$partid}.
1.41      ng       1602: 		','.$ctr.')" />'.$ctr."</td>\n";
                   1603: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
                   1604: 	    $ctr++;
                   1605: 	}
                   1606: 	$result.='</tr></table>';
1.44      ng       1607: 	$result.= '</td><td><b> or </b><input type="text" name="TEXTVAL_'.
1.54      albertel 1608: 	    $partid.'" size="4" '.'onChange="javascript:writePoint(\''.
                   1609: 		$partid.'\','.$weight{$partid}.',\'textval\')" /> /'.
1.42      ng       1610: 	    $weight{$partid}.' (problem weight)</td>'."\n";
                   1611: 	$result.= '</td><td><select name="SELVAL_'.$partid.'"'.
1.54      albertel 1612: 	    'onChange="javascript:writeRadText(\''.$partid.'\','.
                   1613: 		$weight{$partid}.')" /> '.
1.42      ng       1614: 	    '<option selected="on"> </option>'.
                   1615: 	    '<option>excused</option></select></td></tr>'."\n";
                   1616: 	$ctsparts++;
1.41      ng       1617:     }
1.52      albertel 1618:     $result.='</table>'.'</td></tr></table>'.'</td></tr></table>'."\n".
                   1619: 	'<input type="hidden" name="totalparts" value="'.$ctsparts.'" />';
1.42      ng       1620:     $result.='<input type="button" value="Reset" '.
1.43      ng       1621: 	'onClick="javascript:resetEntry('.$ctsparts.');" TARGET=_self> &nbsp; &nbsp;';
1.45      ng       1622:     $result.='<input type="button" value="Submit Changes" '.
                   1623: 	'onClick="javascript:submit();" TARGET=_self />'."\n";
1.41      ng       1624: 
1.44      ng       1625:     #table listing all the students in a section/class
                   1626:     #header of table
1.52      albertel 1627:     $result.= '<h3>Assign Grade to Specific Students in ';
                   1628:     if ($ENV{'form.section'} eq 'all') {
                   1629: 	$result.='the Class </h3>';
                   1630:     } elsif ($ENV{'form.section'} eq 'no') {
                   1631: 	$result.='no Section </h3>';
                   1632:     } else {
                   1633: 	$result.='Section '.$ENV{'form.section'}.'</h3>';
                   1634:     }
1.42      ng       1635:     $result.= '<table border=0><tr><td bgcolor="#777777">'."\n".
1.41      ng       1636: 	'<table border=0><tr bgcolor="#deffff">'.
1.44      ng       1637: 	'<td><b>Fullname</b></td><td><b>Username</b></td><td><b>Domain</b></td>'."\n";
1.41      ng       1638:     my (@parts) = sort(&getpartlist($url));
                   1639:     foreach my $part (@parts) {
                   1640: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
                   1641: 	if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
                   1642: 	if ($display =~ /^Partial Credit Factor/) {
1.54      albertel 1643: 	    my ($partid) = &split_part_type($part);
1.53      albertel 1644: 	    $result.='<td><b>Score Part '.$partid.'<br />(weight = '.
1.42      ng       1645: 		$weight{$partid}.')</b></td>'."\n";
1.41      ng       1646: 	    next;
                   1647: 	}
1.53      albertel 1648: 	$display =~ s|Problem Status|Grade Status<br />|;
1.41      ng       1649: 	$result.='<td><b>'.$display.'</b></td>'."\n";
                   1650:     }
                   1651:     $result.='</tr>';
1.44      ng       1652: 
1.41      ng       1653:     #get info for each student
1.44      ng       1654:     #list all the students - with points and grade status
1.56    ! matthew  1655:     my (undef,undef,$fullname) = &getclasslist($ENV{'form.section'},'0');
1.41      ng       1656:     my $ctr = 0;
1.53      albertel 1657:     foreach (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
1.44      ng       1658: 	my ($uname,$udom) = split(/:/);
                   1659: 	$result.='<input type="hidden" name="ctr'.$ctr.'" value="'.$uname.'" />'."\n";
1.41      ng       1660: 	$result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},
                   1661: 				   $_,$$fullname{$_},\@parts,\%weight);
                   1662: 	$ctr++;
                   1663:     }
                   1664:     $result.='</table></td></tr></table>';
                   1665:     $result.='<input type="hidden" name="total" value="'.$ctr.'" />'."\n";
1.45      ng       1666:     $result.='<input type="button" value="Submit Changes" '.
                   1667: 	'onClick="javascript:submit();" TARGET=_self /></form>'."\n";
1.41      ng       1668:     $result.=&show_grading_menu_form($symb,$url);
                   1669:     return $result;
                   1670: }
                   1671: 
1.44      ng       1672: #--- call by previous routine to display each student
1.41      ng       1673: sub viewstudentgrade {
                   1674:     my ($url,$symb,$courseid,$student,$fullname,$parts,$weight) = @_;
1.44      ng       1675:     my ($uname,$udom) = split(/:/,$student);
                   1676:     my %record=&Apache::lonnet::restore($symb,$courseid,$udom,$uname);
1.41      ng       1677:     my $result='<tr bgcolor="#ffffdd"><td>'.
1.44      ng       1678: 	'<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
                   1679: 	'\')"; TARGET=_self>'.$fullname.'</a>'.
                   1680: 	'</td><td>'.$uname.'</td><td align="middle">'.$udom.'</td>'."\n";
1.41      ng       1681:     foreach my $part (@$parts) {
1.54      albertel 1682: 	my ($part,$type) = &split_part_type($part);
1.41      ng       1683: 	my $score=$record{"resource.$part.$type"};
                   1684: 	if ($type eq 'awarded') {
1.42      ng       1685: 	    my $pts = $score eq '' ? '' : $score*$$weight{$part};
                   1686: 	    $result.='<input type="hidden" name="'.
1.54      albertel 1687: 		'GD_'.$uname.'_'.$part.'_awarded_s" value="'.$pts.'" />'."\n";
1.42      ng       1688: 	    $result.='<td align="middle"><input type="text" name="'.
1.54      albertel 1689: 		'GD_'.$uname.'_'.$part.'_awarded" '.
                   1690: 		'onChange="javascript:changeSelect(\''.$part.'\',\''.$uname.
1.44      ng       1691: 		'\')" value="'.$pts.'" size="4" /></td>'."\n";
1.41      ng       1692: 	} elsif ($type eq 'solved') {
                   1693: 	    my ($status,$foo)=split(/_/,$score,2);
                   1694: 	    $status = 'nothing' if ($status eq '');
1.54      albertel 1695: 	    $result.='<input type="hidden" name="'.'GD_'.$uname.'_'.
                   1696: 		$part.'_solved_s" value="'.$status.'" />'."\n";
1.42      ng       1697: 	    $result.='<td align="middle"><select name="'.
1.54      albertel 1698: 		'GD_'.$uname.'_'.$part.'_solved" '.
                   1699: 		'onChange="javascript:changeOneScore(\''.$part.'\',\''.$uname.'\')" >'."\n";
1.42      ng       1700: 	    my $optsel = '<option selected="on"> </option><option>excused</option>'."\n";
                   1701: 	    $optsel = '<option> </option><option selected="on">excused</option>'."\n"
                   1702: 		if ($status eq 'excused');
1.41      ng       1703: 	    $result.=$optsel;
                   1704: 	    $result.="</select></td>\n";
1.54      albertel 1705: 	} else {
                   1706: 	    $result.='<input type="hidden" name="'.
                   1707: 		'GD_'.$uname.'_'.$part.'_'.$type.'_s" value="'.$score.'" />'.
                   1708: 		    "\n";
                   1709: 	    $result.='<td align="middle"><input type="text" name="'.
                   1710: 		'GD_'.$uname.'_'.$part.'_'.$type.'" '.
                   1711: 		'value="'.$score.'" size="4" /></td>'."\n";
1.41      ng       1712: 	}
                   1713:     }
                   1714:     $result.='</tr>';
                   1715:     return $result;
1.38      ng       1716: }
                   1717: 
1.44      ng       1718: #--- change scores for all the students in a section/class
                   1719: #    record does not get update if unchanged
1.38      ng       1720: sub editgrades {
1.41      ng       1721:     my ($request) = @_;
                   1722: 
                   1723:     my $symb=$ENV{'form.symb'};
1.43      ng       1724:     my $url =$ENV{'form.url'};
1.45      ng       1725:     my $title='<h3><font color="#339933">Current Grade Status</font></h3>';
1.44      ng       1726:     $title.='<font size=+1><b>Resource: </b>'.$ENV{'form.url'}.'</font><br />'."\n";
                   1727:     $title.='<font size=+1><b>Section: </b>'.$ENV{'form.section'}.'</font>'."\n";
                   1728:     $title.= &show_grading_menu_form ($symb,$url);
                   1729:     my $result= '<table border="0"><tr><td bgcolor="#777777">'."\n";
1.43      ng       1730:     $result.= '<table border="0"><tr bgcolor="#deffff">'.
                   1731: 	'<td rowspan=2><b>Username</b></td><td rowspan=2><b>Fullname</b></td>'."\n";
                   1732: 
                   1733:     my %scoreptr = (
                   1734: 		    'correct'  =>'correct_by_override',
                   1735: 		    'incorrect'=>'incorrect_by_override',
                   1736: 		    'excused'  =>'excused',
                   1737: 		    'ungraded' =>'ungraded_attempted',
                   1738: 		    'nothing'  => '',
                   1739: 		    );
1.56    ! matthew  1740:     my ($classlist,undef,$fullname) = &getclasslist($ENV{'form.section'},'0');
1.34      ng       1741: 
1.44      ng       1742:     my (@partid);
                   1743:     my %weight = ();
1.54      albertel 1744:     my %columns = ();
1.44      ng       1745:     my ($i,$ctr,$count,$rec_update) = (0,0,0,0);
1.54      albertel 1746: 
                   1747:     my (@parts) = sort(&getpartlist($url));
                   1748:     my $header;
1.44      ng       1749:     while ($ctr < $ENV{'form.totalparts'}) {
                   1750: 	my $partid = $ENV{'form.partid_'.$ctr};
                   1751: 	push @partid,$partid;
                   1752: 	$weight{$partid} = $ENV{'form.weight_'.$partid};
                   1753: 	$ctr++;
1.54      albertel 1754:     }
                   1755:     foreach my $partid (@partid) {
                   1756: 	$header .= '<td align="center">&nbsp;<b>Old Score</b>&nbsp;</td>'.
                   1757: 	    '<td align="center">&nbsp;<b>New Score</b>&nbsp;</td>';
                   1758: 	$columns{$partid}=2;
                   1759: 	foreach my $stores (@parts) {
                   1760: 	    my ($part,$type) = &split_part_type($stores);
                   1761: 	    if ($part !~ m/^\Q$partid\E/) { next;}
                   1762: 	    if ($type eq 'awarded' || $type eq 'solved') { next; }
                   1763: 	    my $display=&Apache::lonnet::metadata($url,$stores.'.display');
                   1764: 	    $display =~ s/\[Part: (\w)+\]//;
                   1765: 	    $header .= '<td align="center">&nbsp;<b>Old</b> '.$display.'&nbsp;</td>'.
                   1766: 		'<td align="center">&nbsp;<b>New</b> '.$display.'&nbsp;</td>';
                   1767: 	    $columns{$partid}+=2;
                   1768: 	}
                   1769:     }
                   1770:     foreach my $partid (@partid) {
                   1771: 	$result .= '<td colspan="'.$columns{$partid}.
                   1772: 	    '" align="center"><b>Part '.$partid.
1.44      ng       1773: 	    '</b> (Weight = '.$weight{$partid}.')</td>';
1.54      albertel 1774: 
1.44      ng       1775:     }
                   1776:     $result .= '</tr><tr bgcolor="#deffff">';
1.54      albertel 1777:     $result .= $header;
1.44      ng       1778:     $result .= '</tr>'."\n";
1.13      albertel 1779: 
1.44      ng       1780:     for ($i=0; $i<$ENV{'form.total'}; $i++) {
                   1781: 	my $user = $ENV{'form.ctr'.$i};
                   1782: 	my %newrecord;
                   1783: 	my $updateflag = 0;
                   1784: 	my @userdom = grep /^$user:/,keys %$classlist;
1.54      albertel 1785: 	my (undef,$udom) = split(/:/,$userdom[0]);
1.13      albertel 1786: 
1.44      ng       1787: 	$result .= '<tr bgcolor="#ffffde"><td>'.$user.'&nbsp;</td><td>'.
                   1788: 	    $$fullname{$userdom[0]}.'&nbsp;</td>';
                   1789: 	foreach (@partid) {
1.54      albertel 1790: 	    my $old_aw    = $ENV{'form.GD_'.$user.'_'.$_.'_awarded_s'};
                   1791: 	    my $old_part_pcr = $old_aw/($weight{$_} ne '0' ? $weight{$_}:1);
                   1792: 	    my $old_part  = $old_aw eq '' ? '' : $old_part_pcr;
                   1793: 	    my $old_score = $scoreptr{$ENV{'form.GD_'.$user.'_'.$_.'_solved_s'}};
                   1794: 
                   1795: 	    my $awarded   = $ENV{'form.GD_'.$user.'_'.$_.'_awarded'};
                   1796: 	    my $pcr       = $awarded/($weight{$_} ne '0' ? $weight{$_} : 1);
                   1797: 	    my $partial   = $awarded eq '' ? '' : $pcr;
1.44      ng       1798: 	    my $score;
                   1799: 	    if ($partial eq '') {
1.54      albertel 1800: 		$score = $scoreptr{$ENV{'form.GD_'.$user.'_'.$_.'_solved_s'}};
1.44      ng       1801: 	    } elsif ($partial > 0) {
                   1802: 		$score = 'correct_by_override';
                   1803: 	    } elsif ($partial == 0) {
                   1804: 		$score = 'incorrect_by_override';
                   1805: 	    }
1.54      albertel 1806: 	    $score = 'excused' if (($ENV{'form.GD_'.$user.'_'.$_.'_solved'} eq 'excused') &&
1.44      ng       1807: 				   ($score ne 'excused'));
                   1808: 	    $result .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
                   1809: 		'<td align="center">'.$awarded.
                   1810: 		($score eq 'excused' ? $score : '').'&nbsp;</td>';
1.5       albertel 1811: 
1.54      albertel 1812: 	    if (!($old_part eq $partial && $old_score eq $score)) {
                   1813: 		$updateflag = 1;
                   1814: 		$newrecord{'resource.'.$_.'.awarded'}  = $partial if $partial ne '';
                   1815: 		$newrecord{'resource.'.$_.'.solved'}   = $score;
                   1816: 		$rec_update++;
                   1817: 	    }
                   1818: 
                   1819: 	    my $partid=$_;
                   1820: 	    foreach my $stores (@parts) {
                   1821: 		my ($part,$type) = &split_part_type($stores);
                   1822: 		if ($part !~ m/^\Q$partid\E/) { next;}
                   1823: 		if ($type eq 'awarded' || $type eq 'solved') { next; }
                   1824: 		my $old_aw    = $ENV{'form.GD_'.$user.'_'.$part.'_'.$type.'_s'};
                   1825: 		my $awarded   = $ENV{'form.GD_'.$user.'_'.$part.'_'.$type};
                   1826: 		if ($awarded ne '' && $awarded ne $old_aw) {
                   1827: 		    $newrecord{'resource.'.$part.'.'.$type}= $awarded;
                   1828: 		    $updateflag=1;
                   1829: 		}
                   1830: 		$result .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
                   1831: 		    '<td align="center">'.$awarded.'&nbsp;</td>';
                   1832: 	    }
1.44      ng       1833: 	}
                   1834: 	$result .= '</tr>'."\n";
                   1835: 	if ($updateflag) {
                   1836: 	    $count++;
                   1837: 	    $newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
                   1838: 	    &Apache::lonnet::cstore(\%newrecord,$symb,$ENV{'request.course.id'},
                   1839: 				    $udom,$user);
                   1840: 	}
                   1841:     }
                   1842:     $result .= '</table></td></tr></table>'."\n";
                   1843:     my $msg = '<b>Number of records updated = '.$rec_update.
                   1844: 	' for '.$count.' student'.($count <= 1 ? '' : 's').'.</b><br />'.
                   1845: 	'<b>Total number of students = '.$ENV{'form.total'}.'</b><br />';
                   1846:     return $title.$msg.$result;
1.5       albertel 1847: }
1.54      albertel 1848: 
                   1849: sub split_part_type {
                   1850:     my ($partstr) = @_;
                   1851:     my ($temp,@allparts)=split(/_/,$partstr);
                   1852:     my $type=pop(@allparts);
                   1853:     my $part=join('.',@allparts);
                   1854:     return ($part,$type);
                   1855: }
                   1856: 
1.44      ng       1857: #------------- end of section for handling grading by section/class ---------
                   1858: #
                   1859: #----------------------------------------------------------------------------
                   1860: 
1.5       albertel 1861: 
1.44      ng       1862: #----------------------------------------------------------------------------
                   1863: #
                   1864: #-------------------------- Next few routines handles grading by csv upload
                   1865: #
                   1866: #--- Javascript to handle csv upload
1.27      albertel 1867: sub csvupload_javascript_reverse_associate {
                   1868:   return(<<ENDPICK);
                   1869:   function verify(vf) {
                   1870:     var foundsomething=0;
                   1871:     var founduname=0;
                   1872:     var founddomain=0;
                   1873:     for (i=0;i<=vf.nfields.value;i++) {
                   1874:       tw=eval('vf.f'+i+'.selectedIndex');
                   1875:       if (i==0 && tw!=0) { founduname=1; }
                   1876:       if (i==1 && tw!=0) { founddomain=1; }
                   1877:       if (i!=0 && i!=1 && tw!=0) { foundsomething=1; }
                   1878:     }
                   1879:     if (founduname==0 || founddomain==0) {
                   1880:       alert('You need to specify at both the username and domain');
                   1881:       return;
                   1882:     }
                   1883:     if (foundsomething==0) {
                   1884:       alert('You need to specify at least one grading field');
                   1885:       return;
                   1886:     }
                   1887:     vf.submit();
                   1888:   }
                   1889:   function flip(vf,tf) {
                   1890:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   1891:     var i;
                   1892:     for (i=0;i<=vf.nfields.value;i++) {
                   1893:       //can not pick the same destination field for both name and domain
                   1894:       if (((i ==0)||(i ==1)) && 
                   1895:           ((tf==0)||(tf==1)) && 
                   1896:           (i!=tf) &&
                   1897:           (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   1898:         eval('vf.f'+i+'.selectedIndex=0;')
                   1899:       }
                   1900:     }
                   1901:   }
                   1902: ENDPICK
                   1903: }
                   1904: 
                   1905: sub csvupload_javascript_forward_associate {
                   1906:   return(<<ENDPICK);
                   1907:   function verify(vf) {
                   1908:     var foundsomething=0;
                   1909:     var founduname=0;
                   1910:     var founddomain=0;
                   1911:     for (i=0;i<=vf.nfields.value;i++) {
                   1912:       tw=eval('vf.f'+i+'.selectedIndex');
                   1913:       if (tw==1) { founduname=1; }
                   1914:       if (tw==2) { founddomain=1; }
                   1915:       if (tw>2) { foundsomething=1; }
                   1916:     }
                   1917:     if (founduname==0 || founddomain==0) {
                   1918:       alert('You need to specify at both the username and domain');
                   1919:       return;
                   1920:     }
                   1921:     if (foundsomething==0) {
                   1922:       alert('You need to specify at least one grading field');
                   1923:       return;
                   1924:     }
                   1925:     vf.submit();
                   1926:   }
                   1927:   function flip(vf,tf) {
                   1928:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   1929:     var i;
                   1930:     //can not pick the same destination field twice
                   1931:     for (i=0;i<=vf.nfields.value;i++) {
                   1932:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   1933:         eval('vf.f'+i+'.selectedIndex=0;')
                   1934:       }
                   1935:     }
                   1936:   }
                   1937: ENDPICK
                   1938: }
                   1939: 
1.26      albertel 1940: sub csvuploadmap_header {
1.41      ng       1941:     my ($request,$symb,$url,$datatoken,$distotal)= @_;
                   1942:     my $javascript;
                   1943:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   1944: 	$javascript=&csvupload_javascript_reverse_associate();
                   1945:     } else {
                   1946: 	$javascript=&csvupload_javascript_forward_associate();
                   1947:     }
1.45      ng       1948: 
                   1949:     my $result='<table border="0">';
                   1950:     $result.='<tr><td colspan=3><font size=+1><b>Resource: </b>'.$url.'</font></td></tr>';
                   1951:     my ($partlist,$handgrade) = &response_type($url);
                   1952:     my ($resptype,$hdgrade)=('','no');
                   1953:     for (sort keys(%$handgrade)) {
                   1954: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
                   1955: 	$resptype = $responsetype;
                   1956: 	$hdgrade = $handgrade if ($handgrade eq 'yes');
                   1957: 	$result.='<tr><td><b>Part </b>'.(split(/_/))[0].'</td>'.
                   1958: 	    '<td><b>Type: </b>'.$responsetype.'</td>'.
                   1959: 	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
                   1960:     }
                   1961:     $result.='</table>';
1.41      ng       1962:     $request->print(<<ENDPICK);
1.26      albertel 1963: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
1.45      ng       1964: <h3><font color="#339933">Uploading Class Grades</font></h3>
                   1965: $result
1.26      albertel 1966: <hr>
                   1967: <h3>Identify fields</h3>
                   1968: Total number of records found in file: $distotal <hr />
                   1969: Enter as many fields as you can. The system will inform you and bring you back
                   1970: to this page if the data selected is insufficient to run your class.<hr />
                   1971: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
                   1972: <input type="hidden" name="associate"  value="" />
                   1973: <input type="hidden" name="phase"      value="three" />
                   1974: <input type="hidden" name="datatoken"  value="$datatoken" />
                   1975: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
                   1976: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
                   1977: <input type="hidden" name="upfile_associate" 
                   1978:                                        value="$ENV{'form.upfile_associate'}" />
                   1979: <input type="hidden" name="symb"       value="$symb" />
                   1980: <input type="hidden" name="url"        value="$url" />
                   1981: <input type="hidden" name="command"    value="csvuploadassign" />
                   1982: <hr />
                   1983: <script type="text/javascript" language="Javascript">
                   1984: $javascript
                   1985: </script>
                   1986: ENDPICK
1.41      ng       1987: return '';
1.26      albertel 1988: 
                   1989: }
                   1990: 
                   1991: sub csvupload_fields {
1.41      ng       1992:     my ($url) = @_;
                   1993:     my (@parts) = &getpartlist($url);
                   1994:     my @fields=(['username','Student Username'],['domain','Student Domain']);
                   1995:     foreach my $part (sort(@parts)) {
                   1996: 	my @datum;
                   1997: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
                   1998: 	my $name=$part;
                   1999: 	if  (!$display) { $display = $name; }
                   2000: 	@datum=($name,$display);
                   2001: 	push(@fields,\@datum);
                   2002:     }
                   2003:     return (@fields);
1.26      albertel 2004: }
                   2005: 
                   2006: sub csvuploadmap_footer {
1.41      ng       2007:     my ($request,$i,$keyfields) =@_;
                   2008:     $request->print(<<ENDPICK);
1.26      albertel 2009: </table>
                   2010: <input type="hidden" name="nfields" value="$i" />
                   2011: <input type="hidden" name="keyfields" value="$keyfields" />
                   2012: <input type="button" onClick="javascript:verify(this.form)" value="Assign Grades" /><br />
                   2013: </form>
                   2014: ENDPICK
                   2015: }
                   2016: 
                   2017: sub csvuploadmap {
1.41      ng       2018:     my ($request)= @_;
                   2019:     my ($symb,$url)=&get_symb_and_url($request);
                   2020:     if (!$symb) {return '';}
                   2021:     my $datatoken;
                   2022:     if (!$ENV{'form.datatoken'}) {
                   2023: 	$datatoken=&Apache::loncommon::upfile_store($request);
1.26      albertel 2024:     } else {
1.41      ng       2025: 	$datatoken=$ENV{'form.datatoken'};
                   2026: 	&Apache::loncommon::load_tmp_file($request);
1.26      albertel 2027:     }
1.41      ng       2028:     my @records=&Apache::loncommon::upfile_record_sep();
                   2029:     &csvuploadmap_header($request,$symb,$url,$datatoken,$#records+1);
                   2030:     my ($i,$keyfields);
                   2031:     if (@records) {
                   2032: 	my @fields=&csvupload_fields($url);
1.45      ng       2033: 
1.41      ng       2034: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
                   2035: 	    &Apache::loncommon::csv_print_samples($request,\@records);
                   2036: 	    $i=&Apache::loncommon::csv_print_select_table($request,\@records,
                   2037: 							  \@fields);
                   2038: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
                   2039: 	    chop($keyfields);
                   2040: 	} else {
                   2041: 	    unshift(@fields,['none','']);
                   2042: 	    $i=&Apache::loncommon::csv_samples_select_table($request,\@records,
                   2043: 							    \@fields);
                   2044: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
                   2045: 	    $keyfields=join(',',sort(keys(%sone)));
                   2046: 	}
                   2047:     }
                   2048:     &csvuploadmap_footer($request,$i,$keyfields);
                   2049:     return '';
1.27      albertel 2050: }
                   2051: 
                   2052: sub csvuploadassign {
1.41      ng       2053:     my ($request)= @_;
                   2054:     my ($symb,$url)=&get_symb_and_url($request);
                   2055:     if (!$symb) {return '';}
                   2056:     &Apache::loncommon::load_tmp_file($request);
1.44      ng       2057:     my @gradedata = &Apache::loncommon::upfile_record_sep();
1.41      ng       2058:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
                   2059:     my %fields=();
                   2060:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
                   2061: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   2062: 	    if ($ENV{'form.f'.$i} ne 'none') {
                   2063: 		$fields{$keyfields[$i]}=$ENV{'form.f'.$i};
                   2064: 	    }
                   2065: 	} else {
                   2066: 	    if ($ENV{'form.f'.$i} ne 'none') {
                   2067: 		$fields{$ENV{'form.f'.$i}}=$keyfields[$i];
                   2068: 	    }
                   2069: 	}
1.27      albertel 2070:     }
1.41      ng       2071:     $request->print('<h3>Assigning Grades</h3>');
                   2072:     my $courseid=$ENV{'request.course.id'};
                   2073:     my ($classlist) = &getclasslist('all','1');
                   2074:     my @skipped;
                   2075:     my $countdone=0;
                   2076:     foreach my $grade (@gradedata) {
                   2077: 	my %entries=&Apache::loncommon::record_sep($grade);
                   2078: 	my $username=$entries{$fields{'username'}};
                   2079: 	my $domain=$entries{$fields{'domain'}};
                   2080: 	if (!exists($$classlist{"$username:$domain"})) {
                   2081: 	    push(@skipped,"$username:$domain");
                   2082: 	    next;
                   2083: 	}
                   2084: 	my %grades;
                   2085: 	foreach my $dest (keys(%fields)) {
                   2086: 	    if ($dest eq 'username' || $dest eq 'domain') { next; }
                   2087: 	    if ($entries{$fields{$dest}} eq '') { next; }
                   2088: 	    my $store_key=$dest;
                   2089: 	    $store_key=~s/^stores/resource/;
                   2090: 	    $store_key=~s/_/\./g;
                   2091: 	    $grades{$store_key}=$entries{$fields{$dest}};
                   2092: 	}
                   2093: 	$grades{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
                   2094: 	&Apache::lonnet::cstore(\%grades,$symb,$ENV{'request.course.id'},
                   2095: 				$domain,$username);
                   2096: 	$request->print('.');
                   2097: 	$request->rflush();
                   2098: 	$countdone++;
                   2099:     }
                   2100:     $request->print("<br />Stored $countdone students\n");
                   2101:     if (@skipped) {
                   2102: 	$request->print('<br /><font size="+1"><b>Skipped Students</b></font><br />');
                   2103: 	foreach my $student (@skipped) { $request->print("<br />$student"); }
                   2104:     }
                   2105:     $request->print(&view_edit_entire_class_form($symb,$url));
                   2106:     $request->print(&show_grading_menu_form($symb,$url));
                   2107:     return '';
1.26      albertel 2108: }
1.44      ng       2109: #------------- end of section for handling csv file upload ---------
                   2110: #
                   2111: #-------------------------------------------------------------------
                   2112: 
                   2113: #-------------------------- Menu interface -------------------------
                   2114: #
                   2115: #--- Show a Grading Menu button - Calls the next routine ---
                   2116: sub show_grading_menu_form {
                   2117:     my ($symb,$url)=@_;
                   2118:     my $result.='<form action="/adm/grades" method="post">'."\n".
                   2119: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   2120: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
                   2121: 	'<input type="hidden" name="command" value="gradingmenu" />'."\n".
                   2122: 	'<input type="submit" name="submit" value="Grading Menu" />'."\n".
                   2123: 	'</form>'."\n";
                   2124:     return $result;
                   2125: }
                   2126: 
                   2127: #--- Displays the main menu page -------
                   2128: sub gradingmenu {
                   2129:     my ($request) = @_;
                   2130:     my ($symb,$url)=&get_symb_and_url($request);
                   2131:     if (!$symb) {return '';}
1.45      ng       2132:     my $result='<h3>&nbsp;<font color="#339933">Select a Grading Method</font></h3>';
1.44      ng       2133:     $result.='<table border="0">';
                   2134:     $result.='<tr><td colspan=3><font size=+1><b>Resource: </b>'.$url.'</font></td></tr>';
                   2135:     my ($partlist,$handgrade) = &response_type($url);
                   2136:     my ($resptype,$hdgrade)=('','no');
                   2137:     for (sort keys(%$handgrade)) {
                   2138: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
                   2139: 	$resptype = $responsetype;
                   2140: 	$hdgrade = $handgrade if ($handgrade eq 'yes');
                   2141: 	$result.='<tr><td><b>Part </b>'.(split(/_/))[0].'</td>'.
                   2142: 	    '<td><b>Type: </b>'.$responsetype.'</td>'.
                   2143: 	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
                   2144:     }
                   2145:     $result.='</table>';
                   2146:     $result.=&view_edit_entire_class_form($symb,$url).'<br />';
                   2147:     $result.=&upcsvScores_form($symb,$url).'<br />';
                   2148:     $result.=&viewGradeaStu_form($symb,$url,$resptype,$hdgrade).'<br />';
                   2149:     $result.=&verifyReceipt_form($symb,$url) 
                   2150: 	if ((&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) && ($symb));
                   2151:  
                   2152:     return $result;
                   2153: }
                   2154: 
                   2155: #--- Menu for grading a section or the whole class ---
                   2156: sub view_edit_entire_class_form {
                   2157:     my ($symb,$url)=@_;
1.56    ! matthew  2158:     my ($classlist,$sections,undef) = &getclasslist('all','0');
1.44      ng       2159:     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
                   2160:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
1.52      albertel 2161:     $result.='&nbsp;<b>Grade Entire Section or Class</b></td></tr>'."\n";
1.44      ng       2162:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   2163:     $result.='<form action="/adm/grades" method="post">'."\n".
                   2164: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   2165: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
                   2166: 	'<input type="hidden" name="command" value="viewgrades" />'."\n";
                   2167:     $result.='&nbsp;<b>Select section:</b> <select name="section">'."\n";
1.49      albertel 2168:     if (ref($sections)) {
                   2169: 	foreach (sort (@$sections)) {
                   2170: 	    $result.= '<option>'.$_.'</option>'."\n";
                   2171: 	}
1.44      ng       2172:     }
                   2173:     $result.='<option selected="on">all</select>'."<br />\n";
1.51      albertel 2174:     $result.='&nbsp;<input type="button" onClick="submit();" value="Grade" /></form>'."\n";
1.44      ng       2175:     $result.='</td></tr></table>'."\n";
                   2176:     $result.='</td></tr></table>'."\n";
                   2177:     return $result;
                   2178: }
                   2179: 
                   2180: #--- Menu to upload a csv scores ---
                   2181: sub upcsvScores_form {
                   2182:     my ($symb,$url) = @_;
                   2183:     if (!$symb) {return '';}
1.46      ng       2184:     my $result = '<script type="text/javascript" language="javascript">'."\n".
                   2185: 	'  function checkUpload(formname) {'."\n".
                   2186: 	'    if (formname.upfile.value == "") {'."\n".
                   2187: 	'       alert("Please use the browse button to select a file from your local directory.");'."\n".
                   2188: 	'       return false;'."\n".
                   2189: 	'    }'."\n".
                   2190: 	'    formname.submit();'."\n".
                   2191: 	'  }'."\n".
                   2192: 	'</script>'."\n";
                   2193: 
                   2194:     $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
1.44      ng       2195:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
                   2196:     $result.='&nbsp;<b>Specify a file containing the class scores for above resource</b></td></tr>'."\n";
                   2197:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   2198:     my $upfile_select=&Apache::loncommon::upfile_select_html();
                   2199:   $result.=<<ENDUPFORM;
                   2200: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                   2201: <input type="hidden" name="symb" value="$symb" />
                   2202: <input type="hidden" name="url" value="$url" />
                   2203: <input type="hidden" name="command" value="csvuploadmap" />
                   2204: $upfile_select
1.46      ng       2205: <br />&nbsp;<input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Grades" />
1.44      ng       2206: </form>
                   2207: ENDUPFORM
                   2208:     $result.='</td></tr></table>'."\n";
                   2209:     $result.='</td></tr></table>'."\n";
                   2210:     return $result;
                   2211: }
                   2212: 
                   2213: #--- Handgrading problems ---
                   2214: sub viewGradeaStu_form {
                   2215:     my ($symb,$url,$response,$handgrade) = @_;
                   2216:     my ($classlist,$sections) = &getclasslist('all','0');
                   2217:     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
                   2218:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
1.49      albertel 2219:     $result.='&nbsp;<b>';
                   2220:     if ($handgrade eq 'yes') {
                   2221: 	$result.="View/Grade ";
                   2222:     } else {
                   2223: 	$result.="View ";
                   2224:     }
                   2225:     $result.='an Individual Student\'s Submission</b></td></tr>'."\n";
1.44      ng       2226:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   2227:     $result.='<form action="/adm/grades" method="post">'."\n".
                   2228: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   2229: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
                   2230: 	'<input type="hidden" name="response" value="'.$response.'" />'."\n".
                   2231: 	'<input type="hidden" name="handgrade" value="'.$handgrade.'" />'."\n".
                   2232: 	'<input type="hidden" name="showgrading" value="yes" />'."\n".
                   2233: 	'<input type="hidden" name="command" value="submission" />'."\n";
1.26      albertel 2234: 
1.44      ng       2235:     $result.='&nbsp;<b>Select section:</b> <select name="section">'."\n";
1.49      albertel 2236:     if (ref($sections)) {
                   2237: 	foreach (sort (@$sections)) {$result.='<option>'.$_.'</option>'."\n";}
1.44      ng       2238:     }
                   2239:     $result.= '<option selected="on">all</select>'."\n";
                   2240:     $result.='&nbsp;&nbsp;<b>Display students who has: </b>'.
                   2241: 	'<input type="radio" name="submitonly" value="yes" checked> submitted'.
                   2242: 	'<input type="radio" name="submitonly" value="all"> everybody <br />';
1.49      albertel 2243:     if (ref($sections)) {
                   2244: 	$result.='&nbsp;(Section "no" implies the students were not assigned a section.)<br />' 
                   2245: 	    if (grep /no/,@$sections);
                   2246:     }
                   2247: 
                   2248: 
                   2249:     $result.='<br />&nbsp;<input type="button" onClick="submit();" value="';
                   2250:     if ($handgrade eq 'yes') {
                   2251: 	$result.="View/Grade";
                   2252:     } else {
                   2253: 	$result.="View";
                   2254:     }
                   2255:     $result.='" />'."\n".'</form>'."\n";
1.44      ng       2256:     $result.='</td></tr></table>'."\n";
                   2257:     $result.='</td></tr></table>'."\n";
                   2258:     return $result;
1.2       albertel 2259: }
                   2260: 
1.44      ng       2261: #--- Form to input a receipt number ---
                   2262: sub verifyReceipt_form {
                   2263:     my ($symb,$url) = @_;
1.46      ng       2264:     my $result = '<script type="text/javascript" language="javascript">'."\n".
                   2265: 	'  function checkEntry(formname) {'."\n".
                   2266: 	'    var receipt = formname.receipt.value;'."\n".
                   2267: 	'    if (isNaN(receipt) || receipt == "") {'."\n".
                   2268: 	'       alert("Please enter a receipt number given by a student in the box.");'."\n".
                   2269: 	'       return false;'."\n".
                   2270: 	'    }'."\n".
                   2271: 	'    formname.submit();'."\n".
                   2272: 	'  }'."\n".
                   2273: 	'</script>'."\n";
                   2274: 
1.44      ng       2275:     my $hostver=unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'});
                   2276: 
1.46      ng       2277:     $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
1.44      ng       2278:     $result.='<table width=100% border=0><tr><td bgcolor=#e6ffff>'."\n";
                   2279:     $result.='&nbsp;<b>Verify a Submission Receipt Issued by this Server</td></tr>'."\n";
                   2280:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
1.46      ng       2281:     $result.='<form action="/adm/grades" method="post" name="verifyform">'."\n";
1.44      ng       2282:     $result.='&nbsp;<tt>'.$hostver.'-<input type="text" name="receipt" size="4"></tt><br />'."\n";
1.46      ng       2283:     $result.='&nbsp;<input type="button" onClick="javascript:checkEntry(this.form);"'.
                   2284: 	' value="Verify Receipt">'."\n";
1.44      ng       2285:     $result.='<input type="hidden" name="command" value="verify">'."\n";
                   2286:     if ($ENV{'form.url'}) {
                   2287: 	$result.='<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />';
                   2288:     }
                   2289:     if ($ENV{'form.symb'}) {
                   2290: 	$result.='<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />';
                   2291:     }
                   2292:     $result.='</form>';
                   2293:     $result.='</td></tr></table>'."\n";
                   2294:     $result.='</td></tr></table>'."\n";
                   2295:     return $result;
1.2       albertel 2296: }
                   2297: 
1.1       albertel 2298: sub handler {
1.41      ng       2299:     my $request=$_[0];
                   2300:     
                   2301:     if ($ENV{'browser.mathml'}) {
                   2302: 	$request->content_type('text/xml');
                   2303:     } else {
                   2304: 	$request->content_type('text/html');
                   2305:     }
                   2306:     $request->send_http_header;
1.44      ng       2307:     return '' if $request->header_only;
1.41      ng       2308:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                   2309:     my $url=$ENV{'form.url'};
                   2310:     my $symb=$ENV{'form.symb'};
                   2311:     my $command=$ENV{'form.command'};
                   2312:     if (!$url) {
                   2313: 	my ($temp1,$temp2);
                   2314: 	($temp1,$temp2,$ENV{'form.url'})=split(/___/,$symb);
                   2315: 	$url = $ENV{'form.url'};
                   2316:     }
                   2317:     &send_header($request);
                   2318:     if ($url eq '' && $symb eq '') {
                   2319: 	if ($ENV{'user.adv'}) {
                   2320: 	    if (($ENV{'form.codeone'}) && ($ENV{'form.codetwo'}) &&
                   2321: 		($ENV{'form.codethree'})) {
                   2322: 		my $token=$ENV{'form.codeone'}.'*'.$ENV{'form.codetwo'}.'*'.
                   2323: 		    $ENV{'form.codethree'};
                   2324: 		my ($tsymb,$tuname,$tudom,$tcrsid)=
                   2325: 		    &Apache::lonnet::checkin($token);
                   2326: 		if ($tsymb) {
                   2327: 		    my ($map,$id,$url)=split(/\_\_\_/,$tsymb);
                   2328: 		    if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
                   2329: 			$request->print(
                   2330: 					&Apache::lonnet::ssi('/res/'.$url,
                   2331: 							     ('grade_username' => $tuname,
                   2332: 							      'grade_domain' => $tudom,
                   2333: 							      'grade_courseid' => $tcrsid,
                   2334: 							      'grade_symb' => $tsymb)));
                   2335: 		    } else {
1.45      ng       2336: 			$request->print('<h3>Not authorized: '.$token.'</h3>');
1.41      ng       2337: 		    }           
                   2338: 		} else {
1.45      ng       2339: 		    $request->print('<h3>Not a valid DocID: '.$token.'</h3>');
1.41      ng       2340: 		}
1.14      www      2341: 	    } else {
1.41      ng       2342: 		$request->print(&Apache::lonxml::tokeninputfield());
                   2343: 	    }
                   2344: 	}
                   2345:     } else {
                   2346: 	$Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                   2347: 	if ($command eq 'submission') {
                   2348: 	    &listStudents($request) if ($ENV{'form.student'} eq '');
                   2349: 	    &submission($request,0,0) if ($ENV{'form.student'} ne '');
                   2350: 	} elsif ($command eq 'processGroup') {
                   2351: 	    &processGroup($request);
                   2352: 	} elsif ($command eq 'gradingmenu') {
                   2353: 	    $request->print(&gradingmenu($request));
                   2354: 	} elsif ($command eq 'viewgrades') {
                   2355: 	    $request->print(&viewgrades($request));
                   2356: 	} elsif ($command eq 'handgrade') {
                   2357: 	    $request->print(&processHandGrade($request));
                   2358: 	} elsif ($command eq 'editgrades') {
                   2359: 	    $request->print(&editgrades($request));
                   2360: 	} elsif ($command eq 'verify') {
                   2361: 	    $request->print(&verifyreceipt($request));
                   2362: 	} elsif ($command eq 'csvupload') {
                   2363: 	    $request->print(&csvupload($request));
                   2364: 	} elsif ($command eq 'viewclasslist') {
                   2365: 	    $request->print(&viewclasslist($request));
                   2366: 	} elsif ($command eq 'csvuploadmap') {
                   2367: 	    $request->print(&csvuploadmap($request));
                   2368: 	} elsif ($command eq 'csvuploadassign') {
                   2369: 	    if ($ENV{'form.associate'} ne 'Reverse Association') {
                   2370: 		$request->print(&csvuploadassign($request));
                   2371: 	    } else {
                   2372: 		if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
                   2373: 		    $ENV{'form.upfile_associate'} = 'reverse';
                   2374: 		} else {
                   2375: 		    $ENV{'form.upfile_associate'} = 'forward';
                   2376: 		}
                   2377: 		$request->print(&csvuploadmap($request));
                   2378: 	    }
1.26      albertel 2379: 	} else {
1.41      ng       2380: 	    $request->print("Unknown action: $command:");
1.26      albertel 2381: 	}
1.2       albertel 2382:     }
1.41      ng       2383:     &send_footer($request);
1.44      ng       2384:     return '';
                   2385: }
                   2386: 
                   2387: sub send_header {
                   2388:     my ($request)= @_;
                   2389:     $request->print(&Apache::lontexconvert::header());
                   2390: #  $request->print("
                   2391: #<script>
                   2392: #remotewindow=open('','homeworkremote');
                   2393: #remotewindow.close();
                   2394: #</script>"); 
1.47      www      2395:     $request->print(&Apache::loncommon::bodytag('Grading'));
1.44      ng       2396: }
                   2397: 
                   2398: sub send_footer {
                   2399:     my ($request)= @_;
                   2400:     $request->print('</body>');
                   2401:     $request->print(&Apache::lontexconvert::footer());
1.1       albertel 2402: }
                   2403: 
                   2404: 1;
                   2405: 
1.13      albertel 2406: __END__;

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