--- loncom/interface/londocs.pm 2016/08/08 13:37:42 1.484.2.65 +++ loncom/interface/londocs.pm 2017/02/20 18:29:22 1.622 @@ -1,7 +1,7 @@ # The LearningOnline Network # Documents # -# $Id: londocs.pm,v 1.484.2.65 2016/08/08 13:37:42 raeburn Exp $ +# $Id: londocs.pm,v 1.622 2017/02/20 18:29:22 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -43,10 +43,13 @@ use Apache::lonnavdisplay(); use Apache::lonextresedit(); use Apache::lontemplate(); use Apache::lonsimplepage(); +use Apache::lonhomework(); +use Apache::lonpublisher(); use HTML::Entities; use HTML::TokeParser; use GDBM_File; use File::MMagic; +use File::Copy; use Apache::lonlocal; use Cwd; use LONCAPA qw(:DEFAULT :match); @@ -139,7 +142,43 @@ sub clean { return $title; } - +sub default_folderpath { + my ($coursenum,$coursedom,$navmapref) = @_; + return unless ($coursenum && $coursedom && ref($navmapref)); +# Check if entire course is hidden and/or encrypted + my ($hiddenmap,$encryptmap,$folderpath,$hiddentop); + my $toplevel = "uploaded/$coursedom/$coursenum/default.sequence"; + unless (ref($$navmapref)) { + $$navmapref = Apache::lonnavmaps::navmap->new(); + } + if (ref($$navmapref)) { + if (lc($$navmapref->get_mapparam(undef,$toplevel,"0.hiddenresource")) eq 'yes') { + my $filterFunc = sub { my $res = shift; return (!$res->randomout() && !$res->is_map()) }; + my @resources = $$navmapref->retrieveResources($toplevel,$filterFunc,1,1); + unless (@resources) { + $hiddenmap = 1; + unless ($env{'request.role.adv'}) { + $hiddentop = 1; + if ($env{'form.folder'}) { + undef($env{'form.folder'}); + } + } + } + } + if (lc($$navmapref->get_mapparam(undef,$toplevel,"0.encrypturl")) eq 'yes') { + $encryptmap = 1; + } + } + unless ($hiddentop) { + $folderpath='default&'.&escape(&mt('Main Content')). + '::'.$hiddenmap.':'.$encryptmap.'::'; + } + if (wantarray) { + return ($folderpath,$hiddentop); + } else { + return $folderpath; + } +} sub dumpcourse { my ($r) = @_; @@ -196,7 +235,7 @@ ENDJS } else { &Apache::loncourserespicker::enumerate_course_contents($navmap,\%maps,\%resources,\%titles, 'dumpdocs',$cdom,$cnum); - } + } my @todump = &Apache::loncommon::get_env_multiple('form.archive'); my (%tocopy,%replacehash,%lookup,%deps,%display,%result,%depresult,%simpleproblems,%simplepages, %newcontent,%has_simpleprobs); @@ -207,7 +246,7 @@ ENDJS if ($res =~ m{^uploaded/$cdom/$cnum/\E((?:docs|supplemental)/.+)$}) { $tocopy{$1} = $name; $display{$item} = $1; - $lookup{$1} = $item; + $lookup{$1} = $item; } elsif ($res eq 'lib/templates/simpleproblem.problem') { $simpleproblems{$item} = { symb => $resources{$item}, @@ -307,7 +346,7 @@ $contents{content}.' '; } if ($contents{webreferences}) { - $content .= ' + $content .= '

'.&mt('Web References').'

