首页 / 教程文章 / WordPress小批量定制插件实现供应商绩效评估的教程

WordPress小批量定制插件实现供应商绩效评估的教程

本教程指导如何开发WordPress小批量定制插件,实现供应商绩效评估功能。插件涵盖供应商信息管理、自定义评估指标、绩效记录与评分计算、数据可视化报表等核心模块。开发步骤包括创建插件基础结构、设计数据库表、编写管理类与界面。使用说明详细介绍了安装激活、添加供应商、设置指标、进行评估和查看结果的流程,并提供了扩展评估维度、集成图表工具等定制建议,助力企业高效管理供应链绩效。

WordPress小批量定制插件实现供应商绩效评估的教程

概述

在当今的商业环境中,对供应商进行有效的绩效评估是企业供应链管理的重要组成部分。对于使用WordPress搭建的企业网站或采购管理系统,一个定制化的供应商绩效评估插件可以极大地提高管理效率。本教程将指导您如何开发一个适用于小批量定制需求的WordPress插件,用于实现供应商绩效评估功能。

插件规划与设计

功能需求分析

在开始编码之前,我们需要明确插件的基本功能:

  1. 供应商信息管理(添加、编辑、删除)
  2. 评估指标管理(可自定义评估维度)
  3. 绩效评估记录(多次评估历史)
  4. 绩效评分计算与展示
  5. 数据可视化报表

数据库结构设计

我们将创建两个自定义数据表来存储供应商信息和评估记录。

插件开发步骤

第一步:创建插件基础结构

首先,在WordPress的wp-content/plugins目录下创建一个新文件夹,命名为supplier-performance-evaluator,然后创建主插件文件:

<?php
/**
 * Plugin Name: 供应商绩效评估系统
 * Plugin URI: https://yourwebsite.com/
 * Description: 用于管理和评估供应商绩效的定制插件
 * Version: 1.0.0
 * Author: 您的名称
 * License: GPL v2 or later
 */

// 防止直接访问
if (!defined('ABSPATH')) {
    exit;
}

// 定义插件常量
define('SPE_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('SPE_PLUGIN_URL', plugin_dir_url(__FILE__));
define('SPE_VERSION', '1.0.0');

// 包含必要文件
require_once SPE_PLUGIN_DIR . 'includes/class-database.php';
require_once SPE_PLUGIN_DIR . 'includes/class-supplier-manager.php';
require_once SPE_PLUGIN_DIR . 'includes/class-evaluation-manager.php';
require_once SPE_PLUGIN_DIR . 'admin/admin-menu.php';

// 激活和停用钩子
register_activation_hook(__FILE__, 'spe_activate_plugin');
register_deactivation_hook(__FILE__, 'spe_deactivate_plugin');

/**
 * 插件激活时创建数据库表
 */
function spe_activate_plugin() {
    require_once SPE_PLUGIN_DIR . 'includes/class-database.php';
    SPE_Database::create_tables();
    
    // 添加默认评估指标
    spe_add_default_evaluation_criteria();
}

/**
 * 插件停用时的清理工作
 */
function spe_deactivate_plugin() {
    // 这里可以添加清理代码,但通常不删除数据
}

/**
 * 添加默认评估指标
 */
function spe_add_default_evaluation_criteria() {
    $default_criteria = array(
        'quality' => '产品质量',
        'delivery' => '交货准时率',
        'price' => '价格竞争力',
        'service' => '客户服务',
        'communication' => '沟通效率'
    );
    
    update_option('spe_evaluation_criteria', $default_criteria);
}

第二步:创建数据库管理类

<?php
// includes/class-database.php

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

class SPE_Database {
    
    /**
     * 创建插件所需的数据表
     */
    public static function create_tables() {
        global $wpdb;
        
        $charset_collate = $wpdb->get_charset_collate();
        
        // 供应商表
        $suppliers_table = $wpdb->prefix . 'spe_suppliers';
        $suppliers_sql = "CREATE TABLE IF NOT EXISTS $suppliers_table (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            name varchar(100) NOT NULL,
            contact_person varchar(100),
            email varchar(100),
            phone varchar(30),
            address text,
            registration_date datetime DEFAULT CURRENT_TIMESTAMP,
            status varchar(20) DEFAULT 'active',
            PRIMARY KEY (id)
        ) $charset_collate;";
        
        // 评估记录表
        $evaluations_table = $wpdb->prefix . 'spe_evaluations';
        $evaluations_sql = "CREATE TABLE IF NOT EXISTS $evaluations_table (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            supplier_id mediumint(9) NOT NULL,
            evaluator_id mediumint(9) NOT NULL,
            evaluation_date datetime DEFAULT CURRENT_TIMESTAMP,
            quality_score tinyint(3),
            delivery_score tinyint(3),
            price_score tinyint(3),
            service_score tinyint(3),
            communication_score tinyint(3),
            overall_score decimal(4,2),
            comments text,
            PRIMARY KEY (id),
            FOREIGN KEY (supplier_id) REFERENCES {$wpdb->prefix}spe_suppliers(id) ON DELETE CASCADE
        ) $charset_collate;";
        
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($suppliers_sql);
        dbDelta($evaluations_sql);
    }
}

