Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SearchKit - Allow random sorting (Fixes dev/report#75) #21177

Merged
merged 1 commit into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Civi/Api4/Query/SqlFunctionRAND.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Query;

/**
* Sql function
*/
class SqlFunctionRAND extends SqlFunction {

protected static $category = self::CATEGORY_MATH;

protected static function params(): array {
return [];
}

/**
* @return string
*/
public static function getTitle(): string {
return ts('Random Number');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,18 @@
return _.findIndex(ctrl.display.settings.sort, [key]) >= 0;
}
return {
results: [{
text: ts('Columns'),
children: ctrl.crmSearchAdmin.getSelectFields(disabledIf)
}].concat(ctrl.crmSearchAdmin.getAllFields('', ['Field', 'Custom'], disabledIf))
results: [
{
text: ts('Random'),
icon: 'crm-i fa-random',
id: 'RAND()',
disabled: disabledIf('RAND()')
},
{
text: ts('Columns'),
children: ctrl.crmSearchAdmin.getSelectFields(disabledIf)
}
].concat(ctrl.crmSearchAdmin.getAllFields('', ['Field', 'Custom'], disabledIf))
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="form-inline" ng-repeat="sort in $ctrl.display.settings.sort">
<label for="crm-search-display-sort-{{$index}}">{{ $index ? ts('Also by') : ts('Sort by') }}</label>
<input id="crm-search-display-sort-{{$index}}" class="form-control huge" ng-model="sort[0]" crm-ui-select="{data: $ctrl.parent.fieldsForSort}" />
<select class="form-control" ng-model="sort[1]">
<select class="form-control" ng-model="sort[1]" ng-show="sort[0] !== 'RAND()'">
<option value="ASC">{{ ts('Ascending') }}</option>
<option value="DESC">{{ ts('Descending') }}</option>
</select>
Expand Down
17 changes: 17 additions & 0 deletions tests/phpunit/api/v4/Action/SqlFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,21 @@ public function testIncorrectNumberOfArguments() {
}
}

public function testRandFunction() {
$cid = Contact::create(FALSE)
->addValue('first_name', 'hello')
->execute()->first()['id'];

$result = Contact::get(FALSE)
->addSelect('RAND() AS rand')
->addOrderBy('RAND()')
->setDebug(TRUE)
->setLimit(1)
->execute();

$this->assertStringContainsString('ORDER BY RAND()', $result->debug['sql'][0]);
$this->assertGreaterThanOrEqual(0, $result[0]['rand']);
$this->assertLessThan(1, $result[0]['rand']);
}

}