Annotation of loncom/interface/statistics/lonproblemstatistics.pm, revision 1.108

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: #
1.108   ! raeburn     3: # $Id: lonproblemstatistics.pm,v 1.107 2006/05/01 19:29:13 albertel Exp $
1.1       stredwic    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: #
1.47      matthew    29: ###############################################
                     30: ###############################################
                     31: 
                     32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
                     36: lonproblemstatistics
                     37: 
                     38: =head1 SYNOPSIS
                     39: 
                     40: Routines to present problem statistics to instructors via tables,
                     41: Excel files, and plots.
                     42: 
                     43: =over 4
                     44: 
                     45: =cut
                     46: 
                     47: ###############################################
                     48: ###############################################
1.1       stredwic   49: 
1.36      minaeibi   50: package Apache::lonproblemstatistics;
1.1       stredwic   51: 
                     52: use strict;
1.104     albertel   53: use Apache::lonnet;
1.62      matthew    54: use Apache::loncommon();
1.1       stredwic   55: use Apache::lonhtmlcommon;
                     56: use Apache::loncoursedata;
1.41      matthew    57: use Apache::lonstatistics;
1.84      matthew    58: use LONCAPA::lonmetadata();
1.59      matthew    59: use Apache::lonlocal;
1.44      matthew    60: use Spreadsheet::WriteExcel;
1.70      matthew    61: use Apache::lonstathelpers();
1.71      matthew    62: use Time::HiRes;
1.73      matthew    63: 
                     64: my @StatsArray;
1.79      matthew    65: my %SeqStat;    # keys are symbs, values are hash refs
1.73      matthew    66: 
1.59      matthew    67: ##
                     68: ## Localization notes:
                     69: ##
                     70: ## in @Fields[0]->{'long_title'} is placed in Excel files and is used as the
                     71: ## header for plots created with Graph.pm, both of which more than likely do
                     72: ## not support localization.
                     73: ##
1.79      matthew    74: #
                     75: #
                     76: ##
                     77: ## Description of Field attributes
                     78: ##
                     79: ## Attribute     Required   Value       Meaning or Use
                     80: ##
                     81: ## name            yes      any scalar  Used to uniquely identify field
                     82: ## title           yes      any scalar  This is what the user sees to identify
                     83: ##                                      the field.  Passed through &mt().
                     84: ## long_title      yes      any scalar  Used as graph heading and in excel
                     85: ##                                      output.  NOT translated
                     86: ## align           no    (left|right|center)  HTML cell contents alignment
                     87: ## color           yes      html color  HTML cell background color
                     88: ##                                      used to visually group statistics
                     89: ## special         no          (link)   Indicates a link, target is name.link
                     90: ##                                      Currently set in &get_statistics()
                     91: ## graphable       no      (yes|no)     Can a bar graph of the field be 
                     92: ##                                      produced?
                     93: ## sortable        no      (yes|no)     Should a sort link be put in the
                     94: ##                                      column header?
                     95: ## selectable      yes     (yes|no)     Can the column be removed from the
                     96: ##                                      statistics display?
                     97: ## selected        yes     (yes|no)     Is the column selected by default?
                     98: ##
1.85      matthew    99: ## format          no      sprintf format string
                    100: ##
                    101: ## excel_format    no      excel format type 
                    102: ##                               (see &Apache::loncommon::define_excel_formats
1.49      matthew   103: my @Fields = (
                    104:            { name => 'problem_num',
                    105:              title => 'P#',
                    106:              align => 'right',
1.76      matthew   107:              color => '#FFFFE6',
                    108:              selectable => 'no',
1.80      matthew   109:              defaultselected => 'yes',
1.76      matthew   110:            },
1.49      matthew   111:            { name   => 'container',
1.51      matthew   112:              title  => 'Sequence or Folder',
1.49      matthew   113:              align  => 'left',
                    114:              color  => '#FFFFE6',
1.76      matthew   115:              sortable => 'yes',
                    116:              selectable => 'no',
1.80      matthew   117:              defaultselected => 'yes',
1.76      matthew   118:            },
1.49      matthew   119:            { name   => 'title',
                    120:              title  => 'Title',
                    121:              align  => 'left',
                    122:              color  => '#FFFFE6',
                    123:              special  => 'link',
1.76      matthew   124:              sortable => 'yes', 
                    125:              selectable => 'no',
1.80      matthew   126:              defaultselected => 'yes',
1.76      matthew   127:            },
1.49      matthew   128:            { name   => 'part', 
                    129:              title  => 'Part',
                    130:              align  => 'left',
1.55      matthew   131:              color  => '#FFFFE6',
1.76      matthew   132:              selectable => 'no',
1.80      matthew   133:              defaultselected => 'yes',
1.76      matthew   134:            },
1.49      matthew   135:            { name   => 'num_students',
                    136:              title  => '#Stdnts',
                    137:              align  => 'right',
                    138:              color  => '#EEFFCC',
                    139:              format => '%d',
                    140:              sortable  => 'yes',
                    141:              graphable => 'yes',
1.76      matthew   142:              long_title => 'Number of Students Attempting Problem',
                    143:              selectable => 'yes',
1.80      matthew   144:              defaultselected => 'yes',
1.76      matthew   145:            },
1.49      matthew   146:            { name   => 'tries',
                    147:              title  => 'Tries',
                    148:              align  => 'right',
                    149:              color  => '#EEFFCC',
                    150:              format => '%d',
                    151:              sortable  => 'yes',
                    152:              graphable => 'yes',
1.76      matthew   153:              long_title => 'Total Number of Tries',
                    154:              selectable => 'yes',
1.80      matthew   155:              defaultselected => 'yes',
1.76      matthew   156:            },
1.49      matthew   157:            { name   => 'max_tries',
                    158:              title  => 'Max Tries',
                    159:              align  => 'right',
                    160:              color  => '#DDFFFF',
                    161:              format => '%d',
                    162:              sortable  => 'yes',
                    163:              graphable => 'yes',
1.76      matthew   164:              long_title => 'Maximum Number of Tries',
                    165:              selectable => 'yes',
1.80      matthew   166:              defaultselected => 'yes',
1.76      matthew   167:            },
1.73      matthew   168:            { name   => 'min_tries',
                    169:              title  => 'Min Tries',
                    170:              align  => 'right',
                    171:              color  => '#DDFFFF',
                    172:              format => '%d',
                    173:              sortable  => 'yes',
                    174:              graphable => 'yes',
1.76      matthew   175:              long_title => 'Minumum Number of Tries',
                    176:              selectable => 'yes',
1.80      matthew   177:              defaultselected => 'yes',
1.76      matthew   178:            },
1.49      matthew   179:            { name   => 'mean_tries',
                    180:              title  => 'Mean Tries',
                    181:              align  => 'right',
                    182:              color  => '#DDFFFF',
                    183:              format => '%5.2f',
                    184:              sortable  => 'yes',
                    185:              graphable => 'yes',
1.76      matthew   186:              long_title => 'Average Number of Tries',
                    187:              selectable => 'yes',
1.80      matthew   188:              defaultselected => 'yes',
1.76      matthew   189:            },
1.49      matthew   190:            { name   => 'std_tries',
                    191:              title  => 'S.D. tries',
                    192:              align  => 'right',
                    193:              color  => '#DDFFFF',
                    194:              format => '%5.2f',
                    195:              sortable  => 'yes',
                    196:              graphable => 'yes',
1.76      matthew   197:              long_title => 'Standard Deviation of Number of Tries',
                    198:              selectable => 'yes',
1.80      matthew   199:              defaultselected => 'yes',
1.76      matthew   200:            },
1.49      matthew   201:            { name   => 'skew_tries',
                    202:              title  => 'Skew Tries',
                    203:              align  => 'right',
                    204:              color  => '#DDFFFF',
                    205:              format => '%5.2f',
                    206:              sortable  => 'yes',
                    207:              graphable => 'yes',
1.76      matthew   208:              long_title => 'Skew of Number of Tries',
                    209:              selectable => 'yes',
1.80      matthew   210:              defaultselected => 'no',
1.76      matthew   211:            },
1.49      matthew   212:            { name   => 'num_solved',
                    213:              title  => '#YES',
                    214:              align  => 'right',
                    215:              color  => '#FFDDDD',
1.63      matthew   216:              format => '%4.1f',#             format => '%d',
1.49      matthew   217:              sortable  => 'yes',
                    218:              graphable => 'yes',
1.76      matthew   219:              long_title => 'Number of Students able to Solve',
1.77      matthew   220:              selectable => 'yes',
1.80      matthew   221:              defaultselected => 'yes',
1.76      matthew   222:            },
1.49      matthew   223:            { name   => 'num_override',
                    224:              title  => '#yes',
                    225:              align  => 'right',
                    226:              color  => '#FFDDDD',
1.63      matthew   227:              format => '%4.1f',#             format => '%d',
1.49      matthew   228:              sortable  => 'yes',
                    229:              graphable => 'yes',
1.76      matthew   230:              long_title => 'Number of Students given Override',
                    231:              selectable => 'yes',
1.80      matthew   232:              defaultselected => 'yes',
1.76      matthew   233:            },
1.95      matthew   234:            { name   => 'tries_per_correct',
                    235:              title  => 'tries/correct',
                    236:              align  => 'right',
                    237:              color  => '#FFDDDD',
                    238:              format => '%4.1f',
                    239:              sortable  => 'yes',
                    240:              graphable => 'yes',
                    241:              long_title => 'Tries per Correct Answer',
                    242:              selectable => 'yes',
                    243:              defaultselected => 'yes',
                    244:            },
1.73      matthew   245:            { name   => 'num_wrong',
                    246:              title  => '#Wrng',
1.49      matthew   247:              align  => 'right',
1.73      matthew   248:              color  => '#FFDDDD',
1.49      matthew   249:              format => '%4.1f',
                    250:              sortable  => 'yes',
                    251:              graphable => 'yes',
1.93      matthew   252:              long_title => 'Number of students whose final answer is wrong',
                    253:              selectable => 'yes',
                    254:              defaultselected => 'yes',
                    255:            },
                    256:            { name   => 'per_wrong',
                    257:              title  => '%Wrng',
                    258:              align  => 'right',
                    259:              color  => '#FFDDDD',
                    260:              format => '%4.1f',
                    261:              sortable  => 'yes',
                    262:              graphable => 'yes',
1.76      matthew   263:              long_title => 'Percent of students whose final answer is wrong',
                    264:              selectable => 'yes',
1.80      matthew   265:              defaultselected => 'yes',
1.76      matthew   266:            },
1.73      matthew   267:            { name   => 'deg_of_diff',
                    268:              title  => 'DoDiff',
                    269:              align  => 'right',
                    270:              color  => '#FFFFE6',
                    271:              format => '%5.2f',
                    272:              sortable  => 'yes',
                    273:              graphable => 'yes',
                    274:              long_title => 'Degree of Difficulty'.
1.76      matthew   275:                            '[ 1 - ((#YES+#yes) / Tries) ]',
                    276:              selectable => 'yes',
1.80      matthew   277:              defaultselected => 'yes',
1.76      matthew   278:            },
1.71      matthew   279:            { name   => 'deg_of_disc',
1.73      matthew   280:              title  => 'DoDisc',
1.71      matthew   281:              align  => 'right',
                    282:              color  => '#FFFFE6',
                    283:              format => '%4.2f',
                    284:              sortable  => 'yes',
                    285:              graphable => 'yes',
1.76      matthew   286:              long_title => 'Degree of Discrimination',
                    287:              selectable => 'yes',
1.89      matthew   288:              defaultselected => 'yes',
1.76      matthew   289:            },
1.85      matthew   290: ##   duedate included for research purposes.  Commented out most of the time.
                    291: #           { name => 'duedate',
                    292: #             title => 'Due Date',
                    293: #             align => 'left',
                    294: #             color => '#FFFFFF',
                    295: #             sortable => 'yes',
                    296: #             graphable => 'no',
                    297: #             long_title => 'Due date of resource for instructor',
                    298: #             selectable => 'no',
                    299: #             defaultselected => 'yes',
                    300: #            },
                    301: ##   opendate included for research purposes.  Commented out most of the time.
                    302: #           { name => 'opendate',
                    303: #             title => 'Open Date',
                    304: #             align => 'left',
                    305: #             color => '#FFFFFF',
                    306: #             sortable => 'yes',
                    307: #             graphable => 'no',
                    308: #             long_title => 'date resource became answerable',
                    309: #             selectable => 'no',
                    310: #             defaultselected => 'yes',
                    311: #            },
                    312: ##   symb included for research purposes.  Commented out most of the time.
                    313: #           { name => 'symb',
                    314: #             title => 'Symb',
                    315: #             align => 'left',
                    316: #             color => '#FFFFFF',
                    317: #             sortable => 'yes',
                    318: #             graphable => 'no',
                    319: #             long_title => 'Unique LON-CAPA identifier for problem',
                    320: #             selectable => 'no',
                    321: #             defaultselected => 'yes',
                    322: #            },
1.86      matthew   323: ##   resptypes included for research purposes.  Commented out most of the time.
                    324: #           { name => 'resptypes',
                    325: #             title => 'Response Types',
                    326: #             align => 'left',
                    327: #             color => '#FFFFFF',
                    328: #             sortable => 'no',
                    329: #             graphable => 'no',
                    330: #             long_title => 'Response Types used in this problem',
                    331: #             selectable => 'no',
                    332: #             defaultselected => 'yes',
                    333: #            },
1.94      matthew   334: ##   maxtries included for research purposes.  Commented out most of the time.
                    335: #           { name => 'maxtries',
                    336: #             title => 'Maxtries',
                    337: #             align => 'left',
                    338: #             color => '#FFFFFF',
                    339: #             sortable => 'no',
                    340: #             graphable => 'no',
                    341: #             long_title => 'Maximum number of tries',
                    342: #             selectable => 'no',
                    343: #             defaultselected => 'yes',
                    344: #            },
                    345: ##   hinttries included for research purposes.  Commented out most of the time.
                    346: #           { name => 'hinttries',
                    347: #             title => 'hinttries',
                    348: #             align => 'left',
                    349: #             color => '#FFFFFF',
                    350: #             sortable => 'no',
                    351: #             graphable => 'no',
                    352: #             long_title => 'Number of tries before a hint appears',
                    353: #             selectable => 'no',
                    354: #             defaultselected => 'yes',
                    355: #            },
1.98      matthew   356: #
                    357: ##   problem weight for instructor
                    358:            { name => 'weight',
                    359:              title => 'weight',
                    360:              align => 'right',
                    361:              color => '#FFFFFF',
                    362:              sortable => 'no',
                    363:              graphable => 'no',
                    364:              long_title => 'Problem weight (for instructor)',
1.100     matthew   365:              selectable => 'yes',
1.98      matthew   366:              defaultselected => 'yes',
                    367:             },
1.49      matthew   368: );
                    369: 
