2

Because of an excellent answer here in the forum, I was able to find out via sql which Moodle courses a certain Moodle user is enrolled in.

I have now used this help in a PHP script, which also works extremely well.

//Jetzt erfolgt die Überprüfung und Ausgabe, in welchen Kursen der Benutzer eingeschrieben ist. Die User-Identifikation erfolgt anhzand der user_id
                $sql = mysqli_query($db,"
                                    SELECT DISTINCT CONCAT(u.firstname, ' ' ,u.lastname) AS Student, course.fullname AS Course
                                    FROM mdl_course AS course 
                                    JOIN mdl_enrol AS en ON en.courseid = course.id
                                    JOIN mdl_user_enrolments AS ue ON ue.enrolid = en.id
                                    JOIN mdl_user AS u ON ue.userid = u.id
                                    WHERE u.id = '$user_id[$i]'"
                                    );
            
                
                $sql_durchfuehren=mysqli_fetch_row($sql);                   
                //echo is_array($sql_durchfuehren) ? "Ja-Array" : "Nein kein Array";
                if (is_array($sql_durchfuehren))
                {   
                    echo "Der Benutzer ist in die folgenden Kurse eingeschrieben: <br>";                
                    while($row = mysqli_fetch_array($sql))
                    {
                $course_name = $row['Course'];
                    echo $course_name." <br>";      
                    }   
                }
                else 
                {
                    echo "<br>Der Benutzer ".$first_name[$i]." ".$last_name[$i]." ist in keinen Kurs eingeschrieben!<br>";
                }

With the script I now output the courses in which the respective user is enrolled.

Challenge: Now I would like to directly link the courses in which the user is enrolled.

So: User Tim Müller is enrolled in the Business English course. By clicking on "Business English" you should land directly in the course.

How can I achieve this?

Thanks and best regards You're great!!! D-T

D-T
  • 45
  • 3
  • Well, you need to make the PHP echo a HTML hyperlink which points to the URL of that course. Beyond that we can't really help you because we don't know the URL format. Also it's unclear if you know how to make a hyperlink (but if not, you can look up the syntax in any HTML guide). – ADyson Dec 14 '20 at 16:00

2 Answers2

3

If this is Moodle code, then there are a number of issues with what you've already written - you really should be using the built-in database API to fire off SQL queries, rather than the mysqli_* functions. This is partly from a security perspective (for example, your first SQL query looks like it is at risk of an SQL injection attack, unless you are doing some work to clean $user_id[$i] in advance), but it also helps your code to support all possible database engines supported by Moodle itself.

The first thing you will need for the URL is the courseid, so I suggest you update your SQL SELECT statement to add:

 ..., course.fullname AS Course, course.id AS courseid

With regards to generating a URL to a course, if you're writing Moodle code, then the "correct" solution is to use this code:

$format = course_get_format($row['courseid']);
$url = $format->get_view_url(null);
echo html_writer::link($url, $course_name);

A slight short-cut to this (which will work for every course format I've ever come across in Moodle) is:

$url = new moodle_url('/course/view.php', ['id' => $row['courseid']]);
echo html_writer::link($url, $course_name);

Or if you're not using Moodle code at all, but just writing something that directly pulls data from the Moodle database:

$siteurl = 'put the URL of your site here, without the ending /';
$url = $siteurl.'/course/view.php?id='.$row['courseid'];
echo '<a href="'.$url.'">'.$course_name.'</a>';
davosmith
  • 6,037
  • 2
  • 14
  • 23
  • Hey you are great. Tomorrow i will test the code. I hope i can ask again, if there are some questions... ... thx a lot! – D-T Dec 16 '20 at 17:35
1

I've been trying for something similar (but trying to to get a specific course) and found this:

 $courses = enrol_get_my_courses();

It gets all of the user's presently enrolled courses and outputs the information in an array.

 print_object($courses);

Output:

    Array
(
    [4] => stdClass Object
        (
            [id] => 4
            [category] => 1
            [sortorder] => 10001
            [shortname] => course3
            [fullname] => Course 3
            [idnumber] => 
            [startdate] => 1234567890
            [visible] => 1
            [groupmode] => 0
            [groupmodeforce] => 0
            [cacherev] => 1234567890
            [showactivitydates] => 1
            [showcompletionconditions] => 1
        )

    [2] => stdClass Object
        (
            [id] => 2
            [category] => 1
            [sortorder] => 10003
            [shortname] => MyCourse
            [fullname] => My Course Long Name
            [idnumber] => 
            [startdate] => 1234567890
            [visible] => 1
            [groupmode] => 0
            [groupmodeforce] => 0
            [cacherev] => 1234567890
            [showactivitydates] => 1
            [showcompletionconditions] => 1
        )

)

From there, you can do a foreach loop using $course->id and $course->shortname or $course->fullname to populate the code you're using to generate the url for each course as it loops through.

  foreach($courses as $course){ 
      /**
       *your preferred url code style goes here 
       *with $course->id $course->shortname or $course->fullname
       *populating the url dynamically.
       */
   }
Kurt_eh
  • 49
  • 2
  • 10
  • 1
    Please do not use images for code. See [ask], [answer] and "[Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/a/285557/128421)" "[Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812)" – the Tin Man Jan 21 '22 at 23:50
  • Done, thanks for the advice. I'm still new to Stackoverflow! – Kurt_eh Jan 22 '22 at 02:14