Annotation of loncom/interface/loncreatecourse.pm, revision 1.33

1.1       www         1: # The LearningOnline Network
                      2: # Create a course
1.5       albertel    3: #
1.33    ! www         4: # $Id: loncreatecourse.pm,v 1.32 2003/09/02 19:47:47 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: 
                    101: # ============================================================= Copy a userfile
                    102: 
                    103: sub copyfile {
                    104:     my ($origcrsid,$newcrsid,$which)=@_;
                    105:     return &writefile($newcrsid,$which,&readfile($origcrsid,$which));
1.30      www       106: }
                    107: 
                    108: # =============================================================== Copy a dbfile
                    109: 
                    110: sub copydb {
                    111:     my ($origcrsid,$newcrsid,$which)=@_;
                    112:     $which=~s/\.db$//;
                    113:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
                    114:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
                    115:     my %data=&Apache::lonnet::dump
                    116: 	($which,$origcrsdata{'domain'},$origcrsdata{'num'});
                    117:     return &Apache::lonnet::put
                    118: 	($which,\%data,$newcrsdata{'domain'},$newcrsdata{'num'});
                    119: }
                    120: 
                    121: # ========================================================== Copy all userfiles
                    122: 
                    123: sub copyuserfiles {
                    124:     my ($origcrsid,$newcrsid)=@_;
                    125:     foreach (&crsdirlist($origcrsid,'userfiles')) {
                    126: 	&copyfile($origcrsid,$newcrsid,$_);
                    127:     }
                    128: }
                    129: # ========================================================== Copy all userfiles
                    130: 
                    131: sub copydbfiles {
                    132:     my ($origcrsid,$newcrsid)=@_;
                    133:     foreach (&crsdirlist($origcrsid)) {
                    134: 	if ($_=~/\.db$/) {
                    135: 	    unless 
                    136:              ($_=~/^(nohist\_|discussiontimes|classlist|versionupdate)/) {
                    137: 		 &copydb($origcrsid,$newcrsid,$_);
                    138: 	     }
                    139: 	}
                    140:     }
1.31      www       141: }
                    142: 
                    143: # ======================================================= Copy all course files
                    144: 
                    145: sub copycoursefiles {
                    146:     my ($origcrsid,$newcrsid)=@_;
                    147:     &copyuserfiles($origcrsid,$newcrsid);
                    148:     &copydbfiles($origcrsid,$newcrsid);
1.28      www       149: }
1.13      www       150: 
1.2       www       151: # ===================================================== Phase one: fill-in form
                    152: 
