Mechanized Moodle

One of the main things I use Perl for is to manage my grading chores on Chi Nan University’s Moodle system. Moodle is a very useful teaching tool, one I’ve been using for over 10 years.

So if it’s such a great system, why use Perl? I’ve used Perl with Moodle for various reasons over the years, but the main reason I use it now is to download student work to my computer, process it using a variety of fast and dirty methods that are very specific to how I do stuff, and then upload the results, usually comments and grades, back to Moodle for students to review.

The problem with this is Moodle changes, often. Some of these changes are quite significant, requiring you to completely revise your workflow.

The university tries to avoid upgrading Moodle in the middle of the semester for just this reason, but when an upgrade includes important security and bug fixes, the school sometimes bites the bullet and upgrades, and as a result I’m stuck with broken scripts in the middle of the semester, as happened last week.

The way the script broke, however, helped me learn a little bit more about Perl and particularly its extremely useful WWW::Mechanize package, so I’m putting up this note to remind myself of what the deal was and to let the one or two stray readers out there know as well.

The main reason I use WWW::Mechanize is to put up comments/grades for my translation assignments. I generally have 8 to 10 single sentences for one translation assignment, and anywhere from 25 to 40 students, though I have had as many as 60 students in the past, meaning that for one assignment I might have to download 300-600 answers and upload 600-1200 grades and comments.

Why download answers? After all, Moodle has a grading interface for this kind of question. Well, I have my methods, as Holmes said, and they’re fast and efficient for what I do. Downloading is not a big problem, it’s just screen scraping. Uploading grades and comments, however, is a different story. Doing grades and comments on my home computer and then copying them manually to the Moodle interface is enormously time consuming and requires very close attention, or you will put the wrong scores in the wrong boxes, a highly embarrassing mistake.

This workload can negate all the time I save grading and commenting using my home methods, no matter how much more efficient they are than Moodle’s methods. To avoid this gigantic waste of time, I wrote a Perl script.

The script gets the comments from the database table where I put them after grading and commenting, and uses WWW::Mechanize to upload them to the school’s Moodle site; all I have to do is input the assignment number and the rest is…mechanized.

This means I don’t have to consider the time spent uploading to Moodle at all, and can put in all sorts of safety checks to make sure the right grade goes to the right person; these safety checks have meant that I often catch mistakes I might otherwise have missed. Anyway, Mechanize is the main workhorse in the upload process.

The main pain in writing this script was finding the parameters to upload the data through Moodle, but after a lengthy process of hair-pulling and many bad words, I finally got them all down. Unforunately, when Moodle’s grading interface changes even a touch, I’m screwed. Basically, I am screen scraping to get the parameters I need to upload my local data, and I have to put these in a properly formatted html form for Moodle to accept everything. Even minor changes in formatting can break my script, with the result that the data simply doesn’t go in.

This happened with the security upgrade just last week, a very typical example of breakable screen scraping. I ran the script to upload comments and grades for the latest assignment, and the grades went up, but the comments did not. Why? Why?? Why???

To find out, I turned on fiddler, an HTTP debugging proxy, and set mech to go through it as follows:

my $mech = WWW::Mechanize->new();
#$mech->proxy(['http'], 'http://localhost:8888/');

I also set Firefox to use fiddler and filled out the Moodle page there as well, then compared the results of ‘post’ through Perl and through Firefox to see what was going wrong. Here I omit several days of aggravation, pulling out what’s left of my hair, and many more bad words.

The short of the story is that the Moodle format did indeed change. My original script set the parameters for the original Moodle page as follows:

First I get my local data from an mysql table and format it using this loop:

while ( my ($cmt,$mark) = $select->fetchrow_array ) {
push(@CommentsGrades, [ textarea => $cmt ], [ text => $mark ]);
}

Then I use Mechanize to get the page and set the HTML form parameters like this (omitting where I input passwords and do sanity checks):
$mech->get($qpage);
$mech->set_visible(@CommentsGrades);
$mech->submit();

