File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.153: download - view: text, annotated - select for diffs
Sat Oct 29 14:47:00 2022 UTC (19 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Entity encoding for last item in breadcrumbs trail.

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

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