--- loncom/interface/lonsyllabus.pm 2013/05/10 17:49:11 1.122 +++ loncom/interface/lonsyllabus.pm 2013/05/14 14:30:15 1.123 @@ -1,7 +1,7 @@ # The LearningOnline Network # Syllabus # -# $Id: lonsyllabus.pm,v 1.122 2013/05/10 17:49:11 raeburn Exp $ +# $Id: lonsyllabus.pm,v 1.123 2013/05/14 14:30:15 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -1059,28 +1059,9 @@ sub save_changes { $syllabus->{'uploaded.lastmodified'} = $now; my $putres = &Apache::lonnet::put('syllabus',$syllabus,$cdom,$cnum); if ($putres eq 'ok') { - my %storehash; - if ($courseenv->{'uploadedsyllabus'}) { - &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.uploadedsyllabus'); - $storehash{'uploadedsyllabus'} = ''; - } - if ($courseenv->{'externalsyllabus'}) { - &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.externalsyllabus'); - $storehash{'externalsyllabus'} = ''; - } - if ($courseenv->{'minimalsyllabus'}) { - &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.minimalsyllabus'); - $storehash{'minimalsyllabus'} = ''; - } - $storehash{'updatedsyllabus'} = $now; - &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum); - &Apache::lonnet::appenv({'course.'.$env{'request.course.id'}.'.updatedsyllabus' => $now}); - if (($courseenv->{'externalsyllabus'} || $courseenv->{'uploadedsyllabus'}) || - ($courseenv->{'minimalsyllabus'})) { - undef($uploaded); - undef($external); - undef($minimal); - } + ($uploaded,$minimal,$external) = + &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded, + $minimal,$external); $output = '
'. &Apache::lonhtmlcommon::confirm_success(&mt('Template saved.')). '
'; @@ -1095,21 +1076,9 @@ sub save_changes { $output = '
'. &mt('External URL unchanged.'). '
'; - if ($uploaded) { - my $prefix; - my $home=&Apache::lonnet::homeserver($cnum,$cdom); - if ($home ne 'no_host') { - my $protocol = $Apache::lonnet::protocol{$home}; - $protocol = 'http' if ($protocol ne 'https'); - $prefix = $protocol.'://'.&Apache::lonnet::hostname($home); - } - unless ($external =~ m{^\Q$prefix/uploaded/$cdom/$cnum/portfolio/syllabus\E}) { - &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.uploadedsyllabus'); - &Apache::lonnet::put('environment',{uploadedsyllabus => ''}, - $cdom,$cnum); - undef($uploaded); - } - } + ($uploaded,$minimal,$external) = + &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded, + $minimal,$external); } else { $external=$env{'form.externalsyllabus'}; $external =~ s/(`)//g; @@ -1121,21 +1090,9 @@ sub save_changes { $output = '
'. &Apache::lonhtmlcommon::confirm_success(&mt('External URL saved.')). '
'; - if ($uploaded) { - my $prefix; - my $home=&Apache::lonnet::homeserver($cnum,$cdom); - if ($home ne 'no_host') { - my $protocol = $Apache::lonnet::protocol{$home}; - $protocol = 'http' if ($protocol ne 'https'); - $prefix = $protocol.'://'.&Apache::lonnet::hostname($home); - } - unless ($external =~ m{^\Q$prefix/uploaded/$cdom/$cnum/portfolio/syllabus\E}) { - &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.uploadedsyllabus'); - &Apache::lonnet::put('environment',{uploadedsyllabus => ''}, - $cdom,$cnum); - undef($uploaded); - } - } + ($uploaded,$minimal,$external) = + &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded, + $minimal,$external); } else { $output = '
'. &mt('An error occurred storing the external URL: [_1]',$putres). @@ -1187,12 +1144,9 @@ END 'portfolio/syllabus/loncapa.html'); } if ($url =~ m{^/uploaded/\Q$cdom\E/\Q$cnum\E.*/[^/]+$}) { - my $exturl; - my $home=&Apache::lonnet::homeserver($cnum,$cdom); - if ($home ne 'no_host') { - my $protocol = $Apache::lonnet::protocol{$home}; - $protocol = 'http' if ($protocol ne 'https'); - $exturl = $protocol.'://'.&Apache::lonnet::hostname($home).$url; + my $exturl = &home_http_host($cdom,$cnum); + if ($exturl) { + $exturl .= $url; } my %storehash; if ($env{'form.choice'} eq 'minimal') { @@ -1224,6 +1178,9 @@ END $output .= &return_to_editor($cdom,$cnum); $earlyout = 1; } + ($uploaded,$minimal,$external) = + &update_syllabus_env($cdom,$cnum,$courseenv,$env{'form.choice'},$uploaded, + $minimal,$external); } else { $error = 1; $errormsg = $putres; @@ -1273,6 +1230,88 @@ END return ($earlyout,$uploaded,$external,$minimal,$output); } +sub update_syllabus_env { + my ($cdom,$cnum,$courseenv,$saved,$uploaded,$minimal,$external); + return unless(ref($courseenv) eq 'HASH'); + my $now = time; + my (@envkeys,%storehash); + if ($saved eq 'template') { + if ($uploaded) { + push(@envkeys,'uploaded'); + } + if ($minimal) { + push(@envkeys,'minimal'); + } + if ($external) { + push(@envkeys,'external'); + } + $storehash{'updatedsyllabus'} = $now; + &Apache::lonnet::appenv({'course.'.$env{'request.course.id'}.'.updatedsyllabus' => $now +}); + + } elsif ($saved eq 'url') { + my $prefix = &home_http_host($cdom,$cnum); + if ($external =~ m{^\Q$prefix/uploaded/$cdom/$cnum/portfolio/syllabus/\E(.+)$}) { + my $file = $1; + if ($file eq 'loncapa.html') { + if ($uploaded) { + push(@envkeys,'uploaded'); + } + } elsif ($minimal) { + push(@envkeys,'minimal'); + } + } else { + if ($uploaded) { + push(@envkeys,'uploaded'); + } + if ($minimal) { + push(@envkeys,'minimal'); + } + } + } elsif ($saved eq 'file') { + if ($minimal) { + push(@envkeys,'minimal'); + } + } elsif ($saved eq 'minimal') { + if ($uploaded) { + push(@envkeys,'uploaded'); + } + } + if (@envkeys > 0) { + foreach my $item (@envkeys) { + my $key = $item.'syllabus'; + if ($courseenv->{$key}) { + &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.'.$key); + $storehash{$key} = ''; + } + if ($item eq 'uploaded') { + undef($uploaded); + } + if ($item eq 'external') { + undef($external); + } + if ($item eq 'minimal') { + undef($minimal); + } + } + } + if (keys(%storehash) > 0) { + &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum); + } + return ($uploaded,$minimal,$external); +} + +sub home_http_host { + my ($cdom,$cnum) = @_; + my $home=&Apache::lonnet::homeserver($cnum,$cdom); + if ($home ne 'no_host') { + my $protocol = $Apache::lonnet::protocol{$home}; + $protocol = 'http' if ($protocol ne 'https'); + return $protocol.'//'.$home; + } + return; +} + sub process_upload { my ($upload_output,$cnum,$cdom,$allfiles,$codebase) = @_; my ($parseaction,$showupload,$mimetype);