Skip to content

Commit

Permalink
Merge pull request #4155 from Automattic/add/view-results-button
Browse files Browse the repository at this point in the history
Add View Results button
  • Loading branch information
gikaragia authored Apr 7, 2021
2 parents 631a24d + aa55baa commit b74835c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
2 changes: 2 additions & 0 deletions assets/blocks/single-course.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import TakeCourseBlock from './take-course-block';
import CourseProgressBlock from './course-progress-block';
import { OutlineBlock, LessonBlock, ModuleBlock } from './course-outline';
import ConditionalContentBlock from './conditional-content-block';
import ViewResults from './view-results-block';

registerSenseiBlocks( [
OutlineBlock,
Expand All @@ -14,4 +15,5 @@ registerSenseiBlocks( [
TakeCourseBlock,
CourseProgressBlock,
ConditionalContentBlock,
ViewResults,
] );
36 changes: 36 additions & 0 deletions assets/blocks/view-results-block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { BlockStyles, createButtonBlockType } from '../button';
import ToggleLegacyCourseMetaboxesWrapper from '../toggle-legacy-course-metaboxes-wrapper';

/**
* View results button block.
*/
export default createButtonBlockType( {
tagName: 'a',
EditWrapper: ToggleLegacyCourseMetaboxesWrapper,
settings: {
name: 'sensei-lms/button-view-results',
description: __(
'Allow an enrolled user to navigate to the course results page. The block is only displayed if the user is enrolled to the course.',
'sensei-lms'
),
title: __( 'View Results', 'sensei-lms' ),
attributes: {
text: {
default: __( 'View Results', 'sensei-lms' ),
},
},
styles: [
BlockStyles.Fill,
{ ...BlockStyles.Outline, isDefault: true },
BlockStyles.Link,
],
},
} );
60 changes: 60 additions & 0 deletions includes/blocks/class-sensei-block-view-results.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* File containing the Sensei_Block_View_Results class.
*
* @package sensei
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Block for View Results button.
*/
class Sensei_Block_View_Results {

/**
* Sensei_Block_View_Results constructor.
*/
public function __construct() {
$this->register_block();
}

/**
* Register View Results button block.
*
* @access private
*/
public function register_block() {
Sensei_Blocks::register_sensei_block(
'sensei-lms/button-view-results',
[
'render_callback' => [ $this, 'render' ],
]
);
}

/**
* Render the View Results button.
*
* @param array $attributes Block attributes.
* @param string $content Block HTML.
*
* @return string The HTML of the block.
*/
public function render( $attributes, $content ): string {
if ( ! Sensei()->course::is_user_enrolled( get_the_ID() ) ) {
return '';
}

$results_link = esc_url( Sensei()->course_results->get_permalink( get_the_ID() ) );

return preg_replace(
'/<a(.*)>/',
'<a href="' . $results_link . '" $1>',
$content,
1
);
}
}
1 change: 1 addition & 0 deletions includes/blocks/class-sensei-course-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function initialize_blocks() {
$this->contact_teacher = new Sensei_Block_Contact_Teacher();
$this->take_course = new Sensei_Block_Take_Course();
new Sensei_Conditional_Content_Block();
new Sensei_Block_View_Results();

$post_type_object = get_post_type_object( 'course' );

Expand Down

0 comments on commit b74835c

Please sign in to comment.