Browse Source

Add configuration "exercise_category_report_user_extra_fields" BT#13789

- Option to add extra fields to the exercise_category_report.php page
jmontoyaa 7 years ago
parent
commit
a8974e8061

+ 38 - 18
main/inc/ajax/model.ajax.php

@@ -1228,28 +1228,49 @@ switch ($action) {
         $documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path']."/document";
         $sessionId = api_get_session_id();
 
-        $columns = array(
+        $columns = [
             'firstname',
             'lastname',
-            'username',
-            'session',
-            'session_access_start_date',
-            'exe_date',
-            'score'
-        );
+            'username'
+        ];
+        $extraFieldsToAdd = [];
+        $extraFields = api_get_configuration_value('exercise_category_report_user_extra_fields');
+        if (!empty($extraFields) && isset($extraFields['fields'])) {
+            $extraField = new ExtraField('user');
+            foreach ($extraFields['fields'] as $variable) {
+                $info = $extraField->get_handler_field_info_by_field_variable($variable);
+                if ($info) {
+                    $extraFieldsToAdd[] = $variable;
+                }
+            }
+        }
+        if (!empty($extraFieldsToAdd)) {
+            $columns = array_merge($columns, $extraFieldsToAdd);
+        }
+
+        $columns[] = 'session';
+        $columns[] = 'session_access_start_date';
+        $columns[] = 'exe_date';
+        $columns[] = 'score';
 
         if ($operation == 'excel') {
-            $columns = array(
+            $columns = [
                 'firstname',
                 'lastname',
-                'username',
-                'session',
-                'session_access_start_date',
-                'exe_date',
-                'score_percentage',
-                'only_score',
-                'total'
-            );
+                'username'
+            ];
+
+            if (!empty($extraFieldsToAdd)) {
+                $columns = array_merge($columns, $extraFieldsToAdd);
+            }
+
+            $columns[] = 'session';
+            $columns[] = 'session_access_start_date';
+            $columns[] = 'exe_date';
+            $columns[] = 'score_percentage';
+            $columns[] = 'only_score';
+            $columns[] = 'total';
+
             $overwriteColumnHeaderExport['session_access_start_date'] = get_lang('SessionStartDate');
             $overwriteColumnHeaderExport['exe_date'] = get_lang('StartDate');
             $overwriteColumnHeaderExport['score_percentage'] = get_lang('Score');
@@ -1262,7 +1283,6 @@ switch ($action) {
         if (!empty($categoryList)) {
             foreach ($categoryList as $categoryInfo) {
                 $label = 'category_'.$categoryInfo['id'];
-
                 if ($operation == 'excel') {
                     $columns[] = $label.'_score_percentage';
                     $columns[] = $label.'_only_score';
@@ -1292,7 +1312,7 @@ switch ($action) {
             $courseInfo['code'],
             true,
             true,
-            !empty($sessionId)
+            $extraFieldsToAdd
         );
         break;
     case 'get_hotpotatoes_exercise_results':

+ 21 - 11
main/inc/lib/exercise.lib.php

@@ -1660,6 +1660,7 @@ HOTSPOT;
      * @param string $courseCode
      * @param bool $showSessionField
      * @param bool $showExerciseCategories
+     * @param array $userExtraFieldsToAdd
      *
      * @return array
      */
@@ -1673,7 +1674,8 @@ HOTSPOT;
         $get_count = false,
         $courseCode = null,
         $showSessionField = false,
-        $showExerciseCategories = false
+        $showExerciseCategories = false,
+        $userExtraFieldsToAdd = []
     ) {
         //@todo replace all this globals
         global $documentPath, $filter;
@@ -1954,7 +1956,7 @@ HOTSPOT;
             $oldIds = array_column($lp_list, 'lp_old_id', 'iid');
 
             if (is_array($results)) {
-                $users_array_id = array();
+                $users_array_id = [];
                 $from_gradebook = false;
                 if (isset($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
                     $from_gradebook = true;
@@ -2045,24 +2047,18 @@ HOTSPOT;
                     $dt = api_convert_and_format_date($results[$i]['exe_weighting']);
 
                     // we filter the results if we have the permission to
+                    $result_disabled = 0;
                     if (isset($results[$i]['results_disabled'])) {
                         $result_disabled = intval(
                             $results[$i]['results_disabled']
                         );
-                    } else {
-                        $result_disabled = 0;
                     }
-
                     if ($result_disabled == 0) {
                         $my_res = $results[$i]['exe_result'];
                         $my_total = $results[$i]['exe_weighting'];
 
-                        $results[$i]['start_date'] = api_get_local_time(
-                            $results[$i]['start_date']
-                        );
-                        $results[$i]['exe_date'] = api_get_local_time(
-                            $results[$i]['exe_date']
-                        );
+                        $results[$i]['start_date'] = api_get_local_time($results[$i]['start_date']);
+                        $results[$i]['exe_date'] = api_get_local_time($results[$i]['exe_date']);
 
                         if (!$results[$i]['propagate_neg'] && $my_res < 0) {
                             $my_res = 0;
@@ -2218,6 +2214,20 @@ HOTSPOT;
                             $actions .= $attempt_link;
                         }
                         $actions .= '</div>';
+
+                        if (!empty($userExtraFieldsToAdd)) {
+                            foreach ($userExtraFieldsToAdd as $variable) {
+                                $extraFieldValue = new ExtraFieldValue('user');
+                                $values = $extraFieldValue->get_values_by_handler_and_field_variable(
+                                    $results[$i]['user_id'],
+                                    $variable
+                                );
+                                if (isset($values['value'])) {
+                                    $results[$i][$variable] = $values['value'];
+                                }
+                            }
+                        }
+
                         $exeId = $results[$i]['exe_id'];
                         $results[$i]['id'] = $exeId;
                         $category_list = [];

+ 3 - 0
main/install/configuration.dist.php

@@ -711,6 +711,9 @@ $_configuration['gradebook_badge_sidebar'] = [
 // Block access to any user to "my progress" page
 //$_configuration['block_my_progress_page'] = false;
 
+// Add user extra fields in report: main/mySpace/exercise_category_report.php
+//$_configuration['exercise_category_report_user_extra_fields'] = ['fields' => ['skype', 'rssfeeds']];
+
 
 // ------ Custom DB changes
 // Add user activation by confirmation email

+ 72 - 13
main/mySpace/exercise_category_report.php

@@ -82,6 +82,8 @@ $form->addButtonSearch(get_lang('Search'));
 Display::display_header($nameTools);
 $form->display();
 
+$extraFields = api_get_configuration_value('exercise_category_report_user_extra_fields');
+
 if ($form->validate() && !empty($courseInfo)) {
     $values = $form->getSubmitValues();
     $exerciseId = isset($values['exercise_id']) ? $values['exercise_id'] : 0;
@@ -93,13 +95,24 @@ if ($form->validate() && !empty($courseInfo)) {
     $columns = [
         get_lang('FirstName'),
         get_lang('LastName'),
-        get_lang('LoginName'),
-        get_lang('Session'),
-        get_lang('SessionStartDate'),
-        get_lang('StartDate'),
-        get_lang('Score')
+        get_lang('LoginName')
     ];
 
+    if (!empty($extraFields) && isset($extraFields['fields'])) {
+        $extraField = new ExtraField('user');
+        foreach ($extraFields['fields'] as $variable) {
+            $info = $extraField->get_handler_field_info_by_field_variable($variable);
+            if ($info) {
+                $columns[] = $info['display_text'];
+            }
+        }
+    }
+
+    $columns[] = get_lang('Session');
+    $columns[] = get_lang('SessionStartDate');
+    $columns[] = get_lang('StartDate');
+    $columns[] = get_lang('Score');
+
     if (!empty($categoryList)) {
         foreach ($categoryList as $categoryInfo) {
             $columns[] = $categoryInfo['title'];
@@ -109,18 +122,64 @@ if ($form->validate() && !empty($courseInfo)) {
 
     $columnModel = [
         ['name' => 'firstname', 'index' => 'firstname', 'width' => '50', 'align' => 'left', 'search' => 'true'],
-        ['name' => 'lastname', 'index' => 'lastname', 'width' => '50', 'align' => 'left', 'formatter' => 'action_formatter', 'search' => 'true'],
-        ['name' => 'login', 'index' => 'username', 'width' => '40', 'align' => 'left', 'search' => 'true', 'hidden' => 'true'],
-        ['name' => 'session', 'index' => 'session', 'width' => '40', 'align' => 'left', 'search' => 'false'],
         [
-            'name' => 'session_access_start_date',
-            'index' => 'session_access_start_date',
+            'name' => 'lastname',
+            'index' => 'lastname',
             'width' => '50',
-            'align' => 'center',
+            'align' => 'left',
+            'formatter' => 'action_formatter',
             'search' => 'true',
         ],
-        ['name' => 'exe_date', 'index' => 'exe_date', 'width' => '60', 'align' => 'left', 'search' => 'true'],
-        ['name' => 'score', 'index' => 'exe_result', 'width' => '50', 'align' => 'center', 'search' => 'true'],
+        [
+            'name' => 'login',
+            'index' => 'username',
+            'width' => '40',
+            'align' => 'left',
+            'search' => 'true',
+            'hidden' => 'true',
+        ],
+    ];
+
+    if (!empty($extraFields) && isset($extraFields['fields'])) {
+        $extraField = new ExtraField('user');
+        foreach ($extraFields['fields'] as $variable) {
+            $columnModel[] = [
+                'name' => $variable,
+                'index' => $variable,
+                'width' => '40',
+                'align' => 'left',
+                'search' => 'false',
+            ];
+        }
+    }
+
+    $columnModel[] = [
+        'name' => 'session',
+        'index' => 'session',
+        'width' => '40',
+        'align' => 'left',
+        'search' => 'false',
+    ];
+    $columnModel[] = [
+        'name' => 'session_access_start_date',
+        'index' => 'session_access_start_date',
+        'width' => '50',
+        'align' => 'center',
+        'search' => 'true',
+    ];
+    $columnModel[] = [
+        'name' => 'exe_date',
+        'index' => 'exe_date',
+        'width' => '60',
+        'align' => 'left',
+        'search' => 'true',
+    ];
+    $columnModel[] = [
+        'name' => 'score',
+        'index' => 'exe_result',
+        'width' => '50',
+        'align' => 'center',
+        'search' => 'true',
     ];
 
     if (!empty($categoryList)) {