Annotation of loncom/interface/statistics/longradinganalysis.pm, revision 1.14

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: #
1.14    ! raeburn     3: # $Id: longradinganalysis.pm,v 1.13 2014/02/12 16:51:16 bisitz Exp $
1.1       albertel    4: #
                      5: # Copyright Michigan State University Board of Trustees
                      6: #
                      7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      8: #
                      9: # LON-CAPA is free software; you can redistribute it and/or modify
                     10: # it under the terms of the GNU General Public License as published by
                     11: # the Free Software Foundation; either version 2 of the License, or
                     12: # (at your option) any later version.
                     13: #
                     14: # LON-CAPA is distributed in the hope that it will be useful,
                     15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17: # GNU General Public License for more details.
                     18: #
                     19: # You should have received a copy of the GNU General Public License
                     20: # along with LON-CAPA; if not, write to the Free Software
                     21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22: #
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: package Apache::longradinganalysis;
                     28: 
                     29: use strict;
                     30: use Apache::lonnet;
                     31: use Apache::loncommon();
                     32: use Apache::lonhtmlcommon();
                     33: use Apache::loncoursedata();
                     34: use Apache::lonstatistics;
                     35: use Apache::lonlocal;
                     36: use Apache::lonstathelpers();
1.3       albertel   37: use Spreadsheet::WriteExcel;
                     38: use Spreadsheet::WriteExcel::Utility();
