--- loncom/interface/statistics/lonsubmissiontimeanalysis.pm	2003/10/30 16:20:18	1.3
+++ loncom/interface/statistics/lonsubmissiontimeanalysis.pm	2012/12/17 02:20:13	1.36
@@ -1,6 +1,6 @@
 # The LearningOnline Network with CAPA
 #
-# $Id: lonsubmissiontimeanalysis.pm,v 1.3 2003/10/30 16:20:18 matthew Exp $
+# $Id: lonsubmissiontimeanalysis.pm,v 1.36 2012/12/17 02:20:13 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -28,15 +28,16 @@
 package Apache::lonsubmissiontimeanalysis;
 
 use strict;
-use Apache::lonnet();
+use Apache::lonnet;
 use Apache::loncommon();
 use Apache::lonhtmlcommon();
+use Apache::lonquickgrades();
 use Apache::loncoursedata();
 use Apache::lonstatistics;
+use Apache::lonstathelpers;
 use Apache::lonlocal;
 use HTML::Entities();
 use Time::Local();
-use Spreadsheet::WriteExcel();
 
 my $plotcolors = ['#33ff00', 
                   '#ff33cc', '#990000', '#aaaa66', '#663399', '#ff9933',
@@ -44,86 +45,110 @@ my $plotcolors = ['#33ff00',
                   ]; 
 
 my @SubmitButtons = (
-                     { name => 'ProblemAnalyis',
+                     { name => 'PrevProblemAnalysis',
+                       text => 'Previous Problem' },
+                     { name => 'ProblemAnalysis',
                        text => 'Analyze Problem Again' },
+                     { name => 'NextProblemAnalysis',
+                       text => 'Next Problem' },
                      { name => 'SelectAnother',
-                       text => 'Choose a different resource' },
+                       text => 'Choose a different Problem' },
                      );
 
-sub render_resource {
-    my ($resource) = @_;
-    ##
-    ## Render the problem
-    my $base;
-    ($base,undef) = ($resource->{'src'} =~ m|(.*/)[^/]*$|);
-    $base = "http://".$ENV{'SERVER_NAME'}.$base;
-    my $rendered_problem = 
-        &Apache::lonnet::ssi_body($resource->{'src'});
-    $rendered_problem =~ s/<\s*form\s*/<nop /g;
-    $rendered_problem =~ s|(<\s*/form\s*>)|<\/nop>|g;
-    return '<table bgcolor="ffffff"><tr><td>'.
-        '<base href="'.$base.'" />'.
-        $rendered_problem.
-        '</td></tr></table>';
-}
-
 sub BuildSubmissionTimePage {
     my ($r,$c)=@_;
-    $r->print('<h2>'.&mt('Submission Time Plots').'</h2>');
+    #
+    my %Saveable_Parameters = ('Status' => 'scalar',
+                               'Section' => 'array');
+    &Apache::loncommon::store_course_settings('submissiontime_analysis',
+                                              \%Saveable_Parameters);
+    &Apache::loncommon::restore_course_settings('submissiontime_analysis',
+                                                \%Saveable_Parameters);
+    #
+    &Apache::lonstatistics::PrepareClasslist();    
+    #
+    $r->print(&Apache::lonhtmlcommon::breadcrumbs('Submission Time Plots'));
+    &Apache::lonquickgrades::startGradeScreen($r,'statistics');
     $r->print(&CreateInterface());
     #
     my @Students = @Apache::lonstatistics::Students;
     #
     if (@Students < 1) {
-        $r->print('<h2>There are no students in the sections selected</h2>');
+        $r->print('<div class="LC_warning">'
+                 .&mt('There are no students in the sections selected.')
+                 .'</div>'
+        );
     }
     #
-    &Apache::loncoursedata::clear_internal_caches();
-    if (exists($ENV{'form.ClearCache'}) || 
-        exists($ENV{'form.updatecaches'}) ||
-        (exists($ENV{'form.firstanalysis'}) &&
-         $ENV{'form.firstanalysis'} ne 'no')) {
-        &Apache::lonstatistics::Gather_Full_Student_Data($r);
-    }
-    if (! exists($ENV{'form.firstanalysis'})) {
-        $r->print('<input type="hidden" name="firstanalysis" value="yes" />');
-    } else {
-        $r->print('<input type="hidden" name="firstanalysis" value="no" />');
-    }
+    my @CacheButtonHTML = 
+        &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
     $r->rflush();
     #
-    if (! exists($ENV{'form.problemchoice'}) ||
-        exists($ENV{'form.SelectAnother'})) {
-        $r->print('<input type="submit" name="" value="'.
-                  &mt('Graph Problem Submission Times').'" />');
-        $r->print('&nbsp;'x5);
+    if (! exists($env{'form.problemchoice'}) ||
+        exists($env{'form.SelectAnother'})) {
+        my $submit_button = '<input type="submit" name="" value="'.
+#           &mt('Graph Problem Submission Times').'" />';
+            &mt('Generate Graph').'" />';
+        $r->print($submit_button.'&nbsp;'x5);
         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
-        $r->print(&ProblemSelector());
+        $r->print(&Apache::lonstathelpers::problem_selector('.',
+                                                            $submit_button));
     } else {
         foreach my $button (@SubmitButtons) {
             $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
                       'value="'.&mt($button->{'text'}).'" />');
             $r->print('&nbsp;'x5);
         }
+        foreach my $html (@CacheButtonHTML) {
+            $r->print($html.('&nbsp;'x5));
+        }
+        $r->rflush();
+        #
+        # Determine which problem we are to analyze
+        my $current_problem = &Apache::lonstathelpers::get_target_from_id
+            ($env{'form.problemchoice'});
+        #
+        my ($navmap,$prev,$curr,$next) = 
+            &Apache::lonstathelpers::get_prev_curr_next($current_problem,
+                                                        '.',
+                                                        'part');
+        if (exists($env{'form.PrevProblemAnalysis'}) && defined($prev)) {
+            $current_problem = $prev;
+        } elsif (exists($env{'form.NextProblemAnalysis'}) && defined($next)) {
+            $current_problem = $next;
+        } else {
+            $current_problem = $curr;
+        }
+        #
+        # Store the current problem choice and send it out in the form
+        $env{'form.problemchoice'} = 
+            &Apache::lonstathelpers::make_target_id($current_problem);
         $r->print('<input type="hidden" name="problemchoice" value="'.
-                  $ENV{'form.problemchoice'}.'" />');
+                  $env{'form.problemchoice'}.'" />');
         #
         $r->print('<hr />');
-        #
-        my ($symb,$part) = &get_problem_symb(
-                  &Apache::lonnet::unescape($ENV{'form.problemchoice'}));
         $r->rflush();
         #
-        my $resource = &get_resource_from_symb($symb);
+        my $resource = $current_problem->{'resource'};
         if (! defined($resource)) {
-            $r->print('resource is undefined');
+            $r->print('<div class="LC_warning">'
+                     .&mt('Resource is undefined.')
+                     .'</div>'
+            );
         } else {
-            $r->print('<h1>'.$resource->{'title'}.'</h1>');
-            $r->print('<h3>'.$resource->{'src'}.'</h3>');
+            $r->print('<h1>'.$resource->compTitle.'</h1>');
+            $r->print('<h3>'.$resource->src.'</h3>');
+            $r->print('<p>'.
+                 &Apache::lonstatistics::section_and_enrollment_description().
+                      '</p>');
             $r->rflush();
-            $r->print(&render_resource($resource));
+            $r->print(&Apache::lonstathelpers::render_resource($resource));
+            $r->print('<br />');
             $r->rflush();
-            $r->print(&analyze_times($r,$resource,\@Students,$part));
+	    if (@Students) {	    
+		$r->print(&analyze_times($r,$resource->symb,\@Students,
+					 $current_problem->{'part'}));
+	    }
         }
         $r->print('<hr />');
     }
@@ -148,7 +173,8 @@ sub get_week_start {
 }
 
 sub analyze_times {
-    my ($r,$resource,$students,$part) = @_;
+    my ($r,$symb,$students,$part) = @_;
+    my $htmltable;
     #
     # Convenience arrays
     my @FullWeekDay = (qw/Sunday Monday Tuesday Wednesday Thursday Friday
@@ -159,14 +185,21 @@ sub analyze_times {
     my $html; # holds results of analysis
     # Get the data
     my $SubData = &Apache::loncoursedata::get_response_time_data
-        ($students,$resource->{'symb'},$part);
+        ([&Apache::lonstatistics::get_selected_sections()],
+         [&Apache::lonstatistics::get_selected_groups()],
+         $Apache::lonstatistics::enrollment_status,
+         $symb,$part);
     if (! defined($SubData) || ! ref($SubData)) {
-        $html.= '<h2>There is no submission data for this resource</h2>';
+        $html.= '<div class="LC_warning">'
+               .&mt('There is no submission data for this problem at all.')
+               .'</div>';
         return $html;
     }
     my $NumSub = scalar(@{$SubData});
     if (! @{$SubData}) {
-        $html.= '<h2>There is no submission data for this resource</h2>';
+        $html.= '<div class="LC_warning">'
+               .&mt('There is no submission data for this problem.')
+               .'</div>';
         return $html;
     }
     # Process the data
@@ -182,20 +215,23 @@ sub analyze_times {
     my $endtime = $day_start;
     #
     # Initialize loop variables
-    my $max;
-    my @Ydata=(0);
-    my @AnsData=(0);
-    my @Xlabel=($WeekDay[$wday]);
-    my $cumulative_answers = 0;
+    my $max;            # The sum of @Ydata
+    my @Ydata=(0);      # number of submissions
+    my @AnsData=(0);    # number of correct submissions
+    my @Xlabel=($WeekDay[$wday]); # Labels of itmes
+    my @BinEnd;                   # The end time of each bin
+    my $cumulative_answers = 0;   # The sum of @AnsData
+    my %students;       # which students have attempted the problem?
     #
     foreach my $row (@$SubData) {
         my $subtime = &get_time_from_row($row);
         while ($subtime > $endtime && $endtime < time) {
             # Create a new bin
             $bincount++;
-            $Ydata[$bincount]=0;
-            $AnsData[$bincount]=$AnsData[$bincount-1];
+            $Ydata[$bincount]   = 0;
+            $AnsData[$bincount] = 0;
             $endtime += $binsize;
+            push(@BinEnd,$endtime);
             if ($bincount % (86400/$binsize) == 0) {
                 $wday++;
                 $wday %= 7;
@@ -208,21 +244,16 @@ sub analyze_times {
         $max = $Ydata[$bincount] if ($max < $Ydata[$bincount]);
         $AnsData[$bincount] += &successful_submission($row);
         $cumulative_answers += &successful_submission($row);
+        $students{$row->[&Apache::loncoursedata::RT_student_id()]}++;
     }
-    foreach my $maximum (10,15,20,25,30,40,50,60,70,80,90,100,
-                          120,150,200,250,300,350,400,450,500,
-                          600,700,800,900,1000,1100,1200,1500,2000,
-                          2500,3000,4000,5000) {
-        if ($max < $maximum) {
-            $max = $maximum;
-            last;
-        }
-    }
+    #
+    # Pad the data to a full day
     while ($bincount % $bins_per_day != 0) {
         $bincount++;
         $Ydata[$bincount]=0;
-        $AnsData[$bincount]=$AnsData[$bincount-1];
+        $AnsData[$bincount]=0;
         $endtime += $binsize;
+        push(@BinEnd,$endtime);
         if ($bincount % (86400/$binsize) == 0) {
             $wday ++;
             $wday %= 7;
@@ -231,8 +262,65 @@ sub analyze_times {
             $Xlabel[$bincount] = '';
         }
     }
-
-    my $title = 'Number of Submissions and Number Correct';
+    my $numstudents = scalar(keys(%students));
+    #
+    # Determine a nice maximum value to use
+    foreach my $maximum (10,15,20,25,30,40,50,60,70,80,90,100,
+                          120,150,200,250,300,350,400,450,500,
+                          600,700,800,900,1000,1100,1200,1500,2000,
+                          2500,3000,4000,5000) {
+        if ($max < $maximum) {
+            $max = $maximum;
+            last;
+        }
+    }
+    #
+    # Build the data table
+    $htmltable = '<br><h3>'.&mt('Student submission data').'</h3><p>'.
+        &Apache::loncommon::start_data_table().
+        &Apache::loncommon::start_data_table_header_row().
+        &Apache::loncommon::start_data_table_row().
+        '<th valign="bottom">'.&mt('Begin').'</th>'.
+        '<th valign="bottom">'.&mt('End').'</th>'.
+        '<th valign="bottom">'.&mt('Submissions (plotted)').'</th>'.
+        '<th valign="bottom">'.&mt('Correct Submissions (not plotted)').'</th>'.
+        '<th valign="bottom">'.&mt('Cumulative Correct of those attempting the problem (not plotted)').'</th>'.
+        '<th valign="bottom">'.&mt('Cumulative Percent Correct of those attempting the problem (not plotted)').'</th>'.
+        '<th valign="bottom">'.&mt('Cumulative Percent Correct of selected students (plotted)').'</th>'.
+        &Apache::loncommon::end_data_table_row().
+        &Apache::loncommon::end_data_table_header_row().
+        '<tbody>';
+    my @CumulativeCorrect=(0);
+    my @corr_as_percent_of_selected;
+    my @corr_as_percent_of_answering;
+    for (my $i=0;$i<=$#Ydata;$i++) {
+        $CumulativeCorrect[$i]=$CumulativeCorrect[-1]+$AnsData[$i];
+        $corr_as_percent_of_answering[$i] = 
+            sprintf('%3.1f',100*$CumulativeCorrect[$i]/$numstudents);
+        $corr_as_percent_of_selected[$i] = 
+            sprintf('%3.1f',100*$CumulativeCorrect[$i]/scalar(@$students));
+        if ($Ydata[$i] != 0) {
+            next if (! defined($BinEnd[$i]) || $BinEnd[$i] == 0);
+            $htmltable .=
+               &Apache::loncommon::start_data_table_row().
+                '<td align="right"><span class="LC_nobreak">'.
+                &Apache::lonlocal::locallocaltime($BinEnd[$i]-$binsize).
+                '</span></td>'.
+                '<td align="right"><span class="LC_nobreak">'.
+                    &Apache::lonlocal::locallocaltime($BinEnd[$i]).'</td>'.
+                '</span></td>'.
+                '<td align="right">'.$Ydata[$i].('&nbsp;'x3).'</td>'.
+                '<td align="right">'.$AnsData[$i].('&nbsp;'x3).'</td>'.
+                '<td align="right">'.$CumulativeCorrect[$i].'</td>'.
+                '<td align="right">'.$corr_as_percent_of_answering[$i].'</td>'.
+                '<td align="right">'.$corr_as_percent_of_selected[$i].'</td>'.
+               &Apache::loncommon::end_data_table_row().$/;
+        }
+    }
+    $htmltable .= '</tbody>'.&Apache::loncommon::end_data_table().'</p>';
+    #
+    # Build the plot
+    my $title = '';#'Number of Submissions and Number Correct';
     my $xlabel;
     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($day_start);
     $xlabel .= $FullWeekDay[$wday].' '.
@@ -240,21 +328,29 @@ sub analyze_times {
     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($endtime);
     $xlabel .= $FullWeekDay[$wday].' '.
         join(' ',($Month[$month],$mday,1900+$year));
+    my $width = 50+2*$bincount;
+    if ($width < 250) {
+        $width = 250;
+    }
+    #
     $html .= &Apache::loncommon::DrawXYYGraph($title,
                                               $xlabel,
-                                              'Number of Submissions per hour',
+                                              'Submissions vs Time',
                                               $plotcolors,
                                               \@Xlabel,
-                                              \@Ydata,
-                                              0,$max,
-                                              \@AnsData,
-                                              0,scalar(@$students),
+                                              \@Ydata,0,$max,
+                                              \@corr_as_percent_of_selected,0,100,
                                               (xskip => $bins_per_day,
                                                x_ticks => $bins_per_day,
                                                x_tick_offset => $bins_per_day,
-                                               width => 50+2*$bincount)
+                                               width => $width,
+                      y1_label=>'Number of Submissions per hour',
+                      y2_label=>'Percent of Students answering Correctly',
+                     'data.1.label'=>'Submissions per hour',
+                     'data.2.label'=>'Percent correct',
+                                               )
                                               );
-    $html .= '<br />';
+    $html .= '<br />'.$htmltable;
     return $html;
 }
 
@@ -264,7 +360,6 @@ sub successful_submission {
         return $row->[&Apache::loncoursedata::RT_awarded()];
     }
     return undef;
-    return 0;
 }
 
 sub get_time_from_row {
@@ -300,116 +395,42 @@ sub Process_Row {
 sub CreateInterface {
     ##
     ## Environment variable initialization
-    if (! exists$ENV{'form.AnalyzeOver'}) {
-        $ENV{'form.AnalyzeOver'} = 'Tries';
+    if (! exists$env{'form.AnalyzeOver'}) {
+        $env{'form.AnalyzeOver'} = 'Tries';
     }
     ##
     ## Build the menu
     my $Str = '';
-    $Str .= '<table cellspacing="5">'."\n";
-    $Str .= '<tr>';
-    $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
-    $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
-    $Str .= '<td align="center">&nbsp;</td>';
-    $Str .= '</tr>'."\n";
+    $Str .= '<p>';
+    $Str .= &Apache::loncommon::start_data_table();
+    $Str .= &Apache::loncommon::start_data_table_header_row();
+    $Str .= '<th>'.&mt('Sections').'</th>';
+    $Str .= '<th>'.&mt('Groups').'</th>';
+    $Str .= '<th>'.&mt('Access Status').'</th>';
+    $Str .= &Apache::loncommon::end_data_table_header_row();
     ##
     ## 
-    $Str .= '<tr><td align="center">'."\n";
-    $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
+    $Str .= &Apache::loncommon::start_data_table_row();
+    $Str .= '<td align="center">'."\n";
+    $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',4);
+    $Str .= '</td>';
+    #
+    $Str .= '<td align="center">'."\n";
+    $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',4);
     $Str .= '</td>';
     #
     $Str .= '<td align="center">';
-    $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
+    $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,4);
     $Str .= '</td>';
     #
-    my $only_seq_with_assessments = sub { 
-        my $s=shift;
-        if ($s->{'num_assess'} < 1) { 
-            return 0;
-        } else { 
-            return 1;
-        }
-    };
-    &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
-                                              $only_seq_with_assessments);
-    ##
+    $Str .= &Apache::loncommon::end_data_table_row();
+    $Str .= &Apache::loncommon::end_data_table();
+    #
+    $Str .= '</p>';
     ##
-    $Str .= '</tr>'."\n";
-    $Str .= '</table>'."\n";
     return $Str;
 }
 
-sub ProblemSelector {
-    my $Str;
-    $Str = "<table>\n";
-    foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
-        next if ($seq->{'num_assess'}<1);
-        my $seq_str = '';
-        foreach my $res (@{$seq->{'contents'}}) {
-            next if ($res->{'type'} ne 'assessment');
-            if (@{$res->{'parts'}} == 1) {
-                my $value = &Apache::lonnet::escape($res->{'symb'}.':'.
-                                                    $res->{'parts'}->[0]);
-                my $checked;
-                if ($ENV{'form.problemchoice'} eq $value) {
-                    $checked = 'checked ';
-                }
-                $seq_str .= '<tr><td>'.
-  '<input type="radio" name="problemchoice" value="'.$value.'" '.$checked.'/>'.
-  '</td><td>'.
-  '<a href="'.$res->{'src'}.'">'.$res->{'title'}.'</a> ';
-            } else {
-                $seq_str .= '<tr><td>'.
-                    '&nbsp;'.'</td><td>'.
-                    '<a href="'.$res->{'src'}.'">'.$res->{'title'}.'</a>'.
-                    "</td></tr>\n";
-                foreach my $part (@{$res->{'parts'}}) {
-                    my $value = &Apache::lonnet::escape
-                                               ($res->{'symb'}.':'.$part);
-                    my $checked;
-                    if ($ENV{'form.problemchoice'} eq $value) {
-                        $checked = 'checked ';
-                    }
-                    $seq_str .= '<tr><td>'.
-  '<input type="radio" name="problemchoice" value="'.$value.'" '.$checked.'/>'.
-  '</td><td>'.('&nbsp;'x5).'part '.$part."</td></tr>\n";
-                }
-            }
-        }
-        if ($seq_str ne '') {
-            $Str .= '<tr><td>&nbsp</td><td><b>'.$seq->{'title'}.'</b></td>'.
-                "</tr>\n".$seq_str;
-        }
-    }
-    $Str .= "</table>\n";
-    return $Str;
-}
-
-#########################################################
-#########################################################
-##
-##      Misc functions (ought to be put in a module)
-##
-#########################################################
-#########################################################
-sub get_problem_symb {
-    my $problemstring = shift();
-    my ($symb,$partid) = ($problemstring=~ /^(.*):([^:]*)$/);
-    return ($symb,$partid);
-}
-
-sub get_resource_from_symb {
-    my ($symb) = @_;
-    foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
-        foreach my $res (@{$seq->{'contents'}}) {
-            if ($res->{'symb'} eq $symb) {
-                return $res;
-            }
-        }
-    }
-    return undef;
-}
-
 1;
 
 __END__