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