MDL-22139 backup - minor fix to query (grade_item->id is in backup_ids_temp->itemid)
[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
77 // Get the course of the section
78 $courseid = $DB->get_field('course_sections', 'course', array('id' => $sectionid));
79
80 // Get all course modules belonging to requested section
81 $modulesarr = array();
82 $modules = $DB->get_records_sql("
83 SELECT cm.id, m.name AS modname
84 FROM {course_modules} cm
85 JOIN {modules} m ON m.id = cm.module
86 WHERE cm.course = ?
87 AND cm.section = ?", array($courseid, $sectionid));
88 foreach ($modules as $module) {
89 $module = array('id' => $module->id, 'modname' => $module->modname);
90 $modulesarr[] = (object)$module;
91 }
92 return $modulesarr;
93 }
94
95 /**
96 * Given one course id, return one array with all the course_sections belonging to it
97 */
98 public static function get_sections_from_courseid($courseid) {
99 global $DB;
100
101 // Get all sections belonging to requested course
102 $sectionsarr = array();
103 $sections = $DB->get_records('course_sections', array('course' => $courseid));
104 foreach ($sections as $section) {
105 $sectionsarr[] = $section->id;
106 }
107 return $sectionsarr;
108 }
109
110 /**
111 * Return the wwwroot of the $CFG->mnet_localhost_id host
112 * caching it along the request
113 */
114 public static function get_mnet_localhost_wwwroot() {
115 global $CFG, $DB;
116
117 static $wwwroot = null;
118
119 if (is_null($wwwroot)) {
120 $wwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $CFG->mnet_localhost_id));
121 }
122 return $wwwroot;
123 }
be3e542d
EL
124
125 /**
126 * Returns the default backup filename, based in passed params.
127 *
128 * Default format is (see MDL-22145)
129 * backup word - format - type - name - date - info . zip
130 * where name is variable (course shortname, section name/id, activity modulename + cmid)
131 * and info can be (nu = no user info, an = anonymized)
132 */
133 public static function get_default_backup_filename($format, $type, $id, $users, $anonymised) {
134 global $DB;
135
136 // Calculate backup word
137 $backupword = str_replace(' ', '_', moodle_strtolower(get_string('backupfilename')));
138 $backupword = trim(clean_filename($backupword), '_');
139
140 // Calculate proper name element (based on type)
141 switch ($type) {
142 case backup::TYPE_1COURSE:
143 $shortname = $DB->get_field('course', 'shortname', array('id' => $id));
144 break;
145 case backup::TYPE_1SECTION:
146 if (!$shortname = $DB->get_field('course_sections', 'name', array('id' => $id))) {
147 $shortname = $DB->get_field('course_sections', 'section', array('id' => $id));
148 }
149 break;
150 case backup::TYPE_1ACTIVITY:
151 $cm = get_coursemodule_from_id(null, $id);
152 $shortname = $cm->modname . $id;
153 break;
154 }
155 $shortname = str_replace(' ', '_', $shortname);
156 $shortname = moodle_strtolower(trim(clean_filename($shortname), '_'));
157 $name = empty($shortname) ? $id : $shortname;
158
159 // Calculate date
160 $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig'));
161 $date = userdate(time(), $backupdateformat, 99, false);
162 $date = moodle_strtolower(trim(clean_filename($date), '_'));
163
164 // Calculate info
165 $info = '';
166 if (!$users) {
167 $info = '-nu';
168 } else if ($anonymised) {
169 $info = '-an';
170 }
171
172 return $backupword . '-' . $format . '-' . $type . '-' .
173 $name . '-' . $date . $info . '.zip';
174 }
315f6d8e
AD
175
176 /**
177 * Returns a flag indicating the need to backup gradebook elements like calculated grade items and category visibility
178 * If all activity related grade items are being backed up we can also backup calculated grade items and categories
179 */
180 public static function require_gradebook_backup($courseid, $backupid) {
181 global $DB;
182
315f6d8e 183 $sql = "SELECT count(id)
c60a8c82
EL
184 FROM {grade_items}
185 WHERE courseid=:courseid
186 AND itemtype = 'mod'
187 AND id NOT IN (
188 SELECT bi.itemid
189 FROM {backup_ids_temp} bi
190 WHERE bi.itemname = 'grade_itemfinal'
191 AND bi.backupid = :backupid)";
315f6d8e
AD
192 $params = array('courseid'=>$courseid, 'backupid'=>$backupid);
193
c60a8c82 194
315f6d8e
AD
195 $count = $DB->count_records_sql($sql, $params);
196
197 //if there are 0 activity grade items not already included in the backup
c60a8c82 198 return $count == 0;
315f6d8e 199 }
69dd0c8c 200}