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

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

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