Annotation of loncom/interface/loncoursedata.pm, revision 1.81

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: #
1.81    ! matthew     3: # $Id: loncoursedata.pm,v 1.80 2003/06/25 19:25:54 www 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: ###
                     28: 
                     29: =pod
                     30: 
                     31: =head1 NAME
                     32: 
                     33: loncoursedata
                     34: 
                     35: =head1 SYNOPSIS
                     36: 
1.22      stredwic   37: Set of functions that download and process student and course information.
1.1       stredwic   38: 
                     39: =head1 PACKAGES USED
                     40: 
                     41:  Apache::Constants qw(:common :http)
                     42:  Apache::lonnet()
1.22      stredwic   43:  Apache::lonhtmlcommon
1.1       stredwic   44:  HTML::TokeParser
                     45:  GDBM_File
                     46: 
                     47: =cut
                     48: 
                     49: package Apache::loncoursedata;
                     50: 
                     51: use strict;
                     52: use Apache::Constants qw(:common :http);
                     53: use Apache::lonnet();
1.13      stredwic   54: use Apache::lonhtmlcommon;
1.57      matthew    55: use Time::HiRes;
                     56: use Apache::lonmysql;
1.1       stredwic   57: use HTML::TokeParser;
                     58: use GDBM_File;
                     59: 
                     60: =pod
                     61: 
                     62: =head1 DOWNLOAD INFORMATION
                     63: 
1.22      stredwic   64: This section contains all the functions that get data from other servers 
                     65: and/or itself.
1.1       stredwic   66: 
                     67: =cut
                     68: 
1.50      matthew    69: ####################################################
                     70: ####################################################
1.45      matthew    71: 
                     72: =pod
                     73: 
                     74: =item &get_sequence_assessment_data()
                     75: 
                     76: AT THIS TIME THE USE OF THIS FUNCTION IS *NOT* RECOMMENDED
                     77: 
                     78: Use lonnavmaps to build a data structure describing the order and 
                     79: assessment contents of each sequence in the current course.
                     80: 
                     81: The returned structure is a hash reference. 
                     82: 
1.61      matthew    83: { title => 'title',
                     84:   symb  => 'symb',
                     85:   src   => '/s/o/u/r/c/e',
1.45      matthew    86:   type  => (container|assessment),
1.50      matthew    87:   num_assess   => 2,               # only for container
1.45      matthew    88:   parts        => [11,13,15],      # only for assessment
1.50      matthew    89:   response_ids => [12,14,16],      # only for assessment
                     90:   contents     => [........]       # only for container
1.45      matthew    91: }
                     92: 
1.50      matthew    93: $hash->{'contents'} is a reference to an array of hashes of the same structure.
                     94: 
                     95: Also returned are array references to the sequences and assessments contained
                     96: in the course.
1.49      matthew    97: 
1.45      matthew    98: 
                     99: =cut
                    100: 
1.50      matthew   101: ####################################################
                    102: ####################################################
1.45      matthew   103: sub get_sequence_assessment_data {
                    104:     my $fn=$ENV{'request.course.fn'};
                    105:     ##
                    106:     ## use navmaps
1.65      bowersj2  107:     my $navmap = Apache::lonnavmaps::navmap->new($fn.".db",
1.61      matthew   108:                                                  $fn."_parms.db",1,0);
1.45      matthew   109:     if (!defined($navmap)) {
                    110:         return 'Can not open Coursemap';
                    111:     }
1.75      matthew   112:     # We explicity grab the top level map because I am not sure we
                    113:     # are pulling it from the iterator.
                    114:     my $top_level_map = $navmap->getById('0.0');
                    115:     #
1.45      matthew   116:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
1.61      matthew   117:     my $curRes = $iterator->next(); # Top level sequence
1.45      matthew   118:     ##
                    119:     ## Prime the pump 
                    120:     ## 
                    121:     ## We are going to loop until we run out of sequences/pages to explore for
                    122:     ## resources.  This means we have to start out with something to look
                    123:     ## at.
1.76      matthew   124:     my $title = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1.75      matthew   125:     my $symb  = $top_level_map->symb();
                    126:     my $src   = $top_level_map->src();
                    127:     my $randompick = $top_level_map->randompick();
1.45      matthew   128:     #
1.49      matthew   129:     my @Sequences; 
                    130:     my @Assessments;
1.45      matthew   131:     my @Nested_Sequences = ();   # Stack of sequences, keeps track of depth
                    132:     my $top = { title    => $title,
1.52      matthew   133:                 src      => $src,
1.45      matthew   134:                 symb     => $symb,
                    135:                 type     => 'container',
                    136:                 num_assess => 0,
1.53      matthew   137:                 num_assess_parts => 0,
1.75      matthew   138:                 contents   => [], 
                    139:                 randompick => $randompick,
                    140:             };
1.49      matthew   141:     push (@Sequences,$top);
1.45      matthew   142:     push (@Nested_Sequences, $top);
                    143:     #
                    144:     # We need to keep track of which sequences contain homework problems
                    145:     # 
1.78      matthew   146:     my $previous_too;
1.52      matthew   147:     my $previous;
1.45      matthew   148:     while (scalar(@Nested_Sequences)) {
1.78      matthew   149:         $previous_too = $previous;
1.50      matthew   150:         $previous = $curRes;
1.45      matthew   151:         $curRes = $iterator->next();
                    152:         my $currentmap = $Nested_Sequences[-1]; # Last one on the stack
                    153:         if ($curRes == $iterator->BEGIN_MAP()) {
1.78      matthew   154:             if (! ref($previous)) {
                    155:                 $previous = $previous_too;
                    156:             }
                    157:             if (! ref($previous)) {
                    158:                 next;
                    159:             }
1.45      matthew   160:             # get the map itself, instead of BEGIN_MAP
1.51      matthew   161:             $title = $previous->title();
                    162:             $symb  = $previous->symb();
                    163:             $src   = $previous->src();
1.81    ! matthew   164:             # pick up the filename if there is no title available
        !           165:             if (! defined($title) || $title eq '') {
        !           166:                 ($title) = ($src=~/\/([^\/]*)$/);
        !           167:             }
1.75      matthew   168:             $randompick = $previous->randompick();
1.45      matthew   169:             my $newmap = { title    => $title,
                    170:                            src      => $src,
                    171:                            symb     => $symb,
                    172:                            type     => 'container',
                    173:                            num_assess => 0,
1.75      matthew   174:                            randompick => $randompick,
1.45      matthew   175:                            contents   => [],
                    176:                        };
                    177:             push (@{$currentmap->{'contents'}},$newmap); # this is permanent
1.49      matthew   178:             push (@Sequences,$newmap);
1.45      matthew   179:             push (@Nested_Sequences, $newmap); # this is a stack
                    180:             next;
                    181:         }
                    182:         if ($curRes == $iterator->END_MAP()) {
                    183:             pop(@Nested_Sequences);
                    184:             next;
                    185:         }
                    186:         next if (! ref($curRes));
1.50      matthew   187:         next if (! $curRes->is_problem());# && !$curRes->randomout);
1.45      matthew   188:         # Okay, from here on out we only deal with assessments
                    189:         $title = $curRes->title();
                    190:         $symb  = $curRes->symb();
                    191:         $src   = $curRes->src();
                    192:         my $parts = $curRes->parts();
                    193:         my $assessment = { title => $title,
                    194:                            src   => $src,
                    195:                            symb  => $symb,
                    196:                            type  => 'assessment',
1.53      matthew   197:                            parts => $parts,
                    198:                            num_parts => scalar(@$parts),
1.45      matthew   199:                        };
1.49      matthew   200:         push(@Assessments,$assessment);
1.45      matthew   201:         push(@{$currentmap->{'contents'}},$assessment);
                    202:         $currentmap->{'num_assess'}++;
1.53      matthew   203:         $currentmap->{'num_assess_parts'}+= scalar(@$parts);
1.45      matthew   204:     }
1.58      matthew   205:     $navmap->untieHashes();
1.49      matthew   206:     return ($top,\@Sequences,\@Assessments);
1.45      matthew   207: }
1.50      matthew   208: 
1.4       stredwic  209: sub LoadDiscussion {
1.13      stredwic  210:     my ($courseID)=@_;
1.5       minaeibi  211:     my %Discuss=();
                    212:     my %contrib=&Apache::lonnet::dump(
                    213:                 $courseID,
                    214:                 $ENV{'course.'.$courseID.'.domain'},
                    215:                 $ENV{'course.'.$courseID.'.num'});
                    216: 				 
                    217:     #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
                    218: 
1.4       stredwic  219:     foreach my $temp(keys %contrib) {
                    220: 	if ($temp=~/^version/) {
                    221: 	    my $ver=$contrib{$temp};
                    222: 	    my ($dummy,$prb)=split(':',$temp);
                    223: 	    for (my $idx=1; $idx<=$ver; $idx++ ) {
                    224: 		my $name=$contrib{"$idx:$prb:sendername"};
1.5       minaeibi  225: 		$Discuss{"$name:$prb"}=$idx;	
1.4       stredwic  226: 	    }
                    227: 	}
                    228:     }       
1.5       minaeibi  229: 
                    230:     return \%Discuss;
1.1       stredwic  231: }
                    232: 
