MDL-22414 Fixed standard copyright file headers (R0417)
[moodle.git] / backup / util / helper / convert_helper.class.php
CommitLineData
17252e2d 1<?php
e48477d9
DM
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 core
20 * @subpackage backup-convert
21 * @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
17252e2d 27/**
e48477d9 28 * General convert helper
17252e2d
MN
29 */
30abstract class convert_helper {
31 public static function generate_id($entropy) {
32 return md5(time() . '-' . $entropy . '-' . random_string(20));
33 }
c5c8b350
MN
34
35 /**
36 * @static
37 * @throws coding_exception|restore_controller_exception
38 * @param string $tempdir The directory to convert
39 * @param string $format The current format, if already detected
40 * @return void
41 */
42 public static function to_moodle2_format($tempdir, $format = NULL) {
43 if (is_null($format)) {
44 $format = backup_general_helper::detect_backup_format($tempdir);
45 }
46 while (!in_array($format, array(backup::FORMAT_MOODLE, backup::FORMAT_UNKNOWN))) {
47 $converter = convert_factory::converter($format, $tempdir);
48
49 if (!$converter->can_convert()) {
50 throw new coding_exception('Converter detection failed, the loaded converter cannot convert this format');
51 }
52 $converter->convert();
53
54 // Re-detect format
55 $format = backup_general_helper::detect_backup_format($tempdir);
56 }
57 if ($format == backup::FORMAT_UNKNOWN) {
58 throw new restore_controller_exception('cannot_convert_from_unknown_format'); // @todo Change exception class
59 }
60 }
142ec51f
PC
61
62 /**
63 * Inserts an inforef into the conversion temp table
64 */
65 public static function set_inforef($contextid) {
66 global $DB;
eba6af75
MN
67
68
142ec51f
PC
69 }
70
71 public static function get_inforef($contextid) {
72 }
73
74 /**
75 * Converts a plain old php object (popo?) into a string...
76 * Useful for debuging failed db inserts, or anything like that
77 */
78 public static function obj_to_readable($obj) {
79 $mapper = function($field, $value) { return "$field=$value"; };
80 $fields = get_object_vars($obj);
81
82 return implode(", ", array_map($mapper, array_keys($fields), array_values($fields)));
83 }
84
6c0235b3
MN
85 /**
86 * Generate an artificial context ID
87 *
88 * @static
89 * @throws Exception
90 * @param int $instance The moodle component instance ID, same value used for get_context_instance()
91 * @param string $component The moodle component, like block_html, mod_quiz, etc
92 * @param string $converterid The converter ID
93 * @return int
62c5daf1
MN
94 * @todo Add caching?
95 * @todo Can we make the lookup faster? Not taking advantage of indexes
6c0235b3 96 */
eba6af75 97 public static function get_contextid($instance, $component = 'moodle', $converterid = NULL) {
142ec51f
PC
98 global $DB;
99
100 // Attempt to retrieve the contextid
56bd1ab0
MN
101 $contextid = $DB->get_field_select('backup_ids_temp', 'id',
102 $DB->sql_compare_text('info', 100).' = ? AND itemid = ? AND itemname = ?',
103 array($component, $instance, 'context')
104 );
eba6af75 105
e10e8879
MN
106 if (!empty($contextid)) {
107 return $contextid;
142ec51f
PC
108 }
109
110 $context = new stdClass;
eba6af75 111 $context->itemid = $instance;
142ec51f 112 $context->itemname = 'context';
eba6af75
MN
113 $context->info = $component;
114
115 if (!is_null($converterid)) {
116 $context->backupid = $converterid;
117 }
56bd1ab0 118 if ($id = $DB->insert_record('backup_ids_temp', $context)) {
142ec51f
PC
119 return $id;
120 } else {
121 $msg = self::obj_to_readable($context);
122 throw new Exception(sprintf("Could not insert context record into temp table: %s", $msg));
eba6af75 123 }
142ec51f
PC
124 }
125}