<?php
// Generates a CSV (comma seperated values) file for inactive Moodle sites with a specific date cut-off to determine inactivity with data DaysInactive,Instructor names and e-mails, with duplicate courses for multiple course instructors
// - Elvedin Trnjanin

require_once("config.php");
$courses get_records_sql("select course, datediff(curdate(),from_unixtime(max(time))) as inactivity from {$CFG->prefix}log where course<>0 group by course order by inactivity desc;"); // this neat trick is from some admin plugin, sorry forgot the name
// the query returns inactivity in days, datediff(exp1,exp2) = exp1-exp2
header("Content-Disposition: attachment; filename=InactiveCourseReport.csv"); // send to browser the data generated below with filename=
header("Pragma: no-cache");
header("Expires: 0");
echo 
"DaysInactive, URL, Instructor, email\n"// column names
foreach ($courses as $course) {
        @
ini_set("max_execution_time","1000"); // might be a big mdl_log table
        
if ($course->inactivity 182) { // 182 days ~ 6 months
                
continue;
        }
        
$instructors get_records_sql("select userid from {$CFG->prefix}role_assignments where roleid=3 and contextid=(select id from {$CFG->prefix}context where contextlevel=50 and instanceid=$course->course)");
        foreach (
$instructors as $instructor) {
                
$userdetails get_records_sql("select firstname, lastname, email from {$CFG->prefix}user where id=$instructor->userid");
                foreach (
$userdetails as $userdetail) {
                echo 
"$course->course,$CFG->wwwroot/course/view.php?id=$course->course, $userdetail->lastname $userdetail->firstname, $userdetail->email\n";
                }
        }
  }

?>
~