Skip to content

Instantly share code, notes, and snippets.

@mreidsma
Last active December 20, 2015 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mreidsma/6186338 to your computer and use it in GitHub Desktop.
Save mreidsma/6186338 to your computer and use it in GitHub Desktop.
An idea for auto-updating the user's major in Illiad, pulled from a local backup of Banner data (or from a live query). Brainstormed on a drive through the Smokey Mountains after a conversation with @griffey. Untested, but should work if you fill in the workee bits.
user_id | email | major | last_updated
------------+----------------------------+--------------------------+---------------------
1231o8433 | joeblow@library.edu | Economics | 1375980483
------------+----------------------------+--------------------------+---------------------
873648737 | sueseashells@library.edu | Philosophy | 1375980483
------------+----------------------------+--------------------------+---------------------
879472974 | billybob@library.edu | Engineering | 1375980483
------------+----------------------------+--------------------------+---------------------
<!--
I'm assuming you would just enter this script in the include_footer.html template, to be called on any page.
The trick is getting around the same-origin policy. We need to get information from Illiad to
our local server that hosts the database of folks' majors (or a script that queries Banner), but same-origin won't let
us do that for obvious security reasons (read more about it here: http://en.wikipedia.org/wiki/Same_origin_policy
However, we can include any kind of file as a <script> as long as the data that is included is Javascript. So, we
include a php file from our server, passing it the info it needs from our Illiad session, and then use the file
to echo the appropriate Javascript to do what we need to do. The only change to Illiad should be including this line:
-->
<script type="text/javascript" src="http://myserver.edu/illiad-update.php?email=<#USER field="EMailAddress">&major=<#USER field="Major">"></script>
<?php
if((isset($_GET['email'])) || (isset($_GET['major']))) { // Probably should also check for empty $_GET
// We've got data. Let's look this user up
// Connect to database
$db = new mysqli('host', 'user', 'password', 'database');
// Obligitory error checking
if ($db->connect_errno) {
printf("Connect failed: %s\n", $db->connect_error);
exit();
}
// Watch out for naughty bits
$email = $db->real_escape_string($_GET['email']);
$major = $db->real_escape_string($_GET['major']);
// Set some needed stuff
$now = time(); // Right now.
$interval = 7884000;// # of seconds in 3 months. Change this to change the interval.
$query = "SELECT * FROM users WHERE email='$email'"; // assumes unique emails.
// Grab the user's record
$result = $db->query($query) or die($db->error);
if($result) { // It worked....
if($result->num_rows == 1) { // Did we get one, no more, no less?
if($now-$interval <= $result->last_updated) { // Major hasn't been updated in the oast 3 months. Check it.
if($result->major == $major) { // No change. Update the timestamp to reflect it was checked today
$db->query("UPDATE users SET last_updated='$now' WHERE user_id='$result->user_id'");
} else {
// Major has been changed. Let's update it in Illiad.
/*
This is the strategy: echo some jQuery to do an AJAX update to the ChangeInformation.html form
with the new major. I don't have access to an Illiad server right now, but it should be
trivial, right?
You could also load the form in a hidden div and then submit it in the background, but that
would cause a refresh of the whole page.
*/
echo '$.ajax({
type: "POST",
url: illiad.dll,
data: "IlliadForm=ChangeUserInformation&Major=' . $major . '
});';
// Then, update the timestamp.
$db->query("UPDATE users SET last_updated='$now' WHERE user_id='$result->user_id'");
/*
The browser will see this included PHP file as a JS file, since we're echoing JS. We just use the
PHP to get the bits we need into place.
The info in the post request will probably need some tweaking, and you might have to send ALL of the info
and not just the changed info. As I said, I haven't tested this.
*/
} // End echo of JavaScript into Illiad
} // End check of whether major has been updated in past 3 months
} // End check of whether 1 record was returned
} // End if($result)
} // End check of GET values set
@mreidsma
Copy link
Author

mreidsma commented Aug 8, 2013

Man, that PHP is ugly.

@mreidsma
Copy link
Author

mreidsma commented Aug 8, 2013

Problems I already see: it's going to run on every page load. How do we make it only show up once per session? That's an extra server request and work on your local service (although not much in the way of download. Maybe set a cookie?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment