Annotation of loncom/interface/statistics/lonsubmissiontimeanalysis.pm, revision 1.31

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: #
1.31    ! bisitz      3: # $Id: lonsubmissiontimeanalysis.pm,v 1.30 2008/10/30 17:45:25 bisitz Exp $
1.1       matthew     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;
1.23      albertel   31: use Apache::lonnet;
1.1       matthew    32: use Apache::loncommon();
                     33: use Apache::lonhtmlcommon();
                     34: use Apache::loncoursedata();
                     35: use Apache::lonstatistics;
1.10      matthew    36: use Apache::lonstathelpers;
1.1       matthew    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 = (
1.11      matthew    47:                      { name => 'PrevProblemAnalysis',
                     48:                        text => 'Previous Problem' },
                     49:                      { name => 'ProblemAnalysis',
1.1       matthew    50:                        text => 'Analyze Problem Again' },
1.11      matthew    51:                      { name => 'NextProblemAnalysis',
                     52:                        text => 'Next Problem' },
1.1       matthew    53:                      { name => 'SelectAnother',
1.11      matthew    54:                        text => 'Choose a different Problem' },
1.1       matthew    55:                      );
                     56: 
                     57: sub BuildSubmissionTimePage {
                     58:     my ($r,$c)=@_;
1.4       matthew    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:     #
1.1       matthew    69:     $r->print(&CreateInterface());
                     70:     #
                     71:     my @Students = @Apache::lonstatistics::Students;
                     72:     #
                     73:     if (@Students < 1) {
1.30      bisitz     74:         $r->print('<div class="LC_warning">'
                     75:                  .&mt('There are no students in the sections selected.'
                     76:                  .'</div>')
                     77:         );
1.1       matthew    78:     }
                     79:     #
1.15      matthew    80:     my @CacheButtonHTML = 
                     81:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
1.1       matthew    82:     $r->rflush();
                     83:     #
1.23      albertel   84:     if (! exists($env{'form.problemchoice'}) ||
                     85:         exists($env{'form.SelectAnother'})) {
1.24      matthew    86:         my $submit_button = '<input type="submit" name="" value="'.
1.30      bisitz     87: #           &mt('Graph Problem Submission Times').'" />';
                     88:             &mt('Generate Graph').'" />';
1.24      matthew    89:         $r->print($submit_button.'&nbsp;'x5);
1.1       matthew    90:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
1.24      matthew    91:         $r->print(&Apache::lonstathelpers::problem_selector('.',
                     92:                                                             $submit_button));
1.1       matthew    93:     } else {
                     94:         foreach my $button (@SubmitButtons) {
                     95:             $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
                     96:                       'value="'.&mt($button->{'text'}).'" />');
                     97:             $r->print('&nbsp;'x5);
                     98:         }
1.15      matthew    99:         foreach my $html (@CacheButtonHTML) {
                    100:             $r->print($html.('&nbsp;'x5));
                    101:         }
                    102:         $r->rflush();
1.11      matthew   103:         #
                    104:         # Determine which problem we are to analyze
                    105:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
1.23      albertel  106:             ($env{'form.problemchoice'});
1.11      matthew   107:         #
1.18      matthew   108:         my ($navmap,$prev,$curr,$next) = 
1.11      matthew   109:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
                    110:                                                         '.',
                    111:                                                         'part');
1.23      albertel  112:         if (exists($env{'form.PrevProblemAnalysis'}) && defined($prev)) {
1.11      matthew   113:             $current_problem = $prev;
1.23      albertel  114:         } elsif (exists($env{'form.NextProblemAnalysis'}) && defined($next)) {
1.11      matthew   115:             $current_problem = $next;
                    116:         } else {
                    117:             $current_problem = $curr;
                    118:         }
                    119:         #
                    120:         # Store the current problem choice and send it out in the form
1.23      albertel  121:         $env{'form.problemchoice'} = 
1.11      matthew   122:             &Apache::lonstathelpers::make_target_id($current_problem);
1.1       matthew   123:         $r->print('<input type="hidden" name="problemchoice" value="'.
1.23      albertel  124:                   $env{'form.problemchoice'}.'" />');
1.1       matthew   125:         #
                    126:         $r->print('<hr />');
                    127:         $r->rflush();
                    128:         #
