Browse Source

Add "user_certificate" extra field + add "downloaded_at" field

See BT#12910
jmontoyaa 7 years ago
parent
commit
7cd39814aa
2 changed files with 35 additions and 11 deletions
  1. 30 10
      main/inc/lib/certificate.lib.php
  2. 5 1
      main/inc/lib/extra_field.lib.php

+ 30 - 10
main/inc/lib/certificate.lib.php

@@ -73,9 +73,8 @@ class Certificate extends Model
             }
         }
 
-        //Setting the qr and html variables
-        if (
-            isset($certificate_id) &&
+        // Setting the qr and html variables
+        if (isset($certificate_id) &&
             !empty($this->certification_user_path) &&
             isset($this->certificate_data['path_certificate'])
         ) {
@@ -92,7 +91,7 @@ class Certificate extends Model
     {
         $this->certification_user_path = null;
 
-        //Setting certification path
+        // Setting certification path
         $path_info = UserManager::getUserPathById($this->user_id, 'system');
         $web_path_info = UserManager::getUserPathById($this->user_id, 'web');
 
@@ -212,7 +211,10 @@ class Certificate extends Model
 
                             $my_new_content_html = str_replace(
                                 '((certificate_barcode))',
-                                Display::img($this->certification_web_user_path.$file_info['filename'].'_qr.png', 'QR'),
+                                Display::img(
+                                    $this->certification_web_user_path.$file_info['filename'].'_qr.png',
+                                    'QR'
+                                ),
                                 $new_content_html['content']
                             );
 
@@ -262,10 +264,10 @@ class Certificate extends Model
         $user_id,
         $path_certificate
     ) {
-        $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
+        $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
         $now = api_get_utc_datetime();
         if (!UserManager::is_user_certified($cat_id, $user_id)) {
-            $sql = 'UPDATE '.$table_certificate.' SET 
+            $sql = 'UPDATE '.$table.' SET 
                         path_certificate="'.Database::escape_string($path_certificate).'",
                         created_at = "'.$now.'"
                     WHERE cat_id="'.intval($cat_id).'" AND user_id="'.intval($user_id).'" ';
@@ -295,9 +297,10 @@ class Certificate extends Model
 
     /**
      * Generates a QR code for the certificate. The QR code embeds the text given
-     * @param    string    $text Text to be added in the QR code
-     * @param    string    $path file path of the image
-     * */
+     * @param string $text Text to be added in the QR code
+     * @param string $path file path of the image
+     * @return bool
+     **/
     public function generate_qr($text, $path)
     {
         //Make sure HTML certificate is generated
@@ -427,6 +430,23 @@ class Certificate extends Model
         $user_certificate = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
         if (file_exists($user_certificate)) {
             $certificateContent = (string) file_get_contents($user_certificate);
+
+            if ($this->user_id == api_get_user_id() && !empty($this->certificate_data)) {
+                $certificateId = $this->certificate_data['id'];
+                $extraFieldValue = new ExtraFieldValue('user_certificate');
+                $value = $extraFieldValue->get_values_by_handler_and_field_variable(
+                    $certificateId,
+                    'downloaded_at'
+                );
+                if (empty($value)) {
+                    $params = [
+                        'item_id' => $this->certificate_data['id'],
+                        'extra_downloaded_at' => api_get_utc_datetime(),
+                    ];
+                    $extraFieldValue->saveFieldValues($params);
+                }
+            }
+
             echo $certificateContent;
             exit;
         }

+ 5 - 1
main/inc/lib/extra_field.lib.php

@@ -132,6 +132,9 @@ class ExtraField extends Model
             case 'career':
                 $this->extraFieldType = EntityExtraField::CAREER_FIELD_TYPE;
                 break;
+            case 'user_certificate':
+                $this->extraFieldType = EntityExtraField::USER_CERTIFICATE;
+                break;
         }
 
         $this->pageUrl  = 'extra_fields.php?type='.$this->type;
@@ -162,7 +165,8 @@ class ExtraField extends Model
             'lp_item',
             'skill',
             'work',
-            'career'
+            'career',
+            'user_certificate'
         );
     }