第三步:供应商管理类

<?php
// includes/class-supplier-manager.php

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

class SPE_Supplier_Manager {
    
    private $table_name;
    
    public function __construct() {
        global $wpdb;
        $this->table_name = $wpdb->prefix . 'spe_suppliers';
    }
    
    /**
     * 添加新供应商
     * @param array $data 供应商数据
     * @return int|false 插入的ID或false
     */
    public function add_supplier($data) {
        global $wpdb;
        
        $defaults = array(
            'status' => 'active',
            'registration_date' => current_time('mysql')
        );
        
        $data = wp_parse_args($data, $defaults);
        
        $result = $wpdb->insert(
            $this->table_name,
            $data,
            array('%s', '%s', '%s', '%s', '%s', '%s', '%s')
        );
        
        return $result ? $wpdb->insert_id : false;
    }
    
    /**
     * 获取所有供应商
     * @param string $status 供应商状态筛选
     * @return array 供应商列表
     */
    public function get_all_suppliers($status = '') {
        global $wpdb;
        
        $where = '';
        if (!empty($status)) {
            $where = $wpdb->prepare("WHERE status = %s", $status);
        }
        
        $query = "SELECT * FROM {$this->table_name} {$where} ORDER BY name ASC";
        return $wpdb->get_results($query, ARRAY_A);
    }
    
    /**
     * 根据ID获取供应商
     * @param int $supplier_id 供应商ID
     * @return array|null 供应商数据或null
     */
    public function get_supplier($supplier_id) {
        global $wpdb;
        
        $query = $wpdb->prepare(
            "SELECT * FROM {$this->table_name} WHERE id = %d",
            $supplier_id
        );
        
        return $wpdb->get_row($query, ARRAY_A);
    }
    
    /**
     * 更新供应商信息
     * @param int $supplier_id 供应商ID
     * @param array $data 更新数据
     * @return bool 是否成功
     */
    public function update_supplier($supplier_id, $data) {
        global $wpdb;
        
        return $wpdb->update(
            $this->table_name,
            $data,
            array('id' => $supplier_id),
            array('%s', '%s', '%s', '%s', '%s', '%s', '%s'),
            array('%d')
        );
    }
    
    /**
     * 删除供应商
     * @param int $supplier_id 供应商ID
     * @return bool 是否成功
     */
    public function delete_supplier($supplier_id) {
        global $wpdb;
        
        return $wpdb->delete(
            $this->table_name,
            array('id' => $supplier_id),
            array('%d')
        );
    }
}

第四步:评估管理类

<?php
// includes/class-evaluation-manager.php

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

class SPE_Evaluation_Manager {
    
    private $table_name;
    
