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

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: #
1.81.2.1! albertel    3: # $Id: loncoursedata.pm,v 1.81 2003/07/14 13:10:39 matthew 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:     }
1.81.2.1! albertel 1168:     ## Get misc parameters
        !          1169:     $request = 'SELECT c.symb,a.parameter,a.value '.
        !          1170:         "FROM $student_table AS b ".
        !          1171:         "LEFT JOIN $parameters_table AS a ON b.student_id=a.student_id ".
        !          1172:         "LEFT JOIN $symb_table AS c ON c.symb_id = a.symb_id ".
        !          1173:         "WHERE student='$student'";
        !          1174:     if (defined($symb) && $symb ne '') {
        !          1175:         $request .= " AND c.symb=".$dbh->quote($symb);
        !          1176:     }
        !          1177:     $sth = $dbh->prepare($request);
        !          1178:     $sth->execute();
        !          1179:     if ($sth->err()) {
        !          1180:         &Apache::lonnet::logthis("Unable to execute MySQL request:");
        !          1181:         &Apache::lonnet::logthis("\n".$request."\n");
        !          1182:         &Apache::lonnet::logthis("error is:".$sth->errstr());
        !          1183:         if (defined($symb) && $symb ne '') {
        !          1184:             $studentdata = $studentdata->{$symb};
        !          1185:         }
        !          1186:         return $studentdata;
        !          1187:     }
        !          1188:     #
        !          1189:     foreach my $row (@{$sth->fetchall_arrayref}) {
        !          1190:         $rows_retrieved++;
        !          1191:         my ($symb,$parameter,$value) = (@$row);
        !          1192:         $studentdata->{$symb}->{$parameter}  = $value;
        !          1193:     }
        !          1194:     #
1.67      matthew  1195:     if (defined($symb) && $symb ne '') {
                   1196:         $studentdata = $studentdata->{$symb};
1.57      matthew  1197:     }
                   1198:     return $studentdata;
                   1199: }
                   1200: 
                   1201: ################################################
                   1202: ################################################
                   1203: 
                   1204: =pod
                   1205: 
                   1206: =item &get_current_state()
                   1207: 
                   1208: Input: $sname,$sdom,$symb,$courseid
                   1209: 
                   1210: Output: Described below
1.46      matthew  1211: 
1.47      matthew  1212: Retrieve the current status of a students performance.  $sname and
1.46      matthew  1213: $sdom are the only required parameters.  If $symb is undef the results
1.47      matthew  1214: of an &Apache::lonnet::currentdump() will be returned.  
1.46      matthew  1215: If $courseid is undef it will be retrieved from the environment.
                   1216: 
                   1217: The return structure is based on &Apache::lonnet::currentdump.  If
                   1218: $symb is unspecified, all the students data is returned in a hash of
                   1219: the form:
                   1220: ( 
                   1221:   symb1 => { param1 => value1, param2 => value2 ... },
                   1222:   symb2 => { param1 => value1, param2 => value2 ... },
                   1223: )
                   1224: 
                   1225: If $symb is specified, a hash of 
                   1226: (
                   1227:   param1 => value1, 
                   1228:   param2 => value2,
                   1229: )
                   1230: is returned.
                   1231: 
