Embedded data extraction

I recently developed a small website that allowed users to add students to a database and then allow them to be assigned software license keys. Part of the problem that the website aimed to solve was that previously students were purchasing the license keys when they weren’t necessarily entitled to them. Even after writing instructions for those selling the keys there was a failure of the students’ details being checked.

To check the details we required the student’s surname and access to the University of Melbourne email directory. Not terribly difficult but it did require those selling the keys to open a bookmarked page, enter the surname, browse the search results.

In order to improve accountability and the tracking of the purchases I decided to develop the website to allow the sellers (i.e. the users of the system) to more effectively manage the selling of keys to students. This improved management would hopefully:

  • Stop students from purchasing multiple keys wherein not entitled.
  • Make sure only eligible students were purchasing keys.

The first problem was easily solved by uniquely identifying the students and using the database. I won’t go into how this was solved as it is fairly trivial. The cool part was the checking of student’s eligibility for the keys.

Students are only eligible if they are enrolled in certain courses (Computer Science, Software Engineering, Information Science, Melbourne Model IT degrees). At first I linked to the Unimelb email directory on the student creation page. However, I decided I’d like a solution wherein the user did not even need to leave the website.

Hence, I developed the following Javascript (with jQuery support) to enable an embedded extraction of the Unimelb email directory’s data:

\\ A small anchor element with id 'email_check' sits next to the surname input field on the page:
$("#email_check").click(function()
{
  \\ The embedded results from the extraction are placed in a div with id 'check_list':
  $("#check_list").html("");

  \\ The surname input field has id 'surname_field', jQuery allows us to access the value attribute:
  var surname = $("#surname_field").attr("value");

  \\ .get is a jQuery Ajax function. We're going to invoke another page on the site, parse.php
  \\ and give it the surname we're interested in. The function argument is a callback for when
  \\ the Ajax call is complete. The returned page data is a string called 'page_data'.
  $.get("./parse.php?surname=" + surname, function(page_data)
  {
    \\ From the page_data we want to look at each of the results. The data is luckily
    \\ grouped within <tr> elements with the class 'vcard':
    $("tr.vcard", page_data).each(function ()
    {
      \\ Within these <tr> elements are the full names of the students and their courses,
      \\ kept in <td> elements with classes 'fn' and 'org', respectively.
      var fullname = $("td.fn", this).text();
      var course = $("td.org", this).text();

      \\ Finally we want return this data to the actual page of our website.
      \\ Using the append function we can drop new HTML into the existing dom:
      $("#check_list").append("<li><label>" + fullname + "</label>" + course + "</li>");
    });
  });
});

Accessing the off-site web page (the Unimelb email directory) was achieved via the parse.php page on my site, which took the form of:

<?php
  $surname = $_GET['surname'];

  $file = fopen("http://www.unimelb.edu.au/cgi-bin/mail/email.pl?name=".$surname, "r");

  while(!feof($file))
  {
    echo fgets($file);
  }
  fclose($file);
?>

The results work quite well. Users of the system are able to easily search for a student’s surname without having to leave the comforts of our website!

Now if anyone is caught selling keys inappropriately there are absolutely no excuses :-)

This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>