--- loncom/interface/lonrequestcourse.pm	2017/08/11 15:26:38	1.102
+++ loncom/interface/lonrequestcourse.pm	2018/03/23 01:01:21	1.103
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Request a course
 #
-# $Id: lonrequestcourse.pm,v 1.102 2017/08/11 15:26:38 raeburn Exp $
+# $Id: lonrequestcourse.pm,v 1.103 2018/03/23 01:01:21 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -155,6 +155,23 @@ sub handler {
     }
 
     if ($canreq) {
+        if (($env{'form.crstype'} eq 'lti') && ($env{'request.lti.login'}) &&
+            ($env{'form.lti.reqrole'} eq 'cc') && ($env{'form.lti.reqcrs'}) &&
+            ($env{'form.lti.sourcecrs'} ne '')) {
+            if ($action eq 'process') {
+                if ($can_request{'lti'}) {
+                    my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
+                    &process_textbook_request($r,$dom,$action,\%domdefs,\%domconfig,\%can_request,'lti');
+                } else {
+                    $r->print(&header('Course Request','','','',{ 'only_body' => 1}).
+                              '<div>'.
+                              '<p class="LC_info">'.&mt('You do not have privileges to request creation of LTI courses.').'</p>'.
+                              '</div>'.
+                              &Apache::loncommon::end_page());
+                }
+            }
+            return OK;
+        }
         if (($env{'form.crstype'} eq 'textbook') || 
             (scalar(keys(%can_request)) == 1) && ($can_request{'textbook'})) {
             my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
@@ -192,7 +209,8 @@ sub handler {
                 }
             } else {
                 if ($can_request{'textbook'}) {
-                    &print_textbook_form($r,$dom,\@incdoms,\%domdefs,$domconfig{'requestcourses'},\%can_request);
+                    &print_textbook_form($r,$dom,\@incdoms,\%domdefs,$domconfig{'requestcourses'},
+                                         \%can_request,'textbook');
                 } else {
                     &textbook_request_disabled($r,$dom,$action,\%can_request);
                 }
@@ -4541,9 +4559,11 @@ sub generate_date_items {
 }
 
 sub print_textbook_form {
-    my ($r,$dom,$incdoms,$domdefs,$settings,$can_request) = @_;
+    my ($r,$dom,$incdoms,$domdefs,$settings,$can_request,$crstype,$formhash) = @_;
     my (%prefab,%ordered,%numprefab);
-    my $crstype = 'textbook';
+    if ($crstype eq '') {
+        $crstype = 'textbook';
+    }
 #
 #  Retrieve list of prefabricated courses (textbook courses and templates) cloneable by user
 #
@@ -4660,31 +4680,37 @@ sub print_textbook_form {
 
     my $jscript = &textbook_request_javascript(\%numprefab,$numcurrent,$numdomcourses,$customvalidationjs);
     $jscript .= $customjs;
-    my %loaditems;
+    my (%loaditems,$args);
     $loaditems{'onload'} = 'javascript:uncheckAllRadio();'.$customonload;
-    $r->print(&header('Course Request',$jscript,\%loaditems));
+    if ($crstype eq 'lti') {
+       $args = { 'only_body' => 1};
+    }
+    $r->print(&header('Course Request',$jscript,\%loaditems,undef,$args));
 
     if (ref($can_request) eq 'HASH') {
-        unless ((scalar(keys(%{$can_request})) == 1) && ($can_request->{'textbook'})) {
+        unless (((scalar(keys(%{$can_request})) == 1) && ($can_request->{'textbook'})) ||
+                ($crstype eq 'lti')) {
             &Apache::lonhtmlcommon::add_breadcrumb(
                 { href => '/adm/requestcourse',
                   text => 'Pick action',
                 });
         }
     }
-    &Apache::lonhtmlcommon::add_breadcrumb({text=>'Course Request'});
-    $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests'));
+    unless ($crstype eq 'lti') {
+        &Apache::lonhtmlcommon::add_breadcrumb({text=>'Course Request'});
+        $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests'));
 
-    &startContentScreen($r,'textbookrequests');
+        &startContentScreen($r,'textbookrequests');
 #
 # Show domain selector form, if required.
 #
-    if (@{$incdoms} > 1) {
-        my $onchange = 'this.form.submit()';
-        $r->print('<form name="domforcourse" method="post" action="/adm/requestcourse">'.
-                  '<div><fieldset><legend>'.&mt('Domain').'</legend>'.
-                  &Apache::loncommon::select_dom_form($dom,'showdom','',1,$onchange,$incdoms).
-                  '</fieldset></form>');
+        if (@{$incdoms} > 1) {
+            my $onchange = 'this.form.submit()';
+            $r->print('<form name="domforcourse" method="post" action="/adm/requestcourse">'.
+                      '<div><fieldset><legend>'.&mt('Domain').'</legend>'.
+                      &Apache::loncommon::select_dom_form($dom,'showdom','',1,$onchange,$incdoms).
+                      '</fieldset></form>');
+        }
     }
 
 #
@@ -4864,15 +4890,24 @@ sub print_textbook_form {
 #
 # Submit button
 #
-    $r->print('<input type="hidden" name="crstype" value="textbook" />'.
+    $r->print('<input type="hidden" name="crstype" value="'.$crstype.'" />'.
               '<input type="hidden" name="action" value="process" />'.
               '<input type="submit" value="'.&mt('Create course').'" />');
 
 #
 # End request form
 #
+
+    if (($crstype eq 'lti') && (ref($formhash) eq 'HASH')) {
+        foreach my $item (keys(%{$formhash})) {
+            $r->print('<input type="hidden" name="'.$item.'" value="'.$formhash->{$item}.'" />'."\n");
+        }
+    }
+
     $r->print('</form>');
-    &endContentScreen($r).
+    unless ($crstype eq 'lti') {
+        &endContentScreen($r);
+    }
     $r->print(&Apache::loncommon::end_page());
     return;
 }
@@ -4959,9 +4994,11 @@ sub clone_selection_table {
 }
 
 sub process_textbook_request {
-    my ($r,$dom,$action,$domdefs,$domconfig,$can_request) = @_;
+    my ($r,$dom,$action,$domdefs,$domconfig,$can_request,$crstype) = @_;
     my ($uniquecode,$req_notifylist);
-    my $crstype = 'textbook';
+    if ($crstype eq '') {
+        $crstype = 'textbook';
+    }
     if (ref($domconfig) eq 'HASH') {
         if (ref($domconfig->{'requestcourses'}) eq 'HASH') {
             if (ref($domconfig->{'requestcourses'}{'notify'}) eq 'HASH') {
@@ -5003,27 +5040,33 @@ sub process_textbook_request {
         }
     }
     my $js = &processing_javascript();
-    my $loaditems = { 
-                      onload => 'javascript:hideProcessing();',
-                    };
-    $r->print(&header('Course Creation',$js,$loaditems));
-
-    if (ref($can_request) eq 'HASH') {
-        unless ((scalar(keys(%{$can_request})) == 1) && ($can_request->{'textbook'})) {
-            &Apache::lonhtmlcommon::add_breadcrumb(
-                { href => '/adm/requestcourse',
-                  text => 'Pick action',
-                });
+    my ($loaditems,$args);
+    $loaditems = {
+                   onload => 'javascript:hideProcessing();',
+                 };
+    if ($crstype eq 'lti') {
+       $args = { 'only_body' => 1};
+    }
+    $r->print(&header('Course Creation',$js,$loaditems,undef,$args));
+
+    unless ($crstype eq 'lti') {
+        if (ref($can_request) eq 'HASH') {
+            unless ((scalar(keys(%{$can_request})) == 1) && ($can_request->{'textbook'})) {
+                &Apache::lonhtmlcommon::add_breadcrumb(
+                    { href => '/adm/requestcourse',
+                      text => 'Pick action',
+                    });
+            }
         }
+        &Apache::lonhtmlcommon::add_breadcrumb(
+                                               { href => '/adm/requestcourse',
+                                                 text => "Create Course",
+                                               }
+                                              );
+        &Apache::lonhtmlcommon::add_breadcrumb({text=>'Request Processed'});
+        $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests'));
+        &startContentScreen($r,'textbookrequests');
     }
-    &Apache::lonhtmlcommon::add_breadcrumb(
-                                           { href => '/adm/requestcourse',
-                                             text => "Create Course",
-                                           }
-                                          );
-    &Apache::lonhtmlcommon::add_breadcrumb({text=>'Request Processed'});
-    $r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Requests','Course_Requests'));
-    &startContentScreen($r,'textbookrequests');
 
     my $details = {
                     owner          => $env{'user.name'},
@@ -5068,7 +5111,12 @@ sub process_textbook_request {
     my ($result,$output,$customized) = &process_request($r,$lonhost,$dom,$cnum,$crstype,$now,$details,
                                                         '',$req_notifylist,[],$domconfig);
     $r->print($output);
-    if (&Apache::loncoursequeueadmin::author_prompt()) {
+    if ($crstype eq 'lti') {
+        my %consumers = &Apache::lonnet::get_dom('lticonsumers',[$env{'form.sourcecrs'}],$dom);
+        if (($env{'form.lti.sourcecrs'} ne '')  && ($consumers{$env{'form.lti.sourcecrs'}} eq '') && ($cnum ne '')) {
+            &Apache::lonnet::put_dom('lticonsumers',{ $env{'form.lti.sourcecrs'} => $cnum },$dom);
+        }
+    } elsif (&Apache::loncoursequeueadmin::author_prompt()) {
         unless ($customized) {
             &print_author_prompt($r,$action,$cnum,$dom,$crstype,$result);
         }
@@ -5077,7 +5125,9 @@ sub process_textbook_request {
             $r->print('<p><a href="/adm/requestcourse">'.&mt('Create another course').'</a></p>');
         }
     }
-    &endContentScreen($r);
+    unless ($crstype eq 'lti') {
+        &endContentScreen($r);
+    }
     $r->print(&Apache::loncommon::end_page());
 }