1 |
efrain |
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 |
|
|
|
81 |
// Get the section->sequence contents (it roots the activities order)
|
|
|
82 |
// Get all course modules belonging to requested section
|
|
|
83 |
$modulesarr = array();
|
|
|
84 |
$modules = $DB->get_records_sql("
|
|
|
85 |
SELECT cm.id, m.name AS modname
|
|
|
86 |
FROM {course_modules} cm
|
|
|
87 |
JOIN {modules} m ON m.id = cm.module
|
|
|
88 |
WHERE cm.course = ?
|
|
|
89 |
AND cm.section = ?
|
|
|
90 |
AND cm.deletioninprogress <> 1", array($courseid, $sectionid));
|
11 |
efrain |
91 |
foreach (explode(',', (string) $secrec->sequence) as $moduleid) {
|
1 |
efrain |
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,
|
|
|
204 |
$useidonly = false, $files = true) {
|
|
|
205 |
global $DB;
|
|
|
206 |
|
|
|
207 |
// Calculate backup word
|
|
|
208 |
$backupword = str_replace(' ', '_', core_text::strtolower(get_string('backupfilename')));
|
|
|
209 |
$backupword = trim(clean_filename($backupword), '_');
|
|
|
210 |
|
|
|
211 |
// Not $useidonly, lets fetch the name
|
|
|
212 |
$shortname = '';
|
|
|
213 |
if (!$useidonly) {
|
|
|
214 |
// Calculate proper name element (based on type)
|
|
|
215 |
switch ($type) {
|
|
|
216 |
case backup::TYPE_1COURSE:
|
|
|
217 |
$shortname = $DB->get_field('course', 'shortname', array('id' => $id));
|
|
|
218 |
$context = context_course::instance($id);
|
|
|
219 |
$shortname = format_string($shortname, true, array('context' => $context));
|
|
|
220 |
break;
|
|
|
221 |
case backup::TYPE_1SECTION:
|
|
|
222 |
if (!$shortname = $DB->get_field('course_sections', 'name', array('id' => $id))) {
|
|
|
223 |
$shortname = $DB->get_field('course_sections', 'section', array('id' => $id));
|
|
|
224 |
}
|
|
|
225 |
break;
|
|
|
226 |
case backup::TYPE_1ACTIVITY:
|
|
|
227 |
$cm = get_coursemodule_from_id(null, $id);
|
|
|
228 |
$shortname = $cm->modname . $id;
|
|
|
229 |
break;
|
|
|
230 |
}
|
|
|
231 |
$shortname = str_replace(' ', '_', $shortname);
|
|
|
232 |
$shortname = core_text::strtolower(trim(clean_filename($shortname), '_'));
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
// The name will always contain the ID, but we append the course short name if requested.
|
|
|
236 |
$name = $id;
|
|
|
237 |
if (!$useidonly && $shortname != '') {
|
|
|
238 |
$name .= '-' . $shortname;
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
// Calculate date
|
|
|
242 |
$backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig'));
|
|
|
243 |
$date = userdate(time(), $backupdateformat, 99, false);
|
|
|
244 |
$date = core_text::strtolower(trim(clean_filename($date), '_'));
|
|
|
245 |
|
|
|
246 |
// Calculate info
|
|
|
247 |
$info = '';
|
|
|
248 |
if (!$users) {
|
|
|
249 |
$info = '-nu';
|
|
|
250 |
} else if ($anonymised) {
|
|
|
251 |
$info = '-an';
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
// Indicate if backup doesn't contain files.
|
|
|
255 |
if (!$files) {
|
|
|
256 |
$info .= '-nf';
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
return $backupword . '-' . $format . '-' . $type . '-' .
|
|
|
260 |
$name . '-' . $date . $info . '.mbz';
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
/**
|
|
|
264 |
* Returns a flag indicating the need to backup gradebook elements like calculated grade items and category visibility
|
|
|
265 |
* If all activity related grade items are being backed up we can also backup calculated grade items and categories
|
|
|
266 |
*/
|
|
|
267 |
public static function require_gradebook_backup($courseid, $backupid) {
|
|
|
268 |
global $DB;
|
|
|
269 |
|
|
|
270 |
$sql = "SELECT count(id)
|
|
|
271 |
FROM {grade_items}
|
|
|
272 |
WHERE courseid=:courseid
|
|
|
273 |
AND itemtype = 'mod'
|
|
|
274 |
AND id NOT IN (
|
|
|
275 |
SELECT bi.itemid
|
|
|
276 |
FROM {backup_ids_temp} bi
|
|
|
277 |
WHERE bi.itemname = 'grade_itemfinal'
|
|
|
278 |
AND bi.backupid = :backupid)";
|
|
|
279 |
$params = array('courseid'=>$courseid, 'backupid'=>$backupid);
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
$count = $DB->count_records_sql($sql, $params);
|
|
|
283 |
|
|
|
284 |
//if there are 0 activity grade items not already included in the backup
|
|
|
285 |
return $count == 0;
|
|
|
286 |
}
|
|
|
287 |
}
|