• Resolved liqipotato

    (@liqipotato)


    Hi, I’m trying to show a dropdown menu, showing the available lessons in a given course. Is there a function in the PHP files that would return me a list of available lessons if I were to put in a course id? The closest I could get is get_existing_posts function in /lifterlms/includes/admin/class.llms.admin.builder.php but this function is a private static function. I tried changing it to a public function but it doesn’t seem to work. Any advice on this?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter liqipotato

    (@liqipotato)

    Hey Thomas,

    I’ve also tried that function but it doesn’t seem to work. I’m not sure if I’m using it correctly but here’s my code. My objective here is to append the lesson titles into a list.

    
    $courselist = [];
    if ($student && $student->is_enrolled(6895)){
        $courseid = 6895;
        $lessonlist = $courseid->get_lessons( $return = 'lessons' );
    	foreach ($lessonlist as $lesson){
    	    array_push($courselist, $lesson);
    	}
    }

    Correct me if I’m wrong but get_lessons is a public function so a course object is needed for get_lessons() to work. So because of that, I got an error saying “Fatal error: Uncaught Error: Call to a member function get_lessons() on int in /home/customer/www….” Let me know how I can solve this, thank you!

    • This reply was modified 3 years, 4 months ago by liqipotato.

    @liqipotato,

    Try this:

    
    $course_id = 123;
    $course = llms_get_post( $course_id ); // Returns an LLMS_Course object.
    if ( ! $course ) {
      // Course ID is not a valid course id.
    } else {
      // Use the course object to do stuff.
    }
    
    Thread Starter liqipotato

    (@liqipotato)

    Thank you for assisting, but it still doesn’t work. Here’s my code.

    if ($student && $student->is_enrolled(5580)){  
    	$course = llms_get_post(5580); //returns an LLMS_Course object
    	$lessonlist = $course->get_lessons( $return = 'lessons' ); 
    	foreach ($lessonlist as $lesson){
    		array_push($courselist, $lesson);
    	}
    }

    I’m assuming that $course->get_lessons() returns a list of lesson objects? Because the new error I’m seeing is “Fatal error: Uncaught Error: Object of class LLMS_Lesson could not be converted to string in /home/customer/www/……” Any idea how to get lesson titles from the lesson object?

    @liqipotato

    $lesson->get( 'title' )

    You can access any lesson properties (as defined on the object, see the docs linked below) using the generic get() method.

    Other (mostly computed) information (such as relationships) are accessible via the class methods.

    LLMS_Lesson docs: https://developer.lifterlms.com/reference/classes/llms_lesson/

    Best!

    Also this is (technically) wrong:

    $lessonlist = $course->get_lessons( $return = 'lessons' );

    Should be:

    $lessonlist = $course->get_lessons( 'lessons' );

    • This reply was modified 3 years, 4 months ago by Thomas Patrick Levy. Reason: added "technically"
    Thread Starter liqipotato

    (@liqipotato)

    I see, I was trying to find something like a get_title method in the lesson class file but couldn’t find it. It works now so thank you for your assistance! Wishing you a great weekend ahead ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Getting a list of lessons from a function in backend’ is closed to new replies.