1.71      matthew   233: ################################################
                    234: ################################################
                    235: 
                    236: =pod
                    237: 
                    238: =item &GetUserName(username,userdomain)
                    239: 
                    240: Returns a hash with the following entries:
                    241:    'firstname', 'middlename', 'lastname', 'generation', and 'fullname'
                    242: 
                    243:    'fullname' is the result of &Apache::loncoursedata::ProcessFullName.
                    244: 
                    245: =cut
                    246: 
                    247: ################################################
                    248: ################################################
                    249: sub GetUserName {
                    250:     my ($username,$userdomain) = @_;
                    251:     $username = $ENV{'user.name'} if (! defined($username));
                    252:     $userdomain = $ENV{'user.domain'} if (! defined($username));
                    253:     my %userenv = &Apache::lonnet::get('environment',
                    254:                            ['firstname','middlename','lastname','generation'],
                    255:                                        $userdomain,$username);
                    256:     $userenv{'fullname'} = &ProcessFullName($userenv{'lastname'},
                    257:                                             $userenv{'generation'},
                    258:                                             $userenv{'firstname'},
                    259:                                             $userenv{'middlename'});
                    260:     return %userenv;
                    261: }
                    262: 
                    263: ################################################
                    264: ################################################
                    265: 
1.1       stredwic  266: =pod
                    267: 
                    268: =item &ProcessFullName()
                    269: 
                    270: Takes lastname, generation, firstname, and middlename (or some partial
                    271: set of this data) and returns the full name version as a string.  Format
                    272: is Lastname generation, firstname middlename or a subset of this.
                    273: 
                    274: =cut
                    275: 
1.71      matthew   276: ################################################
                    277: ################################################
1.1       stredwic  278: sub ProcessFullName {
                    279:     my ($lastname, $generation, $firstname, $middlename)=@_;
                    280:     my $Str = '';
                    281: 
1.34      matthew   282:     # Strip whitespace preceeding & following name components.
                    283:     $lastname   =~ s/(\s+$|^\s+)//g;
                    284:     $generation =~ s/(\s+$|^\s+)//g;
                    285:     $firstname  =~ s/(\s+$|^\s+)//g;
                    286:     $middlename =~ s/(\s+$|^\s+)//g;
                    287: 
1.1       stredwic  288:     if($lastname ne '') {
1.34      matthew   289: 	$Str .= $lastname;
                    290: 	$Str .= ' '.$generation if ($generation ne '');
                    291: 	$Str .= ',';
                    292:         $Str .= ' '.$firstname  if ($firstname ne '');
                    293:         $Str .= ' '.$middlename if ($middlename ne '');
1.1       stredwic  294:     } else {
1.34      matthew   295:         $Str .= $firstname      if ($firstname ne '');
                    296:         $Str .= ' '.$middlename if ($middlename ne '');
                    297:         $Str .= ' '.$generation if ($generation ne '');
1.1       stredwic  298:     }
                    299: 
                    300:     return $Str;
                    301: }
                    302: 
1.46      matthew   303: ################################################
                    304: ################################################
                    305: 
                    306: =pod
                    307: 
1.47      matthew   308: =item &make_into_hash($values);
                    309: 
                    310: Returns a reference to a hash as described by $values.  $values is
                    311: assumed to be the result of 
1.57      matthew   312:     join(':',map {&Apache::lonnet::escape($_)} %orighash);
1.47      matthew   313: 
                    314: This is a helper function for get_current_state.
                    315: 
                    316: =cut
                    317: 
                    318: ################################################
                    319: ################################################
                    320: sub make_into_hash {
                    321:     my $values = shift;
                    322:     my %tmp = map { &Apache::lonnet::unescape($_); }
                    323:                                            split(':',$values);
                    324:     return \%tmp;
                    325: }
                    326: 
                    327: 
                    328: ################################################
                    329: ################################################
                    330: 
                    331: =pod
                    332: 
