在moodle中选择所有文件及其课程路径

时间:2013-04-10 23:19:23

标签: sql moodle

我正在尝试编写一个查询来获取Moodle中课程文件的名称和路径,但似乎我永远无法做到这一点。以下是我现在所做的事情,对此的任何帮助都非常感谢:)

SELECT SOURCE file,
              course,
              contenthash path
FROM mdl_files
INNER JOIN mdl_context ON mdl_files.contextid = mdl_context.id
JOIN mdl_resource ON instanceid = mdl_resource.id
WHERE course=
    (SELECT id
     FROM mdl_course
     WHERE fullname="XXXX")

2 个答案:

答案 0 :(得分:5)

在任何Moodle表中都没有名为“file”的列。你实际上是指“mdl_files.filename”列吗?

若是,请尝试此查询:

SELECT
    mdl_files.source,
    mdl_files.filename,
    mdl_files.contenthash,
    mdl_context.path,
    mdl_resource.course,
    mdl_resource.name,
    mdl_course.fullname, 
    mdl_course.shortname
FROM
    mdl_files
INNER JOIN mdl_context ON mdl_files.contextid = mdl_context.id
INNER JOIN mdl_resource ON mdl_context.instanceid = mdl_resource.id
INNER JOIN mdl_course ON mdl_resource.course = mdl_course.id
WHERE (mdl_course.fullname = 'XXXX')

这就是你想要的吗?

答案 1 :(得分:3)

试试这个......只需将下面的“ 58 ”替换为相应的课程编号

即可
SELECT cm.id, cm.course, cm.module, mdl.name AS type,
       CASE
            WHEN mf.name IS NOT NULL THEN mf.name
            WHEN mb.name IS NOT NULL THEN mb.name
            WHEN mr.name IS NOT NULL THEN mr.name
            WHEN mu.name IS NOT NULL THEN mu.name
            WHEN mq.name IS NOT NULL THEN mq.name
            WHEN mp.name IS NOT NULL THEN mp.name
            WHEN ml.name IS NOT NULL THEN ml.name
            ELSE NULL
       END AS activityname,
       CASE
            WHEN mf.name IS NOT NULL THEN CONCAT('/mod/forum/view.php?id=', cm.id)
            WHEN mb.name IS NOT NULL THEN CONCAT('/mod/book/view.php?id=', cm.id)
            WHEN mr.name IS NOT NULL THEN CONCAT('/mod/resource/view.php?id=', cm.id)
            WHEN mu.name IS NOT NULL THEN CONCAT('/mod/url/view.php?id=', cm.id)
            WHEN mq.name IS NOT NULL THEN CONCAT('/mod/quiz/view.php?id=', cm.id)
            WHEN mp.name IS NOT NULL THEN CONCAT('/mod/page/view.php?id=', cm.id)
            WHEN ml.name IS NOT NULL THEN CONCAT('/mod/lesson/view.php?id=', cm.id)
            ELSE NULL
       END AS linkurl, f.id AS fileid, f.filepath, f.filename, CONCAT('/filedir/', SUBSTRING(f.contenthash, 1, 2), '/', SUBSTRING(f.contenthash, 3, 2), '/', f.contenthash) AS filesystempath, f.userid AS fileuserid, f.filesize, f.mimetype, f.author AS fileauthor, f.timecreated, f.timemodified
FROM mdl_course_modules AS cm
INNER JOIN mdl_context AS ctx ON ctx.contextlevel = 70 AND ctx.instanceid = cm.id
INNER JOIN mdl_modules AS mdl ON cm.module = mdl.id
LEFT JOIN mdl_forum AS mf ON mdl.name = 'forum' AND cm.instance = mf.id
LEFT JOIN mdl_book AS mb ON mdl.name = 'book' AND cm.instance = mb.id
LEFT JOIN mdl_resource AS mr ON mdl.name = 'resource' AND cm.instance = mr.id
LEFT JOIN mdl_url AS mu ON mdl.name = 'url' AND cm.instance = mu.id
LEFT JOIN mdl_quiz AS mq ON mdl.name = 'quiz' AND cm.instance = mq.id
LEFT JOIN mdl_page AS mp ON mdl.name = 'page' AND cm.instance = mp.id
LEFT JOIN mdl_lesson AS ml ON mdl.name = 'lesson' AND cm.instance = ml.id
LEFT JOIN mdl_files AS f ON f.contextid = ctx.id
WHERE cm.course = 58
AND mdl.name = 'resource'
AND ((f.mimetype = 'application/pdf') OR (f.id IS NULL))

注意:最初受到DarkoMiletić解决方案的启发:https://moodle.org/mod/forum/discuss.php?d=153059#p1166404