PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/platform/dokeos185/data_class/dokeos185_dropbox_file.class.php

https://bitbucket.org/chamilo/chamilo-migration-dev/
PHP | 394 lines | 224 code | 61 blank | 109 comment | 13 complexity | f3baec18e29255fda04dbbf2884ec5cf MD5 | raw file
  1. <?php
  2. namespace migration;
  3. use repository\content_object\document\Document;
  4. use common\libraries\Text;
  5. use common\libraries\Translation;
  6. use repository\RepositoryDataManager;
  7. use common\libraries\Utilities;
  8. use common\libraries\Path;
  9. use common\libraries\FileSystem;
  10. /**
  11. * $Id: dokeos185_dropbox_file.class.php 221 2009-11-13 14:36:41Z vanpouckesven $
  12. * @package migration.lib.platform.dokeos185
  13. */
  14. require_once dirname(__FILE__) . '/../dokeos185_course_data_migration_data_class.class.php';
  15. /**
  16. * This class presents a Dokeos185 dropbox_file
  17. *
  18. * @author Sven Vanpoucke
  19. */
  20. class Dokeos185DropboxFile extends Dokeos185CourseDataMigrationDataClass
  21. {
  22. private $directory;
  23. const CLASS_NAME = __CLASS__;
  24. const TABLE_NAME = 'dropbox_file';
  25. /**
  26. * Dokeos185DropboxFile properties
  27. */
  28. const PROPERTY_ID = 'id';
  29. const PROPERTY_UPLOADER_ID = 'uploader_id';
  30. const PROPERTY_FILENAME = 'filename';
  31. const PROPERTY_FILESIZE = 'filesize';
  32. const PROPERTY_TITLE = 'title';
  33. const PROPERTY_DESCRIPTION = 'description';
  34. const PROPERTY_AUTHOR = 'author';
  35. const PROPERTY_UPLOAD_DATE = 'upload_date';
  36. const PROPERTY_LAST_UPLOAD_DATE = 'last_upload_date';
  37. const PROPERTY_CAT_ID = 'cat_id';
  38. const PROPERTY_SESSION_ID = 'session_id';
  39. /**
  40. * Default properties stored in an associative array.
  41. */
  42. private $defaultProperties;
  43. /**
  44. * Creates a new Dokeos185DropboxFile object
  45. * @param array $defaultProperties The default properties
  46. */
  47. function __construct($defaultProperties = array())
  48. {
  49. $this->defaultProperties = $defaultProperties;
  50. }
  51. /**
  52. * Gets a default property by name.
  53. * @param string $name The name of the property.
  54. */
  55. function get_default_property($name)
  56. {
  57. return $this->defaultProperties[$name];
  58. }
  59. /**
  60. * Gets the default properties
  61. * @return array An associative array containing the properties.
  62. */
  63. function get_default_properties()
  64. {
  65. return $this->defaultProperties;
  66. }
  67. /**
  68. * Get the default properties
  69. * @return array The property names.
  70. */
  71. static function get_default_property_names()
  72. {
  73. return array(self :: PROPERTY_ID, self :: PROPERTY_UPLOADER_ID, self :: PROPERTY_FILENAME, self :: PROPERTY_FILESIZE, self :: PROPERTY_TITLE, self :: PROPERTY_DESCRIPTION, self :: PROPERTY_AUTHOR, self :: PROPERTY_UPLOAD_DATE, self :: PROPERTY_LAST_UPLOAD_DATE, self :: PROPERTY_CAT_ID, self :: PROPERTY_SESSION_ID);
  74. }
  75. /**
  76. * Sets a default property by name.
  77. * @param string $name The name of the property.
  78. * @param mixed $value The new value for the property.
  79. */
  80. function set_default_property($name, $value)
  81. {
  82. $this->defaultProperties[$name] = $value;
  83. }
  84. /**
  85. * Sets the default properties of this class
  86. */
  87. function set_default_properties($defaultProperties)
  88. {
  89. $this->defaultProperties = $defaultProperties;
  90. }
  91. /**
  92. * Returns the id of this Dokeos185DropboxFile.
  93. * @return the id.
  94. */
  95. function get_id()
  96. {
  97. return $this->get_default_property(self :: PROPERTY_ID);
  98. }
  99. /**
  100. * Returns the uploader_id of this Dokeos185DropboxFile.
  101. * @return the uploader_id.
  102. */
  103. function get_uploader_id()
  104. {
  105. return $this->get_default_property(self :: PROPERTY_UPLOADER_ID);
  106. }
  107. /**
  108. * Returns the filename of this Dokeos185DropboxFile.
  109. * @return the filename.
  110. */
  111. function get_filename()
  112. {
  113. return $this->get_default_property(self :: PROPERTY_FILENAME);
  114. }
  115. /**
  116. * Returns the filesize of this Dokeos185DropboxFile.
  117. * @return the filesize.
  118. */
  119. function get_filesize()
  120. {
  121. return $this->get_default_property(self :: PROPERTY_FILESIZE);
  122. }
  123. /**
  124. * Returns the title of this Dokeos185DropboxFile.
  125. * @return the title.
  126. */
  127. function get_title()
  128. {
  129. return $this->get_default_property(self :: PROPERTY_TITLE);
  130. }
  131. /**
  132. * Returns the description of this Dokeos185DropboxFile.
  133. * @return the description.
  134. */
  135. function get_description()
  136. {
  137. return $this->get_default_property(self :: PROPERTY_DESCRIPTION);
  138. }
  139. /**
  140. * Returns the author of this Dokeos185DropboxFile.
  141. * @return the author.
  142. */
  143. function get_author()
  144. {
  145. return $this->get_default_property(self :: PROPERTY_AUTHOR);
  146. }
  147. /**
  148. * Returns the upload_date of this Dokeos185DropboxFile.
  149. * @return the upload_date.
  150. */
  151. function get_upload_date()
  152. {
  153. return $this->get_default_property(self :: PROPERTY_UPLOAD_DATE);
  154. }
  155. /**
  156. * Returns the last_upload_date of this Dokeos185DropboxFile.
  157. * @return the last_upload_date.
  158. */
  159. function get_last_upload_date()
  160. {
  161. return $this->get_default_property(self :: PROPERTY_LAST_UPLOAD_DATE);
  162. }
  163. /**
  164. * Returns the cat_id of this Dokeos185DropboxFile.
  165. * @return the cat_id.
  166. */
  167. function get_cat_id()
  168. {
  169. return $this->get_default_property(self :: PROPERTY_CAT_ID);
  170. }
  171. /**
  172. * Returns the session_id of this Dokeos185DropboxFile.
  173. * @return the session_id.
  174. */
  175. function get_session_id()
  176. {
  177. return $this->get_default_property(self :: PROPERTY_SESSION_ID);
  178. }
  179. /**
  180. * Check if the dropboxfile is valid
  181. * @param array $courses the parameters for the validation
  182. */
  183. function is_valid()
  184. {
  185. $course = $this->get_course();
  186. $this->set_item_property($this->get_data_manager()->get_item_property($course, 'dropbox', $this->get_id()));
  187. $filename = $this->get_filename();
  188. $old_rel_path = 'courses/' . $course->get_directory() . '/dropbox/';
  189. //$filename = iconv("UTF-8", "ISO-8859-1", $filename);
  190. //$old_rel_path = iconv("UTF-8", "ISO-8859-1", $old_rel_path);
  191. $this->directory = $this->get_data_manager()->get_sys_path() . $old_rel_path;
  192. if (!$this->get_id())
  193. {
  194. //echo 'Error in ID at course : ' . $course->get_db_name() .' ';
  195. $this->create_failed_element($this->get_id());
  196. $this->set_message(Translation :: get('GeneralInvalidMessage', array('TYPE' => 'dropbox_file', 'ID' => $this->get_id())));
  197. return false;
  198. }
  199. else
  200. if (!$this->get_item_property())
  201. {
  202. //echo 'Error in property at course : ' . $course->get_db_name() .' ';
  203. $this->create_failed_element($this->get_id());
  204. $this->set_message(Translation :: get('GeneralInvalidMessage', array('TYPE' => 'dropbox_file', 'ID' => $this->get_id())));
  205. return false;
  206. }
  207. else
  208. if (!$this->get_item_property()->get_ref())
  209. {
  210. //echo 'Error in reference at course : ' . $course->get_db_name() .' ';
  211. $this->create_failed_element($this->get_id());
  212. $this->set_message(Translation :: get('GeneralInvalidMessage', array('TYPE' => 'dropbox_file', 'ID' => $this->get_id())));
  213. return false;
  214. }
  215. else
  216. if (!$this->get_item_property()->get_insert_date())
  217. {
  218. //echo 'Error in insert_date at course : ' . $course->get_db_name() .' ';
  219. $this->create_failed_element($this->get_id());
  220. $this->set_message(Translation :: get('GeneralInvalidMessage', array('TYPE' => 'dropbox_file', 'ID' => $this->get_id())));
  221. return false;
  222. }
  223. else
  224. if (!file_exists($this->directory . $filename))
  225. {
  226. //echo 'Error in full_path at course : ' . $course->get_db_name() .'ID : ' . $this->get_id();
  227. $this->create_failed_element($this->get_id());
  228. $this->set_message(Translation :: get('GeneralInvalidMessage', array('TYPE' => 'dropbox_file', 'ID' => $this->get_id())));
  229. return false;
  230. }
  231. return true;
  232. }
  233. /**
  234. * Convert to new dropbox file
  235. * @param array $array the parameters for the conversion
  236. * @return the new dropbox file
  237. */
  238. function convert_data()
  239. {
  240. if ($this->get_uploader_id())
  241. {
  242. $new_user_id = $this->get_id_reference($this->get_uploader_id(), 'main_database.user');
  243. }
  244. else
  245. {
  246. $new_user_id = $this->get_id_reference($this->get_item_property()->get_insert_user_id(), 'main_database.user');
  247. }
  248. $course = $this->get_course();
  249. $new_course_code = $this->get_id_reference($course->get_code(), 'main_database.course');
  250. //$new_to_group_id[] = $this->get_id_reference($this->get_item_property()->get_to_group_id(), $this->get_database_name() . '.group_info');
  251. //$new_to_user_id[] = $this->get_id_reference($this->get_item_property()->get_to_user_id(), 'main_database.user');
  252. if (!$new_user_id)
  253. {
  254. $new_user_id = $this->get_data_manager()->get_owner_id($new_course_code);
  255. }
  256. $new_path = $new_user_id . '/';
  257. $old_rel_path = 'courses/' . $course->get_directory() . '/dropbox/';
  258. $new_rel_path = 'files/repository/' . $new_path;
  259. //$filename_split = split('/', $this->get_);
  260. $original_filename = $this->get_title(); //get_file_name returns a hash
  261. $base_hash = md5($original_filename);
  262. $new_path = Path :: get(SYS_REPO_PATH) . $new_user_id . '/' . Text :: char_at($base_hash, 0) . '/';
  263. $unique_hash = FileSystem :: create_unique_name($new_path, $base_hash);
  264. //$directory = iconv("UTF-8", "ISO-8859-1", $this->directory);
  265. //$filename = iconv("UTF-8", "ISO-8859-1", $this->get_filename());
  266. $directory = $this->directory;
  267. $filename = $this->get_filename();
  268. $hash_filename = $this->migrate_file($directory, $new_path, $filename, $unique_hash);
  269. if ($hash_filename)
  270. {
  271. //Create document in repository
  272. $chamilo_repository_document = new Document();
  273. $chamilo_repository_document->set_filename($original_filename);
  274. $chamilo_repository_document->set_path($new_user_id . '/' . Text :: char_at($base_hash, 0) . '/' . $unique_hash); //!!!!!!!
  275. $chamilo_repository_document->set_filesize($this->get_filesize());
  276. $chamilo_repository_document->set_hash($unique_hash);
  277. if ($this->get_title())
  278. {
  279. $chamilo_repository_document->set_title($this->get_title());
  280. }
  281. else
  282. {
  283. $chamilo_repository_document->set_title($original_filename);
  284. }
  285. if($this->get_description())
  286. {
  287. $chamilo_repository_document->set_description($this->get_description());
  288. }
  289. else
  290. {
  291. $chamilo_repository_document->set_description($this->get_title());
  292. }
  293. $chamilo_repository_document->set_owner_id($new_user_id);
  294. $chamilo_repository_document->set_creation_date(strtotime($this->get_item_property()->get_insert_date()));
  295. $chamilo_repository_document->set_modification_date(strtotime($this->get_item_property()->get_lastedit_date()));
  296. $repository_category_course_id = $this->get_repository_migration_course_category_id($new_user_id);
  297. $chamilo_category_id = RepositoryDataManager :: get_repository_category_by_name_or_create_new($new_user_id, Translation :: get('Documents'), $repository_category_course_id);
  298. $chamilo_repository_document->set_parent_id($chamilo_category_id);
  299. if ($this->get_item_property()->get_visibility() == 2)
  300. {
  301. $chamilo_repository_document->set_state(1);
  302. }
  303. //Create document in db
  304. $chamilo_repository_document->create();
  305. //Add id references to migration table
  306. $this->create_id_reference($this->get_id(), $chamilo_repository_document->get_id());
  307. $target_users = array();
  308. // Retrieve all the connections to the different quizzes and convert them because we need to store ponderation and position as well
  309. $dropbox_persons = $this->get_data_manager()->retrieve_dropbox_persons($course, $this->get_id());
  310. while($dropbox_person = $dropbox_persons->next_result())
  311. {
  312. $user_id = $dropbox_person->get_user_id();
  313. $new_user_id = $this->get_id_reference($this->get_item_property()->get_insert_user_id(), 'main_database.user');
  314. if($new_user_id)
  315. {
  316. $target_users[] = $new_user_id;
  317. }
  318. }
  319. //publication
  320. $this->create_publication($chamilo_repository_document, $new_course_code, $new_user_id, 'document', null, $target_users);
  321. $this->set_message(Translation :: get('GeneralConvertedMessage', array('TYPE' => 'dropbox_file', 'OLD_ID' => $this->get_id(), 'NEW_ID' => $chamilo_repository_document->get_id())));
  322. }
  323. }
  324. static function get_table_name()
  325. {
  326. return Utilities :: camelcase_to_underscores(substr(Utilities :: get_classname_from_namespace(__CLASS__), 9)); ;
  327. }
  328. static function get_class_name()
  329. {
  330. return self :: CLASS_NAME;
  331. }
  332. }
  333. ?>