WeBWorK Main Forum

Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Paul Seeburger -
Number of replies: 11
Hi!

I am having trouble getting the PDF Hardcopy feature to work for any of my homework sets in one of my courses.

It appears to only be a problem when I have inserted the following into my PGcourse.pl in my macros folder.

loadMacros("problemRandomize.pl"); # Allow for the user to try another version of the problem once they get it correct. ProblemRandomize(when=>"Correct",onlyAfterDue=>0);

Is there something that I can do to modify this code so that it does not get run on a hardcopy call yet still allows students this option of trying new versions of each problem?

Here's the error:

WeBWorK error

An error occured while processing your request. For help, please send mail to this site's webmaster (webwork@missouri.edu), including all of the following information as well as what what you were doing when the error occured.

Thu Sep 18 11:25:51 2014

Warning messages

Error messages

Undefined subroutine &main::avg_problem_grader called at (eval 50965) line 358.

Call stack

The information below can help locate the source of the problem.

  • in WeBWorK::PG::Local::new called at line 49 of /opt/webwork/webwork2/lib/WeBWorK/PG.pm
  • in WeBWorK::PG::new called at line 1073 of /opt/webwork/webwork2/lib/WeBWorK/ContentGenerator/Hardcopy.pm
  • in WeBWorK::ContentGenerator::Hardcopy::write_problem_tex called at line 954 of /opt/webwork/webwork2/lib/WeBWorK/ContentGenerator/Hardcopy.pm
  • in WeBWorK::ContentGenerator::Hardcopy::write_set_tex called at line 863 of /opt/webwork/webwork2/lib/WeBWorK/ContentGenerator/Hardcopy.pm
  • in WeBWorK::ContentGenerator::Hardcopy::write_multiset_tex called at line 837 of /opt/webwork/webwork2/lib/WeBWorK/ContentGenerator/Hardcopy.pm
  • in WeBWorK::ContentGenerator::Hardcopy::write_multiuser_tex called at line 610 of /opt/webwork/webwork2/lib/WeBWorK/ContentGenerator/Hardcopy.pm
  • in WeBWorK::ContentGenerator::Hardcopy::generate_hardcopy called at line 254 of /opt/webwork/webwork2/lib/WeBWorK/ContentGenerator/Hardcopy.pm
  • in WeBWorK::ContentGenerator::Hardcopy::pre_header_initialize called at line 183 of /opt/webwork/webwork2/lib/WeBWorK/ContentGenerator.pm
  • in WeBWorK::ContentGenerator::go called at line 370 of /opt/webwork/webwork2/lib/WeBWorK.pm

Request information

Method POST
URI /webwork2/MTH_104_Intermediate_Algebra/hardcopy/HW1-IntroToWeBWorK/
HTTP Headers
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Connection keep-alive
Referer http://webwork.math.missouri.edu/webwork2/MTH_104_Intermediate_Algebra/hardcopy/HW1-IntroToWeBWorK/?effectiveUser=pseeburger&user=pseeburger&key=8FfRXVTcLJBb5Jl5mT90ost45xCzEwUC
Content-Length 2482
Accept-Encoding gzip, deflate
Content-Type multipart/form-data; boundary=---------------------------187161971819895
Accept-Language en-us,en;q=0.5
Cookie WeBWorKCourseAuthen.MTH_104_Intermediate_Algebra=pseeburger%098FfRXVTcLJBb5Jl5mT90ost45xCzEwUC%091411057545
Host webwork.math.missouri.edu

In reply to Paul Seeburger

Re: Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Alex Jordan -
I don't know if this solves the issue, but the 'X' should not be there in:
ProblemRandomize(when=>"Correct",XonlyAfterDue=>0);

In a post related to this macro a little bit ago, this forum's auto-formatting of posts was inserting that X for reasons I can't understand. Maybe it did for you too, but if you copied code from somewhere on these forums, the X is a mistake.
In reply to Alex Jordan

Re: Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Paul Seeburger -
Well, the X was not in my PGcourse.pl file.  I guess it was added when I copied it here.  So I think there is another issue.

Is there a way to have the PGcourse.pl not load for a PDF Hardcopy call?

Or perhaps a way to run the code within the PGcourse.pl only if it is not a PDF Hardcopy call?

This is what is actually in my PGcourse.pl file:

loadMacros("problemRandomize.pl");

#  Allow for the user to try another version of the problem once they get it correct.
ProblemRandomize(when=>"Correct",onlyAfterDue=>0);


In reply to Paul Seeburger

Re: Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Michael Gage -
problemRandomize.pl  calls the macro  &main::avg_problem_grader when it 
sets up the problemRandomize object.

avg_problem_grader is defined in the macro file   PGanswermacros.pl

Was PGanswermacros.pl loaded when you loaded problemRandomize.pl?  You 
might have to makes sure that is loaded as well.  In particular the header file doesn't usually load the PGanswermacros.pl file but it might load the PGcourse.pl file.

-- Mike (and Geoff)
In reply to Michael Gage

Re: Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Dick Lane -
The first example at
        http://webwork.maa.org/wiki/Customize_Course
is a snippet from my PGcourse.pl which I've used for several years without any difficulty involving production of hardcopy.
(When I get some of that mythical free time, I may edit-out the parts related to my providing observer status to some of my colleagues.)

Since my production server runs WW2.9/PG2.9 on Ubuntu 12.04 LTS, and since there are reports suggesting use of Ubuntu 14.04 LTS (and newer Perl) has caused problems, I wonder whether the search should look at the OS/etc.
In reply to Paul Seeburger

Re: Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Paul Pearson -
Hi Paul,

You can have mode-dependent code.  See below.

