File:  [LON-CAPA] / loncom / homework / daxepage.pm
Revision 1.15: download - view: text, annotated - select for diffs
Sun Apr 14 17:12:28 2024 UTC (7 weeks, 5 days ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Available editors in Course Authoring Space, or when editing an html file
  created in a course folder using the Course Editor is a domain default,
  which can be overridden in specific course(s) by a Domain Coordinator.

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

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