File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.137: download - view: text, annotated - select for diffs
Tue Jun 17 23:22:14 2014 UTC (9 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_1, version_2_11_0, HEAD
- On Apache/SSL servers syllabus should always be served by http:// to avoid
  "mixed content" issue when syllabus is an external resource on http:// server).
  - Set $env{'request.use_absolute'} and pass in args to start page so postdata
    is preserved. (bug 6662).
  - Code used for same purpose in rat/lonwrapper.pm rev. 1.44 for external
    resources moved to lonacc.pm to facilitate re-use.
- Detect if syllabus template replaced with uploaded PDF or link to external
  PDF file, and send as arg in call to lonwrapper.pm, to circumvent lack of
  scrolling for multi-page PDFs in iframes on iOS.

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

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