Exemplo n.º 1
0
<?php

defined('_JLMS_EXEC') or die('Restricted access');
require_once _JOOMLMS_FRONT_HOME . "/includes/libchart/libchart.php";
JLMS_cleanLibChartCache();
function JLMS_GraphStatistics($option, $id, $quiz_id, $i, $z, $row = array(), $c_question_id = 0, $group_id = 0, $str_user_in_groups = '', $no_js = 0)
{
    global $JLMS_DB, $JLMS_CONFIG;
    $obj = new stdClass();
    if (isset($row->c_type)) {
        switch ($row->c_type) {
            case '1':
            case '2':
            case '3':
            case '12':
            case '13':
                $query = "SELECT c_id FROM #__lms_quiz_t_choice WHERE c_question_id = '" . $c_question_id . "' ORDER BY ordering";
                $JLMS_DB->SetQuery($query);
                $choice_ids = $JLMS_DB->LoadResultArray();
                $choice_id = implode(',', $choice_ids);
                if (!$choice_id) {
                    $choice_id = '0';
                }
                $query = "SELECT COUNT(*) FROM #__lms_quiz_r_student_quiz as a, #__lms_quiz_r_student_question as q, #__lms_quiz_r_student_choice as c";
                $query .= "\n WHERE q.c_id = c.c_sq_id AND q.c_question_id = '" . $row->c_id . "' AND c.c_choice_id IN (" . $choice_id . ")";
                $query .= "\n AND a.c_id = q.c_stu_quiz_id";
                if ($group_id) {
                    $query .= "\n AND a.c_student_id IN (" . $str_user_in_groups . ")";
                }
                $JLMS_DB->setQuery($query);
                $total_count = $JLMS_DB->loadResult();
Exemplo n.º 2
0
function JLMS_sreportAccess($option, $is_full)
{
    global $JLMS_DB, $JLMS_CONFIG, $JLMS_SESSION, $my, $Itemid;
    $JLMS_ACL =& JLMSFactory::getACL();
    $view = mosGetParam($_REQUEST, 'view', '');
    if ($view == 'csv' || $view == 'xls') {
        $is_full = 1;
    }
    $start_date = mosGetParam($_REQUEST, 'start_date', "");
    $end_date = mosGetParam($_REQUEST, 'end_date', "");
    $limit = intval(mosGetParam($_GET, 'limit', $JLMS_SESSION->get('list_limit', $JLMS_CONFIG->get('list_limit'))));
    $JLMS_SESSION->set('list_limit', $limit);
    $limitstart = intval(mosGetParam($_GET, 'limitstart', 0));
    $filt_group = intval(mosGetParam($_REQUEST, 'filt_group', 0));
    $lists = array();
    $reporting_header = array();
    $filt_cat = intval(mosGetParam($_REQUEST, 'filt_cat', 0));
    //FLMS multicat
    $levels = array();
    if ($JLMS_CONFIG->get('multicat_use', 0)) {
        $query = "SELECT * FROM #__lms_course_cats_config ORDER BY id";
        $JLMS_DB->setQuery($query);
        $levels = $JLMS_DB->loadObjectList();
        if (count($levels) == 0) {
            for ($i = 0; $i < 5; $i++) {
                if ($i > 0) {
                    $levels[$i]->cat_name = _JLMS_COURSES_COURSES_GROUPS;
                } else {
                    $levels[$i]->cat_name = _JLMS_COURSES_COURSES_GROUPS;
                }
            }
        }
        $level_id = array();
        for ($i = 0; $i < count($levels); $i++) {
            if ($i == 0) {
                $level_id[$i] = intval(mosGetParam($_REQUEST, 'filter_id_' . $i . '', $JLMS_SESSION->get('FLMS_filter_id_' . $i . '', 0)));
                $_REQUEST['filter_id_' . $i] = $level_id[$i];
                $JLMS_SESSION->set('FLMS_filter_id_' . $i . '', $level_id[$i]);
            } else {
                $level_id[$i] = intval(mosGetParam($_REQUEST, 'filter_id_' . $i . '', $JLMS_SESSION->get('FLMS_filter_id_' . $i . '', 0)));
                $_REQUEST['filter_id_' . $i] = $level_id[$i];
                $JLMS_SESSION->set('FLMS_filter_id_' . $i . '', $level_id[$i]);
            }
            if ($i == 0) {
                $parent_id[$i] = 0;
            } else {
                $parent_id[$i] = $level_id[$i - 1];
            }
            if ($i == 0 || $parent_id[$i]) {
                //(Max): extra requests
                $query = "SELECT count(id) FROM `#__lms_course_cats` WHERE parent = '" . $parent_id[$i] . "' ORDER BY c_category";
                $JLMS_DB->setQuery($query);
                $groups = $JLMS_DB->loadResult();
                if ($groups == 0) {
                    $level_id[$i] = 0;
                    $JLMS_SESSION->set('FLMS_filter_id_' . $i . '', $level_id[$i]);
                }
            }
        }
        for ($i = 0; $i < count($levels); $i++) {
            if ($i > 0 && $level_id[$i - 1] == 0) {
                $level_id[$i] = 0;
                $_REQUEST['filter_id_' . $i] = $level_id[$i];
                $JLMS_SESSION->set('FLMS_filter_id_' . $i . '', $level_id[$i]);
                $parent_id[$i] = 0;
            } elseif ($i == 0 && $level_id[$i] == 0) {
                $level_id[$i] = 0;
                $_REQUEST['filter_id_' . $i] = $level_id[$i];
                $JLMS_SESSION->set('FLMS_filter_id_' . $i . '', $level_id[$i]);
                $parent_id[$i] = 0;
            }
        }
        $javascript = 'onclick="javascript:read_filter();" onchange="javascript:write_filter();submitFormView(\'\');"';
        $query1 = "SELECT group_id FROM `#__lms_users_in_global_groups` WHERE user_id = '" . $my->id . "'";
        $JLMS_DB->setQuery($query1);
        $user_group_ids = $JLMS_DB->loadResultArray();
        $categories_reporting = array();
        $name_categories_reporting = array();
        for ($i = 0; $i < count($levels); $i++) {
            if ($i == 0 || $parent_id[$i]) {
                //(Max): extra requests
                if ($parent_id[$i] == 0 && !$JLMS_ACL->CheckPermissions('lms', 'create_course')) {
                    $query = "SELECT * FROM `#__lms_course_cats` WHERE `parent` = '0'";
                    $query .= "\n AND (";
                    if (count($user_group_ids)) {
                        $query .= "( `restricted` = 1 AND ( `groups` LIKE '%|{$user_group_ids['0']}|%'";
                        for ($i1 = 1; $i1 < count($user_group_ids); $i1++) {
                            $query .= "\n OR `groups` like '%|{$user_group_ids[$i1]}|%'";
                        }
                        $query .= "\n ) ) \n OR ";
                    }
                    $query .= "(`restricted` = 0 )) ";
                    $query .= "\n ORDER BY `c_category`";
                } else {
                    $query = "SELECT * FROM `#__lms_course_cats` WHERE parent = '" . $parent_id[$i] . "' ORDER BY c_category";
                }
                $JLMS_DB->setQuery($query);
                $groups = $JLMS_DB->loadObjectList();
                if ($parent_id[$i] && $i > 0 && count($groups)) {
                    $type_level[$i][] = mosHTML::makeOption(0, ' &nbsp; ');
                    foreach ($groups as $group) {
                        $type_level[$i][] = mosHTML::makeOption($group->id, $group->c_category);
                        if ($group->id == $level_id[$i]) {
                            $name_categories_reporting[] = $levels[$i]->cat_name;
                            $categories_reporting[] = $group->c_category;
                        }
                    }
                    $lists['filter_' . $i . ''] = mosHTML::selectList($type_level[$i], 'filter_id_' . $i . '', 'class="inputbox" size="1" style="width: 100%;" ' . $javascript, 'value', 'text', $level_id[$i]);
                    //onchange="document.location.href=\''. $link_multi .'\';"
                } elseif ($i == 0) {
                    $type_level[$i][] = mosHTML::makeOption(0, ' &nbsp; ');
                    foreach ($groups as $group) {
                        $type_level[$i][] = mosHTML::makeOption($group->id, $group->c_category);
                        if ($group->id == $level_id[$i]) {
                            $name_categories_reporting[] = $levels[$i]->cat_name;
                            $categories_reporting[] = $group->c_category;
                        }
                    }
                    $lists['filter_' . $i . ''] = mosHTML::selectList($type_level[$i], 'filter_id_' . $i . '', 'class="inputbox" size="1" style="width: 100%;" ' . $javascript, 'value', 'text', $level_id[$i]);
                    //onchange="document.location.href=\''. $link_multi .'\';"
                }
            }
        }
        $reporting_header['name_categories'] = $name_categories_reporting;
        $reporting_header['categories'] = $categories_reporting;
    }
    //FLMS multicat
    $s_date_db = '';
    $start_date = $start_date == "-" ? "" : $start_date;
    $end_date = $end_date == "-" ? "" : $end_date;
    if ($start_date) {
        $start_date = JLMS_dateToDB($start_date);
        $s_date = explode('-', $start_date);
        $s_date_db = date("Y-m-d H:i:s", mktime(0, 0, 0, $s_date[1], $s_date[2], $s_date[0]));
    }
    $e_date_db = '';
    if ($end_date) {
        $end_date = JLMS_dateToDB($end_date);
        $e_date = explode('-', $end_date);
        $e_date_db = date("Y-m-d H:i:s", mktime(23, 59, 0, $e_date[1], $e_date[2], $e_date[0]));
    }
    $teacher_in_courses = $JLMS_CONFIG->get('teacher_in_courses', array());
    $parent_in_courses = array();
    $parent_in_courses = $JLMS_CONFIG->get('parent_in_courses', array());
    $courses = array_merge($teacher_in_courses, $parent_in_courses);
    //var_dump($courses);
    if (count($courses)) {
        $courses_str = implode(',', $courses);
        $JLMS_DB->setQuery('SELECT id FROM #__lms_courses WHERE id IN(' . $courses_str . ')');
        $courses = $JLMS_DB->loadResultArray();
        $g_items = array();
        $g_items[] = mosHTML::makeOption(0, _JLMS_ATT_FILTER_ALL_GROUPS);
        if ($JLMS_CONFIG->get('use_global_groups', 1)) {
            if (!count($courses)) {
                $courses = array(0);
            }
            $query = "SELECT user_id FROM #__lms_users_in_groups WHERE course_id IN (" . implode(',', $courses) . ")";
            $JLMS_DB->setQuery($query);
            $cid = $JLMS_DB->loadResultArray();
            if (!$cid) {
                $cid = array(-1);
            }
            $query = "SELECT group_id FROM #__lms_users_in_global_groups WHERE user_id IN (" . implode(',', $cid) . ")";
            $JLMS_DB->setQuery($query);
            $gid = $JLMS_DB->loadResultArray();
            if (!$gid) {
                $gid = array(-1);
            }
            $query = "SELECT distinct id AS value, ug_name AS text FROM #__lms_usergroups WHERE id IN (" . implode(',', $gid) . ") AND course_id = 0 ORDER BY text";
            //course id check just in case))
            $JLMS_DB->setQuery($query);
            $groups = $JLMS_DB->loadObjectList();
        } else {
            if (!count($courses)) {
                $courses = array(0);
            }
            $query = "SELECT distinct a.id as value, a.ug_name as text FROM #__lms_usergroups as a, #__lms_users_in_groups as b" . "\n WHERE a.course_id IN (" . implode(',', $courses) . ") AND b.group_id = a.id ORDER BY a.ug_name";
            $JLMS_DB->SetQuery($query);
            $groups = $JLMS_DB->LoadObjectList();
        }
        $g_items = array_merge($g_items, $groups);
        $link = "javascript:document.adminForm.submit();";
        $lists['filter'] = mosHTML::selectList($g_items, 'filt_group', 'class="inputbox" size="1" style="width: 100%;" onchange="' . $link . '"', 'value', 'text', $filt_group);
        $groups_reporting = array();
        foreach ($groups as $grp) {
            if ($filt_group && $grp->value == $filt_group) {
                $groups_reporting[] = $grp->text;
            }
        }
        $name_groups_reporting[] = 'Usergroup';
        $reporting_header['name_groups'] = $name_groups_reporting;
        $reporting_header['groups'] = $groups_reporting;
        //---
        $where = '';
        if ($JLMS_CONFIG->get('multicat_use', 0)) {
            //NEW MUSLTICATS
            //			$tmp_level = array();
            $last_catid = 0;
            $tmp_cats_filter = JLMS_getFilterMulticategories($last_catid);
            $catids = implode(",", $tmp_cats_filter);
            if ($last_catid && count($tmp_cats_filter)) {
                $where .= "\n AND ( cat_id IN (" . $catids . ")";
                if ($JLMS_CONFIG->get('sec_cat_use', 0)) {
                    foreach ($tmp_cats_filter as $tmp_cats_filter_one) {
                        $where .= "\n OR sec_cat LIKE '%|" . $tmp_cats_filter_one . "|%'";
                    }
                }
                $where .= "\n )";
            }
            //NEW MUSLTICATS
        }
        if (!$courses_str) {
            $courses_str = '0';
        }
        $query = "SELECT id FROM #__lms_courses WHERE id IN (" . $courses_str . ")" . $where . " ORDER BY course_name, id";
        $JLMS_DB->setQuery($query);
        $courses2 = $JLMS_DB->loadResultArray();
        $courses = $courses2;
        //---
        if ($JLMS_CONFIG->get('use_global_groups', 1) && $filt_group) {
            $query = "SELECT DISTINCT(a.id)" . "\n FROM #__users as a, #__lms_users_in_global_groups as c" . "\n WHERE a.id = c.user_id " . ($filt_group ? "\n AND c.group_id = '" . $filt_group . "'" : '');
        } else {
            $query = "SELECT DISTINCT(a.id)" . "\n FROM #__users as a, #__lms_users_in_groups as c" . "\n WHERE a.id = c.user_id AND c.course_id IN (" . $courses_str . ")" . ($filt_group ? "\n AND c.group_id = '" . $filt_group . "'" : '');
        }
        $JLMS_DB->SetQuery($query);
        $users2 = $JLMS_DB->LoadResultArray();
        require_once _JOOMLMS_FRONT_HOME . DS . "includes" . DS . "classes" . DS . "lms.pagination.php";
        $pageNav = new JLMSPageNav(count($users2), $limitstart, $limit);
        if ($JLMS_CONFIG->get('use_global_groups', 1) && $filt_group) {
            $query = "SELECT DISTINCT(a.id)" . "\n FROM #__users as a, #__lms_users_in_global_groups as c" . "\n WHERE a.id = c.user_id " . ($filt_group ? "\n AND c.group_id = '" . $filt_group . "'" : '') . "\n ORDER BY a.name " . (!$is_full ? "LIMIT {$pageNav->limitstart}, {$pageNav->limit}" : "");
        } else {
            $query = "SELECT DISTINCT(a.id)" . "\n FROM #__users as a, #__lms_users_in_groups as c" . "\n WHERE a.id = c.user_id AND c.course_id IN (" . $courses_str . ")" . ($filt_group ? "\n AND c.group_id = '" . $filt_group . "'" : '') . "\n ORDER BY a.name " . (!$is_full ? "LIMIT {$pageNav->limitstart}, {$pageNav->limit}" : "");
        }
        $JLMS_DB->SetQuery($query);
        $users = $JLMS_DB->LoadResultArray();
        //var_dump($users);
        if (count($users)) {
            // 21 May 2007
            // prepare tracking images
            // 18 August 2007 - changes (DEN) - added check for GD and FreeType support
            if ($JLMS_CONFIG->get('show_reports_images', 0)) {
                $generate_images = true;
            } else {
                $generate_images = false;
            }
            $msg = '';
            if (!function_exists('imageftbbox') || !function_exists('imagecreatetruecolor')) {
                $generate_images = false;
                $sec = false;
                if (!function_exists('imagecreatetruecolor')) {
                    $msg = 'This function requires GD 2.0.1 or later (2.0.28 or later is recommended).';
                    $sec = true;
                }
                if (!function_exists('imageftbbox')) {
                    $msg .= ($sec ? '<br />' : '') . 'This function is only available if PHP is compiled with freetype support.';
                }
            }
            // end of GD and FreeType support check
            $users_str = implode(',', $users);
            $new_image_ym_stats = new stdClass();
            if ($JLMS_CONFIG->get('temp_folder', '') && $generate_images) {
                // temp folder setup is ready.
                $img_gl_width = $JLMS_CONFIG->get('visual_set_tracking_image_base_width', 400);
                //parameter added 09.06.2007
                $img_gl_height = $JLMS_CONFIG->get('visual_set_tracking_image_base_height', 260);
                //parameter added 09.06.2007
                require_once _JOOMLMS_FRONT_HOME . "/includes/libchart/libchart.php";
                JLMS_cleanLibChartCache();
                // Year/Month statistic
                $chart = new PieChart($img_gl_width * 2, $img_gl_height + 40, false, 'no_title');
                //(700, 70 + count($img_names)*30);
                $query = "SELECT COUNT(*) as hits,c.course_name FROM #__lms_track_hits as h LEFT JOIN #__users as u ON h.user_id=u.id LEFT JOIN #__lms_courses as c ON h.course_id = c.id WHERE h.course_id IN (" . $courses_str . ") AND h.user_id IN  (" . implode(',', $users2) . ") " . ($s_date_db ? "AND h.track_time > '" . $s_date_db . "'" : "") . " " . ($e_date_db ? "AND h.track_time < '" . $e_date_db . "'" : "") . "  group by h.course_id ORDER BY hits desc LIMIT 10";
                $JLMS_DB->SetQuery($query);
                $img_names = $JLMS_DB->LoadObjectLIST();
                //var_dump($img_names);
                for ($i = 0, $n = count($img_names); $i < $n; $i++) {
                    $chart->addPoint(new Point($img_names[$i]->course_name, $img_names[$i]->hits));
                }
                $title = "Top 10 courses";
                //$title = JLMS_TR_temp_fix($title);
                $mas[0] = $title;
                $title = '';
                $chart->setTitle($title);
                $filename = time() . '_' . md5(uniqid(rand(), true)) . ".png";
                $new_image_ym_stats->filename = $filename;
                $new_image_ym_stats->width = $img_gl_width;
                $new_image_ym_stats->height = $img_gl_height;
                $new_image_ym_stats->alt = $title;
                $new_image_ym_stats->title = $mas[0];
                $chart->render($JLMS_CONFIG->get('absolute_path') . "/" . $JLMS_CONFIG->get('temp_folder', '') . "/{$filename}");
            }
            $query = "SELECT COUNT(*) as hits,h.course_id as c_id FROM #__lms_track_hits as h LEFT JOIN #__users as u ON h.user_id=u.id LEFT JOIN #__lms_courses as c ON h.course_id = c.id WHERE h.course_id IN (" . $courses_str . ") AND h.user_id IN  (" . implode(',', $users2) . ") " . ($s_date_db ? "AND h.track_time > '" . $s_date_db . "'" : "") . " " . ($e_date_db ? "AND h.track_time < '" . $e_date_db . "'" : "") . "   group by h.course_id ";
            $JLMS_DB->SetQuery($query);
            $tot_hits = $JLMS_DB->LoadObjectLIST();
            $query = "SELECT COUNT(*) as hits,h.user_id as usr_id,h.course_id as c_id FROM #__lms_track_hits as h LEFT JOIN #__users as u ON h.user_id=u.id LEFT JOIN #__lms_courses as c ON h.course_id = c.id WHERE h.course_id IN (" . $courses_str . ") AND h.user_id IN  (" . $users_str . ") " . ($s_date_db ? "AND h.track_time > '" . $s_date_db . "'" : "") . " " . ($e_date_db ? "AND h.track_time < '" . $e_date_db . "'" : "") . "   group by h.course_id,h.user_id ORDER BY h.course_id,h.user_id";
            $JLMS_DB->SetQuery($query);
            $hits = $JLMS_DB->LoadObjectLIST();
            //var_dump($hits);
            if ($view == 'csv') {
                JLMS_REP_exportCsv($hits, $tot_hits, $users, $courses, $pageNav, $lists, $levels, $filt_cat, $filt_group, $option, 1);
            } else {
                if ($view == 'xls') {
                    JLMS_REP_exportXLS($hits, $tot_hits, $users, $courses, $reporting_header);
                } else {
                    JLMS_reports_html::JLMS_sreportAccess($tot_hits, $new_image_ym_stats, $hits, $users, $courses, $pageNav, $start_date, $end_date, $lists, $levels, $filt_cat, $filt_group, $option, $is_full);
                }
            }
        }
    }
}
Exemplo n.º 3
0
 function JLMS_outputCertificate($id, $course_id, $txt_mes_obj = null, $user_obj = null)
 {
     global $JLMS_DB, $JLMS_CONFIG, $my;
     if (is_null($user_obj)) {
         $user_obj = new stdClass();
         $user_obj->id = $my->id;
         $user_obj->username = $my->username;
         $user_obj->email = $my->email;
         $user_obj->name = $my->name;
     }
     $JLMS_ACL =& JLMSFactory::getACL();
     $is_preview = false;
     $is_exist = false;
     $quiz_id = 0;
     $quiz_name = '';
     $course_name = '';
     if (isset($txt_mes_obj->is_preview)) {
         $is_preview = $txt_mes_obj->is_preview;
     }
     if (isset($txt_mes_obj->quiz_id)) {
         $quiz_id = $txt_mes_obj->quiz_id;
     }
     if (isset($txt_mes_obj->quiz_name)) {
         $quiz_name = $txt_mes_obj->quiz_name;
     }
     if (isset($txt_mes_obj->course_name)) {
         $course_name = $txt_mes_obj->course_name;
     }
     $do_s = true;
     $crtf_role = 0;
     if ($is_preview) {
         $crtf_role = intval(mosGetParam($_REQUEST, 'crtf_role', 0));
     } else {
         $crtf_role = intval($JLMS_ACL->GetRole(1));
     }
     if ($crtf_role) {
         $query = "SELECT a.*, b.course_name FROM #__lms_certificates as a, #__lms_courses as b WHERE a.course_id = '" . $course_id . "' AND a.course_id = b.id AND a.parent_id = {$id} AND a.crtf_type = {$crtf_role}";
         $JLMS_DB->SetQuery($query);
         $crts = $JLMS_DB->loadObjectList();
         if (count($crts) == 1) {
             if ($crts[0]->file_id) {
                 $do_s = false;
             } else {
                 $query = "SELECT file_id FROM #__lms_certificates as a WHERE a.id = '" . $id . "' AND a.course_id = '" . $course_id . "' AND a.parent_id = 0";
                 $JLMS_DB->SetQuery($query);
                 $crts[0]->file_id = $JLMS_DB->LoadResult();
                 if ($crts[0]->file_id) {
                     $do_s = false;
                 }
             }
         }
     }
     if ($do_s) {
         $query = "SELECT a.*, b.course_name FROM #__lms_certificates as a, #__lms_courses as b WHERE a.id = '" . $id . "' AND a.course_id = '" . $course_id . "' AND a.course_id = b.id AND a.parent_id = 0";
         $JLMS_DB->SetQuery($query);
         $crts = $JLMS_DB->loadObjectList();
     }
     if (count($crts) == 1) {
         $is_duplicate = false;
         $print_duplicate_watermark = $JLMS_CONFIG->get('crtf_duplicate_wm', true);
         $crt = $crts[0];
         $JLMS_DB->SetQuery("SELECT file_srv_name FROM #__lms_files WHERE id = '" . $crt->file_id . "'");
         $cert_name = $JLMS_DB->LoadResult();
         if ($cert_name) {
             $ucode = md5(uniqid(rand(), true));
             $ex_crtf_id = 0;
             $is_saved_on_server = false;
             $ucode = substr($ucode, 0, 10);
             if (!$is_preview) {
                 $query = "SELECT * FROM #__lms_certificate_prints WHERE user_id = {$user_obj->id} AND role_id = {$crtf_role} AND course_id = {$course_id} AND crtf_id = {$id} AND quiz_id = {$quiz_id}";
                 $JLMS_DB->SetQuery($query);
                 $cr_pr = $JLMS_DB->LoadObject();
                 if (is_object($cr_pr) && isset($cr_pr->id)) {
                     $is_exist = $cr_pr->id;
                     $ex_crtf_id = $cr_pr->id;
                     $ucode = $cr_pr->uniq_id;
                     $txt_mes_obj->name = $cr_pr->name;
                     $txt_mes_obj->username = $cr_pr->username;
                     //$txt_mes_obj->course_name = $cr_pr->course_name;
                     if (isset($txt_mes_obj->force_update_print_date) && $txt_mes_obj->force_update_print_date && isset($txt_mes_obj->crtf_date) && $txt_mes_obj->crtf_date) {
                         $query = "UPDATE #__lms_certificate_prints SET crtf_date = '" . $txt_mes_obj->crtf_date . "' WHERE id = " . $cr_pr->id . " AND user_id = {$user_obj->id} AND role_id = {$crtf_role} AND course_id = {$course_id} AND crtf_id = {$id} AND quiz_id = {$quiz_id}";
                         $JLMS_DB->SetQuery($query);
                         $JLMS_DB->query();
                     } else {
                         $txt_mes_obj->crtf_date = strtotime($cr_pr->crtf_date);
                     }
                     $is_duplicate = true;
                     if ($JLMS_CONFIG->get('save_certificates', 1)) {
                         $im_crtf_path = $JLMS_CONFIG->get('jlms_crtf_folder', '');
                         $file_on_srv = $im_crtf_path . '/' . md5($ex_crtf_id . '_' . $ucode) . '.png';
                         if (file_exists($file_on_srv)) {
                             $is_saved_on_server = true;
                         }
                     }
                 }
             }
             if ($is_saved_on_server) {
                 $loadFile = $file_on_srv;
             } else {
                 $loadFile = _JOOMLMS_DOC_FOLDER . $cert_name;
             }
             $im_fullsize = getimagesize($loadFile);
             if (isset($im_fullsize[2])) {
                 if ($im_fullsize[2] == 1) {
                     $im = imagecreatefromgif($loadFile);
                 } elseif ($im_fullsize[2] == 2) {
                     $im = imagecreatefromjpeg($loadFile);
                 } elseif ($im_fullsize[2] == 3) {
                     $im = imagecreatefrompng($loadFile);
                     if (function_exists('imagesavealpha')) {
                         imagesavealpha($im, true);
                     }
                 } else {
                     die;
                 }
             } else {
                 die('Bad image format.');
             }
             if (!$is_saved_on_server) {
                 require_once dirname(__FILE__) . '/libchart/barcode.php';
                 $b_params = array();
                 if ($JLMS_CONFIG->get('crtf_show_sn', 1)) {
                     $b_params[] = 'text';
                 }
                 if ($JLMS_CONFIG->get('crtf_show_barcode', 1)) {
                     $b_params[] = 'bar';
                 }
             }
             $origWidth = $im_fullsize[0];
             $origHeight = $im_fullsize[1];
             if ($is_duplicate && $print_duplicate_watermark) {
                 require_once _JOOMLMS_FRONT_HOME . DS . "includes" . DS . "libchart" . DS . "libchart.php";
                 JLMS_cleanLibChartCache();
                 $watermark = _JOOMLMS_FRONT_HOME . DS . "lms_images" . DS . "duplicate.png";
                 $wmTarget = $JLMS_CONFIG->getCfg('absolute_path') . "/" . ($JLMS_CONFIG->get('temp_folder', '') ? $JLMS_CONFIG->get('temp_folder', '') . "/" : '') . time() . '_' . md5(uniqid(rand(), true)) . ".png";
                 $waterMarkInfo = getimagesize($watermark);
                 $waterMarkWidth = $waterMarkInfo[0];
                 $waterMarkHeight = $waterMarkInfo[1];
                 $placementX = 0;
                 $placementY = 0;
                 $waterMarkDestWidth = $waterMarkWidth;
                 $waterMarkDestHeight = $waterMarkHeight;
                 $waterMarkDestWidth = round($origWidth / $waterMarkDestWidth * $waterMarkDestWidth);
                 $waterMarkDestHeight = round($origHeight / $waterMarkDestHeight * $waterMarkDestHeight);
                 // both of the watermark dimensions need to be 5% more than the original image...
                 // adjust width first.
                 #if($waterMarkWidth > $origWidth*0.95 && $waterMarkHeight > $origHeight*0.95) {
                 // both are already larger than the original by at least 5%...
                 // we need to make the watermark *smaller* for this one.
                 /*
                 					// where is the largest difference?
                 					$wdiff=$waterMarkDestWidth - $origWidth;
                 					$hdiff=$waterMarkDestHeight - $origHeight;
                 					if($wdiff > $hdiff) {
                 						// the width has the largest difference - get percentage
                 						$sizer=($wdiff/$waterMarkDestWidth)+0.05;
                 					} else {
                 						$sizer=($hdiff/$waterMarkDestHeight)+0.05;
                 					}
                 					$waterMarkDestWidth-=$waterMarkDestWidth * $sizer;
                 					$waterMarkDestHeight-=$waterMarkDestHeight * $sizer;*/
                 #$waterMarkDestWidth = round(($origWidth / $waterMarkDestWidth) * $waterMarkDestWidth);
                 #$waterMarkDestHeight = round(($origHeight / $waterMarkDestHeight) * $waterMarkDestHeight);
                 #} else {
                 // the watermark will need to be enlarged for this one
                 // where is the largest difference?
                 /*$wdiff=$origWidth - $waterMarkDestWidth;
                 		$hdiff=$origHeight - $waterMarkDestHeight;
                 		if($wdiff > $hdiff) {
                 			// the width has the largest difference - get percentage
                 			$sizer=($wdiff/$waterMarkDestWidth)+0.05;
                 		} else {
                 			$sizer=($hdiff/$waterMarkDestHeight)+0.05;
                 		}
                 		$waterMarkDestWidth+=$waterMarkDestWidth * $sizer;
                 		$waterMarkDestHeight+=$waterMarkDestHeight * $sizer;*/
                 #$waterMarkDestWidth = round(($origWidth / $waterMarkDestWidth) * $waterMarkDestWidth);
                 #$waterMarkDestHeight = round(($origHeight / $waterMarkDestHeight) * $waterMarkDestHeight);
                 #}
                 JLMS_Certificates::resize_png_image($watermark, $waterMarkDestWidth, $waterMarkDestHeight, $wmTarget, false);
                 // get the size info for this watermark.
                 $wmInfo = getimagesize($wmTarget);
                 $waterMarkDestWidth = $wmInfo[0];
                 $waterMarkDestHeight = $wmInfo[1];
                 $differenceX = $origWidth - $waterMarkDestWidth;
                 $differenceY = $origHeight - $waterMarkDestHeight;
                 $placementX = round($differenceX / 2);
                 $placementY = round($differenceY / 2);
             }
             if (!$is_saved_on_server) {
                 if (!empty($b_params)) {
                     $barcode = new JLMS_barcode($ucode, $b_params);
                     $barcode->generate($im, $origWidth, $origHeight);
                 }
                 $white = imagecolorallocate($im, 255, 255, 255);
                 $grey = imagecolorallocate($im, 128, 128, 128);
                 $black = imagecolorallocate($im, 0, 0, 0);
                 $text_messages = array();
                 $crtf_msg = new stdClass();
                 $crtf_msg->text_size = $crt->text_size;
                 $crtf_msg->text_x = $crt->text_x;
                 $crtf_msg->text_y = $crt->text_y;
                 $crtf_msg->crtf_font = isset($crt->crtf_font) && $crt->crtf_font ? $crt->crtf_font : 'arial.ttf';
                 $crtf_msg->crtf_text = $crt->crtf_text;
                 $crtf_msg->course_name = $crt->course_name;
                 $crtf_msg->crtf_shadow = $crt->crtf_shadow;
                 $crtf_msg->crtf_align = $crt->crtf_align;
                 $text_messages[] = $crtf_msg;
                 $query = "SELECT * FROM #__lms_certificates WHERE course_id = {$course_id} AND parent_id = {$crt->id} AND crtf_type = '-2' ORDER BY crtf_align";
                 $JLMS_DB->SetQuery($query);
                 $add_cert_msgs = $JLMS_DB->LoadObjectList();
                 foreach ($add_cert_msgs as $acms) {
                     $crtf_msg = new stdClass();
                     $crtf_msg->text_size = $acms->text_size;
                     $crtf_msg->text_x = $acms->text_x;
                     $crtf_msg->text_y = $acms->text_y;
                     $crtf_msg->crtf_font = isset($acms->crtf_font) && $acms->crtf_font ? $acms->crtf_font : 'arial.ttf';
                     $crtf_msg->crtf_text = $acms->crtf_text;
                     $crtf_msg->course_name = $crt->course_name;
                     $crtf_msg->crtf_shadow = $acms->crtf_shadow;
                     $crtf_msg->crtf_align = 0;
                     $text_messages[] = $crtf_msg;
                 }
                 foreach ($text_messages as $crt7) {
                     $font_size = $crt7->text_size;
                     $font_x = $crt7->text_x;
                     $font_y = $crt7->text_y;
                     $font_filename = $crt7->crtf_font;
                     $inform = array();
                     $font_text = $crt7->crtf_text;
                     $username = isset($txt_mes_obj->username) ? $txt_mes_obj->username : '';
                     $name = isset($txt_mes_obj->name) ? $txt_mes_obj->name : '';
                     $course_name = isset($txt_mes_obj->course_name) ? $txt_mes_obj->course_name : $crt7->course_name;
                     //$spec_answer = isset($txt_mes_obj->crtf_spec_answer)?$txt_mes_obj->crtf_spec_answer:'';
                     $crtf_date = isset($txt_mes_obj->crtf_date) ? $txt_mes_obj->crtf_date : time();
                     $font_text = str_replace('#username#', $username, $font_text);
                     $font_text = str_replace('#name#', $name, $font_text);
                     $font_text = str_replace('#course#', $course_name, $font_text);
                     $font_text = JLMS_Certificates::ReplaceCourseRegAnswers($font_text, $txt_mes_obj, $user_obj->id, $course_id);
                     //$font_text = str_replace('#reg_answer#', $spec_answer, $font_text);
                     $font_text = JLMS_Certificates::ReplaceQuizAnswers($font_text, $txt_mes_obj, $user_obj->id, $course_id);
                     $font_text = JLMS_Certificates::ReplaceEventOptions($font_text, $txt_mes_obj, $user_obj->id, $course_id);
                     $font_text = JLMS_Certificates::ReplaceCBProfileOptions($font_text, $txt_mes_obj, $user_obj->id, $course_id);
                     //$font_text = JLMS_Certificates::ReplaceUPN($font_text, $txt_mes_obj, $user_obj->id, $course_id);
                     // replace #date#
                     $str_format = 'Y-m-d';
                     $str_format_pre = '';
                     $first_pos = strpos($font_text, '#date');
                     if ($first_pos !== false) {
                         $first_str = substr($font_text, $first_pos + 5, strlen($font_text) - $first_pos - 5);
                         $sec_pos = strpos($first_str, '#');
                         $str_format = substr($first_str, 0, $sec_pos);
                         $str_format_pre = $str_format;
                         echo $str_format;
                         if ($str_format) {
                             if (substr($str_format, 0, 1) == '(') {
                                 $str_format = substr($str_format, 1);
                             }
                             if (substr($str_format, -1) == ')') {
                                 $str_format = substr($str_format, 0, -1);
                             }
                         }
                         echo $str_format;
                     }
                     if (!$str_format) {
                         $str_format = 'Y-m-d';
                     }
                     $font_text = str_replace('#date' . $str_format_pre . '#', date($str_format, $crtf_date), $font_text);
                     // end of #date#
                     $font = JPATH_SITE . "/media/arial.ttf";
                     if (file_exists(JPATH_SITE . "/media/" . $font_filename)) {
                         $font = JPATH_SITE . "/media/" . $font_filename;
                     }
                     $text_array = explode("\n", $font_text);
                     #print_r($text_array);die;
                     $count_lines = count($text_array);
                     $text_lines_xlefts = array();
                     $text_lines_xrights = array();
                     $text_lines_heights = array();
                     for ($i = 0; $i < $count_lines; $i++) {
                         $font_box = imagettfbbox($font_size, 0, $font, $text_array[$i]);
                         $text_lines_xlefts[$i] = $font_box[0];
                         $text_lines_xrights[$i] = $font_box[2];
                         $text_lines_heights[$i] = $font_box[1] - $font_box[7];
                         if ($text_lines_heights[$i] < $font_size) {
                             $text_lines_heights[$i] = $font_size;
                         }
                     }
                     $min_x = 0;
                     $max_x = 0;
                     $max_w = 0;
                     for ($i = 0; $i < $count_lines; $i++) {
                         if ($min_x > $text_lines_xlefts[$i]) {
                             $min_x = $text_lines_xlefts[$i];
                         }
                         if ($max_x < $text_lines_xrights[$i]) {
                             $max_x = $text_lines_xrights[$i];
                         }
                         if ($max_w < $text_lines_xrights[$i] - $text_lines_xlefts[$i]) {
                             $max_w = $text_lines_xrights[$i] - $text_lines_xlefts[$i];
                         }
                     }
                     #$crt->crtf_text
                     #$alignment = 'left';
                     $allow_shadow = $crt7->crtf_shadow == 1;
                     #$alignment = 'left';
                     switch (intval($crt7->crtf_align)) {
                         case 1:
                             for ($i = 0; $i < $count_lines; $i++) {
                                 $cur_w = $text_lines_xrights[$i] - $text_lines_xlefts[$i];
                                 $ad = intval(($max_w - $cur_w) / 2) - intval($max_w / 2);
                                 if ($allow_shadow) {
                                     imagettftext($im, $font_size, 0, $font_x + $ad + 2, $font_y + 2, $grey, $font, $text_array[$i]);
                                 }
                                 imagettftext($im, $font_size, 0, $font_x + $ad, $font_y, $black, $font, $text_array[$i]);
                                 $font_y = $font_y + $text_lines_heights[$i] + 3;
                             }
                             break;
                         case 2:
                             for ($i = 0; $i < $count_lines; $i++) {
                                 $cur_w = $text_lines_xrights[$i] - $text_lines_xlefts[$i];
                                 $ad = intval($max_w - $cur_w) - intval($max_w);
                                 if ($allow_shadow) {
                                     imagettftext($im, $font_size, 0, $font_x + $ad + 2, $font_y + 2, $grey, $font, $text_array[$i]);
                                 }
                                 imagettftext($im, $font_size, 0, $font_x + $ad, $font_y, $black, $font, $text_array[$i]);
                                 $font_y = $font_y + $text_lines_heights[$i] + 3;
                             }
                             break;
                         default:
                             for ($i = 0; $i < $count_lines; $i++) {
                                 $cur_w = $text_lines_xrights[$i] - $text_lines_xlefts[$i];
                                 $ad = 0;
                                 //intval(($max_w - $cur_w)/2);
                                 if ($allow_shadow) {
                                     imagettftext($im, $font_size, 0, $font_x + $ad + 2, $font_y + 2, $grey, $font, $text_array[$i]);
                                 }
                                 imagettftext($im, $font_size, 0, $font_x + $ad, $font_y, $black, $font, $text_array[$i]);
                                 $font_y = $font_y + $text_lines_heights[$i] + 3;
                             }
                             break;
                     }
                 }
                 #$font_box = imagettfbbox($font_size, 0, $font, $font_text);
                 #imagettftext($im, $font_size, 0, $font_x, $font_y, $grey, $font, $font_text, 'R');
                 #imagettftext($im, $font_size, 0, $font_x, $font_y, $black, $font, $font_text, 'R');
                 #@ob_end_clean();
             }
             if (!$is_preview) {
                 if (!$is_exist) {
                     $query = "INSERT INTO #__lms_certificate_prints (uniq_id, user_id, role_id, crtf_date, crtf_id, crtf_text, last_printed, name, username, course_id, course_name, quiz_id, quiz_name ) VALUES" . "\n (" . $JLMS_DB->Quote($ucode) . ", {$user_obj->id}, {$crtf_role}," . $JLMS_DB->Quote(date('Y-m-d H:i:s', $crtf_date)) . ", {$id}, " . $JLMS_DB->Quote($crt->crtf_text) . "," . "\n " . $JLMS_DB->Quote(date('Y-m-d H:i:s')) . ", " . $JLMS_DB->Quote($user_obj->name) . ", " . $JLMS_DB->Quote($user_obj->username) . "," . "\n {$course_id}, " . $JLMS_DB->Quote($course_name) . ", {$quiz_id}, " . $JLMS_DB->Quote($quiz_name) . ")";
                     $JLMS_DB->SetQuery($query);
                     $JLMS_DB->query();
                     $ex_crtf_id = $JLMS_DB->insertid();
                 } else {
                     $query = "UPDATE #__lms_certificate_prints SET last_printed = " . $JLMS_DB->Quote(date('Y-m-d H:i:s')) . "," . "\n crtf_text = " . $JLMS_DB->Quote($crt->crtf_text) . ", course_name = " . $JLMS_DB->Quote($course_name) . "," . "\n quiz_name = " . $JLMS_DB->Quote($quiz_name) . "\n WHERE id = {$is_exist}";
                     $JLMS_DB->SetQuery($query);
                     $JLMS_DB->query();
                     $ex_crtf_id = $is_exist;
                 }
             }
             if (preg_match('/Opera(\\/| )([0-9].[0-9]{1,2})/', $_SERVER['HTTP_USER_AGENT'])) {
                 $UserBrowser = "Opera";
             } elseif (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $_SERVER['HTTP_USER_AGENT'])) {
                 $UserBrowser = "IE";
             } else {
                 $UserBrowser = '';
             }
             $file_name = 'Certificate.png';
             header('Content-Type: image/png');
             header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
             if ($UserBrowser == 'IE') {
                 header('Content-Disposition: inline; filename="' . $file_name . '";');
                 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                 header('Pragma: public');
             } else {
                 header('Content-Disposition: inline; filename="' . $file_name . '";');
                 header('Pragma: no-cache');
             }
             @ob_end_clean();
             if (!$is_saved_on_server && !$is_preview) {
                 if ($JLMS_CONFIG->get('save_certificates', 1)) {
                     $im_crtf_path = $JLMS_CONFIG->get('jlms_crtf_folder', '');
                     if ($im_crtf_path && is_writable($im_crtf_path)) {
                         imagepng($im, $im_crtf_path . '/' . md5($ex_crtf_id . '_' . $ucode) . '.png');
                     }
                 }
             }
             if ($is_duplicate && $print_duplicate_watermark) {
                 imagealphablending($im, TRUE);
                 $finalWaterMarkImage = imagecreatefrompng($wmTarget);
                 $finalWaterMarkWidth = imagesx($finalWaterMarkImage);
                 $finalWaterMarkHeight = imagesy($finalWaterMarkImage);
                 imagecopy($im, $finalWaterMarkImage, $placementX, $placementY, 0, 0, $finalWaterMarkWidth, $finalWaterMarkHeight);
                 imagealphablending($im, FALSE);
                 imagesavealpha($im, TRUE);
             }
             imagepng($im);
             imagedestroy($im);
             exit;
         } elseif ($is_preview) {
             @ob_end_clean();
             echo 'Certificate preview is not available. Please make sure an image for the certificate is uploaded.';
             exit;
         }
     }
 }
Exemplo n.º 4
0
function getCertificateFile($params)
{
    global $my, $JLMS_DB, $JLMS_CONFIG, $Itemid;
    $course_id = $params['course_id'];
    //$usertype = JLMS_GetUserType($my->id, $course_id);
    $JLMS_ACL =& JLMSFactory::getACL();
    $user_id_request = $params['user_id'];
    $user_id = $my->id;
    if ($user_id_request && $user_id_request != $user_id) {
        if ($JLMS_ACL->isCourseTeacher()) {
            $user_id = $user_id_request;
        } elseif ($JLMS_ACL->isStaff() && isset($JLMS_ACL->_staff_learners) && is_array($JLMS_ACL->_staff_learners) && in_array($user_id_request, $JLMS_ACL->_staff_learners)) {
            $user_id = $user_id_request;
        }
    }
    $query = "SELECT id, crt_date FROM #__lms_certificate_users WHERE course_id = '" . $course_id . "' AND user_id = '" . $user_id . "' AND crt_option = 1";
    $JLMS_DB->SetQuery($query);
    $row = $JLMS_DB->LoadObject();
    $tm_obj = new stdClass();
    if ($row->crt_date == '0000-00-00 00:00:00' || !$row->crt_date) {
        $row->crt_date = date('Y-m-d H:i:s');
        $query = "UPDATE #__lms_certificate_users SET crt_date = '" . date('Y-m-d H:i:s') . "' WHERE id = " . $row->id . " AND course_id = '" . $course_id . "' AND user_id = '" . $user_id . "' AND crt_option = 1";
        $JLMS_DB->SetQuery($query);
        $JLMS_DB->query();
        $tm_obj->force_update_print_date = true;
        //to fix bug with 'null' date of print.
    }
    $query = "SELECT id FROM #__lms_certificates WHERE course_id = '" . $course_id . "' AND crtf_type = 1 AND published = 1";
    $JLMS_DB->SetQuery($query);
    $crtf_id = $JLMS_DB->LoadResult();
    require_once _JOOMLMS_FRONT_HOME . "/includes/lms_certificates.php";
    $query = "SELECT * FROM #__users WHERE id = '" . $user_id . "'";
    $JLMS_DB->SetQuery($query);
    $u_data = $JLMS_DB->LoadObjectList();
    $tm_obj->username = isset($u_data[0]->username) ? $u_data[0]->username : '';
    $tm_obj->name = isset($u_data[0]->name) ? $u_data[0]->name : '';
    $tm_obj->crtf_date = strtotime($row->crt_date);
    //time();
    $tm_obj->crtf_spec_answer = '';
    $tm_obj->is_preview = false;
    $user = new stdClass();
    $user->id = isset($u_data[0]->id) ? $u_data[0]->id : 0;
    $user->username = isset($u_data[0]->username) ? $u_data[0]->username : '';
    $user->name = isset($u_data[0]->name) ? $u_data[0]->name : '';
    $user->email = isset($u_data[0]->email) ? $u_data[0]->email : '';
    $id = $crtf_id;
    $course_id = $course_id;
    $txt_mes_obj = isset($tm_obj) ? $tm_obj : null;
    $user_obj = isset($user) ? $user : null;
    if (is_null($user_obj)) {
        $user_obj = new stdClass();
        $user_obj->id = $my->id;
        $user_obj->username = $my->username;
        $user_obj->email = $my->email;
        $user_obj->name = $my->name;
    }
    //	$JLMS_ACL = & JLMSFactory::getACL();
    $is_preview = false;
    $is_exist = false;
    $quiz_id = 0;
    $quiz_name = '';
    $course_name = '';
    if (isset($txt_mes_obj->is_preview)) {
        $is_preview = $txt_mes_obj->is_preview;
    }
    if (isset($txt_mes_obj->quiz_id)) {
        $quiz_id = $txt_mes_obj->quiz_id;
    }
    if (isset($txt_mes_obj->quiz_name)) {
        $quiz_name = $txt_mes_obj->quiz_name;
    }
    if (isset($txt_mes_obj->course_name)) {
        $course_name = $txt_mes_obj->course_name;
    }
    $do_s = true;
    $crtf_role = 0;
    if ($is_preview) {
        $crtf_role = intval(mosGetParam($_REQUEST, 'crtf_role', 0));
    } else {
        $crtf_role = intval($JLMS_ACL->GetRole(1));
    }
    if ($crtf_role) {
        $query = "SELECT a.*, b.course_name FROM #__lms_certificates as a, #__lms_courses as b WHERE a.course_id = '" . $course_id . "' AND a.course_id = b.id AND a.parent_id = {$id} AND a.crtf_type = {$crtf_role}";
        $JLMS_DB->SetQuery($query);
        $crts = $JLMS_DB->loadObjectList();
        if (count($crts) == 1) {
            if ($crts[0]->file_id) {
                $do_s = false;
            } else {
                $query = "SELECT file_id FROM #__lms_certificates as a WHERE a.id = '" . $id . "' AND a.course_id = '" . $course_id . "' AND a.parent_id = 0";
                $JLMS_DB->SetQuery($query);
                $crts[0]->file_id = $JLMS_DB->LoadResult();
                if ($crts[0]->file_id) {
                    $do_s = false;
                }
            }
        }
    }
    if ($do_s) {
        $query = "SELECT a.*, b.course_name FROM #__lms_certificates as a, #__lms_courses as b WHERE a.id = '" . $id . "' AND a.course_id = '" . $course_id . "' AND a.course_id = b.id AND a.parent_id = 0";
        $JLMS_DB->SetQuery($query);
        $crts = $JLMS_DB->loadObjectList();
    }
    if (count($crts) == 1) {
        $is_duplicate = false;
        $print_duplicate_watermark = $JLMS_CONFIG->get('crtf_duplicate_wm', true);
        $crt = $crts[0];
        $JLMS_DB->SetQuery("SELECT file_srv_name FROM #__lms_files WHERE id = '" . $crt->file_id . "'");
        $cert_name = $JLMS_DB->LoadResult();
        if ($cert_name) {
            $ucode = md5(uniqid(rand(), true));
            $ex_crtf_id = 0;
            $is_saved_on_server = false;
            $ucode = substr($ucode, 0, 10);
            if (!$is_preview) {
                $query = "SELECT * FROM #__lms_certificate_prints WHERE user_id = {$user_obj->id} AND role_id = {$crtf_role} AND course_id = {$course_id} AND crtf_id = {$id} AND quiz_id = {$quiz_id}";
                $JLMS_DB->SetQuery($query);
                $cr_pr = $JLMS_DB->LoadObject();
                if (is_object($cr_pr) && isset($cr_pr->id)) {
                    $is_exist = $cr_pr->id;
                    $ex_crtf_id = $cr_pr->id;
                    $ucode = $cr_pr->uniq_id;
                    $txt_mes_obj->name = $cr_pr->name;
                    $txt_mes_obj->username = $cr_pr->username;
                    //$txt_mes_obj->course_name = $cr_pr->course_name;
                    if (isset($txt_mes_obj->force_update_print_date) && $txt_mes_obj->force_update_print_date && isset($txt_mes_obj->crtf_date) && $txt_mes_obj->crtf_date) {
                        $query = "UPDATE #__lms_certificate_prints SET crtf_date = '" . $txt_mes_obj->crtf_date . "' WHERE id = " . $cr_pr->id . " AND user_id = {$user_obj->id} AND role_id = {$crtf_role} AND course_id = {$course_id} AND crtf_id = {$id} AND quiz_id = {$quiz_id}";
                        $JLMS_DB->SetQuery($query);
                        $JLMS_DB->query();
                    } else {
                        $txt_mes_obj->crtf_date = strtotime($cr_pr->crtf_date);
                    }
                    $is_duplicate = true;
                    if ($JLMS_CONFIG->get('save_certificates', 1)) {
                        $im_crtf_path = $JLMS_CONFIG->get('jlms_crtf_folder', '');
                        $file_on_srv = $im_crtf_path . '/' . md5($ex_crtf_id . '_' . $ucode) . '.png';
                        if (file_exists($file_on_srv)) {
                            $is_saved_on_server = true;
                        }
                    }
                }
            }
            if ($is_saved_on_server) {
                $loadFile = $file_on_srv;
            } else {
                $loadFile = _JOOMLMS_DOC_FOLDER . $cert_name;
            }
            $im_fullsize = getimagesize($loadFile);
            if (isset($im_fullsize[2])) {
                if ($im_fullsize[2] == 1) {
                    $im = imagecreatefromgif($loadFile);
                } elseif ($im_fullsize[2] == 2) {
                    $im = imagecreatefromjpeg($loadFile);
                } elseif ($im_fullsize[2] == 3) {
                    $im = imagecreatefrompng($loadFile);
                    if (function_exists('imagesavealpha')) {
                        imagesavealpha($im, true);
                    }
                } else {
                    die;
                }
            } else {
                die('Bad image format.');
            }
            if (!$is_saved_on_server) {
                require_once _JOOMLMS_FRONT_HOME . DS . 'includes' . DS . 'libchart' . DS . 'barcode.php';
                $b_params = array();
                if ($JLMS_CONFIG->get('crtf_show_sn', 1)) {
                    $b_params[] = 'text';
                }
                if ($JLMS_CONFIG->get('crtf_show_barcode', 1)) {
                    $b_params[] = 'bar';
                }
            }
            $origWidth = $im_fullsize[0];
            $origHeight = $im_fullsize[1];
            if ($is_duplicate && $print_duplicate_watermark) {
                require_once _JOOMLMS_FRONT_HOME . DS . "includes" . DS . "libchart" . DS . "libchart.php";
                JLMS_cleanLibChartCache();
                $watermark = _JOOMLMS_FRONT_HOME . DS . "lms_images" . DS . "duplicate.png";
                $wmTarget = $JLMS_CONFIG->getCfg('absolute_path') . "/" . ($JLMS_CONFIG->get('temp_folder', '') ? $JLMS_CONFIG->get('temp_folder', '') . "/" : '') . time() . '_' . md5(uniqid(rand(), true)) . ".png";
                $waterMarkInfo = getimagesize($watermark);
                $waterMarkWidth = $waterMarkInfo[0];
                $waterMarkHeight = $waterMarkInfo[1];
                $placementX = 0;
                $placementY = 0;
                $waterMarkDestWidth = $waterMarkWidth;
                $waterMarkDestHeight = $waterMarkHeight;
                $waterMarkDestWidth = round($origWidth / $waterMarkDestWidth * $waterMarkDestWidth);
                $waterMarkDestHeight = round($origHeight / $waterMarkDestHeight * $waterMarkDestHeight);
                JLMS_Certificates::resize_png_image($watermark, $waterMarkDestWidth, $waterMarkDestHeight, $wmTarget, false);
                // get the size info for this watermark.
                $wmInfo = getimagesize($wmTarget);
                $waterMarkDestWidth = $wmInfo[0];
                $waterMarkDestHeight = $wmInfo[1];
                $differenceX = $origWidth - $waterMarkDestWidth;
                $differenceY = $origHeight - $waterMarkDestHeight;
                $placementX = round($differenceX / 2);
                $placementY = round($differenceY / 2);
            }
            if (!$is_saved_on_server) {
                if (!empty($b_params)) {
                    $barcode = new JLMS_barcode($ucode, $b_params);
                    $barcode->generate($im, $origWidth, $origHeight);
                }
                $white = imagecolorallocate($im, 255, 255, 255);
                $grey = imagecolorallocate($im, 128, 128, 128);
                $black = imagecolorallocate($im, 0, 0, 0);
                $text_messages = array();
                $crtf_msg = new stdClass();
                $crtf_msg->text_size = $crt->text_size;
                $crtf_msg->text_x = $crt->text_x;
                $crtf_msg->text_y = $crt->text_y;
                $crtf_msg->crtf_font = isset($crt->crtf_font) && $crt->crtf_font ? $crt->crtf_font : 'arial.ttf';
                $crtf_msg->crtf_text = $crt->crtf_text;
                $crtf_msg->course_name = $crt->course_name;
                $crtf_msg->crtf_shadow = $crt->crtf_shadow;
                $crtf_msg->crtf_align = $crt->crtf_align;
                $text_messages[] = $crtf_msg;
                $query = "SELECT * FROM #__lms_certificates WHERE course_id = {$course_id} AND parent_id = {$crt->id} AND crtf_type = '-2' ORDER BY crtf_align";
                $JLMS_DB->SetQuery($query);
                $add_cert_msgs = $JLMS_DB->LoadObjectList();
                foreach ($add_cert_msgs as $acms) {
                    $crtf_msg = new stdClass();
                    $crtf_msg->text_size = $acms->text_size;
                    $crtf_msg->text_x = $acms->text_x;
                    $crtf_msg->text_y = $acms->text_y;
                    $crtf_msg->crtf_font = isset($acms->crtf_font) && $acms->crtf_font ? $acms->crtf_font : 'arial.ttf';
                    $crtf_msg->crtf_text = $acms->crtf_text;
                    $crtf_msg->course_name = $crt->course_name;
                    $crtf_msg->crtf_shadow = $acms->crtf_shadow;
                    $crtf_msg->crtf_align = 0;
                    $text_messages[] = $crtf_msg;
                }
                foreach ($text_messages as $crt7) {
                    $font_size = $crt7->text_size;
                    $font_x = $crt7->text_x;
                    $font_y = $crt7->text_y;
                    $font_filename = $crt7->crtf_font;
                    $inform = array();
                    $font_text = $crt7->crtf_text;
                    $username = isset($txt_mes_obj->username) ? $txt_mes_obj->username : '';
                    $name = isset($txt_mes_obj->name) ? $txt_mes_obj->name : '';
                    $course_name = isset($txt_mes_obj->course_name) ? $txt_mes_obj->course_name : $crt7->course_name;
                    //$spec_answer = isset($txt_mes_obj->crtf_spec_answer)?$txt_mes_obj->crtf_spec_answer:'';
                    $crtf_date = isset($txt_mes_obj->crtf_date) ? $txt_mes_obj->crtf_date : time();
                    $font_text = str_replace('#username#', $username, $font_text);
                    $font_text = str_replace('#name#', $name, $font_text);
                    $font_text = str_replace('#course#', $course_name, $font_text);
                    $font_text = JLMS_Certificates::ReplaceCourseRegAnswers($font_text, $txt_mes_obj, $user_obj->id, $course_id);
                    //$font_text = str_replace('#reg_answer#', $spec_answer, $font_text);
                    $font_text = JLMS_Certificates::ReplaceQuizAnswers($font_text, $txt_mes_obj, $user_obj->id, $course_id);
                    $font_text = JLMS_Certificates::ReplaceEventOptions($font_text, $txt_mes_obj, $user_obj->id, $course_id);
                    $font_text = JLMS_Certificates::ReplaceCBProfileOptions($font_text, $txt_mes_obj, $user_obj->id, $course_id);
                    //$font_text = JLMS_Certificates::ReplaceUPN($font_text, $txt_mes_obj, $user_obj->id, $course_id);
                    // replace #date#
                    $str_format = 'Y-m-d';
                    $str_format_pre = '';
                    $first_pos = strpos($font_text, '#date');
                    if ($first_pos !== false) {
                        $first_str = substr($font_text, $first_pos + 5, strlen($font_text) - $first_pos - 5);
                        $sec_pos = strpos($first_str, '#');
                        $str_format = substr($first_str, 0, $sec_pos);
                        $str_format_pre = $str_format;
                        echo $str_format;
                        if ($str_format) {
                            if (substr($str_format, 0, 1) == '(') {
                                $str_format = substr($str_format, 1);
                            }
                            if (substr($str_format, -1) == ')') {
                                $str_format = substr($str_format, 0, -1);
                            }
                        }
                        echo $str_format;
                    }
                    if (!$str_format) {
                        $str_format = 'Y-m-d';
                    }
                    $font_text = str_replace('#date' . $str_format_pre . '#', date($str_format, $crtf_date), $font_text);
                    // end of #date#
                    $font = JPATH_SITE . "/media/arial.ttf";
                    if (file_exists(JPATH_SITE . "/media/" . $font_filename)) {
                        $font = JPATH_SITE . "/media/" . $font_filename;
                    }
                    $text_array = explode("\n", $font_text);
                    #print_r($text_array);die;
                    $count_lines = count($text_array);
                    $text_lines_xlefts = array();
                    $text_lines_xrights = array();
                    $text_lines_heights = array();
                    for ($i = 0; $i < $count_lines; $i++) {
                        $font_box = imagettfbbox($font_size, 0, $font, $text_array[$i]);
                        $text_lines_xlefts[$i] = $font_box[0];
                        $text_lines_xrights[$i] = $font_box[2];
                        $text_lines_heights[$i] = $font_box[1] - $font_box[7];
                        if ($text_lines_heights[$i] < $font_size) {
                            $text_lines_heights[$i] = $font_size;
                        }
                    }
                    $min_x = 0;
                    $max_x = 0;
                    $max_w = 0;
                    for ($i = 0; $i < $count_lines; $i++) {
                        if ($min_x > $text_lines_xlefts[$i]) {
                            $min_x = $text_lines_xlefts[$i];
                        }
                        if ($max_x < $text_lines_xrights[$i]) {
                            $max_x = $text_lines_xrights[$i];
                        }
                        if ($max_w < $text_lines_xrights[$i] - $text_lines_xlefts[$i]) {
                            $max_w = $text_lines_xrights[$i] - $text_lines_xlefts[$i];
                        }
                    }
                    #$crt->crtf_text
                    #$alignment = 'left';
                    $allow_shadow = $crt7->crtf_shadow == 1;
                    #$alignment = 'left';
                    switch (intval($crt7->crtf_align)) {
                        case 1:
                            for ($i = 0; $i < $count_lines; $i++) {
                                $cur_w = $text_lines_xrights[$i] - $text_lines_xlefts[$i];
                                $ad = intval(($max_w - $cur_w) / 2) - intval($max_w / 2);
                                if ($allow_shadow) {
                                    imagettftext($im, $font_size, 0, $font_x + $ad + 2, $font_y + 2, $grey, $font, $text_array[$i]);
                                }
                                imagettftext($im, $font_size, 0, $font_x + $ad, $font_y, $black, $font, $text_array[$i]);
                                $font_y = $font_y + $text_lines_heights[$i] + 3;
                            }
                            break;
                        case 2:
                            for ($i = 0; $i < $count_lines; $i++) {
                                $cur_w = $text_lines_xrights[$i] - $text_lines_xlefts[$i];
                                $ad = intval($max_w - $cur_w) - intval($max_w);
                                if ($allow_shadow) {
                                    imagettftext($im, $font_size, 0, $font_x + $ad + 2, $font_y + 2, $grey, $font, $text_array[$i]);
                                }
                                imagettftext($im, $font_size, 0, $font_x + $ad, $font_y, $black, $font, $text_array[$i]);
                                $font_y = $font_y + $text_lines_heights[$i] + 3;
                            }
                            break;
                        default:
                            for ($i = 0; $i < $count_lines; $i++) {
                                $cur_w = $text_lines_xrights[$i] - $text_lines_xlefts[$i];
                                $ad = 0;
                                //intval(($max_w - $cur_w)/2);
                                if ($allow_shadow) {
                                    imagettftext($im, $font_size, 0, $font_x + $ad + 2, $font_y + 2, $grey, $font, $text_array[$i]);
                                }
                                imagettftext($im, $font_size, 0, $font_x + $ad, $font_y, $black, $font, $text_array[$i]);
                                $font_y = $font_y + $text_lines_heights[$i] + 3;
                            }
                            break;
                    }
                }
                #$font_box = imagettfbbox($font_size, 0, $font, $font_text);
                #imagettftext($im, $font_size, 0, $font_x, $font_y, $grey, $font, $font_text, 'R');
                #imagettftext($im, $font_size, 0, $font_x, $font_y, $black, $font, $font_text, 'R');
                #@ob_end_clean();
            }
            if (!$is_preview) {
                if (!$is_exist) {
                    $query = "INSERT INTO #__lms_certificate_prints (uniq_id, user_id, role_id, crtf_date, crtf_id, crtf_text, last_printed, name, username, course_id, course_name, quiz_id, quiz_name ) VALUES" . "\n (" . $JLMS_DB->Quote($ucode) . ", {$user_obj->id}, {$crtf_role}," . $JLMS_DB->Quote(date('Y-m-d H:i:s', $crtf_date)) . ", {$id}, " . $JLMS_DB->Quote($crt->crtf_text) . "," . "\n " . $JLMS_DB->Quote(date('Y-m-d H:i:s')) . ", " . $JLMS_DB->Quote($user_obj->name) . ", " . $JLMS_DB->Quote($user_obj->username) . "," . "\n {$course_id}, " . $JLMS_DB->Quote($course_name) . ", {$quiz_id}, " . $JLMS_DB->Quote($quiz_name) . ")";
                    $JLMS_DB->SetQuery($query);
                    $JLMS_DB->query();
                    $ex_crtf_id = $JLMS_DB->insertid();
                } else {
                    $query = "UPDATE #__lms_certificate_prints SET last_printed = " . $JLMS_DB->Quote(date('Y-m-d H:i:s')) . "," . "\n crtf_text = " . $JLMS_DB->Quote($crt->crtf_text) . ", course_name = " . $JLMS_DB->Quote($course_name) . "," . "\n quiz_name = " . $JLMS_DB->Quote($quiz_name) . "\n WHERE id = {$is_exist}";
                    $JLMS_DB->SetQuery($query);
                    $JLMS_DB->query();
                    $ex_crtf_id = $is_exist;
                }
            }
            $file_server_name = '';
            if ($is_saved_on_server) {
                $file_server_name = $im_crtf_path . '/' . md5($ex_crtf_id . '_' . $ucode) . '.png';
            } else {
                header('Content-Type: image/png');
                if (!$is_saved_on_server && !$is_preview) {
                    if ($JLMS_CONFIG->get('save_certificates', 1)) {
                        $im_crtf_path = $JLMS_CONFIG->get('jlms_crtf_folder', '');
                        if ($im_crtf_path && is_writable($im_crtf_path)) {
                            $file_server_name = $im_crtf_path . '/' . md5($ex_crtf_id . '_' . $ucode) . '.png';
                            imagepng($im, $file_server_name);
                        }
                    }
                }
                imagedestroy($im);
            }
            return $file_server_name;
        }
    }
    return '';
}