1.79      matthew   370: my @SeqFields = (
                    371:            { name   => 'title',
                    372:              title  => 'Sequence',
                    373:              align  => 'left',
                    374:              color  => '#FFFFE6',
                    375:              special  => 'no',
                    376:              sortable => 'no', 
                    377:              selectable => 'yes',
1.80      matthew   378:              defaultselected => 'no',
1.79      matthew   379:            },
                    380:            { name   => 'items',
                    381:              title  => '#Items',
                    382:              align  => 'right',
                    383:              color  => '#FFFFE6',
                    384:              format => '%4d',
                    385:              sortable  => 'no',
                    386:              graphable => 'no',
                    387:              long_title => 'Number of Items in Sequence',
                    388:              selectable => 'yes',
1.80      matthew   389:              defaultselected => 'no',
1.79      matthew   390:            },
                    391:            { name   => 'scoremean',
                    392:              title  => 'Score Mean',
                    393:              align  => 'right',
                    394:              color  => '#FFFFE6',
                    395:              format => '%4.2f',
                    396:              sortable  => 'no',
                    397:              graphable => 'no',
                    398:              long_title => 'Mean Sequence Score',
                    399:              selectable => 'yes',
1.80      matthew   400:              defaultselected => 'no',
1.79      matthew   401:            },
                    402:            { name   => 'scorestd',
                    403:              title  => 'Score STD',
                    404:              align  => 'right',
                    405:              color  => '#FFFFE6',
                    406:              format => '%4.2f',
                    407:              sortable  => 'no',
                    408:              graphable => 'no',
                    409:              long_title => 'Standard Deviation of Sequence Scores',
                    410:              selectable => 'yes',
1.80      matthew   411:              defaultselected => 'no',
1.79      matthew   412:            },
                    413:            { name   => 'scoremax',
                    414:              title  => 'Score Max',
                    415:              align  => 'right',
                    416:              color  => '#FFFFE6',
                    417:              format => '%4.2f',
                    418:              sortable  => 'no',
                    419:              graphable => 'no',
                    420:              long_title => 'Maximum Sequence Score',
                    421:              selectable => 'yes',
1.80      matthew   422:              defaultselected => 'no',
1.79      matthew   423:            },
                    424:            { name   => 'scoremin',
                    425:              title  => 'Score Min',
                    426:              align  => 'right',
                    427:              color  => '#FFFFE6',
                    428:              format => '%4.2f',
                    429:              sortable  => 'no',
                    430:              graphable => 'no',
                    431:              long_title => 'Minumum Sequence Score',
                    432:              selectable => 'yes',
1.80      matthew   433:              defaultselected => 'no',
1.79      matthew   434:            },
                    435:            { name   => 'scorecount',
                    436:              title  => 'Score N',
                    437:              align  => 'right',
                    438:              color  => '#FFFFE6',
                    439:              format => '%4d',
                    440:              sortable  => 'no',
                    441:              graphable => 'no',
                    442:              long_title => 'Number of Students in score computations',
                    443:              selectable => 'yes',
1.80      matthew   444:              defaultselected => 'no',
1.79      matthew   445:            },
                    446:            { name   => 'countmean',
                    447:              title  => 'Count Mean',
                    448:              align  => 'right',
                    449:              color  => '#FFFFFF',
                    450:              format => '%4.2f',
                    451:              sortable  => 'no',
                    452:              graphable => 'no',
                    453:              long_title => 'Mean Sequence Score',
                    454:              selectable => 'yes',
1.80      matthew   455:              defaultselected => 'no',
1.79      matthew   456:            },
                    457:            { name   => 'countstd',
                    458:              title  => 'Count STD',
                    459:              align  => 'right',
                    460:              color  => '#FFFFFF',
                    461:              format => '%4.2f',
                    462:              sortable  => 'no',
                    463:              graphable => 'no',
                    464:              long_title => 'Standard Deviation of Sequence Scores',
                    465:              selectable => 'yes',
1.80      matthew   466:              defaultselected => 'no',
1.79      matthew   467:            },
                    468:            { name   => 'countmax',
                    469:              title  => 'Count Max',
                    470:              align  => 'right',
                    471:              color  => '#FFFFFF',
                    472:              format => '%4.2f',
                    473:              sortable  => 'no',
                    474:              graphable => 'no',
                    475:              long_title => 'Maximum Number of Correct Problems',
                    476:              selectable => 'yes',
1.80      matthew   477:              defaultselected => 'no',
1.79      matthew   478:            },
                    479:            { name   => 'countmin',
                    480:              title  => 'Count Min',
                    481:              align  => 'right',
                    482:              color  => '#FFFFFF',
                    483:              format => '%4.2f',
                    484:              sortable  => 'no',
                    485:              graphable => 'no',
                    486:              long_title => 'Minumum Number of Correct Problems',
                    487:              selectable => 'yes',
1.80      matthew   488:              defaultselected => 'no',
1.79      matthew   489:            },
                    490:            { name   => 'count',
                    491:              title  => 'Count N',
                    492:              align  => 'right',
                    493:              color  => '#FFFFFF',
                    494:              format => '%4d',
                    495:              sortable  => 'no',
                    496:              graphable => 'no',
                    497:              long_title => 'Number of Students in score computations',
                    498:              selectable => 'yes',
1.80      matthew   499:              defaultselected => 'no',
1.79      matthew   500:            },
                    501:            { name   => 'KR-21',
                    502:              title  => 'KR-21',
                    503:              align  => 'right',
                    504:              color  => '#FFAAAA',
                    505:              format => '%4.2f',
                    506:              sortable  => 'no',
                    507:              graphable => 'no',
                    508:              long_title => 'KR-21 reliability statistic',
                    509:              selectable => 'yes',
1.80      matthew   510:              defaultselected => 'no',
1.79      matthew   511:            },           
                    512: );
                    513: 
