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

1.17      albertel    1: # The LearningOnline Network with CAPA
1.13      albertel    2: # The LON-CAPA Grading handler
1.17      albertel    3: #
1.57    ! matthew     4: # $Id: grades.pm,v 1.56 2002/10/16 19:23:48 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".
1.57    ! matthew   342: 	'&nbsp;<b>View Problem: </b><input type="radio" name="vProb" value="no" > no '."\n".
        !           343: 	'<input type="radio" name="vProb" value="yes" checked > 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.'.$_.
1.57    ! matthew   919: 					    '.maxcollaborators',
        !           920:                                             $symb,$udom,$uname);
        !           921: 	    next if ($ncol <= 0);
        !           922:             s/\_/\./g;
        !           923:             next if ($record{'resource.'.$_.'.collaborators'} eq '');
        !           924:             my (@collaborators) = split(/,?\s+/,
        !           925:                                    $record{'resource.'.$_.'.collaborators'});
        !           926:             my (@badcollaborators);
        !           927:             if (scalar(@collaborators) != 0) {
        !           928:                 $result.='<b>Collaborators: </b>';
        !           929:                 foreach my $collaborator (@collaborators) {
        !           930:                     my ($co_name,$co_dom) = split /\@|:/,$collaborator;
        !           931:                     $co_dom = $udom if (! defined($co_dom));
        !           932:                     next if ($co_name eq $uname && $co_dom eq $udom);
        !           933:                     # Doing this grep allows 'fuzzy' specification
        !           934:                     my @Matches = grep /^$co_name:$co_dom/i,
        !           935:                     keys %$classlist;
        !           936:                     if (! scalar(@Matches)) {
        !           937:                         push @badcollaborators,$collaborator;
        !           938:                         next;
        !           939:                     }
        !           940:                     push @col_list, @Matches;
        !           941:                     foreach (@Matches) {
        !           942:                         my ($lastname,$givenn) = split(/,/,$$fullname{$_});
        !           943:                         push @col_fullnames, $givenn.' '.$lastname;
        !           944:                         $result.=$$fullname{$_}.'&nbsp; &nbsp; &nbsp;';
        !           945:                     }
        !           946:                 }
        !           947:                 $result.='<br />'."\n";
        !           948:                 if (scalar(@badcollaborators) > 0) {
        !           949:                     $result.='<table border="0"><tr bgcolor="#ffbbbb"><td>';
        !           950:                     $result.='This student has submitted ';
        !           951:                     if (scalar(@badcollaborators) == 1) {
        !           952:                         $result .= 'an invalid collaborator';
        !           953:                     } else {
        !           954:                         $result .= 'invalid collaborators';
        !           955:                     }
        !           956:                     $result .= ': '.join(', ',@badcollaborators);
        !           957:                     
        !           958:                 }
        !           959:                 if (scalar(@collaborators > $ncol)) {
        !           960:                     $result .= '<table border="0"><tr bgcolor="#ffbbbb"><td>';
        !           961:                     $result .= 'This student has sumbitted too many '.
        !           962:                         'collaborators.  Maximum is '.$ncol;
        !           963:                     $result .= '</td></tr></table>';
        !           964:                 }
        !           965:                 $result.='<input type="hidden" name="collaborator'.$counter.
        !           966:                     '" value="'.(join ':',@col_list).'" />'."\n";
        !           967:             }
1.41      ng        968: 	}
                    969:     }
1.44      ng        970:     $request->print($result."\n");
1.33      ng        971: 
1.44      ng        972:     # print student answer/submission
                    973:     # Options are (1) Handgaded submission only
                    974:     #             (2) Last submission, includes submission that is not handgraded 
                    975:     #                  (for multi-response type part)
                    976:     #             (3) Last submission plus the parts info
                    977:     #             (4) The whole record for this student
1.41      ng        978:     if ($ENV{'form.lastSub'} =~ /^(lastonly|hdgrade)$/) {
                    979: 	if ($ENV{'form.'.$uname.':'.$udom.':submitted_by'}) {
1.44      ng        980: 	    my $submitby=''.
1.41      ng        981: 		'<b>Collaborative submission by: </b>'.
1.44      ng        982: 		'<a href="javascript:viewSubmitter(\''.
                    983: 		$ENV{'form.'.$uname.':'.$udom.':submitted_by'}.
1.41      ng        984: 		'\')"; TARGET=_self>'.
                    985: 		$$fullname{$ENV{'form.'.$uname.':'.$udom.':submitted_by'}}.'</a>';
                    986: 	    $request->print($submitby);
                    987: 	} else {
1.44      ng        988: 	    my ($string,$timestamp)=
1.46      ng        989: 		&get_last_submission (%record);
1.44      ng        990: 	    my $lastsubonly.=''.
                    991: 		($$timestamp eq '' ? '' : '<b>Date Submitted:</b> '.
                    992: 		 $$timestamp).'';
1.41      ng        993: 	    if ($$timestamp eq '') {
1.45      ng        994: 		$lastsubonly.='<tr><td bgcolor="#ffffe6">'.$$string[0].'</td></tr>'."\n";
1.41      ng        995: 	    } else {
                    996: 		for my $part (sort keys(%$handgrade)) {
                    997: 		    foreach (@$string) {
                    998: 			my ($partid,$respid) = /^resource\.(\d+)\.(\d+)\.submission/;
                    999: 			if ($part eq ($partid.'_'.$respid)) {
                   1000: 			    my ($ressub,$subval) = split(/:/,$_,2);
1.44      ng       1001: 			    $lastsubonly.='<tr><td bgcolor="#ffffe6"><b>Part '.
                   1002: 				$partid.'</b> <font color="#999999">( ID '.$respid.
                   1003: 				' )</font>&nbsp; &nbsp;<b>Answer: </b>'.
1.45      ng       1004: 				&keywords_highlight($subval).'</td></tr>'."\n"
1.41      ng       1005: 				if ($ENV{'form.lastSub'} eq 'lastonly' || 
1.44      ng       1006: 				    ($ENV{'form.lastSub'} eq 'hdgrade' && 
                   1007: 				     $$handgrade{$part} =~ /:yes$/));
1.41      ng       1008: 			}
                   1009: 		    }
                   1010: 		}
                   1011: 	    }
1.45      ng       1012: 	    $lastsubonly.='</td></tr>'."\n";
1.41      ng       1013: 	    $request->print($lastsubonly);
                   1014: 	}
                   1015:     } else {
                   1016: 	$request->print(&Apache::loncommon::get_previous_attempt($symb,$uname,$udom,
1.44      ng       1017: 								 $ENV{'request.course.id'},
                   1018: 								 $last,'.submission',
                   1019: 								 'Apache::grades::keywords_highlight'));
1.41      ng       1020:     }
                   1021:     
1.44      ng       1022:     # return if view submission with no grading option
1.41      ng       1023:     if ($ENV{'form.showgrading'} eq '') {
1.45      ng       1024: 	$request->print('</td></tr></table></td></tr></table></form>'."\n");
1.41      ng       1025: 	return;
                   1026:     }
1.33      ng       1027: 
1.44      ng       1028:     # Grading options
1.41      ng       1029:     $result='<input type="hidden" name="newmsg'.$counter.'" value="" />'."\n".
                   1030: 	'<input type="hidden" name="includemsg'.$counter.'" value="" />'."\n".
1.45      ng       1031: 	'<input type="hidden" name="unamedom'.$counter.'" value="'.$uname.':'
                   1032: 	.$udom.'" />'."\n";
                   1033:     my ($lastname,$givenn) = split(/,/,$ENV{'form.fullname'});
                   1034:     my $msgfor = $givenn.' '.$lastname;
                   1035:     if (scalar(@col_fullnames) > 0) {
                   1036: 	my $lastone = pop @col_fullnames;
                   1037: 	$msgfor .= ', '.(join ', ',@col_fullnames).' and '.$lastone.'.';
                   1038:     }
                   1039:     $result.='<tr><td bgcolor="#ffffff">'."\n".
                   1040: 	'&nbsp;<a href="javascript:msgCenter(document.SCORE,'.$counter.
                   1041: 	',\''.$msgfor.'\')"; TARGET=_self>'.
                   1042: 	'Compose Message to student'.(scalar(@col_fullnames) >= 1 ? 's' : '').'</a>'.
1.44      ng       1043: 	'<br />&nbsp;(Message will be sent when you click on Save & Next below.)'."\n" 
                   1044: 	if ($ENV{'form.handgrade'} eq 'yes');
1.41      ng       1045:     $request->print($result);
                   1046: 
                   1047:     my %seen = ();
                   1048:     my @partlist;
                   1049:     for (sort keys(%$handgrade)) {
                   1050: 	my ($partid,$respid) = split(/_/);
                   1051: 	next if ($seen{$partid} > 0);
                   1052: 	$seen{$partid}++;
                   1053: 	next if ($$handgrade{$_} =~ /:no$/);
                   1054: 	push @partlist,$partid;
                   1055: 	my $wgt    = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb,$udom,$uname);
1.44      ng       1056: 	my $wgtmsg = ($wgt > 0 ? '(problem weight)' : 
                   1057: 		      '<font color="red">problem weight assigned by computer</font>');
1.41      ng       1058: 	$wgt       = ($wgt > 0 ? $wgt : '1');
                   1059: 	my $score  = ($record{'resource.'.$partid.'.awarded'} eq '' ?
                   1060: 		      '' : $record{'resource.'.$partid.'.awarded'}*$wgt);
                   1061: 	$result='<input type="hidden" name="WGT'.$counter.'_'.$partid.'" value="'.$wgt.'" />';
1.44      ng       1062: 	$result.='<table border="0"><tr><td><b>Part </b>'.$partid.' <b>Points: </b></td><td>';
1.41      ng       1063: 
                   1064: 	my $ctr = 0;
                   1065: 	$result.='<table border="0"><tr>';  # display radio buttons in a nice table 10 across
                   1066: 	while ($ctr<=$wgt) {
                   1067: 	    $result.= '<td><input type="radio" name="RADVAL'.$counter.'_'.$partid.'" '.
1.43      ng       1068: 		'onclick="javascript:writeBox(this.form.GD_BOX'.$counter.'_'.$partid.
                   1069: 		',this.form.GD_SEL'.$counter.'_'.$partid.','.$ctr.
1.41      ng       1070: 		',this.form.stores'.$counter.'_'.$partid.')" '.
                   1071: 		($score eq $ctr ? 'checked':'').' /> '.$ctr."</td>\n";
                   1072: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
                   1073: 	    $ctr++;
                   1074: 	}
                   1075: 	$result.='</tr></table>';
1.39      ng       1076: 
1.41      ng       1077: 	$result.='</td><td>&nbsp;<b>or</b>&nbsp;</td>';
1.43      ng       1078: 	$result.='<td><input type="text" name="GD_BOX'.$counter.'_'.$partid.'"'.
1.41      ng       1079: 	    ($score ne ''? ' value = "'.$score.'"':'').' size="4" '.
                   1080: 	    'onChange="javascript:updateRadio(this.form.RADVAL'.$counter.'_'.$partid.
1.43      ng       1081: 	    ',this.form.GD_BOX'.$counter.'_'.$partid.
                   1082: 	    ',this.form.GD_SEL'.$counter.'_'.$partid.
