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