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

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

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