MIDDAG for Moodle

bigquery_sendcsv_service

Table of Contents

Methods

can_execute()  : bool
execute()  : bool
Executes the upload process for a CSV file, including data validation, file preparation, and interaction with the BigQuery API. This method handles exceptions and ensures that the operation complies with the required configuration and logic.
foreignkeys()  : string
Gets the foreign keys for the table.
generate_data()  : array<int, array<string, mixed>>
Generates and returns an array of data rows mapped based on the column mapping configuration.
get_fields()  : array<int, array{name: mixed, type: mixed}>
Gets the list of fields with their names and types based on the column mapping configuration.
get_header()  : array<int, string>
Gets the header row for the CSV file based on the column mapping configuration.
get_name()  : string
Gets the name of the service.
get_options()  : array<string, mixed>
Generates and returns an array of options for configuring a BigQuery table.
is_valid_data_type()  : bool
Validates if the data types of the given data entries match the specified types.
primarykey()  : string
Gets the primary key for the table.
get_filename()  : string
Generates a unique filename based on the class name and current timestamp.
get_filepath()  : string
Generates and retrieves the full file path for a given file extension.
save_to_csv()  : string
Saves the provided data to a CSV file with an optional header row.
save_to_json()  : string
Saves the given data array to a JSON file, where each element is written as a single JSON object per line.

Methods

execute()

Executes the upload process for a CSV file, including data validation, file preparation, and interaction with the BigQuery API. This method handles exceptions and ensures that the operation complies with the required configuration and logic.

public execute(uploadcsvfile $uploadcsvfile) : bool
Parameters
$uploadcsvfile : uploadcsvfile
Return values
bool

true if the operation completes successfully, otherwise an exception is thrown

generate_data()

Generates and returns an array of data rows mapped based on the column mapping configuration.

public generate_data(mixed $uploadcsvfile) : array<int, array<string, mixed>>
Parameters
$uploadcsvfile : mixed
Return values
array<int, array<string, mixed>>

processed data rows with mapped columns

get_fields()

Gets the list of fields with their names and types based on the column mapping configuration.

public get_fields(mixed $uploadcsvfile) : array<int, array{name: mixed, type: mixed}>
Parameters
$uploadcsvfile : mixed
Return values
array<int, array{name: mixed, type: mixed}>

get_header()

Gets the header row for the CSV file based on the column mapping configuration.

public get_header(mixed $uploadcsvfile) : array<int, string>
Parameters
$uploadcsvfile : mixed
Return values
array<int, string>

get_options()

Generates and returns an array of options for configuring a BigQuery table.

public get_options(mixed $uploadcsvfile) : array<string, mixed>
Parameters
$uploadcsvfile : mixed
Return values
array<string, mixed>

is_valid_data_type()

Validates if the data types of the given data entries match the specified types.

public is_valid_data_type(array<string|int, mixed> $data, mixed $uploadcsvfile) : bool
Parameters
$data : array<string|int, mixed>

an array of data rows to validate, where each row contains key-value pairs representing the data

$uploadcsvfile : mixed
Return values
bool

true if all data entries have valid types; false otherwise

get_filename()

Generates a unique filename based on the class name and current timestamp.

protected static get_filename(string $extension) : string
Parameters
$extension : string

the file extension to be appended to the filename

Return values
string

The generated filename in the format: ClassName-YYYY-MM-DD-HH-MM-SS.extension

get_filepath()

Generates and retrieves the full file path for a given file extension.

protected get_filepath(string $extension) : string
Parameters
$extension : string

the file extension used to construct the filename

Return values
string

the complete file path including directory and filename

save_to_csv()

Saves the provided data to a CSV file with an optional header row.

protected save_to_csv(array<string|int, mixed> $data, array<string|int, mixed> $header) : string
Parameters
$data : array<string|int, mixed>

The data to be written to the CSV file. Each element represents a row.

$header : array<string|int, mixed>

The header row to be written at the top of the CSV file. Pass an empty array if no header is needed.

Tags
throws
Exception

if there is an error during the file writing process

Return values
string

the file path of the generated CSV file

save_to_json()

Saves the given data array to a JSON file, where each element is written as a single JSON object per line.

protected save_to_json(array<string|int, mixed> $data) : string
Parameters
$data : array<string|int, mixed>

the data to be written to the JSON file, with each element representing a row

Tags
throws
Exception

if an error occurs while saving the file, an exception is thrown with relevant details

Return values
string

the file path of the saved JSON file


        
On this page

Search results