File:  [LON-CAPA] / loncom / interface / lonsyllabus.pm
Revision 1.138.2.5.2.2: download - view: text, annotated - select for diffs
Mon Jan 20 17:21:08 2020 UTC (4 years, 4 months ago) by raeburn
Branches: version_2_11_3_msu
- For 2.11.3 (modified)
  Include changes in 1.148

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

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