File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.144: download - view: text, annotated - select for diffs
Thu Dec 27 18:14:25 2018 UTC (5 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Call Apache::lonnet::hostname($lonhost) before accessing
  $Apache::lonnet::protocol{$lonhost) so %Apache::lonnet::protocol is populated.

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

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