    public function __construct() {
        global $wpdb;
        $this->table_name = $wpdb->prefix . 'spe_evaluations';
    }
    
    /**
     * 添加新的评估记录
     * @param array $data 评估数据
     * @return int|false 插入的ID或false
     */
    public function add_evaluation($data) {
        global $wpdb;
        
        // 计算总分
        $scores = array(
            $data['quality_score'],
            $data['delivery_score'],
            $data['price_score'],
            $data['service_score'],
            $data['communication_score']
        );
        
        $data['overall_score'] = array_sum($scores) / count($scores);
        $data['evaluation_date'] = current_time('mysql');
        $data['evaluator_id'] = get_current_user_id();
        
        $result = $wpdb->insert(
            $this->table_name,
            $data,
            array('%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%f', '%s')
        );
        
        return $result ? $wpdb->insert_id : false;
    }
    
    /**
     * 获取供应商的所有评估记录
     * @param int $supplier_id 供应商ID
     * @return array 评估记录
     */
    public function get_supplier_evaluations($supplier_id) {
        global $wpdb;
        
        $query = $wpdb->prepare(
            "SELECT e.*, u.display_name as evaluator_name 
             FROM {$this->table_name} e
             LEFT JOIN {$wpdb->users} u ON e.evaluator_id = u.ID
             WHERE supplier_id = %d
             ORDER BY evaluation_date DESC",
            $supplier_id
        );
        
        return $wpdb->get_results($query, ARRAY_A);
    }
    
    /**
     * 计算供应商的平均绩效分数
     * @param int $supplier_id 供应商ID
     * @return array 各维度平均分和总分
     */
    public function calculate_supplier_score($supplier_id) {
        global $wpdb;
        
        $query = $wpdb->prepare(
            "SELECT 
                AVG(quality_score) as avg_quality,
                AVG(delivery_score) as avg_delivery,
                AVG(price_score) as avg_price,
                AVG(service_score) as avg_service,
                AVG(communication_score) as avg_communication,
                AVG(overall_score) as avg_overall
             FROM {$this->table_name}
             WHERE supplier_id = %d",
            $supplier_id
        );
        
        return $wpdb->get_row($query, ARRAY_A);
    }
    
    /**
     * 获取所有供应商的绩效排名
     * @param int $limit 返回数量限制
     * @return array 供应商排名
     */
    public function get_supplier_rankings($limit = 10) {
        global $wpdb;
        
        $suppliers_table = $wpdb->prefix . 'spe_suppliers';
        
        $query = "SELECT 
                    s.id,
                    s.name,
                    AVG(e.overall_score) as avg_score,
                    COUNT(e.id) as evaluation_count
                  FROM {$suppliers_table} s
                  LEFT JOIN {$this->table_name} e ON s.id = e.supplier_id
                  WHERE s.status = 'active'
                  GROUP BY s.id
                  HAVING evaluation_count > 0
                  ORDER BY avg_score DESC
                  LIMIT %d";
        
        $query = $wpdb->prepare($query, $limit);
        
        return $wpdb->get_results($query, ARRAY_A);
    }
}

第五步:创建管理界面

<?php
// admin/admin-menu.php

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

/**
 * 添加管理菜单
 */
function spe_add_admin_menu() {
    add_menu_page(
        '供应商绩效评估',
        '供应商评估',
        'manage_options',
        'supplier-performance',
        'spe_dashboard_page',
        'dashicons-chart-bar',
        30
    );
    
    add_submenu_page(
        'supplier-performance',
        '供应商管理',
        '供应商管理',
        'manage_options',
        'spe-suppliers',
        'spe_suppliers_page'
    );
    
    add_submenu_page(
        'supplier-performance',
        '绩效评估',
        '绩效评估',
        'manage_options',
        'spe-evaluations',
        'spe_evaluations_page'
    );
    
    add_submenu_page(
        'supplier-performance',
        '评估报表',
        '评估报表',
        'manage_options',
        'spe-reports',
        'spe_reports_page'
    );
}
add_action('admin_menu', 'spe_add_admin_menu');