1.1       albertel   39: use HTML::Entities();
                     40: use Time::Local();
                     41: use Data::Dumper;
                     42: 
                     43: my @submit_buttons = ({ name => 'PrevProblemAnalysis',
                     44: 			text => 'Previous Problem' },
                     45: 		      { name => 'ProblemAnalysis',
                     46: 			text => 'Analyze Problem Again' },
                     47: 		      { name => 'NextProblemAnalysis',
                     48: 			text => 'Next Problem' },
                     49: 		      { name => 'break'},
                     50: 		      { name => 'SelectAnother',
                     51: 			text => 'Choose a different Problem' });
                     52: 
                     53: sub build_grading_analysis_page {
                     54:     my ($r,$c)=@_;
                     55:     #
                     56:     my %saveable_parameters = ('Status' => 'scalar',
                     57:                                'Section' => 'array',
1.5       raeburn    58:                                'Group' => 'array',
1.1       albertel   59:                                );
                     60:     &Apache::loncommon::store_course_settings('grading_analysis',
                     61:                                               \%saveable_parameters);
                     62:     &Apache::loncommon::restore_course_settings('grading_analysis',
                     63:                                                 \%saveable_parameters);
                     64:     #
                     65:     &Apache::lonstatistics::PrepareClasslist();
                     66:     #
                     67:     $r->print(&create_interface());
                     68:     #
                     69:     my @students = @Apache::lonstatistics::Students;
                     70:     #
                     71:     if (@students < 1 && exists($env{'form.firstrun'})) {
1.13      bisitz     72:         $r->print(
                     73:             '<p class="LC_info">'
                     74:            .&mt('There are no students in the sections/groups selected.')
1.14    ! raeburn    75:            .'</p>');
1.1       albertel   76:     }
                     77:     #
                     78:     #my @cache_button_HTML = 
                     79:     #    &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
                     80:     $r->rflush();
                     81:     #
                     82:     if (exists($env{'form.problemchoice'}) && 
                     83:         ! exists($env{'form.SelectAnother'})) {
                     84:         foreach my $button (@submit_buttons) {
                     85:             if ($button->{'name'} eq 'break') {
                     86:                 $r->print("<br />\n");
                     87:             } else {
                     88:                 $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
                     89:                           'value="'.&mt($button->{'text'}).'" />');
                     90:                 $r->print('&nbsp;'x5);
                     91:             }
                     92:         }
                     93: #        foreach my $html (@cache_button_HTML) {
                     94: #            $r->print($html.('&nbsp;'x5));
                     95: #        }
                     96:         #
                     97:         #$r->print(&Apache::lonstathelpers::submission_report_form('grading_analysis'));
                     98:         #
                     99:         $r->print('<hr />');
                    100:         $r->rflush();
                    101:         #
                    102:         # Determine which problem we are to analyze
                    103:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
                    104:             ($env{'form.problemchoice'});
                    105:         #
                    106:         my ($navmap,$prev,$curr,$next) = 
                    107:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
                    108: 							undef,
                    109:                                                         'part_task',
                    110:                                                         );
                    111:         if (exists($env{'form.PrevProblemAnalysis'}) && defined($prev)) {
                    112:             $current_problem = $prev;
                    113:         } elsif (exists($env{'form.NextProblemAnalysis'}) && defined($next)) {
                    114:             $current_problem = $next;
                    115:         } else {
                    116:             $current_problem = $curr;
                    117:         }
                    118:         #
                    119:         # Store the current problem choice and send it out in the form
                    120:         $env{'form.problemchoice'} = 
                    121:             &Apache::lonstathelpers::make_target_id($current_problem);
                    122:         $r->print('<input type="hidden" name="problemchoice" value="'.
                    123:                   $env{'form.problemchoice'}.'" />');
                    124:         #
                    125: 	if (! defined($current_problem->{'resource'})) {
1.13      bisitz    126:             $r->print('<p class="LC_error">'.&mt('resource is undefined').'</p>');
1.1       albertel  127:         } else {
                    128:             my $resource = $current_problem->{'resource'};
                    129:             $r->print('<h1>'.$resource->compTitle.'</h1>');
                    130:             $r->print('<h3>'.$resource->src.'</h3>');
                    131:             $r->print('<h4>'.&Apache::lonstatistics::section_and_enrollment_description().'</h4>');
                    132:             $r->rflush();
                    133:             if ($resource->is_task()) {
                    134: 		&task_analysis($r,$resource,\@students);
                    135:             } else {
1.13      bisitz    136:                 $r->print(
                    137:                     '<p class="LC_warning">'
                    138:                    .&mt('Analysis of [_1] is not supported.',$resource->src())
                    139:                    .'</p>');
1.1       albertel  140:             }
                    141:         }
                    142:         $r->print('<hr />');
                    143:     } else {
                    144:         my $submit_button = '<input type="submit" '.
                    145:             'name="ProblemAnalysis" value="'.
                    146:             &mt('Analyze Problem').'" />';
                    147:         $r->print($submit_button);
                    148:         $r->print('&nbsp;'x5);
                    149:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
                    150:         #FIXME need a task only selector.
                    151:         $r->print(&Apache::lonstathelpers::problem_selector('.',
                    152:                                                             $submit_button));
                    153:     }
                    154: }
                    155: 
                    156: sub task_analysis {
                    157:     my ($r,$problem,$students) = @_;
1.11      www       158:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,scalar(@$students));
1.1       albertel  159:     my %graders;
                    160:     foreach my $student (@$students) {
                    161: 	my $sname = $student->{'username'};
                    162:         my $sdom = $student->{'domain'};
                    163: 	my %data =  &Apache::lonnet::restore($problem->symb(),
                    164: 					     $env{'request.course.id'},
                    165: 					     $sdom,$sname);
1.2       albertel  166: 	my $last_grader;
1.1       albertel  167: 	foreach my $ver (0..$data{'version'}) {
                    168: 	    if (exists($data{"$ver:resource.0.regrader"})
                    169: 		&& $data{"$ver:resource.0.regrader"} =~ /\S/) {
1.2       albertel  170: 		$last_grader=$data{"$ver:resource.0.regrader"};
1.1       albertel  171: 	    }
1.2       albertel  172: 	    if (exists($data{"$ver:resource.0.version"})
                    173: 		&& $last_grader =~ /\S/) {
                    174: 		$graders{$last_grader}++;
                    175: 	    }
                    176: 	}
                    177: 	if ($last_grader =~ /\S/) {
                    178: 	    $graders{$last_grader}++;
1.1       albertel  179: 	}
1.2       albertel  180: 
1.1       albertel  181: 	&Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
1.10      bisitz    182:                                                      'last student');
1.1       albertel  183: 
                    184:     }
