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;
         }
     }
 }
 function JLMS_Course_HomePage($course_id, $add_elements_page = false)
 {
     global $option, $my, $JLMS_DB, $Itemid, $JLMS_CONFIG, $JLMS_SESSION;
     //TODO: replace global option declaration
     $usertype = $JLMS_CONFIG->get('current_usertype', 0);
     $JLMS_ACL =& JLMSFactory::getACL();
     $is_teacher = $JLMS_ACL->isTeacher();
     $AND_ST = "";
     if (false !== ($enroll_period = JLMS_getEnrolPeriod($my->id, $course_id))) {
         $AND_ST = " AND IF(is_time_related, (show_period < '" . $enroll_period . "' ), 1) ";
     }
     $this->course_id = $course_id;
     $curr_date = date("Y-m-d");
     $is_curtopic = intval(mosgetparam($_REQUEST, 't_id', 0));
     //create list of topics
     if ($usertype == 1) {
         $published = '1';
     } else {
         $published = "published=1 AND (publish_start=0 OR start_date<='{$curr_date}') AND (publish_end=0 OR end_date>='{$curr_date}')";
     }
     $query = "SELECT * FROM #__lms_topics WHERE course_id={$course_id} " . $AND_ST . " AND {$published} " . ($is_curtopic ? " AND id=" . $is_curtopic : "") . " ORDER BY ordering";
     $JLMS_DB->setQuery($query);
     $this->topics = $JLMS_DB->loadObjectList('id');
     //get items of the course
     //2 - documents
     if ($add_elements_page) {
         $rows = array();
         $possibilities = new stdClass();
         JLMSDocs::FillList($course_id, $rows, $possibilities, false, false);
         $this->elements[_DOCUMENT_ID] = JLMSDocs::GetItemsbyPermission($rows, 'manage');
     } else {
         $AND_ST_D = "";
         if (false !== ($enroll_period = JLMS_getEnrolPeriod($my->id, $course_id))) {
             $AND_ST_D = " AND IF(a.is_time_related, (a.show_period < '" . $enroll_period . "' ), 1) ";
         }
         $query = "SELECT a.*, b.file_name FROM #__lms_documents as a LEFT JOIN #__lms_files as b ON a.file_id = b.id AND a.folder_flag = 0 WHERE a.course_id={$course_id}" . $AND_ST_D;
         $JLMS_DB->setQuery($query);
         $this->elements[_DOCUMENT_ID] = $JLMS_DB->loadObjectList('id');
         foreach ($this->elements[_DOCUMENT_ID] as $elem) {
             if ($elem->folder_flag == 3) {
                 $query = "SELECT a.*, b.file_name" . "\n FROM #__lms_outer_documents as a LEFT JOIN #__lms_files as b ON a.file_id = b.id AND a.folder_flag = 0 " . "\n WHERE a.folder_flag = 0 AND a.id = " . $elem->file_id;
                 $JLMS_DB->SetQuery($query);
                 $out_row = $JLMS_DB->LoadObjectList();
                 if (count($out_row) && isset($out_row[0]->allow_link) && $out_row[0]->allow_link == 1) {
                     $this->elements[_DOCUMENT_ID][$elem->id]->doc_name = $out_row[0]->doc_name;
                     $this->elements[_DOCUMENT_ID][$elem->id]->doc_description = $out_row[0]->doc_description;
                     $this->elements[_DOCUMENT_ID][$elem->id]->file_id = $out_row[0]->file_id;
                     $this->elements[_DOCUMENT_ID][$elem->id]->file_name = $out_row[0]->file_name;
                 } else {
                     unset($this->elements[_DOCUMENT_ID][$elem->id]);
                 }
             }
         }
         $this->elements[_DOCUMENT_ID] = AppendFileIcons_toList($this->elements[_DOCUMENT_ID]);
     }
     //get max tree level
     $this->max_lvl = getDirNesting($this->elements[_DOCUMENT_ID], 0);
     global $max_lvl;
     $max_lvl = $this->max_lvl;
     //3 - links
     $query = "SELECT * FROM #__lms_links WHERE course_id={$course_id}" . $AND_ST;
     $JLMS_DB->setQuery($query);
     $this->elements[_LINK_ID] = $JLMS_DB->loadObjectList('id');
     //4 - contents ... er... TODO
     //5 - quizs
     $query = "SELECT *, c_id AS id FROM #__lms_quiz_t_quiz WHERE course_id={$course_id}" . $AND_ST;
     $JLMS_DB->setQuery($query);
     $this->elements[_QUIZ_ID] = $JLMS_DB->loadObjectList('c_id');
     $quizzes_i = array();
     foreach ($this->elements[_QUIZ_ID] as $row) {
         $quizzes_i[] = $row->c_id;
     }
     //add results for student
     if ($usertype == 2 && count($this->elements[_QUIZ_ID])) {
         $quizzes_r = array();
         foreach ($this->elements[_QUIZ_ID] as $row) {
             $quizzes_r[] = $row->c_id;
         }
         $q_items_num = array();
         $extra_info_cells = array();
         if (!empty($quizzes_r)) {
             $quizzes_r_cid = implode(',', $quizzes_r);
             $query = "SELECT * FROM #__lms_quiz_results WHERE quiz_id IN ({$quizzes_r_cid}) AND course_id={$course_id} AND user_id={$my->id} GROUP BY quiz_id";
             $JLMS_DB->SetQuery($query);
             $extra_info_cells = $JLMS_DB->loadObjectList();
         }
         foreach ($this->elements[_QUIZ_ID] as $i => $value) {
             $this->elements[_QUIZ_ID][$i]->start_date = '-';
             $this->elements[_QUIZ_ID][$i]->end_date = '-';
             $this->elements[_QUIZ_ID][$i]->status = -1;
             $this->elements[_QUIZ_ID][$i]->user_passed = -1;
             $this->elements[_QUIZ_ID][$i]->points = -1;
             $this->elements[_QUIZ_ID][$i]->user_score = 0;
             $this->elements[_QUIZ_ID][$i]->quiz_max_score = 0;
             foreach ($extra_info_cells as $cell) {
                 if ($cell->quiz_id == $this->elements[_QUIZ_ID][$i]->c_id) {
                     $this->elements[_QUIZ_ID][$i]->start_date = JLMS_dateToDisplay($cell->quiz_date, false, $JLMS_CONFIG->get('offset') * 60 * 60 - $cell->user_time, '\\<\\b\\r \\/>H:i:s');
                     $this->elements[_QUIZ_ID][$i]->end_date = JLMS_dateToDisplay($cell->quiz_date, false, $JLMS_CONFIG->get('offset') * 60 * 60, '\\<\\b\\r \\/>H:i:s');
                     $this->elements[_QUIZ_ID][$i]->points = $cell->user_score;
                     $this->elements[_QUIZ_ID][$i]->status = $cell->user_passed;
                     $this->elements[_QUIZ_ID][$i]->user_passed = $cell->user_passed;
                     $this->elements[_QUIZ_ID][$i]->user_score = $cell->user_score;
                     $this->elements[_QUIZ_ID][$i]->quiz_max_score = $cell->quiz_max_score;
                 }
             }
         }
         unset($extra_info_cells);
         require_once _JOOMLMS_FRONT_HOME . DS . "includes" . DS . "lms_certificates.php";
         $arr = array();
         JLMS_Certificates::JLMS_GB_getUserCertificates($course_id, $my->id, $arr);
         $arr1 = isset($arr['user_quiz_certificates']) ? $arr['user_quiz_certificates'] : array();
         foreach ($this->elements[_QUIZ_ID] as $i => $value) {
             for ($j = 0; $j < count($arr1); $j++) {
                 if ($arr1[$j]->c_quiz_id == $this->elements[_QUIZ_ID][$i]->c_id) {
                     $this->elements[_QUIZ_ID][$i]->link_certificate = "<a target = \"_blank\" href = \"" . $JLMS_CONFIG->get('live_site') . "/index.php?tmpl=component&option=" . $option . "&amp;no_html=1&amp;task=print_quiz_cert&amp;course_id=" . $course_id . "&amp;stu_quiz_id=" . $arr1[$j]->stu_quiz_id . "&amp;user_unique_id=" . $arr1[$j]->user_unique_id . "\"><img src = \"" . $JLMS_CONFIG->get('live_site') . "/components/com_joomla_lms/lms_images/toolbar/btn_certificate.png\" border = \"0\" align=\"top\" alt=\"certificate\"/></a>";
                 }
             }
         }
     }
     //6 - scorm ... er... TODO
     //7 - learning paths
     $lpaths = array();
     if ($course_id && $usertype) {
         //$JLMS_SESSION->clear('redirect_to_learnpath');
         if ($usertype == 1) {
             $query = "SELECT * FROM #__lms_learn_paths WHERE course_id = '" . $course_id . "'" . $AND_ST . "\n ORDER BY ordering";
         } elseif ($usertype == 2) {
             //$query = "SELECT a.*, b.user_status as r_status, b.start_time as r_start, b.end_time as r_end"
             //. "\n FROM #__lms_learn_paths as a LEFT JOIN #__lms_learn_path_results as b ON a.id = b.lpath_id AND b.course_id = '".$course_id."' AND b.user_id = '".$my->id."'"
             $query = "SELECT a.*, '' as r_status, '' as r_start, '' as r_end" . "\n FROM #__lms_learn_paths as a" . "\n WHERE a.course_id = '" . $course_id . "'" . "\n AND a.published = 1" . "\n ORDER BY a.ordering";
         }
         $JLMS_DB->SetQuery($query);
         $lpaths = $JLMS_DB->LoadObjectList();
         if ($usertype == 2) {
             $user_ids = array();
             $user_ids[] = $my->id;
             require_once _JOOMLMS_FRONT_HOME . "/includes/lms_grades.lib.php";
             JLMS_LP_populate_results($course_id, $lpaths, $user_ids);
             // 13 August 2007 (DEN) Check for prerequisites.
             // 1. get the list of lpath_ids.
             $lpath_ids = array();
             foreach ($lpaths as $lpath) {
                 $lpath_ids[] = $lpath->id;
             }
             if (!empty($lpath_ids)) {
                 $lpath_ids_str = implode(',', $lpath_ids);
                 // 2. get the list of prerequisites
                 // SELECT from two tables (+ #__lms_learn_paths) - because the prereq lpath could be deleted...
                 $query = "SELECT a.* FROM #__lms_learn_path_prerequisites as a, #__lms_learn_paths as b" . "\n WHERE a.lpath_id IN ({$lpath_ids_str}) AND a.req_id = b.id";
                 $JLMS_DB->SetQuery($query);
                 $prereqs = $JLMS_DB->LoadObjectList();
                 if (!empty($prereqs)) {
                     // 3. compare lists of prereqs to the lists of lpaths.
                     $i = 0;
                     while ($i < count($lpaths)) {
                         $is_hidden = false;
                         $o = 0;
                         while ($o < count($prereqs)) {
                             if ($prereqs[$o]->lpath_id == $lpaths[$i]->id) {
                                 $j = 0;
                                 while ($j < count($lpaths)) {
                                     if ($lpaths[$j]->id == $prereqs[$o]->req_id) {
                                         if (!$lpaths[$j]->item_id) {
                                             if (empty($lpaths[$j]->r_status)) {
                                                 $is_hidden = true;
                                                 break;
                                             } else {
                                                 $end_time = strtotime($lpaths[$j]->r_end);
                                                 $current_time = strtotime(date("Y-m-d H:i:s"));
                                                 if ($current_time > $end_time && $current_time - $end_time < $prereqs[$o]->time_minutes * 60) {
                                                     $is_hidden = true;
                                                     break;
                                                 }
                                             }
                                         } else {
                                             if (empty($lpaths[$j]->s_status)) {
                                                 $is_hidden = true;
                                                 break;
                                             } else {
                                                 $end_time = strtotime($lpaths[$j]->r_end);
                                                 $current_time = strtotime(date("Y-m-d H:i:s"));
                                                 if ($current_time > $end_time && $current_time - $end_time < $prereqs[$o]->time_minutes * 60) {
                                                     $is_hidden = true;
                                                     break;
                                                 }
                                             }
                                         }
                                     }
                                     $j++;
                                 }
                             }
                             $o++;
                         }
                         $lpaths[$i]->is_hidden = $is_hidden;
                         $i++;
                     }
                 }
             }
         }
     }
     foreach ($lpaths as $lpath) {
         $this->elements[_LPATH_ID][$lpath->id] = $lpath;
     }
     //populate topics with links to their items
     $query = "SELECT * FROM #__lms_topic_items WHERE course_id={$course_id} ORDER BY ordering";
     $JLMS_DB->setQuery($query);
     $items = $JLMS_DB->loadObjectList();
     for ($i = 0; $i < count($items); $i++) {
         if ($items[$i]->item_type == 7) {
             $query = "SELECT lp_type FROM #__lms_learn_paths WHERE id = '" . $items[$i]->item_id . "'" . $AND_ST;
             $JLMS_DB->SetQuery($query);
             $lp_type = $JLMS_DB->LoadResult();
             if ($lp_type == 2) {
                 $query = "SELECT item_id FROM #__lms_learn_paths WHERE id = '" . $items[$i]->item_id . "'";
                 $JLMS_DB->SetQuery($query);
                 $learn_path_id = $JLMS_DB->LoadResult();
                 $query = "SELECT scorm_package, params as scorm_params, width as scorm_width, height as scorm_height FROM #__lms_n_scorm WHERE id = '" . $learn_path_id . "'";
                 $JLMS_DB->SetQuery($query);
                 $outer_doc = null;
                 $scorm_info = null;
                 $scorm_info = $JLMS_DB->LoadObject();
                 if (is_object($scorm_info)) {
                     $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_params = $scorm_info->scorm_params;
                     $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_width = $scorm_info->scorm_width;
                     $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_height = $scorm_info->scorm_height;
                     $scorm_package = $scorm_info->scorm_package;
                     $query = "SELECT id FROM #__lms_n_scorm WHERE scorm_package = '" . $scorm_package . "' AND course_id = 0";
                     $JLMS_DB->SetQuery($query);
                     $scorm_lib_id = $JLMS_DB->LoadResult();
                     $query = "SELECT outdoc_share, owner_id, allow_link FROM #__lms_outer_documents WHERE file_id = '" . $scorm_lib_id . "' AND folder_flag = 3";
                     $JLMS_DB->SetQuery($query);
                     $outer_doc = $JLMS_DB->LoadObject();
                 }
                 if (is_object($outer_doc) && isset($outer_doc->allow_link) && $outer_doc->allow_link == 1) {
                     // 01May2009: new library policy: if 'allow_link' is enabled - we can view already added resource !
                 } else {
                     unset($items[$i]);
                 }
             } elseif ($lp_type == 1) {
                 $query = "SELECT item_id FROM #__lms_learn_paths WHERE id = '" . $items[$i]->item_id . "'";
                 $JLMS_DB->SetQuery($query);
                 $learn_path_id = $JLMS_DB->LoadResult();
                 if ($learn_path_id) {
                     $query = "SELECT params as scorm_params, width as scorm_width, height as scorm_height FROM #__lms_n_scorm WHERE id = '" . $learn_path_id . "'";
                     $JLMS_DB->SetQuery($query);
                     $scorm_info = null;
                     $scorm_info = $JLMS_DB->LoadObject();
                     if (is_object($scorm_info)) {
                         $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_params = $scorm_info->scorm_params;
                         $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_width = $scorm_info->scorm_width;
                         $this->elements[_LPATH_ID][$items[$i]->item_id]->scorm_height = $scorm_info->scorm_height;
                     }
                 }
             }
         }
     }
     if (!$is_teacher) {
         $this->_filterItemsByShowPeriod($items, $course_id);
     }
     $mas = array();
     foreach ($items as $k => $v) {
         $mas[] = $items[$k];
     }
     unset($lpath);
     $items = $mas;
     $links = array();
     foreach ($items as $item) {
         $item_tmp->id = $item->item_id;
         $item_tmp->type = $item->item_type;
         $item_tmp->ordering = $item->ordering;
         $links[$item->topic_id][] = $item;
     }
     $this->links = $links;
 }
 function JQ_ListQuizzes_Stu($option, $course_id)
 {
     global $JLMS_DB, $JLMS_SESSION, $my, $JLMS_CONFIG;
     $JLMS_ACL =& JLMSFactory::getACL();
     $cat_id = intval(mosGetParam($_REQUEST, 'cat_id', $JLMS_SESSION->get('cat_id', 0)));
     $JLMS_SESSION->set('cat_id', $cat_id);
     $limit = intval(mosGetParam($_REQUEST, 'limit', $JLMS_SESSION->get('list_limit', $JLMS_CONFIG->get('list_limit'))));
     $JLMS_SESSION->set('list_limit', $limit);
     $limitstart = intval(mosGetParam($_REQUEST, 'limitstart', 0));
     $AND_ST = "";
     if (false !== ($enroll_period = JLMS_getEnrolPeriod($my->id, $course_id))) {
         $AND_ST = " AND IF(a.is_time_related, (a.show_period < '" . $enroll_period . "' ), 1) ";
     }
     $query = "SELECT COUNT(*)" . "\n FROM #__lms_quiz_t_quiz AS a" . "\n WHERE a. course_id = '" . $course_id . "'" . $AND_ST . ($JLMS_ACL->CheckPermissions('quizzes', 'view_all') ? '' : "\n AND a.published = 1") . ($cat_id ? "\n AND a.c_category_id = {$cat_id}" : '');
     $JLMS_DB->setQuery($query);
     $total = $JLMS_DB->loadResult();
     require_once _JOOMLMS_FRONT_HOME . DS . "includes" . DS . "classes" . DS . "lms.pagination.php";
     $pageNav = new JLMSPageNav($total, $limitstart, $limit);
     $query = "SELECT a.*, b.c_category, u.name as author_name " . "\n FROM #__lms_quiz_t_quiz a LEFT JOIN #__lms_quiz_t_category b ON a.c_category_id = b.c_id AND b.course_id = '" . $course_id . "' AND b.is_quiz_cat = 1 LEFT JOIN #__users as u ON a.c_user_id = u.id" . "\n WHERE a.course_id = '" . $course_id . "'" . $AND_ST . ($JLMS_ACL->CheckPermissions('quizzes', 'view_all') ? '' : "\n AND a.published = 1") . ($cat_id ? "\n AND a.c_category_id = {$cat_id}" : '') . "\n ORDER BY a.c_title, b.c_category" . "\n LIMIT {$pageNav->limitstart}, {$pageNav->limit}";
     $JLMS_DB->setQuery($query);
     $rows = $JLMS_DB->loadObjectList();
     $quizzes_i = array();
     foreach ($rows as $row) {
         $quizzes_i[] = $row->c_id;
     }
     $q_items_num = array();
     if (!empty($quizzes_i)) {
         $quizzes_i_cid = implode(',', $quizzes_i);
         $query = "SELECT sum(items_number) as items_count, quiz_id FROM #__lms_quiz_t_quiz_pool WHERE quiz_id IN ({$quizzes_i_cid}) GROUP BY quiz_id";
         $JLMS_DB->SetQuery($query);
         $q_items_num = $JLMS_DB->loadObjectList();
     }
     for ($i = 0, $n = count($rows); $i < $n; $i++) {
         $rows[$i]->quests_from_pool = 0;
         foreach ($q_items_num as $qin) {
             if ($qin->quiz_id == $rows[$i]->c_id) {
                 if ($qin->items_count) {
                     $rows[$i]->quests_from_pool = $qin->items_count;
                 }
                 break;
             }
         }
     }
     $javascript = 'onchange="document.adminForm.submit();"';
     $query = "SELECT c_id AS value, c_category AS text" . "\n FROM #__lms_quiz_t_category WHERE course_id = '" . $course_id . "' AND is_quiz_cat = 1" . "\n ORDER BY c_category";
     $JLMS_DB->setQuery($query);
     $categories[] = mosHTML::makeOption('0', _JLMS_SB_SELECT_CATEGORY);
     $categories = array_merge($categories, $JLMS_DB->loadObjectList());
     $category = mosHTML::selectList($categories, 'cat_id', 'class="inputbox" size="1" ' . $javascript, 'value', 'text', $cat_id);
     $lists['category'] = $category;
     $lists['used_category_filter'] = $cat_id;
     $arr1 = array();
     if (!empty($rows)) {
         if (empty($rows)) {
             $quiz_ids = array(0);
         } else {
             $quiz_ids = array();
             foreach ($rows as $row1) {
                 $quiz_ids[] = $row1->c_id;
             }
             $query = "SELECT * FROM #__lms_quiz_results WHERE quiz_id IN (" . implode(',', $quiz_ids) . ") AND course_id='" . $course_id . "' AND user_id = '" . $my->id . "'";
             //  GROUP BY quiz_id";
             $JLMS_DB->SetQuery($query);
             $user_results = $JLMS_DB->loadObjectList();
             for ($i = 0; $i < count($rows); $i++) {
                 $rows[$i]->user_passed = -1;
                 $rows[$i]->quiz_max_score = 0;
                 $rows[$i]->user_score = 0;
                 foreach ($user_results as $user_result1) {
                     if ($user_result1->course_id == $rows[$i]->course_id && $user_result1->quiz_id == $rows[$i]->c_id) {
                         $rows[$i]->user_passed = $user_result1->user_passed;
                         $rows[$i]->quiz_max_score = $user_result1->quiz_max_score;
                         $rows[$i]->user_score = $user_result1->user_score;
                     }
                 }
             }
         }
         require_once _JOOMLMS_FRONT_HOME . DS . "includes" . DS . "lms_certificates.php";
         $arr = array();
         JLMS_Certificates::JLMS_GB_getUserCertificates($course_id, $my->id, $arr);
         $arr1 = isset($arr['user_quiz_certificates']) ? $arr['user_quiz_certificates'] : array();
     }
     for ($i = 0; $i < count($rows); $i++) {
         for ($j = 0; $j < count($arr1); $j++) {
             if ($arr1[$j]->c_quiz_id == $rows[$i]->c_id) {
                 $rows[$i]->link_certificate = "<a class=\"jlms_img_link\" target = \"_blank\" href = \"" . $JLMS_CONFIG->get('live_site') . "/index.php?tmpl=component&option=" . $option . "&amp;no_html=1&amp;task=print_quiz_cert&amp;course_id=" . $course_id . "&amp;stu_quiz_id=" . $arr1[$j]->stu_quiz_id . "&amp;user_unique_id=" . $arr1[$j]->user_unique_id . "\"><img src = \"" . $JLMS_CONFIG->get('live_site') . "/components/com_joomla_lms/lms_images/toolbar/btn_certificate.png\" border = \"0\" align=\"top\" alt=\"certificate\"/></a>";
                 $rows[$i]->user_score = $arr1[$j]->user_score;
                 $rows[$i]->quiz_max_score = $arr1[$j]->quiz_max_score;
             }
         }
     }
     $lms_titles_cache =& JLMSFactory::getTitles();
     $lms_titles_cache->setArray('quiz_t_quiz', $rows, 'c_id', 'c_title');
     //	echo '<pre>';
     //	print_r($rows);
     //	echo '</pre>';
     JLMS_quiz_admin_html_class::JQ_showQuizList_Stu($rows, $lists, $pageNav, $option, $course_id);
 }
 function JQ_printCertificate()
 {
     global $JLMS_DB, $my, $JLMS_CONFIG;
     global $JLMS_LANGUAGE, $JLMS_CONFIG;
     JLMS_require_lang($JLMS_LANGUAGE, 'quiz.lang', $JLMS_CONFIG->get('default_language'));
     require _JOOMLMS_FRONT_HOME . '/includes/quiz/quiz_language.php';
     global $jq_language;
     $stu_quiz_id = intval(mosGetParam($_GET, 'stu_quiz_id', 0));
     $user_unique_id = strval(mosGetParam($_GET, 'user_unique_id', ''));
     $course_id = $JLMS_CONFIG->get('course_id', 0);
     $query = "SELECT sq.c_passed, sq.c_student_id, sq.c_total_score, sq.unique_id, sq.c_date_time as completion_datetime," . "\n qtq.c_full_score, qtq.c_title, qtq.c_certificate, qtq.course_id, qtq.c_id as quiz_id, qtq.course_id" . "\n FROM #__lms_quiz_r_student_quiz AS sq, #__lms_quiz_t_quiz AS qtq" . "\n WHERE sq.c_id = '" . $stu_quiz_id . "' and qtq.c_id = sq.c_quiz_id";
     $JLMS_DB->SetQuery($query);
     $stu_quiz = $JLMS_DB->LoadObjectList();
     if (count($stu_quiz) && $course_id) {
         $stu_quiz = $stu_quiz[0];
         $query = "SELECT course_id FROM #__lms_quiz_t_quiz WHERE c_id = '" . $stu_quiz->quiz_id . "'";
         $JLMS_DB->SetQuery($query);
         $quiz_course = $JLMS_DB->LoadResult();
         if ($user_unique_id == $stu_quiz->unique_id && $quiz_course == $course_id) {
             $JLMS_ACL =& JLMSFactory::getACL();
             $i_can_view_these_results = false;
             if ($my->id == $stu_quiz->c_student_id) {
                 // user who passed the quiz
                 $i_can_view_these_results = true;
             } elseif ($JLMS_ACL->isCourseTeacher()) {
                 // course teacher
                 $i_can_view_these_results = true;
             } elseif ($JLMS_ACL->isStaff() && isset($JLMS_ACL->_staff_learners) && is_array($JLMS_ACL->_staff_learners) && in_array($stu_quiz->c_student_id, $JLMS_ACL->_staff_learners)) {
                 //users CEO
                 $i_can_view_these_results = true;
             }
             if ($i_can_view_these_results) {
                 $user_id = $stu_quiz->c_student_id;
                 if ($stu_quiz->c_passed != 1) {
                     echo $jq_language['quiz_mes_notpassed'];
                     die;
                 }
                 if (!$stu_quiz->c_certificate) {
                     echo $jq_language['quiz_mes_notavail'];
                     die;
                 }
                 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 = new stdClass();
                 $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_spec_answer = '';
                 $course_id = $stu_quiz->course_id;
                 $tm_obj->is_preview = false;
                 $tm_obj->quiz_id = $stu_quiz->quiz_id;
                 $tm_obj->quiz_name = $stu_quiz->c_title;
                 $tm_obj->stu_quiz_id = $stu_quiz_id;
                 $tm_obj->crtf_date = strtotime($stu_quiz->completion_datetime);
                 $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 : '';
                 JLMS_Certificates::JLMS_outputCertificate($stu_quiz->c_certificate, $stu_quiz->course_id, $tm_obj, $user);
             }
         }
     }
     echo $jq_language['quiz_mes_notavail'];
 }
    function showCourseCertificate($course_id, $option, $row, $lists)
    {
        global $Itemid;
        ?>
<script language="JavaScript" type="text/javascript">
<!--//--><![CDATA[//><!--
function CRT_save(pressbutton) {
	var form = document.adminForm;
		form.task.value = pressbutton;
		form.submit();
}
function CRT_preview(txt_tooltip) {

}
//--><!]]>
</script>


<?php 
        JLMS_TMPL::OpenMT();
        $hparams = array();
        $toolbar = array();
        $toolbar[] = array('btn_type' => 'save', 'btn_js' => "javascript:CRT_save('crt_save')");
        $toolbar[] = array('btn_type' => 'preview', 'btn_js' => "javascript:jlms_ShowCertificatePreview()");
        $toolbar[] = array('btn_type' => 'back', 'btn_js' => sefRelToAbs("index.php?option={$option}&amp;Itemid={$Itemid}&amp;task=gradebook&amp;id={$course_id}"));
        JLMS_TMPL::ShowHeader('certificate', _JLMS_GBC_TITLE, $hparams, $toolbar);
        JLMS_TMPL::OpenTS();
        ?>
	<form action="<?php 
        echo sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}");
        ?>
