diff --git a/classes/helper.php b/classes/helper.php index bc6806b..3856ec4 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -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; @@ -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; } @@ -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'); @@ -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"; } @@ -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) { @@ -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#section-'.$record->id, ['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; @@ -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;