[LON-CAPA-cvs] cvs: loncom /homework lonhomework.pm

albertel lon-capa-cvs-allow@mail.lon-capa.org
Sat, 03 Nov 2007 00:52:15 -0000


albertel		Fri Nov  2 20:52:15 2007 EDT

  Modified files:              
    /loncom/homework	lonhomework.pm 
  Log:
  - BUG#5511 - make all of the .problem templates available to those creating
               .survey or .exam resources
  - BUG#5485 - make at least the titles of the templates translatable
  
  
Index: loncom/homework/lonhomework.pm
diff -u loncom/homework/lonhomework.pm:1.281 loncom/homework/lonhomework.pm:1.282
--- loncom/homework/lonhomework.pm:1.281	Wed Oct 31 13:40:00 2007
+++ loncom/homework/lonhomework.pm	Fri Nov  2 20:52:06 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Homework handler
 #
-# $Id: lonhomework.pm,v 1.281 2007/10/31 17:40:00 albertel Exp $
+# $Id: lonhomework.pm,v 1.282 2007/11/03 00:52:06 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -49,11 +49,11 @@
 use Apache::chemresponse();
 use Apache::drawimage();
 use Apache::Constants qw(:common);
-use HTML::Entities();
 use Apache::loncommon();
 use Apache::lonlocal;
 use Time::HiRes qw( gettimeofday tv_interval );
-use Apache::lonnet();
+use HTML::Entities();
+use File::Copy();
 
 # FIXME - improve commenting
 
@@ -1059,52 +1059,49 @@
     undef($Apache::lonhomework::parsing_a_task);
 }
 
-# with no arg it returns a HTML <option> list of the template titles
-# with one arg it returns the filename associated with the arg passed
 sub get_template_list {
-    my ($namewanted,$extension) = @_;
+    my ($extension) = @_;
     my $result;
     my @allnames;
     &Apache::lonxml::debug("Looking for :$extension:");
-    foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
-	&Apache::lonxml::debug("Looking at $file");
-	my $name=&Apache::lonnet::metadata($file,'title');
-	&Apache::lonxml::debug("Got a name $name");
-	if ($namewanted && ($name eq $namewanted)) {
-	    $result=$file;
-	    last;
-	} else {
-	    if ($name) { push (@allnames, $name); }
-	}
-    }
-    if (@allnames && !$result) {
-	$result="<option>".&mt("Select a")." $extension ".&mt('template')."</option>\n<option>".
-	    join('</option><option>',sort(@allnames)).'</option>';
+    my $glob_extension  = $extension;
+    if ($extension eq 'survey' || $extension eq 'exam') {
+	$glob_extension = 'problem';
+    }
+    my @files = glob($Apache::lonnet::perlvar{'lonIncludes'}.
+		     '/templates/*.'.$glob_extension);
+    @files = map {[$_,&mt(&Apache::lonnet::metadata($_, 'title'))]} (@files);
+    @files = sort {$a->[1] cmp $b->[1]} (@files);
+    foreach my $file (@files) {
+	next if ($file->[1] !~ /\S/);
+	$result .=
+	    '<label><input type="radio" name="template" value="'.$file->[0].'" />'.
+	    $file->[1].'</label><br />';
     }
     return $result;
 }
 
 sub newproblem {
     my ($request) = @_;
-    my $extension=$request->uri;
-    $extension=~s:^.*\.([\w]+)$:$1:;
-    &Apache::lonxml::debug("Looking for :$extension:");
-    my $templatelist=&get_template_list('',$extension);
-    if ($env{'form.template'} &&
-	$env{'form.template'} ne "Select a $extension template") {
-	use File::Copy;
-	my $file = &get_template_list($env{'form.template'},$extension);
+
+    if ($env{'form.template'}) {
+	my $file = $env{'form.template'};
 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
-	copy($file,$dest);
+	&File::Copy::copy($file,$dest);
 	&renderpage($request,$dest);
-    } elsif($env{'form.newfile'} && !$templatelist) {
-	# I don't like hard-coded filenames but for now, this will work.
-	use File::Copy;
+	return;
+    }
+
+    my ($extension) = ($request->uri =~ m/\.(\w+)$/);
+    &Apache::lonxml::debug("Looking for :$extension:");
+    my $templatelist=&get_template_list($extension);
+    if ($env{'form.newfile'} && !$templatelist) {
+	# no templates found
 	my $templatefilename =
 	    $request->dir_config('lonIncludes').'/templates/blank.'.$extension;
 	&Apache::lonxml::debug("$templatefilename");
 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
-	copy($templatefilename,$dest);
+	&File::Copy::copy($templatefilename,$dest);
 	&renderpage($request,$dest);
     } else {
 	my $url=&HTML::Entities::encode($request->uri,'<>&"');
@@ -1112,9 +1109,6 @@
 	$shownurl=~s-^/~-/priv/-;
 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
 	my $errormsg;
-	if ($env{'form.newfile'}) {
-	    $errormsg='<p><span class="LC_error">'.&mt('You did not select a template.').'</span></p>'."\n";
-	}
 	my $instructions;
 	my $start_page = 
 	    &Apache::loncommon::start_page("Create New $extension");
@@ -1127,16 +1121,17 @@
 <p>
 <b>
 ".&mt("To create a new $extension, select a template from the".
-      "pull-down menu below. Then click on the \"Create $extension\" button.")."</b>
+      " list below. Then click on the \"Create $extension\" button.")."</b>
 </p><form action=\"$url\" method=\"POST\">");
 
 	if (defined($templatelist)) {
-	    $request->print("<select name=\"template\">$templatelist</select>");
+	    $request->print($templatelist);
 	}
-	$request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"".&mt('Create')." $extension\" />");
+	$request->print('<br /><input type="submit" name="newfile" value="'.
+			&mt("Create $extension").'" />');
 	$request->print("</form></p>".&Apache::loncommon::end_page());
     }
-    return '';
+    return;
 }
 
 sub update_construct_style {