[ Index ] |
PHP Cross Reference of Unnamed Project |
[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_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 = ?", array($courseid, $sectionid)); 91 foreach (explode(',', $sequence) as $moduleid) { 92 if (isset($modules[$moduleid])) { 93 $module = array('id' => $modules[$moduleid]->id, 'modname' => $modules[$moduleid]->modname); 94 $modulesarr[] = (object)$module; 95 unset($modules[$moduleid]); 96 } 97 } 98 if (!empty($modules)) { // This shouldn't happen, but one borked sequence can lead to it. Add the rest 99 foreach ($modules as $module) { 100 $module = array('id' => $module->id, 'modname' => $module->modname); 101 $modulesarr[] = (object)$module; 102 } 103 } 104 return $modulesarr; 105 } 106 107 /** 108 * Given one course id, return one array with all the course_sections belonging to it 109 */ 110 public static function get_sections_from_courseid($courseid) { 111 global $DB; 112 113 // Get all sections belonging to requested course 114 $sectionsarr = array(); 115 $sections = $DB->get_records('course_sections', array('course' => $courseid), 'section'); 116 foreach ($sections as $section) { 117 $sectionsarr[] = $section->id; 118 } 119 return $sectionsarr; 120 } 121 122 /** 123 * Given one course id, return its format in DB 124 */ 125 public static function get_courseformat_from_courseid($courseid) { 126 global $DB; 127 128 return $DB->get_field('course', 'format', array('id' => $courseid)); 129 } 130 131 /** 132 * Given a course id, returns its theme. This can either be the course 133 * theme or (if not specified in course) the category, site theme. 134 * 135 * User, session, and inherited-from-mnet themes cannot have backed-up 136 * per course data. This is course-related data so it must be in a course 137 * theme specified as part of the course structure 138 * @param int $courseid 139 * @return string Name of course theme 140 * @see moodle_page#resolve_theme() 141 */ 142 public static function get_theme_from_courseid($courseid) { 143 global $DB, $CFG; 144 145 // Course theme first 146 if (!empty($CFG->allowcoursethemes)) { 147 $theme = $DB->get_field('course', 'theme', array('id' => $courseid)); 148 if ($theme) { 149 return $theme; 150 } 151 } 152 153 // Category themes in reverse order 154 if (!empty($CFG->allowcategorythemes)) { 155 $catid = $DB->get_field('course', 'category', array('id' => $courseid)); 156 while($catid) { 157 $category = $DB->get_record('course_categories', array('id'=>$catid), 158 'theme,parent', MUST_EXIST); 159 if ($category->theme) { 160 return $category->theme; 161 } 162 $catid = $category->parent; 163 } 164 } 165 166 // Finally use site theme 167 return $CFG->theme; 168 } 169 170 /** 171 * Return the wwwroot of the $CFG->mnet_localhost_id host 172 * caching it along the request 173 */ 174 public static function get_mnet_localhost_wwwroot() { 175 global $CFG, $DB; 176 177 static $wwwroot = null; 178 179 if (is_null($wwwroot)) { 180 $wwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $CFG->mnet_localhost_id)); 181 } 182 return $wwwroot; 183 } 184 185 /** 186 * Returns the default backup filename, based in passed params. 187 * 188 * Default format is (see MDL-22145) 189 * backup word - format - type - name - date - info . mbz 190 * where name is variable (course shortname, section name/id, activity modulename + cmid) 191 * and info can be (nu = no user info, an = anonymized). The last param $useidasname, 192 * defaulting to false, allows to replace the course shortname by the course id (used 193 * by automated backups, to avoid non-ascii chars in OS filesystem) 194 * 195 * @param string $format One of backup::FORMAT_ 196 * @param string $type One of backup::TYPE_ 197 * @param int $courseid/$sectionid/$cmid 198 * @param bool $users Should be true is users were included in the backup 199 * @param bool $anonymised Should be true is user information was anonymized. 200 * @param bool $useidonly only use the ID in the file name 201 * @return string The filename to use 202 */ 203 public static function get_default_backup_filename($format, $type, $id, $users, $anonymised, $useidonly = false) { 204 global $DB; 205 206 // Calculate backup word 207 $backupword = str_replace(' ', '_', core_text::strtolower(get_string('backupfilename'))); 208 $backupword = trim(clean_filename($backupword), '_'); 209 210 // Not $useidonly, lets fetch the name 211 $shortname = ''; 212 if (!$useidonly) { 213 // Calculate proper name element (based on type) 214 switch ($type) { 215 case backup::TYPE_1COURSE: 216 $shortname = $DB->get_field('course', 'shortname', array('id' => $id)); 217 $context = context_course::instance($id); 218 $shortname = format_string($shortname, true, array('context' => $context)); 219 break; 220 case backup::TYPE_1SECTION: 221 if (!$shortname = $DB->get_field('course_sections', 'name', array('id' => $id))) { 222 $shortname = $DB->get_field('course_sections', 'section', array('id' => $id)); 223 } 224 break; 225 case backup::TYPE_1ACTIVITY: 226 $cm = get_coursemodule_from_id(null, $id); 227 $shortname = $cm->modname . $id; 228 break; 229 } 230 $shortname = str_replace(' ', '_', $shortname); 231 $shortname = core_text::strtolower(trim(clean_filename($shortname), '_')); 232 } 233 234 // The name will always contain the ID, but we append the course short name if requested. 235 $name = $id; 236 if (!$useidonly && $shortname != '') { 237 $name .= '-' . $shortname; 238 } 239 240 // Calculate date 241 $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig')); 242 $date = userdate(time(), $backupdateformat, 99, false); 243 $date = core_text::strtolower(trim(clean_filename($date), '_')); 244 245 // Calculate info 246 $info = ''; 247 if (!$users) { 248 $info = '-nu'; 249 } else if ($anonymised) { 250 $info = '-an'; 251 } 252 253 return $backupword . '-' . $format . '-' . $type . '-' . 254 $name . '-' . $date . $info . '.mbz'; 255 } 256 257 /** 258 * Returns a flag indicating the need to backup gradebook elements like calculated grade items and category visibility 259 * If all activity related grade items are being backed up we can also backup calculated grade items and categories 260 */ 261 public static function require_gradebook_backup($courseid, $backupid) { 262 global $DB; 263 264 $sql = "SELECT count(id) 265 FROM {grade_items} 266 WHERE courseid=:courseid 267 AND itemtype = 'mod' 268 AND id NOT IN ( 269 SELECT bi.itemid 270 FROM {backup_ids_temp} bi 271 WHERE bi.itemname = 'grade_itemfinal' 272 AND bi.backupid = :backupid)"; 273 $params = array('courseid'=>$courseid, 'backupid'=>$backupid); 274 275 276 $count = $DB->count_records_sql($sql, $params); 277 278 //if there are 0 activity grade items not already included in the backup 279 return $count == 0; 280 } 281 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |