Annotation of loncom/interface/loncoursedata.pm, revision 1.109
1.1 stredwic 1: # The LearningOnline Network with CAPA
2: #
1.109 ! matthew 3: # $Id: loncoursedata.pm,v 1.108 2003/10/30 16:20:18 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: Use lonnavmaps to build a data structure describing the order and
77: assessment contents of each sequence in the current course.
78:
79: The returned structure is a hash reference.
80:
1.61 matthew 81: { title => 'title',
82: symb => 'symb',
83: src => '/s/o/u/r/c/e',
1.45 matthew 84: type => (container|assessment),
1.50 matthew 85: num_assess => 2, # only for container
1.45 matthew 86: parts => [11,13,15], # only for assessment
1.50 matthew 87: response_ids => [12,14,16], # only for assessment
88: contents => [........] # only for container
1.45 matthew 89: }
90:
1.50 matthew 91: $hash->{'contents'} is a reference to an array of hashes of the same structure.
92:
93: Also returned are array references to the sequences and assessments contained
94: in the course.
1.49 matthew 95:
1.45 matthew 96:
97: =cut
98:
1.50 matthew 99: ####################################################
100: ####################################################
1.45 matthew 101: sub get_sequence_assessment_data {
102: my $fn=$ENV{'request.course.fn'};
103: ##
104: ## use navmaps
1.83 bowersj2 105: my $navmap = Apache::lonnavmaps::navmap->new();
1.45 matthew 106: if (!defined($navmap)) {
107: return 'Can not open Coursemap';
108: }
1.75 matthew 109: # We explicity grab the top level map because I am not sure we
110: # are pulling it from the iterator.
111: my $top_level_map = $navmap->getById('0.0');
112: #
1.45 matthew 113: my $iterator = $navmap->getIterator(undef, undef, undef, 1);
1.61 matthew 114: my $curRes = $iterator->next(); # Top level sequence
1.45 matthew 115: ##
116: ## Prime the pump
117: ##
118: ## We are going to loop until we run out of sequences/pages to explore for
119: ## resources. This means we have to start out with something to look
120: ## at.
1.76 matthew 121: my $title = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1.75 matthew 122: my $symb = $top_level_map->symb();
123: my $src = $top_level_map->src();
124: my $randompick = $top_level_map->randompick();
1.45 matthew 125: #
1.49 matthew 126: my @Sequences;
127: my @Assessments;
1.45 matthew 128: my @Nested_Sequences = (); # Stack of sequences, keeps track of depth
129: my $top = { title => $title,
1.52 matthew 130: src => $src,
1.45 matthew 131: symb => $symb,
132: type => 'container',
133: num_assess => 0,
1.53 matthew 134: num_assess_parts => 0,
1.75 matthew 135: contents => [],
136: randompick => $randompick,
137: };
1.49 matthew 138: push (@Sequences,$top);
1.45 matthew 139: push (@Nested_Sequences, $top);
140: #
141: # We need to keep track of which sequences contain homework problems
142: #
1.78 matthew 143: my $previous_too;
1.52 matthew 144: my $previous;
1.45 matthew 145: while (scalar(@Nested_Sequences)) {
1.78 matthew 146: $previous_too = $previous;
1.50 matthew 147: $previous = $curRes;
1.45 matthew 148: $curRes = $iterator->next();
149: my $currentmap = $Nested_Sequences[-1]; # Last one on the stack
150: if ($curRes == $iterator->BEGIN_MAP()) {
1.78 matthew 151: if (! ref($previous)) {
152: $previous = $previous_too;
153: }
154: if (! ref($previous)) {
155: next;
156: }
1.45 matthew 157: # get the map itself, instead of BEGIN_MAP
1.51 matthew 158: $title = $previous->title();
1.84 matthew 159: $title =~ s/\:/\&\#058;/g;
1.51 matthew 160: $symb = $previous->symb();
161: $src = $previous->src();
1.81 matthew 162: # pick up the filename if there is no title available
163: if (! defined($title) || $title eq '') {
164: ($title) = ($src=~/\/([^\/]*)$/);
165: }
1.75 matthew 166: $randompick = $previous->randompick();
1.45 matthew 167: my $newmap = { title => $title,
168: src => $src,
169: symb => $symb,
170: type => 'container',
171: num_assess => 0,
1.75 matthew 172: randompick => $randompick,
1.45 matthew 173: contents => [],
174: };
175: push (@{$currentmap->{'contents'}},$newmap); # this is permanent
1.49 matthew 176: push (@Sequences,$newmap);
1.45 matthew 177: push (@Nested_Sequences, $newmap); # this is a stack
178: next;
179: }
180: if ($curRes == $iterator->END_MAP()) {
181: pop(@Nested_Sequences);
182: next;
183: }
184: next if (! ref($curRes));
1.50 matthew 185: next if (! $curRes->is_problem());# && !$curRes->randomout);
1.45 matthew 186: # Okay, from here on out we only deal with assessments
187: $title = $curRes->title();
1.84 matthew 188: $title =~ s/\:/\&\#058;/g;
1.45 matthew 189: $symb = $curRes->symb();
190: $src = $curRes->src();
191: my $parts = $curRes->parts();
1.87 matthew 192: my %partdata;
193: foreach my $part (@$parts) {
1.88 matthew 194: my @Responses = $curRes->responseType($part);
195: my @Ids = $curRes->responseIds($part);
196: $partdata{$part}->{'ResponseTypes'}= \@Responses;
197: $partdata{$part}->{'ResponseIds'} = \@Ids;
1.91 matthew 198: # Count how many responses of each type there are in this part
199: foreach (@Responses) {
200: $partdata{$part}->{$_}++;
201: }
1.87 matthew 202: }
1.45 matthew 203: my $assessment = { title => $title,
204: src => $src,
205: symb => $symb,
206: type => 'assessment',
1.53 matthew 207: parts => $parts,
208: num_parts => scalar(@$parts),
1.87 matthew 209: partdata => \%partdata,
1.45 matthew 210: };
1.49 matthew 211: push(@Assessments,$assessment);
1.45 matthew 212: push(@{$currentmap->{'contents'}},$assessment);
213: $currentmap->{'num_assess'}++;
1.53 matthew 214: $currentmap->{'num_assess_parts'}+= scalar(@$parts);
1.45 matthew 215: }
1.58 matthew 216: $navmap->untieHashes();
1.49 matthew 217: return ($top,\@Sequences,\@Assessments);
1.45 matthew 218: }
1.50 matthew 219:
1.4 stredwic 220: sub LoadDiscussion {
1.13 stredwic 221: my ($courseID)=@_;
1.5 minaeibi 222: my %Discuss=();
223: my %contrib=&Apache::lonnet::dump(
224: $courseID,
225: $ENV{'course.'.$courseID.'.domain'},
226: $ENV{'course.'.$courseID.'.num'});
227:
228: #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
229:
1.4 stredwic 230: foreach my $temp(keys %contrib) {
231: if ($temp=~/^version/) {
232: my $ver=$contrib{$temp};
233: my ($dummy,$prb)=split(':',$temp);
234: for (my $idx=1; $idx<=$ver; $idx++ ) {
235: my $name=$contrib{"$idx:$prb:sendername"};
1.5 minaeibi 236: $Discuss{"$name:$prb"}=$idx;
1.4 stredwic 237: }
238: }
239: }
1.5 minaeibi 240:
241: return \%Discuss;
1.1 stredwic 242: }
243:
1.71 matthew 244: ################################################
245: ################################################
246:
247: =pod
248:
249: =item &GetUserName(username,userdomain)
250:
251: Returns a hash with the following entries:
252: 'firstname', 'middlename', 'lastname', 'generation', and 'fullname'
253:
254: 'fullname' is the result of &Apache::loncoursedata::ProcessFullName.
255:
256: =cut
257:
258: ################################################
259: ################################################
260: sub GetUserName {
261: my ($username,$userdomain) = @_;
262: $username = $ENV{'user.name'} if (! defined($username));
263: $userdomain = $ENV{'user.domain'} if (! defined($username));
264: my %userenv = &Apache::lonnet::get('environment',
265: ['firstname','middlename','lastname','generation'],
266: $userdomain,$username);
267: $userenv{'fullname'} = &ProcessFullName($userenv{'lastname'},
268: $userenv{'generation'},
269: $userenv{'firstname'},
270: $userenv{'middlename'});
271: return %userenv;
272: }
273:
274: ################################################
275: ################################################
276:
1.1 stredwic 277: =pod
278:
279: =item &ProcessFullName()
280:
281: Takes lastname, generation, firstname, and middlename (or some partial
282: set of this data) and returns the full name version as a string. Format
283: is Lastname generation, firstname middlename or a subset of this.
284:
285: =cut
286:
1.71 matthew 287: ################################################
288: ################################################
1.1 stredwic 289: sub ProcessFullName {
290: my ($lastname, $generation, $firstname, $middlename)=@_;
291: my $Str = '';
292:
1.34 matthew 293: # Strip whitespace preceeding & following name components.
294: $lastname =~ s/(\s+$|^\s+)//g;
295: $generation =~ s/(\s+$|^\s+)//g;
296: $firstname =~ s/(\s+$|^\s+)//g;
297: $middlename =~ s/(\s+$|^\s+)//g;
298:
1.1 stredwic 299: if($lastname ne '') {
1.34 matthew 300: $Str .= $lastname;
301: $Str .= ' '.$generation if ($generation ne '');
302: $Str .= ',';
303: $Str .= ' '.$firstname if ($firstname ne '');
304: $Str .= ' '.$middlename if ($middlename ne '');
1.1 stredwic 305: } else {
1.34 matthew 306: $Str .= $firstname if ($firstname ne '');
307: $Str .= ' '.$middlename if ($middlename ne '');
308: $Str .= ' '.$generation if ($generation ne '');
1.1 stredwic 309: }
310:
311: return $Str;
312: }
313:
1.46 matthew 314: ################################################
315: ################################################
316:
317: =pod
318:
1.47 matthew 319: =item &make_into_hash($values);
320:
321: Returns a reference to a hash as described by $values. $values is
322: assumed to be the result of
1.57 matthew 323: join(':',map {&Apache::lonnet::escape($_)} %orighash);
1.47 matthew 324:
325: This is a helper function for get_current_state.
326:
327: =cut
328:
329: ################################################
330: ################################################
331: sub make_into_hash {
332: my $values = shift;
333: my %tmp = map { &Apache::lonnet::unescape($_); }
334: split(':',$values);
335: return \%tmp;
336: }
337:
338:
339: ################################################
340: ################################################
341:
342: =pod
343:
1.57 matthew 344: =head1 LOCAL DATA CACHING SUBROUTINES
345:
346: The local caching is done using MySQL. There is no fall-back implementation
347: if MySQL is not running.
348:
349: The programmers interface is to call &get_current_state() or some other
350: primary interface subroutine (described below). The internals of this
351: storage system are documented here.
352:
353: There are six tables used to store student performance data (the results of
354: a dumpcurrent). Each of these tables is created in MySQL with a name of
355: $courseid_*****, where ***** is 'symb', 'part', or whatever is appropriate
356: for the table. The tables and their purposes are described below.
357:
358: Some notes before we get started.
359:
360: Each table must have a PRIMARY KEY, which is a column or set of columns which
361: will serve to uniquely identify a row of data. NULL is not allowed!
362:
363: INDEXes work best on integer data.
364:
365: JOIN is used to combine data from many tables into one output.
366:
367: lonmysql.pm is used for some of the interface, specifically the table creation
368: calls. The inserts are done in bulk by directly calling the database handler.
369: The SELECT ... JOIN statement used to retrieve the data does not have an
370: interface in lonmysql.pm and I shudder at the thought of writing one.
371:
372: =head3 Table Descriptions
373:
374: =over 4
375:
1.89 matthew 376: =item Tables used to store meta information
377:
378: The following tables hold data required to keep track of the current status
379: of a students data in the tables or to look up the students data in the tables.
380:
381: =over 4
382:
1.57 matthew 383: =item $symb_table
384:
385: The symb_table has two columns. The first is a 'symb_id' and the second
386: is the text name for the 'symb' (limited to 64k). The 'symb_id' is generated
387: automatically by MySQL so inserts should be done on this table with an
388: empty first element. This table has its PRIMARY KEY on the 'symb_id'.
389:
390: =item $part_table
391:
392: The part_table has two columns. The first is a 'part_id' and the second
393: is the text name for the 'part' (limited to 100 characters). The 'part_id' is
394: generated automatically by MySQL so inserts should be done on this table with
395: an empty first element. This table has its PRIMARY KEY on the 'part' (100
396: characters) and a KEY on 'part_id'.
397:
398: =item $student_table
399:
400: The student_table has two columns. The first is a 'student_id' and the second
401: is the text description of the 'student' (typically username:domain) (less
402: than 100 characters). The 'student_id' is automatically generated by MySQL.
403: The use of the name 'student_id' is loaded, I know, but this ID is used ONLY
404: internally to the MySQL database and is not the same as the students ID
405: (stored in the students environment). This table has its PRIMARY KEY on the
406: 'student' (100 characters).
407:
1.87 matthew 408: =item $studentdata_table
1.57 matthew 409:
1.89 matthew 410: The studentdata_table has four columns: 'student_id' (the unique id of
411: the student), 'updatetime' (the time the students data was last updated),
412: 'fullupdatetime' (the time the students full data was last updated),
413: 'section', and 'classification'( the students current classification).
414: This table has its PRIMARY KEY on 'student_id'.
415:
416: =back
417:
418: =item Tables used to store current status data
419:
420: The following tables store data only about the students current status on
421: a problem, meaning only the data related to the last attempt on a problem.
422:
423: =over 4
1.57 matthew 424:
425: =item $performance_table
426:
427: The performance_table has 9 columns. The first three are 'symb_id',
428: 'student_id', and 'part_id'. These comprise the PRIMARY KEY for this table
429: and are directly related to the $symb_table, $student_table, and $part_table
430: described above. MySQL does better indexing on numeric items than text,
431: so we use these three "index tables". The remaining columns are
432: 'solved', 'tries', 'awarded', 'award', 'awarddetail', and 'timestamp'.
433: These are either the MySQL type TINYTEXT or various integers ('tries' and
434: 'timestamp'). This table has KEYs of 'student_id' and 'symb_id'.
435: For use of this table, see the functions described below.
436:
437: =item $parameters_table
438:
439: The parameters_table holds the data that does not fit neatly into the
440: performance_table. The parameters table has four columns: 'symb_id',
441: 'student_id', 'parameter', and 'value'. 'symb_id', 'student_id', and
442: 'parameter' comprise the PRIMARY KEY for this table. 'parameter' is
443: limited to 255 characters. 'value' is limited to 64k characters.
444:
445: =back
446:
1.89 matthew 447: =item Tables used for storing historic data
448:
449: The following tables are used to store almost all of the transactions a student
450: has made on a homework problem. See loncapa/docs/homework/datastorage for
451: specific information about each of the parameters stored.
452:
453: =over 4
454:
455: =item $fulldump_response_table
456:
457: The response table holds data (documented in loncapa/docs/homework/datastorage)
458: associated with a particular response id which is stored when a student
459: attempts a problem. The following are the columns of the table, in order:
460: 'symb_id','part_id','response_id','student_id','transaction','tries',
1.93 matthew 461: 'awarddetail', 'response_specific' (data particular to the response
1.89 matthew 462: type), 'response_specific_value', and 'submission (the text of the students
463: submission). The primary key is based on the first five columns listed above.
464:
465: =item $fulldump_part_table
466:
467: The part table holds data (documented in loncapa/docs/homework/datastorage)
468: associated with a particular part id which is stored when a student attempts
469: a problem. The following are the columns of the table, in order:
470: 'symb_id','part_id','student_id','transaction','tries','award','awarded',
471: and 'previous'. The primary key is based on the first five columns listed
472: above.
473:
474: =item $fulldump_timestamp_table
475:
476: The timestamp table holds the timestamps of the transactions which are
477: stored in $fulldump_response_table and $fulldump_part_table. This data is
478: about both the response and part data. Columns: 'symb_id','student_id',
479: 'transaction', and 'timestamp'.
480: The primary key is based on the first 3 columns.
481:
482: =back
483:
484: =back
485:
1.57 matthew 486: =head3 Important Subroutines
487:
488: Here is a brief overview of the subroutines which are likely to be of
489: interest:
490:
491: =over 4
492:
493: =item &get_current_state(): programmers interface.
494:
495: =item &init_dbs(): table creation
496:
497: =item &update_student_data(): data storage calls
498:
499: =item &get_student_data_from_performance_cache(): data retrieval
500:
501: =back
502:
503: =head3 Main Documentation
504:
505: =over 4
506:
507: =cut
508:
509: ################################################
510: ################################################
511:
512: ################################################
513: ################################################
1.89 matthew 514: { # Begin scope of table identifiers
1.57 matthew 515:
516: my $current_course ='';
517: my $symb_table;
518: my $part_table;
519: my $student_table;
1.87 matthew 520: my $studentdata_table;
1.57 matthew 521: my $performance_table;
522: my $parameters_table;
1.89 matthew 523: my $fulldump_response_table;
524: my $fulldump_part_table;
525: my $fulldump_timestamp_table;
1.57 matthew 526:
1.89 matthew 527: my @Tables;
1.57 matthew 528: ################################################
529: ################################################
530:
531: =pod
532:
533: =item &init_dbs()
534:
535: Input: course id
536:
537: Output: 0 on success, positive integer on error
538:
539: This routine issues the calls to lonmysql to create the tables used to
540: store student data.
541:
542: =cut
543:
544: ################################################
545: ################################################
546: sub init_dbs {
547: my $courseid = shift;
548: &setup_table_names($courseid);
549: #
1.73 matthew 550: # Drop any of the existing tables
1.89 matthew 551: foreach my $table (@Tables) {
1.73 matthew 552: &Apache::lonmysql::drop_table($table);
553: }
554: #
1.57 matthew 555: # Note - changes to this table must be reflected in the code that
556: # stores the data (calls &Apache::lonmysql::store_row with this table
557: # id
558: my $symb_table_def = {
559: id => $symb_table,
560: permanent => 'no',
561: columns => [{ name => 'symb_id',
562: type => 'MEDIUMINT UNSIGNED',
563: restrictions => 'NOT NULL',
564: auto_inc => 'yes', },
565: { name => 'symb',
566: type => 'MEDIUMTEXT',
567: restrictions => 'NOT NULL'},
568: ],
569: 'PRIMARY KEY' => ['symb_id'],
570: };
571: #
572: my $part_table_def = {
573: id => $part_table,
574: permanent => 'no',
575: columns => [{ name => 'part_id',
576: type => 'MEDIUMINT UNSIGNED',
577: restrictions => 'NOT NULL',
578: auto_inc => 'yes', },
579: { name => 'part',
580: type => 'VARCHAR(100)',
581: restrictions => 'NOT NULL'},
582: ],
583: 'PRIMARY KEY' => ['part (100)'],
584: 'KEY' => [{ columns => ['part_id']},],
585: };
586: #
587: my $student_table_def = {
588: id => $student_table,
589: permanent => 'no',
590: columns => [{ name => 'student_id',
591: type => 'MEDIUMINT UNSIGNED',
592: restrictions => 'NOT NULL',
593: auto_inc => 'yes', },
594: { name => 'student',
595: type => 'VARCHAR(100)',
596: restrictions => 'NOT NULL'},
1.85 matthew 597: { name => 'classification',
598: type => 'varchar(100)', },
1.57 matthew 599: ],
600: 'PRIMARY KEY' => ['student (100)'],
601: 'KEY' => [{ columns => ['student_id']},],
602: };
603: #
1.87 matthew 604: my $studentdata_table_def = {
605: id => $studentdata_table,
1.57 matthew 606: permanent => 'no',
1.87 matthew 607: columns => [{ name => 'student_id',
608: type => 'MEDIUMINT UNSIGNED',
1.57 matthew 609: restrictions => 'NOT NULL UNIQUE',},
610: { name => 'updatetime',
1.89 matthew 611: type => 'INT UNSIGNED'},
612: { name => 'fullupdatetime',
613: type => 'INT UNSIGNED'},
1.87 matthew 614: { name => 'section',
615: type => 'VARCHAR(100)'},
616: { name => 'classification',
617: type => 'VARCHAR(100)', },
1.57 matthew 618: ],
1.87 matthew 619: 'PRIMARY KEY' => ['student_id'],
1.57 matthew 620: };
621: #
622: my $performance_table_def = {
623: id => $performance_table,
624: permanent => 'no',
625: columns => [{ name => 'symb_id',
626: type => 'MEDIUMINT UNSIGNED',
627: restrictions => 'NOT NULL' },
628: { name => 'student_id',
629: type => 'MEDIUMINT UNSIGNED',
630: restrictions => 'NOT NULL' },
631: { name => 'part_id',
632: type => 'MEDIUMINT UNSIGNED',
633: restrictions => 'NOT NULL' },
1.73 matthew 634: { name => 'part',
635: type => 'VARCHAR(100)',
636: restrictions => 'NOT NULL'},
1.57 matthew 637: { name => 'solved',
638: type => 'TINYTEXT' },
639: { name => 'tries',
640: type => 'SMALLINT UNSIGNED' },
641: { name => 'awarded',
642: type => 'TINYTEXT' },
643: { name => 'award',
644: type => 'TINYTEXT' },
645: { name => 'awarddetail',
646: type => 'TINYTEXT' },
647: { name => 'timestamp',
648: type => 'INT UNSIGNED'},
649: ],
650: 'PRIMARY KEY' => ['symb_id','student_id','part_id'],
651: 'KEY' => [{ columns=>['student_id'] },
652: { columns=>['symb_id'] },],
653: };
654: #
1.89 matthew 655: my $fulldump_part_table_def = {
656: id => $fulldump_part_table,
657: permanent => 'no',
658: columns => [
659: { name => 'symb_id',
660: type => 'MEDIUMINT UNSIGNED',
661: restrictions => 'NOT NULL' },
662: { name => 'part_id',
663: type => 'MEDIUMINT UNSIGNED',
664: restrictions => 'NOT NULL' },
665: { name => 'student_id',
666: type => 'MEDIUMINT UNSIGNED',
667: restrictions => 'NOT NULL' },
668: { name => 'transaction',
669: type => 'MEDIUMINT UNSIGNED',
670: restrictions => 'NOT NULL' },
671: { name => 'tries',
672: type => 'SMALLINT UNSIGNED',
673: restrictions => 'NOT NULL' },
674: { name => 'award',
675: type => 'TINYTEXT' },
676: { name => 'awarded',
677: type => 'TINYTEXT' },
678: { name => 'previous',
679: type => 'SMALLINT UNSIGNED' },
680: # { name => 'regrader',
681: # type => 'TINYTEXT' },
682: # { name => 'afterduedate',
683: # type => 'TINYTEXT' },
684: ],
685: 'PRIMARY KEY' => ['symb_id','part_id','student_id','transaction'],
686: 'KEY' => [
687: { columns=>['symb_id'] },
688: { columns=>['part_id'] },
689: { columns=>['student_id'] },
690: ],
691: };
692: #
693: my $fulldump_response_table_def = {
694: id => $fulldump_response_table,
695: permanent => 'no',
696: columns => [
697: { name => 'symb_id',
698: type => 'MEDIUMINT UNSIGNED',
699: restrictions => 'NOT NULL' },
700: { name => 'part_id',
701: type => 'MEDIUMINT UNSIGNED',
702: restrictions => 'NOT NULL' },
703: { name => 'response_id',
704: type => 'MEDIUMINT UNSIGNED',
705: restrictions => 'NOT NULL' },
706: { name => 'student_id',
707: type => 'MEDIUMINT UNSIGNED',
708: restrictions => 'NOT NULL' },
709: { name => 'transaction',
710: type => 'MEDIUMINT UNSIGNED',
711: restrictions => 'NOT NULL' },
712: { name => 'awarddetail',
713: type => 'TINYTEXT' },
714: # { name => 'message',
715: # type => 'CHAR' },
716: { name => 'response_specific',
717: type => 'TINYTEXT' },
718: { name => 'response_specific_value',
719: type => 'TINYTEXT' },
720: { name => 'submission',
721: type => 'TEXT'},
722: ],
723: 'PRIMARY KEY' => ['symb_id','part_id','response_id','student_id',
724: 'transaction'],
725: 'KEY' => [
726: { columns=>['symb_id'] },
727: { columns=>['part_id','response_id'] },
728: { columns=>['student_id'] },
729: ],
730: };
731: my $fulldump_timestamp_table_def = {
732: id => $fulldump_timestamp_table,
733: permanent => 'no',
734: columns => [
735: { name => 'symb_id',
736: type => 'MEDIUMINT UNSIGNED',
737: restrictions => 'NOT NULL' },
738: { name => 'student_id',
739: type => 'MEDIUMINT UNSIGNED',
740: restrictions => 'NOT NULL' },
741: { name => 'transaction',
742: type => 'MEDIUMINT UNSIGNED',
743: restrictions => 'NOT NULL' },
744: { name => 'timestamp',
745: type => 'INT UNSIGNED'},
746: ],
747: 'PRIMARY KEY' => ['symb_id','student_id','transaction'],
748: 'KEY' => [
749: { columns=>['symb_id'] },
750: { columns=>['student_id'] },
751: { columns=>['transaction'] },
752: ],
753: };
754:
755: #
1.57 matthew 756: my $parameters_table_def = {
757: id => $parameters_table,
758: permanent => 'no',
759: columns => [{ name => 'symb_id',
760: type => 'MEDIUMINT UNSIGNED',
761: restrictions => 'NOT NULL' },
762: { name => 'student_id',
763: type => 'MEDIUMINT UNSIGNED',
764: restrictions => 'NOT NULL' },
765: { name => 'parameter',
766: type => 'TINYTEXT',
767: restrictions => 'NOT NULL' },
768: { name => 'value',
769: type => 'MEDIUMTEXT' },
770: ],
771: 'PRIMARY KEY' => ['symb_id','student_id','parameter (255)'],
772: };
773: #
774: # Create the tables
775: my $tableid;
776: $tableid = &Apache::lonmysql::create_table($symb_table_def);
777: if (! defined($tableid)) {
778: &Apache::lonnet::logthis("error creating symb_table: ".
779: &Apache::lonmysql::get_error());
780: return 1;
781: }
782: #
783: $tableid = &Apache::lonmysql::create_table($part_table_def);
784: if (! defined($tableid)) {
785: &Apache::lonnet::logthis("error creating part_table: ".
786: &Apache::lonmysql::get_error());
787: return 2;
788: }
789: #
790: $tableid = &Apache::lonmysql::create_table($student_table_def);
791: if (! defined($tableid)) {
792: &Apache::lonnet::logthis("error creating student_table: ".
793: &Apache::lonmysql::get_error());
794: return 3;
795: }
796: #
1.87 matthew 797: $tableid = &Apache::lonmysql::create_table($studentdata_table_def);
1.57 matthew 798: if (! defined($tableid)) {
1.87 matthew 799: &Apache::lonnet::logthis("error creating studentdata_table: ".
1.57 matthew 800: &Apache::lonmysql::get_error());
801: return 4;
802: }
803: #
804: $tableid = &Apache::lonmysql::create_table($performance_table_def);
805: if (! defined($tableid)) {
806: &Apache::lonnet::logthis("error creating preformance_table: ".
807: &Apache::lonmysql::get_error());
808: return 5;
809: }
810: #
811: $tableid = &Apache::lonmysql::create_table($parameters_table_def);
812: if (! defined($tableid)) {
813: &Apache::lonnet::logthis("error creating parameters_table: ".
814: &Apache::lonmysql::get_error());
815: return 6;
816: }
1.89 matthew 817: #
818: $tableid = &Apache::lonmysql::create_table($fulldump_part_table_def);
819: if (! defined($tableid)) {
820: &Apache::lonnet::logthis("error creating fulldump_part_table: ".
821: &Apache::lonmysql::get_error());
822: return 7;
823: }
824: #
825: $tableid = &Apache::lonmysql::create_table($fulldump_response_table_def);
826: if (! defined($tableid)) {
827: &Apache::lonnet::logthis("error creating fulldump_response_table: ".
828: &Apache::lonmysql::get_error());
829: return 8;
830: }
831: $tableid = &Apache::lonmysql::create_table($fulldump_timestamp_table_def);
832: if (! defined($tableid)) {
833: &Apache::lonnet::logthis("error creating fulldump_timestamp_table: ".
834: &Apache::lonmysql::get_error());
835: return 9;
836: }
1.57 matthew 837: return 0;
1.70 matthew 838: }
839:
840: ################################################
841: ################################################
842:
843: =pod
844:
845: =item &delete_caches()
846:
847: =cut
848:
849: ################################################
850: ################################################
851: sub delete_caches {
852: my $courseid = shift;
853: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
854: #
855: &setup_table_names($courseid);
856: #
857: my $dbh = &Apache::lonmysql::get_dbh();
1.89 matthew 858: foreach my $table (@Tables) {
1.70 matthew 859: my $command = 'DROP TABLE '.$table.';';
860: $dbh->do($command);
861: if ($dbh->err) {
862: &Apache::lonnet::logthis($command.' resulted in error: '.$dbh->errstr);
863: }
864: }
865: return;
1.57 matthew 866: }
867:
868: ################################################
869: ################################################
870:
871: =pod
872:
873: =item &get_part_id()
874:
875: Get the MySQL id of a problem part string.
876:
877: Input: $part
878:
879: Output: undef on error, integer $part_id on success.
880:
881: =item &get_part()
882:
883: Get the string describing a part from the MySQL id of the problem part.
884:
885: Input: $part_id
886:
887: Output: undef on error, $part string on success.
888:
889: =cut
890:
891: ################################################
892: ################################################
893:
1.61 matthew 894: my $have_read_part_table = 0;
1.57 matthew 895: my %ids_by_part;
896: my %parts_by_id;
897:
898: sub get_part_id {
899: my ($part) = @_;
1.61 matthew 900: $part = 0 if (! defined($part));
901: if (! $have_read_part_table) {
902: my @Result = &Apache::lonmysql::get_rows($part_table);
903: foreach (@Result) {
904: $ids_by_part{$_->[1]}=$_->[0];
905: }
906: $have_read_part_table = 1;
907: }
1.57 matthew 908: if (! exists($ids_by_part{$part})) {
909: &Apache::lonmysql::store_row($part_table,[undef,$part]);
910: undef(%ids_by_part);
911: my @Result = &Apache::lonmysql::get_rows($part_table);
912: foreach (@Result) {
913: $ids_by_part{$_->[1]}=$_->[0];
914: }
915: }
916: return $ids_by_part{$part} if (exists($ids_by_part{$part}));
917: return undef; # error
918: }
919:
920: sub get_part {
921: my ($part_id) = @_;
922: if (! exists($parts_by_id{$part_id}) ||
923: ! defined($parts_by_id{$part_id}) ||
924: $parts_by_id{$part_id} eq '') {
925: my @Result = &Apache::lonmysql::get_rows($part_table);
926: foreach (@Result) {
927: $parts_by_id{$_->[0]}=$_->[1];
928: }
929: }
930: return $parts_by_id{$part_id} if(exists($parts_by_id{$part_id}));
931: return undef; # error
932: }
933:
934: ################################################
935: ################################################
936:
937: =pod
938:
939: =item &get_symb_id()
940:
941: Get the MySQL id of a symb.
942:
943: Input: $symb
944:
945: Output: undef on error, integer $symb_id on success.
946:
947: =item &get_symb()
948:
949: Get the symb associated with a MySQL symb_id.
950:
951: Input: $symb_id
952:
953: Output: undef on error, $symb on success.
954:
955: =cut
956:
957: ################################################
958: ################################################
959:
1.61 matthew 960: my $have_read_symb_table = 0;
1.57 matthew 961: my %ids_by_symb;
962: my %symbs_by_id;
963:
964: sub get_symb_id {
965: my ($symb) = @_;
1.61 matthew 966: if (! $have_read_symb_table) {
967: my @Result = &Apache::lonmysql::get_rows($symb_table);
968: foreach (@Result) {
969: $ids_by_symb{$_->[1]}=$_->[0];
970: }
971: $have_read_symb_table = 1;
972: }
1.57 matthew 973: if (! exists($ids_by_symb{$symb})) {
974: &Apache::lonmysql::store_row($symb_table,[undef,$symb]);
975: undef(%ids_by_symb);
976: my @Result = &Apache::lonmysql::get_rows($symb_table);
977: foreach (@Result) {
978: $ids_by_symb{$_->[1]}=$_->[0];
979: }
980: }
981: return $ids_by_symb{$symb} if(exists( $ids_by_symb{$symb}));
982: return undef; # error
983: }
984:
985: sub get_symb {
986: my ($symb_id) = @_;
987: if (! exists($symbs_by_id{$symb_id}) ||
988: ! defined($symbs_by_id{$symb_id}) ||
989: $symbs_by_id{$symb_id} eq '') {
990: my @Result = &Apache::lonmysql::get_rows($symb_table);
991: foreach (@Result) {
992: $symbs_by_id{$_->[0]}=$_->[1];
993: }
994: }
995: return $symbs_by_id{$symb_id} if(exists( $symbs_by_id{$symb_id}));
996: return undef; # error
997: }
998:
999: ################################################
1000: ################################################
1001:
1002: =pod
1003:
1004: =item &get_student_id()
1005:
1006: Get the MySQL id of a student.
1007:
1008: Input: $sname, $dom
1009:
1010: Output: undef on error, integer $student_id on success.
1011:
1012: =item &get_student()
1013:
1014: Get student username:domain associated with the MySQL student_id.
1015:
1016: Input: $student_id
1017:
1018: Output: undef on error, string $student (username:domain) on success.
1019:
1020: =cut
1021:
1022: ################################################
1023: ################################################
1024:
1.61 matthew 1025: my $have_read_student_table = 0;
1.57 matthew 1026: my %ids_by_student;
1027: my %students_by_id;
1028:
1029: sub get_student_id {
1030: my ($sname,$sdom) = @_;
1031: my $student = $sname.':'.$sdom;
1.61 matthew 1032: if (! $have_read_student_table) {
1033: my @Result = &Apache::lonmysql::get_rows($student_table);
1034: foreach (@Result) {
1035: $ids_by_student{$_->[1]}=$_->[0];
1036: }
1037: $have_read_student_table = 1;
1038: }
1.57 matthew 1039: if (! exists($ids_by_student{$student})) {
1.106 matthew 1040: &Apache::lonmysql::store_row($student_table,
1041: [undef,$student,undef]);
1.57 matthew 1042: undef(%ids_by_student);
1043: my @Result = &Apache::lonmysql::get_rows($student_table);
1044: foreach (@Result) {
1045: $ids_by_student{$_->[1]}=$_->[0];
1046: }
1047: }
1048: return $ids_by_student{$student} if(exists( $ids_by_student{$student}));
1049: return undef; # error
1050: }
1051:
1052: sub get_student {
1053: my ($student_id) = @_;
1054: if (! exists($students_by_id{$student_id}) ||
1055: ! defined($students_by_id{$student_id}) ||
1056: $students_by_id{$student_id} eq '') {
1057: my @Result = &Apache::lonmysql::get_rows($student_table);
1058: foreach (@Result) {
1059: $students_by_id{$_->[0]}=$_->[1];
1060: }
1061: }
1062: return $students_by_id{$student_id} if(exists($students_by_id{$student_id}));
1063: return undef; # error
1064: }
1.99 matthew 1065:
1066: ################################################
1067: ################################################
1068:
1069: =pod
1070:
1071: =item &clear_internal_caches()
1072:
1073: Causes the internal caches used in get_student_id, get_student,
1074: get_symb_id, get_symb, get_part_id, and get_part to be undef'd.
1075:
1076: Needs to be called before the first operation with the MySQL database
1077: for a given Apache request.
1078:
1079: =cut
1080:
1081: ################################################
1082: ################################################
1083: sub clear_internal_caches {
1084: $have_read_part_table = 0;
1085: undef(%ids_by_part);
1086: undef(%parts_by_id);
1087: $have_read_symb_table = 0;
1088: undef(%ids_by_symb);
1089: undef(%symbs_by_id);
1090: $have_read_student_table = 0;
1091: undef(%ids_by_student);
1092: undef(%students_by_id);
1093: }
1094:
1.57 matthew 1095:
1096: ################################################
1097: ################################################
1098:
1099: =pod
1100:
1.89 matthew 1101: =item &update_full_student_data($sname,$sdom,$courseid)
1102:
1103: Does a lonnet::dump on a student to populate the courses tables.
1104:
1105: Input: $sname, $sdom, $courseid
1106:
1107: Output: $returnstatus
1108:
1109: $returnstatus is a string describing any errors that occured. 'okay' is the
1110: default.
1111:
1112: This subroutine loads a students data using lonnet::dump and inserts
1113: it into the MySQL database. The inserts are done on three tables,
1114: $fulldump_response_table, $fulldump_part_table, and $fulldump_timestamp_table.
1115: The INSERT calls are made directly by this subroutine, not through lonmysql
1116: because we do a 'bulk'insert which takes advantage of MySQLs non-SQL
1117: compliant INSERT command to insert multiple rows at a time.
1118: If anything has gone wrong during this process, $returnstatus is updated with
1119: a description of the error.
1120:
1121: Once the "fulldump" tables are updated, the tables used for chart and
1122: spreadsheet (which hold only the current state of the student on their
1123: homework, not historical data) are updated. If all updates have occured
1124: successfully, the studentdata table is updated to reflect the time of the
1125: update.
1126:
1127: Notice we do not insert the data and immediately query it. This means it
1128: is possible for there to be data returned this first time that is not
1129: available the second time. CYA.
1130:
1131: =cut
1132:
1133: ################################################
1134: ################################################
1135: sub update_full_student_data {
1136: my ($sname,$sdom,$courseid) = @_;
1137: #
1138: # Set up database names
1139: &setup_table_names($courseid);
1140: #
1141: my $student_id = &get_student_id($sname,$sdom);
1142: my $student = $sname.':'.$sdom;
1143: #
1144: my $returnstatus = 'okay';
1145: #
1146: # Download students data
1147: my $time_of_retrieval = time;
1148: my @tmp = &Apache::lonnet::dump($courseid,$sdom,$sname);
1149: if (@tmp && $tmp[0] =~ /^error/) {
1150: $returnstatus = 'error retrieving full student data';
1151: return $returnstatus;
1152: } elsif (! @tmp) {
1153: $returnstatus = 'okay: no student data';
1154: return $returnstatus;
1155: }
1156: my %studentdata = @tmp;
1157: #
1158: # Get database handle and clean out the tables
1159: my $dbh = &Apache::lonmysql::get_dbh();
1160: $dbh->do('DELETE FROM '.$fulldump_response_table.' WHERE student_id='.
1161: $student_id);
1162: $dbh->do('DELETE FROM '.$fulldump_part_table.' WHERE student_id='.
1163: $student_id);
1164: $dbh->do('DELETE FROM '.$fulldump_timestamp_table.' WHERE student_id='.
1165: $student_id);
1166: #
1167: # Parse and store the data into a form we can handle
1168: my $partdata;
1169: my $respdata;
1170: while (my ($key,$value) = each(%studentdata)) {
1171: next if ($key =~ /^(\d+):(resource$|subnum$|keys:)/);
1172: my ($transaction,$symb,$parameter) = split(':',$key);
1173: my $symb_id = &get_symb_id($symb);
1174: if ($parameter eq 'timestamp') {
1175: # We can deal with 'timestamp' right away
1176: my @timestamp_storage = ($symb_id,$student_id,
1177: $transaction,$value);
1.98 matthew 1178: my $store_command = 'INSERT IGNORE INTO '.$fulldump_timestamp_table.
1.89 matthew 1179: " VALUES ('".join("','",@timestamp_storage)."');";
1180: $dbh->do($store_command);
1181: if ($dbh->err()) {
1182: &Apache::lonnet::logthis('unable to execute '.$store_command);
1183: &Apache::lonnet::logthis($dbh->errstr());
1184: }
1185: next;
1186: } elsif ($parameter eq 'version') {
1187: next;
1.90 matthew 1188: } elsif ($parameter =~ /^resource\.(.*)\.(tries|
1189: award|
1190: awarded|
1191: previous|
1192: solved|
1193: awarddetail|
1194: submission|
1195: submissiongrading|
1196: molecule)\s*$/x){
1.89 matthew 1197: # we do not have enough information to store an
1198: # entire row, so we save it up until later.
1199: my ($part_and_resp_id,$field) = ($1,$2);
1200: my ($part,$part_id,$resp,$resp_id);
1201: if ($part_and_resp_id =~ /\./) {
1202: ($part,$resp) = split(/\./,$part_and_resp_id);
1203: $part_id = &get_part_id($part);
1204: $resp_id = &get_part_id($resp);
1205: } else {
1206: $part_id = &get_part_id($part_and_resp_id);
1207: }
1.90 matthew 1208: # Deal with part specific data
1.89 matthew 1209: if ($field =~ /^(tries|award|awarded|previous)$/) {
1210: $partdata->{$symb_id}->{$part_id}->{$transaction}->{$field}=$value;
1211: }
1.90 matthew 1212: # deal with response specific data
1.89 matthew 1213: if (defined($resp_id) &&
1.100 matthew 1214: $field =~ /^(awarddetail|
1.90 matthew 1215: submission|
1216: submissiongrading|
1217: molecule)$/x) {
1.89 matthew 1218: if ($field eq 'submission') {
1219: # We have to be careful with user supplied input.
1220: # most of the time we are okay because it is escaped.
1221: # However, there is one wrinkle: submissions which end in
1222: # and odd number of '\' cause insert errors to occur.
1223: # Best trap this somehow...
1.102 matthew 1224: $value =~ s/\'/\\\'/g;
1.89 matthew 1225: my ($offensive_string) = ($value =~ /(\\+)$/);
1226: if (length($offensive_string) % 2) {
1227: $value =~ s/\\$/\\\\/;
1228: }
1229: }
1.90 matthew 1230: if ($field eq 'submissiongrading' ||
1231: $field eq 'molecule') {
1232: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{'response_specific'}=$field;
1233: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{'response_specific_value'}=$value;
1234: } else {
1235: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{$field}=$value;
1236: }
1.89 matthew 1237: }
1238: }
1239: }
1240: ##
1241: ## Store the part data
1.98 matthew 1242: my $store_command = 'INSERT IGNORE INTO '.$fulldump_part_table.
1.89 matthew 1243: ' VALUES '."\n";
1244: my $store_rows = 0;
1245: while (my ($symb_id,$hash1) = each (%$partdata)) {
1246: while (my ($part_id,$hash2) = each (%$hash1)) {
1247: while (my ($transaction,$data) = each (%$hash2)) {
1248: $store_command .= "('".join("','",$symb_id,$part_id,
1249: $student_id,
1250: $transaction,
1.101 matthew 1251: $data->{'tries'},
1.89 matthew 1252: $data->{'award'},
1253: $data->{'awarded'},
1254: $data->{'previous'})."'),";
1255: $store_rows++;
1256: }
1257: }
1258: }
1259: if ($store_rows) {
1260: chop($store_command);
1261: $dbh->do($store_command);
1262: if ($dbh->err) {
1263: $returnstatus = 'error storing part data';
1264: &Apache::lonnet::logthis('insert error '.$dbh->errstr());
1265: &Apache::lonnet::logthis("While attempting\n".$store_command);
1266: }
1267: }
1268: ##
1269: ## Store the response data
1.98 matthew 1270: $store_command = 'INSERT IGNORE INTO '.$fulldump_response_table.
1.89 matthew 1271: ' VALUES '."\n";
1272: $store_rows = 0;
1273: while (my ($symb_id,$hash1) = each (%$respdata)) {
1274: while (my ($part_id,$hash2) = each (%$hash1)) {
1275: while (my ($resp_id,$hash3) = each (%$hash2)) {
1276: while (my ($transaction,$data) = each (%$hash3)) {
1277: $store_command .= "('".join("','",$symb_id,$part_id,
1278: $resp_id,$student_id,
1279: $transaction,
1280: $data->{'awarddetail'},
1.90 matthew 1281: $data->{'response_specific'},
1282: $data->{'response_specific_value'},
1.89 matthew 1283: $data->{'submission'})."'),";
1284: $store_rows++;
1285: }
1286: }
1287: }
1288: }
1289: if ($store_rows) {
1290: chop($store_command);
1291: $dbh->do($store_command);
1292: if ($dbh->err) {
1293: $returnstatus = 'error storing response data';
1294: &Apache::lonnet::logthis('insert error '.$dbh->errstr());
1295: &Apache::lonnet::logthis("While attempting\n".$store_command);
1296: }
1297: }
1298: ##
1299: ## Update the students "current" data in the performance
1300: ## and parameters tables.
1301: my ($status,undef) = &store_student_data
1302: ($sname,$sdom,$courseid,
1303: &Apache::lonnet::convert_dump_to_currentdump(\%studentdata));
1304: if ($returnstatus eq 'okay' && $status ne 'okay') {
1305: $returnstatus = 'error storing current data:'.$status;
1306: } elsif ($status ne 'okay') {
1307: $returnstatus .= ' error storing current data:'.$status;
1308: }
1309: ##
1310: ## Update the students time......
1311: if ($returnstatus eq 'okay') {
1312: &Apache::lonmysql::replace_row
1313: ($studentdata_table,
1314: [$student_id,$time_of_retrieval,$time_of_retrieval,undef,undef]);
1315: }
1316: return $returnstatus;
1317: }
1318:
1319: ################################################
1320: ################################################
1321:
1322: =pod
1323:
1.57 matthew 1324: =item &update_student_data()
1325:
1326: Input: $sname, $sdom, $courseid
1327:
1328: Output: $returnstatus, \%student_data
1329:
1330: $returnstatus is a string describing any errors that occured. 'okay' is the
1331: default.
1332: \%student_data is the data returned by a call to lonnet::currentdump.
1333:
1334: This subroutine loads a students data using lonnet::currentdump and inserts
1335: it into the MySQL database. The inserts are done on two tables,
1336: $performance_table and $parameters_table. $parameters_table holds the data
1337: that is not included in $performance_table. See the description of
1338: $performance_table elsewhere in this file. The INSERT calls are made
1339: directly by this subroutine, not through lonmysql because we do a 'bulk'
1340: insert which takes advantage of MySQLs non-SQL compliant INSERT command to
1341: insert multiple rows at a time. If anything has gone wrong during this
1342: process, $returnstatus is updated with a description of the error and
1343: \%student_data is returned.
1344:
1345: Notice we do not insert the data and immediately query it. This means it
1346: is possible for there to be data returned this first time that is not
1347: available the second time. CYA.
1348:
1349: =cut
1350:
1351: ################################################
1352: ################################################
1353: sub update_student_data {
1354: my ($sname,$sdom,$courseid) = @_;
1355: #
1.60 matthew 1356: # Set up database names
1357: &setup_table_names($courseid);
1358: #
1.57 matthew 1359: my $student_id = &get_student_id($sname,$sdom);
1360: my $student = $sname.':'.$sdom;
1361: #
1362: my $returnstatus = 'okay';
1363: #
1364: # Download students data
1365: my $time_of_retrieval = time;
1366: my @tmp = &Apache::lonnet::currentdump($courseid,$sdom,$sname);
1367: if ((scalar(@tmp) > 0) && ($tmp[0] =~ /^error:/)) {
1368: &Apache::lonnet::logthis('error getting data for '.
1369: $sname.':'.$sdom.' in course '.$courseid.
1370: ':'.$tmp[0]);
1371: $returnstatus = 'error getting data';
1.79 matthew 1372: return ($returnstatus,undef);
1.57 matthew 1373: }
1374: if (scalar(@tmp) < 1) {
1375: return ('no data',undef);
1376: }
1377: my %student_data = @tmp;
1.89 matthew 1378: my @Results = &store_student_data($sname,$sdom,$courseid,\%student_data);
1379: #
1380: # Set the students update time
1.96 matthew 1381: if ($Results[0] eq 'okay') {
1.95 matthew 1382: &Apache::lonmysql::replace_row($studentdata_table,
1.89 matthew 1383: [$student_id,$time_of_retrieval,undef,undef,undef]);
1.95 matthew 1384: }
1.89 matthew 1385: #
1386: return @Results;
1387: }
1388:
1389: sub store_student_data {
1390: my ($sname,$sdom,$courseid,$student_data) = @_;
1391: #
1392: my $student_id = &get_student_id($sname,$sdom);
1393: my $student = $sname.':'.$sdom;
1394: #
1395: my $returnstatus = 'okay';
1.57 matthew 1396: #
1397: # Remove all of the students data from the table
1.60 matthew 1398: my $dbh = &Apache::lonmysql::get_dbh();
1399: $dbh->do('DELETE FROM '.$performance_table.' WHERE student_id='.
1400: $student_id);
1401: $dbh->do('DELETE FROM '.$parameters_table.' WHERE student_id='.
1402: $student_id);
1.57 matthew 1403: #
1404: # Store away the data
1405: #
1406: my $starttime = Time::HiRes::time;
1407: my $elapsed = 0;
1408: my $rows_stored;
1.98 matthew 1409: my $store_parameters_command = 'INSERT IGNORE INTO '.$parameters_table.
1.60 matthew 1410: ' VALUES '."\n";
1.61 matthew 1411: my $num_parameters = 0;
1.98 matthew 1412: my $store_performance_command = 'INSERT IGNORE INTO '.$performance_table.
1.60 matthew 1413: ' VALUES '."\n";
1.79 matthew 1414: return ('error',undef) if (! defined($dbh));
1.89 matthew 1415: while (my ($current_symb,$param_hash) = each(%{$student_data})) {
1.57 matthew 1416: #
1417: # make sure the symb is set up properly
1418: my $symb_id = &get_symb_id($current_symb);
1419: #
1420: # Load data into the tables
1.63 matthew 1421: while (my ($parameter,$value) = each(%$param_hash)) {
1.57 matthew 1422: my $newstring;
1.63 matthew 1423: if ($parameter !~ /(timestamp|resource\.(.*)\.(solved|tries|awarded|award|awarddetail|previous))/) {
1.57 matthew 1424: $newstring = "('".join("','",
1425: $symb_id,$student_id,
1.69 matthew 1426: $parameter)."',".
1427: $dbh->quote($value)."),\n";
1.61 matthew 1428: $num_parameters ++;
1.57 matthew 1429: if ($newstring !~ /''/) {
1430: $store_parameters_command .= $newstring;
1431: $rows_stored++;
1432: }
1433: }
1434: next if ($parameter !~ /^resource\.(.*)\.solved$/);
1435: #
1436: my $part = $1;
1437: my $part_id = &get_part_id($part);
1438: next if (!defined($part_id));
1439: my $solved = $value;
1440: my $tries = $param_hash->{'resource.'.$part.'.tries'};
1441: my $awarded = $param_hash->{'resource.'.$part.'.awarded'};
1442: my $award = $param_hash->{'resource.'.$part.'.award'};
1443: my $awarddetail = $param_hash->{'resource.'.$part.'.awarddetail'};
1444: my $timestamp = $param_hash->{'timestamp'};
1.60 matthew 1445: #
1.74 matthew 1446: $solved = '' if (! defined($solved));
1.57 matthew 1447: $tries = '' if (! defined($tries));
1448: $awarded = '' if (! defined($awarded));
1449: $award = '' if (! defined($award));
1450: $awarddetail = '' if (! defined($awarddetail));
1.73 matthew 1451: $newstring = "('".join("','",$symb_id,$student_id,$part_id,$part,
1.57 matthew 1452: $solved,$tries,$awarded,$award,
1.63 matthew 1453: $awarddetail,$timestamp)."'),\n";
1.57 matthew 1454: $store_performance_command .= $newstring;
1455: $rows_stored++;
1456: }
1457: }
1458: chop $store_parameters_command;
1.60 matthew 1459: chop $store_parameters_command;
1460: chop $store_performance_command;
1.57 matthew 1461: chop $store_performance_command;
1462: my $start = Time::HiRes::time;
1.94 matthew 1463: $dbh->do($store_performance_command);
1464: if ($dbh->err()) {
1465: &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1466: &Apache::lonnet::logthis('command = '.$store_performance_command);
1467: $returnstatus = 'error: unable to insert performance into database';
1468: return ($returnstatus,$student_data);
1469: }
1.61 matthew 1470: $dbh->do($store_parameters_command) if ($num_parameters>0);
1.57 matthew 1471: if ($dbh->err()) {
1472: &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1.61 matthew 1473: &Apache::lonnet::logthis('command = '.$store_parameters_command);
1.87 matthew 1474: &Apache::lonnet::logthis('rows_stored = '.$rows_stored);
1475: &Apache::lonnet::logthis('student_id = '.$student_id);
1.57 matthew 1476: $returnstatus = 'error: unable to insert parameters into database';
1.89 matthew 1477: return ($returnstatus,$student_data);
1.57 matthew 1478: }
1479: $elapsed += Time::HiRes::time - $start;
1.89 matthew 1480: return ($returnstatus,$student_data);
1.57 matthew 1481: }
1482:
1.89 matthew 1483: ######################################
1484: ######################################
1.57 matthew 1485:
1486: =pod
1487:
1.89 matthew 1488: =item &ensure_tables_are_set_up($courseid)
1.57 matthew 1489:
1.89 matthew 1490: Checks to be sure the MySQL tables for the given class are set up.
1491: If $courseid is omitted it will be obtained from the environment.
1.57 matthew 1492:
1.89 matthew 1493: Returns nothing on success and 'error' on failure
1.57 matthew 1494:
1495: =cut
1496:
1.89 matthew 1497: ######################################
1498: ######################################
1499: sub ensure_tables_are_set_up {
1500: my ($courseid) = @_;
1.61 matthew 1501: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1502: #
1503: # Clean out package variables
1.57 matthew 1504: &setup_table_names($courseid);
1505: #
1506: # if the tables do not exist, make them
1507: my @CurrentTable = &Apache::lonmysql::tables_in_db();
1.87 matthew 1508: my ($found_symb,$found_student,$found_part,$found_studentdata,
1.89 matthew 1509: $found_performance,$found_parameters,$found_fulldump_part,
1510: $found_fulldump_response,$found_fulldump_timestamp);
1.57 matthew 1511: foreach (@CurrentTable) {
1512: $found_symb = 1 if ($_ eq $symb_table);
1513: $found_student = 1 if ($_ eq $student_table);
1514: $found_part = 1 if ($_ eq $part_table);
1.87 matthew 1515: $found_studentdata = 1 if ($_ eq $studentdata_table);
1.57 matthew 1516: $found_performance = 1 if ($_ eq $performance_table);
1517: $found_parameters = 1 if ($_ eq $parameters_table);
1.89 matthew 1518: $found_fulldump_part = 1 if ($_ eq $fulldump_part_table);
1519: $found_fulldump_response = 1 if ($_ eq $fulldump_response_table);
1520: $found_fulldump_timestamp = 1 if ($_ eq $fulldump_timestamp_table);
1.57 matthew 1521: }
1.87 matthew 1522: if (!$found_symb || !$found_studentdata ||
1.57 matthew 1523: !$found_student || !$found_part ||
1.89 matthew 1524: !$found_performance || !$found_parameters ||
1525: !$found_fulldump_part || !$found_fulldump_response ||
1526: !$found_fulldump_timestamp ) {
1.57 matthew 1527: if (&init_dbs($courseid)) {
1.89 matthew 1528: return 'error';
1.57 matthew 1529: }
1530: }
1.89 matthew 1531: }
1532:
1533: ################################################
1534: ################################################
1535:
1536: =pod
1537:
1538: =item &ensure_current_data()
1539:
1540: Input: $sname, $sdom, $courseid
1541:
1542: Output: $status, $data
1543:
1544: This routine ensures the data for a given student is up to date.
1545: The $studentdata_table is queried to determine the time of the last update.
1546: If the students data is out of date, &update_student_data() is called.
1547: The return values from the call to &update_student_data() are returned.
1548:
1549: =cut
1550:
1551: ################################################
1552: ################################################
1553: sub ensure_current_data {
1554: my ($sname,$sdom,$courseid) = @_;
1555: my $status = 'okay'; # return value
1556: #
1557: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1558: &ensure_tables_are_set_up($courseid);
1.57 matthew 1559: #
1560: # Get the update time for the user
1561: my $updatetime = 0;
1.60 matthew 1562: my $modifiedtime = &Apache::lonnet::GetFileTimestamp
1563: ($sdom,$sname,$courseid.'.db',
1564: $Apache::lonnet::perlvar{'lonUsersDir'});
1.57 matthew 1565: #
1.87 matthew 1566: my $student_id = &get_student_id($sname,$sdom);
1567: my @Result = &Apache::lonmysql::get_rows($studentdata_table,
1568: "student_id ='$student_id'");
1.57 matthew 1569: my $data = undef;
1570: if (@Result) {
1571: $updatetime = $Result[0]->[1];
1572: }
1573: if ($modifiedtime > $updatetime) {
1574: ($status,$data) = &update_student_data($sname,$sdom,$courseid);
1575: }
1576: return ($status,$data);
1577: }
1578:
1579: ################################################
1580: ################################################
1581:
1582: =pod
1583:
1.89 matthew 1584: =item &ensure_current_full_data($sname,$sdom,$courseid)
1585:
1586: Input: $sname, $sdom, $courseid
1587:
1588: Output: $status
1589:
1590: This routine ensures the fulldata (the data from a lonnet::dump, not a
1591: lonnet::currentdump) for a given student is up to date.
1592: The $studentdata_table is queried to determine the time of the last update.
1593: If the students fulldata is out of date, &update_full_student_data() is
1594: called.
1595:
1596: The return value from the call to &update_full_student_data() is returned.
1597:
1598: =cut
1599:
1600: ################################################
1601: ################################################
1602: sub ensure_current_full_data {
1603: my ($sname,$sdom,$courseid) = @_;
1604: my $status = 'okay'; # return value
1605: #
1606: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1607: &ensure_tables_are_set_up($courseid);
1608: #
1609: # Get the update time for the user
1610: my $modifiedtime = &Apache::lonnet::GetFileTimestamp
1611: ($sdom,$sname,$courseid.'.db',
1612: $Apache::lonnet::perlvar{'lonUsersDir'});
1613: #
1614: my $student_id = &get_student_id($sname,$sdom);
1615: my @Result = &Apache::lonmysql::get_rows($studentdata_table,
1616: "student_id ='$student_id'");
1617: my $updatetime;
1618: if (@Result && ref($Result[0]) eq 'ARRAY') {
1619: $updatetime = $Result[0]->[2];
1620: }
1621: if (! defined($updatetime) || $modifiedtime > $updatetime) {
1622: $status = &update_full_student_data($sname,$sdom,$courseid);
1623: }
1624: return $status;
1625: }
1626:
1627: ################################################
1628: ################################################
1629:
1630: =pod
1631:
1.57 matthew 1632: =item &get_student_data_from_performance_cache()
1633:
1634: Input: $sname, $sdom, $symb, $courseid
1635:
1636: Output: hash reference containing the data for the given student.
1637: If $symb is undef, all the students data is returned.
1638:
1639: This routine is the heart of the local caching system. See the description
1640: of $performance_table, $symb_table, $student_table, and $part_table. The
1641: main task is building the MySQL request. The tables appear in the request
1642: in the order in which they should be parsed by MySQL. When searching
1643: on a student the $student_table is used to locate the 'student_id'. All
1644: rows in $performance_table which have a matching 'student_id' are returned,
1645: with data from $part_table and $symb_table which match the entries in
1646: $performance_table, 'part_id' and 'symb_id'. When searching on a symb,
1647: the $symb_table is processed first, with matching rows grabbed from
1648: $performance_table and filled in from $part_table and $student_table in
1649: that order.
1650:
1651: Running 'EXPLAIN ' on the 'SELECT' statements generated can be quite
1652: interesting, especially if you play with the order the tables are listed.
1653:
1654: =cut
1655:
1656: ################################################
1657: ################################################
1658: sub get_student_data_from_performance_cache {
1659: my ($sname,$sdom,$symb,$courseid)=@_;
1660: my $student = $sname.':'.$sdom if (defined($sname) && defined($sdom));
1.61 matthew 1661: &setup_table_names($courseid);
1.57 matthew 1662: #
1663: # Return hash
1664: my $studentdata;
1665: #
1666: my $dbh = &Apache::lonmysql::get_dbh();
1667: my $request = "SELECT ".
1.73 matthew 1668: "d.symb,a.part,a.solved,a.tries,a.awarded,a.award,a.awarddetail,".
1.63 matthew 1669: "a.timestamp ";
1.57 matthew 1670: if (defined($student)) {
1671: $request .= "FROM $student_table AS b ".
1672: "LEFT JOIN $performance_table AS a ON b.student_id=a.student_id ".
1.73 matthew 1673: # "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ".
1.57 matthew 1674: "LEFT JOIN $symb_table AS d ON d.symb_id = a.symb_id ".
1675: "WHERE student='$student'";
1676: if (defined($symb) && $symb ne '') {
1.67 matthew 1677: $request .= " AND d.symb=".$dbh->quote($symb);
1.57 matthew 1678: }
1679: } elsif (defined($symb) && $symb ne '') {
1680: $request .= "FROM $symb_table as d ".
1681: "LEFT JOIN $performance_table AS a ON d.symb_id=a.symb_id ".
1.73 matthew 1682: # "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ".
1.57 matthew 1683: "LEFT JOIN $student_table AS b ON b.student_id = a.student_id ".
1684: "WHERE symb='".$dbh->quote($symb)."'";
1685: }
1686: my $starttime = Time::HiRes::time;
1687: my $rows_retrieved = 0;
1688: my $sth = $dbh->prepare($request);
1689: $sth->execute();
1690: if ($sth->err()) {
1691: &Apache::lonnet::logthis("Unable to execute MySQL request:");
1692: &Apache::lonnet::logthis("\n".$request."\n");
1693: &Apache::lonnet::logthis("error is:".$sth->errstr());
1694: return undef;
1695: }
1696: foreach my $row (@{$sth->fetchall_arrayref}) {
1697: $rows_retrieved++;
1.63 matthew 1698: my ($symb,$part,$solved,$tries,$awarded,$award,$awarddetail,$time) =
1.57 matthew 1699: (@$row);
1700: my $base = 'resource.'.$part;
1701: $studentdata->{$symb}->{$base.'.solved'} = $solved;
1702: $studentdata->{$symb}->{$base.'.tries'} = $tries;
1703: $studentdata->{$symb}->{$base.'.awarded'} = $awarded;
1704: $studentdata->{$symb}->{$base.'.award'} = $award;
1705: $studentdata->{$symb}->{$base.'.awarddetail'} = $awarddetail;
1706: $studentdata->{$symb}->{'timestamp'} = $time if (defined($time) && $time ne '');
1.67 matthew 1707: }
1.97 matthew 1708: ## Get misc parameters
1709: $request = 'SELECT c.symb,a.parameter,a.value '.
1710: "FROM $student_table AS b ".
1711: "LEFT JOIN $parameters_table AS a ON b.student_id=a.student_id ".
1712: "LEFT JOIN $symb_table AS c ON c.symb_id = a.symb_id ".
1713: "WHERE student='$student'";
1714: if (defined($symb) && $symb ne '') {
1715: $request .= " AND c.symb=".$dbh->quote($symb);
1716: }
1717: $sth = $dbh->prepare($request);
1718: $sth->execute();
1719: if ($sth->err()) {
1720: &Apache::lonnet::logthis("Unable to execute MySQL request:");
1721: &Apache::lonnet::logthis("\n".$request."\n");
1722: &Apache::lonnet::logthis("error is:".$sth->errstr());
1723: if (defined($symb) && $symb ne '') {
1724: $studentdata = $studentdata->{$symb};
1725: }
1726: return $studentdata;
1727: }
1728: #
1729: foreach my $row (@{$sth->fetchall_arrayref}) {
1730: $rows_retrieved++;
1731: my ($symb,$parameter,$value) = (@$row);
1732: $studentdata->{$symb}->{$parameter} = $value;
1733: }
1734: #
1.67 matthew 1735: if (defined($symb) && $symb ne '') {
1736: $studentdata = $studentdata->{$symb};
1.57 matthew 1737: }
1738: return $studentdata;
1739: }
1740:
1741: ################################################
1742: ################################################
1743:
1744: =pod
1745:
1746: =item &get_current_state()
1747:
1748: Input: $sname,$sdom,$symb,$courseid
1749:
1750: Output: Described below
1.46 matthew 1751:
1.47 matthew 1752: Retrieve the current status of a students performance. $sname and
1.46 matthew 1753: $sdom are the only required parameters. If $symb is undef the results
1.47 matthew 1754: of an &Apache::lonnet::currentdump() will be returned.
1.46 matthew 1755: If $courseid is undef it will be retrieved from the environment.
1756:
1757: The return structure is based on &Apache::lonnet::currentdump. If
1758: $symb is unspecified, all the students data is returned in a hash of
1759: the form:
1760: (
1761: symb1 => { param1 => value1, param2 => value2 ... },
1762: symb2 => { param1 => value1, param2 => value2 ... },
1763: )
1764:
1765: If $symb is specified, a hash of
1766: (
1767: param1 => value1,
1768: param2 => value2,
1769: )
1770: is returned.
1771:
1.57 matthew 1772: If no data is found for $symb, or if the student has no performance data,
1.46 matthew 1773: an empty list is returned.
1774:
1775: =cut
1776:
1777: ################################################
1778: ################################################
1779: sub get_current_state {
1.47 matthew 1780: my ($sname,$sdom,$symb,$courseid,$forcedownload)=@_;
1781: #
1.46 matthew 1782: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1.47 matthew 1783: #
1.61 matthew 1784: return () if (! defined($sname) || ! defined($sdom));
1785: #
1.57 matthew 1786: my ($status,$data) = &ensure_current_data($sname,$sdom,$courseid);
1.77 matthew 1787: # &Apache::lonnet::logthis
1788: # ('sname = '.$sname.
1789: # ' domain = '.$sdom.
1790: # ' status = '.$status.
1791: # ' data is '.(defined($data)?'defined':'undefined'));
1.73 matthew 1792: # while (my ($symb,$hash) = each(%$data)) {
1793: # &Apache::lonnet::logthis($symb."\n----------------------------------");
1794: # while (my ($key,$value) = each (%$hash)) {
1795: # &Apache::lonnet::logthis(" ".$key." = ".$value);
1796: # }
1797: # }
1.47 matthew 1798: #
1.79 matthew 1799: if (defined($data) && defined($symb) && ref($data->{$symb})) {
1800: return %{$data->{$symb}};
1801: } elsif (defined($data) && ! defined($symb) && ref($data)) {
1802: return %$data;
1803: }
1804: if ($status eq 'no data') {
1.57 matthew 1805: return ();
1806: } else {
1807: if ($status ne 'okay' && $status ne '') {
1808: &Apache::lonnet::logthis('status = '.$status);
1.47 matthew 1809: return ();
1810: }
1.57 matthew 1811: my $returnhash = &get_student_data_from_performance_cache($sname,$sdom,
1812: $symb,$courseid);
1813: return %$returnhash if (defined($returnhash));
1.46 matthew 1814: }
1.57 matthew 1815: return ();
1.61 matthew 1816: }
1817:
1818: ################################################
1819: ################################################
1820:
1821: =pod
1822:
1823: =item &get_problem_statistics()
1824:
1825: Gather data on a given problem. The database is assumed to be
1826: populated and all local caching variables are assumed to be set
1827: properly. This means you need to call &ensure_current_data for
1828: the students you are concerned with prior to calling this routine.
1829:
1830: Inputs: $students, $symb, $part, $courseid
1831:
1.64 matthew 1832: =over 4
1833:
1834: =item $students is an array of hash references.
1835: Each hash must contain at least the 'username' and 'domain' of a student.
1836:
1837: =item $symb is the symb for the problem.
1838:
1839: =item $part is the part id you need statistics for
1840:
1841: =item $courseid is the course id, of course!
1842:
1843: =back
1844:
1.66 matthew 1845: Outputs: See the code for up to date information. A hash reference is
1846: returned. The hash has the following keys defined:
1.64 matthew 1847:
1848: =over 4
1849:
1.66 matthew 1850: =item num_students The number of students attempting the problem
1851:
1852: =item tries The total number of tries for the students
1853:
1854: =item max_tries The maximum number of tries taken
1855:
1856: =item mean_tries The average number of tries
1857:
1858: =item num_solved The number of students able to solve the problem
1859:
1860: =item num_override The number of students whose answer is 'correct_by_override'
1861:
1862: =item deg_of_diff The degree of difficulty of the problem
1863:
1864: =item std_tries The standard deviation of the number of tries
1865:
1866: =item skew_tries The skew of the number of tries
1.64 matthew 1867:
1.66 matthew 1868: =item per_wrong The number of students attempting the problem who were not
1869: able to answer it correctly.
1.64 matthew 1870:
1871: =back
1872:
1.61 matthew 1873: =cut
1874:
1875: ################################################
1876: ################################################
1877: sub get_problem_statistics {
1878: my ($students,$symb,$part,$courseid) = @_;
1879: return if (! defined($symb) || ! defined($part));
1880: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1881: #
1.100 matthew 1882: &setup_table_names($courseid);
1.61 matthew 1883: my $symb_id = &get_symb_id($symb);
1884: my $part_id = &get_part_id($part);
1885: my $stats_table = $courseid.'_problem_stats';
1886: #
1887: my $dbh = &Apache::lonmysql::get_dbh();
1888: return undef if (! defined($dbh));
1889: #
1890: $dbh->do('DROP TABLE '.$stats_table); # May return an error
1891: my $request =
1892: 'CREATE TEMPORARY TABLE '.$stats_table.
1.109 ! matthew 1893: ' SELECT student_id,solved,award,awarded,tries FROM '.$performance_table.
1.61 matthew 1894: ' WHERE symb_id='.$symb_id.' AND part_id='.$part_id;
1.64 matthew 1895: if (defined($students)) {
1896: $request .= ' AND ('.
1897: join(' OR ', map {'student_id='.
1898: &get_student_id($_->{'username'},
1899: $_->{'domain'})
1900: } @$students
1901: ).')';
1902: }
1.61 matthew 1903: # &Apache::lonnet::logthis($request);
1904: $dbh->do($request);
1.109 ! matthew 1905: # &Apache::lonnet::logthis('request = '.$/.$request);
! 1906: $request = 'SELECT COUNT(*),SUM(tries),MAX(tries),AVG(tries),STD(tries) '.
! 1907: 'FROM '.$stats_table;
1.61 matthew 1908: my ($num,$tries,$mod,$mean,$STD) = &execute_SQL_request
1.109 ! matthew 1909: ($dbh,$request);
! 1910: # &Apache::lonnet::logthis('request = '.$/.$request);
! 1911: $request = 'SELECT SUM(awarded) FROM '.$stats_table;
! 1912: my ($Solved) = &execute_SQL_request($dbh,$request);
! 1913: # &Apache::lonnet::logthis('request = '.$/.$request);
! 1914: $request = 'SELECT SUM(awarded) FROM '.$stats_table.
! 1915: " WHERE solved='correct_by_override'";
! 1916: # &Apache::lonnet::logthis('request = '.$/.$request);
! 1917: my ($solved) = &execute_SQL_request($dbh,$request);
! 1918: # $Solved = int($Solved);
! 1919: # $solved = int($solved);
! 1920: #
1.61 matthew 1921: $num = 0 if (! defined($num));
1922: $tries = 0 if (! defined($tries));
1923: $mod = 0 if (! defined($mod));
1924: $STD = 0 if (! defined($STD));
1925: $Solved = 0 if (! defined($Solved));
1926: $solved = 0 if (! defined($solved));
1927: #
1928: my $DegOfDiff = 'nan';
1.66 matthew 1929: $DegOfDiff = 1-($Solved)/$tries if ($tries>0);
1.61 matthew 1930:
1931: my $SKEW = 'nan';
1.66 matthew 1932: my $wrongpercent = 0;
1.61 matthew 1933: if ($num > 0) {
1934: ($SKEW) = &execute_SQL_request($dbh,'SELECT SQRT(SUM('.
1935: 'POWER(tries - '.$STD.',3)'.
1936: '))/'.$num.' FROM '.$stats_table);
1.66 matthew 1937: $wrongpercent=int(10*100*($num-$Solved+$solved)/$num)/10;
1.61 matthew 1938: }
1939: #
1.109 ! matthew 1940: # $dbh->do('DROP TABLE '.$stats_table); # May return an error
1.81 matthew 1941: #
1942: # Store in metadata
1943: #
1.80 www 1944: if ($num) {
1945: my %storestats=();
1946:
1.86 www 1947: my $urlres=(&Apache::lonnet::decode_symb($symb))[2];
1.80 www 1948:
1949: $storestats{$courseid.'___'.$urlres.'___timestamp'}=time;
1950: $storestats{$courseid.'___'.$urlres.'___stdno'}=$num;
1951: $storestats{$courseid.'___'.$urlres.'___avetries'}=$mean;
1952: $storestats{$courseid.'___'.$urlres.'___difficulty'}=$DegOfDiff;
1953:
1954: $urlres=~/^(\w+)\/(\w+)/;
1955: &Apache::lonnet::put('nohist_resevaldata',\%storestats,$1,$2);
1956: }
1.81 matthew 1957: #
1958: # Return result
1959: #
1.66 matthew 1960: return { num_students => $num,
1961: tries => $tries,
1962: max_tries => $mod,
1963: mean_tries => $mean,
1964: std_tries => $STD,
1965: skew_tries => $SKEW,
1966: num_solved => $Solved,
1967: num_override => $solved,
1968: per_wrong => $wrongpercent,
1.81 matthew 1969: deg_of_diff => $DegOfDiff };
1.61 matthew 1970: }
1971:
1972: sub execute_SQL_request {
1973: my ($dbh,$request)=@_;
1974: # &Apache::lonnet::logthis($request);
1975: my $sth = $dbh->prepare($request);
1976: $sth->execute();
1977: my $row = $sth->fetchrow_arrayref();
1978: if (ref($row) eq 'ARRAY' && scalar(@$row)>0) {
1979: return @$row;
1980: }
1981: return ();
1982: }
1983:
1.105 matthew 1984: sub get_student_data {
1985: my ($students,$courseid) = @_;
1986: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1987: &setup_table_names($courseid);
1988: my $dbh = &Apache::lonmysql::get_dbh();
1989: return undef if (! defined($dbh));
1990: my $request = 'SELECT '.
1991: 'student_id, student '.
1992: 'FROM '.$student_table;
1993: if (defined($students)) {
1994: $request .= ' WHERE ('.
1995: join(' OR ', map {'student_id='.
1996: &get_student_id($_->{'username'},
1997: $_->{'domain'})
1998: } @$students
1999: ).')';
2000: }
2001: $request.= ' ORDER BY student_id';
2002: my $sth = $dbh->prepare($request);
2003: $sth->execute();
2004: if ($dbh->err) {
2005: &Apache::lonnet::logthis('error = '.$dbh->errstr());
2006: return undef;
2007: }
2008: my $dataset = $sth->fetchall_arrayref();
2009: if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) {
2010: return $dataset;
2011: }
2012: }
2013:
1.108 matthew 2014: sub RD_student_id { return 0; }
2015: sub RD_awarddetail { return 1; }
2016: sub RD_response_eval { return 2; }
2017: sub RD_submission { return 3; }
2018: sub RD_timestamp { return 4; }
2019: sub RD_tries { return 5; }
2020: sub RD_sname { return 6; }
2021:
2022: sub get_response_data {
1.100 matthew 2023: my ($students,$symb,$response,$courseid) = @_;
1.103 matthew 2024: return undef if (! defined($symb) ||
1.100 matthew 2025: ! defined($response));
2026: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
2027: #
2028: &setup_table_names($courseid);
2029: my $symb_id = &get_symb_id($symb);
2030: my $response_id = &get_part_id($response);
2031: #
2032: my $dbh = &Apache::lonmysql::get_dbh();
2033: return undef if (! defined($dbh));
2034: my $request = 'SELECT '.
1.105 matthew 2035: 'a.student_id, a.awarddetail, a.response_specific_value, '.
1.108 matthew 2036: 'a.submission, b.timestamp, c.tries, d.student '.
1.100 matthew 2037: 'FROM '.$fulldump_response_table.' AS a '.
2038: 'LEFT JOIN '.$fulldump_timestamp_table.' AS b '.
2039: 'ON a.symb_id=b.symb_id AND a.student_id=b.student_id AND '.
2040: 'a.transaction = b.transaction '.
2041: 'LEFT JOIN '.$fulldump_part_table.' AS c '.
2042: 'ON a.symb_id=c.symb_id AND a.student_id=c.student_id AND '.
2043: 'a.part_id=c.part_id AND a.transaction = c.transaction '.
1.108 matthew 2044: 'LEFT JOIN '.$student_table.' AS d '.
2045: 'ON a.student_id=d.student_id '.
1.100 matthew 2046: 'WHERE '.
2047: 'a.symb_id='.$symb_id.' AND a.response_id='.$response_id;
2048: if (defined($students)) {
2049: $request .= ' AND ('.
1.103 matthew 2050: join(' OR ', map {'a.student_id='.
1.100 matthew 2051: &get_student_id($_->{'username'},
2052: $_->{'domain'})
2053: } @$students
2054: ).')';
2055: }
2056: $request .= ' ORDER BY b.timestamp';
1.103 matthew 2057: # &Apache::lonnet::logthis("request =\n".$request);
1.100 matthew 2058: my $sth = $dbh->prepare($request);
2059: $sth->execute();
1.105 matthew 2060: if ($dbh->err) {
2061: &Apache::lonnet::logthis('error = '.$dbh->errstr());
2062: return undef;
2063: }
1.100 matthew 2064: my $dataset = $sth->fetchall_arrayref();
2065: if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) {
1.103 matthew 2066: return $dataset;
1.100 matthew 2067: }
1.106 matthew 2068: }
1.108 matthew 2069:
2070: sub RT_student_id { return 0; }
2071: sub RT_awarded { return 1; }
2072: sub RT_tries { return 2; }
2073: sub RT_timestamp { return 3; }
1.106 matthew 2074:
2075: sub get_response_time_data {
1.107 matthew 2076: my ($students,$symb,$part,$courseid) = @_;
1.106 matthew 2077: return undef if (! defined($symb) ||
1.107 matthew 2078: ! defined($part));
1.106 matthew 2079: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
2080: #
2081: &setup_table_names($courseid);
2082: my $symb_id = &get_symb_id($symb);
1.107 matthew 2083: my $part_id = &get_part_id($part);
1.106 matthew 2084: #
2085: my $dbh = &Apache::lonmysql::get_dbh();
2086: return undef if (! defined($dbh));
2087: my $request = 'SELECT '.
1.107 matthew 2088: 'a.student_id, a.awarded, a.tries, b.timestamp '.
2089: 'FROM '.$fulldump_part_table.' AS a '.
1.106 matthew 2090: 'NATURAL LEFT JOIN '.$fulldump_timestamp_table.' AS b '.
2091: # 'ON a.symb_id=b.symb_id AND a.student_id=b.student_id AND '.
2092: # 'a.transaction = b.transaction '.
2093: 'WHERE '.
1.107 matthew 2094: 'a.symb_id='.$symb_id.' AND a.part_id='.$part_id;
1.106 matthew 2095: if (defined($students)) {
2096: $request .= ' AND ('.
2097: join(' OR ', map {'a.student_id='.
2098: &get_student_id($_->{'username'},
2099: $_->{'domain'})
2100: } @$students
2101: ).')';
2102: }
2103: $request .= ' ORDER BY b.timestamp';
2104: # &Apache::lonnet::logthis("request =\n".$request);
2105: my $sth = $dbh->prepare($request);
2106: $sth->execute();
2107: if ($dbh->err) {
2108: &Apache::lonnet::logthis('error = '.$dbh->errstr());
2109: return undef;
2110: }
2111: my $dataset = $sth->fetchall_arrayref();
2112: if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) {
2113: return $dataset;
2114: }
2115:
1.100 matthew 2116: }
1.61 matthew 2117:
2118: ################################################
2119: ################################################
2120:
2121: =pod
2122:
2123: =item &setup_table_names()
2124:
2125: input: course id
2126:
2127: output: none
2128:
2129: Cleans up the package variables for local caching.
2130:
2131: =cut
2132:
2133: ################################################
2134: ################################################
2135: sub setup_table_names {
2136: my ($courseid) = @_;
2137: if (! defined($courseid)) {
2138: $courseid = $ENV{'request.course.id'};
2139: }
2140: #
2141: if (! defined($current_course) || $current_course ne $courseid) {
2142: # Clear out variables
2143: $have_read_part_table = 0;
2144: undef(%ids_by_part);
2145: undef(%parts_by_id);
2146: $have_read_symb_table = 0;
2147: undef(%ids_by_symb);
2148: undef(%symbs_by_id);
2149: $have_read_student_table = 0;
2150: undef(%ids_by_student);
2151: undef(%students_by_id);
2152: #
2153: $current_course = $courseid;
2154: }
2155: #
2156: # Set up database names
2157: my $base_id = $courseid;
2158: $symb_table = $base_id.'_'.'symb';
2159: $part_table = $base_id.'_'.'part';
2160: $student_table = $base_id.'_'.'student';
1.87 matthew 2161: $studentdata_table = $base_id.'_'.'studentdata';
1.61 matthew 2162: $performance_table = $base_id.'_'.'performance';
2163: $parameters_table = $base_id.'_'.'parameters';
1.89 matthew 2164: $fulldump_part_table = $base_id.'_'.'partdata';
2165: $fulldump_response_table = $base_id.'_'.'responsedata';
2166: $fulldump_timestamp_table = $base_id.'_'.'timestampdata';
2167: #
2168: @Tables = (
2169: $symb_table,
2170: $part_table,
2171: $student_table,
2172: $studentdata_table,
2173: $performance_table,
2174: $parameters_table,
2175: $fulldump_part_table,
2176: $fulldump_response_table,
2177: $fulldump_timestamp_table,
2178: );
1.61 matthew 2179: return;
1.3 stredwic 2180: }
1.1 stredwic 2181:
1.35 matthew 2182: ################################################
2183: ################################################
2184:
2185: =pod
2186:
1.57 matthew 2187: =back
2188:
2189: =item End of Local Data Caching Subroutines
2190:
2191: =cut
2192:
2193: ################################################
2194: ################################################
2195:
1.89 matthew 2196: } # End scope of table identifiers
1.57 matthew 2197:
2198: ################################################
2199: ################################################
2200:
2201: =pod
2202:
2203: =head3 Classlist Subroutines
2204:
1.35 matthew 2205: =item &get_classlist();
2206:
2207: Retrieve the classist of a given class or of the current class. Student
2208: information is returned from the classlist.db file and, if needed,
2209: from the students environment.
2210:
2211: Optional arguments are $cid, $cdom, and $cnum (course id, course domain,
2212: and course number, respectively). Any omitted arguments will be taken
2213: from the current environment ($ENV{'request.course.id'},
2214: $ENV{'course.'.$cid.'.domain'}, and $ENV{'course.'.$cid.'.num'}).
2215:
2216: Returns a reference to a hash which contains:
2217: keys '$sname:$sdom'
1.54 bowersj2 2218: values [$sdom,$sname,$end,$start,$id,$section,$fullname,$status]
2219:
2220: The constant values CL_SDOM, CL_SNAME, CL_END, etc. can be used
2221: as indices into the returned list to future-proof clients against
2222: changes in the list order.
1.35 matthew 2223:
2224: =cut
2225:
2226: ################################################
2227: ################################################
1.54 bowersj2 2228:
2229: sub CL_SDOM { return 0; }
2230: sub CL_SNAME { return 1; }
2231: sub CL_END { return 2; }
2232: sub CL_START { return 3; }
2233: sub CL_ID { return 4; }
2234: sub CL_SECTION { return 5; }
2235: sub CL_FULLNAME { return 6; }
2236: sub CL_STATUS { return 7; }
1.35 matthew 2237:
2238: sub get_classlist {
2239: my ($cid,$cdom,$cnum) = @_;
2240: $cid = $cid || $ENV{'request.course.id'};
2241: $cdom = $cdom || $ENV{'course.'.$cid.'.domain'};
2242: $cnum = $cnum || $ENV{'course.'.$cid.'.num'};
1.57 matthew 2243: my $now = time;
1.35 matthew 2244: #
2245: my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
2246: while (my ($student,$info) = each(%classlist)) {
1.60 matthew 2247: if ($student =~ /^(con_lost|error|no_such_host)/i) {
2248: &Apache::lonnet::logthis('get_classlist error for '.$cid.':'.$student);
2249: return undef;
2250: }
1.35 matthew 2251: my ($sname,$sdom) = split(/:/,$student);
2252: my @Values = split(/:/,$info);
2253: my ($end,$start,$id,$section,$fullname);
2254: if (@Values > 2) {
2255: ($end,$start,$id,$section,$fullname) = @Values;
2256: } else { # We have to get the data ourselves
2257: ($end,$start) = @Values;
1.37 matthew 2258: $section = &Apache::lonnet::getsection($sdom,$sname,$cid);
1.35 matthew 2259: my %info=&Apache::lonnet::get('environment',
2260: ['firstname','middlename',
2261: 'lastname','generation','id'],
2262: $sdom, $sname);
2263: my ($tmp) = keys(%info);
2264: if ($tmp =~/^(con_lost|error|no_such_host)/i) {
2265: $fullname = 'not available';
2266: $id = 'not available';
1.38 matthew 2267: &Apache::lonnet::logthis('unable to retrieve environment '.
2268: 'for '.$sname.':'.$sdom);
1.35 matthew 2269: } else {
2270: $fullname = &ProcessFullName(@info{qw/lastname generation
2271: firstname middlename/});
2272: $id = $info{'id'};
2273: }
1.36 matthew 2274: # Update the classlist with this students information
2275: if ($fullname ne 'not available') {
2276: my $enrolldata = join(':',$end,$start,$id,$section,$fullname);
2277: my $reply=&Apache::lonnet::cput('classlist',
2278: {$student => $enrolldata},
2279: $cdom,$cnum);
2280: if ($reply !~ /^(ok|delayed)/) {
2281: &Apache::lonnet::logthis('Unable to update classlist for '.
2282: 'student '.$sname.':'.$sdom.
2283: ' error:'.$reply);
2284: }
2285: }
1.35 matthew 2286: }
2287: my $status='Expired';
2288: if(((!$end) || $now < $end) && ((!$start) || ($now > $start))) {
2289: $status='Active';
2290: }
2291: $classlist{$student} =
2292: [$sdom,$sname,$end,$start,$id,$section,$fullname,$status];
2293: }
2294: if (wantarray()) {
2295: return (\%classlist,['domain','username','end','start','id',
2296: 'section','fullname','status']);
2297: } else {
2298: return \%classlist;
2299: }
2300: }
2301:
1.1 stredwic 2302: # ----- END HELPER FUNCTIONS --------------------------------------------
2303:
2304: 1;
2305: __END__
1.36 matthew 2306:
1.35 matthew 2307:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>