File:  [LON-CAPA] / loncom / interface / statistics / lonsubmissiontimeanalysis.pm
Revision 1.28: download - view: text, annotated - select for diffs
Thu Dec 21 19:52:28 2006 UTC (17 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: version_2_7_X, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_99_1, HEAD
- bug5095 - handle case of no students and no spread in provided answers
            more correctly (rather than infinite loop)

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

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