From 33d34b4b77ecc3eb0afd84a2aad09ef9b8716b7e Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Thu, 11 Nov 2010 01:21:16 +0000 Subject: [PATCH] restore MDL-24757 Fixed up course and category selectors --- backup/util/ui/renderer.php | 133 ++++++++-------- backup/util/ui/restore_ui_components.php | 186 ++++------------------- lang/en/backup.php | 11 +- 3 files changed, 102 insertions(+), 228 deletions(-) diff --git a/backup/util/ui/renderer.php b/backup/util/ui/renderer.php index da2acaf7476..26e6550de5d 100644 --- a/backup/util/ui/renderer.php +++ b/backup/util/ui/renderer.php @@ -178,23 +178,25 @@ class core_backup_renderer extends plugin_renderer_base { $form .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value)); } - $html = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore')); + $hasrestoreoption = false; - if (!empty($categories) && ($categories->get_resultscount() > 0 || $categories->get_search() == '')) { + $html = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore')); + if (!empty($categories) && ($categories->get_count() > 0 || $categories->get_search())) { // New course + $hasrestoreoption = true; $html .= $form; $html .= html_writer::start_tag('div', array('class'=>'bcs-new-course backup-section')); $html .= $this->output->heading(get_string('restoretonewcourse', 'backup'), 2, array('class'=>'header')); $html .= $this->backup_detail_input(get_string('restoretonewcourse', 'backup'), 'radio', 'target', backup::TARGET_NEW_COURSE, array('checked'=>'checked')); - //$html .= $this->backup_detail_select(get_string('coursecategory', 'backup'), 'targetid', $categories); $html .= $this->backup_detail_pair(get_string('selectacategory', 'backup'), $this->render($categories)); $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue')))); $html .= html_writer::end_tag('div'); $html .= html_writer::end_tag('form'); } - // Current course if (!empty($currentcourse)) { + // Current course + $hasrestoreoption = true; $html .= $form; $html .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'targetid', 'value'=>$currentcourse)); $html .= html_writer::start_tag('div', array('class'=>'bcs-current-course backup-section')); @@ -206,8 +208,9 @@ class core_backup_renderer extends plugin_renderer_base { $html .= html_writer::end_tag('form'); } - if (!empty($courses) && ($courses->get_resultscount() > 0 || $courses->get_search() == '')) { + if (!empty($courses) && ($courses->get_count() > 0 || $courses->get_search())) { // Existing course + $hasrestoreoption = true; $html .= $form; $html .= html_writer::start_tag('div', array('class'=>'bcs-existing-course backup-section')); $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class'=>'header')); @@ -219,6 +222,10 @@ class core_backup_renderer extends plugin_renderer_base { $html .= html_writer::end_tag('form'); } + if (!$hasrestoreoption) { + echo $this->output->notification(get_string('norestoreoptions','backup')); + } + $html .= html_writer::end_tag('div'); return $html; } @@ -451,40 +458,42 @@ class core_backup_renderer extends plugin_renderer_base { $url = $component->get_url(); $output = html_writer::start_tag('div', array('class' => 'restore-course-search')); - if ($component->get_totalcount() === 0) { - $output .= $this->output->notification(get_string('nomatchingcourses', 'backup')); - $output .= html_writer::end_tag('div'); - return $output; - } - - $output .= html_writer::tag('div', get_string('totalcoursesearchresults', 'backup', $component->get_totalcount()), array('class'=>'rcs-totalresults')); - $output .= html_writer::start_tag('div', array('class' => 'rcs-results')); - if ($component->get_totalpages()>1) { - $pagingbar = new paging_bar($component->get_totalcount(), $component->get_page(), $component->get_pagelimit(), new moodle_url($url, array('searchcourses'=>1)), restore_course_search::$VAR_PAGE); - $output .= $this->output->render($pagingbar); - } $table = new html_table(); $table->head = array('', get_string('shortname'), get_string('fullname')); $table->data = array(); - foreach ($component->get_results() as $course) { - $row = new html_table_row(); - $row->attributes['class'] = 'rcs-course'; - if (!$course->visible) { - $row->attributes['class'] .= ' dimmed'; + if ($component->get_count() !== 0) { + foreach ($component->get_results() as $course) { + $row = new html_table_row(); + $row->attributes['class'] = 'rcs-course'; + if (!$course->visible) { + $row->attributes['class'] .= ' dimmed'; + } + $row->cells = array( + html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$course->id)), + $course->shortname, + $course->fullname + ); + $table->data[] = $row; } - $row->cells = array( - html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$course->id)), - $course->shortname, - $course->fullname - ); + if ($component->has_more_results()) { + $cell = new html_table_cell(get_string('moreresults', 'backup')); + $cell->colspan = 3; + $cell->attributes['class'] = 'notifyproblem'; + $row = new html_table_row(array($cell)); + $row->attributes['class'] = 'rcs-course'; + $table->data[] = $row; + } + } else { + $cell = new html_table_cell(get_string('nomatchingcourses', 'backup')); + $cell->colspan = 3; + $cell->attributes['class'] = 'notifyproblem'; + $row = new html_table_row(array($cell)); + $row->attributes['class'] = 'rcs-course'; $table->data[] = $row; } $output .= html_writer::table($table); - if (isset($pagingbar)) { - $output .= $this->output->render($pagingbar); - } $output .= html_writer::end_tag('div'); $output .= html_writer::start_tag('div', array('class'=>'rcs-search')); @@ -506,19 +515,15 @@ class core_backup_renderer extends plugin_renderer_base { $url = $component->get_url(); $output = html_writer::start_tag('div', array('class' => 'import-course-search')); - if ($component->get_totalcount() === 0) { + if ($component->get_count() === 0) { $output .= $this->output->notification(get_string('nomatchingcourses', 'backup')); $output .= html_writer::end_tag('div'); return $output; } - $output .= html_writer::tag('div', get_string('totalcoursesearchresults', 'backup', $component->get_totalcount()), array('class'=>'ics-totalresults')); + $output .= html_writer::tag('div', get_string('totalcoursesearchresults', 'backup', $component->get_count()), array('class'=>'ics-totalresults')); $output .= html_writer::start_tag('div', array('class' => 'ics-results')); - if ($component->get_totalpages()>1) { - $pagingbar = new paging_bar($component->get_totalcount(), $component->get_page(), $component->get_pagelimit(), new moodle_url($url, array('searchcourses'=>1)), restore_course_search::$VAR_PAGE); - $output .= $this->output->render($pagingbar); - } $table = new html_table(); $table->head = array('', get_string('shortname'), get_string('fullname')); @@ -537,9 +542,6 @@ class core_backup_renderer extends plugin_renderer_base { $table->data[] = $row; } $output .= html_writer::table($table); - if (isset($pagingbar)) { - $output .= $this->output->render($pagingbar); - } $output .= html_writer::end_tag('div'); $output .= html_writer::start_tag('div', array('class'=>'ics-search')); @@ -561,40 +563,43 @@ class core_backup_renderer extends plugin_renderer_base { $url = $component->get_url(); $output = html_writer::start_tag('div', array('class' => 'restore-course-search')); - if ($component->get_totalcount() === 0) { - $output .= $this->output->notification(get_string('nomatchingcourses', 'backup')); - $output .= html_writer::end_tag('div'); - return $output; - } - - $output .= html_writer::tag('div', get_string('totalcategorysearchresults', 'backup', $component->get_totalcount()), array('class'=>'rcs-totalresults')); - $output .= html_writer::start_tag('div', array('class' => 'rcs-results')); - if ($component->get_totalpages()>1) { - $pagingbar = new paging_bar($component->get_totalcount(), $component->get_page(), $component->get_pagelimit(), new moodle_url($url, array('searchcourses'=>1)), restore_category_search::$VAR_PAGE); - $output .= $this->output->render($pagingbar); - } $table = new html_table(); $table->head = array('', get_string('name'), get_string('description')); $table->data = array(); - foreach ($component->get_results() as $category) { - $row = new html_table_row(); - $row->attributes['class'] = 'rcs-course'; - if (!$category->visible) { - $row->attributes['class'] .= ' dimmed'; + + if ($component->get_count() !== 0) { + foreach ($component->get_results() as $category) { + $row = new html_table_row(); + $row->attributes['class'] = 'rcs-course'; + if (!$category->visible) { + $row->attributes['class'] .= ' dimmed'; + } + $row->cells = array( + html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$category->id)), + $category->name, + format_text($category->description, $category->descriptionformat, array('overflowdiv'=>true)) + ); + $table->data[] = $row; } - $row->cells = array( - html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$category->id)), - $category->name, - format_text($category->description, $category->descriptionformat, array('overflowdiv'=>true)) - ); + if ($component->has_more_results()) { + $cell = new html_table_cell(get_string('moreresults', 'backup')); + $cell->attributes['class'] = 'notifyproblem'; + $cell->colspan = 3; + $row = new html_table_row(array($cell)); + $row->attributes['class'] = 'rcs-course'; + $table->data[] = $row; + } + } else { + $cell = new html_table_cell(get_string('nomatchingcourses', 'backup')); + $cell->colspan = 3; + $cell->attributes['class'] = 'notifyproblem'; + $row = new html_table_row(array($cell)); + $row->attributes['class'] = 'rcs-course'; $table->data[] = $row; } $output .= html_writer::table($table); - if (isset($pagingbar)) { - $output .= $this->output->render($pagingbar); - } $output .= html_writer::end_tag('div'); $output .= html_writer::start_tag('div', array('class'=>'rcs-search')); diff --git a/backup/util/ui/restore_ui_components.php b/backup/util/ui/restore_ui_components.php index 604b56d4749..5243c1d881b 100644 --- a/backup/util/ui/restore_ui_components.php +++ b/backup/util/ui/restore_ui_components.php @@ -31,40 +31,20 @@ abstract class restore_search_base implements renderable { /** * The default values for this components params */ - const DEFAULT_PAGE = 0; - const DEFAULT_PAGELIMIT = 5; const DEFAULT_SEARCH = ''; - /** - * The param used to convey the current page - * @var string - */ - static $VAR_PAGE = 'page'; - /** - * The param used to convey the current page limit - * @var string - */ - static $VAR_PAGELIMIT = 'pagelimit'; /** * 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 current page - * @var int - */ - private $page = null; - /** - * The current page limit - * @var int - */ - private $pagelimit = null; /** * The URL for this page including required params to return to it * @var moodle_url @@ -92,8 +72,6 @@ abstract class restore_search_base implements renderable { */ public function __construct(array $config=array()) { - $this->page = optional_param($this->get_varpage(), self::DEFAULT_PAGE, PARAM_INT); - $this->pagelimit = optional_param($this->get_varpagelimit(), self::DEFAULT_PAGELIMIT, PARAM_INT); $this->search = optional_param($this->get_varsearch(), self::DEFAULT_SEARCH, PARAM_ALPHANUMEXT); foreach ($config as $name=>$value) { @@ -103,20 +81,6 @@ abstract class restore_search_base implements renderable { } } } - /** - * The current page - * @return int - */ - final public function get_page() { - return ($this->page !== null)?$this->page:self::DEFAULT_PAGE; - } - /** - * The current page limit - * @return int - */ - final public function get_pagelimit() { - return ($this->pagelimit !== null)?$this->pagelimit:self::DEFAULT_PAGELIMIT; - } /** * The URL for this search * @global moodle_page $PAGE @@ -125,8 +89,6 @@ abstract class restore_search_base implements renderable { final public function get_url() { global $PAGE; $params = array( - $this->get_varpage() => $this->get_page(), - $this->get_varpagelimit() => $this->get_pagelimit(), $this->get_varsearch() => $this->get_search() ); return ($this->url !== null)?new moodle_url($this->url, $params):new moodle_url($PAGE->url, $params); @@ -142,22 +104,12 @@ abstract class restore_search_base implements renderable { * The total number of results * @return int */ - final public function get_totalcount() { + final public function get_count() { if ($this->totalcount === null) { $this->search(); } return $this->totalcount; } - /** - * The number of results in this result set - * @return int - */ - final public function get_resultscount() { - if ($this->results === null) { - $this->search(); - } - return count($this->results); - } /** * Returns an array of results from the search * @return array @@ -168,32 +120,6 @@ abstract class restore_search_base implements renderable { } return $this->results; } - /** - * Sets the current page - * @param int $page - */ - final public function set_page($page) { - $this->page = abs((int)$page); - $this->invalidate_results(); - } - /** - * Sets the page limit for this component - * @param int $pagelimit - */ - final public function set_pagelimit($pagelimit) { - $this->pagelimit = abs((int)$pagelimit); - if ($this->pagelimit < 5 || $this->pagelimit > 500) { - $this->pagelimit = null; - } - $this->invalidate_results(); - } - /** - * Returns the total number of pages - * @return int - */ - final public function get_totalpages() { - return ceil($this->get_totalcount()/$this->pagelimit); - } /** * Sets the page URL * @param moodle_url $url @@ -232,42 +158,41 @@ abstract class restore_search_base implements renderable { if (!is_null($this->results)) { return $this->results; } - $params = array( - 2=>$this->get_page()*$this->get_pagelimit(), - 3=>$this->get_pagelimit() - ); - $this->results = call_user_func_array(array($DB, 'get_records_sql'), $this->get_searchsql()+$params); - $this->totalcount = call_user_func_array(array($DB, 'count_records_sql'), $this->get_countsql()+$params); - if (count($this->requiredcapabilities) > 0) { - $contextlevel = $this->get_itemcontextlevel(); - foreach ($this->results as $key=>$result) { - context_instance_preload($result); - $context = get_context_instance($contextlevel, $result->id); + $this->results = array(); + $this->totalcount = 0; + $contextlevel = $this->get_itemcontextlevel(); + list($sql, $params) = $this->get_searchsql(); + $resultset = $DB->get_recordset_sql($sql, $params, 0, 250); + 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'])) { - unset($this->results[$key]); - break 1; + continue 2; } } } + $this->results[$result->id] = $result; + $this->totalcount++; + if ($this->totalcount >= self::$MAXRESULTS) { + break; + } } - $this->format_results(); - - return $this->get_resultscount(); + 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(); - /** - * Returns an array containing the SQL to get the total number of search results - * as well as an array of params for it - * @return array - */ - abstract protected function get_countsql(); /** * Gets the context level associated with this components items * @return CONTEXT_* @@ -277,16 +202,6 @@ abstract class restore_search_base implements renderable { * Formats the results */ abstract protected function format_results(); - /** - * Gets the string used to transfer the page for this compontents requests - * @return string - */ - abstract public function get_varpage(); - /** - * Gets the string used to transfer the page limit for this compontents requests - * @return string - */ - abstract public function get_varpagelimit(); /** * Gets the string used to transfer the search string for this compontents requests * @return string @@ -299,8 +214,6 @@ abstract class restore_search_base implements renderable { */ class restore_course_search extends restore_search_base { - static $VAR_PAGE = 'page'; - static $VAR_PAGELIMIT = 'pagelimit'; static $VAR_SEARCH = 'search'; protected $currentcourseid = null; @@ -323,7 +236,7 @@ class restore_course_search extends restore_search_base { list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); $params = array( - 'fullnamesearch' => $this->get_search().'%', + 'fullnamesearch' => '%'.$this->get_search().'%', 'shortnamesearch' => '%'.$this->get_search().'%', 'siteid' => SITEID ); @@ -340,36 +253,10 @@ class restore_course_search extends restore_search_base { return array($select.$ctxselect.$from.$ctxjoin.$where.$orderby, $params); } - protected function get_countsql() { - global $DB; - - $params = array( - 'fullnamesearch' => $this->get_search().'%', - 'shortnamesearch' => '%'.$this->get_search().'%', - 'siteid' => SITEID - ); - - $select = " SELECT COUNT(c.id) "; - $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"; - - if ($this->currentcourseid !== null) { - $where .= " AND c.id <> :currentcourseid"; - $params['currentcourseid'] = $this->currentcourseid; - } - - return array($select.$from.$where, $params); - } protected function get_itemcontextlevel() { return CONTEXT_COURSE; } protected function format_results() {} - public function get_varpage() { - return self::$VAR_PAGE; - } - public function get_varpagelimit() { - return self::$VAR_PAGELIMIT; - } public function get_varsearch() { return self::$VAR_SEARCH; } @@ -380,8 +267,6 @@ class restore_course_search extends restore_search_base { */ class restore_category_search extends restore_search_base { - static $VAR_PAGE = 'catpage'; - static $VAR_PAGELIMIT = 'catpagelimit'; static $VAR_SEARCH = 'catsearch'; public function __construct(array $config=array()) { @@ -397,7 +282,7 @@ class restore_category_search extends restore_search_base { list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx'); $params = array( - 'namesearch' => $this->get_search().'%', + 'namesearch' => '%'.$this->get_search().'%', ); $select = " SELECT c.id,c.name,c.visible,c.sortorder,c.description,c.descriptionformat "; @@ -407,29 +292,10 @@ class restore_category_search extends restore_search_base { return array($select.$ctxselect.$from.$ctxjoin.$where.$orderby, $params); } - protected function get_countsql() { - global $DB; - - $params = array( - 'namesearch' => $this->get_search().'%', - ); - - $select = " SELECT COUNT(c.id) "; - $from = " FROM {course_categories} c "; - $where = " WHERE ".$DB->sql_like('c.name', ':namesearch', false); - - return array($select.$from.$where, $params); - } protected function get_itemcontextlevel() { return CONTEXT_COURSECAT; } protected function format_results() {} - public function get_varpage() { - return self::$VAR_PAGE; - } - public function get_varpagelimit() { - return self::$VAR_PAGELIMIT; - } public function get_varsearch() { return self::$VAR_SEARCH; } diff --git a/lang/en/backup.php b/lang/en/backup.php index 17075f65526..4351861e774 100644 --- a/lang/en/backup.php +++ b/lang/en/backup.php @@ -27,6 +27,10 @@ $string['automatedbackupschedule'] = 'Schedule'; $string['automatedbackupschedulehelp'] = 'Choose which days of the week to perform automated backups.'; $string['automatedbackupsinactive'] = 'Automated backups haven\'t been enabled by the site admin'; $string['automatedbackupstatus'] = 'Automated backup status'; +$string['automatedsetup'] = 'Automated backup setup'; +$string['automatedsettings'] = 'Automated backup settings'; +$string['automatedstorage'] = 'Automated backup storage'; +$string['automatedstoragehelp'] = 'Choose the location where you want backups to be stored when they are automatically created.'; $string['backupactivity'] = 'Backup activity: {$a}'; $string['backupcourse'] = 'Backup course: {$a}'; $string['backupcoursedetails'] = 'Course details'; @@ -86,6 +90,7 @@ $string['currentstage4'] = 'Confirmation and review'; $string['currentstage8'] = 'Perform backup'; $string['currentstage16'] = 'Complete'; $string['dependenciesenforced'] = 'Your settings have been altered due to unmet dependencies'; +$string['enterasearch'] = 'Enter a search'; $string['errorfilenamerequired'] = 'You must enter a valid filename for this backup'; $string['errorfilenamemustbezip'] = 'The filename you enter must be a ZIP file and have the .mbz extension'; $string['errorminbackup20version'] = 'This backup file has been created with one development version of Moodle backup ({$a->backup}). Minimum required is {$a->min}. Cannot be restored.'; @@ -139,7 +144,9 @@ $string['lockedbyconfig'] = 'This setting has been locked by the default backup $string['lockedbyhierarchy'] = 'Locked by dependencies'; $string['managefiles'] = 'Manage backup files'; $string['moodleversion'] = 'Moodle version'; +$string['moreresults'] = 'There are to many results, enter a more specific search.'; $string['nomatchingcourses'] = 'There are no courses to display'; +$string['norestoreoptions'] = 'There are no categories or existing courses you can restore to.'; $string['originalwwwroot'] = 'URL of backup'; $string['previousstage'] = 'Previous'; $string['qcategory2coursefallback'] = 'The questions category "{$a->name}", originally at system/course category context in backup file, will be created at course context by restore'; @@ -193,10 +200,6 @@ $string['rootsettingcomments'] = 'Include comments'; $string['rootsettinguserscompletion'] = 'Include user completion details'; $string['rootsettinglogs'] = 'Include course logs'; $string['rootsettinggradehistories'] = 'Include grade history'; -$string['automatedsetup'] = 'Automated backup setup'; -$string['automatedsettings'] = 'Automated backup settings'; -$string['automatedstorage'] = 'Automated backup storage'; -$string['automatedstoragehelp'] = 'Choose the location where you want backups to be stored when they are automatically created.'; $string['storagecourseonly'] = 'Course backup filearea'; $string['storagecourseandexternal'] = 'Course backup filearea and the specified directory'; $string['storageexternalonly'] = 'Specified directory for automated backups';