File:  [LON-CAPA] / loncom / interface / statistics / lonsubmissiontimeanalysis.pm
Revision 1.32: download - view: text, annotated - select for diffs
Mon Jan 17 00:19:41 2011 UTC (13 years, 4 months ago) by www
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD
Add statistics and reports under "grading"

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonsubmissiontimeanalysis.pm,v 1.32 2011/01/17 00:19:41 www 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: 
   28: package Apache::lonsubmissiontimeanalysis;
   29: 
   30: use strict;
   31: use Apache::lonnet;
   32: use Apache::loncommon();
   33: use Apache::lonhtmlcommon();
   34: use Apache::lonquickgrades();
   35: use Apache::loncoursedata();
   36: use Apache::lonstatistics;
   37: use Apache::lonstathelpers;
   38: use Apache::lonlocal;
   39: use HTML::Entities();
   40: use Time::Local();
   41: 
   42: my $plotcolors = ['#33ff00', 
   43:                   '#ff33cc', '#990000', '#aaaa66', '#663399', '#ff9933',
   44:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
   45:                   ]; 
   46: 
   47: my @SubmitButtons = (
   48:                      { name => 'PrevProblemAnalysis',
   49:                        text => 'Previous Problem' },
   50:                      { name => 'ProblemAnalysis',
   51:                        text => 'Analyze Problem Again' },
   52:                      { name => 'NextProblemAnalysis',
   53:                        text => 'Next Problem' },
   54:                      { name => 'SelectAnother',
   55:                        text => 'Choose a different Problem' },
   56:                      );
   57: 
   58: sub BuildSubmissionTimePage {
   59:     my ($r,$c)=@_;
   60:     #
   61:     my %Saveable_Parameters = ('Status' => 'scalar',
   62:                                'Section' => 'array');
   63:     &Apache::loncommon::store_course_settings('submissiontime_analysis',
   64:                                               \%Saveable_Parameters);
   65:     &Apache::loncommon::restore_course_settings('submissiontime_analysis',
   66:                                                 \%Saveable_Parameters);
   67:     #
   68:     &Apache::lonstatistics::PrepareClasslist();    
   69:     #
   70:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Submission Time Plots'));
   71:     &Apache::lonquickgrades::startGradeScreen($r,'statistics');
   72:     $r->print(&CreateInterface());
   73:     #
   74:     my @Students = @Apache::lonstatistics::Students;
   75:     #
   76:     if (@Students < 1) {
   77:         $r->print('<div class="LC_warning">'
   78:                  .&mt('There are no students in the sections selected.'
   79:                  .'</div>')
   80:         );
   81:     }
   82:     #
   83:     my @CacheButtonHTML = 
   84:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
   85:     $r->rflush();
   86:     #
   87:     if (! exists($env{'form.problemchoice'}) ||
   88:         exists($env{'form.SelectAnother'})) {
   89:         my $submit_button = '<input type="submit" name="" value="'.
   90: #           &mt('Graph Problem Submission Times').'" />';
   91:             &mt('Generate Graph').'" />';
   92:         $r->print($submit_button.'&nbsp;'x5);
   93:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
   94:         $r->print(&Apache::lonstathelpers::problem_selector('.',
   95:                                                             $submit_button));
   96:     } else {
   97:         foreach my $button (@SubmitButtons) {
   98:             $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
   99:                       'value="'.&mt($button->{'text'}).'" />');
  100:             $r->print('&nbsp;'x5);
  101:         }
  102:         foreach my $html (@CacheButtonHTML) {
  103:             $r->print($html.('&nbsp;'x5));
  104:         }
  105:         $r->rflush();
  106:         #
  107:         # Determine which problem we are to analyze
  108:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
  109:             ($env{'form.problemchoice'});
  110:         #
  111:         my ($navmap,$prev,$curr,$next) = 
  112:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
  113:                                                         '.',
  114:                                                         'part');
  115:         if (exists($env{'form.PrevProblemAnalysis'}) && defined($prev)) {
  116:             $current_problem = $prev;
  117:         } elsif (exists($env{'form.NextProblemAnalysis'}) && defined($next)) {
  118:             $current_problem = $next;
  119:         } else {
  120:             $current_problem = $curr;
  121:         }
  122:         #
  123:         # Store the current problem choice and send it out in the form
  124:         $env{'form.problemchoice'} = 
  125:             &Apache::lonstathelpers::make_target_id($current_problem);
  126:         $r->print('<input type="hidden" name="problemchoice" value="'.
  127:                   $env{'form.problemchoice'}.'" />');
  128:         #
  129:         $r->print('<hr />');
  130:         $r->rflush();
  131:         #
  132:         my $resource = $current_problem->{'resource'};
  133:         if (! defined($resource)) {
  134:             $r->print('<div class="LC_warning">'
  135:                      .&mt('Resource is undefined.')
  136:                      .'</div>'
  137:             );
  138:         } else {
  139:             $r->print('<h1>'.$resource->compTitle.'</h1>');
  140:             $r->print('<h3>'.$resource->src.'</h3>');
  141:             $r->print('<h4>'.
  142:                  &Apache::lonstatistics::section_and_enrollment_description().
  143:                       '</h4>');
  144:             $r->rflush();
  145:             $r->print('<hr />');
  146:             $r->print(&Apache::lonstathelpers::render_resource($resource));
  147:             $r->print('<hr /><br />');
  148:             $r->rflush();
  149: 	    if (@Students) {	    
  150: 		$r->print(&analyze_times($r,$resource->symb,\@Students,
  151: 					 $current_problem->{'part'}));
  152: 	    }
  153:         }
  154:         $r->print('<hr />');
  155:     }
  156: }
  157: 
  158: #########################################################
  159: #########################################################
  160: ##
  161: ##                  Time Analysis
  162: ##
  163: #########################################################
  164: #########################################################
  165: sub get_week_start {
  166:     my ($randomtime) = @_;
  167:     my ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = 
  168:         localtime($randomtime);
  169:     $randomtime -= $wday * 86400;
  170:     ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = 
  171:         localtime($randomtime);
  172:     my $week_start = &Time::Local::timelocal(0,0,0,$mday,$month,$year);
  173:     return $week_start;
  174: }
  175: 
  176: sub analyze_times {
  177:     my ($r,$symb,$students,$part) = @_;
  178:     my $htmltable;
  179:     #
  180:     # Convenience arrays
  181:     my @FullWeekDay = (qw/Sunday Monday Tuesday Wednesday Thursday Friday
  182:                        Saturday/);
  183:     my @WeekDay = (qw/SUN MON TUE WED THU FRI SAT SUN/);
  184:     my @Month = (qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/);
  185:     #
  186:     my $html; # holds results of analysis
  187:     # Get the data
  188:     my $SubData = &Apache::loncoursedata::get_response_time_data
  189:         ([&Apache::lonstatistics::get_selected_sections()],
  190:          [&Apache::lonstatistics::get_selected_groups()],
  191:          $Apache::lonstatistics::enrollment_status,
  192:          $symb,$part);
  193:     if (! defined($SubData) || ! ref($SubData)) {
  194:         $html.= '<div class="LC_warning">'
  195:                .&mt('There is no submission data for this problem at all.')
  196:                .'</div>';
  197:         return $html;
  198:     }
  199:     my $NumSub = scalar(@{$SubData});
  200:     if (! @{$SubData}) {
  201:         $html.= '<div class="LC_warning">'
  202:                .&mt('There is no submission data for this problem.')
  203:                .'</div>';
  204:         return $html;
  205:     }
  206:     # Process the data
  207:     #
  208:     my (undef,undef,undef,$mday,$month,$year,$wday,$yday,$isdst) = 
  209:         localtime(&get_time_from_row($SubData->[0]));
  210:     my $day_start = &Time::Local::timelocal(0,0,0,$mday,$month,$year);
  211:     #
  212:     # Configure the bins used to store the data.
  213:     my $binsize = 3600; # seconds
  214:     my $bins_per_day = 86400/$binsize;
  215:     my $bincount = 0;
  216:     my $endtime = $day_start;
  217:     #
  218:     # Initialize loop variables
  219:     my $max;            # The sum of @Ydata
  220:     my @Ydata=(0);      # number of submissions
  221:     my @AnsData=(0);    # number of correct submissions
  222:     my @Xlabel=($WeekDay[$wday]); # Labels of itmes
  223:     my @BinEnd;                   # The end time of each bin
  224:     my $cumulative_answers = 0;   # The sum of @AnsData
  225:     my %students;       # which students have attempted the problem?
  226:     #
  227:     foreach my $row (@$SubData) {
  228:         my $subtime = &get_time_from_row($row);
  229:         while ($subtime > $endtime && $endtime < time) {
  230:             # Create a new bin
  231:             $bincount++;
  232:             $Ydata[$bincount]   = 0;
  233:             $AnsData[$bincount] = 0;
  234:             $endtime += $binsize;
  235:             push(@BinEnd,$endtime);
  236:             if ($bincount % (86400/$binsize) == 0) {
  237:                 $wday++;
  238:                 $wday %= 7;
  239:                 $Xlabel[$bincount] = $WeekDay[$wday];
  240:             } else {
  241:                 $Xlabel[$bincount] = '';
  242:             }
  243:         }
  244:         $Ydata[$bincount]++;
  245:         $max = $Ydata[$bincount] if ($max < $Ydata[$bincount]);
  246:         $AnsData[$bincount] += &successful_submission($row);
  247:         $cumulative_answers += &successful_submission($row);
  248:         $students{$row->[&Apache::loncoursedata::RT_student_id()]}++;
  249:     }
  250:     #
  251:     # Pad the data to a full day
  252:     while ($bincount % $bins_per_day != 0) {
  253:         $bincount++;
  254:         $Ydata[$bincount]=0;
  255:         $AnsData[$bincount]=0;
  256:         $endtime += $binsize;
  257:         push(@BinEnd,$endtime);
  258:         if ($bincount % (86400/$binsize) == 0) {
  259:             $wday ++;
  260:             $wday %= 7;
  261:             $Xlabel[$bincount] = $WeekDay[$wday];
  262:         } else {
  263:             $Xlabel[$bincount] = '';
  264:         }
  265:     }
  266:     my $numstudents = scalar(keys(%students));
  267:     #
  268:     # Determine a nice maximum value to use
  269:     foreach my $maximum (10,15,20,25,30,40,50,60,70,80,90,100,
  270:                           120,150,200,250,300,350,400,450,500,
  271:                           600,700,800,900,1000,1100,1200,1500,2000,
  272:                           2500,3000,4000,5000) {
  273:         if ($max < $maximum) {
  274:             $max = $maximum;
  275:             last;
  276:         }
  277:     }
  278:     #
  279:     # Build the data table
  280:     $htmltable = '<p>'.
  281:         '<table rules="groups" frame="border" '.
  282:         'summary="Student submission data">'.
  283:         '<thead>'.
  284:         '<tr>'.
  285:         '<th valign="bottom">'.&mt('Begin').'</th>'.
  286:         '<th>'.('&nbsp;'x3).'</th>'.
  287:         '<th valign="bottom">'.&mt('End').'</th>'.
  288:         '<th valign="bottom">'.&mt('Submissions (plotted)').'</th>'.
  289:         '<th>'.('&nbsp;'x3).'</th>'.
  290:         '<th valign="bottom">'.&mt('Correct Submissions (not plotted)').'</th>'.
  291:         '<th>'.('&nbsp;'x3).'</th>'.
  292:         '<th valign="bottom">'.&mt('Cumulative Correct of those attempting the problem (not plotted)').'</th>'.
  293:         '<th>'.('&nbsp;'x3).'</th>'.
  294:         '<th valign="bottom">'.&mt('Cumulative Percent Correct of those attempting the problem (not plotted)').'</th>'.
  295:         '<th>'.('&nbsp;'x3).'</th>'.
  296:         '<th valign="bottom">'.&mt('Cumulative Percent Correct of selected students (plotted)').'</th>'.
  297:         '</tr>'.
  298:         '</thead>'.
  299:         '<tbody>';
  300:     my @CumulativeCorrect=(0);
  301:     my @corr_as_percent_of_selected;
  302:     my @corr_as_percent_of_answering;
  303:     for (my $i=0;$i<=$#Ydata;$i++) {
  304:         $CumulativeCorrect[$i]=$CumulativeCorrect[-1]+$AnsData[$i];
  305:         $corr_as_percent_of_answering[$i] = 
  306:             sprintf('%3.1f',100*$CumulativeCorrect[$i]/$numstudents);
  307:         $corr_as_percent_of_selected[$i] = 
  308:             sprintf('%3.1f',100*$CumulativeCorrect[$i]/scalar(@$students));
  309:         if ($Ydata[$i] != 0) {
  310:             next if (! defined($BinEnd[$i]) || $BinEnd[$i] == 0);
  311:             $htmltable .= 
  312:                 '<tr>'.
  313:                 '<td align="right"><span class="LC_nobreak">'.
  314:                 &Apache::lonlocal::locallocaltime($BinEnd[$i]-$binsize).
  315:                 '</span></td>'.
  316:                 '<td>&nbsp;</td>'.
  317:                 '<td align="right"><span class="LC_nobreak">'.
  318:                     &Apache::lonlocal::locallocaltime($BinEnd[$i]).'</td>'.
  319:                 '</span></td>'.
  320:                 '<td align="right">'.$Ydata[$i].('&nbsp;'x3).'</td>'.
  321:                 '<td>&nbsp;</td>'.
  322:                 '<td align="right">'.$AnsData[$i].('&nbsp;'x3).'</td>'.
  323:                 '<td>&nbsp;</td>'.
  324:                 '<td align="right">'.$CumulativeCorrect[$i].'</td>'.
  325:                 '<td>&nbsp;</td>'.
  326:                 '<td align="right">'.$corr_as_percent_of_answering[$i].'</td>'.
  327:                 '<td>&nbsp;</td>'.
  328:                 '<td align="right">'.$corr_as_percent_of_selected[$i].'</td>'.
  329:                 '</tr>'.$/;
  330:         }
  331:     }
  332:     $htmltable .= '</tbody></table></p>';
  333:     #
  334:     # Build the plot
  335:     my $title = '';#'Number of Submissions and Number Correct';
  336:     my $xlabel;
  337:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($day_start);
  338:     $xlabel .= $FullWeekDay[$wday].' '.
  339:         join(' ',($Month[$month],$mday,1900+$year)).' - ';
  340:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($endtime);
  341:     $xlabel .= $FullWeekDay[$wday].' '.
  342:         join(' ',($Month[$month],$mday,1900+$year));
  343:     my $width = 50+2*$bincount;
  344:     if ($width < 250) {
  345:         $width = 250;
  346:     }
  347:     #
  348:     $html .= &Apache::loncommon::DrawXYYGraph($title,
  349:                                               $xlabel,
  350:                                               'Submissions vs Time',
  351:                                               $plotcolors,
  352:                                               \@Xlabel,
  353:                                               \@Ydata,0,$max,
  354:                                               \@corr_as_percent_of_selected,0,100,
  355:                                               (xskip => $bins_per_day,
  356:                                                x_ticks => $bins_per_day,
  357:                                                x_tick_offset => $bins_per_day,
  358:                                                width => $width,
  359:                       y1_label=>'Number of Submissions per hour',
  360:                       y2_label=>'Percent of Students answering Correctly',
  361:                      'data.1.label'=>'Submissions per hour',
  362:                      'data.2.label'=>'Percent correct',
  363:                                                )
  364:                                               );
  365:     $html .= '<br />'.$htmltable;
  366:     return $html;
  367: }
  368: 
  369: sub successful_submission {
  370:     my ($row) = @_;
  371:     if (ref($row) eq 'ARRAY') {
  372:         return $row->[&Apache::loncoursedata::RT_awarded()];
  373:     }
  374:     return undef;
  375: }
  376: 
  377: sub get_time_from_row {
  378:     my ($row) = @_;
  379:     if (ref($row) eq 'ARRAY') {
  380:         return $row->[&Apache::loncoursedata::RT_timestamp()];
  381:     } 
  382:     return undef;
  383: }
  384: 
  385: sub get_tries_from_row {
  386:     my ($row) = @_;
  387:     if (ref($row) eq 'ARRAY') {
  388:         return $row->[&Apache::loncoursedata::RT_tries()];
  389:     }
  390:     return undef;
  391: }
  392: 
  393: sub Process_Row {
  394:     my ($row) = @_;
  395:     my %RowData;
  396:     my ($student_id,$award,$tries,$time) = @$row;
  397:     return %RowData;
  398: }
  399: 
  400: #########################################################
  401: #########################################################
  402: ##
  403: ##   Generic Interface Routines
  404: ##
  405: #########################################################
  406: #########################################################
  407: sub CreateInterface {
  408:     ##
  409:     ## Environment variable initialization
  410:     if (! exists$env{'form.AnalyzeOver'}) {
  411:         $env{'form.AnalyzeOver'} = 'Tries';
  412:     }
  413:     ##
  414:     ## Build the menu
  415:     my $Str = '';
  416:     $Str .= '<p>';
  417:     $Str .= &Apache::loncommon::start_data_table();
  418:     $Str .= &Apache::loncommon::start_data_table_header_row();
  419:     $Str .= '<th>'.&mt('Sections').'</th>';
  420:     $Str .= '<th>'.&mt('Groups').'</th>';
  421:     $Str .= '<th>'.&mt('Access Status').'</th>';
  422:     $Str .= &Apache::loncommon::end_data_table_header_row();
  423:     ##
  424:     ## 
  425:     $Str .= &Apache::loncommon::start_data_table_row();
  426:     $Str .= '<td align="center">'."\n";
  427:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',4);
  428:     $Str .= '</td>';
  429:     #
  430:     $Str .= '<td align="center">'."\n";
  431:     $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',4);
  432:     $Str .= '</td>';
  433:     #
  434:     $Str .= '<td align="center">';
  435:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,4);
  436:     $Str .= '</td>';
  437:     #
  438:     $Str .= &Apache::loncommon::end_data_table_row();
  439:     $Str .= &Apache::loncommon::end_data_table();
  440:     #
  441:     $Str .= '<p><span class="LC_nobreak">'
  442:            .&mt('Status: [_1]',
  443:                     '<input type="text" name="stats_status"'
  444:                    .' size="60" value="" readonly="readonly" />')
  445:            .'</span></p>';
  446:     $Str .= '</p>';
  447:     ##
  448:     return $Str;
  449: }
  450: 
  451: 1;
  452: 
  453: __END__

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