Annotation of loncom/interface/lonsyllabus.pm, revision 1.131

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

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