Skip to content

Commit

Permalink
Matching course
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Sep 18, 2024
1 parent fb908d4 commit 9a08211
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 29 deletions.
109 changes: 87 additions & 22 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,52 @@ class helper {
/** @var string ALL_COLUMNS Flag to indicate we search all columns in a table **/
const ALL_COLUMNS = 'all columns';

/**
* Get columns to search for in a table.
*
* @param string $table The table to search.
* @param string $column The column to search.
* @return array The columns to search.
*/
public static function get_columns(string $table, string $column = ''): array {
global $DB;
$columns = $DB->get_columns($table);

if ($column !== self::ALL_COLUMNS) {
// Only search the specified column.
$columns = array_filter($columns, function($col) use ($column) {
return $col->name == $column;
});
}

return $columns;
}

/**
* Find course field in the table.
*
* @param string $table The table to search.
* @return string The course field name.
*/
public static function find_course_field(string $table): string {
global $DB;

// Potential course field names.
$coursefields = ['course', 'courseid'];

$columns = $DB->get_columns($table);
$coursefield = '';

foreach ($columns as $column) {
if (in_array($column->name, $coursefields)) {
$coursefield = $column->name;
break;
}
}

return $coursefield;
}

/**
* Perform a plain text search on a table and column.
*
Expand All @@ -45,24 +91,31 @@ private static function plain_text_search(string $search, string $table,

$results = [];

$columns = $DB->get_columns($table);
$columns = self::get_columns($table, $column);

if ($column !== self::ALL_COLUMNS) {
// Only search the specified column.
$columns = array_filter($columns, function($col) use ($column) {
return $col->name == $column;
});
}
// Potential course field in the table.
$coursefield = self::find_course_field($table);

// Table alias.
$tablealias = 't';

foreach ($columns as $column) {
$columnname = $DB->get_manager()->generator->getEncQuoted($column->name);

$searchsql = $DB->sql_like($columnname, '?', false);
$searchsql = $DB->sql_like("$tablealias." . $columnname, '?', false);
$searchparam = '%'.$DB->sql_like_escape($search).'%';

$sql = "SELECT id, $columnname
FROM {".$table."}
WHERE $searchsql";
if (!empty($coursefield)) {
$sql = "SELECT $tablealias.id,
$tablealias.$columnname,
$tablealias.$coursefield as courseid,
c.idnumber as courseidnumber
FROM {".$table."} t
JOIN {course} c ON c.id = t.$coursefield
WHERE $searchsql";
} else {
$sql = "SELECT id, $columnname FROM {".$table."} $tablealias WHERE $searchsql";
}

if ($column->meta_type === 'X' || $column->meta_type === 'C') {
$records = $DB->get_records_sql($sql, [$searchparam], 0, $limit);
Expand All @@ -82,11 +135,11 @@ private static function plain_text_search(string $search, string $table,
* @param string $search The regular expression to search for.
* @param string $table The table to search.
* @param string $column The column to search.
* @param $limit The maximum number of results to return.
* @param int $limit The maximum number of results to return.
* @return array
*/
private static function regular_expression_search(string $search, string $table,
string $column = self::ALL_COLUMNS, $limit = 0): array {
string $column = self::ALL_COLUMNS, int $limit = 0): array {
global $DB;

// Check if the database supports regular expression searches.
Expand All @@ -96,23 +149,35 @@ private static function regular_expression_search(string $search, string $table,

$results = [];

$columns = $DB->get_columns($table);
$columns = self::get_columns($table, $column);

if ($column !== self::ALL_COLUMNS) {
// Only search the specified column.
$columns = array_filter($columns, function($col) use ($column) {
return $col->name == $column;
});
}
// Find Potential course field in the table.
$coursefield = self::find_course_field($table);

// Table alias.
$tablealias = 't';

foreach ($columns as $column) {
$columnname = $DB->get_manager()->generator->getEncQuoted($column->name);

$select = $columnname . ' ' . $DB->sql_regex() . ' :pattern ';
$select = "$tablealias." . $columnname . ' ' . $DB->sql_regex() . ' :pattern ';
$params = ['pattern' => $search];

if ($column->meta_type === 'X' || $column->meta_type === 'C') {
$records = $DB->get_records_select($table, $select, $params, '', '*', 0, $limit);

if (!empty($coursefield)) {
$sql = "SELECT $tablealias.id,
$tablealias.$columnname,
$tablealias.$coursefield as courseid,
c.idnumber as courseidnumber
FROM {".$table."} $tablealias
JOIN {course} c ON c.id = $tablealias.$coursefield
WHERE $select";
} else {
$sql = "SELECT id, $columnname FROM {".$table."} $tablealias WHERE $select";
}

$records = $DB->get_records_sql($sql, $params, 0, $limit);

if ($records) {
$results[$table][$column->name] = $records;
Expand Down
19 changes: 12 additions & 7 deletions cli/find.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
}

// Perform the search.
$result = helper::search($search, !empty($options['regex-match']), $tables, $options['regex-match'] ? 1 : 0);
$result = helper::search($search, !empty($options['regex-match']), $tables, $options['summary'] ? 1 : 0);

// Notifying the user if no results were found.
if (empty($result)) {
Expand All @@ -106,20 +106,25 @@

// Show header.
if (!$options['summary']) {
fputcsv($fp, ['Table', 'Column', 'ID', 'Match']);
fputcsv($fp, ['Table', 'Column', 'courseid', 'idnumber', 'ID', 'Match']);
} else {
fputcsv($fp, ['Table', 'Column']);
fputcsv($fp, ['Table', 'Column', 'courseid', 'idnumber']);
}

// Output the result.
foreach ($result as $table => $columns) {
foreach ($columns as $column => $rows) {
if ($options['summary']) {
echo "$table, $column\n";
$courseid = reset($rows)->courseid ?? '';
$courseidnumber = reset($rows)->courseidnumber ?? '';
fputcsv($fp, [$table, $column, $courseid, $courseidnumber]);
} else {
foreach ($rows as $row) {
// Fields to show.
$id = $row->id;
$courseid = $row->courseid ?? '';
$courseidnumber = $row->courseidnumber ?? '';
$fields = [$table, $column, $courseid, $courseidnumber, $row->id];
// Matched data.
$data = $row->$column;

if (!empty($options['regex-match'])) {
Expand All @@ -134,12 +139,12 @@
if (!empty($matches[0])) {
// Show the result foreach match.
foreach ($matches[0] as $match) {
fputcsv($fp, [$table, $column, $id, $match]);
fputcsv($fp, array_merge($fields, [$match]));
}
}
} else {
// Show the result for simple plain text search.
fputcsv($fp, [$table, $column, $id, $data]);
fputcsv($fp, array_merge($fields, [$data]));
}
}
}
Expand Down

0 comments on commit 9a08211

Please sign in to comment.