package Apache::imsimport;
use strict;
use Apache::Constants qw(:common :http :methods);
use Apache::loncacc;
use Apache::loncommon();
use Apache::Log();
use Apache::lonnet;
use Apache::imsprocessor;
use HTML::Parser;
use HTML::Entities();
use Apache::lonlocal;
use Apache::lonupload;
use File::Basename();
# ---------------------------------------------------------------- Jscript One
sub jscript_one {
my ($fullpath,$jsref) = @_;
$$jsref = <<"END_OF_ONE";
function verify() {
if ((document.forms.dataForm.newdir.value == '') || (!document.forms.dataForm.newdir.value)) {
alert("You must choose a destination directory for the import")
return false
}
if (document.forms.dataForm.source.selectedIndex == 0) {
alert("You must choose the Course Management System from which the IMS package was exported");
return false
}
return true
}
function nextPage() {
if (verify()) {
document.forms.dataForm.submit()
}
}
function createWin() {
document.dataForm.newdir.value = "";
newWindow = window.open("","CreateDir","HEIGHT=400,WIDTH=750,scrollbars=yes")
newWindow.document.open()
newWindow.document.write("
ENDBLOCKTWO
if ($count{board} + $count{users} > 0) {
$r->print("
Choose a course to receive bulletin boards and user enrollment.
");
if ($$numcrs > 0) {
$r->print("
A listing of possible course targets will be displayed if import of bulletin boards and/or enrollment is checked above (step 3). If you do not plan to import either of these content types, there is no need to specify a course.
Choose course:
");
} else {
$r->print("
You do not have active course coordinator status in any LON-CAPA courses currently, so bulletin boards and enrollment information included in your IMS package will be discarded, regardless of your import choice for these two items above (step 3). If you wish to import bulletin boards and/or user information into LON-CAPA please click 'Exit now' to quit the current IMS import process, and contact your domain coordinator and request a course coordinator role in a LON-CAPA course into which you can upload bulletin boards and/or enroll users.
");
}
}
$r->print(<
Once you have checked the checkboxes for all areas you wish to import from the IMS package, and selected additional options (if available) you should click the 'Import package' button.
ENDBLOCK
if ($count{board} == 0) {
$r->print(' '."\n");
}
if ($count{users} == 0) {
$r->print(' '."\n");
}
$r->print(<
ENDDOCUMENT
} else {
$r->print("Unpacking of your IMS package failed because an IMS manifest file was not located in the package\n");
}
} else {
$r->print("Processing of your IMS package failed because the file you uploaded could not be unzipped\n");
}
}
# ---------------------------------------------------------------- Display Three
sub display_three {
my ($r,$uname,$udom,$areas,$areaname,$cmsmap,$destdir,$newdir) = @_;
my $crs = '';
my $cdom = '';
my $chome = '';
my $db_handling = '';
my $timenow = time;
my $announce_handling = 'ok';
my $cms = $ENV{'form.source'};
if ( defined($ENV{'form.bb_crs'}) ) {
($cdom,$crs) = split/\//,$ENV{'form.bb_crs'};
$chome = &Apache::lonnet::homeserver($crs,$cdom);
}
if ( defined($ENV{'form.bb_handling'}) ) {
$db_handling = $ENV{'form.bb_handling'};
}
my $user_crs = '';
my $user_cdom = '';
my $user_handling = '';
if ( defined($ENV{'form.user_crs'}) ) {
($user_cdom,$user_crs) = split/\//,$ENV{'form.user_crs'};
}
if ( defined($ENV{'form.user_handling'}) ) {
$user_handling = $ENV{'form.user_handling'};
}
my $seqstem = "/res/$udom/$uname/$newdir";
my %imports = ();
my @targets = ();
my %resources = ();
my %items = ();
my %hrefs = ();
my %urls = ();
my %resinfo = ();
my %total = (
page => 0,
prob => 0,
seq => 0,
board => 0,
quiz => 0,
surv => 0,
);
my @pages = ();
my @sequences = ();
my @resrcfiles = ();
my $tempdir = $ENV{'form.tempdir'};
foreach my $area (@{$areas}) {
if (defined($ENV{"form.$area"}) ) {
if ($cms eq 'angel' && $area eq 'doc') {
foreach (@{$$cmsmap{$cms}{$area}}) {
$imports{$_} = 1;
}
} else {
$imports{$$cmsmap{$cms}{$area}} = 1;
}
if ($area eq 'board') {
$db_handling = $ENV{'form.db_handling'};
} elsif ($area eq 'users') {
$user_handling = $ENV{'form.user_handling'};
}
}
}
my $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,\%resources,\%items,\%hrefs,\%resinfo);
if ($manifest_result eq 'ok') {
&Apache::imsprocessor::target_resources(\%resources,\%imports,\@targets);
my $copy_result = &Apache::imsprocessor::copy_resources('CSTR',$cms,\%hrefs,$tempdir,\@targets,\%urls,$crs,$cdom,$chome,$destdir,$timenow);
my @boards = ();
my @announcements = ();
my @quizzes = ();
my @surveys = ();
my @groups = ();
my %messages = ();
my @timestamp = ();
my %boardnum = ();
my @topurls = ();
my @topnames = ();
&Apache::imsprocessor::process_resinfo($cms,$tempdir,$destdir,\%items,\%resources,\@boards,\@announcements,\@quizzes,\@surveys,\@groups,\%messages,\@timestamp,\%boardnum,\%resinfo,$udom,$uname,$cdom,$crs,$db_handling,$user_handling,\%total,$newdir,$seqstem,\@resrcfiles);
&Apache::imsprocessor::build_structure($cms,'CSTR',$destdir,\%items,\%resinfo,\%resources,\%hrefs,$udom,$uname,$newdir,$timenow,$cdom,$crs,\@timestamp,\%total,\@boards,\@announcements,\@quizzes,\@surveys,\%boardnum,\@pages,\@sequences,\@topurls,\@topnames);
$r->print("
Step 3: Publish your new LON-CAPA materials
");
if ($cms eq 'bb5') {
$r->print("Your IMS package has been processed successfully. A total of $total{seq} sequences, $total{page} pages, $total{board} bulletin boards, $total{quiz} quizzes, $total{surv} surveys and $total{prob} problems have been created.
\n");
} elsif ($cms eq 'angel') {
$r->print("Your IMS package has been processed successfully. A total of $total{seq} sequences, $total{page} pages, and $total{board} bulletin boards have been created.
\n");
}
# system (" rm -r -f $destdir/temp");
} elsif ($manifest_result eq 'nomanifest') {
$r->print("Processing of your IMS package failed, because the IMS content package did not contain an IMS manifest file .");
}
}
# ---------------------------------------------------------------- Get LON-CAPA Course Coordinator roles for this user
sub get_ccroles {
my ($user,$dom,$crsentry,$crslist) = @_;
my %roles = ();
unless ($user eq '') {
%roles = &Apache::lonnet::dump('roles',$dom,$user);
}
my $iter = 0;
my @codes = ();
my %courses = ();
my @crslist = ();
my %descrip =();
foreach my $key (keys %roles ) {
if ($key =~ m/^\/(\w+)\/(\w+)_cc$/) {
my $cdom = $1;
my $crs = $2;
my $role_end = 0;
my $role_start = 0;
my $active_chk = 1;
if ( $roles{$key} =~ m/^cc_(\d+)/ ) {
$role_end = $1;
if ( $roles{$key} =~ m/^cc_($role_end)_(\d+)$/ )
{
$role_start = $2;
}
}
if ($role_start > 0) {
if (time < $role_start) {
$active_chk = 0;
}
}
if ($role_end > 0) {
if (time > $role_end) {
$active_chk = 0;
}
}
if ($active_chk) {
my $currcode = '';
my %settings = &Apache::lonnet::get('environment',['internal.coursecode','description'],$cdom,$crs);
if (defined($settings{'description'}) ) {
$descrip{$crs} = $settings{'description'};
} else {
$descrip{$crs} = 'Unknown';
}
if (defined($settings{'internal.coursecode'}) ) {
$currcode = $settings{'internal.coursecode'};
if ($currcode eq '') {
$currcode = "____".$iter;
$iter ++;
}
} else {
$currcode = "____".$iter;
$iter ++;
}
unless (grep/^$currcode$/,@codes) {
push @codes,$currcode;
@{$courses{$currcode}} = ();
}
push @{$courses{$currcode}}, $cdom.'/'.$crs;
}
}
}
foreach my $code (sort @codes) {
foreach my $crsdom (@{$courses{$code}}) {
my ($cdom,$crs) = split/\//,$crsdom;
my $showcode = '';
unless ($code =~m/^____\d+$/) { $showcode = $code; }
$$crsentry{$crsdom} = $showcode.':'.$descrip{$crs};
push @{$crslist}, $crsdom;
}
}
return;
}
# ---------------------------------------------------------------- Main Handler
sub handler {
my $r=shift;
my $uname;
my $udom;
my $javascript = '';
my $page_name = '';
my $current_page = '';
my $loadentries = '';
my $qcount = '';
# get personal information for this user
my $user=$ENV{'user.name'};
my $dom=$ENV{'user.domain'};
#
# re-attach user
#
if ($ENV{'form.uploaduname'}) {
$ENV{'form.filename'}='/priv/'.$ENV{'form.uploaduname'}.'/'.
$ENV{'form.filename'};
}
($uname,$udom)=
&Apache::loncacc::constructaccess($ENV{'form.filename'},
$r->dir_config('lonDefDomain'));
unless (($uname) && ($udom)) {
$r->log_reason($uname.' at '.$udom.
' trying to publish file '.$ENV{'form.filename'}.
' - not authorized',
$r->filename);
return HTTP_NOT_ACCEPTABLE;
}
my $fn;
if ($ENV{'form.filename'}) {
$fn=$ENV{'form.filename'};
$fn=~s/^http\:\/\/[^\/]+\///;
$fn=~s/^\///;
$fn=~s/(\~|priv\/)(\w+)//;
$fn=~s/\/+/\//g;
} else {
$r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
' unspecified filename for upload', $r->filename);
return HTTP_NOT_FOUND;
}
my $zipupload = '/home/'.$uname.'/public_html'.$fn;
my $pathname = &File::Basename::dirname($fn);
my $fullpath = '/priv/'.$uname.$pathname;
unless ($pathname eq '/') {
$fullpath .= '/';
}
my $loadentries = '';
my @areas = ();
my %cmsmap = ();
my %areaname = ();
my $numcrs = 0;
&Apache::imsprocessor::ims_config(\@areas,\%cmsmap,\%areaname);
# ----------------------------------------------------------- Start page output
&Apache::loncommon::content_type($r,'text/html');
$r->send_http_header;
if ($ENV{'form.phase'} eq 'two') {
&jscript_one($fullpath,\$javascript,$uname,$udom);
} elsif ($ENV{'form.phase'} eq 'three') {
&jscript_two(\$javascript,$user,$dom,\$numcrs);
} elsif ($ENV{'form.phase'} eq 'four') {
&jscript_three(\$javascript);
}
$r->print("LON-CAPA Construction Space\n");
$r->print(&Apache::loncommon::bodytag('Upload IMS package to Construction Space',undef,$loadentries));
if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
$r->print('
'.&mt('Co-Author').': '.$uname.
&mt(' at ').$udom.'
');
}
if ($ENV{'form.phase'} eq 'two') {
my $flag = &Apache::lonupload::phasetwo($r,$fn,$uname,$udom,'imsimport');
if ($flag eq 'ok') {
&display_one($r,$uname,$fn,$fullpath);
}
} elsif ( ($ENV{'form.phase'} eq 'three') || ($ENV{'form.phase'} eq 'four') ) {
my $docroot = $ENV{'form.newdir'};
my $newdir = '';
if ($docroot =~ m|public_html/(.+)$|) {
$newdir = $1;
}
if ($ENV{'form.phase'} eq 'three') {
&display_two ($r,$zipupload,\@areas,\%areaname,\%cmsmap,$uname,$newdir,\$numcrs,$fullpath);
} elsif ($ENV{'form.phase'} eq 'four') {
&display_three ($r,$uname,$udom,\@areas,\%areaname,\%cmsmap,$docroot,$newdir);
}
} else {
&Apache::lonupload::phaseone($r,$fn,$uname,$udom,'imsimport');
}
$r->print('