File:  [LON-CAPA] / loncom / homework / daxepage.pm
Revision 1.17: download - view: text, annotated - select for diffs
Fri Apr 19 03:48:23 2024 UTC (13 days, 12 hours ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- No breadcrumbs in LON-CAPA menus when using Daxe editor to edit file in
  Authoring Space, so include "Exit Daxe" icon/link in Functions menu.

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

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