1.76      matthew   514: my %SelectedFields;
                    515: 
                    516: sub parse_field_selection {
                    517:     #
                    518:     # Pull out the defaults
1.104     albertel  519:     if (! defined($env{'form.fieldselections'})) {
                    520:         $env{'form.fieldselections'} = [];
1.76      matthew   521:         foreach my $field (@Fields) {
                    522:             next if ($field->{'selectable'} ne 'yes');
1.80      matthew   523:             if ($field->{'defaultselected'} eq 'yes') {
1.104     albertel  524:                 push(@{$env{'form.fieldselections'}},$field->{'name'});
1.76      matthew   525:             }
                    526:         }
                    527:     }
                    528:     #
1.80      matthew   529:     # Make sure the data we are plotting is there
                    530:     my %NeededFields;
1.104     albertel  531:     if (exists($env{'form.plot'}) && $env{'form.plot'} ne '' &&
                    532:         $env{'form.plot'} ne 'none') {
                    533:         if ($env{'form.plot'} eq 'degrees') {
1.80      matthew   534:             $NeededFields{'deg_of_diff'}++;
                    535:             $NeededFields{'deg_of_disc'}++;
1.104     albertel  536:         } elsif ($env{'form.plot'} eq 'tries statistics') {
1.80      matthew   537:             $NeededFields{'mean_tries'}++;
                    538:             $NeededFields{'std_tries'}++;
                    539:             $NeededFields{'problem_num'}++;
                    540:         } else {
1.104     albertel  541:             $NeededFields{$env{'form.plot'}}++;
1.80      matthew   542:         }
                    543:     }
                    544:     #
1.76      matthew   545:     # This should not happen, but in case it does...
1.104     albertel  546:     if (ref($env{'form.fieldselections'}) ne 'ARRAY') {
                    547:         $env{'form.fieldselections'} = [$env{'form.fieldselections'}];
1.76      matthew   548:     }
                    549:     #
                    550:     # Set the field data and the selected fields (for easier checking)
                    551:     undef(%SelectedFields);
                    552:     foreach my $field (@Fields) {
1.80      matthew   553:         if ($field->{'selectable'} ne 'yes') {
                    554:             $field->{'selected'} = 'yes';
                    555:         } else {
                    556:             $field->{'selected'} = 'no';
                    557:         }
                    558:         if (exists($NeededFields{$field->{'name'}})) {
                    559:             $field->{'selected'} = 'yes';
                    560:             $SelectedFields{$field->{'name'}}++;
                    561:         }
1.104     albertel  562:         foreach my $selection (@{$env{'form.fieldselections'}}) {
1.76      matthew   563:             if ($selection eq $field->{'name'} || $selection eq 'all') {
                    564:                 $field->{'selected'} = 'yes';
                    565:                 $SelectedFields{$field->{'name'}}++;
                    566:             }
                    567:         }
                    568:     }
1.82      matthew   569:     #
                    570:     # Always show all the sequence statistics (for now)
                    571:     foreach my $field (@SeqFields) {
                    572:         $field->{'selected'} = 'yes';
                    573:     }
1.76      matthew   574:     return;
                    575: }
                    576: 
                    577: sub field_selection_input {
                    578:     my $Str = '<select name="fieldselections" multiple size="5">'."\n";
                    579:     $Str .= '<option value="all">all</option>'."\n";
                    580:     foreach my $field (@Fields) {
                    581:         next if ($field->{'selectable'} ne 'yes');
                    582:         $Str .= '    <option value="'.$field->{'name'}.'" ';
                    583:         if ($field->{'selected'} eq 'yes') {
                    584:             $Str .= 'selected ';
                    585:         }
                    586:         $Str .= '>'.$field->{'title'}.'</option>'."\n";
                    587:     }
                    588:     $Str .= "</select>\n";
                    589: }
                    590: 
1.47      matthew   591: ###############################################
                    592: ###############################################
                    593: 
                    594: =pod 
                    595: 
                    596: =item &CreateInterface()
                    597: 
                    598: Create the main intereface for the statistics page.  Allows the user to
                    599: select sections, maps, and output.
                    600: 
                    601: =cut
1.1       stredwic  602: 
1.47      matthew   603: ###############################################
                    604: ###############################################
1.41      matthew   605: sub CreateInterface {
1.87      matthew   606:     my ($r) = @_;
1.80      matthew   607:     #
1.76      matthew   608:     &parse_field_selection();
1.80      matthew   609:     #
1.41      matthew   610:     my $Str = '';
1.107     albertel  611:     $Str .= &Apache::lonhtmlcommon::breadcrumbs('Overall Problem Statistics',
                    612: 						'Statistics_Overall_Key');
1.41      matthew   613:     $Str .= '<table cellspacing="5">'."\n";
                    614:     $Str .= '<tr>';
1.59      matthew   615:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
1.108   ! raeburn   616:     $Str .= '<td align="center"><b>'.&mt('Groups').'</b></td>';
1.59      matthew   617:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
                    618:     $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
1.76      matthew   619:     $Str .= '<td align="center"><b>'.&mt('Statistics').'</b></td>';
1.70      matthew   620:     $Str .= '<td rowspan="2">'.
                    621:         &Apache::lonstathelpers::limit_by_time_form().'</td>';
1.41      matthew   622:     $Str .= '</tr>'."\n";
                    623:     #
                    624:     $Str .= '<tr><td align="center">'."\n";
                    625:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
1.50      matthew   626:     $Str .= '</td><td align="center">';
1.108   ! raeburn   627:     $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5);
        !           628:     $Str .= '</td><td align="center">';
1.50      matthew   629:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
1.41      matthew   630:     $Str .= '</td><td align="center">';
                    631:     #
1.97      matthew   632:     $Str .= &Apache::lonstatistics::map_select('Maps','multiple,all',5);
1.76      matthew   633:     $Str .= '</td><td>'.&field_selection_input();
1.41      matthew   634:     $Str .= '</td></tr>'."\n";
                    635:     $Str .= '</table>'."\n";
1.87      matthew   636:     #
                    637:     $Str .= '<p>'.&mt('Status: [_1]',
                    638:                          '<input type="text" '.
                    639:                          'name="stats_status" size="60" value="" />'
                    640:                          ).
                    641:                          '</nobr></p>';
                    642:     #
1.59      matthew   643:     $Str .= '<input type="submit" name="GenerateStatistics" value="'.
                    644:         &mt('Generate Statistics').'" />';
1.54      matthew   645:     $Str .= '&nbsp;'x5;
1.73      matthew   646:     $Str .= 'Plot '.&plot_dropdown().('&nbsp;'x10);
1.87      matthew   647:     #
1.73      matthew   648:     return $Str;
1.41      matthew   649: }
1.25      stredwic  650: 
1.41      matthew   651: ###############################################
                    652: ###############################################
1.28      stredwic  653: 
1.47      matthew   654: =pod 
                    655: 
                    656: =item &BuildProblemStatisticsPage()
                    657: 
                    658: Main interface to problem statistics.
                    659: 
                    660: =cut
                    661: 
1.41      matthew   662: ###############################################
                    663: ###############################################
1.97      matthew   664: my $navmap;
                    665: my @sequences;
                    666: 
1.105     albertel  667: sub clean_up {
                    668:     undef($navmap);
                    669:     undef(@sequences);
                    670: }
                    671: 
1.41      matthew   672: sub BuildProblemStatisticsPage {
                    673:     my ($r,$c)=@_;
1.97      matthew   674:     undef($navmap);
                    675:     undef(@sequences);
1.61      matthew   676:     #
                    677:     my %Saveable_Parameters = ('Status' => 'scalar',
                    678:                                'statsoutputmode' => 'scalar',
                    679:                                'Section' => 'array',
1.108   ! raeburn   680:                                'Groups' => 'array',
1.61      matthew   681:                                'StudentData' => 'array',
1.77      matthew   682:                                'Maps' => 'array',
                    683:                                'fieldselections'=> 'array');
1.61      matthew   684:     &Apache::loncommon::store_course_settings('statistics',
                    685:                                               \%Saveable_Parameters);
                    686:     &Apache::loncommon::restore_course_settings('statistics',
                    687:                                                 \%Saveable_Parameters);
                    688:     #
                    689:     &Apache::lonstatistics::PrepareClasslist();
1.41      matthew   690:     #
1.73      matthew   691:     # Clear the package variables
                    692:     undef(@StatsArray);
1.79      matthew   693:     undef(%SeqStat);
1.71      matthew   694:     #
1.73      matthew   695:     # Finally let the user know we are here
1.87      matthew   696:     my $interface = &CreateInterface($r);
1.57      matthew   697:     $r->print($interface);
1.104     albertel  698:     $r->print('<input type="hidden" name="sortby" value="'.$env{'form.sortby'}.
1.41      matthew   699:               '" />');
1.73      matthew   700:     #
1.87      matthew   701:     my @CacheButtonHTML = 
                    702:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
                    703:     my $Str;
                    704:     foreach my $html (@CacheButtonHTML) {
                    705:         $Str.=$html.('&nbsp;'x5);
                    706:     }
                    707:     #
                    708:     $r->print($Str);
1.104     albertel  709:     if (! exists($env{'form.firstrun'})) {
1.73      matthew   710:         $r->print('<h3>'.
                    711:                   &mt('Press "Generate Statistics" when you are ready.').
                    712:                   '</h3><p>'.
                    713:                   &mt('It may take some time to update the student data '.
                    714:                       'for the first analysis.  Future analysis this session '.
                    715:                       ' will not have this delay.').
                    716:                   '</p>');
1.105     albertel  717: 	&clean_up();
1.41      matthew   718:         return;
1.28      stredwic  719:     }
1.73      matthew   720:     $r->rflush();
1.41      matthew   721:     #
1.73      matthew   722:     # This probably does not need to be done each time we are called, but
                    723:     # it does not slow things down noticably.
                    724:     &Apache::loncoursedata::populate_weight_table();
1.75      matthew   725:     #
1.99      matthew   726:     ($navmap,@sequences) = 
                    727:         &Apache::lonstatistics::selected_sequences_with_assessments();
                    728:     if (! ref($navmap)) {
                    729:         $r->print('<h1>'.&mt('A course-wide error occured.').'</h1>'.
                    730:                   '<h3>'.$navmap.'</h3>');
1.105     albertel  731: 	&clean_up();
1.99      matthew   732:         return;
                    733:     }
1.104     albertel  734:     if (exists($env{'form.Excel'})) {
1.102     matthew   735:         $r->print('<h4>'.
                    736:                   &Apache::lonstatistics::section_and_enrollment_description().
                    737:                   '</h4>');
1.73      matthew   738:         &Excel_output($r);
1.87      matthew   739:     } else { 
                    740:         $r->print('<input type="submit" name="Excel" value="'.
                    741:                   &mt('Produce Excel Output').'" />'.'&nbsp;'x5);
                    742:         $r->rflush();
1.102     matthew   743:         $r->print('<h4>'.
                    744:                   &Apache::lonstatistics::section_and_enrollment_description().
                    745:                   '</h4>');
1.75      matthew   746:         my $count = 0;
1.97      matthew   747:         foreach my $seq (@sequences) {
                    748:             my @resources = 
                    749:                 &Apache::lonstathelpers::get_resources($navmap,$seq);
                    750:             $count += scalar(@resources);
1.75      matthew   751:         }
                    752:         if ($count > 10) {
                    753:             $r->print('<h2>'.
                    754:                       &mt('Compiling statistics for [_1] problems',$count).
                    755:                       '</h2>');
                    756:             if ($count > 30) {
                    757:                 $r->print('<h3>'.&mt('This will take some time.').'</h3>');
                    758:             }
                    759:             $r->rflush();
                    760:         }
                    761:         #
1.104     albertel  762:         my $sortby = $env{'form.sortby'};
1.73      matthew   763:         $sortby = 'container' if (! defined($sortby) || $sortby =~ /^\s*$/);
1.104     albertel  764:         my $plot = $env{'form.plot'};
1.75      matthew   765:         if ($plot eq '' || $plot eq 'none') {
                    766:             undef($plot);
                    767:         }
1.73      matthew   768:         if ($sortby eq 'container' && ! defined($plot)) {
1.79      matthew   769:             &output_sequence_statistics($r);
1.73      matthew   770:             &output_html_by_sequence($r);
                    771:         } else {
                    772:             if (defined($plot)) {
                    773:                 &make_plot($r,$plot);
                    774:             }
                    775:             &output_html_stats($r);
1.79      matthew   776:             &output_sequence_statistics($r);
1.73      matthew   777:         }
                    778:     }
1.105     albertel  779:     &clean_up();
1.73      matthew   780:     return;
                    781: }
                    782: 
