[LON-CAPA-cvs] cvs: loncom /interface loncreatecourse.pm

raeburn raeburn@source.lon-capa.org
Mon, 28 Sep 2009 19:21:38 -0000


This is a MIME encoded message

--raeburn1254165698
Content-Type: text/plain

raeburn		Mon Sep 28 19:21:38 2009 EDT

  Modified files:              
    /loncom/interface	loncreatecourse.pm 
  Log:
  - Display information about courses created in a domain, filtered by creation date,
    and (optionally) by course type and creation context.
  
  
--raeburn1254165698
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20090928192138.txt"

Index: loncom/interface/loncreatecourse.pm
diff -u loncom/interface/loncreatecourse.pm:1.126 loncom/interface/loncreatecourse.pm:1.127
--- loncom/interface/loncreatecourse.pm:1.126	Thu Aug 27 00:06:18 2009
+++ loncom/interface/loncreatecourse.pm	Mon Sep 28 19:21:38 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Create a course
 #
-# $Id: loncreatecourse.pm,v 1.126 2009/08/27 00:06:18 raeburn Exp $
+# $Id: loncreatecourse.pm,v 1.127 2009/09/28 19:21:38 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -55,6 +55,20 @@
 
 =item process_batchfile()
 
+=item courserequestbrowser_javascript()
+
+=item print_creation_logs()
+
+=item creation_display_filter()
+
+=item course_types()
+
+=item context_names()
+
+=item instcode_srchstr()
+
+=item syllabuslink_javascript()
+
 =back
 
 =cut
@@ -766,7 +780,7 @@
 	&Apache::loncommon::construct_course($args,\$logmsg,\$courseid,
 					     \$crsudom,\$crsunum,
 					     $env{'user.domain'},
-					     $env{'user.name'});
+					     $env{'user.name'},'dc_create');
     $r->print($output);
     if ($success) {
         #
@@ -819,7 +833,12 @@
                    { internal_name => 'requestdisplay',
                      name => &mt('Approve or reject course requests'),
                      short_description =>
-    &mt('Display course creation requests submitted by authorized users held pending approval by a Domain Coordinator.'),
+    &mt('Display course creation requests submitted by authorized users, held pending approval by a Domain Coordinator.'),
+                   },
+                   { internal_name => 'creationlog',
+                     name => &mt('View course creation log'),
+                     short_description =>
+    &mt('Display information about when, how and by whom courses were created in this domain.'),
                    },
     );
     my $options;
@@ -850,7 +869,7 @@
     my $end_page =
         &Apache::loncommon::end_page();
     $r->print($start_page.$crumbs);
-    $r->print('<h3>'.&mt('Upload a courses or groups attributes file').'</h3>');
+    $r->print('<h3>'.&mt('Upload a courses or communities attributes file').'</h3>');
     $r->print('<form name="batchcreate" method="post" '.
                 'enctype="multipart/form-data" action="/adm/createcourse">'.
               '<input type="file" name="coursecreatorxml" />'.
@@ -921,6 +940,361 @@
 ENDREQBRW
 }
 