1.44      ng       1083: 	    ',this.form.stores'.$counter.'_'.$partid.
                   1084: 	    ','.$wgt.')" /></td>'."\n";
1.41      ng       1085: 	$result.='<td>/'.$wgt.' '.$wgtmsg.' </td><td>';
                   1086: 
1.43      ng       1087: 	$result.='<select name="GD_SEL'.$counter.'_'.$partid.'" '.
1.41      ng       1088: 	    'onChange="javascript:clearRadBox(this.form.RADVAL'.$counter.'_'.$partid.
1.43      ng       1089: 	    ',this.form.GD_BOX'.$counter.'_'.$partid.
                   1090: 	    ',this.form.GD_SEL'.$counter.'_'.$partid.
1.41      ng       1091: 	    ',this.form.stores'.$counter.'_'.$partid.')" />'."\n".
                   1092: 	    '<option selected="on"> </option>'.
                   1093: 	    '<option>excused</option></select>'."&nbsp&nbsp\n";
                   1094: 	$result.='<input type="hidden" name="stores'.$counter.'_'.$partid.'" value="0" />';
1.45      ng       1095: 	$result.='</td></tr></table>'."\n";
1.41      ng       1096: 	$request->print($result);
                   1097:     }
1.45      ng       1098:     $result='<input type="hidden" name="partlist'.$counter.
                   1099: 	'" value="'.(join ":",@partlist).'" />'."\n";
                   1100:     my $ctr = 0;
                   1101:     while ($ctr < scalar(@partlist)) {
                   1102: 	$result.='<input type="hidden" name="partid'.$counter.'_'.$ctr.'" value="'.
                   1103: 	    $partlist[$ctr].'" />'."\n";
                   1104: 	$ctr++;
                   1105:     }
                   1106:     $request->print($result.'</td></tr></table></td></tr></table>'."\n");
1.41      ng       1107: 
                   1108:     # print end of form
                   1109:     if ($counter == $total) {
1.45      ng       1110: 	my $endform='<table border="0"><tr><td>'.
                   1111: 	    '<input type="hidden" name="gradeOpt" value="" />'."\n";
                   1112: 	if ($ENV{'form.handgrade'} eq 'yes') {
                   1113: 	    $endform.='<input type="button" value="Save & Next" '.
                   1114: 		'onClick="javascript:checksubmit(\'Save & Next\','.
                   1115: 		$total.','.scalar(@partlist).');" TARGET=_self> &nbsp;'."\n";
                   1116: 	    my $ntstu ='<select name="NTSTU">'.
                   1117: 		'<option>1</option><option>2</option>'.
                   1118: 		'<option>3</option><option>5</option>'.
                   1119: 		'<option>7</option><option>10</option></select>'."\n";
                   1120: 	    my $nsel = ($ENV{'form.NTSTU'} ne '' ? $ENV{'form.NTSTU'} : '1');
                   1121: 	    $ntstu =~ s/<option>$nsel</<option selected="on">$nsel</;
                   1122: 	    $endform.=$ntstu.'student(s) &nbsp;&nbsp;';
                   1123: 	} else {
                   1124: 	    $endform.='<input type="hidden" name="NTSTU" value="1" />'."\n";
                   1125: 	}
                   1126: 	$endform.='<input type="button" value="Next" '.
                   1127: 	    'onClick="javascript:checksubmit(\'Next\');" TARGET=_self> &nbsp;'."\n".
                   1128: 	    '<input type="button" value="Previous" '.
                   1129: 	    'onClick="javascript:checksubmit(\'Previous\');" TARGET=_self> &nbsp;';
                   1130: 	$endform.='(Next and Previous do not save the scores.)'."\n" 
                   1131: 	    if ($ENV{'form.handgrade'} eq 'yes');
                   1132: 	$endform.='</td><tr></table></form>';
1.50      albertel 1133: 	$endform.=&show_grading_menu_form($symb,$url);
1.41      ng       1134: 	$request->print($endform);
                   1135:     }
                   1136:     return '';
1.38      ng       1137: }
                   1138: 
