File:  [LON-CAPA] / loncom / homework / daxepage.pm
Revision 1.13: download - view: text, annotated - select for diffs
Mon Mar 25 17:29:23 2024 UTC (2 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- In Daxe Editor use current language for user determined by LON-CAPA,
  if available, otherwise default to en.

    1: # The LearningOnline Network
    2: # Page with Daxe on the left side and the preview on the right side
    3: #
    4: # $Id: daxepage.pm,v 1.13 2024/03/25 17:29:23 raeburn Exp $
    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: #
   28: ###
   29: 
   30: package Apache::daxepage;
   31: use strict;
   32: 
   33: use Apache::loncommon();
   34: use Apache::lonnet();
   35: use Apache::lonhtmlcommon();
   36: use Apache::lonxml();
   37: use Apache::edit();
   38: use Apache::lonmenu();
   39: use Apache::lonlocal;
   40: use Apache::Constants qw(:common);
   41: use LONCAPA qw(:DEFAULT :match);
   42: use HTML::Entities();
   43: 
   44: sub handler {
   45:     my $request = shift;
   46:     my $uri = $request->uri;
   47:     $uri =~ s{^/daxepage}{};
   48:     &Apache::loncommon::content_type($request,'text/html');
   49:     my ($is_not_assess,$is_assess);
   50:     if ($uri =~/\.(xml|html|htm|xhtml|xhtm)$/) {
   51:         $is_not_assess = 1;
   52:     } elsif ($uri =~ /$LONCAPA::assess_re/) {
   53:         unless ($uri =~ /\.form$/) {
   54:             $is_assess = 1;
   55:         }
   56:     }
   57:     unless ($is_not_assess || $is_assess) {
   58:         $request->status(406);
   59:         return OK;
   60:     }
   61:     my %editors = &Apache::loncommon::permitted_editors();
   62:     unless ($editors{'daxe'}) {
   63:         my $msg = '<p class="LC_warning">'.
   64:                   &mt('Daxe editor is not enabled for this Authoring Space.').'</p>';
   65:         &do_redirect($request,$uri,$msg);
   66:         return OK;
   67:     }
   68:     if ($is_not_assess) {
   69:         delete($editors{'xml'});
   70:         $editors{'edit'} = 1;
   71:     }
   72:     my %lt = &Apache::lonlocal::texthash(
   73:                                           'noif' => 'No iframe support.',
   74:                                           'show' => 'Show content in pop-up window',
   75:                                           'save' => 'Save',
   76:                                           'oeds' => 'other editors',
   77:                                           'othe' => 'other editor',
   78:                                           'edit' => 'Save and Edit',
   79:                                           'disc' => 'Discard and View',
   80:                                           'save' => 'Save and View',
   81:                                         );
   82:     my $name = $uri;
   83:     $name =~ s/^.*\/([^\/]+)$/$1/;
   84:     my $lang = &Apache::lonlocal::current_language();
   85:     my $filearg = '/daxeopen'.$uri;
   86:     my $daxeurl = '/adm/daxe/daxe.html?config=config/loncapa_config.xml&save=/daxesave'.
   87:                   '&file='.$filearg;
   88:     my $headjs = &Apache::loncommon::iframe_wrapper_headjs().
   89:                  &listener_js($lang,$filearg).
   90:                  &toggle_LCmenus_js().&saveandview_js().
   91:                  &Apache::edit::js_change_detection();
   92: 
   93:     my ($clickexit,$clicksave,$clickedit);
   94:     if ($is_assess) {
   95:         $headjs .= &Apache::lonxml::setmode_javascript();
   96:         $clickexit = "javascript:setmode(this.form,'view');";
   97:     } else {
   98:         $headjs .= &Apache::lonxml::seteditor_javascript();
   99:         $clickexit = "javascript:seteditmode(this.form,'view');";
  100:     }
  101:     $clicksave = "javascript:daxesave('exit');";
  102:     $clickedit = "javascript:daxesave();";
  103:     my $form_events = &Apache::edit::form_change_detection();
  104:     my $editheader = '<form '.$form_events.' method="post" name="daxeedit" action="'.$uri.'">'.
  105:                      '<input type="hidden" name="problemmode" value="daxe" />'."\n".
  106:                      '<div class="LC_edit_problem_editxml_header">'."\n";
  107:     my $saveeditbutton = '<input type="button" name="submitmode" accesskey="s" value="'.$lt{'edit'}.
  108:                      '" onclick="'.$clickedit.'" />'."\n";
  109:     my $exitbutton = '<input type="button" name="submitmode" accesskey="d" value="'.$lt{'disc'}.
  110:                      '" onclick="'.$clickexit.'" />'."\n";
  111:     my $saveexitbutton = '<input type="button" name="submitmode" accesskey="v" value="'.$lt{'save'}.
  112:                      '" onclick="'.$clicksave.'" />'."\n";
  113:     $editheader .= '<table class="LC_edit_problem_header_title"><tr><td>'.
  114:                    $uri.'</td><td align="right"><span class="LC_nobreak">'.
  115:                    $saveeditbutton.$saveexitbutton.$exitbutton.'</span>';
  116:     if ($editors{'edit'} || $editors{'xml'}) {
  117:         my $other = (($editors{'edit'} && $editors{'xml'})? $lt{'oeds'} : $lt{'othe'});
  118:         $editheader .= '&nbsp;&nbsp;|&nbsp;&nbsp;<span class="LC_nobreak">'.$other.':</span> '.
  119:                        '<span class="LC_nobreak">';
  120:         if ($is_not_assess) {
  121:             $editheader .= '<input type="hidden" name="editmode" value="" />'."\n".
  122:                            '<input type="button" name="editordefault" value="'.$lt{'edit'}.
  123:                            '" onclick="seteditmode(this.form,'."'edit'".');" />'."\n";
  124:         } else {
  125:             if ($editors{'edit'}) {
  126:                 $editheader .= '<input type="button" name="submitmode" accesskey="e" value="'.&mt('Edit').'" '.
  127:                                'onclick="javascript:setmode(this.form,'."'edit'".')" />'."\n";
  128:             }
  129:             if ($editors{'xml'}) {
  130:                 $editheader .= '<input type="button" name="submitmode" accesskey="x" value="'.&mt('EditXML').'" '.
  131:                                'onclick="javascript:setmode(this.form,'."'editxml'".')" />'."\n";
  132:             }
  133:         }
  134:         $editheader .= '</span>';
  135:     }
  136:     $editheader .= '</td></tr></table></div></form>'."\n";
  137:     my $start_collapsed = &collapsible_std_LCmenus();
  138:     my $args = {
  139:                 'collapsible_header' => $editheader,
  140:                 'start_collapsed'    => $start_collapsed,
  141:                };
  142:     my $startpage = &Apache::loncommon::start_page('Daxe: '.$name,$headjs,$args).
  143:                     &Apache::lonmenu::constspaceform();
  144:     my $endpage = &Apache::loncommon::end_page();
  145: 
  146:     # javascript will position the iframe if window was resized (or zoomed)
  147:     my $script = &Apache::loncommon::iframe_wrapper_resizejs();
  148:     my $dest = &HTML::Entities::encode($daxeurl,'&<>"');
  149:     my $noiframe = &Apache::loncommon::modal_link($dest,$lt{'show'},500,400);
  150: 
  151:     $request->print(<<"ENDFRAME");
  152: $startpage
  153: $script
  154: <div class="LC_iframecontainer" style="padding-right: 5px">
  155: <iframe src="$dest" id="lcdiframe">$lt{'noif'} $noiframe</iframe>
  156: </div>
  157: $endpage
  158: ENDFRAME
  159:     return OK;
  160: }
  161: 
  162: sub listener_js {
  163:     my ($lang,$filearg) = @_;
  164:     return <<"ENDJS";
  165: <script type="text/javascript">
  166: //<![CDATA[
  167: 
  168: var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
  169: var eventer = window[eventMethod];
  170: var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
  171: 
  172: eventer(messageEvent,function(e) {
  173:     var reqdOrigin = window.location.protocol+'//'+window.location.hostname;
  174:     if (e.origin == reqdOrigin) {
  175:         if (e.data == '$filearg') {
  176:             setmode(document.daxeedit,'view');
  177:         } else if ((e.data == 'userlclang') || (e.data == 'userlang')) {
  178:             window.myIframe = document.getElementById("lcdiframe").contentWindow;
  179:             window.myIframe.postMessage(e.data+':$lang',reqdOrigin);
  180:         }
  181:         return;
  182:     }
  183: },false);
  184: 
  185: //]]>
  186: </script>
  187: ENDJS
  188: 
  189: }
  190: 
  191: sub saveandview_js {
  192:     return <<"ENDJS";
  193: 
  194: <script type="text/javascript">
  195: //<![CDATA[
  196: 
  197: function daxesave(exit) {
  198:     window.myIframe = document.getElementById("lcdiframe").contentWindow;
  199:     window.myIframe.focus();
  200:     window.myIframe.savelcdoc(exit);
  201:     return;
  202: }
  203: 
  204: //]]>
  205: </script>
  206: ENDJS
  207: }
  208: 
  209: sub toggle_LCmenus_js {
  210:     my %lt = &Apache::lonlocal::texthash(
  211:                                          altc => 'menu state: collapsed',
  212:                                          alte => 'menu state: explanded',
  213:                                          ttlc => 'display standard menus',
  214:                                          ttle => 'hide standard menus',
  215:     );
  216:     return <<"ENDJS";
  217: <script type="text/javascript">
  218: //<![CDATA[
  219: \$(document).ready (function () {
  220:     \$(".LC_collapse_trigger").on("click", function (e) {
  221:         var id = this.id;
  222:         var \$content = \$(this).next (".LC_menus_content");
  223:         var expanded = \$content.hasClass ("shown");
  224:         if (expanded) {
  225:             \$content.removeClass ("shown");
  226:             \$content.addClass ("hidden");
  227:         } else {
  228:             \$content.removeClass ("hidden");
  229:             \$content.addClass ("shown");
  230:         }
  231: 
  232:         \$(this).find ("[aria-expanded]")
  233:         .attr ("aria-expanded", !expanded);
  234: 
  235:         \$(this).find ("[aria-pressed]")
  236:         .attr ("aria-pressed", !expanded);
  237: 
  238:         \$(this).find (".LC_collapsible_indicator").attr ({
  239:         "src":
  240:         (expanded)? "/res/adm/pages/collapsed.png" : "/res/adm/pages/expanded.png",
  241:         "alt":
  242:         (expanded)? "$lt{altc}" : "$lt{alte}",
  243:         "title":
  244:         (expanded)? "$lt{ttlc}" : "$lt{ttle}"
  245:         });
  246: 
  247:         \$(window).trigger('resize');
  248:         \$(this).focus ();
  249:     });
  250: 
  251:     \$("#LC_expandingContainer").attr ("aria-live", "off");
  252: });
  253: //]]>
  254: </script>
  255: ENDJS
  256: 
  257: }
  258: 
  259: sub do_redirect {
  260:     my ($request,$uri,$msg) = @_;
  261:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  262:     $request->print(
  263:         &Apache::loncommon::start_page('Authoring Space',undef,
  264:                                        {'redirect'       => [2,$uri]}).
  265: 
  266:         '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
  267:         "$msg\n".
  268:         &Apache::loncommon::end_page());
  269:     return;
  270: }
  271: 
  272: sub collapsible_std_LCmenus {
  273:     my $daxecollapse = $Apache::lonnet::env{'environment.daxecollapse'};
  274:     unless ($daxecollapse) {
  275:         my %domdefs = &Apache::lonnet::get_domain_defaults($Apache::lonnet::env{'user.domain'});
  276:         if ($domdefs{'daxecollapse'}) {
  277:             $daxecollapse = 'yes';
  278:         }
  279:     }
  280:     if ($daxecollapse eq 'yes') {
  281:         return 1;
  282:     }
  283:     return;
  284: }
  285: 
  286: 1;
  287: __END__

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