Sunday, December 08, 2024

automating user preferences modifications on Moodle - SikuliX

We had to change a large number of users' forum preferences in Moodle as noted in the previous post. Checking the database for changes when such changes were made, it appeared that new entries would be added to prefix_user_preferences table, with 
name = message_provider_mod_forum_digests_enabled
value = popup
and so on.

Since this was not as straightforward as just changing a flag in a user preferences table, I thought it would be safer to do it via the Moodle UI instead of messing around with database changes. 

To automate the task of changing 200+ users' preferences, I once again took the help of SikuliX. Once again, I chose the simplistic method of making SikuliX scripts with hard-coded Location(x,y) values. Using the 'Run in slow motion' mode, which shows the Location with a red cross-hair, I used moveMouse commands to find the x and y co-ordinates of the points where I wanted the clicks to happen. Unfortunately, the points were not directly 1:1 corresponding to x and y co-ordinates of my 1920x1080 fullscreen capture - Locations based on those co-ordinates threw up errors that the co-ordinates did not correspond to any location (being out of range). 

With the Edge browser screen set to approx. 80% zoom in order to show all the elements we needed on the single screen, the two scripts to update the Moodle preferences pages were as follows.

Documents > notifpref.sikuli > notifpref.py

from time import sleep

sleeptime=0.5
sleeptillpageload=2.0
urlbar=Location(925,64)
test1=Location(1300,746)
test2=Location(1405,564)
webpref=Location(1180,746)
emailpref=Location(1300,746)
id=126

while (id<345): 
  click(urlbar)
  sleep(sleeptime)
  sleep(sleeptime)
  click(urlbar)
  #type(BACKSPACE 3 times)
  type("\b")
  type("\b")
  type("\b")
  type(str(id))
  #type ENTER
  type("\n")
  #mouseMove(test1)
  popup("waiting for no error")
  #sleep(sleeptime)
  #mouseMove(test2)
  mouseMove(webpref)
  #sleep(sleeptime)
  click(webpref)
  #mouseMove(emailpref)
  #sleep(sleeptime)
  click(emailpref)
  id=id+1
  #popup("waiting for no error")

Documents > forumprefs.sikuli > forumprefs.py

from time import sleep

sleeptime=0.5
sleeptillpageload=2.0
urlbar=Location(925,64)
subjectonly=Location(700,525)
test2=Location(505,704)
savebutton=Location(505,704)
emailtype=Location(700,446)
id=4362
url="https://ourserver.org/user/forum.php?id="

while (id>4135): 
  click(urlbar)
  #select all
  type("a",KeyModifier.CTRL)
  #type(paste the url already copied)
  type("v",KeyModifier.CTRL)
  type(str(id))
  #type ENTER
  type("\n")
  popup("waiting for no error")
  click(emailtype)
  sleep(sleeptime)
  click(subjectonly)
  id=id-1
  mouseMove(test2)
  click(savebutton)
  popup("waiting for no error")

  



  

No comments:

Post a Comment