MDL-31558 add missing mimetype to send_temp_file()
[moodle.git] / backup / util / dbops / backup_plan_dbops.class.php
blobf6e25b7f49890a5456daadbd87c1bd71ce8e06fd
1 <?php
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.
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/>.
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
25 /**
26 * Non instantiable helper class providing DB support to the @backup_plan class
28 * This class contains various static methods available for all the DB operations
29 * performed by the @backup_plan (and builder) classes
31 * TODO: Finish phpdocs
33 abstract class backup_plan_dbops extends backup_dbops {
35 /**
36 * Given one course module id, return one array with all the block intances that belong to it
38 public static function get_blockids_from_moduleid($moduleid) {
39 global $DB;
41 // Get the context of the module
42 $contextid = get_context_instance(CONTEXT_MODULE, $moduleid)->id;
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;
50 return $blockids;
53 /**
54 * Given one course id, return one array with all the block intances that belong to it
56 public static function get_blockids_from_courseid($courseid) {
57 global $DB;
59 // Get the context of the course
60 $contextid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
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;
68 return $blockids;
71 /**
72 * Given one section id, return one array with all the course modules that belong to it
74 public static function get_modules_from_sectionid($sectionid) {
75 global $DB;
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;
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]);
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;
104 return $modulesarr;
108 * Given one course id, return one array with all the course_sections belonging to it
110 public static function get_sections_from_courseid($courseid) {
111 global $DB;
113 // Get all sections belonging to requested course
114 $sectionsarr = array();
115 $sections = $DB->get_records('course_sections', array('course' => $courseid));
116 foreach ($sections as $section) {
117 $sectionsarr[] = $section->id;
119 return $sectionsarr;
123 * Given one course id, return its format in DB
125 public static function get_courseformat_from_courseid($courseid) {
126 global $DB;
128 return $DB->get_field('course', 'format', array('id' => $courseid));
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.
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()
142 public static function get_theme_from_courseid($courseid) {
143 global $DB, $CFG;
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;
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;
162 $catid = $category->parent;
166 // Finally use site theme
167 return $CFG->theme;
171 * Return the wwwroot of the $CFG->mnet_localhost_id host
172 * caching it along the request
174 public static function get_mnet_localhost_wwwroot() {
175 global $CFG, $DB;
177 static $wwwroot = null;
179 if (is_null($wwwroot)) {
180 $wwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $CFG->mnet_localhost_id));
182 return $wwwroot;
186 * Returns the default backup filename, based in passed params.
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)
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 $useidasname true to use id, false to use strings (default)
201 * @return string The filename to use
203 public static function get_default_backup_filename($format, $type, $id, $users, $anonymised, $useidasname = false) {
204 global $DB;
206 // Calculate backup word
207 $backupword = str_replace(' ', '_', moodle_strtolower(get_string('backupfilename')));
208 $backupword = trim(clean_filename($backupword), '_');
210 $shortname = '';
211 // Not $useidasname, lets calculate it, else $id will be used
212 if (!$useidasname) {
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 = get_context_instance(CONTEXT_COURSE, $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));
224 break;
225 case backup::TYPE_1ACTIVITY:
226 $cm = get_coursemodule_from_id(null, $id);
227 $shortname = $cm->modname . $id;
228 break;
230 $shortname = str_replace(' ', '_', $shortname);
231 $shortname = moodle_strtolower(trim(clean_filename($shortname), '_'));
234 $name = empty($shortname) ? $id : $shortname;
236 // Calculate date
237 $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig'));
238 $date = userdate(time(), $backupdateformat, 99, false);
239 $date = moodle_strtolower(trim(clean_filename($date), '_'));
241 // Calculate info
242 $info = '';
243 if (!$users) {
244 $info = '-nu';
245 } else if ($anonymised) {
246 $info = '-an';
249 return $backupword . '-' . $format . '-' . $type . '-' .
250 $name . '-' . $date . $info . '.mbz';
254 * Returns a flag indicating the need to backup gradebook elements like calculated grade items and category visibility
255 * If all activity related grade items are being backed up we can also backup calculated grade items and categories
257 public static function require_gradebook_backup($courseid, $backupid) {
258 global $DB;
260 $sql = "SELECT count(id)
261 FROM {grade_items}
262 WHERE courseid=:courseid
263 AND itemtype = 'mod'
264 AND id NOT IN (
265 SELECT bi.itemid
266 FROM {backup_ids_temp} bi
267 WHERE bi.itemname = 'grade_itemfinal'
268 AND bi.backupid = :backupid)";
269 $params = array('courseid'=>$courseid, 'backupid'=>$backupid);
272 $count = $DB->count_records_sql($sql, $params);
274 //if there are 0 activity grade items not already included in the backup
275 return $count == 0;