public static function set_favourite_courses(
        array $courses
    ) {
        global $USER;

        $params = self::validate_parameters(self::set_favourite_courses_parameters(),
            array(
                'courses' => $courses
            )
        );

        $warnings = [];

        $ufservice = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($USER->id));

        foreach ($params['courses'] as $course) {

            $warning = [];

            $favouriteexists = $ufservice->favourite_exists('core_course', 'courses', $course['id'],
                    \context_course::instance($course['id']));

            if ($course['favourite']) {
                if (!$favouriteexists) {
                    try {
                        $ufservice->create_favourite('core_course', 'courses', $course['id'],
                                \context_course::instance($course['id']));
                    } catch (Exception $e) {
                        $warning['courseid'] = $course['id'];
                        if ($e instanceof moodle_exception) {
                            $warning['warningcode'] = $e->errorcode;
                        } else {
                            $warning['warningcode'] = $e->getCode();
                        }
                        $warning['message'] = $e->getMessage();
                        $warnings[] = $warning;
                        $warnings[] = $warning;
                    }
                } else {
                    $warning['courseid'] = $course['id'];
                    $warning['warningcode'] = 'coursealreadyfavourited';
                    $warning['message'] = 'Course already favourited';
                    $warnings[] = $warning;
                }
            } else {
                if ($favouriteexists) {
                    try {
                        $ufservice->delete_favourite('core_course', 'courses', $course['id'],
                                \context_course::instance($course['id']));
                    } catch (Exception $e) {
                        $warning['courseid'] = $course['id'];
                        if ($e instanceof moodle_exception) {
                            $warning['warningcode'] = $e->errorcode;
                        } else {
                            $warning['warningcode'] = $e->getCode();
                        }
                        $warning['message'] = $e->getMessage();
                        $warnings[] = $warning;
                        $warnings[] = $warning;
                    }
                } else {
                    $warning['courseid'] = $course['id'];
                    $warning['warningcode'] = 'cannotdeletefavourite';
                    $warning['message'] = 'Could not delete favourite status for course';
                    $warnings[] = $warning;
                }
            }
        }

        return [
            'warnings' => $warnings
        ];
    }
public static function set_favourite_courses(
        array $courses
    ) {
        global $USER;

        $params = self::validate_parameters(self::set_favourite_courses_parameters(),
            array(
                'courses' => $courses
            )
        );

        $warnings = [];

        $ufservice = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($USER->id));

        foreach ($params['courses'] as $course) {

            $warning = [];

            $favouriteexists = $ufservice->favourite_exists('core_course', 'courses', $course['id'],
                    \context_course::instance($course['id']));

            if ($course['favourite']) {
                if (!$favouriteexists) {
                    try {
                        $ufservice->create_favourite('core_course', 'courses', $course['id'],
                                \context_course::instance($course['id']));
                    } catch (Exception $e) {
                        $warning['courseid'] = $course['id'];
                        if ($e instanceof moodle_exception) {
                            $warning['warningcode'] = $e->errorcode;
                        } else {
                            $warning['warningcode'] = $e->getCode();
                        }
                        $warning['message'] = $e->getMessage();
                        $warnings[] = $warning;
                        $warnings[] = $warning;
                    }
                } else {
                    $warning['courseid'] = $course['id'];
                    $warning['warningcode'] = 'coursealreadyfavourited';
                    $warning['message'] = 'Course already favourited';
                    $warnings[] = $warning;
                }
            } else {
                if ($favouriteexists) {
                    try {
                        $ufservice->delete_favourite('core_course', 'courses', $course['id'],
                                \context_course::instance($course['id']));
                    } catch (Exception $e) {
                        $warning['courseid'] = $course['id'];
                        if ($e instanceof moodle_exception) {
                            $warning['warningcode'] = $e->errorcode;
                        } else {
                            $warning['warningcode'] = $e->getCode();
                        }
                        $warning['message'] = $e->getMessage();
                        $warnings[] = $warning;
                        $warnings[] = $warning;
                    }
                } else {
                    $warning['courseid'] = $course['id'];
                    $warning['warningcode'] = 'cannotdeletefavourite';
                    $warning['message'] = 'Could not delete favourite status for course';
                    $warnings[] = $warning;
                }
            }
        }

        return [
            'warnings' => $warnings
        ];
    }
public static function set_favourite_courses ( array $ courses ) { global $ USER ; $ params = self :: validate_parameters ( self :: set_favourite_courses_parameters ( ) , array ( 'courses' => $ courses ) ) ; $ warnings = [ ] ; $ ufservice = \ core_favourites \ service_factory :: get_service_for_user_context ( \ context_user :: instance ( $ USER -> id ) ) ; foreach ( $ params [ 'courses' ] as $ course ) { $ warning = [ ] ; $ favouriteexists = $ ufservice -> favourite_exists ( 'core_course' , 'courses' , $ course [ 'id' ] , \ context_course :: instance ( $ course [ 'id' ] ) ) ; if ( $ course [ 'favourite' ] ) { if ( ! $ favouriteexists ) { try { $ ufservice -> create_favourite ( 'core_course' , 'courses' , $ course [ 'id' ] , \ context_course :: instance ( $ course [ 'id' ] ) ) ; } catch ( Exception $ e ) { $ warning [ 'courseid' ] = $ course [ 'id' ] ; if ( $ e instanceof moodle_exception ) { $ warning [ 'warningcode' ] = $ e -> errorcode ; } else { $ warning [ 'warningcode' ] = $ e -> getCode ( ) ; } $ warning [ 'message' ] = $ e -> getMessage ( ) ; $ warnings [ ] = $ warning ; $ warnings [ ] = $ warning ; } } else { $ warning [ 'courseid' ] = $ course [ 'id' ] ; $ warning [ 'warningcode' ] = 'coursealreadyfavourited' ; $ warning [ 'message' ] = 'Course already favourited' ; $ warnings [ ] = $ warning ; } } else { if ( $ favouriteexists ) { try { $ ufservice -> delete_favourite ( 'core_course' , 'courses' , $ course [ 'id' ] , \ context_course :: instance ( $ course [ 'id' ] ) ) ; } catch ( Exception $ e ) { $ warning [ 'courseid' ] = $ course [ 'id' ] ; if ( $ e instanceof moodle_exception ) { $ warning [ 'warningcode' ] = $ e -> errorcode ; } else { $ warning [ 'warningcode' ] = $ e -> getCode ( ) ; } $ warning [ 'message' ] = $ e -> getMessage ( ) ; $ warnings [ ] = $ warning ; $ warnings [ ] = $ warning ; } } else { $ warning [ 'courseid' ] = $ course [ 'id' ] ; $ warning [ 'warningcode' ] = 'cannotdeletefavourite' ; $ warning [ 'message' ] = 'Could not delete favourite status for course' ; $ warnings [ ] = $ warning ; } } } return [ 'warnings' => $ warnings ] ; }
Set the course favourite status for an array of courses.

@param  array $courses List with course id's and favourite status.
@return array Array with an array of favourite courses.
Set the course favourite status for an array of courses .