how to get courses created by particular user in moodle 3 - moodle

Currently I'm working on moodle-3 plug-in development,and I have a user Id who is a course creator and i want to get all courses created by that user,is their any function, I can use or if you can help me with names of tables where I can fetch this data.I tried table called standard log where I can get all events is that only way to do it.

Not sure if this will work but you could try searching for courses that have the coursecreator role assigned.
SELECT r.shortname, u.username, c.fullname AS coursename
FROM mdl_role r
JOIN mdl_role_assignments ra ON ra.roleid = r.id
JOIN mdl_context ctx ON ctx.id = ra.contextid AND ctx.contextlevel = 50
JOIN mdl_course c ON c.id = ctx.instanceid
JOIN mdl_user u ON u.id = ra.userid
WHERE r.shortname = 'coursecreator'

Related

Incorrect number of query parameters in Moodle *How to get the external links from course page

My Dear
I tried to run the following ad-hoc database query in Moodle but i got it error
there any way to fix it , thanks
*My target to find all the external links (URL) from course page
SELECT
concat('<a target="_new" href=%%WWWROOT%%/course/view.php?id=',
c.id, '">', c.fullname, '</a>') AS Course
,c.shortname,r.name
,(
SELECT CONCAT(u.firstname,' ', u.lastname) AS Teacher
FROM prefix_role_assignments AS ra
JOIN prefix_context AS ctx ON ra.contextid = ctx.id
JOIN prefix_user AS u ON u.id = ra.userid
WHERE ra.roleid = 3
AND ctx.instanceid = c.id
LIMIT 1
) AS Teacher
,concat('<a target="_new" href="%%WWWROOT%%/mod/resource/view.php?id=',
r.id, '">', r.name, '</a>') AS Resource
FROM prefix_resource AS r
JOIN prefix_course AS c ON r.course = c.id
WHERE r.reference LIKE 'https://stackoverflow.com/%'
Error message :
" Error when executing the query: ERROR: Incorrect number of query
parameters. Expected 2, got 0"
When executing SQL statements in Moodle, any '?' characters are used to indicate placeholders for parameters that need to be provided along with the query.
From the style of your query, I'm assuming you're using report_customsql or block_configurablereports for your query, so you won't have the option of providing such parameters.
If you look at the documentation here: https://docs.moodle.org/en/Custom_SQL_queries_report you will see that the workaround for this is to replace any '?' characters in your query with '%%Q%%'.
So the fixed query should look like:
SELECT concat('<a target="_new" href="%%WWWROOT%%/course/view.php%%Q%%id=',c.id,'">',c.fullname,'</a>') AS Course,
c.shortname,r.name, (SELECT CONCAT(u.firstname,' ', u.lastname) AS Teacher
FROM prefix_role_assignments AS ra
JOIN prefix_context AS ctx ON ra.contextid = ctx.id
JOIN prefix_user AS u ON u.id = ra.userid
WHERE ra.roleid = 3 AND ctx.instanceid = c.id LIMIT 1) AS Teacher,
concat('<a target="_new" href="%%WWWROOT%%/mod/resource/view.php%%Q%%id=',r.id,'">',r.name,'</a>') AS Resource
FROM prefix_resource AS r
JOIN prefix_course AS c ON r.course = c.id
WHERE r.reference LIKE 'https://stackoverflow.com/%'

Randomize students in Moodle

I have a Moodle classroom of about 60 students. All the students have to give a presentation. Is it possible to randomize the students in Moodle to decide the order of presentation?
I can't think of anything in Moodle that will do that.
But you could use SQL - replace xxx with the course id and RANDOM() with the relevant random function for your database
SELECT RANDOM() AS randid, u.id AS userid, u.firstname, u.lastname, u.email
FROM mdl_user u
JOIN (
SELECT DISTINCT ue.userid, e.courseid
FROM mdl_user_enrolments ue
JOIN mdl_enrol e ON e.id = ue.enrolid
) ue ON ue.userid = u.id
JOIN mdl_course c ON c.id = ue.courseid
WHERE c.id = xxx
AND u.deleted = 0
AND u.suspended = 0
ORDER BY 1