1.10      matthew   153: sub print_course_creation_page {
1.2       www       154:     my $r=shift;
1.10      matthew   155:     my $defdom=$ENV{'request.role.domain'};
                    156:     my %host_servers = &Apache::loncommon::get_library_servers($defdom);
                    157:     my $course_home = '<select name="course_home" size="1">'."\n";
                    158:     foreach my $server (sort(keys(%host_servers))) {
1.14      matthew   159:         $course_home .= qq{<option value="$server"};
                    160:         if ($server eq $Apache::lonnet::perlvar{'lonHostID'}) {
                    161:             $course_home .= " selected ";
                    162:         }
                    163:         $course_home .= qq{>$server $host_servers{$server}</option>};
1.10      matthew   164:     }
                    165:     $course_home .= "\n</select>\n";
1.9       matthew   166:     my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
1.12      www       167:     my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
1.17      www       168:     my $helplink=&Apache::loncommon::help_open_topic('Create_Course','Help on Creating Courses');
1.32      www       169:     my $cloneform=&Apache::loncommon::select_dom_form
                    170: 	($ENV{'request.role.domain'},'clonedomain').
                    171: 		     &Apache::loncommon::selectcourse_link
                    172: 		     ('ccrs','clonecourse','clonedomain');
                    173:     my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript();
1.2       www       174:     $r->print(<<ENDDOCUMENT);
                    175: <html>
1.6       matthew   176: <script language="JavaScript" type="text/javascript">
                    177: var editbrowser = null;
                    178: function openbrowser(formname,elementname) {
                    179:     var url = '/res/?';
                    180:     if (editbrowser == null) {
                    181:         url += 'launch=1&';
                    182:     }
                    183:     url += 'catalogmode=interactive&';
                    184:     url += 'mode=edit&';
                    185:     url += 'form=' + formname + '&';
1.7       matthew   186:     url += 'element=' + elementname + '&';
                    187:     url += 'only=sequence' + '';
1.6       matthew   188:     var title = 'Browser';
                    189:     var options = 'scrollbars=1,resizable=1,menubar=0';
                    190:     options += ',width=700,height=600';
                    191:     editbrowser = open(url,title,options,'1');
                    192:     editbrowser.focus();
                    193: }
                    194: </script>
1.32      www       195: $coursebrowserjs
1.2       www       196: <head>
                    197: <title>The LearningOnline Network with CAPA</title>
                    198: </head>
1.12      www       199: $bodytag
1.17      www       200: $helplink
1.6       matthew   201: <form action="/adm/createcourse" method="post" name="ccrs">
1.10      matthew   202: <h2>Course Information</h2>
                    203: <p>
                    204: <b>Course Title:</b>
1.6       matthew   205: <input type="text" size="50" name="title">
1.10      matthew   206: </p><p>
1.13      www       207: <b>Course Home Server:</b>$course_home
                    208: </p><p>
                    209: <b>Course ID/Number (optional)</b>
                    210: <input type="text" size="30" name="crsid">
                    211: </p>
                    212: <h2>Course Content</h2>
1.32      www       213: <table border="2">
                    214: <tr><th>Completely new course</th><th>Clone an existing course</th></tr>
                    215: <tr><td>
1.13      www       216: <p>
1.11      www       217: <b>Map:</b>
1.6       matthew   218: <input type="text" size="50" name="topmap">
1.24      www       219: <a href="javascript:openbrowser('ccrs','topmap')">Select Map</a>
1.10      matthew   220: </p><p>
1.32      www       221: <b>Do NOT generate as standard course</b><br /> 
1.11      www       222: (only check if you know what you are doing):
                    223: <input type="checkbox" name="nonstandard">
1.13      www       224: </p>
                    225: <p>
1.32      www       226: <b>First Resource</b><br />(standard courses only):
1.17      www       227: <input type="radio" name="firstres" value="blank">Blank
1.13      www       228: &nbsp;
1.17      www       229: <input type="radio" name="firstres" value="syl" checked>Syllabus
1.13      www       230: &nbsp;
                    231: <input type="radio" name="firstres" value="nav">Navigate
                    232: </p>
1.32      www       233: </td><td>
                    234: Course ID: <input input type="text" size="25" name="clonecourse" value="" />
                    235: <br />
                    236: Domain: 
                    237: $cloneform<br />&nbsp;<br />
                    238: Additional settings, if specified below, will override cloned settings.
                    239: </td></tr>
                    240: </table>
1.13      www       241: <h2>Assessment Parameters</h2>
                    242: <p>
1.11      www       243: <b>Open all assessments: </b>
                    244: <input type="checkbox" name="openall" checked>
1.13      www       245: </p>
                    246: <h2>Messaging</h2>
                    247: <p>
1.11      www       248: <b>Set course policy feedback to Course Coordinator: </b>
                    249: <input type="checkbox" name="setpolicy" checked>
                    250: </p><p>
                    251: <b>Set content feedback to Course Coordinator: </b>
                    252: <input type="checkbox" name="setcontent" checked>
                    253: </p>
1.16      www       254: <h2>Communication</h2>
                    255: <p>
                    256: <b>Disable student resource discussion: </b>
1.26      matthew   257: <input type="checkbox" name="disresdis" /> <br />
                    258: <b>Disable student use of chatrooms: </b>
                    259: <input type="checkbox" name="disablechat" />
1.16      www       260: </p>
1.18      www       261: <h2>Access Control</h2>
                    262: <p>
                    263: <b>Students need access key to enter course: </b>
                    264: <input type="checkbox" name="setkeys" />
                    265: </p>
1.10      matthew   266: <h2>Course Coordinator</h2>
                    267: <p>
1.11      www       268: <b>Username:</b> <input type="text" size="15" name="ccuname" />
                    269: </p><p>
                    270: <b>Domain:</b> $domform
1.10      matthew   271: </p><p>
1.11      www       272: <b>Immediately expire own role as Course Coordinator:</b>
                    273: <input type="checkbox" name="expireown" checked>
1.10      matthew   274: </p><p>
                    275: <input type="hidden" name="phase" value="two" />
1.6       matthew   276: <input type="submit" value="Open Course">
1.10      matthew   277: </p>
1.2       www       278: </form>
                    279: </body>
                    280: </html>
                    281: ENDDOCUMENT
                    282: }
                    283: 
                    284: # ====================================================== Phase two: make course
                    285: 
