File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.152: download - view: text, annotated - select for diffs
Thu Oct 27 20:33:31 2022 UTC (19 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Sanity checking of value set for $env{'form.folderpath'} before calling
  lonhtmlcommon::docs_breadcrumbs() to create breadcrumbs train when
  displaying resources in Supplemental Content area.

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

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