File:  [LON-CAPA] / loncom / homework / chemresponse.pm
Revision 1.66: download - view: text, annotated - select for diffs
Tue May 30 12:45:36 2006 UTC (17 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
&Apache::lonnet::unescape -> &unescape
&Apache::lonnet::escape -> &escape

# The LearningOnline Network with CAPA
# chemical equation style response
#
# $Id: chemresponse.pm,v 1.66 2006/05/30 12:45:36 www Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
#
package Apache::chemresponse;
use strict;
use Apache::lonxml;
use Apache::lonnet;
use Apache::lonlocal;
use lib '/home/httpd/lib/perl/';
use LONCAPA;
 

BEGIN {
    &Apache::lonxml::register('Apache::chemresponse',('organicresponse','organicstructure','reactionresponse','chem'));
}

sub chem_standard_order {
    my ($reaction) = @_;
    my ($re,$pr) = split(/->|<=>/,$reaction);
    my @reactants = split(/\s\+/,$re);
    my @products =  split(/\s\+/,$pr);
    foreach my $substance (@reactants,@products) {
	$substance =~ s/(\^\d*)\s+/$1_/g;         # protect superscript space
	$substance =~ s/\s*//g;                   # strip whitespace
	$substance =~ s/_/ /g;                    # restore superscript space
    }
    @reactants = sort @reactants;
    @products = sort @products;
    my $standard = '';
    foreach my $substance (@reactants) {
	$standard .= $substance;
	$standard .= ' + ';
    }
    $standard =~ s/ \+ $//;              # get rid of trailing plus sign
    $standard .= ' -> ';
    foreach my $substance (@products) {
	$standard .= $substance;
	$standard .= ' + ';
    }
    $standard =~ s/ \+ $//;              # get rid of trailing plus sign
    return $standard;
}

sub separate_jme_window {
    my ($smile_input,$jme_input,$molecule,$options,$shown_text)=@_;
    my $smilesection;
    if (defined($smile_input)) {
	$smilesection=<<SMILESECTION;
        smiles = document.applets.JME.smiles();
	opener.document.lonhomework.$smile_input.value = smiles;
SMILESECTION
    }
    my $jmesection;
    if (defined($jme_input)) {
	$jmesection=<<JMESECTION;
	jmeFile = document.applets.JME.jmeFile();
	opener.document.lonhomework.$jme_input.value = jmeFile;
JMESECTION
    }

    if ($molecule) { $molecule='<param name="jme" value="'.$molecule.'" />'; }
    my $insert_answer;
    if ($shown_text eq '') { 
	$insert_answer=
	    '<input type="button" name="submit" value="Insert Answer" onclick="javascript:submitSmiles();" />';
    }

    my $js = <<CHEMJS;
<script type="text/javascript">
function submitSmiles() {
    jmeFile = document.applets.JME.jmeFile();
    if (jmeFile == "") {
	alert("Nothing to submit");
    } else {
        $jmesection
        $smilesection
	window.close();
    }
}
function openHelpWindow() {
    window.open("/adm/jme/jme_help.html","","scrollbars=yes,resizable=yes,width=500,height=600");
}
function substituent(r) {document.applets.JME.setSubstituent(r);}
</script>
CHEMJS

    my $start_page = 
        &Apache::loncommon::start_page('Molecule Editor',$js,
				       {'only_body' => 1,
					'bgcolor'   => '#FFFFFF',});
    my $end_page = 
	&Apache::loncommon::end_page();

    my $body=<<CHEMPAGE;
$start_page
<center>
<form>
  <table width="440"><tr>
    <td></td>
    <td align="right">
      <select onchange="javascript:substituent(options[selectedIndex].text)">
        <option>Select substituent</option>
        <option>-C(=O)OH</option>
        <option>-C(=O)OMe</option>
        <option>-OC(=O)Me</option>
        <option>-CMe3</option>
        <option>-CF3</option>
        <option>-CCl3</option>
        <option>-NO2</option>
        <option>-SO2-NH2</option>
        <option>-NH-SO2-Me</option>
        <option>-NMe2</option>
        <option>-C#N</option>
        <option>-C#C-Me</option>
        <option>-C#CH</option>
      </select>
    </td></tr>
  </table>
<applet code="JME.class" name="JME" archive="/adm/jme/JME.jar" width="440" height="390" mayscript>
You have to enable Java and JavaScript on your machine.
$molecule
<param name="options" value="$options" />
</applet><br />
<font face="arial,helvetica,sans-serif" size="-1"><a href="http://www.molinspiration.com/jme/index.html">JME Editor</a> courtesy of Peter Ertl, Novartis</font>
<br />
$insert_answer
<br />
<input type="button" value="  Close  " onclick = "javascript:window.close()" />
&nbsp;&nbsp;
<input type="button" value="  Help  " onclick = "javascript:openHelpWindow()" />
</form>
</center>
$end_page
CHEMPAGE

    $body=&HTML::Entities::encode($body,'<>&"');
    $body=~s/\n/ /g;
    my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
    my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
    my $display=&mt('Draw Molecule');
    if (defined($shown_text)) { $display=&mt($shown_text); }
    my $result=<<CHEMINPUT;
<input type="button" value="$display" onclick="javascript:editor=window.open($nothing,'jmeedit','width=500,height=500,menubar=no,scrollbars=no,resizable=yes');editor.$docopen;editor.document.write('$body');editor.document.close();editor.focus()" />
CHEMINPUT
    return $result;
}

sub jme_img {
    my ($jme,$smile,$width,$options)=@_;
    my $id=&Apache::loncommon::get_cgi_id();
    my $result='<img alt="'.$smile.'" src="/cgi-bin/convertjme.pl?'.$id.'"';
    if ($options =~ /border/) { $result.= ' border="1"'; }
    $result.=' />';
    &Apache::lonnet::appenv('cgi.'.$id.'.JME'   =>
			    &escape($jme),
			    'cgi.'.$id.'.PNG'   => 1,
			    'cgi.'.$id.'.WIDTH' => $width);
    return $result;
}

sub start_organicresponse {
    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
    my $result;
    my $partid = $Apache::inputtags::part;
    my $id = &Apache::response::start_response($parstack,$safeeval);
    if ($target eq 'meta') {
	$result=&Apache::response::meta_package_write('organicresponse');
    } elsif ($target eq 'web') {
	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
						 $safeeval);
	if (  &Apache::response::show_answer() && $jmeanswer ne '') {
	    my $options=&Apache::lonxml::get_param('options',$parstack,
						   $safeeval);
	    my $width=&Apache::lonxml::get_param('width',$parstack,
						   $safeeval);
	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
							 $safeeval);
	    $result.=&jme_img($jmeanswer,$answers[0],$width,$options);
	} else {
	    my $molecule;
	    if (defined($Apache::lonhomework::history{"resource.$partid.$id.molecule"})) {
		$molecule=$Apache::lonhomework::history{"resource.$partid.$id.molecule"};
	    } else {
		$molecule=&Apache::lonxml::get_param('molecule',$parstack,
						     $safeeval);
	    }
	    my $options=&Apache::lonxml::get_param('options',$parstack,
						   $safeeval);
	    my $shown_text;
	    if (&Apache::response::show_answer()) {
		$shown_text="Show Your Last Answer";
	    }
	    $result=&separate_jme_window("HWVAL_$id","MOLECULE_$id",$molecule,
					 $options,$shown_text);
	    $result.= '<input type="hidden" name="MOLECULE_'.$id.'" value="" />';
	}
    } elsif ($target eq 'edit') {
	$result .=&Apache::edit::tag_start($target,$token);
	my $options=&Apache::lonxml::get_param('options',$parstack,
					       $safeeval);
	if ($options !~ /multipart/) { $options.=',multipart'; }
	$result .='<nobr>'.
	    &Apache::edit::text_arg('Starting Molecule:','molecule',
				    $token,40);
	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
						$safeeval);
	$result .=&separate_jme_window(undef,
		      &Apache::edit::html_element_name('molecule'),
		      $molecule,$options);
	$result .='</nobr><br /><nobr>';
	$result .=&Apache::edit::text_arg('Correct Answer:','answer',
					  $token,40);
	$result .=&Apache::edit::hidden_arg('jmeanswer',$token);
	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
						 $safeeval);
	$result .=&separate_jme_window(
                      &Apache::edit::html_element_name('answer'),
                      &Apache::edit::html_element_name('jmeanswer'),
		      $jmeanswer,$options);
	$result .='</nobr><br />';
	$result .=&Apache::edit::checked_arg('Options:','options',
				    [ ['autoez','Auto E,Z stereochemistry'],
				      ['multipart','Multipart Structures'],
				      ['nostereo','No stereochemistry'],
				      ['reaction','Is a reaction'],
				      ['number','Able to number atoms'] ],
					     ,$token);
	$result .=&Apache::edit::text_arg('Width of correct answer image:',
					  'width',$token,10);
	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
    } elsif ($target eq 'modified') {
	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
						     $safeeval,'molecule',
						     'answer','jmeanswer',
						     'options','width');
	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
    }

    return $result;
}

