Skip to content

Commit

Permalink
implemented test sort to allow for runtime sort
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffcaseyyet committed Nov 12, 2024
1 parent 8ae97a0 commit 8f978d0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/transformer/repos/MoodleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ public function __construct($store) {
*
* @param string $type The name of the table to retrieve from.
* @param array $query Any additional conditions to add to the query.
* @param string $sort Sort string for how to order the data.
* @return array
*/
public function read_records(string $type, array $query) {
return $this->store->get_records($type, $query);
public function read_records(string $type, array $query, string $sort = '') {
return $this->store->get_records($type, $query, $sort);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/transformer/repos/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ abstract class Repository extends PhpObj {
*
* @param string $type The name of the table to retrieve from.
* @param array $query Any additional conditions to add to the query.
* @param string $sort Sort string for how to order the data.
* @return array
*/
abstract public function read_records(string $type, array $query);
abstract public function read_records(string $type, array $query, string $sort = '');

/**
* Reads an object from the store with the given type and query.
Expand Down
20 changes: 19 additions & 1 deletion src/transformer/repos/TestRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ public function __construct($testdata) {
*
* @param string $type The name of the table to retrieve from.
* @param array $query Any additional conditions to add to the query.
* @param string $sort Sort string for how to order the data.
* @return array
*/
public function read_records(string $type, array $query) {
public function read_records(string $type, array $query, string $sort = '') {
$records = $this->testdata->$type;
$matchingrecords = [];

Expand All @@ -61,6 +62,23 @@ public function read_records(string $type, array $query) {
}
}

//Must account for lack of SQL and implement multidimensional sort with SQL syntax
if ($sort != ''){
//Split by commas for each field argument
$fields = explode(',', $sort);

$sortargs = array();
foreach ($fields as $field_declaration){
//Remove (and record) direction, trim
$desc = str_contains(strtolower($field_declaration), 'desc');
$field = preg_replace('/(DESC|ASC|\s)/i', '', $field_declaration);

array_push($sortargs, array_column($matchingrecords, $field), ($desc) ? SORT_DESC : SORT_ASC);
}
$sortargs[] = &$matchingrecords;
array_multisort(...$sortargs);
}

return $matchingrecords;
}
}
8 changes: 4 additions & 4 deletions tests/mod_choice/answer_created/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
],
"choice_options": [
{
"id": 1,
"id": 2,
"choiceid": 1,
"text": "To be"
"text": "Not to be"
},
{
"id": 2,
"id": 1,
"choiceid": 1,
"text": "Not to be"
"text": "To be"
}
],
"choice_answers": [
Expand Down

0 comments on commit 8f978d0

Please sign in to comment.