1.57      matthew  1232: If no data is found for $symb, or if the student has no performance data,
1.46      matthew  1233: an empty list is returned.
                   1234: 
                   1235: =cut
                   1236: 
                   1237: ################################################
                   1238: ################################################
                   1239: sub get_current_state {
1.47      matthew  1240:     my ($sname,$sdom,$symb,$courseid,$forcedownload)=@_;
                   1241:     #
1.46      matthew  1242:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1.47      matthew  1243:     #
1.61      matthew  1244:     return () if (! defined($sname) || ! defined($sdom));
                   1245:     #
1.57      matthew  1246:     my ($status,$data) = &ensure_current_data($sname,$sdom,$courseid);
1.77      matthew  1247: #    &Apache::lonnet::logthis
                   1248: #        ('sname = '.$sname.
                   1249: #         ' domain = '.$sdom.
                   1250: #         ' status = '.$status.
                   1251: #         ' data is '.(defined($data)?'defined':'undefined'));
1.73      matthew  1252: #    while (my ($symb,$hash) = each(%$data)) {
                   1253: #        &Apache::lonnet::logthis($symb."\n----------------------------------");
                   1254: #        while (my ($key,$value) = each (%$hash)) {
                   1255: #            &Apache::lonnet::logthis("   ".$key." = ".$value);
                   1256: #        }
                   1257: #    }
1.47      matthew  1258:     #
1.79      matthew  1259:     if (defined($data) && defined($symb) && ref($data->{$symb})) {
                   1260:         return %{$data->{$symb}};
                   1261:     } elsif (defined($data) && ! defined($symb) && ref($data)) {
                   1262:         return %$data;
                   1263:     } 
                   1264:     if ($status eq 'no data') {
1.57      matthew  1265:         return ();
                   1266:     } else {
                   1267:         if ($status ne 'okay' && $status ne '') {
                   1268:             &Apache::lonnet::logthis('status = '.$status);
1.47      matthew  1269:             return ();
                   1270:         }
1.57      matthew  1271:         my $returnhash = &get_student_data_from_performance_cache($sname,$sdom,
                   1272:                                                       $symb,$courseid);
                   1273:         return %$returnhash if (defined($returnhash));
1.46      matthew  1274:     }
1.57      matthew  1275:     return ();
1.61      matthew  1276: }
                   1277: 
                   1278: ################################################
                   1279: ################################################
                   1280: 
                   1281: =pod
                   1282: 
                   1283: =item &get_problem_statistics()
                   1284: 
                   1285: Gather data on a given problem.  The database is assumed to be 
                   1286: populated and all local caching variables are assumed to be set
                   1287: properly.  This means you need to call &ensure_current_data for
                   1288: the students you are concerned with prior to calling this routine.
                   1289: 
                   1290: Inputs: $students, $symb, $part, $courseid
                   1291: 
1.64      matthew  1292: =over 4
                   1293: 
                   1294: =item $students is an array of hash references.  
                   1295: Each hash must contain at least the 'username' and 'domain' of a student.
                   1296: 
                   1297: =item $symb is the symb for the problem.
                   1298: 
                   1299: =item $part is the part id you need statistics for
                   1300: 
                   1301: =item $courseid is the course id, of course!
                   1302: 
                   1303: =back
                   1304: 
1.66      matthew  1305: Outputs: See the code for up to date information.  A hash reference is
                   1306: returned.  The hash has the following keys defined:
1.64      matthew  1307: 
                   1308: =over 4
                   1309: 
1.66      matthew  1310: =item num_students The number of students attempting the problem
                   1311:       
                   1312: =item tries The total number of tries for the students
                   1313:       
                   1314: =item max_tries The maximum number of tries taken
                   1315:       
                   1316: =item mean_tries The average number of tries
                   1317:       
                   1318: =item num_solved The number of students able to solve the problem
                   1319:       
                   1320: =item num_override The number of students whose answer is 'correct_by_override'
                   1321:       
                   1322: =item deg_of_diff The degree of difficulty of the problem
                   1323:       
                   1324: =item std_tries The standard deviation of the number of tries
                   1325:       
                   1326: =item skew_tries The skew of the number of tries
1.64      matthew  1327: 
1.66      matthew  1328: =item per_wrong The number of students attempting the problem who were not
                   1329: able to answer it correctly.
1.64      matthew  1330: 
                   1331: =back
                   1332: 
