See Release Notes
Long Term Support Release
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_plan class 27 * 28 * This class contains various static methods available for all the DB operations 29 * performed by the @backup_plan (and builder) classes 30 * 31 * TODO: Finish phpdocs 32 */ 33 abstract class backup_plan_dbops extends backup_dbops { 34 35 /** 36 * Given one course module id, return one array with all the block intances that belong to it 37 */ 38 public static function get_blockids_from_moduleid($moduleid) { 39 global $DB; 40 41 // Get the context of the module 42 $contextid = context_module::instance($moduleid)->id; 43 44 // Get all the block instances which parentcontextid is the module contextid 45 $blockids = array(); 46 $instances = $DB->get_records('block_instances', array('parentcontextid' => $contextid), '', 'id'); 47 foreach ($instances as $instance) { 48 $blockids[] = $instance->id; 49 } 50 return $blockids; 51 } 52 53 /** 54 * Given one course id, return one array with all the block intances that belong to it 55 */ 56 public static function get_blockids_from_courseid($courseid) { 57 global $DB; 58 59 // Get the context of the course 60 $contextid = context_course::instance($courseid)->id; 61 62 // Get all the block instances which parentcontextid is the course contextid 63 $blockids = array(); 64 $instances = $DB->get_records('block_instances', array('parentcontextid' => $contextid), '', 'id'); 65 foreach ($instances as $instance) { 66 $blockids[] = $instance->id; 67 } 68 return $blockids; 69 } 70 71 /** 72 * Given one section id, return one array with all the course modules that belong to it 73 */ 74 public static function get_modules_from_sectionid($sectionid) { 75 global $DB; 76 77 // Get the course and sequence of the section 78 $secrec = $DB->get_record('course_sections', array('id' => $sectionid), 'course, sequence'); 79 $courseid = $secrec->course; 80 $sequence = $secrec->sequence; 81 82 // Get the section->sequence contents (it roots the activities order) 83 // Get all course modules belonging to requested section 84 $modulesarr = array(); 85 $modules = $DB->get_records_sql(" 86 SELECT cm.id, m.name AS modname 87 FROM {course_modules} cm 88 JOIN {modules} m ON m.id = cm.module 89 WHERE cm.course = ? 90 AND cm.section = ? 91 AND cm.deletioninprogress <> 1", array($courseid, $sectionid)); 92 foreach (explode(',', $sequence) as $moduleid) { 93 if (isset($modules[$moduleid])) { 94 $module = array('id' => $modules[$moduleid]->id, 'modname' => $modules[$moduleid]->modname); 95 $modulesarr[] = (object)$module; 96 unset($modules[$moduleid]); 97 } 98 } 99 if (!empty($modules)) { // This shouldn't happen, but one borked sequence can lead to it. Add the rest 100 foreach ($modules as $module) { 101 $module = array('id' => $module->id, 'modname' => $module->modname); 102 $modulesarr[] = (object)$module; 103 } 104 } 105 return $modulesarr; 106 } 107 108 /** 109 * Given one course id, return one array with all the course_sections belonging to it 110 */ 111 public static function get_sections_from_courseid($courseid) { 112 global $DB; 113 114 // Get all sections belonging to requested course 115 $sectionsarr = array(); 116 $sections = $DB->get_records('course_sections', array('course' => $courseid), 'section'); 117 foreach ($sections as $section) { 118 $sectionsarr[] = $section->id; 119 } 120 return $sectionsarr; 121 } 122 123 /** 124 * Given one course id, return its format in DB 125 */ 126 public static function get_courseformat_from_courseid($courseid) { 127 global $DB; 128 129 return $DB->get_field('course', 'format', array('id' => $courseid)); 130 } 131 132 /** 133 * Given a course id, returns its theme. This can either be the course 134 * theme or (if not specified in course) the category, site theme. 135 * 136 * User, session, and inherited-from-mnet themes cannot have backed-up 137 * per course data. This is course-related data so it must be in a course 138 * theme specified as part of the course structure 139 * @param int $courseid 140 * @return string Name of course theme 141 * @see moodle_page#resolve_theme() 142 */ 143 public static function get_theme_from_courseid($courseid) { 144 global $DB, $CFG; 145 146 // Course theme first 147 if (!empty($CFG->allowcoursethemes)) { 148 $theme = $DB->get_field('course', 'theme', array('id' => $courseid)); 149 if ($theme) { 150 return $theme; 151 } 152 } 153 154 // Category themes in reverse order 155 if (!empty($CFG->allowcategorythemes)) { 156 $catid = $DB->get_field('course', 'category', array('id' => $courseid)); 157 while($catid) { 158 $category = $DB->get_record('course_categories', array('id'=>$catid), 159 'theme,parent', MUST_EXIST); 160 if ($category->theme) { 161 return $category->theme; 162 } 163 $catid = $category->parent; 164 } 165 } 166 167 // Finally use site theme 168 return $CFG->theme; 169 } 170 171 /** 172 * Return the wwwroot of the $CFG->mnet_localhost_id host 173 * caching it along the request 174 */ 175 public static function get_mnet_localhost_wwwroot() { 176 global $CFG, $DB; 177 178 static $wwwroot = null; 179 180 if (is_null($wwwroot)) { 181 $wwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $CFG->mnet_localhost_id)); 182 } 183 return $wwwroot; 184 } 185 186 /** 187 * Returns the default backup filename, based in passed params. 188 * 189 * Default format is (see MDL-22145) 190 * backup word - format - type - name - date - info . mbz 191 * where name is variable (course shortname, section name/id, activity modulename + cmid) 192 * and info can be (nu = no user info, an = anonymized). The last param $useidasname, 193 * defaulting to false, allows to replace the course shortname by the course id (used 194 * by automated backups, to avoid non-ascii chars in OS filesystem) 195 * 196 * @param string $format One of backup::FORMAT_ 197 * @param string $type One of backup::TYPE_ 198 * @param int $courseid/$sectionid/$cmid 199 * @param bool $users Should be true is users were included in the backup 200 * @param bool $anonymised Should be true is user information was anonymized. 201 * @param bool $useidonly only use the ID in the file name 202 * @return string The filename to use 203 */ 204 public static function get_default_backup_filename($format, $type, $id, $users, $anonymised, 205 $useidonly = false, $files = true) { 206 global $DB; 207 208 // Calculate backup word 209 $backupword = str_replace(' ', '_', core_text::strtolower(get_string('backupfilename'))); 210 $backupword = trim(clean_filename($backupword), '_'); 211 212 // Not $useidonly, lets fetch the name 213 $shortname = ''; 214 if (!$useidonly) { 215 // Calculate proper name element (based on type) 216 switch ($type) { 217 case backup::TYPE_1COURSE: 218 $shortname = $DB->get_field('course', 'shortname', array('id' => $id)); 219 $context = context_course::instance($id); 220 $shortname = format_string($shortname, true, array('context' => $context)); 221 break; 222 case backup::TYPE_1SECTION: 223 if (!$shortname = $DB->get_field('course_sections', 'name', array('id' => $id))) { 224 $shortname = $DB->get_field('course_sections', 'section', array('id' => $id)); 225 } 226 break; 227 case backup::TYPE_1ACTIVITY: 228 $cm = get_coursemodule_from_id(null, $id); 229 $shortname = $cm->modname . $id; 230 break; 231 } 232 $shortname = str_replace(' ', '_', $shortname); 233 $shortname = core_text::strtolower(trim(clean_filename($shortname), '_')); 234 } 235 236 // The name will always contain the ID, but we append the course short name if requested. 237 $name = $id; 238 if (!$useidonly && $shortname != '') { 239 $name .= '-' . $shortname; 240 } 241 242 // Calculate date 243 $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig')); 244 $date = userdate(time(), $backupdateformat, 99, false); 245 $date = core_text::strtolower(trim(clean_filename($date), '_')); 246 247 // Calculate info 248 $info = ''; 249 if (!$users) { 250 $info = '-nu'; 251 } else if ($anonymised) { 252 $info = '-an'; 253 } 254 255 // Indicate if backup doesn't contain files. 256 if (!$files) { 257 $info .= '-nf'; 258 } 259 260 return $backupword . '-' . $format . '-' . $type . '-' . 261 $name . '-' . $date . $info . '.mbz'; 262 } 263 264 /** 265 * Returns a flag indicating the need to backup gradebook elements like calculated grade items and category visibility 266 * If all activity related grade items are being backed up we can also backup calculated grade items and categories 267 */ 268 public static function require_gradebook_backup($courseid, $backupid) { 269 global $DB; 270 271 $sql = "SELECT count(id) 272 FROM {grade_items} 273 WHERE courseid=:courseid 274 AND itemtype = 'mod' 275 AND id NOT IN ( 276 SELECT bi.itemid 277 FROM {backup_ids_temp} bi 278 WHERE bi.itemname = 'grade_itemfinal' 279 AND bi.backupid = :backupid)"; 280 $params = array('courseid'=>$courseid, 'backupid'=>$backupid); 281 282 283 $count = $DB->count_records_sql($sql, $params); 284 285 //if there are 0 activity grade items not already included in the backup 286 return $count == 0; 287 } 288 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body