$searchterm) {
if (strlen($searchterm) < 2) {
unset($searchterms[$key]);
}
}
$search = trim(implode(" ", $searchterms));
}
$site = get_site();
$urlparams = array();
foreach (array('search', 'page', 'blocklist', 'modulelist', 'edit') as $param) {
if (!empty($$param)) {
$urlparams[$param] = $$param;
}
}
if ($perpage != 10) {
$urlparams['perpage'] = $perpage;
}
$PAGE->set_url('/course/search.php', $urlparams);
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard');
if ($CFG->forcelogin) {
require_login();
}
//Editing is possible if user have system or category level create and manage capability
if (can_edit_in_category() || !empty($usercatlist)) {
if ($edit !== -1) {
$USER->editing = $edit;
}
$adminediting = $PAGE->user_is_editing();
// Set perpage if user can edit in category
if ($perpage != 99999) {
$perpage = 30;
}
} else {
$adminediting = false;
}
/// Editing functions
if (has_capability('moodle/course:visibility', context_system::instance())) {
/// Hide or show a course
if ($hide or $show and confirm_sesskey()) {
if ($hide) {
$course = $DB->get_record("course", array("id"=>$hide));
$visible = 0;
} else {
$course = $DB->get_record("course", array("id"=>$show));
$visible = 1;
}
if ($course) {
$DB->set_field("course", "visible", $visible, array("id"=>$course->id));
}
}
}
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist);
$strcourses = get_string("courses");
$strsearch = get_string("search");
$strsearchresults = get_string("searchresults");
$strcategory = get_string("category");
$strselect = get_string("select");
$strselectall = get_string("selectall");
$strdeselectall = get_string("deselectall");
$stredit = get_string("edit");
$strfrontpage = get_string('frontpage', 'admin');
$strnovalidcourses = get_string('novalidcourses');
if (empty($search) and empty($blocklist) and empty($modulelist) and empty($moveto) and ($edit != -1)) {
$PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
$PAGE->navbar->add($strsearch);
$PAGE->set_title("$site->fullname : $strsearch");
$PAGE->set_heading($site->fullname);
echo $OUTPUT->header();
echo $OUTPUT->box_start();
echo "
";
echo "
";
print_course_search("", false, "plain");
echo "
";
print_string("searchhelp");
echo "
";
echo "";
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
exit;
}
$courses = array();
if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
if (!$destcategory = $DB->get_record("course_categories", array("id" => $moveto))) {
print_error('cannotfindcategory', '', '', $moveto);
}
//User should have manage and create capablity on destination category.
require_capability('moodle/category:manage', context_coursecat::instance($moveto));
require_capability('moodle/course:create', context_coursecat::instance($moveto));
foreach ( $data as $key => $value ) {
if (preg_match('/^c\d+$/', $key)) {
$courseid = substr($key, 1);
// user must have category:manage and course:create capability for the course to be moved.
$coursecontext = context_course::instance($courseid);
foreach ($capabilities as $capability) {
require_capability($capability, $coursecontext);
array_push($courses, $courseid);
}
}
}
move_courses($courses, $moveto);
}
// get list of courses containing blocks if required
if (!empty($blocklist) and confirm_sesskey()) {
$blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
$courses = array();
$courses = $DB->get_records_sql("
SELECT * FROM {course} WHERE id IN (
SELECT DISTINCT ctx.instanceid
FROM {context} ctx
JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
array($blockname));
$totalcount = count($courses);
//Keep only chunk of array which you want to display
if ($totalcount > $perpage) {
$courses = array_chunk($courses, $perpage, true);
$courses = $courses[$page];
}
foreach ($courses as $course) {
$courses[$course->id] = $course;
}
} elseif (!empty($modulelist) and confirm_sesskey()) { // get list of courses containing modules
$modulename = $modulelist;
$sql = "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
." WHERE module.course=c.id";
$courseids = $DB->get_records_sql($sql);
$courses = array();
if (!empty($courseids)) {
$firstcourse = $page*$perpage;
$lastcourse = $page*$perpage + $perpage -1;
$i = 0;
foreach ($courseids as $courseid) {
if ($i>= $firstcourse && $i<=$lastcourse) {
$courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id));
}
$i++;
}
$totalcount = count($courseids);
}
else {
$totalcount = 0;
}
} 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 = get_string("turneditingoff");
$edit = "off";
} else {
$string = get_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');
} else {
$searchform = print_course_search($search, true, "navbar");
}
$PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
$PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
if (!empty($search)) {
$PAGE->navbar->add(s($search));
}
$PAGE->set_title("$site->fullname : $strsearchresults");
$PAGE->set_heading($site->fullname);
$PAGE->set_button($searchform);
echo $OUTPUT->header();
$lastcategory = -1;
if ($courses) {
echo $OUTPUT->heading("$strsearchresults: $totalcount");
$encodedsearch = urlencode($search);
// add the module/block parameter to the paging bar if they exists
$modulelink = "";
if (!empty($modulelist) and confirm_sesskey()) {
$modulelink = "&modulelist=".$modulelist."&sesskey=".sesskey();
} else if (!empty($blocklist) and confirm_sesskey()) {
$modulelink = "&blocklist=".$blocklist."&sesskey=".sesskey();
}
print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
// Show list of courses
if (!$adminediting) { //Not editing mode
foreach ($courses as $course) {
// front page don't belong to any category and block can exist.
if ($course->category > 0) {
$course->summary .= "
";
$course->summary .= "$strcategory: category\">";
$course->summary .= $displaylist[$course->category];
$course->summary .= "
";
}
print_course($course, $search);
echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
}
} else { //editing mode
echo "";
}
print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
} else {
if (!empty($search)) {
echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
}
else {
echo $OUTPUT->heading( $strnovalidcourses );
}
}
echo "
";
print_course_search($search);
echo $OUTPUT->footer();
/**
* Print a list navigation bar
* Display page numbers, and a link for displaying all entries
* @param integer $totalcount - number of entry to display
* @param integer $page - page number
* @param integer $perpage - number of entry per page
* @param string $encodedsearch
* @param string $modulelink - module name
*/
function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) {
global $OUTPUT;
echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
//display
if ($perpage != 99999 && $totalcount > $perpage) {
echo "";
echo "".get_string("showall", "", $totalcount)."";
echo "
";
} else if ($perpage === 99999) {
$defaultperpage = 10;
//If user has course:create or category:manage capability the show 30 records.
$capabilities = array('moodle/course:create', 'moodle/category:manage');
if (has_any_capability($capabilities, context_system::instance())) {
$defaultperpage = 30;
}
echo "";
echo "".get_string("showperpage", "", $defaultperpage)."";
echo "
";
}
}