[LON-CAPA-cvs] cvs: loncom /homework inputtags.pm

foxr foxr at source.lon-capa.org
Mon Sep 24 06:34:19 EDT 2012


foxr		Mon Sep 24 10:34:19 2012 EDT

  Modified files:              
    /loncom/homework	inputtags.pm 
  Log:
  Support internationalized spellchecking.
  
  
  
Index: loncom/homework/inputtags.pm
diff -u loncom/homework/inputtags.pm:1.309 loncom/homework/inputtags.pm:1.310
--- loncom/homework/inputtags.pm:1.309	Fri Sep 14 11:23:58 2012
+++ loncom/homework/inputtags.pm	Mon Sep 24 10:34:19 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # input  definitons
 #
-# $Id: inputtags.pm,v 1.309 2012/09/14 11:23:58 goltermann Exp $
+# $Id: inputtags.pm,v 1.310 2012/09/24 10:34:19 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -125,6 +125,37 @@
     %Apache::inputtags::submission_display=();
 }
 
+#
+#  provides the onblur binding for spellchecking.  This could be an
+#  empty string if spellchecking was not enabled.
+#  Jquery selector binding is done rather than setting an onblur
+#  attribute because we'll need to set the element's spellcheck language
+#  option dynamically so we need $(this) to be defined.
+#
+# @param id   - The element id to bind.
+# @param lang - Language in which spellchecking is desired.
+#               if undef, nothing is generated.  
+# @return string - onblur specification to do the requested spellchecking.
+#
+sub spellcheck_onblur {
+    my ($id, $lang) = @_;
+    my $result = '';
+    if ($lang) {
+
+	$result = <<JAVASCRIPT;
+<script type="text/javascript">
+\$('\#$id').blur(function() {
+    doSpellcheck('\#$id', '$lang');
+ });
+</script>
+
+JAVASCRIPT
+
+
+    }
+    return $result;
+}
+
 sub check_for_duplicate_ids {
     my %check;
     foreach my $id (@Apache::inputtags::partlist,
@@ -194,13 +225,13 @@
 	    if ( $rows eq '') { $rows = 16; }
 	    my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
 	    $result='';
+	    my $tagident = 'HWVAL_' . $resid;
 	    if ($addchars) {
-		$result.=&addchars('HWVAL_'.$resid,$addchars);
+		$result.=&addchars($tagident, $addchars);
 	    }
             my $textareaclass = 'class="LC_richDetectHtml spellchecked"';
-	    $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" ' .
+	    $result.= '<textarea wrap="hard" name="'.$tagident.'" id="'.$tagident.'" ' .
 		      'rows="'.$rows.'" cols="'.$cols.'" '.$textareaclass
-		      . "onblur='\$(this).spellchecker(\"check\")'"
 		      .'>'.
                       &HTML::Entities::encode($oldresponse,'<>&"');
 	    if ($oldresponse ne '') {
@@ -268,9 +299,14 @@
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
     my $result;
     if ($target eq 'web') {
+	my $spellcheck = &Apache::lonxml::get_param('spellcheck', $parstack, $safeeval);
 	$Apache::lonxml::evaluate++;
 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
-	    return "</textarea>";
+	    my $resid = $Apache::inputtags::response[-1];
+	    my $tagident = 'HWVAL_' . $resid;
+	    my $result =  "</textarea>";
+	    $result .= &spellcheck_onblur($tagident, $spellcheck);
+	    return $result;
 	}
     } elsif ($target eq 'edit') {
 	$result=&Apache::edit::end_table();
@@ -334,6 +370,12 @@
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
     my $result = "";
     my $input_id = &start_input($parstack,$safeeval);
+
+    # The spellcheck attribute 
+    # 1. enables spellchecking.
+    # 2. Provides the language code in which the spellchecking will be performed.
+
+    my $spellcheck = &Apache::lonxml::get_param('spellcheck', $parstack, $safeeval);
     if ($target eq 'web') {
 	$Apache::lonxml::evaluate--;
 	my $partid=$Apache::inputtags::part;
@@ -384,9 +426,15 @@
 		if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
 		    $name = "none";
 		}
-		$result.= '<input onkeydown="javascript:setSubmittedPart(\''.$partid.'\');" type="text" '.$readonly.' name="'.$name.'" value="'.
-		    $oldresponse.'" class="spellchecked"  size="'.$size.'"'.$maxlength.'
-                    onblur=' . "'\$(this).spellchecker(\"check\")'"  . '/>';
+		$result.= '<input onkeydown="javascript:setSubmittedPart(\''.$partid.'\');"'
+		     . ' type="text" '
+		     . $readonly.' name="'. $name . '"'
+		     . ' id="' . $name . '"'
+		     . ' value="'.  $oldresponse.'"'
+		     . ' class="spellchecked"  size="'.$size.'"'.$maxlength
+		     . '/>';
+
+		$result .= &spellcheck_onblur($name, $spellcheck);
 	    }
 	    if ($Apache::lonhomework::type eq 'exam'
 		&& &needs_exam_box($tagstack)) {




More information about the LON-CAPA-cvs mailing list