Moodle: a few common queries

May 2, 2020

php command to see a lot of moodle global variables in memory

echo '< pre >';
#var_dump ( get_defined_vars($CFG) );
print_r($CFG);
echo '< /pre > ';

examples
$CFG->dbname
$CFG->release ex: “3.3.4 (Build: 20180115)”


echo '< pre >';
#var_dump ( get_defined_vars($USER) );
print_r($USER);
echo '< /pre > ';

example:
$USER->id
$USER->username

 

display all users who upper cased their name etc. …
or were too lazy to capitalize it etc.

SELECT u.id, u.firstname, u.lastname, u.email,
u.phone1, u.address, u.city,
FROM_UNIXTIME(u.lastaccess) AS lastaccess
FROM mdl_user AS u
WHERE UPPER(firstname) = BINARY firstname
OR UPPER(lastname) = BINARY lastname
OR LOWER(firstname) = BINARY firstname
OR LOWER(lastname) = BINARY lastname
ORDER BY id DESC

 

to see the most recent users logged in
example: in the past 15 minutes

      SELECT  log.id,   log.action,  log.target,  log.objectid, 
              log.userid,   log.courseid AS cid,  log.timecreated,   
              FROM_UNIXTIME(log.timecreated) AS logTimeCreated, 
              CONCAT(u.firstname, " ", u.lastname) AS uname  
        FROM  mdl_logstore_standard_log  AS log 
   LEFT JOIN  mdl_logstore_standard_log  AS log2 
          ON  (log.userid = log2.userid AND log.id < log2.id) 
        JOIN  mdl_user AS u  
          ON  (log.userid = u.id   AND 
               log.timecreated >= u.lastaccess)
       WHERE  log2.id IS NULL 
         AND  log.userid > 0 
         AND  log.timecreated > 
                  UNIX_TIMESTAMP(CURDATE() - INTERVAL 15 MINUTE )
    ORDER BY  log.id  DESC 

ex: to see enough log records to confirm the results above

       SELECT  log.id,  action,  target,  objectid,  
               userid,  log.courseid AS cid, 
               FROM_UNIXTIME(log.timecreated) AS logTimeCreated,    
               FROM_UNIXTIME(u.lastaccess) AS lastaccess,  
               CONCAT(u.firstname, ' ', u.lastname) AS Uname  
         FROM  mdl_logstore_standard_log  AS log 
         JOIN  mdl_user AS u 
           ON  (log.userid = u.id   AND 
                log.timecreated >= u.lastaccess)
        WHERE  log.timecreated > 
                   UNIX_TIMESTAMP(CURDATE() - INTERVAL 15 MINUTE )
          AND  log.userid > 0 
     ORDER BY  log.id  DESC 

Comments are closed.

We try to post all comments within 1 business day