" method="post" name="adminForm" enctype="multipart/form-data">

<?php 
        require_once _JOOMLMS_FRONT_HOME . "/includes/lms_certificates.php";
        JLMS_Certificates::JLMS_editCertificate_Page($row, true);
        ?>
		<input type="hidden" name="option" value="<?php 
        echo $option;
        ?>
" />
		<input type="hidden" name="Itemid" value="<?php 
        echo $Itemid;
        ?>
" />
		<input type="hidden" name="task" value="gb_certificates" />
		<input type="hidden" name="state" value="0" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="id" value="<?php 
        echo $course_id;
        ?>
" />
	</form>		
<?php 
        JLMS_TMPL::CloseTS();
        JLMS_TMPL::CloseMT();
    }
Beispiel #6
0
    function JQ_GetQuestData_nojs($q_data, $jq_language, $stu_quiz_id = 0)
    {
        global $JLMS_DB, $my, $option, $JLMS_CONFIG;
        $ret_str = '';
        $cur_template = 'joomlaquiz_lms_template';
        if ($cur_template) {
            require_once dirname(__FILE__) . '/templates/' . $cur_template . '/jq_template.php';
            $ret_add = '';
            $test_text = $q_data->c_question;
            require_once dirname(__FILE__) . '/../lms_certificates.php';
            $a = new stdClass();
            $a->quiz_id = $q_data->c_quiz_id;
            $a->stu_quiz_id = $stu_quiz_id;
            $test_text = JLMS_Certificates::ReplaceQuizAnswers($test_text, $a, $my->id, $q_data->course_id);
            $test_text = JLMS_Certificates::ReplaceEventOptions($test_text, $a, $my->id, $q_data->course_id);
            $test_text = str_replace('#date#', date('m-d-Y', time() - date('Z')), $test_text);
            $q_data->c_question = $test_text;
            if ($q_data->c_type == 7) {
                //temporary for 'hotspot' support
                $ret_add = '<div style="text-align:center;">' . $q_data->c_question . '</div>';
            } else {
                $q_description = JLMS_ShowText_WithFeatures($q_data->c_question, true);
                $ret_add = '<div style="text-align:center;">' . $q_description . '</div>';
            }
            //		$ret_str = "\t" . '<quest_data>'.$ret_add.'</quest_data>' . "\n";
            $ret_str = "\t" . $ret_add . "\n";
            $ret_str .= "\t" . '<input type="hidden" name="quest_type" value="' . $q_data->c_type . '"/>' . "\n";
            $ret_str .= "\t" . '<input type="hidden" name="quest_id" value="' . $q_data->c_id . '"/>' . "\n";
            $ret_str .= "\t" . '<input type="hidden" name="quest_score" value="' . $q_data->c_point . '"/>' . "\n";
            $qtype = $q_data->c_type;
            $qu_id = $q_data->c_id;
            if ($q_data->c_pool) {
                $qu_id = $q_data->c_pool;
            }
            $quest_params = new JLMSParameters($q_data->params);
            switch ($qtype) {
                case 1:
                case 12:
                    if ($quest_params->get('mandatory') == 1 && $quest_params->get('survey_question') == 1) {
                        $is_mandatory = 1;
                    } else {
                        $is_mandatory = 0;
                    }
                    $sql_var = '';
                    $sql_table = '';
                    $sql_where = '';
                    if ($qtype == 12) {
                        $sql_var .= ', b.imgs_name';
                        $sql_table = ', #__lms_quiz_images as b';
                        $sql_where .= ' AND a.c_choice = b.imgs_id';
                    }
                    $query = "SELECT a.c_id as value, a.c_choice as text, '0' as c_right, '0' as c_review" . $sql_var . " FROM #__lms_quiz_t_choice as a" . $sql_table . " WHERE a.c_question_id = '" . $qu_id . "'" . $sql_where . " ORDER BY a.ordering";
                    $JLMS_DB->SetQuery($query);
                    $choice_data = $JLMS_DB->LoadObjectList();
                    $qhtml = JoomlaQuiz_template_class::JQ_createMChoice($choice_data, $qtype);
                    //				$ret_str .= "\t" . '<quest_data_user><div>'.$qhtml.'<input type="hidden" name="ismandatory" value="'.$is_mandatory.'" /></div></quest_data_user>' . "\n";
                    $ret_str .= "\t" . '<div>' . $qhtml . '<input type="hidden" name="ismandatory" value="' . $is_mandatory . '" /></div>' . "\n";
                    break;
                case 3:
                    $query = "SELECT c_id as value, c_choice as text, '0' as c_right, '0' as c_review FROM #__lms_quiz_t_choice WHERE c_question_id = '" . $qu_id . "' ORDER BY ordering";
                    $JLMS_DB->SetQuery($query);
                    $choice_data = $JLMS_DB->LoadObjectList();
                    $i = 0;
                    while ($i < count($choice_data)) {
                        if ($choice_data[$i]->text == 'true' || $choice_data[$i]->text == 'True') {
                            $choice_data[$i]->text = $jq_language['quiz_simple_true'];
                        } elseif ($choice_data[$i]->text == 'false' || $choice_data[$i]->text == 'False') {
                            $choice_data[$i]->text = $jq_language['quiz_simple_false'];
                        }
                        $i++;
                    }
                    $qhtml = JoomlaQuiz_template_class::JQ_createMChoice($choice_data, $qtype);
                    $ret_str .= "\t" . '<div style="text-align:center;">' . $qhtml . '</div>' . "\n";
                    break;
                case 2:
                case 13:
                    $sql_var = '';
                    $sql_table = '';
                    $sql_where = '';
                    if ($qtype == 13) {
                        $sql_var .= ', b.imgs_name';
                        $sql_table = ', #__lms_quiz_images as b';
                        $sql_where .= ' AND a.c_choice = b.imgs_id';
                    }
                    $query = "SELECT a.c_id as value, a.c_choice as text, '0' as c_right, '0' as c_review" . $sql_var . " FROM #__lms_quiz_t_choice as a" . $sql_table . " WHERE a.c_question_id = '" . $qu_id . "'" . $sql_where . " ORDER BY a.ordering";
                    $JLMS_DB->SetQuery($query);
                    $choice_data = $JLMS_DB->LoadObjectList();
                    $qhtml = JoomlaQuiz_template_class::JQ_createMResponse($choice_data, $qtype, 1);
                    $ret_str .= "\t" . '<div>' . $qhtml . '</div>' . "\n";
                    break;
                case 4:
                    $query = "SELECT *, c_right_text as c_val FROM #__lms_quiz_t_matching WHERE c_question_id = '" . $qu_id . "'" . "\n ORDER BY ordering";
                    $JLMS_DB->SetQuery($query);
                    $match_data = $JLMS_DB->LoadObjectList();
                    $shuffle_match = $match_data;
                    shuffle($shuffle_match);
                    for ($di = 0, $dn = count($shuffle_match); $di < $dn; $di++) {
                        $shuffle_match[$di]->c_val = md5($shuffle_match[$di]->c_val);
                    }
                    //				$ret_str .= "\t" . '<div>'
                    //				. '<div id="cont" class="d_cont"><div id="col_1" style="float:left; width:49%;">';
                    //				for ($i=0, $n = count( $match_data ); $i < $n; $i++ ) {
                    //					$ret_str .= '<div id=\'cdiv_'.($i+1).'\' class=\'jq_drop\'>'
                    //					. $match_data[$i]->c_left_text . '</div>';
                    //				}
                    //				$ret_str .= '</div><div id="col_2" style="float:left; width:49%;">';
                    //				for ($i=0, $n = count( $shuffle_match ); $i < $n; $i++ ) {
                    //					$ret_str .= '<div id=\'ddiv_'.($i+1).'\' class=\'jq_drag\'>' //onmousedown=\'startDrag()\' onmouseup=\'stopDrag()\'
                    //					. $shuffle_match[$i]->c_right_text . '</div>';
                    //				}
                    //				$ret_str .= '</div><div style="clear:both;"><!-- --></div></div>';
                    //				$ret_str .= '</div>' . "\n";
                    $shuffle_match1 = array();
                    $shuffle_match1[0]->c_right_text = _JLMS_QUIZ_SHOW_SELECT_YOUR_ANSWER;
                    $shuffle_match1[0]->c_val = '{0}';
                    $shuffle_match1 = array_merge($shuffle_match1, $shuffle_match);
                    $qdata = array();
                    for ($i = 0, $n = count($match_data); $i < $n; $i++) {
                        $qdata[$i]->c_left_text = '<div class=\'jq_drop\'>' . $match_data[$i]->c_left_text . '</div>';
                        $qdata[$i]->c_right_text = mosHTML::selectList($shuffle_match1, 'quest_match[' . $i . ']', 'class="inputbox" size="1" ', 'c_val', 'c_right_text', null);
                    }
                    $qhtml = JoomlaQuiz_template_class::JQ_createMDropDown($qdata);
                    $ret_str .= "\t" . '<div>' . $qhtml . '</div>' . "\n";
                    break;
                case 5:
                    $query = "SELECT *, c_right_text as c_val FROM #__lms_quiz_t_matching WHERE c_question_id = '" . $qu_id . "'" . "\n ORDER BY ordering";
                    $JLMS_DB->SetQuery($query);
                    $match_data = $JLMS_DB->LoadObjectList();
                    $shuffle_match = $match_data;
                    shuffle($shuffle_match);
                    for ($di = 0, $dn = count($shuffle_match); $di < $dn; $di++) {
                        $shuffle_match[$di]->c_val = md5($shuffle_match[$di]->c_val);
                    }
                    $shuffle_match1 = array();
                    $shuffle_match1[0]->c_right_text = _JLMS_QUIZ_SHOW_SELECT_YOUR_ANSWER;
                    $shuffle_match1[0]->c_val = '{0}';
                    $shuffle_match1 = array_merge($shuffle_match1, $shuffle_match);
                    $qdata = array();
                    for ($i = 0, $n = count($match_data); $i < $n; $i++) {
                        $qdata[$i]->c_left_text = $match_data[$i]->c_left_text;
                        $qdata[$i]->c_right_text = mosHTML::selectList($shuffle_match1, 'quest_match[' . $i . ']', 'class="inputbox" size="1" ', 'c_val', 'c_right_text', null);
                    }
                    $qhtml = JoomlaQuiz_template_class::JQ_createMDropDown($qdata);
                    $ret_str .= "\t" . '<div>' . $qhtml . '</div>' . "\n";
                    break;
                case 6:
                    //$quest_params = new JLMSParameters($q_data->params);
                    if ($quest_params->get('mandatory') == 1 && $quest_params->get('survey_question') == 1) {
                        $is_mandatory = 1;
                    } else {
                        $is_mandatory = 0;
                    }
                    $def_cb = '';
                    $def_cb = '';
                    if ($quest_params->get('survey_question')) {
                        $query = "SELECT c_default FROM #__lms_quiz_t_blank WHERE c_question_id = '" . $qu_id . "'";
                        $JLMS_DB->SetQuery($query);
                        $blank_data = $JLMS_DB->LoadObjectList();
                        if (isset($blank_data[0]->c_default) && $blank_data[0]->c_default) {
                            $def_cb = $blank_data[0]->c_default;
                        }
                    }
                    if ($def_cb) {
                        $def_cb = str_replace('{', '', $def_cb);
                        $def_cb = str_replace('}', '', $def_cb);
                        require _JOOMLMS_FRONT_HOME . '/includes/classes/lms.cb_join.php';
                        $def_cb2 = JLMSCBJoin::getASSOC($def_cb);
                        if ($def_cb2) {
                            $def_cb = $def_cb2;
                        }
                        $def_cb = str_replace('#date#', date('m-d-Y', time() - date('Z')), $def_cb);
                    }
                    $qhtml = JoomlaQuiz_template_class::JQ_createBlank('', $def_cb);
                    $ret_str .= '<div style="text-align:center;">' . $qhtml . '</div>' . "\n" . "\t" . '<input type="hidden" name="ismandatory" value="' . $is_mandatory . '" />' . "\n";
                    break;
                case 7:
                    global $JLMS_CONFIG;
                    $is_hotspot_manual_correct = $JLMS_CONFIG->get('quiz_hs_offset_manual_correction');
                    $hs_div_offset_class = $JLMS_CONFIG->get('quiz_hs_offset_div_class');
                    $hs_manual_offset = $JLMS_CONFIG->get('quiz_hs_ofset_manual_value');
                    $src = 'images/joomlaquiz/images/' . $q_data->c_image . '';
                    $qhtml = '<input type="image" name="hotspot" value="hs" src="' . $src . '"/>';
                    $ret_str .= "\t" . '<div style="text-align:center;">' . $qhtml . '</div>' . "\n";
                    break;
                case 8:
                    if ($quest_params->get('mandatory') == 1) {
                        $is_mandatory = 1;
                    } else {
                        $is_mandatory = 0;
                    }
                    $qhtml = JoomlaQuiz_template_class::JQ_createSurvey();
                    // 'Survey' - question type
                    $ret_str .= '<div style="text-align:center;">' . $qhtml . "</div>\n" . "\t" . '<input type="hidden" name="ismandatory" value="' . $is_mandatory . '" />' . "\n";
                    break;
                case 9:
                    if ($quest_params->get('mandatory') == 1) {
                        $is_mandatory = 1;
                    } else {
                        $is_mandatory = 0;
                    }
                    $query = "SELECT * FROM #__lms_quiz_t_scale WHERE c_question_id = '" . $qu_id . "'" . "\n ORDER BY ordering";
                    $JLMS_DB->SetQuery($query);
                    $scale_data = $JLMS_DB->LoadObjectList();
                    $qhtml = JoomlaQuiz_template_class::JQ_createScale($scale_data, 0);
                    $ret_str .= "\t" . '<div>' . $qhtml . '<input type="hidden" name="ismandatory" value="' . $is_mandatory . '" /></div>' . "\n";
                    break;
                case 10:
                    $ret_str .= "\n" . "\t" . '' . "\n";
                    break;
                case 11:
                    $query = "SELECT a.*, a.c_right_text as c_val, b.imgs_name as left_name, c.imgs_name as right_name FROM #__lms_quiz_t_matching as a, #__lms_quiz_images as b, #__lms_quiz_images as c WHERE c_question_id = '" . $qu_id . "' AND a.c_left_text = b.imgs_id AND a.c_right_text = c.imgs_id" . "\n ORDER BY a.ordering";
                    $JLMS_DB->SetQuery($query);
                    $match_data = $JLMS_DB->LoadObjectList();
                    $shuffle_match = $match_data;
                    shuffle($shuffle_match);
                    for ($di = 0, $dn = count($shuffle_match); $di < $dn; $di++) {
                        $shuffle_match[$di]->c_val = md5($shuffle_match[$di]->c_val);
                    }
                    $shuffle_match1 = array();
                    $shuffle_match1[0]->right_name = _JLMS_QUIZ_SHOW_SELECT_YOUR_ANSWER;
                    $shuffle_match1[0]->c_val = '{0}';
                    $shuffle_match1 = array_merge($shuffle_match1, $shuffle_match);
                    $qdata = array();
                    for ($i = 0, $n = count($match_data); $i < $n; $i++) {
                        $link = 'index.php?tmpl=component&option=' . $option . '&task=quizzes&page=imgs_v&file_id=' . $match_data[$i]->c_left_text . '&imgs_name=' . $match_data[$i]->left_name;
                        $qdata[$i]->c_left_text = '<div class=\'jq_drop\' style=\'width: ' . $JLMS_CONFIG->get('quiz_match_max_width', 250) . 'px; height: ' . $JLMS_CONFIG->get('quiz_match_max_height', 30) . 'px;\'>
					<img src="' . ampReplace($JLMS_CONFIG->get('live_site') . '/index.php?tmpl=component&option=' . $option . '&task=quizzes&page=imgs_v&id=' . $q_data->course_id . '&file_id=' . $match_data[$i]->c_left_text . '&imgs_name=' . $match_data[$i]->left_name . '&pic_width=' . $JLMS_CONFIG->get('quiz_match_max_width', 250) . '&pic_height=' . $JLMS_CONFIG->get('quiz_match_max_height', 250) . '&bg_color=dddddd" border="0" alt="' . $match_data[$i]->left_name) . '" title="' . $match_data[$i]->left_name . '"/>
					</div>';
                        $qdata[$i]->c_right_text = mosHTML::selectList($shuffle_match1, 'quest_match[' . $i . ']', 'class="inputbox" size="1" ', 'c_val', 'right_name', null);
                    }
                    $qhtml = JoomlaQuiz_template_class::JQ_createMDropDown($qdata);
                    $ret_str .= "\t" . '<div>' . $qhtml . '</div>' . "\n";
                    break;
            }
        }
        return $ret_str;
    }
