Skip to content

Commit

Permalink
Merge branch 'MDL-26037' of git://github.com/stronk7/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Mar 5, 2012
2 parents 37b6e7a + cb7fc35 commit 9394d86
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
6 changes: 6 additions & 0 deletions backup/util/ui/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,12 @@ public function render_import_course_search(import_course_search $component) {
$output = html_writer::start_tag('div', array('class' => 'import-course-search'));
if ($component->get_count() === 0) {
$output .= $this->output->notification(get_string('nomatchingcourses', 'backup'));

$output .= html_writer::start_tag('div', array('class'=>'ics-search'));
$output .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>restore_course_search::$VAR_SEARCH, 'value'=>$component->get_search()));
$output .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'searchcourses', 'value'=>get_string('search')));
$output .= html_writer::end_tag('div');

$output .= html_writer::end_tag('div');
return $output;
}
Expand Down
41 changes: 28 additions & 13 deletions backup/util/ui/restore_ui_components.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,37 @@ final public function search() {
$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'])) {
continue 2;
$blocksz = 5000;
$offs = 0;
// Get total number, to avoid some incorrect iterations
$countsql = preg_replace('/ORDER BY.*/', '', $sql);
$totalcourses = $DB->count_records_sql("SELECT COUNT(*) FROM ($countsql) sel", $params);
// User to be checked is always the same (usually null, get it form first element)
$firstcap = reset($this->requiredcapabilities);
$userid = isset($firstcap['user']) ? $firstcap['user'] : null;
// Extract caps to check, this saves us a bunch of iterations
$requiredcaps = array();
foreach ($this->requiredcapabilities as $cap) {
$requiredcaps[] = $cap['capability'];
}
// Iterate while we have records and haven't reached MAXRESULTS
while ($totalcourses > $offs and $this->totalcount < self::$MAXRESULTS) {
$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($requiredcaps) > 0) {
if (!has_all_capabilities($requiredcaps, $context, $userid)) {
continue;
}
}
$this->results[$result->id] = $result;
$this->totalcount++;
if ($this->totalcount >= self::$MAXRESULTS) {
break;
}
}
$this->results[$result->id] = $result;
$this->totalcount++;
if ($this->totalcount >= self::$MAXRESULTS) {
break;
}
$offs += $blocksz;
}

return $this->totalcount;
Expand Down

0 comments on commit 9394d86

Please sign in to comment.