File:  [LON-CAPA] / loncom / interface / statistics / lonsubmissiontimeanalysis.pm
Revision 1.35.2.2: download - view: text, annotated - select for diffs
Mon Dec 17 02:22:12 2012 UTC (11 years, 6 months ago) by raeburn
Branches: version_2_11_X
CVS tags: 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_RC2, version_2_11_0_RC1, version_2_11_0
- For 2.11
  - Backport 1.36.

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

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