1.79      matthew   783: sub output_sequence_statistics {
                    784:     my ($r) = @_;
                    785:     my $c=$r->connection();
1.90      matthew   786:     $r->print('<h2>'.&mt('Sequence Statistics').
                    787: 	      &Apache::loncommon::help_open_topic('Statistics_Sequence').
                    788: 	      '</h2>');
1.79      matthew   789:     $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n".
                    790:               '<table border="0" cellpadding="3">'."\n".
                    791:               '<tr bgcolor="#FFFFE6">');
                    792:     $r->print(&sequence_html_header());
1.97      matthew   793:     foreach my $seq (@sequences) { 
1.79      matthew   794:         last if ($c->aborted);
                    795:         &compute_sequence_statistics($seq);
                    796:         $r->print(&sequence_html_output($seq));
                    797:     }
                    798:     $r->print('</table>');
                    799:     $r->print('</table>');
                    800:     $r->rflush();
                    801:     return;
                    802: }
                    803: 
                    804: 
1.73      matthew   805: ##########################################################
                    806: ##########################################################
                    807: ##
                    808: ## HTML output routines
                    809: ##
                    810: ##########################################################
                    811: ##########################################################
                    812: sub output_html_by_sequence {
                    813:     my ($r) = @_;
                    814:     my $c = $r->connection();
                    815:     $r->print(&html_preamble());
1.41      matthew   816:     #
1.97      matthew   817:     foreach my $seq (@sequences) {
1.73      matthew   818:         last if ($c->aborted);
1.97      matthew   819:         $r->print("<h3>".$seq->compTitle."</h3>".
1.73      matthew   820:                   '<table border="0"><tr><td bgcolor="#777777">'."\n".
                    821:                   '<table border="0" cellpadding="3">'."\n".
                    822:                   '<tr bgcolor="#FFFFE6">'.
                    823:                   &statistics_table_header('no container')."</tr>\n");
                    824:         my @Data = &compute_statistics_on_sequence($seq);
                    825:         foreach my $data (@Data) {
                    826:             $r->print('<tr>'.&statistics_html_table_data($data,
                    827:                                                          'no container').
                    828:                       "</tr>\n");
1.70      matthew   829:         }
1.73      matthew   830:         $r->print('</table>'."\n".'</table>'."\n");
1.41      matthew   831:         $r->rflush();
1.28      stredwic  832:     }
1.41      matthew   833:     return;
                    834: }
1.21      stredwic  835: 
1.73      matthew   836: sub output_html_stats {
                    837:     my ($r)=@_;
                    838:     &compute_all_statistics($r);
                    839:     $r->print(&html_preamble());
1.104     albertel  840:     &sort_data($env{'form.sortby'});
1.73      matthew   841:     #
                    842:     my $count=0;
                    843:     foreach my $data (@StatsArray) {
                    844:         if ($count++ % 50 == 0) {
                    845:             $r->print("</table>\n</table>\n");
                    846:             $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n".
                    847:                       '<table border="0" cellpadding="3">'."\n".
                    848:                       '<tr bgcolor="#FFFFE6">'.
                    849:                       '<tr bgcolor="#FFFFE6">'.
                    850:                       &statistics_table_header().
                    851:                       "</tr>\n");
                    852:         }
                    853:         $r->print('<tr>'.&statistics_html_table_data($data)."</tr>\n");
                    854:     }
                    855:     $r->print("</table>\n</table>\n");
                    856:     return;
                    857: }
1.47      matthew   858: 
1.73      matthew   859: sub html_preamble {
                    860:     my $Str='';
                    861:     $Str .= "<h2>".
1.104     albertel  862:         $env{'course.'.$env{'request.course.id'}.'.description'}.
1.73      matthew   863:         "</h2>\n";
                    864:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
                    865:     if (defined($starttime) || defined($endtime)) {
                    866:         # Inform the user what the time limits on the data are.
                    867:         $Str .= '<h3>'.&mt('Statistics on submissions from [_1] to [_2]',
                    868:                            &Apache::lonlocal::locallocaltime($starttime),
                    869:                            &Apache::lonlocal::locallocaltime($endtime)
                    870:                            ).'</h3>';
                    871:     }
                    872:     $Str .= "<h3>".&mt('Compiled on [_1]',
                    873:                        &Apache::lonlocal::locallocaltime(time))."</h3>";
                    874:     return $Str;
                    875: }
1.47      matthew   876: 
                    877: 
1.44      matthew   878: ###############################################
                    879: ###############################################
1.73      matthew   880: ##
                    881: ## Misc HTML output routines
                    882: ##
                    883: ###############################################
                    884: ###############################################
                    885: sub statistics_html_table_data {
                    886:     my ($data,$options) = @_;
                    887:     my $row = '';
                    888:     foreach my $field (@Fields) {
                    889:         next if ($options =~ /no $field->{'name'}/);
1.76      matthew   890:         next if ($field->{'selected'} ne 'yes');
1.73      matthew   891:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
                    892:         if (exists($field->{'align'})) {
                    893:             $row .= ' align="'.$field->{'align'}.'"';
1.41      matthew   894:             }
1.73      matthew   895:         $row .= '>';
                    896:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
                    897:             $row .= '<a href="'.$data->{$field->{'name'}.'.link'}.'">';
1.41      matthew   898:         }
1.92      matthew   899:         if (exists($field->{'format'}) && $data->{$field->{'name'}} !~ /[A-Z]/i) {
1.73      matthew   900:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
                    901:         } else {
                    902:             $row .= $data->{$field->{'name'}};
                    903:         }
                    904:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
                    905:             $row.= '</a>';
                    906:         }
                    907:         $row .= '</td>';
1.21      stredwic  908:     }
1.73      matthew   909:     return $row;
1.41      matthew   910: }
1.25      stredwic  911: 
1.73      matthew   912: sub statistics_table_header {
                    913:     my ($options) = @_;
                    914:     my $header_row;
                    915:     foreach my $field (@Fields) {
                    916:         next if ($options =~ /no $field->{'name'}/);
1.76      matthew   917:         next if ($field->{'selected'} ne 'yes');
1.73      matthew   918:         $header_row .= '<th>';
                    919:         if (exists($field->{'sortable'}) && $field->{'sortable'} eq 'yes') {
                    920:             $header_row .= '<a href="javascript:'.
                    921:                 'document.Statistics.sortby.value='."'".$field->{'name'}."'".
                    922:                     ';document.Statistics.submit();">';
                    923:         }
                    924:         $header_row .= &mt($field->{'title'});
                    925:         if ($options =~ /sortable/) {
                    926:             $header_row.= '</a>';
                    927:         }
                    928:         if ($options !~ /no plots/        && 
                    929:             exists($field->{'graphable'}) && 
                    930:             $field->{'graphable'} eq 'yes') {
                    931:             $header_row.=' (';
                    932:             $header_row .= '<a href="javascript:'.
                    933:                 "document.Statistics.plot.value='$field->{'name'}'".
                    934:                     ';document.Statistics.submit();">';
                    935:             $header_row .= &mt('plot').'</a>)';
                    936:         }
                    937:         $header_row .= '</th>';
                    938:     }
                    939:     return $header_row;
                    940: }
