File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.138.2.8.2.2: download - view: text, annotated - select for diffs
Sat Jan 21 21:34:08 2023 UTC (17 months, 1 week ago) by raeburn
Branches: version_2_11_4_msu
Diff to branchpoint 1.138.2.8: preferred, unified
- For 2.11.4 (modified)
  Include changes in 1.152, 1.153

    1: # The LearningOnline Network
    2: # Syllabus
    3: #
    4: # $Id: lonsyllabus.pm,v 1.138.2.8.2.2 2023/01/21 21:34:08 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: package Apache::lonsyllabus;
   30: 
   31: use strict;
   32: use Apache::lontemplate;
   33: use Apache::Constants qw(:common);
   34: use Apache::loncommon;
   35: use Apache::lonnet;
   36: use Apache::lontexconvert;
   37: use Apache::lonfeedback;
   38: use Apache::lonhtmlgateway;
   39: use Apache::lonannounce;
   40: use Apache::lonlocal;
   41: use Apache::lonhtmlcommon;
   42: use Apache::lonspeller();
   43: use HTML::Entities();
   44: 
   45: sub handler {
   46:     my $r = shift;
   47:     &Apache::loncommon::content_type($r,'text/html');
   48:     $r->send_http_header;
   49:     return OK if $r->header_only;
   50: 
   51:     my $target=$env{'form.grade_target'};
   52: # --------------------------------------------------- Get course info from URL
   53:     my (undef,undef,$cdom,$cnum)=split(/\//,$r->uri);
   54: # ------------------------------------------------------------ Get query string
   55:     &Apache::loncommon::get_unprocessed_cgi
   56:                         ($ENV{'QUERY_STRING'},['register','forceedit','todocs',
   57:                                                'folderpath','title','only_body']);
   58: # ----------------------------------------------------- Is this even a course?
   59:     my $homeserver=&Apache::lonnet::homeserver($cnum,$cdom);
   60:     if ($homeserver eq 'no_host') {
   61:         &Apache::loncommon::content_type($r,'text/html');
   62:         $r->send_http_header;
   63:         &Apache::loncommon::simple_error_page($r,'No syllabus available',
   64:                           'No syllabus available');
   65:         return OK;
   66:     } elsif (!&Apache::lonnet::is_course($cdom,$cnum)) {
   67:         &Apache::loncommon::content_type($r,'text/html');
   68:         $r->send_http_header;
   69:         &Apache::loncommon::simple_error_page($r,'No syllabus available',
   70:                           'The course/community for which the syllabus was requested does not exist.');
   71:         return OK;
   72:     }
   73: # ------------------------------------- There is such a course, get environment
   74:     my %courseenv=&Apache::lonnet::dump('environment',$cdom,$cnum);
   75:     my $crstype = &Apache::loncommon::course_type();
   76: 
   77: # --------------------------------------------------------------- Force Student
   78:     my ($forceedit,$forcestudent);
   79:     if ($env{'form.forceedit'}) { $forceedit=1; }
   80:     if (!$forceedit) {
   81:         $forcestudent=1;
   82:     }
   83: 
   84: # --------------------------------------------------------------- Check Privileges
   85:     my $allowed = 0;
   86:     if ($env{'user.environment'}) {
   87: # does this user have privileges to post, etc?
   88:         if ($env{'request.course.id'}
   89:         && $cdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}
   90:         && $cnum eq $env{'course.'.$env{'request.course.id'}.'.num'}) {
   91:             $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
   92:             if ($forcestudent or $target eq 'tex') { $allowed=0; }
   93:         }
   94:     }
   95: 
   96: # -------------------------------------------------- Let's see who handles this
   97:     my $external=$courseenv{'externalsyllabus'};
   98:     my $uploaded=$courseenv{'uploadedsyllabus'};
   99:     my $minimal=$courseenv{'minimalsyllabus'};
  100: 
  101:     if (($minimal =~/\w/) || ($uploaded =~/\w/)) {
  102:         my ($item,$is_pdf);
  103:         if ($minimal =~/\w/) {
  104:             if ($external =~ m{\Q$minimal\E$}) {
  105:                 undef($external);
  106:             }
  107:             $item = $minimal;
  108:         } elsif ($uploaded =~/\w/) {
  109:             if ($external =~ m{\Q$uploaded\E$}) {
  110:                 undef($external);
  111:             }
  112:             $item = $uploaded;
  113:             if ($item =~ /\.pdf$/i) {
  114:                 $is_pdf = 1;
  115:             }
  116:         }
  117:         unless ($allowed && $forceedit) {
  118:             my $file=&Apache::lonnet::filelocation("",$item);
  119:             if ($file =~ /\.(tex|x?html?)$/) {
  120:                 my $filetype = $1;
  121:                 my $filecontents=&Apache::lonnet::getfile($file);
  122:                 if ($filecontents eq -1) {
  123:                     $r->print(&mt('Syllabus file unavailable'));
  124:                 } elsif ($filetype eq 'tex') {
  125:                     if ($target eq 'tex') {
  126:                         $r->print($filecontents);
  127:                     } else {
  128:                         my $texengine = $env{'form.texengine'};
  129:                         if ($texengine eq '') {
  130:                             $texengine = 'tth';
  131:                         } elsif (lc($texengine) eq 'jsmath') {
  132:                             $texengine = 'MathJax';
  133:                         }
  134:                         my $result = &Apache::lontexconvert::converted(\$filecontents,$texengine);
  135:                         my %args;
  136:                         &get_breadcrumbs($cdom,$cnum,$crstype,\%args);
  137:                         if ($env{'form.only_body'}) {
  138:                             $args{'only_body'} = 1;
  139:                         }
  140:                         if ($env{'request.use_absolute'}) {
  141:                             $args{'use_absolute'} = $env{'request.use_absolute'};
  142:                         }
  143:                         $r->print(&Apache::loncommon::start_page("Syllabus",undef,\%args).
  144:                                   $result.
  145:                                   &Apache::loncommon::end_page());
  146:                     }
  147:                 } else {
  148:                     my %mystyle;
  149:                     unless ($target eq 'tex') {
  150:                         $target = 'web';
  151:                     }
  152:                     &Apache::structuretags::reset_problem_globals();
  153:                     my $oldfile = $env{'request.filename'};
  154:                     $env{'request.filename'} = $item;
  155:                     my $result = &Apache::lonxml::xmlparse($r,$target,$filecontents,
  156:                                                            '',%mystyle);
  157:                     &Apache::structuretags::reset_problem_globals();
  158:                     &Apache::lonhomework::finished_parsing();
  159:                     $env{'request.filename'} = $oldfile;
  160:                     &Apache::lonxml::add_messages(\$result);
  161:                     $r->print($result);
  162:                 }
  163:             } else {
  164:                 if ($target eq 'tex') {
  165:                     $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}).
  166:                               ' \strut \\\\ \textbf{'.&mt('Syllabus').'} \strut \\\\ '.
  167:                               &mt('Unsupported file type.').' \strut \\\\ '.
  168:                               &mt('Print the syllabus directly from your web browser').
  169:                               '\end{document}');
  170:                 } else {
  171:                     my $brcrum;
  172:                     if ($env{'form.folderpath'} =~ /^supplemental/) {
  173:                         &Apache::loncommon::validate_folderpath(1,'',$cnum,$cdom);
  174:                         my $title = $env{'form.title'};
  175:                         if ($title eq '') {
  176:                             $title = &mt('Syllabus');
  177:                         }
  178:                         $brcrum =
  179:                             &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  180:                     }
  181:                     $r->print(&Apache::lonwrapper::wrapper($r,$item,$brcrum,$env{'request.use_absolute'},
  182:                                                            undef,$is_pdf,undef,'','',&mt('Syllabus')));
  183:                 }
  184:             }
  185:             return OK;
  186:         }
  187:     } elsif ($external=~/\w/) {
  188:         unless ($allowed && $forceedit) {
  189:             if (($env{'user.name'} eq 'public') && ($env{'user.domain'} eq 'public') &&
  190:                 ($ENV{'SERVER_PORT'} == 443) && ($external =~ m{^http://}) && !($env{'form.usehttp'})) {
  191:                 my $hostname = $r->hostname();
  192:                 unless ((&Apache::lonnet::uses_sts()) || (&Apache::lonnet::waf_allssl($hostname))) {
  193:                     &redirect_to_http($r);
  194:                     return OK;
  195:                 }
  196:             }
  197:             if ($target eq 'tex') {
  198:                 $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}).
  199:                          ' \strut \\\\ \textbf{'.&mt('Syllabus').'} \strut \\\\ '.$external.' '.
  200:                          ' \strut \\\\ '.&mt('Print the syllabus directly from your web browser').
  201:                          '\end{document}');
  202:             } else {
  203:                 my $is_ext = 1;
  204:                 my ($is_pdf,$brcrum);
  205:                 if ($external =~ /\.pdf$/i) {
  206:                     $is_pdf = 1;
  207:                 }
  208:                 if ($env{'form.folderpath'} =~ /^supplemental/) {
  209:                     &Apache::loncommon::validate_folderpath(1,'',$cnum,$cdom);
  210:                     my $title = $env{'form.title'};
  211:                     if ($title eq '') {
  212:                         $title = &mt('Syllabus');
  213:                     }
  214:                     $title = &HTML::Entities::encode($title,'\'"<>&');
  215:                     $brcrum =
  216:                         &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  217:                 }
  218:                 $r->print(&Apache::lonwrapper::wrapper($r,$external,$brcrum,$env{'request.use_absolute'},
  219:                                                        $is_ext,$is_pdf,undef,'','',&mt('Syllabus')));
  220:             }
  221:             return OK;
  222:         }
  223:     }
  224: 
  225: # ------------------------------------------------------------ Print the screen
  226: 
  227:     if ($target eq 'tex') {
  228:         $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
  229:     }
  230: 
  231: # ------------------------------ The buck stops here: internal syllabus display
  232: # --------------------------------------------------------- The syllabus fields
  233:     my %syllabusfields=&Apache::lonlocal::texthash(
  234:        'aaa_instructorinfo' => 'Instructor Information',
  235:        'bbb_description'    => 'Course Description',
  236:        'ccc_prereq'         => 'Prerequisites',
  237:        'cdc_classhours'     => 'Class Hours',
  238:        'ddd_officehours'    => 'Office Hours',
  239:        'eee_helproom'       => 'Helproom Hours',
  240:        'efe_projectinfo'    => 'Project Information',
  241:        'fff_examinfo'       => 'Exam Information',
  242:        'fgf_deadlines'      => 'Deadlines',
  243:        'ggg_grading'        => 'Grading Information',
  244:        'hhh_readings'       => 'Readings',
  245:        'iii_coursepack'     => 'Coursepack',
  246:        'jjj_weblinks'       => 'Web Links',
  247:        'kkk_textbook'       => 'Textbook',
  248:        'lll_includeurl'     => 'URLs To Include in Syllabus');
  249: # ---------------------------------------------------------- Load syllabus info
  250:     my %syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum);
  251:     my ($output,%displayfields,%noshow);
  252: 
  253: # This handler might be called anonymously ...
  254: # ----------------------------------------------------- Only if not public call
  255:     if ($allowed) {
  256:         if (($env{'form.choice'} =~ /^(template|minimal|url|file)$/) ||
  257:             ($env{'form.phase'} =~ /^(upload|check)_embedded$/)) {
  258:             my $earlyout;
  259:             ($earlyout,$uploaded,$external,$minimal,$output) =
  260:                 &save_changes($cnum,$cdom,$uploaded,$external,$minimal,
  261:                               \%syllabus,\%syllabusfields,\%courseenv);
  262:             if (($env{'form.choice'} eq 'minimal') && 
  263:                 ($minimal eq "/uploaded/$cdom/$cnum/portfolio/syllabus/loncapa.html")) { 
  264:                 delete($env{'form.symb'});
  265:                 delete($env{'request.symb'});
  266:                 $r->internal_redirect("$minimal?editmode=1&forceedit=1");
  267:                 return OK;
  268:             }
  269:             if ($earlyout) {
  270:                 if ($target ne 'tex') {
  271:                     &print_header($r,$cnum,$cdom,$crstype,$allowed,$forceedit,
  272:                                   \%syllabus,\%syllabusfields);
  273:                     $r->print($output.
  274:                               &Apache::loncommon::end_page());
  275:                 }
  276:                 return OK;
  277:             }
  278:         }
  279:     }
  280:     if ($target ne 'tex') {
  281:         &print_header($r,$cnum,$cdom,$crstype,$allowed,$forceedit,\%syllabus,
  282:                       \%syllabusfields);
  283:         $r->print($output);
  284:     }
  285: 
  286: # -------------------------------------------- Determine which fields are shown 
  287: 
  288:     if ($syllabus{'uploaded.fields'}) {
  289:         if ($syllabus{'uploaded.fields'} eq 'none') {
  290:             foreach my $field (keys(%syllabusfields)) {
  291:                 $displayfields{$field} = ' style="display:none;"';
  292:                 $noshow{$field} = 1;
  293:             }
  294:         } else {
  295:             my %included;
  296:             map { $included{$_} = 1; } split(/,/,$syllabus{'uploaded.fields'});
  297:             foreach my $field (keys(%syllabusfields)) {
  298:                 my ($prefix) = split(/_/,$field);
  299:                 if ($included{$prefix}) {
  300:                     $displayfields{$field} = ' style="display:block;"';
  301:                 } else {
  302:                     $displayfields{$field} = ' style="display:none;"';
  303:                     $noshow{$field} = 1;
  304:                 }
  305:             }
  306:         }
  307:     } else {
  308:         foreach my $field (keys(%syllabusfields)) {
  309:             if ($syllabus{$field} ne '') {
  310:                 $displayfields{$field} = ' style="display:block;"';
  311:             } else {
  312:                 $displayfields{$field} = ' style="display:none;"';
  313:             }
  314:         }
  315:     }
  316: 
  317:     if ($allowed) {
  318: #---------------------------------- Print External URL Syllabus Info if editing
  319:         if ($target ne 'tex') {
  320:             my $hostname = &Apache::lonnet::hostname($homeserver);
  321:             my $protocol = $Apache::lonnet::protocol{$homeserver};
  322:             $protocol = 'http' if ($protocol ne 'https');
  323:             my $alias = &Apache::lonnet::use_proxy_alias($r,$homeserver);
  324:             $hostname = $alias if ($alias ne '');
  325:             my $link = $protocol.'://'.$hostname.$r->uri;
  326:             $r->print('<div class="LC_left_float">'
  327:                      .'<span class="LC_help_open_topic LC_info">'
  328:                      .'<span class="LC_info">'
  329:                      .&mt('Public link (no log-in): [_1]','<tt>'.$link.'</tt>')
  330:                      .'&nbsp;</span>'.&Apache::loncommon::help_open_topic('Syllabus_ExtLink')
  331:                      .'</span>'
  332:                      .'</div><div style="padding:0;clear:both;margin:0;border:0"></div>'."\n");
  333:             my $lonhost = $r->dir_config('lonHostID');
  334:             $r->print(&chooser($r,$external,$uploaded,$minimal,$cdom,$cnum,$lonhost,
  335:                                \%syllabusfields,\%syllabus));
  336:         }
  337:     } else {
  338: #--------------------------------------------- Print last update unless editing
  339:         my $lastmod=$syllabus{'uploaded.lastmodified'};
  340:         $lastmod=($lastmod?&Apache::lonlocal::locallocaltime($lastmod):&mt('never'));
  341:         my $who;
  342:         if ($syllabus{'uploaded.lastmodified'}) {
  343:             if (($env{'user.name'} ne 'public') && ($env{'user.domain'} ne 'public')) {
  344:                 $who = &Apache::loncommon::aboutmewrapper(
  345:                        &Apache::loncommon::plainname($syllabus{'uploaded.name'},
  346:                        $syllabus{'uploaded.domain'}),$syllabus{'uploaded.name'},
  347:                        $syllabus{'uploaded.domain'});
  348:             } else {
  349: # Public user?
  350: # Only display name of user, but no link to personal information page
  351:                 $who = &Apache::loncommon::plainname(
  352:                            $syllabus{'uploaded.name'},
  353:                            $syllabus{'uploaded.domain'});
  354:             }
  355:         }
  356:         if ($target ne 'tex') {
  357:             $r->print('<div class="LC_info">'.&mt('Last updated').': '.
  358:                       $lastmod . ' '.
  359:                       ($who ? &mt('by').' '.$who
  360:                            : '' ) .
  361:                       '</div>' );
  362:         } else {
  363:             $r->print('\\\\ '.&mt('Last updated').': '.$lastmod.' '.
  364:                      ($who? &mt('by').'\\\\ '.
  365:                      &Apache::loncommon::plainname($syllabus{'uploaded.name'},$syllabus{'uploaded.domain'})
  366:                      :'')
  367:                     .'\\\\');
  368:         }
  369:     }
  370: 
  371: #-------------------------------------------------------------- Print Headtitle
  372:     if ($target ne 'tex') {
  373:         my $display = 'block';
  374:         if ($external || $uploaded || $minimal) {
  375:             $display = 'none';
  376:         }
  377:         $r->print('<div class="LC_Box" id="template" style="display: '.$display.'">'.
  378:                    '<h2 class="LC_hcell">'.$courseenv{'description'}.'</h2>');
  379:         if ($allowed) {
  380:             $r->print('<div style="margin: 0; float:left;">'.
  381:                       '<h3>'.&Apache::lonnet::domain($cdom,'description').'</h3>'.
  382:                       '</div>');
  383: # Print Help Text if editing at right side of screen
  384:             $r->print('<div style="margin: 0; float:right;">'.
  385:                       &Apache::loncommon::help_open_topic('Uploaded_Templates_TextBoxes',&mt('Help with filling in text boxes')).
  386:                       '</div><br clear="all" />');
  387:         } else {
  388:             $r->print('<h3>'.&Apache::lonnet::domain($cdom,'description').'</h3>');
  389:         }
  390:     } else {
  391:         $r->print('\noindent{\large\textbf{'.$courseenv{'description'}.'}}\\\\\\\\\textbf{'.
  392:         &Apache::lonnet::domain($cdom,'description').'}\\\\');
  393:     }
  394: # -------------------------------------------------------- Get course personnel
  395:     my $hidepersonnel;
  396:     if (($syllabus{'uploaded.fields'}) &&
  397:         (($syllabus{'uploaded.fields'} eq 'none') ||
  398:          ($syllabus{'uploaded.fields'} !~ /000/))) {
  399:         $hidepersonnel = 1;
  400:     }
  401:     if ($target ne 'tex') {
  402:         if ($allowed) {
  403:             my $display = ' style="display:block;"';
  404:             if ($hidepersonnel) {
  405:                 $display = ' style="display:none;"';
  406:             }
  407:             &Apache::lontemplate::print_start_template($r,&mt('Personnel'),'LC_Box',
  408:                                                        'box_000_showpeople',$display);
  409:             $r->print(&get_personnel($r,$target,$cdom,$cnum,$allowed,$crstype,\%syllabus));
  410:             &Apache::lontemplate::print_end_template($r);
  411:         } else {
  412:             unless ($hidepersonnel) {
  413:                 &Apache::lontemplate::print_start_template($r,&mt('Personnel'),'LC_Box');
  414:                 $r->print(&get_personnel($r,$target,$cdom,$cnum,$allowed,$crstype,\%syllabus));  
  415:                 &Apache::lontemplate::print_end_template($r);
  416:             }
  417:         }
  418:     } else {
  419:         unless ($hidepersonnel) {
  420:             $r->print(&get_personnel($r,$target,$cdom,$cnum,$allowed,$crstype,%syllabus));
  421:         }
  422:     }
  423: # -------------------------------------------------------------- Announcements?
  424:     my $day = &Apache::lonannounce::showday(time,2,
  425:              &Apache::lonannounce::readcalendar($cdom.'_'.$cnum));
  426:     my $hidefeeds;
  427:     if (($syllabus{'uploaded.fields'}) &&
  428:         (($syllabus{'uploaded.fields'} eq 'none') ||
  429:          ($syllabus{'uploaded.fields'} !~ /111/))) {
  430:         $hidefeeds = 1;
  431:     }
  432:     if ($target ne 'tex') {
  433:         if ($allowed) {
  434:             my $display = ' style="display:block;"';
  435:             if ($hidefeeds) {
  436:                 $display = ' style="display:none;"';
  437:             }
  438:             &Apache::lontemplate::print_start_template($r,&mt('RSS Feeds and Blogs'),'LC_Box',
  439:                                                        'box_111_showrssfeeds',$display);
  440:             my ($numfeeds,$hiddenfeeds,$rsslinktext);
  441:             my $feeds=&Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit,\$numfeeds,
  442:                                                       \$hiddenfeeds);
  443:             if ($numfeeds) {
  444:                 $r->print($feeds);
  445:                 $rsslinktext = &mt('New RSS Feed or Blog');
  446:             } else {
  447:                 my $msg = '<br />'.
  448:                           &mt("RSS Feeds and Blogs item is not included in a student's view of the syllabus.");
  449:                 if ($hiddenfeeds) {
  450:                     $r->print('<p class="LC_info">'.
  451:                               &mt('All feeds currently hidden').
  452:                               $msg.
  453:                               '</p>');
  454:                 } else {
  455:                     $r->print('<p class="LC_info">'.
  456:                               &mt('No current feeds').
  457:                               $msg.
  458:                               '</p>');
  459:                 }
  460:                 $rsslinktext = &mt('Manage Course RSS Feeds/Blogs');
  461:                 if ($crstype eq 'Community') {
  462:                     $rsslinktext = &mt('Manage Community RSS Feeds/Blogs');
  463:                 }
  464:             }
  465:             my $editurl= &Apache::lonnet::absolute_url().'/adm/'.$cdom.'/'.$cnum.'/_rss.html';
  466:             $r->print( '<a href="'.$editurl.'">'.$rsslinktext.'</a>');
  467:             &Apache::lontemplate::print_end_template($r);
  468:         } else {
  469:             unless ($hidefeeds) {
  470:                 my $feeds = &Apache::lonrss::advertisefeeds($cnum,$cdom,$forceedit);
  471:                 if ($feeds ne '') {
  472:                     &Apache::lontemplate::print_start_template($r,&mt('RSS Feeds and Blogs'),'LC_Box');
  473:                     $r->print($feeds);
  474:                     &Apache::lontemplate::print_end_template($r);
  475:                 }
  476:             }
  477:         }
  478:     } else {
  479:         $r->print(&Apache::lonxml::xmlparse($r,'tex',$day));
  480:     }
  481: # ---------------------------------------------------------------- Get syllabus
  482:     if (($syllabus{'uploaded.lastmodified'}) || ($allowed)) {
  483: 		my $url_include_handler = sub {
  484: 			my ($r, $field, $message, $group, $data_ref, $fields_ref, $target, $allowed, $display) = @_;
  485: 			my %data = %{$data_ref};
  486: 			my %fields = %{$fields_ref};
  487: 			my $urls=$message;
  488: 			$message='';
  489: 			foreach my $filelink (split(/\n/,$urls)) {
  490: 				my $output='';
  491: 			   # embed style?
  492: 				my ($curfext)=($filelink=~/\.([^\.]+)$/);
  493: 				my $embstyle=&Apache::loncommon::fileembstyle($curfext);
  494: 				if (($embstyle eq 'ssi') || ($curfext=~/\/$/)) {# make ssi call and remove everything but the body contents
  495: 					$output=&Apache::lonnet::ssi_body($filelink);
  496: 				} elsif ($embstyle eq 'img') {# embed as an image
  497: 					$output='<img src="'.$filelink.'" />';
  498: 				}
  499: 				if ($output ne '') {
  500: 					   if ($target ne 'tex') {
  501: 						   $message.='<p>'.$output.'</p>';
  502: 					   } else {
  503: 						   $message.=' '.&Apache::lonxml::xmlparse($r,'tex','<p>'.$output.'</p>').' ';
  504: 					   }
  505: 				}
  506: 			}
  507: 			if ($allowed) {
  508: 				 &Apache::lonfeedback::newline_to_br(\$urls);
  509: 				 &Apache::lontemplate::print_start_template($r,$fields{$field}.
  510: 						  &Apache::loncommon::help_open_topic('Syllabus_URLs'),'LC_Box',
  511:                                                   'box_'.$field,$display);
  512: 				 $r->print($urls);
  513: 				 $r->print("<br /><div>");
  514: 				 &Apache::lontemplate::print_textarea_template($r, $data{$field},
  515: 					$field, Apache::lontemplate->RICH_TEXT_ALWAYS_OFF);
  516: 				 &Apache::lontemplate::print_saveall_template($r);                         
  517: 				 $r->print("</div>");
  518: 				 &Apache::lontemplate::print_end_template($r);
  519: 
  520: 			} else {
  521: 				$r->print($message);
  522: 			}
  523: 		};
  524: 		my %custom_hash = ( 'lll_includeurl' => $url_include_handler );
  525: 		&Apache::lontemplate::print_template_fields($r, \%syllabus, \%syllabusfields, 
  526: 			$target, $allowed, Apache::lontemplate->RICH_TEXT_DETECT_HTML, \%custom_hash,
  527:                         undef,\%displayfields,\%noshow);
  528:         if ($allowed) {
  529:             $r->print('</div></form>'.
  530:             &Apache::lonhtmlcommon::htmlareaselectactive());
  531:         }
  532:     } else {
  533:         if ($target ne 'tex') {$r->print('<p class="LC_info">');} else {$r->print('\par ');}
  534:         $r->print(&mt('No syllabus information provided.'));
  535:         if ($target ne 'tex') {$r->print('</p>');}
  536:     }
  537:     if ($target ne 'tex') {
  538:         if ($env{'form.backto'} eq 'coursecatalog') {
  539:             $r->print('<form name="backtocat" method="post" action="/adm/coursecatalog">'.
  540:                       &Apache::lonhtmlcommon::echo_form_input(['backto','courseid']).
  541:                       '</form>');
  542:         }
  543:         $r->print(&Apache::loncommon::end_page());
  544:     } else {
  545:         $r->print('\end{document}');
  546:     }
  547:     return OK;
  548: }
  549: 
  550: sub print_header {
  551:     my ($r,$cnum,$cdom,$crstype,$allowed,$forceedit,$syllabus,$syllabusfields) = @_;
  552:     return unless ((ref($syllabus) eq 'HASH') || (ref($syllabusfields) eq 'HASH'));
  553: # ----------------------------------------------------------------- Make header
  554:     my $rss_link = &Apache::lonrss::rss_link($cnum,$cdom);
  555:     my $js;
  556:     if ($env{'form.backto'} eq 'coursecatalog') {
  557:         $js .= <<"ENDSCRIPT";
  558: 
  559: <script type="text/javascript">
  560: // <![CDATA[
  561: 
  562: function ToCatalog(caller) {
  563:     numidx = getIndexByName('coursenum');
  564:         if (numidx > -1) {
  565:             if (caller != 'details') {
  566:                 document.backtocat.elements[numidx].value = '';
  567:             }
  568:         }
  569:     document.backtocat.submit();
  570: }
  571: 
  572: function getIndexByName(item) {
  573:     for (var i=0;i<document.backtocat.elements.length;i++) {
  574:         if (document.backtocat.elements[i].name == item) {
  575:             return i;
  576:         }
  577:     }
  578:     return -1;
  579: }
  580: 
  581: // ]]>
  582: </script>
  583: 
  584: ENDSCRIPT
  585:     }
  586:     if ($allowed && $forceedit) {
  587:         my $check_uncheck = &Apache::loncommon::check_uncheck_jscript();
  588:         my @fieldnames = sort(keys(%{$syllabusfields}));
  589:         unshift(@fieldnames,'000_showpeople','111_showrssfeeds');
  590:         my (@checked,@unchecked);
  591:         if ($syllabus->{'uploaded.fields'} eq 'none') {
  592:             my $lastidx = scalar(@fieldnames)-1;
  593:             @unchecked = (0..$lastidx);
  594:         } elsif ($syllabus->{'uploaded.fields'}) {
  595:             my %included;
  596:             map { $included{$_} = 1; } split(/,/,$syllabus->{'uploaded.fields'});
  597:             for (my $i=0; $i<@fieldnames; $i++) {
  598:                 my ($prefix) = split(/_/,$fieldnames[$i]);
  599:                 if ($included{$prefix}) {
  600:                     push(@checked,$i);
  601:                 } else {
  602:                     push(@unchecked,$i);
  603:                 }
  604:             }
  605:         } else {
  606:             @checked = (0,1);
  607:             for (my $i=2; $i<@fieldnames; $i++) {
  608:                 if ($syllabus->{$fieldnames[$i]}) {
  609:                     push(@checked,$i);
  610:                 } else {
  611:                     push(@unchecked,$i);
  612:                 }
  613:             }
  614:         }
  615:         my $fieldstr = "var fields = new Array('".join("','",@fieldnames)."');";
  616:         my $checkedstr = "var include = new Array('".join("','",@checked)."');";
  617:         my $uncheckedstr = "var exclude = new Array('".join("','",@unchecked)."');";
  618:         my $invurl = &mt('Invalid URL');
  619:         &js_escape(\$invurl);
  620:         my $urlregexp = <<'ENDREGEXP';
  621: /^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
  622: ENDREGEXP
  623: 
  624:         $js .= <<"ENDSCRIPT";
  625: 
  626: <script type="text/javascript">
  627: // <![CDATA[
  628: 
  629: function toggleEditor(pick) {
  630:     var choices = new Array('template','minimal','url','file','templatebox');
  631:     for (var i=0; i<choices.length; i++) {
  632:         if (((choices[i] == 'templatebox') && (pick == 'template')) ||
  633:             (choices[i] == pick)) {
  634:             document.getElementById(choices[i]).style.display='block';
  635:         } else {
  636:             document.getElementById(choices[i]).style.display='none';
  637:         }
  638:     }
  639:     return;
  640: }
  641: 
  642: var regexp = $urlregexp;
  643: 
  644: function extUrlPreview(caller,protocol) {
  645:     if (document.getElementById(caller)) {
  646:         var url = document.getElementById(caller).value;
  647:         if (regexp.test(url)) {
  648:             var http_regex = /^http\:\/\//gi;
  649:             if ((protocol == 'https') && (http_regex.test(url))) {
  650:                 window.open(url,"syllabuspreview","height=400,width=500,scrollbars=1,resizable=1,menubar=0,location=1");
  651:             } else {
  652:                 openMyModal(url,500,400,'yes');
  653:             }
  654:         } else {
  655:             alert("$invurl");
  656:         }
  657:     }
  658: }
  659: 
  660: function toggleBox(name,caller) {
  661:     if (name == 'all') {
  662:         if (document.syllabus.showfield.length > 0) {
  663:             for (var i=0; i<document.syllabus.showfield.length; i++) {
  664:                 if (document.syllabus.showfield[i].checked) {
  665:                     if (document.getElementById('box_'+document.syllabus.showfield[i].value)) {
  666:                         document.getElementById('box_'+document.syllabus.showfield[i].value).style.display='block';
  667:                     }
  668:                 } else {
  669:                     if (document.getElementById('box_'+document.syllabus.showfield[i].value)) {
  670:                         document.getElementById('box_'+document.syllabus.showfield[i].value).style.display='none';
  671:                     }
  672:                 }
  673:             }
  674:         }
  675:     } else {
  676:         if (caller.checked) {
  677:             if (document.getElementById('box_'+caller.value)) {
  678:                 document.getElementById('box_'+caller.value).style.display='block';
  679:             }
  680:         } else {
  681:             if (document.getElementById('box_'+caller.value)) {
  682:                 document.getElementById('box_'+caller.value).style.display='none';
  683:             }
  684:         }
  685:     }
  686:     return;
  687: }
  688: 
  689: function setTemplateBoxes() {
  690:     $fieldstr
  691:     $checkedstr
  692:     $uncheckedstr
  693:     if (include.length > 0) {
  694:         for (var i=0; i<include.length; i++) {
  695:             if (document.getElementById('showfield_'+include[i])) {
  696:                 document.getElementById('showfield_'+include[i]).checked = true;
  697:                 if (document.getElementById('box_'+fields[include[i]])) {
  698:                     document.getElementById('box_'+fields[include[i]]).style.display='block';
  699:                 }
  700:             }
  701:         }
  702:     }
  703:     if (exclude.length > 0) {
  704:         for (var i=0; i<exclude.length; i++) {
  705:             if (document.getElementById('showfield_'+exclude[i])) {
  706:                 document.getElementById('showfield_'+exclude[i]).checked = false;
  707:                 if (document.getElementById('box_'+fields[exclude[i]])) {
  708:                     document.getElementById('box_'+fields[exclude[i]]).style.display='none';
  709:                 }
  710:             }
  711:         }
  712:     }
  713:     return;
  714: }
  715: 
  716: $check_uncheck
  717: 
  718: // ]]>
  719: </script>
  720: 
  721: ENDSCRIPT
  722:     }
  723:     my $args = {'function'       => undef,
  724:                 'domain'         => $cdom};
  725:     my $forcereg;
  726:     if ($env{'form.register'}) {
  727:         $forcereg = 1;
  728:         $args->{'force_register'} = $forcereg;
  729:     }
  730:     if ($env{'form.backto'} eq 'coursecatalog') {
  731:         &Apache::lonhtmlcommon::clear_breadcrumbs();
  732:         my $brcrum = [{href=>"javascript:ToCatalog();",
  733:                        text=>&mt('Course/Community Catalog'),
  734:                        no_mt=>1}
  735:                      ];
  736:         if ($env{'form.coursenum'} ne '') {
  737:             push(@{$brcrum},
  738:                   {href=>"javascript:ToCatalog('details')",
  739:                    text=>"Course details"});
  740:         }
  741:         push(@{$brcrum},
  742:               {href=>$r->uri,
  743:                text=>"Course syllabus"});
  744:         $args->{'bread_crumbs'} = $brcrum;
  745:     } else {
  746:         &get_breadcrumbs($cdom,$cnum,$crstype,$args);
  747:     }
  748:     if ($allowed) {
  749:         my %loaditem = (
  750:                          onload => 'setTemplateBoxes();',
  751:                        );
  752:         $args->{'add_entries'} = \%loaditem;
  753:     } else {
  754:         if ($env{'request.use_absolute'}) {
  755:             $args->{'use_absolute'} = $env{'request.use_absolute'};
  756:         }
  757:     }
  758:     if ($env{'form.only_body'}) {
  759:         $args->{'only_body'} = 1;
  760:     }
  761:     $args->{'hostname'} = $r->hostname();
  762:     my $start_page =
  763:         &Apache::loncommon::start_page("Syllabus", $rss_link.$js,$args);
  764:     if ($start_page) {
  765:         $r->print($start_page);
  766:     }
  767: }
  768: 
  769: sub get_breadcrumbs{
  770:     my ($cdom,$cnum,$crstype,$args) = @_;
  771:     return unless (ref($args) eq 'HASH');
  772:     if ($env{'form.folderpath'} =~ /^supplemental/) {
  773:         &Apache::loncommon::validate_folderpath(1,'',$cnum,$cdom);  
  774:         my $title = $env{'form.title'};
  775:         if ($title eq '') {
  776:             $title = &mt('Syllabus');
  777:         }
  778:         my $brcrum =
  779:             &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  780:         if (ref($brcrum) eq 'ARRAY') {
  781:             $args->{'bread_crumbs'} = $brcrum;
  782:         }
  783:     } else {
  784:         if ((&Apache::lonnet::is_on_map("public/$cdom/$cnum/syllabus"))
  785:                  && (($env{'form.symb'}) || ($env{'form.register'}))) {
  786:             &Apache::lonhtmlcommon::clear_breadcrumbs();
  787:         } else {
  788:             $args->{'bread_crumbs'} = [
  789:                                         {'href' => "/public/$cdom/$cnum/syllabus",
  790:                                          'text' => 'Syllabus'},
  791:                                       ];
  792:         }
  793:     }
  794:     return;
  795: }
  796: 
  797: sub chooser {
  798:     my ($r,$external,$uploaded,$minimal,$cdom,$cnum,$lonhost,$fields,$values) = @_;
  799:     my %lt = &Apache::lonlocal::texthash(
  800:                  'type'          => 'Syllabus Type',
  801:                  'url'           => 'External URL',
  802:                  'file'          => 'Uploaded file',
  803:                  'minimal'       => 'Minimal template',
  804:                  'template'      => 'Standard template',
  805:                  'templateboxes' => 'Choose template items ... ',
  806:                  'curr'          => 'Current:',
  807:                  'rep'           => 'Replace:',
  808:                  'upl'           => 'Upload:',
  809:                  'pr'            => 'Preview',
  810:                  'save'          => 'Save',
  811:                  'sved'          => 'Save and Edit',
  812:                  'chourl'        => 'External URL',
  813:                  'chofile'       => 'Uploaded syllabus file',
  814:                  'parse'         => 'Upload embedded images/multimedia files if HTML file',
  815:     );
  816:     my %checked = (
  817:                     file     => '',
  818:                     minimal  => '',
  819:                     url      => '',
  820:                     template => '',
  821:                   );
  822:     my %display = (
  823:                     file        => 'none',
  824:                     minimal     => 'none',
  825:                     url         => 'none',
  826:                     templatebox => 'none',
  827:                   );
  828:     my $check = ' checked="checked" ';
  829:     if ($uploaded) {
  830:         $checked{'file'} = $check;
  831:         $display{'file'} = 'block';
  832:     } elsif ($external) {
  833:         $checked{'url'}  = $check;
  834:         $display{'url'} = 'block';
  835:     } elsif ($minimal) {
  836:         $checked{'minimal'} = $check;
  837:         $display{'minimal'} = 'block';
  838:     } else {
  839:         $checked{'template'} = $check;
  840:         $checked{'templatebox'} = $check;
  841:         $display{'templatebox'} = 'block';
  842:     }
  843:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https':'http');
  844: 
  845:     my $output = 
  846:         '<form name="syllabus" method="post" enctype="multipart/form-data" action="">'."\n".
  847:         '<input type="hidden" name="forceedit" value="1" />'."\n".
  848:         '<div class="LC_left_float"><fieldset><legend>'.$lt{'type'}.'</legend>';
  849:     foreach my $item ('minimal','template','url','file') {
  850:         $output .= '<label><input type="radio" name="choice" value="'.$item.'" '.$checked{$item}.' onclick="toggleEditor('."'$item'".')" />'.
  851:                    $lt{$item}.'</label><br />';
  852:     }
  853:     $output .= '</fieldset></div>'."\n".
  854:                '<div id="url" class="LC_left_float" style="display: '.$display{'url'}.'">'."\n".
  855:                '<fieldset><legend>'.$lt{'chourl'}.'</legend><span class="LC_nobreak">'."\n".
  856:                '<a href="javascript:extUrlPreview('."'syllabusurl','$protocol'".');">'.$lt{'pr'}.'</a></span>&nbsp;'."\n".
  857:                '<input type="text" id="syllabusurl" name="externalsyllabus" value="'.$external.'" size="55" />'."\n".
  858:                '&nbsp;<input type="submit" name="storeurl" value="'.$lt{'save'}.'" />'."\n".
  859:                '</fieldset></div>'."\n".
  860:                '<div id="minimal" class="LC_left_float" style="display: '.$display{'minimal'}.'">'."\n".
  861:                '<fieldset><legend>'.$lt{'minimal'}.'</legend>';
  862:     if ($minimal) {
  863:         my ($absurl,$filename,$depbutton) = &syllabus_file_info($r,$minimal,$cnum,$cdom,$lonhost,'minimal');
  864:         $output .= '<a href="javascript:extUrlPreview('."'currminimal'".');">'.$lt{'pr'}.'</a>'.
  865:                    '<input type="hidden" name="minimalfile" value="'.&HTML::Entities::encode($absurl).'?inhibitmenu=yes" id="currminimal" />'.
  866:                    $depbutton;
  867:     } else {
  868:         $output .= &mt('Title of Syllabus Page:').'&nbsp;'.
  869:                    '<input type="text" id="minimaltitle" name="syllabustitle" value="'.&mt('Syllabus').'" size="30" />'."\n".
  870:                    '&nbsp;<input type="submit" name="storeminimal" value="'.$lt{'sved'}.'" />'."\n";
  871:     }
  872:     $output .= '</fieldset></div>'."\n".
  873:                '<div id="file" class="LC_left_float" style="display: '.$display{'file'}.'">'."\n".
  874:                '<fieldset><legend>'.$lt{'file'}.'</legend>';
  875:     if ($uploaded) {
  876:         my ($absurl,$filename,$depbutton) = &syllabus_file_info($r,$uploaded,$cnum,$cdom,$lonhost,'file');
  877:         $output .= '<span class="LC_nobreak">'.$lt{'curr'}.'&nbsp;'.
  878:                    '<input type="hidden" name="uploadedfile" value="'.&HTML::Entities::encode($absurl).'?inhibitmenu=yes" id="currfile" />'.
  879:                    '<a href="javascript:extUrlPreview('."'currfile'".');">'.$filename.'</a></span>'.$depbutton.
  880:                    '<br /><br />'.$lt{'rep'};
  881:     } else {
  882:         $output .= $lt{'upl'};
  883:     }
  884:     $output .= '<br />'."\n".
  885:                '<span class="LC_nobreak">'.
  886:                '<input type="file" name="syllabusfile" size="55" />'."\n".
  887:                '&nbsp;<input type="submit" name="storefile" value="'.$lt{'save'}.'" />'.
  888:                '</span><br />'.
  889:                '<label>'.
  890:                '<input type="checkbox" name="parserflag" checked="checked" />'.
  891:                $lt{'parse'}.
  892:                '</label>'.
  893:                '</fieldset></div>'.
  894:                '<div id="templatebox" class="LC_left_float" style="display: '.
  895:                $display{'templatebox'}.';"><fieldset><legend>'.$lt{'templateboxes'}.
  896:                '&nbsp;<input type="button" value="'.&mt('check all').'" '.
  897:                'onclick="javascript:checkAll('."document.syllabus.showfield".');javascript:toggleBox('."'all'".');" />'.
  898:                ('&nbsp;'x2).
  899:                '<input type="button" value="'.&mt('uncheck all').'" '.
  900:                'onclick="javascript:uncheckAll('."document.syllabus.showfield".');javascript:toggleBox('."'all'".');" />'.
  901:                '</legend>'.
  902:                &fields_check_uncheck($fields,$values).
  903:                '</fieldset><br />'.
  904:                '<input type="submit" name="storesyl" value="'.&mt('Save All').'" />'.
  905:                '</div>';
  906:     $output .= '<div style="padding:0;clear:both;margin:0;border:0"></div>';
  907:     return $output;
  908: }
  909: 
  910: sub syllabus_file_info {
  911:     my ($r,$item,$cnum,$cdom,$lonhost,$context) = @_;
  912:     my $hostname = &Apache::lonnet::hostname($lonhost);
  913:     my $protocol = $Apache::lonnet::protocol{$lonhost};
  914:     $protocol = 'http' if ($protocol ne 'https');
  915:     my $alias = &Apache::lonnet::use_proxy_alias($r,$lonhost);
  916:     $hostname = $alias if ($alias ne '');
  917:     my $absurl = $protocol.'://'.$hostname.$item;
  918:     my ($filename) = ($item =~ m{([^/]+)$});
  919:     my $file=&Apache::lonnet::filelocation("",$item);
  920:     my ($depbutton,$filetype,$editable);
  921:     if ($file =~ /\.(xhtml|xml|tex|html|htm)$/) {
  922:         $filetype=$1;
  923:     }
  924:     if ($filetype) {
  925:         unless ($filetype eq 'tex') {
  926:             $filetype='html';
  927:         }
  928:     }
  929:     if ($filetype eq 'html') {
  930:         my $filecontents=&Apache::lonnet::getfile($file);
  931:         unless ($filecontents eq -1) {
  932:             my $mm = new File::MMagic;
  933:             my $mimetype = $mm->checktype_contents($filecontents);
  934:             if ($mimetype eq 'text/html') {
  935:                 my (%codebase,%allfiles);
  936:                 my $parse_result = &Apache::lonnet::extract_embedded_items($item,\%allfiles,
  937:                                                                            \%codebase,\$filecontents);
  938:                 my $actionurl = "/public/$cdom/$cnum/syllabus";
  939:                 my ($ignore,$num,$numpathchanges,$existing,$mapping) =
  940:                     &Apache::loncommon::ask_for_embedded_content($actionurl,undef,\%allfiles,
  941:                                                                  \%codebase,
  942:                                                                  {'context' => 'rewrites',
  943:                                                                   'ignore_remote_references' => 1,});
  944:                 $editable = 1;
  945:             }
  946:         }
  947:     }
  948:     $depbutton = ('&nbsp;' x 3).
  949:                  &editfile_button($item,$context,$editable).
  950:                  &editbutton_js();
  951:     return ($absurl,$filename,$depbutton);
  952: }
  953: 
  954: sub fields_check_uncheck {
  955:     my ($fields,$values) = @_;
  956:     return unless ((ref($fields) eq 'HASH') && (ref($values) eq 'HASH'));
  957:     my $numinrow = 4;
  958:     my $table;
  959:     my @fieldnames = sort(keys(%{$fields}));
  960:     unshift(@fieldnames,'000_showpeople','111_showrssfeeds'); 
  961:     my $numfields = scalar(@fieldnames);
  962:     my %included;
  963:     if (($values->{'uploaded.fields'}) && ($values->{'uploaded.fields'} ne 'none')) {
  964:         map { $included{$_} = 1; } split(/,/,$values->{'uploaded.fields'});
  965:     }
  966:     for (my $i=0; $i<$numfields; $i++) {
  967:         my ($name,$checked);
  968:         if ($fieldnames[$i] eq '000_showpeople') {
  969:             $name = &mt('Personnel');
  970:         } elsif ($fieldnames[$i] eq '111_showrssfeeds') {
  971:             $name = &mt('RSS Feeds and Blogs');
  972:         } else {
  973:             $name = $fields->{$fieldnames[$i]};
  974:         }
  975:         if ($values->{'uploaded.fields'}) {
  976:             unless ($values->{'uploaded.fields'} eq 'none') {
  977:                 my ($prefix) = split(/_/,$fieldnames[$i]);
  978:                 if ($included{$prefix}) {
  979:                     $checked = ' checked="checked"';
  980:                 }
  981:             }
  982:         } else {
  983:             if ($fieldnames[$i] eq '000_showpeople') {
  984:                 $checked = ' checked="checked"';
  985:             } elsif ($fieldnames[$i] eq '111_showrssfeeds') {
  986:                 $checked = ' checked="checked"';
  987:             } else {
  988:                 if ($values->{$fieldnames[$i]} ne '') {
  989:                     $checked = ' checked="checked"';
  990:                 }
  991:             }
  992:         }
  993:         my $rem = $i%($numinrow);
  994:         if ($rem == 0) {
  995:             if (($i > 0) && ($i < $numfields)) {
  996:                 $table .= '</tr>';
  997:             }
  998:             if ($i < $numfields) {
  999:                 $table .= '<tr>';
 1000:             }
 1001:         }
 1002:         if ($i == $numfields-1) {
 1003:             my $rem = $numfields%($numinrow);
 1004:             my $colsleft = $numinrow - $rem;
 1005:             if ($colsleft > 1) {
 1006:                 $table .= '<td colspan="'.$colsleft.'">';
 1007:             } else {
 1008:                 $table .= '</td>';
 1009:             }
 1010:         } else {
 1011:             $table .= '<td>';
 1012:         }
 1013:         $table .=
 1014:             '<label><input type="checkbox" name="showfield" value="'.$fieldnames[$i].'" '.
 1015:             $checked.' id="showfield_'.$i.'" onclick="javascript:toggleBox('."'$fieldnames[$i]',this".');" />'.
 1016:             $name.'</label></td>'."\n";
 1017:     }
 1018:     if ($table ne '') {
 1019:         my $rem = $numfields%($numinrow);
 1020:         my $colsleft = $numinrow - $rem;
 1021:         if ($colsleft > 1 ) {
 1022:             $table .= '<td colspan="'.$colsleft.'">&nbsp;</td>';
 1023:         } elsif ($colsleft == 1) {
 1024:             $table .= '<td>&nbsp;</td>';
 1025:         }
 1026:         $table = '<table>'.$table.'</tr></table>';
 1027:     }
 1028:     return $table;
 1029: }
 1030: 
 1031: sub get_personnel {
 1032:     my ($r,$target,$cdom,$cnum,$allowed,$crstype,$syllabus) = @_;
 1033:     my (%hiddenroles,%hiddenusers);
 1034:     if (ref($syllabus) eq 'HASH') {
 1035:         if (ref($syllabus->{'personnel'}) eq 'HASH') {
 1036:             if ($syllabus->{'personnel'}{'hiderole'}) {
 1037:                 map { $hiddenroles{$_} = 1; } split(/,/,$syllabus->{'personnel'}{'hiderole'});
 1038:             }
 1039:             if ($syllabus->{'personnel'}{'hideuser'}) {
 1040:                 map { $hiddenusers{$_} = 1; } split(/,/,$syllabus->{'personnel'}{'hideuser'});
 1041:             }
 1042:         }
 1043:     }
 1044:     my $output;
 1045:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum,1);
 1046:     if ($target ne 'tex') {
 1047:         if ($allowed) {
 1048:             $r->print(&Apache::loncommon::start_data_table().
 1049:                       &Apache::loncommon::start_data_table_header_row().
 1050:                       '<th>'.&mt('Role hidden?').'</th><th>'.&mt('Role').'</th>'.
 1051:                       '<th>'.&mt('Personnel (hidden if checked)').'</th>'.
 1052:                       &Apache::loncommon::end_data_table_header_row());
 1053:         } else {
 1054:             $r->print(&Apache::lonhtmlcommon::start_pick_box());
 1055:         }
 1056:     } else {
 1057:         $r->print('\begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}\hline');
 1058:     }
 1059:     my @personnel=sort(keys(%coursepersonnel));
 1060:     my $lastpers=$personnel[$#personnel];
 1061:     foreach my $element (@personnel) {
 1062:         unless ($allowed) {
 1063:             next if ($hiddenroles{$element}) 
 1064:         }
 1065:         my ($role,$sec);
 1066:         if ($element =~ /:/) {
 1067:             ($role,$sec) = split(/:/,$element);
 1068:         } else {
 1069:             $role = $element;
 1070:         }
 1071:         my $roletext = &Apache::lonnet::plaintext($role,$crstype);
 1072:         if ($sec) {
 1073:             $roletext .=' ('.&Apache::lonlocal::mt('Section [_1]',$sec).')';
 1074:         }
 1075:         if ($target ne 'tex') {
 1076:             if ($allowed) {
 1077:                 my $checked;
 1078:                 if ($hiddenroles{$element}) {
 1079:                     $checked = ' checked="checked"';
 1080:                 }
 1081:                 $r->print(&Apache::loncommon::start_data_table_row().
 1082:                           '<td>'.
 1083:                           '<input type="checkbox" name="hiderole" value="'.$element.'" '.
 1084:                           $checked.' />'.
 1085:                           '</td><td>'.$roletext.'</td><td>');
 1086:             } else {
 1087:                 $r->print(&Apache::lonhtmlcommon::row_title($roletext));
 1088:             }
 1089:         } else {
 1090:             $r->print(' '.&Apache::lonxml::xmlparse($r,'tex',$roletext).' & ');
 1091:         }
 1092:         my @coursepersonlist;
 1093:         foreach my $user (split(/\,/,$coursepersonnel{$element})) {
 1094:             my ($puname,$pudom)=split(/\:/,$user);
 1095:             if ($target ne 'tex') {
 1096:                 my $courseperson = &Apache::loncommon::plainname($puname,$pudom);
 1097:                 my $checked;
 1098:                 if ($hiddenusers{$element.'&'.$puname.':'.$pudom}) {
 1099:                     $checked = ' checked="checked"';
 1100:                 }
 1101:                 if ($allowed) {
 1102:                     my $item = '<span class="LC_nobreak"><label>'.
 1103:                                '<input type="checkbox" name="hideuser" value="'.$element.'&amp;'.$puname.':'.$pudom.'"'.$checked.' />'.
 1104:                                &Apache::loncommon::aboutmewrapper($courseperson
 1105: ,
 1106:                                                $puname,$pudom).
 1107:                                '</label>'.('&nbsp;'x2).'</span> ';
 1108:                     push(@coursepersonlist,$item);
 1109: 
 1110:                 } else {
 1111:                     next if ($hiddenusers{$element.'&'.$puname.':'.$pudom});
 1112:                     if (($env{'user.name'} eq '') || ($env{'user.name'} eq 'public') ||
 1113:                         ($env{'user.domain'} eq '') || ($env{'user.domain'} eq 'public')) {
 1114:                         push(@coursepersonlist,$courseperson);
 1115:                     } else {
 1116:                         push(@coursepersonlist,&Apache::loncommon::aboutmewrapper($courseperson,
 1117:                                                $puname,$pudom));
 1118:                     }
 1119:                 }
 1120:             } else {
 1121:                 push(@coursepersonlist,&Apache::loncommon::plainname($puname,
 1122:                               $pudom).' ');
 1123:             }
 1124:         }
 1125:         if ($allowed) {
 1126:             $r->print(join('',@coursepersonlist));
 1127:         } else {
 1128:             $r->print(join(', ',@coursepersonlist));
 1129:         }
 1130:         if ($target ne 'tex') {
 1131:             if ($allowed) {
 1132:                 $r->print('</td>'.&Apache::loncommon::end_data_table_row());
 1133:             } else {
 1134:                 my $lastclose=$element eq $lastpers?1:0;
 1135:                 $r->print(&Apache::lonhtmlcommon::row_closure($lastclose));
 1136:             }
 1137:         } else {
 1138:             $r->print('\\\\ \hline');
 1139:         }
 1140:     }
 1141:     if ($target ne 'tex') {
 1142:         if ($allowed) {
 1143:             $r->print(&Apache::loncommon::end_data_table());
 1144:         } else {
 1145:             $r->print(&Apache::lonhtmlcommon::end_pick_box());
 1146:         }
 1147:     } else {
 1148:         $r->print('\end{tabular}\\\\');
 1149:     }
 1150:     return;
 1151: }
 1152: 
 1153: sub save_changes {
 1154:     my ($cnum,$cdom,$uploaded,$external,$minimal,$syllabus,$syllabusfields,$courseenv) = @_;
 1155:     my ($earlyout,$output);
 1156:     unless ((ref($syllabus) eq 'HASH') && (ref($syllabusfields) eq 'HASH') ||
 1157:             (ref($courseenv) eq 'HASH')) {
 1158:         return ($earlyout,$uploaded,$external,$minimal,$output);
 1159:     }
 1160:     my ($was_ext,$is_ext,$putres);
 1161:     if ($external) {
 1162:         $was_ext = $external;
 1163:     }
 1164:     if (($env{'form.deleteuploaded_file'}) || ($env{'form.deleteuploaded_minimal'})) {
 1165:         my %storehash;
 1166:         if (($env{'form.choice'} eq 'file') && 
 1167:             ($env{'form.deleteuploaded_file'}) && ($uploaded =~ /\w/)) {
 1168:             if ($courseenv->{'uploadedsyllabus'} =~ m{^\Q/uploaded/$cdom/$cnum/portfolio\E(/syllabus/.+)$}) {
 1169:                 my $filename = $1;
 1170:                 &update_access_permissions($cdom,$cnum,$filename);
 1171:             }
 1172:             &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.uploadedsyllabus');
 1173:             &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.externalsyllabus');
 1174:             $storehash{'uploadedsyllabus'} = '';
 1175:             $storehash{'externalsyllabus'} = '';
 1176:             $putres = &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum);
 1177:             undef($uploaded);
 1178:             undef($external);
 1179:         } elsif (($env{'form.choice'} eq 'minimal') &&
 1180:                  ($env{'form.deleteuploaded_minimal'}) && ($minimal =~ /\w/)) {
 1181:             my $minimalurl = "/uploaded/$cdom/$cnum/portfolio/syllabus/loncapa.html";
 1182:             if ($courseenv->{'minimalsyllabus'} eq "$minimalurl") {
 1183:                 my $filecontents=&Apache::lonnet::getfile(&Apache::lonnet::filelocation("",$minimalurl));
 1184:                 unless ($filecontents eq -1) {
 1185:                     $env{'form.output'} = $filecontents; 
 1186:                     &Apache::lonnet::finishuserfileupload($cnum,$cdom,'output',
 1187:                                                           'portfolio/syllabus/loncapa.html.bak');
 1188:                     $minimalurl = &default_minimal_syllabus($cnum,$cdom);
 1189:                 }
 1190:             }
 1191:             &update_access_permissions($cdom,$cnum,'/syllabus/loncapa.html');
 1192:             &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.externalsyllabus');
 1193:             &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.minimalsyllabus');
 1194:             $storehash{'externalsyllabus'} = '';
 1195:             $storehash{'minimalsyllabus'} = '';
 1196:             $putres = &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum);
 1197:             undef($external);
 1198:             undef($minimal);
 1199:         }
 1200:     } elsif ($env{'form.choice'} eq 'template') {
 1201: #store what the user typed in to the template
 1202:         my @shown = &Apache::loncommon::get_env_multiple('form.showfield');
 1203:         $syllabus->{'uploaded.fields'} = '';
 1204:         if (@shown == 0) {
 1205:             $syllabus->{'uploaded.fields'} = 'none';
 1206:         } else {
 1207:             foreach my $field (sort(@shown)) {
 1208:                 if (($field eq '000_showpeople') ||
 1209:                     ($field eq '111_showrssfeeds') ||
 1210:                     ($syllabusfields->{$field})) {
 1211:                     my ($prefix) = split(/_/,$field);
 1212:                     $syllabus->{'uploaded.fields'} .= $prefix.',';
 1213:                 }
 1214:                 if ($field eq '000_showpeople') {
 1215:                     my @hideusers = &Apache::loncommon::get_env_multiple('form.hideuser');
 1216:                     my @hideroles = &Apache::loncommon::get_env_multiple('form.hiderole');
 1217:                     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum,1);
 1218:                     my %personnel;
 1219:                     foreach my $key (keys(%coursepersonnel)) {
 1220:                         map { $personnel{$key}{$_} = 1; } split(/,/,$coursepersonnel{$key});
 1221:                     }
 1222:                     %{$syllabus->{'personnel'}} = ();
 1223:                     $syllabus->{'personnel'}{'hideuser'} = '';
 1224:                     $syllabus->{'personnel'}{'hiderole'} = '';
 1225:                     foreach my $role (@hideroles) {
 1226:                         if (exists($personnel{$role})) {
 1227:                             $syllabus->{'personnel'}{'hiderole'} .= $role.',';
 1228:                         }
 1229:                     }
 1230:                     foreach my $item (@hideusers) {
 1231:                         my ($role,$user) = split(/\&/,$item);
 1232:                         if (ref($personnel{$role}) eq 'HASH') {
 1233:                             if ($personnel{$role}{$user}) {
 1234:                                 $syllabus->{'personnel'}{'hideuser'} .= $item.',';
 1235:                             }
 1236:                         }
 1237:                     }
 1238:                     $syllabus->{'personnel'}{'hideuser'} =~ s/,$//;
 1239:                     $syllabus->{'personnel'}{'hiderole'} =~ s/,$//;
 1240:                 }
 1241:             }
 1242:             $syllabus->{'uploaded.fields'} =~ s/,$//;
 1243:         }
 1244:         foreach my $syl_field (keys(%{$syllabusfields})) {
 1245:             my $field=$env{'form.'.$syl_field};
 1246:             chomp($field);
 1247:             my $gateway = Apache::lonhtmlgateway->new();
 1248:             $field = $gateway->process_incoming_html($field,1);
 1249:                             #here it will be stored
 1250:             $syllabus->{$syl_field}=$field;
 1251:             if ($syl_field eq 'lll_includeurl') { # clean up included URLs
 1252:                 my $field='';
 1253:                 foreach my $value (split(/\n/,$syllabus->{$syl_field})) {
 1254:                     my $url=$value;
 1255: # get rid of leading and trailing spaces
 1256:                     $url=~s/^\s+//;
 1257:                     $url=~s/\s+$//;
 1258:                     if ($url=~m|^https?\://([^/]+)/(.+)$|) {
 1259:                         my $host = $1;
 1260:                         my $remainder=$2;
 1261: # remove the hostname from internal URLs
 1262:                         my $hostname = &Apache::lonnet::hostname($host);
 1263:                         my %all_hostnames = &Apache::lonnet::all_hostnames();
 1264:                         foreach my $possible_host (keys(%all_hostnames)) {
 1265:                             if ($possible_host =~ /\Q$hostname\E/i) {
 1266:                                 $url=$remainder;
 1267:                             }
 1268:                         }
 1269:                     }
 1270: # norm internal URLs
 1271:                     unless ($url=~/^https?\:/) {
 1272:                         $url=&Apache::lonnet::clutter($url);
 1273:                     }
 1274: # re-assemble field
 1275:                     if ($url) {
 1276:                         $field.=$url."\n";
 1277:                     }
 1278:                 }
 1279:                 $syllabus->{$syl_field}=$field;
 1280:             }
 1281:         }
 1282:         my $now = time;
 1283:         $syllabus->{'uploaded.domain'}=$env{'user.domain'};
 1284:         $syllabus->{'uploaded.name'}=$env{'user.name'};
 1285:         $syllabus->{'uploaded.lastmodified'} = $now;
 1286:         $putres = &Apache::lonnet::put('syllabus',$syllabus,$cdom,$cnum);
 1287:         if ($putres eq 'ok') {
 1288:             ($uploaded,$minimal,$external) = 
 1289:                 &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded,
 1290:                                      $minimal,$external);
 1291:             $output = '<div>'.
 1292:                       &Apache::lonhtmlcommon::confirm_success(&mt('Template saved.')).
 1293:                       '</div>';
 1294:         } else {
 1295:             $output = '<div class="LC_error">'.
 1296:                       &mt('An error occurred storing the template: [_1]',$putres).
 1297:                       '</div>';
 1298:         }
 1299:     } elsif ($env{'form.choice'} eq 'url') {
 1300:         if ($env{'form.externalsyllabus'} =~ m{^https?://}) {
 1301:             if ($env{'form.externalsyllabus'} eq $external) {
 1302:                 $output = '<div class="LC_info">'.
 1303:                           &mt('External URL unchanged.').
 1304:                           '</div>';
 1305:                 ($uploaded,$minimal,$external) =
 1306:                     &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded,
 1307:                                          $minimal,$external);
 1308:             } else {
 1309:                 $external=$env{'form.externalsyllabus'};
 1310:                 $external =~ s/(`)//g;
 1311:                 $putres =
 1312:                     &Apache::lonnet::put('environment',{externalsyllabus=>$external},
 1313:                                          $cdom,$cnum);
 1314:                 if ($putres eq 'ok') {
 1315:                     &Apache::lonnet::appenv({'course.'.$env{'request.course.id'}.'.externalsyllabus' => $external});
 1316:                     $output = '<div>'.
 1317:                               &Apache::lonhtmlcommon::confirm_success(&mt('External URL saved.')).
 1318:                              '</div>';
 1319:                     ($uploaded,$minimal,$external) =
 1320:                         &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded,
 1321:                                              $minimal,$external);
 1322:                 } else {
 1323:                     $output = '<div class="LC_error">'.
 1324:                               &mt('An error occurred storing the external URL: [_1]',$putres).
 1325:                               '</div>';
 1326:                 }
 1327:             }
 1328:             $is_ext = $external;
 1329:         } else {
 1330:             $output = '<div class="LC_error">'.
 1331:                       &mt('External URL not saved -- invalid URL.').
 1332:                       '</div>';
 1333:         }
 1334:     } elsif (($env{'form.choice'} eq 'file') || ($env{'form.choice'} eq 'minimal')) {
 1335:         # Process file upload - phase one - upload and parse primary file.
 1336:         my ($upload_result,$uploadphase,$url,$needlink,$error,$errormsg);
 1337:         if ($env{'form.choice'} eq 'file') {
 1338:             if ($env{'form.syllabusfile.filename'}) {
 1339:                 my %allfiles = ();
 1340:                 my %codebase = ();
 1341:                 ($url,$needlink) = &process_upload(\$output,$cnum,$cdom,
 1342:                                                    \%allfiles,\%codebase);
 1343:             } else {
 1344:                 $output = '<div class="LC_info">'.
 1345:                           &mt('No file uploaded').
 1346:                           '</div>';
 1347:             }
 1348:         } elsif ($env{'form.choice'} eq 'minimal') {
 1349:             $url = "/uploaded/$cdom/$cnum/portfolio/syllabus/loncapa.html";
 1350:             my $filecontents=&Apache::lonnet::getfile(&Apache::lonnet::filelocation("",$url));
 1351:             if ($filecontents eq -1) {
 1352:                 $url = &default_minimal_syllabus($cnum,$cdom);
 1353:             }
 1354:         }
 1355:         if ($url =~ m{^/uploaded/\Q$cdom\E/\Q$cnum\E.*/[^/]+$}) {
 1356:             my $exturl = &home_http_host($cdom,$cnum);
 1357:             if ($exturl) {
 1358:                 $exturl .= $url;
 1359:             }
 1360:             my %storehash;
 1361:             if ($env{'form.choice'} eq 'minimal') {
 1362:                 $storehash{'minimalsyllabus'} = $url;
 1363:             } else {
 1364:                 $storehash{'uploadedsyllabus'} = $url;
 1365:             }
 1366:             if ($exturl) {
 1367:                 $storehash{'externalsyllabus'} = $exturl;
 1368:                 if ($exturl =~ /\.(html?|txt|js|css)$/) {
 1369:                     $exturl .= '?inhibitmenu=yes';
 1370:                 }
 1371:             } else {
 1372:                 $storehash{'externalsyllabus'} = '',
 1373:             }
 1374:             $putres =
 1375:                 &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum);
 1376:             if ($putres eq 'ok') {
 1377:                 &Apache::lonnet::make_public_indefinitely($url);
 1378:                 foreach my $key (keys(%storehash)) {
 1379:                     &Apache::lonnet::appenv({'course.'.$env{'request.course.id'}.'.'.$key => $storehash{$key}});
 1380:                 }
 1381:                 if ($env{'form.choice'} eq 'minimal') {
 1382:                     $minimal = $url;
 1383:                 } else {
 1384:                     $uploaded = $url;
 1385:                 }
 1386:                 if ($needlink) {
 1387:                     $output .= &return_to_editor($cdom,$cnum);
 1388:                     $earlyout = 1;
 1389:                 }
 1390:                 ($uploaded,$minimal,$external) =
 1391:                     &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded,
 1392:                                          $minimal,$external);
 1393:             } else {
 1394:                 $error = 1;
 1395:                 $errormsg = $putres;
 1396:             }
 1397:         } else {
 1398:             $error = 1;
 1399:         }
 1400:         if ($error) {
 1401:             $output = '<div class="LC_error">';
 1402:             if ($env{'form.choice'} eq 'minimal') {
 1403:                 $output = &mt('An error occurred creating the minimal template file [_1]',$errormsg);
 1404:             } else {
 1405:                 $output = &mt('An error occurred storing the uploaded file [_1]',$errormsg);
 1406:             }
 1407:             $output .= '</div>';
 1408:         }
 1409:     } elsif ($env{'form.phase'} eq 'upload_embedded') {
 1410:         # Process file upload - phase two - upload embedded objects
 1411:         my $uploadphase = 'check_embedded';
 1412:         my $primaryurl = &HTML::Entities::encode($env{'form.primaryurl'},'<>&"');
 1413:         my $state = &embedded_form_elems($uploadphase,$primaryurl);
 1414:         my $url_root = '/uploaded/'.$cdom.'/'.$cnum;
 1415:         my $actionurl = "/public/$cdom/$cnum/syllabus";
 1416:         my ($result,$flag,$numpathchgs) =
 1417:             &Apache::loncommon::upload_embedded('syllabus','portfolio/syllabus',
 1418:                 $cnum,$cdom,'/userfiles',$url_root,undef,undef,undef,$state,
 1419:                 $actionurl);
 1420:         unless ($numpathchgs) {
 1421:             my $modres =
 1422:                 &Apache::loncommon::modify_html_refs('syllabus','portfolio/syllabus',
 1423:                                                      $cnum,$cdom,
 1424:                                                      '/userfiles',$env{'form.primaryurl'});
 1425:             $result .= $modres;
 1426:         }
 1427:         $output = $result.&return_to_editor($cdom,$cnum);
 1428:         $earlyout = 1;
 1429:     } elsif ($env{'form.phase'} eq 'check_embedded') {
 1430:         # Process file upload - phase three - modify references in HTML file
 1431:         my $uploadphase = 'modified_orightml';
 1432:         my $result =
 1433:             &Apache::loncommon::modify_html_refs('syllabus','portfolio/syllabus',
 1434:                                                  $cnum,$cdom,
 1435:                                                  '/userfiles',$env{'form.primaryurl'});
 1436:         $output = $result.&return_to_editor($cdom,$cnum);
 1437:         $earlyout = 1;
 1438:     }
 1439:     if ($putres eq 'ok') {
 1440:         if ((($is_ext) && ($is_ext ne $was_ext)) || ($was_ext)) {
 1441:             my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
 1442:             unless ($chome eq 'no_host') {
 1443:                 my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 1444:                 if (ref($crsinfo{$cdom.'_'.$cnum}) eq 'HASH') {
 1445:                     if ($external =~ m{^http://}) {
 1446:                         $crsinfo{$cdom.'_'.$cnum}{'extsyllplain'} = 1;
 1447:                     } elsif ($crsinfo{$cdom.'_'.$cnum}{'extsyllplain'}) {
 1448:                         delete($crsinfo{$cdom.'_'.$cnum}{'extsyllplain'});
 1449:                     }
 1450:                     &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
 1451:                 }
 1452:             }
 1453:         }
 1454:     }
 1455:     return ($earlyout,$uploaded,$external,$minimal,$output);
 1456: }
 1457: 
 1458: sub default_minimal_syllabus {
 1459:     my ($cnum,$cdom) = @_;
 1460:     my $title;
 1461:     if ($env{'form.syllabustitle'}) {
 1462:         $title = $env{'form.syllabustitle'};
 1463:         $title =~ s{`}{}g;
 1464:         $title=~s/^\s+//;
 1465:         $title=~s/\s+$//;
 1466:     }
 1467:     if ($title eq '') {
 1468:         $title = &mt('Syllabus');
 1469:     }
 1470:     my $initialtext = &mt('Replace with your own content.');
 1471:     my $newhtml = <<END;
 1472: <html>
 1473: <head>
 1474: <title>$title</title>
 1475: </head>
 1476: <body bgcolor="#ffffff">
 1477: <h2>$title</h2>
 1478: $initialtext
 1479: </body>
 1480: </html>
 1481: END
 1482:     $env{'form.output'}=$newhtml;
 1483:     return &Apache::lonnet::finishuserfileupload($cnum,$cdom,'output',
 1484:                                                  'portfolio/syllabus/loncapa.html');
 1485: }
 1486: 
 1487: sub update_syllabus_env {
 1488:     my ($cdom,$cnum,$courseenv,$saved,$uploaded,$minimal,$external) = @_;
 1489:     return ($uploaded,$minimal,$external) unless(ref($courseenv) eq 'HASH');
 1490:     my $now = time;
 1491:     my (@envkeys,%storehash);
 1492:     if ($saved eq 'template') {
 1493:         if ($uploaded || $env{'course.'.$env{'request.course.id'}.'.uploadedsyllabus'}) {
 1494:             push(@envkeys,'uploaded');
 1495:         }
 1496:         if ($minimal || $env{'course.'.$env{'request.course.id'}.'.minimalsyllabus'}) {
 1497:             push(@envkeys,'minimal');
 1498:         }
 1499:         if ($external || $env{'course.'.$env{'request.course.id'}.'.externalsyllabus'}) {
 1500:             push(@envkeys,'external');
 1501:         }
 1502:         $storehash{'updatedsyllabus'} = $now;
 1503:         &Apache::lonnet::appenv({'course.'.$env{'request.course.id'}.'.updatedsyllabus' => $now
 1504: });
 1505:     } elsif ($saved eq 'url') {
 1506:         my $prefix = &home_http_host($cdom,$cnum);
 1507:         if ($external =~ m{^\Q$prefix/uploaded/$cdom/$cnum/portfolio/syllabus/\E(.+)$}) {
 1508:             my $file = $1;
 1509:             if ($file eq 'loncapa.html') {
 1510:                 if ($uploaded || $env{'course.'.$env{'request.course.id'}.'.uploadedsyllabus'}) {
 1511:                     push(@envkeys,'uploaded');
 1512:                 }
 1513:             } elsif ($minimal || $env{'course.'.$env{'request.course.id'}.'.minimalsyllabus'}) {
 1514:                 push(@envkeys,'minimal');
 1515:             }
 1516:         } else {
 1517:             if ($uploaded || $env{'course.'.$env{'request.course.id'}.'.uploadedsyllabus'}) {
 1518:                 push(@envkeys,'uploaded');
 1519:             }
 1520:             if ($minimal || $env{'course.'.$env{'request.course.id'}.'.minimalsyllabus'}) {
 1521:                 push(@envkeys,'minimal');
 1522:             }
 1523:         }
 1524:     } elsif ($saved eq 'file') {
 1525:         if ($minimal || $env{'course.'.$env{'request.course.id'}.'.minimalsyllabus'}) {
 1526:             push(@envkeys,'minimal');
 1527:         }
 1528:     } elsif ($saved eq 'minimal') {
 1529:         if ($uploaded || $env{'course.'.$env{'request.course.id'}.'.uploadedsyllabus'}) {
 1530:             push(@envkeys,'uploaded');
 1531:         }
 1532:     }
 1533:     if (@envkeys > 0) {
 1534:         foreach my $item (@envkeys) {
 1535:             my $key = $item.'syllabus';
 1536:             if ($courseenv->{$key} ne '') {
 1537:                 &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.'.$key);
 1538:                 if ($item eq 'minimal') {
 1539:                     &update_access_permissions($cdom,$cnum,'/syllabus/loncapa.html');
 1540:                 } elsif ($item eq 'uploaded') {
 1541:                     if ($courseenv->{$key} =~ m{^\Q/uploaded/$cdom/$cnum/portfolio\E(/syllabus/.+)$}) {
 1542:                         my $filename = $1;
 1543:                         &update_access_permissions($cdom,$cnum,$filename);
 1544:                     }
 1545:                 }
 1546:                 $storehash{$key} = '';
 1547:             } elsif ($env{'course.'.$env{'request.course.id'}.'.'.$key} ne '') {
 1548:                 &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.'.$key);
 1549:             }
 1550:             if ($item eq 'uploaded') {
 1551:                 undef($uploaded);
 1552:             }
 1553:             if ($item eq 'external') {
 1554:                 undef($external);
 1555:             }
 1556:             if ($item eq 'minimal') {
 1557:                 undef($minimal);
 1558:             }
 1559:         }
 1560:     }
 1561:     if (keys(%storehash) > 0) {
 1562:         &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum);
 1563:     }
 1564:     return ($uploaded,$minimal,$external);
 1565: }
 1566: 
 1567: sub update_access_permissions {
 1568:     my ($cdom,$cnum,$file_name) = @_;
 1569:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($cdom,$cnum);
 1570:     my %access_controls =
 1571:         &Apache::lonnet::get_access_controls($current_permissions,'',$file_name);
 1572:     if (keys(%access_controls) > 0) {
 1573:         my %changes;
 1574:         foreach my $key (keys(%{$access_controls{$file_name}})) {
 1575:             $changes{'delete'}{$key} = 1;
 1576:         }
 1577:         if (keys(%changes) > 0) {
 1578:             my ($outcome,$deloutcome,$new_values,$translation) =
 1579:                 &Apache::lonnet::modify_access_controls($file_name,\%changes,
 1580:                                                         $cdom,$cnum);
 1581:         }
 1582:     }
 1583:     return; 
 1584: }
 1585: 
 1586: sub home_http_host {
 1587:     my ($cdom,$cnum) = @_;
 1588:     my $home=&Apache::lonnet::homeserver($cnum,$cdom);
 1589:     if ($home ne 'no_host') {
 1590:         my $hostname = &Apache::lonnet::hostname($home);
 1591:         my $protocol = $Apache::lonnet::protocol{$home};
 1592:         $protocol = 'http' if ($protocol ne 'https');
 1593:         return $protocol.'://'.$hostname;
 1594:     }
 1595:     return;
 1596: }
 1597: 
 1598: sub process_upload {
 1599:     my ($upload_output,$cnum,$cdom,$allfiles,$codebase) = @_;
 1600:     my ($parseaction,$showupload,$mimetype);
 1601:     my $dest = 'portfolio/syllabus';
 1602:     if ($env{'form.parserflag'}) {
 1603:         $parseaction = 'parse';
 1604:     }
 1605:     my $url=&Apache::lonnet::userfileupload('syllabusfile','syllabus',$dest,
 1606:                                             $parseaction,$allfiles,
 1607:                                             $codebase,undef,undef,undef,undef,
 1608:                                             undef,undef,\$mimetype);
 1609:     if ($url =~ m{^/uploaded/\Q$cdom\E/\Q$cnum\E.*/([^/]+)$}) {
 1610:         my $stored = $1;
 1611:         $showupload = '<p>'.&mt('Uploaded [_1]',
 1612:                                 '<span class="LC_filename">'.$stored.'</span>').
 1613:                       '</p>';
 1614:     } else {
 1615:         my ($filename) = ($env{'form.syllabusfile.filename'} =~ m{([^/]+)$});
 1616:         $$upload_output = '<div class="LC_error" id="uploadfileresult">'.
 1617:                           &mt('Unable to save file [_1].',
 1618:                               '<span class="LC_filename">'.$filename.'</span>').
 1619:                           '</div>';
 1620:         return (); 
 1621:     }
 1622:     my $needlink;
 1623:     if (($parseaction eq 'parse') && ($mimetype eq 'text/html')) {
 1624:         $$upload_output = $showupload;
 1625:         my $total_embedded = scalar(keys(%{$allfiles}));
 1626:         if ($total_embedded > 0) {
 1627:             my $uploadphase = 'upload_embedded';
 1628:             my $primaryurl = &HTML::Entities::encode($url,'<>&"');
 1629:             my $state = &embedded_form_elems($uploadphase,$primaryurl);
 1630:             my $actionurl = "/public/$cdom/$cnum/syllabus";
 1631:             my ($embedded,$num,$numpathchanges,$existing);
 1632:             ($embedded,$num,$numpathchanges,$existing) =
 1633:                 &Apache::loncommon::ask_for_embedded_content($actionurl,$state,
 1634:                                                              $allfiles,$codebase,
 1635:                                                             {'error_on_invalid_names'   => 1,
 1636:                                                              'ignore_remote_references' => 1,});
 1637:             if ($embedded) {
 1638:                 $needlink = 1;
 1639:                 if ($num) {
 1640:                     $$upload_output .=
 1641:                         '<p>'.&mt('This file contains embedded multimedia objects, which need to be uploaded.').'</p>'.$embedded;
 1642:                 } elsif ($numpathchanges) {
 1643:                     $$upload_output .= $embedded;
 1644:                 } else {
 1645:                     $$upload_output .= $embedded;
 1646:                     &Apache::loncommon::modify_html_refs('syllabus','portfolio/syllabus',
 1647:                                                          $cnum,$cdom,'/userfiles',$url);
 1648:                 }
 1649:             } else {
 1650:                 $$upload_output .= &mt('Embedded item(s) already present, so no additional upload(s) required').'<br />';
 1651:                 &Apache::loncommon::modify_html_refs('syllabus','portfolio/syllabus',
 1652:                                                      $cnum,$cdom,'/userfiles',$url);
 1653: 
 1654:             }
 1655:         } else {
 1656:             $$upload_output .= &mt('No embedded items identified').'<br />';
 1657:         }
 1658:         $$upload_output = '<div id="uploadfileresult">'.$$upload_output.'</div>';
 1659:     }
 1660:     return ($url,$needlink);
 1661: }
 1662: 
 1663: sub embedded_form_elems {
 1664:     my ($phase,$primaryurl) = @_;
 1665:     return <<STATE;
 1666:     <input type="hidden" name="forceedit" value="1" />
 1667:     <input type="hidden" name="cmd" value="upload_embedded" />
 1668:     <input type="hidden" name="phase" value="$phase" />
 1669:     <input type="hidden" name="primaryurl" value="$primaryurl" />
 1670: STATE
 1671: }
 1672: 
 1673: sub return_to_editor {
 1674:     my ($cdom,$cnum) = @_;
 1675:     my $actionurl = "/public/$cdom/$cnum/syllabus";
 1676:     return '<p><form name="backtoeditor" method="post" action="'.$actionurl.'" />'.
 1677:            '<input type="hidden" name="forceedit" value="1" />'."\n".
 1678:            '<a href="javascript:document.backtoeditor.submit();">'.&mt('Return to Editor').
 1679:            '</a></p>';
 1680: }
 1681: 
 1682: sub editfile_button {
 1683:     my ($url,$context,$editable) = @_;
 1684:     my $edittext=&mt('Edit');
 1685:     my $deltext=&mt('Delete');
 1686:     my $output;
 1687:     if ($editable) {
 1688:         $output = '
 1689:                 <input type="button" value="'.$edittext.'" onclick="javascript:gotoeditor('."'$url'".');" name="edit_'.$context.'" />
 1690:                 &nbsp;&nbsp;&nbsp;';
 1691:     }
 1692:     $output .= '
 1693:                 <input type="button" value="'.$deltext.'" onclick="javascript:dodelete('."'$context'".');" name="del_'.$context.'" />
 1694:                 <input type="hidden" value="" name="deleteuploaded_'.$context.'" id="deleteuploaded_'.$context.'" />
 1695:     ';
 1696:     return $output;
 1697: }
 1698: 
 1699: sub editbutton_js {
 1700:     my %js_lt = &Apache::lonlocal::texthash(
 1701:                min    => 'Are you sure you want to delete the contents of the syllabus template?',
 1702:                file   => 'Are you sure you want to delete the uploaded syllabus file?',
 1703:                noundo => 'This action cannot be reversed.'
 1704:              );
 1705:     &js_escape(\%js_lt);
 1706:     return <<ENDJS;
 1707:                 <script type="text/javascript">
 1708:                 // <![CDATA[
 1709:                   function gotoeditor(url) {
 1710:                       document.location.href = url+'?editmode=1&forceedit=1';
 1711:                   }
 1712:                   function dodelete(caller,url) {
 1713:                       if (document.getElementById('deleteuploaded_'+caller)) {
 1714:                           document.getElementById('deleteuploaded_'+caller).value=1;
 1715:                           if (caller == 'minimal') {
 1716:                               if (confirm("$js_lt{'min'}"+"\\n"+"$js_lt{'noundo'}")) {
 1717:                                   document.syllabus.submit();
 1718:                               }
 1719:                           }
 1720:                           if (caller == 'file') {
 1721:                               if (confirm("$js_lt{'file'}"+"\\n"+"$js_lt{'noundo'}")) {
 1722:                                   document.syllabus.submit();
 1723:                               }
 1724:                           }
 1725:                       }
 1726:                       return;   
 1727:                   }
 1728:                 // ]]>
 1729:                 </script>
 1730: ENDJS
 1731: }
 1732: 
 1733: sub redirect_to_http {
 1734:     my ($r) = @_;
 1735:     &Apache::loncommon::content_type($r,'text/html');
 1736:     &Apache::loncommon::no_cache($r);
 1737:     $r->send_http_header;
 1738:     my $url = 'http://'.$r->hostname().$r->uri().'?usehttp=1';
 1739:     $r->print(&Apache::loncommon::start_page(undef,undef,
 1740:                                              {'redirect' => [0,$url],}).
 1741:               &Apache::loncommon::end_page());
 1742:     return;
 1743: }
 1744: 
 1745: 1;
 1746: __END__

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