관리-도구
편집 파일: CFCourse_cirtificate_manager.php
<?php use Dompdf\Dompdf as Dpdf; if (!class_exists('CFCourse_cirtificate_manager')) { class CFCourse_cirtificate_manager { public $loader; public $funnel_id; public $course_id; function __construct($arr) { $this->loader = $arr['loader']; $this->funnel_id = 0; $this->course_id = 0; } function export($funnel_id, $course_id) { if (isset($GLOBALS['cf_post_cert_data'])) { $cert_data = $GLOBALS['cf_post_cert_data']; $this->funnel_id = $funnel_id; $this->course_id = $course_id; if (isset($cert_data['export_cert_for_completion'])) { $exam_date = $cert_data['export_cert_for_completion']; $data = self::exportForCourseComplition('completion', compact('exam_date')); } else if (isset($cert_data['export_cert_for_exam'])) { $exam_type = $cert_data['cfcourse_cert_exam_type']; $exam_id = (int)cf_enc($cert_data['cfcourse_cert_exam_id'], 'decrypt'); $res_id = (int) cf_enc($cert_data['cfcourse_cert_res_id'], 'decrypt'); $data = self::exportForCourseComplition('exam', compact('exam_type', 'exam_id', 'res_id')); } else if (isset($cert_data['export_cert_for_down']) || isset($cert_data['share_cert_for_completion'])) { $exam_date = $cert_data['export_cert_for_down']; $data = self::exportForCourseComplition('completion', compact('exam_date')); } } } function exportForCourseComplition($type, $exam_data = array()) { if (get_current_member($this->funnel_id)) { $member = get_current_member($this->funnel_id); $quiz_ob = $this->loader->load("quiz_stud_control"); $mem_data = $quiz_ob->checkIfIsAdmin($member['id']); } else { $member = get_member($_GET['student_id']); $quiz_ob = $this->loader->load("quiz_stud_control"); $mem_data = $quiz_ob->checkIfIsAdmin($member['id']); } $mid = $mem_data['mid']; $content = ""; $page_size = "A4"; $page_orientation = "landscape"; if ($member) { $course_ob = $this->loader->load('course_control'); $setup = self::getSetup(); $course = $course_ob->get_course_by_id($this->course_id); $bg_img = plugins_url('../assets/image/cirtificate_bg/1.jpg', __FILE__); $export = false; if ($setup) { if ($type === 'completion') { if ($course['end_cirtificate'] && (int)$course_ob->getProgress($mid, $this->course_id) === 100) { $export = true; } } else if ($type === 'exam') { if ($exam_data['exam_type'] === 'quiz') { $quiz_data = $course_ob->get_quiz_by_id($exam_data['exam_id']); if ($quiz_data && (int)$quiz_data['show_cert'] === 1) { $export = true; } } else if ($exam_data['exam_type'] === 'assign' || $exam_data['exam_type'] === 'test') { $assign_ob = $this->loader->load('assign_stud_control'); $assign_data = $assign_ob->get_assign_by_id($exam_data['exam_id']); if ($assign_data && (int)$assign_data['show_cert'] === 1) { $export = true; } } $exam_status = []; if ($export) { $exam_status = self::getExamStatus($exam_data['exam_type'], $mid, $exam_data['res_id']); } } $page_size = $setup[$type . '_page_size']; $page_orientation = $setup[$type . '_page_orientation']; $theme = $setup[$type . '_page_theme']; if (is_numeric($theme)) { $bg_img = plugins_url('../assets/image/cirtificate_bg/' . $theme . '.jpg', __FILE__); } elseif (filter_var($theme, FILTER_VALIDATE_URL)) { $bg_img = $theme; } } if ($export) { $dir = plugin_dir_path(__FILE__); ob_start(); require_once($dir . '../views/cirtificate.php'); $content = ob_get_clean(); } } require_once('vendor/autoload.php'); if (strlen($content) > 0) { $app_loader = $GLOBALS['loader']; $sell_ob = $app_loader->loadSell(); $member_ob = $app_loader->loadMember(); $content = $member_ob->membrshipTemplatecreator($content, array($member)); $content = $sell_ob->productTemplatecreator($content, array($this->course_id)); if ($type === 'exam') { $content = arrayIndexToStr($content, $exam_status); } } if (isset($GLOBALS['export_cert_for_share'])) { $file_path = 'plugins/cf_course/assets/image/certificate/'; if (isset($_GET['student_id'])) { $file_name = $file_path . 'certificate' . $_GET['student_id'] . $_GET['course_id'] . $_GET['funnel_id'] . '.html'; } else { $file_name = $file_path . 'certificateu' . $_GET['id'] . $_GET['content'] . '.html'; } $content = '<html><head><meta property="og:url" content="' . $file_name . '" /><meta property="og:type" content="article" /><meta property="og:title" content="I successfully completed the ' . $course['title'] . ' course" /><meta property="og:description" content="sfdsaf" /> <meta property="og:image" content="http://static01.nyt.com/images/2015/02/19/arts/international/19iht-btnumbers19A/19iht-btnumbers19A-facebookJumbo-v2.jpg" /></head>' . $content; $content .= "<style> img{ padding-left:5%; } </style>"; $content = self::replceImages($content); ob_end_clean(); if (!file_exists($file_name)) { file_put_contents($file_name, $content); } else { unlink($file_name); file_put_contents($file_name, $content); } if (isset($GLOBALS['export_cert_for_share']['submit_button_twitter'])) { $share_url = 'https://twitter.com/intent/tweet?text=' . get_option('install_url') . '/' . $file_name; echo "<script>window.open('$share_url', 'popup', 'width=600,height=400,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');</script>"; } else if (isset($GLOBALS['export_cert_for_share']['submit_button_fb'])) { $share_url = 'https://www.facebook.com/sharer/sharer.php?u=' . get_option('install_url') . '/' . $file_name; echo "<script>window.open('$share_url', 'popup', 'width=600,height=400,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');</script>"; } else if (isset($GLOBALS['export_cert_for_share']['submit_button_linkedin'])) { $share_url = 'https://www.linkedin.com/sharing/share-offsite/?url=' . get_option('install_url') . '/' . $file_name; echo "<script>window.open('$share_url', 'popup', 'width=600,height=400,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');</script>"; } else if (isset($GLOBALS['export_cert_for_share']['submit_button_google'])) { $share_url = 'https://plus.google.com/share?url=' . get_option('install_url') . '/' . $file_name; echo "<script>window.open('$share_url', 'popup', 'width=600,height=400,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');</script>"; } else if (isset($GLOBALS['export_cert_for_share']['submit_button_instagram'])) { $share_url = 'https://www.instagram.com/accounts/login/?next=' . get_option('install_url') . '/' . $file_name; echo "<script>window.open('$share_url', 'popup', 'width=600,height=400,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');</script>"; } else if (isset($GLOBALS['export_cert_for_share']['submit_button_telegram'])) { $share_url = 'https://telegram.me/share/url?url=' . get_option('install_url') . '/' . $file_name; echo "<script>window.open('$share_url', 'popup', 'width=600,height=400,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');</script>"; } else { die('Somethink went wrong'); } } else { $content .= "<style> img{ widht:100%; } </style>"; $content = self::replceImages($content); ob_end_clean(); $pdf = new Dpdf(); $pdf->loadHtml($content); $pdf->setPaper($page_size, $page_orientation); $pdf->render(); $pdf->stream(); } } function getSetup($index = false) { $setup = get_option('cfcourse_cirtification_setup'); if ($setup) { $setup = (array)json_decode($setup); if (is_array($setup) && count($setup) > 0) { if ($index !== false) { return $setup[$index]; } else { return $setup; } } } return false; } function createDefaultSetup() { $this->setSetup( array( 'completion_content' => "", 'completion_page_size' => 'A4', 'completion_page_orientation' => 'portrait', 'completion_page_theme' => '1', 'exam_content' => "", 'exam_page_size' => "A4", 'exam_page_orientation' => "portrait", 'exam_page_theme' => '1' ), true ); } function createPageSize($current = 'A4') { $arr = array(); for ($i = 1; $i <= 10; $i++) { $selected = ""; if ($current == 'A' . $i) { $selected = " selected"; } array_push($arr, "<option" . $selected . ">A" . $i . "</option>"); } return implode("", $arr); } function setSetup($data = array(), $init = false) { $arr = array( 'completion_content' => $data['completion_content'], 'completion_page_size' => $data['completion_page_size'], 'completion_page_orientation' => $data['completion_page_orientation'], 'completion_page_theme' => $data['completion_page_theme'], 'exam_content' => $data['exam_content'], 'exam_page_size' => $data['exam_page_size'], 'exam_page_orientation' => $data['exam_page_orientation'], 'exam_page_theme' => $data['exam_page_theme'], ); $jsn = json_encode($arr); if ($init && !get_option('cfcourse_cirtification_setup')) { add_option('cfcourse_cirtification_setup', $jsn); } else if (!$init) { update_option('cfcourse_cirtification_setup', $jsn); } return true; } function openManager() { self::createDefaultSetup(); $path = plugin_dir_path(__FILE__); if (isset($_POST['save_cirtificate_setup'])) { self::setSetup($_POST); echo "<script>window.location='';</script>"; } $setup = self::getSetup(); require_once($path . '../views/manage_cirtificate.php'); } function replceImages($content) { $reg = "/src(\s)*=(\s)*(\"|')(.(?!(\"|')))+.(\"|')/i"; $content = preg_replace_callback($reg, function ($data) { $data = $data[0]; $data = preg_replace("/(src=|\"|')/i", "", $data); $data = trim($data); $data = self::readImage($data); return 'src="' . $data . '"'; }, $content); return $content; } function readImage($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $data = curl_exec($ch); $data = 'data:image/png;base64,' . base64_encode($data); return $data; } function getExamStatus($type, $mem_id, $content_id) { global $mysqli; global $dbpref; $mem_id = $mysqli->real_escape_string($mem_id); $type = $mysqli->real_escape_string($type); $content_id = $mysqli->real_escape_string($content_id); $res = array( 'candidate_id' => 0, 'student_id' => 0, 'candidate_name' => "", 'candidate_email' => "", 'exam_name' => "", 'exam_description' => "", 'exam_id' => "", 'correct_answers' => 0, 'total_questions' => 0, 'number_of_attempts' => 0, 'number_of_unanswerd' => 0, 'result_in_percentage' => 0, 'result_status' => '', 'time_spent' => 0, 'result_out_date' => '0000-00-00 00:00:00', ); if ($type == "assign" || $type == "test") { $table = $dbpref . "stud_assignment"; $sql = "SELECT * FROM `" . $table . "` WHERE `id`=$content_id AND `member_id`=" . $mem_id . " order by `id` desc limit 1"; $qry = $mysqli->query($sql); if ($qry->num_rows > 0) { $r = $qry->fetch_assoc(); $res['candidate_id'] = cf_enc($r['id']); $res['student_id'] = cf_enc($r['member_id']); $res['candidate_name'] = $r['name']; $res['candidate_email'] = $r['email']; $res['exam_name'] = $r['assign_title']; $res['exam_description'] = $r['assign_desc']; $res['exam_id'] = cf_enc($r['assign_id']); $res['correct_answers'] = $r['no_correct_answers']; $res['total_questions'] = $r['no_of_questions']; $r['attempt_answer'] = json_decode($r['attempt_answer']); $attempt_answered = (isset($r['attempt_answer']->answerd)) ? $r['attempt_answer']->answerd : 0; $attempt_unanswerd = (isset($r['attempt_answer']->unanswerd)) ? $r['attempt_answer']->unanswerd : 0; $res['number_of_attempts'] = $attempt_answered; $res['number_of_unanswerd'] = $attempt_unanswerd; $res['result_in_percentage'] = $r['percentage']; $res['result_status'] = $r['comment']; $res['time_spent'] = $r['time_spent']; $res['result_out_date'] = date("d M Y", strtotime($r['result_out_date'])); } } else if ($type == "quiz") { $table = $dbpref . "stud_quiz"; $sql = "SELECT * FROM `" . $table . "` WHERE `id`=$content_id AND `member_id`=" . $mem_id . " order by `id` desc limit 1"; $qry = $mysqli->query($sql); if ($qry->num_rows > 0) { $r = $qry->fetch_assoc(); $res['candidate_id'] = cf_enc($r['id']); $res['student_id'] = cf_enc($r['member_id']); $res['candidate_name'] = $r['name']; $res['candidate_email'] = $r['email']; $res['exam_name'] = $r['quiz_title']; $res['exam_description'] = $r['quiz_description']; $res['exam_id'] = cf_enc($r['quiz_id']); $res['correct_answers'] = $r['no_correct_answers']; $res['total_questions'] = $r['no_of_questions']; $r['attempt_answer'] = json_decode($r['attempt_answer']); $attempt_answered = (isset($r['attempt_answer']->answerd)) ? $r['attempt_answer']->answerd : 0; $attempt_unanswerd = (isset($r['attempt_answer']->unanswerd)) ? $r['attempt_answer']->unanswerd : 0; $res['number_of_attempts'] = $attempt_answered; $res['number_of_unanswerd'] = $attempt_unanswerd; $res['result_in_percentage'] = $r['percentage']; $res['result_status'] = $r['comment']; $res['time_spent'] = $r['time_spent']; $res['result_out_date'] = date("d M Y", strtotime($r['result_out_date'])); } } return $res; } } }