Copying Arrays (and Objects) in Javascript

This is a bit of an amateur mistake, but I definitely wasn’t the first person to make it and I suspect I won’t be the last. I had a JavaScript array that I wanted to copy, then make some change to the copy, so that I could access the original and the modified copy. Therefore, being used to PHP, I did the following, assuming it would make a ‘proper’ copy of the original:

var original = [1,2,3];  //Define original array
var copy = original;     //Attempt to copy original
copy[2] = 4;             //Modify the copy
alert(original[2]);      //Hoped for 3, got 4

I’d created a reference to the original array, rather than a copy of it. As a result, updating the copy also updated the original.

So, how do I create a copy of an array, rather than a reference. Thankfully, the slice method makes this very easy:

var original = [1,2,3];        //Define original array
var copy = original.slice(0);  //Copy original using slice
copy[2] = 4;                   //Modify the copy
alert(original[2]);            //Got 3 this time

This works fine for simple, one-dimensional arrays, where the array contains only booleans, numbers or strings. If the array is multi-dimensional, i.e. it contains other arrays or objects, then only the top level array will be copied, the arrays/objects it contains will be referenced. Therefore, it is necessary to create a function to do this – this seems to be a good way to do:

Alternatively, use jQuery, and take advantage of the extend method:

var original = {team:"Arsenal"};               //Define original object
var copy = jQuery.extend(true, {}, original);  //Copy original using extend = "Man Utd" ;                        //Modify the copy
alert(;                          //Alerts Arsenal

Virtual microscopy in the classroom

This morning saw the first use of CSlide teaching slides within the laboratory to demonstrate the features that students should be looking for down their own microscopes. Monitors around the laboratory display the demonstrator’s computer screen or a view down the demonstrators microscope. The two main benefits of using CSlide on the computer, rather than projecting images from the demonstrator’s microscope, are:

  1. CSlide starts with a view of the whole slide and the demonstrator zooms in by double-clicking. This helps students get a feel for where they will find the relevant structures on their own slides. With a projected microscope, this overview is generally not possible and the change of lenses required for zooming in can make the experience disjointed.
  2. Each slide can be opened before the class, ready for use, in a separate window. This makes it very much quicker and easier to switch between slides, when teaching or responding to student questions, than with a traditional microscope.

The next stage is to embed the slides in students’ online teaching materials as has been done in the second year Nervous System course (Oxford users only). Annotations and ‘tours’ around the slides will give students a chance to come back to slides of interest outside the time-pressured environment of the laboratory. We also hope to build on these with self-test quizzes and decision maze case-studies.


The Basics of writing a Basic LTI Tool Provider

I hope this will be helpful for anyone just getting started with (Basic) LTI and wanting to create their first Tool Provider. Apologies for any abuse/misuse of the terminology – this is just how I understand it. To recap the two halves of an LTI launch:

Tool Consumer (TC) = An LTI-enabled VLE/LMS/other system that can make an LTI launch request. Generally (or at least the way we are using it), the TC manages user accounts/passwords, so that the Tool Provider doesn’t have to.
Tool Provider (TP) = an external tool that receives an LTI request from a TC and uses the launch data to work out what the user is able to see/do within the tool.

Useful links

I found the following useful when getting to grips with LTI and creating my first TP (in PHP):

Thanks of course to Dr Chuck and the rest of the LTI community for developing this specification and the above Classes, Tools and Tutorials.

Basic Implementation

The PHP Basic LTI class makes it very easy to do the LTI/OAuth bit of the TP. Here’s my pseudo-PHP code for the basic process:

//All of the LTI Launch data gets passed through in $_REQUEST
if(isset($_REQUEST['lti_message_type'])) {    //Is this an LTI Request?

    //We store oauth_consumer_key and secret pairs in our database, so we look the secret up here, but it can just be hard-coded (especially for testing)
    $secret = [secret];

    //Get BLTI class to do all the hard work (more explanation of these below)
    // - first parameter is the secret, which is required
    // - second parameter (defaults to true) tells BLTI whether to store the launch data is stored in the session ($_SESSION['_basic_lti_context'])
    // - third parameter (defaults to true) tells BLTI whether to redirect the user after successful validation
    $context = new BLTI($secret, true, false);

    //Deal with results from BLTI class 
    if($context->complete) exit(); //True if redirect was done by BLTI class
    if($context->valid) { //True if LTI request was verified
     //Let the user in
else { //Not an LTI request, so either don't let this user in, or provide another way for them to authenticate, or show them only public content

Just to further explain the parameters passed when instantiating the BLTI class, the first argument is the secret, which is required and would usually be a string. Alternatively, you can pass through an associative array of database information (e.g. ‘table’ => ‘lti_keys’, ‘key_column’ => ‘oauth_consumer_key’), and the BLTI class will look up the secret from the database.

The second argument (true by default) tells the BLTI class whether to store the launch data in the session (from which it can be retrieved using $_SESSION[‘_basic_lti_context’]) and whether to try to automatically retrieve any stored LTI launch data if someone tries to access a tool without coming in through LTI. This means that if a user has initially come to a tool through LTI, then closes the browser tab containing the tool, and then goes directly back to the tool, without coming through LTI, as long as their session has not expired they will be allowed back into the tool, even though have not come through LTI. I would generally recommend keeping this as true, as I think this would usually be useful behaviour.

The third argument (true by default, but I generally set it to false) tells the BLTI class whether to do the redirect or not after validation of the request. Setting this to false will prevent it from doing the redirect.

I hope this is helpful. My understanding is pretty (cheap pun alert!) basic, so I would welcome any thoughts, queries, suggestions or corrections.

For further information/discussion of LTI, and how we have used it to allow access to our iCases system through WebLearn (our VLE), please see these posts:

Twitter Widget – filter by @username and #hashtag

If you want to embed tweets in your blog or website from a @username but only show those with a given #hashtag, Twitter’s Widgets makes it easy:

For example, to show only tweets from my @allotmentor account which are tagged with #allotment, I simply enter

from: @allotmentor AND #allotment

in the Search Query box on the Configure a search widget page. Then copy and paste the generated code into your site. See it in action here.

Thanks to this answer on Stack Overflow

Award hat trick

A duo of awards over the summer:

was topped off yesterday by news that we have been awarded FENS Funding for European Neuroscience History Projects in 2013 to capture and make available on our History of Medical Sciences website ‘3-dimensional images of physiological apparatus and models with historic interest’.
See all our summer news in the MSDLT Update