MDL-35603 - Backup - Course import selector notice
[moodle.git] / backup / util / ui / restore_ui_components.php
CommitLineData
3acc9b81
SH
1<?php
2
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.
14//
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/>.
17
18/**
19 * This file contains components used by the restore UI
20 *
21 * @package moodlecore
22 * @copyright 2010 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26/**
27 * A base class that can be used to build a specific search upon
28 */
29abstract class restore_search_base implements renderable {
30
31 /**
32 * The default values for this components params
33 */
3acc9b81
SH
34 const DEFAULT_SEARCH = '';
35
3acc9b81
SH
36 /**
37 * The param used to convey the current search string
38 * @var string
39 */
40 static $VAR_SEARCH = 'search';
33d34b4b 41
3acc9b81
SH
42 /**
43 * The current search string
44 * @var string|null
45 */
46 private $search = null;
3acc9b81
SH
47 /**
48 * The URL for this page including required params to return to it
49 * @var moodle_url
50 */
51 private $url = null;
52 /**
53 * The results of the search
54 * @var array|null
55 */
56 private $results = null;
57 /**
58 * The total number of results available
59 * @var int
60 */
61 private $totalcount = null;
62 /**
63 * Array of capabilities required for each item in the search
64 * @var array
65 */
66 private $requiredcapabilities = array();
c4dbfb2c
RL
67 /**
68 * Max number of courses to return in a search.
69 * @var int
70 */
71 private $maxresults = null;
72 /**
73 * Indicates if we have more than maxresults found.
74 * @var boolean
75 */
76 private $has_more_results = false;
3acc9b81
SH
77
78 /**
79 * Constructor
80 * @param array $config Config options
81 */
82 public function __construct(array $config=array()) {
83
858d100f 84 $this->search = optional_param($this->get_varsearch(), self::DEFAULT_SEARCH, PARAM_NOTAGS);
c4dbfb2c 85 $this->maxresults = get_config('backup', 'import_general_maxresults');
3acc9b81
SH
86
87 foreach ($config as $name=>$value) {
88 $method = 'set_'.$name;
89 if (method_exists($this, $method)) {
90 $this->$method($value);
91 }
92 }
93 }
3acc9b81
SH
94 /**
95 * The URL for this search
96 * @global moodle_page $PAGE
97 * @return moodle_url The URL for this page
98 */
99 final public function get_url() {
100 global $PAGE;
101 $params = array(
3acc9b81
SH
102 $this->get_varsearch() => $this->get_search()
103 );
104 return ($this->url !== null)?new moodle_url($this->url, $params):new moodle_url($PAGE->url, $params);
105 }
106 /**
107 * The current search string
108 * @return string
109 */
110 final public function get_search() {
111 return ($this->search !== null)?$this->search:self::DEFAULT_SEARCH;
112 }
113 /**
114 * The total number of results
115 * @return int
116 */
33d34b4b 117 final public function get_count() {
3acc9b81
SH
118 if ($this->totalcount === null) {
119 $this->search();
120 }
121 return $this->totalcount;
122 }
3acc9b81
SH
123 /**
124 * Returns an array of results from the search
125 * @return array
126 */
127 final public function get_results() {
128 if ($this->results === null) {
129 $this->search();
130 }
131 return $this->results;
132 }
3acc9b81
SH
133 /**
134 * Sets the page URL
135 * @param moodle_url $url
136 */
137 final public function set_url(moodle_url $url) {
138 $this->url = $url;
139 }
140 /**
141 * Invalidates the results collected so far
142 */
143 final public function invalidate_results() {
144 $this->results = null;
165b7bd9 145 $this->totalcount = null;
3acc9b81
SH
146 }
147 /**
148 * Adds a required capability which all results will be checked against
149 * @param string $capability
150 * @param int|null $user
151 */
152 final public function require_capability($capability, $user=null) {
153 if (!is_int($user)) {
154 $user = null;
155 }
156 $this->requiredcapabilities[] = array(
157 'capability' => $capability,
158 'user' => $user
159 );
160 }
161 /**
162 * Executes the search
163 *
164 * @global moodle_database $DB
165 * @return int The number of results
166 */
167 final public function search() {
168 global $DB;
169 if (!is_null($this->results)) {
170 return $this->results;
171 }
3acc9b81 172
33d34b4b
SH
173 $this->results = array();
174 $this->totalcount = 0;
175 $contextlevel = $this->get_itemcontextlevel();
176 list($sql, $params) = $this->get_searchsql();
d0c94a1e
TL
177 $blocksz = 5000;
178 $offs = 0;
cb7fc350
EL
179 // Get total number, to avoid some incorrect iterations
180 $countsql = preg_replace('/ORDER BY.*/', '', $sql);
181 $totalcourses = $DB->count_records_sql("SELECT COUNT(*) FROM ($countsql) sel", $params);
182 // User to be checked is always the same (usually null, get it form first element)
183 $firstcap = reset($this->requiredcapabilities);
184 $userid = isset($firstcap['user']) ? $firstcap['user'] : null;
185 // Extract caps to check, this saves us a bunch of iterations
186 $requiredcaps = array();
187 foreach ($this->requiredcapabilities as $cap) {
188 $requiredcaps[] = $cap['capability'];
189 }
c4dbfb2c
RL
190 // Iterate while we have records and haven't reached $this->maxresults.
191 while ($totalcourses > $offs and $this->totalcount < $this->maxresults) {
cb7fc350 192 $resultset = $DB->get_records_sql($sql, $params, $offs, $blocksz);
d0c94a1e
TL
193 foreach ($resultset as $result) {
194 context_instance_preload($result);
a689cd1d
AG
195 $classname = context_helper::get_class_for_level($contextlevel);
196 $context = $classname::instance($result->id);
cb7fc350
EL
197 if (count($requiredcaps) > 0) {
198 if (!has_all_capabilities($requiredcaps, $context, $userid)) {
199 continue;
3acc9b81
SH
200 }
201 }
c4dbfb2c
RL
202 // Check if we are over the limit.
203 if ($this->totalcount+1 > $this->maxresults) {
204 $this->has_more_results = true;
cb7fc350 205 break;
d0c94a1e 206 }
c4dbfb2c
RL
207 // If not, then continue.
208 $this->totalcount++;
209 $this->results[$result->id] = $result;
3acc9b81 210 }
d0c94a1e 211 $offs += $blocksz;
3acc9b81
SH
212 }
213
33d34b4b
SH
214 return $this->totalcount;
215 }
3acc9b81 216
33d34b4b 217 final public function has_more_results() {
c4dbfb2c
RL
218 if ($this->results === null) {
219 $this->search();
220 }
221 return $this->has_more_results;
3acc9b81 222 }
33d34b4b 223
3acc9b81
SH
224 /**
225 * Returns an array containing the SQL for the search and the params
226 * @return array
227 */
228 abstract protected function get_searchsql();
3acc9b81
SH
229 /**
230 * Gets the context level associated with this components items
231 * @return CONTEXT_*
232 */
233 abstract protected function get_itemcontextlevel();
234 /**
235 * Formats the results
236 */
237 abstract protected function format_results();
3acc9b81
SH
238 /**
239 * Gets the string used to transfer the search string for this compontents requests
240 * @return string
241 */
242 abstract public function get_varsearch();
243}
244
245/**
246 * A course search component
247 */
248class restore_course_search extends restore_search_base {
249
3acc9b81
SH
250 static $VAR_SEARCH = 'search';
251
10618448 252 protected $currentcourseid = null;
165b7bd9 253 protected $includecurrentcourse;
10618448
SH
254
255 /**
256 * @param array $config
257 * @param int $currentcouseid The current course id so it can be ignored
258 */
259 public function __construct(array $config=array(), $currentcouseid = null) {
3acc9b81 260 parent::__construct($config);
96576a2a 261 $this->setup_restrictions();
10618448 262 $this->currentcourseid = $currentcouseid;
165b7bd9 263 $this->includecurrentcourse = false;
01d6182f 264 }
96576a2a
HB
265 /**
266 * Sets up any access restrictions for the courses to be displayed in the search.
01d6182f 267 *
96576a2a
HB
268 * This will typically call $this->require_capability().
269 */
270 protected function setup_restrictions() {
271 $this->require_capability('moodle/restore:restorecourse');
3acc9b81
SH
272 }
273 /**
274 *
275 * @global moodle_database $DB
276 */
277 protected function get_searchsql() {
278 global $DB;
279
280 list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
3acc9b81 281 $params = array(
33d34b4b 282 'fullnamesearch' => '%'.$this->get_search().'%',
10618448
SH
283 'shortnamesearch' => '%'.$this->get_search().'%',
284 'siteid' => SITEID
3acc9b81
SH
285 );
286
287 $select = " SELECT c.id,c.fullname,c.shortname,c.visible,c.sortorder ";
288 $from = " FROM {course} c ";
10618448 289 $where = " WHERE (".$DB->sql_like('c.fullname', ':fullnamesearch', false)." OR ".$DB->sql_like('c.shortname', ':shortnamesearch', false).") AND c.id <> :siteid";
3acc9b81
SH
290 $orderby = " ORDER BY c.sortorder";
291
165b7bd9 292 if ($this->currentcourseid !== null && !$this->includecurrentcourse) {
10618448
SH
293 $where .= " AND c.id <> :currentcourseid";
294 $params['currentcourseid'] = $this->currentcourseid;
295 }
296
3acc9b81
SH
297 return array($select.$ctxselect.$from.$ctxjoin.$where.$orderby, $params);
298 }
3acc9b81
SH
299 protected function get_itemcontextlevel() {
300 return CONTEXT_COURSE;
301 }
302 protected function format_results() {}
3acc9b81
SH
303 public function get_varsearch() {
304 return self::$VAR_SEARCH;
305 }
165b7bd9
EL
306 public function set_include_currentcourse() {
307 $this->includecurrentcourse = true;
308 }
3acc9b81
SH
309}
310
311/**
312 * A category search component
313 */
314class restore_category_search extends restore_search_base {
315
3acc9b81
SH
316 static $VAR_SEARCH = 'catsearch';
317
318 public function __construct(array $config=array()) {
319 parent::__construct($config);
320 $this->require_capability('moodle/course:create');
321 }
322 /**
323 *
324 * @global moodle_database $DB
325 */
326 protected function get_searchsql() {
327 global $DB;
328
329 list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx');
3acc9b81 330 $params = array(
33d34b4b 331 'namesearch' => '%'.$this->get_search().'%',
3acc9b81
SH
332 );
333
334 $select = " SELECT c.id,c.name,c.visible,c.sortorder,c.description,c.descriptionformat ";
335 $from = " FROM {course_categories} c ";
88bb11eb 336 $where = " WHERE ".$DB->sql_like('c.name', ':namesearch', false);
3acc9b81
SH
337 $orderby = " ORDER BY c.sortorder";
338
339 return array($select.$ctxselect.$from.$ctxjoin.$where.$orderby, $params);
340 }
3acc9b81
SH
341 protected function get_itemcontextlevel() {
342 return CONTEXT_COURSECAT;
343 }
344 protected function format_results() {}
3acc9b81
SH
345 public function get_varsearch() {
346 return self::$VAR_SEARCH;
347 }
348}
349