1.26      stredwic  941: 
1.79      matthew   942: sub sequence_html_header {
                    943:     my $Str .= '<tr>';
                    944:     foreach my $field (@SeqFields) {
                    945: #        next if ($field->{'selected'} ne 'yes');
                    946:         $Str .= '<th bgcolor="'.$field->{'color'}.'"';
                    947:         $Str .= '>'.$field->{'title'}.'</th>';
                    948:     }
                    949:     $Str .= '</tr>';
                    950:     return $Str;
                    951: }
                    952: 
                    953: 
                    954: sub sequence_html_output {
                    955:     my ($seq) = @_;
1.97      matthew   956:     my $data = $SeqStat{$seq->symb};
1.79      matthew   957:     my $row = '<tr>';
                    958:     foreach my $field (@SeqFields) {
1.82      matthew   959:         next if ($field->{'selected'} ne 'yes');
1.79      matthew   960:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
                    961:         if (exists($field->{'align'})) {
                    962:             $row .= ' align="'.$field->{'align'}.'"';
                    963:         }
                    964:         $row .= '>';
                    965:         if (exists($field->{'format'})) {
                    966:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
                    967:         } else {
                    968:             $row .= $data->{$field->{'name'}};
                    969:         }
                    970:         $row .= '</td>';
                    971:     }
                    972:     $row .= '</tr>'."\n";
                    973:     return $row;
                    974: }
                    975: 
1.73      matthew   976: ####################################################
                    977: ####################################################
                    978: ##
                    979: ##    Plotting Routines
                    980: ##
                    981: ####################################################
                    982: ####################################################
                    983: sub make_plot {
                    984:     my ($r,$plot) = @_;
                    985:     &compute_all_statistics($r);
1.104     albertel  986:     &sort_data($env{'form.sortby'});
1.73      matthew   987:     if ($plot eq 'degrees') {
                    988:         &degrees_plot($r);
1.74      matthew   989:     } elsif ($plot eq 'tries statistics') {
                    990:         &tries_data_plot($r);
1.73      matthew   991:     } else {
                    992:         &make_single_stat_plot($r,$plot);
                    993:     }
                    994:     return;
                    995: }
1.47      matthew   996: 
1.73      matthew   997: sub make_single_stat_plot {
                    998:     my ($r,$datafield) = @_;
1.41      matthew   999:     #
1.73      matthew  1000:     my $title; my $yaxis;
                   1001:     foreach my $field (@Fields) {
                   1002:         next if ($field->{'name'} ne $datafield);
                   1003:         $title = $field->{'long_title'};
                   1004:         $yaxis = $field->{'title'};
                   1005:         last;
                   1006:     }
                   1007:     if ($title eq '' || $yaxis eq '') {
                   1008:         # datafield is something we do not know enough about to plot
                   1009:         $r->print('<h3>'.
                   1010:                   &mt('Unable to plot the requested statistic.').
                   1011:                   '</h3>');
                   1012:         return;
1.49      matthew  1013:     }
                   1014:     #
1.73      matthew  1015:     # Build up the data sets to plot
                   1016:     my @Labels; 
                   1017:     my @Data;
                   1018:     my $max = 1;
                   1019:     foreach my $data (@StatsArray) {
                   1020:         push(@Labels,$data->{'problem_num'});
                   1021:         push(@Data,$data->{$datafield});
                   1022:         if ($data->{$datafield}>$max) {
                   1023:             $max = $data->{$datafield};
                   1024:         }
                   1025:     }
                   1026:     foreach (1,2,3,4,5,10,15,20,25,40,50,75,100,150,200,250,300,500,600,750,
                   1027:              1000,1500,2000,2500,3000,3500,4000,5000,7500,10000,15000,20000) {
                   1028:         if ($max <= $_) {
                   1029:             $max = $_;
                   1030:             last;
1.42      matthew  1031:         }
                   1032:     }
1.73      matthew  1033:     if ($max > 20000) {
                   1034:         $max = 10000*(int($max/10000)+1);
1.42      matthew  1035:     }
1.73      matthew  1036:     #
                   1037:     $r->print("<p>".&Apache::loncommon::DrawBarGraph($title,
                   1038:                                                      'Problem Number',
                   1039:                                                      $yaxis,
                   1040:                                                      $max,
                   1041:                                                      undef, # colors
                   1042:                                                      \@Labels,
                   1043:                                                      \@Data)."</p>\n");
                   1044:     return;
                   1045: }
                   1046: 
                   1047: sub degrees_plot {
                   1048:     my ($r)=@_;
                   1049:     my $count = scalar(@StatsArray);
                   1050:     my $width = 50 + 10*$count;
                   1051:     $width = 300 if ($width < 300);
                   1052:     my $height = 300;
                   1053:     my $plot = '';
                   1054:     my $ymax = 0;
                   1055:     my $ymin = 0;
                   1056:     my @Disc; my @Diff; my @Labels;    
                   1057:     foreach my $data (@StatsArray) {
                   1058:         push(@Labels,$data->{'problem_num'});
                   1059:         my $disc = $data->{'deg_of_disc'};
                   1060:         my $diff = $data->{'deg_of_diff'};
                   1061:         push(@Disc,$disc);
                   1062:         push(@Diff,$diff);
                   1063:         #
                   1064:         $ymin = $disc if ($ymin > $disc);
                   1065:         $ymin = $diff if ($ymin > $diff);
                   1066:         $ymax = $disc if ($ymax < $disc);
                   1067:         $ymax = $diff if ($ymax < $diff);
                   1068:     }
                   1069:     #
                   1070:     # Make sure we show relevant information.
                   1071:     if ($ymin < 0) {
                   1072:         if (abs($ymin) < 0.05) {
                   1073:             $ymin = 0;
                   1074:         } else {
                   1075:             $ymin = -1;
1.42      matthew  1076:         }
                   1077:     }
1.73      matthew  1078:     if ($ymax > 0) {
                   1079:         if (abs($ymax) < 0.05) {
                   1080:             $ymax = 0;
1.42      matthew  1081:         } else {
1.73      matthew  1082:             $ymax = 1;
1.42      matthew  1083:         }
1.43      matthew  1084:     }
1.49      matthew  1085:     #
1.73      matthew  1086:     my $xmax = $Labels[-1];
                   1087:     if ($xmax > 50) {
                   1088:         if ($xmax % 10 != 0) {
                   1089:             $xmax = 10 * (int($xmax/10)+1);
                   1090:         }
                   1091:     } else {
                   1092:         if ($xmax % 5 != 0) {
                   1093:             $xmax = 5 * (int($xmax/5)+1);
1.49      matthew  1094:         }
1.26      stredwic 1095:     }
1.41      matthew  1096:     #
1.73      matthew  1097:     my $discdata .= '<data>'.join(',',@Labels).'</data>'.$/.
                   1098:                     '<data>'.join(',',@Disc).'</data>'.$/;
                   1099:     #
                   1100:     my $diffdata .= '<data>'.join(',',@Labels).'</data>'.$/.
                   1101:                     '<data>'.join(',',@Diff).'</data>'.$/;
                   1102:     #
1.81      matthew  1103:     my $title = 'Degree of Discrimination\nand Degree of Difficulty';
                   1104:     if ($xmax > 50) {
                   1105:         $title = 'Degree of Discrimination and Degree of Difficulty';
                   1106:     }
                   1107:     #
1.73      matthew  1108:     $plot=<<"END";
                   1109: <gnuplot 
                   1110:     texfont="10"
                   1111:     fgcolor="x000000"
                   1112:     plottype="Cartesian"
                   1113:     font="large"
                   1114:     grid="on"
                   1115:     align="center"
                   1116:     border="on"
                   1117:     transparent="on"
1.81      matthew  1118:     alttag="Degree of Discrimination and Degree of Difficulty Plot"
1.73      matthew  1119:     samples="100"
                   1120:     bgcolor="xffffff"
                   1121:     height="$height"
                   1122:     width="$width">
                   1123:     <key 
                   1124:         pos="top right"
                   1125:         title=""
                   1126:         box="off" />
1.81      matthew  1127:     <title>$title</title>
1.73      matthew  1128:     <axis xmin="0" ymin="$ymin" xmax="$xmax" ymax="$ymax" color="x000000" />
                   1129:     <xlabel>Problem Number</xlabel>
                   1130:     <curve 
                   1131:         linestyle="linespoints" 
                   1132:         name="DoDisc" 
                   1133:         pointtype="0" 
                   1134:         color="x000000">
                   1135:         $discdata
                   1136:     </curve>
                   1137:     <curve 
                   1138:         linestyle="linespoints" 
                   1139:         name="DoDiff" 
                   1140:         pointtype="0" 
                   1141:         color="xFF0000">
                   1142:         $diffdata
                   1143:     </curve>
                   1144: </gnuplot>
                   1145: END
                   1146:     my $plotresult = 
                   1147:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
                   1148:     $r->print($plotresult);
1.41      matthew  1149:     return;
1.42      matthew  1150: }
                   1151: 
