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

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

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