Annotation of loncom/interface/loncoursedata.pm, revision 1.141
1.1 stredwic 1: # The LearningOnline Network with CAPA
2: #
1.141 ! albertel 3: # $Id: loncoursedata.pm,v 1.140 2004/10/07 22:12:47 albertel Exp $
1.1 stredwic 4: #
5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
8: #
9: # LON-CAPA is free software; you can redistribute it and/or modify
10: # it under the terms of the GNU General Public License as published by
11: # the Free Software Foundation; either version 2 of the License, or
12: # (at your option) any later version.
13: #
14: # LON-CAPA is distributed in the hope that it will be useful,
15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: # GNU General Public License for more details.
18: #
19: # You should have received a copy of the GNU General Public License
20: # along with LON-CAPA; if not, write to the Free Software
21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27: ###
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.139 matthew 158: $title = $previous->compTitle;
1.51 matthew 159: $symb = $previous->symb();
160: $src = $previous->src();
1.75 matthew 161: $randompick = $previous->randompick();
1.45 matthew 162: my $newmap = { title => $title,
163: src => $src,
164: symb => $symb,
165: type => 'container',
166: num_assess => 0,
1.75 matthew 167: randompick => $randompick,
1.45 matthew 168: contents => [],
169: };
170: push (@{$currentmap->{'contents'}},$newmap); # this is permanent
1.49 matthew 171: push (@Sequences,$newmap);
1.45 matthew 172: push (@Nested_Sequences, $newmap); # this is a stack
173: next;
174: }
175: if ($curRes == $iterator->END_MAP()) {
176: pop(@Nested_Sequences);
177: next;
178: }
179: next if (! ref($curRes));
1.121 matthew 180: next if (! $curRes->is_problem() && $curRes->src() !~ /\.survey$/);
1.45 matthew 181: # Okay, from here on out we only deal with assessments
1.139 matthew 182: $title = $curRes->compTitle();
1.45 matthew 183: $symb = $curRes->symb();
184: $src = $curRes->src();
185: my $parts = $curRes->parts();
1.87 matthew 186: my %partdata;
187: foreach my $part (@$parts) {
1.88 matthew 188: my @Responses = $curRes->responseType($part);
189: my @Ids = $curRes->responseIds($part);
190: $partdata{$part}->{'ResponseTypes'}= \@Responses;
191: $partdata{$part}->{'ResponseIds'} = \@Ids;
1.135 matthew 192: $partdata{$part}->{'Survey'} = $curRes->is_survey($part);
1.91 matthew 193: # Count how many responses of each type there are in this part
194: foreach (@Responses) {
195: $partdata{$part}->{$_}++;
196: }
1.87 matthew 197: }
1.45 matthew 198: my $assessment = { title => $title,
199: src => $src,
200: symb => $symb,
201: type => 'assessment',
1.53 matthew 202: parts => $parts,
203: num_parts => scalar(@$parts),
1.87 matthew 204: partdata => \%partdata,
1.45 matthew 205: };
1.49 matthew 206: push(@Assessments,$assessment);
1.45 matthew 207: push(@{$currentmap->{'contents'}},$assessment);
208: $currentmap->{'num_assess'}++;
1.53 matthew 209: $currentmap->{'num_assess_parts'}+= scalar(@$parts);
1.45 matthew 210: }
1.49 matthew 211: return ($top,\@Sequences,\@Assessments);
1.45 matthew 212: }
1.50 matthew 213:
1.4 stredwic 214: sub LoadDiscussion {
1.13 stredwic 215: my ($courseID)=@_;
1.5 minaeibi 216: my %Discuss=();
217: my %contrib=&Apache::lonnet::dump(
218: $courseID,
219: $ENV{'course.'.$courseID.'.domain'},
220: $ENV{'course.'.$courseID.'.num'});
221:
222: #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
223:
1.4 stredwic 224: foreach my $temp(keys %contrib) {
225: if ($temp=~/^version/) {
226: my $ver=$contrib{$temp};
227: my ($dummy,$prb)=split(':',$temp);
228: for (my $idx=1; $idx<=$ver; $idx++ ) {
229: my $name=$contrib{"$idx:$prb:sendername"};
1.5 minaeibi 230: $Discuss{"$name:$prb"}=$idx;
1.4 stredwic 231: }
232: }
233: }
1.5 minaeibi 234:
235: return \%Discuss;
1.1 stredwic 236: }
237:
1.71 matthew 238: ################################################
239: ################################################
240:
241: =pod
242:
1.47 matthew 243: =item &make_into_hash($values);
244:
245: Returns a reference to a hash as described by $values. $values is
246: assumed to be the result of
1.57 matthew 247: join(':',map {&Apache::lonnet::escape($_)} %orighash);
1.47 matthew 248:
249: This is a helper function for get_current_state.
250:
251: =cut
252:
253: ################################################
254: ################################################
255: sub make_into_hash {
256: my $values = shift;
257: my %tmp = map { &Apache::lonnet::unescape($_); }
258: split(':',$values);
259: return \%tmp;
260: }
261:
262:
263: ################################################
264: ################################################
265:
266: =pod
267:
1.57 matthew 268: =head1 LOCAL DATA CACHING SUBROUTINES
269:
270: The local caching is done using MySQL. There is no fall-back implementation
271: if MySQL is not running.
272:
273: The programmers interface is to call &get_current_state() or some other
274: primary interface subroutine (described below). The internals of this
275: storage system are documented here.
276:
277: There are six tables used to store student performance data (the results of
278: a dumpcurrent). Each of these tables is created in MySQL with a name of
279: $courseid_*****, where ***** is 'symb', 'part', or whatever is appropriate
280: for the table. The tables and their purposes are described below.
281:
282: Some notes before we get started.
283:
284: Each table must have a PRIMARY KEY, which is a column or set of columns which
285: will serve to uniquely identify a row of data. NULL is not allowed!
286:
287: INDEXes work best on integer data.
288:
289: JOIN is used to combine data from many tables into one output.
290:
291: lonmysql.pm is used for some of the interface, specifically the table creation
292: calls. The inserts are done in bulk by directly calling the database handler.
293: The SELECT ... JOIN statement used to retrieve the data does not have an
294: interface in lonmysql.pm and I shudder at the thought of writing one.
295:
296: =head3 Table Descriptions
297:
298: =over 4
299:
1.89 matthew 300: =item Tables used to store meta information
301:
302: The following tables hold data required to keep track of the current status
303: of a students data in the tables or to look up the students data in the tables.
304:
305: =over 4
306:
1.57 matthew 307: =item $symb_table
308:
309: The symb_table has two columns. The first is a 'symb_id' and the second
310: is the text name for the 'symb' (limited to 64k). The 'symb_id' is generated
311: automatically by MySQL so inserts should be done on this table with an
312: empty first element. This table has its PRIMARY KEY on the 'symb_id'.
313:
314: =item $part_table
315:
316: The part_table has two columns. The first is a 'part_id' and the second
317: is the text name for the 'part' (limited to 100 characters). The 'part_id' is
318: generated automatically by MySQL so inserts should be done on this table with
319: an empty first element. This table has its PRIMARY KEY on the 'part' (100
320: characters) and a KEY on 'part_id'.
321:
322: =item $student_table
323:
1.113 matthew 324: The student_table has 7 columns. The first is a 'student_id' assigned by
325: MySQL. The second is 'student' which is username:domain. The third through
326: fifth are 'section', 'status' (enrollment status), and 'classification'
327: (to be used in the future). The sixth and seventh ('updatetime' and
328: 'fullupdatetime') contain the time of last update and full update of student
329: data. This table has its PRIMARY KEY on the 'student_id' column and is indexed
330: on 'student', 'section', and 'status'.
1.89 matthew 331:
332: =back
333:
334: =item Tables used to store current status data
335:
336: The following tables store data only about the students current status on
337: a problem, meaning only the data related to the last attempt on a problem.
338:
339: =over 4
1.57 matthew 340:
341: =item $performance_table
342:
343: The performance_table has 9 columns. The first three are 'symb_id',
344: 'student_id', and 'part_id'. These comprise the PRIMARY KEY for this table
345: and are directly related to the $symb_table, $student_table, and $part_table
346: described above. MySQL does better indexing on numeric items than text,
347: so we use these three "index tables". The remaining columns are
348: 'solved', 'tries', 'awarded', 'award', 'awarddetail', and 'timestamp'.
349: These are either the MySQL type TINYTEXT or various integers ('tries' and
350: 'timestamp'). This table has KEYs of 'student_id' and 'symb_id'.
351: For use of this table, see the functions described below.
352:
353: =item $parameters_table
354:
355: The parameters_table holds the data that does not fit neatly into the
356: performance_table. The parameters table has four columns: 'symb_id',
357: 'student_id', 'parameter', and 'value'. 'symb_id', 'student_id', and
358: 'parameter' comprise the PRIMARY KEY for this table. 'parameter' is
359: limited to 255 characters. 'value' is limited to 64k characters.
360:
361: =back
362:
1.89 matthew 363: =item Tables used for storing historic data
364:
365: The following tables are used to store almost all of the transactions a student
366: has made on a homework problem. See loncapa/docs/homework/datastorage for
367: specific information about each of the parameters stored.
368:
369: =over 4
370:
371: =item $fulldump_response_table
372:
373: The response table holds data (documented in loncapa/docs/homework/datastorage)
374: associated with a particular response id which is stored when a student
375: attempts a problem. The following are the columns of the table, in order:
376: 'symb_id','part_id','response_id','student_id','transaction','tries',
1.93 matthew 377: 'awarddetail', 'response_specific' (data particular to the response
1.89 matthew 378: type), 'response_specific_value', and 'submission (the text of the students
379: submission). The primary key is based on the first five columns listed above.
380:
381: =item $fulldump_part_table
382:
383: The part table holds data (documented in loncapa/docs/homework/datastorage)
384: associated with a particular part id which is stored when a student attempts
385: a problem. The following are the columns of the table, in order:
386: 'symb_id','part_id','student_id','transaction','tries','award','awarded',
387: and 'previous'. The primary key is based on the first five columns listed
388: above.
389:
390: =item $fulldump_timestamp_table
391:
392: The timestamp table holds the timestamps of the transactions which are
393: stored in $fulldump_response_table and $fulldump_part_table. This data is
394: about both the response and part data. Columns: 'symb_id','student_id',
395: 'transaction', and 'timestamp'.
396: The primary key is based on the first 3 columns.
397:
1.127 matthew 398: =item $weight_table
399:
400: The weight table holds the weight for the problems used in the class.
401: Whereas the weight of a problem can vary by section and student the data
402: here is applied to the class as a whole.
403: Columns: 'symb_id','part_id','response_id','weight'.
404:
1.89 matthew 405: =back
406:
407: =back
408:
1.57 matthew 409: =head3 Important Subroutines
410:
411: Here is a brief overview of the subroutines which are likely to be of
412: interest:
413:
414: =over 4
415:
416: =item &get_current_state(): programmers interface.
417:
418: =item &init_dbs(): table creation
419:
420: =item &update_student_data(): data storage calls
421:
422: =item &get_student_data_from_performance_cache(): data retrieval
423:
424: =back
425:
426: =head3 Main Documentation
427:
428: =over 4
429:
430: =cut
431:
432: ################################################
433: ################################################
434:
435: ################################################
436: ################################################
1.89 matthew 437: { # Begin scope of table identifiers
1.57 matthew 438:
439: my $current_course ='';
440: my $symb_table;
441: my $part_table;
442: my $student_table;
443: my $performance_table;
444: my $parameters_table;
1.89 matthew 445: my $fulldump_response_table;
446: my $fulldump_part_table;
447: my $fulldump_timestamp_table;
1.127 matthew 448: my $weight_table;
1.57 matthew 449:
1.89 matthew 450: my @Tables;
1.57 matthew 451: ################################################
452: ################################################
453:
454: =pod
455:
456: =item &init_dbs()
457:
458: Input: course id
459:
460: Output: 0 on success, positive integer on error
461:
462: This routine issues the calls to lonmysql to create the tables used to
463: store student data.
464:
465: =cut
466:
467: ################################################
468: ################################################
469: sub init_dbs {
1.134 matthew 470: my ($courseid,$drop) = @_;
1.57 matthew 471: &setup_table_names($courseid);
472: #
1.73 matthew 473: # Drop any of the existing tables
1.134 matthew 474: if ($drop) {
475: foreach my $table (@Tables) {
476: &Apache::lonmysql::drop_table($table);
477: }
1.73 matthew 478: }
479: #
1.57 matthew 480: # Note - changes to this table must be reflected in the code that
481: # stores the data (calls &Apache::lonmysql::store_row with this table
482: # id
483: my $symb_table_def = {
484: id => $symb_table,
485: permanent => 'no',
486: columns => [{ name => 'symb_id',
487: type => 'MEDIUMINT UNSIGNED',
488: restrictions => 'NOT NULL',
489: auto_inc => 'yes', },
490: { name => 'symb',
491: type => 'MEDIUMTEXT',
492: restrictions => 'NOT NULL'},
493: ],
494: 'PRIMARY KEY' => ['symb_id'],
495: };
496: #
497: my $part_table_def = {
498: id => $part_table,
499: permanent => 'no',
500: columns => [{ name => 'part_id',
501: type => 'MEDIUMINT UNSIGNED',
502: restrictions => 'NOT NULL',
503: auto_inc => 'yes', },
504: { name => 'part',
1.132 matthew 505: type => 'VARCHAR(100) BINARY',
1.57 matthew 506: restrictions => 'NOT NULL'},
507: ],
508: 'PRIMARY KEY' => ['part (100)'],
509: 'KEY' => [{ columns => ['part_id']},],
510: };
511: #
512: my $student_table_def = {
513: id => $student_table,
514: permanent => 'no',
515: columns => [{ name => 'student_id',
516: type => 'MEDIUMINT UNSIGNED',
517: restrictions => 'NOT NULL',
518: auto_inc => 'yes', },
519: { name => 'student',
1.132 matthew 520: type => 'VARCHAR(100) BINARY',
1.113 matthew 521: restrictions => 'NOT NULL UNIQUE'},
522: { name => 'section',
1.132 matthew 523: type => 'VARCHAR(100) BINARY',
1.113 matthew 524: restrictions => 'NOT NULL'},
525: { name => 'status',
1.132 matthew 526: type => 'VARCHAR(15) BINARY',
1.57 matthew 527: restrictions => 'NOT NULL'},
1.85 matthew 528: { name => 'classification',
1.132 matthew 529: type => 'VARCHAR(100) BINARY', },
1.57 matthew 530: { name => 'updatetime',
1.89 matthew 531: type => 'INT UNSIGNED'},
532: { name => 'fullupdatetime',
533: type => 'INT UNSIGNED'},
1.57 matthew 534: ],
1.87 matthew 535: 'PRIMARY KEY' => ['student_id'],
1.113 matthew 536: 'KEY' => [{ columns => ['student (100)',
537: 'section (100)',
538: 'status (15)',]},],
1.57 matthew 539: };
540: #
541: my $performance_table_def = {
542: id => $performance_table,
543: permanent => 'no',
544: columns => [{ name => 'symb_id',
545: type => 'MEDIUMINT UNSIGNED',
546: restrictions => 'NOT NULL' },
547: { name => 'student_id',
548: type => 'MEDIUMINT UNSIGNED',
549: restrictions => 'NOT NULL' },
550: { name => 'part_id',
551: type => 'MEDIUMINT UNSIGNED',
552: restrictions => 'NOT NULL' },
1.73 matthew 553: { name => 'part',
1.132 matthew 554: type => 'VARCHAR(100) BINARY',
1.73 matthew 555: restrictions => 'NOT NULL'},
1.57 matthew 556: { name => 'solved',
557: type => 'TINYTEXT' },
558: { name => 'tries',
559: type => 'SMALLINT UNSIGNED' },
560: { name => 'awarded',
1.127 matthew 561: type => 'REAL' },
1.57 matthew 562: { name => 'award',
563: type => 'TINYTEXT' },
564: { name => 'awarddetail',
565: type => 'TINYTEXT' },
566: { name => 'timestamp',
567: type => 'INT UNSIGNED'},
568: ],
569: 'PRIMARY KEY' => ['symb_id','student_id','part_id'],
570: 'KEY' => [{ columns=>['student_id'] },
571: { columns=>['symb_id'] },],
572: };
573: #
1.89 matthew 574: my $fulldump_part_table_def = {
575: id => $fulldump_part_table,
576: permanent => 'no',
577: columns => [
578: { name => 'symb_id',
579: type => 'MEDIUMINT UNSIGNED',
580: restrictions => 'NOT NULL' },
581: { name => 'part_id',
582: type => 'MEDIUMINT UNSIGNED',
583: restrictions => 'NOT NULL' },
584: { name => 'student_id',
585: type => 'MEDIUMINT UNSIGNED',
586: restrictions => 'NOT NULL' },
587: { name => 'transaction',
588: type => 'MEDIUMINT UNSIGNED',
589: restrictions => 'NOT NULL' },
590: { name => 'tries',
591: type => 'SMALLINT UNSIGNED',
592: restrictions => 'NOT NULL' },
593: { name => 'award',
594: type => 'TINYTEXT' },
595: { name => 'awarded',
1.127 matthew 596: type => 'REAL' },
1.89 matthew 597: { name => 'previous',
598: type => 'SMALLINT UNSIGNED' },
599: # { name => 'regrader',
600: # type => 'TINYTEXT' },
601: # { name => 'afterduedate',
602: # type => 'TINYTEXT' },
603: ],
604: 'PRIMARY KEY' => ['symb_id','part_id','student_id','transaction'],
605: 'KEY' => [
606: { columns=>['symb_id'] },
607: { columns=>['part_id'] },
608: { columns=>['student_id'] },
609: ],
610: };
611: #
612: my $fulldump_response_table_def = {
613: id => $fulldump_response_table,
614: permanent => 'no',
615: columns => [
616: { name => 'symb_id',
617: type => 'MEDIUMINT UNSIGNED',
618: restrictions => 'NOT NULL' },
619: { name => 'part_id',
620: type => 'MEDIUMINT UNSIGNED',
621: restrictions => 'NOT NULL' },
622: { name => 'response_id',
623: type => 'MEDIUMINT UNSIGNED',
624: restrictions => 'NOT NULL' },
625: { name => 'student_id',
626: type => 'MEDIUMINT UNSIGNED',
627: restrictions => 'NOT NULL' },
628: { name => 'transaction',
629: type => 'MEDIUMINT UNSIGNED',
630: restrictions => 'NOT NULL' },
631: { name => 'awarddetail',
632: type => 'TINYTEXT' },
633: # { name => 'message',
1.132 matthew 634: # type => 'CHAR BINARY'},
1.89 matthew 635: { name => 'response_specific',
636: type => 'TINYTEXT' },
637: { name => 'response_specific_value',
638: type => 'TINYTEXT' },
639: { name => 'submission',
640: type => 'TEXT'},
641: ],
642: 'PRIMARY KEY' => ['symb_id','part_id','response_id','student_id',
643: 'transaction'],
644: 'KEY' => [
645: { columns=>['symb_id'] },
646: { columns=>['part_id','response_id'] },
647: { columns=>['student_id'] },
648: ],
649: };
650: my $fulldump_timestamp_table_def = {
651: id => $fulldump_timestamp_table,
652: permanent => 'no',
653: columns => [
654: { name => 'symb_id',
655: type => 'MEDIUMINT UNSIGNED',
656: restrictions => 'NOT NULL' },
657: { name => 'student_id',
658: type => 'MEDIUMINT UNSIGNED',
659: restrictions => 'NOT NULL' },
660: { name => 'transaction',
661: type => 'MEDIUMINT UNSIGNED',
662: restrictions => 'NOT NULL' },
663: { name => 'timestamp',
664: type => 'INT UNSIGNED'},
665: ],
666: 'PRIMARY KEY' => ['symb_id','student_id','transaction'],
667: 'KEY' => [
668: { columns=>['symb_id'] },
669: { columns=>['student_id'] },
670: { columns=>['transaction'] },
671: ],
672: };
673: #
1.57 matthew 674: my $parameters_table_def = {
675: id => $parameters_table,
676: permanent => 'no',
677: columns => [{ name => 'symb_id',
678: type => 'MEDIUMINT UNSIGNED',
679: restrictions => 'NOT NULL' },
680: { name => 'student_id',
681: type => 'MEDIUMINT UNSIGNED',
682: restrictions => 'NOT NULL' },
683: { name => 'parameter',
684: type => 'TINYTEXT',
685: restrictions => 'NOT NULL' },
686: { name => 'value',
687: type => 'MEDIUMTEXT' },
688: ],
689: 'PRIMARY KEY' => ['symb_id','student_id','parameter (255)'],
690: };
691: #
1.127 matthew 692: my $weight_table_def = {
693: id => $weight_table,
694: permanent => 'no',
695: columns => [{ name => 'symb_id',
696: type => 'MEDIUMINT UNSIGNED',
697: restrictions => 'NOT NULL' },
698: { name => 'part_id',
699: type => 'MEDIUMINT UNSIGNED',
700: restrictions => 'NOT NULL' },
701: { name => 'weight',
702: type => 'REAL',
703: restrictions => 'NOT NULL' },
704: ],
705: 'PRIMARY KEY' => ['symb_id','part_id'],
706: };
707: #
1.57 matthew 708: # Create the tables
709: my $tableid;
710: $tableid = &Apache::lonmysql::create_table($symb_table_def);
711: if (! defined($tableid)) {
712: &Apache::lonnet::logthis("error creating symb_table: ".
713: &Apache::lonmysql::get_error());
714: return 1;
715: }
716: #
717: $tableid = &Apache::lonmysql::create_table($part_table_def);
718: if (! defined($tableid)) {
719: &Apache::lonnet::logthis("error creating part_table: ".
720: &Apache::lonmysql::get_error());
721: return 2;
722: }
723: #
724: $tableid = &Apache::lonmysql::create_table($student_table_def);
725: if (! defined($tableid)) {
726: &Apache::lonnet::logthis("error creating student_table: ".
727: &Apache::lonmysql::get_error());
728: return 3;
729: }
730: #
731: $tableid = &Apache::lonmysql::create_table($performance_table_def);
732: if (! defined($tableid)) {
733: &Apache::lonnet::logthis("error creating preformance_table: ".
734: &Apache::lonmysql::get_error());
735: return 5;
736: }
737: #
738: $tableid = &Apache::lonmysql::create_table($parameters_table_def);
739: if (! defined($tableid)) {
740: &Apache::lonnet::logthis("error creating parameters_table: ".
741: &Apache::lonmysql::get_error());
742: return 6;
743: }
1.89 matthew 744: #
745: $tableid = &Apache::lonmysql::create_table($fulldump_part_table_def);
746: if (! defined($tableid)) {
747: &Apache::lonnet::logthis("error creating fulldump_part_table: ".
748: &Apache::lonmysql::get_error());
749: return 7;
750: }
751: #
752: $tableid = &Apache::lonmysql::create_table($fulldump_response_table_def);
753: if (! defined($tableid)) {
754: &Apache::lonnet::logthis("error creating fulldump_response_table: ".
755: &Apache::lonmysql::get_error());
756: return 8;
757: }
758: $tableid = &Apache::lonmysql::create_table($fulldump_timestamp_table_def);
759: if (! defined($tableid)) {
760: &Apache::lonnet::logthis("error creating fulldump_timestamp_table: ".
761: &Apache::lonmysql::get_error());
762: return 9;
763: }
1.127 matthew 764: $tableid = &Apache::lonmysql::create_table($weight_table_def);
765: if (! defined($tableid)) {
766: &Apache::lonnet::logthis("error creating weight_table: ".
767: &Apache::lonmysql::get_error());
768: return 10;
769: }
1.57 matthew 770: return 0;
1.70 matthew 771: }
772:
773: ################################################
774: ################################################
775:
776: =pod
777:
778: =item &delete_caches()
779:
780: =cut
781:
782: ################################################
783: ################################################
784: sub delete_caches {
785: my $courseid = shift;
786: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
787: #
788: &setup_table_names($courseid);
789: #
790: my $dbh = &Apache::lonmysql::get_dbh();
1.89 matthew 791: foreach my $table (@Tables) {
1.70 matthew 792: my $command = 'DROP TABLE '.$table.';';
793: $dbh->do($command);
794: if ($dbh->err) {
795: &Apache::lonnet::logthis($command.' resulted in error: '.$dbh->errstr);
796: }
797: }
798: return;
1.57 matthew 799: }
800:
801: ################################################
802: ################################################
803:
804: =pod
805:
806: =item &get_part_id()
807:
808: Get the MySQL id of a problem part string.
809:
810: Input: $part
811:
812: Output: undef on error, integer $part_id on success.
813:
814: =item &get_part()
815:
816: Get the string describing a part from the MySQL id of the problem part.
817:
818: Input: $part_id
819:
820: Output: undef on error, $part string on success.
821:
822: =cut
823:
824: ################################################
825: ################################################
826:
1.61 matthew 827: my $have_read_part_table = 0;
1.57 matthew 828: my %ids_by_part;
829: my %parts_by_id;
830:
831: sub get_part_id {
832: my ($part) = @_;
1.61 matthew 833: $part = 0 if (! defined($part));
834: if (! $have_read_part_table) {
835: my @Result = &Apache::lonmysql::get_rows($part_table);
836: foreach (@Result) {
837: $ids_by_part{$_->[1]}=$_->[0];
838: }
839: $have_read_part_table = 1;
840: }
1.57 matthew 841: if (! exists($ids_by_part{$part})) {
842: &Apache::lonmysql::store_row($part_table,[undef,$part]);
843: undef(%ids_by_part);
844: my @Result = &Apache::lonmysql::get_rows($part_table);
845: foreach (@Result) {
846: $ids_by_part{$_->[1]}=$_->[0];
847: }
848: }
849: return $ids_by_part{$part} if (exists($ids_by_part{$part}));
850: return undef; # error
851: }
852:
853: sub get_part {
854: my ($part_id) = @_;
855: if (! exists($parts_by_id{$part_id}) ||
856: ! defined($parts_by_id{$part_id}) ||
857: $parts_by_id{$part_id} eq '') {
858: my @Result = &Apache::lonmysql::get_rows($part_table);
859: foreach (@Result) {
860: $parts_by_id{$_->[0]}=$_->[1];
861: }
862: }
863: return $parts_by_id{$part_id} if(exists($parts_by_id{$part_id}));
864: return undef; # error
865: }
866:
867: ################################################
868: ################################################
869:
870: =pod
871:
872: =item &get_symb_id()
873:
874: Get the MySQL id of a symb.
875:
876: Input: $symb
877:
878: Output: undef on error, integer $symb_id on success.
879:
880: =item &get_symb()
881:
882: Get the symb associated with a MySQL symb_id.
883:
884: Input: $symb_id
885:
886: Output: undef on error, $symb on success.
887:
888: =cut
889:
890: ################################################
891: ################################################
892:
1.61 matthew 893: my $have_read_symb_table = 0;
1.57 matthew 894: my %ids_by_symb;
895: my %symbs_by_id;
896:
897: sub get_symb_id {
898: my ($symb) = @_;
1.61 matthew 899: if (! $have_read_symb_table) {
900: my @Result = &Apache::lonmysql::get_rows($symb_table);
901: foreach (@Result) {
902: $ids_by_symb{$_->[1]}=$_->[0];
903: }
904: $have_read_symb_table = 1;
905: }
1.57 matthew 906: if (! exists($ids_by_symb{$symb})) {
907: &Apache::lonmysql::store_row($symb_table,[undef,$symb]);
908: undef(%ids_by_symb);
909: my @Result = &Apache::lonmysql::get_rows($symb_table);
910: foreach (@Result) {
911: $ids_by_symb{$_->[1]}=$_->[0];
912: }
913: }
914: return $ids_by_symb{$symb} if(exists( $ids_by_symb{$symb}));
915: return undef; # error
916: }
917:
918: sub get_symb {
919: my ($symb_id) = @_;
920: if (! exists($symbs_by_id{$symb_id}) ||
921: ! defined($symbs_by_id{$symb_id}) ||
922: $symbs_by_id{$symb_id} eq '') {
923: my @Result = &Apache::lonmysql::get_rows($symb_table);
924: foreach (@Result) {
925: $symbs_by_id{$_->[0]}=$_->[1];
926: }
927: }
928: return $symbs_by_id{$symb_id} if(exists( $symbs_by_id{$symb_id}));
929: return undef; # error
930: }
931:
932: ################################################
933: ################################################
934:
935: =pod
936:
937: =item &get_student_id()
938:
939: Get the MySQL id of a student.
940:
941: Input: $sname, $dom
942:
943: Output: undef on error, integer $student_id on success.
944:
945: =item &get_student()
946:
947: Get student username:domain associated with the MySQL student_id.
948:
949: Input: $student_id
950:
951: Output: undef on error, string $student (username:domain) on success.
952:
953: =cut
954:
955: ################################################
956: ################################################
957:
1.61 matthew 958: my $have_read_student_table = 0;
1.57 matthew 959: my %ids_by_student;
960: my %students_by_id;
961:
962: sub get_student_id {
963: my ($sname,$sdom) = @_;
964: my $student = $sname.':'.$sdom;
1.61 matthew 965: if (! $have_read_student_table) {
966: my @Result = &Apache::lonmysql::get_rows($student_table);
967: foreach (@Result) {
968: $ids_by_student{$_->[1]}=$_->[0];
969: }
970: $have_read_student_table = 1;
971: }
1.57 matthew 972: if (! exists($ids_by_student{$student})) {
1.113 matthew 973: &populate_student_table();
1.57 matthew 974: undef(%ids_by_student);
1.113 matthew 975: undef(%students_by_id);
1.57 matthew 976: my @Result = &Apache::lonmysql::get_rows($student_table);
977: foreach (@Result) {
978: $ids_by_student{$_->[1]}=$_->[0];
979: }
980: }
981: return $ids_by_student{$student} if(exists( $ids_by_student{$student}));
982: return undef; # error
983: }
984:
985: sub get_student {
986: my ($student_id) = @_;
987: if (! exists($students_by_id{$student_id}) ||
988: ! defined($students_by_id{$student_id}) ||
989: $students_by_id{$student_id} eq '') {
990: my @Result = &Apache::lonmysql::get_rows($student_table);
991: foreach (@Result) {
992: $students_by_id{$_->[0]}=$_->[1];
993: }
994: }
995: return $students_by_id{$student_id} if(exists($students_by_id{$student_id}));
996: return undef; # error
997: }
1.99 matthew 998:
1.113 matthew 999: sub populate_student_table {
1000: my ($courseid) = @_;
1001: if (! defined($courseid)) {
1002: $courseid = $ENV{'request.course.id'};
1003: }
1004: #
1005: &setup_table_names($courseid);
1.134 matthew 1006: &init_dbs($courseid,0);
1.113 matthew 1007: my $dbh = &Apache::lonmysql::get_dbh();
1008: my $request = 'INSERT IGNORE INTO '.$student_table.
1009: "(student,section,status) VALUES ";
1010: my $classlist = &get_classlist($courseid);
1011: my $student_count=0;
1012: while (my ($student,$data) = each %$classlist) {
1013: my ($section,$status) = ($data->[&CL_SECTION()],
1014: $data->[&CL_STATUS()]);
1015: if ($section eq '' || $section =~ /^\s*$/) {
1016: $section = 'none';
1017: }
1018: $request .= "('".$student."','".$section."','".$status."'),";
1019: $student_count++;
1020: }
1021: return if ($student_count == 0);
1022: chop($request);
1023: $dbh->do($request);
1024: if ($dbh->err()) {
1025: &Apache::lonnet::logthis("error ".$dbh->errstr().
1026: " occured executing \n".
1027: $request);
1028: }
1029: return;
1030: }
1031:
1.99 matthew 1032: ################################################
1033: ################################################
1034:
1035: =pod
1036:
1037: =item &clear_internal_caches()
1038:
1039: Causes the internal caches used in get_student_id, get_student,
1040: get_symb_id, get_symb, get_part_id, and get_part to be undef'd.
1041:
1042: Needs to be called before the first operation with the MySQL database
1043: for a given Apache request.
1044:
1045: =cut
1046:
1047: ################################################
1048: ################################################
1049: sub clear_internal_caches {
1050: $have_read_part_table = 0;
1051: undef(%ids_by_part);
1052: undef(%parts_by_id);
1053: $have_read_symb_table = 0;
1054: undef(%ids_by_symb);
1055: undef(%symbs_by_id);
1056: $have_read_student_table = 0;
1057: undef(%ids_by_student);
1058: undef(%students_by_id);
1059: }
1060:
1.57 matthew 1061:
1062: ################################################
1063: ################################################
1064:
1065: =pod
1066:
1.89 matthew 1067: =item &update_full_student_data($sname,$sdom,$courseid)
1068:
1069: Does a lonnet::dump on a student to populate the courses tables.
1070:
1071: Input: $sname, $sdom, $courseid
1072:
1073: Output: $returnstatus
1074:
1075: $returnstatus is a string describing any errors that occured. 'okay' is the
1076: default.
1077:
1078: This subroutine loads a students data using lonnet::dump and inserts
1079: it into the MySQL database. The inserts are done on three tables,
1080: $fulldump_response_table, $fulldump_part_table, and $fulldump_timestamp_table.
1081: The INSERT calls are made directly by this subroutine, not through lonmysql
1082: because we do a 'bulk'insert which takes advantage of MySQLs non-SQL
1083: compliant INSERT command to insert multiple rows at a time.
1084: If anything has gone wrong during this process, $returnstatus is updated with
1085: a description of the error.
1086:
1087: Once the "fulldump" tables are updated, the tables used for chart and
1088: spreadsheet (which hold only the current state of the student on their
1089: homework, not historical data) are updated. If all updates have occured
1.113 matthew 1090: successfully, $student_table is updated to reflect the time of the update.
1.89 matthew 1091:
1092: Notice we do not insert the data and immediately query it. This means it
1093: is possible for there to be data returned this first time that is not
1094: available the second time. CYA.
1095:
1096: =cut
1097:
1098: ################################################
1099: ################################################
1100: sub update_full_student_data {
1101: my ($sname,$sdom,$courseid) = @_;
1102: #
1103: # Set up database names
1104: &setup_table_names($courseid);
1105: #
1106: my $student_id = &get_student_id($sname,$sdom);
1107: my $student = $sname.':'.$sdom;
1108: #
1109: my $returnstatus = 'okay';
1110: #
1111: # Download students data
1112: my $time_of_retrieval = time;
1113: my @tmp = &Apache::lonnet::dump($courseid,$sdom,$sname);
1114: if (@tmp && $tmp[0] =~ /^error/) {
1115: $returnstatus = 'error retrieving full student data';
1116: return $returnstatus;
1117: } elsif (! @tmp) {
1118: $returnstatus = 'okay: no student data';
1119: return $returnstatus;
1120: }
1121: my %studentdata = @tmp;
1122: #
1123: # Get database handle and clean out the tables
1124: my $dbh = &Apache::lonmysql::get_dbh();
1125: $dbh->do('DELETE FROM '.$fulldump_response_table.' WHERE student_id='.
1126: $student_id);
1127: $dbh->do('DELETE FROM '.$fulldump_part_table.' WHERE student_id='.
1128: $student_id);
1129: $dbh->do('DELETE FROM '.$fulldump_timestamp_table.' WHERE student_id='.
1130: $student_id);
1131: #
1132: # Parse and store the data into a form we can handle
1133: my $partdata;
1134: my $respdata;
1135: while (my ($key,$value) = each(%studentdata)) {
1136: next if ($key =~ /^(\d+):(resource$|subnum$|keys:)/);
1137: my ($transaction,$symb,$parameter) = split(':',$key);
1138: my $symb_id = &get_symb_id($symb);
1139: if ($parameter eq 'timestamp') {
1140: # We can deal with 'timestamp' right away
1141: my @timestamp_storage = ($symb_id,$student_id,
1142: $transaction,$value);
1.98 matthew 1143: my $store_command = 'INSERT IGNORE INTO '.$fulldump_timestamp_table.
1.89 matthew 1144: " VALUES ('".join("','",@timestamp_storage)."');";
1145: $dbh->do($store_command);
1146: if ($dbh->err()) {
1147: &Apache::lonnet::logthis('unable to execute '.$store_command);
1148: &Apache::lonnet::logthis($dbh->errstr());
1149: }
1150: next;
1151: } elsif ($parameter eq 'version') {
1152: next;
1.90 matthew 1153: } elsif ($parameter =~ /^resource\.(.*)\.(tries|
1154: award|
1155: awarded|
1156: previous|
1157: solved|
1158: awarddetail|
1159: submission|
1160: submissiongrading|
1161: molecule)\s*$/x){
1.89 matthew 1162: # we do not have enough information to store an
1163: # entire row, so we save it up until later.
1164: my ($part_and_resp_id,$field) = ($1,$2);
1165: my ($part,$part_id,$resp,$resp_id);
1166: if ($part_and_resp_id =~ /\./) {
1167: ($part,$resp) = split(/\./,$part_and_resp_id);
1168: $part_id = &get_part_id($part);
1169: $resp_id = &get_part_id($resp);
1170: } else {
1171: $part_id = &get_part_id($part_and_resp_id);
1172: }
1.90 matthew 1173: # Deal with part specific data
1.89 matthew 1174: if ($field =~ /^(tries|award|awarded|previous)$/) {
1175: $partdata->{$symb_id}->{$part_id}->{$transaction}->{$field}=$value;
1176: }
1.90 matthew 1177: # deal with response specific data
1.89 matthew 1178: if (defined($resp_id) &&
1.100 matthew 1179: $field =~ /^(awarddetail|
1.90 matthew 1180: submission|
1181: submissiongrading|
1182: molecule)$/x) {
1.89 matthew 1183: if ($field eq 'submission') {
1184: # We have to be careful with user supplied input.
1185: # most of the time we are okay because it is escaped.
1186: # However, there is one wrinkle: submissions which end in
1187: # and odd number of '\' cause insert errors to occur.
1188: # Best trap this somehow...
1.116 matthew 1189: $value = $dbh->quote($value);
1.89 matthew 1190: }
1.90 matthew 1191: if ($field eq 'submissiongrading' ||
1192: $field eq 'molecule') {
1193: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{'response_specific'}=$field;
1194: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{'response_specific_value'}=$value;
1195: } else {
1196: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{$field}=$value;
1197: }
1.89 matthew 1198: }
1199: }
1200: }
1201: ##
1202: ## Store the part data
1.98 matthew 1203: my $store_command = 'INSERT IGNORE INTO '.$fulldump_part_table.
1.89 matthew 1204: ' VALUES '."\n";
1205: my $store_rows = 0;
1206: while (my ($symb_id,$hash1) = each (%$partdata)) {
1207: while (my ($part_id,$hash2) = each (%$hash1)) {
1208: while (my ($transaction,$data) = each (%$hash2)) {
1209: $store_command .= "('".join("','",$symb_id,$part_id,
1210: $student_id,
1211: $transaction,
1.101 matthew 1212: $data->{'tries'},
1.89 matthew 1213: $data->{'award'},
1214: $data->{'awarded'},
1215: $data->{'previous'})."'),";
1216: $store_rows++;
1217: }
1218: }
1219: }
1220: if ($store_rows) {
1221: chop($store_command);
1222: $dbh->do($store_command);
1223: if ($dbh->err) {
1224: $returnstatus = 'error storing part data';
1225: &Apache::lonnet::logthis('insert error '.$dbh->errstr());
1226: &Apache::lonnet::logthis("While attempting\n".$store_command);
1227: }
1228: }
1229: ##
1230: ## Store the response data
1.98 matthew 1231: $store_command = 'INSERT IGNORE INTO '.$fulldump_response_table.
1.89 matthew 1232: ' VALUES '."\n";
1233: $store_rows = 0;
1234: while (my ($symb_id,$hash1) = each (%$respdata)) {
1235: while (my ($part_id,$hash2) = each (%$hash1)) {
1236: while (my ($resp_id,$hash3) = each (%$hash2)) {
1237: while (my ($transaction,$data) = each (%$hash3)) {
1.112 matthew 1238: my $submission = $data->{'submission'};
1239: # We have to be careful with user supplied input.
1240: # most of the time we are okay because it is escaped.
1241: # However, there is one wrinkle: submissions which end in
1242: # and odd number of '\' cause insert errors to occur.
1243: # Best trap this somehow...
1244: $submission = $dbh->quote($submission);
1245: $store_command .= "('".
1246: join("','",$symb_id,$part_id,
1247: $resp_id,$student_id,
1248: $transaction,
1249: $data->{'awarddetail'},
1250: $data->{'response_specific'},
1251: $data->{'response_specific_value'}).
1252: "',".$submission."),";
1.89 matthew 1253: $store_rows++;
1254: }
1255: }
1256: }
1257: }
1258: if ($store_rows) {
1259: chop($store_command);
1260: $dbh->do($store_command);
1261: if ($dbh->err) {
1262: $returnstatus = 'error storing response data';
1263: &Apache::lonnet::logthis('insert error '.$dbh->errstr());
1264: &Apache::lonnet::logthis("While attempting\n".$store_command);
1265: }
1266: }
1267: ##
1268: ## Update the students "current" data in the performance
1269: ## and parameters tables.
1270: my ($status,undef) = &store_student_data
1271: ($sname,$sdom,$courseid,
1272: &Apache::lonnet::convert_dump_to_currentdump(\%studentdata));
1273: if ($returnstatus eq 'okay' && $status ne 'okay') {
1274: $returnstatus = 'error storing current data:'.$status;
1275: } elsif ($status ne 'okay') {
1276: $returnstatus .= ' error storing current data:'.$status;
1277: }
1278: ##
1279: ## Update the students time......
1280: if ($returnstatus eq 'okay') {
1.113 matthew 1281: &store_updatetime($student_id,$time_of_retrieval,$time_of_retrieval);
1282: if ($dbh->err) {
1283: if ($returnstatus eq 'okay') {
1284: $returnstatus = 'error updating student time';
1285: } else {
1286: $returnstatus = 'error updating student time';
1287: }
1288: }
1.89 matthew 1289: }
1290: return $returnstatus;
1291: }
1292:
1293: ################################################
1294: ################################################
1295:
1296: =pod
1297:
1.57 matthew 1298: =item &update_student_data()
1299:
1300: Input: $sname, $sdom, $courseid
1301:
1302: Output: $returnstatus, \%student_data
1303:
1304: $returnstatus is a string describing any errors that occured. 'okay' is the
1305: default.
1306: \%student_data is the data returned by a call to lonnet::currentdump.
1307:
1308: This subroutine loads a students data using lonnet::currentdump and inserts
1309: it into the MySQL database. The inserts are done on two tables,
1310: $performance_table and $parameters_table. $parameters_table holds the data
1311: that is not included in $performance_table. See the description of
1312: $performance_table elsewhere in this file. The INSERT calls are made
1313: directly by this subroutine, not through lonmysql because we do a 'bulk'
1314: insert which takes advantage of MySQLs non-SQL compliant INSERT command to
1315: insert multiple rows at a time. If anything has gone wrong during this
1316: process, $returnstatus is updated with a description of the error and
1317: \%student_data is returned.
1318:
1319: Notice we do not insert the data and immediately query it. This means it
1320: is possible for there to be data returned this first time that is not
1321: available the second time. CYA.
1322:
1323: =cut
1324:
1325: ################################################
1326: ################################################
1327: sub update_student_data {
1328: my ($sname,$sdom,$courseid) = @_;
1329: #
1.60 matthew 1330: # Set up database names
1331: &setup_table_names($courseid);
1332: #
1.57 matthew 1333: my $student_id = &get_student_id($sname,$sdom);
1334: my $student = $sname.':'.$sdom;
1335: #
1336: my $returnstatus = 'okay';
1337: #
1338: # Download students data
1339: my $time_of_retrieval = time;
1340: my @tmp = &Apache::lonnet::currentdump($courseid,$sdom,$sname);
1341: if ((scalar(@tmp) > 0) && ($tmp[0] =~ /^error:/)) {
1342: &Apache::lonnet::logthis('error getting data for '.
1343: $sname.':'.$sdom.' in course '.$courseid.
1344: ':'.$tmp[0]);
1345: $returnstatus = 'error getting data';
1.79 matthew 1346: return ($returnstatus,undef);
1.57 matthew 1347: }
1348: if (scalar(@tmp) < 1) {
1349: return ('no data',undef);
1350: }
1351: my %student_data = @tmp;
1.89 matthew 1352: my @Results = &store_student_data($sname,$sdom,$courseid,\%student_data);
1353: #
1354: # Set the students update time
1.96 matthew 1355: if ($Results[0] eq 'okay') {
1.113 matthew 1356: &store_updatetime($student_id,$time_of_retrieval,$time_of_retrieval);
1.95 matthew 1357: }
1.89 matthew 1358: #
1359: return @Results;
1360: }
1361:
1.113 matthew 1362: sub store_updatetime {
1363: my ($student_id,$updatetime,$fullupdatetime)=@_;
1364: my $values = '';
1365: if (defined($updatetime)) {
1366: $values = 'updatetime='.$updatetime.' ';
1367: }
1368: if (defined($fullupdatetime)) {
1369: if ($values ne '') {
1370: $values .= ',';
1371: }
1372: $values .= 'fullupdatetime='.$fullupdatetime.' ';
1373: }
1374: return if ($values eq '');
1375: my $dbh = &Apache::lonmysql::get_dbh();
1376: my $request = 'UPDATE '.$student_table.' SET '.$values.
1377: ' WHERE student_id='.$student_id.' LIMIT 1';
1378: $dbh->do($request);
1379: }
1380:
1.89 matthew 1381: sub store_student_data {
1382: my ($sname,$sdom,$courseid,$student_data) = @_;
1383: #
1384: my $student_id = &get_student_id($sname,$sdom);
1385: my $student = $sname.':'.$sdom;
1386: #
1387: my $returnstatus = 'okay';
1.57 matthew 1388: #
1389: # Remove all of the students data from the table
1.60 matthew 1390: my $dbh = &Apache::lonmysql::get_dbh();
1391: $dbh->do('DELETE FROM '.$performance_table.' WHERE student_id='.
1392: $student_id);
1393: $dbh->do('DELETE FROM '.$parameters_table.' WHERE student_id='.
1394: $student_id);
1.57 matthew 1395: #
1396: # Store away the data
1397: #
1398: my $starttime = Time::HiRes::time;
1399: my $elapsed = 0;
1400: my $rows_stored;
1.98 matthew 1401: my $store_parameters_command = 'INSERT IGNORE INTO '.$parameters_table.
1.60 matthew 1402: ' VALUES '."\n";
1.61 matthew 1403: my $num_parameters = 0;
1.98 matthew 1404: my $store_performance_command = 'INSERT IGNORE INTO '.$performance_table.
1.60 matthew 1405: ' VALUES '."\n";
1.79 matthew 1406: return ('error',undef) if (! defined($dbh));
1.89 matthew 1407: while (my ($current_symb,$param_hash) = each(%{$student_data})) {
1.57 matthew 1408: #
1409: # make sure the symb is set up properly
1410: my $symb_id = &get_symb_id($current_symb);
1411: #
1412: # Load data into the tables
1.63 matthew 1413: while (my ($parameter,$value) = each(%$param_hash)) {
1.57 matthew 1414: my $newstring;
1.63 matthew 1415: if ($parameter !~ /(timestamp|resource\.(.*)\.(solved|tries|awarded|award|awarddetail|previous))/) {
1.57 matthew 1416: $newstring = "('".join("','",
1417: $symb_id,$student_id,
1.69 matthew 1418: $parameter)."',".
1419: $dbh->quote($value)."),\n";
1.61 matthew 1420: $num_parameters ++;
1.57 matthew 1421: if ($newstring !~ /''/) {
1422: $store_parameters_command .= $newstring;
1423: $rows_stored++;
1424: }
1425: }
1426: next if ($parameter !~ /^resource\.(.*)\.solved$/);
1427: #
1428: my $part = $1;
1429: my $part_id = &get_part_id($part);
1430: next if (!defined($part_id));
1431: my $solved = $value;
1432: my $tries = $param_hash->{'resource.'.$part.'.tries'};
1433: my $awarded = $param_hash->{'resource.'.$part.'.awarded'};
1434: my $award = $param_hash->{'resource.'.$part.'.award'};
1435: my $awarddetail = $param_hash->{'resource.'.$part.'.awarddetail'};
1436: my $timestamp = $param_hash->{'timestamp'};
1.60 matthew 1437: #
1.74 matthew 1438: $solved = '' if (! defined($solved));
1.57 matthew 1439: $tries = '' if (! defined($tries));
1440: $awarded = '' if (! defined($awarded));
1441: $award = '' if (! defined($award));
1442: $awarddetail = '' if (! defined($awarddetail));
1.73 matthew 1443: $newstring = "('".join("','",$symb_id,$student_id,$part_id,$part,
1.57 matthew 1444: $solved,$tries,$awarded,$award,
1.63 matthew 1445: $awarddetail,$timestamp)."'),\n";
1.57 matthew 1446: $store_performance_command .= $newstring;
1447: $rows_stored++;
1448: }
1449: }
1450: chop $store_parameters_command;
1.60 matthew 1451: chop $store_parameters_command;
1452: chop $store_performance_command;
1.57 matthew 1453: chop $store_performance_command;
1454: my $start = Time::HiRes::time;
1.94 matthew 1455: $dbh->do($store_performance_command);
1456: if ($dbh->err()) {
1457: &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1458: &Apache::lonnet::logthis('command = '.$store_performance_command);
1459: $returnstatus = 'error: unable to insert performance into database';
1460: return ($returnstatus,$student_data);
1461: }
1.61 matthew 1462: $dbh->do($store_parameters_command) if ($num_parameters>0);
1.57 matthew 1463: if ($dbh->err()) {
1464: &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1.61 matthew 1465: &Apache::lonnet::logthis('command = '.$store_parameters_command);
1.87 matthew 1466: &Apache::lonnet::logthis('rows_stored = '.$rows_stored);
1467: &Apache::lonnet::logthis('student_id = '.$student_id);
1.57 matthew 1468: $returnstatus = 'error: unable to insert parameters into database';
1.89 matthew 1469: return ($returnstatus,$student_data);
1.57 matthew 1470: }
1471: $elapsed += Time::HiRes::time - $start;
1.89 matthew 1472: return ($returnstatus,$student_data);
1.57 matthew 1473: }
1474:
1.89 matthew 1475: ######################################
1476: ######################################
1.57 matthew 1477:
1478: =pod
1479:
1.89 matthew 1480: =item &ensure_tables_are_set_up($courseid)
1.57 matthew 1481:
1.89 matthew 1482: Checks to be sure the MySQL tables for the given class are set up.
1483: If $courseid is omitted it will be obtained from the environment.
1.57 matthew 1484:
1.89 matthew 1485: Returns nothing on success and 'error' on failure
1.57 matthew 1486:
1487: =cut
1488:
1.89 matthew 1489: ######################################
1490: ######################################
1491: sub ensure_tables_are_set_up {
1492: my ($courseid) = @_;
1.61 matthew 1493: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1494: #
1495: # Clean out package variables
1.57 matthew 1496: &setup_table_names($courseid);
1497: #
1498: # if the tables do not exist, make them
1499: my @CurrentTable = &Apache::lonmysql::tables_in_db();
1.113 matthew 1500: my ($found_symb,$found_student,$found_part,
1.89 matthew 1501: $found_performance,$found_parameters,$found_fulldump_part,
1.127 matthew 1502: $found_fulldump_response,$found_fulldump_timestamp,
1503: $found_weight);
1.57 matthew 1504: foreach (@CurrentTable) {
1505: $found_symb = 1 if ($_ eq $symb_table);
1506: $found_student = 1 if ($_ eq $student_table);
1507: $found_part = 1 if ($_ eq $part_table);
1508: $found_performance = 1 if ($_ eq $performance_table);
1509: $found_parameters = 1 if ($_ eq $parameters_table);
1.89 matthew 1510: $found_fulldump_part = 1 if ($_ eq $fulldump_part_table);
1511: $found_fulldump_response = 1 if ($_ eq $fulldump_response_table);
1512: $found_fulldump_timestamp = 1 if ($_ eq $fulldump_timestamp_table);
1.127 matthew 1513: $found_weight = 1 if ($_ eq $weight_table);
1.57 matthew 1514: }
1.127 matthew 1515: if (!$found_symb ||
1516: !$found_student || !$found_part ||
1517: !$found_performance || !$found_parameters ||
1.89 matthew 1518: !$found_fulldump_part || !$found_fulldump_response ||
1.127 matthew 1519: !$found_fulldump_timestamp || !$found_weight ) {
1.134 matthew 1520: if (&init_dbs($courseid,1)) {
1.89 matthew 1521: return 'error';
1.57 matthew 1522: }
1523: }
1.89 matthew 1524: }
1525:
1526: ################################################
1527: ################################################
1528:
1529: =pod
1530:
1531: =item &ensure_current_data()
1532:
1533: Input: $sname, $sdom, $courseid
1534:
1535: Output: $status, $data
1536:
1537: This routine ensures the data for a given student is up to date.
1.113 matthew 1538: The $student_table is queried to determine the time of the last update.
1.89 matthew 1539: If the students data is out of date, &update_student_data() is called.
1540: The return values from the call to &update_student_data() are returned.
1541:
1542: =cut
1543:
1544: ################################################
1545: ################################################
1546: sub ensure_current_data {
1547: my ($sname,$sdom,$courseid) = @_;
1548: my $status = 'okay'; # return value
1549: #
1550: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1551: &ensure_tables_are_set_up($courseid);
1.57 matthew 1552: #
1553: # Get the update time for the user
1554: my $updatetime = 0;
1.60 matthew 1555: my $modifiedtime = &Apache::lonnet::GetFileTimestamp
1556: ($sdom,$sname,$courseid.'.db',
1557: $Apache::lonnet::perlvar{'lonUsersDir'});
1.57 matthew 1558: #
1.87 matthew 1559: my $student_id = &get_student_id($sname,$sdom);
1.113 matthew 1560: my @Result = &Apache::lonmysql::get_rows($student_table,
1.87 matthew 1561: "student_id ='$student_id'");
1.57 matthew 1562: my $data = undef;
1563: if (@Result) {
1.113 matthew 1564: $updatetime = $Result[0]->[5]; # Ack! This is dumb!
1.57 matthew 1565: }
1566: if ($modifiedtime > $updatetime) {
1567: ($status,$data) = &update_student_data($sname,$sdom,$courseid);
1568: }
1569: return ($status,$data);
1570: }
1571:
1572: ################################################
1573: ################################################
1574:
1575: =pod
1576:
1.89 matthew 1577: =item &ensure_current_full_data($sname,$sdom,$courseid)
1578:
1579: Input: $sname, $sdom, $courseid
1580:
1581: Output: $status
1582:
1583: This routine ensures the fulldata (the data from a lonnet::dump, not a
1584: lonnet::currentdump) for a given student is up to date.
1.113 matthew 1585: The $student_table is queried to determine the time of the last update.
1.89 matthew 1586: If the students fulldata is out of date, &update_full_student_data() is
1587: called.
1588:
1589: The return value from the call to &update_full_student_data() is returned.
1590:
1591: =cut
1592:
1593: ################################################
1594: ################################################
1595: sub ensure_current_full_data {
1596: my ($sname,$sdom,$courseid) = @_;
1597: my $status = 'okay'; # return value
1598: #
1599: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1600: &ensure_tables_are_set_up($courseid);
1601: #
1602: # Get the update time for the user
1603: my $modifiedtime = &Apache::lonnet::GetFileTimestamp
1604: ($sdom,$sname,$courseid.'.db',
1605: $Apache::lonnet::perlvar{'lonUsersDir'});
1606: #
1607: my $student_id = &get_student_id($sname,$sdom);
1.113 matthew 1608: my @Result = &Apache::lonmysql::get_rows($student_table,
1.89 matthew 1609: "student_id ='$student_id'");
1610: my $updatetime;
1611: if (@Result && ref($Result[0]) eq 'ARRAY') {
1.113 matthew 1612: $updatetime = $Result[0]->[6];
1.89 matthew 1613: }
1614: if (! defined($updatetime) || $modifiedtime > $updatetime) {
1615: $status = &update_full_student_data($sname,$sdom,$courseid);
1616: }
1617: return $status;
1618: }
1619:
1620: ################################################
1621: ################################################
1622:
1623: =pod
1624:
1.57 matthew 1625: =item &get_student_data_from_performance_cache()
1626:
1627: Input: $sname, $sdom, $symb, $courseid
1628:
1629: Output: hash reference containing the data for the given student.
1630: If $symb is undef, all the students data is returned.
1631:
1632: This routine is the heart of the local caching system. See the description
1633: of $performance_table, $symb_table, $student_table, and $part_table. The
1634: main task is building the MySQL request. The tables appear in the request
1635: in the order in which they should be parsed by MySQL. When searching
1636: on a student the $student_table is used to locate the 'student_id'. All
1637: rows in $performance_table which have a matching 'student_id' are returned,
1638: with data from $part_table and $symb_table which match the entries in
1639: $performance_table, 'part_id' and 'symb_id'. When searching on a symb,
1640: the $symb_table is processed first, with matching rows grabbed from
1641: $performance_table and filled in from $part_table and $student_table in
1642: that order.
1643:
1644: Running 'EXPLAIN ' on the 'SELECT' statements generated can be quite
1645: interesting, especially if you play with the order the tables are listed.
1646:
1647: =cut
1648:
1649: ################################################
1650: ################################################
1651: sub get_student_data_from_performance_cache {
1652: my ($sname,$sdom,$symb,$courseid)=@_;
1653: my $student = $sname.':'.$sdom if (defined($sname) && defined($sdom));
1.61 matthew 1654: &setup_table_names($courseid);
1.57 matthew 1655: #
1656: # Return hash
1657: my $studentdata;
1658: #
1659: my $dbh = &Apache::lonmysql::get_dbh();
1660: my $request = "SELECT ".
1.73 matthew 1661: "d.symb,a.part,a.solved,a.tries,a.awarded,a.award,a.awarddetail,".
1.63 matthew 1662: "a.timestamp ";
1.57 matthew 1663: if (defined($student)) {
1664: $request .= "FROM $student_table AS b ".
1665: "LEFT JOIN $performance_table AS a ON b.student_id=a.student_id ".
1.73 matthew 1666: # "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ".
1.57 matthew 1667: "LEFT JOIN $symb_table AS d ON d.symb_id = a.symb_id ".
1668: "WHERE student='$student'";
1669: if (defined($symb) && $symb ne '') {
1.67 matthew 1670: $request .= " AND d.symb=".$dbh->quote($symb);
1.57 matthew 1671: }
1672: } elsif (defined($symb) && $symb ne '') {
1673: $request .= "FROM $symb_table as d ".
1674: "LEFT JOIN $performance_table AS a ON d.symb_id=a.symb_id ".
1.73 matthew 1675: # "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ".
1.57 matthew 1676: "LEFT JOIN $student_table AS b ON b.student_id = a.student_id ".
1677: "WHERE symb='".$dbh->quote($symb)."'";
1678: }
1679: my $starttime = Time::HiRes::time;
1680: my $rows_retrieved = 0;
1681: my $sth = $dbh->prepare($request);
1682: $sth->execute();
1683: if ($sth->err()) {
1684: &Apache::lonnet::logthis("Unable to execute MySQL request:");
1685: &Apache::lonnet::logthis("\n".$request."\n");
1686: &Apache::lonnet::logthis("error is:".$sth->errstr());
1687: return undef;
1688: }
1689: foreach my $row (@{$sth->fetchall_arrayref}) {
1690: $rows_retrieved++;
1.63 matthew 1691: my ($symb,$part,$solved,$tries,$awarded,$award,$awarddetail,$time) =
1.57 matthew 1692: (@$row);
1693: my $base = 'resource.'.$part;
1694: $studentdata->{$symb}->{$base.'.solved'} = $solved;
1695: $studentdata->{$symb}->{$base.'.tries'} = $tries;
1696: $studentdata->{$symb}->{$base.'.awarded'} = $awarded;
1697: $studentdata->{$symb}->{$base.'.award'} = $award;
1698: $studentdata->{$symb}->{$base.'.awarddetail'} = $awarddetail;
1699: $studentdata->{$symb}->{'timestamp'} = $time if (defined($time) && $time ne '');
1.67 matthew 1700: }
1.97 matthew 1701: ## Get misc parameters
1702: $request = 'SELECT c.symb,a.parameter,a.value '.
1703: "FROM $student_table AS b ".
1704: "LEFT JOIN $parameters_table AS a ON b.student_id=a.student_id ".
1705: "LEFT JOIN $symb_table AS c ON c.symb_id = a.symb_id ".
1706: "WHERE student='$student'";
1707: if (defined($symb) && $symb ne '') {
1708: $request .= " AND c.symb=".$dbh->quote($symb);
1709: }
1710: $sth = $dbh->prepare($request);
1711: $sth->execute();
1712: if ($sth->err()) {
1713: &Apache::lonnet::logthis("Unable to execute MySQL request:");
1714: &Apache::lonnet::logthis("\n".$request."\n");
1715: &Apache::lonnet::logthis("error is:".$sth->errstr());
1716: if (defined($symb) && $symb ne '') {
1717: $studentdata = $studentdata->{$symb};
1718: }
1719: return $studentdata;
1720: }
1721: #
1722: foreach my $row (@{$sth->fetchall_arrayref}) {
1723: $rows_retrieved++;
1724: my ($symb,$parameter,$value) = (@$row);
1725: $studentdata->{$symb}->{$parameter} = $value;
1726: }
1727: #
1.67 matthew 1728: if (defined($symb) && $symb ne '') {
1729: $studentdata = $studentdata->{$symb};
1.57 matthew 1730: }
1731: return $studentdata;
1732: }
1733:
1734: ################################################
1735: ################################################
1736:
1737: =pod
1738:
1739: =item &get_current_state()
1740:
1741: Input: $sname,$sdom,$symb,$courseid
1742:
1743: Output: Described below
1.46 matthew 1744:
1.47 matthew 1745: Retrieve the current status of a students performance. $sname and
1.46 matthew 1746: $sdom are the only required parameters. If $symb is undef the results
1.47 matthew 1747: of an &Apache::lonnet::currentdump() will be returned.
1.46 matthew 1748: If $courseid is undef it will be retrieved from the environment.
1749:
1750: The return structure is based on &Apache::lonnet::currentdump. If
1751: $symb is unspecified, all the students data is returned in a hash of
1752: the form:
1753: (
1754: symb1 => { param1 => value1, param2 => value2 ... },
1755: symb2 => { param1 => value1, param2 => value2 ... },
1756: )
1757:
1758: If $symb is specified, a hash of
1759: (
1760: param1 => value1,
1761: param2 => value2,
1762: )
1763: is returned.
1764:
1.57 matthew 1765: If no data is found for $symb, or if the student has no performance data,
1.46 matthew 1766: an empty list is returned.
1767:
1768: =cut
1769:
1770: ################################################
1771: ################################################
1772: sub get_current_state {
1.47 matthew 1773: my ($sname,$sdom,$symb,$courseid,$forcedownload)=@_;
1774: #
1.46 matthew 1775: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1.47 matthew 1776: #
1.61 matthew 1777: return () if (! defined($sname) || ! defined($sdom));
1778: #
1.57 matthew 1779: my ($status,$data) = &ensure_current_data($sname,$sdom,$courseid);
1.77 matthew 1780: # &Apache::lonnet::logthis
1781: # ('sname = '.$sname.
1782: # ' domain = '.$sdom.
1783: # ' status = '.$status.
1784: # ' data is '.(defined($data)?'defined':'undefined'));
1.73 matthew 1785: # while (my ($symb,$hash) = each(%$data)) {
1786: # &Apache::lonnet::logthis($symb."\n----------------------------------");
1787: # while (my ($key,$value) = each (%$hash)) {
1788: # &Apache::lonnet::logthis(" ".$key." = ".$value);
1789: # }
1790: # }
1.47 matthew 1791: #
1.79 matthew 1792: if (defined($data) && defined($symb) && ref($data->{$symb})) {
1793: return %{$data->{$symb}};
1794: } elsif (defined($data) && ! defined($symb) && ref($data)) {
1795: return %$data;
1796: }
1797: if ($status eq 'no data') {
1.57 matthew 1798: return ();
1799: } else {
1800: if ($status ne 'okay' && $status ne '') {
1801: &Apache::lonnet::logthis('status = '.$status);
1.47 matthew 1802: return ();
1803: }
1.57 matthew 1804: my $returnhash = &get_student_data_from_performance_cache($sname,$sdom,
1805: $symb,$courseid);
1806: return %$returnhash if (defined($returnhash));
1.46 matthew 1807: }
1.57 matthew 1808: return ();
1.61 matthew 1809: }
1810:
1811: ################################################
1812: ################################################
1813:
1814: =pod
1815:
1816: =item &get_problem_statistics()
1817:
1818: Gather data on a given problem. The database is assumed to be
1819: populated and all local caching variables are assumed to be set
1820: properly. This means you need to call &ensure_current_data for
1821: the students you are concerned with prior to calling this routine.
1822:
1.124 matthew 1823: Inputs: $Sections, $status, $symb, $part, $courseid, $starttime, $endtime
1.61 matthew 1824:
1.64 matthew 1825: =over 4
1826:
1.124 matthew 1827: =item $Sections Array ref containing section names for students.
1828: 'all' is allowed to be the first (and only) item in the array.
1829:
1830: =item $status String describing the status of students
1.64 matthew 1831:
1832: =item $symb is the symb for the problem.
1833:
1834: =item $part is the part id you need statistics for
1835:
1836: =item $courseid is the course id, of course!
1837:
1.122 matthew 1838: =item $starttime and $endtime are unix times which to use to limit
1839: the statistical data.
1840:
1.64 matthew 1841: =back
1842:
1.66 matthew 1843: Outputs: See the code for up to date information. A hash reference is
1844: returned. The hash has the following keys defined:
1.64 matthew 1845:
1846: =over 4
1847:
1.66 matthew 1848: =item num_students The number of students attempting the problem
1849:
1850: =item tries The total number of tries for the students
1851:
1852: =item max_tries The maximum number of tries taken
1853:
1854: =item mean_tries The average number of tries
1855:
1856: =item num_solved The number of students able to solve the problem
1857:
1858: =item num_override The number of students whose answer is 'correct_by_override'
1859:
1860: =item deg_of_diff The degree of difficulty of the problem
1861:
1862: =item std_tries The standard deviation of the number of tries
1863:
1864: =item skew_tries The skew of the number of tries
1.64 matthew 1865:
1.66 matthew 1866: =item per_wrong The number of students attempting the problem who were not
1867: able to answer it correctly.
1.64 matthew 1868:
1869: =back
1870:
1.61 matthew 1871: =cut
1872:
1873: ################################################
1874: ################################################
1875: sub get_problem_statistics {
1.122 matthew 1876: my ($Sections,$status,$symb,$part,$courseid,$starttime,$endtime) = @_;
1.61 matthew 1877: return if (! defined($symb) || ! defined($part));
1878: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1879: #
1.100 matthew 1880: &setup_table_names($courseid);
1.61 matthew 1881: my $symb_id = &get_symb_id($symb);
1882: my $part_id = &get_part_id($part);
1883: my $stats_table = $courseid.'_problem_stats';
1884: #
1885: my $dbh = &Apache::lonmysql::get_dbh();
1886: return undef if (! defined($dbh));
1887: #
1.123 matthew 1888: # Clean out the table
1.61 matthew 1889: $dbh->do('DROP TABLE '.$stats_table); # May return an error
1890: my $request =
1.115 matthew 1891: 'CREATE TEMPORARY TABLE '.$stats_table.' '.
1892: 'SELECT a.student_id,a.solved,a.award,a.awarded,a.tries '.
1893: 'FROM '.$performance_table.' AS a ';
1.123 matthew 1894: #
1895: # See if we need to include some requirements on the students
1.115 matthew 1896: if ((defined($Sections) && lc($Sections->[0]) ne 'all') ||
1897: (defined($status) && lc($status) ne 'any')) {
1898: $request .= 'NATURAL LEFT JOIN '.$student_table.' AS b ';
1899: }
1900: $request .= ' WHERE a.symb_id='.$symb_id.' AND a.part_id='.$part_id;
1.123 matthew 1901: #
1902: # Limit the students included to those specified
1.115 matthew 1903: if (defined($Sections) && lc($Sections->[0]) ne 'all') {
1.64 matthew 1904: $request .= ' AND ('.
1.115 matthew 1905: join(' OR ', map { "b.section='".$_."'" } @$Sections
1.64 matthew 1906: ).')';
1907: }
1.115 matthew 1908: if (defined($status) && lc($status) ne 'any') {
1909: $request .= " AND b.status='".$status."'";
1.122 matthew 1910: }
1911: #
1.123 matthew 1912: # Limit by starttime and endtime
1.122 matthew 1913: my $time_requirements = undef;
1914: if (defined($starttime)) {
1915: $time_requirements .= 'a.timestamp>='.$starttime;
1916: if (defined($endtime)) {
1917: $time_requirements .= ' AND a.timestamp<='.$endtime;
1918: }
1919: } elsif (defined($endtime)) {
1920: $time_requirements .= 'a.timestamp<='.$endtime;
1921: }
1922: if (defined($time_requirements)) {
1923: $request .= ' AND '.$time_requirements;
1.115 matthew 1924: }
1.123 matthew 1925: #
1926: # Finally, execute the request to create the temporary table
1.61 matthew 1927: $dbh->do($request);
1.123 matthew 1928: #
1929: # Collect the first suite of statistics
1.128 matthew 1930: $request = 'SELECT COUNT(*),SUM(tries),'.
1931: 'AVG(tries),STD(tries) '.
1.109 matthew 1932: 'FROM '.$stats_table;
1.128 matthew 1933: my ($num,$tries,$mean,$STD) = &execute_SQL_request
1.109 matthew 1934: ($dbh,$request);
1.128 matthew 1935: #
1936: $request = 'SELECT MAX(tries),MIN(tries) FROM '.$stats_table.
1937: ' WHERE awarded>0';
1938: if (defined($time_requirements)) {
1939: $request .= ' AND '.$time_requirements;
1940: }
1941: my ($max,$min) = &execute_SQL_request($dbh,$request);
1942: #
1.109 matthew 1943: $request = 'SELECT SUM(awarded) FROM '.$stats_table;
1.128 matthew 1944: if (defined($time_requirements)) {
1945: $request .= ' AND '.$time_requirements;
1946: }
1.109 matthew 1947: my ($Solved) = &execute_SQL_request($dbh,$request);
1.128 matthew 1948: #
1.109 matthew 1949: $request = 'SELECT SUM(awarded) FROM '.$stats_table.
1950: " WHERE solved='correct_by_override'";
1.128 matthew 1951: if (defined($time_requirements)) {
1952: $request .= ' AND '.$time_requirements;
1953: }
1.109 matthew 1954: my ($solved) = &execute_SQL_request($dbh,$request);
1955: #
1.133 matthew 1956: $Solved -= $solved;
1957: #
1.61 matthew 1958: $num = 0 if (! defined($num));
1959: $tries = 0 if (! defined($tries));
1.128 matthew 1960: $max = 0 if (! defined($max));
1961: $min = 0 if (! defined($min));
1.61 matthew 1962: $STD = 0 if (! defined($STD));
1.133 matthew 1963: $Solved = 0 if (! defined($Solved) || $Solved < 0);
1.61 matthew 1964: $solved = 0 if (! defined($solved));
1965: #
1.123 matthew 1966: # Compute the more complicated statistics
1.61 matthew 1967: my $DegOfDiff = 'nan';
1.66 matthew 1968: $DegOfDiff = 1-($Solved)/$tries if ($tries>0);
1.123 matthew 1969: #
1.61 matthew 1970: my $SKEW = 'nan';
1.66 matthew 1971: my $wrongpercent = 0;
1.128 matthew 1972: my $numwrong = 'nan';
1.61 matthew 1973: if ($num > 0) {
1974: ($SKEW) = &execute_SQL_request($dbh,'SELECT SQRT(SUM('.
1975: 'POWER(tries - '.$STD.',3)'.
1976: '))/'.$num.' FROM '.$stats_table);
1.128 matthew 1977: $numwrong = $num-$Solved;
1978: $wrongpercent=int(10*100*$numwrong/$num)/10;
1.61 matthew 1979: }
1980: #
1.123 matthew 1981: # Drop the temporary table
1982: $dbh->do('DROP TABLE '.$stats_table); # May return an error
1.81 matthew 1983: #
1984: # Return result
1.66 matthew 1985: return { num_students => $num,
1986: tries => $tries,
1.128 matthew 1987: max_tries => $max,
1988: min_tries => $min,
1.66 matthew 1989: mean_tries => $mean,
1990: std_tries => $STD,
1991: skew_tries => $SKEW,
1992: num_solved => $Solved,
1993: num_override => $solved,
1.128 matthew 1994: num_wrong => $numwrong,
1.66 matthew 1995: per_wrong => $wrongpercent,
1.81 matthew 1996: deg_of_diff => $DegOfDiff };
1.61 matthew 1997: }
1998:
1.127 matthew 1999: ##
2000: ## This is a helper for get_statistics
1.61 matthew 2001: sub execute_SQL_request {
2002: my ($dbh,$request)=@_;
2003: # &Apache::lonnet::logthis($request);
2004: my $sth = $dbh->prepare($request);
2005: $sth->execute();
2006: my $row = $sth->fetchrow_arrayref();
2007: if (ref($row) eq 'ARRAY' && scalar(@$row)>0) {
2008: return @$row;
2009: }
2010: return ();
2011: }
1.123 matthew 2012:
1.127 matthew 2013: ######################################################
2014: ######################################################
2015:
2016: =pod
2017:
2018: =item &populate_weight_table
2019:
2020: =cut
2021:
2022: ######################################################
2023: ######################################################
2024: sub populate_weight_table {
2025: my ($courseid) = @_;
2026: if (! defined($courseid)) {
2027: $courseid = $ENV{'request.course.id'};
2028: }
2029: #
2030: &setup_table_names($courseid);
2031: my ($top,$sequences,$assessments) = get_sequence_assessment_data();
2032: if (! defined($top) || ! ref($top)) {
2033: # There has been an error, better report it
2034: &Apache::lonnet::logthis('top is undefined');
2035: return;
2036: }
2037: # Since we use lonnet::EXT to retrieve problem weights,
2038: # to ensure current data we must clear the caches out.
2039: &Apache::lonnet::clear_EXT_cache_status();
2040: my $dbh = &Apache::lonmysql::get_dbh();
2041: my $request = 'INSERT IGNORE INTO '.$weight_table.
2042: "(symb_id,part_id,weight) VALUES ";
2043: my $weight;
2044: foreach my $res (@$assessments) {
2045: my $symb_id = &get_symb_id($res->{'symb'});
2046: foreach my $part (@{$res->{'parts'}}) {
2047: my $part_id = &get_part_id($part);
2048: $weight = &Apache::lonnet::EXT('resource.'.$part.'.weight',
2049: $res->{'symb'},
2050: undef,undef,undef);
2051: if (!defined($weight) || ($weight eq '')) {
2052: $weight=1;
2053: }
2054: $request .= "('".$symb_id."','".$part_id."','".$weight."'),";
2055: }
2056: }
2057: $request =~ s/(,)$//;
2058: # &Apache::lonnet::logthis('request = '.$/.$request);
2059: $dbh->do($request);
2060: if ($dbh->err()) {
2061: &Apache::lonnet::logthis("error ".$dbh->errstr().
2062: " occured executing \n".
2063: $request);
2064: }
2065: return;
2066: }
2067:
2068: ##########################################################
2069: ##########################################################
1.61 matthew 2070:
1.127 matthew 2071: =pod
2072:
1.129 matthew 2073: =item &limit_by_start_end_times
2074:
2075: Build SQL WHERE condition which limits the data collected by the start
2076: and end times provided
2077:
2078: Inputs: $starttime, $endtime, $table
2079:
2080: Returns: $time_limits
2081:
2082: =cut
2083:
2084: ##########################################################
2085: ##########################################################
2086: sub limit_by_start_end_time {
2087: my ($starttime,$endtime,$table) = @_;
2088: my $time_requirements = undef;
2089: if (defined($starttime)) {
2090: $time_requirements .= $table.".timestamp>='".$starttime."'";
2091: if (defined($endtime)) {
2092: $time_requirements .= " AND ".$table.".timestamp<='".$endtime."'";
2093: }
2094: } elsif (defined($endtime)) {
2095: $time_requirements .= $table.".timestamp<='".$endtime."'";
2096: }
2097: return $time_requirements;
2098: }
2099:
2100: ##########################################################
2101: ##########################################################
2102:
2103: =pod
2104:
1.127 matthew 2105: =item &limit_by_section_and_status
2106:
2107: Build SQL WHERE condition which limits the data collected by section and
2108: student status.
2109:
2110: Inputs: $Sections (array ref)
2111: $enrollment (string: 'any', 'expired', 'active')
2112: $tablename The name of the table that holds the student data
2113:
2114: Returns: $student_requirements,$enrollment_requirements
2115:
2116: =cut
2117:
2118: ##########################################################
2119: ##########################################################
2120: sub limit_by_section_and_status {
2121: my ($Sections,$enrollment,$tablename) = @_;
2122: my $student_requirements = undef;
2123: if ( (defined($Sections) && $Sections->[0] ne 'all')) {
2124: $student_requirements = '('.
2125: join(' OR ', map { $tablename.".section='".$_."'" } @$Sections
2126: ).')';
2127: }
2128: #
2129: my $enrollment_requirements=undef;
2130: if (defined($enrollment) && $enrollment ne 'Any') {
2131: $enrollment_requirements = $tablename.".status='".$enrollment."'";
2132: }
2133: return ($student_requirements,$enrollment_requirements);
2134: }
2135:
2136: ######################################################
2137: ######################################################
2138:
2139: =pod
2140:
2141: =item rank_students_by_scores_on_resources
2142:
2143: Inputs:
2144: $resources: array ref of hash ref. Each hash ref needs key 'symb'.
2145: $Sections: array ref of sections to include,
2146: $enrollment: string,
2147: $courseid (may be omitted)
2148:
2149: Returns; An array of arrays. The sub arrays contain a student name and
2150: their score on the resources.
2151:
2152: =cut
2153:
2154: ######################################################
2155: ######################################################
2156: sub RNK_student { return 0; };
2157: sub RNK_score { return 1; };
2158:
2159: sub rank_students_by_scores_on_resources {
1.130 matthew 2160: my ($resources,$Sections,$enrollment,$courseid,$starttime,$endtime) = @_;
1.127 matthew 2161: return if (! defined($resources) || ! ref($resources) eq 'ARRAY');
2162: if (! defined($courseid)) {
2163: $courseid = $ENV{'request.course.id'};
2164: }
2165: #
2166: &setup_table_names($courseid);
2167: my $dbh = &Apache::lonmysql::get_dbh();
2168: my ($section_limits,$enrollment_limits)=
2169: &limit_by_section_and_status($Sections,$enrollment,'b');
2170: my $symb_limits = '('.join(' OR ',map {'a.symb_id='.&get_symb_id($_);
2171: } @$resources
2172: ).')';
1.130 matthew 2173: my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a');
1.127 matthew 2174: my $request = 'SELECT b.student,SUM(a.awarded*w.weight) AS score FROM '.
2175: $performance_table.' AS a '.
2176: 'NATURAL LEFT JOIN '.$weight_table.' AS w '.
2177: 'LEFT JOIN '.$student_table.' AS b ON a.student_id=b.student_id '.
2178: 'WHERE ';
2179: if (defined($section_limits)) {
2180: $request .= $section_limits.' AND ';
2181: }
2182: if (defined($enrollment_limits)) {
2183: $request .= $enrollment_limits.' AND ';
2184: }
1.130 matthew 2185: if (defined($time_limits)) {
2186: $request .= $time_limits.' AND ';
2187: }
1.127 matthew 2188: if ($symb_limits ne '()') {
2189: $request .= $symb_limits.' AND ';
2190: }
2191: $request =~ s/( AND )$//; # Remove extra conjunction
2192: $request =~ s/( WHERE )$//; # In case there were no limits placed on it
2193: $request .= ' GROUP BY a.student_id ORDER BY score';
2194: #&Apache::lonnet::logthis('request = '.$/.$request);
2195: my $sth = $dbh->prepare($request);
2196: $sth->execute();
2197: my $rows = $sth->fetchall_arrayref();
2198: return ($rows);
2199: }
2200:
2201: ########################################################
2202: ########################################################
2203:
2204: =pod
2205:
2206: =item &get_sum_of_scores
2207:
2208: Inputs: $resource (hash ref, needs {'symb'} key),
2209: $part, (the part id),
2210: $students (array ref, contents of array are scalars holding 'sname:sdom'),
2211: $courseid
2212:
2213: Returns: the sum of the score on the problem part over the students and the
2214: maximum possible value for the sum (taken from the weight table).
2215:
2216: =cut
2217:
2218: ########################################################
2219: ########################################################
2220: sub get_sum_of_scores {
1.130 matthew 2221: my ($resource,$part,$students,$courseid,$starttime,$endtime) = @_;
1.127 matthew 2222: if (! defined($courseid)) {
2223: $courseid = $ENV{'request.course.id'};
2224: }
1.138 matthew 2225: if (defined($students) &&
2226: ((@$students == 0) ||
2227: (@$students == 1 && (! defined($students->[0]) ||
2228: $students->[0] eq ''))
2229: )
2230: ){
2231: undef($students);
2232: }
1.127 matthew 2233: #
2234: &setup_table_names($courseid);
2235: my $dbh = &Apache::lonmysql::get_dbh();
1.130 matthew 2236: my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a');
1.127 matthew 2237: my $request = 'SELECT SUM(a.awarded*w.weight),SUM(w.weight) FROM '.
2238: $performance_table.' AS a '.
2239: 'NATURAL LEFT JOIN '.$weight_table.' AS w ';
2240: $request .= 'WHERE a.symb_id='.&get_symb_id($resource->{'symb'}).
2241: ' AND a.part_id='.&get_part_id($part);
1.130 matthew 2242: if (defined($time_limits)) {
2243: $request .= ' AND '.$time_limits;
2244: }
1.127 matthew 2245: if (defined($students)) {
2246: $request .= ' AND ('.
2247: join(' OR ',map {'a.student_id='.&get_student_id(split(':',$_));
2248: } @$students).
2249: ')';
2250: }
2251: my $sth = $dbh->prepare($request);
2252: $sth->execute();
2253: my $rows = $sth->fetchrow_arrayref();
2254: if ($dbh->err) {
1.138 matthew 2255: &Apache::lonnet::logthis('error 1 = '.$dbh->errstr());
2256: &Apache::lonnet::logthis('prepared then executed, fetchrow_arrayrefed'.
2257: $/.$request);
1.127 matthew 2258: return (undef,undef);
2259: }
2260: return ($rows->[0],$rows->[1]);
2261: }
2262:
1.129 matthew 2263: ########################################################
2264: ########################################################
2265:
2266: =pod
2267:
2268: =item &score_stats
2269:
2270: Inputs: $Sections, $enrollment, $symbs, $starttime,
2271: $endtime, $courseid
2272:
2273: $Sections, $enrollment, $starttime, $endtime, and $courseid are the same as
2274: elsewhere in this module.
2275: $symbs is an array ref of symbs
2276:
2277: Returns: minimum, maximum, mean, s.d., number of students, and maximum
2278: possible of student scores on the given resources
2279:
2280: =cut
2281:
2282: ########################################################
2283: ########################################################
2284: sub score_stats {
2285: my ($Sections,$enrollment,$symbs,$starttime,$endtime,$courseid)=@_;
2286: if (! defined($courseid)) {
2287: $courseid = $ENV{'request.course.id'};
2288: }
2289: #
2290: &setup_table_names($courseid);
2291: my $dbh = &Apache::lonmysql::get_dbh();
2292: #
2293: my ($section_limits,$enrollment_limits)=
2294: &limit_by_section_and_status($Sections,$enrollment,'b');
2295: my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a');
2296: my @Symbids = map { &get_symb_id($_); } @{$symbs};
2297: #
2298: my $stats_table = $courseid.'_problem_stats';
2299: my $symb_restriction = join(' OR ',map {'a.symb_id='.$_;} @Symbids);
2300: my $request = 'DROP TABLE '.$stats_table;
2301: $dbh->do($request);
2302: $request =
2303: 'CREATE TEMPORARY TABLE '.$stats_table.' '.
2304: 'SELECT a.student_id,'.
2305: 'SUM(a.awarded*w.weight) AS score FROM '.
2306: $performance_table.' AS a '.
2307: 'NATURAL LEFT JOIN '.$weight_table.' AS w '.
2308: 'LEFT JOIN '.$student_table.' AS b ON a.student_id=b.student_id '.
2309: 'WHERE ('.$symb_restriction.')';
2310: if ($time_limits) {
2311: $request .= ' AND '.$time_limits;
2312: }
2313: if ($section_limits) {
2314: $request .= ' AND '.$section_limits;
2315: }
2316: if ($enrollment_limits) {
2317: $request .= ' AND '.$enrollment_limits;
2318: }
2319: $request .= ' GROUP BY a.student_id';
2320: # &Apache::lonnet::logthis('request = '.$/.$request);
2321: my $sth = $dbh->prepare($request);
2322: $sth->execute();
2323: $request =
2324: 'SELECT AVG(score),STD(score),MAX(score),MIN(score),COUNT(score) '.
2325: 'FROM '.$stats_table;
2326: my ($ave,$std,$max,$min,$count) = &execute_SQL_request($dbh,$request);
2327: # &Apache::lonnet::logthis('request = '.$/.$request);
2328:
2329: $request = 'SELECT SUM(weight) FROM '.$weight_table.
2330: ' WHERE ('.$symb_restriction.')';
2331: my ($max_possible) = &execute_SQL_request($dbh,$request);
2332: # &Apache::lonnet::logthis('request = '.$/.$request);
2333: return($min,$max,$ave,$std,$count,$max_possible);
2334: }
2335:
2336:
2337: ########################################################
2338: ########################################################
2339:
2340: =pod
2341:
2342: =item &count_stats
2343:
2344: Inputs: $Sections, $enrollment, $symbs, $starttime,
2345: $endtime, $courseid
2346:
2347: $Sections, $enrollment, $starttime, $endtime, and $courseid are the same as
2348: elsewhere in this module.
2349: $symbs is an array ref of symbs
2350:
2351: Returns: minimum, maximum, mean, s.d., and number of students
2352: of the number of items correct on the given resources
2353:
2354: =cut
2355:
2356: ########################################################
2357: ########################################################
2358: sub count_stats {
2359: my ($Sections,$enrollment,$symbs,$starttime,$endtime,$courseid)=@_;
2360: if (! defined($courseid)) {
2361: $courseid = $ENV{'request.course.id'};
2362: }
2363: #
2364: &setup_table_names($courseid);
2365: my $dbh = &Apache::lonmysql::get_dbh();
2366: #
2367: my ($section_limits,$enrollment_limits)=
2368: &limit_by_section_and_status($Sections,$enrollment,'b');
2369: my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a');
2370: my @Symbids = map { &get_symb_id($_); } @{$symbs};
2371: #
2372: my $stats_table = $courseid.'_problem_stats';
2373: my $symb_restriction = join(' OR ',map {'a.symb_id='.$_;} @Symbids);
2374: my $request = 'DROP TABLE '.$stats_table;
2375: $dbh->do($request);
2376: $request =
2377: 'CREATE TEMPORARY TABLE '.$stats_table.' '.
2378: 'SELECT a.student_id,'.
2379: 'COUNT(a.award) AS count FROM '.
2380: $performance_table.' AS a '.
2381: 'LEFT JOIN '.$student_table.' AS b ON a.student_id=b.student_id '.
2382: 'WHERE ('.$symb_restriction.')'.
2383: " AND a.award!='INCORRECT_ATTEMPTED'";
2384: if ($time_limits) {
2385: $request .= ' AND '.$time_limits;
2386: }
2387: if ($section_limits) {
2388: $request .= ' AND '.$section_limits;
2389: }
2390: if ($enrollment_limits) {
2391: $request .= ' AND '.$enrollment_limits;
2392: }
2393: $request .= ' GROUP BY a.student_id';
1.131 matthew 2394: # &Apache::lonnet::logthis('request = '.$/.$request);
1.129 matthew 2395: my $sth = $dbh->prepare($request);
2396: $sth->execute();
2397: $request =
2398: 'SELECT AVG(count),STD(count),MAX(count),MIN(count),COUNT(count) '.
2399: 'FROM '.$stats_table;
2400: my ($ave,$std,$max,$min,$count) = &execute_SQL_request($dbh,$request);
1.131 matthew 2401: # &Apache::lonnet::logthis('request = '.$/.$request);
1.129 matthew 2402: return($min,$max,$ave,$std,$count);
2403: }
1.127 matthew 2404:
2405: ######################################################
2406: ######################################################
2407:
2408: =pod
2409:
2410: =item get_student_data
2411:
2412: =cut
2413:
2414: ######################################################
2415: ######################################################
1.105 matthew 2416: sub get_student_data {
2417: my ($students,$courseid) = @_;
2418: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
2419: &setup_table_names($courseid);
2420: my $dbh = &Apache::lonmysql::get_dbh();
2421: return undef if (! defined($dbh));
2422: my $request = 'SELECT '.
2423: 'student_id, student '.
2424: 'FROM '.$student_table;
2425: if (defined($students)) {
2426: $request .= ' WHERE ('.
2427: join(' OR ', map {'student_id='.
2428: &get_student_id($_->{'username'},
2429: $_->{'domain'})
2430: } @$students
2431: ).')';
2432: }
2433: $request.= ' ORDER BY student_id';
2434: my $sth = $dbh->prepare($request);
2435: $sth->execute();
2436: if ($dbh->err) {
1.138 matthew 2437: &Apache::lonnet::logthis('error 2 = '.$dbh->errstr());
2438: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.105 matthew 2439: return undef;
2440: }
2441: my $dataset = $sth->fetchall_arrayref();
2442: if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) {
2443: return $dataset;
2444: }
2445: }
2446:
1.108 matthew 2447: sub RD_student_id { return 0; }
2448: sub RD_awarddetail { return 1; }
2449: sub RD_response_eval { return 2; }
2450: sub RD_submission { return 3; }
2451: sub RD_timestamp { return 4; }
2452: sub RD_tries { return 5; }
2453: sub RD_sname { return 6; }
2454:
2455: sub get_response_data {
1.126 matthew 2456: my ($Sections,$enrollment,$symb,$response,$courseid) = @_;
1.103 matthew 2457: return undef if (! defined($symb) ||
1.100 matthew 2458: ! defined($response));
2459: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
2460: #
2461: &setup_table_names($courseid);
2462: my $symb_id = &get_symb_id($symb);
1.137 matthew 2463: if (! defined($symb_id)) {
2464: &Apache::lonnet::logthis('Unable to find symb for '.$symb.' in '.$courseid);
2465: return undef;
2466: }
1.100 matthew 2467: my $response_id = &get_part_id($response);
1.137 matthew 2468: if (! defined($response_id)) {
2469: &Apache::lonnet::logthis('Unable to find id for '.$response.' in '.$courseid);
2470: return undef;
2471: }
1.100 matthew 2472: #
2473: my $dbh = &Apache::lonmysql::get_dbh();
2474: return undef if (! defined($dbh));
1.126 matthew 2475: #
1.127 matthew 2476: my ($student_requirements,$enrollment_requirements) =
2477: &limit_by_section_and_status($Sections,$enrollment,'d');
1.100 matthew 2478: my $request = 'SELECT '.
1.105 matthew 2479: 'a.student_id, a.awarddetail, a.response_specific_value, '.
1.108 matthew 2480: 'a.submission, b.timestamp, c.tries, d.student '.
1.100 matthew 2481: 'FROM '.$fulldump_response_table.' AS a '.
2482: 'LEFT JOIN '.$fulldump_timestamp_table.' AS b '.
2483: 'ON a.symb_id=b.symb_id AND a.student_id=b.student_id AND '.
2484: 'a.transaction = b.transaction '.
2485: 'LEFT JOIN '.$fulldump_part_table.' AS c '.
2486: 'ON a.symb_id=c.symb_id AND a.student_id=c.student_id AND '.
2487: 'a.part_id=c.part_id AND a.transaction = c.transaction '.
1.108 matthew 2488: 'LEFT JOIN '.$student_table.' AS d '.
2489: 'ON a.student_id=d.student_id '.
1.100 matthew 2490: 'WHERE '.
2491: 'a.symb_id='.$symb_id.' AND a.response_id='.$response_id;
1.126 matthew 2492: if (defined($student_requirements) || defined($enrollment_requirements)) {
2493: $request .= ' AND ';
2494: if (defined($student_requirements)) {
2495: $request .= $student_requirements.' AND ';
2496: }
2497: if (defined($enrollment_requirements)) {
2498: $request .= $enrollment_requirements.' AND ';
2499: }
2500: $request =~ s/( AND )$//;
1.100 matthew 2501: }
2502: $request .= ' ORDER BY b.timestamp';
1.103 matthew 2503: # &Apache::lonnet::logthis("request =\n".$request);
1.100 matthew 2504: my $sth = $dbh->prepare($request);
2505: $sth->execute();
1.105 matthew 2506: if ($dbh->err) {
1.138 matthew 2507: &Apache::lonnet::logthis('error 3 = '.$dbh->errstr());
2508: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.105 matthew 2509: return undef;
2510: }
1.100 matthew 2511: my $dataset = $sth->fetchall_arrayref();
2512: if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) {
1.117 matthew 2513: # Clear the \'s from around the submission
2514: for (my $i =0;$i<scalar(@$dataset);$i++) {
2515: $dataset->[$i]->[3] =~ s/(\'$|^\')//g;
2516: }
1.103 matthew 2517: return $dataset;
1.100 matthew 2518: }
1.118 matthew 2519: }
2520:
2521:
2522: sub RDs_awarddetail { return 3; }
2523: sub RDs_submission { return 2; }
2524: sub RDs_timestamp { return 1; }
2525: sub RDs_tries { return 0; }
1.119 matthew 2526: sub RDs_awarded { return 4; }
1.118 matthew 2527:
2528: sub get_response_data_by_student {
2529: my ($student,$symb,$response,$courseid) = @_;
2530: return undef if (! defined($symb) ||
2531: ! defined($response));
2532: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
2533: #
2534: &setup_table_names($courseid);
2535: my $symb_id = &get_symb_id($symb);
2536: my $response_id = &get_part_id($response);
2537: #
2538: my $student_id = &get_student_id($student->{'username'},
2539: $student->{'domain'});
2540: #
2541: my $dbh = &Apache::lonmysql::get_dbh();
2542: return undef if (! defined($dbh));
2543: my $request = 'SELECT '.
1.119 matthew 2544: 'c.tries, b.timestamp, a.submission, a.awarddetail, e.awarded '.
1.118 matthew 2545: 'FROM '.$fulldump_response_table.' AS a '.
2546: 'LEFT JOIN '.$fulldump_timestamp_table.' AS b '.
2547: 'ON a.symb_id=b.symb_id AND a.student_id=b.student_id AND '.
2548: 'a.transaction = b.transaction '.
2549: 'LEFT JOIN '.$fulldump_part_table.' AS c '.
2550: 'ON a.symb_id=c.symb_id AND a.student_id=c.student_id AND '.
2551: 'a.part_id=c.part_id AND a.transaction = c.transaction '.
2552: 'LEFT JOIN '.$student_table.' AS d '.
2553: 'ON a.student_id=d.student_id '.
1.119 matthew 2554: 'LEFT JOIN '.$performance_table.' AS e '.
2555: 'ON a.symb_id=e.symb_id AND a.part_id=e.part_id AND '.
2556: 'a.student_id=e.student_id AND c.tries=e.tries '.
1.118 matthew 2557: 'WHERE '.
2558: 'a.symb_id='.$symb_id.' AND a.response_id='.$response_id.
2559: ' AND a.student_id='.$student_id.' ORDER BY b.timestamp';
1.125 matthew 2560: # &Apache::lonnet::logthis("request =\n".$request);
1.118 matthew 2561: my $sth = $dbh->prepare($request);
2562: $sth->execute();
2563: if ($dbh->err) {
1.138 matthew 2564: &Apache::lonnet::logthis('error 4 = '.$dbh->errstr());
2565: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.118 matthew 2566: return undef;
2567: }
2568: my $dataset = $sth->fetchall_arrayref();
2569: if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) {
2570: # Clear the \'s from around the submission
2571: for (my $i =0;$i<scalar(@$dataset);$i++) {
2572: $dataset->[$i]->[2] =~ s/(\'$|^\')//g;
2573: }
2574: return $dataset;
2575: }
2576: return undef; # error occurred
1.106 matthew 2577: }
1.108 matthew 2578:
2579: sub RT_student_id { return 0; }
2580: sub RT_awarded { return 1; }
2581: sub RT_tries { return 2; }
2582: sub RT_timestamp { return 3; }
1.106 matthew 2583:
2584: sub get_response_time_data {
1.107 matthew 2585: my ($students,$symb,$part,$courseid) = @_;
1.106 matthew 2586: return undef if (! defined($symb) ||
1.107 matthew 2587: ! defined($part));
1.106 matthew 2588: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
2589: #
2590: &setup_table_names($courseid);
2591: my $symb_id = &get_symb_id($symb);
1.107 matthew 2592: my $part_id = &get_part_id($part);
1.106 matthew 2593: #
2594: my $dbh = &Apache::lonmysql::get_dbh();
2595: return undef if (! defined($dbh));
2596: my $request = 'SELECT '.
1.107 matthew 2597: 'a.student_id, a.awarded, a.tries, b.timestamp '.
2598: 'FROM '.$fulldump_part_table.' AS a '.
1.106 matthew 2599: 'NATURAL LEFT JOIN '.$fulldump_timestamp_table.' AS b '.
2600: # 'ON a.symb_id=b.symb_id AND a.student_id=b.student_id AND '.
2601: # 'a.transaction = b.transaction '.
2602: 'WHERE '.
1.107 matthew 2603: 'a.symb_id='.$symb_id.' AND a.part_id='.$part_id;
1.106 matthew 2604: if (defined($students)) {
2605: $request .= ' AND ('.
2606: join(' OR ', map {'a.student_id='.
2607: &get_student_id($_->{'username'},
2608: $_->{'domain'})
2609: } @$students
2610: ).')';
2611: }
2612: $request .= ' ORDER BY b.timestamp';
2613: # &Apache::lonnet::logthis("request =\n".$request);
2614: my $sth = $dbh->prepare($request);
2615: $sth->execute();
2616: if ($dbh->err) {
1.138 matthew 2617: &Apache::lonnet::logthis('error 5 = '.$dbh->errstr());
2618: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.106 matthew 2619: return undef;
2620: }
2621: my $dataset = $sth->fetchall_arrayref();
2622: if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) {
2623: return $dataset;
2624: }
2625:
1.100 matthew 2626: }
1.61 matthew 2627:
2628: ################################################
2629: ################################################
2630:
2631: =pod
2632:
1.116 matthew 2633: =item &get_student_scores($Sections,$Symbs,$enrollment,$courseid)
1.113 matthew 2634:
2635: =cut
2636:
2637: ################################################
2638: ################################################
2639: sub get_student_scores {
1.121 matthew 2640: my ($Sections,$Symbs,$enrollment,$courseid,$starttime,$endtime) = @_;
1.113 matthew 2641: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
2642: &setup_table_names($courseid);
2643: my $dbh = &Apache::lonmysql::get_dbh();
2644: return (undef) if (! defined($dbh));
2645: my $tmptable = $courseid.'_temp_'.time;
1.114 matthew 2646: #
2647: my $symb_requirements;
1.113 matthew 2648: if (defined($Symbs) && @$Symbs) {
2649: $symb_requirements = '('.
1.114 matthew 2650: join(' OR ', map{ "(a.symb_id='".&get_symb_id($_->{'symb'}).
1.121 matthew 2651: "' AND a.part_id='".&get_part_id($_->{'part'}).
2652: "')"
1.113 matthew 2653: } @$Symbs).')';
2654: }
1.114 matthew 2655: #
2656: my $student_requirements;
2657: if ( (defined($Sections) && $Sections->[0] ne 'all')) {
1.113 matthew 2658: $student_requirements = '('.
1.114 matthew 2659: join(' OR ', map { "b.section='".$_."'" } @$Sections
1.113 matthew 2660: ).')';
2661: }
1.114 matthew 2662: #
2663: my $enrollment_requirements=undef;
2664: if (defined($enrollment) && $enrollment ne 'Any') {
2665: $enrollment_requirements = "b.status='".$enrollment."'";
2666: }
1.121 matthew 2667: #
2668: my $time_requirements = undef;
2669: if (defined($starttime)) {
2670: $time_requirements .= "a.timestamp>='".$starttime."'";
2671: if (defined($endtime)) {
2672: $time_requirements .= " AND a.timestamp<='".$endtime."'";
2673: }
2674: } elsif (defined($endtime)) {
2675: $time_requirements .= "a.timestamp<='".$endtime."'";
2676: }
1.114 matthew 2677: ##
2678: ##
1.113 matthew 2679: my $request = 'CREATE TEMPORARY TABLE IF NOT EXISTS '.$tmptable.
1.114 matthew 2680: ' SELECT a.student_id,SUM(a.awarded) AS score FROM '.
2681: $performance_table.' AS a ';
2682: if (defined($student_requirements) || defined($enrollment_requirements)) {
2683: $request .= ' NATURAL LEFT JOIN '.$student_table.' AS b ';
2684: }
2685: if (defined($symb_requirements) ||
2686: defined($student_requirements) ||
2687: defined($enrollment_requirements) ) {
1.113 matthew 2688: $request .= ' WHERE ';
2689: }
1.114 matthew 2690: if (defined($symb_requirements)) {
2691: $request .= $symb_requirements.' AND ';
2692: }
2693: if (defined($student_requirements)) {
2694: $request .= $student_requirements.' AND ';
2695: }
2696: if (defined($enrollment_requirements)) {
2697: $request .= $enrollment_requirements.' AND ';
2698: }
1.121 matthew 2699: if (defined($time_requirements)) {
2700: $request .= $time_requirements.' AND ';
2701: }
2702: $request =~ s/ AND $//; # Strip of the trailing ' AND '.
1.114 matthew 2703: $request .= ' GROUP BY a.student_id';
2704: # &Apache::lonnet::logthis("request = \n".$request);
1.113 matthew 2705: my $sth = $dbh->prepare($request);
2706: $sth->execute();
2707: if ($dbh->err) {
1.138 matthew 2708: &Apache::lonnet::logthis('error 6 = '.$dbh->errstr());
2709: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.113 matthew 2710: return undef;
2711: }
2712: $request = 'SELECT score,COUNT(*) FROM '.$tmptable.' GROUP BY score';
2713: # &Apache::lonnet::logthis("request = \n".$request);
2714: $sth = $dbh->prepare($request);
2715: $sth->execute();
2716: if ($dbh->err) {
1.138 matthew 2717: &Apache::lonnet::logthis('error 7 = '.$dbh->errstr());
2718: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.113 matthew 2719: return undef;
2720: }
2721: my $dataset = $sth->fetchall_arrayref();
2722: return $dataset;
2723: }
2724:
2725: ################################################
2726: ################################################
2727:
2728: =pod
2729:
1.61 matthew 2730: =item &setup_table_names()
2731:
2732: input: course id
2733:
2734: output: none
2735:
2736: Cleans up the package variables for local caching.
2737:
2738: =cut
2739:
2740: ################################################
2741: ################################################
2742: sub setup_table_names {
2743: my ($courseid) = @_;
2744: if (! defined($courseid)) {
2745: $courseid = $ENV{'request.course.id'};
2746: }
2747: #
2748: if (! defined($current_course) || $current_course ne $courseid) {
2749: # Clear out variables
2750: $have_read_part_table = 0;
2751: undef(%ids_by_part);
2752: undef(%parts_by_id);
2753: $have_read_symb_table = 0;
2754: undef(%ids_by_symb);
2755: undef(%symbs_by_id);
2756: $have_read_student_table = 0;
2757: undef(%ids_by_student);
2758: undef(%students_by_id);
2759: #
2760: $current_course = $courseid;
2761: }
2762: #
2763: # Set up database names
2764: my $base_id = $courseid;
2765: $symb_table = $base_id.'_'.'symb';
2766: $part_table = $base_id.'_'.'part';
2767: $student_table = $base_id.'_'.'student';
2768: $performance_table = $base_id.'_'.'performance';
2769: $parameters_table = $base_id.'_'.'parameters';
1.89 matthew 2770: $fulldump_part_table = $base_id.'_'.'partdata';
2771: $fulldump_response_table = $base_id.'_'.'responsedata';
2772: $fulldump_timestamp_table = $base_id.'_'.'timestampdata';
1.127 matthew 2773: $weight_table = $base_id.'_'.'weight';
1.89 matthew 2774: #
2775: @Tables = (
2776: $symb_table,
2777: $part_table,
2778: $student_table,
2779: $performance_table,
2780: $parameters_table,
2781: $fulldump_part_table,
2782: $fulldump_response_table,
2783: $fulldump_timestamp_table,
1.127 matthew 2784: $weight_table,
1.89 matthew 2785: );
1.61 matthew 2786: return;
1.3 stredwic 2787: }
1.1 stredwic 2788:
1.35 matthew 2789: ################################################
2790: ################################################
2791:
2792: =pod
2793:
1.57 matthew 2794: =back
2795:
2796: =item End of Local Data Caching Subroutines
2797:
2798: =cut
2799:
2800: ################################################
2801: ################################################
2802:
1.89 matthew 2803: } # End scope of table identifiers
1.57 matthew 2804:
2805: ################################################
2806: ################################################
2807:
2808: =pod
2809:
2810: =head3 Classlist Subroutines
2811:
1.35 matthew 2812: =item &get_classlist();
2813:
2814: Retrieve the classist of a given class or of the current class. Student
2815: information is returned from the classlist.db file and, if needed,
2816: from the students environment.
2817:
2818: Optional arguments are $cid, $cdom, and $cnum (course id, course domain,
2819: and course number, respectively). Any omitted arguments will be taken
2820: from the current environment ($ENV{'request.course.id'},
2821: $ENV{'course.'.$cid.'.domain'}, and $ENV{'course.'.$cid.'.num'}).
2822:
2823: Returns a reference to a hash which contains:
2824: keys '$sname:$sdom'
1.136 raeburn 2825: values [$sdom,$sname,$end,$start,$id,$section,$fullname,$status,$type,$lockedtype]
1.54 bowersj2 2826:
2827: The constant values CL_SDOM, CL_SNAME, CL_END, etc. can be used
2828: as indices into the returned list to future-proof clients against
2829: changes in the list order.
1.35 matthew 2830:
2831: =cut
2832:
2833: ################################################
2834: ################################################
1.54 bowersj2 2835:
2836: sub CL_SDOM { return 0; }
2837: sub CL_SNAME { return 1; }
2838: sub CL_END { return 2; }
2839: sub CL_START { return 3; }
2840: sub CL_ID { return 4; }
2841: sub CL_SECTION { return 5; }
2842: sub CL_FULLNAME { return 6; }
2843: sub CL_STATUS { return 7; }
1.111 raeburn 2844: sub CL_TYPE { return 8; }
1.136 raeburn 2845: sub CL_LOCKEDTYPE { return 9; }
1.35 matthew 2846:
2847: sub get_classlist {
2848: my ($cid,$cdom,$cnum) = @_;
2849: $cid = $cid || $ENV{'request.course.id'};
2850: $cdom = $cdom || $ENV{'course.'.$cid.'.domain'};
2851: $cnum = $cnum || $ENV{'course.'.$cid.'.num'};
1.57 matthew 2852: my $now = time;
1.35 matthew 2853: #
2854: my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
2855: while (my ($student,$info) = each(%classlist)) {
1.60 matthew 2856: if ($student =~ /^(con_lost|error|no_such_host)/i) {
2857: &Apache::lonnet::logthis('get_classlist error for '.$cid.':'.$student);
2858: return undef;
2859: }
1.35 matthew 2860: my ($sname,$sdom) = split(/:/,$student);
2861: my @Values = split(/:/,$info);
1.136 raeburn 2862: my ($end,$start,$id,$section,$fullname,$type,$lockedtype);
1.35 matthew 2863: if (@Values > 2) {
1.136 raeburn 2864: ($end,$start,$id,$section,$fullname,$type,$lockedtype) = @Values;
1.35 matthew 2865: } else { # We have to get the data ourselves
2866: ($end,$start) = @Values;
1.37 matthew 2867: $section = &Apache::lonnet::getsection($sdom,$sname,$cid);
1.35 matthew 2868: my %info=&Apache::lonnet::get('environment',
2869: ['firstname','middlename',
2870: 'lastname','generation','id'],
2871: $sdom, $sname);
2872: my ($tmp) = keys(%info);
2873: if ($tmp =~/^(con_lost|error|no_such_host)/i) {
2874: $fullname = 'not available';
2875: $id = 'not available';
1.38 matthew 2876: &Apache::lonnet::logthis('unable to retrieve environment '.
2877: 'for '.$sname.':'.$sdom);
1.35 matthew 2878: } else {
1.141 ! albertel 2879: $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
1.35 matthew 2880: $id = $info{'id'};
2881: }
1.36 matthew 2882: # Update the classlist with this students information
2883: if ($fullname ne 'not available') {
1.141 ! albertel 2884: my $enrolldata = join(':',$end,$start,$id,$section,$fullname);
! 2885: my $reply=&Apache::lonnet::cput('classlist',
1.36 matthew 2886: {$student => $enrolldata},
2887: $cdom,$cnum);
2888: if ($reply !~ /^(ok|delayed)/) {
2889: &Apache::lonnet::logthis('Unable to update classlist for '.
2890: 'student '.$sname.':'.$sdom.
2891: ' error:'.$reply);
2892: }
2893: }
1.35 matthew 2894: }
2895: my $status='Expired';
2896: if(((!$end) || $now < $end) && ((!$start) || ($now > $start))) {
2897: $status='Active';
2898: }
2899: $classlist{$student} =
1.136 raeburn 2900: [$sdom,$sname,$end,$start,$id,$section,$fullname,$status,$type,$lockedtype];
1.35 matthew 2901: }
2902: if (wantarray()) {
2903: return (\%classlist,['domain','username','end','start','id',
1.136 raeburn 2904: 'section','fullname','status','type','lockedtype']);
1.35 matthew 2905: } else {
2906: return \%classlist;
2907: }
2908: }
2909:
1.1 stredwic 2910: # ----- END HELPER FUNCTIONS --------------------------------------------
2911:
2912: 1;
2913: __END__
1.36 matthew 2914:
1.35 matthew 2915:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>