Annotation of loncom/homework/structuretags.pm, revision 1.42

1.34      albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
1.33      albertel    3: # 2/19 Guy
1.1       albertel    4: package Apache::structuretags; 
                      5: 
                      6: use strict;
                      7: use Apache::lonnet;
                      8: 
                      9: sub BEGIN {
1.34      albertel   10:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
1.37      albertel   11: #  &Apache::lonxml::register_insert('problem','',('part','postanswerdate','preduedate'))
1.10      albertel   12: }
                     13: 
                     14: sub start_web {
1.40      albertel   15:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.10      albertel   16:   my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
1.19      albertel   17:   if ($target eq 'web') {
                     18:     return $bodytext;
                     19:   } 
                     20:   return '';
1.10      albertel   21: }
                     22: 
                     23: sub end_web {
                     24: }
                     25: 
                     26: sub start_tex {
1.40      albertel   27:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.10      albertel   28:   my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
1.19      albertel   29:   if ($target eq 'tex') {
                     30:     return $bodytext
                     31:   }
1.10      albertel   32:   return '';
                     33: }
                     34: 
                     35: sub end_tex {
1.9       albertel   36: }
                     37: 
                     38: sub start_problem {
1.40      albertel   39:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel   40: 
                     41: #intialize globals
                     42:   $Apache::inputtags::part='0';
                     43:   @Apache::inputtags::responselist = ();
1.42    ! albertel   44:   $Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
        !            45:   &Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
        !            46:   if ($Apache::lonhomework::type eq '') {
        !            47:     $Apache::lonhomework::type='homework';
        !            48:   }
1.19      albertel   49: #adeed vars to the scripting enviroment
                     50:   my $expression='$external::part='.$Apache::inputtags::part.';';
                     51:   &Apache::run::run($expression,$safeeval);
1.22      albertel   52:   my $status;
                     53:   my $datemsg;
1.36      albertel   54: 
1.41      albertel   55:   #should get back a <html> or the neccesary stuff to start XML/MathML
1.40      albertel   56:   my $result=&Apache::londefdef::start_html($target,$token,$tagstack,$parstack,$parser,$safeeval);
1.36      albertel   57: 
1.41      albertel   58:   my $head_tag_start='<head>'.&Apache::lonxml::registerurl();
                     59:   my $body_tag_start='<body onLoad="'.&Apache::lonxml::loadevents().'" '.
                     60: 	  'onUnload="'.&Apache::lonxml::unloadevents().'" '.
                     61: 	    'bgcolor="#FFFFFF">';
1.34      albertel   62:   if ($target eq 'web' || $target eq 'grade') {
1.22      albertel   63:     ($status,$datemsg) = &Apache::lonhomework::check_date('0');
                     64:     push (@Apache::inputtags::status,$status);
1.24      albertel   65:     my $expression='$external::datestatus="'.$status.'";';
                     66:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
                     67:     &Apache::run::run($expression,$safeeval);
1.22      albertel   68:     if ( $status eq 'CLOSED' ) {
1.21      albertel   69:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
                     70:       if ( $target eq "web" ) {
1.41      albertel   71: 	$result.= $head_tag_start.'</head>';
                     72: 	return $result . $body_tag_start .
                     73: 	  " <br />Problem is not open to be viewed. The problem $datemsg<br />";
1.21      albertel   74:       }
1.41      albertel   75:     }
1.21      albertel   76:   }
1.19      albertel   77:   if ($target eq 'web') {
1.39      albertel   78:     my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.30      albertel   79:     if ($name eq '') { 
                     80:       $name=&Apache::lonnet::EXT('resource.title');
                     81:       if ($name eq 'con_lost') { $name = ''; }
                     82:     }
                     83:     $Apache::lonhomework::name=$name;
1.22      albertel   84:     if ($status eq 'CAN_ANSWER') {
                     85:       # create a page header and exit
1.41      albertel   86:       $result.="$head_tag_start<title>$name</title></head>\n
                     87:               $body_tag_start\n
1.36      albertel   88:               <form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">".
                     89: 		'<input type="hidden" name="submitted" value="yes" />';
                     90:       if ($ENV{'request.state'} eq "construct") {
                     91: 	$result.='<input type="hidden" name="problemmode" value="View" />
                     92:                  <input type="submit" name="problemmode" value="Edit" /><hr />';
                     93:       }
                     94:       return $result;
1.29      albertel   95:     } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' || $status eq 'CLOSED') {
1.41      albertel   96:       return $result.$head_tag_start."<title>$name</title></head>\n$body_tag_start\n";
1.22      albertel   97:     }
                     98:   }