1.44      ng       1139: #--- Retrieve the last submission for all the parts
1.38      ng       1140: sub get_last_submission {
1.46      ng       1141:     my (%returnhash)=@_;
                   1142:     my (@string,$timestamp);
                   1143:     if ($returnhash{'version'}) {
                   1144: 	my %lasthash=();
                   1145: 	my ($version);
                   1146: 	for ($version=1;$version<=$returnhash{'version'};$version++) {
                   1147: 	    foreach (sort(split(/\:/,$returnhash{$version.':keys'}))) {
                   1148: 		$lasthash{$_}=$returnhash{$version.':'.$_};
                   1149: 		if ($returnhash{$version.':'.$_} =~ /(SUBMITTED|DRAFT)$/) {
                   1150: 		   $timestamp = scalar(localtime($returnhash{$version.':timestamp'}));
                   1151: 	       } 
                   1152: 	    }
                   1153: 	}
                   1154: 	foreach ((keys %lasthash)) {
                   1155: 	    if ($_ =~ /\.submission$/) {
                   1156: 		my ($partid,$foo) = split(/submission$/,$_);
                   1157: 		my $draft  = $lasthash{$partid.'awarddetail'} eq 'DRAFT' ?
                   1158: 		    '<font color="red">Draft Copy</font> ' : '';
                   1159: 		push @string, (join(':',$_,$draft.$lasthash{$_}));
1.41      ng       1160: 	    }
                   1161: 	}
                   1162:     }
1.46      ng       1163:     @string = $string[0] eq '' ? 'Nothing submitted - no attempts.' : @string;
                   1164:     return \@string,\$timestamp;
1.38      ng       1165: }
1.35      ng       1166: 
1.44      ng       1167: #--- High light keywords, with style choosen by user.
1.38      ng       1168: sub keywords_highlight {
1.44      ng       1169:     my $string    = shift;
                   1170:     my $size      = $ENV{'form.kwsize'} eq '0' ? '' : 'size='.$ENV{'form.kwsize'};
                   1171:     my $styleon   = $ENV{'form.kwstyle'} eq ''  ? '' : $ENV{'form.kwstyle'};
1.41      ng       1172:     (my $styleoff = $styleon) =~ s/\</\<\//;
1.44      ng       1173:     my @keylist   = split(/[,\s+]/,$ENV{'form.keywords'});
1.41      ng       1174:     foreach (@keylist) {
                   1175: 	$string =~ s/\b$_(\b|\.)/\<font color\=$ENV{'form.kwclr'} $size\>$styleon$_$styleoff\<\/font\>/gi;
                   1176:     }
1.57    ! matthew  1177:     # This is not really the right place to do this, but I cannot find a
        !          1178:     # better one at this time.  So here we go - the m in the s:::mg causes
        !          1179:     # ^ to match the beginning of a new line.  So we replace(???) the beginning
        !          1180:     # of the line with <br /> to make things formatted a little better.
        !          1181:     $string =~ s:^:<br />:mg;
1.41      ng       1182:     return $string;
1.38      ng       1183: }
1.36      ng       1184: 
1.44      ng       1185: #--- Called from submission routine
1.38      ng       1186: sub processHandGrade {
1.41      ng       1187:     my ($request) = shift;
                   1188:     my $url    = $ENV{'form.url'};
                   1189:     my $symb   = $ENV{'form.symb'};
                   1190:     my $button = $ENV{'form.gradeOpt'};
                   1191:     my $ngrade = $ENV{'form.NCT'};
                   1192:     my $ntstu  = $ENV{'form.NTSTU'};
                   1193: 
1.44      ng       1194:     if ($button eq 'Save & Next') {
                   1195: 	my $ctr = 0;
                   1196: 	while ($ctr < $ngrade) {
                   1197: 	    my ($uname,$udom) = split(/:/,$ENV{'form.unamedom'.$ctr});
1.45      ng       1198: 	    my ($errorflag) = &saveHandGrade($request,$url,$symb,$uname,$udom,$ctr);
1.44      ng       1199: 
                   1200: 	    my $includemsg = $ENV{'form.includemsg'.$ctr};
                   1201: 	    my ($subject,$message,$msgstatus) = ('','','');
                   1202: 	    if ($includemsg =~ /savemsg|new$ctr/) {
                   1203: 		$subject = $ENV{'form.msgsub'} if ($includemsg =~ /^msgsub/);
                   1204: 		my (@msgnum) = split(/,/,$includemsg);
                   1205: 		foreach (@msgnum) {
                   1206: 		    $message.=$ENV{'form.'.$_} if ($_ =~ /savemsg|newmsg/ && $_ ne '');
                   1207: 		}
                   1208: 		$message =~ s/\s+/ /g;
                   1209: 		$msgstatus = &Apache::lonmsg::user_normal_msg ($uname,$udom,
                   1210: 							       $ENV{'form.msgsub'},$message);
                   1211: 	    }
                   1212: 	    if ($ENV{'form.collaborator'.$ctr}) {
                   1213: 		my (@collaborators) = split(/:/,$ENV{'form.collaborator'.$ctr});
                   1214: 		foreach (@collaborators) {
                   1215: 		    &saveHandGrade($request,$url,$symb,$_,$udom,$ctr,
                   1216: 				   $ENV{'form.unamedom'.$ctr});
                   1217: 		    if ($message ne '') {
                   1218: 			$msgstatus = &Apache::lonmsg::user_normal_msg ($_,$udom,
                   1219: 								       $ENV{'form.msgsub'},
                   1220: 								       $message);
                   1221: 		    }
                   1222: 		}
                   1223: 	    }
                   1224: 	    $ctr++;
                   1225: 	}
                   1226:     }
                   1227: 
                   1228:     # Keywords sorted in alphabatical order
1.41      ng       1229:     my $loginuser = $ENV{'user.name'}.':'.$ENV{'user.domain'};
                   1230:     my %keyhash = ();
                   1231:     $ENV{'form.keywords'}           =~ s/,\s{0,}|\s+/ /g;
                   1232:     $ENV{'form.keywords'}           =~ s/^\s+|\s+$//;
1.44      ng       1233:     my (@keywords) = sort(split(/\s+/,$ENV{'form.keywords'}));
                   1234:     $ENV{'form.keywords'} = join(' ',@keywords);
1.41      ng       1235:     $keyhash{$symb.'_keywords'}     = $ENV{'form.keywords'};
                   1236:     $keyhash{$symb.'_subject'}      = $ENV{'form.msgsub'};
                   1237:     $keyhash{$loginuser.'_kwclr'}   = $ENV{'form.kwclr'};
                   1238:     $keyhash{$loginuser.'_kwsize'}  = $ENV{'form.kwsize'};
                   1239:     $keyhash{$loginuser.'_kwstyle'} = $ENV{'form.kwstyle'};
                   1240: 
1.44      ng       1241:     # message center - Order of message gets changed. Blank line is eliminated.
                   1242:     # New messages are saved in ENV for the next student.
                   1243:     # All messages are saved in nohist_handgrade.db
1.41      ng       1244:     my ($ctr,$idx) = (1,1);
                   1245:     while ($ctr <= $ENV{'form.savemsgN'}) {
                   1246: 	if ($ENV{'form.savemsg'.$ctr} ne '') {
                   1247: 	    $keyhash{$symb.'_savemsg'.$idx} = $ENV{'form.savemsg'.$ctr};
                   1248: 	    $idx++;
                   1249: 	}
                   1250: 	$ctr++;
                   1251:     }
                   1252:     $ctr = 0;
                   1253:     while ($ctr < $ngrade) {
                   1254: 	if ($ENV{'form.newmsg'.$ctr} ne '') {
                   1255: 	    $keyhash{$symb.'_savemsg'.$idx} = $ENV{'form.newmsg'.$ctr};
                   1256: 	    $ENV{'form.savemsg'.$idx} = $ENV{'form.newmsg'.$ctr};
                   1257: 	    $idx++;
                   1258: 	}
                   1259: 	$ctr++;
                   1260:     }
                   1261:     $ENV{'form.savemsgN'} = --$idx;
                   1262:     $keyhash{$symb.'_savemsgN'} = $ENV{'form.savemsgN'};
                   1263:     my $putresult = &Apache::lonnet::put
                   1264: 	('nohist_handgrade',\%keyhash,
                   1265: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1266: 	 $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                   1267: 
1.44      ng       1268:     # Called by Save & Refresh from Highlight Attribute Window
1.41      ng       1269:     if ($ENV{'form.refresh'} eq 'on') {
                   1270: 	my $ctr = 0;
                   1271: 	$ENV{'form.NTSTU'}=$ngrade;
                   1272: 	while ($ctr < $ngrade) {
1.44      ng       1273: 	    ($ENV{'form.student'},$ENV{'form.userdom'}) = split(/:/,$ENV{'form.unamedom'.$ctr});
1.41      ng       1274: 	    &submission($request,$ctr,$ngrade-1);
                   1275: 	    $ctr++;
                   1276: 	}
                   1277: 	return '';
                   1278:     }
1.36      ng       1279: 
1.44      ng       1280:     # Get the next/previous one or group of students
1.41      ng       1281:     my $firststu = $ENV{'form.unamedom0'};
                   1282:     my $laststu = $ENV{'form.unamedom'.($ngrade-1)};
                   1283:     $ctr = 2;
                   1284:     while ($laststu eq '') {
                   1285: 	$laststu  = $ENV{'form.unamedom'.($ngrade-$ctr)};
                   1286: 	$ctr++;
                   1287: 	$laststu = $firststu if ($ctr > $ngrade);
                   1288:     }
1.44      ng       1289: 
1.56      matthew  1290:     my (undef,undef,$fullname) = &getclasslist($ENV{'form.section'},'0');
1.41      ng       1291:     my (@parsedlist,@nextlist);
                   1292:     my ($nextflg) = 0;
1.53      albertel 1293:     foreach (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
1.41      ng       1294: 	if ($nextflg == 1 && $button =~ /Next$/) {
                   1295: 	    push @parsedlist,$_;
                   1296: 	}
                   1297: 	$nextflg = 1 if ($_ eq $laststu);
                   1298: 	if ($button eq 'Previous') {
                   1299: 	    last if ($_ eq $firststu);
                   1300: 	    push @parsedlist,$_;
                   1301: 	}
                   1302:     }
                   1303:     $ctr = 0;
                   1304:     my ($partlist,$handgrade) = &response_type($ENV{'form.url'});
                   1305:     @parsedlist = reverse @parsedlist if ($button eq 'Previous');
                   1306:     foreach my $student (@parsedlist) {
                   1307: 	my ($uname,$udom) = split(/:/,$student);
                   1308: 	if ($ENV{'form.submitonly'} eq 'yes') {
1.44      ng       1309: 	    my (%status) = &student_gradeStatus($ENV{'form.url'},$symb,$udom,$uname,$partlist) ;
1.41      ng       1310: 	    my $statusflg = '';
                   1311: 	    foreach (keys(%status)) {
                   1312: 		$statusflg = 1 if ($status{$_} ne 'nothing');
1.44      ng       1313: 		my ($foo,$partid,$foo1) = split(/\./);
1.41      ng       1314: 		$statusflg = '' if ($status{'resource.'.$partid.'.submitted_by'} ne '');
                   1315: 	    }
                   1316: 	    next if ($statusflg eq '');
                   1317: 	}
                   1318: 	push @nextlist,$student if ($ctr < $ntstu);
                   1319: 	$ctr++;
                   1320:     }
1.36      ng       1321: 
1.41      ng       1322:     $ctr = 0;
                   1323:     my $total = scalar(@nextlist)-1;
1.39      ng       1324: 
1.41      ng       1325:     foreach (sort @nextlist) {
                   1326: 	my ($uname,$udom,$submitter) = split(/:/);
1.44      ng       1327: 	$ENV{'form.student'}  = $uname;
                   1328: 	$ENV{'form.userdom'}  = $udom;
1.41      ng       1329: 	$ENV{'form.fullname'} = $$fullname{$_};
                   1330: #	$ENV{'form.'.$_.':submitted_by'} = $submitter;
                   1331: #	print "submitter=$ENV{'form.'.$_.':submitted_by'}= $submitter:<br>";
                   1332: 	&submission($request,$ctr,$total);
                   1333: 	$ctr++;
                   1334:     }
                   1335:     if ($total < 0) {
                   1336: 	my $the_end = '<h3><font color="red">LON-CAPA User Message</font></h3><br />'."\n";
                   1337: 	$the_end.='<b>Message: </b> No more students for this section or class.<br /><br />'."\n";
                   1338: 	$the_end.='Click on the button below to return to the grading menu.<br /><br />'."\n";
                   1339: 	$the_end.=&show_grading_menu_form ($symb,$url);
                   1340: 	$request->print($the_end);
                   1341:     }
                   1342:     return '';
1.38      ng       1343: }
1.36      ng       1344: 
1.44      ng       1345: #---- Save the score and award for each student, if changed
1.38      ng       1346: sub saveHandGrade {
1.41      ng       1347:     my ($request,$url,$symb,$stuname,$domain,$newflg,$submitter) = @_;
1.44      ng       1348:     my %record=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},$domain,$stuname);
1.41      ng       1349:     my %newrecord;
                   1350:     foreach (split(/:/,$ENV{'form.partlist'.$newflg})) {
1.43      ng       1351: 	if ($ENV{'form.GD_SEL'.$newflg.'_'.$_} eq 'excused') {
1.44      ng       1352: 	    $newrecord{'resource.'.$_.'.solved'} = 'excused' 
                   1353: 		if ($record{'resource.'.$_.'.solved'} ne 'excused');
1.41      ng       1354: 	} else {
1.44      ng       1355: 	    my $pts = ($ENV{'form.GD_BOX'.$newflg.'_'.$_} ne '' ? 
                   1356: 		       $ENV{'form.GD_BOX'.$newflg.'_'.$_} : 
                   1357: 		       $ENV{'form.RADVAL'.$newflg.'_'.$_});
                   1358: 	    my $wgt = $ENV{'form.WGT'.$newflg.'_'.$_} eq '' ? 1 : 
                   1359: 		$ENV{'form.WGT'.$newflg.'_'.$_};
1.41      ng       1360: 	    my $partial= $pts/$wgt;
1.44      ng       1361: 	    $newrecord{'resource.'.$_.'.awarded'}  = $partial 
                   1362: 		if ($record{'resource.'.$_.'.awarded'} ne $partial);
                   1363: 	    my $reckey = 'resource.'.$_.'.solved';
1.41      ng       1364: 	    if ($partial == 0) {
1.44      ng       1365: 		$newrecord{$reckey} = 'incorrect_by_override' 
                   1366: 		    if ($record{$reckey} ne 'incorrect_by_override');
1.41      ng       1367: 	    } else {
1.44      ng       1368: 		$newrecord{$reckey} = 'correct_by_override' 
                   1369: 		    if ($record{$reckey} ne 'correct_by_override');
1.41      ng       1370: 	    }
1.44      ng       1371: 	    $newrecord{'resource.'.$_.'.submitted_by'} = $submitter 
                   1372: 		if ($submitter && ($record{'resource.'.$_.'.submitted_by'} ne $submitter));
1.41      ng       1373: 	}
                   1374:     }
1.44      ng       1375: 
                   1376:     if (scalar(keys(%newrecord)) > 0) {
1.41      ng       1377: 	$newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
1.44      ng       1378: 	&Apache::lonnet::cstore(\%newrecord,$symb,
                   1379: 				$ENV{'request.course.id'},$domain,$stuname);
1.41      ng       1380:     }
                   1381:     return '';
1.36      ng       1382: }
1.38      ng       1383: 
1.44      ng       1384: #--------------------------------------------------------------------------------------
                   1385: #
                   1386: #-------------------------- Next few routines handles grading by section or whole class
                   1387: #
                   1388: #--- Javascript to handle grading by section or whole class