/**
 * 仪表板页面
 */
function spe_dashboard_page() {
    ?>
    <div class="wrap">
        <h1>供应商绩效评估系统</h1>
        
        <div class="spe-dashboard">
            <div class="spe-stats-container">
                <?php
                $supplier_manager = new SPE_Supplier_Manager();
                $evaluation_manager = new SPE_Evaluation_Manager();
                
                $suppliers = $supplier_manager->get_all_suppliers('active');
                $rankings = $evaluation_manager->get_supplier_rankings(5);
                ?>
                
                <div class="spe-stat-box">
                    <h3>活跃供应商</h3>
                    <p class="spe-stat-number"><?php echo count($suppliers); ?></p>
                </div>
                
                <div class="spe-stat-box">
                    <h3>绩效评估总数</h3>
                    <p class="spe-stat-number">
                        <?php
                        global $wpdb;
                        $table_name = $wpdb->prefix . 'spe_evaluations';
                        $count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");
                        echo $count ?: 0;
                        ?>
                    </p>
                </div>
            </div>
            
            <div class="spe-ranking-section">
                <h2>供应商绩效排名(前5名)</h2>
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th>排名</th>
                            <th>供应商名称</th>
                            <th>平均分数</th>
                            <th>评估次数</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (!empty($rankings)): ?>
                            <?php $rank = 1; ?>
                            <?php foreach ($rankings as $supplier): ?>
                                <tr>
                                    <td><?php echo $rank++; ?></td>
                                    <td><?php echo esc_html($supplier['name']); ?></td>
                                    <td><?php echo number_format($supplier['avg_score'], 2); ?></td>
                                    <td><?php echo $supplier['evaluation_count']; ?></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="4">暂无评估数据</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    <style>
        .spe-dashboard {
            margin-top: 20px;
        }
        .spe-stats-container {
            display: flex;
            gap: 20px;
            margin-bottom: 30px;
        }
        .spe-stat-box {
            background: #fff;
            border: 1px solid #ccd0d4;
            border-radius: 4px;
            padding: 20px;
            flex: 1;
            text-align: center;
        }
        .spe-stat-number {
            font-size: 36px;
            font-weight: bold;
            color: #2271b1;
            margin: 10px 0 0;
        }
        .spe-ranking-section {
            background: #fff;
            border: 1px solid #ccd0d4;
            border-radius: 4px;
            padding: 20px;
        }
    </style>
    <?php
}

/**
 * 供应商管理页面
 */
function spe_suppliers_page() {
    // 这里实现供应商管理界面
    echo '<div class="wrap"><h1>供应商管理</h1><p>供应商列表和添加/编辑功能将在这里实现。</p></div>';
}

/**
 * 绩效评估页面
 */
function spe_evaluations_page() {
    // 这里实现绩效评估界面
    echo '<div class="wrap"><h1>绩效评估</h1><p>评估表单和评估记录将在这里实现。</p></div>';
}

/**
 * 评估报表页面
 */
function spe_reports_page() {
    // 这里实现报表界面
    echo '<div class="wrap"><h1>评估报表</h1><p>数据可视化报表将在这里显示。</p></div>';
}

插件使用说明

安装与激活

  1. 将整个插件文件夹上传到/wp-content/plugins/目录
  2. 在WordPress后台的"插件"页面找到"供应商绩效评估系统"
  3. 点击"激活"按钮启用插件

基本使用流程

  1. 添加供应商:在"供应商管理"页面添加新的供应商信息
  2. 设置评估指标:系统已预设了5个评估维度,您可以在代码中修改或扩展
  3. 进行评估:在"绩效评估"页面为供应商进行评分
  4. 查看结果:在仪表板和报表页面查看供应商绩效排名和详细分析

扩展与定制建议

添加更多评估维度