1.34      albertel   99:   if ($target eq 'edit') {
1.41      albertel  100:     $result.=$head_tag_start."</head>".$body_tag_start.
                    101:       '<form name="lonhomework" method="POST" action="'.$ENV{'request.uri'}.'">
                    102:        <input type="hidden" name="submitted" value="edit" />
                    103:        <input type="hidden" name="problemmode" value="Edit" />
                    104:        <input type="submit" name="problemmode" value="View" />
                    105:        <input type="submit" name="Undo" value="undo" /> <hr />
                    106:       ';
1.39      albertel  107:     my $temp=&Apache::edit::insertlist($target,$token);
1.36      albertel  108:     $result.=$temp;
                    109:     return $result;
1.34      albertel  110:   }
1.37      albertel  111:   if ($target eq 'modified') {
                    112:     $result=$token->[4];
                    113:     $result.=&Apache::edit::handle_insert();
                    114:     return $result;
                    115:   }
1.19      albertel  116:   return '';
1.9       albertel  117: }
                    118: 
                    119: sub end_problem {
1.40      albertel  120:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.16      albertel  121:   my $result='';
1.24      albertel  122:   my $status=$Apache::inputtags::status['-1'];
1.40      albertel  123:   if ($target eq 'grade' || $target eq 'web' ) {
1.28      albertel  124:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
                    125:        $status eq 'CAN_ANSWER') {
1.19      albertel  126:       # if part is zero, no <part>s existed, so we need to the grading
                    127:       &Apache::inputtags::grade;
                    128:     } elsif ($Apache::inputtags::part eq '0') {
                    129:       # if part is zero, no <part>s existed, so we need show the current 
                    130:       # grading status
1.20      albertel  131:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
1.41      albertel  132:     }
1.22      albertel  133:     if ($target eq 'web') { 
                    134:       if ($status eq 'CAN_ANSWER') {
                    135: 	$result.="</form></body>\n"; 
1.28      albertel  136:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
1.41      albertel  137: 	$result.="</body>\n";
                    138:       }
1.22      albertel  139:     }
1.41      albertel  140:   }
1.34      albertel  141:   if ($target eq 'meta') {
1.18      albertel  142:     if ($Apache::inputtags::part eq '0') {
1.16      albertel  143:       $result=&Apache::response::mandatory_part_meta;
                    144:     }
                    145:   }
1.34      albertel  146:   if ($target eq 'edit') {
                    147:     &Apache::lonxml::debug("in end_problem with $target, edit");
                    148:     $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
                    149:   }