1.42      ng       1389: sub viewgrades_js {
                   1390:     my ($request) = shift;
                   1391: 
1.41      ng       1392:     $request->print(<<VIEWJAVASCRIPT);
                   1393: <script type="text/javascript" language="javascript">
1.45      ng       1394:    function writePoint(partid,weight,point) {
1.42      ng       1395: 	var radioButton = eval("document.classgrade.RADVAL_"+partid);
                   1396: 	var textbox = eval("document.classgrade.TEXTVAL_"+partid);
                   1397: 	if (point == "textval") {
                   1398: 	    var point = eval("document.classgrade.TEXTVAL_"+partid+".value");
                   1399: 	    if (isNaN(point) || point < 0) {
                   1400: 		alert("A number equal or greater than 0 is expected. Entered value = "+point);
                   1401: 		var resetbox = false;
                   1402: 		for (var i=0; i<radioButton.length; i++) {
                   1403: 		    if (radioButton[i].checked) {
                   1404: 			textbox.value = i;
                   1405: 			resetbox = true;
                   1406: 		    }
                   1407: 		}
                   1408: 		if (!resetbox) {
                   1409: 		    textbox.value = "";
                   1410: 		}
                   1411: 		return;
                   1412: 	    }
1.44      ng       1413: 	    if (point > weight) {
                   1414: 		var resp = confirm("You entered a value ("+point+
                   1415: 				   ") greater than the weight for the part. Accept?");
                   1416: 		if (resp == false) {
                   1417: 		    textbox.value = "";
                   1418: 		    return;
                   1419: 		}
                   1420: 	    }
1.42      ng       1421: 	    for (var i=0; i<radioButton.length; i++) {
                   1422: 		radioButton[i].checked=false;
                   1423: 		if (point == i) {
                   1424: 		    radioButton[i].checked=true;
                   1425: 		}
                   1426: 	    }
1.41      ng       1427: 
1.42      ng       1428: 	} else {
                   1429: 	    textbox.value = point;
                   1430: 	}
1.41      ng       1431: 	for (i=0;i<document.classgrade.total.value;i++) {
1.43      ng       1432: 	    var user = eval("document.classgrade.ctr"+i+".value");
                   1433: 	    var scorename = eval("document.classgrade.GD_"+user+
1.54      albertel 1434: 				 "_"+partid+"_awarded");
1.43      ng       1435: 	    var saveval   = eval("document.classgrade.GD_"+user+
1.54      albertel 1436: 				 "_"+partid+"_solved_s.value");
                   1437: 	    var selname   = eval("document.classgrade.GD_"+user+"_"+partid+"_solved");
1.42      ng       1438: 	    if (saveval != "correct") {
                   1439: 		scorename.value = point;
1.43      ng       1440: 		if (selname[0].selected != true) {
                   1441: 		    selname[0].selected = true;
                   1442: 		}
1.42      ng       1443: 	    }
                   1444: 	}
                   1445: 	var selval   = eval("document.classgrade.SELVAL_"+partid);
                   1446: 	selval[0].selected = true;
                   1447:     }
                   1448: 
                   1449:     function writeRadText(partid,weight) {
                   1450: 	var selval   = eval("document.classgrade.SELVAL_"+partid);
1.43      ng       1451: 	var radioButton = eval("document.classgrade.RADVAL_"+partid);
                   1452: 	var textbox = eval("document.classgrade.TEXTVAL_"+partid);
1.42      ng       1453: 	if (selval[1].selected) {
                   1454: 	    for (var i=0; i<radioButton.length; i++) {
                   1455: 		radioButton[i].checked=false;
                   1456: 
                   1457: 	    }
                   1458: 	    textbox.value = "";
                   1459: 
                   1460: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.43      ng       1461: 		var user = eval("document.classgrade.ctr"+i+".value");
                   1462: 		var scorename = eval("document.classgrade.GD_"+user+
1.54      albertel 1463: 				     "_"+partid+"_awarded");
1.43      ng       1464: 		var saveval   = eval("document.classgrade.GD_"+user+
1.54      albertel 1465: 				     "_"+partid+"_solved_s.value");
1.43      ng       1466: 		var selname   = eval("document.classgrade.GD_"+user+
1.54      albertel 1467: 				     "_"+partid+"_solved");
1.42      ng       1468: 		if (saveval != "correct") {
                   1469: 		    scorename.value = "";
                   1470: 		    selname[1].selected = true;
                   1471: 		}
                   1472: 	    }
1.43      ng       1473: 	} else {
                   1474: 	    for (i=0;i<document.classgrade.total.value;i++) {
                   1475: 		var user = eval("document.classgrade.ctr"+i+".value");
                   1476: 		var scorename = eval("document.classgrade.GD_"+user+
1.54      albertel 1477: 				     "_"+partid+"_awarded");
1.43      ng       1478: 		var saveval   = eval("document.classgrade.GD_"+user+
1.54      albertel 1479: 				     "_"+partid+"_solved_s.value");
1.43      ng       1480: 		var selname   = eval("document.classgrade.GD_"+user+
1.54      albertel 1481: 				     "_"+partid+"_solved");
1.43      ng       1482: 		if (saveval != "correct") {
                   1483: 		    scorename.value = eval("document.classgrade.GD_"+user+
1.54      albertel 1484: 				     "_"+partid+"_awarded_s.value");;
1.43      ng       1485: 		    selname[0].selected = true;
                   1486: 		}
                   1487: 	    }
                   1488: 	}	    
1.42      ng       1489:     }
                   1490: 
                   1491:     function changeSelect(partid,user) {
1.54      albertel 1492: 	var selval = eval("document.classgrade.GD_"+user+'_'+partid+"_solved");
                   1493: 	var textbox = eval("document.classgrade.GD_"+user+'_'+partid+"_awarded");
1.44      ng       1494: 	var point  = textbox.value;
                   1495: 	var weight = eval("document.classgrade.weight_"+partid+".value");
                   1496: 
                   1497: 	if (isNaN(point) || point < 0) {
                   1498: 	    alert("A number equal or greater than 0 is expected. Entered value = "+point);
                   1499: 	    textbox.value = "";
                   1500: 	    return;
                   1501: 	}
                   1502: 	if (point > weight) {
                   1503: 	    var resp = confirm("You entered a value ("+point+
                   1504: 			       ") greater than the weight of the part. Accept?");
                   1505: 	    if (resp == false) {
                   1506: 		textbox.value = "";
                   1507: 		return;
                   1508: 	    }
                   1509: 	}
1.42      ng       1510: 	selval[0].selected = true;
                   1511:     }
                   1512: 
                   1513:     function changeOneScore(partid,user) {
1.54      albertel 1514: 	var selval = eval("document.classgrade.GD_"+user+'_'+partid+"_solved");
1.42      ng       1515: 	if (selval[1].selected) {
1.54      albertel 1516: 	    var boxval = eval("document.classgrade.GD_"+user+'_'+partid+"_awarded");
1.42      ng       1517: 	    boxval.value = "";
                   1518: 	}
                   1519:     }
                   1520: 
                   1521:     function resetEntry(numpart) {
                   1522: 	for (ctpart=0;ctpart<numpart;ctpart++) {
                   1523: 	    var partid = eval("document.classgrade.partid_"+ctpart+".value");
                   1524: 	    var radioButton = eval("document.classgrade.RADVAL_"+partid);
                   1525: 	    var textbox = eval("document.classgrade.TEXTVAL_"+partid);
                   1526: 	    var selval  = eval("document.classgrade.SELVAL_"+partid);
                   1527: 	    for (var i=0; i<radioButton.length; i++) {
                   1528: 		radioButton[i].checked=false;
                   1529: 
                   1530: 	    }
                   1531: 	    textbox.value = "";
                   1532: 	    selval[0].selected = true;
                   1533: 
                   1534: 	    for (i=0;i<document.classgrade.total.value;i++) {
1.43      ng       1535: 		var user = eval("document.classgrade.ctr"+i+".value");
                   1536: 		var resetscore = eval("document.classgrade.GD_"+user+
1.54      albertel 1537: 				      "_"+partid+"_awarded");
1.43      ng       1538: 		resetscore.value = eval("document.classgrade.GD_"+user+
1.54      albertel 1539: 					"_"+partid+"_awarded_s.value");
1.42      ng       1540: 
1.43      ng       1541: 		var saveselval   = eval("document.classgrade.GD_"+user+
1.54      albertel 1542: 				     "_"+partid+"_solved_s.value");
1.42      ng       1543: 
1.54      albertel 1544: 		var selname   = eval("document.classgrade.GD_"+user+"_"+partid+"_solved");
1.42      ng       1545: 		if (saveselval == "excused") {
1.43      ng       1546: 		    if (selname[1].selected == false) { selname[1].selected = true;}
1.42      ng       1547: 		} else {
1.43      ng       1548: 		    if (selname[0].selected == false) {selname[0].selected = true};
1.42      ng       1549: 		}
                   1550: 	    }
1.41      ng       1551: 	}
1.42      ng       1552:     }
                   1553: 
1.41      ng       1554: </script>
                   1555: VIEWJAVASCRIPT
1.42      ng       1556: }
                   1557: 
1.44      ng       1558: #--- show scores for a section or whole class w/ option to change/update a score
1.42      ng       1559: sub viewgrades {
                   1560:     my ($request) = shift;
                   1561:     &viewgrades_js($request);
1.41      ng       1562: 
                   1563:     my ($symb,$url) = ($ENV{'form.symb'},$ENV{'form.url'}); 
1.45      ng       1564:     my $result='<h3><font color="#339933">Manual Grading</font></h3>';
1.38      ng       1565: 
1.43      ng       1566:     $result.='<font size=+1><b>Resource: </b>'.$ENV{'form.url'}.'</font>'."\n";
1.41      ng       1567: 
                   1568:     #view individual student submission form - called using Javascript viewOneStudent
1.45      ng       1569:     $result.=&jscriptNform($url,$symb);
1.41      ng       1570: 
1.44      ng       1571:     #beginning of class grading form
1.41      ng       1572:     $result.= '<form action="/adm/grades" method="post" name="classgrade">'."\n".
                   1573: 	'<input type="hidden" name="symb"    value="'.$symb.'" />'."\n".
                   1574: 	'<input type="hidden" name="url"     value="'.$url.'" />'."\n".
1.38      ng       1575: 	'<input type="hidden" name="command" value="editgrades" />'."\n".
1.41      ng       1576: 	'<input type="hidden" name="section" value="'.$ENV{'form.section'}.'" />'."\n";
1.52      albertel 1577:     $result.='<h3>Assign Common Grade To ';
                   1578:     if ($ENV{'form.section'} eq 'all') {
                   1579: 	$result.='Class </h3>';
                   1580:     } elsif ($ENV{'form.section'} eq 'no') {
                   1581: 	$result.='Students in no Section </h3>';
                   1582:     } else {
                   1583: 	$result.='Students in Section '.$ENV{'form.section'}.'</h3>';
                   1584:     }
                   1585:     $result.= '<table border=0><tr><td bgcolor="#777777">'."\n".
                   1586: 	'<table border=0><tr bgcolor="#ffffdd"><td>';
                   1587: #    $result.='To assign the same score for all the students use the radio buttons or '.
                   1588: #	'text box below. To assign scores individually fill in the score boxes for '.
                   1589: #	'each student in the table below. <font color="red">A part that has already '.
                   1590: #	'been graded does not get changed using the radio buttons or text box. '.
                   1591: #	'If needed, it has to be changed individually.</font>';
                   1592: #    $result.='</td></tr><tr><td>';
1.44      ng       1593:     #radio buttons/text box for assigning points for a section or class.
                   1594:     #handles different parts of a problem
1.42      ng       1595:     my ($partlist,$handgrade) = &response_type($ENV{'form.url'});
                   1596:     my %weight = ();
                   1597:     my $ctsparts = 0;
1.41      ng       1598:     $result.='<table border="0">';
1.45      ng       1599:     my %seen = ();
1.42      ng       1600:     for (sort keys(%$handgrade)) {
1.54      albertel 1601: 	my ($partid,$respid) = split (/_/,$_,2);
1.45      ng       1602: 	next if $seen{$partid};
                   1603: 	$seen{$partid}++;
1.42      ng       1604: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
                   1605: 	my $wgt = &Apache::lonnet::EXT('resource.'.$partid.'.weight',$symb);
                   1606: 	$weight{$partid} = $wgt eq '' ? '1' : $wgt;
                   1607: 
1.44      ng       1608: 	$result.='<input type="hidden" name="partid_'.
                   1609: 	    $ctsparts.'" value="'.$partid.'" />'."\n";
                   1610: 	$result.='<input type="hidden" name="weight_'.
                   1611: 	    $partid.'" value="'.$weight{$partid}.'" />'."\n";
                   1612: 	$result.='<tr><td><b>Part  '.$partid.'&nbsp; &nbsp;Point:</b> </td><td>';
1.42      ng       1613: 	$result.='<table border="0"><tr>';  
1.41      ng       1614: 	my $ctr = 0;
1.42      ng       1615: 	while ($ctr<=$weight{$partid}) { # display radio buttons in a nice table 10 across
                   1616: 	    $result.= '<td><input type="radio" name="RADVAL_'.$partid.'" '.
1.54      albertel 1617: 		'onclick="javascript:writePoint(\''.$partid.'\','.$weight{$partid}.
1.41      ng       1618: 		','.$ctr.')" />'.$ctr."</td>\n";
                   1619: 	    $result.=(($ctr+1)%10 == 0 ? '</tr><tr>' : '');
                   1620: 	    $ctr++;
                   1621: 	}
                   1622: 	$result.='</tr></table>';
1.44      ng       1623: 	$result.= '</td><td><b> or </b><input type="text" name="TEXTVAL_'.
1.54      albertel 1624: 	    $partid.'" size="4" '.'onChange="javascript:writePoint(\''.
                   1625: 		$partid.'\','.$weight{$partid}.',\'textval\')" /> /'.
1.42      ng       1626: 	    $weight{$partid}.' (problem weight)</td>'."\n";
                   1627: 	$result.= '</td><td><select name="SELVAL_'.$partid.'"'.
1.54      albertel 1628: 	    'onChange="javascript:writeRadText(\''.$partid.'\','.
                   1629: 		$weight{$partid}.')" /> '.
1.42      ng       1630: 	    '<option selected="on"> </option>'.
                   1631: 	    '<option>excused</option></select></td></tr>'."\n";
                   1632: 	$ctsparts++;
1.41      ng       1633:     }
1.52      albertel 1634:     $result.='</table>'.'</td></tr></table>'.'</td></tr></table>'."\n".
                   1635: 	'<input type="hidden" name="totalparts" value="'.$ctsparts.'" />';
1.42      ng       1636:     $result.='<input type="button" value="Reset" '.
1.43      ng       1637: 	'onClick="javascript:resetEntry('.$ctsparts.');" TARGET=_self> &nbsp; &nbsp;';
1.45      ng       1638:     $result.='<input type="button" value="Submit Changes" '.
                   1639: 	'onClick="javascript:submit();" TARGET=_self />'."\n";
1.41      ng       1640: 
1.44      ng       1641:     #table listing all the students in a section/class
                   1642:     #header of table
1.52      albertel 1643:     $result.= '<h3>Assign Grade to Specific Students in ';
                   1644:     if ($ENV{'form.section'} eq 'all') {
                   1645: 	$result.='the Class </h3>';
                   1646:     } elsif ($ENV{'form.section'} eq 'no') {
                   1647: 	$result.='no Section </h3>';
                   1648:     } else {
                   1649: 	$result.='Section '.$ENV{'form.section'}.'</h3>';
                   1650:     }
1.42      ng       1651:     $result.= '<table border=0><tr><td bgcolor="#777777">'."\n".
1.41      ng       1652: 	'<table border=0><tr bgcolor="#deffff">'.
1.44      ng       1653: 	'<td><b>Fullname</b></td><td><b>Username</b></td><td><b>Domain</b></td>'."\n";
1.41      ng       1654:     my (@parts) = sort(&getpartlist($url));
                   1655:     foreach my $part (@parts) {
                   1656: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
                   1657: 	if  (!$display) { $display = &Apache::lonnet::metadata($url,$part.'.name'); }
                   1658: 	if ($display =~ /^Partial Credit Factor/) {
1.54      albertel 1659: 	    my ($partid) = &split_part_type($part);
1.53      albertel 1660: 	    $result.='<td><b>Score Part '.$partid.'<br />(weight = '.
1.42      ng       1661: 		$weight{$partid}.')</b></td>'."\n";
1.41      ng       1662: 	    next;
                   1663: 	}
1.53      albertel 1664: 	$display =~ s|Problem Status|Grade Status<br />|;
1.41      ng       1665: 	$result.='<td><b>'.$display.'</b></td>'."\n";
                   1666:     }
                   1667:     $result.='</tr>';
1.44      ng       1668: 
1.41      ng       1669:     #get info for each student
1.44      ng       1670:     #list all the students - with points and grade status
1.56      matthew  1671:     my (undef,undef,$fullname) = &getclasslist($ENV{'form.section'},'0');
1.41      ng       1672:     my $ctr = 0;
1.53      albertel 1673:     foreach (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
1.44      ng       1674: 	my ($uname,$udom) = split(/:/);
                   1675: 	$result.='<input type="hidden" name="ctr'.$ctr.'" value="'.$uname.'" />'."\n";
1.41      ng       1676: 	$result.=&viewstudentgrade($url,$symb,$ENV{'request.course.id'},
                   1677: 				   $_,$$fullname{$_},\@parts,\%weight);
                   1678: 	$ctr++;
                   1679:     }
                   1680:     $result.='</table></td></tr></table>';
                   1681:     $result.='<input type="hidden" name="total" value="'.$ctr.'" />'."\n";
1.45      ng       1682:     $result.='<input type="button" value="Submit Changes" '.
                   1683: 	'onClick="javascript:submit();" TARGET=_self /></form>'."\n";
1.41      ng       1684:     $result.=&show_grading_menu_form($symb,$url);
                   1685:     return $result;
                   1686: }
                   1687: 