This is how simple Mechanize solutions can look.

The problem this time arose from the fact that the original Moodle page presented the student answer in a textbox, and then used a textarea form to get comments. The new version uses a readonly textarea form to present the student answer, and a second writeable textarea form to get comments. It also added a dropdown option box to let you decide how you want to format your comments (html, plain text or moodle enhanced). It put this between the comment box and the text box for the mark (grade). This confused the issue even more.

The solution for this change was to modify the array with the comments and grade like this:
push(@CommentsGrades,[ textarea => '' ], [ textarea => $cmt ], [ text => $mark ]);

Funky, isn’t it?

It turns out that the set_visible function keeps track of the parameters it sets by parameter type; my array had only one type of textarea variable in it, for comments, but the new moodle had two textarea variables in it, for answer display and comments; this made for confusions and set_visible tried to load comments into the readonly textarea, hence the comments were not accepted. I fixed this by adding a dummy textarea to my array before the comment textarea.

Why were the grades accepted? There is only one type of visible text box, the one for grades, so set_visible correctly inserted the grade value where it belonged.

As for the dropdown option box, you don’t even need to put it in the array (unless you want fancy formatting for your comments, I guess.) The missing option box will not confuse set_visible at all, and this surprised me; I thought a missing parameter like this would have a cascade effect, knocking everything off by one for each parameter, but since I didn’t put in any data marked as option => xxx, set_visible just ignored the option box.

And I learned all this at the cost of only a few thousand hairs, and however long I have to spend in purgatory for all those ‘sbloods! and ‘by the rood!’s.

Posted in Programming, School | Comments Off on Mechanized Moodle

Zotero to Endnote: part II

As I noted before, I use both Zotero and Endnote to do my bibliography work. For works that I have pdfs of, I keep these in a folder on D: drive. I use the same pdfs for both my Zotero and Endnote bibliographies.

Zotero can export to Endnote via the RIS format, so all seems well. Naturally, I want to have the locations of the pdfs included in the export from Zotero. Naturally, I don’t want to have the pdfs included with the export. Just include the location, not the file, right?

Unfortunately, using Zotero out of the box, when you tell it leave off the pdfs and export, it leaves off everything about them; if you tell it to include the pdfs, well, I have over 20 gb of pdfs, and its a long process.

In this post, I described how to modify the RIS export function in Zotero, so that it exports the names and locations of the pdfs, but not the pdfs themselves. This method works, but there is a hitch that I got hit with the other day and I note it here for future reference.

My solution involved a one line edit to Zotero’s RIS.js file. The hitch is that RIS.js is automatically updated whenever Zotero is updated. This naturally wipes out the one line edit. The solution is–check the RIS.js file every time you want to export, to make sure the edit hasn’t been overwritten

I only found this out when I tried to export part of my Zotero library to an .ris file in c:\my docs. But my C: drive is now getting very short of space, thanks to Windows XP (6 gb) and Acrobat (3 gb). No matter, I still have 5 gb of free space.

Export begins, but why so slow? Yikes! It’s copying all my attachments to the C: drive. Can you tell Zotero to cancel? Nope. Can you turn off Firefox? Nope. How much room do all my attachments take up anyway? 6.5 gigabytes! 6.5 gigabytes!! (I sound like Doc Brown in Back to the Future.) Only one thing to do. Turn off computer. No, no, no, bad idea! Well then, just delete the files as they are copied to c drive–one at a time (press delete, enter, 3000 times).

Ha! ha! ha!—he! he! he!—a very good joke indeed—an excellent jest. We shall have many a rich laugh about it at the palazzo—he! he! he!—over our wine—he! he! he!

Posted in Software | Comments Off on Zotero to Endnote: part II

Upgraded

Serious problems with my very old and partially scrungled ubuntu server finally forced me to upgrade both server and wordpress. So far so good; the new server is much more stable, and a couple of functions that were broken on the old wordpress site are now working. Still making the switch, just put the old web pages back.