1.16      albertel  150:   return $result;
1.1       albertel  151: }
                    152: 
                    153: sub start_block {
1.40      albertel  154:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.1       albertel  155: 
1.38      albertel  156:   if ($target eq 'web' || $target eq 'grade') {
                    157:     my $code = @$parstack[$#$parstack];
                    158:     $code =~ s/\"//g;
                    159:     $code .=';return $condition;';
                    160:     #  print "<br />$code<br />";
                    161:     my $result = &Apache::run::run($code,$safeeval);
                    162:     &Apache::lonxml::debug("block :$code: returned :$result:");
                    163:     if ( ! $result ) { 
                    164:       my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
                    165:       &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
                    166:     }
1.1       albertel  167:   }
                    168:   return "";
                    169: }
                    170: 
                    171: sub end_block {
1.38      albertel  172:   return '';
1.4       tsai      173: }
                    174: 
                    175: sub start_while {
1.40      albertel  176:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.4       tsai      177: 
                    178:   my $code = @$parstack[$#$parstack];
                    179:   $code =~ s/\"//g;
                    180:   $code .=';return $condition;';
                    181: 
1.5       tsai      182:   push( @Apache::structuretags::whileconds, $code); 
1.4       tsai      183:   my $result = &Apache::run::run($code,$safeeval);
                    184:   my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5       tsai      185:   push( @Apache::structuretags::whilebody, $bodytext);
                    186:   if ( $result ) { 
1.8       albertel  187:     &Apache::lonxml::newparser($parser,\$bodytext);
1.4       tsai      188:   }
                    189:   return "";
                    190: }
                    191: 
                    192: sub end_while {
1.40      albertel  193:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.5       tsai      194:   my $code = pop @Apache::structuretags::whileconds;
                    195:   my $bodytext = pop @Apache::structuretags::whilebody;
                    196:   my $result = &Apache::run::run($code,$safeeval);
                    197:   if ( $result ) { 
1.8       albertel  198:     &Apache::lonxml::newparser($parser,\$bodytext);
1.5       tsai      199:   } 
                    200:   return "";
1.1       albertel  201: }
1.6       tsai      202: 
                    203: # <randomlist> 
                    204: #  <tag1>..</tag1>
                    205: #  <tag2>..</tag2>
                    206: #  <tag3>..</tag3>
                    207: #  ... 
                    208: # </randomlist>
                    209: sub start_randomlist {
1.40      albertel  210:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.9       albertel  211:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
1.6       tsai      212:   my $b_parser= HTML::TokeParser->new(\$body);
                    213:   my $b_tok;
                    214:   my @randomlist;
                    215:   my $list_item;
                    216: 
                    217:   while($b_tok = $b_parser->get_token() ) {
                    218:     if($b_tok->[0] eq 'S') { # start tag
                    219:     # get content of the tag until matching end tag
                    220:     # get all text upto the matching tag
                    221:     # and push the content into @randomlist
1.9       albertel  222:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
1.7       tsai      223:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6       tsai      224:       push(@randomlist,$list_item);
1.32      albertel  225:    #  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
1.6       tsai      226:     }
                    227:     if($b_tok->[0] eq 'T') { # text
                    228:     # what to do with text in between tags?
1.32      albertel  229:       #  print "<b>TEXT $b_tok->[1]</b><br />";
1.6       tsai      230:     }
                    231:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
1.32      albertel  232:       #  print "<b>END-TAG $b_tok->[1]</b><br />";
1.6       tsai      233:     # }
                    234:   }
1.7       tsai      235:   my @idx_arr = (0 .. $#randomlist);
                    236:   &Apache::structuretags::shuffle(\@idx_arr);
                    237:   my $bodytext = '';
                    238:   for(0 .. $#randomlist) {
                    239:     $bodytext .= "$randomlist[ $idx_arr[$_] ]";
                    240:   }
1.8       albertel  241: 
                    242:   &Apache::lonxml::newparser($parser,\$bodytext);
1.6       tsai      243:   return "";
1.7       tsai      244: }
                    245: 
                    246: sub shuffle {
                    247:     my $a=shift;
                    248:     my $i;
                    249:     for($i=@$a;--$i;) {
                    250:       my $j=int rand($i+1);
                    251:       next if $i == $j;
                    252:       @$a[$i,$j] = @$a[$j,$i];
                    253:     }
1.6       tsai      254: }
                    255: 
                    256: sub end_randomlist {
                    257: }
                    258: 
1.11      albertel  259: sub start_part {
1.40      albertel  260:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.39      albertel  261:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
1.14      albertel  262:   $Apache::inputtags::part=$id;
1.18      albertel  263:   @Apache::inputtags::responselist = ();
1.15      www       264:   if ($target eq 'meta') {
1.16      albertel  265:     return &Apache::response::mandatory_part_meta;
1.37      albertel  266:   } elsif ($target eq 'web' || $target eq 'grade') {
1.23      albertel  267:     my ($status,$datemsg) = &Apache::lonhomework::check_date("OPEN_DATE",$id);
                    268:     push (@Apache::inputtags::status,$status);
                    269:     my $expression='$external::datestatus="'.$status.'";';
                    270:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
                    271:     &Apache::run::run($expression,$safeeval);
                    272:     if ( $status eq 'CLOSED' ) {
1.21      albertel  273:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
                    274:       if ( $target eq "web" ) {
1.32      albertel  275: 	return "<br />Part is not open to be viewed. It $datemsg<br />";
1.21      albertel  276:       }
                    277:     }
1.15      www       278:   }
1.19      albertel  279:   return '';
1.11      albertel  280: }
                    281: 
                    282: sub end_part {
1.40      albertel  283:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.18      albertel  284:   &Apache::lonxml::debug("in end_part $target ");
1.28      albertel  285:   my $status=$Apache::inputtags::status['-1'];
1.23      albertel  286:   pop @Apache::inputtags::status;
1.19      albertel  287:   if ( $target eq 'meta' ) { return ''; }
1.37      albertel  288:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
                    289:     return &Apache::inputtags::grade;
1.28      albertel  290:   }
1.37      albertel  291:   if ($target eq 'web') {
                    292:     return &Apache::inputtags::gradestatus($Apache::inputtags::part);
                    293:   }
                    294:   return '';
1.11      albertel  295: }
1.1       albertel  296: 
1.25      albertel  297: sub start_preduedate {
1.40      albertel  298:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  299:   if ($target eq 'web' || $target eq 'grade') {
1.29      albertel  300:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
                    301: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
1.26      albertel  302:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
1.24      albertel  303:     }
                    304:   }
                    305:   return '';
                    306: }
                    307: 
1.25      albertel  308: sub end_preduedate {
1.24      albertel  309:   return '';
                    310: }
                    311: 
1.25      albertel  312: sub start_postanswerdate {
1.40      albertel  313:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  314:   if ($target eq 'web' || $target eq 'grade') {
1.24      albertel  315:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
1.26      albertel  316:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
1.24      albertel  317:     }
                    318:   }
                    319:   return '';
                    320: }
                    321: 
