Kaynağa Gözat

Adding QR support for certificates see #3631

Julio Montoya 13 yıl önce
ebeveyn
işleme
8fbe0be4a8

+ 1 - 1
certificates/index.php

@@ -9,7 +9,7 @@
  * Initialization
  */
 
-$language_file= 'gradebook';
+$language_file= array('admin', 'gradebook', 'document');
 
 require_once '../main/inc/global.inc.php';
 require_once api_get_path(LIBRARY_PATH).'certificate.lib.php';

+ 1 - 1
main/document/document.php

@@ -269,7 +269,7 @@ if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' && isse
         // Generate document HTML        
         $content_html = DocumentManager::replace_user_info_into_html(api_get_user_id(), api_get_course_id(), true);
 
-        $new_content_html = $content_html;
+        $new_content_html = $content_html['html_content'];
 
         $path_image = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/images/gallery';
         $new_content_html = str_replace('../images/gallery', $path_image, $new_content_html);

+ 4 - 4
main/gradebook/index.php

@@ -632,7 +632,7 @@ if (isset ($_GET['studentoverview'])) {
 	                Display :: display_reduced_header();
                     Display :: display_warning_message(get_lang('NoCertificateAvailable'));	                
 	            } else {
-	                echo $new_content_html ;
+	                echo $new_content_html['content'] ;
 	            }            
 	        }	        
 	        exit;
