File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.133: download - view: text, annotated - select for diffs
Tue Sep 17 15:04:30 2013 UTC (10 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6669
  - lonhtmlgateway::process_incoming_html() used instead of
    lonfeedback::clear_out_html() to ensure fields contain valid xhtml.
    (more HTML tags now supported).
 - requires perl-XML-LibXML 1.62 or newer:
   update needed on CentOS/RHEL/Scientific Linux 5; SLES 9/10; RHEL 4.

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

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