Skip to content

Commit

Permalink
Issue #93: Add support for common database tables
Browse files Browse the repository at this point in the history
  • Loading branch information
petersistrom committed Dec 4, 2024
1 parent 816cc46 commit 685cb7f
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/question/engine/bank.php');


use core\exception\moodle_exception;
use core_text;
Expand Down Expand Up @@ -175,7 +177,7 @@ public static function build_searching_list(db_search $search, array $tablerowco
foreach ($searchlist as $table => $columns) {
$actualcolumns = self::get_columns($search, $table, $columns);
sort($actualcolumns);
$count += count($actualcolumns) * ($tablerowcounts[$table] ?? 1);
$count += count($actualcolumns) * (($tablerowcounts[$table] ?? 1) ?: 1);
if (!empty($actualcolumns)) {
$actualsearchlist[$table] = $actualcolumns;
}
Expand Down Expand Up @@ -272,6 +274,12 @@ private static function build_search_query(db_search $search, string $table, dat
$wheresql = [];
$params = [];

static $supportedtablemappings = [
'book_chapters' => ['book', 'bookid'],
'forum_posts' => ['forum', 'discussion'],
'lesson_pages' => ['lesson', 'lessonid'],
];

$regex = $search->get('regex');
$prematch = $search->get('prematch');

Expand All @@ -298,6 +306,15 @@ private static function build_search_query(db_search $search, string $table, dat
FROM {".$table."} $tablealias
LEFT JOIN {course} c ON c.id = $tablealias.$coursefield
WHERE $wheresql";
} else if (isset($supportedtablemappings[$table])) {
$sql = "SELECT $tablealias.id,
$tablealias.$columnname,
c.id as courseid,
c.shortname as courseshortname
FROM {".$table."} $tablealias
LEFT JOIN {".$supportedtablemappings[$table][0]."} b ON t.{$supportedtablemappings[$table][1]} = b.id
LEFT JOIN {course} c ON c.id = b.course
WHERE $wheresql";
} else {
$sql = "SELECT id, $columnname FROM {".$table."} $tablealias WHERE $wheresql";
}
Expand Down Expand Up @@ -361,7 +378,17 @@ public static function search_column(db_search $search, string $table, database_
$linkfunction = self::find_link_function($table, $column->name);
foreach ($records as $record) {
if (!empty($linkfunction)) {
$linkstring = $linkfunction($record);
if ($table == 'question') {
$question = \question_bank::load_question($record->id);
$category = $DB->get_record('question_categories', ['id' => $question->category], '*', MUST_EXIST);
$context = \context::instance_by_id($category->contextid);
$course = $DB->get_record('course', array('id' => $context->instanceid));
$record->courseid = $course->id;
$record->courseshortname = $course->shortname;
$linkstring = $linkfunction($record, $course->id);
} else {
$linkstring = $linkfunction($record);
}
}

if (!$regex) {
Expand Down Expand Up @@ -540,12 +567,25 @@ public static function find_link_function($table, $column) {
$url = new \moodle_url('/course/view.php', ['id' => $record->id]);
return $url->out();
},
'course_section' => function($record) {
$url = new \moodle_url('/course/view.php', ['id' => $record->courseid]);
return $url->out();
},
'question' => function($record, $courseid = null) {
$url = new \moodle_url('/question/bank/previewquestion/preview.php',
['id' => $record->id, 'courseid' => $courseid]);
return $url->out(false);
},
];

static $linkmappings = [
'course:fullname' => 'course',
'course:shortname' => 'course',
'course:summary' => 'course',
'course_sections:name' => 'course_section',
'course_sections:summary' => 'course_section',
'question:name' => 'question',
'question:questiontext' => 'question',
];

static $modulefunctions = null;
Expand Down Expand Up @@ -576,9 +616,17 @@ public static function find_link_function($table, $column) {
}
}

static $moduelmappings = [
'book_chapters' => 'book',
'forum_posts' => 'forum',
'lesson_pages' => 'lesson',
];

// Consider links based on the table name being a module.
if (isset($modulefunctions[$table])) {
return $modulefunctions[$table];
} else if (isset($moduelmappings[$table]) && isset($modulefunctions[$moduelmappings[$table]])) {
return $modulefunctions[$moduelmappings[$table]];
}

return null;
Expand Down

0 comments on commit 685cb7f

Please sign in to comment.