要添加新的评估维度,您需要:

  1. 在数据库表中添加新的评分字段
  2. 更新评估表单和处理逻辑
  3. 修改分数计算方法

集成可视化图表

可以考虑集成Chart.js或Google Charts来创建更丰富的可视化报表:

// 示例:使用Chart.js创建绩效趋势图
// 示例:使用Chart.js创建绩效趋势图
function createPerformanceChart(supplierId) {
    // 通过AJAX获取评估数据
    jQuery.ajax({
        url: spe_ajax.ajax_url,
        type: 'POST',
        data: {
            action: 'spe_get_evaluation_history',
            supplier_id: supplierId,
            nonce: spe_ajax.nonce
        },
        success: function(response) {
            if (response.success) {
                const data = response.data;
                const ctx = document.getElementById('performanceChart').getContext('2d');
                
                new Chart(ctx, {
                    type: 'line',
                    data: {
                        labels: data.dates,
                        datasets: [
                            {
                                label: '产品质量',
                                data: data.quality_scores,
                                borderColor: '#FF6384',
                                fill: false
                            },
                            {
                                label: '交货准时率',
                                data: data.delivery_scores,
                                borderColor: '#36A2EB',
                                fill: false
                            },
                            {
                                label: '综合评分',
                                data: data.overall_scores,
                                borderColor: '#4BC0C0',
                                borderWidth: 3,
                                fill: false
                            }
                        ]
                    },
                    options: {
                        responsive: true,
                        title: {
                            display: true,
                            text: '供应商绩效趋势图'
                        },
                        scales: {
                            y: {
                                beginAtZero: true,
                                max: 100
                            }
                        }
                    }
                });
            }
        }
    });
}

添加数据导出功能

<?php
// includes/class-export-manager.php

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

class SPE_Export_Manager {
    
    /**
     * 导出供应商评估数据为CSV
     */
    public static function export_evaluations_csv() {
        global $wpdb;
        
        $evaluations_table = $wpdb->prefix . 'spe_evaluations';
        $suppliers_table = $wpdb->prefix . 'spe_suppliers';
        
        $query = "SELECT 
                    s.name as supplier_name,
                    e.evaluation_date,
                    e.quality_score,
                    e.delivery_score,
                    e.price_score,
                    e.service_score,
                    e.communication_score,
                    e.overall_score,
                    e.comments,
                    u.display_name as evaluator
                  FROM {$evaluations_table} e
                  LEFT JOIN {$suppliers_table} s ON e.supplier_id = s.id
                  LEFT JOIN {$wpdb->users} u ON e.evaluator_id = u.ID
                  ORDER BY e.evaluation_date DESC";
        
        $results = $wpdb->get_results($query, ARRAY_A);
        
        // 设置CSV头部
        header('Content-Type: text/csv; charset=utf-8');
        header('Content-Disposition: attachment; filename=supplier-evaluations-' . date('Y-m-d') . '.csv');
        
        $output = fopen('php://output', 'w');
        
        // 添加BOM头,确保Excel正确识别UTF-8编码
        fwrite($output, "xEFxBBxBF");
        
        // 写入列标题
        if (!empty($results)) {
            fputcsv($output, array_keys($results[0]));
            
            // 写入数据行
            foreach ($results as $row) {
                fputcsv($output, $row);
            }
        }
        
        fclose($output);
        exit;
    }
    