1.61      matthew  1333: =cut
                   1334: 
                   1335: ################################################
                   1336: ################################################
                   1337: sub get_problem_statistics {
                   1338:     my ($students,$symb,$part,$courseid) = @_;
                   1339:     return if (! defined($symb) || ! defined($part));
                   1340:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
                   1341:     #
                   1342:     my $symb_id = &get_symb_id($symb);
                   1343:     my $part_id = &get_part_id($part);
                   1344:     my $stats_table = $courseid.'_problem_stats';
                   1345:     #
                   1346:     my $dbh = &Apache::lonmysql::get_dbh();
                   1347:     return undef if (! defined($dbh));
                   1348:     #
                   1349:     # A) Number of Students attempting problem
                   1350:     # B) Total number of tries of students attempting problem
                   1351:     # C) Mod (largest number of tries for solving the problem)
                   1352:     # D) Mean (average number of tries for solving the problem)
                   1353:     # E) Number of students to solve the problem
                   1354:     # F) Number of students to solve the problem by override
                   1355:     # G) Number of students unable to solve the problem
                   1356:     # H) Degree of difficulty : 1-(E+F)/B
                   1357:     # I) Standard deviation of number of tries
                   1358:     # J) Skew of tries: sqrt(sum(Xi-D)^3)/A
                   1359:     #
                   1360:     $dbh->do('DROP TABLE '.$stats_table);  # May return an error
                   1361:     my $request = 
                   1362:         'CREATE TEMPORARY TABLE '.$stats_table.
                   1363:             ' SELECT student_id,solved,award,tries FROM '.$performance_table.
                   1364:                 ' WHERE symb_id='.$symb_id.' AND part_id='.$part_id;
1.64      matthew  1365:     if (defined($students)) {
                   1366:         $request .= ' AND ('.
                   1367:             join(' OR ', map {'student_id='.
                   1368:                                   &get_student_id($_->{'username'},
                   1369:                                                   $_->{'domain'})
                   1370:                                   } @$students
                   1371:                  ).')';
                   1372:     }
1.61      matthew  1373: #    &Apache::lonnet::logthis($request);
                   1374:     $dbh->do($request);
                   1375:     my ($num,$tries,$mod,$mean,$STD) = &execute_SQL_request
                   1376:         ($dbh,
                   1377:          'SELECT COUNT(*),SUM(tries),MAX(tries),AVG(tries),STD(tries) FROM '.
                   1378:          $stats_table);
                   1379:     my ($Solved) = &execute_SQL_request($dbh,'SELECT COUNT(tries) FROM '.
                   1380:                                         $stats_table.
                   1381:                                         " WHERE solved='correct_by_student'");
                   1382:     my ($solved) = &execute_SQL_request($dbh,'SELECT COUNT(tries) FROM '.
                   1383:                                         $stats_table.
                   1384:                                         " WHERE solved='correct_by_override'");
                   1385:     $num    = 0 if (! defined($num));
                   1386:     $tries  = 0 if (! defined($tries));
                   1387:     $mod    = 0 if (! defined($mod));
                   1388:     $STD    = 0 if (! defined($STD));
                   1389:     $Solved = 0 if (! defined($Solved));
                   1390:     $solved = 0 if (! defined($solved));
                   1391:     #
                   1392:     my $DegOfDiff = 'nan';
1.66      matthew  1393:     $DegOfDiff = 1-($Solved)/$tries if ($tries>0);
1.61      matthew  1394: 
                   1395:     my $SKEW = 'nan';
1.66      matthew  1396:     my $wrongpercent = 0;
1.61      matthew  1397:     if ($num > 0) {
                   1398:         ($SKEW) = &execute_SQL_request($dbh,'SELECT SQRT(SUM('.
                   1399:                                      'POWER(tries - '.$STD.',3)'.
                   1400:                                      '))/'.$num.' FROM '.$stats_table);
1.66      matthew  1401:         $wrongpercent=int(10*100*($num-$Solved+$solved)/$num)/10;
1.61      matthew  1402:     }
                   1403:     #
                   1404:     $dbh->do('DROP TABLE '.$stats_table);  # May return an error
1.81      matthew  1405:     #
                   1406:     # Store in metadata
                   1407:     #
1.80      www      1408:     if ($num) {
                   1409: 	my %storestats=();
                   1410: 
                   1411:         my $urlres=(split(/\_\_\_/,$symb))[2];
                   1412: 
                   1413: 	$storestats{$courseid.'___'.$urlres.'___timestamp'}=time;       
                   1414: 	$storestats{$courseid.'___'.$urlres.'___stdno'}=$num;
                   1415: 	$storestats{$courseid.'___'.$urlres.'___avetries'}=$mean;	   
                   1416: 	$storestats{$courseid.'___'.$urlres.'___difficulty'}=$DegOfDiff;
                   1417: 
                   1418: 	$urlres=~/^(\w+)\/(\w+)/; 
                   1419: 	&Apache::lonnet::put('nohist_resevaldata',\%storestats,$1,$2); 
                   1420:     }
1.81      matthew  1421:     #
                   1422:     # Return result
                   1423:     #
1.66      matthew  1424:     return { num_students => $num,
                   1425:              tries        => $tries,
                   1426:              max_tries    => $mod,
                   1427:              mean_tries   => $mean,
                   1428:              std_tries    => $STD,
                   1429:              skew_tries   => $SKEW,
                   1430:              num_solved   => $Solved,
                   1431:              num_override => $solved,
                   1432:              per_wrong    => $wrongpercent,
1.81      matthew  1433:              deg_of_diff  => $DegOfDiff };
1.61      matthew  1434: }
                   1435: 
                   1436: sub execute_SQL_request {
                   1437:     my ($dbh,$request)=@_;
                   1438: #    &Apache::lonnet::logthis($request);
                   1439:     my $sth = $dbh->prepare($request);
                   1440:     $sth->execute();
                   1441:     my $row = $sth->fetchrow_arrayref();
                   1442:     if (ref($row) eq 'ARRAY' && scalar(@$row)>0) {
                   1443:         return @$row;
                   1444:     }
                   1445:     return ();
                   1446: }
                   1447: 
                   1448: 
                   1449: ################################################
                   1450: ################################################
                   1451: 
                   1452: =pod
                   1453: 
                   1454: =item &setup_table_names()
                   1455: 
                   1456: input: course id
                   1457: 
                   1458: output: none
                   1459: 
                   1460: Cleans up the package variables for local caching.
                   1461: 
                   1462: =cut
                   1463: 
                   1464: ################################################
                   1465: ################################################
                   1466: sub setup_table_names {
                   1467:     my ($courseid) = @_;
                   1468:     if (! defined($courseid)) {
                   1469:         $courseid = $ENV{'request.course.id'};
                   1470:     }
                   1471:     #
                   1472:     if (! defined($current_course) || $current_course ne $courseid) {
                   1473:         # Clear out variables
                   1474:         $have_read_part_table = 0;
                   1475:         undef(%ids_by_part);
                   1476:         undef(%parts_by_id);
                   1477:         $have_read_symb_table = 0;
                   1478:         undef(%ids_by_symb);
                   1479:         undef(%symbs_by_id);
                   1480:         $have_read_student_table = 0;
                   1481:         undef(%ids_by_student);
                   1482:         undef(%students_by_id);
                   1483:         #
                   1484:         $current_course = $courseid;
                   1485:     }
                   1486:     #
                   1487:     # Set up database names
                   1488:     my $base_id = $courseid;
                   1489:     $symb_table        = $base_id.'_'.'symb';
                   1490:     $part_table        = $base_id.'_'.'part';
                   1491:     $student_table     = $base_id.'_'.'student';
                   1492:     $updatetime_table  = $base_id.'_'.'updatetime';
                   1493:     $performance_table = $base_id.'_'.'performance';
                   1494:     $parameters_table  = $base_id.'_'.'parameters';
                   1495:     return;
1.3       stredwic 1496: }
1.1       stredwic 1497: 
1.35      matthew  1498: ################################################
                   1499: ################################################
                   1500: 
                   1501: =pod
                   1502: 
