--- loncom/homework/structuretags.pm 2011/01/07 05:14:57 1.465.2.12 +++ loncom/homework/structuretags.pm 2011/12/26 13:45:10 1.497.2.5 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # definition of tags that give a structure to a document # -# $Id: structuretags.pm,v 1.465.2.12 2011/01/07 05:14:57 raeburn Exp $ +# $Id: structuretags.pm,v 1.497.2.5 2011/12/26 13:45:10 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -69,6 +69,83 @@ BEGIN { &Apache::lonxml::register('Apache::structuretags',('block','languageblock','translated','instructorcomment','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','problemtype','startpartmarker','startouttext','endpartmarker','endouttext','simpleeditbutton','definetag')); } + +#--------------------------------------------------------------------------------- +# +# This section of code deals with hyphenation management. +# We must do three things: +# - keep track fo the desired languages to alter the header. +# - provide hyphenation selection as needed by each language that appears in the +# text. +# - Provide the header text needed to make available the desired hyphenations. +# +# + +# Hash whose keys are the languages encountered in the document/resource. +# + +my %languages_required; +## +# Given a language selection as input returns a chunk of LaTeX that +# selects the required hyphenator. +# +# @param language - the language being selected. +# @return string +# @retval The LaTeX needed to select the hyphenation appropriate to the language. +# +sub select_hyphenation { + my $language = shift; + + $language = &Apache::loncommon::latexlanguage($language); # Translate -> latex language. + + # If there is no latex language there's not much we can do: + + if ($language) { + &require_language($language); + my $babel_hyphenation = "\\selectlanguage{$language}"; + + return $babel_hyphenation; + } else { + return ''; + } +} +## +# Clears the set of languages required by the document being rendered. +# +sub clear_required_languages { + %languages_required = (); +} +## +# Allows an external client of this module to register a need for a language: +# +# @param LaTeX language required: +# +sub require_language { + my $language = shift; + $languages_required{$language} = 1; +} + +## +# Provides the header for babel that indicates the languages +# the document requires. +# @return string +# @retval \usepackage[lang1,lang2...]{babel} +# @retval '' if there are no languages_required. +sub languages_header { + my $header =''; + my @languages = (keys(%languages_required)); + + # Only generate the header if there are languages: + + if (scalar @languages) { + my $language_list = join(',', (@languages)); + $header = '\usepackage['.$language_list."]{babel}\n"; + } + return $header; +} + +#---------------------------------------------------------------------------------- + sub start_web { my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_; if ($target ne 'edit' && $target ne 'modified') { @@ -129,8 +206,16 @@ sub homework_js { <<'JS'; @@ -154,52 +239,11 @@ JS sub setmode_javascript { return <<"ENDSCRIPT"; -ENDSCRIPT -} - -sub file_delchk_js { - my $delfilewarn = &mt('You have indicated you wish to remove some files previously included in your submission.').'\\n'. - &mt('Continue submission with these files removed?'); - return <<"ENDSCRIPT"; - ENDSCRIPT @@ -230,8 +274,62 @@ sub page_start { $extra_head .= &Apache::lonhtmlcommon::htmlareaselectactive(\%textarea_args); } my $is_task = ($env{'request.uri'} =~ /\.task$/); + my $needs_upload; + my ($symb)= &Apache::lonnet::whichuser(); + my ($map,$resid,$resurl)=&Apache::lonnet::decode_symb($symb); if ($is_task) { - $extra_head .= &file_delchk_js(); + $extra_head .= &Apache::lonhtmlcommon::file_submissionchk_js(); + } else { + if (&Apache::lonnet::EXT("resource.$Apache::inputtags::part.uploadedfiletypes") ne '') { + unless ($env{'request.state'} eq 'construct') { + my $navmap = Apache::lonnavmaps::navmap->new(); + if (ref($navmap)) { + my $mapres = $navmap->getResourceByUrl($map); + my $is_page; + if (ref($mapres)) { + $is_page = $mapres->is_page(); + } + unless ($is_page) { + $needs_upload = 1; + } + } + } + } else { + unless ($env{'request.state'} eq 'construct') { + my $navmap = Apache::lonnavmaps::navmap->new(); + if (ref($navmap)) { + my $mapres = $navmap->getResourceByUrl($map); + my $is_page; + if (ref($mapres)) { + $is_page = $mapres->is_page(); + } + unless ($is_page) { + my $res = $navmap->getBySymb($symb); + if (ref($res)) { + my $partlist = $res->parts(); + if (ref($partlist) eq 'ARRAY') { + foreach my $part (@{$partlist}) { + my @types = $res->responseType($part); + my @ids = $res->responseIds($part); + for (my $i=0; $i < scalar(@ids); $i++) { + if ($types[$i] eq 'essay') { + my $partid = $part.'_'.$ids[$i]; + if (&Apache::lonnet::EXT("resource.$partid.uploadedfiletypes") ne '') { + $needs_upload = 1; + last; + } + } + } + } + } + } + } + } + } + } + if ($needs_upload) { + $extra_head .= &Apache::lonhtmlcommon::file_submissionchk_js(); + } } my %body_args; @@ -258,14 +356,11 @@ sub page_start { } elsif (!defined($found{'body'}) && $env{'request.state'} eq 'construct') { if ($target eq 'web' || $target eq 'edit') { - if ($env{'environment.remote'} ne 'off') { - $body_args{'only_body'} = 1; - } # Breadcrumbs for Construction Space &Apache::lonhtmlcommon::clear_breadcrumbs(); &Apache::lonhtmlcommon::add_breadcrumb({ 'text' => 'Construction Space', - 'href' => &Apache::loncommon::authorspace(), + 'href' => &Apache::loncommon::authorspace($env{'request.uri'}), }); # breadcrumbs (and tools) will be created # in start_page->bodytag->innerregister @@ -294,8 +389,7 @@ sub page_start { # $body_args{'no_title'} = 1; $body_args{'force_register'} = 1; $body_args{'add_entries'} = \%add_entries; - if ($env{'environment.remote'} eq 'off' - && $env{'request.state'} eq 'construct') { + if ( $env{'request.state'} eq 'construct') { $body_args{'only_body'} = 1; } } @@ -318,7 +412,6 @@ sub page_start { if (!defined($found{'body'}) && $env{'request.state'} ne 'construct') { $page_start .= &Apache::lonxml::message_location(); } - my $form_tag_start; if (!defined($found{'form'})) { $form_tag_start='
'; } +sub problem_edit_action_button { + my ($name,$action,$accesskey,$text,$flag)=@_; + my $actionscript="setmode(this.form,'$action')"; + return "\n"; +} + sub problem_edit_buttons { - return ' -
- - - -
-
- - -
'; + my ($mode)=@_; +# Buttons that do not save + my $result='
'. + &problem_edit_action_button('subdiscview','discard','d','Discard Edits and View',1); + if ($mode eq 'editxml') { + $result.=&problem_edit_action_button('subedit','edit','e','Edit',1); + $result.=&problem_edit_action_button('subundo','undoxml','u','Undo',1); + $result.=&Apache::lonhtmlcommon::dragmath_button("LC_editxmltext",1); + } else { + $result.=&problem_edit_action_button('subeditxml','editxml','x','EditXML',1); + $result.=&problem_edit_action_button('subundo','undo','u','Undo',1); + } + $result.="\n
"; +# Buttons that save + $result.='
'; + if ($mode eq 'editxml') { + $result.=&problem_edit_action_button('subsaveedit','saveeditxml','s','Save and EditXML'); + $result.=&problem_edit_action_button('subsaveview','saveviewxml','v','Save and View'); + } else { + $result.=&problem_edit_action_button('subsaveedit','saveedit','s','Save and Edit'); + $result.=&problem_edit_action_button('subsaveview','saveview','v','Save and View'); + } + $result.="\n
\n"; + return $result; } sub problem_edit_header { - return ''. - &Apache::structuretags::remember_problem_state().' + return ''. + &remember_problem_state('edit').'
'.&mt('Problem Editing').&Apache::loncommon::help_open_menu('Problem Editing','Problem_Editor_XML_Index',5,'Authoring').'
'. +''. &problem_edit_buttons().'
'.&Apache::lonxml::message_location().' @@ -623,7 +746,7 @@ $show_all
- '; @@ -762,15 +885,15 @@ sub store_aggregates { foreach my $part (@parts) { if ($env{'request.role'} =~/^st/) { if ($Apache::lonhomework::results{'resource.'.$part.'.award'} - eq 'APPROX_ANS' || - $Apache::lonhomework::results{'resource.'.$part.'.award'} - eq 'EXACT_ANS') { + eq 'APPROX_ANS' || + $Apache::lonhomework::results{'resource.'.$part.'.award'} + eq 'EXACT_ANS') { $aggregate{$symb."\0".$part."\0correct"} = 1; } if ($Apache::lonhomework::results{'resource.'.$part.'.tries'} == 1) { $aggregate{$symb."\0".$part."\0users"} = 1; } else { - my (undef,$last_reset) = &Apache::grades::get_last_resets($symb,$env{'request.course.id'},[$part]); + my (undef,$last_reset) = &Apache::grades::get_last_resets($symb,$env{'request.course.id'},[$part]); if ($last_reset) { if (&Apache::grades::get_num_tries(\%Apache::lonhomework::history,$last_reset,$part) == 0) { $aggregate{$symb."\0".$part."\0users"} = 1; @@ -779,7 +902,7 @@ sub store_aggregates { } $aggregate{$symb."\0".$part."\0attempts"} = 1; } - if (($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'anonsurvey') || + if (($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'anonsurvey') || ($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'anonsurveycred') || ($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'randomizetry')) { if ($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'randomizetry') { @@ -788,7 +911,7 @@ sub store_aggregates { $anoncounter{$symb."\0".$part} = 1; } my $needsrelease = $Apache::lonnet::needsrelease{'parameter:type:'.$Apache::lonhomework::results{'resource.'.$part.'.type'}}; - if ($needsrelease) { + if ($needsrelease) { my $curr_required = $env{'course.'.$env{'request.course.id'}.'.internal.releaserequired'}; if ($curr_required eq '') { &Apache::lonnet::update_released_required($needsrelease); @@ -1026,17 +1149,6 @@ sub start_problem { if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval,$target); } if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') { - #handle exam checkout - if ($Apache::lonhomework::type eq 'exam') { - my $token= - $Apache::lonhomework::history{"resource.0.outtoken"}; - if (($env{'form.doescheckout'}) && (!$token)) { - $token=&Apache::lonxml::maketoken(); - $Apache::lonhomework::history{"resource.0.outtoken"}= - $token; - } - $result.=&Apache::lonxml::printtokenheader($target,$token); - } if ($env{'form.markaccess'}) { my @interval=&Apache::lonnet::EXT("resource.0.interval"); &Apache::lonnet::set_first_access($interval[1]); @@ -1162,14 +1274,16 @@ sub start_problem { $result .= ''."\n"; } - foreach my $field ('questiontype','rndseed') { + foreach my $field ('trial','questiontype') { if ($env{"form.grade_$field"} ne '') { $result .= ''."\n"; } } - } + if ($env{'form.grade_imsexport'}) { + $result = ''; + } } elsif ($target eq 'tex') { $result .= 'INSERTTEXFRONTMATTERHERE'; @@ -1228,6 +1342,7 @@ sub end_problem { } my $name_of_resourse= &Apache::lonxml::latex_special_symbols(&get_resource_name($parstack,$safeeval),'header'); my $begin_doc=' \typeout{STAMPOFPASSEDRESOURCESTART Resource

"'.$name_of_resourse.'"

located in
'.$env{'request.uri'}.'
STAMPOFPASSEDRESOURCEEND} \noindent '; + &clear_required_languages(); my $toc_line='\vskip 1 mm\noindent '.$startminipage. '\addcontentsline{toc}{subsection}{'.$name_of_resourse.'}'; @@ -1274,7 +1389,7 @@ sub end_problem { } else { $frontmatter.= $begin_doc.$toc_line; if ($Apache::lonhomework::type eq 'exam' and $allow_print_points==1) { - $frontmatter .= '\fbox{\textit{'.$weight.' pt}}'; + $frontmatter .= '\fbox{\textit{'.&mt('[quant,_1,pt,pt]',$weight ).'}}'; } } } else { @@ -1284,7 +1399,7 @@ sub end_problem { if (not $env{'request.symb'} =~ m/\.page_/) { $frontmatter .= $begin_doc.$toc_line; if (($Apache::lonhomework::type eq 'exam') and ($allow_print_points==1)) { - $frontmatter .= '\fbox{\textit{'.$weight.' pt}}'; + $frontmatter .= '\fbox{\textit{'.&mt('[quant,_1,pt,pt]',$weight ).'}}'; } } else { $frontmatter .= '\vskip 1mm \\\\\\\\'.$startminipage; @@ -1308,7 +1423,8 @@ sub end_problem { } } elsif ( ($target eq 'web' || $target eq 'tex') && $Apache::inputtags::part eq '0' && - $status ne 'UNCHECKEDOUT' && $status ne 'NOT_YET_VIEWED') { + $status ne 'UNCHECKEDOUT' && $status ne 'NOT_YET_VIEWED' + && !$env{'form.grade_imsexport'}) { # if part is zero, no s existed, so we need show the current # grading status my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target); @@ -1318,8 +1434,9 @@ sub end_problem { (($target eq 'web') && ($env{'request.state'} ne 'construct')) || ($target eq 'answer') || ($target eq 'tex') ) { - if ($target ne 'tex' && - $env{'form.answer_output_mode'} ne 'tex') { + if (($target ne 'tex') && + ($env{'form.answer_output_mode'} ne 'tex') && + (!$env{'form.grade_imsexport'})) { $result.=""; } if ($target eq 'web') { @@ -1500,54 +1617,101 @@ sub end_block { } return $result; } - +# +# +# ... +# +# +# This declares the intent to provide content that can be rendered in the +# set of languages in the include specificatino but not in the exclude +# specification. If a currently preferred language is in the include list +# the content in the ... is rendered +# If the currently preferred language is in the exclude list, +# the content in the ..>[2]->{'include'}; my $exclude = $token->[2]->{'exclude'}; my @preferred_languages=&Apache::lonlocal::preferred_languages(); -# This should not even happen, since we should at least have the server language - if (!$preferred_languages[0]) { $preferred_languages[0]='en'; } -# Now loop over all languages in order of preference + + # This should not even happen, since we should at least have the server language + + if (!$preferred_languages[0]) { + $preferred_languages[0]='en'; + } + + # Now loop over all languages in order of preference + + my $render; foreach my $preferred_language (@preferred_languages) { -# If the languageblock has no arguments, show the contents - $result=1; + + # If neither include/nor exlude is present the block is going + # to get rendered. + my $found=0; -# Do we have an include argument? + $render=1; + + # If include is specified, don't render the block + # unless the preferred language is included in the set. + if ($include) { -# If include is specified, by default, don't render the block - $result=0; + $render=0; foreach my $included_language (split(/\,/,$include)) { -# ... but if my preferred language is included, render it if ($included_language eq $preferred_language) { - $result=1; + $render=1; $found=1; + last; # Only need to find the first. } } } -# Do we have an exclude argument? + # Do we have an exclude argument? + # If so, and one of the languages matches a preferred language + # inhibit rendering the block. Note that in the pathalogical case the + # author has specified a preferred language in both the include and exclude + # attribte exclude is preferred. + if ($exclude) { - $result=1; + $render=1; foreach my $excluded_language (split(/\,/,$exclude)) { if ($excluded_language eq $preferred_language) { - $result=0; + $render=0; $found=1; + last; # Only need to find the first. } } } - if ($found) { last; } + if ($found) { + last; # Done on any match of include or exclude. + } } - if ( ! $result ) { + # If $render not true skip the entire block until + # + + if ( ! $render ) { my $skip=&Apache::lonxml::get_all_text("/languageblock",$parser, $style); &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]"); } - $result=''; + # If $render is true, we've not skipped the contents of the + # and the normal loncapa processing flow will render it as a matter of course. + } elsif ($target eq 'edit') { $result .=&Apache::edit::tag_start($target,$token); $result .=&Apache::edit::text_arg(&mt('Include Language:'),'include', @@ -1572,9 +1736,47 @@ sub end_languageblock { } return $result; } - +# languagblock specific tags: { - my %available_texts; + # For chunks of a resource that has translations, this hash contains + # the translations available indexed by language name. + # + + my %available_texts; + + # starts a block of a resource that has multiple translations. + # See the tag as well. + # When is encountered if there is a translation for the + # currently preferred language, that is rendered inthe web/tex/webgrade + # targets. Otherwise, the default text is rendered. + # + # Note that is only registered for the duration of the + # ... block + # + # Pathalogical case handling: + # - If there is no that specifies a 'default' and there is no + # translation that matches a preferred language, nothing is rendered. + # - Nested ... might be linguistically supported by + # XML due to the stack nature of tag registration(?) however the rendered + # output will be incorrect because there is only one %available_texts + # has and end_translated clears it. + # - Material outside of a ... block within the + # ... block won't render either e.g.: + # + # The following will be in your preferred langauge: + # + # This section in english + # + # + # Hier es ist auf Deutsch. + # + # + # En Francais + # + # + # + # The introductory text prior to the first tag is not rendered. + # sub start_translated { my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_; &Apache::lonxml::register('Apache::structuretags',('lang')); @@ -1593,14 +1795,52 @@ sub end_languageblock { my @possibilities = keys(%available_texts); my $which = &Apache::loncommon::languages(\@possibilities) || 'default'; - $result = $available_texts{$which}; + if ($target eq 'tex') { + $result = &select_hyphenation($which); + } + $result .= $available_texts{$which}; } undef(%available_texts); &Apache::lonxml::deregister('Apache::structuretags',('lang')); return $result; } - + # + # Specifies that the block contained within it is a translation + # for a specific language specified by the 'which' attribute. The + # 'other' attribute can be used by itself or in conjunction with + # which to specify this tag _may_ be used as a translation for some + # list of languages. e.g.: + # specifying that the block provides a translation for US (primary) + # Canadian, Australian and UK Englush. + # + # Comment: this seems a bit silly why not just support a list of languages + # e.g. and ditch the other attribute? + # + # Effect: + # The material within the .. block is stored in the + # specified set of $available_texts hash entries, the appropriate one + # is selected at time. + # + # Pathalogical case handling: + # If a language occurs multiple times within a block, + # only the last one is rendered e.g.: + # + # + # + # Red green color blindness is quite common affecting about 7.8% of + # the male population, but onloy about .65% of the female population. + # + # Red green colour blindness is quite common affecting about 7.8% of + # the male population, but onloy about .65% of the female population. + # + # + # + # renders the correct spelling of color (colour) for people who have specified + # a preferred language that is one of the British Commonwealth languages + # even though those are also listed as valid selections for the US english + # block. + # sub start_lang { my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_; if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || @@ -1631,7 +1871,8 @@ sub end_languageblock { } return ''; } -} +} # end langauge block specific tags. + sub start_instructorcomment { my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_; @@ -1845,6 +2086,7 @@ sub ordered_show_check { return $in_order_show; } + sub start_startpartmarker { my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_; my $result=''; @@ -1853,7 +2095,7 @@ sub start_startpartmarker { $result.=&mt('Marker for the start of a part. Place end marker below to wrap in-between tags into a new part.').''; $result.=&Apache::edit::end_table(); - } + } return $result; } @@ -1883,6 +2125,10 @@ sub end_endpartmarker { return @result; } + + + + sub start_part { my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_; if (!$Apache::lonxml::metamode) { @@ -1968,12 +2214,12 @@ sub start_part { $allow_print_points=0; } if (($Apache::lonhomework::type eq 'exam') && ($allow_print_points)) { - $result .= '\vskip 10mm\fbox{\textit{'.$weight.' pt}}'; + $result .= '\vskip 10mm\fbox{\textit{'.&mt('[quant,_1,pt,pt]',$weight ).'}}'; } } elsif ($target eq 'web') { if ($status eq 'CAN_ANSWER') { - my $problemstatus = &get_problem_status($Apache::inputtags::part); + my $problemstatus = &get_problem_status($Apache::inputtags::part); my $probrandomize = &Apache::lonnet::EXT("resource.$Apache::inputtags::partlist[0].type"); my $probrandtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::partlist[0].randomizeontries"); my $num = scalar(@Apache::inputtags::partlist)-1; @@ -1991,7 +2237,7 @@ sub start_part { $result .= &randomizetry_part_header($problemstatus,$reqtries,$num); } } - $result.=''; + $result.=''; } } } @@ -2037,7 +2283,8 @@ sub end_part { !$hidden && $in_order_show) { my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part, $target); - if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') { + if (($Apache::lonhomework::type eq 'exam' && $target eq 'tex') || + ($env{'form.grade_imsexport'})) { $gradestatus=''; } $result.=$gradestatus; @@ -2205,7 +2452,7 @@ sub end_startouttext { .'' .'' .&Apache::lonhtmlcommon::dragmath_button($areaid,1) - .'' + .'' .'' .&Apache::edit::insertlist($target,$token) .'' @@ -2278,11 +2525,12 @@ sub start_simpleeditbutton { # .&mt('Note: it can take up to 10 minutes for changes to take effect for all users.') # .&Apache::loncommon::help_open_topic('Caching') # .'

'; - $result.=&Apache::lonhtmlcommon::start_funclist() + $result.=&Apache::loncommon::head_subbox( + &Apache::lonhtmlcommon::start_funclist() .&Apache::lonhtmlcommon::add_item_funclist( '' .&mt('Edit').'') - .&Apache::lonhtmlcommon::end_funclist(); + .&Apache::lonhtmlcommon::end_funclist()); } return $result;