Conditions | 12 |
Paths | 24 |
Total Lines | 125 |
Code Lines | 61 |
Lines | 26 |
Ratio | 20.8 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
45 | public function convert_document($file, $action_after_conversion = 'make_lp') |
||
46 | { |
||
47 | $_course = api_get_course_info(); |
||
48 | $this->file_name = pathinfo($file['name'], PATHINFO_FILENAME); |
||
49 | // Create the directory |
||
50 | $result = $this->generate_lp_folder($_course, $this->file_name); |
||
51 | |||
52 | // Create the directory |
||
53 | $this->base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'; |
||
54 | ///learning_path/ppt_dirname directory |
||
55 | $this->created_dir = substr($result['dir'], 0, strlen($result['dir']) -1); |
||
56 | $this->file_path = $this->created_dir.'/'.api_replace_dangerous_char($file['name']); |
||
57 | |||
58 | //var_dump($this->file_name, $this->file_path, $this->base_work_dir, $this->created_dir); |
||
59 | |||
60 | /* |
||
61 | * Original code |
||
62 | global $_course, $_user, $_configuration; |
||
63 | |||
64 | $this->file_name = (strrpos($file['name'], '.') > 0 ? substr($file['name'], 0, strrpos($file['name'], '.')) : $file['name']); |
||
65 | $this->file_name = api_replace_dangerous_char($this->file_name, 'strict'); |
||
66 | $this->file_name = strtolower($this->file_name); |
||
67 | |||
68 | $visio_dir = ($action_after_conversion == 'add_docs_to_visio') ? VIDEOCONF_UPLOAD_PATH : ''; |
||
69 | |||
70 | $this->file_path = $visio_dir.'/'.$this->file_name.'.'.pathinfo($file['name'], PATHINFO_EXTENSION); |
||
71 | |||
72 | $dir_name = $visio_dir.'/'.$this->file_name; |
||
73 | |||
74 | |||
75 | // Create the directory. |
||
76 | $this->base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'; |
||
77 | |||
78 | $this->created_dir = create_unexisting_directory($_course, $_user['user_id'], api_get_session_id(), 0, 0, $this->base_work_dir, $dir_name); |
||
79 | |||
80 | var_dump($this->file_name, $this->file_path, $this->base_work_dir, $this->created_dir); |
||
81 | |||
82 | */ |
||
83 | |||
84 | $ppt2lp_host = api_get_setting('service_ppt2lp', 'host'); |
||
85 | |||
86 | if ($ppt2lp_host == 'localhost') { |
||
87 | move_uploaded_file($file['tmp_name'], $this->base_work_dir.'/'.$this->file_path); |
||
88 | //var_dump( $this->base_work_dir.$this->created_dir.$this->file_path); |
||
89 | $perm = api_get_setting('permissions_for_new_files'); |
||
90 | |||
91 | View Code Duplication | if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php |
|
92 | $converter_path = str_replace('/', '\\', api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png'); |
||
93 | $class_path = $converter_path . ';' . $converter_path . '/jodconverter-2.2.2.jar;' . $converter_path . '/jodconverter-cli-2.2.2.jar'; |
||
94 | //$cmd = 'java -cp "'.$class_path.'" DokeosConverter'; |
||
95 | $cmd = 'java -Dfile.encoding=UTF-8 -cp "' . $class_path . '" DokeosConverter'; |
||
96 | } else { |
||
97 | $converter_path = api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png'; |
||
98 | //$class_path = '-cp .:jodconverter-2.2.1.jar:jodconverter-cli-2.2.1.jar'; |
||
99 | $class_path = ' -Dfile.encoding=UTF-8 -cp .:jodconverter-2.2.2.jar:jodconverter-cli-2.2.2.jar'; |
||
100 | $cmd = 'cd ' . $converter_path . ' && java ' . $class_path . ' DokeosConverter'; |
||
101 | } |
||
102 | |||
103 | $cmd .= ' -p ' . api_get_setting('service_ppt2lp', 'port'); |
||
104 | // Call to the function implemented by child. |
||
105 | $cmd .= $this->add_command_parameters(); |
||
106 | // To allow openoffice to manipulate docs. |
||
107 | @chmod($this->base_work_dir, 0777); |
||
108 | @chmod($this->base_work_dir.$this->created_dir, 0777); |
||
109 | @chmod($this->base_work_dir.$this->file_path, 0777); |
||
110 | |||
111 | $locale = $this->original_locale; // TODO: Improve it because we're not sure this locale is present everywhere. |
||
112 | putenv('LC_ALL=' . $locale); |
||
113 | |||
114 | $files = array(); |
||
115 | $return = 0; |
||
116 | $shell = exec($cmd, $files, $return); |
||
117 | |||
118 | View Code Duplication | if ($return != 0) { // If the java application returns an error code. |
|
|
|||
119 | switch ($return) { |
||
120 | // Can't connect to openoffice. |
||
121 | case 1: $this->error = get_lang('CannotConnectToOpenOffice'); |
||
122 | break; |
||
123 | // Conversion failed in openoffice. |
||
124 | case 2: $this->error = get_lang('OogieConversionFailed'); |
||
125 | break; |
||
126 | // Conversion can't be launch because command failed. |
||
127 | case 255: $this->error = get_lang('OogieUnknownError'); |
||
128 | break; |
||
129 | } |
||
130 | DocumentManager::delete_document($_course, $this->created_dir, $this->base_work_dir); |
||
131 | return false; |
||
132 | } |
||
133 | } else { |
||
134 | // get result from webservices |
||
135 | $result = $this->_get_remote_ppt2lp_files($file); |
||
136 | $result = unserialize($result); |
||
137 | // Save remote images to server |
||
138 | chmod($this->base_work_dir.$this->created_dir, api_get_permissions_for_new_directories()); |
||
139 | if (!empty($result['images'])) { |
||
140 | foreach ($result['images'] as $image => $img_data) { |
||
141 | $image_path = $this->base_work_dir.$this->created_dir; |
||
142 | @file_put_contents($image_path . '/' . $image, base64_decode($img_data)); |
||
143 | @chmod($image_path . '/' . $image, 0777); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | // files info |
||
148 | $files = $result['files']; |
||
149 | } |
||
150 | |||
151 | if (!empty($files)) { |
||
152 | // Create lp |
||
153 | $this->lp_id = learnpath::add_lp($_course['id'], $this->file_name, '', 'guess', 'manual'); |
||
154 | // make sure we have a course code available for later |
||
155 | $this->cc = $_course['id']; |
||
156 | |||
157 | // Call to the function implemented by child following action_after_conversion parameter. |
||
158 | switch ($action_after_conversion) { |
||
159 | case 'make_lp': |
||
160 | $this->make_lp($files); |
||
161 | break; |
||
162 | case 'add_docs_to_visio': |
||
163 | $this->add_docs_to_visio($files); |
||
164 | break; |
||
165 | } |
||
166 | chmod($this->base_work_dir, api_get_permissions_for_new_directories()); |
||
167 | } |
||
168 | return $this->first_item; |
||
169 | } |
||
170 | |||
352 |