1.11      matthew   129:         my $resource = $current_problem->{'resource'};
1.1       matthew   130:         if (! defined($resource)) {
1.30      bisitz    131:             $r->print('<div class="LC_warning">'
                    132:                      .&mt('Resource is undefined.')
                    133:                      .'</div>'
                    134:             );
1.1       matthew   135:         } else {
1.18      matthew   136:             $r->print('<h1>'.$resource->compTitle.'</h1>');
                    137:             $r->print('<h3>'.$resource->src.'</h3>');
1.22      matthew   138:             $r->print('<h4>'.
                    139:                  &Apache::lonstatistics::section_and_enrollment_description().
                    140:                       '</h4>');
1.1       matthew   141:             $r->rflush();
1.30      bisitz    142:             $r->print('<hr />');
1.10      matthew   143:             $r->print(&Apache::lonstathelpers::render_resource($resource));
1.30      bisitz    144:             $r->print('<hr /><br />');
1.1       matthew   145:             $r->rflush();
1.28      albertel  146: 	    if (@Students) {	    
                    147: 		$r->print(&analyze_times($r,$resource->symb,\@Students,
                    148: 					 $current_problem->{'part'}));
                    149: 	    }
1.1       matthew   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 {
1.18      matthew   174:     my ($r,$symb,$students,$part) = @_;
1.16      matthew   175:     my $htmltable;
1.2       matthew   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:     #
1.1       matthew   183:     my $html; # holds results of analysis
                    184:     # Get the data
                    185:     my $SubData = &Apache::loncoursedata::get_response_time_data
1.22      matthew   186:         ([&Apache::lonstatistics::get_selected_sections()],
1.26      raeburn   187:          [&Apache::lonstatistics::get_selected_groups()],
1.18      matthew   188:          $Apache::lonstatistics::enrollment_status,
                    189:          $symb,$part);
1.1       matthew   190:     if (! defined($SubData) || ! ref($SubData)) {
1.30      bisitz    191:         $html.= '<div class="LC_warning">'
                    192:                .&mt('There is no submission data for this problem at all.')
                    193:                .'</div>';
1.1       matthew   194:         return $html;
                    195:     }
                    196:     my $NumSub = scalar(@{$SubData});
                    197:     if (! @{$SubData}) {
1.30      bisitz    198:         $html.= '<div class="LC_warning">'
                    199:                .&mt('There is no submission data for this problem.')
                    200:                .'</div>';
1.1       matthew   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:     #
1.2       matthew   209:     # Configure the bins used to store the data.
1.1       matthew   210:     my $binsize = 3600; # seconds
                    211:     my $bins_per_day = 86400/$binsize;
                    212:     my $bincount = 0;
                    213:     my $endtime = $day_start;
                    214:     #
1.2       matthew   215:     # Initialize loop variables
1.17      matthew   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
1.19      matthew   222:     my %students;       # which students have attempted the problem?
1.2       matthew   223:     #
1.1       matthew   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++;
1.17      matthew   229:             $Ydata[$bincount]   = 0;
                    230:             $AnsData[$bincount] = 0;
1.1       matthew   231:             $endtime += $binsize;
1.16      matthew   232:             push(@BinEnd,$endtime);
1.1       matthew   233:             if ($bincount % (86400/$binsize) == 0) {
1.2       matthew   234:                 $wday++;
1.1       matthew   235:                 $wday %= 7;
1.2       matthew   236:                 $Xlabel[$bincount] = $WeekDay[$wday];
1.1       matthew   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);
1.19      matthew   245:         $students{$row->[&Apache::loncoursedata::RT_student_id()]}++;
1.1       matthew   246:     }
1.17      matthew   247:     #
                    248:     # Pad the data to a full day
1.1       matthew   249:     while ($bincount % $bins_per_day != 0) {
                    250:         $bincount++;
                    251:         $Ydata[$bincount]=0;
1.17      matthew   252:         $AnsData[$bincount]=0;
1.1       matthew   253:         $endtime += $binsize;
1.17      matthew   254:         push(@BinEnd,$endtime);
1.1       matthew   255:         if ($bincount % (86400/$binsize) == 0) {
1.2       matthew   256:             $wday ++;
                    257:             $wday %= 7;
1.1       matthew   258:             $Xlabel[$bincount] = $WeekDay[$wday];
                    259:         } else {
                    260:             $Xlabel[$bincount] = '';
                    261:         }
                    262:     }
1.19      matthew   263:     my $numstudents = scalar(keys(%students));
1.17      matthew   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
1.16      matthew   277:     $htmltable = '<p>'.
                    278:         '<table rules="groups" frame="border" '.
                    279:         'summary="Student submission data">'.
                    280:         '<thead>'.
                    281:         '<tr>'.
1.17      matthew   282:         '<th valign="bottom">'.&mt('Begin').'</th>'.
1.16      matthew   283:         '<th>'.('&nbsp;'x3).'</th>'.
1.17      matthew   284:         '<th valign="bottom">'.&mt('End').'</th>'.
1.20      matthew   285:         '<th valign="bottom">'.&mt('Submissions (plotted)').'</th>'.
1.16      matthew   286:         '<th>'.('&nbsp;'x3).'</th>'.
1.20      matthew   287:         '<th valign="bottom">'.&mt('Correct Submissions (not plotted)').'</th>'.
1.16      matthew   288:         '<th>'.('&nbsp;'x3).'</th>'.
1.20      matthew   289:         '<th valign="bottom">'.&mt('Cumulative Correct of those attempting the problem (not plotted)').'</th>'.
1.19      matthew   290:         '<th>'.('&nbsp;'x3).'</th>'.
1.20      matthew   291:         '<th valign="bottom">'.&mt('Cumulative Percent Correct of those attempting the problem (not plotted)').'</th>'.
                    292:         '<th>'.('&nbsp;'x3).'</th>'.
                    293:         '<th valign="bottom">'.&mt('Cumulative Percent Correct of selected students (plotted)').'</th>'.
1.16      matthew   294:         '</tr>'.
                    295:         '</thead>'.
                    296:         '<tbody>';
1.17      matthew   297:     my @CumulativeCorrect=(0);
1.20      matthew   298:     my @corr_as_percent_of_selected;
                    299:     my @corr_as_percent_of_answering;
1.17      matthew   300:     for (my $i=0;$i<=$#Ydata;$i++) {
                    301:         $CumulativeCorrect[$i]=$CumulativeCorrect[-1]+$AnsData[$i];
1.20      matthew   302:         $corr_as_percent_of_answering[$i] = 
                    303:             sprintf('%3.1f',100*$CumulativeCorrect[$i]/$numstudents);
                    304:         $corr_as_percent_of_selected[$i] = 
                    305:             sprintf('%3.1f',100*$CumulativeCorrect[$i]/scalar(@$students));
1.16      matthew   306:         if ($Ydata[$i] != 0) {
                    307:             next if (! defined($BinEnd[$i]) || $BinEnd[$i] == 0);
                    308:             $htmltable .= 
                    309:                 '<tr>'.
1.31    ! bisitz    310:                 '<td align="right"><span class="LC_nobreak">'.
1.16      matthew   311:                 &Apache::lonlocal::locallocaltime($BinEnd[$i]-$binsize).
1.31    ! bisitz    312:                 '</span></td>'.
1.16      matthew   313:                 '<td>&nbsp;</td>'.
1.31    ! bisitz    314:                 '<td align="right"><span class="LC_nobreak">'.
1.16      matthew   315:                     &Apache::lonlocal::locallocaltime($BinEnd[$i]).'</td>'.
1.31    ! bisitz    316:                 '</span></td>'.
1.16      matthew   317:                 '<td align="right">'.$Ydata[$i].('&nbsp;'x3).'</td>'.
                    318:                 '<td>&nbsp;</td>'.
1.17      matthew   319:                 '<td align="right">'.$AnsData[$i].('&nbsp;'x3).'</td>'.
                    320:                 '<td>&nbsp;</td>'.
1.19      matthew   321:                 '<td align="right">'.$CumulativeCorrect[$i].'</td>'.
                    322:                 '<td>&nbsp;</td>'.
1.20      matthew   323:                 '<td align="right">'.$corr_as_percent_of_answering[$i].'</td>'.
                    324:                 '<td>&nbsp;</td>'.
                    325:                 '<td align="right">'.$corr_as_percent_of_selected[$i].'</td>'.
1.16      matthew   326:                 '</tr>'.$/;
                    327:         }
1.6       matthew   328:     }
1.16      matthew   329:     $htmltable .= '</tbody></table></p>';
1.17      matthew   330:     #
                    331:     # Build the plot
                    332:     my $title = '';#'Number of Submissions and Number Correct';
1.1       matthew   333:     my $xlabel;
1.2       matthew   334:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($day_start);
                    335:     $xlabel .= $FullWeekDay[$wday].' '.
                    336:         join(' ',($Month[$month],$mday,1900+$year)).' - ';
                    337:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($endtime);
                    338:     $xlabel .= $FullWeekDay[$wday].' '.
                    339:         join(' ',($Month[$month],$mday,1900+$year));
1.8       matthew   340:     my $width = 50+2*$bincount;
                    341:     if ($width < 250) {
                    342:         $width = 250;
                    343:     }
1.17      matthew   344:     #
1.1       matthew   345:     $html .= &Apache::loncommon::DrawXYYGraph($title,
                    346:                                               $xlabel,
1.6       matthew   347:                                               'Submissions vs Time',
1.1       matthew   348:                                               $plotcolors,
                    349:                                               \@Xlabel,
1.17      matthew   350:                                               \@Ydata,0,$max,
1.20      matthew   351:                                               \@corr_as_percent_of_selected,0,100,
1.1       matthew   352:                                               (xskip => $bins_per_day,
                    353:                                                x_ticks => $bins_per_day,
                    354:                                                x_tick_offset => $bins_per_day,
1.8       matthew   355:                                                width => $width,
1.7       matthew   356:                       y1_label=>'Number of Submissions per hour',
                    357:                       y2_label=>'Percent of Students answering Correctly',
                    358:                      'data.1.label'=>'Submissions per hour',
                    359:                      'data.2.label'=>'Percent correct',
                    360:                                                )
1.1       matthew   361:                                               );
1.16      matthew   362:     $html .= '<br />'.$htmltable;
1.1       matthew   363:     return $html;
                    364: }
                    365: 
                    366: sub successful_submission {
                    367:     my ($row) = @_;
                    368:     if (ref($row) eq 'ARRAY') {
1.3       matthew   369:         return $row->[&Apache::loncoursedata::RT_awarded()];
1.1       matthew   370:     }
                    371:     return undef;
                    372: }
                    373: 
                    374: sub get_time_from_row {
                    375:     my ($row) = @_;
                    376:     if (ref($row) eq 'ARRAY') {
1.3       matthew   377:         return $row->[&Apache::loncoursedata::RT_timestamp()];
1.1       matthew   378:     } 
                    379:     return undef;
                    380: }
                    381: 
                    382: sub get_tries_from_row {
                    383:     my ($row) = @_;
                    384:     if (ref($row) eq 'ARRAY') {
1.3       matthew   385:         return $row->[&Apache::loncoursedata::RT_tries()];
1.1       matthew   386:     }
                    387:     return undef;
                    388: }
                    389: 
                    390: sub Process_Row {
                    391:     my ($row) = @_;
                    392:     my %RowData;
                    393:     my ($student_id,$award,$tries,$time) = @$row;
                    394:     return %RowData;
                    395: }
                    396: 
                    397: #########################################################
                    398: #########################################################
                    399: ##
                    400: ##   Generic Interface Routines
                    401: ##
                    402: #########################################################
                    403: #########################################################
                    404: sub CreateInterface {
                    405:     ##
                    406:     ## Environment variable initialization
1.23      albertel  407:     if (! exists$env{'form.AnalyzeOver'}) {
                    408:         $env{'form.AnalyzeOver'} = 'Tries';
1.1       matthew   409:     }
                    410:     ##
                    411:     ## Build the menu
                    412:     my $Str = '';
1.25      albertel  413:     $Str .= &Apache::lonhtmlcommon::breadcrumbs('Submission Time Plots');
1.15      matthew   414:     $Str .= '<p>';
1.30      bisitz    415:     $Str .= &Apache::loncommon::start_data_table();
                    416:     $Str .= &Apache::loncommon::start_data_table_header_row();
                    417:     $Str .= '<th>'.&mt('Sections').'</th>';
                    418:     $Str .= '<th>'.&mt('Groups').'</th>';
                    419:     $Str .= '<th>'.&mt('Access Status').'</th>';
                    420:     $Str .= &Apache::loncommon::end_data_table_header_row();
1.1       matthew   421:     ##
                    422:     ## 
1.30      bisitz    423:     $Str .= &Apache::loncommon::start_data_table_row();
                    424:     $Str .= '<td align="center">'."\n";
1.16      matthew   425:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',4);
1.1       matthew   426:     $Str .= '</td>';
                    427:     #
1.26      raeburn   428:     $Str .= '<td align="center">'."\n";
                    429:     $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',4);
                    430:     $Str .= '</td>';
                    431:     #
1.1       matthew   432:     $Str .= '<td align="center">';
1.16      matthew   433:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,4);
1.1       matthew   434:     $Str .= '</td>';
1.16      matthew   435:     #
1.30      bisitz    436:     $Str .= &Apache::loncommon::end_data_table_row();
                    437:     $Str .= &Apache::loncommon::end_data_table();
1.1       matthew   438:     #
1.29      bisitz    439:     $Str .= '<p><span class="LC_nobreak">'
                    440:            .&mt('Status: [_1]',
                    441:                     '<input type="text" name="stats_status"'
                    442:                    .' size="60" value="" readonly="readonly" />')
                    443:            .'</span></p>';
                    444:     $Str .= '</p>';
1.1       matthew   445:     ##
                    446:     return $Str;
                    447: }
                    448: 
                    449: 1;
                    450: 
                    451: __END__

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