Moodle Userrole Courseid

i hope you have nice easterdays....
I need help by a special moodle-problem
I want to create a Table of Moodle-Courses
This works :-)
Now i want to check out the name of the Course-Users.... This works :-)
Now comes the problem:
How can i check out which role the course-user have in the special course?
Special: I need an SQL-Statement which gives me the User-Role (1,2,3...) form the User with the ID = 2 (User-ID) who is enroled in the course which the Id 50 (Course-ID).
Nice Regards
Try this query.
SELECT c.fullname as 'Course Name', concat(u.firstname ,' ',u.lastname) AS 'User Name',
r.shortname as 'Role Name', r.id as 'Role ID'
FROM mdl_role_assignments AS ra
JOIN mdl_role AS r ON r.id = ra.roleid
JOIN mdl_user AS u ON u.id = ra.userid
JOIN mdl_context AS ctx ON (ctx.id = ra.contextid AND ctx.contextlevel = 50)
JOIN mdl_course AS c ON ctx.instanceid = c.id
WHERE u.id = 2 # For specific user ID
AND c.id = 50 # For a specific course, remove it if you need to see all courses.
There's no need to use any sql scripts as Moodle already provides a method for it:
$roles = get_user_roles($context, $USER->id)
Here is the moodle forum discussion on the above: here

MOODLE - i need a query that returns latest enrolment date in course

this query is not returning correct latest_enrolment date. whenever i enrol a user in course, it doesnot updates enrolment date in database.. can anyone help?
SELECT TRIM(c.id) course_id,TRIM(c.fullname) course_fullname,FROM_UNIXTIME(u.timecreated) as registration ,FROM_UNIXTIME( ra.timemodified ) latest_enrolment_date,COUNT( * ) AS enrol_count
FROM mdl_user u
INNER JOIN mdl_role_assignments ra ON ra.userid = u.id
INNER JOIN mdl_context ct ON ct.id = ra.contextid
INNER JOIN mdl_course c ON c.id = ct.instanceid
INNER JOIN mdl_role r ON r.id = ra.roleid
INNER JOIN mdl_course_categories cc ON cc.id = c.category
WHERE r.id =5 GROUP BY c.id
Something like this
SELECT MAX(ue.timecreated) AS latest_enrolment_date
FROM mdl_enrol e
JOIN mdl_user_enrolments ue ON ue.enrolid = e.id
WHERE e.courseid = xx

user information and the course name with time spend in a course

Can anybody help me out with the code to get the user's firstname, lastname, the course name and the time spend by the user on that course in moodle 2.6? Using configurable reports doesnot give me the exact solution.
You can use the following sql to find the course participants.
SELECT u.id, u.username, u.firstname, u.lastname FROM mdl_user u JOIN (SELECT DISTINCT eu1_u.id FROM mdl_user eu1_u JOIN mdl_user_enrolments eu1_ue ON eu1_ue.userid = eu1_u.id JOIN mdl_enrol eu1_e ON (eu1_e.id = eu1_ue.enrolid AND eu1_e.courseid = 4) WHERE eu1_u.deleted = 0 AND eu1_u.id <> 1 ) e ON e.id = u.id LEFT JOIN mdl_user_lastaccess ul ON (ul.userid = u.id AND ul.courseid = 4) LEFT JOIN mdl_context ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = 30) ORDER BY u.lastaccess DESC;
There are two ways to find the time spend in a course:
If course completion is enabled and the user has completed the course, then the time spend in the course will be the difference between the timestarted and timecompleted fields in mdl_course_completions table.
You can also calculate the time spent in a course from moodle logs.
hope this helps.