text: 
  SELECT
    lectures.id, lectures.name,
    courses.name as course,
    lectures.type,
    COUNT(tracking.id) as views,
    CASE WHEN lectures.public THEN null ELSE IF(pub.sdate, pub.sdate, IF(cls.sdate, cls.sdate, pvt.sdate)) END AS sdate,
    CASE WHEN lectures.public THEN null ELSE IF(pub.edate, pub.sdate, IF(cls.edate, cls.edate, pvt.edate)) END AS edate,
    CASE WHEN lectures.public THEN 'public' ELSE IF(pub.category, 'public', IF(cls.category, 'group', 'student')) END AS category,
    CASE WHEN lectures.public THEN null ELSE IF(pub.stamp, pub.stamp, IF(cls.stamp, cls.stamp, pvt.stamp)) END AS stamp,
    crt_user.name as creator
  
  FROM
    lectures
      JOIN lecturers ON lecturers.id = $u[id] AND lecturers.status = 1 AND lecturers.history = 0
  
      JOIN users ON users.id = %d AND users.type = 'student' AND users.blocked = 0 AND users.deleted = 0
  
      JOIN courses ON courses.id = lecturers.course AND courses.deleted = 0
      LEFT JOIN enrollments ON
        enrollments.id = users.id AND enrollments.course = courses.id AND
        enrollments.status = 1 AND enrollments.history = 0 AND
        enrollments.sdate <= $stamp AND (enrollments.edate > $stamp OR enrollments.edate = 0)
  
      LEFT JOIN classes ON classes.course = courses.id AND classes.deleted = 0
      LEFT JOIN members ON members.id = users.id AND members.class = classes.id AND members.status = 1 AND members.history = 0
  
      LEFT JOIN students as pvt ON
        pvt.id = users.id AND pvt.category = 'student' AND pvt.iid = lectures.id AND pvt.type = 'lecture' AND
        pvt.status = 1 AND pvt.history = 0 AND pvt.sdate <= $stamp AND (pvt.edate > $stamp OR pvt.edate = 0)
  
      LEFT JOIN students as cls ON
        cls.id = classes.id AND cls.category = 'group' AND cls.iid = lectures.id AND cls.type = 'lecture' AND
        cls.status = 1 AND cls.history = 0 AND cls.sdate <= $stamp AND (cls.edate > $stamp OR cls.edate = 0)
  
      LEFT JOIN students as pub ON
        pub.category = 'public' AND pub.iid = lectures.id AND pub.type = 'lecture' AND
        pub.status = 1 AND pub.sdate <= $stamp AND (pub.edate > $stamp OR pub.edate = 0) AND pub.history = 0
  
      LEFT JOIN tracking ON tracking.id = users.id AND tracking.path = CONCAT('lectures/', CONCAT(lectures.type, CONCAT('s/', lectures.id)))
  
      LEFT JOIN users as crt_user ON crt_user.id = IF(pub.creator, pub.creator, IF(cls.creator, cls.creator, pvt.creator))
  
  WHERE
    (courses.public OR enrollments.id IS NOT NULL) AND
    (
      (
        pvt.allow = 1 OR
        (cls.allow = 1 AND pvt.allow IS NULL) OR
        (pub.allow = 1 AND cls.allow IS NULL AND pvt.allow IS NULL)
      ) OR
      (
        lectures.public = 1 AND
        pub.allow IS NULL AND cls.allow IS NULL AND pvt.allow IS NULL
      )
    ) AND
    lectures.course = courses.id AND lectures.deleted = 0
  
  GROUP BY
    lectures.id
  
  ORDER BY
    lectures.course,
    lectures.nu,
    lectures.name,
    lectures.creator,
    lectures.id;