1.3       albertel  185: 
                    186:     my @output;
                    187:     foreach my $grader (sort(keys(%graders))) {
                    188: 	my ($gname,$gdom) = split(/(?:\:|\@)/,$grader,2);
                    189: 	my $name = &Apache::loncommon::plainname($gname,$gdom);
                    190: 	push(@output,[$name,$gname."@".$gdom,$graders{$grader}]);
                    191:     }
                    192: 
1.1       albertel  193:     if ($env{'form.output'} eq 'csv') {
1.3       albertel  194: 	my ($outputfile,$filename) = &init_csv_output($r);
                    195: 	foreach my $line (@output) {
                    196: 	    print $outputfile 
                    197: 		('"'.join(q{","},
                    198: 			  map {&Apache::loncommon::csv_translate($_)} @{$line})
                    199: 		 .'"'."\n");
                    200: 	}
                    201: 	close($outputfile);
                    202: 	$r->print('<br />'.
1.8       bisitz    203: 		  '<a href="'.$filename.'">'.&mt('Your CSV file.')."</a>\n");
1.1       albertel  204:     } elsif ($env{'form.output'} eq 'excel') {
1.3       albertel  205: 	my ($excel_workbook,$excel_sheet,$filename,$format,$rows_output) = 
                    206: 	    &init_excel_output($r);
                    207: 	foreach my $line (@output) {
                    208: 	    my $cols_output = 0;
                    209: 	    foreach my $item (@{ $line }) {
                    210: 		$excel_sheet->write($rows_output,$cols_output++,$item);
                    211: 	    }
                    212: 	    $rows_output++;
                    213: 	}
                    214: 	# Write the excel file
                    215: 	$excel_workbook->close();
                    216: 
                    217: 	# Tell the user where to get their excel file
                    218: 	$r->print('<br />'.
                    219: 		  '<a href="'.$filename.'">'.
                    220: 		  &mt('Your Excel spreadsheet.').'</a>'."\n");
1.1       albertel  221:     } else {
1.6       albertel  222: 	$r->print(&Apache::loncommon::start_data_table());
                    223: 	$r->print(&Apache::loncommon::start_data_table_header_row().
1.8       bisitz    224: 		  '<th>'.&mt('Name (username)').'</th><th>'.&mt('Grades Assigned').'</th>'.
1.6       albertel  225: 		  &Apache::loncommon::end_data_table_header_row() );
1.3       albertel  226: 	foreach my $line (@output) {
1.6       albertel  227: 	    $r->print(&Apache::loncommon::start_data_table_row().
                    228: 		      sprintf("<td>%s (<tt>%s</tt>)</td><td>%s</td></tr>",
                    229: 			      @{$line}).
                    230: 		      &Apache::loncommon::end_data_table_row());
1.1       albertel  231: 	}
1.6       albertel  232: 	$r->print(&Apache::loncommon::end_data_table());
1.1       albertel  233:     }
                    234:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                    235: }
                    236: 
