Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 39 and 311]

This is a one-line short description of the file You can have a rather longer description of the file as well, if you like, and it can span multiple lines.

Copyright: Petr Skoda
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 571 lines (18 kb)
Included or required:0 times
Referenced: 1 time
Includes or requires: 0 files

Defines 2 classes

csv_import_reader:: (13 methods):
  __construct()
  __destruct()
  load_csv_content()
  get_columns()
  init()
  next()
  close()
  get_error()
  cleanup()
  get_delimiter_list()
  get_delimiter()
  get_encoded_delimiter()
  get_new_iid()

csv_export_writer:: (10 methods):
  __construct()
  set_temp_file_path()
  add_data()
  print_csv_data()
  set_filename()
  send_header()
  download_file()
  download_array()
  print_array()
  __destruct()


Class: csv_import_reader  - X-Ref

Utitily class for importing of CSV files.

__construct($iid, $type)   X-Ref
Contructor

param: int $iid import identifier
param: string $type which script imports?

__destruct()   X-Ref
Make sure the file is closed when this object is discarded.


load_csv_content($content, $encoding, $delimiter_name, $column_validation=null, $enclosure='"')   X-Ref
Parse this content

param: string $content the content to parse.
param: string $encoding content encoding
param: string $delimiter_name separator (comma, semicolon, colon, cfg)
param: string $column_validation name of function for columns validation, must have one param $columns
param: string $enclosure field wrapper. One character only.
return: bool false if error, count of data lines if ok; use get_error() to get error string

get_columns()   X-Ref
Returns list of columns

return: array

init()   X-Ref
Init iterator.

return: bool Success

next()   X-Ref
Get next line

return: mixed false, or an array of values

close()   X-Ref
Release iteration related resources

return: void

get_error()   X-Ref
Get last error

return: string error text of null if none

cleanup($full=false)   X-Ref
Cleanup temporary data

param: boolean $full true means do a full cleanup - all sessions for current user, false only the active iid

get_delimiter_list()   X-Ref
Get list of cvs delimiters

return: array suitable for selection box

get_delimiter($delimiter_name)   X-Ref
Get delimiter character

param: string separator name
return: string delimiter char

get_encoded_delimiter($delimiter_name)   X-Ref
Get encoded delimiter character

param: string separator name
return: string encoded delimiter char

get_new_iid($type)   X-Ref
Create new import id

param: string who imports?
return: int iid

Class: csv_export_writer  - X-Ref

Utitily class for exporting of CSV files.

__construct($delimiter = 'comma', $enclosure = '"', $mimetype = 'application/download', $bom = false)   X-Ref
Constructor for the csv export reader

param: string $delimiter      The name of the character used to seperate fields. Supported types(comma, tab, semicolon, colon, cfg)
param: string $enclosure      The character used for determining the enclosures.
param: string $mimetype       Mime type of the file that we are exporting.
param: boolean $bom           If true, prefix file with byte order mark.

set_temp_file_path()   X-Ref
Set the file path to the temporary file.


add_data($row)   X-Ref
Add data to the temporary file in csv format

param: array $row  An array of values.

print_csv_data($return = false)   X-Ref
Echos or returns a csv data line by line for displaying.

param: bool $return  Set to true to return a string with the csv data.
return: string       csv data.

set_filename($dataname, $extension = '.csv')   X-Ref
Set the filename for the uploaded csv file

param: string $dataname    The name of the module.
param: string $extenstion  File extension for the file.

send_header()   X-Ref
Output file headers to initialise the download of the file.


download_file()   X-Ref
Download the csv file.


download_array($filename, array &$records, $delimiter = 'comma', $enclosure='"')   X-Ref
Creates a file for downloading an array into a deliminated format.
This function is useful if you are happy with the defaults and all of your
information is in one array.

param: string $filename    The filename of the file being created.
param: array $records      An array of information to be converted.
param: string $delimiter   The name of the delimiter. Supported types(comma, tab, semicolon, colon, cfg)
param: string $enclosure   How speical fields are enclosed.

print_array(array &$records, $delimiter = 'comma', $enclosure = '"', $return = false)   X-Ref
This will convert an array of values into a deliminated string.
Like the above function, this is for convenience.

param: array $records     An array of information to be converted.
param: string $delimiter  The name of the delimiter. Supported types(comma, tab, semicolon, colon, cfg)
param: string $enclosure  How speical fields are enclosed.
param: bool $return       If true will return a string with the csv data.
return: string            csv data.

__destruct()   X-Ref
Make sure that everything is closed when we are finished.