1.57      matthew  1503: =back
                   1504: 
                   1505: =item End of Local Data Caching Subroutines
                   1506: 
                   1507: =cut
                   1508: 
                   1509: ################################################
                   1510: ################################################
                   1511: 
                   1512: 
                   1513: }
                   1514: ################################################
                   1515: ################################################
                   1516: 
                   1517: =pod
                   1518: 
                   1519: =head3 Classlist Subroutines
                   1520: 
1.35      matthew  1521: =item &get_classlist();
                   1522: 
                   1523: Retrieve the classist of a given class or of the current class.  Student
                   1524: information is returned from the classlist.db file and, if needed,
                   1525: from the students environment.
                   1526: 
                   1527: Optional arguments are $cid, $cdom, and $cnum (course id, course domain,
                   1528: and course number, respectively).  Any omitted arguments will be taken 
                   1529: from the current environment ($ENV{'request.course.id'},
                   1530: $ENV{'course.'.$cid.'.domain'}, and $ENV{'course.'.$cid.'.num'}).
                   1531: 
                   1532: Returns a reference to a hash which contains:
                   1533:  keys    '$sname:$sdom'
1.54      bowersj2 1534:  values  [$sdom,$sname,$end,$start,$id,$section,$fullname,$status]
                   1535: 
                   1536: The constant values CL_SDOM, CL_SNAME, CL_END, etc. can be used
                   1537: as indices into the returned list to future-proof clients against
                   1538: changes in the list order.
