Annotation of loncom/interface/loncoursedata.pm, revision 1.17
1.1 stredwic 1: # The LearningOnline Network with CAPA
2: # (Publication Handler
3: #
1.17 ! stredwic 4: # $Id: loncoursedata.pm,v 1.16 2002/08/14 16:18:55 stredwic Exp $
1.1 stredwic 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();
1.13 stredwic 54: use Apache::lonhtmlcommon;
1.1 stredwic 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:
1.3 stredwic 74: =item &DownloadClasslist()
1.1 stredwic 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:
1.3 stredwic 105: sub DownloadClasslist {
106: my ($courseID, $lastDownloadTime, $c)=@_;
1.1 stredwic 107: my ($courseDomain,$courseNumber)=split(/\_/,$courseID);
1.3 stredwic 108: my %classlist;
1.1 stredwic 109:
1.7 stredwic 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: }
1.3 stredwic 120:
121: %classlist=&Apache::lonnet::dump('classlist',$courseDomain, $courseNumber);
1.1 stredwic 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);
1.3 stredwic 150: $classlist{$name.':sections'}=\%section;
1.1 stredwic 151: }
152:
1.3 stredwic 153: $classlist{'UpToDate'} = 'false';
154: $classlist{'lastDownloadTime'}=time;
155:
1.1 stredwic 156: return \%classlist;
157: }
158:
159: =pod
160:
1.4 stredwic 161: =item &DownloadCourseInformation()
1.1 stredwic 162:
163: Dump of all the course information for a single student. There is no
1.3 stredwic 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.
1.1 stredwic 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:
1.4 stredwic 185: sub DownloadCourseInformation {
1.12 stredwic 186: my ($namedata,$courseID,$lastDownloadTime,$WhatIWant)=@_;
1.3 stredwic 187: my %courseData;
1.4 stredwic 188: my ($name,$domain) = split(/\:/,$namedata);
1.1 stredwic 189:
1.7 stredwic 190: my $modifiedTime = &GetFileTimestamp($domain, $name,
191: $courseID.'.db',
192: $Apache::lonnet::perlvar{'lonUsersDir'});
193:
1.13 stredwic 194: if($lastDownloadTime >= $modifiedTime && $modifiedTime >= 0) {
195: $courseData{$namedata.':lastDownloadTime'}=time;
196: $courseData{$namedata.':UpToDate'} = 'true';
1.7 stredwic 197: return \%courseData;
198: }
1.3 stredwic 199:
1.4 stredwic 200: # Download course data
1.12 stredwic 201: if(!defined($WhatIWant)) {
202: $WhatIWant = '.';
203: }
204: %courseData=&Apache::lonnet::dump($courseID, $domain, $name, $WhatIWant);
1.3 stredwic 205: $courseData{'UpToDate'} = 'false';
206: $courseData{'lastDownloadTime'}=time;
1.13 stredwic 207:
208: my %newData;
209: foreach (keys(%courseData)) {
210: $newData{$namedata.':'.$_} = $courseData{$_};
211: }
212:
213: return \%newData;
1.1 stredwic 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 {
1.11 stredwic 259: my ($cache,$c)=@_;
1.1 stredwic 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: }
1.10 stredwic 268: if(tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
1.1 stredwic 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:
1.11 stredwic 291: my %sequenceRecord;
1.1 stredwic 292: while(1) {
293: if($c->aborted()) {
294: last;
295: }
296: # HANDLE NEW SEQUENCE!
297: #if page || sequence
1.11 stredwic 298: if(defined($hash{'map_pc_'.$hash{'src_'.$currentResourceID}}) &&
299: !defined($sequenceRecord{$currentResourceID})) {
300: $sequenceRecord{$currentResourceID}++;
1.1 stredwic 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: }
1.12 stredwic 330: next;
1.1 stredwic 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)$/ &&
1.11 stredwic 339: $partA eq $currentSequence &&
340: !defined($sequenceRecord{$currentSequence.':'.
341: $currentResourceID})) {
342: $sequenceRecord{$currentSequence.':'.$currentResourceID}++;
1.1 stredwic 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:
1.2 stredwic 357: my $meta=$hash{'src_'.$currentResourceID};
358: # $cache->{$currentResourceID.':title'}=
359: # &Apache::lonnet::metdata($meta,'title');
360: $cache->{$currentResourceID.':title'}=
361: $hash{'title_'.$currentResourceID};
1.9 minaeibi 362: $cache->{$currentResourceID.':source'}=
363: $hash{'src_'.$currentResourceID};
1.2 stredwic 364:
1.1 stredwic 365: # Get Parts for problem
1.8 stredwic 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: }
1.1 stredwic 393: }
1.8 stredwic 394: if(/^optionresponse/ &&
395: $beenHere{'o:'.$partId.':'.$currentResourceID} == 0) {
396: $beenHere{'o:'.$partId.$currentResourceID}++;
397: if(defined($cache->{'OptionResponses'})) {
398: $cache->{'OptionResponses'}.= ':::'.
1.16 stredwic 399: $currentSequence.':'.$currentResourceID.':'.
400: $partId.':'.$responseId;
401: } else {
402: $cache->{'OptionResponses'}= $currentSequence.':'.
1.8 stredwic 403: $currentResourceID.':'.
404: $partId.':'.$responseId;
1.2 stredwic 405: }
406: }
407: }
1.8 stredwic 408: }
409: }
1.1 stredwic 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};
1.2 stredwic 421: $cache->{$currentSequence.':source'}=
422: $hash{'src_'.$currentResourceID};
1.1 stredwic 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: }
1.11 stredwic 458: }
1.1 stredwic 459:
460: # MOVE!!!
461: # move to next resource
462: unless(defined($hash{'to_'.$currentResourceID})) {
463: # big problem, need to handle. Next is probably wrong
1.11 stredwic 464: my $errorMessage = 'Big problem in ';
465: $errorMessage .= 'loncoursedata::ProcessTopLevelMap.';
466: $errorMessage .= ' bighash to_$currentResourceID not defined!';
467: &Apache::lonnet::logthis($errorMessage);
1.1 stredwic 468: last;
469: }
470: my @nextResources=();
471: foreach (split(/\,/,$hash{'to_'.$currentResourceID})) {
1.11 stredwic 472: if(!defined($sequenceRecord{$currentSequence.':'.
473: $hash{'goesto_'.$_}})) {
474: push(@nextResources, $hash{'goesto_'.$_});
475: }
1.1 stredwic 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:
1.3 stredwic 493: =item &ProcessClasslist()
1.1 stredwic 494:
1.3 stredwic 495: Taking the class list dumped from &DownloadClasslist(), all the
1.1 stredwic 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:
1.3 stredwic 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:
1.1 stredwic 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
1.3 stredwic 512: &DownloadClasslist(). The hash contains a list of students, a pointer
1.1 stredwic 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:
1.3 stredwic 532: sub ProcessClasslist {
533: my ($cache,$classlist,$courseID,$c)=@_;
1.1 stredwic 534: my @names=();
535:
1.3 stredwic 536: $cache->{'ClasslistTimeStamp'}=$classlist->{'lastDownloadTime'};
537: if($classlist->{'UpToDate'} eq 'true') {
538: return split(/:::/,$cache->{'NamesOfStudents'});;
539: }
540:
1.1 stredwic 541: foreach my $name (keys(%$classlist)) {
542: if($name =~ /\:section/ || $name =~ /\:studentInformation/ ||
1.3 stredwic 543: $name eq '' || $name eq 'UpToDate' || $name eq 'lastDownloadTime') {
1.1 stredwic 544: next;
545: }
546: if($c->aborted()) {
1.3 stredwic 547: return ();
1.1 stredwic 548: }
549: push(@names,$name);
1.3 stredwic 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;
1.10 stredwic 557: # Initialize timestamp for student
1.3 stredwic 558: if(!defined($cache->{$name.':lastDownloadTime'})) {
559: $cache->{$name.':lastDownloadTime'}='Not downloaded';
1.6 stredwic 560: $cache->{$name.':updateTime'}=' Not updated';
1.3 stredwic 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;
1.7 stredwic 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: }
1.1 stredwic 615: }
616:
1.3 stredwic 617: $cache->{'ClasslistTimestamp'}=time;
618: $cache->{'NamesOfStudents'}=join(':::',@names);
1.1 stredwic 619:
620: return @names;
621: }
622:
623: =pod
624:
625: =item &ProcessStudentData()
626:
627: Takes the course data downloaded for a student in
1.4 stredwic 628: &DownloadCourseInformation() and breaks it up into key value pairs
1.1 stredwic 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
1.4 stredwic 650: &DownloadCourseInformation().
1.1 stredwic 651:
652: =back
653:
654: =cut
655:
656: sub ProcessStudentData {
657: my ($cache,$courseData,$name)=@_;
658:
1.13 stredwic 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)) {
1.3 stredwic 674: return;
675: }
676:
1.13 stredwic 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;
1.14 stredwic 684: my $totalAwarded = 0;
1.13 stredwic 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'})) {
1.15 stredwic 696: $output->{$name.':'.$problemID.':'.$part.':tries'} = 0;
697: $output->{$name.':'.$problemID.':'.$part.':awarded'} = 0;
698: $output->{$name.':'.$problemID.':'.$part.':code'} = ' ';
1.13 stredwic 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'};
1.14 stredwic 799: $totalAwarded += $partData{$part.':awarded'};
1.13 stredwic 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: }
1.3 stredwic 808:
1.13 stredwic 809: if($partData{$part.':code'} ne 'x') {
810: $totalProblems++;
811: }
812: }
1.1 stredwic 813: }
1.13 stredwic 814:
815: $output->{$name.':'.$sequence.':problemsCorrect'} = $problemsCorrect;
816: $problemsSolved += $problemsCorrect;
817: $problemsCorrect=0;
1.3 stredwic 818: }
819:
1.13 stredwic 820: $output->{$name.':problemsSolved'} = $problemsSolved;
821: $output->{$name.':totalProblems'} = $totalProblems;
1.14 stredwic 822: $output->{$name.':totalAwarded'} = $totalAwarded;
1.1 stredwic 823:
824: return;
1.4 stredwic 825: }
826:
827: sub LoadDiscussion {
1.13 stredwic 828: my ($courseID)=@_;
1.5 minaeibi 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:
1.4 stredwic 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"};
1.5 minaeibi 843: $Discuss{"$name:$prb"}=$idx;
1.4 stredwic 844: }
845: }
846: }
1.5 minaeibi 847:
848: return \%Discuss;
1.1 stredwic 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:
1.13 stredwic 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:
1.1 stredwic 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) {
1.10 stredwic 989: $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_READER(),0640);
1.1 stredwic 990: } else {
1.10 stredwic 991: $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_NEWDB(),0640);
1.1 stredwic 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: }
1.2 stredwic 1007:
1.13 stredwic 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: }
1.17 ! stredwic 1026:
! 1027: my $displayString;
! 1028: my $count=0;
1.13 stredwic 1029: foreach (@$students) {
1030: if($c->aborted()) { return 'Aborted'; }
1031:
1032: if($status eq 'true') {
1.17 ! stredwic 1033: $count++;
1.13 stredwic 1034: my $displayString = $count.'/'.$studentCount.': '.$_;
1035: &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
1036: }
1037:
1038: my $downloadTime='Not downloaded';
1039: if($checkDate eq 'true' &&
1040: tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
1041: $downloadTime = $cache{$_.':lastDownloadTime'};
1042: untie(%cache);
1043: }
1044:
1045: if($c->aborted()) { return 'Aborted'; }
1046:
1047: if($downloadTime eq 'Not downloaded') {
1048: my $courseData =
1049: &DownloadCourseInformation($_, $courseID, $downloadTime,
1050: $WhatIWant);
1051: if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
1052: foreach my $key (keys(%$courseData)) {
1053: if($key =~ /^(con_lost|error|no_such_host)/i) {
1054: $courseData->{$_.':error'} = 'No course data for '.$_;
1055: last;
1056: }
1057: }
1058: if($extract eq 'true') {
1059: &ExtractStudentData($courseData, \%cache, \%cache, $_);
1060: } else {
1061: &ProcessStudentData(\%cache, $courseData, $_);
1062: }
1063: untie(%cache);
1064: } else {
1065: next;
1066: }
1067: }
1068: }
1069: if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
1070:
1071: return 'OK';
1072: }
1073:
1074: sub DownloadStudentCourseDataSeparate {
1075: my ($students,$checkDate,$cacheDB,$extract,$status,$courseID,$r,$c)=@_;
1076: my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
1077: my $title = 'LON-CAPA Statistics';
1078: my $heading = 'Download Course Data';
1079:
1080: my $WhatIWant;
1081: $WhatIWant = '(^version:(\w|\/|\.|-)+?$|';
1082: $WhatIWant .= '^\d+:(\w|\/|\.|-)+?:(resource\.\d+\.';
1083: $WhatIWant .= '(solved|tries|previous|awarded|(\d+\.submission))\s*$';
1084: $WhatIWant .= '|timestamp)';
1085: $WhatIWant .= ')';
1086:
1087: &CheckForResidualDownload($courseID, $cacheDB, $students, $c);
1088:
1089: my %cache;
1090: my %downloadData;
1091: unless(tie(%downloadData,'GDBM_File',$residualFile,&GDBM_NEWDB(),0640)) {
1092: return 'Failed to tie temporary download hash.';
1093: }
1094:
1095: my $studentCount = scalar(@$students);
1096: if($status eq 'true') {
1097: &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
1098: }
1.17 ! stredwic 1099: my $count=0;
! 1100: my $displayString='';
1.13 stredwic 1101: foreach (@$students) {
1102: if($c->aborted()) {
1103: untie(%downloadData);
1104: return 'Aborted';
1105: }
1106:
1107: if($status eq 'true') {
1.17 ! stredwic 1108: $count++;
! 1109: $displayString = $count.'/'.$studentCount.': '.$_;
1.13 stredwic 1110: &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
1111: }
1112:
1113: my $downloadTime='Not downloaded';
1114: if($checkDate eq 'true' &&
1115: tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
1116: $downloadTime = $cache{$_.':lastDownloadTime'};
1117: untie(%cache);
1118: }
1119:
1120: if($c->aborted()) {
1121: untie(%downloadData);
1122: return 'Aborted';
1123: }
1124:
1125: if($downloadTime eq 'Not downloaded') {
1126: my $error = 0;
1127: my $courseData =
1128: &DownloadCourseInformation($_, $courseID, $downloadTime,
1129: $WhatIWant);
1130: foreach my $key (keys(%$courseData)) {
1131: $downloadData{$key} = $courseData->{$key};
1132: if($key =~ /^(con_lost|error|no_such_host)/i) {
1133: $error = 1;
1134: last;
1135: }
1136: }
1137: if($error) {
1138: foreach my $deleteKey (keys(%$courseData)) {
1139: delete $downloadData{$deleteKey};
1140: }
1141: $downloadData{$_.':error'} = 'No course data for '.$_;
1142: }
1143: }
1144: }
1145: if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
1146:
1.17 ! stredwic 1147: untie(%downloadData);
1.13 stredwic 1148: return &CheckForResidualDownload($cacheDB, 'true', 'true',
1149: $courseID, $r, $c);
1150: }
1151:
1152: sub CheckForResidualDownload {
1153: my ($cacheDB,$extract,$status,$courseID,$r,$c)=@_;
1154:
1155: my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
1156: if(!-e $residualFile) {
1.17 ! stredwic 1157: return 'File does not exist';
1.13 stredwic 1158: }
1159:
1160: my %downloadData;
1161: my %cache;
1.17 ! stredwic 1162: unless(tie(%downloadData,'GDBM_File',$residualFile,&GDBM_READER(),0640)) {
! 1163: return 'Can not tie database for check for residual download: tempDB';
! 1164: }
! 1165: unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
! 1166: untie(%downloadData);
! 1167: return 'Can not tie database for check for residual download: cacheDB';
1.13 stredwic 1168: }
1169:
1170: my @dataKeys=keys(%downloadData);
1171: my @students=();
1172: my %checkStudent;
1173: foreach(@dataKeys) {
1174: my @temp = split(':', $_);
1175: my $student = $temp[0].':'.$temp[1];
1176: if(!defined($checkStudent{$student})) {
1177: $checkStudent{$student}++;
1178: push(@students, $student);
1179: }
1180: }
1181:
1182: my $heading = 'Process Course Data';
1183: my $title = 'LON-CAPA Statistics';
1184: my $studentCount = scalar(@students);
1185: if($status eq 'true') {
1186: &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
1187: }
1188:
1189: my $count=1;
1190: foreach my $name (@students) {
1191: last if($c->aborted());
1192:
1193: if($status eq 'true') {
1194: my $displayString = $count.'/'.$studentCount.': '.$_;
1195: &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
1196: }
1197:
1198: if($extract eq 'true') {
1199: &ExtractStudentData(\%downloadData, \%cache, \%cache, $name);
1200: } else {
1201: &ProcessStudentData(\%cache, \%downloadData, $name);
1202: }
1203: foreach (@dataKeys) {
1204: if(/^$name/) {
1205: delete $downloadData{$_};
1206: }
1207: }
1208: $count++;
1209: }
1210:
1211: if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
1212:
1213: untie(%cache);
1214: untie(%downloadData);
1215:
1216: if(!$c->aborted()) {
1217: my @files = ($residualFile);
1218: unlink(@files);
1219: }
1220:
1221: return 'OK';
1222: }
1223:
1.3 stredwic 1224: sub GetFileTimestamp {
1225: my ($studentDomain,$studentName,$filename,$root)=@_;
1226: $studentDomain=~s/\W//g;
1227: $studentName=~s/\W//g;
1228: my $subdir=$studentName.'__';
1229: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
1230: my $proname="$studentDomain/$subdir/$studentName";
1231: $proname .= '/'.$filename;
1232: my @dir = &Apache::lonnet::dirlist($proname, $studentDomain, $studentName,
1233: $root);
1234: my $fileStat = $dir[0];
1235: my @stats = split('&', $fileStat);
1.13 stredwic 1236: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.3 stredwic 1237: return $stats[9];
1238: } else {
1239: return -1;
1240: }
1241: }
1.1 stredwic 1242:
1243: # ----- END HELPER FUNCTIONS --------------------------------------------
1244:
1245: 1;
1246: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>