+sub print_creation_logs {
+    my ($r) = @_;
+    my $formname = 'creationlog';
+    my $dom = $env{'request.role.domain'};
+    $r->print('<form action="/adm/createcourse" method="post" name="'.$formname.'">');
+    # set defaults
+    my $now = time();
+    my $defstart = $now - (7*24*3600); #7 days ago
+    my %defaults = (
+                     page                => '1',
+                     show                => '10',
+                     type                => 'any',
+                     context             => 'any',
+                     created_before_date => $now,
+                     created_after_date  => $defstart,
+                   );
+    my ($contexts,$contextnames) = &context_names();
+    my $more_records = 0;
+    my %curr;
+    foreach my $item ('show','page','type','context') {
+        $curr{$item} = $env{'form.'.$item};
+    }
+    my $createdafter = &Apache::lonhtmlcommon::get_date_from_form('created_after_date');
+    my $createdbefore = &Apache::lonhtmlcommon::get_date_from_form('created_before_date');
+    $curr{'created_after_date'} = &Apache::lonhtmlcommon::get_date_from_form('created_after_date');
+    $curr{'created_before_date'} = &Apache::lonhtmlcommon::get_date_from_form('created_before_date');
+    foreach my $key (keys(%defaults)) {
+        if ($curr{$key} eq '') {
+            $curr{$key} = $defaults{$key};
+        }
+    }
+    my (%whodunit,$version);
+    ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
+    $r->print(&creation_display_filter($formname,\%curr,$version));
+    my $showntablehdr = 0;
+    my $tablehdr = &Apache::loncommon::start_data_table().
+                   &Apache::loncommon::start_data_table_header_row().
+                   '<th>&nbsp;</th><th>'.&mt('Creation Date').'</th>'.
+                   '<th>'.&mt('Creator').'</th><th>'.&mt('Description').'</th>'.
+                   '<th>'.&mt('Course Owner(s)').'</th>';
+    if (($curr{'type'} eq 'official') || ($curr{'type'} eq 'any')) {
+       $tablehdr .= '<th>'.&mt('Institutional Code').'</th>';
+    } 
+    $tablehdr .= '<th>'.&mt('Course Type').'</th>'.
+                 '<th>'.&mt('Creation Context').'</th>'.
+                 &Apache::loncommon::end_data_table_header_row();
+    my ($minshown,$maxshown);
+    $minshown = 1;
+    my $count = 0;
+    if ($curr{'show'} ne &mt('all')) {
+        $maxshown = $curr{'page'} * $curr{'show'};
+        if ($curr{'page'} > 1) {
+            $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
+        }
+    }
+    my $crstype = 'Course';
+    if ($curr{'type'} eq 'any') {
+        $crstype = '.';
+    } elsif ($curr{'type'} eq 'community') {
+        $crstype = 'Community';
+    }
+
+    my ($instcodefilter,$regexpok);
+    my (@codetitles,%cat_titles,%cat_order,%cat_items,$officialjs);
+    my ($jscript,$totcodes,$numtitles,$lasttitle) =
+        &Apache::courseclassifier::instcode_selectors_data($dom,$formname,
+                           \%cat_items,\@codetitles,\%cat_titles,\%cat_order,
+                           $officialjs);
+    if ($numtitles) {
+        if (($curr{'type'} eq 'official') || ($curr{'type'} eq 'unofficial')) {
+            $instcodefilter = &instcode_srchstr($dom,$numtitles);
+                &Apache::courseclassifier::instcode_search_str($dom,$numtitles);
+            if ($curr{'type'} eq 'official') {
+                $regexpok = 1;
+            } else {
+                unless ($instcodefilter eq '') {
+                    $regexpok = -1;
+                }
+            }
+        }
+    }
+    if ($instcodefilter eq '') { $instcodefilter = '.'; }
+
+    my $creationcontext = '.';
+    my $context_regexp = join('|',@{$contexts});
+    if ($curr{'context'} =~ /^($context_regexp)$/) {
+        $creationcontext = $curr{'context'};
+    } 
+    my %courses = 
+        &Apache::lonnet::courseiddump($dom,'.',1,$instcodefilter,'.','.',undef,undef,
+                                      $crstype,$regexpok,undef,undef,'1','creationlog',
+                                      undef,undef,undef,$curr{'created_before_date'},
+                                      $curr{'created_after_date'},$creationcontext);
+    foreach my $cid (sort { $courses{$b}{'created'}<=>$courses{$a}{'created'} } (keys(%courses))) {
+        $count ++;
+        next if ($count < $minshown);
+        if (!$showntablehdr) {
+            $r->print($tablehdr);
+            $showntablehdr = 1;
+        }
+        if ($courses{$cid}{'creator'} ne '') {
+            if ($whodunit{$courses{$cid}{'creator'}} eq '') {
+                my ($uname,$udom) = split(':',$courses{$cid}{'creator'});
+                $whodunit{$courses{$cid}{'creator'}} =
+                    &Apache::loncommon::plainname($uname,$udom);
+            }
+        }
+        my $description = $courses{$cid}{'description'};
+        my @owners;
+        my $singleowner = $courses{$cid}{'owner'};
+        push(@owners,$singleowner);
+        if (ref($courses{$cid}{'co-owners'}) eq 'ARRAY') {
+            foreach my $item (@{$courses{$cid}{'co-owners'}}) {
+                push(@owners,$item);
+            }
+        }
+        my %ownernames;
+        foreach my $owner (@owners) {
+            my ($ownername,$ownerdom);     
+            if ($owner =~ /:/) {
+                ($ownername,$ownerdom) = split(/:/,$owner);
+            } else {
+                $ownername = $owner;
+                if ($owner ne '') {
+                    $ownerdom = $dom;
+                }
+            }
+            if ($ownername ne '' && $ownerdom ne '') {
+                unless (ref($ownernames{$ownername.':'.$ownerdom}) eq 'HASH') { 
+                    my %namehash=&Apache::loncommon::getnames($ownername,$ownerdom);
+                    $ownernames{$ownername.':'.$ownerdom} = \%namehash;
+                }
+            }
+        }
+        my @lastnames;
+        foreach my $owner (keys(%ownernames)) {
+            if (ref($ownernames{$owner}) eq 'HASH') {
+                push(@lastnames,$ownernames{$owner}{'lastname'});
+            }
+        }
+        my $ownerlastnames = join(', ',sort(@lastnames));
+        my $showtype;
+        if ($courses{$cid}{type} eq 'Community') {
+            $showtype = &mt('community');
+        } else {
+            my $instcode = $courses{$cid}{inst_code};
+            if ($instcode ne '')  {
+                $showtype = &mt('official');
+            } else {
+                $showtype = &mt('unofficial');
+            }
+        }
+        my $showcontext;
+        
+        if ($courses{$cid}{context} =~ /^($context_regexp)$/) {
+            $showcontext = $contextnames->{$courses{$cid}{context}};
+        }
+        my $created = &Apache::lonlocal::locallocaltime($courses{$cid}{'created'});
+        my ($cdom,$cnum) = split('_',$cid);
+        $r->print(&Apache::loncommon::start_data_table_row()."\n".
+                  '<td>'.$count.'</td><td>'.$created.'</td>'."\n".
+                  '<td>'.$whodunit{$courses{$cid}{creator}}.'</td>'."\n".
+                  '<td>'.$description.'&nbsp;<a href="javascript:ToSyllabus('."'$cdom','$cnum'".')">'.&mt('Syllabus').'</a></td><td>'.$ownerlastnames.'</td>');
+        if (($curr{'type'} eq 'official') || ($curr{'type'} eq 'any')) {
+            $r->print('<td>'.$courses{$cid}{'inst_code'}.'</td>');
+        }
+        $r->print('<td>'.$showtype.'</td><td>'.$showcontext.'</td><td>'.&Apache::loncommon::end_data_table_row()."\n");
+    }
+    if ($showntablehdr) {
+        $r->print(&Apache::loncommon::end_data_table().'<br />');
+        if (($curr{'page'} > 1) || ($more_records)) {
+            $r->print('<table><tr>');
+            if ($curr{'page'} > 1) {
+                $r->print('<td><a href="javascript:chgPage('."'previous'".');">'.&mt('Previous [_1] changes',$curr{'show'}).'</a></td>');
+            }
+            if ($more_records) {
+                $r->print('<td><a href="javascript:chgPage('."'next'".');">'.&mt('Next [_1] changes',$curr{'show'}).'</a></td>');
+            }
+            $r->print('</tr></table>');
+            $r->print(<<"ENDSCRIPT");
+<script type="text/javascript">
+// <![CDATA[
+function chgPage(caller) {
+    if (caller == 'previous') {
+        document.$formname.page.value --;
+    }
+    if (caller == 'next') {
+        document.$formname.page.value ++;
+    }
+    document.$formname.submit();
+    return;
+}
+// ]]>
+</script>
+ENDSCRIPT
+        }
+    } else {
+        $r->print('<p class="LC_info">'.
+                  &mt('There are no records to display').
+                  '</p>');
+    }
+    $r->print('<input type="hidden" name="page" value="'.$curr{'page'}.'" />'.
+              '<input type="hidden" name="phase" value="creationlog" /></form>');
+    return;
+}
+
+sub creation_display_filter {
+    my ($formname,$curr,$version) = @_;
+    my $nolink = 1;
+    my ($contexts,$contextnames) = &context_names();
+    my $output = '<table><tr><td valign="top">'.
+                 '<span class="LC_nobreak"><b>'.&mt('Records/page:').'</b></span><br />'.
+                 &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
+                                              (&mt('all'),5,10,20,50,100,1000,10000)).
+                 '</td><td>&nbsp;&nbsp;</td>';
+    my $startform =
+        &Apache::lonhtmlcommon::date_setter($formname,'created_after_date',
+                                            $curr->{'created_after_date'},undef,
+                                            undef,undef,undef,undef,undef,undef,$nolink);
+    my $endform =
+        &Apache::lonhtmlcommon::date_setter($formname,'created_before_date',
+                                            $curr->{'created_before_date'},undef,
+                                            undef,undef,undef,undef,undef,undef,$nolink);
+    $output .= '<td valign="top"><b>'.&mt('Window during which course was created:').'</b><br />'.
+               '<table><tr><td>'.&mt('After:').
+               '</td><td>'.$startform.'</td></tr>'.
+               '<tr><td>'.&mt('Before:').'</td>'.
+               '<td>'.$endform.'</td></tr></table>'.
+               '</td>'.
+               '<td>&nbsp;&nbsp;</td>';
+    my ($types,$typenames) = &course_types();
+    if (ref($types) eq 'ARRAY') {
+        if (@{$types} > 1) {
+            $output .= '<td valign="top"><b>'.
+                       &mt('Course Type:').'</b><br /><select name="type">';
+            my $selstr = '';
+            if ($curr->{'type'} eq 'any') {
+                $selstr = ' selected="selected"';
+            }
+            $output .= '<option value="any"'.$selstr.'>'.&mt('Any').'</option>'."\n";
+            foreach my $type (@{$types}) {
+                my $selstr = '';
+                if ($curr->{'type'} eq $type) {
+                    $selstr = ' selected="selected"';
+                }
+                my $typename = $type;
+                if (ref($typenames) eq 'HASH') {
+                    if ($typenames->{$type} ne '') {
+                        $typename = $typenames->{$type};
+                    }
+                }
+                $output .= '<option value="'.$type.'"'.$selstr.'>'.$typename.'</option>'."\n";
+            }
+            $output .= '</select></td>';
+        }
+    }
+    $output .= '<td>&nbsp;&nbsp;</td>'.
+               '<td valign="top"><b>'.
+               &mt('Creation Context:').'</b><br /><select name="context">';
+    foreach my $context ('any',@{$contexts}) {
+        my $selstr = '';
+        if ($curr->{'context'} eq $context) {
+            $selstr = ' selected="selected"';
+        }
+        $output .= '<option value="'.$context.'"'.$selstr.'>';
+        if ($context eq 'any') {
+            $output .= &mt('Any');
+        } else {
+            $output .= $contextnames->{$context};
+        }
+        $output .= '</option>'."\n";
+    }
+    $output .= '</select></td>'.
+               '</tr></table>';
+
+    # Update Display button
+    $output .= '<p>'.
+               '<input type="submit" value="'.&mt('Update Display').'" />'.
+               '</p>';
+
+    # Server version info
+    $output .= '<p class="LC_info">'.
+               &mt('Only courses created from servers running LON-CAPA [_1] or later are displayed.','2.9.0');
+    if ($version) {
+        $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
+    }
+    $output .= '</p><hr />';
+    return $output;
+}
+
+sub course_types {
+    my @types = qw(official unofficial community);
+    my %typename = (
+                         official   => 'Official course',
+                         unofficial => 'Unofficial course',
+                         community  => 'Community',
+                   );
+    return (\@types,\%typename);
+}
+
+sub context_names {
+    my @contexts = qw(auto web dc_create requestcrs);
+    my %contextnames = 
+        &Apache::lonlocal::texthash (
+                            auto       => 'Automated creation from batch file',
+                            web        => 'Batch creation from uploaded file',
+                            dc_create  => 'Single course created by Domain Coordinator', 
+                            requestcrs => 'Processing of submitted course request',
+        );
+    return (\@contexts,\%contextnames);
+}
+
+sub instcode_srchstr {
+    my ($domain,$numtitles) = @_;
+    my $instcode;
+    if (defined($numtitles) && $numtitles == 0) {
+        $instcode = '.+';
+    } else {
+        my (%codedefaults,@code_order);
+        my $defaults_result =
+            &Apache::lonnet::auto_instcode_defaults($domain,\%codedefaults,
+                                                    \@code_order);
+        if ($defaults_result eq 'ok') {
+            $instcode ='^';
+            foreach my $item (@code_order) {
+                $instcode .= $codedefaults{$item};
+            }
+            $instcode .= '$';
+        } else {
+            $instcode = '.+';
+        }
+    }
+    return $instcode;
+}
+
+sub syllabuslink_javascript {
+    return <<"ENDJS";
+
+function ToSyllabus(cdom,cnum) {
+    if (cdom == '' || cdom == null) {
+        return;
+    }
+    if (cnum == '' || cnum == null) {
+        return;
+    }
+    var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
+    var url = "/public/"+cdom+"/"+cnum+"/syllabus";
+    syllwin = window.open(url,'',options,1);
+    syllwin.focus();
+    return;
+}
+
+ENDJS
+
+}
 
 # ===================================================================== Handler
 sub handler {
@@ -990,7 +1364,6 @@
                          'domain',$env{'request.role.domain'}).'</div>'.
                      &Apache::loncommon::end_page());
        } elsif ($env{'form.phase'} eq 'requestchange') {
-
            &Apache::lonhtmlcommon::add_breadcrumb
                  ({href=>"/adm/createcourse?phase=requestdisplay",
                    text=>&mt('[_1] Display Request Queue',),
@@ -1004,12 +1377,26 @@
                      &Apache::loncoursequeueadmin::update_request_queue(
                      'domain',$env{'request.role.domain'}).'</div>'.
                      &Apache::loncommon::end_page());
+       } elsif ($env{'form.phase'} eq 'creationlog') {
+           &Apache::lonhtmlcommon::add_breadcrumb
+                 ({href=>"/adm/createcourse?phase=creationlog",
+                   text=>&mt('[_1] Display Creation History',),});
+           my $js = '<script type="text/javascript">'."\n".
+                    '// <![CDATA['."\n".
+                    &syllabuslink_javascript()."\n".
+                    '// ]]>'."\n".
+                    '</script>';
+           my $start_page=&Apache::loncommon::start_page('Course Creation Logs',$js);
+           my $crumbs = &Apache::lonhtmlcommon::breadcrumbs('Created Courses','Creation_Log',undef,'Creation_Log');
+           $r->print($start_page.$crumbs."\n".'<div>');
+           &print_creation_logs($r);
+           $r->print('</div>'.&Apache::loncommon::end_page());
        } else {
            &print_intro_page($r);
        }
    } else {
       $env{'user.error.msg'}=
-        "/adm/createcourse:ccc:0:0:Cannot create courses or groups";
+        "/adm/createcourse:ccc:0:0:Cannot create courses or communities";
       return HTTP_NOT_ACCEPTABLE; 
    }
    return OK;

--raeburn1254165698--