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

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

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