--- loncom/interface/loncourserespicker.pm 2012/05/07 02:12:47 1.2 +++ loncom/interface/loncourserespicker.pm 2013/11/13 01:40:07 1.8 @@ -1,6 +1,6 @@ # The LearningOnline Network # -# $Id: loncourserespicker.pm,v 1.2 2012/05/07 02:12:47 raeburn Exp $ +# $Id: loncourserespicker.pm,v 1.8 2013/11/13 01:40:07 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -37,7 +37,8 @@ loncourserespicker provides an interface resources are to be either: (a) exported to an IMS Content Package -(b) subject to access blocking for the duriation of an exam/quiz. +(b) subject to access blocking for the duriation of an exam/quiz. +(c) dumped to an Authoring Space =head1 DESCRIPTION @@ -50,8 +51,8 @@ window used to display the Course Conten =head1 OVERVIEW The main subroutine: &create_picker() will display the hierarchy of folders, -sub-folders, and resources in the Main Course Documents area. Items can be -selected using checkboxes, and/or a "Check All" button. Selection of a folder +sub-folders, and resources in the Main Content area. Items can be selected +using checkboxes, and/or a "Check All" button. Selection of a folder causes the contents of the folder to also be selected automatically. The propagation of check status is recursive into sub-folders. Likewise, if an item deep in a nested set of folders and sub-folders is unchecked, the @@ -59,11 +60,11 @@ uncheck will propagate up through the hi a higher level to become unchecked. There is a submit button, which will be named differently according to the -content in which resource/folder selection is being made. +context in which resource/folder selection is being made. -The two contexts currently supported are: IMS export and selection of +The three contexts currently supported are: IMS export, selection of content to be subject to access restructions for the duration of an -exam. +exam, and selection of items for dumping to an Authoring Space. =head1 INTERNAL SUBROUTINES @@ -74,7 +75,7 @@ select items. Checking a folder causes within the folder. Unchecking a resource causing unchecking of folders containing the item back up to the top level. -Inputs: 7. +Inputs: 9. - $navmap -- Reference to LON-CAPA navmap object (encapsulates information about resources in the course). @@ -95,6 +96,17 @@ Inputs: 7. - $block -- An internal ID (integer) used to track which exam block currently being configured. + - $preamble -- HTML form elements used to select Authoring Space + if more than one available, and also set name of 'Folder + in Authoring Space' where content will be dumped, when + context is 'dumpdocs'. + + - $numhome -- number of possible Authoring Spaces where content could + be dumped when context is 'dumpdocs'. + + - $uploadedfiles -- Reference to hash: keys are paths to files in + /home/httpd/lonUsers/$cdom/$1/$2/$3/$cnum/userfiles. + Output: $output is the HTML mark-up for display/selection of content items in the pop-up window. @@ -111,7 +123,7 @@ Inputs: 7. - $numcount -- Total numer of folders and resources in course. - $context -- Context in which resources are being displayed - (imsexport or examblock). + (imsexport, examblock or dumpdocs). - $formname -- Name of form. @@ -121,7 +133,7 @@ Inputs: 7. - $checked_maps -- Reference to array of folders currently checked. -Output: 1. Javascript (witthin <script></script> tags. +Output: 1. Javascript (within <script></script> tags. =item &get_navmap_object() @@ -132,7 +144,48 @@ no object instantiated. Inputs: 2. - $crstype -- Container type: Course or Community - - $context -- Context: imsexport or examblock + - $context -- Context: imsexport, examblock or dumpdocs + + +=item &clean() + +Takes incoming title and replaces non-alphanumeric characters with underscore, +so title can be used as suggested file name (with appended extension) for file +copied from course to Authoring space. + + +=item &enumerate_course_contents() + +Create hashes of maps (for folders/pages) and symbs (for resources) in +a course, where keys are numbers (starting with 1) and values are +map url, or symb, for an iteration through the course, as seen by +a Course Coordinator. Used to generate numerical IDs to facilitate +(a) storage of lists of maps or resources to be blocked during an exam, +(b) processing selected form element during dumping of selected course + content to Authoring space. + +Inputs: 7 + + $navmap - navmaps object + + $map_url - reference to hash to contain URLs of maps in course + + $resource_symb - reference to hash to contain symbs for + resources in course + + $title_ref - reference to hash containing titles for items in + course + + $context - examblock or dumpdocs + + $cdom - course's domain + + $cnum - courseID + +Outputs: None + +Side Effects: $map_url and $resource_symb hashrefs are populated. + =over @@ -153,15 +206,17 @@ use Apache::lonlocal; use LONCAPA qw(:DEFAULT :match); sub create_picker { - my ($navmap,$context,$formname,$crstype,$blockedmaps,$blockedresources,$block) = @_; + my ($navmap,$context,$formname,$crstype,$blockedmaps,$blockedresources,$block,$preamble,$numhome,$uploadedfiles) = @_; return unless (ref($navmap)); - my ($it,$output,$numdisc,%maps,%resources,%discussiontime,%currmaps,%currresources); + my ($it,$output,$numdisc,%maps,%resources,%discussiontime,%currmaps,%currresources,%files); $it = $navmap->getIterator(undef,undef,undef,1,undef,undef); if (ref($blockedmaps) eq 'HASH') { %currmaps = %{$blockedmaps}; } if (ref($blockedresources) eq 'HASH') { %currresources = %{$blockedresources}; + } elsif (ref($uploadedfiles) eq 'HASH') { + %files = %{$uploadedfiles}; } my @checked_maps; my $curRes; @@ -174,42 +229,43 @@ sub create_picker { my %children = (); my %hierarchy = (); my $location=&Apache::loncommon::lonhttpdurl("/adm/lonIcons"); - my $whitespace = + my $whitespace = '<img src="'.$location.'/whitespace_21.gif" class="LC_docs_spacer" alt="" />'; - my $onsubmit; + my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; + my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; + my $crsprefix = &propath($cdom,$cnum).'/userfiles/'; + + my ($info,$display,$onsubmit,$togglebuttons); if ($context eq 'examblock') { my $maps_elem = 'docs_maps_'.$block; my $res_elem = 'docs_resources_'.$block; $onsubmit = ' onsubmit="return writeToOpener('."'$maps_elem','$res_elem'".');"'; + $info = &mt('Items in '.lc($crstype).' for which access will be blocked.'); } - my $display = - '<form name="'.$formname.'" action="" method="post"'.$onsubmit.'>'."\n". - '<p>'; - if ($context eq 'imsexport') { - $display .= &mt('Choose which items you wish to export from your '. - $crstype.'.'); + if ($context eq 'dumpdocs') { + $info = '<span class="LC_fontsize_medium">'. + &mt('Choose the uploaded course items and templated pages/problems to be copied to Authoring space.'). + '</span><br /><br />'; + $startcount = 3 + $numhome; + $onsubmit = ' onsubmit="return checkUnique(document.'.$formname.',document.'.$formname.'.archive);"'; + } elsif ($context eq 'imsexport') { + $info = &mt('Choose which items you wish to export from your '.$crstype.'.'); $startcount = 5; - } elsif ($context eq 'examblock') { - $display .= &mt('Items in '.lc($crstype).' for which access will be blocked.'); } - $display .= '</p>'; + $togglebuttons = '<input type="button" value="'.&mt('check all').'" '. + 'onclick="javascript:checkAll(document.'.$formname.'.archive)" />'. + ' <input type="button" value="'.&mt('uncheck all').'"'. + ' onclick="javascript:uncheckAll(document.'.$formname.'.archive)" />'; + $display = '<form name="'.$formname.'" action="" method="post"'.$onsubmit.'>'."\n"; if ($context eq 'imsexport') { - $display .= '<div class="LC_columnSection">'."\n". + $display .= $info. + '<div class="LC_columnSection">'."\n". '<fieldset>'. - '<legend>'.&mt('Content items').'</legend>'."\n"; - } - $display .= - '<input type="button" value="'.&mt('check all').'" '. - 'onclick="javascript:checkAll(document.'.$formname.'.archive)" />'. - ' <input type="button" value="'.&mt('uncheck all').'"'. - ' onclick="javascript:uncheckAll(document.'.$formname.'.archive)" />'; - if ($context eq 'imsexport') { - $display .= '</fieldset>'; - %discussiontime = - &Apache::lonnet::dump('discussiontimes', - $env{'course.'.$env{'request.course.id'}.'.domain'}, - $env{'course.'.$env{'request.course.id'}.'.num'}); + '<legend>'.&mt('Content items').'</legend>'."\n". + $togglebuttons. + '</fieldset>'; + %discussiontime = &Apache::lonnet::dump('discussiontimes',$cdom,$cnum); $numdisc = keys(%discussiontime); if ($numdisc > 0) { $display .= @@ -221,9 +277,16 @@ sub create_picker { ' onclick="javascript:uncheckAll(document.'.$formname.'.discussion)" />'. '</fieldset>'; } - $display .= '</div>'; + $display .= '</div>'; + } elsif ($context eq 'examblock') { + $display .= $info.$togglebuttons; + } elsif ($context eq 'dumpdocs') { + $display .= $preamble. + '<div class="LC_left_float">'. + '<fieldset>'. + '<legend>'.&mt('Content to copy').(' 'x4).$togglebuttons.'</legend>'. + $info; } - my $curRes; my $lastcontainer = $startcount; $display .= &Apache::loncommon::start_data_table() .&Apache::loncommon::start_data_table_header_row(); @@ -234,12 +297,14 @@ sub create_picker { } } elsif ($context eq 'examblock') { $display .= '<th>'.&mt('Access blocked?').'</th>'; + } elsif ($context eq 'dumpdocs') { + $display .= '<th>'.&mt('Copy?').'</th>'. + '<th>'.&mt("Title in $crstype"). + '<th>'.&mt('Internal Identifier').'</th>'. + '<th>'.&mt('Save as ...').'</th>'; } $display .= &Apache::loncommon::end_data_table_header_row(); while ($curRes = $it->next()) { - if (ref($curRes)) { - $count ++; - } if ($curRes == $it->BEGIN_MAP()) { $depth++; $parent{$depth} = $lastcontainer; @@ -251,18 +316,29 @@ sub create_picker { if (ref($curRes)) { my $symb = $curRes->symb(); my $ressymb = $symb; - if ($ressymb =~ m|adm/($match_domain)/($match_username)/(\d+)/bulletinboard$|) { + if ($context eq 'dumpdocs') { + next unless (($curRes->src() =~ m{^\Q/uploaded/$cdom/$cnum/\E(docs|supplemental|simplepage)}) || + ($curRes->src() =~ m{^\Q/uploaded/$cdom/$cnum/\E(default|supplemental)_\d+\.(sequence|page)}) || + ($curRes->src() eq '/res/lib/templates/simpleproblem.problem') || + ($curRes->src() =~ m{^/adm/$match_domain/$match_username/\d+/smppg})); + } elsif ($ressymb =~ m|adm/($match_domain)/($match_username)/(\d+)/bulletinboard$|) { unless ($ressymb =~ m|adm/wrapper/adm|) { $ressymb = 'bulletin___'.$3.'___adm/wrapper/adm/'.$1.'/'.$2.'/'.$3.'/bulletinboard'; } } - my $currelem = $count+$boards+$startcount; + $count ++; + my $currelem; + if ($context eq 'imsexport') { + $currelem = $count+$boards+$startcount; + } else { + $currelem = $count+$startcount; + } $display .= &Apache::loncommon::start_data_table_row(). '<td>'."\n". '<input type="checkbox" name="archive" value="'.$count.'" '; if (($curRes->is_sequence()) || ($curRes->is_page())) { $lastcontainer = $currelem; - $display .= 'onclick="javascript:checkFolder(this.form,'."'$currelem'".')" '; + $display .= 'onclick="javascript:checkFolder(document.'.$formname.','."'$currelem'".')" '; my $mapurl = (&Apache::lonnet::decode_symb($symb))[2]; if ($currmaps{$mapurl}) { $display .= 'checked="checked"'; @@ -272,18 +348,21 @@ sub create_picker { if ($curRes->is_problem()) { $numprobs ++; } - $display .= 'onclick="javascript:checkResource(this.form,'."'$currelem'".')" '; + $display .= 'onclick="javascript:checkResource(document.'.$formname.','."'$currelem'".')" '; if ($currresources{$symb}) { $display .= 'checked="checked"'; } } $display .= ' />'."\n"; + if ($context eq 'dumpdocs') { + $display .= '</td><td valign="top">'; + } for (my $i=0; $i<$depth; $i++) { $display .= "$whitespace\n"; } my $icon = 'src="'.$location.'/unknown.gif" alt=""'; if ($curRes->is_sequence()) { - $icon = 'src="'.$location.'/navmap.folder.open.gif" alt="'.&mt('"Folder').'"'; + $icon = 'src="'.$location.'/navmap.folder.open.gif" alt="'.&mt('Folder').'"'; } elsif ($curRes->is_page()) { $icon = 'src="'.$location.'/navmap.page.open.gif" alt="'.&mt('Composite Page').'"'; } elsif ($curRes->is_problem()) { @@ -304,7 +383,7 @@ sub create_picker { } } } - $display .= ' '.$curRes->title().'</td>'."\n"; + $display .= ' '.$curRes->title().$whitespace.'</td>'."\n"; if ($context eq 'imsexport') { # Existing discussion posts? @@ -316,6 +395,35 @@ sub create_picker { } elsif ($numdisc > 0) { $display .= '<td> </td>'."\n"; } + } elsif ($context eq 'dumpdocs') { + my $src = $curRes->src(); + my ($filepath,$title); + if ($src =~ m{^\Q/uploaded/$cdom/$cnum/\E}) { + $filepath = &Apache::lonnet::filelocation('',$src); + $filepath =~ s/\Q$crsprefix\E//; + if ($curRes->is_map()) { + $title = $files{$filepath}; + } else { + $filepath =~ s{docs/}{}; + $title = $filepath; + $title =~ s{^(default|\d+)/\d*/?}{}; + } + } else { + $title = $curRes->title(); + $title =~ s{/}{_}g; + $title = &clean($title); + if ($src eq '/res/lib/templates/simpleproblem.problem') { + my ($map,$id,$res) = &Apache::lonnet::decode_symb($symb); + $map =~ s{^uploaded/$cdom/$cnum/}{}; + $filepath = $map.'_'.$id; + $title .= '.problem'; + } elsif ($src =~ m{^/adm/$match_domain/$match_username/(\d+)/smppg}) { + $filepath = 'smppage_'.$1.'.db'; + $title .= '.html'; + } + } + $display .= '<td>'.$filepath.'</td>'. + '<td><input type="text" size="40" name="namefor_'.$count.'" id="namefor_'.$count.'" value="'.$title.'" /></td>'."\n"; } $display .= &Apache::loncommon::end_data_table_row(); } @@ -333,24 +441,37 @@ sub create_picker { ' '.&mt('Text').'</label></span></p>'; } } - $display .= '<p>'; + my $numcount; if ($context eq 'imsexport') { $display .= + '<p>'. '<input type="hidden" name="finishexport" value="1" />'. '<input type="submit" name="exportcourse" value="'. - &mt('Export').'" />'; + &mt('Export').'" /></p>'; + $numcount = $count + $boards + $startcount; } elsif ($context eq 'examblock') { $display .= + '<p>'. '<input type="submit" name="resourceblocks" value="'. - &mt('Copy Choices to Main Window ').'" />'; + &mt('Copy Choices to Main Window').'" /></p>'; + $numcount = $count + $startcount; + } elsif ($context eq 'dumpdocs') { + $display .= '</fieldset>'. + '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'. + '<div>'. + '<input type="submit" name="dumpcourse" value="'.&mt("Copy $crstype Content").'" />'. + '</div>'; + $numcount = $count + $startcount; } - $display .= '</p></form>'; - my $numcount = $count + $boards + $startcount; + $display .= '</form>'; my $scripttag = &respicker_javascript($startcount,$numcount,$context,$formname,\%children, \%hierarchy,\@checked_maps); + if ($context eq 'dumpdocs') { + return $scripttag.$display; + } my ($title,$crumbs,$args); - if ($context eq 'imsexport') { + if ($context eq 'imsexport') { $title = 'Export '.$crstype.' to IMS Package'; } elsif ($context eq 'examblock') { $title = 'Resources with Access blocked'; @@ -358,10 +479,13 @@ sub create_picker { 'add_entries' => { onload => 'javascript:recurseFolders();' }, }; } - my $output = &Apache::loncommon::start_page($title,$scripttag,$args); + $output = &Apache::loncommon::start_page($title,$scripttag,$args); if ($context eq 'imsexport') { $output .= &Apache::lonhtmlcommon::breadcrumbs('IMS Export'). &Apache::londocs::startContentScreen('tools'); + } elsif ($context eq 'dumpdocs') { + $output .= &Apache::lonhtmlcommon::breadcrumbs('Copying to Authoring Space'). + &Apache::londocs::startContentScreen('tools'); } $output .= $display; if ($context eq 'examblock') { @@ -377,6 +501,15 @@ sub respicker_javascript { $checked_maps) = @_; return unless ((ref($children) eq 'HASH') && (ref($hierarchy) eq 'HASH') && (ref($checked_maps) eq 'ARRAY')); + my ($elem,$nested,$nameforelem); + if ($context eq 'dumpdocs') { + $elem='((parseInt(item)-'.$startcount.')*2)+'.$startcount; + $nested='((parseInt(nesting[item][i])-'.$startcount.')*2)+'.$startcount; + $nameforelem=$elem+1; + } else { + $elem='parseInt(item)'; + $nested='parseInt(nesting[item][i])'; + } my $scripttag = <<"START"; <script type="text/javascript"> // <![CDATA[ @@ -401,7 +534,8 @@ function uncheckAll(field) { } function checkFolder(form,item) { - if (form.elements[item].checked == true) { + var elem = $elem; + if (form.elements[elem].checked == true) { containerCheck(form,item); } else { containerUncheck(form,item); @@ -409,7 +543,8 @@ function checkFolder(form,item) { } function checkResource(form,item) { - if (form.elements[item].checked == false) { + var elem = $elem; + if (form.elements[elem].checked == false) { containerUncheck(form,item); } } @@ -453,12 +588,65 @@ function recurseFolders() { } EXTRA + } elsif ($context eq 'dumpdocs') { + my $blankmsg = &mt('An item selected has no filename set in the "Save as ..." column.'); + my $dupmsg = &mt('Items selected for copying need unique filenames in the "Save as ..." column.'); + $scripttag .= <<"EXTRA"; + +function checkUnique(form,field) { + var duplicate = 0; + var blank = 0; + if (field.length > 0) { + for (i=0; i<field.length; i++) { + if (field[i].checked) { + var item = field[i].value; + var namefor = document.getElementById('namefor_'+item); + if (namefor) { + var possval = namefor.value; + if (!possval) { + blank = item; + break; + } + for (j=i+1; j<field.length; j++) { + if (field[j].checked) { + var curritem = field[j].value; + var currnamefor = document.getElementById('namefor_'+curritem); + if (currnamefor) { + var currval = currnamefor.value; + if (currval == possval) { + duplicate = curritem; + break; + } + } + } + } + if (duplicate) { + break; + } + } + } + } + } + if (blank) { + alert('$blankmsg'); + return false; + } + if (duplicate) { + alert('$dupmsg'); + return false; + } + return true; +} + +EXTRA + } $scripttag .= <<"END"; function containerCheck(form,item) { - form.elements[item].checked = true; + var elem = $elem; + form.elements[elem].checked = true; if(Object.prototype.toString.call(parents[item]) === '[object Array]') { if (parents[item].length > 0) { for (var j=0; j<parents[item].length; j++) { @@ -472,7 +660,7 @@ function containerUncheck(form,item) { if(Object.prototype.toString.call(nesting[item]) === '[object Array]') { if (nesting[item].length > 0) { for (var i=0; i<nesting[item].length; i++) { - form.elements[nesting[item][i]].checked = false; + var nested = $nested; } } } @@ -541,7 +729,9 @@ sub get_navmap_object { $outcome = &Apache::loncommon::start_page('Selection of Resources for Blocking', undef,{'only_body' => 1,}). '<h2>'.&mt('Resource Display Failed').'</h2>'; - } + } elsif ($context eq 'dumpdocs') { + $outcome = '<h2>'.&mt('Copying to Authoring Space unavilable'); + } $outcome .= '<div class="LC_error">'; if ($crstype eq 'Community') { $outcome .= &mt('Unable to retrieve information about community contents'); @@ -549,7 +739,7 @@ sub get_navmap_object { $outcome .= &mt('Unable to retrieve information about course contents'); } $outcome .= '</div>'; - if ($context eq 'imsexport') { + if (($context eq 'imsexport') || ($context eq 'dumpdocs')) { $outcome .= '<a href="/adm/coursedocs">'; if ($crstype eq 'Community') { $outcome .= &mt('Return to Community Editor'); @@ -557,7 +747,11 @@ sub get_navmap_object { $outcome .= &mt('Return to Course Editor'); } $outcome .= '</a>'; - &logthis('IMS export failed - could not create navmap object in '.lc($crstype).':'.$env{'request.course.id'}); + if ($context eq 'imsexport') { + &Apache::lonnet::logthis('IMS export failed - could not create navmap object in '.lc($crstype).':'.$env{'request.course.id'}); + } else { + &Apache::lonnet::logthis('Copying to Authoring Space failed - could not create navmap object in '.lc($crstype).':'.$env{'request.course.id'}); + } } elsif ($context eq 'examblock') { $outcome .= '<href="javascript:window.close();">'.&mt('Close window').'</a>'; } @@ -567,4 +761,44 @@ sub get_navmap_object { } } +sub clean { + my ($title)=@_; + $title=~s/[^\w\/\!\$\%\^\*\-\_\=\+\;\:\,\\\|\`\~]+/\_/gs; + return $title; +} + +sub enumerate_course_contents { + my ($navmap,$map_url,$resource_symb,$titleref,$context,$cdom,$cnum) = @_; + if ((ref($navmap)) && (ref($map_url) eq 'HASH') && + (ref($resource_symb) eq 'HASH') && (ref($titleref) eq 'HASH')) { + my $it = $navmap->getIterator(undef,undef,undef,1,undef,undef); + my $count = 0; + while (my $curRes = $it->next()) { + if (ref($curRes)) { + my $symb = $curRes->symb(); + my $ressymb = $symb; + if ($context eq 'dumpdocs') { + next unless (($curRes->src() =~ m{^\Q/uploaded/$cdom/$cnum/\E(docs|supplemental|simplepage)/}) || + ($curRes->src() =~ m{^\Q/uploaded/$cdom/$cnum/\E(default|supplemental)_\d+\.(sequence|page)}) || + ($curRes->src() eq '/res/lib/templates/simpleproblem.problem') || + ($curRes->src() =~ m{^/adm/$match_domain/$match_username/\d+/smppg})); + } elsif ($ressymb =~ m{adm/($match_domain)/($match_username)/(\d+)/bulletinboard$}) { + unless ($ressymb =~ m{adm/wrapper/adm}) { + $ressymb = 'bulletin___'.$3.'___adm/wrapper/adm/'.$1.'/'.$2.'/'.$3. + '/bulletinboard'; + } + } + $count ++; + if (($curRes->is_sequence()) || ($curRes->is_page())) { + $map_url->{$count} = (&Apache::lonnet::decode_symb($symb))[2]; + } else { + $resource_symb->{$count} = $ressymb; + } + $titleref->{$count} = $curRes->title(); + } + } + } + return; +} + 1;