Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
    /**
1441 ariadna 123
     * Given one section id, returns the full section record.
124
     *
125
     * @param int $sectionid
126
     * @return stdClass
127
     */
128
    public static function get_section_from_id($sectionid): stdClass {
129
        global $DB;
130
        return $DB->get_record('course_sections', ['id' => $sectionid]);
131
    }
132
 
133
    /**
1 efrain 134
     * Given one course id, return its format in DB
135
     */
136
    public static function get_courseformat_from_courseid($courseid) {
137
        global $DB;
138
 
139
        return $DB->get_field('course', 'format', array('id' => $courseid));
140
    }
141
 
142
    /**
143
     * Given a course id, returns its theme. This can either be the course
144
     * theme or (if not specified in course) the category, site theme.
145
     *
146
     * User, session, and inherited-from-mnet themes cannot have backed-up
147
     * per course data. This is course-related data so it must be in a course
148
     * theme specified as part of the course structure
149
     * @param int $courseid
150
     * @return string Name of course theme
151
     * @see moodle_page#resolve_theme()
152
     */
153
    public static function get_theme_from_courseid($courseid) {
154
        global $DB, $CFG;
155
 
156
        // Course theme first
157
        if (!empty($CFG->allowcoursethemes)) {
158
            $theme = $DB->get_field('course', 'theme', array('id' => $courseid));
159
            if ($theme) {
160
                return $theme;
161
            }
162
        }
163
 
164
        // Category themes in reverse order
165
        if (!empty($CFG->allowcategorythemes)) {
166
            $catid = $DB->get_field('course', 'category', array('id' => $courseid));
167
            while($catid) {
168
                $category = $DB->get_record('course_categories', array('id'=>$catid),
169
                        'theme,parent', MUST_EXIST);
170
                if ($category->theme) {
171
                    return $category->theme;
172
                }
173
                $catid = $category->parent;
174
            }
175
        }
176
 
177
        // Finally use site theme
178
        return $CFG->theme;
179
    }
180
 
181
    /**
182
     * Return the wwwroot of the $CFG->mnet_localhost_id host
183
     * caching it along the request
184
     */
185
    public static function get_mnet_localhost_wwwroot() {
186
        global $CFG, $DB;
187
 
188
        static $wwwroot = null;
189
 
190
        if (is_null($wwwroot)) {
191
            $wwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $CFG->mnet_localhost_id));
192
        }
193
        return $wwwroot;
194
    }
195
 
196
    /**
197
    * Returns the default backup filename, based in passed params.
198
    *
199
    * Default format is (see MDL-22145)
200
    * backup word - format - type - name - date - info . mbz
201
    * where name is variable (course shortname, section name/id, activity modulename + cmid)
202
    * and info can be (nu = no user info, an = anonymized). The last param $useidasname,
203
    * defaulting to false, allows to replace the course shortname by the course id (used
204
    * by automated backups, to avoid non-ascii chars in OS filesystem)
205
    *
206
    * @param string $format One of backup::FORMAT_
207
    * @param string $type One of backup::TYPE_
208
    * @param int $courseid/$sectionid/$cmid
209
    * @param bool $users Should be true is users were included in the backup
210
    * @param bool $anonymised Should be true is user information was anonymized.
211
    * @param bool $useidonly only use the ID in the file name
212
    * @return string The filename to use
213
    */
214
    public static function get_default_backup_filename($format, $type, $id, $users, $anonymised,
215
            $useidonly = false, $files = true) {
216
        global $DB;
217
 
218
        // Calculate backup word
219
        $backupword = str_replace(' ', '_', core_text::strtolower(get_string('backupfilename')));
220
        $backupword = trim(clean_filename($backupword), '_');
221
 
222
        // Not $useidonly, lets fetch the name
223
        $shortname = '';
224
        if (!$useidonly) {
225
            // Calculate proper name element (based on type)
226
            switch ($type) {
227
                case backup::TYPE_1COURSE:
228
                    $shortname = $DB->get_field('course', 'shortname', array('id' => $id));
229
                    $context = context_course::instance($id);
230
                    $shortname = format_string($shortname, true, array('context' => $context));
231
                    break;
232
                case backup::TYPE_1SECTION:
233
                    if (!$shortname = $DB->get_field('course_sections', 'name', array('id' => $id))) {
234
                        $shortname = $DB->get_field('course_sections', 'section', array('id' => $id));
235
                    }
236
                    break;
237
                case backup::TYPE_1ACTIVITY:
238
                    $cm = get_coursemodule_from_id(null, $id);
239
                    $shortname = $cm->modname . $id;
240
                    break;
241
            }
242
            $shortname = str_replace(' ', '_', $shortname);
243
            $shortname = core_text::strtolower(trim(clean_filename($shortname), '_'));
244
        }
245
 
246
        // The name will always contain the ID, but we append the course short name if requested.
247
        $name = $id;
248
        if (!$useidonly && $shortname != '') {
249
            $name .= '-' . $shortname;
250
        }
251
 
252
        // Calculate date
253
        $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig'));
254
        $date = userdate(time(), $backupdateformat, 99, false);
255
        $date = core_text::strtolower(trim(clean_filename($date), '_'));
256
 
257
        // Calculate info
258
        $info = '';
259
        if (!$users) {
260
            $info = '-nu';
261
        } else if ($anonymised) {
262
            $info = '-an';
263
        }
264
 
265
        // Indicate if backup doesn't contain files.
266
        if (!$files) {
267
            $info .= '-nf';
268
        }
269
 
270
        return $backupword . '-' . $format . '-' . $type . '-' .
271
               $name . '-' . $date . $info . '.mbz';
272
    }
273
 
274
    /**
275
    * Returns a flag indicating the need to backup gradebook elements like calculated grade items and category visibility
276
    * If all activity related grade items are being backed up we can also backup calculated grade items and categories
277
    */
278
    public static function require_gradebook_backup($courseid, $backupid) {
279
        global $DB;
280
 
281
        $sql = "SELECT count(id)
282
                  FROM {grade_items}
283
                 WHERE courseid=:courseid
284
                   AND itemtype = 'mod'
285
                   AND id NOT IN (
286
                       SELECT bi.itemid
287
                         FROM {backup_ids_temp} bi
288
                        WHERE bi.itemname = 'grade_itemfinal'
289
                          AND bi.backupid = :backupid)";
290
        $params = array('courseid'=>$courseid, 'backupid'=>$backupid);
291
 
292
 
293
        $count = $DB->count_records_sql($sql, $params);
294
 
295
        //if there are 0 activity grade items not already included in the backup
296
        return $count == 0;
297
    }
298
}