1.35      matthew  1539: 
                   1540: =cut
                   1541: 
                   1542: ################################################
                   1543: ################################################
1.54      bowersj2 1544: 
                   1545: sub CL_SDOM     { return 0; }
                   1546: sub CL_SNAME    { return 1; }
                   1547: sub CL_END      { return 2; }
                   1548: sub CL_START    { return 3; }
                   1549: sub CL_ID       { return 4; }
                   1550: sub CL_SECTION  { return 5; }
                   1551: sub CL_FULLNAME { return 6; }
                   1552: sub CL_STATUS   { return 7; }
1.35      matthew  1553: 
                   1554: sub get_classlist {
                   1555:     my ($cid,$cdom,$cnum) = @_;
                   1556:     $cid = $cid || $ENV{'request.course.id'};
                   1557:     $cdom = $cdom || $ENV{'course.'.$cid.'.domain'};
                   1558:     $cnum = $cnum || $ENV{'course.'.$cid.'.num'};
1.57      matthew  1559:     my $now = time;
1.35      matthew  1560:     #
                   1561:     my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
                   1562:     while (my ($student,$info) = each(%classlist)) {
1.60      matthew  1563:         if ($student =~ /^(con_lost|error|no_such_host)/i) {
                   1564:             &Apache::lonnet::logthis('get_classlist error for '.$cid.':'.$student);
                   1565:             return undef;
                   1566:         }
1.35      matthew  1567:         my ($sname,$sdom) = split(/:/,$student);
                   1568:         my @Values = split(/:/,$info);
                   1569:         my ($end,$start,$id,$section,$fullname);
                   1570:         if (@Values > 2) {
                   1571:             ($end,$start,$id,$section,$fullname) = @Values;
                   1572:         } else { # We have to get the data ourselves
                   1573:             ($end,$start) = @Values;
1.37      matthew  1574:             $section = &Apache::lonnet::getsection($sdom,$sname,$cid);
1.35      matthew  1575:             my %info=&Apache::lonnet::get('environment',
                   1576:                                           ['firstname','middlename',
                   1577:                                            'lastname','generation','id'],
                   1578:                                           $sdom, $sname);
                   1579:             my ($tmp) = keys(%info);
                   1580:             if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   1581:                 $fullname = 'not available';
                   1582:                 $id = 'not available';
1.38      matthew  1583:                 &Apache::lonnet::logthis('unable to retrieve environment '.
                   1584:                                          'for '.$sname.':'.$sdom);
1.35      matthew  1585:             } else {
                   1586:                 $fullname = &ProcessFullName(@info{qw/lastname generation 
                   1587:                                                        firstname middlename/});
                   1588:                 $id = $info{'id'};
                   1589:             }
1.36      matthew  1590:             # Update the classlist with this students information
                   1591:             if ($fullname ne 'not available') {
                   1592:                 my $enrolldata = join(':',$end,$start,$id,$section,$fullname);
                   1593:                 my $reply=&Apache::lonnet::cput('classlist',
                   1594:                                                 {$student => $enrolldata},
                   1595:                                                 $cdom,$cnum);
                   1596:                 if ($reply !~ /^(ok|delayed)/) {
                   1597:                     &Apache::lonnet::logthis('Unable to update classlist for '.
                   1598:                                              'student '.$sname.':'.$sdom.
                   1599:                                              ' error:'.$reply);
                   1600:                 }
                   1601:             }
1.35      matthew  1602:         }
                   1603:         my $status='Expired';
                   1604:         if(((!$end) || $now < $end) && ((!$start) || ($now > $start))) {
                   1605:             $status='Active';
                   1606:         }
                   1607:         $classlist{$student} = 
                   1608:             [$sdom,$sname,$end,$start,$id,$section,$fullname,$status];
                   1609:     }
                   1610:     if (wantarray()) {
                   1611:         return (\%classlist,['domain','username','end','start','id',
                   1612:                              'section','fullname','status']);
                   1613:     } else {
                   1614:         return \%classlist;
                   1615:     }
                   1616: }
                   1617: 
1.1       stredwic 1618: # ----- END HELPER FUNCTIONS --------------------------------------------
                   1619: 
                   1620: 1;
                   1621: __END__
1.36      matthew  1622: 
1.35      matthew  1623: 

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