Skip to content

Commit

Permalink
feat: implement logging system for Enrollment Requests (#17)
Browse files Browse the repository at this point in the history
* feat: operation logs for changes into post form

* fix: enrollment log for when post is created

* fix: mayus fix in object created message

* fix: deleted comment in spanish

* fix: phpcs fixes

* fix: deleted general info field from form

* fix: enrollment request type in before object fixed

* fix: small adjustments to logs box frontend

* fix: logs are displayed in ascending order

* refactor: deleted title attribute from email input

* fix: post name comparison comment improved

* fix: spanish variable names fixed

* fix: enrollment logs working, deleted spanish comments

* fix: scroll to the bottom without using js

* refactor: move code from files to a new model for db management

* refactor: add try-catch blocks for errors handling

* fix: logs table variable name in english

* fix: enrollment array values sanitized before operation

* refactor: save enrollment code decoupled

* fix: sanitize field without create a variable

* fix: end line in plugin logs file

* fix: added object_status field for new/modified indication

* refactor: styles added to css file

* fix: improved if eval for post status

* fix: logs table renamed in english

* refactor: check main enrollment data decoupled

* fix: check enrollment data method call fixed

* refactor: added comments to new functions

* fix: endline in css file
  • Loading branch information
julianramirez2 authored Aug 3, 2023
1 parent 93d1429 commit 9d05522
Show file tree
Hide file tree
Showing 7 changed files with 435 additions and 123 deletions.
29 changes: 28 additions & 1 deletion admin/css/openedx-woocommerce-plugin-admin.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
/**
* All of the CSS for your admin-specific functionality should be
* included in this file.
*/
*/

#namediv{
padding: 50px 20px 50px 20px;
}

.logs_box {
max-width: 100%;
max-height: 477px;
border: 1px solid #ccc;
padding: 10px;
white-space: pre-wrap;
word-wrap: break-word;
overflow: auto;
display: flex;
flex-direction: column-reverse;
}

.log_entry {
background-color: #f2f2f2;
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
}

.logs_box strong {
font-weight: bold;
}
114 changes: 64 additions & 50 deletions admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace App\admin\views;
use App\model\Openedx_Woocommerce_Plugin_Log;

