Moodle User Pictures from iSAMS

Having linked our Moodle installation with iSAMS to automate course creation and enrolment I began looking around at what other things we could link up and automate. Whilst spending some time on the Moodle forums, I came across this post: https://moodle.org/mod/forum/discuss.php?d=272020 which linked to a command line script someone had added to the Moodle Docs to allow for synchronisation of user profile pictures.

Following the link to the Moodle Docs page lead to a PHP script which someone had uploaded to a forum (if anyone knows the original creator of this script I will happily credit them!) which will copy pictures from a local directory on your Moodle server into the users’ profile pictures, making the relevant database changes too. The only caveat is that the photo files must be named exactly the same as the username in Moodle.

In iSAMS we already have photos of all of our students (and staff too) which are saved on the hard drive of the iSAMS IIS Server in JPG format. I also have a bit of SQL which was provided by iSAMS in their Report Writing Training to let you find the newest picture which has been uploaded against a student record. Armed with this knowledge and the fact that we know from previous posts that our Moodle server can now happily talk to our MSSQL server, I set about writing the scripts to carry out the following steps:

  1. Query the MSSQL iSAMS database for the latest photos for all student records in the database
  2. Connect to the iSAMS IIS server to copy the relevant jpg files over to our Moodle server and rename appropriately
  3. Run the “Update user pictures by command line” script

All of these steps will be wrapped up into one single “wrapper” script which will be run by cron and create its own log files too. This wrapper will be a bash script which calls two PHP scripts. The first PHP script will fetch all of the data from the iSAMS database and write it out to a CSV file on the Moodle server. The bash script will then read each line of this file into an array and use smbclient to copy each user picture from the iSAMS IIS server and rename it appropriately on the Moodle server. Finally we will call our final PHP script which will insert the newly copied and renamed user picture files into our Moodle install.

This walk-through assumes that you have set up your Moodle server to talk to MSSQL and iSAMS as detailed in this post. You do not need to be using the iSAMS to Moodle course enrolment synchronisation, but you do need your Ubuntu Moodle server to be able to query your iSAMS MSSQL server!

Query the iSAMS database

You can download a copy of my PHP script for querying the iSAMS database and writing the CSV file here from my PasteBin account. Once downloaded, open the file and edit lines 20 – 23 with your iSAMS database server details. Save the file as “user-picture-locations.php”.

  1. Upload this file to your “/var/www/scripts” directory creating it if it does not already exist,
  2. Execute the file to check it works by typing
    php ./user-picture-locations.php
  3. All being well you should now be able to open the “user-picture-locations.csv” file and check the contents for details of your student picture locations on your iSAMS server
    less ./user-picture-locations.csv

Connect to the IIS Server

We are going to use smbclient to connect to our Windows share and copy each file over to our Ubuntu server. If you have not already got smbclient installed, type the following

sudo apt-get install smbclient

We are now going to create our bash script which will call the PHP scripts and execute the copying of the picture files from iSAMS to your Moodle server.

  1. Still inside the scripts directory, create the bash script and make it executable
    touch ./student-photo-import.sh
    chmod +x ./student-photo-import.sh
  2. Open the file up for editing
    nano ./student-photo-import.sh
  3. Copy the contents of the bash script available on my PasteBin into nano
  4. Go to line 28 of the file and replace “iSAMS-IIS-Server” with the DNS name of your iSAMS server and change the “e$” after it to match the drive letter that your iSAMS files are stored on
  5. Also on line 28 replace “AD-User-With-Read-Access-to-Share” with a Active Directory user who has read access to the dollar share of your iSAMS IIS server
  6. Next on line 28 replace “ReallyLongComplexPasswordWithoutAnySpecialCharactersInIt” with the password for that user. Ensure the password is set to never expire in AD and that it does not contain any special characters which might bork the bash script…
  7. And finally on line 28 replace the “YourDomainName” with the name of your domain.
  8. Save the file and exit nano
  9. Inside your scripts directory make a new directory called “photoimports”