1.10      matthew   286: sub create_course {
1.2       www       287:     my $r=shift;
                    288:     my $topurl='/res/'.&Apache::lonnet::declutter($ENV{'form.topmap'});
                    289:     my $ccuname=$ENV{'form.ccuname'};
                    290:     my $ccdomain=$ENV{'form.ccdomain'};
                    291:     $ccuname=~s/\W//g;
                    292:     $ccdomain=~s/\W//g;
                    293:     my $cdescr=$ENV{'form.title'};
                    294:     my $curl=$ENV{'form.topmap'};
1.12      www       295:     my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
1.2       www       296:     $r->print(<<ENDENHEAD);
                    297: <html>
                    298: <head>
                    299: <title>The LearningOnline Network with CAPA</title>
                    300: </head>
1.12      www       301: $bodytag
1.2       www       302: ENDENHEAD
1.10      matthew   303:     #
                    304:     # Verify data
                    305:     #
                    306:     # Check the veracity of the course coordinator
1.2       www       307:     if (&Apache::lonnet::homeserver($ccuname,$ccdomain) eq 'no_host') {
1.3       www       308:         $r->print('No such user '.$ccuname.' at '.$ccdomain.'</body></html>');
1.2       www       309: 	return;
                    310:     }
1.10      matthew   311:     # Check the proposed home server for the course
                    312:     my %host_servers = &Apache::loncommon::get_library_servers
                    313:         ($ENV{'request.role.domain'});
                    314:     if (! exists($host_servers{$ENV{'form.course_home'}})) {
                    315:         $r->print('Invalid home server for course: '.
                    316:                   $ENV{'form.course_home'}.'</body></html>');
                    317:         return;
                    318:     }
1.2       www       319: #
                    320: # Open course
                    321: #
1.32      www       322:     my %cenv=();
1.10      matthew   323:     my $courseid=&Apache::lonnet::createcourse($ENV{'request.role.domain'},
                    324:                                                $cdescr,$curl,
1.11      www       325:                                                $ENV{'form.course_home'},
                    326:                                                $ENV{'form.nonstandard'});
1.2       www       327: 
1.27      bowersj2  328:     # Note: The testing routines depend on this being output; see 
                    329:     # Utils::Course. This needs to at least be output as a comment
                    330:     # if anyone ever decides to not show this, and Utils::Course::new
                    331:     # will need to be suitably modified.
1.4       www       332:     $r->print('New LON-CAPA Course ID: '.$courseid.'<br>');
                    333: #
1.12      www       334: # Check if created correctly
1.4       www       335: #
                    336:     my ($crsudom,$crsunum)=($courseid=~/^\/(\w+)\/(\w+)$/);
                    337:     my $crsuhome=&Apache::lonnet::homeserver($crsunum,$crsudom);
                    338:     $r->print('Created on: '.$crsuhome.'<br>');
1.12      www       339: #
1.32      www       340: # Are we cloning?
                    341: #
                    342:     my $cloneid='';
                    343:     if (($ENV{'form.clonecourse'}) && ($ENV{'form.clonedomain'})) {
                    344: 	$cloneid='/'.$ENV{'form.clonedomain'}.'/'.$ENV{'form.clonecourse'};
                    345:         my ($clonecrsudom,$clonecrsunum)=($cloneid=~/^\/(\w+)\/(\w+)$/);
                    346: 	my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
                    347: 	if ($clonehome eq 'no_host') {
                    348: 	    $r->print(
                    349:     '<br /><font color="red">Attempting to clone non-existing course '.$cloneid.'</font>');
                    350: 	} else {
                    351: 	    $r->print(
                    352:     '<br /><font color="green">Cloning course from '.$clonehome.'</font>');
                    353: # Copy all files
                    354: 	    &copycoursefiles($cloneid,$courseid);
                    355: # Restore title
                    356: 	    $cenv{'description'}=$cdescr;
                    357: 	}
                    358:     }
                    359: #
                    360: # Set environment (will override cloned, if existing)
1.12      www       361: #
1.4       www       362:     if ($ENV{'form.crsid'}) {
1.12      www       363:         $cenv{'courseid'}=$ENV{'form.crsid'};
                    364:     }
                    365:     if (($ccdomain) && ($ccuname)) {
                    366:        if ($ENV{'form.setpolicy'}) {
                    367:            $cenv{'policy.email'}=$ccuname.':'.$ccdomain;
                    368:        }
                    369:        if ($ENV{'form.setcontent'}) {
1.18      www       370:            $cenv{'question.email'}=$ccuname.':'.$ccdomain;
                    371:        }
                    372:     }
                    373:     if ($ENV{'form.setkeys'}) {
                    374:        $cenv{'keyaccess'}='yes';
1.16      www       375:     }
                    376:     if ($ENV{'form.disresdis'}) {
                    377:         $cenv{'pch.roles.denied'}='st';
1.26      matthew   378:     }
                    379:     if ($ENV{'form.disablechat'}) {
                    380:         $cenv{'plc.roles.denied'}='st';
1.21      albertel  381:     }
1.23      bowersj2  382: 
1.32      www       383:     # Record we've not yet viewed the Course Initialization Helper for this 
                    384:     # course
1.23      bowersj2  385:     $cenv{'course.helper.not.run'} = 1;
1.21      albertel  386:     #
                    387:     # Use new Randomseed
                    388:     #
1.22      albertel  389:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
1.25      matthew   390:     #
                    391:     # By default, use standard grading
                    392:     $cenv{'grading'} = 'standard';
1.22      albertel  393: 
1.32      www       394:     $r->print('<br />Setting environment: '.                 
1.12      www       395:           &Apache::lonnet::put('environment',\%cenv,$crsudom,$crsunum).'<br>');
                    396: #
                    397: # Open all assignments
                    398: #
                    399:     if ($ENV{'form.openall'}) {
                    400:        my $storeunder=$crsudom.'_'.$crsunum.'.0.opendate';
1.33    ! www       401:        my %storecontent = ($storeunder         => time,
        !           402:                            $storeunder.'.type' => 'date_start');
1.12      www       403:        
                    404:        $r->print('Opening all assignments: '.&Apache::lonnet::cput
                    405:                  ('resourcedata',\%storecontent,$crsudom,$crsunum).'<br>');
                    406:    }
1.13      www       407: #
                    408: # Set first page
                    409: #
                    410:     unless (($ENV{'form.nonstandard'}) || ($ENV{'form.firstres'} eq 'blank')) {
                    411: 	$r->print('Setting first resource: ');
                    412:         my ($errtext,$fatal)=
                    413:            &Apache::londocs::mapread($crsunum,$crsudom,'default.sequence');
                    414:         $r->print(($fatal?$errtext:'read ok').' - ');
                    415:         my $title; my $url;
                    416:         if ($ENV{'form.firstres'} eq 'syl') {
                    417: 	    $title='Syllabus';
                    418:             $url='/public/'.$crsudom.'/'.$crsunum.'/syllabus';
                    419:         } else {
                    420:             $title='Navigate Contents';
                    421:             $url='/adm/navmaps';
                    422:         }
                    423:         $Apache::lonratedt::resources[1]=$title.':'.$url.':false:start:res';
1.15      albertel  424:         ($errtext,$fatal)=
1.13      www       425:            &Apache::londocs::storemap($crsunum,$crsudom,'default.sequence');
                    426:         $r->print(($fatal?$errtext:'write ok').'<br>');
                    427:   }
1.2       www       428: #
                    429: # Make current user course adminstrator
                    430: #
1.12      www       431:     my $end=undef;
                    432:     my $addition='';
                    433:     if ($ENV{'form.expireown'}) { $end=time+5; $addition='expired'; }
                    434:     $r->print('Assigning '.$addition.' role of course coordinator to self: '.
1.2       www       435:     &Apache::lonnet::assignrole(
1.12      www       436:      $ENV{'user.domain'},$ENV{'user.name'},$courseid,'cc',$end).'<br>');
1.2       www       437: #
                    438: # Make additional user course administrator
                    439: #
1.12      www       440:    if (($ccdomain) && ($ccuname)) {
1.2       www       441:     $r->print('Assigning role of course coordinator to '.
                    442:                $ccuname.' at '.$ccdomain.': '.
1.3       www       443:     &Apache::lonnet::assignrole($ccdomain,$ccuname,$courseid,'cc').'<p>');
1.12      www       444:    }
1.20      www       445:     if ($ENV{'form.setkeys'}) {
                    446: 	$r->print(
                    447:  '<p><a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">Manage Access Keys</a></p>');
                    448:     }
                    449:     $r->print('<p>Roles will be active at next login.</p></body></html>');
1.2       www       450: }
                    451: 
                    452: # ===================================================================== Handler
1.1       www       453: sub handler {
                    454:     my $r = shift;
                    455: 
                    456:     if ($r->header_only) {
                    457:        $r->content_type('text/html');
                    458:        $r->send_http_header;
                    459:        return OK;
                    460:     }
                    461: 
1.10      matthew   462:     if (&Apache::lonnet::allowed('ccc',$ENV{'request.role.domain'})) {
1.1       www       463:        $r->content_type('text/html');
                    464:        $r->send_http_header;
                    465: 
1.2       www       466:        if ($ENV{'form.phase'} eq 'two') {
1.10      matthew   467:            &create_course($r);
1.2       www       468:        } else {
1.10      matthew   469: 	   &print_course_creation_page($r);
1.2       www       470:        }
1.1       www       471:    } else {
                    472:       $ENV{'user.error.msg'}=
                    473:         "/adm/createcourse:ccc:0:0:Cannot create courses";
                    474:       return HTTP_NOT_ACCEPTABLE; 
                    475:    }
                    476:    return OK;
                    477: } 
                    478: 
                    479: 1;
                    480: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>