Annotation of loncom/interface/statistics/lonstathelpers.pm, revision 1.20
1.1 matthew 1: # The LearningOnline Network with CAPA
2: #
1.20 ! matthew 3: # $Id: lonstathelpers.pm,v 1.19 2004/08/12 12:54:12 matthew Exp $
1.1 matthew 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:
30: =pod
31:
32: =head1 NAME
33:
34: Apache::lonstathelpers - helper routines used by statistics
35:
36: =head1 SYNOPSIS
37:
38: This module provides a place to consolidate much of the statistics
39: routines that are needed across multiple statistics functions.
40:
41: =head1 OVERVIEW
42:
43:
44: =over 4
45:
46: =cut
47:
48: ####################################################
49: ####################################################
50: package Apache::lonstathelpers;
51:
52: use strict;
53: use Apache::lonnet();
54: use Apache::loncommon();
55: use Apache::lonhtmlcommon();
56: use Apache::loncoursedata();
57: use Apache::lonstatistics;
58: use Apache::lonlocal;
59: use HTML::Entities();
60: use Time::Local();
61: use Spreadsheet::WriteExcel();
1.8 matthew 62: use GDBM_File;
63: use Storable qw(freeze thaw);
1.1 matthew 64:
65: ####################################################
66: ####################################################
67:
68: =pod
69:
70: =item &render_resource($resource)
71:
72: Input: a resource generated from
73: &Apache::loncoursedata::get_sequence_assessment_data().
74:
75: Retunrs: a scalar containing html for a rendering of the problem
76: within a table.
77:
78: =cut
79:
80: ####################################################
81: ####################################################
82: sub render_resource {
83: my ($resource) = @_;
84: ##
85: ## Render the problem
86: my $base;
87: ($base,undef) = ($resource->{'src'} =~ m|(.*/)[^/]*$|);
88: $base = "http://".$ENV{'SERVER_NAME'}.$base;
89: my $rendered_problem =
90: &Apache::lonnet::ssi_body($resource->{'src'});
91: $rendered_problem =~ s/<\s*form\s*/<nop /g;
92: $rendered_problem =~ s|(<\s*/form\s*>)|<\/nop>|g;
93: return '<table bgcolor="ffffff"><tr><td>'.
94: '<base href="'.$base.'" />'.
95: $rendered_problem.
96: '</td></tr></table>';
97: }
1.2 matthew 98:
99: ####################################################
100: ####################################################
101:
102: =pod
103:
104: =item &ProblemSelector($AcceptedResponseTypes)
105:
106: Input: scalar containing regular expression which matches response
107: types to show. '.' will yield all, '(option|radiobutton)' will match
108: all option response and radiobutton problems.
109:
110: Returns: A string containing html for a table which lists the sequences
111: and their contents. A radiobutton is provided for each problem.
1.14 matthew 112: Skips 'survey' problems.
1.2 matthew 113:
114: =cut
115:
116: ####################################################
117: ####################################################
118: sub ProblemSelector {
119: my ($AcceptedResponseTypes) = @_;
120: my $Str;
121: $Str = "\n<table>\n";
1.12 matthew 122: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess('all')) {
1.2 matthew 123: next if ($seq->{'num_assess'}<1);
124: my $seq_str = '';
125: foreach my $res (@{$seq->{'contents'}}) {
126: next if ($res->{'type'} ne 'assessment');
127: foreach my $part (@{$res->{'parts'}}) {
128: my $partdata = $res->{'partdata'}->{$part};
129: for (my $i=0;$i<scalar(@{$partdata->{'ResponseTypes'}});$i++){
130: my $respid = $partdata->{'ResponseIds'}->[$i];
131: my $resptype = $partdata->{'ResponseTypes'}->[$i];
132: if ($resptype =~ m/$AcceptedResponseTypes/) {
133: my $value = &make_target_id({symb=>$res->{'symb'},
134: part=>$part,
135: respid=>$respid,
136: resptype=>$resptype});
137: my $checked = '';
138: if ($ENV{'form.problemchoice'} eq $value) {
139: $checked = 'checked ';
140: }
141: my $title = $res->{'title'};
142: if (! defined($title) || $title eq '') {
143: ($title) = ($res->{'src'} =~ m:/([^/]*)$:);
144: }
145: $seq_str .= '<tr><td>'.
146: '<input type="radio" name="problemchoice" value="'.$value.'" '.$checked.'/>'.
147: '</td><td>'.
148: $resptype.'</td><td>'.
149: '<a href="'.$res->{'src'}.'">'.$title.'</a> ';
150: # '<a href="'.$res->{'src'}.'">'.$resptype.' '.$res->{'title'}.'</a> ';
1.5 matthew 151: if (scalar(@{$partdata->{'ResponseIds'}}) > 1) {
1.2 matthew 152: $seq_str .= &mt('response').' '.$respid;
153: }
154: $seq_str .= "</td></tr>\n";
155: }
156: }
157: }
158: }
159: if ($seq_str ne '') {
160: $Str .= '<tr><td> </td><td colspan="2"><b>'.$seq->{'title'}.'</b></td>'.
161: "</tr>\n".$seq_str;
162: }
163: }
164: $Str .= "</table>\n";
165: return $Str;
166: }
167:
168: ####################################################
169: ####################################################
170:
171: =pod
172:
173: =item &make_target_id($target)
174:
175: Inputs: Hash ref with the following entries:
176: $target->{'symb'}, $target->{'part'}, $target->{'respid'},
177: $target->{'resptype'}.
178:
179: Returns: A string, suitable for a form parameter, which uniquely identifies
180: the problem, part, and response to do statistical analysis on.
181:
182: Used by Apache::lonstathelpers::ProblemSelector().
183:
184: =cut
185:
186: ####################################################
187: ####################################################
188: sub make_target_id {
189: my ($target) = @_;
190: my $id = &Apache::lonnet::escape($target->{'symb'}).':'.
191: &Apache::lonnet::escape($target->{'part'}).':'.
192: &Apache::lonnet::escape($target->{'respid'}).':'.
193: &Apache::lonnet::escape($target->{'resptype'});
194: return $id;
195: }
196:
197: ####################################################
198: ####################################################
199:
200: =pod
201:
202: =item &get_target_from_id($id)
203:
204: Inputs: $id, a scalar string from Apache::lonstathelpers::make_target_id().
205:
206: Returns: A hash reference, $target, containing the following keys:
207: $target->{'symb'}, $target->{'part'}, $target->{'respid'},
208: $target->{'resptype'}.
209:
210: =cut
211:
212: ####################################################
213: ####################################################
214: sub get_target_from_id {
215: my ($id) = @_;
216: my ($symb,$part,$respid,$resptype) = split(':',$id);
217: return ({ symb =>&Apache::lonnet::unescape($symb),
218: part =>&Apache::lonnet::unescape($part),
219: respid =>&Apache::lonnet::unescape($respid),
220: resptype =>&Apache::lonnet::unescape($resptype)});
221: }
222:
223: ####################################################
224: ####################################################
225:
226: =pod
227:
1.13 matthew 228: =item &get_prev_curr_next($target,$AcceptableResponseTypes,$granularity)
1.2 matthew 229:
230: Determine the problem parts or responses preceeding and following the
231: current resource.
232:
233: Inputs: $target (see &Apache::lonstathelpers::get_target_from_id())
234: $AcceptableResponseTypes, regular expression matching acceptable
235: response types,
1.13 matthew 236: $granularity, either 'part', 'response', or 'part_survey'
1.2 matthew 237:
238: Returns: three hash references, $prev, $curr, $next, which refer to the
239: preceeding, current, or following problem parts or responses, depending
240: on the value of $granularity. Values of undef indicate there is no
241: previous or next part/response. A value of undef for all three indicates
242: there was no match found to the current part/resource.
243:
244: The hash references contain the following keys:
245: symb, part, resource
246:
247: If $granularity eq 'response', the following ADDITIONAL keys will be present:
248: respid, resptype
249:
250: =cut
251:
252: ####################################################
253: ####################################################
254: sub get_prev_curr_next {
255: my ($target,$AcceptableResponseTypes,$granularity) = @_;
256: #
257: # Build an array with the data we need to search through
258: my @Resource;
1.12 matthew 259: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess('all')) {
1.2 matthew 260: foreach my $res (@{$seq->{'contents'}}) {
261: next if ($res->{'type'} ne 'assessment');
262: foreach my $part (@{$res->{'parts'}}) {
263: my $partdata = $res->{'partdata'}->{$part};
1.20 ! matthew 264: if ($partdata->{'Survey'} && ($granularity eq 'part_survey')){
! 265: push (@Resource,
! 266: { symb => $res->{symb},
! 267: part => $part,
! 268: resource => $res,
! 269: } );
1.13 matthew 270: } elsif ($granularity eq 'part') {
1.2 matthew 271: push (@Resource,
272: { symb => $res->{symb},
273: part => $part,
274: resource => $res,
275: } );
276: } elsif ($granularity eq 'response') {
277: for (my $i=0;
278: $i<scalar(@{$partdata->{'ResponseTypes'}});
279: $i++){
280: my $respid = $partdata->{'ResponseIds'}->[$i];
281: my $resptype = $partdata->{'ResponseTypes'}->[$i];
282: next if ($resptype !~ m/$AcceptableResponseTypes/);
283: push (@Resource,
284: { symb => $res->{symb},
285: part => $part,
286: respid => $partdata->{'ResponseIds'}->[$i],
287: resource => $res,
288: resptype => $resptype
289: } );
290: }
291: }
292: }
293: }
294: }
295: #
296: # Get the index of the current situation
297: my $curr_idx;
298: for ($curr_idx=0;$curr_idx<$#Resource;$curr_idx++) {
299: my $curr_item = $Resource[$curr_idx];
1.13 matthew 300: if ($granularity eq 'part' || $granularity eq 'part_survey') {
1.2 matthew 301: if ($curr_item->{'symb'} eq $target->{'symb'} &&
302: $curr_item->{'part'} eq $target->{'part'}) {
303: last;
304: }
305: } elsif ($granularity eq 'response') {
306: if ($curr_item->{'symb'} eq $target->{'symb'} &&
307: $curr_item->{'part'} eq $target->{'part'} &&
308: $curr_item->{'respid'} eq $target->{'respid'} &&
309: $curr_item->{'resptype'} eq $target->{'resptype'}) {
310: last;
311: }
312: }
313: }
314: my $curr_item = $Resource[$curr_idx];
1.13 matthew 315: if ($granularity eq 'part' || $granularity eq 'part_survey') {
1.2 matthew 316: if ($curr_item->{'symb'} ne $target->{'symb'} ||
317: $curr_item->{'part'} ne $target->{'part'}) {
318: # bogus symb - return nothing
319: return (undef,undef,undef);
320: }
321: } elsif ($granularity eq 'response') {
322: if ($curr_item->{'symb'} ne $target->{'symb'} ||
323: $curr_item->{'part'} ne $target->{'part'} ||
324: $curr_item->{'respid'} ne $target->{'respid'} ||
325: $curr_item->{'resptype'} ne $target->{'resptype'}){
326: # bogus symb - return nothing
327: return (undef,undef,undef);
328: }
329: }
330: #
331: # Now just pick up the data we need
332: my ($prev,$curr,$next);
333: if ($curr_idx == 0) {
334: $prev = undef;
335: $curr = $Resource[$curr_idx ];
336: $next = $Resource[$curr_idx+1];
337: } elsif ($curr_idx == $#Resource) {
338: $prev = $Resource[$curr_idx-1];
339: $curr = $Resource[$curr_idx ];
340: $next = undef;
341: } else {
342: $prev = $Resource[$curr_idx-1];
343: $curr = $Resource[$curr_idx ];
344: $next = $Resource[$curr_idx+1];
345: }
346: return ($prev,$curr,$next);
1.4 matthew 347: }
348:
1.9 matthew 349:
350: #####################################################
351: #####################################################
352:
353: =pod
354:
355: =item GetStudentAnswers($r,$problem,$Students)
356:
357: Determines the correct answer for a set of students on a given problem.
358: The students answers are stored in the student hashes pointed to by the
359: array @$Students under the key 'answer'.
360:
361: Inputs: $r
362: $problem: hash reference containing the keys 'resource', 'part', and 'respid'.
363: $Students: reference to array containing student hashes (need 'username',
364: 'domain').
365:
366: Returns: nothing
367:
368: =cut
369:
370: #####################################################
371: #####################################################
372: sub GetStudentAnswers {
1.12 matthew 373: my ($r,$problem,$Students,$formname,$inputname) = @_;
374: my $status_type;
375: if (defined($formname)) {
376: $status_type = 'inline';
377: } else {
378: $status_type = 'popup';
379: }
1.9 matthew 380: my $c = $r->connection();
381: my %Answers;
382: my ($resource,$partid,$respid) = ($problem->{'resource'},
383: $problem->{'part'},
384: $problem->{'respid'});
385: # Read in the cache (if it exists) before we start timing things.
386: &Apache::lonstathelpers::ensure_proper_cache($resource->{'symb'});
387: # Open progress window
388: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
389: ($r,'Student Answer Compilation Status',
1.12 matthew 390: 'Student Answer Compilation Progress', scalar(@$Students),
391: $status_type,undef,$formname,$inputname);
1.9 matthew 392: $r->rflush();
393: foreach my $student (@$Students) {
394: last if ($c->aborted());
395: my $sname = $student->{'username'};
396: my $sdom = $student->{'domain'};
397: my $answer = &Apache::lonstathelpers::analyze_problem_as_student
398: ($resource,$sname,$sdom,$partid,$respid);
399: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
400: &mt('last student'));
401: $student->{'answer'} = $answer;
402: }
403: &Apache::lonstathelpers::write_answer_cache();
404: return if ($c->aborted());
405: $r->rflush();
406: # close progress window
407: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
408: return;
409: }
1.4 matthew 410:
411: #####################################################
412: #####################################################
413:
414: =pod
415:
416: =item analyze_problem_as_student
417:
418: Analyzes a homework problem for a student and returns the correct answer
419: for the student. Attempts to put together an answer for problem types
420: that do not natively support it.
421:
422: Inputs: $resource: a resource object
423: $sname, $sdom, $partid, $respid
424:
425: Returns: $answer
426:
1.6 matthew 427: If $partid and $respid are specified, $answer is simply a scalar containing
428: the correct answer for the response.
429:
430: If $partid or $respid are undefined, $answer will be a hash reference with
431: keys $partid.'.'.$respid.'.answer'.
432:
1.4 matthew 433: =cut
434:
435: #####################################################
436: #####################################################
437: sub analyze_problem_as_student {
438: my ($resource,$sname,$sdom,$partid,$respid) = @_;
439: my $returnvalue;
440: my $url = $resource->{'src'};
441: my $symb = $resource->{'symb'};
1.8 matthew 442: my $answer = &get_from_answer_cache($sname,$sdom,$symb,$partid,$respid);
443: if (defined($answer)) {
444: return($answer);
445: }
1.4 matthew 446: my $courseid = $ENV{'request.course.id'};
447: my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze',
448: 'grade_domain' => $sdom,
449: 'grade_username' => $sname,
450: 'grade_symb' => $symb,
451: 'grade_courseid' => $courseid));
452: (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
453: my %Answer=&Apache::lonnet::str2hash($Answ);
1.6 matthew 454: #
1.8 matthew 455: undef($answer);
456: foreach my $partid (@{$resource->{'parts'}}) {
1.6 matthew 457: my $partdata = $resource->{'partdata'}->{$partid};
458: foreach my $respid (@{$partdata->{'ResponseIds'}}) {
459: my $prefix = $partid.'.'.$respid;
460: my $key = $prefix.'.answer';
1.8 matthew 461: $answer->{$partid}->{$respid} = &get_answer($prefix,$key,%Answer);
1.6 matthew 462: }
1.8 matthew 463: }
464: &store_answer($sname,$sdom,$symb,undef,undef,$answer);
465: if (! defined($partid)) {
466: $returnvalue = $answer;
467: } elsif (! defined($respid)) {
468: $returnvalue = $answer->{$partid};
1.6 matthew 469: } else {
1.8 matthew 470: $returnvalue = $answer->{$partid}->{$respid};
1.6 matthew 471: }
472: return $returnvalue;
473: }
474:
475: sub get_answer {
476: my ($prefix,$key,%Answer) = @_;
477: my $returnvalue;
1.4 matthew 478: if (exists($Answer{$key})) {
479: my $student_answer = $Answer{$key}->[0];
480: if (! defined($student_answer)) {
481: $student_answer = $Answer{$key}->[1];
482: }
483: $returnvalue = $student_answer;
484: } else {
485: if (exists($Answer{$prefix.'.shown'})) {
486: # The response has foils
487: my %values;
488: while (my ($k,$v) = each(%Answer)) {
489: next if ($k !~ /^$prefix\.foil\.(value|area)\.(.*)$/);
490: my $foilname = $2;
491: $values{$foilname}=$v;
492: }
493: foreach my $foil (@{$Answer{$prefix.'.shown'}}) {
494: if (ref($values{$foil}) eq 'ARRAY') {
1.10 albertel 495: $returnvalue.=&HTML::Entities::encode($foil,'<>&"').'='.
496: join(',',map {&HTML::Entities::encode($_,'<>&"')} @{$values{$foil}}).'&';
1.4 matthew 497: } else {
1.10 albertel 498: $returnvalue.=&HTML::Entities::encode($foil,'<>&"').'='.
499: &HTML::Entities::encode($values{$foil},'<>&"').'&';
1.4 matthew 500: }
501: }
502: $returnvalue =~ s/ /\%20/g;
503: chop ($returnvalue);
504: }
505: }
506: return $returnvalue;
507: }
1.8 matthew 508:
509:
510: #####################################################
511: #####################################################
512:
513: =pod
514:
515: =item Caching routines
516:
517: =over 4
518:
519: =item &load_answer_cache($symb)
520:
521: Loads the cache for the given symb into memory from disk.
522: Requires the cache filename be set.
523: Only should be called by &ensure_proper_cache.
524:
525: =cut
526:
527: #####################################################
528: #####################################################
529: {
530: my $cache_filename = undef;
531: my $current_symb = undef;
532: my %cache;
533:
534: sub load_answer_cache {
535: my ($symb) = @_;
536: return if (! defined($cache_filename));
537: if (! defined($current_symb) || $current_symb ne $symb) {
538: undef(%cache);
539: my $storedstring;
540: my %cache_db;
541: if (tie(%cache_db,'GDBM_File',$cache_filename,&GDBM_READER(),0640)) {
542: $storedstring = $cache_db{&Apache::lonnet::escape($symb)};
543: untie(%cache_db);
544: }
545: if (defined($storedstring)) {
546: %cache = %{thaw($storedstring)};
547: }
548: }
549: return;
550: }
551:
552: #####################################################
553: #####################################################
554:
555: =pod
556:
557: =item &get_from_answer_cache($sname,$sdom,$symb,$partid,$respid)
558:
559: Returns the appropriate data from the cache, or undef if no data exists.
560: If $respid is undefined, a hash ref containing the answers for the given
561: $partid is returned. If $partid is undefined, a hash ref containing answers
562: for all of the parts is returned.
563:
564: =cut
565:
566: #####################################################
567: #####################################################
568: sub get_from_answer_cache {
569: my ($sname,$sdom,$symb,$partid,$respid) = @_;
570: &ensure_proper_cache($symb);
571: my $returnvalue;
572: if (exists($cache{$sname.':'.$sdom}) &&
573: ref($cache{$sname.':'.$sdom}) eq 'HASH') {
574: if (defined($partid) &&
575: exists($cache{$sname.':'.$sdom}->{$partid})) {
576: if (defined($respid) &&
577: exists($cache{$sname.':'.$sdom}->{$partid}->{$respid})) {
578: $returnvalue = $cache{$sname.':'.$sdom}->{$partid}->{$respid};
579: } else {
580: $returnvalue = $cache{$sname.':'.$sdom}->{$partid};
581: }
582: } else {
583: $returnvalue = $cache{$sname.':'.$sdom};
584: }
585: } else {
586: $returnvalue = undef;
587: }
588: return $returnvalue;
589: }
590:
591: #####################################################
592: #####################################################
593:
594: =pod
595:
596: =item &write_answer_cache($symb)
597:
598: Writes the in memory cache to disk so that it can be read in with
599: &load_answer_cache($symb).
600:
601: =cut
602:
603: #####################################################
604: #####################################################
605: sub write_answer_cache {
606: return if (! defined($current_symb) || ! defined($cache_filename));
607: my %cache_db;
608: my $key = &Apache::lonnet::escape($current_symb);
609: if (tie(%cache_db,'GDBM_File',$cache_filename,&GDBM_WRCREAT(),0640)) {
610: my $storestring = freeze(\%cache);
611: $cache_db{$key}=$storestring;
612: $cache_db{$key.'.time'}=time;
613: untie(%cache_db);
614: }
615: undef(%cache);
616: undef($current_symb);
617: undef($cache_filename);
618: return;
619: }
620:
621: #####################################################
622: #####################################################
623:
624: =pod
625:
626: =item &ensure_proper_cache($symb)
627:
628: Called to make sure we have the proper cache set up. This is called
629: prior to every answer lookup.
630:
631: =cut
632:
633: #####################################################
634: #####################################################
635: sub ensure_proper_cache {
636: my ($symb) = @_;
637: my $cid = $ENV{'request.course.id'};
638: my $new_filename = '/home/httpd/perl/tmp/'.
1.18 matthew 639: 'problemanalysis_'.$cid.'_answer_cache.db';
1.8 matthew 640: if (! defined($cache_filename) ||
641: $cache_filename ne $new_filename ||
642: ! defined($current_symb) ||
643: $current_symb ne $symb) {
644: $cache_filename = $new_filename;
645: # Notice: $current_symb is not set to $symb until after the cache is
646: # loaded. This is what tells &load_answer_cache to load in a new
647: # symb cache.
648: &load_answer_cache($symb);
649: $current_symb = $symb;
650: }
651: }
652:
653: #####################################################
654: #####################################################
655:
656: =pod
657:
658: =item &store_answer($sname,$sdom,$symb,$partid,$respid,$dataset)
659:
660: Stores the answer data in the in memory cache.
661:
662: =cut
663:
664: #####################################################
665: #####################################################
666: sub store_answer {
667: my ($sname,$sdom,$symb,$partid,$respid,$dataset) = @_;
668: return if ($symb ne $current_symb);
669: if (defined($partid)) {
670: if (defined($respid)) {
671: $cache{$sname.':'.$sdom}->{$partid}->{$respid} = $dataset;
672: } else {
673: $cache{$sname.':'.$sdom}->{$partid} = $dataset;
674: }
675: } else {
676: $cache{$sname.':'.$sdom}=$dataset;
677: }
678: return;
679: }
680:
681: }
682: #####################################################
683: #####################################################
684:
685: =pod
686:
687: =back
688:
689: =cut
690:
691: #####################################################
692: #####################################################
1.4 matthew 693:
694: ##
695: ## The following is copied from datecalc1.pl, part of the
696: ## Spreadsheet::WriteExcel CPAN module.
697: ##
698: ##
699: ######################################################################
700: #
701: # Demonstration of writing date/time cells to Excel spreadsheets,
702: # using UNIX/Perl time as source of date/time.
703: #
704: # Copyright 2000, Andrew Benham, adsb@bigfoot.com
705: #
706: ######################################################################
707: #
708: # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
709: # measured in seconds.
710: #
711: # An Excel file can use exactly one of two different date/time systems.
712: # In these systems, a floating point number represents the number of days
713: # (and fractional parts of the day) since a start point. The floating point
714: # number is referred to as a 'serial'.
715: # The two systems ('1900' and '1904') use different starting points:
716: # '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
717: # a leap year - see:
718: # http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
719: # for the excuse^H^H^H^H^H^Hreason.
720: # '1904'; '1.00' is 2 Jan 1904.
721: #
722: # The '1904' system is the default for Apple Macs. Windows versions of
723: # Excel have the option to use the '1904' system.
724: #
725: # Note that Visual Basic's "DateSerial" function does NOT erroneously
726: # regard 1900 as a leap year, and thus its serials do not agree with
727: # the 1900 serials of Excel for dates before 1 Mar 1900.
728: #
729: # Note that StarOffice (at least at version 5.2) does NOT erroneously
730: # regard 1900 as a leap year, and thus its serials do not agree with
731: # the 1900 serials of Excel for dates before 1 Mar 1900.
732: #
733: ######################################################################
734: #
735: # Calculation description
736: # =======================
737: #
738: # 1900 system
739: # -----------
740: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900.
741: # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
742: # were leap years with an extra day.
743: # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and
744: # 1 Jan 1970.
745: # In the 1900 system, '1' is 1 Jan 1900, but as 1900 was not a leap year
746: # 1 Jan 1900 should really be '2', so 1 Jan 1970 is '25569'.
747: #
748: # 1904 system
749: # -----------
750: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904.
751: # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
752: # were leap years with an extra day.
753: # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and
754: # 1 Jan 1970.
755: # In the 1904 system, 2 Jan 1904 being '1', 1 Jan 1970 is '24107'.
756: #
757: ######################################################################
758: #
759: # Copyright (c) 2000, Andrew Benham.
760: # This program is free software. It may be used, redistributed and/or
761: # modified under the same terms as Perl itself.
762: #
763: # Andrew Benham, adsb@bigfoot.com
764: # London, United Kingdom
765: # 11 Nov 2000
766: #
767: ######################################################################
768: #-----------------------------------------------------------
769: # calc_serial()
770: #
771: # Called with (up to) 2 parameters.
772: # 1. Unix timestamp. If omitted, uses current time.
773: # 2. GMT flag. Set to '1' to return serial in GMT.
774: # If omitted, returns serial in appropriate timezone.
775: #
776: # Returns date/time serial according to $DATE_SYSTEM selected
777: #-----------------------------------------------------------
778: sub calc_serial {
779: # Use 1900 date system on all platforms other than Apple Mac (for which
780: # use 1904 date system).
781: my $DATE_SYSTEM = ($^O eq 'MacOS') ? 1 : 0;
782: my $time = (defined $_[0]) ? $_[0] : time();
783: my $gmtflag = (defined $_[1]) ? $_[1] : 0;
784: #
785: # Divide timestamp by number of seconds in a day.
786: # This gives a date serial with '0' on 1 Jan 1970.
787: my $serial = $time / 86400;
788: #
789: # Adjust the date serial by the offset appropriate to the
790: # currently selected system (1900/1904).
791: if ($DATE_SYSTEM == 0) { # use 1900 system
792: $serial += 25569;
793: } else { # use 1904 system
794: $serial += 24107;
795: }
796: #
797: unless ($gmtflag) {
798: # Now have a 'raw' serial with the right offset. But this
799: # gives a serial in GMT, which is false unless the timezone
800: # is GMT. We need to adjust the serial by the appropriate
801: # timezone offset.
802: # Calculate the appropriate timezone offset by seeing what
803: # the differences between localtime and gmtime for the given
804: # time are.
805: #
806: my @gmtime = gmtime($time);
807: my @ltime = localtime($time);
808: #
809: # For the first 7 elements of the two arrays, adjust the
810: # date serial where the elements differ.
811: for (0 .. 6) {
812: my $diff = $ltime[$_] - $gmtime[$_];
813: if ($diff) {
814: $serial += _adjustment($diff,$_);
815: }
816: }
817: }
818: #
819: # Perpetuate the error that 1900 was a leap year by decrementing
820: # the serial if we're using the 1900 system and the date is prior to
821: # 1 Mar 1900. This has the effect of making serial value '60'
822: # 29 Feb 1900.
823: #
824: # This fix only has any effect if UNIX/Perl time on the platform
825: # can represent 1900. Many can't.
826: #
827: unless ($DATE_SYSTEM) {
828: $serial-- if ($serial < 61); # '61' is 1 Mar 1900
829: }
830: return $serial;
831: }
832:
833: sub _adjustment {
834: # Based on the difference in the localtime/gmtime array elements
835: # number, return the adjustment required to the serial.
836: #
837: # We only look at some elements of the localtime/gmtime arrays:
838: # seconds unlikely to be different as all known timezones
839: # have an offset of integral multiples of 15 minutes,
840: # but it's easy to do.
841: # minutes will be different for timezone offsets which are
842: # not an exact number of hours.
843: # hours very likely to be different.
844: # weekday will differ when localtime/gmtime difference
845: # straddles midnight.
846: #
847: # Assume that difference between localtime and gmtime is less than
848: # 5 days, then don't have to do maths for day of month, month number,
849: # year number, etc...
850: #
851: my ($delta,$element) = @_;
852: my $adjust = 0;
853: #
854: if ($element == 0) { # Seconds
855: $adjust = $delta/86400; # 60 * 60 * 24
856: } elsif ($element == 1) { # Minutes
857: $adjust = $delta/1440; # 60 * 24
858: } elsif ($element == 2) { # Hours
859: $adjust = $delta/24; # 24
860: } elsif ($element == 6) { # Day of week number
861: # Catch difference straddling Sat/Sun in either direction
862: $delta += 7 if ($delta < -4);
863: $delta -= 7 if ($delta > 4);
864: #
865: $adjust = $delta;
866: }
867: return $adjust;
868: }
869:
870: ###########################################################
871: ###########################################################
872:
873: =pod
874:
875: =item get_problem_data
876:
877: Returns a data structure describing the problem.
878:
879: Inputs: $url
880:
881: Returns: %Partdata
882:
883: =cut
884:
885: ## note: we must force each foil and option to not begin or end with
886: ## spaces as they are stored without such data.
887: ##
888: ###########################################################
889: ###########################################################
890: sub get_problem_data {
891: my ($url) = @_;
892: my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
893: (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
894: my %Answer;
895: %Answer=&Apache::lonnet::str2hash($Answ);
896: my %Partdata;
897: foreach my $part (@{$Answer{'parts'}}) {
898: while (my($key,$value) = each(%Answer)) {
899: #
900: # Logging code:
1.7 matthew 901: if (0) {
1.4 matthew 902: &Apache::lonnet::logthis($part.' got key "'.$key.'"');
903: if (ref($value) eq 'ARRAY') {
904: &Apache::lonnet::logthis(' @'.join(',',@$value));
905: } else {
906: &Apache::lonnet::logthis(' '.$value);
907: }
908: }
909: # End of logging code
910: next if ($key !~ /^$part/);
911: $key =~ s/^$part\.//;
912: if (ref($value) eq 'ARRAY') {
913: if ($key eq 'options') {
914: $Partdata{$part}->{'_Options'}=$value;
915: } elsif ($key eq 'concepts') {
916: $Partdata{$part}->{'_Concepts'}=$value;
917: } elsif ($key =~ /^concept\.(.*)$/) {
918: my $concept = $1;
919: foreach my $foil (@$value) {
920: $Partdata{$part}->{'_Foils'}->{$foil}->{'_Concept'}=
921: $concept;
922: }
923: } elsif ($key =~ /^(incorrect|answer|ans_low|ans_high)$/) {
924: $Partdata{$part}->{$key}=$value;
925: }
926: } else {
927: if ($key=~ /^foil\.text\.(.*)$/) {
928: my $foil = $1;
929: $Partdata{$part}->{'_Foils'}->{$foil}->{'name'}=$foil;
930: $value =~ s/(\s*$|^\s*)//g;
931: $Partdata{$part}->{'_Foils'}->{$foil}->{'text'}=$value;
932: } elsif ($key =~ /^foil\.value\.(.*)$/) {
933: my $foil = $1;
934: $Partdata{$part}->{'_Foils'}->{$foil}->{'value'}=$value;
935: }
936: }
937: }
938: }
939: return %Partdata;
1.5 matthew 940: }
941:
942: ####################################################
943: ####################################################
944:
945: =pod
946:
947: =item &limit_by_time()
948:
949: =cut
950:
951: ####################################################
952: ####################################################
953: sub limit_by_time_form {
954: my $Starttime_form = '';
955: my $starttime = &Apache::lonhtmlcommon::get_date_from_form
956: ('limitby_startdate');
957: my $endtime = &Apache::lonhtmlcommon::get_date_from_form
958: ('limitby_enddate');
959: if (! defined($endtime)) {
960: $endtime = time;
961: }
962: if (! defined($starttime)) {
963: $starttime = $endtime - 60*60*24*7;
964: }
965: my $state;
966: if (&limit_by_time()) {
967: $state = '';
968: } else {
969: $state = 'disabled';
970: }
971: my $startdateform = &Apache::lonhtmlcommon::date_setter
972: ('Statistics','limitby_startdate',$starttime,undef,undef,$state);
973: my $enddateform = &Apache::lonhtmlcommon::date_setter
974: ('Statistics','limitby_enddate',$endtime,undef,undef,$state);
975: my $Str;
976: $Str .= '<script language="Javascript" >';
977: $Str .= 'function toggle_limitby_activity(state) {';
978: $Str .= ' if (state) {';
979: $Str .= ' limitby_startdate_enable();';
980: $Str .= ' limitby_enddate_enable();';
981: $Str .= ' } else {';
982: $Str .= ' limitby_startdate_disable();';
983: $Str .= ' limitby_enddate_disable();';
984: $Str .= ' }';
985: $Str .= '}';
986: $Str .= '</script>';
987: $Str .= '<fieldset>';
988: my $timecheckbox = '<input type="checkbox" name="limit_by_time" ';
989: if (&limit_by_time()) {
990: $timecheckbox .= ' checked ';
991: }
992: $timecheckbox .= 'OnChange="javascript:toggle_limitby_activity(this.checked);" ';
993: $timecheckbox .= ' />';
994: $Str .= '<legend>'.&mt('[_1] Limit by time',$timecheckbox).'</legend>';
995: $Str .= &mt('Start Time: [_1]',$startdateform).'<br />';
996: $Str .= &mt(' End Time: [_1]',$enddateform).'<br />';
997: $Str .= '</fieldset>';
998: return $Str;
999: }
1000:
1001: sub limit_by_time {
1002: if (exists($ENV{'form.limit_by_time'}) &&
1003: $ENV{'form.limit_by_time'} ne '' ) {
1004: return 1;
1005: } else {
1006: return 0;
1007: }
1008: }
1009:
1010: sub get_time_limits {
1011: my $starttime = &Apache::lonhtmlcommon::get_date_from_form
1012: ('limitby_startdate');
1013: my $endtime = &Apache::lonhtmlcommon::get_date_from_form
1014: ('limitby_enddate');
1015: return ($starttime,$endtime);
1.11 matthew 1016: }
1017:
1018:
1019:
1020: ####################################################
1021: ####################################################
1022:
1023: =pod
1024:
1025: =item sections_description
1026:
1027: Inputs: @Sections, an array of sections
1028:
1029: Returns: A text description of the sections selected.
1030:
1031: =cut
1032:
1033: ####################################################
1034: ####################################################
1035: sub sections_description {
1036: my @Sections = @_;
1037: my $sectionstring = '';
1038: if (scalar(@Sections) > 1) {
1039: if (scalar(@Sections) > 2) {
1040: my $last = pop(@Sections);
1041: $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
1042: } else {
1043: $sectionstring = "Sections ".join(' and ',@Sections);
1044: }
1045: } else {
1046: if ($Sections[0] eq 'all') {
1047: $sectionstring = "All sections";
1048: } else {
1049: $sectionstring = "Section ".$Sections[0];
1050: }
1051: }
1052: return $sectionstring;
1.2 matthew 1053: }
1054:
1055: ####################################################
1056: ####################################################
1057:
1058: =pod
1059:
1.12 matthew 1060: =item &manage_caches
1061:
1062: Inputs: $r, apache request object
1063:
1064: Returns: An array of scalars containing html for buttons.
1065:
1066: =cut
1067:
1068: ####################################################
1069: ####################################################
1070: sub manage_caches {
1071: my ($r,$formname,$inputname) = @_;
1072: &Apache::loncoursedata::clear_internal_caches();
1.16 matthew 1073: my $sectionkey =
1074: join(',',
1075: map {
1076: &Apache::lonnet::escape($_);
1077: } sort(@Apache::lonstatistics::SelectedSections)
1078: );
1079: my $statuskey = $Apache::lonstatistics::enrollment_status;
1.12 matthew 1080: if (exists($ENV{'form.ClearCache'}) ||
1.16 matthew 1081: exists($ENV{'form.updatecaches'}) ||
1082: (exists($ENV{'form.firstrun'}) && $ENV{'form.firstrun'} ne 'no') ||
1083: (exists($ENV{'form.prevsection'}) &&
1084: $ENV{'form.prevsection'} ne $sectionkey) ||
1085: (exists($ENV{'form.prevenrollstatus'}) &&
1086: $ENV{'form.prevenrollstatus'} ne $statuskey)
1087: ) {
1.12 matthew 1088: &Apache::lonstatistics::Gather_Full_Student_Data($r,$formname,
1089: $inputname);
1090: }
1091: #
1.16 matthew 1092: my @Buttons =
1093: ('<input type="submit" name="ClearCache" '.
1094: 'value="'.&mt('Clear Caches').'" />',
1095: '<input type="submit" name="updatecaches" '.
1.17 matthew 1096: 'value="'.&mt('Update Caches').'" />'.
1097: &Apache::loncommon::help_open_topic('Statistics_Cache'),
1.16 matthew 1098: '<input type="hidden" name="prevsection" value="'.$sectionkey.'" />',
1099: '<input type="hidden" name="prevenrollstatus" value="'.$statuskey.'" />'
1100: );
1101: #
1.12 matthew 1102: if (! exists($ENV{'form.firstrun'})) {
1103: $r->print('<input type="hidden" name="firstrun" value="yes" />');
1104: } else {
1105: $r->print('<input type="hidden" name="firstrun" value="no" />');
1106: }
1107: #
1108: return @Buttons;
1109: }
1110:
1111:
1112:
1113:
1114: ####################################################
1115: ####################################################
1116:
1117: =pod
1118:
1.2 matthew 1119: =back
1120:
1121: =cut
1122:
1123: ####################################################
1124: ####################################################
1.1 matthew 1125:
1126: 1;
1127:
1128: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>