@@ -682,10 +682,10 @@ if (isset ($_GET['studentoverview'])) {
     				    $my_path_certificate = $path_directory_user_certificate.$name;
     					if (file_exists($my_path_certificate) && !empty($name)&& !is_dir($my_path_certificate) ) {    					        					    
     						header('Content-Type: text/html; charset='. $charset);    						
-    						echo $new_content_html;
+    						echo $new_content_html['content'];
     					} else {
-    						$my_new_content_html=$new_content_html;
-    						$my_new_content_html=mb_convert_encoding($my_new_content_html,'UTF-8',$charset);
+    						$my_new_content_html = $new_content_html['content'];
+    						$my_new_content_html = mb_convert_encoding($my_new_content_html,'UTF-8',$charset);
     						
     						//Creating new name                            
                             $name    = md5($user_id.$category_id).'.html';

+ 1 - 1
main/gradebook/lib/fe/gradebooktable.class.php

@@ -259,7 +259,7 @@ class GradebookTable extends SortableTable
 
 				$content_html = DocumentManager::replace_user_info_into_html(api_get_user_id(), $course_code);
 
-				$new_content=explode('</head>',$content_html);
+				$new_content=explode('</head>',$content_html['content_html']);
 
 				if (empty($new_content[0])) {
 					$warning_message = get_lang('ThereIsNotACertificateAvailableByDefault');

+ 2 - 2
main/gradebook/lib/gradebook_functions.inc.php

@@ -602,7 +602,7 @@ function get_user_certificate_content($user_id, $course_code, $is_preview = fals
     //generate document HTML    
     $content_html       = DocumentManager::replace_user_info_into_html($user_id, $course_code, $is_preview);
         
-    $new_content        = explode('</head>', $content_html);    
+    $new_content        = explode('</head>', $content_html['html_content']);    
     $new_content_html   = $new_content[1];                    
     $path_image         = api_get_path(WEB_COURSE_PATH).api_get_course_path($course_code).'/document/images/gallery';
     $new_content_html   = str_replace('../images/gallery',$path_image,$new_content_html);
@@ -617,5 +617,5 @@ function get_user_certificate_content($user_id, $course_code, $is_preview = fals
     
     //add header
     $new_content_html = $new_content[0].$print.'</head>'.$new_content_html;
-    return $new_content_html;
+    return array('content' => $new_content_html, 'variables'=>$content_html['variables']);
 }

+ 134 - 34
main/inc/lib/certificate.lib.php

@@ -5,9 +5,16 @@
 class Certificate extends Model {
     var $table;
     var $columns = array('id','cat_id','score_certificate','created_at','path_certificate');
-    var $certificate_data;
+    /**
+     * Certification data 
+     * */
+    var $certificate_data = array();
     
-    var $certification_user_path;    
+    /**
+     * Student's certification path
+     * */    
+    var $certification_user_path = null;  
+    var $certification_web_user_path = null;  
     var $user_id;
     
 	public function __construct($certificate_id = null) {
@@ -22,10 +29,14 @@ class Certificate extends Model {
         	$this->user_id = api_get_user_id();
         }
         
-        if ($this->user_id) {
+        if ($this->user_id) { 
+        	
 			//Need to be called before any operation
 	        $this->check_certificate_path();
-	            
+	        	    
+			//To force certification generation
+	        $this->generate();
+	        
 	        if (isset($this->certificate_data) && $this->certificate_data) {        
 	        	if (empty($this->certificate_data['path_certificate'])) {
 	        		$this->generate();	        		
@@ -35,9 +46,9 @@ class Certificate extends Model {
 	}
 	
 	/**
-	 * Show an HTML file	 
+	 * Shows the student's certificate (HTML file)	 
 	 */
-	public function show() {
+	public function show() {		
 		//Read file or preview file
 		if (!empty($this->certificate_data['path_certificate'])) {
 			$user_certificate = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
@@ -53,8 +64,7 @@ class Certificate extends Model {
 	}
 	
 	/**
-	 * Checks the certificate user path directories
-	 * Enter description here ...
+	 * Checks if the certificate user path directory is created
 	 */
 	public function check_certificate_path() {
 		$this->certification_user_path = null;
@@ -62,9 +72,13 @@ class Certificate extends Model {
 		//Setting certification path
 		$path_info = UserManager::get_user_picture_path_by_id($this->user_id, 'system', true);
 		
-		if (isset($path_info['dir']) && !empty($path_info)) {
+		$web_path_info = UserManager::get_user_picture_path_by_id($this->user_id, 'web', true);
+		
+		if (!empty($path_info) && isset($path_info['dir'])) {
+			
+			$this->certification_user_path = $path_info['dir'].'certificate/';
+			$this->certification_web_user_path = $web_path_info['dir'].'certificate/';
 			
-			$this->certification_user_path = $path_info['dir'].'certificate/';		
 			
 			if (!is_dir($path_info['dir'])) {
 				mkdir($path_info['dir'],0777);
@@ -78,15 +92,16 @@ class Certificate extends Model {
 	}
 	
 	/** 
-	 * 	Generates a certificate 
+	 * 	Generates an HTML Certificate and fills the path_certificate field in the DB 
 	 * */
 	
 	public function generate() {
+		$always_generate = false; // For test purposes
 		
-		if (empty($this->certification_user_path)) {
+		//The user directory should be set
+		if (empty($this->certification_user_path) && $always_generate == false) {
 			return false;
-		}
-		
+		}		
 		require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
 		require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
 		require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/scoredisplay.class.php';
@@ -114,32 +129,47 @@ class Certificate extends Model {
 			$certif_text 		= str_replace("\\n","\n", $certif_text);
 	
 			$date = date('d/m/Y', time());
-		
-			if (is_dir($this->certification_user_path)) {			
-				$name = $this->certificate_data['path_certificate'];		
-				if (!empty($this->certificate_data)) {
+	
+			if (is_dir($this->certification_user_path)) {
+				if (!empty($this->certificate_data)) {	
 					$new_content_html = get_user_certificate_content($this->user_id, $my_category[0]->get_course_code(), false);
-										
+				
 					if ($cat_id = strval(intval($this->certificate_data['cat_id']))) {
-						$my_path_certificate = $this->certification_user_path.$name;
-						if (file_exists($my_path_certificate) && !empty($name)&& !is_dir($my_path_certificate) ) {
-							//header('Content-Type: text/html; charset='. $charset);
-							//echo $new_content_html;
-							//Seems that the file was already generated
+						$name = $this->certificate_data['path_certificate'];
+						$my_path_certificate = $this->certification_user_path.basename($name);
+						if (file_exists($my_path_certificate) && !empty($name) && !is_dir($my_path_certificate) && $always_generate == false) {
+							//Seems that the file was already generated							
 							return true;
 						} else {
-							$my_new_content_html = $new_content_html;
-							$my_new_content_html = mb_convert_encoding($my_new_content_html,'UTF-8', api_get_system_encoding());
-	
 							//Creating new name
 							$name    = md5($this->user_id.$this->certificate_data['cat_id']).'.html';
-							$my_path_certificate = $this->certification_user_path.$name;
-	
-							$result = @file_put_contents($my_path_certificate, $my_new_content_html);						
-							$path_certificate='/'.$name;
-							//@todo move function in this class
-							update_user_info_about_certificate($this->certificate_data['cat_id'], $this->user_id, $path_certificate);
-							$this->certificate_data['path_certificate'] = $path_certificate;
+							$my_path_certificate = $this->certification_user_path.$name;							
+							$path_certificate    ='/'.$name;
+							
+							//Getting QR filename
+							$file_info = pathinfo($path_certificate);
+							$qr_code_filename = $this->certification_user_path.$file_info['filename'].'_qr.png';							
+							$new_content_html['content'] = str_replace('((certificate_bar_code))', Display::img($this->certification_web_user_path.$file_info['filename'].'_qr.png', 'QR'), $new_content_html['content']);
+							
+							$my_new_content_html = $new_content_html['content'];
+						
+							$my_new_content_html = mb_convert_encoding($my_new_content_html,'UTF-8', api_get_system_encoding());
+							
+							$result = @file_put_contents($my_path_certificate, $my_new_content_html);
+							if ($result) {						
+								
+								//@todo move function in this class
+								update_user_info_about_certificate($this->certificate_data['cat_id'], $this->user_id, $path_certificate);
+								$this->certificate_data['path_certificate'] = $path_certificate;
+								
+								if ($this->html_file_is_generated()) {
+									if (!empty($file_info)) {										
+										$text = $this->parse_certificate_variables($new_content_html['variables']);										
+										$this->generate_qr($text, $qr_code_filename);
+									}
+								}
+								
+							}
 							return $result;
 						}						
 					}
@@ -148,4 +178,74 @@ class Certificate extends Model {
 		}
 		return false;
 	}
+	
+	/**
+	 * 
+	 * Check if the file was generated
+	 * 
+	 * @return boolean
+	 */
+	function html_file_is_generated() {
+		if (empty($this->certification_user_path)) {
+			return false;
+		}
+		if (!empty($this->certificate_data) && isset($this->certificate_data['path_certificate']) && !empty($this->certificate_data['path_certificate'])) {
+			return true;			
+		}
+		return false;
+	} 
+	
+	public function generate_qr($text, $path) {		
+		//Make sure HTML certificate is generated
+		if (!empty($text) && !empty($path)) {
+			require_once api_get_path(LIBRARY_PATH).'phpqrcode/qrlib.php';			
+			$return = QRcode::png($text, $path, 'L', 4, 2);						
+		}
+	}
+	
+	private function parse_certificate_variables($array) {
+		$text = '';		
+		$headers = $array[0];
+		$content = $array[1];
+		$final_content = array();
+		
+		foreach($content as $key => $value) {
+			$my_header = $headers[$key];
+			$my_header = str_replace(array('((', '))') , '', $my_header);
+			$final_content[$my_header] = $value;
+		}
+		
+		/*
+		 * 
+		  0 => string '((user_firstname))' (length=18)
+          1 => string '((user_lastname))' (length=17)
+          2 => string '((gradebook_institution))' (length=25)
+          3 => string '((gradebook_sitename))' (length=22)
+          4 => string '((teacher_firstname))' (length=21)
+          5 => string '((teacher_lastname))' (length=20)
+          6 => string '((official_code))' (length=17)
+          7 => string '((date_certificate))' (length=20)
+          8 => string '((course_code))' (length=15)
+          9 => string '((course_title))' (length=16)
+          10 => string '((gradebook_grade))' (length=19)
+          11 => string '((certificate_link))' (length=20)
+          12 => string '((certificate_link_html))' (length=25)
+          13 => string '((certificate_barcode))' (length=23)
+          
+		 */
+		
+		$break_space = " \n\r ";
+		
+		$text = $final_content['gradebook_institution'].' - '.$final_content['gradebook_sitename'].' - '.get_lang('Certification').$break_space.
+				get_lang('Student'). ': '.$final_content['user_firstname'].' '.$final_content['user_lastname'].$break_space.
+				//get_lang('Portal'). ': '.$final_content['gradebook_sitename'].$break_space.
+				get_lang('Teacher'). ': '.$final_content['teacher_firstname'].' '.$final_content['teacher_lastname'].$break_space.
+				get_lang('Date'). ': '.$final_content['date_certificate'].$break_space.
+				get_lang('Score'). ': '.$final_content['gradebook_grade'].$break_space.
+				'URL'. ': '.$final_content['certificate_link'];		
+		return $text;
+		                                                                                                    
+		
+	}
+	
 }

+ 5 - 4
main/inc/lib/document.lib.php

@@ -1344,19 +1344,20 @@ return 'application/octet-stream';
 
         $rs = Database::query($sql);
         $new_content = '';
+        $all_user_info = array();
         if (Database::num_rows($rs)) {
             $row=Database::fetch_array($rs);
            $filepath = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$row['path'];
 
             if (is_file($filepath)) {
-                $my_content_html=file_get_contents($filepath);
+                $my_content_html = file_get_contents($filepath);
             }
-            $all_user_info = self::get_all_info_to_certificate($user_id, $course_id, $is_preview);
+            $all_user_info = self::get_all_info_to_certificate($user_id, $course_id, $is_preview);            
             $info_to_be_replaced_in_content_html=$all_user_info[0];
             $info_to_replace_in_content_html=$all_user_info[1];
             $new_content=str_replace($info_to_be_replaced_in_content_html,$info_to_replace_in_content_html, $my_content_html);
         }
-        return $new_content;
+        return array('html_content' => $new_content, 'variables' => $all_user_info);
     }
 
     /**
@@ -1415,7 +1416,7 @@ return 'application/octet-stream';
                                                      $info_grade_certificate['grade'],
                                                      $url,
                                                      '<a href="'.$url.'" target="_blank">'.get_lang('CertificateOnlineLink').'</a>',
-                                                     $url,
+                                                     '((certificate_barcode))',
                                                     );
         $info_to_be_replaced_in_content_html = array('((user_firstname))',
         											 '((user_lastname))',