1.3       albertel  237: sub init_csv_output {
                    238:     my ($r) = @_;
                    239:     my ($outputfile,$filename) = 
                    240: 	&Apache::loncommon::create_text_file($r,'csv');
                    241:     my $description = $env{'course.'.$env{'request.course.id'}.'.description'};
                    242:     print $outputfile ('"'.&Apache::loncommon::csv_translate($description).
                    243: 		       '","'.&Apache::loncommon::csv_translate(scalar(localtime(time))).
                    244: 		       '"'."\n");
                    245:     print $outputfile ('"'.
                    246: 		       &Apache::loncommon::csv_translate(&Apache::lonstatistics::section_and_enrollment_description()).
                    247: 		       '"'."\n");
                    248:     print $outputfile ('"'  .&Apache::loncommon::csv_translate('Grader Name'));
                    249:     print $outputfile ('","'.&Apache::loncommon::csv_translate('Username'));
                    250:     print $outputfile ('","'.&Apache::loncommon::csv_translate('Grades Assigned').
                    251: 		       '"'."\n");
                    252:     return ($outputfile,$filename);
                    253: }
                    254: 
                    255: sub init_excel_output {
                    256:     my ($r) = @_;
                    257:     my ($excel_workbook,$filename,$format)=
                    258: 	&Apache::loncommon::create_workbook($r);
                    259:     return if (! defined($excel_workbook));
                    260:     my $rows_output = 0;
                    261:     my $cols_output = 0;
                    262:     my $header_row = $rows_output++;
                    263:     my $description_row = $rows_output++;
                    264:     $rows_output++;        # blank row
                    265:     
                    266:     my $sheetname = $env{'course.'.$env{'request.course.id'}.'.description'};
                    267:     $sheetname = &Apache::loncommon::clean_excel_name($sheetname);
                    268:     my $excel_sheet = $excel_workbook->addworksheet($sheetname);
                    269:     $excel_sheet->write($header_row,$cols_output++,
                    270: 			$env{'course.'.$env{'request.course.id'}.'.description'},
                    271: 			$format->{'h1'});
                    272:     $cols_output += 3;
                    273:     my $sectionstring = '';
1.5       raeburn   274: #    my @Sections = &Apache::lonstatistics::get_selected_sections(); #This is never used
1.3       albertel  275:     $excel_sheet->write($header_row,$cols_output++,
                    276: 			&Apache::lonstatistics::section_and_enrollment_description('plaintext'),
                    277: 			$format->{'h3'});
                    278:     
                    279:     $excel_sheet->write($header_row,$cols_output++,
1.13      bisitz    280: 			&mt('Compiled on [_1]',&Apache::lonlocal::locallocaltime(time)),
                    281:                         $format->{'h3'});
1.3       albertel  282:     $cols_output = 0;
                    283:     foreach my $field ('Grader Name','Username','Grades Assigned') {
1.13      bisitz    284: 	$excel_sheet->write($description_row,$cols_output++,&mt($field),
1.3       albertel  285: 			    $format->{'bold'});
                    286:     }
                    287:     return ($excel_workbook,$excel_sheet,$filename,$format,$rows_output);
                    288: }
                    289: 
1.1       albertel  290: #########################################################
                    291: #########################################################
                    292: ##
                    293: ##   Generic Interface Routines
                    294: ##
                    295: #########################################################
                    296: #########################################################
                    297: sub create_interface {
                    298:     ##
                    299:     ## Build the menu
                    300:     my $output_selector = $/.'<select name="output">'.$/;
                    301:     foreach ('HTML','Excel','CSV') {
                    302:         $output_selector .= '    <option value="'.lc($_).'"';
                    303:         if ($env{'form.output'} eq lc($_)) {
1.13      bisitz    304:             $output_selector .= ' selected="selected"';
1.1       albertel  305:         }
                    306:         $output_selector .='>'.&mt($_).'</option>'.$/;
                    307:     } 
                    308:     $output_selector .= '</select>'.$/;
                    309: 
                    310:     my $str = '';
1.4       albertel  311:     $str .= &Apache::lonhtmlcommon::breadcrumbs('Detailed Grading Statistics');
1.1       albertel  312:     $str .= '<table cellspacing="5">'."\n";
                    313:     $str .= '<tr>';
                    314:     $str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
1.5       raeburn   315:     $str .= '<td align="center"><b>'.&mt('Groups').'</b></td>';
1.7       raeburn   316:     $str .= '<td align="center"><b>'.&mt('Access Status').'</b></td>';
1.12      bisitz    317:     $str .= '<td>'.&mt('[_1]Output as[_2] [_3]','<b>',$output_selector,'</b>').'</td>';
1.1       albertel  318:     $str .= '</tr>'."\n";
                    319:     ##
                    320:     ## 
                    321:     $str .= '<tr><td align="center">'."\n";
                    322:     $str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
                    323:     $str .= '</td>';
                    324:     #
1.5       raeburn   325:     $str .= '<td align="center">'."\n";
                    326:     $str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5);
                    327:     $str .= '</td>';
                    328:     #
1.1       albertel  329:     $str .= '<td align="center">';
                    330:     $str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
                    331:     $str .= '</td>';
                    332:     #
                    333:     $str .= '<td>';
                    334:     $str .= '</td>';
                    335:     ##
                    336:     ##
                    337:     $str .= '</tr>'."\n";
                    338:     $str .= '</table>'."\n";
                    339:     return $str;
                    340: }
                    341: 
                    342: 1;
                    343: 
                    344: __END__

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