MDL-25863 backup - take rid of dupe code for filename calculations
[moodle.git] / backup / util / dbops / backup_plan_dbops.class.php
CommitLineData
69dd0c8c
EL
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 */
33abstract 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 = get_context_instance(CONTEXT_MODULE, $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 = get_context_instance(CONTEXT_COURSE, $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
327f3fda
EL
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;
69dd0c8c 81
327f3fda 82 // Get the section->sequence contents (it roots the activities order)
69dd0c8c
EL
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));
327f3fda
EL
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 }
69dd0c8c
EL
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));
116 foreach ($sections as $section) {
117 $sectionsarr[] = $section->id;
118 }
119 return $sectionsarr;
120 }
121
a90659d6
EL
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
69dd0c8c
EL
131 /**
132 * Return the wwwroot of the $CFG->mnet_localhost_id host
133 * caching it along the request
134 */
135 public static function get_mnet_localhost_wwwroot() {
136 global $CFG, $DB;
137
138 static $wwwroot = null;
139
140 if (is_null($wwwroot)) {
141 $wwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $CFG->mnet_localhost_id));
142 }
143 return $wwwroot;
144 }
be3e542d
EL
145
146 /**
147 * Returns the default backup filename, based in passed params.
148 *
149 * Default format is (see MDL-22145)
02cf6919 150 * backup word - format - type - name - date - info . mbz
be3e542d 151 * where name is variable (course shortname, section name/id, activity modulename + cmid)
02cf6919
EL
152 * and info can be (nu = no user info, an = anonymized). The last param $useidasname,
153 * defaulting to false, allows to replace the course shortname by the course id (used
154 * by automated backups, to avoid non-ascii chars in OS filesystem)
155 *
156 * @param string $format One of backup::FORMAT_
157 * @param string $type One of backup::TYPE_
158 * @param int $courseid/$sectionid/$cmid
159 * @param bool $users Should be true is users were included in the backup
160 * @param bool $anonymised Should be true is user information was anonymized.
161 * @param bool $useidasname true to use id, false to use strings (default)
162 * @return string The filename to use
be3e542d 163 */
02cf6919 164 public static function get_default_backup_filename($format, $type, $id, $users, $anonymised, $useidasname = false) {
be3e542d
EL
165 global $DB;
166
167 // Calculate backup word
168 $backupword = str_replace(' ', '_', moodle_strtolower(get_string('backupfilename')));
169 $backupword = trim(clean_filename($backupword), '_');
170
02cf6919
EL
171 $shortname = '';
172 // Not $useidasname, lets calculate it, else $id will be used
173 if (!$useidasname) {
174 // Calculate proper name element (based on type)
175 switch ($type) {
176 case backup::TYPE_1COURSE:
177 $shortname = $DB->get_field('course', 'shortname', array('id' => $id));
178 break;
179 case backup::TYPE_1SECTION:
180 if (!$shortname = $DB->get_field('course_sections', 'name', array('id' => $id))) {
181 $shortname = $DB->get_field('course_sections', 'section', array('id' => $id));
182 }
183 break;
184 case backup::TYPE_1ACTIVITY:
185 $cm = get_coursemodule_from_id(null, $id);
186 $shortname = $cm->modname . $id;
187 break;
188 }
189 $shortname = str_replace(' ', '_', $shortname);
190 $shortname = moodle_strtolower(trim(clean_filename($shortname), '_'));
be3e542d 191 }
02cf6919 192
be3e542d
EL
193 $name = empty($shortname) ? $id : $shortname;
194
195 // Calculate date
196 $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig'));
197 $date = userdate(time(), $backupdateformat, 99, false);
198 $date = moodle_strtolower(trim(clean_filename($date), '_'));
199
200 // Calculate info
201 $info = '';
202 if (!$users) {
203 $info = '-nu';
204 } else if ($anonymised) {
205 $info = '-an';
206 }
207
208 return $backupword . '-' . $format . '-' . $type . '-' .
12c80f79 209 $name . '-' . $date . $info . '.mbz';
be3e542d 210 }
315f6d8e
AD
211
212 /**
213 * Returns a flag indicating the need to backup gradebook elements like calculated grade items and category visibility
214 * If all activity related grade items are being backed up we can also backup calculated grade items and categories
215 */
216 public static function require_gradebook_backup($courseid, $backupid) {
217 global $DB;
218
315f6d8e 219 $sql = "SELECT count(id)
c60a8c82
EL
220 FROM {grade_items}
221 WHERE courseid=:courseid
222 AND itemtype = 'mod'
223 AND id NOT IN (
224 SELECT bi.itemid
225 FROM {backup_ids_temp} bi
226 WHERE bi.itemname = 'grade_itemfinal'
227 AND bi.backupid = :backupid)";
315f6d8e
AD
228 $params = array('courseid'=>$courseid, 'backupid'=>$backupid);
229
c60a8c82 230
315f6d8e
AD
231 $count = $DB->count_records_sql($sql, $params);
232
233 //if there are 0 activity grade items not already included in the backup
c60a8c82 234 return $count == 0;
315f6d8e 235 }
69dd0c8c 236}