public static function search_courses($options) { $params = self::validate_parameters(self::search_courses_parameters(), array('options' => $options)); $searchquery = array(); foreach ($params as $param) { $searchquery[] = $param['query']; } $results = get_courses_search($searchquery); $resultcourses = array(); if ($results) { foreach ($results as $result) { $resultcourses[] = array('id' => $result->id, 'fullname' => $result->fullname, 'shortname' => $result->shortname, 'summary' => $result->summary); } } return $resultcourses; }
/** * Searches courses * * List of found course ids is cached for 10 minutes. Cache may be purged prior * to this when somebody edits courses or categories, however it is very * difficult to keep track of all possible changes that may affect list of courses. * * @param array $search contains search criterias, such as: * - search - search string * - blocklist - id of block (if we are searching for courses containing specific block0 * - modulelist - name of module (if we are searching for courses containing specific module * - tagid - id of tag * @param array $options display options, same as in get_courses() except 'recursive' is ignored - * search is always category-independent * @return course_in_list[] */ public static function search_courses($search, $options = array()) { global $DB; $offset = !empty($options['offset']) ? $options['offset'] : 0; $limit = !empty($options['limit']) ? $options['limit'] : null; $sortfields = !empty($options['sort']) ? $options['sort'] : array('sortorder' => 1); $coursecatcache = cache::make('core', 'coursecat'); $cachekey = 's-' . serialize($search + array('sort' => $sortfields)); $cntcachekey = 'scnt-' . serialize($search); $ids = $coursecatcache->get($cachekey); if ($ids !== false) { // We already cached last search result. $ids = array_slice($ids, $offset, $limit); $courses = array(); if (!empty($ids)) { list($sql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED, 'id'); $records = self::get_course_records("c.id " . $sql, $params, $options); // Preload course contacts if necessary - saves DB queries later to do it for each course separately. if (!empty($options['coursecontacts'])) { self::preload_course_contacts($records); } // If option 'idonly' is specified no further action is needed, just return list of ids. if (!empty($options['idonly'])) { return array_keys($records); } // Prepare the list of course_in_list objects. foreach ($ids as $id) { $courses[$id] = new course_in_list($records[$id]); } } return $courses; } $preloadcoursecontacts = !empty($options['coursecontacts']); unset($options['coursecontacts']); if (!empty($search['search'])) { // Search courses that have specified words in their names/summaries. $searchterms = preg_split('|\\s+|', trim($search['search']), 0, PREG_SPLIT_NO_EMPTY); $searchterms = array_filter($searchterms, create_function('$v', 'return strlen($v) > 1;')); $courselist = get_courses_search($searchterms, 'c.sortorder ASC', 0, 9999999, $totalcount); self::sort_records($courselist, $sortfields); $coursecatcache->set($cachekey, array_keys($courselist)); $coursecatcache->set($cntcachekey, $totalcount); $records = array_slice($courselist, $offset, $limit, true); } else { if (!empty($search['blocklist'])) { // Search courses that have block with specified id. $blockname = $DB->get_field('block', 'name', array('id' => $search['blocklist'])); $where = 'ctx.id in (SELECT distinct bi.parentcontextid FROM {block_instances} bi WHERE bi.blockname = :blockname)'; $params = array('blockname' => $blockname); } else { if (!empty($search['modulelist'])) { // Search courses that have module with specified name. $where = "c.id IN (SELECT DISTINCT module.course " . "FROM {" . $search['modulelist'] . "} module)"; $params = array(); } else { if (!empty($search['tagid'])) { // Search courses that are tagged with the specified tag. $where = "c.id IN (SELECT t.itemid " . "FROM {tag_instance} t WHERE t.tagid = :tagid AND t.itemtype = :itemtype)"; $params = array('tagid' => $search['tagid'], 'itemtype' => 'course'); } else { debugging('No criteria is specified while searching courses', DEBUG_DEVELOPER); return array(); } } } $courselist = self::get_course_records($where, $params, $options, true); self::sort_records($courselist, $sortfields); $coursecatcache->set($cachekey, array_keys($courselist)); $coursecatcache->set($cntcachekey, count($courselist)); $records = array_slice($courselist, $offset, $limit, true); } // Preload course contacts if necessary - saves DB queries later to do it for each course separately. if (!empty($preloadcoursecontacts)) { self::preload_course_contacts($records); } // If option 'idonly' is specified no further action is needed, just return list of ids. if (!empty($options['idonly'])) { return array_keys($records); } // Prepare the list of course_in_list objects. $courses = array(); foreach ($records as $record) { $courses[$record->id] = new course_in_list($record); } return $courses; }
foreach ($cat_courses as $ccourse) { if ($ccourse->id != $course->id && $ccourse->id != SITEID) { $options[$ccourse->id] = format_string($ccourse->fullname); } } $cat = get_record("course_categories", "id", $course->category); if (count($options) > 0) { $mform_post = new course_import_activities_form_1($CFG->wwwroot . '/course/import/activities/index.php', array('options' => $options, 'courseid' => $course->id, 'text' => get_string('coursescategory'))); $mform_post->display(); } if (!empty($creator)) { $mform_post = new course_import_activities_form_2($CFG->wwwroot . '/course/import/activities/index.php', array('courseid' => $course->id)); $mform_post->display(); } if (!empty($fromcoursesearch) && !empty($creator)) { $totalcount = 0; $courses = get_courses_search(explode(" ", $fromcoursesearch), "fullname ASC", $page, 50, $totalcount); if (is_array($courses) and count($courses) > 0) { $table->data[] = array('<b>' . get_string('searchresults') . '</b>', '', ''); foreach ($courses as $scourse) { if ($course->id != $scourse->id) { $table->data[] = array('', format_string($scourse->fullname), '<a href="' . $CFG->wwwroot . '/course/import/activities/index.php?id=' . $course->id . '&fromcourse=' . $scourse->id . '">' . get_string('usethiscourse') . '</a>'); } } } else { $table->data[] = array('', get_string('noresults'), ''); } } if (!empty($table)) { print_table($table); }
if (!empty($courselist)) { $firstcourse = $page * $perpage; $lastcourse = $page * $perpage + $perpage - 1; $i = 0; foreach ($courselist as $course) { if ($i >= $firstcourse && $i <= $lastcourse) { $courses[$course->id] = $course; } $i++; } } $totalcount = count($courselist); } else { if (!empty($searchterm)) { // Donot do search for empty search request. $courses = get_courses_search($searchterms, "fullname ASC", $page, $perpage, $totalcount); } } $searchform = ''; // Turn editing should be visible if user have system or category level capability if (!empty($courses) && (can_edit_in_category() || !empty($usercatlist))) { if ($PAGE->user_is_editing()) { $string = new lang_string("turneditingoff"); $edit = "off"; } else { $string = new lang_string("turneditingon"); $edit = "on"; } $params = array_merge($urlparams, array('sesskey' => sesskey(), 'edit' => $edit)); $aurl = new moodle_url("{$CFG->wwwroot}/course/search.php", $params); $searchform = $OUTPUT->single_button($aurl, $string, 'get');
/** * Searches courses * * List of found course ids is cached for 10 minutes. Cache may be purged prior * to this when somebody edits courses or categories, however it is very * difficult to keep track of all possible changes that may affect list of courses. * * @param array $search contains search criterias, such as: * - search - search string * - blocklist - id of block (if we are searching for courses containing specific block0 * - modulelist - name of module (if we are searching for courses containing specific module * - tagid - id of tag * @param array $options display options, same as in get_courses() except 'recursive' is ignored - * search is always category-independent * @param array $requiredcapabilites List of capabilities required to see return course. * @return course_in_list[] */ public static function search_courses($search, $options = array(), $requiredcapabilities = array()) { global $DB; $offset = !empty($options['offset']) ? $options['offset'] : 0; $limit = !empty($options['limit']) ? $options['limit'] : null; $sortfields = !empty($options['sort']) ? $options['sort'] : array('sortorder' => 1); $coursecatcache = cache::make('core', 'coursecat'); $cachekey = 's-'. serialize( $search + array('sort' => $sortfields) + array('requiredcapabilities' => $requiredcapabilities) ); $cntcachekey = 'scnt-'. serialize($search); $ids = $coursecatcache->get($cachekey); if ($ids !== false) { // We already cached last search result. $ids = array_slice($ids, $offset, $limit); $courses = array(); if (!empty($ids)) { list($sql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED, 'id'); $records = self::get_course_records("c.id ". $sql, $params, $options); // Preload course contacts if necessary - saves DB queries later to do it for each course separately. if (!empty($options['coursecontacts'])) { self::preload_course_contacts($records); } // If option 'idonly' is specified no further action is needed, just return list of ids. if (!empty($options['idonly'])) { return array_keys($records); } // Prepare the list of course_in_list objects. foreach ($ids as $id) { $courses[$id] = new course_in_list($records[$id]); } } return $courses; } $preloadcoursecontacts = !empty($options['coursecontacts']); unset($options['coursecontacts']); // Empty search string will return all results. if (!isset($search['search'])) { $search['search'] = ''; } if (empty($search['blocklist']) && empty($search['modulelist']) && empty($search['tagid'])) { // Search courses that have specified words in their names/summaries. $searchterms = preg_split('|\s+|', trim($search['search']), 0, PREG_SPLIT_NO_EMPTY); $courselist = get_courses_search($searchterms, 'c.sortorder ASC', 0, 9999999, $totalcount, $requiredcapabilities); self::sort_records($courselist, $sortfields); $coursecatcache->set($cachekey, array_keys($courselist)); $coursecatcache->set($cntcachekey, $totalcount); $records = array_slice($courselist, $offset, $limit, true); } else { if (!empty($search['blocklist'])) { // Search courses that have block with specified id. $blockname = $DB->get_field('block', 'name', array('id' => $search['blocklist'])); $where = 'ctx.id in (SELECT distinct bi.parentcontextid FROM {block_instances} bi WHERE bi.blockname = :blockname)'; $params = array('blockname' => $blockname); } else if (!empty($search['modulelist'])) { // Search courses that have module with specified name. $where = "c.id IN (SELECT DISTINCT module.course ". "FROM {".$search['modulelist']."} module)"; $params = array(); } else if (!empty($search['tagid'])) { // Search courses that are tagged with the specified tag. $where = "c.id IN (SELECT t.itemid ". "FROM {tag_instance} t WHERE t.tagid = :tagid AND t.itemtype = :itemtype AND t.component = :component)"; $params = array('tagid' => $search['tagid'], 'itemtype' => 'course', 'component' => 'core'); if (!empty($search['ctx'])) { $rec = isset($search['rec']) ? $search['rec'] : true; $parentcontext = context::instance_by_id($search['ctx']); if ($parentcontext->contextlevel == CONTEXT_SYSTEM && $rec) { // Parent context is system context and recursive is set to yes. // Nothing to filter - all courses fall into this condition. } else if ($rec) { // Filter all courses in the parent context at any level. $where .= ' AND ctx.path LIKE :contextpath'; $params['contextpath'] = $parentcontext->path . '%'; } else if ($parentcontext->contextlevel == CONTEXT_COURSECAT) { // All courses in the given course category. $where .= ' AND c.category = :category'; $params['category'] = $parentcontext->instanceid; } else { // No courses will satisfy the context criterion, do not bother searching. $where = '1=0'; } } } else { debugging('No criteria is specified while searching courses', DEBUG_DEVELOPER); return array(); } $courselist = self::get_course_records($where, $params, $options, true); if (!empty($requiredcapabilities)) { foreach ($courselist as $key => $course) { context_helper::preload_from_record($course); $coursecontext = context_course::instance($course->id); if (!has_all_capabilities($requiredcapabilities, $coursecontext)) { unset($courselist[$key]); } } } self::sort_records($courselist, $sortfields); $coursecatcache->set($cachekey, array_keys($courselist)); $coursecatcache->set($cntcachekey, count($courselist)); $records = array_slice($courselist, $offset, $limit, true); } // Preload course contacts if necessary - saves DB queries later to do it for each course separately. if (!empty($preloadcoursecontacts)) { self::preload_course_contacts($records); } // If option 'idonly' is specified no further action is needed, just return list of ids. if (!empty($options['idonly'])) { return array_keys($records); } // Prepare the list of course_in_list objects. $courses = array(); foreach ($records as $record) { $courses[$record->id] = new course_in_list($record); } return $courses; }
/** * hook for the messagebyroletab. * not correctly namespaced because of limitations in messaging. * * unfortunately we don't seem to be able to pass * parameters from the request here... */ function message_print_byrole() { global $CFG, $USER; require_once $CFG->dirroot . '/local/lib/messagelib.php'; $target = optional_param('target', 0, PARAM_INT); $course = optional_param('lp', 0, PARAM_INT); $page = optional_param('page', 0, PARAM_INT); $perpage = optional_param('perpage', 10, PARAM_INT); $sitecontext = get_context_instance(CONTEXT_COURSE, SITEID); $cansearch = has_capability('moodle/local:cansearchforlptomessage', $sitecontext); $searchform = null; $messageform = null; $courses = array(); $totalcount = 0; if (!empty($course) || !empty($target)) { $c = get_record('course', 'id', $course); $targetobject = (object) tao_message_target_get($target, $c); if ($count = tao_message_count_recipients_by_target($targetobject, $c)) { $targetobject->key = $target; require_capability('moodle/local:' . $targetobject->capability, get_context_instance(CONTEXT_COURSE, $c->id)); // give the message send form require_once $CFG->dirroot . '/local/forms.php'; $messageform = new tao_message_send_form('', array('course' => $c, 'target' => $targetobject)); if ($data = $messageform->get_data()) { // send message $eventdata = array('body' => $data->body, 'from' => $USER->id, 'format' => $data->format, 'course' => $c, 'target' => $targetobject); events_trigger('tao_message_role', $eventdata); echo get_string('messagequeued', 'local'); print_continue($CFG->wwwroot . '/message/index.php?tab=byrole'); return; } else { if (!$messageform->is_cancelled()) { $messageform->display(); return; } } } else { notify(get_string('messagenorecipients', 'local')); } } if ($cansearch) { // set up the search form object and process any search requests require_capability('moodle/local:cansearchforlptomessage', $sitecontext); require_once $CFG->dirroot . '/local/forms.php'; $searchform = new tao_message_lpsearch_form('', array(), 'get'); if ($data = $searchform->get_data()) { $search = trim(strip_tags($data->search)); // trim & clean raw searched string if ($search) { $searchterms = explode(" ", $search); // Search for words independently foreach ($searchterms as $key => $searchterm) { if (strlen($searchterm) < 1) { unset($searchterms[$key]); } } $search = trim(implode(" ", $searchterms)); } if (count($searchterms) > 0) { $courses = get_courses_search($searchterms, "fullname ASC", $page, $perpage, $totalcount); } if (empty($courses)) { $nosearchresults = true; } } } // print the main part of the page $targets = (object) tao_message_targets(); // SITE wide message groups first $sitecontent = ''; foreach ($targets->site as $key => $target) { $target = (object) $target; $target->key = $key; $sitecontent .= tao_print_target($target); } $lpcontent = ''; if (empty($courses)) { // if we haven't come from a search, get all courses they have a direct relationship with if (has_capability('moodle/local:hasdirectlprelationship', $sitecontext)) { // Non-cached - get accessinfo if (isset($USER->access)) { $accessinfo = $USER->access; } else { $accessinfo = get_user_access_sitewide($USER->id); } $courses = get_user_courses_bycap($USER->id, 'moodle/local:hasdirectlprelationship', $accessinfo, false, 'c.fullname', array('fullname')); } } if ($courses) { // either from a search, or from the 'direct' relationships foreach ($courses as $course) { // print the targets for each course $coursecontent = ''; foreach ($targets->lp as $key => $target) { $target = (object) $target; $target->key = $key; $coursecontent .= tao_print_target($target, $course, !empty($search)); } if (!empty($coursecontent)) { $lpcontent .= '<b>' . $course->fullname . '</b><br />' . $coursecontent . '<br />'; } } if (!empty($searchform) && !empty($search)) { $url = $searchform->get_fake_url($CFG->wwwroot . '/message/index.php', array('search' => urlencode(stripslashes($search)), 'perpage' => $perpage)); $lpcontent .= print_paging_bar($totalcount, $page, $perpage, $url, 'page', $perpage == 99999, true); } } if (empty($sitecontent) && empty($lpcontent) && empty($cansearch)) { print_error('nomessagetargets', 'local'); } if (!empty($sitecontent)) { print_heading(get_string('sitelists', 'local')); echo $sitecontent . '<br /><br />'; } if (!empty($lpcontent)) { print_heading(get_string('lplists', 'local')); echo $lpcontent; $lpprinted = true; } if (!empty($searchform)) { if (empty($lpprinted)) { print_heading(get_string('lplists', 'local')); } else { echo '<br /><br />'; } if (!empty($nosearchresults)) { notify(get_string('noresults')); } $searchform->display(); } }
} else { if ($showall and confirm_sesskey()) { $searchtext = ''; $previoussearch = 0; } } } } /// Get all existing students and teachers for this course. if (!($alreadycourses = get_courses_in_metacourse($course->id))) { $alreadycourses = array(); } $numcourses = 0; /// Get search results excluding any users already in this course if ($searchtext != '' and $previoussearch and confirm_sesskey()) { if ($searchcourses = get_courses_search(explode(" ", $searchtext), 'fullname ASC', 0, 99999, $numcourses)) { foreach ($searchcourses as $tmp) { if (array_key_exists($tmp->id, $alreadycourses)) { unset($searchcourses[$tmp->id]); } if (!empty($tmp->metacourse)) { unset($searchcourses[$tmp->id]); } } if (array_key_exists($course->id, $searchcourses)) { unset($searchcourses[$course->id]); } $numcourses = count($searchcourses); } } /// If no search results then get potential students for this course excluding users already in course
$my_courses_flag = 0; $total = 0; $courses['query'] = $query; $course_count = 10; // Default value for course result list if (!empty($_GET['course_count'])) { $course_count = $_GET['course_count']; } if ($_GET['my_courses_flag'] == "true") { $my_courses_flag = $_GET['my_courses_flag']; $courses['results'] = enrol_get_my_courses(array('id', 'shortname'), 'visible DESC,sortorder ASC', $course_count); //Once you have the results, filter the ones matching the search query $mycourses = array(); foreach ($courses['results'] as $objCourse) { if (preg_match('/' . $query . '/i', $objCourse->fullname) != 0) { $mycourses[] = $objCourse; } } $courses['results'] = array_values($mycourses); echo json_encode($courses); } else { $courses['results'] = array_values(get_courses_search(array($query), 'fullname ASC', 0, $course_count, $total)); if (empty($courses['results'])) { $objCourse = new stdClass(); $objCourse->id = 'na'; $courses['results'] = array_values(array($objCourse)); echo json_encode($courses); } else { echo json_encode($courses); } }