1.74      matthew  1152: sub tries_data_plot {
                   1153:     my ($r)=@_;
                   1154:     my $count = scalar(@StatsArray);
                   1155:     my $width = 50 + 10*$count;
                   1156:     $width = 300 if ($width < 300);
                   1157:     my $height = 300;
                   1158:     my $plot = '';
                   1159:     my @STD;  my @Mean; my @Max; my @Min;
                   1160:     my @Labels;
                   1161:     my $ymax = 5;
                   1162:     foreach my $data (@StatsArray) {
                   1163:         my $max = $data->{'mean_tries'} + $data->{'std_tries'};
                   1164:         $ymax = $max if ($ymax < $max);
                   1165:         $ymax = $max if ($ymax < $max);
                   1166:         push(@Labels,$data->{'problem_num'});
                   1167:         push(@STD,$data->{'std_tries'});
                   1168:         push(@Mean,$data->{'mean_tries'});
                   1169:     }
                   1170:     #
                   1171:     # Make sure we show relevant information.
                   1172:     my $xmax = $Labels[-1];
                   1173:     if ($xmax > 50) {
                   1174:         if ($xmax % 10 != 0) {
                   1175:             $xmax = 10 * (int($xmax/10)+1);
                   1176:         }
                   1177:     } else {
                   1178:         if ($xmax % 5 != 0) {
                   1179:             $xmax = 5 * (int($xmax/5)+1);
                   1180:         }
                   1181:     }
                   1182:     $ymax = int($ymax)+1+2;
                   1183:     #
                   1184:     my $std_data .= '<data>'.join(',',@Labels).'</data>'.$/.
                   1185:                     '<data>'.join(',',@Mean).'</data>'.$/;
                   1186:     #
                   1187:     my $std_error_data .= '<data>'.join(',',@Labels).'</data>'.$/.
                   1188:                           '<data>'.join(',',@Mean).'</data>'.$/.
                   1189:                           '<data>'.join(',',@STD).'</data>'.$/;
                   1190:     #
1.81      matthew  1191:     my $title = 'Mean and S.D. of Tries';
                   1192:     if ($xmax > 25) {
                   1193:         $title = 'Mean and Standard Deviation of Tries';
                   1194:     }
                   1195:     #
1.74      matthew  1196:     $plot=<<"END";
                   1197: <gnuplot 
                   1198:     texfont="10"
                   1199:     fgcolor="x000000"
                   1200:     plottype="Cartesian"
                   1201:     font="large"
                   1202:     grid="on"
                   1203:     align="center"
                   1204:     border="on"
                   1205:     transparent="on"
1.81      matthew  1206:     alttag="Mean and S.D of Tries Plot"
1.74      matthew  1207:     samples="100"
                   1208:     bgcolor="xffffff"
                   1209:     height="$height"
                   1210:     width="$width">
1.81      matthew  1211:     <title>$title</title>
1.74      matthew  1212:     <axis xmin="0" ymin="0" xmax="$xmax" ymax="$ymax" color="x000000" />
                   1213:     <xlabel>Problem Number</xlabel>
1.81      matthew  1214:     <ylabel>Number of Tries</ylabel>
1.74      matthew  1215:     <curve 
                   1216:         linestyle="yerrorbars"
                   1217:         name="S.D. Tries" 
                   1218:         pointtype="1" 
                   1219:         color="x666666">
                   1220:         $std_error_data
                   1221:     </curve>
                   1222:     <curve 
                   1223:         linestyle="points"
                   1224:         name="Mean Tries" 
                   1225:         pointtype="1" 
                   1226:         color="xCC4444">
                   1227:         $std_data
                   1228:     </curve>
                   1229: </gnuplot>
                   1230: END
                   1231:     my $plotresult = 
                   1232:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
                   1233:     $r->print($plotresult);
                   1234:     return;
                   1235: }
                   1236: 
1.73      matthew  1237: sub plot_dropdown {
                   1238:     my $current = '';
                   1239:     #
1.104     albertel 1240:     if (defined($env{'form.plot'})) {
                   1241:         $current = $env{'form.plot'};
1.73      matthew  1242:     }
                   1243:     #
                   1244:     my @Additional_Plots = (
                   1245:                             { graphable=>'yes',
                   1246:                               name => 'degrees',
1.81      matthew  1247:                               title => 'Difficulty Indexes' },
1.74      matthew  1248:                             { graphable=>'yes',
                   1249:                               name => 'tries statistics',
1.81      matthew  1250:                               title => 'Tries Statistics' });
1.73      matthew  1251:     #
                   1252:     my $Str= "\n".'<select name="plot" size="1">';
                   1253:     $Str .= '<option name="none"></option>'."\n";
                   1254:     $Str .= '<option name="none2">none</option>'."\n";
1.81      matthew  1255:     foreach my $field (@Additional_Plots,@Fields) {
1.73      matthew  1256:         if (! exists($field->{'graphable'}) ||
                   1257:             $field->{'graphable'} ne 'yes') {
                   1258:             next;
                   1259:         }
                   1260:         $Str .= '<option value="'.$field->{'name'}.'"';
                   1261:         if ($field->{'name'} eq $current) {
                   1262:             $Str .= ' selected ';
                   1263:         }
                   1264:         $Str.= '>'.&mt($field->{'title'}).'</option>'."\n";
                   1265:     }
                   1266:     $Str .= '</select>'."\n";
                   1267:     return $Str;
                   1268: }
                   1269: 
1.41      matthew  1270: ###############################################
                   1271: ###############################################
1.73      matthew  1272: ##
                   1273: ## Excel output routines
                   1274: ##
1.41      matthew  1275: ###############################################
                   1276: ###############################################
1.73      matthew  1277: sub Excel_output {
1.44      matthew  1278:     my ($r) = @_;
1.73      matthew  1279:     $r->print('<h2>'.&mt('Preparing Excel Spreadsheet').'</h2>');
                   1280:     ##
                   1281:     ## Compute the statistics
                   1282:     &compute_all_statistics($r);
                   1283:     my $c = $r->connection;
                   1284:     return if ($c->aborted());
1.101     matthew  1285:     #
                   1286:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
1.73      matthew  1287:     ##
                   1288:     ## Create the excel workbook
1.101     matthew  1289:     my ($excel_workbook,$filename,$format) =
                   1290:         &Apache::loncommon::create_workbook($r);
                   1291:     return if (! defined($excel_workbook));
1.44      matthew  1292:     #
                   1293:     # Add a worksheet
1.104     albertel 1294:     my $sheetname = $env{'course.'.$env{'request.course.id'}.'.description'};
1.44      matthew  1295:     if (length($sheetname) > 31) {
                   1296:         $sheetname = substr($sheetname,0,31);
                   1297:     }
1.73      matthew  1298:     my $excel_sheet = $excel_workbook->addworksheet(
                   1299:         &Apache::loncommon::clean_excel_name($sheetname));
                   1300:     ##
                   1301:     ## Begin creating excel sheet
                   1302:     ##
                   1303:     my ($rows_output,$cols_output) = (0,0);
1.44      matthew  1304:     #
                   1305:     # Put the course description in the header
                   1306:     $excel_sheet->write($rows_output,$cols_output++,
1.104     albertel 1307:                    $env{'course.'.$env{'request.course.id'}.'.description'},
1.82      matthew  1308:                         $format->{'h1'});
1.44      matthew  1309:     $cols_output += 3;
                   1310:     #
                   1311:     # Put a description of the sections listed
                   1312:     my $sectionstring = '';
1.82      matthew  1313:     $excel_sheet->write($rows_output,$cols_output++,
1.103     matthew  1314:                         &Apache::lonstatistics::section_and_enrollment_description('plaintext'),
1.82      matthew  1315:                         $format->{'h3'});
1.102     matthew  1316:     $cols_output += scalar(&Apache::lonstatistics::get_selected_sections());
1.108   ! raeburn  1317:     $cols_output += scalar(&Apache::lonstatistics::get_selected_groups());
1.44      matthew  1318:     #
1.70      matthew  1319:     # Time restrictions
                   1320:     my $time_string;
                   1321:     if (defined($starttime)) {
                   1322:         # call localtime but not lonlocal:locallocaltime because excel probably
                   1323:         # cannot handle localized text.  Probably.
                   1324:         $time_string .= 'Data collected from '.localtime($time_string);
                   1325:         if (defined($endtime)) {
                   1326:             $time_string .= ' to '.localtime($endtime);
                   1327:         }
                   1328:         $time_string .= '.';
                   1329:     } elsif (defined($endtime)) {
                   1330:         # See note above about lonlocal:locallocaltime
                   1331:         $time_string .= 'Data collected before '.localtime($endtime).'.';
                   1332:     }
1.82      matthew  1333:     if (defined($time_string)) {
                   1334:         $excel_sheet->write($rows_output,$cols_output++,$time_string);
                   1335:         $cols_output+= 5;
                   1336:     }
1.70      matthew  1337:     #
1.44      matthew  1338:     # Put the date in there too
                   1339:     $excel_sheet->write($rows_output,$cols_output++,
                   1340:                         'Compiled on '.localtime(time));
                   1341:     #
                   1342:     $rows_output++; 
                   1343:     $cols_output=0;
1.82      matthew  1344:     ##
                   1345:     ## Sequence Statistics
                   1346:     ## 
                   1347:     &write_headers($excel_sheet,$format,\$rows_output,\$cols_output,
                   1348:                    \@SeqFields);
1.97      matthew  1349:     foreach my $seq (@sequences) {
                   1350:         my $data = $SeqStat{$seq->symb};
1.82      matthew  1351:         $cols_output=0;
                   1352:         foreach my $field (@SeqFields) {
                   1353:             next if ($field->{'selected'} ne 'yes');
1.85      matthew  1354:             my $fieldformat = undef;
                   1355:             if (exists($field->{'excel_format'})) {
                   1356:                 $fieldformat = $format->{$field->{'excel_format'}};
                   1357:             }
1.55      matthew  1358:             $excel_sheet->write($rows_output,$cols_output++,
1.85      matthew  1359:                                 $data->{$field->{'name'}},$fieldformat);
1.55      matthew  1360:         }
1.82      matthew  1361:         $rows_output++;
                   1362:         $cols_output=0;
1.55      matthew  1363:     }
1.82      matthew  1364:     ##
                   1365:     ## Resource Statistics
                   1366:     ##
1.55      matthew  1367:     $rows_output++;
                   1368:     $cols_output=0;
1.82      matthew  1369:     &write_headers($excel_sheet,$format,\$rows_output,\$cols_output,
                   1370:                    \@Fields);
                   1371:     #
1.73      matthew  1372:     foreach my $data (@StatsArray) {
                   1373:         $cols_output=0;
                   1374:         foreach my $field (@Fields) {
1.76      matthew  1375:             next if ($field->{'selected'} ne 'yes');
1.73      matthew  1376:             next if ($field->{'name'} eq 'problem_num');
1.85      matthew  1377:             my $fieldformat = undef;
                   1378:             if (exists($field->{'excel_format'})) {
                   1379:                 $fieldformat = $format->{$field->{'excel_format'}};
                   1380:             }
1.73      matthew  1381:             $excel_sheet->write($rows_output,$cols_output++,
1.85      matthew  1382:                                 $data->{$field->{'name'}},$fieldformat);
1.44      matthew  1383:         }
1.73      matthew  1384:         $rows_output++;
1.82      matthew  1385:         $cols_output=0;
1.44      matthew  1386:     }
                   1387:     #
                   1388:     $excel_workbook->close();
1.73      matthew  1389:     #
1.44      matthew  1390:     # Tell the user where to get their excel file
                   1391:     $r->print('<br />'.
1.59      matthew  1392:               '<a href="'.$filename.'">'.
                   1393:               &mt('Your Excel Spreadsheet').'</a>'."\n");
1.44      matthew  1394:     $r->rflush();
                   1395:     return;
                   1396: }
                   1397: 
