Adding root and final tasks
[moodle.git] / backup / util / helper / convert_helper.class.php
CommitLineData
17252e2d
MN
1<?php
2/**
3 * General Convert Helper
4 */
5abstract class convert_helper {
6 public static function generate_id($entropy) {
7 return md5(time() . '-' . $entropy . '-' . random_string(20));
8 }
c5c8b350
MN
9
10 /**
11 * @static
12 * @throws coding_exception|restore_controller_exception
13 * @param string $tempdir The directory to convert
14 * @param string $format The current format, if already detected
15 * @return void
16 */
17 public static function to_moodle2_format($tempdir, $format = NULL) {
18 if (is_null($format)) {
19 $format = backup_general_helper::detect_backup_format($tempdir);
20 }
21 while (!in_array($format, array(backup::FORMAT_MOODLE, backup::FORMAT_UNKNOWN))) {
22 $converter = convert_factory::converter($format, $tempdir);
23
24 if (!$converter->can_convert()) {
25 throw new coding_exception('Converter detection failed, the loaded converter cannot convert this format');
26 }
27 $converter->convert();
28
29 // Re-detect format
30 $format = backup_general_helper::detect_backup_format($tempdir);
31 }
32 if ($format == backup::FORMAT_UNKNOWN) {
33 throw new restore_controller_exception('cannot_convert_from_unknown_format'); // @todo Change exception class
34 }
35 }
17252e2d 36}