Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/ui/restore_ui_components.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00029 abstract class restore_search_base implements renderable {
00030 
00034     const DEFAULT_SEARCH = '';
00035 
00040     static $VAR_SEARCH = 'search';
00041 
00042     static $MAXRESULTS = 10;
00047     private $search = null;
00052     private $url = null;
00057     private $results = null;
00062     private $totalcount = null;
00067     private $requiredcapabilities = array();
00068 
00073     public function __construct(array $config=array()) {
00074 
00075         $this->search = optional_param($this->get_varsearch(), self::DEFAULT_SEARCH, PARAM_NOTAGS);
00076 
00077         foreach ($config as $name=>$value) {
00078             $method = 'set_'.$name;
00079             if (method_exists($this, $method)) {
00080                 $this->$method($value);
00081             }
00082         }
00083     }
00089     final public function get_url() {
00090         global $PAGE;
00091         $params = array(
00092             $this->get_varsearch()    => $this->get_search()
00093         );
00094         return ($this->url !== null)?new moodle_url($this->url, $params):new moodle_url($PAGE->url, $params);
00095     }
00100     final public function get_search() {
00101         return ($this->search !== null)?$this->search:self::DEFAULT_SEARCH;
00102     }
00107     final public function get_count() {
00108         if ($this->totalcount === null) {
00109             $this->search();
00110         }
00111         return $this->totalcount;
00112     }
00117     final public function get_results() {
00118         if ($this->results === null) {
00119             $this->search();
00120         }
00121         return $this->results;
00122     }
00127     final public function set_url(moodle_url $url) {
00128         $this->url = $url;
00129     }
00133     final public function invalidate_results() {
00134         $this->results = null;
00135         $this->totalcount = null;
00136     }
00142     final public function require_capability($capability, $user=null) {
00143         if (!is_int($user)) {
00144             $user = null;
00145         }
00146         $this->requiredcapabilities[] = array(
00147             'capability' => $capability,
00148             'user' => $user
00149         );
00150     }
00157     final public function search() {
00158         global $DB;
00159         if (!is_null($this->results)) {
00160             return $this->results;
00161         }
00162 
00163         $this->results = array();
00164         $this->totalcount = 0;
00165         $contextlevel = $this->get_itemcontextlevel();
00166         list($sql, $params) = $this->get_searchsql();
00167         $resultset = $DB->get_recordset_sql($sql, $params, 0, 250);
00168         foreach ($resultset as $result) {
00169             context_instance_preload($result);
00170             $context = get_context_instance($contextlevel, $result->id);
00171             if (count($this->requiredcapabilities) > 0) {
00172                 foreach ($this->requiredcapabilities as $cap) {
00173                     if (!has_capability($cap['capability'], $context, $cap['user'])) {
00174                         continue 2;
00175                     }
00176                 }
00177             }
00178             $this->results[$result->id] = $result;
00179             $this->totalcount++;
00180             if ($this->totalcount >= self::$MAXRESULTS) {
00181                 break;
00182             }
00183         }
00184 
00185         return $this->totalcount;
00186     }
00187 
00188     final public function has_more_results() {
00189         return $this->get_count() >= self::$MAXRESULTS;
00190     }
00191 
00196     abstract protected function get_searchsql();
00201     abstract protected function get_itemcontextlevel();
00205     abstract protected function format_results();
00210     abstract public function get_varsearch();
00211 }
00212 
00216 class restore_course_search extends restore_search_base {
00217 
00218     static $VAR_SEARCH = 'search';
00219 
00220     protected $currentcourseid = null;
00221     protected $includecurrentcourse;
00222 
00227     public function __construct(array $config=array(), $currentcouseid = null) {
00228         parent::__construct($config);
00229         $this->setup_restrictions();
00230         $this->currentcourseid = $currentcouseid;
00231         $this->includecurrentcourse = false;
00232     }
00238     protected function setup_restrictions() {
00239         $this->require_capability('moodle/restore:restorecourse');
00240     }
00245     protected function get_searchsql() {
00246         global $DB;
00247 
00248         list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
00249         $params = array(
00250             'fullnamesearch' => '%'.$this->get_search().'%',
00251             'shortnamesearch' => '%'.$this->get_search().'%',
00252             'siteid' => SITEID
00253         );
00254 
00255         $select     = " SELECT c.id,c.fullname,c.shortname,c.visible,c.sortorder ";
00256         $from       = " FROM {course} c ";
00257         $where      = " WHERE (".$DB->sql_like('c.fullname', ':fullnamesearch', false)." OR ".$DB->sql_like('c.shortname', ':shortnamesearch', false).") AND c.id <> :siteid";
00258         $orderby    = " ORDER BY c.sortorder";
00259 
00260         if ($this->currentcourseid !== null && !$this->includecurrentcourse) {
00261             $where .= " AND c.id <> :currentcourseid";
00262             $params['currentcourseid'] = $this->currentcourseid;
00263         }
00264 
00265         return array($select.$ctxselect.$from.$ctxjoin.$where.$orderby, $params);
00266     }
00267     protected function get_itemcontextlevel() {
00268         return CONTEXT_COURSE;
00269     }
00270     protected function format_results() {}
00271     public function get_varsearch() {
00272         return self::$VAR_SEARCH;
00273     }
00274     public function set_include_currentcourse() {
00275         $this->includecurrentcourse = true;
00276     }
00277 }
00278 
00282 class restore_category_search extends restore_search_base  {
00283 
00284     static $VAR_SEARCH = 'catsearch';
00285 
00286     public function __construct(array $config=array()) {
00287         parent::__construct($config);
00288         $this->require_capability('moodle/course:create');
00289     }
00294     protected function get_searchsql() {
00295         global $DB;
00296 
00297         list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx');
00298         $params = array(
00299             'namesearch' => '%'.$this->get_search().'%',
00300         );
00301 
00302         $select     = " SELECT c.id,c.name,c.visible,c.sortorder,c.description,c.descriptionformat ";
00303         $from       = " FROM {course_categories} c ";
00304         $where      = " WHERE ".$DB->sql_like('c.name', ':namesearch', false);
00305         $orderby    = " ORDER BY c.sortorder";
00306 
00307         return array($select.$ctxselect.$from.$ctxjoin.$where.$orderby, $params);
00308     }
00309     protected function get_itemcontextlevel() {
00310         return CONTEXT_COURSECAT;
00311     }
00312     protected function format_results() {}
00313     public function get_varsearch() {
00314         return self::$VAR_SEARCH;
00315     }
00316 }
00317 
 All Data Structures Namespaces Files Functions Variables Enumerations