Annotation of loncom/interface/loncreatecourse.pm, revision 1.38
1.1 www 1: # The LearningOnline Network
2: # Create a course
1.5 albertel 3: #
1.38 ! www 4: # $Id: loncreatecourse.pm,v 1.37 2003/09/09 17:26:03 www Exp $
1.5 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.1 www 28: # (My Desk
29: #
30: # (Internal Server Error Handler
31: #
32: # (Login Screen
33: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
34: # 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
35: #
36: # 3/1/1 Gerd Kortemeyer)
37: #
38: # 3/1 Gerd Kortemeyer)
39: #
1.4 www 40: # 2/14,2/16,2/17,7/6 Gerd Kortemeyer
1.1 www 41: #
42: package Apache::loncreatecourse;
43:
44: use strict;
45: use Apache::Constants qw(:common :http);
46: use Apache::lonnet;
1.12 www 47: use Apache::loncommon;
1.13 www 48: use Apache::lonratedt;
49: use Apache::londocs;
1.38 ! www 50: use Apache::lonlocal;
1.28 www 51:
52: # -------------------------------------------- Return path to profile directory
53:
54: sub propath {
55: my ($udom,$uname)=@_;
56: $udom=~s/\W//g;
57: $uname=~s/\W//g;
58: my $subdir=$uname.'__';
59: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
60: my $proname="$Apache::lonnet::perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
61: return $proname;
62: }
63:
64: # ================================================ Get course directory listing
65:
66: sub crsdirlist {
67: my ($courseid,$which)=@_;
68: unless ($which) { $which=''; }
69: my %crsdata=&Apache::lonnet::coursedescription($courseid);
70: my @listing=&Apache::lonnet::dirlist
71: ($which,$crsdata{'domain'},$crsdata{'num'},
72: &propath($crsdata{'domain'},$crsdata{'num'}));
73: my @output=();
74: foreach (@listing) {
75: unless ($_=~/^\./) {
76: push (@output,(split(/\&/,$_))[0]);
77: }
78: }
79: return @output;
1.29 www 80: }
81:
82: # ============================================================= Read a userfile
83:
84: sub readfile {
85: my ($courseid,$which)=@_;
86: my %crsdata=&Apache::lonnet::coursedescription($courseid);
87: return &Apache::lonnet::getfile('/uploaded/'.$crsdata{'domain'}.'/'.
88: $crsdata{'num'}.'/'.$which);
89: }
90:
91: # ============================================================ Write a userfile
92:
93: sub writefile {
1.30 www 94: (my $courseid, my $which,$ENV{'form.output'})=@_;
1.29 www 95: my %crsdata=&Apache::lonnet::coursedescription($courseid);
96: return &Apache::lonnet::finishuserfileupload(
97: $crsdata{'num'},$crsdata{'domain'},
98: $crsdata{'home'},
99: 'output',$which);
100: }
101:
1.36 www 102: # ===================================================================== Rewrite
103:
104: sub rewritefile {
105: my ($contents,%rewritehash)=@_;
106: foreach (keys %rewritehash) {
107: my $pattern=$_;
108: $pattern=~s/(\W)/\\$1/gs;
109: my $new=$rewritehash{$_};
110: $contents=~s/$pattern/$new/gs;
111: }
112: return $contents;
113: }
114:
1.29 www 115: # ============================================================= Copy a userfile
116:
117: sub copyfile {
118: my ($origcrsid,$newcrsid,$which)=@_;
1.36 www 119: unless ($which=~/\.sequence$/) {
120: return &writefile($newcrsid,$which,
121: &readfile($origcrsid,$which));
122: } else {
123: my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
124: my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
125: return &writefile($newcrsid,$which,
126: &rewritefile(
127: &readfile($origcrsid,$which),
128: (
129: '/uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'
130: => '/uploaded/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/'
131: )));
132: }
1.30 www 133: }
134:
135: # =============================================================== Copy a dbfile
136:
137: sub copydb {
138: my ($origcrsid,$newcrsid,$which)=@_;
139: $which=~s/\.db$//;
140: my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
141: my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
142: my %data=&Apache::lonnet::dump
143: ($which,$origcrsdata{'domain'},$origcrsdata{'num'});
144: return &Apache::lonnet::put
145: ($which,\%data,$newcrsdata{'domain'},$newcrsdata{'num'});
146: }
147:
1.35 www 148: # ========================================================== Copy resourcesdata
149:
150: sub copyresourcedb {
151: my ($origcrsid,$newcrsid)=@_;
152: my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
153: my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
154: my %data=&Apache::lonnet::dump
155: ('resourcedata',$origcrsdata{'domain'},$origcrsdata{'num'});
156: $origcrsid=~s/^\///;
157: $origcrsid=~s/\//\_/;
158: $newcrsid=~s/^\///;
159: $newcrsid=~s/\//\_/;
160: my %newdata=();
161: undef %newdata;
162: my $startdate=$data{$origcrsid.'.0.opendate'};
163: my $today=time;
164: my $delta=0;
165: if ($startdate) {
166: my $oneday=60*60*24;
167: $delta=$today-$startdate;
168: $delta=int($delta/$oneday)*$oneday;
169: }
170: # ugly retro fix for broken version of types
171: foreach (keys %data) {
172: if ($_=~/\wtype$/) {
173: my $newkey=$_;
174: $newkey=~s/type$/\.type/;
175: $data{$newkey}=$data{$_};
176: delete $data{$_};
177: }
178: }
1.37 www 179: # adjust symbs
180: my $pattern='uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/';
181: $pattern=~s/(\W)/\\$1/gs;
182: my $new= 'uploaded/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/';
183: foreach (keys %data) {
184: if ($_=~/$pattern/) {
185: my $newkey=$_;
186: $newkey=~s/$pattern/$new/;
187: $data{$newkey}=$data{$_};
188: delete $data{$_};
189: }
190: }
1.35 www 191: # adjust dates
192: foreach (keys %data) {
193: my $thiskey=$_;
194: $thiskey=~s/^$origcrsid/$newcrsid/;
195: $newdata{$thiskey}=$data{$_};
196: if ($data{$_.'.type'}=~/^date/) {
197: $newdata{$thiskey}=$newdata{$thiskey}+$delta;
198: }
199: }
200: return &Apache::lonnet::put
201: ('resourcedata',\%newdata,$newcrsdata{'domain'},$newcrsdata{'num'});
202: }
203:
1.30 www 204: # ========================================================== Copy all userfiles
205:
206: sub copyuserfiles {
207: my ($origcrsid,$newcrsid)=@_;
208: foreach (&crsdirlist($origcrsid,'userfiles')) {
209: ©file($origcrsid,$newcrsid,$_);
210: }
211: }
212: # ========================================================== Copy all userfiles
213:
214: sub copydbfiles {
215: my ($origcrsid,$newcrsid)=@_;
216: foreach (&crsdirlist($origcrsid)) {
217: if ($_=~/\.db$/) {
218: unless
1.34 www 219: ($_=~/^(nohist\_|discussiontimes|classlist|versionupdate|resourcedata)/) {
1.30 www 220: ©db($origcrsid,$newcrsid,$_);
221: }
222: }
223: }
1.31 www 224: }
225:
226: # ======================================================= Copy all course files
227:
228: sub copycoursefiles {
229: my ($origcrsid,$newcrsid)=@_;
230: ©userfiles($origcrsid,$newcrsid);
231: ©dbfiles($origcrsid,$newcrsid);
1.35 www 232: ©resourcedb($origcrsid,$newcrsid);
1.28 www 233: }
1.13 www 234:
1.2 www 235: # ===================================================== Phase one: fill-in form
236:
1.10 matthew 237: sub print_course_creation_page {
1.2 www 238: my $r=shift;
1.10 matthew 239: my $defdom=$ENV{'request.role.domain'};
240: my %host_servers = &Apache::loncommon::get_library_servers($defdom);
241: my $course_home = '<select name="course_home" size="1">'."\n";
242: foreach my $server (sort(keys(%host_servers))) {
1.14 matthew 243: $course_home .= qq{<option value="$server"};
244: if ($server eq $Apache::lonnet::perlvar{'lonHostID'}) {
245: $course_home .= " selected ";
246: }
247: $course_home .= qq{>$server $host_servers{$server}</option>};
1.10 matthew 248: }
249: $course_home .= "\n</select>\n";
1.9 matthew 250: my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
1.12 www 251: my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
1.17 www 252: my $helplink=&Apache::loncommon::help_open_topic('Create_Course','Help on Creating Courses');
1.32 www 253: my $cloneform=&Apache::loncommon::select_dom_form
254: ($ENV{'request.role.domain'},'clonedomain').
255: &Apache::loncommon::selectcourse_link
256: ('ccrs','clonecourse','clonedomain');
257: my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript();
1.2 www 258: $r->print(<<ENDDOCUMENT);
259: <html>
1.6 matthew 260: <script language="JavaScript" type="text/javascript">
261: var editbrowser = null;
262: function openbrowser(formname,elementname) {
263: var url = '/res/?';
264: if (editbrowser == null) {
265: url += 'launch=1&';
266: }
267: url += 'catalogmode=interactive&';
268: url += 'mode=edit&';
269: url += 'form=' + formname + '&';
1.7 matthew 270: url += 'element=' + elementname + '&';
271: url += 'only=sequence' + '';
1.6 matthew 272: var title = 'Browser';
273: var options = 'scrollbars=1,resizable=1,menubar=0';
274: options += ',width=700,height=600';
275: editbrowser = open(url,title,options,'1');
276: editbrowser.focus();
277: }
278: </script>
1.32 www 279: $coursebrowserjs
1.2 www 280: <head>
281: <title>The LearningOnline Network with CAPA</title>
282: </head>
1.12 www 283: $bodytag
1.17 www 284: $helplink
1.6 matthew 285: <form action="/adm/createcourse" method="post" name="ccrs">
1.10 matthew 286: <h2>Course Information</h2>
287: <p>
288: <b>Course Title:</b>
1.6 matthew 289: <input type="text" size="50" name="title">
1.10 matthew 290: </p><p>
1.13 www 291: <b>Course Home Server:</b>$course_home
292: </p><p>
293: <b>Course ID/Number (optional)</b>
294: <input type="text" size="30" name="crsid">
295: </p>
296: <h2>Course Content</h2>
1.32 www 297: <table border="2">
298: <tr><th>Completely new course</th><th>Clone an existing course</th></tr>
299: <tr><td>
1.13 www 300: <p>
1.11 www 301: <b>Map:</b>
1.6 matthew 302: <input type="text" size="50" name="topmap">
1.24 www 303: <a href="javascript:openbrowser('ccrs','topmap')">Select Map</a>
1.10 matthew 304: </p><p>
1.32 www 305: <b>Do NOT generate as standard course</b><br />
1.11 www 306: (only check if you know what you are doing):
307: <input type="checkbox" name="nonstandard">
1.13 www 308: </p>
309: <p>
1.32 www 310: <b>First Resource</b><br />(standard courses only):
1.17 www 311: <input type="radio" name="firstres" value="blank">Blank
1.13 www 312:
1.17 www 313: <input type="radio" name="firstres" value="syl" checked>Syllabus
1.13 www 314:
315: <input type="radio" name="firstres" value="nav">Navigate
316: </p>
1.32 www 317: </td><td>
318: Course ID: <input input type="text" size="25" name="clonecourse" value="" />
319: <br />
320: Domain:
321: $cloneform<br /> <br />
322: Additional settings, if specified below, will override cloned settings.
323: </td></tr>
324: </table>
1.13 www 325: <h2>Assessment Parameters</h2>
326: <p>
1.11 www 327: <b>Open all assessments: </b>
328: <input type="checkbox" name="openall" checked>
1.13 www 329: </p>
330: <h2>Messaging</h2>
331: <p>
1.11 www 332: <b>Set course policy feedback to Course Coordinator: </b>
333: <input type="checkbox" name="setpolicy" checked>
334: </p><p>
335: <b>Set content feedback to Course Coordinator: </b>
336: <input type="checkbox" name="setcontent" checked>
337: </p>
1.16 www 338: <h2>Communication</h2>
339: <p>
340: <b>Disable student resource discussion: </b>
1.26 matthew 341: <input type="checkbox" name="disresdis" /> <br />
342: <b>Disable student use of chatrooms: </b>
343: <input type="checkbox" name="disablechat" />
1.16 www 344: </p>
1.18 www 345: <h2>Access Control</h2>
346: <p>
347: <b>Students need access key to enter course: </b>
348: <input type="checkbox" name="setkeys" />
349: </p>
1.10 matthew 350: <h2>Course Coordinator</h2>
351: <p>
1.11 www 352: <b>Username:</b> <input type="text" size="15" name="ccuname" />
353: </p><p>
354: <b>Domain:</b> $domform
1.10 matthew 355: </p><p>
1.11 www 356: <b>Immediately expire own role as Course Coordinator:</b>
357: <input type="checkbox" name="expireown" checked>
1.10 matthew 358: </p><p>
359: <input type="hidden" name="phase" value="two" />
1.6 matthew 360: <input type="submit" value="Open Course">
1.10 matthew 361: </p>
1.2 www 362: </form>
363: </body>
364: </html>
365: ENDDOCUMENT
366: }
367:
368: # ====================================================== Phase two: make course
369:
1.10 matthew 370: sub create_course {
1.2 www 371: my $r=shift;
372: my $topurl='/res/'.&Apache::lonnet::declutter($ENV{'form.topmap'});
373: my $ccuname=$ENV{'form.ccuname'};
374: my $ccdomain=$ENV{'form.ccdomain'};
375: $ccuname=~s/\W//g;
376: $ccdomain=~s/\W//g;
377: my $cdescr=$ENV{'form.title'};
378: my $curl=$ENV{'form.topmap'};
1.12 www 379: my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
1.2 www 380: $r->print(<<ENDENHEAD);
381: <html>
382: <head>
383: <title>The LearningOnline Network with CAPA</title>
384: </head>
1.12 www 385: $bodytag
1.2 www 386: ENDENHEAD
1.10 matthew 387: #
388: # Verify data
389: #
390: # Check the veracity of the course coordinator
1.2 www 391: if (&Apache::lonnet::homeserver($ccuname,$ccdomain) eq 'no_host') {
1.3 www 392: $r->print('No such user '.$ccuname.' at '.$ccdomain.'</body></html>');
1.2 www 393: return;
394: }
1.10 matthew 395: # Check the proposed home server for the course
396: my %host_servers = &Apache::loncommon::get_library_servers
397: ($ENV{'request.role.domain'});
398: if (! exists($host_servers{$ENV{'form.course_home'}})) {
399: $r->print('Invalid home server for course: '.
400: $ENV{'form.course_home'}.'</body></html>');
401: return;
402: }
1.2 www 403: #
404: # Open course
405: #
1.32 www 406: my %cenv=();
1.10 matthew 407: my $courseid=&Apache::lonnet::createcourse($ENV{'request.role.domain'},
408: $cdescr,$curl,
1.11 www 409: $ENV{'form.course_home'},
410: $ENV{'form.nonstandard'});
1.2 www 411:
1.27 bowersj2 412: # Note: The testing routines depend on this being output; see
413: # Utils::Course. This needs to at least be output as a comment
414: # if anyone ever decides to not show this, and Utils::Course::new
415: # will need to be suitably modified.
1.4 www 416: $r->print('New LON-CAPA Course ID: '.$courseid.'<br>');
417: #
1.12 www 418: # Check if created correctly
1.4 www 419: #
420: my ($crsudom,$crsunum)=($courseid=~/^\/(\w+)\/(\w+)$/);
421: my $crsuhome=&Apache::lonnet::homeserver($crsunum,$crsudom);
422: $r->print('Created on: '.$crsuhome.'<br>');
1.12 www 423: #
1.32 www 424: # Are we cloning?
425: #
426: my $cloneid='';
427: if (($ENV{'form.clonecourse'}) && ($ENV{'form.clonedomain'})) {
428: $cloneid='/'.$ENV{'form.clonedomain'}.'/'.$ENV{'form.clonecourse'};
429: my ($clonecrsudom,$clonecrsunum)=($cloneid=~/^\/(\w+)\/(\w+)$/);
430: my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
431: if ($clonehome eq 'no_host') {
432: $r->print(
433: '<br /><font color="red">Attempting to clone non-existing course '.$cloneid.'</font>');
434: } else {
435: $r->print(
436: '<br /><font color="green">Cloning course from '.$clonehome.'</font>');
1.37 www 437: my %oldcenv=&Apache::lonnet::dump('environment',$crsudom,$crsunum);
1.32 www 438: # Copy all files
439: ©coursefiles($cloneid,$courseid);
1.37 www 440: # Restore URL
441: $cenv{'url'}=$oldcenv{'url'};
1.32 www 442: # Restore title
1.37 www 443: $cenv{'description'}=$oldcenv{'description'};
444: # Mark as cloned
1.35 www 445: $cenv{'clonedfrom'}=$cloneid;
1.32 www 446: }
447: }
448: #
449: # Set environment (will override cloned, if existing)
1.12 www 450: #
1.4 www 451: if ($ENV{'form.crsid'}) {
1.12 www 452: $cenv{'courseid'}=$ENV{'form.crsid'};
453: }
454: if (($ccdomain) && ($ccuname)) {
455: if ($ENV{'form.setpolicy'}) {
456: $cenv{'policy.email'}=$ccuname.':'.$ccdomain;
457: }
458: if ($ENV{'form.setcontent'}) {
1.18 www 459: $cenv{'question.email'}=$ccuname.':'.$ccdomain;
460: }
461: }
462: if ($ENV{'form.setkeys'}) {
463: $cenv{'keyaccess'}='yes';
1.16 www 464: }
465: if ($ENV{'form.disresdis'}) {
466: $cenv{'pch.roles.denied'}='st';
1.26 matthew 467: }
468: if ($ENV{'form.disablechat'}) {
469: $cenv{'plc.roles.denied'}='st';
1.21 albertel 470: }
1.23 bowersj2 471:
1.32 www 472: # Record we've not yet viewed the Course Initialization Helper for this
473: # course
1.23 bowersj2 474: $cenv{'course.helper.not.run'} = 1;
1.21 albertel 475: #
476: # Use new Randomseed
477: #
1.22 albertel 478: $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
1.25 matthew 479: #
480: # By default, use standard grading
481: $cenv{'grading'} = 'standard';
1.22 albertel 482:
1.32 www 483: $r->print('<br />Setting environment: '.
1.12 www 484: &Apache::lonnet::put('environment',\%cenv,$crsudom,$crsunum).'<br>');
485: #
486: # Open all assignments
487: #
488: if ($ENV{'form.openall'}) {
489: my $storeunder=$crsudom.'_'.$crsunum.'.0.opendate';
1.33 www 490: my %storecontent = ($storeunder => time,
491: $storeunder.'.type' => 'date_start');
1.12 www 492:
493: $r->print('Opening all assignments: '.&Apache::lonnet::cput
494: ('resourcedata',\%storecontent,$crsudom,$crsunum).'<br>');
495: }
1.13 www 496: #
497: # Set first page
498: #
499: unless (($ENV{'form.nonstandard'}) || ($ENV{'form.firstres'} eq 'blank')) {
500: $r->print('Setting first resource: ');
501: my ($errtext,$fatal)=
502: &Apache::londocs::mapread($crsunum,$crsudom,'default.sequence');
503: $r->print(($fatal?$errtext:'read ok').' - ');
504: my $title; my $url;
505: if ($ENV{'form.firstres'} eq 'syl') {
506: $title='Syllabus';
507: $url='/public/'.$crsudom.'/'.$crsunum.'/syllabus';
508: } else {
509: $title='Navigate Contents';
510: $url='/adm/navmaps';
511: }
512: $Apache::lonratedt::resources[1]=$title.':'.$url.':false:start:res';
1.15 albertel 513: ($errtext,$fatal)=
1.13 www 514: &Apache::londocs::storemap($crsunum,$crsudom,'default.sequence');
515: $r->print(($fatal?$errtext:'write ok').'<br>');
516: }
1.2 www 517: #
518: # Make current user course adminstrator
519: #
1.12 www 520: my $end=undef;
521: my $addition='';
522: if ($ENV{'form.expireown'}) { $end=time+5; $addition='expired'; }
523: $r->print('Assigning '.$addition.' role of course coordinator to self: '.
1.2 www 524: &Apache::lonnet::assignrole(
1.12 www 525: $ENV{'user.domain'},$ENV{'user.name'},$courseid,'cc',$end).'<br>');
1.2 www 526: #
527: # Make additional user course administrator
528: #
1.12 www 529: if (($ccdomain) && ($ccuname)) {
1.2 www 530: $r->print('Assigning role of course coordinator to '.
531: $ccuname.' at '.$ccdomain.': '.
1.3 www 532: &Apache::lonnet::assignrole($ccdomain,$ccuname,$courseid,'cc').'<p>');
1.12 www 533: }
1.20 www 534: if ($ENV{'form.setkeys'}) {
535: $r->print(
536: '<p><a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">Manage Access Keys</a></p>');
537: }
538: $r->print('<p>Roles will be active at next login.</p></body></html>');
1.2 www 539: }
540:
541: # ===================================================================== Handler
1.1 www 542: sub handler {
543: my $r = shift;
544:
545: if ($r->header_only) {
1.38 ! www 546: &Apache::loncommon::content_type($r,'text/html');
1.1 www 547: $r->send_http_header;
548: return OK;
549: }
550:
1.10 matthew 551: if (&Apache::lonnet::allowed('ccc',$ENV{'request.role.domain'})) {
1.38 ! www 552: &Apache::loncommon::content_type($r,'text/html');
1.1 www 553: $r->send_http_header;
554:
1.2 www 555: if ($ENV{'form.phase'} eq 'two') {
1.10 matthew 556: &create_course($r);
1.2 www 557: } else {
1.10 matthew 558: &print_course_creation_page($r);
1.2 www 559: }
1.1 www 560: } else {
561: $ENV{'user.error.msg'}=
562: "/adm/createcourse:ccc:0:0:Cannot create courses";
563: return HTTP_NOT_ACCEPTABLE;
564: }
565: return OK;
566: }
567:
568: 1;
569: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>