1.44      ng       1688: #--- call by previous routine to display each student
1.41      ng       1689: sub viewstudentgrade {
                   1690:     my ($url,$symb,$courseid,$student,$fullname,$parts,$weight) = @_;
1.44      ng       1691:     my ($uname,$udom) = split(/:/,$student);
                   1692:     my %record=&Apache::lonnet::restore($symb,$courseid,$udom,$uname);
1.41      ng       1693:     my $result='<tr bgcolor="#ffffdd"><td>'.
1.44      ng       1694: 	'<a href="javascript:viewOneStudent(\''.$uname.'\',\''.$udom.
                   1695: 	'\')"; TARGET=_self>'.$fullname.'</a>'.
                   1696: 	'</td><td>'.$uname.'</td><td align="middle">'.$udom.'</td>'."\n";
1.41      ng       1697:     foreach my $part (@$parts) {
1.54      albertel 1698: 	my ($part,$type) = &split_part_type($part);
1.41      ng       1699: 	my $score=$record{"resource.$part.$type"};
                   1700: 	if ($type eq 'awarded') {
1.42      ng       1701: 	    my $pts = $score eq '' ? '' : $score*$$weight{$part};
                   1702: 	    $result.='<input type="hidden" name="'.
1.54      albertel 1703: 		'GD_'.$uname.'_'.$part.'_awarded_s" value="'.$pts.'" />'."\n";
1.42      ng       1704: 	    $result.='<td align="middle"><input type="text" name="'.
1.54      albertel 1705: 		'GD_'.$uname.'_'.$part.'_awarded" '.
                   1706: 		'onChange="javascript:changeSelect(\''.$part.'\',\''.$uname.
1.44      ng       1707: 		'\')" value="'.$pts.'" size="4" /></td>'."\n";
1.41      ng       1708: 	} elsif ($type eq 'solved') {
                   1709: 	    my ($status,$foo)=split(/_/,$score,2);
                   1710: 	    $status = 'nothing' if ($status eq '');
1.54      albertel 1711: 	    $result.='<input type="hidden" name="'.'GD_'.$uname.'_'.
                   1712: 		$part.'_solved_s" value="'.$status.'" />'."\n";
1.42      ng       1713: 	    $result.='<td align="middle"><select name="'.
1.54      albertel 1714: 		'GD_'.$uname.'_'.$part.'_solved" '.
                   1715: 		'onChange="javascript:changeOneScore(\''.$part.'\',\''.$uname.'\')" >'."\n";
1.42      ng       1716: 	    my $optsel = '<option selected="on"> </option><option>excused</option>'."\n";
                   1717: 	    $optsel = '<option> </option><option selected="on">excused</option>'."\n"
                   1718: 		if ($status eq 'excused');
1.41      ng       1719: 	    $result.=$optsel;
                   1720: 	    $result.="</select></td>\n";
1.54      albertel 1721: 	} else {
                   1722: 	    $result.='<input type="hidden" name="'.
                   1723: 		'GD_'.$uname.'_'.$part.'_'.$type.'_s" value="'.$score.'" />'.
                   1724: 		    "\n";
                   1725: 	    $result.='<td align="middle"><input type="text" name="'.
                   1726: 		'GD_'.$uname.'_'.$part.'_'.$type.'" '.
                   1727: 		'value="'.$score.'" size="4" /></td>'."\n";
1.41      ng       1728: 	}
                   1729:     }
                   1730:     $result.='</tr>';
                   1731:     return $result;
1.38      ng       1732: }
                   1733: 
1.44      ng       1734: #--- change scores for all the students in a section/class
                   1735: #    record does not get update if unchanged
