original_string: public static function export_user_role_to_cohort(int $userid) {
          global $DB;
  
          $rolesnames = self::get_roles_name();
          $sql = "SELECT ra.id, ra.contextid, ra.roleid, ra.userid, ra.timemodified, ra.modifierid, r.id as roleid
                    FROM {role_assignments} ra
                    JOIN {context} ctx
                      ON ctx.id = ra.contextid
                     AND ctx.contextlevel = :contextlevel
                     AND ra.component = 'tool_cohortroles'
                    JOIN {role} r
                      ON r.id = ra.roleid
                   WHERE ctx.instanceid = :instanceid
                      OR ra.userid = :userid";
          $params = ['userid' => $userid, 'instanceid' => $userid, 'contextlevel' => CONTEXT_USER];
          $assignments = $DB->get_recordset_sql($sql, $params);
          foreach ($assignments as $assignment) {
              $alldata[$assignment->contextid][$rolesnames[$assignment->roleid]][] = (object)[
                  'timemodified' => transform::datetime($assignment->timemodified),
                  'userid' => transform::user($assignment->userid),
                  'modifierid' => transform::user($assignment->modifierid)
              ];
          }
          $assignments->close();
          if (!empty($alldata)) {
              array_walk($alldata, function($roledata, $contextid) {
                  $context = \context::instance_by_id($contextid);
                  array_walk($roledata, function($data, $rolename) use ($context) {
                      writer::with_context($context)->export_related_data(
                              [get_string('privacy:metadata:role_cohortroles', 'core_role'), $rolename], 'cohortroles',
                              (object)$data);
                  });
              });
          }
      }
code: public static function export_user_role_to_cohort(int $userid) {
          global $DB;
  
          $rolesnames = self::get_roles_name();
          $sql = "SELECT ra.id, ra.contextid, ra.roleid, ra.userid, ra.timemodified, ra.modifierid, r.id as roleid
                    FROM {role_assignments} ra
                    JOIN {context} ctx
                      ON ctx.id = ra.contextid
                     AND ctx.contextlevel = :contextlevel
                     AND ra.component = 'tool_cohortroles'
                    JOIN {role} r
                      ON r.id = ra.roleid
                   WHERE ctx.instanceid = :instanceid
                      OR ra.userid = :userid";
          $params = ['userid' => $userid, 'instanceid' => $userid, 'contextlevel' => CONTEXT_USER];
          $assignments = $DB->get_recordset_sql($sql, $params);
          foreach ($assignments as $assignment) {
              $alldata[$assignment->contextid][$rolesnames[$assignment->roleid]][] = (object)[
                  'timemodified' => transform::datetime($assignment->timemodified),
                  'userid' => transform::user($assignment->userid),
                  'modifierid' => transform::user($assignment->modifierid)
              ];
          }
          $assignments->close();
          if (!empty($alldata)) {
              array_walk($alldata, function($roledata, $contextid) {
                  $context = \context::instance_by_id($contextid);
                  array_walk($roledata, function($data, $rolename) use ($context) {
                      writer::with_context($context)->export_related_data(
                              [get_string('privacy:metadata:role_cohortroles', 'core_role'), $rolename], 'cohortroles',
                              (object)$data);
                  });
              });
          }
      }
code_tokens: public static function export_user_role_to_cohort ( int $ userid ) { global $ DB ; $ rolesnames = self :: get_roles_name ( ) ; $ sql = "SELECT ra.id, ra.contextid, ra.roleid, ra.userid, ra.timemodified, ra.modifierid, r.id as roleid
                    FROM {role_assignments} ra
                    JOIN {context} ctx
                      ON ctx.id = ra.contextid
                     AND ctx.contextlevel = :contextlevel
                     AND ra.component = 'tool_cohortroles'
                    JOIN {role} r
                      ON r.id = ra.roleid
                   WHERE ctx.instanceid = :instanceid
                      OR ra.userid = :userid" ; $ params = [ 'userid' => $ userid , 'instanceid' => $ userid , 'contextlevel' => CONTEXT_USER ] ; $ assignments = $ DB -> get_recordset_sql ( $ sql , $ params ) ; foreach ( $ assignments as $ assignment ) { $ alldata [ $ assignment -> contextid ] [ $ rolesnames [ $ assignment -> roleid ] ] [ ] = ( object ) [ 'timemodified' => transform :: datetime ( $ assignment -> timemodified ) , 'userid' => transform :: user ( $ assignment -> userid ) , 'modifierid' => transform :: user ( $ assignment -> modifierid ) ] ; } $ assignments -> close ( ) ; if ( ! empty ( $ alldata ) ) { array_walk ( $ alldata , function ( $ roledata , $ contextid ) { $ context = \ context :: instance_by_id ( $ contextid ) ; array_walk ( $ roledata , function ( $ data , $ rolename ) use ( $ context ) { writer :: with_context ( $ context ) -> export_related_data ( [ get_string ( 'privacy:metadata:role_cohortroles' , 'core_role' ) , $ rolename ] , 'cohortroles' , ( object ) $ data ) ; } ) ; } ) ; } }
docstring: Exports the data relating to tool_cohortroles component on role assignments by
  Assign user roles to cohort feature.
  
  @param  int $userid The user ID.
docstring_tokens: Exports the data relating to tool_cohortroles component on role assignments by Assign user roles to cohort feature .