File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.130: download - view: text, annotated - select for diffs
Tue Aug 27 03:00:38 2013 UTC (10 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Eliminate printing errors when syllabus is an uploaded HTML file, tex file
  or an external URL.

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

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