    /**
     * 生成供应商绩效报告PDF
     */
    public static function generate_supplier_report_pdf($supplier_id) {
        // 这里可以使用TCPDF或Dompdf库生成PDF报告
        // 由于篇幅限制,这里只展示框架
        
        require_once SPE_PLUGIN_DIR . 'vendor/autoload.php';
        
        $supplier_manager = new SPE_Supplier_Manager();
        $evaluation_manager = new SPE_Evaluation_Manager();
        
        $supplier = $supplier_manager->get_supplier($supplier_id);
        $evaluations = $evaluation_manager->get_supplier_evaluations($supplier_id);
        $average_scores = $evaluation_manager->calculate_supplier_score($supplier_id);
        
        // 创建PDF文档
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        
        // 设置文档信息
        $pdf->SetCreator('供应商绩效评估系统');
        $pdf->SetAuthor('WordPress插件');
        $pdf->SetTitle('供应商绩效报告 - ' . $supplier['name']);
        
        // 添加页面
        $pdf->AddPage();
        
        // 添加内容
        $html = '<h1>供应商绩效评估报告</h1>';
        $html .= '<h2>' . esc_html($supplier['name']) . '</h2>';
        $html .= '<p>生成日期: ' . date('Y年m月d日') . '</p>';
        
        // 添加分数表格
        if ($average_scores) {
            $html .= '<h3>平均绩效分数</h3>';
            $html .= '<table border="1" cellpadding="5">';
            $html .= '<tr><th>评估维度</th><th>平均分数</th></tr>';
            $html .= '<tr><td>产品质量</td><td>' . number_format($average_scores['avg_quality'], 1) . '</td></tr>';
            $html .= '<tr><td>交货准时率</td><td>' . number_format($average_scores['avg_delivery'], 1) . '</td></tr>';
            $html .= '<tr><td>价格竞争力</td><td>' . number_format($average_scores['avg_price'], 1) . '</td></tr>';
            $html .= '<tr><td>综合评分</td><td><strong>' . number_format($average_scores['avg_overall'], 1) . '</strong></td></tr>';
            $html .= '</table>';
        }
        
        $pdf->writeHTML($html, true, false, true, false, '');
        
        // 输出PDF
        $pdf->Output('supplier-report-' . $supplier_id . '.pdf', 'D');
    }
}

添加AJAX处理功能

<?php
// includes/class-ajax-handler.php

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

class SPE_Ajax_Handler {
    
    public function __construct() {
        add_action('wp_ajax_spe_get_supplier_data', array($this, 'get_supplier_data'));
        add_action('wp_ajax_spe_save_evaluation', array($this, 'save_evaluation'));
        add_action('wp_ajax_spe_get_evaluation_history', array($this, 'get_evaluation_history'));
        add_action('wp_ajax_spe_export_data', array($this, 'export_data'));
    }
    
    /**
     * 获取供应商数据
     */
    public function get_supplier_data() {
        // 验证nonce
        if (!wp_verify_nonce($_POST['nonce'], 'spe_ajax_nonce')) {
            wp_die('安全验证失败');
        }
        
        $supplier_id = intval($_POST['supplier_id']);
        $supplier_manager = new SPE_Supplier_Manager();
        $supplier = $supplier_manager->get_supplier($supplier_id);
        
        if ($supplier) {
            wp_send_json_success($supplier);
        } else {
            wp_send_json_error('供应商不存在');
        }
    }
    
    /**
     * 保存评估数据
     */
    public function save_evaluation() {
        // 验证nonce和权限
        if (!wp_verify_nonce($_POST['nonce'], 'spe_ajax_nonce') || 
            !current_user_can('manage_options')) {
            wp_die('权限不足');
        }
        
        $data = array(
            'supplier_id' => intval($_POST['supplier_id']),
            'quality_score' => intval($_POST['quality_score']),
            'delivery_score' => intval($_POST['delivery_score']),
            'price_score' => intval($_POST['price_score']),
            'service_score' => intval($_POST['service_score']),
            'communication_score' => intval($_POST['communication_score']),
            'comments' => sanitize_textarea_field($_POST['comments'])
        );
        
        $evaluation_manager = new SPE_Evaluation_Manager();
        $result = $evaluation_manager->add_evaluation($data);
        
        if ($result) {
            wp_send_json_success(array(
                'message' => '评估已保存',
                'evaluation_id' => $result
            ));
        } else {
            wp_send_json_error('保存失败');
        }
    }
    
