File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.132: download - view: text, annotated - select for diffs
Sun Sep 1 01:12:01 2013 UTC (10 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Syllabus options no longer restricted to standard template, or external URL.
  - replace textbox for External Syllabus in "General Course Settings" interface
    with information about syllabus type in use, and links to View or Edit.

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

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