1.82      matthew  1398: ##
                   1399: ## &write_headers
                   1400: ##
                   1401: sub write_headers {
                   1402:     my ($excel_sheet,$format,$rows_output,$cols_output,$Fields) = @_;
                   1403:     ##
                   1404:     ## First the long titles
                   1405:     foreach my $field (@{$Fields}) {
                   1406:         next if ($field->{'name'} eq 'problem_num');
                   1407:         next if ($field->{'selected'} ne 'yes');
                   1408:         if (exists($field->{'long_title'})) {
                   1409:             $excel_sheet->write($$rows_output,${$cols_output},
                   1410:                                 $field->{'long_title'},
                   1411:                                 $format->{'bold'});
                   1412:         } else {
                   1413:             $excel_sheet->write($$rows_output,${$cols_output},'');
                   1414:         }
                   1415:         ${$cols_output}+= 1;
                   1416:     }
                   1417:     ${$cols_output} =0;
                   1418:     ${$rows_output}+=1;
                   1419:     ##
                   1420:     ## Then the short titles
                   1421:     foreach my $field (@{$Fields}) {
                   1422:         next if ($field->{'selected'} ne 'yes');
                   1423:         next if ($field->{'name'} eq 'problem_num');
                   1424:         # Use english for excel as I am not sure how well excel handles 
                   1425:         # other character sets....
                   1426:         $excel_sheet->write($$rows_output,$$cols_output,
                   1427:                             $field->{'title'},
                   1428:                             $format->{'bold'});
                   1429:         $$cols_output+=1;
                   1430:     }
                   1431:     ${$cols_output} =0;
                   1432:     ${$rows_output}+=1;
                   1433:     return;
                   1434: }
                   1435: 
1.73      matthew  1436: ##################################################
                   1437: ##################################################
                   1438: ##
                   1439: ## Statistics Gathering and Manipulation Routines
                   1440: ##
                   1441: ##################################################
                   1442: ##################################################
                   1443: sub compute_statistics_on_sequence {
                   1444:     my ($seq) = @_;
                   1445:     my @Data;
1.97      matthew  1446:     foreach my $res (&Apache::lonstathelpers::get_resources($navmap,$seq)) {
                   1447:         foreach my $part (@{$res->parts}) {
                   1448:             next if ($res->is_survey($part));
1.73      matthew  1449:             #
                   1450:             # This is where all the work happens
                   1451:             my $data = &get_statistics($seq,$res,$part,scalar(@StatsArray)+1);
                   1452:             push (@Data,$data);
                   1453:             push (@StatsArray,$data);
1.49      matthew  1454:         }
1.26      stredwic 1455:     }
1.73      matthew  1456:     return @Data;
1.41      matthew  1457: }
1.26      stredwic 1458: 
1.73      matthew  1459: sub compute_all_statistics {
                   1460:     my ($r) = @_;
                   1461:     if (@StatsArray > 0) {
                   1462:         # Assume we have already computed the statistics
                   1463:         return;
                   1464:     }
                   1465:     my $c = $r->connection;
1.97      matthew  1466:     foreach my $seq (@sequences) {
1.73      matthew  1467:         last if ($c->aborted);
1.82      matthew  1468:         &compute_sequence_statistics($seq);
1.73      matthew  1469:         &compute_statistics_on_sequence($seq);
1.49      matthew  1470:     }
                   1471: }
                   1472: 
1.73      matthew  1473: sub sort_data {
                   1474:     my ($sortkey) = @_;
                   1475:     return if (! @StatsArray);
1.45      matthew  1476:     #
1.73      matthew  1477:     # Sort the data
                   1478:     my $sortby = undef;
1.49      matthew  1479:     foreach my $field (@Fields) {
1.73      matthew  1480:         if ($sortkey eq $field->{'name'}) {
                   1481:             $sortby = $field->{'name'};
1.45      matthew  1482:         }
1.26      stredwic 1483:     }
1.73      matthew  1484:     if (! defined($sortby) || $sortby eq '' || $sortby eq 'problem_num') {
                   1485:         $sortby = 'container';
                   1486:     }
                   1487:     if ($sortby ne 'container') {
                   1488:         # $sortby is already defined, so we can charge ahead
                   1489:         if ($sortby =~ /^(title|part)$/i) {
                   1490:             # Alpha comparison
                   1491:             @StatsArray = sort {
                   1492:                 lc($a->{$sortby}) cmp lc($b->{$sortby}) ||
                   1493:                 lc($a->{'title'}) cmp lc($b->{'title'}) ||
                   1494:                 lc($a->{'part'}) cmp lc($b->{'part'});
                   1495:             } @StatsArray;
1.24      stredwic 1496:         } else {
1.73      matthew  1497:             # Numerical comparison
                   1498:             @StatsArray = sort {
                   1499:                 my $retvalue = 0;
                   1500:                 if ($b->{$sortby} eq 'nan') {
                   1501:                     if ($a->{$sortby} ne 'nan') {
                   1502:                         $retvalue = -1;
                   1503:                     } else {
                   1504:                         $retvalue = 0;
                   1505:                     }
                   1506:                 }
                   1507:                 if ($a->{$sortby} eq 'nan') {
                   1508:                     if ($b->{$sortby} ne 'nan') {
                   1509:                         $retvalue = 1;
                   1510:                     }
                   1511:                 }
                   1512:                 if ($retvalue eq '0') {
                   1513:                     $retvalue = $b->{$sortby} <=> $a->{$sortby}     ||
                   1514:                             lc($a->{'title'}) <=> lc($b->{'title'}) ||
                   1515:                             lc($a->{'part'})  <=> lc($b->{'part'});
                   1516:                 }
                   1517:                 $retvalue;
                   1518:             } @StatsArray;
1.24      stredwic 1519:         }
                   1520:     }
1.45      matthew  1521:     #
1.73      matthew  1522:     # Renumber the data set
                   1523:     my $count;
                   1524:     foreach my $data (@StatsArray) {
                   1525:         $data->{'problem_num'} = ++$count;
                   1526:     }
1.24      stredwic 1527:     return;
1.48      matthew  1528: }
                   1529: 
1.70      matthew  1530: ########################################################
                   1531: ########################################################
                   1532: 
                   1533: =pod
                   1534: 
                   1535: =item &get_statistics()
                   1536: 
                   1537: Wrapper routine from the call to loncoursedata::get_problem_statistics.  
1.73      matthew  1538: Calls lonstathelpers::get_time_limits() to limit the data set by time
                   1539: and &compute_discrimination_factor
1.70      matthew  1540: 
                   1541: Inputs: $sequence, $resource, $part, $problem_num
                   1542: 
                   1543: Returns: Hash reference with statistics data from 
                   1544: loncoursedata::get_problem_statistics.
                   1545: 
                   1546: =cut
                   1547: 
                   1548: ########################################################
                   1549: ########################################################