if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand All @@ -17,6 +18,15 @@ class Openedx_Woocommerce_Plugin_Enrollment_Info_Form {
*/
public $post_type = 'openedx_enrollment';

/**
* The log manager.
*
* @var Openedx_Woocommerce_Plugin_Log
* @access private
* @since 1.1.1
*/
private $log_manager;

/**
* Constructor function.
*
Expand All @@ -27,6 +37,16 @@ class Openedx_Woocommerce_Plugin_Enrollment_Info_Form {
public function __construct( $enrollment_request ) {
$this->render_enrollment_info_form($enrollment_request);
$this->replace_admin_meta_boxes();
$this->register_log_manager();
}

/**
* Register log manager
*
* @return void
*/
public function register_log_manager() {
$this->log_manager = new Openedx_Woocommerce_Plugin_Log();
}

/**
Expand All @@ -51,6 +71,7 @@ public function render_enrollment_info_form( $post ) {
if (! $course_id && ! $email) {
$new_enrollment = true;
}

?>
<div id="namediv" class="postbox">
<h2 class="">Open edX enrollment request</h2>
Expand All @@ -68,12 +89,11 @@ public function render_enrollment_info_form( $post ) {
<tr>
<td class="first"><label>User Email</label></td>
<td>
<div style="width: 49%; display: inline-table;">
<input type="email"
id="openedx_enrollment_email"
name="enrollment_email"
title="You only need to fill one. Either the email or username"
value="<?php echo( $email ); ?>">
<div style="width: 49%; display: inline-table;">
<input type="email"
id="openedx_enrollment_email"
name="enrollment_email"
value="<?php echo( $email ); ?>">
</div>
</td>
</tr>
Expand Down Expand Up @@ -121,7 +141,9 @@ public function render_enrollment_info_form( $post ) {
</td>
</tr>
<tr>
<td class="first"><label for="openedx_enrollment_is_active">Request Type</label></td>
<td class="first">
<label for="openedx_enrollment_is_active">Request Type</label>
</td>
<td>

<select id="openedx_enrollment_is_active" name="enrollment_request_type">
Expand Down Expand Up @@ -154,40 +176,39 @@ public function render_enrollment_info_form( $post ) {
</td>
</tr>

<tr>
<td class="first"><label>General Info</label></td>
<td>
<p>Edited:
<?php
if ( get_post_meta( $post_id, 'edited', true ) ) {
echo 'yes';
} else {
echo 'no';
}
?>
</p>
<p>Last edited: <?php echo( get_the_modified_time( '', $post_id ) . ' ' . get_the_modified_date( '', $post_id ) ); ?></p>
</td>
</tr>

<tr>
<td class="first"><label>Choose an Action</label></td>
<td>
<select name="enrollment_action" id="actions-select">
<option value="save_no_process"><?php esc_html_e( 'Save without processing', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_sync"><?php esc_html_e( 'Synchronize (pull information)', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_process" selected><?php esc_html_e( 'Process request', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_no_pre"><?php esc_html_e( 'Process no pre-enrollment', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_force"><?php esc_html_e( 'Process --force', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_no_pre_force"><?php esc_html_e( 'Process no pre-enrollment --force', 'wp-edunext-marketing-site' ); ?></option>
<option value="default" disabled selected hidden>
Select an option
</option>
<option value="save_no_process">
<?php esc_html_e('Save without processing', 'wp-edunext-marketing-site'); ?>
</option>
<option value="enrollment_sync">
<?php esc_html_e('Synchronize (pull information)', 'wp-edunext-marketing-site'); ?>
</option>
<option value="enrollment_process">
<?php esc_html_e('Process request', 'wp-edunext-marketing-site'); ?>
</option>
<option value="enrollment_no_pre">
<?php esc_html_e('Process no pre-enrollment', 'wp-edunext-marketing-site'); ?>
</option>
<option value="enrollment_force">
<?php esc_html_e('Process --force', 'wp-edunext-marketing-site'); ?>
</option>
<option value="enrollment_no_pre_force">
<?php esc_html_e('Process no pre-enrollment --force', 'wp-edunext-marketing-site'); ?>
</option>
</select>
</td>
</tr>

<tr>
<td class="first"><label>Create/Update Enrollment</label></td>
<td>
<button class="button save_order button-primary"><span><?php esc_html_e( 'Apply action', 'wp-edunext-marketing-site' ); ?></span></button>
<button class="button save_order button-primary"><span><?php esc_html_e('Apply action', 'wp-edunext-marketing-site'); ?></span></button>
</td>
</tr>
</tbody>
Expand All @@ -196,32 +217,25 @@ public function render_enrollment_info_form( $post ) {
</div>
<?php
}


/**
* Temporary function to get test logs for the logs box
*
* @return string Test logs
*/
public function get_logs() {
$logs = "Log 1\n";
$logs .= "Log 2\n";
$logs .= "Log 3\n";
$logs .= "No errors ocurred\n";
return $logs;
}


/**
* Renders the logs box for the edit post box
* Render logs box
*
* @return void
* @param WP_Post $post Current post object.
*/
public function render_logs_box() {
$logs = $this->get_logs();
public function render_logs_box($post) {
$post_id = $post->ID;
$logs = $this->log_manager->getLogs($post_id);
?>

<style>

</style>
<div class="logs_box">
<pre><?php echo esc_html( $logs ); ?></pre>
<?php echo $logs; ?>
</div>

<?php
}

Expand All @@ -233,7 +247,7 @@ public function render_logs_box() {
public function replace_admin_meta_boxes() {
remove_meta_box( 'submitdiv', $this->post_type, 'side' );

add_meta_box( 'openedx_enrollment_request_actions', 'Enrollment Operation Logs', array( $this, 'render_logs_box' ), $this->post_type, 'side', 'high' );
add_meta_box('openedx_enrollment_request_actions', 'Enrollment Operation Logs', array( $this, 'render_logs_box' ), $this->post_type, 'side', 'high');
}

}
3 changes: 3 additions & 0 deletions includes/Openedx_Woocommerce_Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ private function load_dependencies() {
*/
include_once plugin_dir_path( dirname(__FILE__)) . 'includes/model/Openedx_Woocommerce_Plugin_Post_Type.php';

include_once plugin_dir_path( dirname(__FILE__)) . 'includes/model/Openedx_Woocommerce_Plugin_Logs.php';

/**
* The class responsible for rendering the enrollment info form
*/
Expand Down Expand Up @@ -182,6 +184,7 @@ private function define_admin_hooks() {
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
$this->loader->add_filter( 'gettext', $this, 'openedx_plugin_custom_post_message', 10, 3 );
$this->loader->wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . '../admin/css/openedx-woocommerce-plugin-admin.css', array(), $this->version, 'all' );

}

Expand Down
36 changes: 36 additions & 0 deletions includes/Openedx_Woocommerce_Plugin_Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Openedx_Woocommerce_Plugin_Loader {
*/
protected $filters;

protected $styles;

/**
* Initialize the collections used to maintain the actions and filters.
*
Expand All @@ -52,6 +54,7 @@ public function __construct() {

$this->actions = array();
$this->filters = array();
$this->styles = array();

}

Expand Down Expand Up @@ -83,6 +86,21 @@ public function add_filter( $hook, $component, $callback, $priority = 10, $accep
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
}

/**
* Add a new style to the collection to be registered with WordPress.
*
* @since 1.1.1
* @param string $handle Name of the stylesheet. Should be unique.
* @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
* @param array $deps An array of registered stylesheet handles this stylesheet depends on.
* @param string $ver String specifying the stylesheet version number, if it has one. This parameter is used to ensure that the correct version is sent to the client regardless of caching, and so should be included if a version number is available and makes sense for the stylesheet.
* @param string $media The media for which this stylesheet has been defined.
* @return array The collection of actions and filters registered with WordPress.
*/
public function wp_enqueue_style( $handle, $src, $deps, $ver, $media ) {
$this->styles = $this->add_style($handle, $src, $deps, $ver, $media);
}

/**
* A utility function that is used to register the actions and hooks into a single
* collection.
Expand Down Expand Up @@ -111,6 +129,20 @@ private function add( $hooks, $hook, $component, $callback, $priority, $accepted

}

private function add_style( $handle, $src, $deps, $ver, $media ) {

$hooks[] = array(
'handle' => $handle,
'src' => $src,
'deps' => $deps,
'ver' => $ver,
'media' => $media
);

return $hooks;

}

/**
* Register the filters and actions with WordPress.
*
Expand All @@ -126,6 +158,10 @@ public function run() {
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
}

foreach ( $this->styles as $hook ) {
wp_enqueue_style( $hook['handle'], $hook['src'], $hook['deps'], $hook['ver'], $hook['media'] );
}

}

}
Loading

0 comments on commit 9d05522

Please sign in to comment.