1.38      ng       1736: sub editgrades {
1.41      ng       1737:     my ($request) = @_;
                   1738: 
                   1739:     my $symb=$ENV{'form.symb'};
1.43      ng       1740:     my $url =$ENV{'form.url'};
1.45      ng       1741:     my $title='<h3><font color="#339933">Current Grade Status</font></h3>';
1.44      ng       1742:     $title.='<font size=+1><b>Resource: </b>'.$ENV{'form.url'}.'</font><br />'."\n";
                   1743:     $title.='<font size=+1><b>Section: </b>'.$ENV{'form.section'}.'</font>'."\n";
                   1744:     $title.= &show_grading_menu_form ($symb,$url);
                   1745:     my $result= '<table border="0"><tr><td bgcolor="#777777">'."\n";
1.43      ng       1746:     $result.= '<table border="0"><tr bgcolor="#deffff">'.
                   1747: 	'<td rowspan=2><b>Username</b></td><td rowspan=2><b>Fullname</b></td>'."\n";
                   1748: 
                   1749:     my %scoreptr = (
                   1750: 		    'correct'  =>'correct_by_override',
                   1751: 		    'incorrect'=>'incorrect_by_override',
                   1752: 		    'excused'  =>'excused',
                   1753: 		    'ungraded' =>'ungraded_attempted',
                   1754: 		    'nothing'  => '',
                   1755: 		    );
1.56      matthew  1756:     my ($classlist,undef,$fullname) = &getclasslist($ENV{'form.section'},'0');
1.34      ng       1757: 
1.44      ng       1758:     my (@partid);
                   1759:     my %weight = ();
1.54      albertel 1760:     my %columns = ();
1.44      ng       1761:     my ($i,$ctr,$count,$rec_update) = (0,0,0,0);
1.54      albertel 1762: 
                   1763:     my (@parts) = sort(&getpartlist($url));
                   1764:     my $header;
1.44      ng       1765:     while ($ctr < $ENV{'form.totalparts'}) {
                   1766: 	my $partid = $ENV{'form.partid_'.$ctr};
                   1767: 	push @partid,$partid;
                   1768: 	$weight{$partid} = $ENV{'form.weight_'.$partid};
                   1769: 	$ctr++;
1.54      albertel 1770:     }
                   1771:     foreach my $partid (@partid) {
                   1772: 	$header .= '<td align="center">&nbsp;<b>Old Score</b>&nbsp;</td>'.
                   1773: 	    '<td align="center">&nbsp;<b>New Score</b>&nbsp;</td>';
                   1774: 	$columns{$partid}=2;
                   1775: 	foreach my $stores (@parts) {
                   1776: 	    my ($part,$type) = &split_part_type($stores);
                   1777: 	    if ($part !~ m/^\Q$partid\E/) { next;}
                   1778: 	    if ($type eq 'awarded' || $type eq 'solved') { next; }
                   1779: 	    my $display=&Apache::lonnet::metadata($url,$stores.'.display');
                   1780: 	    $display =~ s/\[Part: (\w)+\]//;
                   1781: 	    $header .= '<td align="center">&nbsp;<b>Old</b> '.$display.'&nbsp;</td>'.
                   1782: 		'<td align="center">&nbsp;<b>New</b> '.$display.'&nbsp;</td>';
                   1783: 	    $columns{$partid}+=2;
                   1784: 	}
                   1785:     }
                   1786:     foreach my $partid (@partid) {
                   1787: 	$result .= '<td colspan="'.$columns{$partid}.
                   1788: 	    '" align="center"><b>Part '.$partid.
1.44      ng       1789: 	    '</b> (Weight = '.$weight{$partid}.')</td>';
1.54      albertel 1790: 
1.44      ng       1791:     }
                   1792:     $result .= '</tr><tr bgcolor="#deffff">';
1.54      albertel 1793:     $result .= $header;
1.44      ng       1794:     $result .= '</tr>'."\n";
1.13      albertel 1795: 
1.44      ng       1796:     for ($i=0; $i<$ENV{'form.total'}; $i++) {
                   1797: 	my $user = $ENV{'form.ctr'.$i};
                   1798: 	my %newrecord;
                   1799: 	my $updateflag = 0;
                   1800: 	my @userdom = grep /^$user:/,keys %$classlist;
1.54      albertel 1801: 	my (undef,$udom) = split(/:/,$userdom[0]);
1.13      albertel 1802: 
1.44      ng       1803: 	$result .= '<tr bgcolor="#ffffde"><td>'.$user.'&nbsp;</td><td>'.
                   1804: 	    $$fullname{$userdom[0]}.'&nbsp;</td>';
                   1805: 	foreach (@partid) {
1.54      albertel 1806: 	    my $old_aw    = $ENV{'form.GD_'.$user.'_'.$_.'_awarded_s'};
                   1807: 	    my $old_part_pcr = $old_aw/($weight{$_} ne '0' ? $weight{$_}:1);
                   1808: 	    my $old_part  = $old_aw eq '' ? '' : $old_part_pcr;
                   1809: 	    my $old_score = $scoreptr{$ENV{'form.GD_'.$user.'_'.$_.'_solved_s'}};
                   1810: 
                   1811: 	    my $awarded   = $ENV{'form.GD_'.$user.'_'.$_.'_awarded'};
                   1812: 	    my $pcr       = $awarded/($weight{$_} ne '0' ? $weight{$_} : 1);
                   1813: 	    my $partial   = $awarded eq '' ? '' : $pcr;
1.44      ng       1814: 	    my $score;
                   1815: 	    if ($partial eq '') {
1.54      albertel 1816: 		$score = $scoreptr{$ENV{'form.GD_'.$user.'_'.$_.'_solved_s'}};
1.44      ng       1817: 	    } elsif ($partial > 0) {
                   1818: 		$score = 'correct_by_override';
                   1819: 	    } elsif ($partial == 0) {
                   1820: 		$score = 'incorrect_by_override';
                   1821: 	    }
1.54      albertel 1822: 	    $score = 'excused' if (($ENV{'form.GD_'.$user.'_'.$_.'_solved'} eq 'excused') &&
1.44      ng       1823: 				   ($score ne 'excused'));
                   1824: 	    $result .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
                   1825: 		'<td align="center">'.$awarded.
                   1826: 		($score eq 'excused' ? $score : '').'&nbsp;</td>';
1.5       albertel 1827: 
1.54      albertel 1828: 	    if (!($old_part eq $partial && $old_score eq $score)) {
                   1829: 		$updateflag = 1;
                   1830: 		$newrecord{'resource.'.$_.'.awarded'}  = $partial if $partial ne '';
                   1831: 		$newrecord{'resource.'.$_.'.solved'}   = $score;
                   1832: 		$rec_update++;
                   1833: 	    }
                   1834: 
                   1835: 	    my $partid=$_;
                   1836: 	    foreach my $stores (@parts) {
                   1837: 		my ($part,$type) = &split_part_type($stores);
                   1838: 		if ($part !~ m/^\Q$partid\E/) { next;}
                   1839: 		if ($type eq 'awarded' || $type eq 'solved') { next; }
                   1840: 		my $old_aw    = $ENV{'form.GD_'.$user.'_'.$part.'_'.$type.'_s'};
                   1841: 		my $awarded   = $ENV{'form.GD_'.$user.'_'.$part.'_'.$type};
                   1842: 		if ($awarded ne '' && $awarded ne $old_aw) {
                   1843: 		    $newrecord{'resource.'.$part.'.'.$type}= $awarded;
                   1844: 		    $updateflag=1;
                   1845: 		}
                   1846: 		$result .= '<td align="center">'.$old_aw.'&nbsp;</td>'.
                   1847: 		    '<td align="center">'.$awarded.'&nbsp;</td>';
                   1848: 	    }
1.44      ng       1849: 	}
                   1850: 	$result .= '</tr>'."\n";
                   1851: 	if ($updateflag) {
                   1852: 	    $count++;
                   1853: 	    $newrecord{'resource.regrader'}="$ENV{'user.name'}:$ENV{'user.domain'}";
                   1854: 	    &Apache::lonnet::cstore(\%newrecord,$symb,$ENV{'request.course.id'},
                   1855: 				    $udom,$user);
                   1856: 	}
                   1857:     }
                   1858:     $result .= '</table></td></tr></table>'."\n";
                   1859:     my $msg = '<b>Number of records updated = '.$rec_update.
                   1860: 	' for '.$count.' student'.($count <= 1 ? '' : 's').'.</b><br />'.
                   1861: 	'<b>Total number of students = '.$ENV{'form.total'}.'</b><br />';
                   1862:     return $title.$msg.$result;
1.5       albertel 1863: }
1.54      albertel 1864: 
                   1865: sub split_part_type {
                   1866:     my ($partstr) = @_;
                   1867:     my ($temp,@allparts)=split(/_/,$partstr);
                   1868:     my $type=pop(@allparts);
                   1869:     my $part=join('.',@allparts);
                   1870:     return ($part,$type);
                   1871: }
                   1872: 
1.44      ng       1873: #------------- end of section for handling grading by section/class ---------
                   1874: #
                   1875: #----------------------------------------------------------------------------
                   1876: 
1.5       albertel 1877: 
1.44      ng       1878: #----------------------------------------------------------------------------
                   1879: #
                   1880: #-------------------------- Next few routines handles grading by csv upload
                   1881: #
                   1882: #--- Javascript to handle csv upload
1.27      albertel 1883: sub csvupload_javascript_reverse_associate {
                   1884:   return(<<ENDPICK);
                   1885:   function verify(vf) {
                   1886:     var foundsomething=0;
                   1887:     var founduname=0;
                   1888:     var founddomain=0;
                   1889:     for (i=0;i<=vf.nfields.value;i++) {
                   1890:       tw=eval('vf.f'+i+'.selectedIndex');
                   1891:       if (i==0 && tw!=0) { founduname=1; }
                   1892:       if (i==1 && tw!=0) { founddomain=1; }
                   1893:       if (i!=0 && i!=1 && tw!=0) { foundsomething=1; }
                   1894:     }
                   1895:     if (founduname==0 || founddomain==0) {
                   1896:       alert('You need to specify at both the username and domain');
                   1897:       return;
                   1898:     }
                   1899:     if (foundsomething==0) {
                   1900:       alert('You need to specify at least one grading field');
                   1901:       return;
                   1902:     }
                   1903:     vf.submit();
                   1904:   }
                   1905:   function flip(vf,tf) {
                   1906:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   1907:     var i;
                   1908:     for (i=0;i<=vf.nfields.value;i++) {
                   1909:       //can not pick the same destination field for both name and domain
                   1910:       if (((i ==0)||(i ==1)) && 
                   1911:           ((tf==0)||(tf==1)) && 
                   1912:           (i!=tf) &&
                   1913:           (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   1914:         eval('vf.f'+i+'.selectedIndex=0;')
                   1915:       }
                   1916:     }
                   1917:   }
                   1918: ENDPICK
                   1919: }
                   1920: 
                   1921: sub csvupload_javascript_forward_associate {
                   1922:   return(<<ENDPICK);
                   1923:   function verify(vf) {
                   1924:     var foundsomething=0;
                   1925:     var founduname=0;
                   1926:     var founddomain=0;
                   1927:     for (i=0;i<=vf.nfields.value;i++) {
                   1928:       tw=eval('vf.f'+i+'.selectedIndex');
                   1929:       if (tw==1) { founduname=1; }
                   1930:       if (tw==2) { founddomain=1; }
                   1931:       if (tw>2) { foundsomething=1; }
                   1932:     }
                   1933:     if (founduname==0 || founddomain==0) {
                   1934:       alert('You need to specify at both the username and domain');
                   1935:       return;
                   1936:     }
                   1937:     if (foundsomething==0) {
                   1938:       alert('You need to specify at least one grading field');
                   1939:       return;
                   1940:     }
                   1941:     vf.submit();
                   1942:   }
                   1943:   function flip(vf,tf) {
                   1944:     var nw=eval('vf.f'+tf+'.selectedIndex');
                   1945:     var i;
                   1946:     //can not pick the same destination field twice
                   1947:     for (i=0;i<=vf.nfields.value;i++) {
                   1948:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                   1949:         eval('vf.f'+i+'.selectedIndex=0;')
                   1950:       }
                   1951:     }
                   1952:   }
                   1953: ENDPICK
                   1954: }
                   1955: 
1.26      albertel 1956: sub csvuploadmap_header {
1.41      ng       1957:     my ($request,$symb,$url,$datatoken,$distotal)= @_;
                   1958:     my $javascript;
                   1959:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   1960: 	$javascript=&csvupload_javascript_reverse_associate();
                   1961:     } else {
                   1962: 	$javascript=&csvupload_javascript_forward_associate();
                   1963:     }
1.45      ng       1964: 
                   1965:     my $result='<table border="0">';
                   1966:     $result.='<tr><td colspan=3><font size=+1><b>Resource: </b>'.$url.'</font></td></tr>';
                   1967:     my ($partlist,$handgrade) = &response_type($url);
                   1968:     my ($resptype,$hdgrade)=('','no');
                   1969:     for (sort keys(%$handgrade)) {
                   1970: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
                   1971: 	$resptype = $responsetype;
                   1972: 	$hdgrade = $handgrade if ($handgrade eq 'yes');
                   1973: 	$result.='<tr><td><b>Part </b>'.(split(/_/))[0].'</td>'.
                   1974: 	    '<td><b>Type: </b>'.$responsetype.'</td>'.
                   1975: 	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
                   1976:     }
                   1977:     $result.='</table>';
1.41      ng       1978:     $request->print(<<ENDPICK);
1.26      albertel 1979: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
1.45      ng       1980: <h3><font color="#339933">Uploading Class Grades</font></h3>
                   1981: $result
1.26      albertel 1982: <hr>
                   1983: <h3>Identify fields</h3>
                   1984: Total number of records found in file: $distotal <hr />
                   1985: Enter as many fields as you can. The system will inform you and bring you back
                   1986: to this page if the data selected is insufficient to run your class.<hr />
                   1987: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
                   1988: <input type="hidden" name="associate"  value="" />
                   1989: <input type="hidden" name="phase"      value="three" />
                   1990: <input type="hidden" name="datatoken"  value="$datatoken" />
                   1991: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
                   1992: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
                   1993: <input type="hidden" name="upfile_associate" 
                   1994:                                        value="$ENV{'form.upfile_associate'}" />
                   1995: <input type="hidden" name="symb"       value="$symb" />
                   1996: <input type="hidden" name="url"        value="$url" />
                   1997: <input type="hidden" name="command"    value="csvuploadassign" />
                   1998: <hr />
                   1999: <script type="text/javascript" language="Javascript">
                   2000: $javascript
                   2001: </script>
                   2002: ENDPICK
