File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.135: download - view: text, annotated - select for diffs
Thu May 18 01:08:50 2006 UTC (18 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
&coursegroups() and &get_group_settings() moved to longroup.pm, which contains general utility functions for asking about groups.  Also contains &group_changes() which is used to add/drop group memberships as a result of role changes, as determined by group settings for auto-add and auto-drop. 'none' and 'all' are now reserved words which may not be used as section or group names, so they can be used instead of _all and _none  when specifying auto-add and auto-drop settings for all sections or no section roles.

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

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