Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d0c94a1e84
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 322 lines (293 sloc) 9.656 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* This file contains components used by the restore UI
*
* @package moodlecore
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* A base class that can be used to build a specific search upon
*/
abstract class restore_search_base implements renderable {

    /**
* The default values for this components params
*/
    const DEFAULT_SEARCH = '';

    /**
* The param used to convey the current search string
* @var string
*/
    static $VAR_SEARCH = 'search';

    static $MAXRESULTS = 10;
    /**
* The current search string
* @var string|null
*/
    private $search = null;
    /**
* The URL for this page including required params to return to it
* @var moodle_url
*/
    private $url = null;
    /**
* The results of the search
* @var array|null
*/
    private $results = null;
    /**
* The total number of results available
* @var int
*/
    private $totalcount = null;
    /**
* Array of capabilities required for each item in the search
* @var array
*/
    private $requiredcapabilities = array();

    /**
* Constructor
* @param array $config Config options
*/
    public function __construct(array $config=array()) {

        $this->search = optional_param($this->get_varsearch(), self::DEFAULT_SEARCH, PARAM_NOTAGS);

        foreach ($config as $name=>$value) {
            $method = 'set_'.$name;
            if (method_exists($this, $method)) {
                $this->$method($value);
            }
        }
    }
    /**
* The URL for this search
* @global moodle_page $PAGE
* @return moodle_url The URL for this page
*/
    final public function get_url() {
        global $PAGE;
        $params = array(
            $this->get_varsearch() => $this->get_search()
        );
        return ($this->url !== null)?new moodle_url($this->url, $params):new moodle_url($PAGE->url, $params);
    }
    /**
* The current search string
* @return string
*/
    final public function get_search() {
        return ($this->search !== null)?$this->search:self::DEFAULT_SEARCH;
    }
    /**
* The total number of results
* @return int
*/
    final public function get_count() {
        if ($this->totalcount === null) {
            $this->search();
        }
        return $this->totalcount;
    }
    /**
* Returns an array of results from the search
* @return array
*/
    final public function get_results() {
        if ($this->results === null) {
            $this->search();
        }
        return $this->results;
    }
    /**
* Sets the page URL
* @param moodle_url $url
*/
    final public function set_url(moodle_url $url) {
        $this->url = $url;
    }
    /**
* Invalidates the results collected so far
*/
    final public function invalidate_results() {
        $this->results = null;
        $this->totalcount = null;
    }
    /**
* Adds a required capability which all results will be checked against
* @param string $capability
* @param int|null $user
*/
    final public function require_capability($capability, $user=null) {
        if (!is_int($user)) {
            $user = null;
        }
        $this->requiredcapabilities[] = array(
            'capability' => $capability,
            'user' => $user
        );
    }
    /**
* Executes the search
*
* @global moodle_database $DB
* @return int The number of results
*/
    final public function search() {
        global $DB;
        if (!is_null($this->results)) {
            return $this->results;
        }

        $this->results = array();
        $this->totalcount = 0;
        $contextlevel = $this->get_itemcontextlevel();
        list($sql, $params) = $this->get_searchsql();
        $blocksz = 5000;
        $offs = 0;
        while ($resultset = $DB->get_records_sql($sql, $params, $offs, $blocksz)){
            foreach ($resultset as $result) {
                context_instance_preload($result);
                $context = get_context_instance($contextlevel, $result->id);
                if (count($this->requiredcapabilities) > 0) {
                    foreach ($this->requiredcapabilities as $cap) {
                        if (!has_capability($cap['capability'], $context, $cap['user'])) {
                            continue 2;
                        }
                    }
                }
                $this->results[$result->id] = $result;
                $this->totalcount++;
                if ($this->totalcount >= self::$MAXRESULTS) {
                    break 2;
                }
            }
            $offs += $blocksz;
        }

        return $this->totalcount;
    }

    final public function has_more_results() {
        return $this->get_count() >= self::$MAXRESULTS;
    }

    /**
* Returns an array containing the SQL for the search and the params
* @return array
*/
    abstract protected function get_searchsql();
    /**
* Gets the context level associated with this components items
* @return CONTEXT_*
*/
    abstract protected function get_itemcontextlevel();
    /**
* Formats the results
*/
    abstract protected function format_results();
    /**
* Gets the string used to transfer the search string for this compontents requests
* @return string
*/
    abstract public function get_varsearch();
}

/**
* A course search component
*/
class restore_course_search extends restore_search_base {

    static $VAR_SEARCH = 'search';

    protected $currentcourseid = null;
    protected $includecurrentcourse;

    /**
* @param array $config
* @param int $currentcouseid The current course id so it can be ignored
*/
    public function __construct(array $config=array(), $currentcouseid = null) {
        parent::__construct($config);
        $this->setup_restrictions();
        $this->currentcourseid = $currentcouseid;
        $this->includecurrentcourse = false;
    }
    /**
* Sets up any access restrictions for the courses to be displayed in the search.
*
* This will typically call $this->require_capability().
*/
    protected function setup_restrictions() {
        $this->require_capability('moodle/restore:restorecourse');
    }
    /**
*
* @global moodle_database $DB
*/
    protected function get_searchsql() {
        global $DB;

        list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
        $params = array(
            'fullnamesearch' => '%'.$this->get_search().'%',
            'shortnamesearch' => '%'.$this->get_search().'%',
            'siteid' => SITEID
        );

        $select = " SELECT c.id,c.fullname,c.shortname,c.visible,c.sortorder ";
        $from = " FROM {course} c ";
        $where = " WHERE (".$DB->sql_like('c.fullname', ':fullnamesearch', false)." OR ".$DB->sql_like('c.shortname', ':shortnamesearch', false).") AND c.id <> :siteid";
        $orderby = " ORDER BY c.sortorder";

        if ($this->currentcourseid !== null && !$this->includecurrentcourse) {
            $where .= " AND c.id <> :currentcourseid";
            $params['currentcourseid'] = $this->currentcourseid;
        }

        return array($select.$ctxselect.$from.$ctxjoin.$where.$orderby, $params);
    }
    protected function get_itemcontextlevel() {
        return CONTEXT_COURSE;
    }
    protected function format_results() {}
    public function get_varsearch() {
        return self::$VAR_SEARCH;
    }
    public function set_include_currentcourse() {
        $this->includecurrentcourse = true;
    }
}

/**
* A category search component
*/
class restore_category_search extends restore_search_base {

    static $VAR_SEARCH = 'catsearch';

    public function __construct(array $config=array()) {
        parent::__construct($config);
        $this->require_capability('moodle/course:create');
    }
    /**
*
* @global moodle_database $DB
*/
    protected function get_searchsql() {
        global $DB;

        list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx');
        $params = array(
            'namesearch' => '%'.$this->get_search().'%',
        );

        $select = " SELECT c.id,c.name,c.visible,c.sortorder,c.description,c.descriptionformat ";
        $from = " FROM {course_categories} c ";
        $where = " WHERE ".$DB->sql_like('c.name', ':namesearch', false);
        $orderby = " ORDER BY c.sortorder";

        return array($select.$ctxselect.$from.$ctxjoin.$where.$orderby, $params);
    }
    protected function get_itemcontextlevel() {
        return CONTEXT_COURSECAT;
    }
    protected function format_results() {}
    public function get_varsearch() {
        return self::$VAR_SEARCH;
    }
}

Something went wrong with that request. Please try again.