1: # The LearningOnline Network with CAPA
2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
3: #
4: # $Id: default_homework.lcpm,v 1.72 2004/03/13 00:36:11 albertel 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: # Guy Albertelli
30: #
31: # 05/25/2001 H. K. Ng
32: # 05/31/2001 H. K. Ng
33: # 12/21/2001 Matthew
34: #
35: #init some globals
36: $hidden::RANDOMINIT=0;
37: $pi=atan2(1,1)*4;
38: $rad2deg=180.0/$pi;
39: $deg2rad=$pi/180.0;
40: $"=' ';
41:
42: sub caparesponse_check {
43: #not properly used yet: calc
44: #not to be used: $ans_fmt
45: my ($type,$tol,$sig,$ans_fmt,$unit,$calc,$samples) =
46: eval $_[2].
47: ';return ($__LC__type,$__LC__tol,$__LC__sig,$__LC__ans_fmt,$__LC__unit,$__LC__calc,$__LC__samples);';
48:
49: my $tol_type=''; # gets it's value from whether tol has a % or not done
50: my $sig_lbound=''; #done
51: my $sig_ubound=''; #done
52: my ($answer,$response,$expr)=@_;
53:
54:
55: #type's definitons come from capaParser.h
56: my $message='';
57: #remove leading and trailing whitespace
58: if (!defined($response)) {
59: $response='';
60: }
61: if ($response=~ /^\s|\s$/) {
62: $response=~ s:^\s+|\s+$::g;
63: $message .="Removed ws now :$response:\n";
64: } else {
65: $message .="no ws in :$response:\n";
66: }
67:
68: if (length($response) > 500) { return "TOO_LONG: Answer too long"; }
69:
70: if ($type eq '' ) {
71: $message .= "Didn't find a type :$type:$expr: defaulting\n";
72: if ( $answer eq ($answer *1.0)) { $type = 2;
73: } else { $type = 3; }
74: } else {
75: if ($type eq 'cs') { $type = 4;
76: } elsif ($type eq 'ci') { $type = 3;
77: } elsif ($type eq 'mc') { $type = 5;
78: } elsif ($type eq 'fml') { $type = 8;
79: } elsif ($type eq 'subj') { $type = 7;
80: } elsif ($type eq 'float') { $type = 2;
81: } elsif ($type eq 'int') { $type = 1;
82: } else { return "ERROR: Unknown type of answer: $type" }
83: }
84:
85: my $points;
86: my $id_list;
87: #formula type setup the sample points
88: if ($type eq '8') {
89: ($id_list,$points)=split(/@/,$samples);
90: $message.="Found :$id_list:$points: points in $samples\n";
91: }
92: if ($tol eq '') {
93: $tol=0.0;
94: $tol_type=1; #TOL_ABSOLUTE
95: } else {
96: if ($tol =~ /%$/) {
97: chop $tol;
98: $tol_type=2; #TOL_PERCENTAGE
99: } else {
100: $tol_type=1; #TOL_ABSOLUTE
101: }
102: }
103:
104: if ($sig eq '') {
105: $sig_lbound = 0; #SIG_LB_DEFAULT
106: $sig_ubound =15; #SIG_UB_DEFAULT
107: } else {
108: ($sig_lbound,$sig_ubound) = split /,/,$sig;
109: if (!defined($sig_lbound)) {
110: $sig_lbound = 0; #SIG_LB_DEFAULT
111: $sig_ubound =15; #SIG_UB_DEFAULT
112: }
113: if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
114: }
115: my $reterror="";
116: my $result = &caparesponse_capa_check_answer($response,$answer,$type,
117: $tol_type,$tol,
118: $sig_lbound,$sig_ubound,
119: $ans_fmt,$unit,$calc,$id_list,
120: $points,$external::randomseed,
121: \$reterror);
122:
123: if ($result == '1') { $result='EXACT_ANS'; }
124: elsif ($result == '2') { $result='APPROX_ANS'; }
125: elsif ($result == '3') { $result='SIG_FAIL'; }
126: elsif ($result == '4') { $result='UNIT_FAIL'; }
127: elsif ($result == '5') { $result='NO_UNIT'; }
128: elsif ($result == '6') { $result='UNIT_OK'; }
129: elsif ($result == '7') { $result='INCORRECT'; }
130: elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
131: elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
132: elsif ($result =='10') { $result='SUB_RECORDED'; }
133: elsif ($result =='11') { $result='BAD_FORMULA'; }
134: elsif ($result =='12') { $result='WANTED_NUMERIC'; }
135: else {$result = "ERROR: Unknown Result:$result:$@:";}
136:
137: return ("$result:\nRetError $reterror:\nError $error:\nAnswer $answer:\nResponse $response:\n type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|\n$message$expr",$reterror);
138: }
139:
140: sub get_array_args {
141: my ($expr,$arg)=@_;
142: # do these first, because who knows what varname the instructor might have used
143: # but it probably isn't $CAPARESPONSE_CHECK_LIST_answer
144: my $CAPARESPONSE_CHECK_LIST_answer = eval $expr.';return $'.$arg; #' stupid emacs
145: my $GET_ARRAY_ARGS_result="expr is $expr\narg is $arg\nfirst answer guess is $CAPARESPONSE_CHECK_LIST_answer\n";
146: my @GET_ARRAY_ARGS_list;
147: if ($CAPARESPONSE_CHECK_LIST_answer =~ /^\s*[\$\@]/) {
148: (@GET_ARRAY_ARGS_list) = eval $CAPARESPONSE_CHECK_LIST_answer;
149: }
150: $GET_ARRAY_ARGS_result.="error:$@:\n";
151: # if the eval fails just use what is in the answer exactly
152: if (!(@GET_ARRAY_ARGS_list) || !defined($GET_ARRAY_ARGS_list[0])) {
153: $GET_ARRAY_ARGS_result.="list zero is undefined\n";
154: $GET_ARRAY_ARGS_list[0]=$CAPARESPONSE_CHECK_LIST_answer;
155: }
156: return $GET_ARRAY_ARGS_result,@GET_ARRAY_ARGS_list;
157: }
158:
159: sub caparesponse_check_list {
160: my ($response,$expr)=@_;
161: # $expr =~ s/\\/\\\\/g;
162: # $expr =~ s/\'/\\\'/g;
163: # my ($result,@list) = &get_array_args($expr,'answer');
164: # $expr =~ s/\\\'/\'/g;
165: # $expr =~ s/\\\\/\\/g;
166: my ($result,@list);
167: @list=@CAPARESPONSE_CHECK_LIST_answer;
168: my $aresult='';
169: my $current_answer;
170: my $answers=join(':',@list);
171: $result.="Got response :$answers:\n";
172: my @responselist;
173: my $type =eval $expr.';return $__LC__type;';
174: $result.="Got type :$type:\n";
175: if ($type ne '' && $#list > 0) {
176: (@responselist)=split /,/,$response;
177: } else {
178: (@responselist)=($response);
179: }
180: my $unit='';
181: $result.="Initial final response :$responselist['-1']:\n";
182: if ($type eq '' || $type eq 'float') {
183: #for numerical problems split off the unit
184: if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
185: $responselist['-1']=$1;
186: $unit=$2;
187: }
188: }
189: $result.="Final final response :$responselist['-1']:\n";
190: $result.=":$#list: answers\n";
191: $unit=~s/\s//;
192: my $i=0;
193: my $awards='';
194: my @msgs;
195: for ($i=0; $i<@list;$i++) {
196: my $msg;
197: $result.="trying answer :$list[$i]:\n";
198: my $thisanswer=$list[$i];
199: # $thisanswer=~ s/\\/\\\\/g;
200: # $thisanswer =~ s/\'/\\\'/g;
201: $result.="trying answer :$thisanswer:\n";
202: if ($unit eq '') {
203: ($aresult,$msg)=&caparesponse_check($thisanswer,$responselist[$i],
204: $expr);
205: } else {
206: ($aresult,$msg)=&caparesponse_check($thisanswer,
207: $responselist[$i]." $unit",
208: $expr);
209: }
210: my ($temp)=split /:/, $aresult;
211: $awards.="$temp,";
212: $result.=$aresult;
213: push(@msgs,$msg);
214: }
215: chop $awards;
216: return ("$awards:\n$result",@msgs);
217: }
218:
219: sub tex {
220: if ( $external::target eq "tex" ) {
221: return $_[0];
222: } else {
223: return $_[1];
224: }
225: }
226:
227: sub var_in_tex {
228: if ( $external::target eq "tex" ) {
229: return $_[0];
230: } else {
231: return "";
232: }
233: }
234:
235: sub web {
236: if ( $external::target eq "tex" ) {
237: return $_[1];
238: } else {
239: if ( $external::target eq "web" || $external::target eq "answer") {
240: return $_[2];
241: } else {
242: return $_[0];
243: }
244: }
245: }
246:
247: sub html {
248: if ( $external::target eq "web" ) {
249: return shift;
250: }
251: }
252:
253: sub hinton {
254: return 0;
255: }
256:
257: sub random {
258: my ($start,$end,$step)=@_;
259: if ( ! $hidden::RANDOMINIT ) {
260: if ($external::randomseed == 0) { $external::randomseed=1; }
261: if ($external::randomseed =~/,/) {
262: my ($num1,$num2)=split(/,/,$seed);
263: &random_set_seed(abs($num1),abs($num2));
264: } else {
265: &random_set_seed(1,int(abs($external::randomseed)));
266: }
267: &math_random_uniform();
268: $hidden::RANDOMINIT=1;
269: }
270: if (!defined($step)) { $step=1; }
271: my $num=1+int(($end-$start)/$step);
272: my $result=$start + int(&math_random_uniform() * $num)*$step;
273: return $result;
274: }
275:
276: sub random_normal {
277: my ($item_cnt,$seed,$av,$std_dev) = @_;
278: my @oldseed=&random_get_seed();
279: my @retArray;
280: &random_set_seed_from_phrase($seed);
281: @retArray=&math_random_normal($item_cnt,$av,$std_dev);
282: &random_set_seed(@oldseed);
283: return @retArray;
284: }
285:
286: sub random_beta {
287: my ($item_cnt,$seed,$aa,$bb) = @_;
288: my @oldseed=&random_get_seed();
289: my @retArray;
290: &random_set_seed_from_phrase($seed);
291: @retArray=&math_random_beta($item_cnt,$aa,$bb);
292: &random_set_seed(@oldseed);
293: return @retArray;
294: }
295:
296: sub random_gamma {
297: my ($item_cnt,$seed,$a,$r) = @_;
298: my @oldseed=&random_get_seed();
299: my @retArray;
300: &random_set_seed_from_phrase($seed);
301: @retArray=&math_random_gamma($item_cnt,$a,$r);
302: &random_set_seed(@oldseed);
303: return @retArray;
304: }
305:
306: sub random_exponential {
307: my ($item_cnt,$seed,$av) = @_;
308: my @oldseed=&random_get_seed();
309: my @retArray;
310: &random_set_seed_from_phrase($seed);
311: @retArray=&math_random_exponential($item_cnt,$av);
312: &random_set_seed(@oldseed);
313: return @retArray;
314: }
315:
316: sub random_poisson {
317: my ($item_cnt,$seed,$mu) = @_;
318: my @oldseed=&random_get_seed();
319: my @retArray;
320: &random_set_seed_from_phrase($seed);
321: @retArray=&math_random_poisson($item_cnt,$mu);
322: &random_set_seed(@oldseed);
323: return @retArray;
324: }
325:
326: sub random_chi {
327: my ($item_cnt,$seed,$df) = @_;
328: my @oldseed=&random_get_seed();
329: my @retArray;
330: &random_set_seed_from_phrase($seed);
331: @retArray=&math_random_chi_square($item_cnt,$df);
332: &random_set_seed(@oldseed);
333: return @retArray;
334: }
335:
336: sub random_noncentral_chi {
337: my ($item_cnt,$seed,$df,$nonc) = @_;
338: my @oldseed=&random_get_seed();
339: my @retArray;
340: &random_set_seed_from_phrase($seed);
341: @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
342: &random_set_seed(@oldseed);
343: return @retArray;
344: }
345:
346: sub random_f {
347: my ($item_cnt,$seed,$dfn,$dfd) = @_;
348: my @oldseed=&random_get_seed();
349: my @retArray;
350: &random_set_seed_from_phrase($seed);
351: @retArray=&math_random_f($item_cnt,$dfn,$dfd);
352: &random_set_seed(@oldseed);
353: return @retArray;
354: }
355:
356: sub random_noncentral_f {
357: my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
358: my @oldseed=&random_get_seed();
359: my @retArray;
360: &random_set_seed_from_phrase($seed);
361: @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
362: &random_set_seed(@oldseed);
363: return @retArray;
364: }
365:
366: sub random_multivariate_normal {
367: my ($item_cnt,$seed,$mean,$covar) = @_;
368: my @oldseed=&random_get_seed();
369: &random_set_seed_from_phrase($seed);
370: @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
371: &random_set_seed(@oldseed);
372: return @retArray;
373: }
374:
375: sub random_multinomial {
376: my ($item_cnt,$seed,@p) = @_;
377: my @oldseed=&random_get_seed();
378: my @retArray;
379: &random_set_seed_from_phrase($seed);
380: @retArray=&math_random_multinomial($item_cnt,@p);
381: &random_set_seed(@oldseed);
382: return @retArray;
383: }
384:
385: sub random_permutation {
386: my ($seed,@inArray) = @_;
387: my @oldseed=&random_get_seed();
388: my @retArray;
389: &random_set_seed_from_phrase($seed);
390: @retArray=&math_random_permutation(@inArray);
391: &random_set_seed(@oldseed);
392: return @retArray;
393: }
394:
395: sub random_uniform {
396: my ($item_cnt,$seed,$low,$high) = @_;
397: my @oldseed=&random_get_seed();
398: my @retArray;
399: &random_set_seed_from_phrase($seed);
400: @retArray=&math_random_uniform($item_cnt,$low,$high);
401: &random_set_seed(@oldseed);
402: return @retArray;
403: }
404:
405: sub random_uniform_integer {
406: my ($item_cnt,$seed,$low,$high) = @_;
407: my @oldseed=&random_get_seed();
408: my @retArray;
409: &random_set_seed_from_phrase($seed);
410: @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
411: &random_set_seed(@oldseed);
412: return @retArray;
413: }
414:
415: sub random_binomial {
416: my ($item_cnt,$seed,$nt,$p) = @_;
417: my @oldseed=&random_get_seed();
418: my @retArray;
419: &random_set_seed_from_phrase($seed);
420: @retArray=&math_random_binomial($item_cnt,$nt,$p);
421: &random_set_seed(@oldseed);
422: return @retArray;
423: }
424:
425: sub random_negative_binomial {
426: my ($item_cnt,$seed,$ne,$p) = @_;
427: my @oldseed=&random_get_seed();
428: my @retArray;
429: &random_set_seed_from_phrase($seed);
430: @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
431: &random_set_seed(@oldseed);
432: return @retArray;
433: }
434:
435: sub abs { abs(shift) }
436: sub sin { sin(shift) }
437: sub cos { cos(shift) }
438: sub exp { exp(shift) }
439: sub int { int(shift) }
440: sub log { log(shift) }
441: sub atan2 { atan2($_[0],$_[1]) }
442: sub sqrt { sqrt(shift) }
443:
444: sub tan { CORE::sin($_[0]) / CORE::cos($_[0]) }
445: #sub atan { atan2($_[0], 1); }
446: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
447: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
448:
449: sub log10 { CORE::log($_[0])/CORE::log(10); }
450:
451: sub factorial {
452: my $input = CORE::int(shift);
453: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
454: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
455: return 1 if $input == 0;
456: my $result = 1;
457: for (my $i=2; $i<=$input; $i++) { $result *= $i }
458: return $result;
459: }
460:
461: sub sgn {
462: return -1 if $_[0] < 0;
463: return 0 if $_[0] == 0;
464: return 1 if $_[0] > 0;
465: }
466:
467: sub min {
468: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
469: return shift @sorted;
470: }
471:
472: sub max {
473: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
474: return pop @sorted;
475: }
476:
477: sub roundto {
478: my ($input,$n) = @_;
479: return sprintf('%.'.$n.'f',$input);
480: }
481:
482: sub to_string {
483: my ($input,$n) = @_;
484: return sprintf($input) if $n eq "";
485: $n = '.'.$n if $n !~ /^\./;
486: return sprintf('%'.$n,$input) if $n ne "";
487: }
488:
489: sub sub_string {
490: my ($str,$start,$len) = @_;
491: return substr($str,$start-1,$len);
492: }
493:
494: sub pow {return $_[0] ** $_[1]; }
495: sub ceil {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
496: sub floor {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
497: #sub floor {return int($_[0]); }
498:
499: sub format {
500: my ($value,$fmt)=@_;
501: my $dollarmode;
502: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
503: $fmt=~s/e/E/g;
504: my $result=sprintf('%.'.$fmt,$value);
505: $result=~s/(E[+-]*)0/$1/;
506: if ($dollarmode) {$result=&dollarmode($result);}
507: return $result;
508: }
509:
510: sub prettyprint {
511: my ($value,$fmt,$target)=@_;
512: my $result;
513: my $dollarmode;
514: if (!$target) { $target = $external::target; }
515: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
516: if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
517: if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
518: my $frac=$1;
519: if ($dollarmode) { $frac=&dollarformat($frac); }
520: my $exponent=$2;
521: $exponent=~s/^\+0*//;
522: $exponent=~s/^-0*/-/;
523: $exponent=~s/^-0*/-/;
524: if ($exponent eq '-') { undef($exponent); }
525: if ($exponent) {
526: if ($target eq 'web') {
527: $result=$frac.'×10<sup>'.$exponent.'</sup>';
528: } elsif ($target eq 'tex') {
529: $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
530: } else {
531: $result=$value;
532: }
533: } else {
534: $result=$frac;
535: }
536: } else {
537: $result=$value;
538: if ($dollarmode) { $result=&dollarformat($result,$target); }
539: }
540: return $result;
541: }
542:
543: sub dollarformat {
544: my ($number,$target) = @_;
545: if (!$target) { $target = $external::target; }
546: if ($number =~ /\./) {
547: while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*$)/) {
548: $number = $1.','.$2.$3;
549: }
550: } else {
551: while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
552: $number = $1.','.$2.$3;
553: }
554: }
555: if ($target eq 'tex') {
556: $number='\$'.$number; #' stupid emacs
557: } else {
558: $number='$'.$number; #' stupid emacs
559: }
560: return $number;
561: }
562:
563: sub map {
564: my ($phrase,$dest,$source)=@_;
565: my @oldseed=&random_get_seed();
566: my @seed = &random_seed_from_phrase($phrase);
567: &random_set_seed(@seed);
568: my $destct = scalar(@$dest);
569: if (!$source) {
570: my @output;
571: my @idx = &math_random_permuted_index($destct);
572: my $ctr = 0;
573: while ($ctr < $destct) {
574: $output[$ctr] = $$dest[$idx[$ctr]];
575: $ctr++;
576: }
577: &random_set_seed(@oldseed);
578: return @output;
579: } else {
580: my $num = scalar(@$source);
581: my @idx = &math_random_permuted_index($num);
582: my $ctr = 0;
583: my $tot = $num;
584: $tot = $destct if $destct < $num;
585: if (ref($$dest[0])) {
586: while ($ctr < $tot) {
587: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
588: $ctr++;
589: }
590: } else {
591: while ($ctr < $tot) {
592: $$dest[$ctr] = $$source[$idx[$ctr]];
593: $ctr++;
594: }
595: }
596: }
597: &random_set_seed(@oldseed);
598: return '';
599: }
600:
601: sub rmap {
602: my ($phrase,$dest,$source)=@_;
603: my @oldseed=&random_get_seed();
604: my @seed = &random_seed_from_phrase($phrase);
605: &random_set_seed(@seed);
606: my $destct = scalar(@$dest);
607: if (!$source) {
608: my @idx = &math_random_permuted_index($destct);
609: my $ctr = 0;
610: my @r_idx;
611: while ($ctr < $destct) {
612: $r_idx[$idx[$ctr]] = $ctr;
613: $ctr++;
614: }
615: my @output;
616: $ctr = 0;
617: while ($ctr < $destct) {
618: $output[$ctr] = $$dest[$r_idx[$ctr]];
619: $ctr++;
620: }
621: &random_set_seed(@oldseed);
622: return @output;
623: } else {
624: my $num = scalar(@$source);
625: my @idx = &math_random_permuted_index($num);
626: my $ctr = 0;
627: my $tot = $num;
628: $tot = $destct if $destct < $num;
629: my @r_idx;
630: while ($ctr < $tot) {
631: $r_idx[$idx[$ctr]] = $ctr;
632: $ctr++;
633: }
634: $ctr = 0;
635: if (ref($$dest[0])) {
636: while ($ctr < $tot) {
637: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
638: $ctr++;
639: }
640: } else {
641: while ($ctr < $tot) {
642: $$dest[$ctr] = $$source[$r_idx[$ctr]];
643: $ctr++;
644: }
645: }
646: }
647: &random_set_seed(@oldseed);
648: return '';
649: }
650:
651: sub capa_id { return }
652:
653: sub problem { return }
654:
655: sub name{
656: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
657: $fullname = "" if $fullname eq ", ";
658: $fullname =~ s/\%2d/-/g;
659: return $fullname;
660: }
661:
662: sub student_number {
663: my $id = &EXT('environment.id');
664: $id = '' if $id eq "";
665: return $id;
666: }
667:
668: sub class {
669: my $course = &EXT('course.description');
670: $course = '' if $course eq "";
671: return $course;
672: }
673:
674: sub sec {
675: my $sec = &EXT('request.course.sec');
676: $sec = '' if $sec eq "";
677: return $sec;
678: }
679:
680: sub open_date {
681: my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
682: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
683: my @hm = split(/:/,$dc[3]);
684: my $ampm = " am";
685: if ($hm[0] > 12) {
686: $hm[0]-=12;
687: $ampm = " pm";
688: }
689: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
690: }
691:
692: sub due_date {
693: my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
694: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
695: my @hm = split(/:/,$dc[3]);
696: my $ampm = " am";
697: if ($hm[0] > 12) {
698: $hm[0]-=12;
699: $ampm = " pm";
700: }
701: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
702: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
703: }
704:
705: sub answer_date {
706: my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
707: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
708: my @hm = split(/:/,$dc[3]);
709: my $ampm = " am";
710: if ($hm[0] > 12) {
711: $hm[0]-=12;
712: $ampm = " pm";
713: }
714: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
715: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
716: }
717:
718: sub array_moments {
719: my @input=@_;
720: my (@output,$N);
721: $N=scalar (@input);
722: $output[0]=$N;
723: if ($N <= 1) {
724: $output[1]=$input[0];
725: $output[1]="Input array not defined" if ($N == 0);
726: $output[2]="variance undefined for N<=1";
727: $output[3]="skewness undefined for N<=1";
728: $output[4]="kurtosis undefined for N<=1";
729: return @output;
730: }
731: my $sum=0;
732: foreach my $line (@input) {
733: $sum+=$line;
734: }
735: $output[1] = $sum/$N;
736: my ($x,$sdev,$var,$skew,$kurt) = 0;
737: foreach my $line (@input) {
738: $x=$line-$output[1];
739: $var+=$x**2;
740: $skew+=$x**3;
741: $kurt+=$x**4;
742: }
743: $output[2]=$var/($N-1);
744: $sdev=CORE::sqrt($output[2]);
745: if ($sdev == 0) {
746: $output[3]="inf-variance=0";
747: $output[4]="inf-variance=0";
748: return @output;
749: }
750: $output[3]=$skew/($sdev**3*$N);
751: $output[4]=$kurt/($sdev**4*$N)-3;
752: return @output;
753: }
754:
755: sub choose {
756: my $num = $_[0];
757: return $_[$num];
758: }
759:
760: # expiremental idea
761: sub proper_path {
762: my ($path)=@_;
763: if ( $external::target eq "tex" ) {
764: return '/home/httpd/html'.$path;
765: } else {
766: return $path;
767: }
768: }
769:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>