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