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