File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.138: download - view: text, annotated - select for diffs
Wed Jun 6 17:30:40 2007 UTC (16 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: 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_99_0, HEAD
- sort number sections numerically

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstatistics.pm,v 1.138 2007/06/06 17:30:40 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: # (Navigate problems for statistical reports
   28: #
   29: ###
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: lonstatistics
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Main handler for statistics and chart.
   40: 
   41: =over 4
   42: 
   43: =cut
   44: 
   45: package Apache::lonstatistics;
   46: 
   47: use strict;
   48: use Apache::Constants qw(:common :http);
   49: use vars qw(
   50:     @FullClasslist 
   51:     @Students
   52:     @Sections
   53:     @Groups 
   54:     %StudentData
   55:     @StudentDataOrder
   56:     @SelectedStudentData
   57:     $enrollment_status);
   58: 
   59: use Apache::lonnet;
   60: use Apache::lonhomework;
   61: use Apache::loncommon;
   62: use Apache::loncoursedata;
   63: use Apache::lonhtmlcommon;
   64: use Apache::lonmysql;
   65: use Apache::lonlocal;
   66: use Apache::longroup;
   67: use Time::HiRes;
   68: #
   69: # Statistics Packages
   70: use Apache::lonproblemanalysis();
   71: use Apache::lonsubmissiontimeanalysis();
   72: use Apache::loncorrectproblemplot();
   73: use Apache::lonproblemstatistics();
   74: use Apache::lonstudentassessment();
   75: use Apache::lonpercentage;
   76: use Apache::lonstudentsubmissions();
   77: use Apache::lonsurveyreports();
   78: use Apache::longradinganalysis();
   79: use LONCAPA;
   80: 
   81: #######################################################
   82: #######################################################
   83: 
   84: =pod
   85: 
   86: =item Package Variables
   87: 
   88: =item @FullClasslist The full classlist
   89: 
   90: =item @Students The students we are concerned with for this invocation
   91: 
   92: =item @Sections The sections available in this class
   93: 
   94: =item @Groups The groups available in the class
   95: 
   96: =item $curr_student The student currently being examined
   97: 
   98: =item $prev_student The student previous in the classlist
   99: 
  100: =item $next_student The student next in the classlist
  101: 
  102: =over
  103: 
  104: =cut 
  105: 
  106: #######################################################
  107: #######################################################
  108: #
  109: # Classlist variables
  110: #
  111: my $curr_student;
  112: my $prev_student;
  113: my $next_student;
  114: 
  115: #######################################################
  116: #######################################################
  117: 
  118: =pod
  119: 
  120: =item &clear_classlist_variables()
  121: 
  122: undef the following package variables:
  123: 
  124: =over
  125: 
  126: =item @FullClasslist
  127: 
  128: =item @Students
  129: 
  130: =item @Sections
  131: 
  132: =item @Groups
  133: 
  134: =item %StudentData
  135: 
  136: =item @StudentDataOrder
  137: 
  138: =item @SelectedStudentData
  139: 
  140: =item $curr_student
  141: 
  142: =item $prev_student
  143: 
  144: =item $next_student
  145: 
  146: =back
  147: 
  148: =cut
  149: 
  150: #######################################################
  151: #######################################################
  152: sub clear_classlist_variables {
  153:     undef(@FullClasslist);
  154:     undef(@Students);
  155:     undef(@Sections);
  156:     undef(@Groups);
  157:     undef(%StudentData);
  158:     undef(@SelectedStudentData);
  159:     undef($curr_student);
  160:     undef($prev_student);
  161:     undef($next_student);
  162: }
  163: 
  164: #######################################################
  165: #######################################################
  166: 
  167: =pod
  168: 
  169: =item &PrepareClasslist()
  170: 
  171: Build up the classlist information.  The classlist information is kept in
  172: the following package variables:
  173: 
  174: =over
  175: 
  176: =item @FullClasslist
  177: 
  178: =item @Students
  179: 
  180: =item @Sections
  181: 
  182: =item @Groups 
  183: 
  184: =item %StudentData
  185: 
  186: =item @SelectedStudentData
  187: 
  188: =item $curr_student
  189: 
  190: =item $prev_student
  191: 
  192: =item $next_student
  193: 
  194: =back
  195: 
  196: $curr_student, $prev_student, and $next_student may not be defined, depending
  197: upon the calling context.
  198: 
  199: =cut
  200: 
  201: #######################################################
  202: #######################################################
  203: sub PrepareClasslist {
  204:     my %Sections;
  205:     &clear_classlist_variables();
  206:     #
  207:     # Retrieve the classlist
  208:     my $cid  = $env{'request.course.id'};
  209:     my $cdom = $env{'course.'.$cid.'.domain'};
  210:     my $cnum = $env{'course.'.$cid.'.num'};
  211:     my ($classlist,$field_names) = &Apache::loncoursedata::get_classlist($cdom,
  212: 									$cnum);
  213:     my @selected_sections = &get_selected_sections();
  214:     my @selected_groups = &get_selected_groups();
  215:     #
  216:     # Deal with instructors with restricted section access
  217:     if ($env{'request.course.sec'} !~ /^\s*$/) {
  218:         @selected_sections = ($env{'request.course.sec'});
  219:     }
  220:     #
  221:     # Set up %StudentData
  222:     @StudentDataOrder = qw/fullname username domain id section status groups comments/;
  223:     foreach my $field (@StudentDataOrder) {
  224:         $StudentData{$field}->{'title'} = &mt($field);
  225:         $StudentData{$field}->{'base_width'} = length(&mt($field));
  226:         $StudentData{$field}->{'width'} = 
  227:                                $StudentData{$field}->{'base_width'};
  228:     }
  229:     #
  230:     # get the status requested
  231:     $enrollment_status = 'Active';
  232:     $enrollment_status = $env{'form.Status'} if (exists($env{'form.Status'}));
  233:     #
  234:     # Get groupmembership
  235:     my ($classgroups,$studentgroups);
  236:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
  237:     if (%curr_groups) {
  238:         ($classgroups,$studentgroups) = 
  239: 	    &Apache::loncoursedata::get_group_memberships($classlist,
  240:                                                           $field_names,
  241: 							  $cdom,$cnum);
  242:     }
  243:     my $now = time;
  244: 
  245:     # Process the classlist
  246:     while (my ($student,$student_data) = each (%$classlist)) {
  247:         my $studenthash = ();
  248:         for (my $i=0; $i< scalar(@$field_names);$i++) {
  249:             my $field = $field_names->[$i];
  250:             # Store the data
  251:             $studenthash->{$field}=$student_data->[$i];
  252:             # Keep track of the width of the fields
  253:             next if (! exists($StudentData{$field}));
  254:             my $length = length($student_data->[$i]);
  255:             if ($StudentData{$field}->{'width'} < $length) {
  256:                 $StudentData{$field}->{'width'} = $length; 
  257:             }
  258:         }
  259:         my @studentsgroups = &Apache::loncoursedata::get_students_groups
  260:                                                    ($student,$enrollment_status,
  261:                                                     $classgroups);
  262:         if (@studentsgroups) {
  263:             $studenthash->{'groups'} = join(', ',@studentsgroups);
  264:             $studenthash->{'groupref'} = \@studentsgroups;
  265:         } else {
  266:             $studenthash->{'groups'} = 'none';
  267:             $studenthash->{'groupref'} = []; 
  268:         }
  269:         push (@FullClasslist,$studenthash);
  270:         #
  271:         # Build up a list of sections
  272:         my $section = $studenthash->{'section'};
  273:         if (! defined($section) || $section =~/^\s*$/ || $section == -1) {
  274:             $studenthash->{'section'} = 'none';
  275:             $section = $studenthash->{'section'};
  276:         }
  277:         $Sections{$section}++;
  278:         #
  279:         # Only put in the list those students we are interested in
  280:         foreach my $sect (@selected_sections) {
  281:             if ( (($sect eq 'all') || 
  282:                   ($section eq $sect)) &&
  283:                  (($studenthash->{'status'} eq $enrollment_status) || 
  284:                   ($enrollment_status eq 'Any')) 
  285:                  ){
  286:                 my $groupcheck = 0;
  287:                 if (grep(/^all$/,@selected_groups)) {
  288:                     push(@Students,$studenthash);
  289:                     last;
  290:                 } elsif (grep(/^none$/,@selected_groups)) {
  291:                     if ($studenthash->{'groups'} eq 'none') {
  292:                         push(@Students,$studenthash);
  293:                         last;
  294:                     }     
  295:                 } else {
  296:                     foreach my $group (@selected_groups) {
  297:                         if (grep(/^$group$/,@studentsgroups)) {
  298:                             push(@Students,$studenthash);
  299:                             $groupcheck = 1;
  300:                             last;
  301:                         }
  302:                     }
  303:                     if ($groupcheck) {
  304:                         last;
  305:                     }
  306:                 }
  307:             }
  308:         }
  309:     }
  310:     #
  311:     # Put the consolidated section data in the right place
  312:     if ($env{'request.course.sec'} !~ /^\s*$/) {
  313:         @Sections = ($env{'request.course.sec'});
  314:     } else {
  315:         @Sections = sort {
  316: 	    if ($a == $a && $b == $b ) { return $a <=> $b; }
  317: 	    return $a cmp $b;
  318: 	} keys(%Sections);
  319: 
  320:         unshift(@Sections,'all'); # Put 'all' at the front of the list
  321:     }
  322:     # Sort the groups
  323:     @Groups = sort {$a cmp $b} keys(%{$studentgroups});
  324:     unshift(@Groups,'all'); # Put 'all' at the front of the list
  325: 
  326:     #
  327:     # Sort the Students
  328:     my $sortby = 'fullname';
  329:     $sortby = $env{'form.sort'} if (exists($env{'form.sort'}));
  330:     my @TmpStudents = sort { lc($a->{$sortby}) cmp lc($b->{$sortby}) ||
  331:                              lc($a->{'fullname'}) cmp lc($b->{'fullname'}) ||
  332: 			     lc($a->{'username'}) cmp lc($b->{'username'}) } @Students;
  333:     @Students = @TmpStudents;
  334:     # 
  335:     # Now deal with that current student thing....
  336:     $curr_student = undef;
  337:     if (exists($env{'form.SelectedStudent'})) {
  338:         my ($current_uname,$current_dom) = 
  339:             split(':',$env{'form.SelectedStudent'});
  340:         my $i;
  341:         for ($i = 0; $i<=$#Students; $i++) {
  342:             next if (($Students[$i]->{'username'} ne $current_uname) || 
  343:                      ($Students[$i]->{'domain'}   ne $current_dom));
  344:             $curr_student = $Students[$i];
  345:             last; # If we get here, we have our student.
  346:         }
  347:         if (defined($curr_student)) {
  348:             if ($i == 0) {
  349:                 $prev_student = undef;
  350:             } else {
  351:                 $prev_student = $Students[$i-1];
  352:             }
  353:             if ($i == $#Students) {
  354:                 $next_student = undef;
  355:             } else {
  356:                 $next_student = $Students[$i+1];
  357:             }
  358:         }
  359:     }
  360:     #
  361:     if (exists($env{'form.StudentData'})) {
  362: 	@SelectedStudentData = 
  363: 	    &Apache::loncommon::get_env_multiple('form.StudentData');
  364:     } else {
  365:         @SelectedStudentData = ('username');
  366:     }
  367:     foreach (@SelectedStudentData) {
  368:         if ($_ eq 'all') {
  369:             @SelectedStudentData = ('all');
  370:             last;
  371:         }
  372:     }
  373:     #
  374:     return;
  375: }
  376: 
  377: #######################################################
  378: #######################################################
  379: 
  380: =pod
  381: 
  382: =item get_selected_sections
  383: 
  384: Returns an array of the selected sections
  385: 
  386: =cut
  387: 
  388: #######################################################
  389: #######################################################
  390: sub get_selected_sections {
  391:     my @selected_sections = 
  392: 	&Apache::loncommon::get_env_multiple('form.Section');
  393:     @selected_sections = ('all') if (! @selected_sections);
  394:     foreach (@selected_sections) {
  395:         if ($_ eq 'all') {
  396:             @selected_sections = ('all');
  397:         }
  398:     }
  399:     #
  400:     # Deal with instructors with restricted section access
  401:     if ($env{'request.course.sec'} !~ /^\s*$/) {
  402:         @selected_sections = ($env{'request.course.sec'});
  403:     }
  404:     return @selected_sections;
  405: }
  406: 
  407: #######################################################
  408: #######################################################
  409:                                                                                     
  410: =pod
  411:                                                                                     
  412: =item get_selected_groups
  413:                                                                                     
  414: Returns an array of the selected groups
  415:                                                                                     
  416: =cut
  417:                                                                                     
  418: #######################################################
  419: #######################################################
  420: sub get_selected_groups {
  421:     my @selected_groups =
  422:         &Apache::loncommon::get_env_multiple('form.Group');
  423:     @selected_groups = ('all') if (! @selected_groups);
  424:     foreach my $grp (@selected_groups) {
  425:         if ($grp eq 'all') {
  426:             @selected_groups = ('all');
  427:             last;
  428:         }
  429:     }
  430:     return @selected_groups;
  431: }
  432:                                                                                     
  433: =pod
  434: 
  435: =item &section_and_enrollment_description
  436: 
  437: Returns a string describing the currently selected section(s), group(s) and 
  438: access status.  
  439: 
  440: Inputs: mode = 'plaintext' or 'localized'  (defaults to 'localized')
  441:     'plaintext' is used for example in Excel spreadsheets.
  442: Returns: scalar description string.
  443: 
  444: =cut
  445: 
  446: #######################################################
  447: #######################################################
  448: sub section_and_enrollment_description {
  449:     my ($mode) = @_;
  450:     if (! defined($mode)) { $mode = 'localized'; }
  451:     my @sections = &Apache::lonstatistics::get_selected_sections();
  452:     my @groups = &Apache::lonstatistics::get_selected_groups();
  453:     my $description;
  454:     if ($mode eq 'localized') {
  455:         $description = &mt('Unable to determine section, groups and access status');
  456:     } elsif ($mode eq 'plaintext') {
  457:         $description = 'Unable to determine section, groups and access status';
  458:     } else {
  459:         $description = 'Bad parameter passed to lonstatistics::section_and_enrollment_description';
  460:         &Apache::lonnet::logthis($description);
  461:     }
  462:     $description = &section_or_group_text($mode,'section',@sections).
  463: 	' '.&section_or_group_text($mode,'group',@groups);
  464:     if ($mode eq 'localized') {
  465:         $description .= &mt(' [_1] access status.',$env{'form.Status'});
  466:     } elsif ($mode eq 'plaintext') {
  467:         $description .= ' '.$env{'form.Status'}.' access status.';
  468:     }
  469:     return $description;
  470: }
  471: 
  472: #######################################################
  473: #######################################################
  474: 
  475: sub section_or_group_text {
  476:     my ($mode,$type,@items) = @_;
  477:     my $text;
  478:     my %phrases = ();
  479:     %{$phrases{'section'}} = (
  480:                               single => 'Section',
  481:                               all => 'All sections',
  482:                               plural => 'Sections',
  483:                              );
  484:     %{$phrases{'group'}} = (
  485:                               single => 'Group',
  486:                               all => 'All groups',
  487:                               plural => 'Groups',
  488:                              );
  489:     if (scalar(@items) == 1 && $items[0] ne 'all') {
  490:         if ($mode eq 'localized') {
  491:             $text = &mt('[_1] [_2].',$phrases{$type}{single},$items[0]);
  492:         } elsif ($mode eq 'plaintext') {
  493:             $text = $phrases{$type}{single}.' '.$items[0].'.';
  494: 
  495:         }
  496:     } elsif (scalar(@items) && $items[0] eq 'all') {
  497:         if ($mode eq 'localized') {
  498:             $text = &mt('[_1].',$phrases{$type}{all});
  499:         } elsif ($mode eq 'plaintext') {
  500:             $text = $phrases{$type}{all}.'.';
  501:         }
  502:     } elsif (scalar(@items)) {
  503:         my $lastitem = pop(@items);
  504:         if ($mode eq 'localized') {
  505:             $text = &mt('[_1] [_2] and [_3].',$phrases{$type}{plural},
  506:                         join(', ',@items),$lastitem);
  507:         } elsif ($mode eq 'plaintext') {
  508:             $text = $phrases{$type}{plural}.' '.join(', ',@items).' and '.
  509:                     $lastitem.'.';
  510:         }
  511:     }
  512:     return $text;
  513: }
  514: 
  515: 
  516: =pod
  517: 
  518: =item get_students
  519: 
  520: Returns a list of the selected students
  521: 
  522: =cut
  523: 
  524: #######################################################
  525: #######################################################
  526: sub get_students {
  527:     if (! @Students) {
  528:         &PrepareClasslist()
  529:     }
  530:     return @Students;
  531: }
  532: 
  533: #######################################################
  534: #######################################################
  535: 
  536: =pod
  537: 
  538: =item &current_student()
  539: 
  540: Returns a pointer to a hash containing data about the currently
  541: selected student.
  542: 
  543: =cut
  544: 
  545: #######################################################
  546: #######################################################
  547: sub current_student { 
  548:     return $curr_student;
  549: }
  550: 
  551: #######################################################
  552: #######################################################
  553: 
  554: =pod
  555: 
  556: =item &previous_student()
  557: 
  558: Returns a pointer to a hash containing data about the student prior
  559: in the list of students.  Or something.  
  560: 
  561: =cut
  562: 
  563: #######################################################
  564: #######################################################
  565: sub previous_student { 
  566:     return $prev_student;
  567: }
  568: 
  569: #######################################################
  570: #######################################################
  571: 
  572: =pod
  573: 
  574: =item &next_student()
  575: 
  576: Returns a pointer to a hash containing data about the next student
  577: to be viewed.
  578: 
  579: =cut
  580: 
  581: #######################################################
  582: #######################################################
  583: sub next_student { 
  584:     return $next_student;
  585: }
  586: 
  587: ##############################################
  588: ##############################################
  589: 
  590: =pod 
  591: 
  592: =item &StudentDataSelect($elementname,$status,$numvisible,$selected)
  593: 
  594: Returns html for a selection box allowing the user to choose one (or more) 
  595: of the fields of student data available (fullname, username, id, section, etc)
  596: 
  597: =over 4
  598: 
  599: =item $elementname The name of the HTML form element
  600: 
  601: =item $status 'multiple' or 'single' selection box
  602: 
  603: =item $numvisible The number of options to be visible
  604: 
  605: =back
  606: 
  607: =cut
  608: 
  609: ##############################################
  610: ##############################################
  611: sub StudentDataSelect {
  612:     my ($elementname,$status,$numvisible)=@_;
  613:     if ($numvisible < 1) {
  614:         return;
  615:     }
  616:     #
  617:     # Build the form element
  618:     my $Str = "\n";
  619:     $Str .= '<select name="'.$elementname.'" ';
  620:     if ($status ne 'single') {
  621:         $Str .= 'multiple="true" ';
  622:     }
  623:     $Str .= 'size="'.$numvisible.'" >'."\n";
  624:     #
  625:     # Deal with 'all'
  626:     $Str .= '    <option value="all" ';
  627:     foreach (@SelectedStudentData) {
  628:         if ($_ eq 'all') {
  629:             $Str .= 'selected ';
  630:             last;
  631:         }
  632:     }
  633:     $Str .= ">all</option>\n";
  634:     #
  635:     # Loop through the student data fields
  636:     foreach my $item (@StudentDataOrder) {
  637:         $Str .= '    <option value="'.$item.'" ';
  638:         foreach (@SelectedStudentData) {
  639:             if ($item eq $_ ) {
  640:                 $Str .= 'selected ';
  641:                 last;
  642:             }
  643:         }
  644:         $Str .= '>'.$item."</option>\n";
  645:     }
  646:     $Str .= "</select>\n";
  647:     return $Str;
  648: }
  649: 
  650: #######################################################
  651: #######################################################
  652: 
  653: =pod
  654: 
  655: =item &get_selected_maps($elementname)
  656: 
  657: Input: Name of the <select> form element used to specify the maps.
  658: 
  659: Returns: Array of symbs of selected maps or the description 'all'.
  660:    If form.$elementname does not exist, 'all' is returned.
  661: 
  662: =cut
  663: 
  664: #######################################################
  665: #######################################################
  666: sub get_selected_maps {
  667:     my ($elementname) = @_;
  668:     my @selected_maps = 
  669: 	&Apache::loncommon::get_env_multiple('form.'.$elementname);
  670:     @selected_maps = ('all') if (! @selected_maps);
  671:     foreach my $map (@selected_maps) {
  672:         if ($map eq 'all') {
  673:             @selected_maps = ('all');
  674:             last;
  675:         }
  676:     }
  677:     return @selected_maps;
  678: }
  679: 
  680: 
  681: #######################################################
  682: #######################################################
  683: 
  684: =pod
  685: 
  686: =item &selected_sequences_with_assessments
  687: 
  688: Retrieve the sequences which were selected by the user to show.  
  689: 
  690: Input: $mode: scalar.  Either 'selected' or 'all'.  If not specified,
  691:     'selected' is used.
  692: 
  693: Returns: an array containing a navmap object and navmap resources, 
  694:     or an array containing a scalar with an error message.
  695: 
  696: =cut
  697: 
  698: #######################################################
  699: #######################################################
  700: sub selected_sequences_with_assessments {
  701:     my ($mode) = @_;
  702:     $mode = 'selected' if (! defined($mode));
  703:     my $navmap = Apache::lonnavmaps::navmap->new();
  704:     if (!defined($navmap)) {
  705:         return ('Can not open Coursemap');
  706:     }
  707:     #
  708:     my @sequences = $navmap->retrieveResources(undef,
  709:                                                sub { shift->is_map(); },1,0,1);
  710:     my @sequences_with_assessments;
  711:     for my $sequence ($navmap->getById('0.0'), @sequences) {
  712: 	if ($navmap->hasResource($sequence,sub { shift->is_problem(); },0,1)){
  713:             push(@sequences_with_assessments,$sequence);
  714:         }
  715:     }
  716:     #
  717:     my @sequences_to_show;
  718:     foreach my $sequence (@sequences_with_assessments) {
  719:         if ($mode eq 'all') {
  720:             push (@sequences_to_show,$sequence);
  721:         } elsif ($mode eq 'selected') {
  722:             foreach my $map_symb (&get_selected_maps('Maps')) {
  723:                 if ($sequence->symb eq $map_symb || $map_symb eq 'all'){
  724:                     push (@sequences_to_show,$sequence);
  725:                     last; # Only put it in once
  726:                 }
  727:             }
  728:         }
  729: 
  730:     }
  731:     return $navmap,@sequences_to_show;
  732: }
  733: 
  734: ##############################################
  735: ##############################################
  736: 
  737: =pod 
  738: 
  739: =item &map_select($elementname,$status,$numvisible,$restriction) 
  740: 
  741: Returns html for a selection box allowing the user to choose one (or more) 
  742: of the sequences in the course.  The values of the sequences are the symbs.
  743: If the top sequence is selected, the value 'top' will result.
  744: 
  745: =over 4
  746: 
  747: =item $elementname The name of the HTML form element
  748: 
  749: =item $status 'multiple' or 'single' selection box
  750: 
  751: =item $numvisible The number of options to be visible
  752: 
  753: =back
  754: 
  755: =cut
  756: 
  757: ##############################################
  758: ##############################################
  759: sub map_select {
  760:     my ($elementname,$status,$numvisible)=@_;
  761:     if ($numvisible < 1) {
  762:         return;
  763:     }
  764:     #
  765:     # Set up array of selected items
  766:     my @selected_maps = &get_selected_maps($elementname);
  767:     #
  768:     # Build the form element
  769:     my $form = "\n";
  770:     $form .= '<select name="'.$elementname.'" ';
  771:     if ($status ne 'single') {
  772:         $form .= 'multiple="true" ';
  773:     }
  774:     $form .= 'size="'.$numvisible.'" >'."\n";
  775:     #
  776:     # Put in option for 'all'
  777:     $form .= '    <option value="all" ';
  778:     if ($selected_maps[0] eq 'all') {
  779:         $form .= 'selected ';
  780:     }
  781:     $form .= ">all</option>\n";
  782:     #
  783:     # Loop through the sequences
  784:     my @sequences = &selected_sequences_with_assessments('all');
  785:     my $navmap;
  786:     if (!ref($sequences[0])) {
  787:         return $sequences[0];
  788:     } else {
  789:         $navmap = shift(@sequences);
  790:     }
  791:     foreach my $seq (@sequences){
  792:         $form .= '    <option value="'.$seq->symb.'" ';
  793:         foreach (@selected_maps) {
  794:             if ($seq->symb eq $_) {
  795:                 $form .= 'selected ';
  796:                 last;
  797:             }
  798:         }
  799:         $form .= '>'.$seq->compTitle."</option>\n";
  800:     }
  801:     $form .= "</select>\n";
  802:     return $form;
  803: }
  804: 
  805: ##############################################
  806: ##############################################
  807: 
  808: =pod 
  809: 
  810: =item &SectionSelect($elementname,$status,$numvisible) 
  811: 
  812: Returns html for a selection box allowing the user to choose one (or more) 
  813: of the sections in the course.  
  814: 
  815: Uses the package variables @Sections
  816: =over 4
  817: 
  818: =item $elementname The name of the HTML form element
  819: 
  820: =item $status 'multiple' or 'single' selection box
  821: 
  822: =item $numvisible The number of options to be visible
  823: 
  824: =back
  825: 
  826: =cut
  827: 
  828: ##############################################
  829: ##############################################
  830: sub SectionSelect {
  831:     my ($elementname,$status,$numvisible)=@_;
  832:     if ($numvisible < 1) {
  833:         return;
  834:     }
  835:     #
  836:     # Make sure we have the data we need to continue
  837:     if (! @Sections) {
  838:         &PrepareClasslist()
  839:     }
  840:     #
  841:     # Build the form element
  842:     my $Str = "\n";
  843:     $Str .= '<select name="'.$elementname.'" ';
  844:     if ($status ne 'single') {
  845:         $Str .= 'multiple="true" ';
  846:     }
  847:     $Str .= 'size="'.$numvisible.'" >'."\n";
  848:     #
  849:     # Loop through the sequences
  850:     foreach my $s (@Sections) {
  851:         $Str .= '    <option value="'.$s.'" ';
  852:         foreach (&get_selected_sections()) {
  853:             if ($s eq $_) {
  854:                 $Str .= 'selected ';
  855:                 last;
  856:             }
  857:         }
  858:         $Str .= '>'.$s."</option>\n";
  859:     }
  860:     $Str .= "</select>\n";
  861:     return $Str;
  862: }
  863: 
  864: ##############################################
  865: ##############################################
  866:                                                                                     
  867: =pod
  868:                                                                                     
  869: =item &GroupSelect($elementname,$status,$numvisible)
  870:                                                                                     
  871: Returns html for a selection box allowing the user to choose one (or more)
  872: of the groups in the course.
  873:                                                                                     
  874: Uses the package variables @Groups
  875: =over 4
  876:                                                                                     
  877: =item $elementname The name of the HTML form element
  878:                                                                                     
  879: =item $status 'multiple' or 'single' selection box
  880:                                                                                     
  881: =item $numvisible The number of options to be visible
  882:                                                                                     
  883: =back
  884:                                                                                     
  885: =cut
  886:                                                                                     
  887: ##############################################
  888: ##############################################
  889: sub GroupSelect {
  890:     my ($elementname,$status,$numvisible)=@_;
  891:     if ($numvisible < 1) {
  892:         return;
  893:     }
  894:     #
  895:     # Make sure we have the data we need to continue
  896:     if (! @Groups) {
  897:         &PrepareClasslist();
  898:     }
  899:     #
  900:     # Build the form element
  901:     my $Str = "\n";
  902:     $Str .= '<select name="'.$elementname.'" ';
  903:     if ($status ne 'single') {
  904:         $Str .= 'multiple="true" ';
  905:     }
  906:     $Str .= 'size="'.$numvisible.'" >'."\n";
  907:     #
  908:     # Loop through the groups
  909:     foreach my $s (@Groups) {
  910:         $Str .= '    <option value="'.$s.'" ';
  911:         foreach my $group (&get_selected_groups()) {
  912:             if ($s eq $group) {
  913:                 $Str .= 'selected ';
  914:                 last;
  915:             }
  916:         }
  917:         $Str .= '>'.$s."</option>\n";
  918:     }
  919:     $Str .= "</select>\n";
  920: }
  921: 
  922: 
  923: ##################################################
  924: ##################################################
  925: sub DisplayClasslist {
  926:     my ($r)=@_;
  927:     &Apache::lonhtmlcommon::add_breadcrumb
  928:         ({text=>'Select One Student'});
  929:     #
  930:     # Output some of the standard interface components
  931:     my $Str;
  932:     $Str .= &Apache::lonhtmlcommon::breadcrumbs('Select One Student');
  933:     $Str .= '<p><table cellspacing="5">'."\n";
  934:     $Str .= '<tr>';
  935:     $Str .= '<th align="center"><b>'.&mt('Sections').'</b></th>';
  936:     $Str .= '<th align="center"><b>'.&mt('Groups').'</b></th>';
  937:     $Str .= '<th align="center"><b>'.&mt('Access Status').'</b></th>';
  938:     $Str .= '</tr>'.$/;
  939:     $Str .= '<tr>';
  940:     $Str .= '<td>'.
  941:         &Apache::lonstatistics::SectionSelect('Section','multiple',5).
  942:         '</td>';
  943:     $Str .=  '<td>'.
  944:         &Apache::lonstatistics::GroupSelect('Group','multiple',5).
  945:         '</td>';
  946:     $Str .= '<td>'.
  947:         &Apache::lonhtmlcommon::StatusOptions(undef,undef,5).
  948:         '</td>';
  949:     
  950:     $Str .= '</tr>'.$/;
  951:     $Str .= '</table></p>';
  952:     $Str .= '<input type="submit" name="selectstudent" value="'.
  953:         &mt('Update Display').'" />';
  954:     $r->print($Str);
  955:     $r->rflush();
  956:     #
  957:     my @Fields = ('fullname','username','domain','id','section','status','groups');
  958:     #
  959:     $Str = '';
  960:     my @selected_sections = &get_selected_sections();
  961:     if (! @Students) {
  962:         if ($selected_sections[0] eq 'all') { 
  963:             if (lc($env{'form.Status'}) eq 'any') {
  964:                 $Str .= '<h2>'.
  965:                     &mt('There are no students in the course.').
  966:                     '</h2>';
  967:             } elsif (lc($env{'form.Status'}) eq 'active') {
  968:                 $Str .= '<h2>'.
  969:                 &mt('There are no currently enrolled students in the course.').
  970:                     '</h2>';
  971:             } elsif (lc($env{'form.Status'}) eq 'expired') {
  972:                 $Str .= '<h2>'.
  973:                     &mt('There are no previously enrolled students in the course.').
  974:                         '</h2>';
  975:             }
  976:         } else { 
  977:             my $sections;
  978:             if (lc($env{'form.Status'}) eq 'any') {
  979:                 $Str .= '<h2>'.
  980:                     &mt('There are no students in the selected sections.').
  981:                     '</h2>';
  982:             } elsif (lc($env{'form.Status'}) eq 'active') {
  983:                 $Str .= '<h2>'.
  984:                     &mt('There are no currently enrolled students in the selected sections.').
  985:                     '</h2>';
  986:             } elsif (lc($env{'form.Status'}) eq 'expired') {
  987:                 $Str .= '<h2>'.
  988:                     &mt('There are no previously enrolled students in the selected sections.').
  989:                     '</h2>';
  990:             }
  991:         }
  992:         $Str.= '<a href="/adm/statistics?reportSelected=student_assessment">'.
  993:             &mt('Click here to return to the chart').'</a>';
  994:         $r->print($Str);
  995:         $r->rflush();
  996:         return;
  997:     }
  998: 
  999:     # "Click" is asinine but it is probably not my place to change the world.
 1000:     $Str .= '<h2>Click on a students name or username to view their chart</h2>';
 1001:     $Str .= '<table border="0"><tr><td bgcolor="#777777">'."\n";
 1002:     $Str .= '<table border="0" cellpadding="3"><tr bgcolor="#e6ffff">'."\n";
 1003:     foreach my $field (@Fields) {
 1004:         $Str .= '<th><a href="/adm/statistics?'.
 1005:             'reportSelected=student_assessment&'.
 1006:             'selectstudent=1&'.
 1007:             'sort='.$field.'">'.&mt($field).
 1008:             '</a></th>';
 1009:     }
 1010:     $Str .= '</tr>'."\n";
 1011:     #
 1012:     my $alternate = 0;
 1013:     foreach my $student (@Students) { # @Students is a package variable
 1014:         my $sname = $student->{'username'}.':'.$student->{'domain'};
 1015:         if($alternate) {
 1016:             $Str .= '<tr bgcolor="#ffffe6">';
 1017:         } else {
 1018:             $Str .= '<tr bgcolor="#ffffc6">';
 1019:         }
 1020:         $alternate = ($alternate + 1) % 2;
 1021:         #
 1022:         foreach my $field (@Fields) {
 1023:             $Str .= '<td>';
 1024:             if ($field eq 'fullname' || $field eq 'username') {
 1025:                 $Str .= '<a href="/adm/statistics?reportSelected=';
 1026:                 $Str .= &escape('student_assessment');
 1027:                 $Str .= '&sort='.&escape($env{'form.sort'});
 1028:                 $Str .= '&SelectedStudent=';
 1029:                 $Str .= &escape($sname).'">';
 1030:                 $Str .= $student->{$field}.'&nbsp';
 1031:                 $Str .= '</a>';
 1032:             } elsif ($field eq 'status') {
 1033:                 $Str .= &mt($student->{$field});
 1034:             } else {
 1035:                 $Str .= $student->{$field};
 1036:             }
 1037:             $Str .= '</td>';
 1038:         }
 1039:         $Str .= "</tr>\n";
 1040:     }
 1041:     $Str .= '</table></td></tr></table>'."\n";
 1042:     #
 1043:     $r->print($Str);
 1044:     $r->rflush();
 1045:     #
 1046:     return;
 1047: }
 1048: 
 1049: ##############################################
 1050: ##############################################
 1051: sub CreateMainMenu {
 1052:     #
 1053:     # Define menu data
 1054:     my @reports = ({ internal_name => 'problem_statistics',
 1055:                      name => &mt('Overall Problem Statistics'),
 1056:                      short_description => 
 1057:     &mt('Student performance statistics on all problems.'),
 1058:                  },
 1059:                    { internal_name => 'problem_analysis',
 1060:                      name => &mt('Detailed Problem Analysis'),
 1061:                      short_description => 
 1062:     &mt('Detailed statistics and graphs of student performance on problems.'),
 1063:                  },
 1064:                    { internal_name => 'submissiontime_analysis',
 1065:                      name => &mt('Submission Time Plots'),
 1066:                      short_description => 
 1067:     &mt('Display and analysis of submission times on assessments.'),
 1068:                  },
 1069:                    { internal_name => 'student_submission_reports',
 1070:                      name => &mt('Student Submission Reports'),
 1071:                      short_description => 
 1072:     &mt('Prepare reports of student submissions.'),
 1073:                  },
 1074:                    { internal_name => 'survey_reports',
 1075:                      name => &mt('Survey Reports'),
 1076:                      short_description => 
 1077:     &mt('Prepare reports on survey results.'),
 1078:                  },
 1079:                    { internal_name => 'correct_problems_plot',
 1080:                      name => &mt('Correct Problems Plot'),
 1081:                      short_description => 
 1082:     &mt('Display a histogram of student performance in the course.'),
 1083:                  },
 1084: #                   { internal_name => 'grading_analysis',
 1085: #                     name => &mt('Detailed Grading Analysis'),
 1086: #                     short_description => 
 1087: #    &mt('Display statistics about who graded who.'),
 1088: #                 },
 1089: #                   { internal_name => 'student_assessment',
 1090: #                     name => &mt('Problem Status Chart'),
 1091: #                     short_description => 
 1092: #    &mt('Brief view of each students performance in course.'),
 1093: #                 },
 1094:                    # 'percentage'  => 'Correct-problems Plot',
 1095:                    # 'activitylog' => 'Activity Log',
 1096:                    );
 1097:     #
 1098:     # Create the menu
 1099:     my $Str;
 1100:     $Str .= '<h2>'.&mt('Please select a report to generate').'</h2>';
 1101:     foreach my $reportdata (@reports) {
 1102:         $Str .='    <h3><a href="/adm/statistics?reportSelected='.
 1103:             $reportdata->{'internal_name'}.'" >'.
 1104:             $reportdata->{'name'}."</a></h3>\n";
 1105:         $Str .= '    '.('&nbsp;'x8).$reportdata->{'short_description'}.
 1106:             "\n";
 1107:     }
 1108:     $Str .="</dl>\n";
 1109:     #
 1110:     return $Str;
 1111: }
 1112: 
 1113: ##############################################
 1114: ##############################################
 1115: sub handler {
 1116:     my $r=shift;
 1117:     my $c = $r->connection();
 1118:     #
 1119:     # Check for overloading
 1120:     my $loaderror=&Apache::lonnet::overloaderror($r);
 1121:     if ($loaderror) { return $loaderror; }
 1122:     $loaderror=
 1123:        &Apache::lonnet::overloaderror($r,
 1124:          $env{'course.'.$env{'request.course.id'}.'.home'});
 1125:     if ($loaderror) { return $loaderror; }
 1126:     #
 1127:     # Check for access
 1128:     if (! &Apache::lonnet::allowed('vgr',$env{'request.course.id'})) {
 1129:         $env{'user.error.msg'}=
 1130:             $r->uri.":vgr:0:0:Cannot view grades for complete course";
 1131:         if (! &Apache::lonnet::allowed('vgr',
 1132:                       $env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
 1133:             $env{'user.error.msg'}=
 1134:                 $r->uri.":vgr:0:0:Cannot view grades with given role";
 1135:             return HTTP_NOT_ACCEPTABLE;
 1136:         }
 1137:     }
 1138:     #
 1139:     # Send the header
 1140:     &Apache::loncommon::no_cache($r);
 1141:     &Apache::loncommon::content_type($r,'text/html');
 1142:     $r->send_http_header;
 1143:     if ($r->header_only) { return OK; }
 1144:     #
 1145:     # Extract form elements from query string
 1146:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1147:                                             ['sort','reportSelected',
 1148:                                              'SelectedStudent']);
 1149:     #
 1150:     # Give the LON-CAPA page header
 1151:     my $style = <<ENDSTYLE;
 1152: <style type="text/css">
 1153:     ul.sub_studentans { list-style-type: none }
 1154:     ul.sub_correctans { list-style-type: none }
 1155:     tr.even           { background-color: \#CCCCCC }
 1156:     td.essay          { border: 1px solid gray; }
 1157: </style>
 1158: ENDSTYLE
 1159:       
 1160:     $r->print(&Apache::loncommon::start_page('Course Statistics and Charts',
 1161: 					     $style));
 1162:     $r->rflush();
 1163:     # 
 1164:     # Either print out a menu for them or send them to a report
 1165:     &Apache::lonhtmlcommon::clear_breadcrumbs();
 1166:     &Apache::lonhtmlcommon::add_breadcrumb({href=>'/adm/statistics',
 1167:                                             title=>'Statistics',
 1168:                                             text =>'Statistics',
 1169:                                             faq=>139,
 1170:                                             bug=>'Statistics and Charts'});
 1171:     if (! exists($env{'form.reportSelected'}) || 
 1172:         $env{'form.reportSelected'} eq '') {
 1173:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('Statistics Main Page').
 1174:                   &CreateMainMenu());
 1175:     } else {
 1176:     #
 1177:         if (! &Apache::lonmysql::verify_sql_connection()) {
 1178:             my $serveradmin = $r->dir_config('lonAdmEMail');
 1179:             $r->print('<h2><font color="Red">'.
 1180:                       &mt('Unable to connect to database!').
 1181:                       '</font></h2>');
 1182:             $r->print('<p>'.
 1183:                       &mt('Please notify the server administrator ').
 1184:                       '<b>'.$serveradmin.'</b></p>');
 1185:             $r->print('<p>'.
 1186:                       &mt('Course Statistics and Charts cannot be '.
 1187:                           'retrieved until the database is restarted.  '.
 1188:                           'Your data is intact but cannot be displayed '.
 1189:                           'at this time.').'</p>');
 1190:             $r->print(&Apache::loncommon::end_page());
 1191:             return;
 1192:         }
 1193:         #
 1194:         # Clean out the caches
 1195:         if (exists($env{'form.ClearCache'})) {
 1196:             &Apache::loncoursedata::delete_caches($env{'requres.course.id'});
 1197:         }
 1198:         #
 1199:         # Begin form output
 1200:         $r->print('<form name="Statistics" ');
 1201:         $r->print('method="post" action="/adm/statistics">');
 1202:         $r->rflush();
 1203:         #
 1204:         my $GoToPage = $env{'form.reportSelected'};
 1205:         #
 1206:         $r->print('<input type="hidden" name="reportSelected" value="'.
 1207:                   $GoToPage.'">');
 1208:         if($GoToPage eq 'activitylog') {
 1209: #        &Apache::lonproblemstatistics::Activity();
 1210:         } elsif($GoToPage eq 'problem_statistics') {
 1211:             &Apache::lonhtmlcommon::add_breadcrumb
 1212:                 ({href=>'/adm/statistics?reportselected=problem_statistics',
 1213:                   text=>'Overall Problem Statistics'});
 1214:             &Apache::lonproblemstatistics::BuildProblemStatisticsPage($r,$c);
 1215:         } elsif($GoToPage eq 'problem_analysis') {
 1216:             &Apache::lonhtmlcommon::add_breadcrumb
 1217:                 ({href=>'/adm/statistics?reportselected=problem_analysis',
 1218:                   text=>'Detailed Problem Analysis'});
 1219:             &Apache::lonproblemanalysis::BuildProblemAnalysisPage($r,$c);
 1220:         } elsif($GoToPage eq 'submissiontime_analysis') {
 1221:             &Apache::lonhtmlcommon::add_breadcrumb
 1222:                 ({href=>
 1223:                       '/adm/statistics?reportselected=submissiontime_analysis',
 1224:                       text=>'Submission Time Plots'});
 1225:             &Apache::lonsubmissiontimeanalysis::BuildSubmissionTimePage($r,$c);
 1226:         } elsif($GoToPage eq 'student_submission_reports') {
 1227:             &Apache::lonhtmlcommon::add_breadcrumb
 1228:                 ({href=>
 1229:                   '/adm/statistics?reportselected=student_submission_reports',
 1230:                   text=>'Student Submission Reports'});
 1231:             &Apache::lonstudentsubmissions::BuildStudentSubmissionsPage($r,$c);
 1232:         } elsif($GoToPage eq 'survey_reports') {
 1233:             &Apache::lonhtmlcommon::add_breadcrumb
 1234:                 ({href=>
 1235:                   '/adm/statistics?reportselected=survey_reports',
 1236:                   text=>'Survey Reports'});
 1237:             &Apache::lonsurveyreports::BuildSurveyReportsPage($r,$c);
 1238:         } elsif($GoToPage eq 'correct_problems_plot') {
 1239:             &Apache::lonhtmlcommon::add_breadcrumb
 1240:                 ({href=>'/adm/statistics?reportselected=correct_problems_plot',
 1241:                   text=>'Correct Problems Plot'});
 1242:             &Apache::loncorrectproblemplot::BuildCorrectProblemsPage($r,$c);
 1243:         } elsif($GoToPage eq 'student_assessment') {
 1244:             &Apache::lonhtmlcommon::clear_breadcrumbs();
 1245:             &Apache::lonhtmlcommon::add_breadcrumb
 1246:                 ({href=>'/adm/statistics?reportselected=student_assessment',
 1247:                   text=>'Chart'});
 1248:             &Apache::lonstudentassessment::BuildStudentAssessmentPage($r,$c);
 1249:         } elsif($GoToPage eq 'grading_analysis') {
 1250:             &Apache::lonhtmlcommon::add_breadcrumb
 1251:                 ({href=>'/adm/statistics?reportselected=grading_anaylsis',
 1252:                   text=>'Grading Analysis'});
 1253:             &Apache::longradinganalysis::build_grading_analysis_page($r,$c);
 1254: 	}
 1255:         #
 1256:         $r->print("</form>\n");
 1257:     }
 1258:     $r->print(&Apache::loncommon::end_page());
 1259:     $r->rflush();
 1260:     #
 1261:     return OK;
 1262: }
 1263: 
 1264: 1;
 1265: 
 1266: #######################################################
 1267: #######################################################
 1268: 
 1269: =pod
 1270: 
 1271: =back
 1272: 
 1273: =cut
 1274: 
 1275: #######################################################
 1276: #######################################################
 1277: 
 1278: __END__
 1279: 

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