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

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/%'

Related

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

PostgreSQL Inner Join Where Get IDs of 3 Products

I have these 5 tables which is sale_order directly connected to sale_order_line that has the products of the order of course which is connected to product_product that is connected to product_template where a table called mrp_bom can connect to product template and a table connected to it which is bom_line. I am trying to Output the products that contain the word 836g
Here is what I have so far:
Select so.name,
pt.name,
sol.name
From sale_order so
Inner Join sale_order_line sol
On so.id = sol.order_id
Inner Join product_template pt
On sol.product_id = pt.id
Inner Join mrp_bom bom
On pt.id = bom.product_tmpl_id
Inner Join mrp_bom_line boml
On bom.id = boml.bom_id
Where boml.product_id = (Select id From product_template Where name Like '%836g%'
Order By so.name
This code outputs an error because I have 3 items with 836g. I tried changing the = in the condition into IN so it will grab all the ids it will return. But still it's giving me an error. This is the best I have so far, I have tried so many times, but I can't figure it out.
I have the feeling that something along these lines is what you actually want:
SELECT so.name,
pt.name,
sol.name
FROM sale_order so
INNER JOIN sale_order_line sol
ON so.id = sol.order_id
INNER JOIN product_template pt
ON sol.product_id = pt.id
INNER JOIN mrp_bom bom
ON pt.id = bom.product_tmpl_id
INNER JOIN mrp_bom_line boml
ON bom.id = boml.bom_id
WHERE pt.name LIKE '%836g%' -- just add a WHERE condition directly in your query
ORDER BY so.name
OMG! I just had the answer. I don't think this will help anyone but i'll post the answer i have
SELECT so.name,
pt.name PT,
sol.name SOL,
(Select pp1.name_template From product_product pp1 Where boml.product_id = pp1.id) BOML,
boml.product_id
FROM sale_order so
INNER JOIN sale_order_line sol
ON so.id = sol.order_id
INNER JOIN product_template pt
ON sol.product_id = pt.id
INNER JOIN mrp_bom bom
ON pt.id = bom.product_tmpl_id
INNER JOIN mrp_bom_line boml
ON bom.id = boml.bom_id
WHERE (Select pp1.name_template From product_product pp1 Where boml.product_id = pp1.id) LIKE '%836g%'
ORDER BY so.name
Thank you all for the effort of helping me! Great community we got here! :D

Is this Correlated SubQuery Correct

I am trying to wrap my head around sub-queries (correlated). Although the book explains (MS SQL Server 2012) it I'm still a little confused. Using three tables, Orders, Products and OrderDetails I want to find the sum of all products that were shipped to USA. I came up with the following query but do not quite understand how I got there except the correlation is referencing the 'where' in the outer query. Can someone correct my work and offer better explanation than book?
SELECT p.productid, p.productname, SUM(qty * od.unitprice) AS TotalAmount
FROM Production.Products AS p
JOIN Sales.OrderDetails AS od
ON p.productid = od.productid
WHERE N'USA' IN
(SELECT o.shipcountry
FROM Sales.Orders AS o
JOIN Sales.OrderDetails AS od
ON o.orderid = od.orderid
WHERE shipcountry = N'USA')
GROUP BY p.productid, p.productname;
SELECT
p.ProductID
, p.ProductName
, SUM(qty * od.UnitPrice) as [Total_Amount]
FROM
Production.Products productid
JOIN Sales.OrderDetails as od on p.productid = od.product
JOIN SalesOrders as o on o.OrderID = od.OrderID
WHERE
o.Shipcountry in ('USA')
Group BY
p.ProductID,
P.ProductName
The following should work - you technically dont require the sub query. Will only bog down the query.

how to get courses created by particular user in moodle 3

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'

Subquery in JPA

I am trying to write the following SQL query as a JPA query. The SQL query works (MySQL database) but I don't know how to translate it. I get a error token right after the first FROM. There are probably other errors here too because I was not able to find any guides on how to do sub-queries in the from part, aliasing and so on.
SQL query
SELECT tbl.* from (
SELECT u.*, COUNT(u.id) AS question_count FROM app_user AS u
INNER JOIN question AS q ON u.id = q.user_id GROUP BY u.id
) AS tbl ORDER BY tbl.question_count DESC LIMIT 10;
JPA query:
SELECT tbl FROM (SELECT u, COUNT(u.id) question_count FROM User u
INNER JOIN u.questions q ON u.id = q.user_id GROUP BY u.id) tbl
ORDER BY tbl.question_count LIMIT 10")
I can't test this with anything right now, but something along the lines of:
final String queryStr = "SELECT u, COUNT(u.id) FROM User u, Questions q WHERE u.id = q.user_id GROUP BY u.id ORDER BY COUNT(u.id) DESC";
Query query = em().createQuery(queryStr);
query.setMaxResults(10);
List<Object[]> results = query.getResultList(); //Index [0] will contain the User-object, [1] will contain Long with result of COUNT(u.id)