File:
[LON-CAPA] /
loncom /
interface /
loncoursedata.pm
Revision
1.16:
download - view:
text,
annotated -
select for diffs
Wed Aug 14 16:18:55 2002 UTC (21 years, 9 months ago) by
stredwic
Branches:
MAIN
CVS tags:
HEAD
Added section selection to problem statistics and problem analysis.
Fixed some divide by zeros in lonproblemstatistics.
Broke up the table of option response problems and gave them the
heading of the sequence in which they lie.
1: # The LearningOnline Network with CAPA
2: # (Publication Handler
3: #
4: # $Id: loncoursedata.pm,v 1.16 2002/08/14 16:18:55 stredwic Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: ###
29:
30: =pod
31:
32: =head1 NAME
33:
34: loncoursedata
35:
36: =head1 SYNOPSIS
37:
38: Set of functions that download and process student information.
39:
40: =head1 PACKAGES USED
41:
42: Apache::Constants qw(:common :http)
43: Apache::lonnet()
44: HTML::TokeParser
45: GDBM_File
46:
47: =cut
48:
49: package Apache::loncoursedata;
50:
51: use strict;
52: use Apache::Constants qw(:common :http);
53: use Apache::lonnet();
54: use Apache::lonhtmlcommon;
55: use HTML::TokeParser;
56: use GDBM_File;
57:
58: =pod
59:
60: =head1 DOWNLOAD INFORMATION
61:
62: This section contains all the files that get data from other servers
63: and/or itself. There is one function that has a call to get remote
64: information but isn't included here which is ProcessTopLevelMap. The
65: usage was small enough to be ignored, but that portion may be moved
66: here in the future.
67:
68: =cut
69:
70: # ----- DOWNLOAD INFORMATION -------------------------------------------
71:
72: =pod
73:
74: =item &DownloadClasslist()
75:
76: Collects lastname, generation, middlename, firstname, PID, and section for each
77: student from their environment database. The list of students is built from
78: collecting a classlist for the course that is to be displayed.
79:
80: =over 4
81:
82: Input: $courseID, $c
83:
84: $courseID: The id of the course
85:
86: $c: The connection class that can determine if the browser has aborted. It
87: is used to short circuit this function so that it doesn't continue to
88: get information when there is no need.
89:
90: Output: \%classlist
91:
92: \%classlist: A pointer to a hash containing the following data:
93:
94: -A list of student name:domain (as keys) (known below as $name)
95:
96: -A hash pointer for each student containing lastname, generation, firstname,
97: middlename, and PID : Key is $name.'studentInformation'
98:
99: -A hash pointer to each students section data : Key is $name.section
100:
101: =back
102:
103: =cut
104:
105: sub DownloadClasslist {
106: my ($courseID, $lastDownloadTime, $c)=@_;
107: my ($courseDomain,$courseNumber)=split(/\_/,$courseID);
108: my %classlist;
109:
110: my $modifiedTime = &GetFileTimestamp($courseDomain, $courseNumber,
111: 'classlist.db',
112: $Apache::lonnet::perlvar{'lonUsersDir'});
113:
114: if($lastDownloadTime ne 'Not downloaded' &&
115: $lastDownloadTime >= $modifiedTime && $modifiedTime >= 0) {
116: $classlist{'lastDownloadTime'}=time;
117: $classlist{'UpToDate'} = 'true';
118: return \%classlist;
119: }
120:
121: %classlist=&Apache::lonnet::dump('classlist',$courseDomain, $courseNumber);
122: my ($checkForError)=keys (%classlist);
123: if($checkForError =~ /^(con_lost|error|no_such_host)/i) {
124: return \%classlist;
125: }
126:
127: foreach my $name (keys(%classlist)) {
128: if($c->aborted()) {
129: $classlist{'error'}='aborted';
130: return \%classlist;
131: }
132:
133: my ($studentName,$studentDomain) = split(/\:/,$name);
134: # Download student environment data, specifically the full name and id.
135: my %studentInformation=&Apache::lonnet::get('environment',
136: ['lastname','generation',
137: 'firstname','middlename',
138: 'id'],
139: $studentDomain,
140: $studentName);
141: $classlist{$name.':studentInformation'}=\%studentInformation;
142:
143: if($c->aborted()) {
144: $classlist{'error'}='aborted';
145: return \%classlist;
146: }
147:
148: #Section
149: my %section=&Apache::lonnet::dump('roles',$studentDomain,$studentName);
150: $classlist{$name.':sections'}=\%section;
151: }
152:
153: $classlist{'UpToDate'} = 'false';
154: $classlist{'lastDownloadTime'}=time;
155:
156: return \%classlist;
157: }
158:
159: =pod
160:
161: =item &DownloadCourseInformation()
162:
163: Dump of all the course information for a single student. There is no
164: pruning of data, it is all stored in a hash and returned. It also
165: checks the timestamp of the students course database file and only downloads
166: if it has been modified since the last download.
167:
168: =over 4
169:
170: Input: $name, $courseID
171:
172: $name: student name:domain
173:
174: $courseID: The id of the course
175:
176: Output: \%courseData
177:
178: \%courseData: A hash pointer to the raw data from the student's course
179: database.
180:
181: =back
182:
183: =cut
184:
185: sub DownloadCourseInformation {
186: my ($namedata,$courseID,$lastDownloadTime,$WhatIWant)=@_;
187: my %courseData;
188: my ($name,$domain) = split(/\:/,$namedata);
189:
190: my $modifiedTime = &GetFileTimestamp($domain, $name,
191: $courseID.'.db',
192: $Apache::lonnet::perlvar{'lonUsersDir'});
193:
194: if($lastDownloadTime >= $modifiedTime && $modifiedTime >= 0) {
195: $courseData{$namedata.':lastDownloadTime'}=time;
196: $courseData{$namedata.':UpToDate'} = 'true';
197: return \%courseData;
198: }
199:
200: # Download course data
201: if(!defined($WhatIWant)) {
202: $WhatIWant = '.';
203: }
204: %courseData=&Apache::lonnet::dump($courseID, $domain, $name, $WhatIWant);
205: $courseData{'UpToDate'} = 'false';
206: $courseData{'lastDownloadTime'}=time;
207:
208: my %newData;
209: foreach (keys(%courseData)) {
210: $newData{$namedata.':'.$_} = $courseData{$_};
211: }
212:
213: return \%newData;
214: }
215:
216: # ----- END DOWNLOAD INFORMATION ---------------------------------------
217:
218: =pod
219:
220: =head1 PROCESSING FUNCTIONS
221:
222: These functions process all the data for all the students. Also, they
223: are the only functions that access the cache database for writing. Thus
224: they are the only functions that cache data. The downloading and caching
225: were separated to reduce problems with stopping downloading then can't
226: tie hash to database later.
227:
228: =cut
229:
230: # ----- PROCESSING FUNCTIONS ---------------------------------------
231:
232: =pod
233:
234: =item &ProcessTopResourceMap()
235:
236: Trace through the "big hash" created in rat/lonuserstate.pm::loadmap.
237: Basically, this function organizes a subset of the data and stores it in
238: cached data. The data stored is the problems, sequences, sequence titles,
239: parts of problems, and their ordering. Column width information is also
240: partially handled here on a per sequence basis.
241:
242: =over 4
243:
244: Input: $cache, $c
245:
246: $cache: A pointer to a hash to store the information
247:
248: $c: The connection class used to determine if an abort has been sent to the
249: browser
250:
251: Output: A string that contains an error message or "OK" if everything went
252: smoothly.
253:
254: =back
255:
256: =cut
257:
258: sub ProcessTopResourceMap {
259: my ($cache,$c)=@_;
260: my %hash;
261: my $fn=$ENV{'request.course.fn'};
262: if(-e "$fn.db") {
263: my $tieTries=0;
264: while($tieTries < 3) {
265: if($c->aborted()) {
266: return;
267: }
268: if(tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
269: last;
270: }
271: $tieTries++;
272: sleep 1;
273: }
274: if($tieTries >= 3) {
275: return 'Coursemap undefined.';
276: }
277: } else {
278: return 'Can not open Coursemap.';
279: }
280:
281: # Initialize state machine. Set information pointing to top level map.
282: my (@sequences, @currentResource, @finishResource);
283: my ($currentSequence, $currentResourceID, $lastResourceID);
284:
285: $currentResourceID=$hash{'ids_/res/'.$ENV{'request.course.uri'}};
286: push(@currentResource, $currentResourceID);
287: $lastResourceID=-1;
288: $currentSequence=-1;
289: my $topLevelSequenceNumber = $currentSequence;
290:
291: my %sequenceRecord;
292: while(1) {
293: if($c->aborted()) {
294: last;
295: }
296: # HANDLE NEW SEQUENCE!
297: #if page || sequence
298: if(defined($hash{'map_pc_'.$hash{'src_'.$currentResourceID}}) &&
299: !defined($sequenceRecord{$currentResourceID})) {
300: $sequenceRecord{$currentResourceID}++;
301: push(@sequences, $currentSequence);
302: push(@currentResource, $currentResourceID);
303: push(@finishResource, $lastResourceID);
304:
305: $currentSequence=$hash{'map_pc_'.$hash{'src_'.$currentResourceID}};
306:
307: # Mark sequence as containing problems. If it doesn't, then
308: # it will be removed when processing for this sequence is
309: # complete. This allows the problems in a sequence
310: # to be outputed before problems in the subsequences
311: if(!defined($cache->{'orderedSequences'})) {
312: $cache->{'orderedSequences'}=$currentSequence;
313: } else {
314: $cache->{'orderedSequences'}.=':'.$currentSequence;
315: }
316:
317: $lastResourceID=$hash{'map_finish_'.
318: $hash{'src_'.$currentResourceID}};
319: $currentResourceID=$hash{'map_start_'.
320: $hash{'src_'.$currentResourceID}};
321:
322: if(!($currentResourceID) || !($lastResourceID)) {
323: $currentSequence=pop(@sequences);
324: $currentResourceID=pop(@currentResource);
325: $lastResourceID=pop(@finishResource);
326: if($currentSequence eq $topLevelSequenceNumber) {
327: last;
328: }
329: }
330: next;
331: }
332:
333: # Handle gradable resources: exams, problems, etc
334: $currentResourceID=~/(\d+)\.(\d+)/;
335: my $partA=$1;
336: my $partB=$2;
337: if($hash{'src_'.$currentResourceID}=~
338: /\.(problem|exam|quiz|assess|survey|form)$/ &&
339: $partA eq $currentSequence &&
340: !defined($sequenceRecord{$currentSequence.':'.
341: $currentResourceID})) {
342: $sequenceRecord{$currentSequence.':'.$currentResourceID}++;
343: my $Problem = &Apache::lonnet::symbclean(
344: &Apache::lonnet::declutter($hash{'map_id_'.$partA}).
345: '___'.$partB.'___'.
346: &Apache::lonnet::declutter($hash{'src_'.
347: $currentResourceID}));
348:
349: $cache->{$currentResourceID.':problem'}=$Problem;
350: if(!defined($cache->{$currentSequence.':problems'})) {
351: $cache->{$currentSequence.':problems'}=$currentResourceID;
352: } else {
353: $cache->{$currentSequence.':problems'}.=
354: ':'.$currentResourceID;
355: }
356:
357: my $meta=$hash{'src_'.$currentResourceID};
358: # $cache->{$currentResourceID.':title'}=
359: # &Apache::lonnet::metdata($meta,'title');
360: $cache->{$currentResourceID.':title'}=
361: $hash{'title_'.$currentResourceID};
362: $cache->{$currentResourceID.':source'}=
363: $hash{'src_'.$currentResourceID};
364:
365: # Get Parts for problem
366: my %beenHere;
367: foreach (split(/\,/,&Apache::lonnet::metadata($meta,'packages'))) {
368: if(/^\w+response_\d+.*/) {
369: my (undef, $partId, $responseId) = split(/_/,$_);
370: if($beenHere{'p:'.$partId} == 0) {
371: $beenHere{'p:'.$partId}++;
372: if(!defined($cache->{$currentSequence.':'.
373: $currentResourceID.':parts'})) {
374: $cache->{$currentSequence.':'.$currentResourceID.
375: ':parts'}=$partId;
376: } else {
377: $cache->{$currentSequence.':'.$currentResourceID.
378: ':parts'}.=':'.$partId;
379: }
380: }
381: if($beenHere{'r:'.$partId.':'.$responseId} == 0) {
382: $beenHere{'r:'.$partId.':'.$responseId}++;
383: if(!defined($cache->{$currentSequence.':'.
384: $currentResourceID.':'.$partId.
385: ':responseIDs'})) {
386: $cache->{$currentSequence.':'.$currentResourceID.
387: ':'.$partId.':responseIDs'}=$responseId;
388: } else {
389: $cache->{$currentSequence.':'.$currentResourceID.
390: ':'.$partId.':responseIDs'}.=':'.
391: $responseId;
392: }
393: }
394: if(/^optionresponse/ &&
395: $beenHere{'o:'.$partId.':'.$currentResourceID} == 0) {
396: $beenHere{'o:'.$partId.$currentResourceID}++;
397: if(defined($cache->{'OptionResponses'})) {
398: $cache->{'OptionResponses'}.= ':::'.
399: $currentSequence.':'.$currentResourceID.':'.
400: $partId.':'.$responseId;
401: } else {
402: $cache->{'OptionResponses'}= $currentSequence.':'.
403: $currentResourceID.':'.
404: $partId.':'.$responseId;
405: }
406: }
407: }
408: }
409: }
410:
411: # if resource == finish resource, then it is the end of a sequence/page
412: if($currentResourceID eq $lastResourceID) {
413: # pop off last resource of sequence
414: $currentResourceID=pop(@currentResource);
415: $lastResourceID=pop(@finishResource);
416:
417: if(defined($cache->{$currentSequence.':problems'})) {
418: # Capture sequence information here
419: $cache->{$currentSequence.':title'}=
420: $hash{'title_'.$currentResourceID};
421: $cache->{$currentSequence.':source'}=
422: $hash{'src_'.$currentResourceID};
423:
424: my $totalProblems=0;
425: foreach my $currentProblem (split(/\:/,
426: $cache->{$currentSequence.
427: ':problems'})) {
428: foreach (split(/\:/,$cache->{$currentSequence.':'.
429: $currentProblem.
430: ':parts'})) {
431: $totalProblems++;
432: }
433: }
434: my @titleLength=split(//,$cache->{$currentSequence.
435: ':title'});
436: # $extra is 3 for problems correct and 3 for space
437: # between problems correct and problem output
438: my $extra = 6;
439: if(($totalProblems + $extra) > (scalar @titleLength)) {
440: $cache->{$currentSequence.':columnWidth'}=
441: $totalProblems + $extra;
442: } else {
443: $cache->{$currentSequence.':columnWidth'}=
444: (scalar @titleLength);
445: }
446: } else {
447: # Remove sequence from list, if it contains no problems to
448: # display.
449: $cache->{'orderedSequences'}=~s/$currentSequence//;
450: $cache->{'orderedSequences'}=~s/::/:/g;
451: $cache->{'orderedSequences'}=~s/^:|:$//g;
452: }
453:
454: $currentSequence=pop(@sequences);
455: if($currentSequence eq $topLevelSequenceNumber) {
456: last;
457: }
458: }
459:
460: # MOVE!!!
461: # move to next resource
462: unless(defined($hash{'to_'.$currentResourceID})) {
463: # big problem, need to handle. Next is probably wrong
464: my $errorMessage = 'Big problem in ';
465: $errorMessage .= 'loncoursedata::ProcessTopLevelMap.';
466: $errorMessage .= ' bighash to_$currentResourceID not defined!';
467: &Apache::lonnet::logthis($errorMessage);
468: last;
469: }
470: my @nextResources=();
471: foreach (split(/\,/,$hash{'to_'.$currentResourceID})) {
472: if(!defined($sequenceRecord{$currentSequence.':'.
473: $hash{'goesto_'.$_}})) {
474: push(@nextResources, $hash{'goesto_'.$_});
475: }
476: }
477: push(@currentResource, @nextResources);
478: # Set the next resource to be processed
479: $currentResourceID=pop(@currentResource);
480: }
481:
482: unless (untie(%hash)) {
483: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
484: "Could not untie coursemap $fn (browse)".
485: ".</font>");
486: }
487:
488: return 'OK';
489: }
490:
491: =pod
492:
493: =item &ProcessClasslist()
494:
495: Taking the class list dumped from &DownloadClasslist(), all the
496: students and their non-class information is processed using the
497: &ProcessStudentInformation() function. A date stamp is also recorded for
498: when the data was processed.
499:
500: Takes data downloaded for a student and breaks it up into managable pieces and
501: stored in cache data. The username, domain, class related date, PID,
502: full name, and section are all processed here.
503:
504:
505: =over 4
506:
507: Input: $cache, $classlist, $courseID, $ChartDB, $c
508:
509: $cache: A hash pointer to store the data
510:
511: $classlist: The hash of data collected about a student from
512: &DownloadClasslist(). The hash contains a list of students, a pointer
513: to a hash of student information for each student, and each student's section
514: number.
515:
516: $courseID: The course ID
517:
518: $ChartDB: The name of the cache database file.
519:
520: $c: The connection class used to determine if an abort has been sent to the
521: browser
522:
523: Output: @names
524:
525: @names: An array of students whose information has been processed, and are to
526: be considered in an arbitrary order.
527:
528: =back
529:
530: =cut
531:
532: sub ProcessClasslist {
533: my ($cache,$classlist,$courseID,$c)=@_;
534: my @names=();
535:
536: $cache->{'ClasslistTimeStamp'}=$classlist->{'lastDownloadTime'};
537: if($classlist->{'UpToDate'} eq 'true') {
538: return split(/:::/,$cache->{'NamesOfStudents'});;
539: }
540:
541: foreach my $name (keys(%$classlist)) {
542: if($name =~ /\:section/ || $name =~ /\:studentInformation/ ||
543: $name eq '' || $name eq 'UpToDate' || $name eq 'lastDownloadTime') {
544: next;
545: }
546: if($c->aborted()) {
547: return ();
548: }
549: push(@names,$name);
550: my $studentInformation = $classlist->{$name.':studentInformation'},
551: my $sectionData = $classlist->{$name.':sections'},
552: my $date = $classlist->{$name},
553: my ($studentName,$studentDomain) = split(/\:/,$name);
554:
555: $cache->{$name.':username'}=$studentName;
556: $cache->{$name.':domain'}=$studentDomain;
557: # Initialize timestamp for student
558: if(!defined($cache->{$name.':lastDownloadTime'})) {
559: $cache->{$name.':lastDownloadTime'}='Not downloaded';
560: $cache->{$name.':updateTime'}=' Not updated';
561: }
562:
563: my ($checkForError)=keys(%$studentInformation);
564: if($checkForError =~ /^(con_lost|error|no_such_host)/i) {
565: $cache->{$name.':error'}=
566: 'Could not download student environment data.';
567: $cache->{$name.':fullname'}='';
568: $cache->{$name.':id'}='';
569: } else {
570: $cache->{$name.':fullname'}=&ProcessFullName(
571: $studentInformation->{'lastname'},
572: $studentInformation->{'generation'},
573: $studentInformation->{'firstname'},
574: $studentInformation->{'middlename'});
575: $cache->{$name.':id'}=$studentInformation->{'id'};
576: }
577:
578: my ($end, $start)=split(':',$date);
579: $courseID=~s/\_/\//g;
580: $courseID=~s/^(\w)/\/$1/;
581:
582: my $sec='';
583: foreach my $key (keys (%$sectionData)) {
584: my $value = $sectionData->{$key};
585: if ($key=~/^$courseID(?:\/)*(\w+)*\_st$/) {
586: my $tempsection=$1;
587: if($key eq $courseID.'_st') {
588: $tempsection='';
589: }
590: my ($dummy,$roleend,$rolestart)=split(/\_/,$value);
591: if($roleend eq $end && $rolestart eq $start) {
592: $sec = $tempsection;
593: last;
594: }
595: }
596: }
597:
598: my $status='Expired';
599: if(((!$end) || time < $end) && ((!$start) || (time > $start))) {
600: $status='Active';
601: }
602: $cache->{$name.':Status'}=$status;
603: $cache->{$name.':section'}=$sec;
604:
605: if($sec eq '' || !defined($sec) || $sec eq ' ') {
606: $sec = 'none';
607: }
608: if(defined($cache->{'sectionList'})) {
609: if($cache->{'sectionList'} !~ /(^$sec:|^$sec$|:$sec$|:$sec:)/) {
610: $cache->{'sectionList'} .= ':'.$sec;
611: }
612: } else {
613: $cache->{'sectionList'} = $sec;
614: }
615: }
616:
617: $cache->{'ClasslistTimestamp'}=time;
618: $cache->{'NamesOfStudents'}=join(':::',@names);
619:
620: return @names;
621: }
622:
623: =pod
624:
625: =item &ProcessStudentData()
626:
627: Takes the course data downloaded for a student in
628: &DownloadCourseInformation() and breaks it up into key value pairs
629: to be stored in the cached data. The keys are comprised of the
630: $username:$domain:$keyFromCourseDatabase. The student username:domain is
631: stored away signifying that the student's information has been downloaded and
632: can be reused from cached data.
633:
634: =over 4
635:
636: Input: $cache, $courseData, $name
637:
638: $cache: A hash pointer to store data
639:
640: $courseData: A hash pointer that points to the course data downloaded for a
641: student.
642:
643: $name: username:domain
644:
645: Output: None
646:
647: *NOTE: There is no output, but an error message is stored away in the cache
648: data. This is checked in &FormatStudentData(). The key username:domain:error
649: will only exist if an error occured. The error is an error from
650: &DownloadCourseInformation().
651:
652: =back
653:
654: =cut
655:
656: sub ProcessStudentData {
657: my ($cache,$courseData,$name)=@_;
658:
659: if(!&CheckDateStampError($courseData, $cache, $name)) {
660: return;
661: }
662:
663: foreach (keys %$courseData) {
664: $cache->{$_}=$courseData->{$_};
665: }
666:
667: return;
668: }
669:
670: sub ExtractStudentData {
671: my ($input, $output, $data, $name)=@_;
672:
673: if(!&CheckDateStampError($input, $data, $name)) {
674: return;
675: }
676:
677: my ($username,$domain)=split(':',$name);
678:
679: my $Version;
680: my $problemsCorrect = 0;
681: my $totalProblems = 0;
682: my $problemsSolved = 0;
683: my $numberOfParts = 0;
684: my $totalAwarded = 0;
685: foreach my $sequence (split(':', $data->{'orderedSequences'})) {
686: foreach my $problemID (split(':', $data->{$sequence.':problems'})) {
687: my $problem = $data->{$problemID.':problem'};
688: my $LatestVersion = $input->{$name.':version:'.$problem};
689:
690: # Output dashes for all the parts of this problem if there
691: # is no version information about the current problem.
692: if(!$LatestVersion) {
693: foreach my $part (split(/\:/,$data->{$sequence.':'.
694: $problemID.
695: ':parts'})) {
696: $output->{$name.':'.$problemID.':'.$part.':tries'} = 0;
697: $output->{$name.':'.$problemID.':'.$part.':awarded'} = 0;
698: $output->{$name.':'.$problemID.':'.$part.':code'} = ' ';
699: $totalProblems++;
700: }
701: $output->{$name.':'.$problemID.':NoVersion'} = 'true';
702: next;
703: }
704:
705: my %partData=undef;
706: # Initialize part data, display skips correctly
707: # Skip refers to when a student made no submissions on that
708: # part/problem.
709: foreach my $part (split(/\:/,$data->{$sequence.':'.
710: $problemID.
711: ':parts'})) {
712: $partData{$part.':tries'}=0;
713: $partData{$part.':code'}=' ';
714: $partData{$part.':awarded'}=0;
715: $partData{$part.':timestamp'}=0;
716: foreach my $response (split(':', $data->{$sequence.':'.
717: $problemID.':'.
718: $part.':responseIDs'})) {
719: $partData{$part.':'.$response.':submission'}='';
720: }
721: }
722:
723: # Looping through all the versions of each part, starting with the
724: # oldest version. Basically, it gets the most recent
725: # set of grade data for each part.
726: my @submissions = ();
727: for(my $Version=1; $Version<=$LatestVersion; $Version++) {
728: foreach my $part (split(/\:/,$data->{$sequence.':'.
729: $problemID.
730: ':parts'})) {
731:
732: if(!defined($input->{"$name:$Version:$problem".
733: ":resource.$part.solved"})) {
734: # No grade for this submission, so skip
735: next;
736: }
737:
738: my $tries=0;
739: my $code=' ';
740: my $awarded=0;
741:
742: $tries = $input->{$name.':'.$Version.':'.$problem.
743: ':resource.'.$part.'.tries'};
744: $awarded = $input->{$name.':'.$Version.':'.$problem.
745: ':resource.'.$part.'.awarded'};
746:
747: $partData{$part.':awarded'}=($awarded) ? $awarded : 0;
748: $partData{$part.':tries'}=($tries) ? $tries : 0;
749:
750: $partData{$part.':timestamp'}=$input->{$name.':'.$Version.':'.
751: $problem.
752: ':timestamp'};
753: if(!$input->{$name.':'.$Version.':'.$problem.':resource.'.$part.
754: '.previous'}) {
755: foreach my $response (split(':',
756: $data->{$sequence.':'.
757: $problemID.':'.
758: $part.':responseIDs'})) {
759: @submissions=($input->{$name.':'.$Version.':'.
760: $problem.
761: ':resource.'.$part.'.'.
762: $response.'.submission'},
763: @submissions);
764: }
765: }
766:
767: my $val = $input->{$name.':'.$Version.':'.$problem.
768: ':resource.'.$part.'.solved'};
769: if ($val eq 'correct_by_student') {$code = '*';}
770: elsif ($val eq 'correct_by_override') {$code = '+';}
771: elsif ($val eq 'incorrect_attempted') {$code = '.';}
772: elsif ($val eq 'incorrect_by_override'){$code = '-';}
773: elsif ($val eq 'excused') {$code = 'x';}
774: elsif ($val eq 'ungraded_attempted') {$code = '#';}
775: else {$code = ' ';}
776: $partData{$part.':code'}=$code;
777: }
778: }
779:
780: foreach my $part (split(/\:/,$data->{$sequence.':'.$problemID.
781: ':parts'})) {
782: $output->{$name.':'.$problemID.':'.$part.':wrong'} =
783: $partData{$part.':tries'};
784:
785: if($partData{$part.':code'} eq '*') {
786: $output->{$name.':'.$problemID.':'.$part.':wrong'}--;
787: $problemsCorrect++;
788: } elsif($partData{$part.':code'} eq '+') {
789: $output->{$name.':'.$problemID.':'.$part.':wrong'}--;
790: $problemsCorrect++;
791: }
792:
793: $output->{$name.':'.$problemID.':'.$part.':tries'} =
794: $partData{$part.':tries'};
795: $output->{$name.':'.$problemID.':'.$part.':code'} =
796: $partData{$part.':code'};
797: $output->{$name.':'.$problemID.':'.$part.':awarded'} =
798: $partData{$part.':awarded'};
799: $totalAwarded += $partData{$part.':awarded'};
800: $output->{$name.':'.$problemID.':'.$part.':timestamp'} =
801: $partData{$part.':timestamp'};
802: foreach my $response (split(':', $data->{$sequence.':'.
803: $problemID.':'.
804: $part.':responseIDs'})) {
805: $output->{$name.':'.$problemID.':'.$part.':'.$response.
806: ':submission'}=join(':::',@submissions);
807: }
808:
809: if($partData{$part.':code'} ne 'x') {
810: $totalProblems++;
811: }
812: }
813: }
814:
815: $output->{$name.':'.$sequence.':problemsCorrect'} = $problemsCorrect;
816: $problemsSolved += $problemsCorrect;
817: $problemsCorrect=0;
818: }
819:
820: $output->{$name.':problemsSolved'} = $problemsSolved;
821: $output->{$name.':totalProblems'} = $totalProblems;
822: $output->{$name.':totalAwarded'} = $totalAwarded;
823:
824: return;
825: }
826:
827: sub LoadDiscussion {
828: my ($courseID)=@_;
829: my %Discuss=();
830: my %contrib=&Apache::lonnet::dump(
831: $courseID,
832: $ENV{'course.'.$courseID.'.domain'},
833: $ENV{'course.'.$courseID.'.num'});
834:
835: #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
836:
837: foreach my $temp(keys %contrib) {
838: if ($temp=~/^version/) {
839: my $ver=$contrib{$temp};
840: my ($dummy,$prb)=split(':',$temp);
841: for (my $idx=1; $idx<=$ver; $idx++ ) {
842: my $name=$contrib{"$idx:$prb:sendername"};
843: $Discuss{"$name:$prb"}=$idx;
844: }
845: }
846: }
847:
848: return \%Discuss;
849: }
850:
851: # ----- END PROCESSING FUNCTIONS ---------------------------------------
852:
853: =pod
854:
855: =head1 HELPER FUNCTIONS
856:
857: These are just a couple of functions do various odd and end
858: jobs.
859:
860: =cut
861:
862: # ----- HELPER FUNCTIONS -----------------------------------------------
863:
864: sub CheckDateStampError {
865: my ($courseData, $cache, $name)=@_;
866: if($courseData->{$name.':UpToDate'} eq 'true') {
867: $cache->{$name.':lastDownloadTime'} =
868: $courseData->{$name.':lastDownloadTime'};
869: if($courseData->{$name.':lastDownloadTime'} eq 'Not downloaded') {
870: $cache->{$name.':updateTime'} = ' Not updated';
871: } else {
872: $cache->{$name.':updateTime'}=
873: localtime($courseData->{$name.':lastDownloadTime'});
874: }
875: return 0;
876: }
877:
878: $cache->{$name.':lastDownloadTime'}=$courseData->{$name.':lastDownloadTime'};
879: if($courseData->{$name.':lastDownloadTime'} eq 'Not downloaded') {
880: $cache->{$name.':updateTime'} = ' Not updated';
881: } else {
882: $cache->{$name.':updateTime'}=
883: localtime($courseData->{$name.':lastDownloadTime'});
884: }
885:
886: if(defined($courseData->{$name.':error'})) {
887: $cache->{$name.':error'}=$courseData->{$name.':error'};
888: return 0;
889: }
890:
891: return 1;
892: }
893:
894: =pod
895:
896: =item &ProcessFullName()
897:
898: Takes lastname, generation, firstname, and middlename (or some partial
899: set of this data) and returns the full name version as a string. Format
900: is Lastname generation, firstname middlename or a subset of this.
901:
902: =cut
903:
904: sub ProcessFullName {
905: my ($lastname, $generation, $firstname, $middlename)=@_;
906: my $Str = '';
907:
908: if($lastname ne '') {
909: $Str .= $lastname.' ';
910: if($generation ne '') {
911: $Str .= $generation;
912: } else {
913: chop($Str);
914: }
915: $Str .= ', ';
916: if($firstname ne '') {
917: $Str .= $firstname.' ';
918: }
919: if($middlename ne '') {
920: $Str .= $middlename;
921: } else {
922: chop($Str);
923: if($firstname eq '') {
924: chop($Str);
925: }
926: }
927: } else {
928: if($firstname ne '') {
929: $Str .= $firstname.' ';
930: }
931: if($middlename ne '') {
932: $Str .= $middlename.' ';
933: }
934: if($generation ne '') {
935: $Str .= $generation;
936: } else {
937: chop($Str);
938: }
939: }
940:
941: return $Str;
942: }
943:
944: =pod
945:
946: =item &TestCacheData()
947:
948: Determine if the cache database can be accessed with a tie. It waits up to
949: ten seconds before returning failure. This function exists to help with
950: the problems with stopping the data download. When an abort occurs and the
951: user quickly presses a form button and httpd child is created. This
952: child needs to wait for the other to finish (hopefully within ten seconds).
953:
954: =over 4
955:
956: Input: $ChartDB
957:
958: $ChartDB: The name of the cache database to be opened
959:
960: Output: -1, 0, 1
961:
962: -1: Couldn't tie database
963: 0: Use cached data
964: 1: New cache database created, use that.
965:
966: =back
967:
968: =cut
969:
970: sub TestCacheData {
971: my ($ChartDB,$isRecalculate,$totalDelay)=@_;
972: my $isCached=-1;
973: my %testData;
974: my $tieTries=0;
975:
976: if(!defined($totalDelay)) {
977: $totalDelay = 10;
978: }
979:
980: if ((-e "$ChartDB") && (!$isRecalculate)) {
981: $isCached = 1;
982: } else {
983: $isCached = 0;
984: }
985:
986: while($tieTries < $totalDelay) {
987: my $result=0;
988: if($isCached) {
989: $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_READER(),0640);
990: } else {
991: $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_NEWDB(),0640);
992: }
993: if($result) {
994: last;
995: }
996: $tieTries++;
997: sleep 1;
998: }
999: if($tieTries >= $totalDelay) {
1000: return -1;
1001: }
1002:
1003: untie(%testData);
1004:
1005: return $isCached;
1006: }
1007:
1008: sub DownloadStudentCourseData {
1009: my ($students,$checkDate,$cacheDB,$extract,$status,$courseID,$r,$c)=@_;
1010:
1011: my $title = 'LON-CAPA Statistics';
1012: my $heading = 'Download and Process Course Data';
1013: my $studentCount = scalar(@$students);
1014: my %cache;
1015:
1016: my $WhatIWant;
1017: $WhatIWant = '(^version:(\w|\/|\.|-)+?$|';
1018: $WhatIWant .= '^\d+:(\w|\/|\.|-)+?:(resource\.\d+\.';
1019: $WhatIWant .= '(solved|tries|previous|awarded|(\d+\.submission))\s*$';
1020: $WhatIWant .= '|timestamp)';
1021: $WhatIWant .= ')';
1022:
1023: if($status eq 'true') {
1024: &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
1025: }
1026: my $count=1;
1027: foreach (@$students) {
1028: if($c->aborted()) { return 'Aborted'; }
1029:
1030: if($status eq 'true') {
1031: my $displayString = $count.'/'.$studentCount.': '.$_;
1032: &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
1033: }
1034:
1035: my $downloadTime='Not downloaded';
1036: if($checkDate eq 'true' &&
1037: tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
1038: $downloadTime = $cache{$_.':lastDownloadTime'};
1039: untie(%cache);
1040: }
1041:
1042: if($c->aborted()) { return 'Aborted'; }
1043:
1044: if($downloadTime eq 'Not downloaded') {
1045: my $courseData =
1046: &DownloadCourseInformation($_, $courseID, $downloadTime,
1047: $WhatIWant);
1048: if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
1049: foreach my $key (keys(%$courseData)) {
1050: if($key =~ /^(con_lost|error|no_such_host)/i) {
1051: $courseData->{$_.':error'} = 'No course data for '.$_;
1052: last;
1053: }
1054: }
1055: if($extract eq 'true') {
1056: &ExtractStudentData($courseData, \%cache, \%cache, $_);
1057: } else {
1058: &ProcessStudentData(\%cache, $courseData, $_);
1059: }
1060: untie(%cache);
1061: } else {
1062: next;
1063: }
1064: }
1065: $count++;
1066: }
1067: if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
1068:
1069: return 'OK';
1070: }
1071:
1072: sub DownloadStudentCourseDataSeparate {
1073: my ($students,$checkDate,$cacheDB,$extract,$status,$courseID,$r,$c)=@_;
1074: my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
1075: my $title = 'LON-CAPA Statistics';
1076: my $heading = 'Download Course Data';
1077:
1078: my $WhatIWant;
1079: $WhatIWant = '(^version:(\w|\/|\.|-)+?$|';
1080: $WhatIWant .= '^\d+:(\w|\/|\.|-)+?:(resource\.\d+\.';
1081: $WhatIWant .= '(solved|tries|previous|awarded|(\d+\.submission))\s*$';
1082: $WhatIWant .= '|timestamp)';
1083: $WhatIWant .= ')';
1084:
1085: &CheckForResidualDownload($courseID, $cacheDB, $students, $c);
1086:
1087: my %cache;
1088: my %downloadData;
1089: unless(tie(%downloadData,'GDBM_File',$residualFile,&GDBM_NEWDB(),0640)) {
1090: return 'Failed to tie temporary download hash.';
1091: }
1092:
1093: my $studentCount = scalar(@$students);
1094: if($status eq 'true') {
1095: &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
1096: }
1097: my $count=1;
1098: foreach (@$students) {
1099: if($c->aborted()) {
1100: untie(%downloadData);
1101: return 'Aborted';
1102: }
1103:
1104: if($status eq 'true') {
1105: my $displayString = $count.'/'.$studentCount.': '.$_;
1106: &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
1107: }
1108:
1109: my $downloadTime='Not downloaded';
1110: if($checkDate eq 'true' &&
1111: tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
1112: $downloadTime = $cache{$_.':lastDownloadTime'};
1113: untie(%cache);
1114: }
1115:
1116: if($c->aborted()) {
1117: untie(%downloadData);
1118: return 'Aborted';
1119: }
1120:
1121: if($downloadTime eq 'Not downloaded') {
1122: my $error = 0;
1123: my $courseData =
1124: &DownloadCourseInformation($_, $courseID, $downloadTime,
1125: $WhatIWant);
1126: foreach my $key (keys(%$courseData)) {
1127: $downloadData{$key} = $courseData->{$key};
1128: if($key =~ /^(con_lost|error|no_such_host)/i) {
1129: $error = 1;
1130: last;
1131: }
1132: }
1133: if($error) {
1134: foreach my $deleteKey (keys(%$courseData)) {
1135: delete $downloadData{$deleteKey};
1136: }
1137: $downloadData{$_.':error'} = 'No course data for '.$_;
1138: }
1139: }
1140: $count++;
1141: }
1142: if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
1143:
1144: return &CheckForResidualDownload($cacheDB, 'true', 'true',
1145: $courseID, $r, $c);
1146: }
1147:
1148: sub CheckForResidualDownload {
1149: my ($cacheDB,$extract,$status,$courseID,$r,$c)=@_;
1150:
1151: my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
1152: if(!-e $residualFile) {
1153: return;
1154: }
1155:
1156: my %downloadData;
1157: my %cache;
1158: unless(tie(%downloadData,'GDBM_File',$residualFile,&GDBM_READER(),0640) &&
1159: tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
1160: return;
1161: }
1162:
1163: my @dataKeys=keys(%downloadData);
1164: my @students=();
1165: my %checkStudent;
1166: foreach(@dataKeys) {
1167: my @temp = split(':', $_);
1168: my $student = $temp[0].':'.$temp[1];
1169: if(!defined($checkStudent{$student})) {
1170: $checkStudent{$student}++;
1171: push(@students, $student);
1172: }
1173: }
1174:
1175: my $heading = 'Process Course Data';
1176: my $title = 'LON-CAPA Statistics';
1177: my $studentCount = scalar(@students);
1178: if($status eq 'true') {
1179: &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
1180: }
1181:
1182: my $count=1;
1183: foreach my $name (@students) {
1184: last if($c->aborted());
1185:
1186: if($status eq 'true') {
1187: my $displayString = $count.'/'.$studentCount.': '.$_;
1188: &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
1189: }
1190:
1191: if($extract eq 'true') {
1192: &ExtractStudentData(\%downloadData, \%cache, \%cache, $name);
1193: } else {
1194: &ProcessStudentData(\%cache, \%downloadData, $name);
1195: }
1196: foreach (@dataKeys) {
1197: if(/^$name/) {
1198: delete $downloadData{$_};
1199: }
1200: }
1201: $count++;
1202: }
1203:
1204: if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
1205:
1206: untie(%cache);
1207: untie(%downloadData);
1208:
1209: if(!$c->aborted()) {
1210: my @files = ($residualFile);
1211: unlink(@files);
1212: }
1213:
1214: return 'OK';
1215: }
1216:
1217: sub GetFileTimestamp {
1218: my ($studentDomain,$studentName,$filename,$root)=@_;
1219: $studentDomain=~s/\W//g;
1220: $studentName=~s/\W//g;
1221: my $subdir=$studentName.'__';
1222: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
1223: my $proname="$studentDomain/$subdir/$studentName";
1224: $proname .= '/'.$filename;
1225: my @dir = &Apache::lonnet::dirlist($proname, $studentDomain, $studentName,
1226: $root);
1227: my $fileStat = $dir[0];
1228: my @stats = split('&', $fileStat);
1229: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1230: return $stats[9];
1231: } else {
1232: return -1;
1233: }
1234: }
1235:
1236: # ----- END HELPER FUNCTIONS --------------------------------------------
1237:
1238: 1;
1239: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>