Main wordpress change that any readers out there will see is in the permalinks, which I have changed to year/month/title links. Tried to get this to work on the old site, and failed repeatedly, to my great frustration. Turns out this was due to the default apache config file on ubuntu, which sets the /var/www directory to AllowOverride None. This not only shuts off mod_rewrite, it tells apache to ignore all .htaccess files. To get mod_rewrite working, you must set this to AllowOverride FileInfo.

Now able to schedule posts on wordpress; this was also broken on the old server.

This kind of stuff is not what I expected to do when I got a degree in literature.

Posted in Site news | Comments Off on Upgraded

perl win32 OLE arcana: 1

An obscure problem encountered while trying to set the cell background color in Excel:

$sheet->Range("q2q6")->Interior->{ColorIndex} =8;

returns the following message: OLE (0.1709) ERROR 0X800a03ec METHOD/PROPERTYGET “Range” at blahblah

A search turned up several long discussions that were ultimately totally irrelevant. The problem is that you can’t set color using this kind of range with this method; if you use this method, you have to do it a cell at a time:

foreach my $y (2..6) {
my $range = 'q' . $y;
$sheet->Range($range)->Interior->{ColorIndex} =8;
}

Posted in Programming | Comments Off on perl win32 OLE arcana: 1

Moving references from Zotero to Endnote: Part I

See the updated version of this post

Software to manage research references can be very useful indeed, and I’ve spent a lot of time using it to organize my references. Currently my two main programs are Zotero, an open source project, sponsored by George Mason University, and Endnote, the most widely used commercial program, published by Thomson Reuters.

There are many functions and options available in Endnote that are not available in Zotero, and also vice versa, so I want to be able to move my references back and forth between them. It is still not possible to do this completely, but there are ways to simplify the task.

One of the most annoying problems has been the difficulty of correctly importing pdf file links from Zotero references into Endnote references. Both programs allow you to put links to pdf files into your references, but there is a catch. When I export my Zotero references into .ris export format, I have to choose whether or not to export files. If I choose this, Zotero will physically copy all the linked files to the folder where the .ris export file is saved, copying them as slowly as possible, sometimes taking over half an hour for just a hundred references! What makes this doubly infuriating is that it is completely unnecessary, because I only need the link, not the file. And even after all this, it will still require hand editing to get the links imported into Endnote to conform to my pdf folder structure.

This is an incredibly annoying problem, but it can be easily resolved with just two keystrokes. Go to the “translators” folder of your zotero installation and use a text editor to open the file RIS.js Search for the following line:

att[j].saveFile(att[j].defaultPath);

comment this line out by adding two slashes in front of it:

//att[j].saveFile(att[j].defaultPath);

Now try to export some references. Zotero will still ask whether you want to export files; check the box, and Zotero will write ONLY an .ris file, with a CORRECT link to the location where the pdf files are really stored, eliminating the horribly slow copying, and the error prone hand editing problems that the old method produced. This means Zotero and Endnote can now share the same pdf files and folders, and you can move your references back and forth between the two programs without messing up the links to the files.

Posted in Research methods, Software | Comments Off on Moving references from Zotero to Endnote: Part I

Alice Munro wins Nobel Prize for Literature

Here is the story on BBC.

This was a surprising award; with the possible exception of Isaac Bashevis Singer, this is the first time someone who writes primarily short stories has won the Nobel Prize for literature. Even Singer is debatable; his bibliography lists around 20 novels, its just that a lot of people (like me) have said his short stories are his best work.

But Alice Munro (艾莉絲‧孟若) has written almost nothing but short stories. Her one novel, The Lives of Girls and Women, is basically seven or eight short stories held together by a common narrator and location, and is by no means her most popular or critically acclaimed work. In any case, the Nobel committee’s award specifically cites her as “master of the contemporary short story”, and that’s a description that has never been used before for a Nobel literature laureate.

