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

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

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