1.57      matthew   333: =head1 LOCAL DATA CACHING SUBROUTINES
                    334: 
                    335: The local caching is done using MySQL.  There is no fall-back implementation
                    336: if MySQL is not running.
                    337: 
                    338: The programmers interface is to call &get_current_state() or some other
                    339: primary interface subroutine (described below).  The internals of this 
                    340: storage system are documented here.
                    341: 
                    342: There are six tables used to store student performance data (the results of
                    343: a dumpcurrent).  Each of these tables is created in MySQL with a name of
                    344: $courseid_*****, where ***** is 'symb', 'part', or whatever is appropriate 
                    345: for the table.  The tables and their purposes are described below.
                    346: 
                    347: Some notes before we get started.
                    348: 
                    349: Each table must have a PRIMARY KEY, which is a column or set of columns which
                    350: will serve to uniquely identify a row of data.  NULL is not allowed!
                    351: 
                    352: INDEXes work best on integer data.
                    353: 
                    354: JOIN is used to combine data from many tables into one output.
                    355: 
                    356: lonmysql.pm is used for some of the interface, specifically the table creation
                    357: calls.  The inserts are done in bulk by directly calling the database handler.
                    358: The SELECT ... JOIN statement used to retrieve the data does not have an
                    359: interface in lonmysql.pm and I shudder at the thought of writing one.
                    360: 
                    361: =head3 Table Descriptions
                    362: 
                    363: =over 4
                    364: 
                    365: =item $symb_table
                    366: 
                    367: The symb_table has two columns.  The first is a 'symb_id' and the second
                    368: is the text name for the 'symb' (limited to 64k).  The 'symb_id' is generated
                    369: automatically by MySQL so inserts should be done on this table with an
                    370: empty first element.  This table has its PRIMARY KEY on the 'symb_id'.
                    371: 
                    372: =item $part_table
                    373: 
                    374: The part_table has two columns.  The first is a 'part_id' and the second
                    375: is the text name for the 'part' (limited to 100 characters).  The 'part_id' is
                    376: generated automatically by MySQL so inserts should be done on this table with
                    377: an empty first element.  This table has its PRIMARY KEY on the 'part' (100
                    378: characters) and a KEY on 'part_id'.
                    379: 
                    380: =item $student_table
                    381: 
                    382: The student_table has two columns.  The first is a 'student_id' and the second
                    383: is the text description of the 'student' (typically username:domain) (less
                    384: than 100 characters).  The 'student_id' is automatically generated by MySQL.
                    385: The use of the name 'student_id' is loaded, I know, but this ID is used ONLY 
                    386: internally to the MySQL database and is not the same as the students ID 
                    387: (stored in the students environment).  This table has its PRIMARY KEY on the
                    388: 'student' (100 characters).
                    389: 
                    390: =item $updatetime_table
                    391: 
                    392: The updatetime_table has two columns.  The first is 'student' (100 characters,
                    393: typically username:domain).  The second is 'updatetime', which is an unsigned
                    394: integer, NOT a MySQL date.  This table has its PRIMARY KEY on 'student' (100
                    395: characters).
                    396: 
                    397: =item $performance_table
                    398: 
                    399: The performance_table has 9 columns.  The first three are 'symb_id', 
                    400: 'student_id', and 'part_id'.  These comprise the PRIMARY KEY for this table
                    401: and are directly related to the $symb_table, $student_table, and $part_table
                    402: described above.  MySQL does better indexing on numeric items than text,
                    403: so we use these three "index tables".  The remaining columns are
                    404: 'solved', 'tries', 'awarded', 'award', 'awarddetail', and 'timestamp'.
                    405: These are either the MySQL type TINYTEXT or various integers ('tries' and 
                    406: 'timestamp').  This table has KEYs of 'student_id' and 'symb_id'.
                    407: For use of this table, see the functions described below.
                    408: 
                    409: =item $parameters_table
                    410: 
                    411: The parameters_table holds the data that does not fit neatly into the
                    412: performance_table.  The parameters table has four columns: 'symb_id',
                    413: 'student_id', 'parameter', and 'value'.  'symb_id', 'student_id', and
                    414: 'parameter' comprise the PRIMARY KEY for this table.  'parameter' is 
                    415: limited to 255 characters.  'value' is limited to 64k characters.
                    416: 
                    417: =back
                    418: 
                    419: =head3 Important Subroutines
                    420: 
                    421: Here is a brief overview of the subroutines which are likely to be of 
                    422: interest:
                    423: 
                    424: =over 4
                    425: 
                    426: =item &get_current_state(): programmers interface.
                    427: 
                    428: =item &init_dbs(): table creation
                    429: 
                    430: =item &update_student_data(): data storage calls
                    431: 
                    432: =item &get_student_data_from_performance_cache(): data retrieval
                    433: 
                    434: =back
                    435: 
                    436: =head3 Main Documentation
                    437: 
                    438: =over 4
                    439: 
                    440: =cut
                    441: 
                    442: ################################################
                    443: ################################################
                    444: 
                    445: ################################################
                    446: ################################################
                    447: {
                    448: 
                    449: my $current_course ='';
                    450: my $symb_table;
                    451: my $part_table;
                    452: my $student_table;
                    453: my $updatetime_table;
                    454: my $performance_table;
                    455: my $parameters_table;
                    456: 
                    457: ################################################
                    458: ################################################
                    459: 
                    460: =pod
                    461: 
                    462: =item &init_dbs()
                    463: 
                    464: Input: course id
                    465: 
                    466: Output: 0 on success, positive integer on error
                    467: 
                    468: This routine issues the calls to lonmysql to create the tables used to
                    469: store student data.
                    470: 
                    471: =cut
                    472: 
                    473: ################################################
                    474: ################################################
                    475: sub init_dbs {
                    476:     my $courseid = shift;
                    477:     &setup_table_names($courseid);
                    478:     #
1.73      matthew   479:     # Drop any of the existing tables
                    480:     foreach my $table ($symb_table,$part_table,$student_table,
                    481:                        $updatetime_table,$performance_table,
                    482:                        $parameters_table) {
                    483:         &Apache::lonmysql::drop_table($table);
                    484:     }
                    485:     #
1.57      matthew   486:     # Note - changes to this table must be reflected in the code that 
                    487:     # stores the data (calls &Apache::lonmysql::store_row with this table
                    488:     # id
                    489:     my $symb_table_def = {
                    490:         id => $symb_table,
                    491:         permanent => 'no',
                    492:         columns => [{ name => 'symb_id',
                    493:                       type => 'MEDIUMINT UNSIGNED',
                    494:                       restrictions => 'NOT NULL',
                    495:                       auto_inc     => 'yes', },
                    496:                     { name => 'symb',
                    497:                       type => 'MEDIUMTEXT',
                    498:                       restrictions => 'NOT NULL'},
                    499:                     ],
                    500:         'PRIMARY KEY' => ['symb_id'],
                    501:     };
                    502:     #
                    503:     my $part_table_def = {
                    504:         id => $part_table,
                    505:         permanent => 'no',
                    506:         columns => [{ name => 'part_id',
                    507:                       type => 'MEDIUMINT UNSIGNED',
                    508:                       restrictions => 'NOT NULL',
                    509:                       auto_inc     => 'yes', },
                    510:                     { name => 'part',
                    511:                       type => 'VARCHAR(100)',
                    512:                       restrictions => 'NOT NULL'},
                    513:                     ],
                    514:         'PRIMARY KEY' => ['part (100)'],
                    515:         'KEY' => [{ columns => ['part_id']},],
                    516:     };
                    517:     #
                    518:     my $student_table_def = {
                    519:         id => $student_table,
                    520:         permanent => 'no',
                    521:         columns => [{ name => 'student_id',
                    522:                       type => 'MEDIUMINT UNSIGNED',
                    523:                       restrictions => 'NOT NULL',
                    524:                       auto_inc     => 'yes', },
                    525:                     { name => 'student',
                    526:                       type => 'VARCHAR(100)',
                    527:                       restrictions => 'NOT NULL'},
                    528:                     ],
                    529:         'PRIMARY KEY' => ['student (100)'],
                    530:         'KEY' => [{ columns => ['student_id']},],
                    531:     };
                    532:     #
                    533:     my $updatetime_table_def = {
                    534:         id => $updatetime_table,
                    535:         permanent => 'no',
                    536:         columns => [{ name => 'student',
                    537:                       type => 'VARCHAR(100)',
                    538:                       restrictions => 'NOT NULL UNIQUE',},
                    539:                     { name => 'updatetime',
                    540:                       type => 'INT UNSIGNED',
                    541:                       restrictions => 'NOT NULL' },
                    542:                     ],
                    543:         'PRIMARY KEY' => ['student (100)'],
                    544:     };
                    545:     #
                    546:     my $performance_table_def = {
                    547:         id => $performance_table,
                    548:         permanent => 'no',
                    549:         columns => [{ name => 'symb_id',
                    550:                       type => 'MEDIUMINT UNSIGNED',
                    551:                       restrictions => 'NOT NULL'  },
                    552:                     { name => 'student_id',
                    553:                       type => 'MEDIUMINT UNSIGNED',
                    554:                       restrictions => 'NOT NULL'  },
                    555:                     { name => 'part_id',
                    556:                       type => 'MEDIUMINT UNSIGNED',
                    557:                       restrictions => 'NOT NULL' },
1.73      matthew   558:                     { name => 'part',
                    559:                       type => 'VARCHAR(100)',
                    560:                       restrictions => 'NOT NULL'},                    
1.57      matthew   561:                     { name => 'solved',
                    562:                       type => 'TINYTEXT' },
                    563:                     { name => 'tries',
                    564:                       type => 'SMALLINT UNSIGNED' },
                    565:                     { name => 'awarded',
                    566:                       type => 'TINYTEXT' },
                    567:                     { name => 'award',
                    568:                       type => 'TINYTEXT' },
                    569:                     { name => 'awarddetail',
                    570:                       type => 'TINYTEXT' },
                    571:                     { name => 'timestamp',
                    572:                       type => 'INT UNSIGNED'},
                    573:                     ],
                    574:         'PRIMARY KEY' => ['symb_id','student_id','part_id'],
                    575:         'KEY' => [{ columns=>['student_id'] },
                    576:                   { columns=>['symb_id'] },],
                    577:     };
                    578:     #
                    579:     my $parameters_table_def = {
                    580:         id => $parameters_table,
                    581:         permanent => 'no',
                    582:         columns => [{ name => 'symb_id',
                    583:                       type => 'MEDIUMINT UNSIGNED',
                    584:                       restrictions => 'NOT NULL'  },
                    585:                     { name => 'student_id',
                    586:                       type => 'MEDIUMINT UNSIGNED',
                    587:                       restrictions => 'NOT NULL'  },
                    588:                     { name => 'parameter',
                    589:                       type => 'TINYTEXT',
                    590:                       restrictions => 'NOT NULL'  },
                    591:                     { name => 'value',
                    592:                       type => 'MEDIUMTEXT' },
                    593:                     ],
                    594:         'PRIMARY KEY' => ['symb_id','student_id','parameter (255)'],
                    595:     };
                    596:     #
                    597:     # Create the tables
                    598:     my $tableid;
                    599:     $tableid = &Apache::lonmysql::create_table($symb_table_def);
                    600:     if (! defined($tableid)) {
                    601:         &Apache::lonnet::logthis("error creating symb_table: ".
                    602:                                  &Apache::lonmysql::get_error());
                    603:         return 1;
                    604:     }
                    605:     #
                    606:     $tableid = &Apache::lonmysql::create_table($part_table_def);
                    607:     if (! defined($tableid)) {
                    608:         &Apache::lonnet::logthis("error creating part_table: ".
                    609:                                  &Apache::lonmysql::get_error());
                    610:         return 2;
                    611:     }
                    612:     #
                    613:     $tableid = &Apache::lonmysql::create_table($student_table_def);
                    614:     if (! defined($tableid)) {
                    615:         &Apache::lonnet::logthis("error creating student_table: ".
                    616:                                  &Apache::lonmysql::get_error());
                    617:         return 3;
                    618:     }
                    619:     #
                    620:     $tableid = &Apache::lonmysql::create_table($updatetime_table_def);
                    621:     if (! defined($tableid)) {
                    622:         &Apache::lonnet::logthis("error creating updatetime_table: ".
                    623:                                  &Apache::lonmysql::get_error());
                    624:         return 4;
                    625:     }
                    626:     #
                    627:     $tableid = &Apache::lonmysql::create_table($performance_table_def);
                    628:     if (! defined($tableid)) {
                    629:         &Apache::lonnet::logthis("error creating preformance_table: ".
                    630:                                  &Apache::lonmysql::get_error());
                    631:         return 5;
                    632:     }
                    633:     #
                    634:     $tableid = &Apache::lonmysql::create_table($parameters_table_def);
                    635:     if (! defined($tableid)) {
                    636:         &Apache::lonnet::logthis("error creating parameters_table: ".
                    637:                                  &Apache::lonmysql::get_error());
                    638:         return 6;
                    639:     }
                    640:     return 0;
1.70      matthew   641: }
                    642: 
                    643: ################################################
                    644: ################################################
                    645: 
                    646: =pod
                    647: 
                    648: =item &delete_caches()
                    649: 
                    650: =cut
                    651: 
                    652: ################################################
                    653: ################################################
                    654: sub delete_caches {
                    655:     my $courseid = shift;
                    656:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
                    657:     #
                    658:     &setup_table_names($courseid);
                    659:     #
                    660:     my $dbh = &Apache::lonmysql::get_dbh();
                    661:     foreach my $table ($symb_table,$part_table,$student_table,
                    662:                        $updatetime_table,$performance_table,
                    663:                        $parameters_table ){
                    664:         my $command = 'DROP TABLE '.$table.';';
                    665:         $dbh->do($command);
                    666:         if ($dbh->err) {
                    667:             &Apache::lonnet::logthis($command.' resulted in error: '.$dbh->errstr);
                    668:         }
                    669:     }
                    670:     return;
1.57      matthew   671: }
                    672: 
                    673: ################################################
                    674: ################################################
                    675: 
                    676: =pod
                    677: 
                    678: =item &get_part_id()
                    679: 
                    680: Get the MySQL id of a problem part string.
                    681: 
                    682: Input: $part
                    683: 
                    684: Output: undef on error, integer $part_id on success.
                    685: 
                    686: =item &get_part()
                    687: 
                    688: Get the string describing a part from the MySQL id of the problem part.
                    689: 
                    690: Input: $part_id
                    691: 
                    692: Output: undef on error, $part string on success.
                    693: 
                    694: =cut
                    695: 
                    696: ################################################
                    697: ################################################
                    698: 
1.61      matthew   699: my $have_read_part_table = 0;
1.57      matthew   700: my %ids_by_part;
                    701: my %parts_by_id;
                    702: 
                    703: sub get_part_id {
                    704:     my ($part) = @_;
1.61      matthew   705:     $part = 0 if (! defined($part));
                    706:     if (! $have_read_part_table) {
                    707:         my @Result = &Apache::lonmysql::get_rows($part_table);
                    708:         foreach (@Result) {
                    709:             $ids_by_part{$_->[1]}=$_->[0];
                    710:         }
                    711:         $have_read_part_table = 1;
                    712:     }
1.57      matthew   713:     if (! exists($ids_by_part{$part})) {
                    714:         &Apache::lonmysql::store_row($part_table,[undef,$part]);
                    715:         undef(%ids_by_part);
                    716:         my @Result = &Apache::lonmysql::get_rows($part_table);
                    717:         foreach (@Result) {
                    718:             $ids_by_part{$_->[1]}=$_->[0];
                    719:         }
                    720:     }
                    721:     return $ids_by_part{$part} if (exists($ids_by_part{$part}));
                    722:     return undef; # error
                    723: }
                    724: 
                    725: sub get_part {
                    726:     my ($part_id) = @_;
                    727:     if (! exists($parts_by_id{$part_id})  || 
                    728:         ! defined($parts_by_id{$part_id}) ||
                    729:         $parts_by_id{$part_id} eq '') {
                    730:         my @Result = &Apache::lonmysql::get_rows($part_table);
                    731:         foreach (@Result) {
                    732:             $parts_by_id{$_->[0]}=$_->[1];
                    733:         }
                    734:     }
                    735:     return $parts_by_id{$part_id} if(exists($parts_by_id{$part_id}));
                    736:     return undef; # error
                    737: }
                    738: 
                    739: ################################################
                    740: ################################################
                    741: 
                    742: =pod
                    743: 
                    744: =item &get_symb_id()
                    745: 
                    746: Get the MySQL id of a symb.
                    747: 
                    748: Input: $symb
                    749: 
                    750: Output: undef on error, integer $symb_id on success.
                    751: 
                    752: =item &get_symb()
                    753: 
                    754: Get the symb associated with a MySQL symb_id.
                    755: 
                    756: Input: $symb_id
                    757: 
                    758: Output: undef on error, $symb on success.
                    759: 
                    760: =cut
                    761: 
                    762: ################################################
                    763: ################################################
                    764: 
1.61      matthew   765: my $have_read_symb_table = 0;
1.57      matthew   766: my %ids_by_symb;
                    767: my %symbs_by_id;
                    768: 
                    769: sub get_symb_id {
                    770:     my ($symb) = @_;
1.61      matthew   771:     if (! $have_read_symb_table) {
                    772:         my @Result = &Apache::lonmysql::get_rows($symb_table);
                    773:         foreach (@Result) {
                    774:             $ids_by_symb{$_->[1]}=$_->[0];
                    775:         }
                    776:         $have_read_symb_table = 1;
                    777:     }
1.57      matthew   778:     if (! exists($ids_by_symb{$symb})) {
                    779:         &Apache::lonmysql::store_row($symb_table,[undef,$symb]);
                    780:         undef(%ids_by_symb);
                    781:         my @Result = &Apache::lonmysql::get_rows($symb_table);
                    782:         foreach (@Result) {
                    783:             $ids_by_symb{$_->[1]}=$_->[0];
                    784:         }
                    785:     }
                    786:     return $ids_by_symb{$symb} if(exists( $ids_by_symb{$symb}));
                    787:     return undef; # error
                    788: }
                    789: 
                    790: sub get_symb {
                    791:     my ($symb_id) = @_;
                    792:     if (! exists($symbs_by_id{$symb_id})  || 
                    793:         ! defined($symbs_by_id{$symb_id}) ||
                    794:         $symbs_by_id{$symb_id} eq '') {
                    795:         my @Result = &Apache::lonmysql::get_rows($symb_table);
                    796:         foreach (@Result) {
                    797:             $symbs_by_id{$_->[0]}=$_->[1];
                    798:         }
                    799:     }
                    800:     return $symbs_by_id{$symb_id} if(exists( $symbs_by_id{$symb_id}));
                    801:     return undef; # error
                    802: }
                    803: 
                    804: ################################################
                    805: ################################################
                    806: 
                    807: =pod
                    808: 
                    809: =item &get_student_id()
                    810: 
                    811: Get the MySQL id of a student.
                    812: 
                    813: Input: $sname, $dom
                    814: 
                    815: Output: undef on error, integer $student_id on success.
                    816: 
                    817: =item &get_student()
                    818: 
                    819: Get student username:domain associated with the MySQL student_id.
                    820: 
                    821: Input: $student_id
                    822: 
                    823: Output: undef on error, string $student (username:domain) on success.
                    824: 
                    825: =cut
                    826: 
                    827: ################################################
                    828: ################################################
                    829: 
1.61      matthew   830: my $have_read_student_table = 0;
1.57      matthew   831: my %ids_by_student;
                    832: my %students_by_id;
                    833: 
                    834: sub get_student_id {
                    835:     my ($sname,$sdom) = @_;
                    836:     my $student = $sname.':'.$sdom;
1.61      matthew   837:     if (! $have_read_student_table) {
                    838:         my @Result = &Apache::lonmysql::get_rows($student_table);
                    839:         foreach (@Result) {
                    840:             $ids_by_student{$_->[1]}=$_->[0];
                    841:         }
                    842:         $have_read_student_table = 1;
                    843:     }
1.57      matthew   844:     if (! exists($ids_by_student{$student})) {
                    845:         &Apache::lonmysql::store_row($student_table,[undef,$student]);
                    846:         undef(%ids_by_student);
                    847:         my @Result = &Apache::lonmysql::get_rows($student_table);
                    848:         foreach (@Result) {
                    849:             $ids_by_student{$_->[1]}=$_->[0];
                    850:         }
                    851:     }
                    852:     return $ids_by_student{$student} if(exists( $ids_by_student{$student}));
                    853:     return undef; # error
                    854: }
                    855: 
                    856: sub get_student {
                    857:     my ($student_id) = @_;
                    858:     if (! exists($students_by_id{$student_id})  || 
                    859:         ! defined($students_by_id{$student_id}) ||
                    860:         $students_by_id{$student_id} eq '') {
                    861:         my @Result = &Apache::lonmysql::get_rows($student_table);
                    862:         foreach (@Result) {
                    863:             $students_by_id{$_->[0]}=$_->[1];
                    864:         }
                    865:     }
                    866:     return $students_by_id{$student_id} if(exists($students_by_id{$student_id}));
                    867:     return undef; # error
                    868: }
                    869: 
                    870: ################################################
                    871: ################################################
                    872: 
                    873: =pod
                    874: 
                    875: =item &update_student_data()
                    876: 
                    877: Input: $sname, $sdom, $courseid
                    878: 
                    879: Output: $returnstatus, \%student_data
                    880: 
                    881: $returnstatus is a string describing any errors that occured.  'okay' is the
                    882: default.
                    883: \%student_data is the data returned by a call to lonnet::currentdump.
                    884: 
                    885: This subroutine loads a students data using lonnet::currentdump and inserts
                    886: it into the MySQL database.  The inserts are done on two tables, 
                    887: $performance_table and $parameters_table.  $parameters_table holds the data 
                    888: that is not included in $performance_table.  See the description of 
                    889: $performance_table elsewhere in this file.  The INSERT calls are made
                    890: directly by this subroutine, not through lonmysql because we do a 'bulk'
                    891: insert which takes advantage of MySQLs non-SQL compliant INSERT command to 
                    892: insert multiple rows at a time.  If anything has gone wrong during this
                    893: process, $returnstatus is updated with a description of the error and
                    894: \%student_data is returned.  
                    895: 
                    896: Notice we do not insert the data and immediately query it.  This means it
                    897: is possible for there to be data returned this first time that is not 
                    898: available the second time.  CYA.
                    899: 
                    900: =cut
                    901: 
                    902: ################################################
                    903: ################################################
                    904: sub update_student_data {
                    905:     my ($sname,$sdom,$courseid) = @_;
                    906:     #
1.60      matthew   907:     # Set up database names
                    908:     &setup_table_names($courseid);
                    909:     #
1.57      matthew   910:     my $student_id = &get_student_id($sname,$sdom);
                    911:     my $student = $sname.':'.$sdom;
                    912:     #
                    913:     my $returnstatus = 'okay';
                    914:     #
                    915:     # Download students data
                    916:     my $time_of_retrieval = time;
                    917:     my @tmp = &Apache::lonnet::currentdump($courseid,$sdom,$sname);
                    918:     if ((scalar(@tmp) > 0) && ($tmp[0] =~ /^error:/)) {
                    919:         &Apache::lonnet::logthis('error getting data for '.
                    920:                                  $sname.':'.$sdom.' in course '.$courseid.
                    921:                                  ':'.$tmp[0]);
                    922:         $returnstatus = 'error getting data';
1.79      matthew   923:         return ($returnstatus,undef);
1.57      matthew   924:     }
                    925:     if (scalar(@tmp) < 1) {
                    926:         return ('no data',undef);
                    927:     }
                    928:     my %student_data = @tmp;
                    929:     #
                    930:     # Remove all of the students data from the table
1.60      matthew   931:     my $dbh = &Apache::lonmysql::get_dbh();
                    932:     $dbh->do('DELETE FROM '.$performance_table.' WHERE student_id='.
                    933:              $student_id);
                    934:     $dbh->do('DELETE FROM '.$parameters_table.' WHERE student_id='.
                    935:              $student_id);
1.57      matthew   936:     #
                    937:     # Store away the data
                    938:     #
                    939:     my $starttime = Time::HiRes::time;
                    940:     my $elapsed = 0;
                    941:     my $rows_stored;
                    942:     my $store_parameters_command  = 'INSERT INTO '.$parameters_table.
1.60      matthew   943:         ' VALUES '."\n";
1.61      matthew   944:     my $num_parameters = 0;
1.57      matthew   945:     my $store_performance_command = 'INSERT INTO '.$performance_table.
1.60      matthew   946:         ' VALUES '."\n";
1.79      matthew   947:     return ('error',undef) if (! defined($dbh));
1.57      matthew   948:     while (my ($current_symb,$param_hash) = each(%student_data)) {
                    949:         #
                    950:         # make sure the symb is set up properly
                    951:         my $symb_id = &get_symb_id($current_symb);
                    952:         #
                    953:         # Load data into the tables
1.63      matthew   954:         while (my ($parameter,$value) = each(%$param_hash)) {
1.57      matthew   955:             my $newstring;
1.63      matthew   956:             if ($parameter !~ /(timestamp|resource\.(.*)\.(solved|tries|awarded|award|awarddetail|previous))/) {
1.57      matthew   957:                 $newstring = "('".join("','",
                    958:                                        $symb_id,$student_id,
1.69      matthew   959:                                        $parameter)."',".
                    960:                                            $dbh->quote($value)."),\n";
1.61      matthew   961:                 $num_parameters ++;
1.57      matthew   962:                 if ($newstring !~ /''/) {
                    963:                     $store_parameters_command .= $newstring;
                    964:                     $rows_stored++;
                    965:                 }
                    966:             }
                    967:             next if ($parameter !~ /^resource\.(.*)\.solved$/);
                    968:             #
                    969:             my $part = $1;
                    970:             my $part_id = &get_part_id($part);
                    971:             next if (!defined($part_id));
                    972:             my $solved  = $value;
                    973:             my $tries   = $param_hash->{'resource.'.$part.'.tries'};
                    974:             my $awarded = $param_hash->{'resource.'.$part.'.awarded'};
                    975:             my $award   = $param_hash->{'resource.'.$part.'.award'};
                    976:             my $awarddetail = $param_hash->{'resource.'.$part.'.awarddetail'};
                    977:             my $timestamp = $param_hash->{'timestamp'};
1.60      matthew   978:             #
1.74      matthew   979:             $solved      = '' if (! defined($solved));
1.57      matthew   980:             $tries       = '' if (! defined($tries));
                    981:             $awarded     = '' if (! defined($awarded));
                    982:             $award       = '' if (! defined($award));
                    983:             $awarddetail = '' if (! defined($awarddetail));
1.73      matthew   984:             $newstring = "('".join("','",$symb_id,$student_id,$part_id,$part,
1.57      matthew   985:                                    $solved,$tries,$awarded,$award,
1.63      matthew   986:                                    $awarddetail,$timestamp)."'),\n";
1.57      matthew   987:             $store_performance_command .= $newstring;
                    988:             $rows_stored++;
                    989:         }
                    990:     }
                    991:     chop $store_parameters_command;
1.60      matthew   992:     chop $store_parameters_command;
                    993:     chop $store_performance_command;
1.57      matthew   994:     chop $store_performance_command;
                    995:     my $start = Time::HiRes::time;
1.61      matthew   996:     $dbh->do($store_parameters_command) if ($num_parameters>0);
1.57      matthew   997:     if ($dbh->err()) {
                    998:         &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1.61      matthew   999:         &Apache::lonnet::logthis('command = '.$store_parameters_command);
1.57      matthew  1000:         $returnstatus = 'error: unable to insert parameters into database';
1.79      matthew  1001:         return ($returnstatus,\%student_data);
1.57      matthew  1002:     }
                   1003:     $dbh->do($store_performance_command);
                   1004:     if ($dbh->err()) {
                   1005:         &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1.61      matthew  1006:         &Apache::lonnet::logthis('command = '.$store_performance_command);
1.57      matthew  1007:         $returnstatus = 'error: unable to insert performance into database';
1.79      matthew  1008:         return ($returnstatus,\%student_data);
1.57      matthew  1009:     }
                   1010:     $elapsed += Time::HiRes::time - $start;
                   1011:     #
                   1012:     # Set the students update time
                   1013:     &Apache::lonmysql::replace_row($updatetime_table,
                   1014:                                    [$student,$time_of_retrieval]);
                   1015:     return ($returnstatus,\%student_data);
                   1016: }
                   1017: 
                   1018: ################################################
                   1019: ################################################
                   1020: 
                   1021: =pod
                   1022: 
                   1023: =item &ensure_current_data()
                   1024: 
                   1025: Input: $sname, $sdom, $courseid
                   1026: 
                   1027: Output: $status, $data
                   1028: 
                   1029: This routine ensures the data for a given student is up to date.  It calls
                   1030: &init_dbs() if the tables do not exist.  The $updatetime_table is queried
                   1031: to determine the time of the last update.  If the students data is out of
                   1032: date, &update_student_data() is called.  The return values from the call
                   1033: to &update_student_data() are returned.
                   1034: 
                   1035: =cut
                   1036: 
                   1037: ################################################
                   1038: ################################################
                   1039: sub ensure_current_data {
                   1040:     my ($sname,$sdom,$courseid) = @_;
                   1041:     my $status = 'okay';   # return value
                   1042:     #
1.61      matthew  1043:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
                   1044:     # 
                   1045:     # Clean out package variables
1.57      matthew  1046:     &setup_table_names($courseid);
                   1047:     #
                   1048:     # if the tables do not exist, make them
                   1049:     my @CurrentTable = &Apache::lonmysql::tables_in_db();
                   1050:     my ($found_symb,$found_student,$found_part,$found_update,
                   1051:         $found_performance,$found_parameters);
                   1052:     foreach (@CurrentTable) {
                   1053:         $found_symb        = 1 if ($_ eq $symb_table);
                   1054:         $found_student     = 1 if ($_ eq $student_table);
                   1055:         $found_part        = 1 if ($_ eq $part_table);
                   1056:         $found_update      = 1 if ($_ eq $updatetime_table);
                   1057:         $found_performance = 1 if ($_ eq $performance_table);
                   1058:         $found_parameters  = 1 if ($_ eq $parameters_table);
                   1059:     }
                   1060:     if (!$found_symb        || !$found_update || 
                   1061:         !$found_student     || !$found_part   ||
                   1062:         !$found_performance || !$found_parameters) {
                   1063:         if (&init_dbs($courseid)) {
1.79      matthew  1064:             return ('error',undef);
1.57      matthew  1065:         }
                   1066:     }
                   1067:     #
                   1068:     # Get the update time for the user
                   1069:     my $updatetime = 0;
1.60      matthew  1070:     my $modifiedtime = &Apache::lonnet::GetFileTimestamp
                   1071:         ($sdom,$sname,$courseid.'.db',
                   1072:          $Apache::lonnet::perlvar{'lonUsersDir'});
1.57      matthew  1073:     #
                   1074:     my $student = $sname.':'.$sdom;
                   1075:     my @Result = &Apache::lonmysql::get_rows($updatetime_table,
                   1076:                                              "student ='$student'");
                   1077:     my $data = undef;
                   1078:     if (@Result) {
                   1079:         $updatetime = $Result[0]->[1];
                   1080:     }
                   1081:     if ($modifiedtime > $updatetime) {
                   1082:         ($status,$data) = &update_student_data($sname,$sdom,$courseid);
                   1083:     }
                   1084:     return ($status,$data);
                   1085: }
                   1086: 
                   1087: ################################################
                   1088: ################################################
                   1089: 
                   1090: =pod
                   1091: 
                   1092: =item &get_student_data_from_performance_cache()
                   1093: 
                   1094: Input: $sname, $sdom, $symb, $courseid
                   1095: 
                   1096: Output: hash reference containing the data for the given student.
                   1097: If $symb is undef, all the students data is returned.
                   1098: 
                   1099: This routine is the heart of the local caching system.  See the description
                   1100: of $performance_table, $symb_table, $student_table, and $part_table.  The
                   1101: main task is building the MySQL request.  The tables appear in the request
                   1102: in the order in which they should be parsed by MySQL.  When searching
                   1103: on a student the $student_table is used to locate the 'student_id'.  All
                   1104: rows in $performance_table which have a matching 'student_id' are returned,
                   1105: with data from $part_table and $symb_table which match the entries in
                   1106: $performance_table, 'part_id' and 'symb_id'.  When searching on a symb,
                   1107: the $symb_table is processed first, with matching rows grabbed from 
                   1108: $performance_table and filled in from $part_table and $student_table in
                   1109: that order.  
                   1110: 
                   1111: Running 'EXPLAIN ' on the 'SELECT' statements generated can be quite 
                   1112: interesting, especially if you play with the order the tables are listed.  
                   1113: 
                   1114: =cut
                   1115: 
                   1116: ################################################
                   1117: ################################################
                   1118: sub get_student_data_from_performance_cache {
                   1119:     my ($sname,$sdom,$symb,$courseid)=@_;
                   1120:     my $student = $sname.':'.$sdom if (defined($sname) && defined($sdom));
1.61      matthew  1121:     &setup_table_names($courseid);
1.57      matthew  1122:     #
                   1123:     # Return hash
                   1124:     my $studentdata;
                   1125:     #
                   1126:     my $dbh = &Apache::lonmysql::get_dbh();
                   1127:     my $request = "SELECT ".
1.73      matthew  1128:         "d.symb,a.part,a.solved,a.tries,a.awarded,a.award,a.awarddetail,".
1.63      matthew  1129:             "a.timestamp ";
1.57      matthew  1130:     if (defined($student)) {
                   1131:         $request .= "FROM $student_table AS b ".
                   1132:             "LEFT JOIN $performance_table AS a ON b.student_id=a.student_id ".
1.73      matthew  1133: #            "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ".
1.57      matthew  1134:             "LEFT JOIN $symb_table AS d ON d.symb_id = a.symb_id ".
                   1135:                 "WHERE student='$student'";
                   1136:         if (defined($symb) && $symb ne '') {
1.67      matthew  1137:             $request .= " AND d.symb=".$dbh->quote($symb);
1.57      matthew  1138:         }
                   1139:     } elsif (defined($symb) && $symb ne '') {
                   1140:         $request .= "FROM $symb_table as d ".
                   1141:             "LEFT JOIN $performance_table AS a ON d.symb_id=a.symb_id ".
1.73      matthew  1142: #            "LEFT JOIN $part_table    AS c ON c.part_id = a.part_id ".
1.57      matthew  1143:             "LEFT JOIN $student_table AS b ON b.student_id = a.student_id ".
                   1144:                 "WHERE symb='".$dbh->quote($symb)."'";
                   1145:     }
                   1146:     my $starttime = Time::HiRes::time;
                   1147:     my $rows_retrieved = 0;
                   1148:     my $sth = $dbh->prepare($request);
                   1149:     $sth->execute();
                   1150:     if ($sth->err()) {
                   1151:         &Apache::lonnet::logthis("Unable to execute MySQL request:");
                   1152:         &Apache::lonnet::logthis("\n".$request."\n");
                   1153:         &Apache::lonnet::logthis("error is:".$sth->errstr());
                   1154:         return undef;
                   1155:     }
                   1156:     foreach my $row (@{$sth->fetchall_arrayref}) {
                   1157:         $rows_retrieved++;
1.63      matthew  1158:         my ($symb,$part,$solved,$tries,$awarded,$award,$awarddetail,$time) = 
1.57      matthew  1159:             (@$row);
                   1160:         my $base = 'resource.'.$part;
                   1161:         $studentdata->{$symb}->{$base.'.solved'}  = $solved;
                   1162:         $studentdata->{$symb}->{$base.'.tries'}   = $tries;
                   1163:         $studentdata->{$symb}->{$base.'.awarded'} = $awarded;
                   1164:         $studentdata->{$symb}->{$base.'.award'}   = $award;
                   1165:         $studentdata->{$symb}->{$base.'.awarddetail'} = $awarddetail;
                   1166:         $studentdata->{$symb}->{'timestamp'} = $time if (defined($time) && $time ne '');
1.67      matthew  1167:     }
                   1168:     if (defined($symb) && $symb ne '') {
                   1169:         $studentdata = $studentdata->{$symb};
1.57      matthew  1170:     }
                   1171:     return $studentdata;
                   1172: }
                   1173: 
                   1174: ################################################
                   1175: ################################################
                   1176: 
                   1177: =pod
                   1178: 
                   1179: =item &get_current_state()
                   1180: 
                   1181: Input: $sname,$sdom,$symb,$courseid
                   1182: 
                   1183: Output: Described below
1.46      matthew  1184: 
1.47      matthew  1185: Retrieve the current status of a students performance.  $sname and
1.46      matthew  1186: $sdom are the only required parameters.  If $symb is undef the results
1.47      matthew  1187: of an &Apache::lonnet::currentdump() will be returned.  
1.46      matthew  1188: If $courseid is undef it will be retrieved from the environment.
                   1189: 
                   1190: The return structure is based on &Apache::lonnet::currentdump.  If
                   1191: $symb is unspecified, all the students data is returned in a hash of
                   1192: the form:
                   1193: ( 
                   1194:   symb1 => { param1 => value1, param2 => value2 ... },
                   1195:   symb2 => { param1 => value1, param2 => value2 ... },
                   1196: )
                   1197: 
                   1198: If $symb is specified, a hash of 
                   1199: (
                   1200:   param1 => value1, 
                   1201:   param2 => value2,
                   1202: )
                   1203: is returned.
                   1204: 
1.57      matthew  1205: If no data is found for $symb, or if the student has no performance data,
1.46      matthew  1206: an empty list is returned.
                   1207: 
                   1208: =cut
                   1209: 
                   1210: ################################################
                   1211: ################################################
                   1212: sub get_current_state {
1.47      matthew  1213:     my ($sname,$sdom,$symb,$courseid,$forcedownload)=@_;
                   1214:     #
1.46      matthew  1215:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1.47      matthew  1216:     #
1.61      matthew  1217:     return () if (! defined($sname) || ! defined($sdom));
                   1218:     #
1.57      matthew  1219:     my ($status,$data) = &ensure_current_data($sname,$sdom,$courseid);
1.77      matthew  1220: #    &Apache::lonnet::logthis
                   1221: #        ('sname = '.$sname.
                   1222: #         ' domain = '.$sdom.
                   1223: #         ' status = '.$status.
                   1224: #         ' data is '.(defined($data)?'defined':'undefined'));
1.73      matthew  1225: #    while (my ($symb,$hash) = each(%$data)) {
                   1226: #        &Apache::lonnet::logthis($symb."\n----------------------------------");
                   1227: #        while (my ($key,$value) = each (%$hash)) {
                   1228: #            &Apache::lonnet::logthis("   ".$key." = ".$value);
                   1229: #        }
                   1230: #    }
1.47      matthew  1231:     #
1.79      matthew  1232:     if (defined($data) && defined($symb) && ref($data->{$symb})) {
                   1233:         return %{$data->{$symb}};
                   1234:     } elsif (defined($data) && ! defined($symb) && ref($data)) {
                   1235:         return %$data;
                   1236:     } 
                   1237:     if ($status eq 'no data') {
1.57      matthew  1238:         return ();
                   1239:     } else {
                   1240:         if ($status ne 'okay' && $status ne '') {
                   1241:             &Apache::lonnet::logthis('status = '.$status);
1.47      matthew  1242:             return ();
                   1243:         }
1.57      matthew  1244:         my $returnhash = &get_student_data_from_performance_cache($sname,$sdom,
                   1245:                                                       $symb,$courseid);
                   1246:         return %$returnhash if (defined($returnhash));
1.46      matthew  1247:     }
1.57      matthew  1248:     return ();
1.61      matthew  1249: }
                   1250: 
                   1251: ################################################
                   1252: ################################################
                   1253: 
                   1254: =pod
                   1255: 
                   1256: =item &get_problem_statistics()
                   1257: 
                   1258: Gather data on a given problem.  The database is assumed to be 
                   1259: populated and all local caching variables are assumed to be set
                   1260: properly.  This means you need to call &ensure_current_data for
                   1261: the students you are concerned with prior to calling this routine.
                   1262: 
                   1263: Inputs: $students, $symb, $part, $courseid
                   1264: 
1.64      matthew  1265: =over 4
                   1266: 
                   1267: =item $students is an array of hash references.  
                   1268: Each hash must contain at least the 'username' and 'domain' of a student.
                   1269: 
                   1270: =item $symb is the symb for the problem.
                   1271: 
                   1272: =item $part is the part id you need statistics for
                   1273: 
                   1274: =item $courseid is the course id, of course!
                   1275: 
                   1276: =back
                   1277: 
1.66      matthew  1278: Outputs: See the code for up to date information.  A hash reference is
                   1279: returned.  The hash has the following keys defined:
1.64      matthew  1280: 
                   1281: =over 4
                   1282: 
1.66      matthew  1283: =item num_students The number of students attempting the problem
                   1284:       
                   1285: =item tries The total number of tries for the students
                   1286:       
                   1287: =item max_tries The maximum number of tries taken
                   1288:       
                   1289: =item mean_tries The average number of tries
                   1290:       
                   1291: =item num_solved The number of students able to solve the problem
                   1292:       
                   1293: =item num_override The number of students whose answer is 'correct_by_override'
                   1294:       
                   1295: =item deg_of_diff The degree of difficulty of the problem
                   1296:       
                   1297: =item std_tries The standard deviation of the number of tries
                   1298:       
                   1299: =item skew_tries The skew of the number of tries
1.64      matthew  1300: 
1.66      matthew  1301: =item per_wrong The number of students attempting the problem who were not
                   1302: able to answer it correctly.
1.64      matthew  1303: 
                   1304: =back
                   1305: 
1.61      matthew  1306: =cut
                   1307: 
                   1308: ################################################
                   1309: ################################################
                   1310: sub get_problem_statistics {
                   1311:     my ($students,$symb,$part,$courseid) = @_;
                   1312:     return if (! defined($symb) || ! defined($part));
                   1313:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
                   1314:     #
                   1315:     my $symb_id = &get_symb_id($symb);
                   1316:     my $part_id = &get_part_id($part);
                   1317:     my $stats_table = $courseid.'_problem_stats';
                   1318:     #
                   1319:     my $dbh = &Apache::lonmysql::get_dbh();
                   1320:     return undef if (! defined($dbh));
                   1321:     #
                   1322:     # A) Number of Students attempting problem
                   1323:     # B) Total number of tries of students attempting problem
                   1324:     # C) Mod (largest number of tries for solving the problem)
                   1325:     # D) Mean (average number of tries for solving the problem)
                   1326:     # E) Number of students to solve the problem
                   1327:     # F) Number of students to solve the problem by override
                   1328:     # G) Number of students unable to solve the problem
                   1329:     # H) Degree of difficulty : 1-(E+F)/B
                   1330:     # I) Standard deviation of number of tries
                   1331:     # J) Skew of tries: sqrt(sum(Xi-D)^3)/A
                   1332:     #
                   1333:     $dbh->do('DROP TABLE '.$stats_table);  # May return an error
                   1334:     my $request = 
                   1335:         'CREATE TEMPORARY TABLE '.$stats_table.
                   1336:             ' SELECT student_id,solved,award,tries FROM '.$performance_table.
                   1337:                 ' WHERE symb_id='.$symb_id.' AND part_id='.$part_id;
1.64      matthew  1338:     if (defined($students)) {
                   1339:         $request .= ' AND ('.
                   1340:             join(' OR ', map {'student_id='.
                   1341:                                   &get_student_id($_->{'username'},
                   1342:                                                   $_->{'domain'})
                   1343:                                   } @$students
                   1344:                  ).')';
                   1345:     }
1.61      matthew  1346: #    &Apache::lonnet::logthis($request);
                   1347:     $dbh->do($request);
                   1348:     my ($num,$tries,$mod,$mean,$STD) = &execute_SQL_request
                   1349:         ($dbh,
                   1350:          'SELECT COUNT(*),SUM(tries),MAX(tries),AVG(tries),STD(tries) FROM '.
                   1351:          $stats_table);
                   1352:     my ($Solved) = &execute_SQL_request($dbh,'SELECT COUNT(tries) FROM '.
                   1353:                                         $stats_table.
                   1354:                                         " WHERE solved='correct_by_student'");
                   1355:     my ($solved) = &execute_SQL_request($dbh,'SELECT COUNT(tries) FROM '.
                   1356:                                         $stats_table.
                   1357:                                         " WHERE solved='correct_by_override'");
                   1358:     $num    = 0 if (! defined($num));
                   1359:     $tries  = 0 if (! defined($tries));
                   1360:     $mod    = 0 if (! defined($mod));
                   1361:     $STD    = 0 if (! defined($STD));
                   1362:     $Solved = 0 if (! defined($Solved));
                   1363:     $solved = 0 if (! defined($solved));
                   1364:     #
                   1365:     my $DegOfDiff = 'nan';
1.66      matthew  1366:     $DegOfDiff = 1-($Solved)/$tries if ($tries>0);
1.61      matthew  1367: 
                   1368:     my $SKEW = 'nan';
1.66      matthew  1369:     my $wrongpercent = 0;
1.61      matthew  1370:     if ($num > 0) {
                   1371:         ($SKEW) = &execute_SQL_request($dbh,'SELECT SQRT(SUM('.
                   1372:                                      'POWER(tries - '.$STD.',3)'.
                   1373:                                      '))/'.$num.' FROM '.$stats_table);
1.66      matthew  1374:         $wrongpercent=int(10*100*($num-$Solved+$solved)/$num)/10;
1.61      matthew  1375:     }
                   1376:     #
                   1377:     $dbh->do('DROP TABLE '.$stats_table);  # May return an error
1.81    ! matthew  1378:     #
        !          1379:     # Store in metadata
        !          1380:     #
1.80      www      1381:     if ($num) {
                   1382: 	my %storestats=();
                   1383: 
                   1384:         my $urlres=(split(/\_\_\_/,$symb))[2];
                   1385: 
                   1386: 	$storestats{$courseid.'___'.$urlres.'___timestamp'}=time;       
                   1387: 	$storestats{$courseid.'___'.$urlres.'___stdno'}=$num;
                   1388: 	$storestats{$courseid.'___'.$urlres.'___avetries'}=$mean;	   
                   1389: 	$storestats{$courseid.'___'.$urlres.'___difficulty'}=$DegOfDiff;
                   1390: 
                   1391: 	$urlres=~/^(\w+)\/(\w+)/; 
                   1392: 	&Apache::lonnet::put('nohist_resevaldata',\%storestats,$1,$2); 
                   1393:     }
1.81    ! matthew  1394:     #
        !          1395:     # Return result
        !          1396:     #
1.66      matthew  1397:     return { num_students => $num,
                   1398:              tries        => $tries,
                   1399:              max_tries    => $mod,
                   1400:              mean_tries   => $mean,
                   1401:              std_tries    => $STD,
                   1402:              skew_tries   => $SKEW,
                   1403:              num_solved   => $Solved,
                   1404:              num_override => $solved,
                   1405:              per_wrong    => $wrongpercent,
1.81    ! matthew  1406:              deg_of_diff  => $DegOfDiff };
1.61      matthew  1407: }
                   1408: 
                   1409: sub execute_SQL_request {
                   1410:     my ($dbh,$request)=@_;
                   1411: #    &Apache::lonnet::logthis($request);
                   1412:     my $sth = $dbh->prepare($request);
                   1413:     $sth->execute();
                   1414:     my $row = $sth->fetchrow_arrayref();
                   1415:     if (ref($row) eq 'ARRAY' && scalar(@$row)>0) {
                   1416:         return @$row;
                   1417:     }
                   1418:     return ();
                   1419: }
                   1420: 
                   1421: 
                   1422: ################################################
                   1423: ################################################
                   1424: 
                   1425: =pod
                   1426: 
                   1427: =item &setup_table_names()
                   1428: 
                   1429: input: course id
                   1430: 
                   1431: output: none
                   1432: 
                   1433: Cleans up the package variables for local caching.
                   1434: 
                   1435: =cut
                   1436: 
                   1437: ################################################
                   1438: ################################################
                   1439: sub setup_table_names {
                   1440:     my ($courseid) = @_;
                   1441:     if (! defined($courseid)) {
                   1442:         $courseid = $ENV{'request.course.id'};
                   1443:     }
                   1444:     #
                   1445:     if (! defined($current_course) || $current_course ne $courseid) {
                   1446:         # Clear out variables
                   1447:         $have_read_part_table = 0;
                   1448:         undef(%ids_by_part);
                   1449:         undef(%parts_by_id);
                   1450:         $have_read_symb_table = 0;
                   1451:         undef(%ids_by_symb);
                   1452:         undef(%symbs_by_id);
                   1453:         $have_read_student_table = 0;
                   1454:         undef(%ids_by_student);
                   1455:         undef(%students_by_id);
                   1456:         #
                   1457:         $current_course = $courseid;
                   1458:     }
                   1459:     #
                   1460:     # Set up database names
                   1461:     my $base_id = $courseid;
                   1462:     $symb_table        = $base_id.'_'.'symb';
                   1463:     $part_table        = $base_id.'_'.'part';
                   1464:     $student_table     = $base_id.'_'.'student';
                   1465:     $updatetime_table  = $base_id.'_'.'updatetime';
                   1466:     $performance_table = $base_id.'_'.'performance';
                   1467:     $parameters_table  = $base_id.'_'.'parameters';
                   1468:     return;
1.3       stredwic 1469: }
1.1       stredwic 1470: 
1.35      matthew  1471: ################################################
                   1472: ################################################
                   1473: 
                   1474: =pod
                   1475: 
1.57      matthew  1476: =back
                   1477: 
                   1478: =item End of Local Data Caching Subroutines
                   1479: 
                   1480: =cut
                   1481: 
                   1482: ################################################
                   1483: ################################################
                   1484: 
                   1485: 
                   1486: }
                   1487: ################################################
                   1488: ################################################
                   1489: 
                   1490: =pod
                   1491: 
                   1492: =head3 Classlist Subroutines
                   1493: 
