[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 /** 19 * @package moodlecore 20 * @subpackage backup-dbops 21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 /** 26 * Non instantiable helper class providing DB support to the backup_structure stuff 27 * 28 * This class contains various static methods available for all the DB operations 29 * performed by the backup_structure stuff (mainly @backup_nested_element class) 30 * 31 * TODO: Finish phpdocs 32 */ 33 abstract class backup_structure_dbops extends backup_dbops { 34 35 public static function get_iterator($element, $params, $processor) { 36 global $DB; 37 // Check we are going to get_iterator for one backup_nested_element 38 if (! $element instanceof backup_nested_element) { 39 throw new base_element_struct_exception('backup_nested_element_expected'); 40 } 41 // If var_array, table and sql are null, and element has no final elements it is one nested element without source 42 // Just return one 1 element iterator without information 43 if ($element->get_source_array() === null && $element->get_source_table() === null && 44 $element->get_source_sql() === null && count($element->get_final_elements()) == 0) { 45 return new backup_array_iterator(array(0 => null)); 46 47 } else if ($element->get_source_array() !== null) { // It's one array, return array_iterator 48 return new backup_array_iterator($element->get_source_array()); 49 50 } else if ($element->get_source_table() !== null) { // It's one table, return recordset iterator 51 return $DB->get_recordset($element->get_source_table(), self::convert_params_to_values($params, $processor), $element->get_source_table_sortby()); 52 53 } else if ($element->get_source_sql() !== null) { // It's one sql, return recordset iterator 54 return $DB->get_recordset_sql($element->get_source_sql(), self::convert_params_to_values($params, $processor)); 55 56 } else { // No sources, supress completely, using null iterator 57 return new backup_null_iterator(); 58 } 59 } 60 61 protected static function convert_params_to_values($params, $processor) { 62 $newparams = array(); 63 foreach ($params as $key => $param) { 64 $newvalue = null; 65 // If we have a base element, get its current value, exception if not set 66 if ($param instanceof base_atom) { 67 if ($param->is_set()) { 68 $newvalue = $param->get_value(); 69 } else { 70 throw new base_element_struct_exception('valueofparamelementnotset', $param->get_name()); 71 } 72 73 } else if ($param < 0) { // Possibly one processor variable, let's process it 74 // See @backup class for all the VAR_XXX variables available. 75 // Note1: backup::VAR_PARENTID is handled by nested elements themselves 76 // Note2: trying to use one non-existing var will throw exception 77 $newvalue = $processor->get_var($param); 78 79 // Else we have one raw param value, use it 80 } else { 81 $newvalue = $param; 82 } 83 84 $newparams[$key] = $newvalue; 85 } 86 return $newparams; 87 } 88 89 public static function insert_backup_ids_record($backupid, $itemname, $itemid) { 90 global $DB; 91 // We need to do some magic with scales (that are stored in negative way) 92 if ($itemname == 'scale') { 93 $itemid = -($itemid); 94 } 95 // Now, we skip any annotation with negatives/zero/nulls, ids table only stores true id (always > 0) 96 if ($itemid <= 0 || is_null($itemid)) { 97 return; 98 } 99 // TODO: Analyze if some static (and limited) cache by the 3 params could save us a bunch of record_exists() calls 100 // Note: Sure it will! 101 if (!$DB->record_exists('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname, 'itemid' => $itemid))) { 102 $DB->insert_record('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname, 'itemid' => $itemid)); 103 } 104 } 105 106 /** 107 * Adds backup id database record for all files in the given file area. 108 * 109 * @param string $backupid Backup ID 110 * @param int $contextid Context id 111 * @param string $component Component 112 * @param string $filearea File area 113 * @param int $itemid Item id 114 * @param \core\progress\base $progress 115 */ 116 public static function annotate_files($backupid, $contextid, $component, $filearea, $itemid, 117 \core\progress\base $progress = null) { 118 global $DB; 119 $sql = 'SELECT id 120 FROM {files} 121 WHERE contextid = ? 122 AND component = ?'; 123 $params = array($contextid, $component); 124 125 if (!is_null($filearea)) { // Add filearea to query and params if necessary 126 $sql .= ' AND filearea = ?'; 127 $params[] = $filearea; 128 } 129 130 if (!is_null($itemid)) { // Add itemid to query and params if necessary 131 $sql .= ' AND itemid = ?'; 132 $params[] = $itemid; 133 } 134 if ($progress) { 135 $progress->start_progress(''); 136 } 137 $rs = $DB->get_recordset_sql($sql, $params); 138 foreach ($rs as $record) { 139 if ($progress) { 140 $progress->progress(); 141 } 142 self::insert_backup_ids_record($backupid, 'file', $record->id); 143 } 144 if ($progress) { 145 $progress->end_progress(); 146 } 147 $rs->close(); 148 } 149 150 /** 151 * Moves all the existing 'item' annotations to their final 'itemfinal' ones 152 * for a given backup. 153 * 154 * @param string $backupid Backup ID 155 * @param string $itemname Item name 156 * @param \core\progress\base $progress Progress tracker 157 */ 158 public static function move_annotations_to_final($backupid, $itemname, \core\progress\base $progress) { 159 global $DB; 160 $progress->start_progress('move_annotations_to_final'); 161 $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname)); 162 $progress->progress(); 163 foreach($rs as $annotation) { 164 // If corresponding 'itemfinal' annotation does not exist, update 'item' to 'itemfinal' 165 if (! $DB->record_exists('backup_ids_temp', array('backupid' => $backupid, 166 'itemname' => $itemname . 'final', 167 'itemid' => $annotation->itemid))) { 168 $DB->set_field('backup_ids_temp', 'itemname', $itemname . 'final', array('id' => $annotation->id)); 169 } 170 $progress->progress(); 171 } 172 $rs->close(); 173 // All the remaining $itemname annotations can be safely deleted 174 $DB->delete_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname)); 175 $progress->end_progress(); 176 } 177 178 /** 179 * Returns true/false if there are annotations for a given item 180 */ 181 public static function annotations_exist($backupid, $itemname) { 182 global $DB; 183 return (bool)$DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname)); 184 } 185 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |