Annotation of loncom/homework/imageresponse.pm, revision 1.2

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # iimage click response style
                      3: 
                      4: package Apache::imageresponse;
                      5: use strict;
                      6: 
                      7: sub BEGIN {
                      8:   &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
                      9: }
                     10: 
                     11: sub start_imageresponse {
                     12:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
                     13:   #when in a radiobutton response use these
                     14:   &Apache::lonxml::register('Apache::imageresponse',('foilgroup','foil','text','image','rectangle'));
                     15:   my $id = &Apache::response::start_response($parstack,$safeeval);
                     16:   return '';
                     17: }
                     18: 
                     19: sub end_imageresponse {
                     20:   &Apache::response::end_response;
                     21:   return '';
                     22: }
                     23: 
                     24: %Apache::response::foilgroup={};
                     25: sub start_foilgroup {
                     26:   %Apache::response::foilgroup={};
                     27:   return '';
                     28: }
                     29: 
                     30: sub setrandomnumber {
                     31:   my $rndseed=&Apache::lonnet::rndseed();
                     32:   $rndseed=unpack("%32i",$rndseed);
                     33:   $rndseed=$rndseed
                     34:     +&Apache::lonnet::numval($Apache::inputtags::part)
                     35:       +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
                     36:   srand($rndseed);
                     37:   return '';
                     38: }
                     39: 
1.2     ! albertel   40: sub getfoilcounts {
        !            41:   my ($parstack,$safeeval)=@_;
        !            42:   my $rrargs ='';
        !            43:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
        !            44:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
        !            45:   my $count = $#{ $Apache::response::foilgroup{'names'} };
        !            46:   return ($count,$max);
        !            47: }
        !            48: 
        !            49: sub whichfoils {
        !            50:   my ($max)=@_;
        !            51:   my @names = @{ $Apache::response::foilgroup{'names'} };
        !            52:   my @whichopt =();
        !            53:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
        !            54:     my $aopt=int rand $#names;
        !            55:     &Apache::lonxml::debug("From $#names elms, picking $aopt");
        !            56:     $aopt=splice(@names,$aopt,1);
        !            57:     &Apache::lonxml::debug("Picked $aopt");
        !            58:     push (@whichopt,$aopt);
        !            59:   }
        !            60:   return @whichopt;
        !            61: }
        !            62: 
        !            63: sub displayfoils {
        !            64:   my (@whichopt) = @_;
        !            65:   my $result ='';
        !            66:   #<input type=image name=timg0 src=/~walt/kap18/picts/ch1map.gif>
        !            67:   my $name;
        !            68:   my $temp=1;
        !            69:   foreach $name (@whichopt) {
        !            70:     $result.=$Apache::response::foilgroup{"$name.text"}."<br />\n";
        !            71:     my $image=$Apache::response::foilgroup{"$name.image"};
        !            72:     $result.="<input type=\"image\" name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\" src=\"$image\"/> <br />\n";
        !            73:     $temp++;
        !            74:   }
        !            75:   return $result;
        !            76: }
        !            77: 
1.1       albertel   78: sub end_foilgroup {
                     79:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.2     ! albertel   80:   my $result='';
        !            81:   if ($target eq 'web' || $target eq 'grade') {
        !            82:     &setrandomnumber();
        !            83:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
        !            84:     if ($count>$max) { $count=$max }
        !            85:     &Apache::lonxml::debug("Count is $count from $max");
        !            86:     my @whichopt = &whichfoils($max);
        !            87:   }
        !            88:   if ($target eq 'web') {
        !            89:     $result=&displayfoils(@whichopt);
        !            90:   }
        !            91:   if ($target eq 'grade') {
        !            92:     &gradefoils(@whichopt);
        !            93:   }
        !            94:   return $result;
1.1       albertel   95: }
                     96: 
                     97: sub start_conceptgroup {
                     98: }
                     99: 
                    100: sub end_conceptgroup {
                    101: }
                    102: 
                    103: $Apache::imageresponse::curname='';
                    104: sub start_foil {
                    105:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
                    106:   if ($target eq 'web' || $target eq 'grade') {
                    107:     my $args ='';
                    108:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                    109:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
                    110:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    111:     $Apache::imageresponse::curname=$name;
                    112:   }
                    113:   return '';
                    114: }
                    115: 
                    116: sub end_foil {
                    117:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
                    118:   return '';
                    119: }
                    120: 
                    121: sub start_text {
                    122:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
                    123:   if ($target eq 'web') { 
                    124:     $Apache::lonxml::redirection--; 
                    125:   }
                    126:   return '';
                    127: }
                    128: 
                    129: sub end_text {
                    130:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
                    131:   if ($target eq 'web') {
                    132:     my $name = $Apache::imageresponse::curname;
                    133:     $Apache::response::foilgroup{"$name.text"} = $Apache::lonxml::outputstack;
                    134:     if ($target eq 'web' ) {
                    135:       $Apache::lonxml::redirection++;
                    136:       if ($Apache::lonxml::redirection == 1) { 
                    137: 	$Apache::lonxml::outputstack=''; 
                    138:       }
                    139:     }
                    140:   }
                    141:   return '';
                    142: }
                    143: 
                    144: sub start_image {
                    145:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.2     ! albertel  146:   if ($target eq 'web') { $Apache::lonxml::redirection--; }
        !           147:   return '';
1.1       albertel  148: }
                    149: 
                    150: sub end_image {
                    151:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.2     ! albertel  152:   if ($target eq 'web') {
        !           153:     my $name = $Apache::imageresponse::curname;
        !           154:     $Apache::response::foilgroup{"$name.image"} = $Apache::lonxml::outputstack;
        !           155:     if ($target eq 'web' ) {
        !           156:       $Apache::lonxml::redirection++;
        !           157:       if ($Apache::lonxml::redirection == 1) {$Apache::lonxml::outputstack='';}
        !           158:     }
        !           159:   }
        !           160:   return '';
1.1       albertel  161: }
                    162: 
                    163: sub start_rectangle {
                    164:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.2     ! albertel  165:   if ($target eq 'web') { $Apache::lonxml::redirection--; }
        !           166:   return '';
1.1       albertel  167: }
                    168: 
                    169: sub end_rectangle {
                    170:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.2     ! albertel  171:   if ($target eq 'web') {
        !           172:     my $name = $Apache::imageresponse::curname;
        !           173:     push ${ $Apache::response::foilgroup{"$name.area"}},"rectangle:$Apache::lonxml::outputstack";
        !           174:     if ($target eq 'web' ) {
        !           175:       $Apache::lonxml::redirection++;
        !           176:       if ($Apache::lonxml::redirection == 1) {$Apache::lonxml::outputstack='';}
        !           177:     }
        !           178:   }
1.1       albertel  179: }
                    180: 1;
                    181: __END__
                    182:  

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>