General developer forum

 
 
Picture of Chris Kenniburg
Display List of Courses by User Outside of Moodle
Group Particularly helpful Moodlers

I have a strange request and I am very much a newbie to all this.  I am hoping to have a php page outside of moodle that can display a list of courses for a user of which they are in a teacher role.

I basically want to be able to add a username to the code and have it query our moodle database and return a list of courses that the username is a teacher role.

I would include a MySQL user with read only rights into the php script to connect to the moodle database.

My ultimate goal is to create a Wordpress Widget that our teachers can add to their blogs and then type in their username and have it display a list of courses to students and parents from their blog.

This is the first step to finding out if it is possible to do so. 

Can anyone help me on this?

Thank you kindly,
Chris

 
Average of ratings: -
Picture of Gregor McNish
Re: Display List of Courses by User Outside of Moodle
 

Look at this forum post https://moodle.org/mod/forum/discuss.php?d=202383

It explains the relevant data relationships. You'll just need to be looking for those with the teacher (or possibly manager or non-editing teacher etc) roles, rather than the student role.

 

 
Average of ratings: -
Picture of Chris Kenniburg
Re: Display List of Courses by User Outside of Moodle
Group Particularly helpful Moodlers

Thank you Gregor.  I saw in the link there was a way to get the user and course role. 

Here  is the code I have so far and this displays a listing of course categories which can be ingnored, but I think the structure is there for displaying a list of data that I want. 

I have included the queries from the link you posted at the bottom.  How would I add those queries to the code so that it would display a course name and link to the course for which a user is a teacher role.

<?php
$con = mysql_connect("localhost","moodle","moodle");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("moodle", $con);

echo "<p> The courses taught are: </p>";

///Display Course Categories
$query_catetories = mysql_query('SELECT cc.id, cc.parent, cc.name FROM mdl_course_categories cc ');
$categories = mysql_fetch_all($query_catetories);

$tmp_categories = array();

foreach ($categories AS $row) {

    $row['id'] = (int) $row['id'];
    $row['parent'] = (int) $row['parent'];
    if (!$tmp_categories[$row['parent']])
        $tmp_categories[$row['parent']] = array();
    $tmp_categories[$row['parent']][] = $row;
}

$course_catetories = buildNode($tmp_categories);

echo '<ul>';
foreach ($course_catetories as $course_catetory) {
    print_category_child($course_catetory);
}
echo '</ul>';

function print_category_child($category) {
    echo '<li>' . $category['name'];
    if (array_key_exists('children', $category)) {
        echo '<ul>';
        foreach ($category['children'] as $child) {
            print_category_child($child);
        }
        echo '</ul>';
    }
    echo '</li>';
}

function buildNode($inputArray, $parent = 0) {
    $return = array();
    foreach ($inputArray[$parent] AS $key => $row) {
        if (@$inputArray[$row['id']]) {
            $row['children'] = buildNode($inputArray, $row['id']);
        }
        $return[] = $row;
    }
    return $return;
}

function mysql_fetch_all($result) {
    $all = array();
    while ($all[] = mysql_fetch_assoc($result)) {

    }
    return array_filter($all);
}

/*  
SELECT
c.id AS courseid,
c.fullname,
u.username,
u.firstname,
u.lastname,
u.email
                                
FROM
mdl_role_assignments ra
JOIN mdl_user u ON u.id = ra.userid
JOIN mdl_role r ON r.id = ra.roleid
JOIN mdl_context cxt ON cxt.id = ra.contextid
JOIN mdl_course c ON c.id = cxt.instanceid

WHERE ra.userid = u.id
                                
AND ra.contextid = cxt.id
AND cxt.contextlevel =50
AND cxt.instanceid = c.id
AND  roleid = 5

ORDER BY c.fullname
*/


?>

 

Your help is much appreciated.

 
Average of ratings: -
Picture of Jitendra Gaur
Re: Display List of Courses by User Outside of Moodle
 

Hi Chris,

for your help i have written down code so quickly, you can test with dynamic usernmae i have puted here admin for just testing and also make note that roleid 5 is for student and roleid 3 for teachers.

$con = mysql_connect("localhost","moodle","moodle");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("moodle", $con);

echo "<p> The courses taught are: </p>";

$username = 'admin';

$username = mysql_real_escape_string($username);

$query_course_list = "SELECT c.id AS courseid, c.fullname
FROM mdl_role_assignments ra
JOIN mdl_user u ON u.id = ra.userid
JOIN mdl_role r ON r.id = ra.roleid
JOIN mdl_context cxt ON cxt.id = ra.contextid
JOIN mdl_course c ON c.id = cxt.instanceid

WHERE ra.userid = u.id

AND ra.contextid = cxt.id AND cxt.contextlevel =50 AND cxt.instanceid = c.id
AND roleid = 3 AND u.username = '$username'
ORDER BY c.fullname ";


$courses = mysql_query($query_course_list);


$output = '<ul>';

while ($course = mysql_fetch_object($courses)){

$output .= '<li>';
$output .= '<a href = "http://www.yourwebsite/course/view.php?id='.$course->courseid.'">';
$output .= $course->fullname;
$output .='</li>';
}
$output .= '</ul>';

echo $output;

 

Hope it will help your..

happy codding.

Thanks & Regards,

Jitendra Gaur

 
Average of ratings: -
Picture of Chris Kenniburg
Re: Display List of Courses by User Outside of Moodle
Group Particularly helpful Moodlers

THANK YOU Jitendra.  I am testing it out today!

 
Average of ratings: -
Picture of Chris Kenniburg
Re: Display List of Courses by User Outside of Moodle
Group Particularly helpful Moodlers

See my post here:  https://moodle.org/mod/forum/discuss.php?d=223107

I got the wordpress widget working using the code posted here.  Thank you Jitendra!

 
Average of ratings: -
Picture of Jitendra Gaur
Re: Display List of Courses by User Outside of Moodle
 

Thanks Chris, 

If any thing else i can do please let me know.

 
Average of ratings: -