Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/dbops/backup_structure_dbops.class.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 
00033 abstract class backup_structure_dbops extends backup_dbops {
00034 
00035     public static function get_iterator($element, $params, $processor) {
00036         global $DB;
00037         // Check we are going to get_iterator for one backup_nested_element
00038         if (! $element instanceof backup_nested_element) {
00039             throw new base_element_struct_exception('backup_nested_element_expected');
00040         }
00041         // If var_array, table and sql are null, and element has no final elements it is one nested element without source
00042         // Just return one 1 element iterator without information
00043         if ($element->get_source_array() === null && $element->get_source_table() === null &&
00044             $element->get_source_sql() === null && count($element->get_final_elements()) == 0) {
00045             return new backup_array_iterator(array(0 => null));
00046 
00047         } else if ($element->get_source_array() !== null) { // It's one array, return array_iterator
00048             return new backup_array_iterator($element->get_source_array());
00049 
00050         } else if ($element->get_source_table() !== null) { // It's one table, return recordset iterator
00051             return $DB->get_recordset($element->get_source_table(), self::convert_params_to_values($params, $processor));
00052 
00053         } else if ($element->get_source_sql() !== null) { // It's one sql, return recordset iterator
00054             return $DB->get_recordset_sql($element->get_source_sql(), self::convert_params_to_values($params, $processor));
00055 
00056         } else { // No sources, supress completely, using null iterator
00057             return new backup_null_iterator();
00058         }
00059     }
00060 
00061     protected static function convert_params_to_values($params, $processor) {
00062         $newparams = array();
00063         foreach ($params as $key => $param) {
00064             $newvalue = null;
00065             // If we have a base element, get its current value, exception if not set
00066             if ($param instanceof base_atom) {
00067                 if ($param->is_set()) {
00068                     $newvalue = $param->get_value();
00069                 } else {
00070                     throw new base_element_struct_exception('valueofparamelementnotset', $param->get_name());
00071                 }
00072 
00073             } else if ($param < 0) { // Possibly one processor variable, let's process it
00074                 // See @backup class for all the VAR_XXX variables available.
00075                 // Note1: backup::VAR_PARENTID is handled by nested elements themselves
00076                 // Note2: trying to use one non-existing var will throw exception
00077                 $newvalue = $processor->get_var($param);
00078 
00079             // Else we have one raw param value, use it
00080             } else {
00081                 $newvalue = $param;
00082             }
00083 
00084             $newparams[$key] = $newvalue;
00085         }
00086         return $newparams;
00087     }
00088 
00089     public static function insert_backup_ids_record($backupid, $itemname, $itemid) {
00090         global $DB;
00091         // We need to do some magic with scales (that are stored in negative way)
00092         if ($itemname == 'scale') {
00093             $itemid = -($itemid);
00094         }
00095         // Now, we skip any annotation with negatives/zero/nulls, ids table only stores true id (always > 0)
00096         if ($itemid <= 0 || is_null($itemid)) {
00097             return;
00098         }
00099         // TODO: Analyze if some static (and limited) cache by the 3 params could save us a bunch of record_exists() calls
00100         // Note: Sure it will!
00101         if (!$DB->record_exists('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname, 'itemid' => $itemid))) {
00102             $DB->insert_record('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname, 'itemid' => $itemid));
00103         }
00104     }
00105 
00106     public static function annotate_files($backupid, $contextid, $component, $filearea, $itemid) {
00107         global $DB;
00108         $sql = 'SELECT id
00109                   FROM {files}
00110                  WHERE contextid = ?
00111                    AND component = ?';
00112         $params = array($contextid, $component);
00113 
00114         if (!is_null($filearea)) { // Add filearea to query and params if necessary
00115             $sql .= ' AND filearea = ?';
00116             $params[] = $filearea;
00117         }
00118 
00119         if (!is_null($itemid)) { // Add itemid to query and params if necessary
00120             $sql .= ' AND itemid = ?';
00121             $params[] = $itemid;
00122         }
00123         $rs = $DB->get_recordset_sql($sql, $params);
00124         foreach ($rs as $record) {
00125             self::insert_backup_ids_record($backupid, 'file', $record->id);
00126         }
00127         $rs->close();
00128     }
00129 
00134     public static function move_annotations_to_final($backupid, $itemname) {
00135         global $DB;
00136         $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname));
00137         foreach($rs as $annotation) {
00138             // If corresponding 'itemfinal' annotation does not exist, update 'item' to 'itemfinal'
00139             if (! $DB->record_exists('backup_ids_temp', array('backupid' => $backupid,
00140                                                               'itemname' => $itemname . 'final',
00141                                                               'itemid' => $annotation->itemid))) {
00142                 $DB->set_field('backup_ids_temp', 'itemname', $itemname . 'final', array('id' => $annotation->id));
00143             }
00144         }
00145         $rs->close();
00146         // All the remaining $itemname annotations can be safely deleted
00147         $DB->delete_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname));
00148     }
00149 
00153     public static function annotations_exist($backupid, $itemname) {
00154         global $DB;
00155         return (bool)$DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname));
00156     }
00157 }
 All Data Structures Namespaces Files Functions Variables Enumerations