Beispiel #7
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 '';
}
    function ShowCertificate($course_id, $option, &$row, &$lists)
    {
        global $Itemid;
        $toolbar = array();
        $toolbar[] = array('btn_type' => 'save', 'btn_txt' => _JLMS_QUIZ_SAVE_CRTF_BTN, 'btn_js' => "javascript:submitbutton('save_crtf');");
        $toolbar[] = array('btn_type' => 'apply', 'btn_txt' => _JLMS_QUIZ_SAVE_CRTF_BTN, 'btn_js' => "javascript:submitbutton('apply_crtf');");
        //$toolbar[] = array('btn_type' => 'preview', 'btn_txt' => _JLMS_QUIZ_PREVIEW_CRTF_BTN, 'btn_js' => sefRelToAbs("index.php?option=$option&Itemid=$Itemid&task=quizzes&id=$course_id&page=preview_crtf&crtf_id=".$row->id), 'btn_target' => '_blank');
        $toolbar[] = array('btn_type' => 'preview', 'btn_txt' => _JLMS_QUIZ_PREVIEW_CRTF_BTN, 'btn_js' => "javascript:jlms_ShowCertificatePreview()");
        $toolbar[] = array('btn_type' => 'cancel', 'btn_txt' => _JLMS_QUIZ_CANCEL_BTN, 'btn_js' => "javascript:submitbutton('cancel_crtf');");
        $h = $row->id ? _JLMS_QUIZ_EDIT_CRTF_BTN : _JLMS_QUIZ_NEW_CRTF_BTN;
        JLMS_quiz_admin_html_class::showQuizHead($course_id, $option, $h, true, $toolbar);
        require_once _JOOMLMS_FRONT_HOME . "/includes/lms_certificates.php";
        ?>
<script language="javascript" type="text/javascript">
<!--
function submitbutton(pressbutton) {
	var proceed_to_submit = true;
	var form = document.adminForm;
	if (pressbutton == 'save_crtf' || pressbutton == 'apply_crtf') {
		if (form.crtf_name.value == '') {
			alert('<?php 
        echo _JLMS_PL_ENTER_NAME;
        ?>
');
			proceed_to_submit = false;
		}
	}
	if (proceed_to_submit) {
		form.page.value = pressbutton;
		form.submit();
	}
}
//-->
</script>
		<form action="<?php 
        echo sefRelToAbs("index.php?option={$option}&Itemid={$Itemid}");
        ?>
" method="post" name="adminForm" enctype="multipart/form-data">
		<table width="100%" cellpadding="0" cellspacing="0" border="0" class="jlms_table_no_borders">
			<tr>
				<td width="100%">
				<?php 
        JLMS_Certificates::JLMS_editCertificate_Page($row);
        ?>
				</td>
			</tr>
		</table>
		<input type="hidden" name="option" value="<?php 
        echo $option;
        ?>
" />
		<input type="hidden" name="page" value="" />
		<input type="hidden" name="task" value="quizzes" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="id" value="<?php 
        echo $course_id;
        ?>
" />
		<input type="hidden" name="crtf_id" value="<?php 
        echo $row->id;
        ?>
" />
		</form>
		<?php 
        JLMS_quiz_admin_html_class::showQuizFooter();
    }