1.25      albertel  322: sub end_postanswerdate {
1.24      albertel  323:   return '';
                    324: }
                    325: 
1.25      albertel  326: sub start_notsolved {
1.40      albertel  327:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  328:   if ($target eq 'web' || $target eq 'grade') {
1.24      albertel  329:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    330:     &Apache::lonxml::debug("not solved has :$gradestatus:");
                    331:     if ($gradestatus =~ /^correct/) {
                    332:       &Apache::lonxml::debug("skipping");
1.26      albertel  333:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
1.24      albertel  334:     }
                    335:   }
                    336:   return '';
                    337: }
                    338: 
1.25      albertel  339: sub end_notsolved {
1.24      albertel  340:   return '';
                    341: }
                    342: 
                    343: sub start_solved {
1.40      albertel  344:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  345:   if ($target eq 'web' || $target eq 'grade') {
1.24      albertel  346:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    347:     if ($gradestatus !~ /^correct/) {
                    348:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
                    349:     }
                    350:   }
                    351:   return '';
                    352: }
                    353: 
                    354: sub end_solved {
                    355:   return '';
                    356: }
1.34      albertel  357: 
                    358: sub start_startouttext {
1.40      albertel  359:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.35      albertel  360:   my @result=(''.'');
                    361:   if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
                    362:   return (@result);
1.34      albertel  363: }
                    364: sub end_startouttext {
1.40      albertel  365:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  366:   my $result='';
1.35      albertel  367:   my $text='';
                    368: 
1.34      albertel  369:   if ($target eq 'edit') {
1.35      albertel  370:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser[$#$parser]);
1.42    ! albertel  371:     $result.="<table width=\"100%\" border=\"2\"><tr><td>Text Block</td>
        !           372: <td>Delete:".
        !           373:   &Apache::edit::deletelist($target,$token)
        !           374:   ."</td>
        !           375: <td>".
        !           376:   &Apache::edit::insertlist($target,$token).
        !           377:     "</td>
        !           378: </tr><tr><td colspan=\"3\">\n".
        !           379: 	&Apache::edit::editfield($token->[1],$text,"",50,5);
1.35      albertel  380:   }
                    381:   if ($target eq 'modified') {
                    382:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
                    383:     $result='<startouttext />'.&Apache::edit::modifiedfield();
1.34      albertel  384:   }
                    385:   return $result;
                    386: }
                    387: sub start_endouttext {
1.40      albertel  388:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  389:   my $result='';
1.42    ! albertel  390:   if ($target eq "edit" ) { $result="</td></tr></table>\n"; }
1.35      albertel  391:   if ($target eq "modified") { $result='<endouttext />'; }
1.34      albertel  392:   return $result;
                    393: }
                    394: sub end_endouttext {
1.40      albertel  395:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.35      albertel  396:   my @result=('','');
                    397:   if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
                    398:   return (@result);
1.34      albertel  399: }
                    400: 
                    401: 
1.1       albertel  402: 1;
                    403: __END__

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