Best regards,

Paul Pearson

if ($displayMode eq 'TeX') {
  # stuff
} else {
  # not in TeX mode, do other stuff
}
In reply to Paul Pearson

Re: Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Paul Seeburger -
Ok, I tried a combination of the solutions offered and must have entered something incorrectly since it now totally crashes all problems run with it.

It also still caused errors when Hardcopy was run too, actually here's one of them:
  • [edit] Errors encountered while processing problem '6' in set 'HW2-OrderOfOperations' for user 'pseeburger'. This problem has been omitted from the hardcopy. Error text:
    ERRORS from evaluating PG file: <br/> Error detected while loading [TMPL]/macros/PGcourse.pl:
    PG_macro_file_eval detected error at line 123 of file [PG]/lib/PGloadfiles.pm
    Undefined subroutine &main::ProblemRandomize called at line 14 of [TMPL]/macros/PGcourse.pl, <MACROFILE> chunk 1.
    The calling package is PGloadfiles
       Died within PGloadfiles::compile_file called at line 166 of [PG]/lib/PGloadfiles.pm
       from within PGloadfiles::loadMacros called at line 431 of [PG]/macros/PG.pl
       from within main::loadMacros called at line 30 of (eval 19590)
    <br/>
What I thought was odd was that it was still complaining about ProblemRandomize as if it was seeing the code in the middle.

Here's my code that caused the errors:

if ($displayMode ne 'TeX') {
  loadMacros("PGanswermacros.pl, problemRandomize.pl");

#  Allow for the user to try another version of the problem once they get it correct.
# ProblemRandomize(when=>"Correct",onlyAfterDue=>0);

  $guest   = ($effectivePermissionLevel == -5) ; #### practice user
 
 #### Presume that faculty observers are given TA status (permission 5).
   $observe = ($effectivePermissionLevel >=  5) ; #### TA or Professor (permission 10)
 
 #### Allow a Guest or Observer to always get a new version of problem.
  ProblemRandomize( when => "Always", onlyAfterDue => 0, style => "Button" )
  if ($guest or $observe) ;
 
 #### Allow everybody else (e.g., a student) to get a new version (for more practice)
 #### after correct answer(s) to first (scored) version OR after the assignment's due date
   $when = (time >= $main::dueDate ? "Always" : "Correct");
   ProblemRandomize( when => $when, onlyAfterDue => 0, style => "Button" )
  if not ($guest or $observe) ;
}

Can you see what I may have done incorrectly?

By the way, what is in the standard PGcourse.pl?  Anything?

Thanks!
In reply to Paul Seeburger

Re: Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Dick Lane -
My full PGcourse.pl is attached to my post in the Oct-2012 thread
    http://webwork.maa.org/moodle/mod/forum/discuss.php?d=2803

I suggest you post your PGcourse.pl as an attachment because trying to include it in the body of your message seems to mangle parts by inserting spurious "X" characters.

FWiW: I suspect your version does not include
    sub _PGcourse_init {}; #### Don't reload this file.
as first command and does not end with
    1;
I do not know whether either of those is mandatory.

Note: there is a do-nothing version of PGcourse.pl near the end of the normal path which WeBWorK searches to find macro files:
        OpenProblemLibrary/macros/Union/PGcourse.pl
If a file with the same name is found earlier in the search path (e.g., in the course's macro folder), that version will be loaded.  For a problem author, that implies it should be a benign act to have PGcourse.pl be the last-loaded macro file.
In reply to Paul Seeburger

Re: Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Davide Cervone -
One problem is your line
    loadMacros("PGanswermacros.pl, problemRandomize.pl");
which should be
    loadMacros("PGanswermacros.pl", "problemRandomize.pl");
(note the additional quotation marks). This would cause loadMacros to try to load a single macro file with a name that includes a comma and a space. So that would account for the fact that ProblemRandomize() is not defined (since the macro file would never be loaded).

But I don't see why you wold be getting to this section of the code in hardcopy mode. And I don't see why loadMacros() would not have thrown an error about not finding the specified file (perhaps hardcopy handles that differently -- I've never checked).

I'm with Dick, you need to attach the actual file you used. it is too easy to have the copy and paste not include the actual problem.

In reply to Davide Cervone

Re: Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Paul Seeburger -
Thanks, Davide!

You caught my error.  I don't know how I missed that.

Everything works fine now for the problems AND for the Hardcopy PDF generation.

I am still not sure why it was causing trouble earlier though.

Why would the PGanswermacros.pl be needed to be loaded into the PGcourse.pl before problemRandomization.pl?

It does not appear that the if ($displayMode ne 'TeX') { } was really needed here.

Why might I need this file to be loaded while it did not seem to be needed by others?

I've attached the current PGcourse.pl file in case you are curious.

Thanks!
In reply to Paul Seeburger

Re: Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Michael Gage -
see http://webwork.maa.org/moodle/mod/forum/discuss.php?d=3480#p9118

problemRandomize.pl calls macros from PGanswermacros.pl so that needs to be loaded first.  In some problems you have already loaded PGanswermacros.pl before PGcourse.pl is called but that is not always the case -- particularly in the header files where PGanswermacros.pl is not loaded at all.




In reply to Michael Gage

Re: Trouble getting PDF Hardcopy to work with problemRandomize.pl in PGcourse.pl

by Paul Seeburger -
Thanks, Mike!

It's beginning to make more sense now.

Then it seems that our recommendation for including problemRandomize.pl in the PGcourse.pl file should include a recommendation to load PGanswermacros.pl first in this PGcourse.pl file as I am now doing.

I was indeed getting errors right away in the problem set header files.

Thanks!

Paul