sub end_organicresponse {
    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
    my $result;

    my $partid = $Apache::inputtags::part;
    my $id = $Apache::inputtags::response['-1'];

    if ($target eq 'grade' 
	&& &Apache::response::submitted()
	&& $Apache::lonhomework::type eq 'exam') {

	&Apache::response::scored_response($partid,$id);

    } elsif ($target eq 'grade' 
	&& &Apache::response::submitted()
	&& $Apache::lonhomework::type ne 'exam') {

	&Apache::response::setup_params($$tagstack[-1],$safeeval);
	my $response = &Apache::response::getresponse();
	if ( $response =~ /[^\s]/) {
	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
	    my $ad;
	    foreach my $answer (@answers) {
		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
		if ($response eq $answer) {
		    $ad='EXACT_ANS';
		    last;
		} else {
		    $ad='INCORRECT';
		}
	    }
	    if ($ad && $Apache::lonhomework::type eq 'survey') {
		$ad='SUBMITTED';
	    }
	    &Apache::response::handle_previous(\%previous,$ad);
	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
	    $Apache::lonhomework::results{"resource.$partid.$id.molecule"}=$env{"form.MOLECULE_$id"};
	}
    } elsif ($target eq "edit") {
	$result.= &Apache::edit::tag_end($target,$token,'');
    } elsif ($target eq 'answer') {
	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
						     $safeeval);
	$result.=&Apache::response::answer_header('organicresponse');
	foreach my $answer (@answers) {
	    $result.=&Apache::response::answer_part('organicresponse',$answer);
	}
	$result.=&Apache::response::answer_footer('organicresponse');
    }

    if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
	$target eq 'tex' || $target eq 'analyze') {
	&Apache::lonxml::increment_counter(&Apache::response::repetition());
    }
    &Apache::response::end_response();
    return $result;
}

