Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/course/search.php
Go to the documentation of this file.
00001 <?php
00002 
00004 
00005     require_once("../config.php");
00006     require_once("lib.php");
00007 
00008     $search    = optional_param('search', '', PARAM_RAW);  // search words
00009     $page      = optional_param('page', 0, PARAM_INT);     // which page to show
00010     $perpage   = optional_param('perpage', 10, PARAM_INT); // how many per page
00011     $moveto    = optional_param('moveto', 0, PARAM_INT);   // move to category
00012     $edit      = optional_param('edit', -1, PARAM_BOOL);
00013     $hide      = optional_param('hide', 0, PARAM_INT);
00014     $show      = optional_param('show', 0, PARAM_INT);
00015     $blocklist = optional_param('blocklist', 0, PARAM_INT);
00016     $modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
00017 
00018     $PAGE->set_url('/course/search.php', compact('search', 'page', 'perpage', 'blocklist', 'modulelist', 'edit'));
00019     $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
00020     $search = trim(strip_tags($search)); // trim & clean raw searched string
00021 
00022     if ($search) {
00023         $searchterms = explode(" ", $search);    // Search for words independently
00024         foreach ($searchterms as $key => $searchterm) {
00025             if (strlen($searchterm) < 2) {
00026                 unset($searchterms[$key]);
00027             }
00028         }
00029         $search = trim(implode(" ", $searchterms));
00030     }
00031 
00032     $site = get_site();
00033 
00034     $urlparams = array();
00035     foreach (array('search', 'page', 'blocklist', 'modulelist') as $param) {
00036         if (!empty($$param)) {
00037             $urlparams[$param] = $$param;
00038         }
00039     }
00040     if ($perpage != 10) {
00041         $urlparams['perpage'] = $perpage;
00042     }
00043     $PAGE->set_url('/course/search.php', $urlparams);
00044     $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
00045     $PAGE->set_pagelayout('standard');
00046 
00047     if ($CFG->forcelogin) {
00048         require_login();
00049     }
00050 
00051     if (can_edit_in_category()) {
00052         if ($edit !== -1) {
00053             $USER->editing = $edit;
00054         }
00055         $adminediting = $PAGE->user_is_editing();
00056     } else {
00057         $adminediting = false;
00058     }
00059 
00061     if (has_capability('moodle/course:visibility', get_context_instance(CONTEXT_SYSTEM))) {
00063         if ($hide or $show and confirm_sesskey()) {
00064             if ($hide) {
00065                 $course = $DB->get_record("course", array("id"=>$hide));
00066                 $visible = 0;
00067             } else {
00068                 $course = $DB->get_record("course", array("id"=>$show));
00069                 $visible = 1;
00070             }
00071             if ($course) {
00072                 $DB->set_field("course", "visible", $visible, array("id"=>$course->id));
00073             }
00074         }
00075     }
00076 
00077     $capabilities = array('moodle/course:create', 'moodle/category:manage');
00078     if (has_any_capability($capabilities, get_context_instance(CONTEXT_SYSTEM)) && ($perpage != 99999)) {
00079         $perpage = 30;
00080     }
00081 
00082     $displaylist = array();
00083     $parentlist = array();
00084     make_categories_list($displaylist, $parentlist);
00085 
00086     $strcourses = get_string("courses");
00087     $strsearch = get_string("search");
00088     $strsearchresults = get_string("searchresults");
00089     $strcategory = get_string("category");
00090     $strselect   = get_string("select");
00091     $strselectall = get_string("selectall");
00092     $strdeselectall = get_string("deselectall");
00093     $stredit = get_string("edit");
00094     $strfrontpage = get_string('frontpage', 'admin');
00095     $strnovalidcourses = get_string('novalidcourses');
00096 
00097     if (empty($search) and empty($blocklist) and empty($modulelist)) {
00098         $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
00099         $PAGE->navbar->add($strsearch);
00100         $PAGE->set_title("$site->fullname : $strsearch");
00101         $PAGE->set_heading($site->fullname);
00102 
00103         echo $OUTPUT->header();
00104         echo $OUTPUT->box_start();
00105         echo "<center>";
00106         echo "<br />";
00107         print_course_search("", false, "plain");
00108         echo "<br /><p>";
00109         print_string("searchhelp");
00110         echo "</p>";
00111         echo "</center>";
00112         echo $OUTPUT->box_end();
00113         echo $OUTPUT->footer();
00114         exit;
00115     }
00116 
00117     if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) {   // Some courses are being moved
00118         if (! $destcategory = $DB->get_record("course_categories", array("id"=>$data->moveto))) {
00119             print_error('cannotfindcategory', '', '', $data->moveto);
00120         }
00121 
00122         $courses = array();
00123         foreach ( $data as $key => $value ) {
00124             if (preg_match('/^c\d+$/', $key)) {
00125                 array_push($courses, substr($key, 1));
00126             }
00127         }
00128         move_courses($courses, $data->moveto);
00129     }
00130 
00131     // get list of courses containing blocks if required
00132     if (!empty($blocklist) and confirm_sesskey()) {
00133         $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
00134         $courses = array();
00135         $courses = $DB->get_records_sql("
00136                 SELECT * FROM {course} WHERE id IN (
00137                     SELECT DISTINCT ctx.instanceid
00138                     FROM {context} ctx
00139                     JOIN {block_instances} bi ON bi.parentcontextid = ctx.id
00140                     WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
00141                 array($blockname));
00142         $totalcount = count($courses);
00143         //Keep only chunk of array which you want to display
00144         if ($totalcount > $perpage) {
00145             $courses = array_chunk($courses, $perpage, true);
00146             $courses = $courses[$page];
00147         }
00148         foreach ($courses as $course) {
00149             $courses[$course->id] = $course;
00150         }
00151     }
00152     // get list of courses containing modules if required
00153     elseif (!empty($modulelist) and confirm_sesskey()) {
00154         $modulename = $modulelist;
00155         $sql =  "SELECT DISTINCT c.id FROM {".$modulelist."} module, {course} c"
00156             ." WHERE module.course=c.id";
00157 
00158         $courseids = $DB->get_records_sql($sql);
00159         $courses = array();
00160         if (!empty($courseids)) {
00161             $firstcourse = $page*$perpage;
00162             $lastcourse = $page*$perpage + $perpage -1;
00163             $i = 0;
00164             foreach ($courseids as $courseid) {
00165                 if ($i>= $firstcourse && $i<=$lastcourse) {
00166                     $courses[$courseid->id] = $DB->get_record('course', array('id'=> $courseid->id));
00167                 }
00168                 $i++;
00169             }
00170             $totalcount = count($courseids);
00171         }
00172         else {
00173             $totalcount = 0;
00174         }
00175     }
00176     else {
00177         $courses = get_courses_search($searchterms, "fullname ASC",
00178             $page, $perpage, $totalcount);
00179     }
00180 
00181     $searchform = print_course_search($search, true, "navbar");
00182 
00183     if (!empty($courses) && has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
00184         $searchform = '';
00185         // not sure if this capability is the best  here
00186         if (has_capability('moodle/category:manage', get_context_instance(CONTEXT_SYSTEM))) {
00187             if ($PAGE->user_is_editing()) {
00188                 $string = get_string("turneditingoff");
00189                 $edit = "off";
00190             } else {
00191                 $string = get_string("turneditingon");
00192                 $edit = "on";
00193             }
00194 
00195             $aurl = new moodle_url("$CFG->wwwroot/course/search.php", array(
00196                     'edit' => $edit,
00197                     'sesskey' => sesskey(),
00198                     'search' => $search,
00199                     'page' => $page,
00200                     'perpage' => $perpage));
00201             $searchform = $OUTPUT->single_button($aurl, $string, 'get');
00202         }
00203     }
00204 
00205     $PAGE->navbar->add($strcourses, new moodle_url('/course/index.php'));
00206     $PAGE->navbar->add($strsearch, new moodle_url('/course/search.php'));
00207     if (!empty($search)) {
00208         $PAGE->navbar->add(s($search));
00209     }
00210     $PAGE->set_title("$site->fullname : $strsearchresults");
00211     $PAGE->set_heading($site->fullname);
00212     $PAGE->set_button($searchform);
00213 
00214     echo $OUTPUT->header();
00215 
00216     $lastcategory = -1;
00217     if ($courses) {
00218         echo $OUTPUT->heading("$strsearchresults: $totalcount");
00219         $encodedsearch = urlencode($search);
00220 
00221         // add the module/block parameter to the paging bar if they exists
00222         $modulelink = "";
00223         if (!empty($modulelist) and confirm_sesskey()) {
00224             $modulelink = "&amp;modulelist=".$modulelist."&amp;sesskey=".sesskey();
00225         } else if (!empty($blocklist) and confirm_sesskey()) {
00226             $modulelink = "&amp;blocklist=".$blocklist."&amp;sesskey=".sesskey();
00227         }
00228 
00229         print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $modulelink);
00230 
00231         if (!$adminediting) {
00232             foreach ($courses as $course) {
00233 
00234                 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00235 
00236                 $course->summary .= "<br /><p class=\"category\">";
00237                 $course->summary .= "$strcategory: <a href=\"category.php?id=$course->category\">";
00238                 $course->summary .= $displaylist[$course->category];
00239                 $course->summary .= "</a></p>";
00240                 print_course($course, $search);
00241                 echo $OUTPUT->spacer(array('height'=>5, 'width'=>5, 'br'=>true)); // should be done with CSS instead
00242             }
00243         } else {
00245             echo "<form id=\"movecourses\" action=\"search.php\" method=\"post\">\n";
00246             echo "<div><input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n";
00247             echo "<input type=\"hidden\" name=\"search\" value=\"".s($search)."\" />\n";
00248             echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />\n";
00249             echo "<input type=\"hidden\" name=\"perpage\" value=\"$perpage\" /></div>\n";
00250             echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" class=\"generalbox boxaligncenter\">\n<tr>\n";
00251             echo "<th scope=\"col\">$strcourses</th>\n";
00252             echo "<th scope=\"col\">$strcategory</th>\n";
00253             echo "<th scope=\"col\">$strselect</th>\n";
00254             echo "<th scope=\"col\">$stredit</th></tr>\n";
00255 
00256             foreach ($courses as $course) {
00257 
00258                 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00259 
00260                 $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
00261 
00262                 // are we displaying the front page (courseid=1)?
00263                 if ($course->id == 1) {
00264                     echo "<tr>\n";
00265                     echo "<td><a href=\"$CFG->wwwroot\">$strfrontpage</a></td>\n";
00266 
00267                     // can't do anything else with the front page
00268                     echo "  <td>&nbsp;</td>\n"; // category place
00269                     echo "  <td>&nbsp;</td>\n"; // select place
00270                     echo "  <td>&nbsp;</td>\n"; // edit place
00271                     echo "</tr>\n";
00272                     continue;
00273                 }
00274 
00275                 echo "<tr>\n";
00276                 echo "<td><a $linkcss href=\"view.php?id=$course->id\">"
00277                     . highlight($search, format_string($course->fullname)) . "</a></td>\n";
00278                 echo "<td>".$displaylist[$course->category]."</td>\n";
00279                 echo "<td>\n";
00280 
00281                 // this is ok since this will get inherited from course category context
00282                 // if it is set
00283                 if (has_capability('moodle/category:manage', $coursecontext)) {
00284                     echo "<input type=\"checkbox\" name=\"c$course->id\" />\n";
00285                 } else {
00286                     echo "<input type=\"checkbox\" name=\"c$course->id\" disabled=\"disabled\" />\n";
00287                 }
00288 
00289                 echo "</td>\n";
00290                 echo "<td>\n";
00291 
00292                 // checks whether user can update course settings
00293                 if (has_capability('moodle/course:update', $coursecontext)) {
00294                     echo "<a title=\"".get_string("settings")."\" href=\"$CFG->wwwroot/course/edit.php?id=$course->id\">\n<img".
00295                         " src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" alt=\"".get_string("settings")."\" /></a>\n ";
00296                 }
00297 
00298                 // checks whether user can do role assignment
00299                 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
00300                     echo'<a title="'.get_string('enrolledusers', 'enrol').'" href="'.$CFG->wwwroot.'/enrol/users.php?id='.$course->id.'">';
00301                     echo '<img src="'.$OUTPUT->pix_url('i/users') . '" class="iconsmall" alt="'.get_string('enrolledusers', 'enrol').'" /></a> ' . "\n";
00302                 }
00303 
00304                 // checks whether user can delete course
00305                 if (has_capability('moodle/course:delete', $coursecontext)) {
00306                     echo "<a title=\"".get_string("delete")."\" href=\"delete.php?id=$course->id\">\n<img".
00307                         " src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"".get_string("delete")."\" /></a>\n ";
00308                 }
00309 
00310                 // checks whether user can change visibility
00311                 if (has_capability('moodle/course:visibility', $coursecontext)) {
00312                     if (!empty($course->visible)) {
00313                         echo "<a title=\"".get_string("hide")."\" href=\"search.php?search=$encodedsearch&amp;perpage=$perpage&amp;page=$page&amp;hide=$course->id&amp;sesskey=".sesskey()."\">\n<img".
00314                             " src=\"" . $OUTPUT->pix_url('t/hide') . "\" class=\"iconsmall\" alt=\"".get_string("hide")."\" /></a>\n ";
00315                     } else {
00316                         echo "<a title=\"".get_string("show")."\" href=\"search.php?search=$encodedsearch&amp;perpage=$perpage&amp;page=$page&amp;show=$course->id&amp;sesskey=".sesskey()."\">\n<img".
00317                             " src=\"" . $OUTPUT->pix_url('t/show') . "\" class=\"iconsmall\" alt=\"".get_string("show")."\" /></a>\n ";
00318                     }
00319                 }
00320 
00321                 // checks whether user can do site backup
00322                 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
00323                     echo "<a title=\"".get_string("backup")."\" href=\"../backup/backup.php?id=$course->id\">\n<img".
00324                         " src=\"" . $OUTPUT->pix_url('t/backup') . "\" class=\"iconsmall\" alt=\"".get_string("backup")."\" /></a>\n ";
00325                 }
00326 
00327                 // checks whether user can do restore
00328                 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
00329                     echo "<a title=\"".get_string("restore")."\" href=\"../files/index.php?id=$course->id&amp;wdir=/backupdata\">\n<img".
00330                         " src=\"" . $OUTPUT->pix_url('t/restore') . "\" class=\"iconsmall\" alt=\"".get_string("restore")."\" /></a>\n ";
00331                 }
00332 
00333                 echo "</td>\n</tr>\n";
00334             }
00335             echo "<tr>\n<td colspan=\"4\" style=\"text-align:center\">\n";
00336             echo "<br />";
00337             echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
00338             echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\n";
00339             echo html_writer::select($displaylist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid'));
00340             $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
00341             echo "</td>\n</tr>\n";
00342             echo "</table>\n</form>";
00343 
00344         }
00345 
00346         print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink);
00347 
00348     } else {
00349         if (!empty($search)) {
00350             echo $OUTPUT->heading(get_string("nocoursesfound",'', s($search)));
00351         }
00352         else {
00353             echo $OUTPUT->heading( $strnovalidcourses );
00354         }
00355     }
00356 
00357     echo "<br /><br />";
00358 
00359     print_course_search($search);
00360 
00361     echo $OUTPUT->footer();
00362 
00372     function print_navigation_bar($totalcount,$page,$perpage,$encodedsearch,$modulelink) {
00373         global $OUTPUT;
00374         echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "search.php?search=$encodedsearch".$modulelink."&perpage=$perpage");
00375 
00376         //display
00377         if ($perpage != 99999 && $totalcount > $perpage) {
00378             echo "<center><p>";
00379             echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
00380             echo "</p></center>";
00381         } else if ($perpage === 99999) {
00382             $defaultperpage = 10;
00383             //If user has course:create or category:manage capability the show 30 records.
00384             $capabilities = array('moodle/course:create', 'moodle/category:manage');
00385             if (has_any_capability($capabilities, get_context_instance(CONTEXT_SYSTEM))) {
00386                 $defaultperpage = 30;
00387             }
00388 
00389             echo "<center><p>";
00390             echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
00391             echo "</p></center>";
00392         }
00393     }
00394 
00395 
 All Data Structures Namespaces Files Functions Variables Enumerations