-
Notifications
You must be signed in to change notification settings - Fork 1
/
import_form.php
110 lines (94 loc) · 3.48 KB
/
import_form.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* block/taskchain_navigation/import_form.php
*
* @package block
* @subpackage taskchain_navigation
* @copyright 2014 Gordon Bateson (gordon.bateson@gmail.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
/** Prevent direct access to this script */
defined('MOODLE_INTERNAL') || die();
/** Include required files */
require_once($CFG->dirroot.'/lib/formslib.php');
/**
* block_taskchain_navigation_import_form
*
* @package tool
* @subpackage taskchain_navigation
* @copyright 2014 Gordon Bateson (gordon.bateson@gmail.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class block_taskchain_navigation_import_form extends moodleform {
/**
* constructor
*/
public function __construct($action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true) {
if (method_exists('moodleform', '__construct')) {
parent::__construct($action, $customdata, $method, $target, $attributes, $editable);
} else {
parent::moodleform($action, $customdata, $method, $target, $attributes, $editable);
}
}
/**
* definition
*/
public function definition() {
$this->_form->addElement('filepicker', 'importfile', get_string('file'));
$this->add_action_buttons(true, get_string('import'));
}
/**
* import
*
* @param string $xml
* @param object $block_instance
* @param object $course
* @return boolean true if import was successful, false otherwise
*/
public function import($xml, $block_instance, $course) {
global $DB;
if (! $xml = xmlize($xml, 0)) {
return false;
}
// set main XML tag name for this block's config settings
$BLOCK = strtoupper($block_instance->blockname);
$BLOCK = strtr($BLOCK, array('_' => '')).'BLOCK';
if (! isset($xml[$BLOCK]['#']['CONFIGFIELDS'][0]['#']['CONFIGFIELD'])) {
return false;
}
$configfield = &$xml[$BLOCK]['#']['CONFIGFIELDS'][0]['#']['CONFIGFIELD'];
$config = unserialize(base64_decode($block_instance->configdata));
if (empty($config)) {
$config = new stdClass();
}
$i = 0;
while (isset($configfield[$i]['#'])) {
$name = $configfield[$i]['#']['NAME'][0]['#'];
$value = $configfield[$i]['#']['VALUE'][0]['#'];
$config->$name = $value;
$i++;
}
if ($i==0) {
return false;
}
$block_instance->configdata = base64_encode(serialize($config));
$DB->set_field('block_instances', 'configdata', $block_instance->configdata, array('id' => $block_instance->id));
return true;
}
}