3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This file contains components used by the restore UI
22 * @copyright 2010 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * A base class that can be used to build a specific search upon
29 abstract class restore_search_base
implements renderable
{
32 * The default values for this components params
34 const DEFAULT_SEARCH
= '';
37 * The param used to convey the current search string
40 static $VAR_SEARCH = 'search';
42 static $MAXRESULTS = 10;
44 * The current search string
47 private $search = null;
49 * The URL for this page including required params to return to it
54 * The results of the search
57 private $results = null;
59 * The total number of results available
62 private $totalcount = null;
64 * Array of capabilities required for each item in the search
67 private $requiredcapabilities = array();
71 * @param array $config Config options
73 public function __construct(array $config=array()) {
75 $this->search
= optional_param($this->get_varsearch(), self
::DEFAULT_SEARCH
, PARAM_NOTAGS
);
77 foreach ($config as $name=>$value) {
78 $method = 'set_'.$name;
79 if (method_exists($this, $method)) {
80 $this->$method($value);
85 * The URL for this search
86 * @global moodle_page $PAGE
87 * @return moodle_url The URL for this page
89 final public function get_url() {
92 $this->get_varsearch() => $this->get_search()
94 return ($this->url
!== null)?
new moodle_url($this->url
, $params):new moodle_url($PAGE->url
, $params);
97 * The current search string
100 final public function get_search() {
101 return ($this->search
!== null)?
$this->search
:self
::DEFAULT_SEARCH
;
104 * The total number of results
107 final public function get_count() {
108 if ($this->totalcount
=== null) {
111 return $this->totalcount
;
114 * Returns an array of results from the search
117 final public function get_results() {
118 if ($this->results
=== null) {
121 return $this->results
;
125 * @param moodle_url $url
127 final public function set_url(moodle_url
$url) {
131 * Invalidates the results collected so far
133 final public function invalidate_results() {
134 $this->results
= null;
135 $this->totalcount
= null;
138 * Adds a required capability which all results will be checked against
139 * @param string $capability
140 * @param int|null $user
142 final public function require_capability($capability, $user=null) {
143 if (!is_int($user)) {
146 $this->requiredcapabilities
[] = array(
147 'capability' => $capability,
152 * Executes the search
154 * @global moodle_database $DB
155 * @return int The number of results
157 final public function search() {
159 if (!is_null($this->results
)) {
160 return $this->results
;
163 $this->results
= array();
164 $this->totalcount
= 0;
165 $contextlevel = $this->get_itemcontextlevel();
166 list($sql, $params) = $this->get_searchsql();
167 $resultset = $DB->get_recordset_sql($sql, $params, 0, 250);
168 foreach ($resultset as $result) {
169 context_instance_preload($result);
170 $context = get_context_instance($contextlevel, $result->id
);
171 if (count($this->requiredcapabilities
) > 0) {
172 foreach ($this->requiredcapabilities
as $cap) {
173 if (!has_capability($cap['capability'], $context, $cap['user'])) {
178 $this->results
[$result->id
] = $result;
180 if ($this->totalcount
>= self
::$MAXRESULTS) {
185 return $this->totalcount
;
188 final public function has_more_results() {
189 return $this->get_count() >= self
::$MAXRESULTS;
193 * Returns an array containing the SQL for the search and the params
196 abstract protected function get_searchsql();
198 * Gets the context level associated with this components items
201 abstract protected function get_itemcontextlevel();
203 * Formats the results
205 abstract protected function format_results();
207 * Gets the string used to transfer the search string for this compontents requests
210 abstract public function get_varsearch();
214 * A course search component
216 class restore_course_search
extends restore_search_base
{
218 static $VAR_SEARCH = 'search';
220 protected $currentcourseid = null;
221 protected $includecurrentcourse;
224 * @param array $config
225 * @param int $currentcouseid The current course id so it can be ignored
227 public function __construct(array $config=array(), $currentcouseid = null) {
228 parent
::__construct($config);
229 $this->setup_restrictions();
230 $this->currentcourseid
= $currentcouseid;
231 $this->includecurrentcourse
= false;
234 * Sets up any access restrictions for the courses to be displayed in the search.
236 * This will typically call $this->require_capability().
238 protected function setup_restrictions() {
239 $this->require_capability('moodle/restore:restorecourse');
243 * @global moodle_database $DB
245 protected function get_searchsql() {
248 list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE
, 'ctx');
250 'fullnamesearch' => '%'.$this->get_search().'%',
251 'shortnamesearch' => '%'.$this->get_search().'%',
255 $select = " SELECT c.id,c.fullname,c.shortname,c.visible,c.sortorder ";
256 $from = " FROM {course} c ";
257 $where = " WHERE (".$DB->sql_like('c.fullname', ':fullnamesearch', false)." OR ".$DB->sql_like('c.shortname', ':shortnamesearch', false).") AND c.id <> :siteid";
258 $orderby = " ORDER BY c.sortorder";
260 if ($this->currentcourseid
!== null && !$this->includecurrentcourse
) {
261 $where .= " AND c.id <> :currentcourseid";
262 $params['currentcourseid'] = $this->currentcourseid
;
265 return array($select.$ctxselect.$from.$ctxjoin.$where.$orderby, $params);
267 protected function get_itemcontextlevel() {
268 return CONTEXT_COURSE
;
270 protected function format_results() {}
271 public function get_varsearch() {
272 return self
::$VAR_SEARCH;
274 public function set_include_currentcourse() {
275 $this->includecurrentcourse
= true;
280 * A category search component
282 class restore_category_search
extends restore_search_base
{
284 static $VAR_SEARCH = 'catsearch';
286 public function __construct(array $config=array()) {
287 parent
::__construct($config);
288 $this->require_capability('moodle/course:create');
292 * @global moodle_database $DB
294 protected function get_searchsql() {
297 list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT
, 'ctx');
299 'namesearch' => '%'.$this->get_search().'%',
302 $select = " SELECT c.id,c.name,c.visible,c.sortorder,c.description,c.descriptionformat ";
303 $from = " FROM {course_categories} c ";
304 $where = " WHERE ".$DB->sql_like('c.name', ':namesearch', false);
305 $orderby = " ORDER BY c.sortorder";
307 return array($select.$ctxselect.$from.$ctxjoin.$where.$orderby, $params);
309 protected function get_itemcontextlevel() {
310 return CONTEXT_COURSECAT
;
312 protected function format_results() {}
313 public function get_varsearch() {
314 return self
::$VAR_SEARCH;