sub start_organicstructure {
    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
    my $result;
    if ($target eq 'web') {
	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
	my $id=&Apache::loncommon::get_cgi_id();
	$result="<img src='/cgi-bin/convertjme.pl?$id'";
	if ($options =~ /border/) { $result.= ' border="1"'; }
	$result.=' />';
	&Apache::lonnet::appenv(
            'cgi.'.$id.'.JME'   => &escape($molecule),
	    'cgi.'.$id.'.PNG' => 1,
	    'cgi.'.$id.'.WIDTH' => $width );
    } elsif ($target eq 'tex') {
	my $texwidth=&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1);
	my $webwidth=&Apache::lonxml::get_param('width', $parstack, $safeeval);
	my $webheight=&Apache::lonxml::get_param('height', $parstack, $safeeval);
	if (!$webheight) { $webheight = $webwidth; }
	if (!$texwidth) { $texwidth='90'; }
	$result = "%DYNAMICIMAGE:$webwidth:$webheight:$texwidth\n";
	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,$safeeval);
	my $options=&Apache::lonxml::get_param('options',$parstack,$safeeval);
	my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
	    '_'.time.'_'.$$.int(rand(1000)).'_organicstructure';
	my $id=$filename;
	&Apache::lonnet::appenv(
		     'cgi.'.$id.'.JME'   => &escape($molecule),
		     'cgi.'.$id.'.PS' => 1,
		     'cgi.'.$id.'.WIDTH' => $texwidth );
	$id=&escape($id);
	&Apache::lonxml::register_ssi("/cgi-bin/convertjme.pl?$id");
	if ($options =~ /border/) { $result.= '\fbox{'; }
	$result .= '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$texwidth.' mm]{'.$filename.'.eps}';
	if ($options =~ /border/) { $result.= '} '; }
    } elsif ($target eq 'edit') {
	$result .=&Apache::edit::tag_start($target,$token);
	$result .=&Apache::edit::text_arg('Width (pixels):','width',$token,5);
	$result .=&Apache::edit::text_arg('TeXwidth (mm):','texwidth',$token,5);
	$result .='<nobr>';
	$result .=&Apache::edit::text_arg('Molecule:','molecule',$token,40);
	my $molecule=&Apache::lonxml::get_param('molecule',$parstack,
						$safeeval);
	my $options=&Apache::lonxml::get_param('options',$parstack,
					       $safeeval);
	if ($options !~ /reaction/) {
	    $options.= ',multipart,number';
	}
						   
	$result .=&separate_jme_window(undef,
				 &Apache::edit::html_element_name('molecule'),
				       $molecule,$options);
	$result.="</nobr><br />";
	$result .=&Apache::edit::checked_arg('Options:','options',
					     [ ['reaction','Is a reaction'],
					       ['border','Draw a border'] ],
					     $token);
	$result .=&Apache::edit::end_row();
    } elsif ($target eq 'modified') {
	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
						     $safeeval,'molecule',
						     'width','texwidth',
						     'options');
	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
    }
    return $result;
}

sub end_organicstructure {
    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
    my $result;
    if ($target eq "edit") {
	$result.= &Apache::edit::tag_end($target,$token,'');
    }
    return $result;
}

sub edit_reaction_button {
    my ($id,$field,$reaction)=@_;
    my $id_es=&escape($id);
    my $field_es=&escape($field);
    my $reaction_es=&escape($reaction);
    my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
    my $start_page = 
	&Apache::loncommon::start_page('LON-CAPA Reaction Editor',undef,
				       {'frameset'    => 1,
					'js_ready'    => 1,
					'add_entries' => {
					    'rows'   => "30%",
					    'border' => "0",}},);
    my $end_page = 
	&Apache::loncommon::end_page({'frameset' => 1,
				      'js_ready' => 1});
    my $result=<<EDITREACTION;
<script type="text/javascript">
// <!--
    function create_reaction_window_${id}_${field} () {
	editor=window.open('','','width=500,height=270,scrollbars=no,resizable=yes');
	editor.$docopen;
	editor.document.writeln('$start_page <frame src="/res/adm/pages/reactionresponse/reaction_viewer.html?inhibitmenu=yes" name="viewer" scrolling="no" />  <frame src="/res/adm/pages/reactionresponse/reaction_editor.html?inhibitmenu=yes&reaction=$reaction_es&id=$id_es&field=$field_es" name="editor" scrolling="no" /> $end_page');
    }
// -->
</script>
<input type='button' value='Edit Answer' onclick="javascript:create_reaction_window_${id}_${field}();void(0);" />
EDITREACTION
    return $result;
}

