File:
[LON-CAPA] /
loncom /
imspackages /
imsimportdocs.pm
Revision
1.36:
download - view:
text,
annotated -
select for diffs
Sun Nov 5 20:04:30 2017 UTC (6 years, 9 months ago) by
raeburn
Branches:
MAIN
CVS tags:
version_2_11_X,
version_2_11_5_msu,
version_2_11_5,
version_2_11_4_uiuc,
version_2_11_4_msu,
version_2_11_4,
version_2_11_3_uiuc,
version_2_11_3_msu,
version_2_11_3,
version_2_11_2_msu,
HEAD
- Replace use of system() with File::Path::remove_tree().
1: # The LearningOnline Network with CAPA
2: #
3: # $Id: imsimportdocs.pm,v 1.36 2017/11/05 20:04:30 raeburn Exp $
4: #
5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
8: #
9: # LON-CAPA is free software; you can redistribute it and/or modify
10: # it under the terms of the GNU General Public License as published by
11: # the Free Software Foundation; either version 2 of the License, or
12: # (at your option) any later version.
13: #
14: # LON-CAPA is distributed in the hope that it will be useful,
15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: # GNU General Public License for more details.
18: #
19: # You should have received a copy of the GNU General Public License
20: # along with LON-CAPA; if not, write to the Free Software
21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27:
28: package Apache::imsimportdocs;
29:
30: use Apache::Constants qw(:common :http :methods);
31: use Apache::lonnet;
32: use Apache::londocs;
33: use Apache::loncommon;
34: use Apache::lonlocal;
35: use Apache::imsprocessor;
36: use LONCAPA::map();
37: use lib '/home/httpd/lib/perl/';
38: use LONCAPA;
39: use File::Path();
40:
41: use strict;
42:
43: sub jscript_one {
44: my %lt = &Apache::lonlocal::texthash(
45: se => 'Select',
46: to => 'Import topics only',
47: tp => 'Import topics + posts (with author)',
48: tn => 'Import topics + posts (no author)',
49: es => 'Enroll students only',
50: ea => 'Enroll all users',
51: nr => 'Not required',
52: id => 'You must select one of the additional options when importing Discussion Boards.',
53: ie => 'You must select one of the additional options when importing Enrollment.',
54: ct => 'You must check at least one Content Type.',
55: );
56: return <<"ENDJS";
57: function setOptions(caller,itemnum) {
58: var opForm = document.forms.pickoptions
59: var menu = 1 + itemnum*2
60: opForm.elements[menu].length = 0
61: if (opForm.elements[itemnum*2].checked == true) {
62: if (caller == "board") {
63: opForm.elements[menu].options[0] = new Option("$lt{'se'}","-1",true,true)
64: opForm.elements[menu].options[1] = new Option("$lt{'to'}","topics",true,true)
65: opForm.elements[menu].options[2] = new Option("$lt{'tp'}","allpost",true,true)
66: opForm.elements[menu].options[3] = new Option("$lt{'tn'}","allanon",true,true)
67: }
68: else {
69: if (caller == "users") {
70: opForm.elements[menu].length = 0
71: opForm.elements[menu].options[0] = new Option("$lt{'se'}","-1",true,true)
72: opForm.elements[menu].options[1] = new Option("$lt{'es'}","students",true,true)
73: opForm.elements[menu].options[2] = new Option("$lt{'ea'}","all",true,true)
74: }
75: }
76: }
77: else {
78: opForm.elements[menu].options[0] = new Option("$lt{'nr'}","0",true,true)
79: }
80: opForm.elements[menu].selectedIndex = 0
81: }
82:
83: function verify(caller) {
84: var opForm = document.forms.pickoptions
85: var totcheck = 0;
86: for (var i=0; i<caller; i++) {
87: if (opForm.elements[2*i].checked == true) {
88: totcheck ++
89: if (opForm.elements[2*i].name == "board") {
90: if (opForm.elements[2*i+1].selectedIndex == 0) {
91: alert("$lt{'id'}")
92: return false
93: }
94: }
95: if (opForm.elements[2*i].name == "users") {
96: if (opForm.elements[2*i+1].selectedIndex == 0) {
97: alert("$lt{'ie'}")
98: return false
99: }
100: }
101: }
102: }
103: if (totcheck == 0) {
104: alert("$lt{'ct'}");
105: return false
106: }
107: return true
108: }
109:
110: function nextPage(caller) {
111: if (verify(caller)) {
112: document.forms.pickoptions.submit()
113: }
114: }
115:
116: ENDJS
117:
118: }
119:
120: sub jscript_two {
121: return <<"ENDJS";
122: function init(tf) {
123: setTimeout("self.close()",3000)
124: tf.submit();
125: }
126:
127: ENDJS
128: }
129:
130: sub handler {
131: my $r = shift;
132: &Apache::loncommon::content_type($r,'text/html');
133: $r->send_http_header;
134: return OK if $r->header_only;
135:
136: my @areas = ();
137: my %cmsmap = ();
138: my %areaname = ();
139: &Apache::imsprocessor::ims_config(\@areas,\%cmsmap,\%areaname);
140:
141: # get course data
142: my $coursenum=$env{'course.'.$env{'request.course.id'}.'.num'};
143: my $coursedom=$env{'course.'.$env{'request.course.id'}.'.domain'};
144:
145: # get personal data
146:
147: my $uname=$env{'user.name'};
148: my $udom=$env{'user.domain'};
149: my $plainname=&escape(
150: &Apache::loncommon::plainname($uname,$udom));
151:
152: # does this user have privileges to post, etc?
153: my $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
154: unless ($allowed) {
155: $r->print(&Apache::loncommon::start_page('Import IMS package',undef,
156: {'only_body' => 1,}));
157: $r->print(
158: '<p class="LC_error">'
159: .&mt('Modification of Course Contents Disallowed')
160: .'</p><p>'
161: .&mt('Your current role does not grant you the right to modify course content in this course.')
162: .'</p>'
163: .&Apache::loncommon::end_page()
164: );
165: return OK;
166: }
167:
168: my $javascript;
169: if ($env{'form.phase'} eq 'one') {
170: $javascript = &jscript_one();
171: } elsif ($env{'form.phase'} eq 'two') {
172: $javascript = &jscript_two();
173: }
174:
175: $javascript =
176: "<script type=\"text/javascript\">\n".
177: "//<!--\n$javascript\n// --></script>\n";
178: my $headline = 'Import IMS package';
179: my $start_page = &Apache::loncommon::start_page($headline,
180: $javascript,
181: {'only_body' => 1,})
182: .'<h1>'.&mt($headline).'</h1>';
183: # print screen
184: $r->print($start_page);
185:
186: if ($env{'form.phase'} eq 'one') {
187: &display_one($r,$coursenum,\@areas,\%areaname,%cmsmap);
188: } elsif ($env{'form.phase'} eq 'two') {
189: &display_two($r,$coursenum,$coursedom,$uname,$udom,\@areas,%cmsmap);
190: }
191: $r->print(&Apache::loncommon::end_page());
192: return OK;
193: }
194:
195: sub display_one {
196: my ($r,$crs,$areasref,$areaname,%cmsmap) = @_;
197: my $cms = $env{'form.source'};
198: my $timenow = time;
199: my $tempdir = &Apache::imsprocessor::create_tempdir('DOCS',$crs,$timenow);
200: my $fname = &Apache::imsprocessor::uploadzip('DOCS',$tempdir);
201: my $unzip_result = '';
202: my $manifest_result = '';
203: unless ($tempdir eq '') {
204: $unzip_result = &Apache::imsprocessor::expand_zip($tempdir,$fname);
205: }
206: my %resources = ();
207: my %includedres = ();
208: my %includeditems = ();
209: my %items = ();
210: my %hrefs = ();
211: my %resinfo = ();
212: my %count = ();
213: my $counter = 0;
214: my %count = (
215: announce => 0,
216: board => 0,
217: doc => 0,
218: extlink => 0,
219: msg => 0,
220: pool => 0,
221: quiz => 0,
222: staff => 0,
223: survey => 0,
224: users => 0,
225: );
226:
227: if ($unzip_result ne 'ok') {
228: $r->print(
229: &Apache::loncommon::confirmwrapper(
230: &Apache::lonhtmlcommon::confirm_success(
231: &mt('Processing of your IMS package failed because the file you'
232: .' uploaded could not be unzipped.'),1)
233: .'<br />'.&mt('Error: [_1]',$unzip_result))
234: );
235: return();
236: }
237:
238: # Get manifest file from package
239: $manifest_result = &Apache::imsprocessor::process_manifest(
240: $cms,$tempdir,\%resources,\%items,\%hrefs,
241: \%resinfo,'choose',\%includedres,\%includeditems);
242: if ($manifest_result ne 'ok') {
243: $r->print(
244: '<br />'.&Apache::loncommon::confirmwrapper(
245: &Apache::lonhtmlcommon::confirm_success(
246: &mt('Unpacking of your IMS package failed because an IMS manifest file'
247: .' was not located in the package.'),1))
248: );
249: return();
250: }
251:
252: # Count areas depending on cms version
253: foreach my $res (sort(keys(%resources))) {
254: if ($cms eq 'bb5' || $cms eq 'bb6' || $cms eq 'webctce4') {
255: foreach my $area (keys(%{$cmsmap{$cms}})) {
256: if ($resources{$res}{type} eq $cmsmap{$cms}{$area}) {
257: $count{$area} ++;
258: }
259: }
260: } elsif ($cms eq 'angel5') {
261: foreach my $area (keys(%{$cmsmap{$cms}})) {
262: if ($area eq 'doc') {
263: if (grep/^$resources{$res}{type}$/,@{$cmsmap{$cms}{doc}}) {
264: $count{$area} ++;
265: }
266: } elsif ($resources{$res}{type} eq $cmsmap{$cms}{$area}) {
267: $count{$area} ++;
268: }
269: }
270: } else { # Unknown cms format
271: $r->print(
272: '<span class="LC_warning">'
273: .&mt('Unsupported IMS format: [_1]',$cms)
274: .'</span><br />'
275: );
276: # return();
277: }
278: }
279:
280:
281: # Start output: Step 1 and step 2
282:
283: $r->print(
284: '<form name="pickoptions" method="post" action="">'
285: .&Apache::lonhtmlcommon::topic_bar(
286: 1,&mt('Choose which content types you wish to import'))
287: .'<p>'
288: .&mt('Check the checkboxes for all areas you wish to import from the IMS package:')
289: .'</p>'
290: );
291:
292: $r->print(
293: &Apache::loncommon::start_data_table()
294: .&Apache::loncommon::start_data_table_header_row()
295: .'<th>'.&mt('Import?').'</th>'
296: .'<th>'.&mt('Content type').'</th>'
297: .'<th>'.&mt('Additional options').'</th>'
298: .&Apache::loncommon::end_data_table_header_row()
299: );
300:
301: # Display import row for each area/content type
302: foreach my $area (@{$areasref}) {
303: unless ($count{$area} > 0) { next };
304:
305: my $count_tag = 'flag_'.$counter;
306:
307: # Checkbox: Import?
308: $r->print(
309: &Apache::loncommon::start_data_table_row()
310: .'<td><input name="'.$area.'" type="checkbox"'
311: );
312: if ($area eq 'board' || $area eq 'users') {
313: $r->print(qq| onclick='javascript:setOptions("$area","$counter")'|);
314: }
315:
316: $r->print(' /></td>');
317:
318: # Content Type
319: $r->print(
320: '<td>'
321: .$$areaname{$area}.' - '
322: .&mt('[quant,_1,item]',$count{$area})
323: .'</td>'
324: );
325:
326: # Additional Options
327: $r->print('<td>');
328: if ($area eq 'board') {
329: $r->print(
330: '<select name="db_handling">'
331: .'<option value="-2"><-- '.&mt('Check Import first').'</option>'
332: .'</select>'
333: );
334: } elsif ($area eq 'users') {
335: $r->print(
336: '<select name="user_handling">'
337: .'<option value="-2"><-- '.&mt('Check Import first').'</option>'
338: .'</select>'
339: );
340: } else {
341: $r->print(
342: &mt('None')
343: .'<input type="hidden" name="'.$count_tag.'" />'
344: );
345: }
346: $r->print('</td>');
347:
348: $r->print(&Apache::loncommon::end_data_table_row());
349: $counter ++;
350: }
351:
352: $r->print(&Apache::loncommon::end_data_table());
353:
354: $r->print(
355: &Apache::lonhtmlcommon::topic_bar(
356: 2,&mt('Choose display options for listing of contents of top level of package'))
357: .'<p>'
358: .&mt('Select a display option for the package content:')
359: .'</p>'
360: );
361: $r->print(
362: '<label>'
363: .'<input type="radio" name="toplevel" value="newfolder" />'
364: .&mt('Display listing of contents in a new folder, with folder name:')
365: .'</label>'
366: .' <input type="text" name="foldername" size="15" value="'.&mt('Type Name Here').'" />'
367: .'<br />'
368: .'<label>'
369: .'<input type="radio" name="toplevel" value="oldfolder" checked="checked" />'
370: .&mt('Append listing of contents of top level of package to contents list for the current folder.')
371: .'</label>'
372: );
373:
374: # Buttons
375: $r->print(
376: '<input type="hidden" name="folder" value="'.$env{'form.folder'}.'" />'
377: .'<input type="hidden" name="source" value="'.$cms.'" />'
378: .'<input type="hidden" name="tempdir" value="'.$tempdir.'" />'
379: .'<input type="hidden" name="phase" value="two" />'
380: );
381: $r->print(
382: '<hr />'
383: .'<p>'
384: .'<input type="button" name="exitpage" value="'.&mt('Cancel').'"'
385: .' onclick="javascript:self.close()" />'
386: .' '
387: .'<input type="button" name="nextpage" value="'.&mt('Finish Import').'"'
388: .' onclick="javascript:nextPage('.$counter.')" />'
389: .'</p>'
390: );
391:
392: $r->print('</form>');
393: }
394:
395: sub display_two {
396: my ($r,$crs,$cdom,$uname,$udom,$areas,%cmsmap) = @_;
397: my $folder = $env{'form.folder'};
398: my $cms = $env{'form.source'};
399: my $tempdir = $env{'form.tempdir'};
400: my %importareas = ();
401: my %includedres = ();
402: my %includeditems = ();
403: my @targets = ();
404: my %resources = ();
405: my %items = ();
406: my %hrefs = ();
407: my %urls = ();
408: my %resinfo = ();
409: my %total = (
410: page => 0,
411: prob => 0,
412: seq => 0,
413: board => 0,
414: quiz => 0,
415: surv => 0,
416: );
417: my @pages = ();
418: my @sequences = ();
419: my @resrcfiles = ();
420:
421: my $timenow = time;
422:
423: my $destdir = $Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.$cdom.'/'.$crs.'/'.$timenow;
424: my $seqstem = "/uploaded/$cdom/$crs/$timenow";
425: my $db_handling = '';
426: my $user_handling = '';
427:
428: my $toplevel = '';
429: my $foldername = '';
430: my %topitems = ();
431: if (defined($env{'form.toplevel'}) ) {
432: $toplevel = $env{'form.toplevel'};
433: }
434: if (defined($env{'form.foldername'}) ) {
435: $foldername = $env{'form.foldername'};
436: }
437:
438: foreach my $area (@{$areas}) {
439: if (defined($env{"form.$area"}) && ($env{'form.'.$area} ne '')) {
440: if ($cms eq 'angel5' && $area eq 'doc') {
441: foreach (@{$cmsmap{$cms}{$area}}) {
442: $importareas{$_} = 1;
443: }
444: } else {
445: $importareas{$cmsmap{$cms}{$area}} = 1;
446: }
447: if ($area eq 'board') {
448: $db_handling = $env{'form.db_handling'};
449: } elsif ($area eq 'users') {
450: $user_handling = $env{'form.user_handling'};
451: }
452: }
453: }
454:
455: my $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,\%resources,\%items,\%hrefs,\%resinfo,'prepare',\%includedres,\%includeditems);
456: if ($manifest_result eq 'ok') {
457: foreach my $res (sort(keys(%resources))) {
458: if ($importareas{$resources{$res}{type}}) {
459: $includedres{$res} = 1;
460: }
461: }
462: foreach my $itm (sort(keys(%items))) {
463: &Apache::imsprocessor::get_imports(\%includeditems,\%items,\%resources,\%importareas,$itm);
464: }
465: }
466: foreach my $itm (sort(keys(%includeditems))) {
467: &Apache::imsprocessor::get_parents(\%includeditems,\%items,$itm);
468: }
469:
470: $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,\%resources,\%items,\%hrefs,\%resinfo,'build',\%includedres,\%includeditems);
471: if ($manifest_result eq 'ok') {
472:
473: my @path = ($cdom,$crs,$timenow);
474: my $fullpath = $Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles';
475: foreach my $item (@path) {
476: $fullpath .= '/'.$item;
477: if (!-e "$fullpath") {
478: mkdir("$fullpath",0770);
479: }
480: }
481: my @namedirs = ("resfiles","sequences","pages","problems");
482: foreach my $name (@namedirs) {
483: if (!-e "$fullpath/$name") {
484: mkdir("$fullpath/$name",0770);
485: }
486: }
487: &Apache::imsprocessor::target_resources(\%resources,\%importareas,\@targets);
488:
489: my @boards = ();
490: my @announcements = ();
491: my @quizzes = ();
492: my @surveys = ();
493: my @pools = ();
494: my @groups = ();
495: my %messages = ();
496: my @timestamp = ();
497: my %boardnum = ();
498: my @topurls = ();
499: my @topnames = ();
500: my @packages = ();
501:
502: &Apache::imsprocessor::process_resinfo($cms,'DOCS',$tempdir,$destdir,\%items,\%resources,\@targets,\@boards,\@announcements,\@quizzes,\@surveys,\@pools,\@groups,\%messages,\@timestamp,\%boardnum,\%resinfo,$udom,$uname,$cdom,$crs,$db_handling,$user_handling,\%total,$seqstem,$seqstem,\@resrcfiles,\@packages,\%hrefs,\@pages,\@sequences);
503:
504: my $copy_result = &Apache::imsprocessor::copy_resources('DOCS',$cms,\%hrefs,\%resources,$tempdir,\@targets,\%urls,$crs,$cdom,$destdir,$timenow,\%importareas);
505:
506: &Apache::imsprocessor::build_structure($cms,'DOCS',$destdir,\%items,\%resinfo,\%resources,\@targets,\%hrefs,$udom,$uname,'',$timenow,$cdom,$crs,\@timestamp,\%total,\@boards,\@announcements,\@quizzes,\@surveys,\@pools,\%boardnum,\@pages,\@sequences,\@topurls,\@topnames,\@packages,\%includeditems);
507:
508: foreach my $item (@pages) {
509: my $filename = $timenow.'/pages/'.$item;
510: my $fetchresult= &Apache::lonnet::process_coursefile('propagate',$crs,$cdom,$filename,'');
511: }
512: foreach my $item (@sequences) {
513: unless ($item eq 'Top.sequence' && $toplevel eq 'oldfolder') {
514: my $filename = $timenow.'/sequences/'.$item;
515: my $fetchresult= &Apache::lonnet::process_coursefile('propagate',$crs,$cdom,$filename,'');
516: }
517: }
518: foreach my $item (@resrcfiles) {
519: my $filename = $timenow.'/resfiles/'.$item;
520: my $fetchresult= &Apache::lonnet::process_coursefile('propagate',$crs,$cdom,$filename,'');
521: }
522:
523: my @imports = ();
524: if ($toplevel eq 'oldfolder') {
525: for (my $i=0; $i<@topurls; $i++) {
526: my $url = &unescape($topurls[$i]);
527: my $name = &unescape($topnames[$i]);
528: push(@imports, [$name, $url]);
529: }
530: } elsif ($toplevel eq 'newfolder') {
531: my $url = &unescape("/uploaded/$cdom/$crs/$timenow/sequences/Top.sequence");
532: my $name = &unescape("$env{'form.foldername'}");
533: push(@imports, [$name, $url]);
534: }
535: my $errtext='';
536: my $fatal=0;
537: ($errtext,$fatal)= &Apache::londocs::mapread($crs,$cdom,$folder.'.sequence');
538: if ($#LONCAPA::map::order<1) {
539: $LONCAPA::map::order[0]=1;
540: $LONCAPA::map::resources[1]='';
541: }
542: my ($errtext,$fatal)=&Apache::londocs::group_import($crs,$cdom,$folder,'sequence','imsimport',@imports);
543: if ($fatal) {
544: &Apache::lonnet::logthis("Fatal error during group_import.");
545: }
546: }
547: if ($tempdir =~ m/^\/home\/httpd\/perl\/tmp\/$crs\/\d{10}/) {
548: &File::Path::remove_tree($tempdir,{ safe => 1 });
549: }
550:
551: # All done, display success message
552: $r->print(
553: '<p class="LC_success">'
554: .&mt('Your import is complete.')
555: .'</p>'
556: );
557: # Re-initialize Button
558: my $initbutton =
559: '<input type="button" value="'
560: .&mt('re-initializing Course')
561: .'" onclick="javascript:init(this.form)" />';
562: $r->print(
563: '<form method="post" action="/adm/roles" target="loncapaclient" name="importDone">'
564: .'<input type="hidden" name="orgurl" value="/adm/coursedocs" />'
565: .'<input type="hidden" name="selectrole" value="1" />'
566: .'<input type="hidden" name="'.$env{'request.role'}.'" value="1" />'
567: .'<p class="LC_warning">'
568: .&mt('Changes will become active for your current session after [_1]'
569: .', or the next time you log in.'
570: ,$initbutton)
571: .'</p>'
572: .'</form>'
573: );
574: }
575:
576: 1;
577: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>