1.35      matthew  1494: =item &get_classlist();
                   1495: 
                   1496: Retrieve the classist of a given class or of the current class.  Student
                   1497: information is returned from the classlist.db file and, if needed,
                   1498: from the students environment.
                   1499: 
                   1500: Optional arguments are $cid, $cdom, and $cnum (course id, course domain,
                   1501: and course number, respectively).  Any omitted arguments will be taken 
                   1502: from the current environment ($ENV{'request.course.id'},
                   1503: $ENV{'course.'.$cid.'.domain'}, and $ENV{'course.'.$cid.'.num'}).
                   1504: 
                   1505: Returns a reference to a hash which contains:
                   1506:  keys    '$sname:$sdom'
1.54      bowersj2 1507:  values  [$sdom,$sname,$end,$start,$id,$section,$fullname,$status]
                   1508: 
                   1509: The constant values CL_SDOM, CL_SNAME, CL_END, etc. can be used
                   1510: as indices into the returned list to future-proof clients against
                   1511: changes in the list order.
1.35      matthew  1512: 
                   1513: =cut
                   1514: 
                   1515: ################################################
                   1516: ################################################
1.54      bowersj2 1517: 
                   1518: sub CL_SDOM     { return 0; }
                   1519: sub CL_SNAME    { return 1; }
                   1520: sub CL_END      { return 2; }
                   1521: sub CL_START    { return 3; }
                   1522: sub CL_ID       { return 4; }
                   1523: sub CL_SECTION  { return 5; }
                   1524: sub CL_FULLNAME { return 6; }
                   1525: sub CL_STATUS   { return 7; }
