text: 
  SELECT
    users.id
  
  FROM
    users
      JOIN classes ON classes.deleted = 0
      JOIN members ON members.id = users.id AND members.class = classes.id AND members.status = 1 AND members.history = 0
      JOIN courses ON courses.id = classes.course AND courses.deleted = 0
      LEFT JOIN enrollments ON
        enrollments.id = users.id AND enrollments.course = courses.id AND
        enrollments.status = 1 AND enrollments.allow = 1 AND enrollments.history = 0 AND
        enrollments.sdate <= $stamp AND (enrollments.edate > $stamp OR enrollments.edate = 0)
  
  WHERE
    (enrollments.allow = 1 OR (courses.public = 1 AND enrollments.allow IS NULL)) AND
    users.type = 'student' AND users.blocked = 0 AND users.deleted = 0
  
  GROUP BY
    users.id;