    /**
     * 获取评估历史数据
     */
    public function get_evaluation_history() {
        $supplier_id = intval($_POST['supplier_id']);
        $evaluation_manager = new SPE_Evaluation_Manager();
        $evaluations = $evaluation_manager->get_supplier_evaluations($supplier_id);
        
        $dates = array();
        $quality_scores = array();
        $delivery_scores = array();
        $overall_scores = array();
        
        foreach ($evaluations as $eval) {
            $dates[] = date('Y-m-d', strtotime($eval['evaluation_date']));
            $quality_scores[] = $eval['quality_score'];
            $delivery_scores[] = $eval['delivery_score'];
            $overall_scores[] = $eval['overall_score'];
        }
        
        wp_send_json_success(array(
            'dates' => $dates,
            'quality_scores' => $quality_scores,
            'delivery_scores' => $delivery_scores,
            'overall_scores' => $overall_scores
        ));
    }
    
    /**
     * 导出数据
     */
    public function export_data() {
        if (!current_user_can('manage_options')) {
            wp_die('权限不足');
        }
        
        $export_type = sanitize_text_field($_GET['type']);
        
        switch ($export_type) {
            case 'csv':
                SPE_Export_Manager::export_evaluations_csv();
                break;
            case 'pdf':
                $supplier_id = intval($_GET['supplier_id']);
                SPE_Export_Manager::generate_supplier_report_pdf($supplier_id);
                break;
            default:
                wp_die('不支持的导出格式');
        }
    }
}

// 初始化AJAX处理器
new SPE_Ajax_Handler();

添加短代码支持

<?php
// includes/class-shortcodes.php

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

class SPE_Shortcodes {
    
    public function __construct() {
        add_shortcode('supplier_performance_table', array($this, 'performance_table_shortcode'));
        add_shortcode('supplier_evaluation_form', array($this, 'evaluation_form_shortcode'));
    }
    
    /**
     * 显示供应商绩效表格的短代码
     */
    public function performance_table_shortcode($atts) {
        $atts = shortcode_atts(array(
            'limit' => 10,
            'show_rank' => true
        ), $atts, 'supplier_performance_table');
        
        $evaluation_manager = new SPE_Evaluation_Manager();
        $rankings = $evaluation_manager->get_supplier_rankings($atts['limit']);
        
        ob_start();
        ?>
        <div class="spe-frontend-table">
            <h3>供应商绩效排名</h3>
            <table class="spe-performance-table">
                <thead>
                    <tr>
                        <?php if ($atts['show_rank']): ?>
                            <th>排名</th>
                        <?php endif; ?>
                        <th>供应商名称</th>
                        <th>平均分数</th>
                        <th>评估次数</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($rankings)): ?>
                        <?php $rank = 1; ?>
                        <?php foreach ($rankings as $supplier): ?>
                            <tr>
                                <?php if ($atts['show_rank']): ?>
                                    <td><?php echo $rank++; ?></td>
                                <?php endif; ?>
                                <td><?php echo esc_html($supplier['name']); ?></td>
                                <td>
                                    <div class="spe-score-bar">
                                        <div class="spe-score-fill" 
                                             style="width: <?php echo ($supplier['avg_score'] / 100) * 100; ?>%">
                                            <?php echo number_format($supplier['avg_score'], 1); ?>
                                        </div>
                                    </div>
                                </td>
                                <td><?php echo $supplier['evaluation_count']; ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="<?php echo $atts['show_rank'] ? 4 : 3; ?>">
                                暂无评估数据
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <style>
            .spe-performance-table {
                width: 100%;
                border-collapse: collapse;
                margin: 20px 0;
            }
            .spe-performance-table th,
            .spe-performance-table td {
                padding: 12px;
                border: 1px solid #ddd;
                text-align: left;
            }
            .spe-performance-table th {
                background-color: #f2f2f2;
                font-weight: bold;
            }
            .spe-score-bar {
                width: 100%;
                height: 24px;
                background-color: #f0f0f0;
                border-radius: 12px;
                overflow: hidden;
                position: relative;
            }
            .spe-score-fill {
                height: 100%;
                background: linear-gradient(90deg, #4CAF50, #8BC34A);
                color: white;
                text-align: center;
                line-height: 24px;
                font-size: 12px;
                transition: width 0.3s ease;
            }
        </style>
        <?php
        
        return ob_get_clean();
    }
    