I will now just talk through the various sections of the script:

  1. We start by calling the PHP script set up in the previous section; notice all output is being redirected to a log file
  2. We then delete all JPG files inside the “photoimports” directory and then move into the directory
  3. We then save the current Internal Field Separator to $OIFS and set $IFS to a comma
  4. Now we cat the contents of the CSV file generated by the PHP script into bash a read each line into a while loop
  5. We then use smbclient to connect to the iSAMS IIS server and get the picture file and save to the Ubuntu server
  6. Still in the loop we then rename the file to match the user name
  7. We now end the loop and move onto the next line of the CSV file, repeating steps 5 and 6 until we reach the end of the file
  8. Next we call the “updatepics.php” file we fetched from Moodle
  9. Finally we set $IFS back to what it was at the beginning

Run “Update user pictures script”

First off, download the updatepics.php script from the Moodle forums; I have also put a copy up here on my PasteBin account. Now save this file to your Moodle server in the “/var/www/scripts” directory created earlier.

  1. We need to edit this script so that it can run from outside of Moodle
    cd /var/www/scripts
    nano updatepics.php
  2. On line 5 of the script change
    require (dirname(dirname(dirname(__FILE__))).'/config.php');

    to be the full path to your Moodle config.php script. For example mine now looks like this

    require '/var/www/html/moodle/config.php';
  3. Save the file and exit nano

You should now be good to give this script a test run! All being well there will be no output to the screen, but when it finishes you should be able to view the contents of the log file created and also see your updated pictures in Moodle!

sudo  ./student-photo-import.sh

Last few adjustments!

The final steps are to set this up as a scheduled cron job, set up log rotate for the log file and (if you want) turn off the ability in Moodle for users to change their profile pictures.

Create the Scheduled CRON Job

  1. Edit Root’scrontab:
    crontab -e
  2. Enter the following to run the script at 3am every morning:
    0 3 * * * /var/www/scripts/student-photo-import.sh >/dev/null 2>&1 #Moodle iSAMS Student Photo Import
  3. Press “Ctrl + O”, Enter and “Ctrl + X” to save and exit Nano

Set up Log Rotate to rotate our script logs

We will make use of the already defined Apache Log Rotate settings and just include our new logs directory in those settings.

  1. Move to the Log Rotate directory:
    cd /etc/logrotate.d/
  2. Open the Apache2 configuration for editing:
    nano ./apache2
  3. Edit the first line of the configuration file adding “/var/www/scripts-logs/*.log” just before the “{“. My edited file now looks like this:
    /var/log/apache2/*.log /var/www/scripts-logs/*.log {
    weekly
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
    if /etc/init.d/apache2 status > /dev/null ; then \
    /etc/init.d/apache2 reload > /dev/null; \
    fi;
    endscript
    prerotate
    if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
    run-parts /etc/logrotate.d/httpd-prerotate; \
    fi; \
    endscript
    }
  4. Press “Ctrl + O”, Enter and “Ctrl + X” to save and exit nano

Moodle Adjustments

You may now want to remove the ability for users to change their profile pictures in Moodle. If you do then follow these steps:

  1. Log into your Moodle as an administrator
  2. In the Site Administration menu navigate to “Site Administration > Security > Site Policies”
  3. Scroll down towards the bottom of the page and put a tick against the setting “Disable user profile images”

Users will no longer be able to change their profile image but your script will still update them!

Summing up

So we have combined three steps here really:

  1. Used a PHP script to query our iSAMS database for locations of picture files of students. We wrote this info into a CSV file on the Ubuntu server
  2. We then used bash scripting to read each line of this CSV file and pass the contents into smbclient to copy the files from our iSAMS IIS server to our Ubuntu Moodle server
  3. Finally we used the script from Moodle to import these photos into Moodle

As an administrator in Moodle you can now visit http://<your-moodle>/userpix to see all profile pictures in Moodle.

It would be a fairly easy change to adjust these scripts to do the same for staff images too!

Leave a Reply

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