@@ -3066,29 +3066,23 @@ public function survey_is_public_master() {
3066
3066
* @param array $questionsbyposition
3067
3067
* @param int $nbinfocols
3068
3068
* @param int $numrespcols
3069
- * @param int $showincompletes
3069
+ * @param array $options
3070
+ * @param array $identityfields
3070
3071
* @return array
3071
3072
*/
3072
3073
protected function process_csv_row (array &$ row ,
3073
3074
stdClass $ resprow ,
3074
3075
$ currentgroupid ,
3075
3076
array &$ questionsbyposition ,
3076
3077
$ nbinfocols ,
3077
- $ numrespcols , $ showincompletes = 0 ) {
3078
+ $ numrespcols ,
3079
+ $ options ,
3080
+ $ identityfields ) {
3078
3081
global $ DB ;
3079
3082
3080
- static $ config = null ;
3081
3083
// If using an anonymous response, map users to unique user numbers so that number of unique anonymous users can be seen.
3082
3084
static $ anonumap = [];
3083
3085
3084
- if ($ config === null ) {
3085
- $ config = get_config ('questionnaire ' , 'downloadoptions ' );
3086
- }
3087
- $ options = empty ($ config ) ? array () : explode (', ' , $ config );
3088
- if ($ showincompletes == 1 ) {
3089
- $ options [] = 'complete ' ;
3090
- }
3091
-
3092
3086
$ positioned = [];
3093
3087
$ user = new stdClass ();
3094
3088
foreach ($ this ->user_fields () as $ userfield ) {
@@ -3184,6 +3178,9 @@ protected function process_csv_row(array &$row,
3184
3178
if (in_array ('complete ' , $ options )) {
3185
3179
array_push ($ positioned , $ resprow ->complete );
3186
3180
}
3181
+ foreach ($ identityfields as $ field ) {
3182
+ array_push ($ positioned , $ resprow ->$ field );
3183
+ }
3187
3184
3188
3185
for ($ c = $ nbinfocols ; $ c < $ numrespcols ; $ c ++) {
3189
3186
if (isset ($ row [$ c ])) {
@@ -3234,11 +3231,18 @@ public function generate_csv($currentgroupid, $rid='', $userid='', $choicecodes=
3234
3231
if (in_array ($ option , array ('response ' , 'submitted ' , 'id ' ))) {
3235
3232
$ columns [] = get_string ($ option , 'questionnaire ' );
3236
3233
$ types [] = 0 ;
3234
+ } else if ($ option == 'useridentityfields ' ) {
3235
+ // Ignore option.
3236
+ continue ;
3237
3237
} else {
3238
3238
$ columns [] = get_string ($ option );
3239
3239
$ types [] = 1 ;
3240
3240
}
3241
3241
}
3242
+ $ identityfields = $ this ->get_identity_fields ($ options );
3243
+ foreach ($ identityfields as $ field ) {
3244
+ $ columns [] = \core_user \fields::get_display_name ($ field );
3245
+ }
3242
3246
$ nbinfocols = count ($ columns );
3243
3247
3244
3248
$ idtocsvmap = array (
@@ -3469,6 +3473,7 @@ public function generate_csv($currentgroupid, $rid='', $userid='', $choicecodes=
3469
3473
if ($ rankaverages ) {
3470
3474
$ averagerow = [];
3471
3475
}
3476
+ $ useridentityfields = [];
3472
3477
foreach ($ allresponsesrs as $ responserow ) {
3473
3478
$ rid = $ responserow ->rid ;
3474
3479
$ qid = $ responserow ->question_id ;
@@ -3478,6 +3483,21 @@ public function generate_csv($currentgroupid, $rid='', $userid='', $choicecodes=
3478
3483
continue ;
3479
3484
}
3480
3485
3486
+ if (!empty ($ identityfields )) {
3487
+ // Get identity fields for user.
3488
+ if (isset ($ useridentityfields [$ responserow ->userid ])) {
3489
+ $ customfields = $ useridentityfields [$ responserow ->userid ];
3490
+ } else {
3491
+ $ customfields = self ::get_user_identity_fields ($ this ->context , $ responserow ->userid );
3492
+ $ useridentityfields [$ responserow ->userid ] = $ customfields ;
3493
+ }
3494
+
3495
+ // Set profile fields for user in response row.
3496
+ foreach ($ identityfields as $ field ) {
3497
+ $ responserow ->{$ field } = $ customfields ->{$ field };
3498
+ }
3499
+ }
3500
+
3481
3501
$ question = $ this ->questions [$ qid ];
3482
3502
$ qtype = intval ($ question ->type_id );
3483
3503
if ($ rankaverages ) {
@@ -3494,7 +3514,7 @@ public function generate_csv($currentgroupid, $rid='', $userid='', $choicecodes=
3494
3514
3495
3515
if ($ prevresprow !== false && $ prevresprow ->rid !== $ rid ) {
3496
3516
$ output [] = $ this ->process_csv_row ($ row , $ prevresprow , $ currentgroupid , $ questionsbyposition ,
3497
- $ nbinfocols , $ numrespcols , $ showincompletes );
3517
+ $ nbinfocols , $ numrespcols , $ options , $ identityfields );
3498
3518
$ row = [];
3499
3519
}
3500
3520
@@ -3576,7 +3596,7 @@ public function generate_csv($currentgroupid, $rid='', $userid='', $choicecodes=
3576
3596
if ($ prevresprow !== false ) {
3577
3597
// Add final row to output. May not exist if no response data was ever present.
3578
3598
$ output [] = $ this ->process_csv_row ($ row , $ prevresprow , $ currentgroupid , $ questionsbyposition ,
3579
- $ nbinfocols , $ numrespcols , $ showincompletes );
3599
+ $ nbinfocols , $ numrespcols , $ options , $ identityfields );
3580
3600
}
3581
3601
3582
3602
// Add averages row if appropriate.
@@ -4121,4 +4141,39 @@ public function get_all_file_areas() {
4121
4141
4122
4142
return $ areas ;
4123
4143
}
4144
+
4145
+ /**
4146
+ * Gets the identity fields.
4147
+ *
4148
+ * @param array $options
4149
+ * @return array
4150
+ */
4151
+ protected function get_identity_fields ($ options ) {
4152
+ $ fields = !in_array ('useridentityfields ' , $ options ) || $ this ->respondenttype == 'anonymous ' ? [] :
4153
+ \core_user \fields::get_identity_fields ($ this ->context );
4154
+ return $ fields ;
4155
+ }
4156
+
4157
+ /**
4158
+ * Gets the identity fields values for a user.
4159
+ *
4160
+ * @param object $context
4161
+ * @param int $userid
4162
+ * @return array
4163
+ */
4164
+ public static function get_user_identity_fields ($ context , $ userid ) {
4165
+ global $ DB ;
4166
+
4167
+ $ fields = \core_user \fields::for_identity ($ context );
4168
+ [
4169
+ 'selects ' => $ selects ,
4170
+ 'joins ' => $ joins ,
4171
+ 'params ' => $ params
4172
+ ] = (array )$ fields ->get_sql ('u ' , false , '' , '' , false );
4173
+ $ sql = "SELECT $ selects
4174
+ FROM {user} u $ joins
4175
+ WHERE u.id = ? " ;
4176
+ $ row = $ DB ->get_record_sql ($ sql , array_merge ($ params , [$ userid ]));
4177
+ return $ row ;
4178
+ }
4124
4179
}
0 commit comments