1.41      ng       2003: return '';
1.26      albertel 2004: 
                   2005: }
                   2006: 
                   2007: sub csvupload_fields {
1.41      ng       2008:     my ($url) = @_;
                   2009:     my (@parts) = &getpartlist($url);
                   2010:     my @fields=(['username','Student Username'],['domain','Student Domain']);
                   2011:     foreach my $part (sort(@parts)) {
                   2012: 	my @datum;
                   2013: 	my $display=&Apache::lonnet::metadata($url,$part.'.display');
                   2014: 	my $name=$part;
                   2015: 	if  (!$display) { $display = $name; }
                   2016: 	@datum=($name,$display);
                   2017: 	push(@fields,\@datum);
                   2018:     }
                   2019:     return (@fields);
1.26      albertel 2020: }
                   2021: 
                   2022: sub csvuploadmap_footer {
1.41      ng       2023:     my ($request,$i,$keyfields) =@_;
                   2024:     $request->print(<<ENDPICK);
1.26      albertel 2025: </table>
                   2026: <input type="hidden" name="nfields" value="$i" />
                   2027: <input type="hidden" name="keyfields" value="$keyfields" />
                   2028: <input type="button" onClick="javascript:verify(this.form)" value="Assign Grades" /><br />
                   2029: </form>
                   2030: ENDPICK
                   2031: }
                   2032: 
                   2033: sub csvuploadmap {
1.41      ng       2034:     my ($request)= @_;
                   2035:     my ($symb,$url)=&get_symb_and_url($request);
                   2036:     if (!$symb) {return '';}
                   2037:     my $datatoken;
                   2038:     if (!$ENV{'form.datatoken'}) {
                   2039: 	$datatoken=&Apache::loncommon::upfile_store($request);
1.26      albertel 2040:     } else {
1.41      ng       2041: 	$datatoken=$ENV{'form.datatoken'};
                   2042: 	&Apache::loncommon::load_tmp_file($request);
1.26      albertel 2043:     }
1.41      ng       2044:     my @records=&Apache::loncommon::upfile_record_sep();
                   2045:     &csvuploadmap_header($request,$symb,$url,$datatoken,$#records+1);
                   2046:     my ($i,$keyfields);
                   2047:     if (@records) {
                   2048: 	my @fields=&csvupload_fields($url);
1.45      ng       2049: 
1.41      ng       2050: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
                   2051: 	    &Apache::loncommon::csv_print_samples($request,\@records);
                   2052: 	    $i=&Apache::loncommon::csv_print_select_table($request,\@records,
                   2053: 							  \@fields);
                   2054: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
                   2055: 	    chop($keyfields);
                   2056: 	} else {
                   2057: 	    unshift(@fields,['none','']);
                   2058: 	    $i=&Apache::loncommon::csv_samples_select_table($request,\@records,
                   2059: 							    \@fields);
                   2060: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
                   2061: 	    $keyfields=join(',',sort(keys(%sone)));
                   2062: 	}
                   2063:     }
                   2064:     &csvuploadmap_footer($request,$i,$keyfields);
                   2065:     return '';
1.27      albertel 2066: }
                   2067: 
                   2068: sub csvuploadassign {
1.41      ng       2069:     my ($request)= @_;
                   2070:     my ($symb,$url)=&get_symb_and_url($request);
                   2071:     if (!$symb) {return '';}
                   2072:     &Apache::loncommon::load_tmp_file($request);
1.44      ng       2073:     my @gradedata = &Apache::loncommon::upfile_record_sep();
1.41      ng       2074:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
                   2075:     my %fields=();
                   2076:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
                   2077: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   2078: 	    if ($ENV{'form.f'.$i} ne 'none') {
                   2079: 		$fields{$keyfields[$i]}=$ENV{'form.f'.$i};
                   2080: 	    }
                   2081: 	} else {
                   2082: 	    if ($ENV{'form.f'.$i} ne 'none') {
                   2083: 		$fields{$ENV{'form.f'.$i}}=$keyfields[$i];
                   2084: 	    }
                   2085: 	}
1.27      albertel 2086:     }
1.41      ng       2087:     $request->print('<h3>Assigning Grades</h3>');
                   2088:     my $courseid=$ENV{'request.course.id'};
                   2089:     my ($classlist) = &getclasslist('all','1');
                   2090:     my @skipped;
                   2091:     my $countdone=0;
                   2092:     foreach my $grade (@gradedata) {
                   2093: 	my %entries=&Apache::loncommon::record_sep($grade);
                   2094: 	my $username=$entries{$fields{'username'}};
                   2095: 	my $domain=$entries{$fields{'domain'}};
                   2096: 	if (!exists($$classlist{"$username:$domain"})) {
                   2097: 	    push(@skipped,"$username:$domain");
                   2098: 	    next;
                   2099: 	}
                   2100: 	my %grades;
                   2101: 	foreach my $dest (keys(%fields)) {
                   2102: 	    if ($dest eq 'username' || $dest eq 'domain') { next; }
                   2103: 	    if ($entries{$fields{$dest}} eq '') { next; }
                   2104: 	    my $store_key=$dest;
                   2105: 	    $store_key=~s/^stores/resource/;
                   2106: 	    $store_key=~s/_/\./g;
                   2107: 	    $grades{$store_key}=$entries{$fields{$dest}};
                   2108: 	}
                   2109: 	$grades{"resource.regrader"}="$ENV{'user.name'}:$ENV{'user.domain'}";
                   2110: 	&Apache::lonnet::cstore(\%grades,$symb,$ENV{'request.course.id'},
                   2111: 				$domain,$username);
                   2112: 	$request->print('.');
                   2113: 	$request->rflush();
                   2114: 	$countdone++;
                   2115:     }
                   2116:     $request->print("<br />Stored $countdone students\n");
                   2117:     if (@skipped) {
                   2118: 	$request->print('<br /><font size="+1"><b>Skipped Students</b></font><br />');
                   2119: 	foreach my $student (@skipped) { $request->print("<br />$student"); }
                   2120:     }
                   2121:     $request->print(&view_edit_entire_class_form($symb,$url));
                   2122:     $request->print(&show_grading_menu_form($symb,$url));
                   2123:     return '';
1.26      albertel 2124: }
1.44      ng       2125: #------------- end of section for handling csv file upload ---------
                   2126: #
                   2127: #-------------------------------------------------------------------
                   2128: 
                   2129: #-------------------------- Menu interface -------------------------
                   2130: #
                   2131: #--- Show a Grading Menu button - Calls the next routine ---
                   2132: sub show_grading_menu_form {
                   2133:     my ($symb,$url)=@_;
                   2134:     my $result.='<form action="/adm/grades" method="post">'."\n".
                   2135: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   2136: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
                   2137: 	'<input type="hidden" name="command" value="gradingmenu" />'."\n".
                   2138: 	'<input type="submit" name="submit" value="Grading Menu" />'."\n".
                   2139: 	'</form>'."\n";
                   2140:     return $result;
                   2141: }
                   2142: 
                   2143: #--- Displays the main menu page -------
                   2144: sub gradingmenu {
                   2145:     my ($request) = @_;
                   2146:     my ($symb,$url)=&get_symb_and_url($request);
                   2147:     if (!$symb) {return '';}
1.45      ng       2148:     my $result='<h3>&nbsp;<font color="#339933">Select a Grading Method</font></h3>';
1.44      ng       2149:     $result.='<table border="0">';
                   2150:     $result.='<tr><td colspan=3><font size=+1><b>Resource: </b>'.$url.'</font></td></tr>';
                   2151:     my ($partlist,$handgrade) = &response_type($url);
                   2152:     my ($resptype,$hdgrade)=('','no');
                   2153:     for (sort keys(%$handgrade)) {
                   2154: 	my ($responsetype,$handgrade)=split(/:/,$$handgrade{$_});
                   2155: 	$resptype = $responsetype;
                   2156: 	$hdgrade = $handgrade if ($handgrade eq 'yes');
                   2157: 	$result.='<tr><td><b>Part </b>'.(split(/_/))[0].'</td>'.
                   2158: 	    '<td><b>Type: </b>'.$responsetype.'</td>'.
                   2159: 	    '<td><b>Handgrade: </b>'.$handgrade.'</font></td></tr>';
                   2160:     }
                   2161:     $result.='</table>';
                   2162:     $result.=&view_edit_entire_class_form($symb,$url).'<br />';
                   2163:     $result.=&upcsvScores_form($symb,$url).'<br />';
                   2164:     $result.=&viewGradeaStu_form($symb,$url,$resptype,$hdgrade).'<br />';
                   2165:     $result.=&verifyReceipt_form($symb,$url) 
                   2166: 	if ((&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) && ($symb));
                   2167:  
                   2168:     return $result;
                   2169: }
                   2170: 
                   2171: #--- Menu for grading a section or the whole class ---
                   2172: sub view_edit_entire_class_form {
                   2173:     my ($symb,$url)=@_;
1.56      matthew  2174:     my ($classlist,$sections,undef) = &getclasslist('all','0');
1.44      ng       2175:     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
                   2176:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
1.52      albertel 2177:     $result.='&nbsp;<b>Grade Entire Section or Class</b></td></tr>'."\n";
1.44      ng       2178:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   2179:     $result.='<form action="/adm/grades" method="post">'."\n".
                   2180: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   2181: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
                   2182: 	'<input type="hidden" name="command" value="viewgrades" />'."\n";
                   2183:     $result.='&nbsp;<b>Select section:</b> <select name="section">'."\n";
1.49      albertel 2184:     if (ref($sections)) {
                   2185: 	foreach (sort (@$sections)) {
                   2186: 	    $result.= '<option>'.$_.'</option>'."\n";
                   2187: 	}
1.44      ng       2188:     }
                   2189:     $result.='<option selected="on">all</select>'."<br />\n";
1.51      albertel 2190:     $result.='&nbsp;<input type="button" onClick="submit();" value="Grade" /></form>'."\n";
1.44      ng       2191:     $result.='</td></tr></table>'."\n";
                   2192:     $result.='</td></tr></table>'."\n";
                   2193:     return $result;
                   2194: }
                   2195: 
                   2196: #--- Menu to upload a csv scores ---
                   2197: sub upcsvScores_form {
                   2198:     my ($symb,$url) = @_;
                   2199:     if (!$symb) {return '';}
1.46      ng       2200:     my $result = '<script type="text/javascript" language="javascript">'."\n".
                   2201: 	'  function checkUpload(formname) {'."\n".
                   2202: 	'    if (formname.upfile.value == "") {'."\n".
                   2203: 	'       alert("Please use the browse button to select a file from your local directory.");'."\n".
                   2204: 	'       return false;'."\n".
                   2205: 	'    }'."\n".
                   2206: 	'    formname.submit();'."\n".
                   2207: 	'  }'."\n".
                   2208: 	'</script>'."\n";
                   2209: 
                   2210:     $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
1.44      ng       2211:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
                   2212:     $result.='&nbsp;<b>Specify a file containing the class scores for above resource</b></td></tr>'."\n";
                   2213:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   2214:     my $upfile_select=&Apache::loncommon::upfile_select_html();
                   2215:   $result.=<<ENDUPFORM;
                   2216: <form method="post" enctype="multipart/form-data" action="/adm/grades" name="gradesupload">
                   2217: <input type="hidden" name="symb" value="$symb" />
                   2218: <input type="hidden" name="url" value="$url" />
                   2219: <input type="hidden" name="command" value="csvuploadmap" />
                   2220: $upfile_select
1.46      ng       2221: <br />&nbsp;<input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Grades" />
1.44      ng       2222: </form>
                   2223: ENDUPFORM
                   2224:     $result.='</td></tr></table>'."\n";
                   2225:     $result.='</td></tr></table>'."\n";
                   2226:     return $result;
                   2227: }
                   2228: 
                   2229: #--- Handgrading problems ---
                   2230: sub viewGradeaStu_form {
                   2231:     my ($symb,$url,$response,$handgrade) = @_;
                   2232:     my ($classlist,$sections) = &getclasslist('all','0');
                   2233:     my $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
                   2234:     $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
1.49      albertel 2235:     $result.='&nbsp;<b>';
                   2236:     if ($handgrade eq 'yes') {
                   2237: 	$result.="View/Grade ";
                   2238:     } else {
                   2239: 	$result.="View ";
                   2240:     }
                   2241:     $result.='an Individual Student\'s Submission</b></td></tr>'."\n";
1.44      ng       2242:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   2243:     $result.='<form action="/adm/grades" method="post">'."\n".
                   2244: 	'<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
                   2245: 	'<input type="hidden" name="url" value="'.$url.'" />'."\n".
                   2246: 	'<input type="hidden" name="response" value="'.$response.'" />'."\n".
                   2247: 	'<input type="hidden" name="handgrade" value="'.$handgrade.'" />'."\n".
                   2248: 	'<input type="hidden" name="showgrading" value="yes" />'."\n".
                   2249: 	'<input type="hidden" name="command" value="submission" />'."\n";
1.26      albertel 2250: 
1.44      ng       2251:     $result.='&nbsp;<b>Select section:</b> <select name="section">'."\n";
1.49      albertel 2252:     if (ref($sections)) {
                   2253: 	foreach (sort (@$sections)) {$result.='<option>'.$_.'</option>'."\n";}
1.44      ng       2254:     }
                   2255:     $result.= '<option selected="on">all</select>'."\n";
                   2256:     $result.='&nbsp;&nbsp;<b>Display students who has: </b>'.
                   2257: 	'<input type="radio" name="submitonly" value="yes" checked> submitted'.
                   2258: 	'<input type="radio" name="submitonly" value="all"> everybody <br />';
1.49      albertel 2259:     if (ref($sections)) {
                   2260: 	$result.='&nbsp;(Section "no" implies the students were not assigned a section.)<br />' 
                   2261: 	    if (grep /no/,@$sections);
                   2262:     }
                   2263: 
                   2264: 
                   2265:     $result.='<br />&nbsp;<input type="button" onClick="submit();" value="';
                   2266:     if ($handgrade eq 'yes') {
                   2267: 	$result.="View/Grade";
                   2268:     } else {
                   2269: 	$result.="View";
                   2270:     }
                   2271:     $result.='" />'."\n".'</form>'."\n";
1.44      ng       2272:     $result.='</td></tr></table>'."\n";
                   2273:     $result.='</td></tr></table>'."\n";
                   2274:     return $result;
1.2       albertel 2275: }
                   2276: 
