mirrored from git://git.moodle.org/moodle.git
/
convert_helper.class.php
84 lines (70 loc) · 2.76 KB
/
convert_helper.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* General Convert Helper
*/
abstract class convert_helper {
public static function generate_id($entropy) {
return md5(time() . '-' . $entropy . '-' . random_string(20));
}
/**
* @static
* @throws coding_exception|restore_controller_exception
* @param string $tempdir The directory to convert
* @param string $format The current format, if already detected
* @return void
*/
public static function to_moodle2_format($tempdir, $format = NULL) {
if (is_null($format)) {
$format = backup_general_helper::detect_backup_format($tempdir);
}
while (!in_array($format, array(backup::FORMAT_MOODLE, backup::FORMAT_UNKNOWN))) {
$converter = convert_factory::converter($format, $tempdir);
if (!$converter->can_convert()) {
throw new coding_exception('Converter detection failed, the loaded converter cannot convert this format');
}
$converter->convert();
// Re-detect format
$format = backup_general_helper::detect_backup_format($tempdir);
}
if ($format == backup::FORMAT_UNKNOWN) {
throw new restore_controller_exception('cannot_convert_from_unknown_format'); // @todo Change exception class
}
}
/**
* Inserts an inforef into the conversion temp table
*/
public static function set_inforef($contextid) {
global $DB;
}
public static function get_inforef($contextid) {
}
/**
* Converts a plain old php object (popo?) into a string...
* Useful for debuging failed db inserts, or anything like that
*/
public static function obj_to_readable($obj) {
$mapper = function($field, $value) { return "$field=$value"; };
$fields = get_object_vars($obj);
return implode(", ", array_map($mapper, array_keys($fields), array_values($fields)));
}
public static function get_contextid($converterid, $itemid, $info='component') {
global $DB;
// Attempt to retrieve the contextid
$context = $DB->get_record('backup_ids_temp', array('backupid' => $converterid,
'itemid' => $itemid));
if($context) {
return $context->id;
}
$context = new stdClass;
$context->itemid = $itemid;
$context->backupid = $converterid;
$context->itemname = 'context';
$context->info = $info;
if($id = $DB->insert('backup_ids_temp', $context)) {
return $id;
} else {
$msg = self::obj_to_readable($context);
throw new Exception(sprintf("Could not insert context record into temp table: %s", $msg));
}
}
}