1.48      matthew  1550: sub get_statistics {
1.49      matthew  1551:     my ($sequence,$resource,$part,$problem_num) = @_;
1.48      matthew  1552:     #
1.70      matthew  1553:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
1.97      matthew  1554:     my $symb = $resource->symb;
1.104     albertel 1555:     my $courseid = $env{'request.course.id'};
1.48      matthew  1556:     #
1.49      matthew  1557:     my $data = &Apache::loncoursedata::get_problem_statistics
1.102     matthew  1558:                         ([&Apache::lonstatistics::get_selected_sections()],
1.108   ! raeburn  1559:                          [&Apache::lonstatistics::get_selected_groups()],
1.66      matthew  1560:                          $Apache::lonstatistics::enrollment_status,
1.70      matthew  1561:                          $symb,$part,$courseid,$starttime,$endtime);
1.85      matthew  1562:     $data->{'symb'}        = $symb;
1.49      matthew  1563:     $data->{'part'}        = $part;
                   1564:     $data->{'problem_num'} = $problem_num;
1.97      matthew  1565:     $data->{'container'}   = $sequence->compTitle;
                   1566:     $data->{'title'}       = $resource->compTitle;
                   1567:     $data->{'title.link'}  = $resource->src.'?symb='.
                   1568:         &Apache::lonnet::escape($resource->symb);
1.49      matthew  1569:     #
1.76      matthew  1570:     if ($SelectedFields{'deg_of_disc'}) {
                   1571:         $data->{'deg_of_disc'} = 
                   1572:             &compute_discrimination_factor($resource,$part,$sequence);
                   1573:     }
1.83      matthew  1574:     #
                   1575:     # Store in metadata if computations were done for all students
1.84      matthew  1576:     if ($data->{'num_students'} > 1) {
1.102     matthew  1577:         my @Sections = &Apache::lonstatistics::get_selected_sections();
1.84      matthew  1578:         my $sections = '"'.join(' ',@Sections).'"';
                   1579:         $sections =~ s/&+/_/g;  # Ensure no special characters
                   1580:         $data->{'sections'}=$sections;
1.104     albertel 1581:         $data->{'course'} = $env{'request.course.id'};
1.97      matthew  1582:         my $urlres=(&Apache::lonnet::decode_symb($resource->symb))[2];
1.84      matthew  1583:         $data->{'urlres'}=$urlres;
                   1584:         my %storestats = 
                   1585:             &LONCAPA::lonmetadata::dynamic_metadata_storage($data);
1.83      matthew  1586:         my ($dom,$user) = $urlres=~/^(\w+)\/(\w+)/; 
                   1587:         &Apache::lonnet::put('nohist_resevaldata',\%storestats,$dom,$user);
                   1588:     }
1.85      matthew  1589:     #
1.95      matthew  1590:     $data->{'tries_per_correct'} = $data->{'tries'} / 
                   1591:         ($data->{'num_solved'}+0.1);
                   1592:     #
1.85      matthew  1593:     # Get the due date for research purposes (commented out most of the time)
                   1594: #    $data->{'duedate'} = 
                   1595: #        &Apache::lonnet::EXT('resource.'.$part.'.duedate',$symb);
                   1596: #    $data->{'opendate'} = 
                   1597: #        &Apache::lonnet::EXT('resource.'.$part.'.opendate',$symb);
1.94      matthew  1598: #    $data->{'maxtries'} = 
                   1599: #        &Apache::lonnet::EXT('resource.'.$part.'.maxtries',$symb);
                   1600: #    $data->{'hinttries'} =
                   1601: #        &Apache::lonnet::EXT('resource.'.$part.'.hinttries',$symb);
1.98      matthew  1602:     $data->{'weight'} =
                   1603:         &Apache::lonnet::EXT('resource.'.$part.'.weight',$symb);
1.86      matthew  1604: #    $data->{'resptypes'} = join(',',@{$resource->{'partdata'}->{$part}->{'ResponseTypes'}});
1.49      matthew  1605:     return $data;
1.71      matthew  1606: }
                   1607: 
                   1608: ###############################################
                   1609: ###############################################
                   1610: 
                   1611: =pod
                   1612: 
                   1613: =item &compute_discrimination_factor()
                   1614: 
                   1615: Inputs: $Resource, $Sequence
                   1616: 
                   1617: Returns: integer between -1 and 1
                   1618: 
                   1619: =cut
                   1620: 
                   1621: ###############################################
                   1622: ###############################################
                   1623: sub compute_discrimination_factor {
1.97      matthew  1624:     my ($resource,$part,$seq) = @_;
                   1625:     my $symb = $resource->symb;
1.71      matthew  1626:     my @Resources;
1.97      matthew  1627:     foreach my $res (&Apache::lonstathelpers::get_resources($navmap,$seq)){
                   1628:         next if ($res->symb eq $symb);
                   1629:         push (@Resources,$res->symb);
1.71      matthew  1630:     }
                   1631:     #
                   1632:     # rank
1.83      matthew  1633:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
1.71      matthew  1634:     my $ranking = 
                   1635:         &Apache::loncoursedata::rank_students_by_scores_on_resources
                   1636:         (\@Resources,
1.102     matthew  1637:          [&Apache::lonstatistics::get_selected_sections()],
1.108   ! raeburn  1638:          [&Apache::lonstatistics::get_selected_groups()],
1.83      matthew  1639:          $Apache::lonstatistics::enrollment_status,undef,
1.106     bowersj2 1640:          $starttime,$endtime, $symb);
1.71      matthew  1641:     #
                   1642:     # compute their percent scores on the problems in the sequence,
                   1643:     my $number_to_grab = int(scalar(@{$ranking})/4);
                   1644:     my $num_students = scalar(@{$ranking});
                   1645:     my @BottomSet = map { $_->[&Apache::loncoursedata::RNK_student()]; 
                   1646:                       } @{$ranking}[0..$number_to_grab];
                   1647:     my @TopSet    = 
                   1648:         map { 
                   1649:             $_->[&Apache::loncoursedata::RNK_student()]; 
1.106     bowersj2 1650:           } @{$ranking}[-$number_to_grab..0];
1.91      matthew  1651:     if (! @BottomSet || (@BottomSet == 1 && $BottomSet[0] eq '') ||
                   1652:         ! @TopSet    || (@TopSet    == 1 && $TopSet[0]    eq '')) {
                   1653:         return 'nan';
                   1654:     }
1.71      matthew  1655:     my ($bottom_sum,$bottom_max) = 
1.97      matthew  1656:         &Apache::loncoursedata::get_sum_of_scores($symb,$part,\@BottomSet,
1.83      matthew  1657:                                                   undef,$starttime,$endtime);
1.71      matthew  1658:     my ($top_sum,$top_max) = 
1.97      matthew  1659:         &Apache::loncoursedata::get_sum_of_scores($symb,$part,\@TopSet,
1.83      matthew  1660:                                                   undef,$starttime,$endtime);
1.71      matthew  1661:     my $deg_of_disc;
                   1662:     if ($top_max == 0 || $bottom_max==0) {
                   1663:         $deg_of_disc = 'nan';
                   1664:     } else {
                   1665:         $deg_of_disc = ($top_sum/$top_max) - ($bottom_sum/$bottom_max);
                   1666:     }
                   1667:     #&Apache::lonnet::logthis('    '.$top_sum.'/'.$top_max.
                   1668:     #                         ' - '.$bottom_sum.'/'.$bottom_max);
                   1669:     return $deg_of_disc;
1.1       stredwic 1670: }
1.12      minaeibi 1671: 
1.45      matthew  1672: ###############################################
                   1673: ###############################################
1.79      matthew  1674: ##
                   1675: ## Compute KR-21
                   1676: ##
                   1677: ## To compute KR-21, you need the following information:
                   1678: ##
                   1679: ## K=the number of items in your test
                   1680: ## M=the mean score on the test
                   1681: ## s=the standard deviation of the scores on your test 
                   1682: ##
                   1683: ## then:
                   1684: ## 
                   1685: ## KR-21 rk= [K/(K-1)] * [1- (M*(K-M))/(K*s^2))]
                   1686: ##
                   1687: ###############################################
                   1688: ###############################################
                   1689: sub compute_sequence_statistics {
                   1690:     my ($seq) = @_;
1.97      matthew  1691:     my $symb = $seq->symb;
1.79      matthew  1692:     my @Resources;
1.97      matthew  1693:     my $part_count;
                   1694:     foreach my $res (&Apache::lonstathelpers::get_resources($navmap,$seq)) {
                   1695:         push (@Resources,$res->symb);
                   1696:         $part_count += scalar(@{$res->parts});
1.79      matthew  1697:     }
                   1698:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
                   1699:     #
                   1700:     # First compute statistics based on student scores
                   1701:     my ($smin,$smax,$sMean,$sSTD,$scount,$sMAX) = 
                   1702:         &Apache::loncoursedata::score_stats
1.102     matthew  1703:                     ([&Apache::lonstatistics::get_selected_sections()],
1.108   ! raeburn  1704:                      [&Apache::lonstatistics::get_selected_groups()],
1.79      matthew  1705:                      $Apache::lonstatistics::enrollment_status,
                   1706:                      \@Resources,$starttime,$endtime,undef);
1.97      matthew  1707:     $SeqStat{$symb}->{'title'}  = $seq->compTitle;
1.79      matthew  1708:     $SeqStat{$symb}->{'scoremax'}  = $smax;
                   1709:     $SeqStat{$symb}->{'scoremin'}  = $smin;
                   1710:     $SeqStat{$symb}->{'scoremean'} = $sMean;
                   1711:     $SeqStat{$symb}->{'scorestd'}  = $sSTD;
                   1712:     $SeqStat{$symb}->{'scorecount'} = $scount;
                   1713:     $SeqStat{$symb}->{'max_possible'} = $sMAX;
                   1714:     #
                   1715:     # Compute statistics based on the number of correct problems
                   1716:     # 'correct' is taken to mean 
                   1717:     my ($cmin,$cmax,$cMean,$cSTD,$ccount)=
                   1718:         &Apache::loncoursedata::count_stats
1.102     matthew  1719:         ([&Apache::lonstatistics::get_selected_sections()],
1.108   ! raeburn  1720:          [&Apache::lonstatistics::get_selected_groups()],
1.79      matthew  1721:          $Apache::lonstatistics::enrollment_status,
                   1722:          \@Resources,$starttime,$endtime,undef);
1.97      matthew  1723:     my $K = $part_count;
1.79      matthew  1724:     my $kr_21;
                   1725:     if ($K > 1 && $cSTD > 0) {
                   1726:         $kr_21 =  ($K/($K-1)) * (1 - $cMean*($K-$cMean)/($K*$cSTD**2));
                   1727:     } else {
                   1728:         $kr_21 = 'nan';
                   1729:     }
                   1730:     $SeqStat{$symb}->{'countmax'} = $cmax;
                   1731:     $SeqStat{$symb}->{'countmin'} = $cmin;
                   1732:     $SeqStat{$symb}->{'countstd'} = $cSTD;
1.82      matthew  1733:     $SeqStat{$symb}->{'countmean'} = $cMean;
1.79      matthew  1734:     $SeqStat{$symb}->{'count'} = $ccount;
                   1735:     $SeqStat{$symb}->{'items'} = $K;
                   1736:     $SeqStat{$symb}->{'KR-21'}=$kr_21;
                   1737:     return;
                   1738: }
                   1739: 
                   1740: 
1.47      matthew  1741: 
                   1742: =pod 
                   1743: 
1.73      matthew  1744: =item ProblemStatisticsLegend
                   1745: 
                   1746: =over 4
                   1747: 
                   1748: =item #Stdnts
                   1749: Total number of students attempted the problem.
                   1750: 
                   1751: =item Tries
                   1752: Total number of tries for solving the problem.
1.59      matthew  1753: 
1.73      matthew  1754: =item Max Tries
                   1755: Largest number of tries for solving the problem by a student.
                   1756: 
                   1757: =item Mean
                   1758: Average number of tries. [ Tries / #Stdnts ]
                   1759: 
                   1760: =item #YES
                   1761: Number of students solved the problem correctly.
                   1762: 
                   1763: =item #yes
                   1764: Number of students solved the problem by override.
                   1765: 
                   1766: =item %Wrong
                   1767: Percentage of students who tried to solve the problem 
                   1768: but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]
                   1769: 
                   1770: =item DoDiff
                   1771: Degree of Difficulty of the problem.  
                   1772: [ 1 - ((#YES+#yes) / Tries) ]
                   1773: 
                   1774: =item S.D.
                   1775: Standard Deviation of the tries.  
                   1776: [ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1) 
                   1777: where Xi denotes every student\'s tries ]
                   1778: 
                   1779: =item Skew.
                   1780: Skewness of the students tries.
                   1781: [(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]
                   1782: 
                   1783: =item Dis.F.
                   1784: Discrimination Factor: A Standard for evaluating the 
                   1785: problem according to a Criterion<br>
                   1786: 
                   1787: =item [Criterion to group students into %27 Upper Students - 
                   1788: and %27 Lower Students]
                   1789: 1st Criterion for Sorting the Students: 
                   1790: Sum of Partial Credit Awarded / Total Number of Tries
                   1791: 2nd Criterion for Sorting the Students: 
                   1792: Total number of Correct Answers / Total Number of Tries
                   1793: 
                   1794: =item Disc.
                   1795: Number of Students had at least one discussion.
                   1796: 
                   1797: =back
1.47      matthew  1798: 
                   1799: =cut
1.73      matthew  1800: 
                   1801: ############################################################
                   1802: ############################################################
1.4       minaeibi 1803: 
1.1       stredwic 1804: 1;
                   1805: __END__

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