Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/dbops/backup_plan_dbops.class.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00033 abstract class backup_plan_dbops extends backup_dbops {
00034 
00038     public static function get_blockids_from_moduleid($moduleid) {
00039         global $DB;
00040 
00041         // Get the context of the module
00042         $contextid = get_context_instance(CONTEXT_MODULE, $moduleid)->id;
00043 
00044         // Get all the block instances which parentcontextid is the module contextid
00045         $blockids = array();
00046         $instances = $DB->get_records('block_instances', array('parentcontextid' => $contextid), '', 'id');
00047         foreach ($instances as $instance) {
00048             $blockids[] = $instance->id;
00049         }
00050         return $blockids;
00051     }
00052 
00056     public static function get_blockids_from_courseid($courseid) {
00057         global $DB;
00058 
00059         // Get the context of the course
00060         $contextid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
00061 
00062         // Get all the block instances which parentcontextid is the course contextid
00063         $blockids = array();
00064         $instances = $DB->get_records('block_instances', array('parentcontextid' => $contextid), '', 'id');
00065         foreach ($instances as $instance) {
00066             $blockids[] = $instance->id;
00067         }
00068         return $blockids;
00069     }
00070 
00074     public static function get_modules_from_sectionid($sectionid) {
00075         global $DB;
00076 
00077         // Get the course and sequence of the section
00078         $secrec = $DB->get_record('course_sections', array('id' => $sectionid), 'course, sequence');
00079         $courseid = $secrec->course;
00080         $sequence = $secrec->sequence;
00081 
00082         // Get the section->sequence contents (it roots the activities order)
00083         // Get all course modules belonging to requested section
00084         $modulesarr = array();
00085         $modules = $DB->get_records_sql("
00086             SELECT cm.id, m.name AS modname
00087               FROM {course_modules} cm
00088               JOIN {modules} m ON m.id = cm.module
00089              WHERE cm.course = ?
00090                AND cm.section = ?", array($courseid, $sectionid));
00091         foreach (explode(',', $sequence) as $moduleid) {
00092             if (isset($modules[$moduleid])) {
00093                 $module = array('id' => $modules[$moduleid]->id, 'modname' => $modules[$moduleid]->modname);
00094                 $modulesarr[] = (object)$module;
00095                 unset($modules[$moduleid]);
00096             }
00097         }
00098         if (!empty($modules)) { // This shouldn't happen, but one borked sequence can lead to it. Add the rest
00099             foreach ($modules as $module) {
00100                 $module = array('id' => $module->id, 'modname' => $module->modname);
00101                 $modulesarr[] = (object)$module;
00102             }
00103         }
00104         return $modulesarr;
00105     }
00106 
00110     public static function get_sections_from_courseid($courseid) {
00111         global $DB;
00112 
00113         // Get all sections belonging to requested course
00114         $sectionsarr = array();
00115         $sections = $DB->get_records('course_sections', array('course' => $courseid));
00116         foreach ($sections as $section) {
00117             $sectionsarr[] = $section->id;
00118         }
00119         return $sectionsarr;
00120     }
00121 
00125     public static function get_courseformat_from_courseid($courseid) {
00126         global $DB;
00127 
00128         return $DB->get_field('course', 'format', array('id' => $courseid));
00129     }
00130 
00142     public static function get_theme_from_courseid($courseid) {
00143         global $DB, $CFG;
00144 
00145         // Course theme first
00146         if (!empty($CFG->allowcoursethemes)) {
00147             $theme = $DB->get_field('course', 'theme', array('id' => $courseid));
00148             if ($theme) {
00149                 return $theme;
00150             }
00151         }
00152 
00153         // Category themes in reverse order
00154         if (!empty($CFG->allowcategorythemes)) {
00155             $catid = $DB->get_field('course', 'category', array('id' => $courseid));
00156             while($catid) {
00157                 $category = $DB->get_record('course_categories', array('id'=>$catid),
00158                         'theme,parent', MUST_EXIST);
00159                 if ($category->theme) {
00160                     return $category->theme;
00161                 }
00162                 $catid = $category->parent;
00163             }
00164         }
00165 
00166         // Finally use site theme
00167         return $CFG->theme;
00168     }
00169 
00174     public static function get_mnet_localhost_wwwroot() {
00175         global $CFG, $DB;
00176 
00177         static $wwwroot = null;
00178 
00179         if (is_null($wwwroot)) {
00180             $wwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $CFG->mnet_localhost_id));
00181         }
00182         return $wwwroot;
00183     }
00184 
00203     public static function get_default_backup_filename($format, $type, $id, $users, $anonymised, $useidasname = false) {
00204         global $DB;
00205 
00206         // Calculate backup word
00207         $backupword = str_replace(' ', '_', moodle_strtolower(get_string('backupfilename')));
00208         $backupword = trim(clean_filename($backupword), '_');
00209 
00210         $shortname = '';
00211         // Not $useidasname, lets calculate it, else $id will be used
00212         if (!$useidasname) {
00213             // Calculate proper name element (based on type)
00214             switch ($type) {
00215                 case backup::TYPE_1COURSE:
00216                     $shortname = $DB->get_field('course', 'shortname', array('id' => $id));
00217                     $context = get_context_instance(CONTEXT_COURSE, $id);
00218                     $shortname = format_string($shortname, true, array('context' => $context));
00219                     break;
00220                 case backup::TYPE_1SECTION:
00221                     if (!$shortname = $DB->get_field('course_sections', 'name', array('id' => $id))) {
00222                         $shortname = $DB->get_field('course_sections', 'section', array('id' => $id));
00223                     }
00224                     break;
00225                 case backup::TYPE_1ACTIVITY:
00226                     $cm = get_coursemodule_from_id(null, $id);
00227                     $shortname = $cm->modname . $id;
00228                     break;
00229             }
00230             $shortname = str_replace(' ', '_', $shortname);
00231             $shortname = moodle_strtolower(trim(clean_filename($shortname), '_'));
00232         }
00233 
00234         $name = empty($shortname) ? $id : $shortname;
00235 
00236         // Calculate date
00237         $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig'));
00238         $date = userdate(time(), $backupdateformat, 99, false);
00239         $date = moodle_strtolower(trim(clean_filename($date), '_'));
00240 
00241         // Calculate info
00242         $info = '';
00243         if (!$users) {
00244             $info = '-nu';
00245         } else if ($anonymised) {
00246             $info = '-an';
00247         }
00248 
00249         return $backupword . '-' . $format . '-' . $type . '-' .
00250                $name . '-' . $date . $info . '.mbz';
00251     }
00252 
00257     public static function require_gradebook_backup($courseid, $backupid) {
00258         global $DB;
00259 
00260         $sql = "SELECT count(id)
00261                   FROM {grade_items}
00262                  WHERE courseid=:courseid
00263                    AND itemtype = 'mod'
00264                    AND id NOT IN (
00265                        SELECT bi.itemid
00266                          FROM {backup_ids_temp} bi
00267                         WHERE bi.itemname = 'grade_itemfinal'
00268                           AND bi.backupid = :backupid)";
00269         $params = array('courseid'=>$courseid, 'backupid'=>$backupid);
00270 
00271 
00272         $count = $DB->count_records_sql($sql, $params);
00273 
00274         //if there are 0 activity grade items not already included in the backup
00275         return $count == 0;
00276     }
00277 }
 All Data Structures Namespaces Files Functions Variables Enumerations