    /**
     * 显示评估表单的短代码
     */
    public function evaluation_form_shortcode($atts) {
        // 仅限登录用户使用
        if (!is_user_logged_in()) {
            return '<p>请先登录以进行供应商评估。</p>';
        }
        
        $atts = shortcode_atts(array(
            'supplier_id' => 0
        ), $atts, 'supplier_evaluation_form');
        
        $supplier_manager = new SPE_Supplier_Manager();
        $suppliers = $supplier_manager->get_all_suppliers('active');
        
        ob_start();
        ?>
        <div class="spe-evaluation-form-container">
            <h3>供应商绩效评估</h3>
            
            <form id="speEvaluationForm" method="post">
                <?php wp_nonce_field('spe_evaluation_action', 'spe_evaluation_nonce'); ?>
                
                <div class="form-group">
                    <label for="supplier_id">选择供应商:</label>
                    <select name="supplier_id" id="supplier_id" required>
                        <option value="">请选择供应商</option>
                        <?php foreach ($suppliers as $supplier): ?>
                            <option value="<?php echo $supplier['id']; ?>"
                                <?php selected($atts['supplier_id'], $supplier['id']); ?>>
                                <?php echo esc_html($supplier['name']); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="form-group">
                    <label>产品质量:</label>
                    <div class="score-slider">
                        <input type="range" name="quality_score" min="0" max="100" value="50" 
                               class="score-input" data-label="quality-label">
                        <span id="quality-label">50</span>
                    </div>
                </div>
                
                <div class="form-group">
                    <label>交货准时率:</label>
                    <div class="score-slider">
                        <input type="range" name="delivery_score" min="0" max="100" value="50" 
                               class="score-input" data-label="delivery-label">
                        <span id="delivery-label">50</span>
                    </div>
                </div>
                
                <div class="form-group">
                    <label>价格竞争力:</label>
                    <div class="score-slider">
                        <input type="range" name="price_score" min="0" max="100" value="50" 
                               class="score-input" data-label="price-label">
                        <span id="price-label">50</span>
                    </div>
                </div>
                
                <div class="form-group">
                    <label>客户服务:</label>
                    <div class="score-slider">
                        <input type="range" name="service_score" min="0" max="100" value="50" 
                               class="score-input" data-label="service-label">
                        <span id="service-label">50</span>
                    </div>
                </div>
                
                <div class="form-group">
                    <label>沟通效率:</label>
                    <div class="score-slider">
                        <input type="range" name="communication_score" min="0" max="100" value="50" 
                               class="score-input" data-label="communication-label">
                        <span id="communication-label">50</span>
                    </div>
                </div>
                
                <div class="form-group">
                    <label>评价意见:</label>
                    <textarea name="comments" rows="4" 
                              placeholder="请输入具体的评价意见..."></textarea>
                </div>
                
                <div class="form-group">
                    <button type="submit" class="submit-button">提交评估</button>
                    <div id="form-message" style="display:none;"></div>
                </div>
            </form>
        </div>
        
        <script>
        jQuery(document).ready(function($) {
            // 更新滑块值显示
            $('.score-input').on('input', function() {
本文来自网络投稿,不代表本站点的立场,转载请注明出处:https://www.gongxiangcang.com/6551.html

溯源库®作者

漳州柔性供应链服务有限公司 小批量订单定制化服务商( 投稿邮箱:vip@jiaochengku.com)
上一篇
下一篇

为您推荐

联系我们

联系我们

18559313275

在线咨询: QQ交谈

邮箱: vip@suyuanku.com

工作时间:周一至周五,9:00-17:30,节假日休息
关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部