File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.134: download - view: text, annotated - select for diffs
Thu May 11 20:05:02 2006 UTC (18 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Include keylist as argument passed to loncoursedata::get_group_memberships().  Used to add comma-separated list of active groups to a students classlist data in $classlist->{$student}->[index], where index is set so as to add to the end of the existing array.

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

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