'. $contents{webreferences}.' @@ -317,10 +356,10 @@ $contents{webreferences}.' '; - $newcontent{'/'.$simplepages{$item}{res}} = $content; + $newcontent{'/'.$simplepages{$item}{res}} = $content; } } - foreach my $item (keys(%tocopy)) { + foreach my $item (keys(%tocopy)) { unless ($item=~/\.(sequence|page)$/) { my $currurlpath = $prefix.$item; my $currdirpath = &Apache::lonnet::filelocation('',$currurlpath); @@ -351,39 +390,39 @@ $contents{webreferences}.' if ($simpleproblems{$num}) { $newfilename=$title.'/'.$simpleproblems{$num}{'name'}; } else { - $newfilename=$title.'/'.$replacehash{$item}; + $newfilename=$title.'/'.$replacehash{$item}; } - $newfilename=~s/\.(\w+)$//; - my $ext=$1; - $newfilename=&clean($newfilename); - $newfilename.='.'.$ext; - my ($newrelpath) = ($newfilename =~ m{^\Q$title/\E(.+)$}); + $newfilename=~s/\.(\w+)$//; + my $ext=$1; + $newfilename=&clean($newfilename); + $newfilename.='.'.$ext; + my ($newrelpath) = ($newfilename =~ m{^\Q$title/\E(.+)$}); if ($newrelpath ne $replacehash{$item}) { $replacehash{$item} = $newrelpath; } - my @dirs=split(/\//,$newfilename); - my $path=$r->dir_config('lonDocRoot')."/priv/$cd/$ca"; - my $makepath=$path; - my $fail; + my @dirs=split(/\//,$newfilename); + my $path=$r->dir_config('lonDocRoot')."/priv/$cd/$ca"; + my $makepath=$path; + my $fail; my $origin; - for (my $i=0;$i<$#dirs;$i++) { - $makepath.='/'.$dirs[$i]; - unless (-e $makepath) { - unless(mkdir($makepath,0755)) { + for (my $i=0;$i<$#dirs;$i++) { + $makepath.='/'.$dirs[$i]; + unless (-e $makepath) { + unless(mkdir($makepath,0755)) { $fail = &mt('Directory creation failed.'); } - } - } + } + } if ($i == 0) { - $result = '
'.$item.' => '.$newfilename.': '; + $result = '
'.$item.' => '.$newfilename.': '; } else { $depresult .= '
  • '.$item.' => '.$newfilename.' '. ''. &mt('(dependency)').': '; } if (-e $path.'/'.$newfilename) { - $fail = &mt('Destination already exists -- not overwriting.'); - } else { + $fail = &mt('Destination already exists -- not overwriting.'); + } else { if (my $fh=Apache::File->new('>'.$path.'/'.$newfilename)) { if (($item =~ m{^/adm/$match_domain/$match_username/\d+/smppg}) || ($item =~ /^simpleproblem_/)) { @@ -405,18 +444,18 @@ $contents{webreferences}.' while (my $token = $parser->get_token) { if ($token->[0] eq 'S') { if (($token->[1] eq 'resource') && - ($token->[2]->{'src'} eq '/res/lib/templates/simpleproblem.problem') && + ($token->[2]->{'src'} eq '/res/lib/templates/simpleproblem.problem') && ($changes{$token->[2]->{'id'}})) { my $id = $token->[2]->{'id'}; $updatedcontent .= '<'.$token->[1]; foreach my $attrib (@{$token->[3]}) { - next unless ($attrib =~ /^(src|type|title|id)$/); + next unless ($attrib =~ /^(src|type|title|id)$/); if ($attrib eq 'src') { - my ($file) = ($display{$changes{$id}} =~ /^\Qsimpleproblem_\E(.+)$/); + my ($file) = ($display{$changes{$id}} =~ /^\Qsimpleproblem_\E(.+)$/); if ($file) { $updatedcontent .= ' '.$attrib.'="'.$file.'"'; } else { - $updatedcontent .= ' '.$attrib.'="'.$token->[2]->{$attrib}.'"'; + $updatedcontent .= ' '.$attrib.'="'.$token->[2]->{$attrib}.'"'; } } else { $updatedcontent .= ' '.$attrib.'="'.$token->[2]->{$attrib}.'"'; @@ -431,36 +470,36 @@ $contents{webreferences}.' } } print $fh $updatedcontent; - } else { - print $fh &Apache::lonclonecourse::rewritefile( + } else { + print $fh &Apache::lonclonecourse::rewritefile( &Apache::lonclonecourse::readfile($env{'request.course.id'},$item), - (%replacehash,$crs => '') - ); + (%replacehash,$crs => '') + ); } } else { - print $fh + print $fh &Apache::lonclonecourse::readfile($env{'request.course.id'},$item); - } + } } else { - $fail = &mt('Source does not exist.'); + $fail = &mt('Source does not exist.'); } } $fh->close(); - } else { - $fail = &mt('Could not write to destination.'); + } else { + $fail = &mt('Could not write to destination.'); } - } + } my $text; - if ($fail) { + if ($fail) { $text = ''.&mt('fail').(' 'x3).$fail.''; - } else { + } else { $text = ''.&mt('ok').''; } if ($i == 0) { $result .= $text; } else { $depresult .= $text.'
  • '; - } + } } $r->print($result); if ($depresult) { @@ -477,63 +516,63 @@ $contents{webreferences}.' $r->rflush(); my ($preamble,$formname); $formname = 'dumpdoc'; - unless ($home==1) { - $preamble = '
    '. - '
    '. + unless ($home==1) { + $preamble = '
    '. + '
    '. &mt('Select the Authoring Space'). ''; - } else { - $preamble .= ''; - } - } - unless ($home==1) { - $preamble .= '
    '."\n"; - } - my $title=$origcrsdata{'description'}; - $title=~s/[\/\s]+/\_/gs; - $title=&clean($title); - $preamble .= '
    '. + if ($home==1) { + $preamble .= ''; + } else { + $preamble .= ''; + } + } + unless ($home==1) { + $preamble .= '
    '."\n"; + } + my $title=$origcrsdata{'description'}; + $title=~s/[\/\s]+/\_/gs; + $title=&clean($title); + $preamble .= '
    '. '
    '.&mt('Folder in Authoring Space').''. ''. '
    '."\n"; my %uploadedfiles; - &tiehash(); - foreach my $file (&Apache::lonclonecourse::crsdirlist($origcrsid,'userfiles')) { - my ($ext)=($file=~/\.(\w+)$/); + &tiehash(); + foreach my $file (&Apache::lonclonecourse::crsdirlist($origcrsid,'userfiles')) { + my ($ext)=($file=~/\.(\w+)$/); # FIXME Check supplemental here - my $title=$hash{'title_'.$hash{ - 'ids_/uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'.$file}}; - if (!$title) { - $title=$file; - } else { - $title=~s|/|_|g; - } - $title=~s/\.(\w+)$//; - $title=&clean($title); - $title.='.'.$ext; -# $r->print("\n" + my $title=$hash{'title_'.$hash{ + 'ids_/uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'.$file}}; + if (!$title) { + $title=$file; + } else { + $title=~s|/|_|g; + } + $title=~s/\.(\w+)$//; + $title=&clean($title); + $title.='.'.$ext; +# $r->print("\n" $uploadedfiles{$file} = $title; - } - &untiehash(); + } + &untiehash(); $r->print(&Apache::loncourserespicker::create_picker($navmap,'dumpdocs',$formname,$crstype,undef, undef,undef,$preamble,$home,\%uploadedfiles)); } @@ -564,16 +603,16 @@ sub recurse_html { } else { $relfile = $dependency; $depurl = $currurlpath; - $depurl =~ s{[^/]+$}{}; + $depurl =~ s{[^/]+$}{}; $depurl .= $dependency; - ($newcontainer) = ($depurl =~ m{^\Q$prefix\E(.+)$}); + ($newcontainer) = ($depurl =~ m{^\Q$prefix\E(.+)$}); } next if ($relfile eq ''); my $newname = $replacehash->{$container}; $newname =~ s{[^/]+$}{}; $replacehash->{$newcontainer} = $newname.$relfile; $deps->{$item}{$newcontainer} = 1; - my ($newurlpath) = ($depurl =~ m{^(.*)/[^/]+$}); + my ($newurlpath) = ($depurl =~ m{^(.*)/[^/]+$}); my $depfile = &Apache::lonnet::filelocation('',$depurl); my $type = $mm->checktype_filename($depfile); if ($type eq 'text/html') { @@ -586,7 +625,7 @@ sub recurse_html { } sub group_import { - my ($coursenum, $coursedom, $folder, $container, $caller, @files) = @_; + my ($coursenum, $coursedom, $folder, $container, $caller, $ltitoolsref, @files) = @_; my ($donechk,$allmaps,%hierarchy,%titles,%addedmaps,%removefrommap, %removeparam,$importuploaded,$fixuperrors); $allmaps = {}; @@ -615,6 +654,89 @@ sub group_import { } } if ($url) { + if ($url =~ m{^(/adm/$coursedom/$coursenum/(\d+)/exttool)s?\:?(.*)$}) { + $url = $1; + my $marker = $2; + my $info = $3; + my ($toolid,%toolhash,%toolsettings); + my @toolinfo = split(/:/,$info); + if ($residx) { + %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$coursedom,$coursenum); + $toolid = $toolsettings{'id'}; + } else { + $toolid = shift(@toolinfo); + } + $toolid =~ s/\D//g; + ($toolhash{'target'},$toolhash{'width'},$toolhash{'height'}, + $toolhash{'crslabel'},$toolhash{'crstitle'}) = @toolinfo; + $toolhash{'crslabel'} = &unescape($toolhash{'crslabel'}); + $toolhash{'crstitle'} = &unescape($toolhash{'crstitle'}); + if (ref($ltitoolsref) eq 'HASH') { + my @deleted; + if (ref($ltitoolsref->{$toolid}) eq 'HASH') { + if ($ltitoolsref->{$toolid}->{'url'} =~ m{^https://}) { + $url =~ s/exttool$/exttools/; + } + $toolhash{'id'} = $toolid; + if (($toolhash{'target'} eq 'iframe') || ($toolhash{'target'} eq 'window')) { + if ($toolhash{'target'} eq 'window') { + foreach my $item ('width','height') { + $toolhash{$item} =~ s/^\s+//; + $toolhash{$item} =~ s/\s+$//; + } + } + } elsif ($residx) { + $toolhash{'target'} = $toolsettings{'target'}; + if ($toolhash{'target'} eq 'window') { + $toolhash{'width'} = $toolsettings{'width'}; + $toolhash{'height'} = $toolsettings{'height'}; + } + } elsif (ref($ltitoolsref->{$toolid}->{'display'}) eq 'HASH') { + $toolhash{'target'} = $ltitoolsref->{$toolid}->{'display'}->{'target'}; + if ($toolhash{'target'} eq 'window') { + $toolhash{'width'} = $ltitoolsref->{$toolid}->{'display'}->{'width'}; + $toolhash{'height'} = $ltitoolsref->{$toolid}->{'display'}->{'height'}; + } + } + if ($toolhash{'target'} eq 'iframe') { + delete($toolhash{'width'}); + delete($toolhash{'height'}); + if ($residx) { + if ($toolsettings{'width'}) { + push(@deleted,'width'); + } + if ($toolsettings{'height'}) { + push(@deleted,'height'); + } + } + } + if (ref($ltitoolsref->{$toolid}->{'crsconf'}) eq 'HASH') { + foreach my $item ('label','title') { + if ($ltitoolsref->{$toolid}->{'crsconf'}->{$item}) { + $toolhash{'crs'.$item} =~ s/^\s+//; + $toolhash{'crs'.$item} =~ s/\s+$//; + if ($toolhash{'crs'.$item} eq '') { + delete($toolhash{'crs'.$item}); + } + } else { + delete($toolhash{'crs'.$item}); + } + if (($residx) && (exists($toolsettings{'crs'.$item}))) { + unless (exists($toolhash{'crs'.$item})) { + push(@deleted,'crs'.$item); + } + } + } + } + my $putres = &Apache::lonnet::put('exttool_'.$marker,\%toolhash,$coursedom,$coursenum); + if ($putres eq 'ok') { + if (@deleted) { + &Apache::lonnet::del('exttool_'.$marker,\@deleted,$coursedom,$coursenum); + } + } + } + } + } if (($caller eq 'londocs') && ($folder =~ /^default/)) { if (($url =~ /\.(page|sequence)$/) && (!$donechk)) { @@ -667,7 +789,7 @@ $initialtext END $env{'form.output'}=$newhtml; - my $result = + my $result = &Apache::lonnet::finishuserfileupload($coursenum,$coursedom, 'output', "$filepath/$residx/$fname.html"); @@ -692,7 +814,7 @@ END removefrommap => \%removefrommap, removeparam => \%removeparam, ); - my ($result,$msgsarray,$lockerror) = + my ($result,$msgsarray,$lockerror) = &apply_fixups($folder,1,$coursedom,$coursenum,\%import_errors,\%updated); if (keys(%import_errors) > 0) { $fixuperrors = @@ -791,12 +913,14 @@ sub log_docs { } sub docs_change_log { - my ($r,$coursenum,$coursedom,$folder,$allowed,$crstype,$iconpath)=@_; + my ($r,$coursenum,$coursedom,$folder,$allowed,$crstype,$iconpath,$canedit)=@_; my $supplementalflag=($env{'form.folderpath'}=~/^supplemental/); + my $navmap; my $js = ''."\n"; + .''."\n" + .''."\n"; # Breadcrumbs &Apache::lonhtmlcommon::clear_breadcrumbs(); @@ -4784,34 +5412,52 @@ sub handler { $r->print(&Apache::loncommon::start_page("$crstype documents",undef, {'force_register' => $showdoc,})); } elsif ($toolsflag) { + my ($breadtext,$breadtitle); + $breadtext = "$crstype Editor"; + if ($canedit) { + $breadtitle = 'Editing '.$crstype.' Contents'; + } else { + $breadtext .= ' (View-only mode)'; + $breadtitle = 'Viewing '.$crstype.' Contents'; + } &Apache::lonhtmlcommon::add_breadcrumb({ - href=>"/adm/coursedocs",text=>"$crstype Contents"}); + href=>"/adm/coursedocs",text=>$breadtext}); $r->print(&Apache::loncommon::start_page("$crstype Contents", $script) .&Apache::loncommon::help_open_menu('','',273,'RAT') .&Apache::lonhtmlcommon::breadcrumbs( - 'Editing Course Contents') + $breadtitle) ); } elsif ($r->uri eq '/adm/supplemental') { my $brcrum = &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype); $r->print(&Apache::loncommon::start_page("Supplemental $crstype Content",undef, {'bread_crumbs' => $brcrum,})); } else { + my ($breadtext,$breadtitle,$helpitem); + $breadtext = "$crstype Editor"; + if ($canedit) { + $breadtitle = 'Editing '.$crstype.' Contents'; + $helpitem = 'Docs_Adding_Course_Doc'; + } else { + $breadtext .= ' (View-only mode)'; + $breadtitle = 'Viewing '.$crstype.' Contents'; + $helpitem = 'Docs_Viewing_Course_Doc'; + } &Apache::lonhtmlcommon::add_breadcrumb({ - href=>"/adm/coursedocs",text=>"$crstype Contents"}); + href=>"/adm/coursedocs",text=>$breadtext}); $r->print(&Apache::loncommon::start_page("$crstype Contents", $script, {'add_entries' => $addentries} ) .&Apache::loncommon::help_open_menu('','',273,'RAT') .&Apache::lonhtmlcommon::breadcrumbs( - 'Editing '.$crstype.' Contents', - 'Docs_Adding_Course_Doc') + $breadtitle, + $helpitem) ); } my %allfiles = (); my %codebase = (); my ($upload_result,$upload_output,$uploadphase); - if ($allowed) { + if ($canedit) { if (($env{'form.uploaddoc.filename'}) && ($env{'form.cmd'}=~/^upload_(\w+)/)) { my $context = $1; @@ -4863,7 +5509,7 @@ sub handler { if ($allowed && $toolsflag) { $r->print(&startContentScreen('tools')); - $r->print(&generate_admin_menu($crstype)); + $r->print(&generate_admin_menu($crstype,$canedit)); $r->print(&endContentScreen()); } elsif ((!$showdoc) && (!$uploadphase)) { # ----------------------------------------------------------------------------- @@ -4875,6 +5521,9 @@ sub handler { 'impo' => 'Import', 'lnks' => 'Import from Stored Links', 'impm' => 'Import from Assembled Map', + 'imcr' => 'Import from Course Resources', + 'extr' => 'External Resource', + 'extt' => 'External Tool', 'selm' => 'Select Map', 'load' => 'Load Map', 'newf' => 'New Folder', @@ -4883,7 +5532,10 @@ sub handler { 'navc' => 'Table of Contents', 'sipa' => 'Simple Course Page', 'sipr' => 'Simple Problem', - 'webp' => 'Blank Web Page (editable)', + 'webp' => 'Blank Web Page (editable)', + 'stpr' => 'Standard Problem', + 'news' => 'New sub-directory', + 'crpr' => 'Create Problem', 'drbx' => 'Drop Box', 'scuf' => 'External Scores (handgrade, upload, clicker)', 'bull' => 'Discussion Board', @@ -4897,18 +5549,32 @@ sub handler { 'se' => 'Select', 'file' => 'File', 'title' => 'Title', + 'addp' => 'Add Placeholder to course?', + 'uste' => 'Use Template?', + 'fnam' => 'File Name:', + 'loca' => 'Location:', + 'dire' => 'Directory:', + 'cate' => 'Category:', + 'tmpl' => 'Template:', 'comment' => 'Comment', 'parse' => 'Upload embedded images/multimedia files if HTML file', 'bb5' => 'Blackboard 5', 'bb6' => 'Blackboard 6', 'angel5' => 'ANGEL 5.5', 'webctce4' => 'WebCT 4 Campus Edition', + 'yes' => 'Yes', + 'no' => 'No', + 'er' => 'Editing rights unavailable for your current role.', ); # ----------------------------------------------------------------------------- - # Calculate free quota space for a user or course. + + # Calculate free quota space for a user or course. A javascript function checks + # file size to determine if upload should be allowed. my $quotatype = 'unofficial'; if ($crstype eq 'Community') { $quotatype = 'community'; + } elsif ($crstype eq 'Placement') { + $quotatype = 'placement'; } elsif ($env{'course.'.$coursedom.'_'.$coursenum.'.internal.coursecode'}) { $quotatype = 'official'; } elsif ($env{'course.'.$coursedom.'_'.$coursenum.'.internal.textbook'}) { @@ -4928,7 +5594,7 @@ sub handler { if ($disk_quota == 0) { $percent = 100.0; } else { - $percent = 100*($current_disk_usage/$disk_quota); + $percent = 100*($usage/$disk_quota); } $usage = sprintf("%.2f",$usage); $quota = sprintf("%.2f",$quota); @@ -4939,7 +5605,8 @@ sub handler { my $fileupload=(< - + + FIUP my $checkbox=(< --> CHBO my $imsfolder = $env{'form.folder'}; @@ -4964,7 +5631,7 @@ CHBO

    $lt{'cms'}:  - @@ -4974,7 +5641,7 @@ CHBO

    - + IMSFORM @@ -4986,10 +5653,10 @@ IMSFORM FUFORM + my $mapimportjs; + if ($canedit) { + $mapimportjs = "javascript:openbrowser('mapimportform','importmap','sequence,page','');"; + } else { + $mapimportjs = "javascript:alert('".&js_escape($lt{'er'})."');"; + } my $importpubform=(< $lt{'impm'}$help{'Load_Map'} @@ -5012,31 +5685,73 @@ FUFORM $lt{'copm'}
    -  $lt{'selm'}
    - + onfocus="this.blur();$mapimportjs" $disabled /> +  $lt{'selm'}
    + SEDFFORM + my $importcrsresform; + my ($numdirs,$pickfile) = + &Apache::loncommon::import_crsauthor_form('crsresimportform','coursepath','coursefile', + "resize_scrollbox('contentscroll','1','0');", + undef,'res'); + if ($pickfile) { + $importcrsresform=(< + $lt{'imcr'}$help{'Course_Resources'} +
    + +
    +CRSFORM + } + + my $fromstoredjs; + if ($canedit) { + $fromstoredjs = 'open_StoredLinks_Import()'; + } else { + $fromstoredjs = "alert('".&js_escape($lt{'er'})."')"; + } + my @importpubforma = ( { ''.$lt{srch}.'' => $pathitem."$lt{'srch'}" }, { ''.$lt{impo}.'' => "$lt{'impo'}$help{'Importing_LON-CAPA_Resource'}" }, - { ''.$lt{lnks}.'' => "$lt{'lnks'}" }, - { ''.$lt{impm}.'' => $importpubform } - ); + { ''.$lt{lnks}.'' => ''.$lt{'lnks'}.'' }, + { ''.$lt{impm}.'' => $importpubform }, + ); + if ($pickfile) { + push(@importpubforma,{ ''.$lt{imcr}.'' => $importcrsresform}); + } $importpubform = &create_form_ul(&create_list_elements(@importpubforma)); my $extresourcesform = &Apache::lonextresedit::extedit_form(0,0,undef,undef,$pathitem, - $help{'Adding_External_Resource'}); + $help{'Adding_External_Resource'}, + undef,undef,undef,undef,undef,undef,$disabled); + my $exttoolform = + &Apache::lonextresedit::extedit_form(0,0,undef,undef,$pathitem, + $help{'Adding_External_Tool'},undef, + undef,'tool',$coursedom,$coursenum, + \%ltitools,$disabled); if ($allowed) { my $folder = $env{'form.folder'}; if ($folder eq '') { $folder='default'; } - my $output = &update_paste_buffer($coursenum,$coursedom,$folder); - if ($output) { - $r->print($output); + if ($canedit) { + my $output = &update_paste_buffer($coursenum,$coursedom,$folder); + if ($output) { + $r->print($output); + } } $r->print(< @@ -5068,12 +5783,12 @@ HIDDENFORM } # - + my $hostname = $r->hostname(); my $savefolderpath; if ($allowed) { my $folder=$env{'form.folder'}; - if ($folder eq '' || $supplementalflag) { + if ((($folder eq '') && (!$hiddentop)) || ($supplementalflag)) { $folder='default'; $savefolderpath = $env{'form.folderpath'}; $env{'form.folderpath'}='default&'.&escape(&mt('Main Content')); @@ -5101,7 +5816,7 @@ HIDDENFORM $pathitem - $lt{'navc'} + $lt{'navc'} $help{'Navigate_Content'} NNFORM @@ -5162,7 +5877,7 @@ NBFORM $pathitem - $lt{'mypi'} + $lt{'mypi'} $help{'My Personal Information Page'} NAMFORM @@ -5182,7 +5897,7 @@ NASOFORM $pathitem - $lt{'rost'} + $lt{'rost'} $help{'Course_Roster'} NROSTFORM @@ -5206,7 +5921,224 @@ NROSTFORM $help{'Web_Page'} NWEBFORM - + + my @ids=&Apache::lonnet::current_machine_ids(); + my %select_menus; + my $numauthor = 0; + my $numcrsdirs = 0; + my $toppath = "/priv/$env{'user.domain'}/$env{'user.name'}"; + if ($env{'user.author'}) { + $numauthor ++; + $select_menus{'author'}->{'text'} = &Apache::lonnet::plaintext('au'); + if (grep(/^\Q$env{'user.home'}\E$/,@ids)) { + my $is_home = 1; + my %subdirs; + &Apache::lonnet::recursedirs($is_home,'priv',$londocroot,$toppath,'',\%subdirs); + $select_menus{'author'}->{'default'} = '/'; + $select_menus{'author'}->{'select2'}->{'/'} = '/'; + my @ordered = ('/'); + foreach my $relpath (sort { lc($a) cmp lc($b) } (keys(%subdirs))) { + $select_menus{'author'}->{'select2'}->{$relpath} = $relpath; + push(@ordered,$relpath); + } + $select_menus{'author'}->{'order'} = \@ordered; + } else { + $select_menus{'author'}->{'select2'}->{'switch'} = &mt('Switch server required'); + $select_menus{'author'}->{'default'} = 'switch'; + $select_menus{'author'}->{'order'} = ['switch']; + } + } + my %roleshash = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},'userroles', + ['active'],['ca','aa']); + my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'}; + my %by_roletype; + if (keys(%roleshash)) { + foreach my $entry (keys(%roleshash)) { + my ($auname,$audom,$roletype) = split(/:/,$entry); + my $key = $entry; + $key =~ s/:/___/g; + $by_roletype{$roletype}{$auname.'___'.$audom} = 1; + $select_menus{$key}->{'text'} = &Apache::lonnet::plaintext($roletype)." ($audom/$auname)"; + my $rolehome = &Apache::lonnet::homeserver($auname,$audom); + if (grep(/^\Q$rolehome\E$/,@ids)) { + my $is_home = 1; + my (%subdirs,@ordered); + my $toppath="/priv/$audom/$auname"; + &Apache::lonnet::recursedirs($is_home,'priv',$londocroot,$toppath,'',\%subdirs); + $select_menus{$key}->{'default'} = '/'; + $select_menus{$key}->{'select2'}->{'/'} = '/'; + my @ordered = ('/'); + foreach my $relpath (sort { lc($a) cmp lc($b) } (keys(%subdirs))) { + $select_menus{$key}->{'select2'}->{$relpath} = $relpath; + push(@ordered,$relpath); + } + $select_menus{$key}->{'order'} = \@ordered; + } else { + $select_menus{$key}->{'select2'}->{'switch'} = &mt('Switch server required'); + $select_menus{$key}->{'default'} = 'switch'; + $select_menus{$key}->{'order'} = ['switch']; + } + $numauthor ++; + } + } + my ($pickdir,$showtitle);; + if ($numauthor) { + my @order; + my $defrole; + if ($env{'user.author'}) { + push(@order,'author'); + $defrole = 'author'; + } + if (keys(%by_roletype)) { + foreach my $possrole ('ca','aa') { + if (ref($by_roletype{$possrole}) eq 'HASH') { + foreach my $author (sort { lc($a) cmp lc($b) } (keys(%{$by_roletype{$possrole}}))) { + unless ($defrole) { + $defrole = $author; + } + push(@order,$author.'___'.$possrole); + } + } + } + } + $select_menus{'course'}->{'text'} = &mt('Course Resource'); + if (grep(/^\Q$crshome\E$/,@ids)) { + my $is_home = 1; + my %subdirs; + my $toppath="/priv/$coursedom/$coursenum"; + &Apache::lonnet::recursedirs($is_home,'priv',$londocroot,$toppath,'',\%subdirs); + $numcrsdirs = keys(%subdirs); + $select_menus{'course'}->{'default'} = '/'; + $select_menus{'course'}->{'select2'}->{'/'} = '/'; + my @ordered = ('/'); + foreach my $relpath (sort { lc($a) cmp lc($b) } (keys(%subdirs))) { + $select_menus{'course'}->{'select2'}->{$relpath} = $relpath; + push(@ordered,$relpath); + } + $select_menus{'course'}->{'order'} = \@ordered; + } else { + $select_menus{'course'}->{'select2'}->{'switch'} = &mt('Switch server required'); + $select_menus{'course'}->{'default'} = 'switch'; + $select_menus{'course'}->{'order'} = ['switch']; + } + push(@order,'course'); + $pickdir = $lt{'loca'}. + &Apache::loncommon::linked_select_forms('courseresform','
    '.$lt{'dire'}, + $defrole,'authorrole','authorpath', + \%select_menus,\@order,'toggleCrsResTitle();', + '','priv').'
    '; + $showtitle = 'none'; + } else { + my $is_home; + $showtitle = 'inline'; + if (grep(/^\Q$crshome\E$/,@ids)) { + $is_home = 1; + $pickdir .= ''; + my $toppath="/priv/$coursedom/$coursenum'}"; + my %subdirs; + &Apache::lonnet::recursedirs($is_home,'priv',$londocroot,$toppath,'',\%subdirs); + $numcrsdirs = keys(%subdirs); + if ($numcrsdirs) { + $pickdir .= &mt('Directory: ').''; + } else { + $pickdir .= ''."\n"; + } + } + } + + my %seltemplate_menus; + my @files = &Apache::lonhomework::get_template_list('problem'); + my @noexamplelink = ('blank.problem','blank.library','script.library'); + my $currentcategory = ''; + my @ordered = (''); + my %templatehelp; + my $defcategory = ''; + my @catorder = ($defcategory); + $seltemplate_menus{$defcategory}->{'order'} = ['']; + $seltemplate_menus{$defcategory}->{'text'} = ''; + foreach my $file (@files) { + if (ref($file) eq 'ARRAY') { + my ($path,$title,$category,$help) = @{$file}; + next if ($title !~ /\S/); + if (&js_escape($category) ne $currentcategory) { + $currentcategory = &js_escape($category); + push(@catorder,&js_escape($currentcategory)); + $seltemplate_menus{$currentcategory}->{'text'} = $category; + $seltemplate_menus{$currentcategory}->{'default'} = ''; + $seltemplate_menus{$currentcategory}->{'select2'}->{''} = ''; + push(@{$seltemplate_menus{$currentcategory}->{'order'}},''); + } + if ($path) { + $seltemplate_menus{$currentcategory}->{'select2'}->{&js_escape($path)} = $title; + push(@{$seltemplate_menus{$currentcategory}->{'order'}},&js_escape($path)); + if ($help) { + $templatehelp{$path} = $help; + } + } + } + } + + my $templates = $lt{'cate'}.' '. + &Apache::loncommon::linked_select_forms('courseresform','
    '.$lt{'tmpl'}.' ', + $defcategory,'tempcategory','template', + \%seltemplate_menus,\@catorder, + "resize_scrollbox('contentscroll','1','0');", + "toggleExampleText();",'template').'
    '; + my $templatepreview = ''. + ''.&mt('Example').''; + my $crsresform=(< + $lt{'stpr'}$help{'Course_Resource'} +
    + +
    + +RESFORM my $specialdocumentsform; my @specialdocumentsforma; @@ -5245,7 +6177,7 @@ NFFORM $pathitem - $lt{'syll'} + $lt{'syll'} $help{'Syllabus'} @@ -5257,14 +6189,14 @@ NSYLFORM $pathitem - $lt{'grpo'} + $lt{'grpo'} $help{'Group Portfolio'} NGFFORM @specialdocumentsforma=( {''.$lt{newp}.''=>$newpageform}, - {''.$lt{syll}.''=>$newsylform}, - {''.$lt{navc}.''=>$newnavform}, + {''.$lt{syll}.''=>$newsylform}, + {''.$lt{navc}.''=>$newnavform}, {''.$lt{sipa}.''=>$newsmppageform}, {''.$lt{webp}.''=>$newwebpageform}, ); @@ -5274,6 +6206,11 @@ NGFFORM my @importdoc = ( {''.$lt{extr}.''=>$extresourcesform} ); + if (keys(%ltitools)) { + push(@importdoc, + {''.$lt{extt}.''=>$exttoolform}, + ); + } unless ($container eq 'page') { push(@importdoc, {''.$lt{imsf}.''=>$imspform} @@ -5288,7 +6225,7 @@ NGFFORM {''.$lt{sipr}.''=>$newsmpproblemform}, {''.$lt{drbx}.''=>$newdropboxform}, {''.$lt{scuf}.''=>$newexuploadform}, - + {''.$lt{stpr}.''=>$crsresform}, ); $gradingform = &create_form_ul(&create_list_elements(@gradingforma)); @@ -5296,8 +6233,8 @@ NGFFORM {''.$lt{bull}.''=>$newbulform}, {''.$lt{mypi}.''=>$newaboutmeform}, {''.$lt{abou}.''=>$newaboutsomeoneform}, - {''.$lt{rost}.''=>$newrosterform}, - {''.$lt{grpo}.''=>$newgroupfileform}, + {''.$lt{rost}.''=>$newrosterform}, + {''.$lt{grpo}.''=>$newgroupfileform}, ); $communityform = &create_form_ul(&create_list_elements(@communityforma)); @@ -5315,7 +6252,9 @@ unless ($container eq 'page') { $hadchanges=0; unless (($supplementalflag || $toolsflag)) { my $error = &editor($r,$coursenum,$coursedom,$folder,$allowed,'',$crstype, - $supplementalflag,\%orderhash,$iconpath,$pathitem); + $supplementalflag,\%orderhash,$iconpath,$pathitem, + \%ltitools,$canedit,$hostname,\$navmap,$hiddentop); + undef($navmap); if ($error) { $r->print('

    '.$error.'

    '); } @@ -5380,7 +6319,15 @@ SNFFORM my $supextform = &Apache::lonextresedit::extedit_form(1,0,undef,undef,$pathitem, - $help{'Adding_External_Resource'}); + $help{'Adding_External_Resource'}, + undef,undef,undef,undef,undef,undef, + $disabled); + + my $supexttoolform = + &Apache::lonextresedit::extedit_form(1,0,undef,undef,$pathitem, + $help{'Adding_External_Tool'}, + undef,undef,'tool',$coursedom, + $coursenum,\%ltitools,$disabled); my $supnewsylform=(< @@ -5388,7 +6335,7 @@ SNFFORM $pathitem - $lt{'syll'} + $lt{'syll'} $help{'Syllabus'} SNSFORM @@ -5399,7 +6346,7 @@ SNSFORM $pathitem - $lt{'mypi'} + $lt{'mypi'} $help{'My Personal Information Page'} SNAMFORM @@ -5426,19 +6373,25 @@ SWEBFORM my @specialdocs = ( - {''.$lt{syll}.'' + {''.$lt{syll}.'' =>$supnewsylform}, - {''.$lt{mypi}.'' + {''.$lt{mypi}.'' =>$supnewaboutmeform}, {''.$lt{webp}.''=>$supwebpageform}, ); my @supimportdoc = ( {''.$lt{extr}.'' - =>$supextform}, - {''.$lt{upl}.'' + =>$supextform}); + if (keys(%ltitools)) { + push(@supimportdoc, + {''.$lt{extt}.'' + =>$supexttoolform}); + } + push(@supimportdoc, + {''.$lt{upl}.'' =>$supupdocform}, - ); + ); $supupdocform = &create_form_ul(&create_list_elements(@supimportdoc)); my %suporderhash = ( @@ -5448,7 +6401,8 @@ my %suporderhash = ( ); if ($supplementalflag) { my $error = &editor($r,$coursenum,$coursedom,$folder,$allowed,'',$crstype, - $supplementalflag,\%suporderhash,$iconpath,$pathitem); + $supplementalflag,\%suporderhash,$iconpath,$pathitem, + \%ltitools,$canedit,$hostname); if ($error) { $r->print('

    '.$error.'

    '); } else { @@ -5463,12 +6417,12 @@ my %suporderhash = ( } &Apache::lonnet::get_numsuppfiles($coursenum,$coursedom,1); undef($suppchanges); - } - } + } + } } } elsif ($supplementalflag) { my $error = &editor($r,$coursenum,$coursedom,$folder,$allowed,'',$crstype, - $supplementalflag,'',$iconpath,$pathitem); + $supplementalflag,'',$iconpath,$pathitem,'','',$hostname); if ($error) { $r->print('

    '.$error.'

    '); } @@ -5495,7 +6449,9 @@ my %suporderhash = ( &entryline(0,&mt("Click to download or use your browser's Save Link function"),$showdoc).''); } } - $r->print(&Apache::loncommon::end_page()); + unless ($noendpage) { + $r->print(&Apache::loncommon::end_page()); + } return OK; } @@ -5653,7 +6609,7 @@ sub remove_archive { } sub generate_admin_menu { - my ($crstype) = @_; + my ($crstype,$canedit) = @_; my $lc_crstype = lc($crstype); my ($home,$other,%outhash)=&authorhosts(); my %lt= ( # do not translate here @@ -5711,7 +6667,9 @@ sub generate_admin_menu { linktitle => "List the unique identifier used for each resource instance in your $lc_crstype" }, ] - }, + }); + if ($canedit) { + push(@menu, { categorytitle=>'Export', items =>[ { linktext => $lt{'imse'}, @@ -5730,6 +6688,7 @@ sub generate_admin_menu { }, ] }); + } return '
    '."\n". ''."\n". &Apache::lonhtmlcommon::generate_menu(@menu)."\n". @@ -5738,15 +6697,16 @@ sub generate_admin_menu { sub generate_edit_table { my ($tid,$orderhash_ref,$to_show,$iconpath,$jumpto,$readfile, - $need_save,$copyfolder) = @_; + $need_save,$copyfolder,$canedit) = @_; return unless(ref($orderhash_ref) eq 'HASH'); my %orderhash = %{$orderhash_ref}; - my $form; - my $activetab; - my $active; + my ($form, $activetab, $active, $disabled); if (($env{'form.active'} ne '') && ($env{'form.active'} ne '00')) { $activetab = $env{'form.active'}; } + unless ($canedit) { + $disabled = ' disabled="disabled"'; + } my $backicon = $iconpath.'clickhere.gif'; my $backtext = &mt('Exit Editor'); $form = '
    '. @@ -5810,7 +6770,7 @@ sub generate_edit_table { - +
    END @@ -5836,7 +6796,8 @@ END } sub editing_js { - my ($udom,$uname,$supplementalflag) = @_; + my ($udom,$uname,$supplementalflag,$coursedom,$coursenum,$posslti, + $londocroot,$canedit,$hostname,$navmapref) = @_; my %js_lt = &Apache::lonlocal::texthash( p_mnf => 'Name of New Folder', t_mnf => 'New Folder', @@ -5848,6 +6809,7 @@ sub editing_js { p_mdb => 'Title for the Drop Box', p_mbb => 'Title for the Discussion Board', p_mwp => 'Title for Web Page', + p_mnr => 'Title for the Resource', p_mab => "Enter user:domain for User's Personal Information Page", p_mab2 => 'Personal Information Page of ', p_mab_alrt1 => 'Not a valid user:domain', @@ -5876,8 +6838,11 @@ sub editing_js { noor => 'No actions selected or changes to settings specified.', noch => 'No changes to settings specified.', noac => 'No actions selected.', + nofi => 'No file selected', + tinc => 'Title in course', + sunm => 'Sub-directory name', + edri => 'Editing rights unavailable for your current role.', ); - &js_escape(\%js_lt); my $crstype = &Apache::loncommon::course_type(); my $docs_folderpath = &HTML::Entities::encode($env{'environment.internal.'.$env{'request.course.id'}.'.docs_folderpath.folderpath'},'<>&"'); @@ -5885,11 +6850,10 @@ sub editing_js { if (&HTML::Entities::decode($env{'environment.internal.'.$env{'request.course.id'}.'.docs_folderpath.folderpath'}) =~ /\:1$/) { $main_container_page = 1; } - my $toplevelmain = - &escape(&mt('Main Content').':::::'); + my $backtourl; + my $toplevelmain = &escape(&default_folderpath($coursenum,$coursedom,$navmapref)); my $toplevelsupp = &supplemental_base(); - my $backtourl; if ($env{'docs.exit.'.$env{'request.course.id'}} =~ /^direct_(.+)$/) { my $caller = $1; if ($caller =~ /^supplemental/) { @@ -5898,9 +6862,31 @@ sub editing_js { my ($map,$id,$res)=&Apache::lonnet::decode_symb($caller); $res = &Apache::lonnet::clutter($res); if (&Apache::lonnet::is_on_map($res)) { - $backtourl = &HTML::Entities::encode(&Apache::lonnet::clutter($res),'<>&"').'?symb='. + my ($url,$anchor); + if ($res =~ /^([^#]+)#([^#]+)$/) { + $url = $1; + $anchor = $2; + if (($caller =~ m{^([^#]+)\Q#$anchor\E$})) { + $caller = $1.&escape('#').$anchor; + } + } else { + $url = $res; + } + $backtourl = &HTML::Entities::encode(&Apache::lonnet::clutter($url),'<>&"').'?symb='. &HTML::Entities::encode($caller,'<>&"'); - $backtourl = &Apache::loncommon::escape_single($backtourl); + if ($backtourl =~ m{^\Q/public/$coursedom/$coursenum/syllabus\E}) { + if (($ENV{'SERVER_PORT'} == 443) && + ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://})) { + if ($hostname ne '') { + $backtourl = 'http://'.$hostname.$backtourl; + } + $backtourl .= (($backtourl =~ /\?/) ? '&':'?').'usehttp=1'; + } + } + if ($anchor ne '') { + $backtourl .= '#'.&HTML::Entities::encode($anchor,'<>&"'); + } + $backtourl = &Apache::loncommon::escape_single($backtourl); } else { $backtourl = '/adm/navmaps'; } @@ -5914,14 +6900,22 @@ sub editing_js { } my $fieldsets = "'ext','doc'"; + if ($posslti) { + $fieldsets .= ",'tool'"; + } unless ($main_container_page) { $fieldsets .=",'ims'"; } if ($supplementalflag) { $fieldsets = "'suppext','suppdoc'"; + if ($posslti) { + $fieldsets .= ",'supptool'"; + } } - - return < 0) { + document.courseresform.authorrole.selectedIndex = 0; + select1priv_changed(); + document.courseresform.authorpath.selectedIndex = 0; + document.courseresform.newresourceadd.selectedIndex = 0; + toggleNewInCourse(document.courseresform); + if (document.getElementById('newresource')) { + document.getElementById('newresource').style.display = 'none'; + } + } else { + if (numcrsdirs) { + document.courseresform.authorpath.selectedIndex = 0; + } + } + if (document.courseresform.newresusetemp.length) { + document.courseresform.newresusetemp[0].checked = true; + toggleWithTemplate(document.courseresform); + } + document.courseresform.newresourcename.value = ''; + } + } + if (document.courseresform.newsubdir.length) { + for (var j=0; j 1) { + select1res_changed(); + } + } + } + document.getElementById('importcrsresform').style.display=disp; + resize_scrollbox('contentscroll','1','0'); + } + return; +} + function makeims(imsform) { if ((imsform.uploaddoc.value == '') || (!imsform.uploaddoc.value)) { alert("$js_lt{'imsfile'}"); @@ -6061,17 +7333,6 @@ function makeims(imsform) { imsform.submit(); } -function changename(folderpath,index,oldtitle) { -var title=prompt('$js_lt{"p_chn"}',oldtitle); -if (title) { -this.document.forms.renameform.markcopy.value=''; -this.document.forms.renameform.title.value=title; -this.document.forms.renameform.cmd.value='rename_'+index; -this.document.forms.renameform.folderpath.value=folderpath; -this.document.forms.renameform.submit(); -} -} - function updatePick(targetform,index,caller) { var pickitem; var picknumitem; @@ -6237,6 +7498,11 @@ function propagateState(form,param) { } function checkForSubmit(targetform,param,context,idx,folderpath,index,oldtitle,skip_confirm,container,folder,confirm_removal) { + var canedit = '$canedit'; + if (canedit == '') { + alert("$js_lt{'edri'}"); + return; + } var dosettings; var doaction; var control = document.togglemultsettings; @@ -6360,11 +7626,11 @@ for (i = 0; i < currentLis.length; i++) function hideAll(current, nav, data) { unselectInactive(nav); -if (current) { +if (current) { if (current.className == 'right'){ - current.className = 'right active' + current.className = 'right active' } else { - current.className = 'active'; + current.className = 'active'; } } currentData = document.getElementById(data); @@ -6402,13 +7668,15 @@ function showPage(current, pageId, nav, unselectInactive(nav); if ((currstate == 'active') || (currstate == 'right active')) { if (currstate == 'active') { - current.className = ''; + current.className = ''; } else { current.className = 'right'; } - activeTab = ''; + activeTab = ''; toggleUpload(); toggleMap(); + toggleCrsRes(); + toggleImportCrsres(); resize_scrollbox('contentscroll','1','0'); return; } else { @@ -6419,6 +7687,8 @@ function showPage(current, pageId, nav, activeTab = pageId; toggleUpload(); toggleMap(); + toggleCrsRes(); + toggleImportCrsres(); if (nav == 'mainnav') { var storedpath = "$docs_folderpath"; var storedpage = "$main_container_page"; @@ -6782,7 +8052,34 @@ function setBoxes(value) { return; } -ENDNEWSCRIPT +function validImportCrsRes() { + var path = document.crsresimportform.coursepath.options[document.crsresimportform.coursepath.selectedIndex].value; + var fname = document.crsresimportform.coursefile.options[document.crsresimportform.coursefile.selectedIndex].value; + if ((fname == '') || (fname == null)) { + alert("$js_lt{'nofi'}"); + return false; + } + var url = '/res/$coursedom/$coursenum/'; + if (path && path != '/') { + url += path+'/'; + } + if (fname != '') { + url += fname; + } + var title = document.crsresimportform.crsrestitle.value; + document.crsresimportform.importdetail.value=escape(title)+'='+escape(url); + return true; +} + +function validateNewRes(caller) { + if (caller == 'single') { + var role = document.courseresform.authorrole.options[document.courseresform.authorrole.selectedIndex].value; + var authorpath = document.courseresform.authorpath.options[document.courseresform.authorpath.selectedIndex].value; + var resname = document.courseresform.newresourcename.value; + } +} + +ENDSCRIPT } sub history_tab_js { @@ -6912,6 +8209,262 @@ sub makesimpleeditform { SIMPFORM } +sub makenewproblem { + my ($r,$coursedom,$coursenum) = @_; +# Creating a new problem + my ($redirect,$error); + if ($env{'form.authorrole'}) { + my ($newsubdir,$filename); + if ($env{'form.newsubdir'}) { + if ($env{'form.newsubdirname'} ne '') { + $newsubdir = $env{'form.newsubdirname'}; + } + } + if ($env{'form.newresourcename'}) { + $filename = $env{'form.newresourcename'}; + $filename =~ s/\.(\d+)(\.\w+)$/$2/; + $filename =~ s/`//g; + $filename =~ s{/\.\./}{_}g; + $filename =~ s/\.+/./g; + $filename =~ s{/+}{_}g; + if ($filename ne '') { + my ($name,$ext) = ($filename =~ /(.+)\.([^.]+)$/); + if (($ext) && ($ext ne '.problem')) { + $filename = $name.'.problem'; + } elsif ($ext eq '') { + $filename .= '.problem'; + } + my $docroot = $r->dir_config('lonDocRoot'); + my @ids=&Apache::lonnet::current_machine_ids(); + if ($env{'form.authorrole'} eq 'author') { + if ($env{'user.author'}) { + if ($env{'user.home'} && grep(/^\Q$env{'user.home'}\E$/,@ids)) { + my $url = "/priv/$env{'user.domain'}/$env{'user.name'}"; + my $path = $docroot.$url; + my $subdir = $env{'form.authorpath'}; + $redirect = &finishnewprob($url,$path,$subdir,$newsubdir,$filename); + } + } + } elsif ($env{'form.authorrole'} eq 'course') { + my $chome = $env{'course.'.$env{'request.course.id'}.'.home'}; + if ($chome && grep(/^\Q$chome\E$/,@ids)) { + my $url = "/priv/$coursedom/$coursenum"; + my $path=$docroot.$url; + my $subdir = $env{'form.authorpath'}; + $redirect = &finishnewprob($url,$path,$subdir,$newsubdir,$filename); + if ($redirect) { + my $rightsfile = 'default.rights'; + my $sourcerights = "$path/$rightsfile"; + my $targetrights = $docroot."/res/$coursedom/$coursenum/$rightsfile"; + my $now = time; + if (!-e $sourcerights) { + my $cid = $coursedom.'_'.$coursenum; + if (open(my $fh,">$sourcerights")) { + print $fh < + +END + close($fh); + } + } + if (!-e "$sourcerights.meta") { + if (open(my $fh,">$sourcerights.meta")) { + my $author=$env{'environment.firstname'}.' '. + $env{'environment.middlename'}.' '. + $env{'environment.lastname'}.' '. + $env{'environment.generation'}; + $author =~ s/\s+$//; + print $fh <<"END"; + + +$author +$coursenum:$coursedom +private +$now + + +$coursedom +0 + +notset +$now +0 +rights +$env{'user.name'}:$env{'user.domain'} + + + +$coursenum:$coursedom +deny:::course,allow:$cid::course + + + + +END + close($fh); + } + if ((-e $sourcerights) && (-e "$sourcerights.meta")) { + if (!-e "$docroot/res/$coursedom") { + mkdir("$docroot/res/$coursedom",0755); + } + if (!-e "$docroot/res/$coursedom/$coursenum") { + mkdir("$docroot/res/$coursedom/$coursenum",0755); + } + if ((-e "$docroot/res/$coursedom/$coursenum") && (!-e $targetrights)) { + my $nokeyref = &Apache::lonpublisher::getnokey($r->dir_config('lonIncludes')); + my $output = &Apache::lonpublisher::batchpublish($r,$sourcerights,$targetrights,$nokeyref,1); + } + } + } + if ($env{'form.newresourceadd'}) { + my $template = $env{'form.template'}; + my $source = $docroot.$redirect; + my $target = $redirect; + $target =~ s{^/priv/}{/res/}; + $target = $docroot.$target; + if (!-e $source) { + my $copyfrom; + if ($template) { + my %templates; + my @files = &Apache::lonhomework::get_template_list('problem'); + foreach my $poss (@files) { + if (ref($poss) eq 'ARRAY') { + if ($template eq $poss->[0]) { + $templates{$template} = 1; + last; + } + } + } + if ($templates{$template}) { + $copyfrom = $template; + } + } + unless ($copyfrom) { + $copyfrom = $r->dir_config('lonIncludes').'/templates/blank.problem'; + } + &File::Copy::copy($copyfrom,$source); + } + if (!-e "$source.meta") { + my $cid = $coursedom.'_'.$coursenum; + my $now = time; + if (open(my $fh,">$source.meta")) { + my $author=$env{'environment.firstname'}.' '. + $env{'environment.middlename'}.' '. + $env{'environment.lastname'}.' '. + $env{'environment.generation'}; + $author =~ s/\s+$//; + my $title = $env{'form.newresourcetitle'}; + $title =~ s/^\s+|\s+$//g; + print $fh < +$author +$coursenum:$coursedom +custom +$now +/res/$coursedom/$coursenum/default.rights + +$coursedom +0 + +notset +$now +0 +problem +$coursenum:$coursedom + + + +$coursenum:$coursedom + + + +$title +END + close($fh); + } + } + } + } + } + } else { + my ($auname,$audom,$role) = split('___',$env{'form.authorrole'}); + my $rolehome = &Apache::lonnet::homeserver($auname,$audom); + if (grep(/^\Q$rolehome\E$/,@ids)) { + my $now = time; + if (exists($env{'user.role.'.$role.'./'.$audom.'/'.$auname})) { + my ($start,$end) = split(/\./,$env{'user.role.'.$role.'./'.$audom.'/'.$auname}); + if (($start <= $now) && (($end == 0) || ($end >= $now))) { + my $url = "/priv/$audom/$auname"; + my $path = $r->dir_config('lonDocRoot').$url; + my $subdir = $env{'form.authorpath'}; + $redirect = &finishnewprob($url,$path,$subdir,$newsubdir,$filename); + } + } + } + } + } + } + } + return ($redirect,$error); +} + +sub finishnewprob { + my ($url,$path,$subdir,$newsubdir,$filename) = @_; + unless (-d $path) { + unless (mkdir($path,02770)) { + return; + } + } + my $redirect; + if ($subdir ne '/') { + $subdir = &cleandir($subdir); + if (($subdir ne '') && (-d "$path/$subdir")) { + $path .= "/$subdir"; + $url .= "/$subdir"; + } + } + my $dest; + if ($newsubdir ne '') { + $newsubdir = &cleandir($newsubdir); + } + if ($newsubdir ne '') { + if (-d "$path/$newsubdir") { + $dest = "$path/$newsubdir/$filename"; + } else { + my $dirok; + unless (-e "$path/$newsubdir") { + if (mkdir("$path/$newsubdir",02770)) { + if (chmod(02770,"$path/$newsubdir")) { + $dirok = 1; + } + } + } + if ($dirok) { + $dest = "$path/$newsubdir/$filename"; + } + } + if (($dest ne '') && (!-e $dest)) { + $redirect = "$url/$newsubdir/$filename"; + } + } else { + $dest = "$path/$filename"; + if (($dest ne '') && (!-e $dest)) { + $redirect = "$url/$filename"; + } + } + return $redirect; +} + +sub cleandir { + my ($dir) = @_; + $dir =~ s/^\s+//; + $dir =~ s/\s+$//; + $dir =~ s/\.+//g; + $dir =~ s/[\#\?&%\":]//g; + return $dir; +} + 1; __END__