Annotation of loncom/homework/structuretags.pm, revision 1.272
1.34 albertel 1: # The LearningOnline Network with CAPA
2: # definition of tags that give a structure to a document
1.74 albertel 3: #
1.272 ! albertel 4: # $Id: structuretags.pm,v 1.271 2004/11/10 22:20:50 albertel Exp $
1.74 albertel 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: #
1.254 www 28: ###
1.54 www 29:
1.133 sakharuk 30:
1.1 albertel 31: package Apache::structuretags;
32:
33: use strict;
34: use Apache::lonnet;
1.101 sakharuk 35: use Apache::File();
1.147 www 36: use Apache::lonmenu;
1.210 albertel 37: use Apache::lonlocal;
1.231 sakharuk 38: use Apache::lonxml;
1.267 albertel 39: use Time::HiRes qw( gettimeofday tv_interval );
1.78 harris41 40: BEGIN {
1.248 albertel 41: &Apache::lonxml::register('Apache::structuretags',('block','languageblock','instructorcomment','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','problemtype','startouttext','endouttext','simpleeditbutton','definetag'));
1.10 albertel 42: }
43:
44: sub start_web {
1.159 albertel 45: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.200 albertel 46: my $bodytext=&Apache::lonxml::get_all_text("/web",$parser);
47: if ($target eq 'web') {
48: return $bodytext;
1.159 albertel 49: }
50: return '';
1.10 albertel 51: }
52:
53: sub end_web {
1.44 ng 54: return '';
1.10 albertel 55: }
56:
57: sub start_tex {
1.159 albertel 58: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.198 sakharuk 59: my $result='';
1.200 albertel 60: my $bodytext=&Apache::lonxml::get_all_text("/tex",$parser);
61: if ($target eq 'tex') {
62: return $bodytext.' ';
1.159 albertel 63: }
1.198 sakharuk 64: return $result;;
1.10 albertel 65: }
66:
67: sub end_tex {
1.44 ng 68: return '';
1.9 albertel 69: }
70:
1.48 albertel 71: sub page_start {
1.159 albertel 72: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
73: my %found;
1.207 albertel 74: foreach my $taginside (@$tagstack) {
1.159 albertel 75: foreach my $taglookedfor ('html','body','form') {
76: if ($taginside =~ /^$taglookedfor$/i) {$found{$taglookedfor} = 1;}
77: }
78: }
79:
80: my $result;
81: my $head_tag_start;
82: if (!defined($found{'html'})) {
83: $result=&Apache::londefdef::start_html($target,$token,$tagstack,
84: $parstack,$parser,$safeeval);
1.254 www 85: $head_tag_start='<head>'.&Apache::lonmenu::registerurl(undef,$target).
1.261 www 86: &Apache::lonhtmlcommon::htmlareaheaders().
1.270 albertel 87: &Apache::lonhtmlcommon::spellheader().
88: &Apache::lonxml::fontsettings();
1.159 albertel 89: }
90: my $body_tag_start;
1.272 ! albertel 91: if (!defined($found{'body'}) && $ENV{'request.state'} eq 'construct'
! 92: && $ENV{'environment.remote'} eq 'off') {
! 93: $body_tag_start=&Apache::loncommon::bodytag();
! 94: } elsif (!defined($found{'body'})) {
1.270 albertel 95: $body_tag_start='<body onload="'.&Apache::lonmenu::loadevents().'" '.
96: 'onunload="'.&Apache::lonmenu::unloadevents().'" ';
1.159 albertel 97: my $background=&Apache::lonxml::get_param('background',$parstack,
98: $safeeval);
1.199 www 99: if ($ENV{'browser.imagesuppress'} eq 'on') { $background=''; }
1.159 albertel 100: if ($background) {
101: $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
102: $background;
103: $body_tag_start.='background="'.$background.'" ';
104: } else {
105: my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,
106: $safeeval);
1.199 www 107: if (($bgcolor) && ($ENV{'browser.blackwhite'} ne 'on')) {
1.159 albertel 108: $body_tag_start.='bgcolor="'.$bgcolor.'" ';
109: } else {
110: $body_tag_start.='bgcolor="#ffffff"';
111: }
1.199 www 112: }
113: if ($ENV{'browser.fontenhance'} eq 'on') {
114: $body_tag_start.=' style="font-size: x-large;" ';
1.159 albertel 115: }
116: $body_tag_start.='>'.&Apache::lonmenu::menubuttons(undef,$target,1);
117: if ($target eq 'web' && $ENV{'request.state'} ne 'construct') {
1.165 albertel 118: my ($symb,undef,undef,undef,$publicuser)=
119: &Apache::lonxml::whichuser();
120: if ($symb eq '' && !$publicuser) {
1.159 albertel 121: my $help = &Apache::loncommon::help_open_topic("Ambiguous_Reference");
1.211 albertel 122: $help=&mt("Browsing resource, all submissions are temporary.")."<br />";
1.159 albertel 123: $body_tag_start.=$help;
124: }
125: }
1.245 albertel 126: $body_tag_start.=&Apache::lonxml::message_location();
1.159 albertel 127: }
128: my $form_tag_start;
129: if (!defined($found{'form'})) {
1.271 albertel 130: $form_tag_start='<form name="lonhomework" method="post" action="';
131: my $uri=$ENV{'request.uri'};
132: if ($ENV{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
133: $form_tag_start.=$uri.'">';
1.159 albertel 134: }
135: return ($result,$head_tag_start,$body_tag_start,$form_tag_start);
1.105 albertel 136: }
137:
1.141 matthew 138: #use Time::HiRes();
1.105 albertel 139: sub get_resource_name {
1.159 albertel 140: my ($parstack,$safeeval)=@_;
1.204 albertel 141: if (defined($Apache::lonhomework::name)) {
142: return $Apache::lonhomework::name;
143: }
1.159 albertel 144: my $name=&Apache::lonnet::gettitle();
145: if ($name eq '') {
146: $name=&Apache::lonnet::EXT('resource.title');
147: if ($name eq 'con_lost') { $name = ''; }
148: }
1.204 albertel 149: if ($name!~/\S+/) {
150: $name=$ENV{'request.uri'};
1.205 albertel 151: $name=~s-.*/([^/]+)$-$1-;
1.204 albertel 152: }
1.159 albertel 153: $Apache::lonhomework::name=$name;
154: return $name;
1.105 albertel 155: }
156:
157: sub setup_rndseed {
1.159 albertel 158: my ($safeeval)=@_;
159: my $rndseed;
1.162 albertel 160: my ($symb)=&Apache::lonxml::whichuser();
1.247 albertel 161: if ($ENV{'request.state'} eq "construct" || $symb eq '' ||
162: $Apache::lonhomework::history{'resource.CODE'}) {
1.159 albertel 163: $rndseed=$ENV{'form.rndseed'};
164: if (!$rndseed) {
1.162 albertel 165: $rndseed=$Apache::lonhomework::history{'rndseed'};
166: if (!$rndseed) {
167: $rndseed=time;
168: }
1.237 albertel 169: $ENV{'form.rndseed'}=$rndseed;
1.162 albertel 170: }
1.210 albertel 171: if ($ENV{'form.resetdata'} eq &mt('New Problem Variation') ||
172: $ENV{'form.newrandomization'} eq &mt('New Randomization')) {
1.190 albertel 173: srand(time);
174: $rndseed=int(rand(2100000000));
1.159 albertel 175: $ENV{'form.rndseed'}=$rndseed;
1.180 albertel 176: delete($ENV{'form.resetdata'});
177: delete($ENV{'form.newrandomization'});
1.159 albertel 178: }
1.187 albertel 179: if (defined($rndseed) && $rndseed ne int($rndseed)) {
180: $rndseed=join(',',&Math::Random::random_seed_from_phrase($rndseed));
181: }
1.247 albertel 182: if ($Apache::lonhomework::history{'resource.CODE'}) {
183: $rndseed=&Apache::lonnet::rndseed();
184: }
1.221 albertel 185: if ($safeeval) {
186: &Apache::lonxml::debug("Setting rndseed to $rndseed");
1.250 albertel 187: &Apache::run::run('$external::randomseed="'.$rndseed.'";',$safeeval);
1.221 albertel 188: }
1.159 albertel 189: }
190: return $rndseed;
1.105 albertel 191: }
192:
1.268 albertel 193: sub remember_problem_state {
194: return '
195: <input type="hidden" name="problemstate" value="'.$ENV{'form.problemstate'}.'" />
196: <input type="hidden" name="problemtype" value="'.$ENV{'form.problemtype'}.'" />
197: <input type="hidden" name="problemstatus" value="'.$ENV{'form.problemstatus'}.'" />';
198: }
199:
1.105 albertel 200: sub problem_edit_header {
1.268 albertel 201: return '<input type="hidden" name="submitted" value="edit" />'.
202: &Apache::structuretags::remember_problem_state().'
1.210 albertel 203: <input type="hidden" name="problemmode" value="'.&mt('Edit').'" />
1.243 matthew 204: <input type="submit" name="problemmode" accesskey="d" value="'.&mt('Discard Edits and View').'" />
205: <input type="submit" name="problemmode" accesskey="x" value="'.&mt('EditXML').'" />
206: <input type="submit" name="Undo" accesskey="u" value="'.&mt('undo').'" /> <hr />
207: <input type="submit" name="submit" accesskey="s" value="'.&mt('Submit Changes and Edit').'" />
208: <input type="submit" name="submit" accesskey="v" value="'.&mt('Submit Changes and View').'" /><table><tr><td>'.
1.260 albertel 209: &Apache::loncommon::help_open_menu('','Problem Editing Help','Problem_Editor_XML_Index','',5,'Authoring',undef,undef,undef,'Problem Editing Help')
210: .'</td></tr></table>'.
1.228 www 211: '<table border="0"><tr><td bgcolor="#DDDDDD">';
1.105 albertel 212: }
213:
214: sub problem_edit_footer {
1.210 albertel 215: return '</td></tr></table><br /><input type="submit" name="submit" value="'.&mt('Submit Changes and Edit').'" />
1.255 www 216: <input type="submit" name="submit" value="'.&mt('Submit Changes and View').'" />'.
217: &Apache::lonhtmlcommon::htmlareaselectactive(@Apache::lonxml::htmlareafields).
218: "\n</form>\n</body>\n</html>";
1.105 albertel 219: }
220:
1.235 albertel 221: sub option {
222: my ($value,$name) = @_;
223: my $result ="<option value='".$value."' ";
224: if ($ENV{'form.'.$name} eq $value) {
225: $result.=" selected='on' ";
226: }
227: $result.='>';
228: return $result;
229: }
230:
1.105 albertel 231: sub problem_web_to_edit_header {
1.159 albertel 232: my ($rndseed)=@_;
1.210 albertel 233: my $result.='<input type="hidden" name="problemmode" value="'.&mt('View').'" />
1.243 matthew 234: <input type="submit" name="problemmode" accesskey="e" value="'.&mt('Edit').'" />
235: <input type="submit" name="problemmode" accesskey="x" value="'.&mt('EditXML').'" />
236: <input type="submit" name="newrandomization" accesskey="a" value="'.&mt('New Randomization').'" />
237: <input type="submit" name="resetdata" accesskey="r" value="'.&mt('Reset Submissions').'" />
1.210 albertel 238: <nobr><input type="submit" name="changerandseed" value="'.&mt('Change Random Seed To:').'" />
1.270 albertel 239: <input type="text" name="rndseed" size="10" value="'.
1.175 albertel 240: $rndseed.'"
1.270 albertel 241: onchange="javascript:document.lonhomework.changerandseed.click()" /></nobr>
1.105 albertel 242: <input type="checkbox" name="showallfoils" ';
1.159 albertel 243: if (defined($ENV{'form.showallfoils'})) { $result.='checked="on"'; }
1.228 www 244: $result.= ' />'.&mt(' Show All Foils').
245: &Apache::loncommon::help_open_topic('Problem_Editor_Testing_Area','Testing Problems').
246: '<hr />';
1.235 albertel 247: $result.="
248: <nobr>
249: Problem Status:
250: <select name='problemstate'>
1.270 albertel 251: <option value=''></option>
1.235 albertel 252: ".&option('CLOSED' ,'problemstate').&mt("Closed")."</option>
253: ".&option('CAN_ANSWER' ,'problemstate').&mt("Answerable")."</option>
254: ".&option('CANNOT_ANSWER_tries' ,'problemstate').&mt("Open with full tries")."</option>
255: ".&option('CANNOT_ANSWER_correct','problemstate').&mt("Open and correct")."</option>
256: ".&option('SHOW_ANSWER' ,'problemstate').&mt("Show Answer")."</option>
257: </select>
258: </nobr>
259: <nobr>
260: Problem Type:
261: <select name='problemtype'>
1.270 albertel 262: <option value=''></option>
1.242 albertel 263: ".&option('exam' ,'problemtype').&mt("Exam Problem")."</option>
264: ".&option('problem','problemtype').&mt("Homework problem")."</option>
265: ".&option('survey' ,'problemtype').&mt("Survey Question")."</option>
1.235 albertel 266: </select>
267: </nobr>
268: <nobr>
269: Feedback Mode:
270: <select name='problemstatus'>
271: <option value=''></option>
1.242 albertel 272: ".&option('yes','problemstatus').&mt("Show Feedback")."</option>
273: ".&option('no', 'problemstatus').&mt("Don't Show Feedback")."</option>
1.235 albertel 274: </select>
275: </nobr>
1.242 albertel 276: <input type='submit' name='changeproblemmode' value='".&mt("Change")."' />
1.235 albertel 277: <hr />";
1.159 albertel 278: my $numtoanalyze=$ENV{'form.numtoanalyze'};
1.176 albertel 279: if (!$numtoanalyze) { $numtoanalyze=20; }
1.210 albertel 280: $result.= '<input type="submit" name="problemmode" value='.
281: &mt('"Calculate answers').'" /> for
1.136 albertel 282: <input type="text" name="numtoanalyze" value="'.
1.210 albertel 283: $numtoanalyze.'" size="5" /> '.&mt('versions of this problem').
284: '.'.&Apache::loncommon::help_open_topic("Analyze_Problem",
1.176 albertel 285: '',undef,undef,300).
1.210 albertel 286: '<hr />';
1.159 albertel 287: return $result;
1.48 albertel 288: }
289:
1.65 albertel 290: sub initialize_storage {
1.159 albertel 291: %Apache::lonhomework::results=();
1.162 albertel 292: %Apache::lonhomework::history=();
1.159 albertel 293: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.162 albertel 294: if ($ENV{'request.state'} eq 'construct' || $symb eq '') {
1.159 albertel 295: %Apache::lonhomework::history=
296: &Apache::lonnet::tmprestore($ENV{'request.uri'},'',$domain,$name);
297: my ($temp)=keys %Apache::lonhomework::history ;
298: &Apache::lonxml::debug("Return message of $temp");
299: } else {
300: %Apache::lonhomework::history=
301: &Apache::lonnet::restore($symb,$courseid,$domain,$name);
302: }
303: #ignore error conditions
1.67 albertel 304: my ($temp)=keys %Apache::lonhomework::history ;
1.159 albertel 305: if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
1.65 albertel 306: }
307:
308: # -------------------------------------------------------------finalize_storage
309: # Stores away the result has to a student's environment
310: # checks form.grade_ for specific values, other wises stores
311: # to the running users environment
312: sub finalize_storage {
1.159 albertel 313: my $result;
314: my ($temp) = keys %Apache::lonhomework::results;
315: if ( $temp ne '' ) {
316: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.162 albertel 317: if ($ENV{'request.state'} eq 'construct' || $symb eq '') {
318: $Apache::lonhomework::results{'rndseed'}=$ENV{'form.rndseed'};
1.159 albertel 319: $result=&Apache::lonnet::tmpstore(\%Apache::lonhomework::results,
320: $ENV{'request.uri'},'',$domain,$name);
321: &Apache::lonxml::debug('Construct Store return message:'.$result);
322: } else {
323: $result=&Apache::lonnet::cstore(\%Apache::lonhomework::results,
324: $symb,$courseid,$domain,$name);
325: &Apache::lonxml::debug('Store return message:'.$result);
326: }
1.67 albertel 327: }
1.159 albertel 328: return $result;
1.65 albertel 329: }
330:
331: sub checkout_msg {
1.211 albertel 332: my %lt=&Apache::lonlocal::texthash(
333: 'resource'=>'The resource needs to be checked out',
334: 'id_expln'=>'As a resource gets checked out, a unique timestamped ID is given to it, and a permanent record is left in the system.',
335: 'warning'=>'Checking out resources is subject to course policies, and may exclude future credit even if done erroneously.',
336: 'checkout'=>'Check out Exam for Viewing',
337: 'checkout?'=>'Check out Exam?');
1.159 albertel 338: return (<<ENDCHECKOUT);
1.211 albertel 339: <h2>$lt{'resource'}</h2>
340: <p>$lt{'id_expln'}</p>
341: <font color="red">
342: <p>$lt{'warning'}</p>
1.91 albertel 343: </font>
1.270 albertel 344: <form name="checkout" method="post" action="$ENV{'request.uri'}">
1.91 albertel 345: <input type="hidden" name="doescheckout" value="yes" />
1.211 albertel 346: <input type="button" name="checkoutbutton" value="$lt{'checkout'}" onClick="javascript:if (confirm('$lt{'checkout?'}')) { document.checkout.submit(); }" />
1.65 albertel 347: </form>
348: ENDCHECKOUT
349: }
350:
1.252 albertel 351: sub firstaccess_msg {
1.253 albertel 352: my ($time,$symb)=@_;
353: my ($map)=&Apache::lonnet::decode_symb($symb);
354: my $foldertitle=&Apache::lonnet::gettitle($map);
355: &Apache::lonxml::debug("map is $map title is $foldertitle");
1.252 albertel 356: return (<<ENDCHECKOUT);
1.253 albertel 357: <h2>The resources in "$foldertitle" are open for a limited time. Once you click the 'Show Resource' button below you have $time to complete all resources "$foldertitle".</h2>
1.270 albertel 358: <form name="markaccess" method="post" action="$ENV{'request.uri'}">
1.252 albertel 359: <input type="hidden" name="markaccess" value="yes" />
360: <input type="button" name="accessbutton" value="Show Resource" onClick="javascript:if (confirm('Start Timer?')) { document.markaccess.submit(); }" />
361: </form>
362: ENDCHECKOUT
363: }
364:
1.204 albertel 365: sub init_problem_globals {
366: my ($type)=@_;
367: #initialize globals
368: if ($type eq 'problem') {
369: $Apache::inputtags::part='0';
370: @Apache::inputtags::partlist=('0');
1.266 albertel 371: $Apache::lonhomework::problemstatus=&get_problem_status('0');
372: $Apache::lonhomework::ignore_response_errors=0;
373: } elsif ($type eq 'library') {
1.204 albertel 374: $Apache::inputtags::part='';
375: @Apache::inputtags::partlist=();
376: $Apache::lonhomework::problemstatus='';
1.266 albertel 377: $Apache::lonhomework::ignore_response_errors=1;
1.204 albertel 378: }
379: @Apache::inputtags::responselist = ();
380: @Apache::inputtags::importlist = ();
381: @Apache::inputtags::previous=();
382: @Apache::inputtags::previous_version=();
383: $Apache::structuretags::printanswer='No';
384: @Apache::structuretags::whileconds=();
385: @Apache::structuretags::whilebody=();
386: @Apache::structuretags::whileline=();
387: $Apache::lonhomework::scantronmode=0;
388: undef($Apache::lonhomework::name);
389:
390: }
391:
392: sub reset_problem_globals {
393: my ($type)=@_;
394: undef(%Apache::lonhomework::history);
395: undef(%Apache::lonhomework::results);
396: undef($Apache::inputtags::part);
1.208 albertel 397: #don't undef this, lonhomework.pm takes care of this, we use this to
398: #detect if we try to do 2 problems in one file
399: # undef($Apache::lonhomework::parsing_a_problem);
1.204 albertel 400: undef($Apache::lonhomework::name);
401: }
402:
1.241 albertel 403: sub set_problem_state {
1.240 albertel 404: my ($part)=@_;
405: if ($ENV{'form.problemstate'} eq 'CANNOT_ANSWER_correct') {
406: $Apache::lonhomework::history{"resource.$part.solved"}=
407: 'correct_by_student';
408: }
409: }
410:
1.241 albertel 411: sub get_problem_status {
412: my ($part)=@_;
1.267 albertel 413: my $problem_status;
1.241 albertel 414: if ($ENV{'request.state'} eq 'construct' &&
415: defined($ENV{'form.problemstatus'})) {
416: $problem_status=$ENV{'form.problemstatus'};
1.267 albertel 417: } else {
418: $problem_status=&Apache::lonnet::EXT("resource.$part.problemstatus");
419: &Apache::lonxml::debug("problem status for $part is $problem_status");
420: &Apache::lonxml::debug("env probstat is ".$ENV{'form.problemstatus'});
1.241 albertel 421: }
422: return $problem_status;
423: }
424:
1.9 albertel 425: sub start_problem {
1.159 albertel 426: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19 albertel 427:
1.184 albertel 428: if ( $Apache::inputtags::part ne '' ||
429: $Apache::lonhomework::parsing_a_problem) {
430: &Apache::lonxml::error('Only one <problem> allowed in a .problem file');
431: #my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
1.159 albertel 432: return '';
433: }
1.184 albertel 434:
435: $Apache::lonhomework::parsing_a_problem=1;
1.204 albertel 436: &init_problem_globals('problem');
1.166 albertel 437:
438: if (defined($ENV{'scantron.maxquest'})) {
439: $Apache::lonhomework::scantronmode=1;
440: }
1.161 albertel 441:
1.246 albertel 442: &initialize_storage();
1.159 albertel 443: if ($target ne 'analyze') {
444: if ($target eq 'web') {
445: &Apache::lonhomework::showhash(%Apache::lonhomework::history);
1.238 albertel 446: }
1.241 albertel 447: if ($ENV{'request.state'} eq 'construct') { &set_problem_state('0'); }
1.159 albertel 448: $Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
1.237 albertel 449: if (($ENV{'request.state'} eq 'construct') &&
450: defined($ENV{'form.problemtype'})) {
451: $Apache::lonhomework::type=$ENV{'form.problemtype'};
452: }
1.240 albertel 453: &Apache::lonxml::debug("Found this to be of type :$Apache::ltonhomework::type:");
1.159 albertel 454: }
1.164 albertel 455: if ($Apache::lonhomework::type eq '' ) {
1.159 albertel 456: my $uri=$ENV{'request.uri'};
457: if ($uri=~/\.(\w+)$/) {
458: $Apache::lonhomework::type=$1;
459: &Apache::lonxml::debug("Using type of $1");
460: } else {
461: $Apache::lonhomework::type='problem';
462: &Apache::lonxml::debug("Using default type, problem, :$uri:");
463: }
1.87 albertel 464: }
1.58 www 465:
1.159 albertel 466: #added vars to the scripting enviroment
1.213 albertel 467: my $expression='$external::part=\''.$Apache::inputtags::part.'\';';
1.248 albertel 468: $expression.='$external::type=\''.$Apache::lonhomework::type.'\';';
1.24 albertel 469: &Apache::run::run($expression,$safeeval);
1.159 albertel 470: my $status;
471: my $accessmsg;
472:
473: #should get back a <html> or the neccesary stuff to start XML/MathML
474: my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
475: &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
476: if ($target eq 'tex' and $ENV{'request.symb'} =~ m/\.page_/) {$result='';}
477:
478: if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval); }
479: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
480: $target eq 'tex') {
481: #handle exam checkout
482: if ($Apache::lonhomework::type eq 'exam') {
483: my $token=
484: $Apache::lonhomework::history{"resource.0.outtoken"};
485: if (($ENV{'form.doescheckout'}) && (!$token)) {
486: $token=&Apache::lonxml::maketoken();
487: $Apache::lonhomework::history{"resource.0.outtoken"}=
488: $token;
489: }
490: $body_tag_start.=&Apache::lonxml::printtokenheader($target,$token);
1.142 albertel 491: }
1.252 albertel 492: if ($ENV{'form.markaccess'}) {
493: &Apache::lonnet::set_first_access('map');
494: }
1.159 albertel 495: #handle rand seed in construction space
496: my $rndseed=&setup_rndseed($safeeval);
1.162 albertel 497: my ($symb)=&Apache::lonxml::whichuser();
1.163 albertel 498: if ($ENV{'request.state'} ne "construct" && $symb eq '') {
1.162 albertel 499: $form_tag_start.='<input type="hidden" name="rndseed" value="'.
500: $rndseed.'" />'.
501: '<input type="submit" name="resetdata"
1.211 albertel 502: value="'.&mt('New Problem Variation').'" />'.
1.164 albertel 503: '<input type="hidden" name="username"
1.267 albertel 504: value="'.$ENV{'form.username'}.'" />';
505: if ($ENV{'user.adv'}) {
506: $form_tag_start.=
507: ' <input type="checkbox" name="showallfoils" ';
508: if (defined($ENV{'form.showallfoils'})) {
509: $form_tag_start.='checked="on"';
510: }
511: $form_tag_start.= ' />'.&mt(' Show All Foils');
512: }
513: $form_tag_start.='<hr />';
1.162 albertel 514: }
1.159 albertel 515: ($status,$accessmsg) = &Apache::lonhomework::check_access('0');
516: push (@Apache::inputtags::status,$status);
517: my $expression='$external::datestatus="'.$status.'";';
518: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
519: &Apache::run::run($expression,$safeeval);
520: &Apache::lonxml::debug("Got $status");
521: if (( $status eq 'CLOSED' ) ||
522: ( $status eq 'UNCHECKEDOUT') ||
1.252 albertel 523: ( $status eq 'NOT_YET_VIEWED') ||
1.159 albertel 524: ( $status eq 'BANNED') ||
1.216 albertel 525: ( $status eq 'UNAVAILABLE') ||
526: ( $status eq 'INVALID_ACCESS')) {
1.159 albertel 527: my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
528: if ( $target eq "web" ) {
529: $result.= $head_tag_start.'</head>';
530: my $msg=$body_tag_start;
531: if ($status eq 'UNAVAILABLE') {
1.245 albertel 532: $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.253 albertel 533: } elsif ($status ne 'NOT_YET_VIEWED') {
1.245 albertel 534: $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
1.159 albertel 535: }
1.216 albertel 536: if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
1.159 albertel 537: $msg.='The problem '.$accessmsg;
538: } elsif ($status eq 'UNCHECKEDOUT') {
539: $msg.=&checkout_msg;
1.252 albertel 540: } elsif ($status eq 'NOT_YET_VIEWED') {
1.253 albertel 541: $msg.=&firstaccess_msg($accessmsg,$symb);
1.159 albertel 542: }
543: $result.=$msg.'<br />';
544: } elsif ($target eq 'tex') {
545: $result.='\begin{document}\noindent \vskip 1 mm \begin{minipage}{\textwidth}\vskip 0 mm';
546: if ($status eq 'UNAVAILABLE') {
1.211 albertel 547: $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
1.159 albertel 548: } else {
1.211 albertel 549: $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
1.159 albertel 550: }
551: }
552: } elsif ($target eq 'web') {
553: my $name= &get_resource_name($parstack,$safeeval);
1.227 albertel 554: $result.="$head_tag_start<title>$name</title></head>
1.158 sakharuk 555: $body_tag_start \n $form_tag_start".
1.227 albertel 556: '<input type="hidden" name="submitted" value="yes" />';
557: # create a page header and exit
558: if ($ENV{'request.state'} eq "construct") {
559: $result.= &problem_web_to_edit_header($ENV{'form.rndseed'});
560: }
561: # if we are viewing someone else preserve that info
562: if (defined $ENV{'form.grade_symb'}) {
563: foreach my $field ('symb','courseid','domain','username') {
564: $result .= '<input type="hidden" name="grade_'.$field.
565: '" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
1.159 albertel 566: }
567: }
568: } elsif ($target eq 'tex') {
1.192 sakharuk 569: my $startminipage = '';
1.191 sakharuk 570: if (not $ENV{'form.problem_split'}=~/yes/) {
1.192 sakharuk 571: $startminipage = '\begin{minipage}{\textwidth}';
1.178 sakharuk 572: }
1.159 albertel 573: my $id = $Apache::inputtags::part;
574: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.223 sakharuk 575: my $packages=&Apache::lonnet::metadata($ENV{'request.uri'},'packages');
576: my @packages = split /,/,$packages;
577: my $allow_print_points = 0;
578: foreach my $partial_key (@packages) {
1.230 albertel 579: if ($partial_key=~m/^part_0$/) {
1.223 sakharuk 580: $allow_print_points=1;
1.159 albertel 581: }
582: }
583: my $duedate = &Apache::lonnet::EXT("resource.$id.duedate");
584: $duedate = POSIX::strftime("%c",localtime($duedate));
585: my $temp_file;
586: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.due";
587: if (-e $filename) {
588: $temp_file = Apache::File->new($filename);
589: } else {
590: $temp_file = Apache::File->new('>>'.$filename);
591: }
592: my @due_file_content = <$temp_file>;
593: my $due_file_content = $due_file_content[$#due_file_content];
594: chomp $due_file_content;
1.257 sakharuk 595: my $name_of_resourse= &Apache::lonxml::latex_special_symbols(&get_resource_name($parstack,$safeeval),'header');
1.159 albertel 596: if ($due_file_content ne $duedate) {
597: $temp_file = Apache::File->new('>'.$filename);
598: print $temp_file "$duedate\n";
599: if (not $ENV{'request.symb'} =~ m/\.page_/) {
600: if(not $duedate=~m/1969/ and $Apache::lonhomework::type ne 'exam') {
1.206 sakharuk 601: $result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent\textit{Due date: '.$duedate.'} \vskip 1 mm\noindent '.$startminipage.'\addcontentsline{toc}{subsection}{'.$name_of_resourse.'}';
1.159 albertel 602: } else {
1.206 sakharuk 603: $result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent \vskip 1 mm \noindent'.$startminipage.'\addcontentsline{toc}{subsection}{'.$name_of_resourse.'}';
1.159 albertel 604: if ($Apache::lonhomework::type eq 'exam' and $allow_print_points==1) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
605: }
1.104 sakharuk 606: } else {
1.159 albertel 607: $result .= '\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\';
1.104 sakharuk 608: }
1.101 sakharuk 609: } else {
1.159 albertel 610: if (not $ENV{'request.symb'} =~ m/\.page_/) {
1.206 sakharuk 611: $result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent \vskip 1 mm\noindent'.$startminipage.'\addcontentsline{toc}{subsection}{'.$name_of_resourse.'}';
1.159 albertel 612: if (($Apache::lonhomework::type eq 'exam') and ($allow_print_points==1)) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
613: } else {
614: $result .= '\vskip 1mm \\\\\\\\';
615: }
616: }
1.99 sakharuk 617: }
1.159 albertel 618: } elsif ($target eq 'edit') {
619: $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
620: &problem_edit_header();
1.226 albertel 621: $Apache::lonxml::warnings_error_header=
622: &mt("Editor Errors - these errors might not effect the running of the problem, but they will likely cause problems with further use of the Edit mode. Please use the EditXML mode to fix these errors.")."<br />";
1.159 albertel 623: my $temp=&Apache::edit::insertlist($target,$token);
624: $result.=$temp;
625: } elsif ($target eq 'modified') {
626: $result=$token->[4];
627: $result.=&Apache::edit::handle_insert();
628: } else {
629: # page_start returned a starting result, delete it if we don't need it
630: $result = '';
1.99 sakharuk 631: }
1.159 albertel 632: return $result;
1.9 albertel 633: }
634:
635: sub end_problem {
1.159 albertel 636: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
637: my $result='';
638: my $status=$Apache::inputtags::status['-1'];
639: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
640: $target eq 'tex') {
1.249 albertel 641: if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
1.159 albertel 642: # if part is zero, no <part>s existed, so we need to the grading
1.249 albertel 643: if ($status eq 'CAN_ANSWER' ||$Apache::lonhomework::scantronmode) {
644: &Apache::inputtags::grade;
645: } else {
646: # move any submission data to .hidden
647: &Apache::inputtags::hidealldata($Apache::inputtags::part);
648: }
1.159 albertel 649: } elsif ( ($target eq 'web' || $target eq 'tex') &&
650: $Apache::inputtags::part eq '0' &&
1.252 albertel 651: $status ne 'UNCHECKEDOUT' && $status ne 'NOT_YET_VIEWED') {
1.159 albertel 652: # if part is zero, no <part>s existed, so we need show the current
653: # grading status
654: my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
655: $result.= $gradestatus;
656: }
657: if (
658: (($target eq 'web') && ($ENV{'request.state'} ne 'construct')) ||
659: ($target eq 'answer') || ($target eq 'tex')
660: ) {
1.227 albertel 661: if ($target ne 'tex' &&
662: $ENV{'form.answer_output_mode'} ne 'tex') {
1.254 www 663: $result.="</form>";
664: $result.=&Apache::lonhtmlcommon::htmlareaselectactive(@Apache::lonxml::htmlareafields);
665: $result.="</body>\n";
1.159 albertel 666: }
667: if ($target eq 'web') {
668: $result.=&Apache::lonxml::xmlend();
669: } elsif ($target eq 'tex') {
1.178 sakharuk 670: my $endminipage = '';
1.191 sakharuk 671: if (not $ENV{'form.problem_split'}=~/yes/) {
1.178 sakharuk 672: $endminipage = '\end{minipage}';
673: }
1.257 sakharuk 674: if ($ENV{'form.print_discussions'} eq 'yes') {
1.263 sakharuk 675: $result.=&Apache::lonxml::xmlend($target,$parser);
1.257 sakharuk 676: $result=~s/<\/html>//;
1.159 albertel 677: } else {
1.262 sakharuk 678: $result .= '\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
679: if (not $ENV{'request.symb'} =~ m/\.page_/) {
680: $result .= $endminipage.'\end{document} ';
681: } else {
682: $result .= '';
683: }
1.159 albertel 684: }
685: }
686: }
687: if ($target eq 'grade') {
688: &Apache::lonhomework::showhash(%Apache::lonhomework::results);
689: &finalize_storage();
690: }
1.172 albertel 691: if ($target eq 'answer' && ($ENV{'request.state'} eq 'construct')
1.173 albertel 692: && $ENV{'form.answer_output_mode'} ne 'tex') {
1.172 albertel 693: $result.='</html>'; # normally we get it from xmlend, but in CSTR
694: # we always show answer mode too.
1.159 albertel 695: }
696: } elsif ($target eq 'meta') {
697: if ($Apache::inputtags::part eq '0') {
1.179 albertel 698: @Apache::inputtags::response=();
1.159 albertel 699: $result=&Apache::response::mandatory_part_meta;
700: }
1.215 albertel 701: $result.=&Apache::response::meta_part_order();
1.258 albertel 702: $result.=&Apache::response::meta_response_order();
1.159 albertel 703: } elsif ($target eq 'edit') {
704: &Apache::lonxml::debug("in end_problem with $target, edit");
705: $result = &problem_edit_footer();
706: }
1.155 albertel 707:
1.177 albertel 708: if ($ENV{'request.state'} eq 'construct' && $target eq 'web') {
709: &Apache::inputtags::check_for_duplicate_ids();
710: }
1.204 albertel 711:
712: &reset_problem_globals('problem');
1.159 albertel 713:
714: return $result;
1.48 albertel 715: }
716:
1.108 albertel 717:
1.48 albertel 718: sub start_library {
1.159 albertel 719: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
720: my ($result,$head_tag_start,$body_tag_start,$form_tag_start);
1.244 albertel 721: if ($$tagstack[0] eq 'library') {
722: &init_problem_globals('library');
723: $Apache::lonhomework::type='problem';
724: }
1.159 albertel 725: if ($target eq 'edit') {
726: ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
727: &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
728: $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
729: &problem_edit_header();
730: my $temp=&Apache::edit::insertlist($target,$token);
731: $result.=$temp;
732: } elsif ($target eq 'modified') {
733: $result=$token->[4];
734: $result.=&Apache::edit::handle_insert();
735: } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
736: $ENV{'request.state'} eq "construct" ) {
737: ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
738: &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
739: my $name=&get_resource_name($parstack,$safeeval);
740: my $rndseed=&setup_rndseed($safeeval);
741: $result.="$head_tag_start<title>$name</title></head>
1.105 albertel 742: $body_tag_start \n $form_tag_start".
1.159 albertel 743: '<input type="hidden" name="submitted" value="yes" />';
744: $result.=&problem_web_to_edit_header($rndseed);
745: }
746: return $result;
1.48 albertel 747: }
748:
749: sub end_library {
1.159 albertel 750: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
751: my $result='';
752: if ($target eq 'edit') {
753: $result=&problem_edit_footer();
754: } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
755: $ENV{'request.state'} eq "construct") {
756: $result.='</form></body>'.&Apache::lonxml::xmlend();
757: }
1.204 albertel 758: if ($$tagstack[0] eq 'library') { &reset_problem_globals('library') };
1.159 albertel 759: return $result;
1.197 www 760: }
761:
762: sub start_definetag {
763: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
764:
765: my $result;
766:
767: my $name = $token->[2]->{'name'};
768: my $skip=&Apache::lonxml::get_all_text("/definetag",$parser);
769: if ($name=~/^\//) {
770: $result=
771: '<br /><table bgcolor="#FFBBBB"><tr><th>END <tt>'.$name.'</tt></th></tr>';
772: } else {
773: $result=
774: '<br /><table bgcolor="#BBFFBB"><tr><th>BEGIN <tt>'.$name.'</tt></th></tr>';
775: }
776: $skip=~s/\</\<\;/gs;
777: $skip=~s/\>/\>\;/gs;
778: $result.='<tr><td><pre>'.$skip.'</pre></td></tr></table>';
779: return $result;
780: }
781:
782: sub end_definetag {
783: return '';
1.1 albertel 784: }
785:
786: sub start_block {
1.201 albertel 787: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.131 albertel 788:
789: my $result;
1.1 albertel 790:
1.160 albertel 791: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1.131 albertel 792: $target eq 'tex' || $target eq 'analyze') {
1.159 albertel 793: my $code = $token->[2]->{'condition'};
1.201 albertel 794: if (defined($code)) {
1.137 albertel 795: if (!$Apache::lonxml::default_homework_loaded) {
796: &Apache::lonxml::default_homework_load($safeeval);
797: }
1.131 albertel 798: $result = &Apache::run::run($code,$safeeval);
799: &Apache::lonxml::debug("block :$code: returned :$result:");
800: } else {
801: $result='1';
802: }
803: if ( ! $result ) {
1.201 albertel 804: my $skip=&Apache::lonxml::get_all_text("/block",$parser,$style);
1.131 albertel 805: &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
806: }
807: $result='';
808: } elsif ($target eq 'edit') {
809: $result .=&Apache::edit::tag_start($target,$token);
810: $result .=&Apache::edit::text_arg('Test Condition:','condition',
811: $token,40);
812: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
813: } elsif ($target eq 'modified') {
814: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
815: $safeeval,'condition');
816: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.38 albertel 817: }
1.131 albertel 818: return $result;
1.1 albertel 819: }
820:
821: sub end_block {
1.167 www 822: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
823: my $result;
824: if ($target eq "edit") {
825: $result.= &Apache::edit::tag_end($target,$token,'');
826: }
827: return $result;
828: }
829:
830: sub start_languageblock {
1.201 albertel 831: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.167 www 832:
833: my $result;
834:
835: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
836: $target eq 'tex' || $target eq 'analyze') {
1.201 albertel 837: my $include = $token->[2]->{'include'};
838: my $exclude = $token->[2]->{'exclude'};
839: my %languages=&Apache::loncommon::display_languages();
840: $result='1';
841: if ($include) {
842: $result='';
843: foreach (split(/\,/,$include)) {
844: if ($languages{$_}) { $result='1'; }
845: }
846: }
847: if ($exclude) {
848: foreach (split(/\,/,$exclude)) {
849: if ($languages{$_}) { $result='0'; }
850: }
851: }
852: if ( ! $result ) {
853: my $skip=&Apache::lonxml::get_all_text("/languageblock",$parser,
854: $style);
855: &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
856: }
857: $result='';
1.167 www 858: } elsif ($target eq 'edit') {
859: $result .=&Apache::edit::tag_start($target,$token);
1.211 albertel 860: $result .=&Apache::edit::text_arg(&mt('Include Language:'),'include',
1.167 www 861: $token,40);
1.211 albertel 862: $result .=&Apache::edit::text_arg(&mt('Exclude Language:'),'exclude',
1.167 www 863: $token,40);
864: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
865: } elsif ($target eq 'modified') {
866: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.168 albertel 867: $safeeval,'include',
868: 'exclude');
1.167 www 869: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
870: }
871: return $result;
872: }
873:
874: sub end_languageblock {
1.170 www 875: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
876: my $result;
1.201 albertel 877: if ($target eq "edit") {
1.170 www 878: $result.= &Apache::edit::tag_end($target,$token,'');
879: }
880: return $result;
881: }
882:
883: sub start_instructorcomment {
1.201 albertel 884: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.170 www 885:
886: my $result;
887:
888: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
889: $target eq 'tex' || $target eq 'analyze') {
890: $result=($ENV{'request.role'}=~/^(in|cc|au|ca|li)/);
1.269 sakharuk 891: if ( (! $result) or ($ENV{'form.instructor_comments'} eq 'hide')) {
1.201 albertel 892: my $skip=&Apache::lonxml::get_all_text("/instructorcomment",
893: $parser,$style);
1.170 www 894: &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
895: }
896: $result='';
897: } elsif ($target eq 'edit') {
898: $result .=&Apache::edit::tag_start($target,$token);
899: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
900: }
901: return $result;
902: }
903:
904: sub end_instructorcomment {
1.159 albertel 905: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.160 albertel 906: my $result;
907: if ($target eq "edit") {
908: $result.= &Apache::edit::tag_end($target,$token,'');
909: }
910: return $result;
1.4 tsai 911: }
912:
913: sub start_while {
1.159 albertel 914: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
915:
1.160 albertel 916: my $result;
1.161 albertel 917: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1.160 albertel 918: $target eq 'tex' || $target eq 'analyze') {
919: my $code = $token->[2]->{'condition'};
1.4 tsai 920:
1.160 albertel 921: push( @Apache::structuretags::whileconds, $code);
922: if (!$Apache::lonxml::default_homework_loaded) {
923: &Apache::lonxml::default_homework_load($safeeval);
924: }
925: my $result = &Apache::run::run($code,$safeeval);
1.161 albertel 926: my $bodytext=&Apache::lonxml::get_all_text("/while",$parser);
1.160 albertel 927: push( @Apache::structuretags::whilebody, $bodytext);
1.161 albertel 928: push( @Apache::structuretags::whileline, $token->[5]);
929: &Apache::lonxml::debug("s code $code got -$result-");
1.160 albertel 930: if ( $result ) {
931: &Apache::lonxml::newparser($parser,\$bodytext);
932: }
933: } elsif ($target eq 'edit') {
934: $result .=&Apache::edit::tag_start($target,$token);
1.211 albertel 935: $result .=&Apache::edit::text_arg(&mt('Test Condition:'),'condition',
1.160 albertel 936: $token,40);
937: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
938: } elsif ($target eq 'modified') {
939: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
940: $safeeval,'condition');
941: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.159 albertel 942: }
1.160 albertel 943: return $result;
1.4 tsai 944: }
945:
946: sub end_while {
1.159 albertel 947: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.160 albertel 948: my $result;
949:
950: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
951: $target eq 'tex' || $target eq 'analyze') {
952: my $code = pop(@Apache::structuretags::whileconds);
953: my $bodytext = pop(@Apache::structuretags::whilebody);
1.161 albertel 954: my $line = pop(@Apache::structuretags::whileline);
955: my $return = &Apache::run::run($code,$safeeval);
956: my $starttime=time;
957: my $error=0;
958: while ($return) {
959: if (time-$starttime >
960: $Apache::lonnet::perlvar{'lonScriptTimeout'}) {
1.270 albertel 961: #$return = 0; $error=1; next;
1.161 albertel 962: }
963: $result.=&Apache::scripttag::xmlparse($bodytext);
964: $return = &Apache::run::run($code,$safeeval);
965: }
966: if ($error) {
1.270 albertel 967: &Apache::lonxml::error('<pre>'.&mt('Code ran too long. It ran for more than').' '.$Apache::lonnet::perlvar{'lonScriptTimeout'}.' '.&mt('seconds occured while running <while> on line').' '.$line.'</pre>');
1.160 albertel 968: }
969: } elsif ($target eq "edit") {
970: $result.= &Apache::edit::tag_end($target,$token,'');
1.159 albertel 971: }
1.160 albertel 972: return $result;
1.1 albertel 973: }
1.6 tsai 974:
1.160 albertel 975: # <randomlist show="1">
1.6 tsai 976: # <tag1>..</tag1>
977: # <tag2>..</tag2>
978: # <tag3>..</tag3>
1.160 albertel 979: # ...
1.6 tsai 980: # </randomlist>
981: sub start_randomlist {
1.159 albertel 982: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
983: my $result;
984: if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
985: $target eq 'tex' || $target eq 'analyze') {
986: my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
987: my $b_parser= HTML::TokeParser->new(\$body);
988: my $b_tok;
989: my @randomlist;
990: my $list_item;
991: while($b_tok = $b_parser->get_token() ) {
992: if($b_tok->[0] eq 'S') { # start tag
993: # get content of the tag until matching end tag
994: # get all text upto the matching tag
995: # and push the content into @randomlist
996: $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
997: $b_parser);
998: $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
999: push(@randomlist,$list_item);
1000: # print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
1001: # $list_item</b>";
1002: }
1003: if($b_tok->[0] eq 'T') { # text
1004: # what to do with text in between tags?
1005: # print "<b>TEXT $b_tok->[1]</b><br />";
1006: }
1007: # if($b_tok->[0] eq 'E') { # end tag, should not happen
1008: # print "<b>END-TAG $b_tok->[1]</b><br />";
1009: # }
1010: }
1011: my @idx_arr = (0 .. $#randomlist);
1012: &Apache::structuretags::shuffle(\@idx_arr);
1013: my $bodytext = '';
1014: my $show=$#randomlist;
1015: my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
1016: $showarg--;
1017: if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
1018: for(0 .. $show) {
1019: $bodytext .= "$randomlist[ $idx_arr[$_] ]";
1020: }
1021: &Apache::lonxml::newparser($parser,\$bodytext);
1022: } elsif ($target eq 'edit' ) {
1023: $result .=&Apache::edit::tag_start($target,$token);
1024: $result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
1025: $token,5);
1026: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1027: } elsif ($target eq 'modified' ) {
1028: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1029: $safeeval,'show');
1030: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1031: }
1032: return $result;
1.7 tsai 1033: }
1034:
1035: sub shuffle {
1036: my $a=shift;
1037: my $i;
1.70 albertel 1038: if (defined(@$a)) {
1.251 albertel 1039: &Apache::response::pushrandomnumber();
1.159 albertel 1040: for($i=@$a;--$i;) {
1041: my $j=int(&Math::Random::random_uniform() * ($i+1));
1042: next if $i == $j;
1043: @$a[$i,$j] = @$a[$j,$i];
1044: }
1.251 albertel 1045: &Apache::response::poprandomnumber();
1.7 tsai 1046: }
1.6 tsai 1047: }
1048:
1049: sub end_randomlist {
1.159 albertel 1050: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1051: my $result;
1052: if ($target eq 'edit' ) {
1053: $result=&Apache::edit::tag_end($target,$token,
1054: 'End Randomly Parsed Block');
1055: }
1056: return $result;
1.6 tsai 1057: }
1058:
1.11 albertel 1059: sub start_part {
1.159 albertel 1060: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1061: my $result='';
1062: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
1063: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
1064: $Apache::inputtags::part=$id;
1.177 albertel 1065: push(@Apache::inputtags::partlist,$id);
1066: @Apache::inputtags::response=();
1.159 albertel 1067: @Apache::inputtags::previous=();
1068: @Apache::inputtags::previous_version=();
1.241 albertel 1069: $Apache::lonhomework::problemstatus=&get_problem_status($id);
1.159 albertel 1070: my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
1.259 albertel 1071: my $newtype=&Apache::lonnet::EXT("resource.$id.type");
1072: if ($newtype) { $Apache::lonhomework::type=$newtype; }
1073:
1.214 albertel 1074: my $expression='$external::part=\''.$Apache::inputtags::part.'\';';
1.259 albertel 1075: $expression.='$external::type=\''.$Apache::lonhomework::type.'\';';
1.209 albertel 1076: &Apache::run::run($expression,$safeeval);
1.159 albertel 1077:
1078: if ($target eq 'meta') {
1.224 www 1079: my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
1080: return &Apache::response::mandatory_part_meta.
1081: &Apache::response::meta_parameter_write('display','string',$display,'Part Description');
1.159 albertel 1082: } elsif ($target eq 'web' || $target eq 'grade' ||
1083: $target eq 'answer' || $target eq 'tex') {
1084: if ($hidden) {
1085: my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
1086: } else {
1087: my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
1088: push (@Apache::inputtags::status,$status);
1089: my $expression='$external::datestatus="'.$status.'";';
1090: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
1091: &Apache::run::run($expression,$safeeval);
1.240 albertel 1092: if ($ENV{'request.state'} eq 'construct') {
1.241 albertel 1093: &set_problem_state($Apache::inputtags::part);
1.240 albertel 1094: }
1.216 albertel 1095: if (( $status eq 'CLOSED' ) ||
1096: ( $status eq 'UNCHECKEDOUT') ||
1.252 albertel 1097: ( $status eq 'NOT_YET_VIEWED') ||
1.216 albertel 1098: ( $status eq 'BANNED') ||
1099: ( $status eq 'UNAVAILABLE') ||
1100: ( $status eq 'INVALID_ACCESS')) {
1.159 albertel 1101: my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
1102: if ( $target eq "web" ) {
1.211 albertel 1103: $result="<br />".&mt('Part is not open to be viewed. It')." $accessmsg<br />";
1.159 albertel 1104: } elsif ( $target eq 'tex' ) {
1.195 sakharuk 1105: if (not $ENV{'form.problem_split'}=~/yes/) {
1.211 albertel 1106: $result="\\end{minipage}\\vskip 0 mm ".&mt('Part is not open to be viewed. It')." $accessmsg \\\\\\begin{minipage}{\\textwidth}";
1.195 sakharuk 1107: } else {
1.211 albertel 1108: $result="\\vskip 0 mm ".&mt('Part is not open to be viewed. It')." $accessmsg \\\\";
1.195 sakharuk 1109: }
1.159 albertel 1110: }
1111: } else {
1112: if ($target eq 'tex') {
1.195 sakharuk 1113: if (not $ENV{'form.problem_split'}=~/yes/) {
1.264 sakharuk 1114: if ($$tagstack[-2] eq 'td') {
1115: $result.='\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
1116: } else {
1117: $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
1118: }
1.195 sakharuk 1119: }
1.159 albertel 1120: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.230 albertel 1121: my $allkeys=&Apache::lonnet::metadata($ENV{'request.uri'},'packages');
1.222 sakharuk 1122: my @allkeys = split /,/,$allkeys;
1123: my $allow_print_points = 0;
1124: foreach my $partial_key (@allkeys) {
1.230 albertel 1125: if ($partial_key=~m/^part_(.*)$/) {
1.222 sakharuk 1126: if ($1 ne '0') {$allow_print_points=1;}
1127: }
1128: }
1129: if (($Apache::lonhomework::type eq 'exam') && ($allow_print_points)) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
1.233 www 1130: } elsif ($target eq 'web') {
1131: $result.='<a name="'.&Apache::lonnet::escape($Apache::inputtags::part).'" />';
1.159 albertel 1132: }
1133: }
1134: }
1135: } elsif ($target eq 'edit') {
1136: $result.=&Apache::edit::tag_start($target,$token);
1137: $result.=&Apache::edit::text_arg('Part ID:','id',$token).
1138: &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
1.224 www 1139: ' '.
1140: &Apache::edit::text_arg('Displayed Part Description:','display',$token).
1.159 albertel 1141: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1142: } elsif ($target eq 'modified') {
1143: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.225 albertel 1144: $safeeval,'id','display');
1.159 albertel 1145: if ($constructtag) {
1.225 albertel 1146: #limiting ids to only letters numbers, and space
1.224 www 1147: $token->[2]->{'id'}=~s/[^A-Za-z0-9 ]//gs;
1.159 albertel 1148: $result = &Apache::edit::rebuild_tag($token);
1149: $result.=&Apache::edit::handle_insert();
1150: }
1151: }
1152: return $result;
1.11 albertel 1153: }
1154:
1155: sub end_part {
1.159 albertel 1156: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1157: &Apache::lonxml::debug("in end_part $target ");
1158: my $status=$Apache::inputtags::status['-1'];
1159: my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
1160: my $result='';
1161: if ( $target eq 'meta' ) {
1162: $result='';
1.249 albertel 1163: } elsif ($target eq 'grade') {
1164: if (($status eq 'CAN_ANSWER' || $Apache::lonhomework::scantronmode) &&
1165: !$hidden) {
1166: $result=&Apache::inputtags::grade;
1167: } else {
1168: # move any submission data to .hidden
1169: &Apache::inputtags::hidealldata($Apache::inputtags::part);
1170: }
1.159 albertel 1171: } elsif (($target eq 'web' || $target eq 'tex') && !$hidden ) {
1172: my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
1173: $target);
1.212 albertel 1174: if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
1175: $gradestatus='';
1176: }
1.159 albertel 1177: $result=$gradestatus;
1.265 sakharuk 1178: if ($$tagstack[-2] eq 'td' and $target eq 'tex') {$result.='\end{minipage}';}
1.181 albertel 1179: } elsif ($target eq 'edit') {
1180: $result=&Apache::edit::end_table();
1.159 albertel 1181: }
1182: pop @Apache::inputtags::status;
1183: $Apache::inputtags::part='';
1184: return $result;
1.11 albertel 1185: }
1.1 albertel 1186:
1.25 albertel 1187: sub start_preduedate {
1.159 albertel 1188: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1189: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.236 albertel 1190: &Apache::lonxml::debug("State in preduedate is ". $Apache::inputtags::status['-1']);
1.159 albertel 1191: if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
1.236 albertel 1192: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
1193: &Apache::lonxml::debug("Wha? ". ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER'));
1.159 albertel 1194: &Apache::lonxml::get_all_text("/preduedate",$parser);
1195: }
1.24 albertel 1196: }
1.159 albertel 1197: return '';
1.24 albertel 1198: }
1199:
1.25 albertel 1200: sub end_preduedate {
1.159 albertel 1201: return '';
1.24 albertel 1202: }
1203:
1.25 albertel 1204: sub start_postanswerdate {
1.159 albertel 1205: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.234 albertel 1206: if ($target eq 'web' || $target eq 'grade') {
1.159 albertel 1207: if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
1208: &Apache::lonxml::get_all_text("/postanswerdate",$parser);
1209: }
1210: } elsif ($target eq 'tex') {
1.234 albertel 1211: &Apache::lonxml::get_all_text("/postanswerdate",$parser);
1.159 albertel 1212: }
1213: return '';
1.24 albertel 1214: }
1215:
1.25 albertel 1216: sub end_postanswerdate {
1.159 albertel 1217: return '';
1.24 albertel 1218: }
1219:
1.25 albertel 1220: sub start_notsolved {
1.159 albertel 1221: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1222: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1223: $target eq 'tex') {
1224: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
1225: &Apache::lonxml::debug("not solved has :$gradestatus:");
1.239 albertel 1226: if ($gradestatus =~ /^correct/ &&
1227: &Apache::response::show_answer()) {
1.159 albertel 1228: &Apache::lonxml::debug("skipping");
1229: &Apache::lonxml::get_all_text("/notsolved",$parser);
1230: }
1.24 albertel 1231: }
1.159 albertel 1232: return '';
1.24 albertel 1233: }
1234:
1.25 albertel 1235: sub end_notsolved {
1.159 albertel 1236: return '';
1.24 albertel 1237: }
1238:
1239: sub start_solved {
1.159 albertel 1240: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1241: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1242: $target eq 'tex') {
1243: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
1.239 albertel 1244: if ($gradestatus !~ /^correct/ ||
1245: !&Apache::response::show_answer()) {
1.159 albertel 1246: &Apache::lonxml::get_all_text("/solved",$parser);
1247: }
1.24 albertel 1248: }
1.159 albertel 1249: return '';
1.24 albertel 1250: }
1251:
1252: sub end_solved {
1.248 albertel 1253: return '';
1254: }
1255:
1256: sub start_problemtype {
1257: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1258: my $result;
1259: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1260: $target eq 'tex' || $target eq 'analyze') {
1261: my $mode=lc(&Apache::lonxml::get_param('mode',$parstack,$safeeval));
1262: if (!defined($mode)) { $mode='show'; }
1263: my $for=&Apache::lonxml::get_param('for',$parstack,$safeeval);
1264: my $found=0;
1265: foreach my $type (split(',',$for)) {
1266: if ($Apache::lonhomework::type eq lc($type)) { $found=1; }
1267: }
1268: if ($mode eq 'show' && !$found) {
1269: &Apache::lonxml::get_all_text("/problemtype",$parser);
1270: }
1271: if ($mode eq 'hide' && $found) {
1272: &Apache::lonxml::get_all_text("/problemtype",$parser);
1273: }
1274: } elsif ($target eq 'edit') {
1275: $result .=&Apache::edit::tag_start($target,$token);
1276: $result.=&Apache::edit::select_arg('Mode:','mode',
1277: [['show','Show'],
1278: ['hide','Hide']]
1279: ,$token);
1280: $result .=&Apache::edit::checked_arg('When used as type(s):','for',
1281: [ ['exam','Exam/Quiz Problem'],
1282: ['survey','Survey'],
1283: ['problem','Homework Problem'] ]
1284: ,$token);
1285: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1286: } elsif ($target eq 'modified') {
1287: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1288: $safeeval,'mode','for');
1289: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1290: }
1291: return $result;
1292: }
1293:
1294: sub end_problemtype {
1.159 albertel 1295: return '';
1.24 albertel 1296: }
1.34 albertel 1297:
1298: sub start_startouttext {
1.159 albertel 1299: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1300: my @result=(''.'');
1301: if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
1302: return (@result);
1.34 albertel 1303: }
1.159 albertel 1304:
1.34 albertel 1305: sub end_startouttext {
1.159 albertel 1306: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1307: my $result='';
1308: my $text='';
1309:
1310: if ($target eq 'edit') {
1311: $text=&Apache::lonxml::get_all_text("endouttext",$parser);
1.211 albertel 1312: $result.=&Apache::edit::start_table($token)."<tr><td>".&mt('Text Block')."</td>
1313: <td>".&mt('Delete:').
1.159 albertel 1314: &Apache::edit::deletelist($target,$token)
1315: ."</td>
1.42 albertel 1316: <td>".
1.159 albertel 1317: &Apache::edit::insertlist($target,$token).
1318: &Apache::edit::end_row().
1319: &Apache::edit::start_spanning_row()."\n"
1.188 bowersj2 1320: . &Apache::loncommon::helpLatexCheatsheet () .
1.255 www 1321: &Apache::edit::editfield($token->[1],$text,"",80,8,1);
1.159 albertel 1322: }
1323: if ($target eq 'modified') {
1.219 albertel 1324: $result='<startouttext />'.&Apache::edit::modifiedfield("endouttext",$parser);
1.159 albertel 1325: }
1326: if ($target eq 'tex') {
1327: $result .= '\noindent ';
1328: }
1329: return $result;
1.34 albertel 1330: }
1.159 albertel 1331:
1.34 albertel 1332: sub start_endouttext {
1.159 albertel 1333: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1334: my $result='';
1335: if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
1336: if ($target eq "modified") {
1337: $result='<endouttext />'.
1338: &Apache::edit::handle_insertafter('startouttext'); }
1339: return $result;
1.34 albertel 1340: }
1.159 albertel 1341:
1.34 albertel 1342: sub end_endouttext {
1.159 albertel 1343: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1344: my @result=('','');
1345: if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
1346: return (@result);
1.34 albertel 1347: }
1.159 albertel 1348:
1.45 albertel 1349: sub delete_startouttext {
1.159 albertel 1350: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1351: # my $text=&Apache::lonxml::get_all_text("endouttext",$parser);
1352: my $text=$$parser['-1']->get_text("/endouttext");
1353: my $ntoken=$$parser['-1']->get_token();
1354: &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
1355: &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
1356: # Deleting 2 parallel tag pairs, but we need the numbers later to look like
1357: # they did the last time round
1358: &Apache::lonxml::increasedepth($ntoken);
1359: &Apache::lonxml::decreasedepth($ntoken);
1360: return 1;
1.193 www 1361: }
1362:
1363: sub start_simpleeditbutton {
1364: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1365: my $result='';
1366: if (($target eq 'web') &&
1367: (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
1.220 www 1368: my $url=$ENV{'request.noversionuri'};
1.193 www 1369: $url=~s/\?.*$//;
1.194 www 1370: $result='<table width="100%" bgcolor="#FFFFAA" border="2"><tr><td>'.
1.211 albertel 1371: '<a href="'.$url.'/smpedit">'.&mt('Simple Problem Editor').'</a> - '.&mt('Note: it can take up to 10 minutes for changes to take effect for all users.').
1.196 www 1372: &Apache::loncommon::help_open_topic('Caching').'</td></tr></table><br />';
1.193 www 1373: }
1374: return $result;
1375: }
1376:
1377: sub end_simpleeditbutton {
1378: return '';
1.45 albertel 1379: }
1.34 albertel 1380:
1.1 albertel 1381: 1;
1382: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>