sub start_reactionresponse {
    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
    my $result;
    my $id = &Apache::response::start_response($parstack,$safeeval);
    if ($target eq 'meta') {
	$result=&Apache::response::meta_package_write('reactionresponse');
    } elsif ($target eq 'web') {
	my $partid = $Apache::inputtags::part;
	my $id = $Apache::inputtags::response['-1'];
	my $reaction=$Apache::lonhomework::history{"resource.$partid.$id.submission"};
	if ($reaction eq '') {  $reaction=&Apache::lonxml::get_param('initial',$parstack,$safeeval); }
	my $status=$Apache::inputtags::status['-1'];
	if ($status eq 'CAN_ANSWER') {
	    $result.=&edit_reaction_button($id,"HWVAL_$id",$reaction);
	}
	if (  &Apache::response::show_answer() ) {
	    my $ans=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
	    if (!$Apache::lonxml::default_homework_loaded) {
		&Apache::lonxml::default_homework_load($safeeval);
	    }
	    @Apache::scripttag::parser_env = @_;
	    $Apache::inputtags::answertxt{$id}=&Apache::run::run("return &chemparse(q\0$ans\0);",$safeeval);
	}
    } elsif ($target eq "edit") {
	$result .=&Apache::edit::tag_start($target,$token);
	my $answer=&Apache::lonxml::get_param('answer',$parstack,
						$safeeval);
	$result .='<nobr>'.
	    &Apache::edit::text_arg('Answer:','answer',$token,40);
	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('answer'),$answer).'</nobr>';
	my $initial=&Apache::lonxml::get_param('initial',$parstack,$safeeval);
	$result.='<nobr>'.
	    &Apache::edit::text_arg('Initial Reaction:','initial',$token,40);
	$result .=&edit_reaction_button($id,&Apache::edit::html_element_name('initial'),$initial).'</nobr>';
	
	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
    }  elsif ($target eq 'modified') {
	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
						     $safeeval,'answer',
						     'initial');
	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
    }
    return $result;
}

sub end_reactionresponse {
    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
    my $result;

    my $partid = $Apache::inputtags::part;
    my $id = $Apache::inputtags::response['-1'];

    if ($target eq 'grade' 
	&& &Apache::response::submitted()
	&& $Apache::lonhomework::type eq 'exam') {

	&Apache::response::scored_response($partid,$id);

    } elsif ($target eq 'grade' 
	&& &Apache::response::submitted()
	&& $Apache::lonhomework::type ne 'exam') {

	&Apache::response::setup_params($$tagstack[-1],$safeeval);
	my $response = &Apache::response::getresponse();
	if ( $response =~ /[^\s]/) {
	    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
	    my %previous = &Apache::response::check_for_previous($response,$partid,$id);
	    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
	    my $ad;
	    foreach my $answer (@answers) {
		&Apache::lonxml::debug("submitted a $response for $answer<br \>\n");
		if (&chem_standard_order($response) eq 
		    &chem_standard_order($answer)) {
		    $ad='EXACT_ANS';
		} else {
		    $ad='INCORRECT';
		}
	    }
	    if ($ad && $Apache::lonhomework::type eq 'survey') {
		$ad='SUBMITTED';
	    }
	    &Apache::response::handle_previous(\%previous,$ad);
	    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
	}
    }  elsif ($target eq "edit") {
	$result.= &Apache::edit::tag_end($target,$token,'');
    } elsif ($target eq 'answer') {
	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
						     $safeeval);
	$result.=&Apache::response::answer_header('reactionresponse');
	foreach my $answer (@answers) {
	    $result.=&Apache::response::answer_part('reactionresponse',
						    $answer);
	}
	$result.=&Apache::response::answer_footer('reactionresponse');
    }

    if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
	$target eq 'tex' || $target eq 'analyze') {
	&Apache::lonxml::increment_counter(&Apache::response::repetition());
    }
    &Apache::response::end_response();
    return $result;
}

sub start_chem {
    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
    my $result = '';
    my $inside = &Apache::lonxml::get_all_text_unbalanced("/chem",$parser);
    if ($target eq 'tex' || $target eq 'web') {
	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
	if (!$Apache::lonxml::default_homework_loaded) {
	    &Apache::lonxml::default_homework_load($safeeval);
	}
	@Apache::scripttag::parser_env = @_;
	$result=&Apache::run::run("return &chemparse(q\0$inside\0);",$safeeval);
    }    
    return $result;
}

sub end_chem {
    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
    my $result = '';
    return $result;
}

1;
__END__

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