File:  [LON-CAPA] / loncom / interface / statistics / lonsubmissiontimeanalysis.pm
Revision 1.10: download - view: text, annotated - select for diffs
Mon Jan 19 21:31:08 2004 UTC (20 years, 5 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added entry for lonstathelpers.pm in loncapafiles.
Modified lonsubmissiontimeanalysis and lonproblemanalysis to use
lonstathelpers.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonsubmissiontimeanalysis.pm,v 1.10 2004/01/19 21:31:08 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 => 'ProblemAnalyis',
   49:                        text => 'Analyze Problem Again' },
   50:                      { name => 'SelectAnother',
   51:                        text => 'Choose a different resource' },
   52:                      );
   53: 
   54: sub BuildSubmissionTimePage {
   55:     my ($r,$c)=@_;
   56:     #
   57:     my %Saveable_Parameters = ('Status' => 'scalar',
   58:                                'Section' => 'array');
   59:     &Apache::loncommon::store_course_settings('submissiontime_analysis',
   60:                                               \%Saveable_Parameters);
   61:     &Apache::loncommon::restore_course_settings('submissiontime_analysis',
   62:                                                 \%Saveable_Parameters);
   63:     #
   64:     &Apache::lonstatistics::PrepareClasslist();    
   65:     #
   66:     $r->print('<h2>'.&mt('Submission Time Plots').'</h2>');
   67:     $r->print(&CreateInterface());
   68:     #
   69:     my @Students = @Apache::lonstatistics::Students;
   70:     #
   71:     if (@Students < 1) {
   72:         $r->print('<h2>There are no students in the sections selected</h2>');
   73:     }
   74:     #
   75:     &Apache::loncoursedata::clear_internal_caches();
   76:     if (exists($ENV{'form.ClearCache'}) || 
   77:         exists($ENV{'form.updatecaches'}) ||
   78:         (exists($ENV{'form.firstanalysis'}) &&
   79:          $ENV{'form.firstanalysis'} ne 'no')) {
   80:         &Apache::lonstatistics::Gather_Full_Student_Data($r);
   81:     }
   82:     if (! exists($ENV{'form.firstanalysis'})) {
   83:         $r->print('<input type="hidden" name="firstanalysis" value="yes" />');
   84:     } else {
   85:         $r->print('<input type="hidden" name="firstanalysis" value="no" />');
   86:     }
   87:     $r->rflush();
   88:     #
   89:     if (! exists($ENV{'form.problemchoice'}) ||
   90:         exists($ENV{'form.SelectAnother'})) {
   91:         $r->print('<input type="submit" name="" value="'.
   92:                   &mt('Graph Problem Submission Times').'" />');
   93:         $r->print('&nbsp;'x5);
   94:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
   95:         $r->print(&ProblemSelector());
   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:         }
  102:         $r->print('<input type="hidden" name="problemchoice" value="'.
  103:                   $ENV{'form.problemchoice'}.'" />');
  104:         #
  105:         $r->print('<hr />');
  106:         #
  107:         my ($symb,$part) = &get_problem_symb(
  108:                   &Apache::lonnet::unescape($ENV{'form.problemchoice'}));
  109:         $r->rflush();
  110:         #
  111:         my $resource = &get_resource_from_symb($symb);
  112:         if (! defined($resource)) {
  113:             $r->print('resource is undefined');
  114:         } else {
  115:             $r->print('<h1>'.$resource->{'title'}.'</h1>');
  116:             $r->print('<h3>'.$resource->{'src'}.'</h3>');
  117:             $r->rflush();
  118:             $r->print(&Apache::lonstathelpers::render_resource($resource));
  119:             $r->rflush();
  120:             $r->print(&analyze_times($r,$resource,\@Students,$part));
  121:         }
  122:         $r->print('<hr />');
  123:     }
  124: }
  125: 
  126: #########################################################
  127: #########################################################
  128: ##
  129: ##                  Time Analysis
  130: ##
  131: #########################################################
  132: #########################################################
  133: sub get_week_start {
  134:     my ($randomtime) = @_;
  135:     my ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = 
  136:         localtime($randomtime);
  137:     $randomtime -= $wday * 86400;
  138:     ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = 
  139:         localtime($randomtime);
  140:     my $week_start = &Time::Local::timelocal(0,0,0,$mday,$month,$year);
  141:     return $week_start;
  142: }
  143: 
  144: sub analyze_times {
  145:     my ($r,$resource,$students,$part) = @_;
  146:     #
  147:     # Convenience arrays
  148:     my @FullWeekDay = (qw/Sunday Monday Tuesday Wednesday Thursday Friday
  149:                        Saturday/);
  150:     my @WeekDay = (qw/SUN MON TUE WED THU FRI SAT SUN/);
  151:     my @Month = (qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/);
  152:     #
  153:     my $html; # holds results of analysis
  154:     # Get the data
  155:     my $SubData = &Apache::loncoursedata::get_response_time_data
  156:         ($students,$resource->{'symb'},$part);
  157:     if (! defined($SubData) || ! ref($SubData)) {
  158:         $html.= '<h2>There is no submission data for this problem</h2>';
  159:         return $html;
  160:     }
  161:     my $NumSub = scalar(@{$SubData});
  162:     if (! @{$SubData}) {
  163:         $html.= '<h2>There is no submission data for this problem</h2>';
  164:         return $html;
  165:     }
  166:     # Process the data
  167:     #
  168:     my (undef,undef,undef,$mday,$month,$year,$wday,$yday,$isdst) = 
  169:         localtime(&get_time_from_row($SubData->[0]));
  170:     my $day_start = &Time::Local::timelocal(0,0,0,$mday,$month,$year);
  171:     #
  172:     # Configure the bins used to store the data.
  173:     my $binsize = 3600; # seconds
  174:     my $bins_per_day = 86400/$binsize;
  175:     my $bincount = 0;
  176:     my $endtime = $day_start;
  177:     #
  178:     # Initialize loop variables
  179:     my $max;
  180:     my @Ydata=(0);
  181:     my @AnsData=(0);
  182:     my @Xlabel=($WeekDay[$wday]);
  183:     my $cumulative_answers = 0;
  184:     #
  185:     foreach my $row (@$SubData) {
  186:         my $subtime = &get_time_from_row($row);
  187:         while ($subtime > $endtime && $endtime < time) {
  188:             # Create a new bin
  189:             $bincount++;
  190:             $Ydata[$bincount]=0;
  191:             $AnsData[$bincount]=$AnsData[$bincount-1];
  192:             $endtime += $binsize;
  193:             if ($bincount % (86400/$binsize) == 0) {
  194:                 $wday++;
  195:                 $wday %= 7;
  196:                 $Xlabel[$bincount] = $WeekDay[$wday];
  197:             } else {
  198:                 $Xlabel[$bincount] = '';
  199:             }
  200:         }
  201:         $Ydata[$bincount]++;
  202:         $max = $Ydata[$bincount] if ($max < $Ydata[$bincount]);
  203:         $AnsData[$bincount] += &successful_submission($row);
  204:         $cumulative_answers += &successful_submission($row);
  205:     }
  206:     foreach my $maximum (10,15,20,25,30,40,50,60,70,80,90,100,
  207:                           120,150,200,250,300,350,400,450,500,
  208:                           600,700,800,900,1000,1100,1200,1500,2000,
  209:                           2500,3000,4000,5000) {
  210:         if ($max < $maximum) {
  211:             $max = $maximum;
  212:             last;
  213:         }
  214:     }
  215:     while ($bincount % $bins_per_day != 0) {
  216:         $bincount++;
  217:         $Ydata[$bincount]=0;
  218:         $AnsData[$bincount]=$AnsData[$bincount-1];
  219:         $endtime += $binsize;
  220:         if ($bincount % (86400/$binsize) == 0) {
  221:             $wday ++;
  222:             $wday %= 7;
  223:             $Xlabel[$bincount] = $WeekDay[$wday];
  224:         } else {
  225:             $Xlabel[$bincount] = '';
  226:         }
  227:     }
  228:     my $numstudents = scalar(@$students);
  229:     for (my $i=0;$i<=$#AnsData;$i++) {
  230:         $AnsData[$i] = int(100*($AnsData[$i]/$numstudents));
  231:     }
  232:     my $title = 'Number of Submissions and Number Correct';
  233:     my $xlabel;
  234:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($day_start);
  235:     $xlabel .= $FullWeekDay[$wday].' '.
  236:         join(' ',($Month[$month],$mday,1900+$year)).' - ';
  237:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($endtime);
  238:     $xlabel .= $FullWeekDay[$wday].' '.
  239:         join(' ',($Month[$month],$mday,1900+$year));
  240:     my $width = 50+2*$bincount;
  241:     if ($width < 250) {
  242:         $width = 250;
  243:     }
  244:     $html .= &Apache::loncommon::DrawXYYGraph($title,
  245:                                               $xlabel,
  246:                                               'Submissions vs Time',
  247:                                               $plotcolors,
  248:                                               \@Xlabel,
  249:                                               \@Ydata,
  250:                                               0,$max,
  251:                                               \@AnsData,
  252:                                               0,100,
  253:                                               (xskip => $bins_per_day,
  254:                                                x_ticks => $bins_per_day,
  255:                                                x_tick_offset => $bins_per_day,
  256:                                                width => $width,
  257:                       y1_label=>'Number of Submissions per hour',
  258:                       y2_label=>'Percent of Students answering Correctly',
  259:                      'data.1.label'=>'Submissions per hour',
  260:                      'data.2.label'=>'Percent correct',
  261:                                                )
  262:                                               );
  263:     $html .= '<br />';
  264:     return $html;
  265: }
  266: 
  267: sub successful_submission {
  268:     my ($row) = @_;
  269:     if (ref($row) eq 'ARRAY') {
  270:         return $row->[&Apache::loncoursedata::RT_awarded()];
  271:     }
  272:     return undef;
  273:     return 0;
  274: }
  275: 
  276: sub get_time_from_row {
  277:     my ($row) = @_;
  278:     if (ref($row) eq 'ARRAY') {
  279:         return $row->[&Apache::loncoursedata::RT_timestamp()];
  280:     } 
  281:     return undef;
  282: }
  283: 
  284: sub get_tries_from_row {
  285:     my ($row) = @_;
  286:     if (ref($row) eq 'ARRAY') {
  287:         return $row->[&Apache::loncoursedata::RT_tries()];
  288:     }
  289:     return undef;
  290: }
  291: 
  292: sub Process_Row {
  293:     my ($row) = @_;
  294:     my %RowData;
  295:     my ($student_id,$award,$tries,$time) = @$row;
  296:     return %RowData;
  297: }
  298: 
  299: #########################################################
  300: #########################################################
  301: ##
  302: ##   Generic Interface Routines
  303: ##
  304: #########################################################
  305: #########################################################
  306: sub CreateInterface {
  307:     ##
  308:     ## Environment variable initialization
  309:     if (! exists$ENV{'form.AnalyzeOver'}) {
  310:         $ENV{'form.AnalyzeOver'} = 'Tries';
  311:     }
  312:     ##
  313:     ## Build the menu
  314:     my $Str = '';
  315:     $Str .= '<table cellspacing="5">'."\n";
  316:     $Str .= '<tr>';
  317:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  318:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
  319:     $Str .= '<td align="center">&nbsp;</td>';
  320:     $Str .= '</tr>'."\n";
  321:     ##
  322:     ## 
  323:     $Str .= '<tr><td align="center">'."\n";
  324:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  325:     $Str .= '</td>';
  326:     #
  327:     $Str .= '<td align="center">';
  328:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  329:     $Str .= '</td>';
  330:     #
  331:     my $only_seq_with_assessments = sub { 
  332:         my $s=shift;
  333:         if ($s->{'num_assess'} < 1) { 
  334:             return 0;
  335:         } else { 
  336:             return 1;
  337:         }
  338:     };
  339:     &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  340:                                               $only_seq_with_assessments);
  341:     ##
  342:     ##
  343:     $Str .= '</tr>'."\n";
  344:     $Str .= '</table>'."\n";
  345:     return $Str;
  346: }
  347: 
  348: sub ProblemSelector {
  349:     my $Str;
  350:     $Str = "<table>\n";
  351:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  352:         next if ($seq->{'num_assess'}<1);
  353:         my $seq_str = '';
  354:         foreach my $res (@{$seq->{'contents'}}) {
  355:             next if ($res->{'type'} ne 'assessment');
  356:             if (@{$res->{'parts'}} == 1) {
  357:                 my $value = &Apache::lonnet::escape($res->{'symb'}.':'.
  358:                                                     $res->{'parts'}->[0]);
  359:                 my $checked;
  360:                 if ($ENV{'form.problemchoice'} eq $value) {
  361:                     $checked = 'checked ';
  362:                 }
  363:                 my $title = $res->{'title'};
  364:                 if (! defined($title) || $title eq '') {
  365:                     ($title) = ($res->{'src'} =~ m:/([^/]*)$:);
  366:                 }
  367: #                &Apache::lonnet::logthis('title = :'.$title.':');
  368:                 $seq_str .= '<tr><td>'.
  369:   '<input type="radio" name="problemchoice" value="'.$value.'" '.$checked.'/>'.
  370:   '</td><td>'.
  371:   '<a href="'.$res->{'src'}.'">'.$title.'</a> ';
  372:             } else {
  373:                 my $title = $res->{'title'};
  374:                 if (! defined($title) || $title eq '') {
  375:                     ($title) = ($res->{'src'} =~ m:/([^/]*)$:);
  376:                 }
  377:                 $seq_str .= '<tr><td>'.
  378:                     '&nbsp;'.'</td><td>'.
  379:                     '<a href="'.$res->{'src'}.'">'.$title.'</a>'.
  380:                     "</td></tr>\n";
  381:                 foreach my $part (@{$res->{'parts'}}) {
  382:                     my $value = &Apache::lonnet::escape
  383:                                                ($res->{'symb'}.':'.$part);
  384:                     my $checked;
  385:                     if ($ENV{'form.problemchoice'} eq $value) {
  386:                         $checked = 'checked ';
  387:                     }
  388:                     $seq_str .= '<tr><td>'.
  389:   '<input type="radio" name="problemchoice" value="'.$value.'" '.$checked.'/>'.
  390:   '</td><td>'.('&nbsp;'x5).'part '.$part."</td></tr>\n";
  391:                 }
  392:             }
  393:         }
  394:         if ($seq_str ne '') {
  395:             $Str .= '<tr><td>&nbsp</td><td><b>'.$seq->{'title'}.'</b></td>'.
  396:                 "</tr>\n".$seq_str;
  397:         }
  398:     }
  399:     $Str .= "</table>\n";
  400:     return $Str;
  401: }
  402: 
  403: #########################################################
  404: #########################################################
  405: ##
  406: ##      Misc functions (ought to be put in a module)
  407: ##
  408: #########################################################
  409: #########################################################
  410: sub get_problem_symb {
  411:     my $problemstring = shift();
  412:     my ($symb,$partid) = ($problemstring=~ /^(.*):([^:]*)$/);
  413:     return ($symb,$partid);
  414: }
  415: 
  416: sub get_resource_from_symb {
  417:     my ($symb) = @_;
  418:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  419:         foreach my $res (@{$seq->{'contents'}}) {
  420:             if ($res->{'symb'} eq $symb) {
  421:                 return $res;
  422:             }
  423:         }
  424:     }
  425:     return undef;
  426: }
  427: 
  428: 1;
  429: 
  430: __END__

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