1.44      ng       2277: #--- Form to input a receipt number ---
                   2278: sub verifyReceipt_form {
                   2279:     my ($symb,$url) = @_;
1.46      ng       2280:     my $result = '<script type="text/javascript" language="javascript">'."\n".
                   2281: 	'  function checkEntry(formname) {'."\n".
                   2282: 	'    var receipt = formname.receipt.value;'."\n".
                   2283: 	'    if (isNaN(receipt) || receipt == "") {'."\n".
                   2284: 	'       alert("Please enter a receipt number given by a student in the box.");'."\n".
                   2285: 	'       return false;'."\n".
                   2286: 	'    }'."\n".
                   2287: 	'    formname.submit();'."\n".
                   2288: 	'  }'."\n".
                   2289: 	'</script>'."\n";
                   2290: 
1.44      ng       2291:     my $hostver=unpack("%32C*",$Apache::lonnet::perlvar{'lonHostID'});
                   2292: 
1.46      ng       2293:     $result.='<table width=100% border=0><tr><td bgcolor=#777777>'."\n";
1.44      ng       2294:     $result.='<table width=100% border=0><tr><td bgcolor=#e6ffff>'."\n";
                   2295:     $result.='&nbsp;<b>Verify a Submission Receipt Issued by this Server</td></tr>'."\n";
                   2296:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
1.46      ng       2297:     $result.='<form action="/adm/grades" method="post" name="verifyform">'."\n";
1.44      ng       2298:     $result.='&nbsp;<tt>'.$hostver.'-<input type="text" name="receipt" size="4"></tt><br />'."\n";
1.46      ng       2299:     $result.='&nbsp;<input type="button" onClick="javascript:checkEntry(this.form);"'.
                   2300: 	' value="Verify Receipt">'."\n";
1.44      ng       2301:     $result.='<input type="hidden" name="command" value="verify">'."\n";
                   2302:     if ($ENV{'form.url'}) {
                   2303: 	$result.='<input type="hidden" name="url" value="'.$ENV{'form.url'}.'" />';
                   2304:     }
                   2305:     if ($ENV{'form.symb'}) {
                   2306: 	$result.='<input type="hidden" name="symb" value="'.$ENV{'form.symb'}.'" />';
                   2307:     }
                   2308:     $result.='</form>';
                   2309:     $result.='</td></tr></table>'."\n";
                   2310:     $result.='</td></tr></table>'."\n";
                   2311:     return $result;
1.2       albertel 2312: }
                   2313: 
1.1       albertel 2314: sub handler {
1.41      ng       2315:     my $request=$_[0];
                   2316:     
                   2317:     if ($ENV{'browser.mathml'}) {
                   2318: 	$request->content_type('text/xml');
                   2319:     } else {
                   2320: 	$request->content_type('text/html');
                   2321:     }
                   2322:     $request->send_http_header;
1.44      ng       2323:     return '' if $request->header_only;
1.41      ng       2324:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                   2325:     my $url=$ENV{'form.url'};
                   2326:     my $symb=$ENV{'form.symb'};
                   2327:     my $command=$ENV{'form.command'};
                   2328:     if (!$url) {
                   2329: 	my ($temp1,$temp2);
                   2330: 	($temp1,$temp2,$ENV{'form.url'})=split(/___/,$symb);
                   2331: 	$url = $ENV{'form.url'};
                   2332:     }
                   2333:     &send_header($request);
                   2334:     if ($url eq '' && $symb eq '') {
                   2335: 	if ($ENV{'user.adv'}) {
                   2336: 	    if (($ENV{'form.codeone'}) && ($ENV{'form.codetwo'}) &&
                   2337: 		($ENV{'form.codethree'})) {
                   2338: 		my $token=$ENV{'form.codeone'}.'*'.$ENV{'form.codetwo'}.'*'.
                   2339: 		    $ENV{'form.codethree'};
                   2340: 		my ($tsymb,$tuname,$tudom,$tcrsid)=
                   2341: 		    &Apache::lonnet::checkin($token);
                   2342: 		if ($tsymb) {
                   2343: 		    my ($map,$id,$url)=split(/\_\_\_/,$tsymb);
                   2344: 		    if (&Apache::lonnet::allowed('mgr',$tcrsid)) {
                   2345: 			$request->print(
                   2346: 					&Apache::lonnet::ssi('/res/'.$url,
                   2347: 							     ('grade_username' => $tuname,
                   2348: 							      'grade_domain' => $tudom,
                   2349: 							      'grade_courseid' => $tcrsid,
                   2350: 							      'grade_symb' => $tsymb)));
                   2351: 		    } else {
1.45      ng       2352: 			$request->print('<h3>Not authorized: '.$token.'</h3>');
1.41      ng       2353: 		    }           
                   2354: 		} else {
1.45      ng       2355: 		    $request->print('<h3>Not a valid DocID: '.$token.'</h3>');
1.41      ng       2356: 		}
1.14      www      2357: 	    } else {
1.41      ng       2358: 		$request->print(&Apache::lonxml::tokeninputfield());
                   2359: 	    }
                   2360: 	}
                   2361:     } else {
                   2362: 	$Apache::grades::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                   2363: 	if ($command eq 'submission') {
                   2364: 	    &listStudents($request) if ($ENV{'form.student'} eq '');
                   2365: 	    &submission($request,0,0) if ($ENV{'form.student'} ne '');
                   2366: 	} elsif ($command eq 'processGroup') {
                   2367: 	    &processGroup($request);
                   2368: 	} elsif ($command eq 'gradingmenu') {
                   2369: 	    $request->print(&gradingmenu($request));
                   2370: 	} elsif ($command eq 'viewgrades') {
                   2371: 	    $request->print(&viewgrades($request));
                   2372: 	} elsif ($command eq 'handgrade') {
                   2373: 	    $request->print(&processHandGrade($request));
                   2374: 	} elsif ($command eq 'editgrades') {
                   2375: 	    $request->print(&editgrades($request));
                   2376: 	} elsif ($command eq 'verify') {
                   2377: 	    $request->print(&verifyreceipt($request));
                   2378: 	} elsif ($command eq 'csvupload') {
                   2379: 	    $request->print(&csvupload($request));
                   2380: 	} elsif ($command eq 'viewclasslist') {
                   2381: 	    $request->print(&viewclasslist($request));
                   2382: 	} elsif ($command eq 'csvuploadmap') {
                   2383: 	    $request->print(&csvuploadmap($request));
                   2384: 	} elsif ($command eq 'csvuploadassign') {
                   2385: 	    if ($ENV{'form.associate'} ne 'Reverse Association') {
                   2386: 		$request->print(&csvuploadassign($request));
                   2387: 	    } else {
                   2388: 		if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
                   2389: 		    $ENV{'form.upfile_associate'} = 'reverse';
                   2390: 		} else {
                   2391: 		    $ENV{'form.upfile_associate'} = 'forward';
                   2392: 		}
                   2393: 		$request->print(&csvuploadmap($request));
                   2394: 	    }
1.26      albertel 2395: 	} else {
1.41      ng       2396: 	    $request->print("Unknown action: $command:");
1.26      albertel 2397: 	}
1.2       albertel 2398:     }
1.41      ng       2399:     &send_footer($request);
1.44      ng       2400:     return '';
                   2401: }
                   2402: 
                   2403: sub send_header {
                   2404:     my ($request)= @_;
                   2405:     $request->print(&Apache::lontexconvert::header());
                   2406: #  $request->print("
                   2407: #<script>
                   2408: #remotewindow=open('','homeworkremote');
                   2409: #remotewindow.close();
                   2410: #</script>"); 
1.47      www      2411:     $request->print(&Apache::loncommon::bodytag('Grading'));
1.44      ng       2412: }
                   2413: 
                   2414: sub send_footer {
                   2415:     my ($request)= @_;
                   2416:     $request->print('</body>');
                   2417:     $request->print(&Apache::lontexconvert::footer());
1.1       albertel 2418: }
                   2419: 
                   2420: 1;
                   2421: 
1.13      albertel 2422: __END__;

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