MDL-29072 Import course: Use correct capability for course selection
[moodle.git] / backup / util / ui / restore_ui_components.php
blob85c61c8ab5a25538f29a67b3ea2594c25d4667c1
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * This file contains components used by the restore UI
21 * @package moodlecore
22 * @copyright 2010 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 /**
27 * A base class that can be used to build a specific search upon
29 abstract class restore_search_base implements renderable {
31 /**
32 * The default values for this components params
34 const DEFAULT_SEARCH = '';
36 /**
37 * The param used to convey the current search string
38 * @var string
40 static $VAR_SEARCH = 'search';
42 static $MAXRESULTS = 10;
43 /**
44 * The current search string
45 * @var string|null
47 private $search = null;
48 /**
49 * The URL for this page including required params to return to it
50 * @var moodle_url
52 private $url = null;
53 /**
54 * The results of the search
55 * @var array|null
57 private $results = null;
58 /**
59 * The total number of results available
60 * @var int
62 private $totalcount = null;
63 /**
64 * Array of capabilities required for each item in the search
65 * @var array
67 private $requiredcapabilities = array();
69 /**
70 * Constructor
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);
84 /**
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() {
90 global $PAGE;
91 $params = array(
92 $this->get_varsearch() => $this->get_search()
94 return ($this->url !== null)?new moodle_url($this->url, $params):new moodle_url($PAGE->url, $params);
96 /**
97 * The current search string
98 * @return string
100 final public function get_search() {
101 return ($this->search !== null)?$this->search:self::DEFAULT_SEARCH;
104 * The total number of results
105 * @return int
107 final public function get_count() {
108 if ($this->totalcount === null) {
109 $this->search();
111 return $this->totalcount;
114 * Returns an array of results from the search
115 * @return array
117 final public function get_results() {
118 if ($this->results === null) {
119 $this->search();
121 return $this->results;
124 * Sets the page URL
125 * @param moodle_url $url
127 final public function set_url(moodle_url $url) {
128 $this->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)) {
144 $user = null;
146 $this->requiredcapabilities[] = array(
147 'capability' => $capability,
148 'user' => $user
152 * Executes the search
154 * @global moodle_database $DB
155 * @return int The number of results
157 final public function search() {
158 global $DB;
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'])) {
174 continue 2;
178 $this->results[$result->id] = $result;
179 $this->totalcount++;
180 if ($this->totalcount >= self::$MAXRESULTS) {
181 break;
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
194 * @return array
196 abstract protected function get_searchsql();
198 * Gets the context level associated with this components items
199 * @return CONTEXT_*
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
208 * @return string
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() {
246 global $DB;
248 list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
249 $params = array(
250 'fullnamesearch' => '%'.$this->get_search().'%',
251 'shortnamesearch' => '%'.$this->get_search().'%',
252 'siteid' => SITEID
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() {
295 global $DB;
297 list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx');
298 $params = array(
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;