File:  [LON-CAPA] / loncom / interface / statistics / longradinganalysis.pm
Revision 1.14: download - view: text, annotated - select for diffs
Wed Feb 26 16:01:56 2014 UTC (10 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0, HEAD
- Balance HTML tags -- modification to changes in rev. 1.13.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: longradinganalysis.pm,v 1.14 2014/02/26 16:01:56 raeburn Exp $
    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();
   37: use Spreadsheet::WriteExcel;
   38: use Spreadsheet::WriteExcel::Utility();
   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',
   58:                                'Group' => 'array',
   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'})) {
   72:         $r->print(
   73:             '<p class="LC_info">'
   74:            .&mt('There are no students in the sections/groups selected.')
   75:            .'</p>');
   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'})) {
  126:             $r->print('<p class="LC_error">'.&mt('resource is undefined').'</p>');
  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 {
  136:                 $r->print(
  137:                     '<p class="LC_warning">'
  138:                    .&mt('Analysis of [_1] is not supported.',$resource->src())
  139:                    .'</p>');
  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) = @_;
  158:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,scalar(@$students));
  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);
  166: 	my $last_grader;
  167: 	foreach my $ver (0..$data{'version'}) {
  168: 	    if (exists($data{"$ver:resource.0.regrader"})
  169: 		&& $data{"$ver:resource.0.regrader"} =~ /\S/) {
  170: 		$last_grader=$data{"$ver:resource.0.regrader"};
  171: 	    }
  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}++;
  179: 	}
  180: 
  181: 	&Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  182:                                                      'last student');
  183: 
  184:     }
  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: 
  193:     if ($env{'form.output'} eq 'csv') {
  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 />'.
  203: 		  '<a href="'.$filename.'">'.&mt('Your CSV file.')."</a>\n");
  204:     } elsif ($env{'form.output'} eq 'excel') {
  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");
  221:     } else {
  222: 	$r->print(&Apache::loncommon::start_data_table());
  223: 	$r->print(&Apache::loncommon::start_data_table_header_row().
  224: 		  '<th>'.&mt('Name (username)').'</th><th>'.&mt('Grades Assigned').'</th>'.
  225: 		  &Apache::loncommon::end_data_table_header_row() );
  226: 	foreach my $line (@output) {
  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());
  231: 	}
  232: 	$r->print(&Apache::loncommon::end_data_table());
  233:     }
  234:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  235: }
  236: 
  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 = '';
  274: #    my @Sections = &Apache::lonstatistics::get_selected_sections(); #This is never used
  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++,
  280: 			&mt('Compiled on [_1]',&Apache::lonlocal::locallocaltime(time)),
  281:                         $format->{'h3'});
  282:     $cols_output = 0;
  283:     foreach my $field ('Grader Name','Username','Grades Assigned') {
  284: 	$excel_sheet->write($description_row,$cols_output++,&mt($field),
  285: 			    $format->{'bold'});
  286:     }
  287:     return ($excel_workbook,$excel_sheet,$filename,$format,$rows_output);
  288: }
  289: 
  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($_)) {
  304:             $output_selector .= ' selected="selected"';
  305:         }
  306:         $output_selector .='>'.&mt($_).'</option>'.$/;
  307:     } 
  308:     $output_selector .= '</select>'.$/;
  309: 
  310:     my $str = '';
  311:     $str .= &Apache::lonhtmlcommon::breadcrumbs('Detailed Grading Statistics');
  312:     $str .= '<table cellspacing="5">'."\n";
  313:     $str .= '<tr>';
  314:     $str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  315:     $str .= '<td align="center"><b>'.&mt('Groups').'</b></td>';
  316:     $str .= '<td align="center"><b>'.&mt('Access Status').'</b></td>';
  317:     $str .= '<td>'.&mt('[_1]Output as[_2] [_3]','<b>',$output_selector,'</b>').'</td>';
  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:     #
  325:     $str .= '<td align="center">'."\n";
  326:     $str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5);
  327:     $str .= '</td>';
  328:     #
  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>