Merge branch 'MDL-47323-enrol-plugin-backup' of https://github.com/mpetrowi/moodle
authorDan Poltawski <dan@moodle.com>
Tue, 9 Dec 2014 22:29:37 +0000 (22:29 +0000)
committerDan Poltawski <dan@moodle.com>
Tue, 9 Dec 2014 22:29:37 +0000 (22:29 +0000)
1  2 
backup/moodle2/restore_stepslib.php

@@@ -1884,12 -1884,12 +1884,12 @@@ class restore_enrolments_structure_ste
  
      protected function define_structure() {
  
-         $paths = array();
-         $paths[] = new restore_path_element('enrol', '/enrolments/enrols/enrol');
-         $paths[] = new restore_path_element('enrolment', '/enrolments/enrols/enrol/user_enrolments/enrolment');
+         $enrol = new restore_path_element('enrol', '/enrolments/enrols/enrol');
+         $enrolment = new restore_path_element('enrolment', '/enrolments/enrols/enrol/user_enrolments/enrolment');
+         // Attach local plugin stucture to enrol element.
+         $this->add_plugin_structure('enrol', $enrol);
  
-         return $paths;
+         return array($enrol, $enrolment);
      }
  
      /**
@@@ -3187,52 -3187,11 +3187,52 @@@ class restore_block_instance_structure_
          }
  
          if (!$bi->instance_allow_multiple()) {
 -            if ($DB->record_exists_sql("SELECT bi.id
 -                                          FROM {block_instances} bi
 -                                          JOIN {block} b ON b.name = bi.blockname
 -                                         WHERE bi.parentcontextid = ?
 -                                           AND bi.blockname = ?", array($data->parentcontextid, $data->blockname))) {
 +            // The block cannot be added twice, so we will check if the same block is already being
 +            // displayed on the same page. For this, rather than mocking a page and using the block_manager
 +            // we use a similar query to the one in block_manager::load_blocks(), this will give us
 +            // a very good idea of the blocks already displayed in the context.
 +            $params =  array(
 +                'blockname' => $data->blockname
 +            );
 +
 +            // Context matching test.
 +            $context = context::instance_by_id($data->parentcontextid);
 +            $contextsql = 'bi.parentcontextid = :contextid';
 +            $params['contextid'] = $context->id;
 +
 +            $parentcontextids = $context->get_parent_context_ids();
 +            if ($parentcontextids) {
 +                list($parentcontextsql, $parentcontextparams) =
 +                        $DB->get_in_or_equal($parentcontextids, SQL_PARAMS_NAMED);
 +                $contextsql = "($contextsql OR (bi.showinsubcontexts = 1 AND bi.parentcontextid $parentcontextsql))";
 +                $params = array_merge($params, $parentcontextparams);
 +            }
 +
 +            // Page type pattern test.
 +            $pagetypepatterns = matching_page_type_patterns_from_pattern($data->pagetypepattern);
 +            list($pagetypepatternsql, $pagetypepatternparams) =
 +                $DB->get_in_or_equal($pagetypepatterns, SQL_PARAMS_NAMED);
 +            $params = array_merge($params, $pagetypepatternparams);
 +
 +            // Sub page pattern test.
 +            $subpagepatternsql = 'bi.subpagepattern IS NULL';
 +            if ($data->subpagepattern !== null) {
 +                $subpagepatternsql = "($subpagepatternsql OR bi.subpagepattern = :subpagepattern)";
 +                $params['subpagepattern'] = $data->subpagepattern;
 +            }
 +
 +            $exists = $DB->record_exists_sql("SELECT bi.id
 +                                                FROM {block_instances} bi
 +                                                JOIN {block} b ON b.name = bi.blockname
 +                                               WHERE bi.blockname = :blockname
 +                                                 AND $contextsql
 +                                                 AND bi.pagetypepattern $pagetypepatternsql
 +                                                 AND $subpagepatternsql", $params);
 +            if ($exists) {
 +                // There is at least one very similar block visible on the page where we
 +                // are trying to restore the block. In these circumstances the block API
 +                // would not allow the user to add another instance of the block, so we
 +                // apply the same rule here.
                  return false;
              }
          }