Skip to content

Commit

Permalink
Merge pull request #7231 from Automattic/add/setting-not-allowing-sel…
Browse files Browse the repository at this point in the history
…f-enrollment

Add a course setting to not allow self-enrollment
  • Loading branch information
renatho authored Oct 31, 2023
2 parents 208915b + b58d51a commit 67e35a9
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 27 deletions.
19 changes: 19 additions & 0 deletions assets/js/admin/course-general-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const CourseGeneralSidebar = () => {
const featured = meta._course_featured;
const prerequisite = meta._course_prerequisite;
const notification = meta.disable_notification;
const selfEnrollmentNotAllowed = meta._sensei_self_enrollment_not_allowed;
const openAccess = meta._open_access;

useEffect( () =>
Expand Down Expand Up @@ -171,6 +172,24 @@ const CourseGeneralSidebar = () => {
/>
) : null }

<HorizontalRule />

<h3>{ __( 'Enrollment', 'sensei-lms' ) }</h3>
<CheckboxControl
label={ __( "Don't allow self-enrollment", 'sensei-lms' ) }
checked={ selfEnrollmentNotAllowed }
onChange={ ( checked ) =>
setMeta( {
...meta,
_sensei_self_enrollment_not_allowed: checked,
} )
}
help={ __(
'Students need to be manually enrolled by teachers or administrators.',
'sensei-lms'
) }
/>

{ window.sensei.courseSettingsSidebar.features?.open_access && (
<>
<HorizontalRule />
Expand Down
4 changes: 4 additions & 0 deletions changelog/add-setting-not-allowing-self-enrollment
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add a setting to not allow self-enrollment on courses
4 changes: 4 additions & 0 deletions changelog/fix-take-course-notices
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fix course notices that are intended to be displayed only on the course page but were currently appearing on the courses archive page
14 changes: 13 additions & 1 deletion includes/blocks/class-sensei-block-take-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ public function render_take_course_block( $attributes, $content ): string {
return '';
}

$is_course_page = is_single();

if ( Sensei_Course::can_current_user_manually_enrol( $course_id ) ) {
if ( ! Sensei_Course::is_prerequisite_complete( $course_id ) ) {
Sensei()->notices->add_notice( Sensei()->course::get_course_prerequisite_message( $course_id ), 'info', 'sensei-take-course-prerequisite' );
if ( $is_course_page ) {
Sensei()->notices->add_notice( Sensei()->course::get_course_prerequisite_message( $course_id ), 'info', 'sensei-take-course-prerequisite' );
}
$html = $this->render_disabled( $content );
} else {
// Replace button label in case it's coming from a sign in with redirect to take course.
Expand All @@ -71,6 +75,14 @@ public function render_take_course_block( $attributes, $content ): string {
}
$html = $this->render_with_start_course_form( $course_id, $content );
}
} elseif ( Sensei_Course::is_self_enrollment_not_allowed( $course_id ) && ! Sensei_Course::is_user_enrolled( $course_id, get_current_user_id() ) ) {
if ( $is_course_page ) {
Sensei()->notices->add_notice(
__( 'Please contact the course administrator to sign up for this course.', 'sensei-lms' ),
'info'
);
}
$html = $this->render_disabled( $content );
} elseif ( ! is_user_logged_in() ) {
$html = $this->render_with_login( $content );
}
Expand Down
104 changes: 90 additions & 14 deletions includes/class-sensei-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,7 @@ public function set_up_meta_fields() {
'show_in_rest' => true,
'single' => true,
'type' => 'integer',
'auth_callback' => function ( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
},
'auth_callback' => [ $this, 'post_meta_auth_callback' ],
]
);
register_post_meta(
Expand All @@ -663,9 +661,17 @@ public function set_up_meta_fields() {
'single' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'auth_callback' => function ( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
},
'auth_callback' => [ $this, 'post_meta_auth_callback' ],
]
);
register_post_meta(
'course',
'_sensei_self_enrollment_not_allowed',
[
'show_in_rest' => true,
'single' => true,
'type' => 'boolean',
'auth_callback' => [ $this, 'post_meta_auth_callback' ],
]
);
register_post_meta(
Expand All @@ -675,9 +681,7 @@ public function set_up_meta_fields() {
'show_in_rest' => true,
'single' => true,
'type' => 'boolean',
'auth_callback' => function ( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
},
'auth_callback' => [ $this, 'post_meta_auth_callback' ],
]
);
register_post_meta(
Expand All @@ -687,9 +691,7 @@ public function set_up_meta_fields() {
'show_in_rest' => true,
'single' => true,
'type' => 'boolean',
'auth_callback' => function ( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
},
'auth_callback' => [ $this, 'post_meta_auth_callback' ],
]
);
/**
Expand All @@ -705,6 +707,21 @@ public function set_up_meta_fields() {
$this->meta_fields = apply_filters( 'sensei_course_meta_fields', [ 'course_prerequisite', 'course_featured', 'course_video_embed' ] );
}

/**
* Add the course meta boxes.
*
* @internal
*
* @param bool $allowed Whether the user can add the meta.
* @param string $meta_key The meta key.
* @param int $post_id The post ID where the meta key is being edited.
*
* @return boolean Whether the user can edit the meta key.
*/
public function post_meta_auth_callback( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
}

/**
* meta_box_setup function.
*
Expand Down Expand Up @@ -3719,6 +3736,8 @@ public static function the_course_enrolment_actions() {
/**
* Check if a user can manually enrol themselves.
*
* @since $$next-version$$ Add a check whether self-enrollment is not allowed.
*
* @param int $course_id Course post ID.
*
* @return bool
Expand All @@ -3728,7 +3747,10 @@ public static function can_current_user_manually_enrol( $course_id ) {
// Check if the user is already enrolled through any provider.
$is_user_enrolled = self::is_user_enrolled( $course_id, get_current_user_id() );

$default_can_user_manually_enrol = is_user_logged_in() && ! $is_user_enrolled;
// Check if self-enrollment is not allowed for this course.
$is_self_enrollment_not_allowed = self::is_self_enrollment_not_allowed( $course_id );

$default_can_user_manually_enrol = is_user_logged_in() && ! $is_user_enrolled && ! $is_self_enrollment_not_allowed;

$can_user_manually_enrol = apply_filters_deprecated(
'sensei_display_start_course_form',
Expand All @@ -3751,6 +3773,32 @@ public static function can_current_user_manually_enrol( $course_id ) {
return (bool) apply_filters( 'sensei_can_user_manually_enrol', $can_user_manually_enrol, $course_id );
}

/**
* Check if self-enrollment is not allowed for the given course.
*
* @since $$next-version$$
*
* @param int $course_id Course post ID.
*
* @return boolean Whether self-enrollment is not allowed.
*/
public static function is_self_enrollment_not_allowed( $course_id ) {
$self_enrollment_not_allowed = (bool) get_post_meta( $course_id, '_sensei_self_enrollment_not_allowed', true );

/**
* Check if self-enrollment is not allowed.
*
* @since $$next-version$$
*
* @hook sensei_self_enrollment_not_allowed
*
* @param {bool} $self_enrollment_not_allowed True if self-enrollment is not allowed, false otherwise.
* @param {int} $course_id Course post ID.
* @return {bool} Filtered value.
*/
return (bool) apply_filters( 'sensei_self_enrollment_not_allowed', $self_enrollment_not_allowed, $course_id );
}

/**
* Output the course actions like start taking course, register, etc. Note
* that this expects that the user is not already taking the course; that
Expand All @@ -3775,7 +3823,7 @@ public static function output_course_enrolment_actions() {

if ( self::can_current_user_manually_enrol( $post->ID ) ) {
sensei_start_course_form( $post->ID );
} else {
} elseif ( ! self::is_self_enrollment_not_allowed( $post->ID ) ) {
if ( get_option( 'users_can_register' ) ) {

// set the permissions message
Expand Down Expand Up @@ -4098,6 +4146,29 @@ public static function prerequisite_complete_message() {
}
}

/**
* Show a message telling the user that they are not allowed to self enroll if the setting is enabled.
*
* @since $$next-version$$
*
* @internal
*/
public static function self_enrollment_not_allowed_message() {
$course_id = get_the_ID();
$user_id = get_current_user_id();

if ( false === $course_id ) {
return;
}

if ( self::is_self_enrollment_not_allowed( $course_id ) && ! self::is_user_enrolled( $course_id, $user_id ) ) {
Sensei()->notices->add_notice(
__( 'Please contact the course administrator to sign up for this course.', 'sensei-lms' ),
'info'
);
}
}

/**
* Generate the HTML of the course prerequisite notice.
*
Expand Down Expand Up @@ -4313,6 +4384,8 @@ public function add_legacy_course_hooks( $sensei ) {
// Course prerequisite completion message.
add_action( 'sensei_single_course_content_inside_before', [ 'Sensei_Course', 'prerequisite_complete_message' ], 20 );

// Self-enrollment not allowed message.
add_action( 'sensei_single_course_content_inside_before', [ 'Sensei_Course', 'self_enrollment_not_allowed_message' ], 20 );
}

/**
Expand All @@ -4336,6 +4409,9 @@ public function remove_legacy_course_actions() {
// Course prerequisite completion message.
remove_action( 'sensei_single_course_content_inside_before', [ 'Sensei_Course', 'prerequisite_complete_message' ], 20 );

// Self-enrollment not allowed message.
remove_action( 'sensei_single_course_content_inside_before', [ 'Sensei_Course', 'self_enrollment_not_allowed_message' ], 20 );

// Add message links to courses.
remove_action( 'sensei_single_course_content_inside_before', [ Sensei()->post_types->messages, 'send_message_link' ], 35 );

Expand Down
4 changes: 4 additions & 0 deletions includes/class-sensei-lesson.php
Original file line number Diff line number Diff line change
Expand Up @@ -4785,6 +4785,10 @@ public static function course_signup_link() {
// translators: The placeholder %1$s is a link to the Course.
$message_default = sprintf( esc_html__( 'Please sign up for the %1$s before starting the lesson.', 'sensei-lms' ), $course_link );

if ( Sensei_Course::is_self_enrollment_not_allowed( $course_id ) ) {
$message_default = esc_html__( 'Please contact the course administrator to access the course content.', 'sensei-lms' );
}

/**
* Filter the course sign up notice message on the lesson page.
*
Expand Down
35 changes: 25 additions & 10 deletions includes/course-theme/class-sensei-course-theme-lesson.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,9 @@ private function maybe_add_lesson_prerequisite_notice() {
* @return void
*/
private function maybe_add_not_enrolled_notice() {
$lesson_id = \Sensei_Utils::get_current_lesson();
$course_id = Sensei()->lesson->get_course_id( $lesson_id );
$lesson_id = \Sensei_Utils::get_current_lesson();
$course_id = Sensei()->lesson->get_course_id( $lesson_id );
$is_preview = $lesson_id && Sensei_Utils::is_preview_lesson( $lesson_id );

if ( Sensei_Course::is_user_enrolled( $course_id ) ) {
return;
Expand All @@ -262,6 +263,24 @@ private function maybe_add_not_enrolled_notice() {
$notice_title = __( 'You don\'t have access to this lesson', 'sensei-lms' );
$notice_icon = 'lock';

if ( $is_preview ) {
$notice_title = __( 'This is a preview lesson', 'sensei-lms' );
$notice_icon = 'eye';
}

// Check if self-enrollment is allowed.
if ( Sensei_Course::is_self_enrollment_not_allowed( $course_id ) ) {
$notices->add_notice(
$notice_key,
__( 'Please contact the course administrator to take this lesson.', 'sensei-lms' ),
$notice_title,
[],
$notice_icon
);

return;
}

// Course prerequisite notice.
if ( ! Sensei_Course::is_prerequisite_complete( $course_id ) ) {
$notices->add_notice(
Expand Down Expand Up @@ -298,10 +317,8 @@ private function maybe_add_not_enrolled_notice() {

$notice_text = __( 'Please register or sign in to access the course content.', 'sensei-lms' );

if ( $lesson_id && Sensei_Utils::is_preview_lesson( $lesson_id ) ) {
$notice_text = __( 'Register or sign in to take this lesson.', 'sensei-lms' );
$notice_title = __( 'This is a preview lesson', 'sensei-lms' );
$notice_icon = 'eye';
if ( $is_preview ) {
$notice_text = __( 'Register or sign in to take this lesson.', 'sensei-lms' );
}

$notices->add_notice(
Expand All @@ -327,10 +344,8 @@ private function maybe_add_not_enrolled_notice() {

$notice_text = __( 'Please register for this course to access the content.', 'sensei-lms' );

if ( $lesson_id && Sensei_Utils::is_preview_lesson( $lesson_id ) ) {
$notice_text = __( 'Register for this course to take this lesson.', 'sensei-lms' );
$notice_title = __( 'This is a preview lesson', 'sensei-lms' );
$notice_icon = 'eye';
if ( $is_preview ) {
$notice_text = __( 'Register for this course to take this lesson.', 'sensei-lms' );
}

$notices->add_notice(
Expand Down
Loading

0 comments on commit 67e35a9

Please sign in to comment.