File:  [LON-CAPA] / loncom / interface / statistics / lonsubmissiontimeanalysis.pm
Revision 1.13: download - view: text, annotated - select for diffs
Fri Feb 20 16:38:49 2004 UTC (20 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Translate text passed in to &Apache::lonhtmlcommon::breadcrumbs and
&Apache::lonhtmlcommon::add_breadcrumb.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonsubmissiontimeanalysis.pm,v 1.13 2004/02/20 16:38:49 matthew 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: use Spreadsheet::WriteExcel();
   41: 
   42: my $plotcolors = ['#33ff00', 
   43:                   '#ff33cc', '#990000', '#aaaa66', '#663399', '#ff9933',
   44:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
   45:                   ]; 
   46: 
   47: my @SubmitButtons = (
   48:                      { name => 'PrevProblemAnalysis',
   49:                        text => 'Previous Problem' },
   50:                      { name => 'ProblemAnalysis',
   51:                        text => 'Analyze Problem Again' },
   52:                      { name => 'NextProblemAnalysis',
   53:                        text => 'Next Problem' },
   54:                      { name => 'SelectAnother',
   55:                        text => 'Choose a different Problem' },
   56:                      );
   57: 
   58: sub BuildSubmissionTimePage {
   59:     my ($r,$c)=@_;
   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:     #
   70:     $r->print(&CreateInterface());
   71:     #
   72:     my @Students = @Apache::lonstatistics::Students;
   73:     #
   74:     if (@Students < 1) {
   75:         $r->print('<h2>There are no students in the sections selected</h2>');
   76:     }
   77:     #
   78:     &Apache::loncoursedata::clear_internal_caches();
   79:     if (exists($ENV{'form.ClearCache'}) || 
   80:         exists($ENV{'form.updatecaches'}) ||
   81:         (exists($ENV{'form.firstanalysis'}) &&
   82:          $ENV{'form.firstanalysis'} ne 'no')) {
   83:         &Apache::lonstatistics::Gather_Full_Student_Data($r);
   84:     }
   85:     if (! exists($ENV{'form.firstanalysis'})) {
   86:         $r->print('<input type="hidden" name="firstanalysis" value="yes" />');
   87:     } else {
   88:         $r->print('<input type="hidden" name="firstanalysis" value="no" />');
   89:     }
   90:     $r->rflush();
   91:     #
   92:     if (! exists($ENV{'form.problemchoice'}) ||
   93:         exists($ENV{'form.SelectAnother'})) {
   94:         $r->print('<input type="submit" name="" value="'.
   95:                   &mt('Graph Problem Submission Times').'" />');
   96:         $r->print('&nbsp;'x5);
   97:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
   98:         $r->print(&Apache::lonstathelpers::ProblemSelector('.'));
   99:     } else {
  100:         foreach my $button (@SubmitButtons) {
  101:             $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
  102:                       'value="'.&mt($button->{'text'}).'" />');
  103:             $r->print('&nbsp;'x5);
  104:         }
  105:         #
  106:         # Determine which problem we are to analyze
  107:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
  108:             ($ENV{'form.problemchoice'});
  109:         #
  110:         my ($prev,$curr,$next) = 
  111:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
  112:                                                         '.',
  113:                                                         'part');
  114:         if (exists($ENV{'form.PrevProblemAnalysis'}) && defined($prev)) {
  115:             $current_problem = $prev;
  116:         } elsif (exists($ENV{'form.NextProblemAnalysis'}) && defined($next)) {
  117:             $current_problem = $next;
  118:         } else {
  119:             $current_problem = $curr;
  120:         }
  121:         #
  122:         # Store the current problem choice and send it out in the form
  123:         $ENV{'form.problemchoice'} = 
  124:             &Apache::lonstathelpers::make_target_id($current_problem);
  125:         $r->print('<input type="hidden" name="problemchoice" value="'.
  126:                   $ENV{'form.problemchoice'}.'" />');
  127:         #
  128:         $r->print('<hr />');
  129:         $r->rflush();
  130:         #
  131:         my $resource = $current_problem->{'resource'};
  132:         if (! defined($resource)) {
  133:             $r->print('resource is undefined');
  134:         } else {
  135:             $r->print('<h1>'.$resource->{'title'}.'</h1>');
  136:             $r->print('<h3>'.$resource->{'src'}.'</h3>');
  137:             $r->rflush();
  138:             $r->print(&Apache::lonstathelpers::render_resource($resource));
  139:             $r->rflush();
  140:             $r->print(&analyze_times($r,$resource,\@Students,
  141:                                      $current_problem->{'part'}));
  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,$resource,$students,$part) = @_;
  167:     #
  168:     # Convenience arrays
  169:     my @FullWeekDay = (qw/Sunday Monday Tuesday Wednesday Thursday Friday
  170:                        Saturday/);
  171:     my @WeekDay = (qw/SUN MON TUE WED THU FRI SAT SUN/);
  172:     my @Month = (qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/);
  173:     #
  174:     my $html; # holds results of analysis
  175:     # Get the data
  176:     my $SubData = &Apache::loncoursedata::get_response_time_data
  177:         ($students,$resource->{'symb'},$part);
  178:     if (! defined($SubData) || ! ref($SubData)) {
  179:         $html.= '<h2>There is no submission data for this problem</h2>';
  180:         return $html;
  181:     }
  182:     my $NumSub = scalar(@{$SubData});
  183:     if (! @{$SubData}) {
  184:         $html.= '<h2>There is no submission data for this problem</h2>';
  185:         return $html;
  186:     }
  187:     # Process the data
  188:     #
  189:     my (undef,undef,undef,$mday,$month,$year,$wday,$yday,$isdst) = 
  190:         localtime(&get_time_from_row($SubData->[0]));
  191:     my $day_start = &Time::Local::timelocal(0,0,0,$mday,$month,$year);
  192:     #
  193:     # Configure the bins used to store the data.
  194:     my $binsize = 3600; # seconds
  195:     my $bins_per_day = 86400/$binsize;
  196:     my $bincount = 0;
  197:     my $endtime = $day_start;
  198:     #
  199:     # Initialize loop variables
  200:     my $max;
  201:     my @Ydata=(0);
  202:     my @AnsData=(0);
  203:     my @Xlabel=($WeekDay[$wday]);
  204:     my $cumulative_answers = 0;
  205:     #
  206:     foreach my $row (@$SubData) {
  207:         my $subtime = &get_time_from_row($row);
  208:         while ($subtime > $endtime && $endtime < time) {
  209:             # Create a new bin
  210:             $bincount++;
  211:             $Ydata[$bincount]=0;
  212:             $AnsData[$bincount]=$AnsData[$bincount-1];
  213:             $endtime += $binsize;
  214:             if ($bincount % (86400/$binsize) == 0) {
  215:                 $wday++;
  216:                 $wday %= 7;
  217:                 $Xlabel[$bincount] = $WeekDay[$wday];
  218:             } else {
  219:                 $Xlabel[$bincount] = '';
  220:             }
  221:         }
  222:         $Ydata[$bincount]++;
  223:         $max = $Ydata[$bincount] if ($max < $Ydata[$bincount]);
  224:         $AnsData[$bincount] += &successful_submission($row);
  225:         $cumulative_answers += &successful_submission($row);
  226:     }
  227:     foreach my $maximum (10,15,20,25,30,40,50,60,70,80,90,100,
  228:                           120,150,200,250,300,350,400,450,500,
  229:                           600,700,800,900,1000,1100,1200,1500,2000,
  230:                           2500,3000,4000,5000) {
  231:         if ($max < $maximum) {
  232:             $max = $maximum;
  233:             last;
  234:         }
  235:     }
  236:     while ($bincount % $bins_per_day != 0) {
  237:         $bincount++;
  238:         $Ydata[$bincount]=0;
  239:         $AnsData[$bincount]=$AnsData[$bincount-1];
  240:         $endtime += $binsize;
  241:         if ($bincount % (86400/$binsize) == 0) {
  242:             $wday ++;
  243:             $wday %= 7;
  244:             $Xlabel[$bincount] = $WeekDay[$wday];
  245:         } else {
  246:             $Xlabel[$bincount] = '';
  247:         }
  248:     }
  249:     my $numstudents = scalar(@$students);
  250:     for (my $i=0;$i<=$#AnsData;$i++) {
  251:         $AnsData[$i] = int(100*($AnsData[$i]/$numstudents));
  252:     }
  253:     my $title = 'Number of Submissions and Number Correct';
  254:     my $xlabel;
  255:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($day_start);
  256:     $xlabel .= $FullWeekDay[$wday].' '.
  257:         join(' ',($Month[$month],$mday,1900+$year)).' - ';
  258:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($endtime);
  259:     $xlabel .= $FullWeekDay[$wday].' '.
  260:         join(' ',($Month[$month],$mday,1900+$year));
  261:     my $width = 50+2*$bincount;
  262:     if ($width < 250) {
  263:         $width = 250;
  264:     }
  265:     $html .= &Apache::loncommon::DrawXYYGraph($title,
  266:                                               $xlabel,
  267:                                               'Submissions vs Time',
  268:                                               $plotcolors,
  269:                                               \@Xlabel,
  270:                                               \@Ydata,
  271:                                               0,$max,
  272:                                               \@AnsData,
  273:                                               0,100,
  274:                                               (xskip => $bins_per_day,
  275:                                                x_ticks => $bins_per_day,
  276:                                                x_tick_offset => $bins_per_day,
  277:                                                width => $width,
  278:                       y1_label=>'Number of Submissions per hour',
  279:                       y2_label=>'Percent of Students answering Correctly',
  280:                      'data.1.label'=>'Submissions per hour',
  281:                      'data.2.label'=>'Percent correct',
  282:                                                )
  283:                                               );
  284:     $html .= '<br />';
  285:     return $html;
  286: }
  287: 
  288: sub successful_submission {
  289:     my ($row) = @_;
  290:     if (ref($row) eq 'ARRAY') {
  291:         return $row->[&Apache::loncoursedata::RT_awarded()];
  292:     }
  293:     return undef;
  294:     return 0;
  295: }
  296: 
  297: sub get_time_from_row {
  298:     my ($row) = @_;
  299:     if (ref($row) eq 'ARRAY') {
  300:         return $row->[&Apache::loncoursedata::RT_timestamp()];
  301:     } 
  302:     return undef;
  303: }
  304: 
  305: sub get_tries_from_row {
  306:     my ($row) = @_;
  307:     if (ref($row) eq 'ARRAY') {
  308:         return $row->[&Apache::loncoursedata::RT_tries()];
  309:     }
  310:     return undef;
  311: }
  312: 
  313: sub Process_Row {
  314:     my ($row) = @_;
  315:     my %RowData;
  316:     my ($student_id,$award,$tries,$time) = @$row;
  317:     return %RowData;
  318: }
  319: 
  320: #########################################################
  321: #########################################################
  322: ##
  323: ##   Generic Interface Routines
  324: ##
  325: #########################################################
  326: #########################################################
  327: sub CreateInterface {
  328:     ##
  329:     ## Environment variable initialization
  330:     if (! exists$ENV{'form.AnalyzeOver'}) {
  331:         $ENV{'form.AnalyzeOver'} = 'Tries';
  332:     }
  333:     ##
  334:     ## Build the menu
  335:     my $Str = '';
  336:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
  337:         (undef,&mt('Submission Time Plots'));
  338:     $Str .= '<table cellspacing="5">'."\n";
  339:     $Str .= '<tr>';
  340:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  341:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
  342:     $Str .= '<td align="center">&nbsp;</td>';
  343:     $Str .= '</tr>'."\n";
  344:     ##
  345:     ## 
  346:     $Str .= '<tr><td align="center">'."\n";
  347:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  348:     $Str .= '</td>';
  349:     #
  350:     $Str .= '<td align="center">';
  351:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  352:     $Str .= '</td>';
  353:     #
  354:     my $only_seq_with_assessments = sub { 
  355:         my $s=shift;
  356:         if ($s->{'num_assess'} < 1) { 
  357:             return 0;
  358:         } else { 
  359:             return 1;
  360:         }
  361:     };
  362:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  363:                                               $only_seq_with_assessments);
  364:     ##
  365:     ##
  366:     $Str .= '</tr>'."\n";
  367:     $Str .= '</table>'."\n";
  368:     return $Str;
  369: }
  370: 
  371: 1;
  372: 
  373: __END__

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