1.35      matthew  1526: 
                   1527: sub get_classlist {
                   1528:     my ($cid,$cdom,$cnum) = @_;
                   1529:     $cid = $cid || $ENV{'request.course.id'};
                   1530:     $cdom = $cdom || $ENV{'course.'.$cid.'.domain'};
                   1531:     $cnum = $cnum || $ENV{'course.'.$cid.'.num'};
1.57      matthew  1532:     my $now = time;
1.35      matthew  1533:     #
                   1534:     my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
                   1535:     while (my ($student,$info) = each(%classlist)) {
1.60      matthew  1536:         if ($student =~ /^(con_lost|error|no_such_host)/i) {
                   1537:             &Apache::lonnet::logthis('get_classlist error for '.$cid.':'.$student);
                   1538:             return undef;
                   1539:         }
1.35      matthew  1540:         my ($sname,$sdom) = split(/:/,$student);
                   1541:         my @Values = split(/:/,$info);
                   1542:         my ($end,$start,$id,$section,$fullname);
                   1543:         if (@Values > 2) {
                   1544:             ($end,$start,$id,$section,$fullname) = @Values;
                   1545:         } else { # We have to get the data ourselves
                   1546:             ($end,$start) = @Values;
1.37      matthew  1547:             $section = &Apache::lonnet::getsection($sdom,$sname,$cid);
1.35      matthew  1548:             my %info=&Apache::lonnet::get('environment',
                   1549:                                           ['firstname','middlename',
                   1550:                                            'lastname','generation','id'],
                   1551:                                           $sdom, $sname);
                   1552:             my ($tmp) = keys(%info);
                   1553:             if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   1554:                 $fullname = 'not available';
                   1555:                 $id = 'not available';
1.38      matthew  1556:                 &Apache::lonnet::logthis('unable to retrieve environment '.
                   1557:                                          'for '.$sname.':'.$sdom);
1.35      matthew  1558:             } else {
                   1559:                 $fullname = &ProcessFullName(@info{qw/lastname generation 
                   1560:                                                        firstname middlename/});
                   1561:                 $id = $info{'id'};
                   1562:             }
1.36      matthew  1563:             # Update the classlist with this students information
                   1564:             if ($fullname ne 'not available') {
                   1565:                 my $enrolldata = join(':',$end,$start,$id,$section,$fullname);
                   1566:                 my $reply=&Apache::lonnet::cput('classlist',
                   1567:                                                 {$student => $enrolldata},
                   1568:                                                 $cdom,$cnum);
                   1569:                 if ($reply !~ /^(ok|delayed)/) {
                   1570:                     &Apache::lonnet::logthis('Unable to update classlist for '.
                   1571:                                              'student '.$sname.':'.$sdom.
                   1572:                                              ' error:'.$reply);
                   1573:                 }
                   1574:             }
1.35      matthew  1575:         }
                   1576:         my $status='Expired';
                   1577:         if(((!$end) || $now < $end) && ((!$start) || ($now > $start))) {
                   1578:             $status='Active';
                   1579:         }
                   1580:         $classlist{$student} = 
                   1581:             [$sdom,$sname,$end,$start,$id,$section,$fullname,$status];
                   1582:     }
                   1583:     if (wantarray()) {
                   1584:         return (\%classlist,['domain','username','end','start','id',
                   1585:                              'section','fullname','status']);
                   1586:     } else {
                   1587:         return \%classlist;
                   1588:     }
                   1589: }
                   1590: 
1.1       stredwic 1591: # ----- END HELPER FUNCTIONS --------------------------------------------
                   1592: 
                   1593: 1;
                   1594: __END__
1.36      matthew  1595: 
1.35      matthew  1596: 

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