I predict that this will be a very popular choice, simply because Munro is a very popular writer. She’s my favorite living short story writer, for sure. Unfortunately, the literature translation market being what it is in Taiwan (and China), she is very poorly represented in Chinese at the moment; short story collections are hardly ever translated because they sell so poorly. This is true in English, as Munro has often ruefully observered, and triply true in Taiwan and China. To my knowledge, the only one of Munro’s books available in Chinese is Hateship, Friendship, Courtship, Loveship, Marriage, translated as Ganqing youxi 感情遊戲 by Chang Jang 張讓, and published by China Times Publishing in 2003. Oh, there is also a partial translation of “The Lives of Girls and Women” by Lan Ya-chieh (藍雅婕), done as part of her MA thesis on Munro (Chi Nan University Dept. of Foreign Languages and Literature, 2004); this is currently the only thesis that has been done on Munro in Taiwan.

Posted in Literature, Translation | Comments Off on Alice Munro wins Nobel Prize for Literature

A belated hello to my students for the new semester

Hello to all my students,

It’s the beginning of another really big semester, and I’m late starting, as usual. I’m teaching fewer classes than usual this semester, working on a rather large project, but for the classes I am teaching, I’ve had fun meeting you in the last two weeks. The Moodle software has gone through another version, so I’m still learning the latest kinks, please excuse any delays in postings, etc. If you have any questions, drop me a line via moodle or my school email. And a special hello to the lucky people who won the big lottery and got me as advisor. We’ll get together later this month!

Posted in School | Comments Off on A belated hello to my students for the new semester

The remains of the semester

Another semester is over, with grades in and a summer of writing to look forward to. To my students, congratulations to the many who passed, condolences to the few who didn’t, and remember: tomorrow is another day!

Posted in School | Comments Off on The remains of the semester

Another (*&%&*^%R* Earthquake!

A 6.3 earthquake this afternoon. Supposedly only 5.0 in Puli, but one of the strongest and longest I’ve felt in a long time. Bookshelves down again, *&^%&!@$#!! This time both at home and in my office AGAIN, the second time this semester. Trying to remember how many times I’ve had damn earthquakes knock my bookshelves down since I moved to the Big Rumble; seems like it must be somewhere around a dozen. Jeez.

Posted in Quotidiana | Comments Off on Another (*&%&*^%R* Earthquake!

Ironic twist

In literature classes, one of the most difficult things to explain to Chinese speaking students is the difference between “irony”, “satire”, and “sarcasm.” Apparently distinguishing these is not always easy even for native speakers, but on the Chinese side, virtually every English-Chinese dictionary translates these as one thing: 諷刺. Yet the differences are vast.

This came back to me very sharply when my fiction class this semester read the stories “Gift of the Magi” and “The Diamond Necklace”; my question for the reading response began: “These two stories are both ‘ironic’ as we discussed in class…” One student’s response was 這兩個故事說不上是諷刺。比較像是 surprise ending,就是會覺得解果怎麼會是這樣,滿意外的…鑽石項鍊比較偏向諷刺,但是智者的禮物反而沒有諷刺的感覺,覺得有點可愛,好笑.

Naturally the student gets full credit for not cravenly surrendering her reasoning powers to the teacher, but as I said in my comments on the homework: “I said ironic, not 諷刺.” This is what “failure to communicate” really looks like.

Afterthought: Actually, now I’m wondering whether I got this wrong. Is the Gift of the Magi not ironic? We can at least say that, even though Jim and Della’s gifts to each other were useless in the end, they were not totally in vain. The gifts they chose revealed how much they cared for each other, and Jim, for one, took consolation in this. In “The Diamond Necklace”, however, the revelation of the necklace’s true value at the end destroys whatever consolation Mathilde has found. Does this make it more ironic?

In fact, even “The Diamond Necklace” was not ironic to some students, just silly: “Why didn’t Mathilde just tell Madame Forestier the necklace was lost instead of wasting ten years?” Good point. [That’s sarcastic, guys.]

Posted in Literature, Translation | Comments Off on Ironic twist