PageRenderTime 21ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/repository_manager/component/mover.class.php

https://bitbucket.org/chamilo/chamilo-repository-dev/
PHP | 180 lines | 139 code | 19 blank | 22 comment | 17 complexity | 81d9cfb59e1e515fc507a1db4dc39564 MD5 | raw file
  1. <?php
  2. namespace repository;
  3. use common\libraries\Request;
  4. use common\libraries\FormValidator;
  5. use common\libraries\Translation;
  6. use common\libraries\Breadcrumb;
  7. use common\libraries\BreadcrumbTrail;
  8. use common\libraries\EqualityCondition;
  9. use common\libraries\Application;
  10. use common\libraries\NotCondition;
  11. use common\libraries\AndCondition;
  12. use common\libraries\Utilities;
  13. use common\extensions\category_manager\PlatformCategory;
  14. /**
  15. * $Id: mover.class.php 204 2009-11-13 12:51:30Z kariboe $
  16. * @package repository.lib.repository_manager.component
  17. */
  18. /**
  19. * Repository manager component to move learning objects between categories in
  20. * the repository.
  21. */
  22. class RepositoryManagerMoverComponent extends RepositoryManager
  23. {
  24. /**
  25. * Runs this component and displays its output.
  26. */
  27. function run()
  28. {
  29. $ids = Request :: get(RepositoryManager :: PARAM_CONTENT_OBJECT_ID);
  30. if (! empty($ids))
  31. {
  32. if (! is_array($ids))
  33. {
  34. $ids = array($ids);
  35. }
  36. $object = $this->retrieve_content_object($ids[0]);
  37. $parent = $object->get_parent_id();
  38. $this->tree = array();
  39. if ($parent != 0)
  40. $this->tree[] = Translation :: get('Repository');
  41. $this->get_categories_for_select(0, $parent);
  42. $form = new FormValidator('move', 'post', $this->get_url(array(
  43. RepositoryManager :: PARAM_CONTENT_OBJECT_ID => $ids)));
  44. $form->addElement('select', RepositoryManager :: PARAM_DESTINATION_CONTENT_OBJECT_ID, Translation :: get('NewCategory'), $this->tree);
  45. $form->addElement('submit', 'submit', Translation :: get('Move', null, Utilities :: COMMON_LIBRARIES));
  46. if ($form->validate())
  47. {
  48. $destination = $form->exportValue(RepositoryManager :: PARAM_DESTINATION_CONTENT_OBJECT_ID);
  49. $failures = 0;
  50. foreach ($ids as $id)
  51. {
  52. $object = $this->retrieve_content_object($id);
  53. $versions = $this->get_version_ids($object);
  54. foreach ($versions as $version)
  55. {
  56. $object = $this->retrieve_content_object($version);
  57. // TODO: Roles & Rights.
  58. if ($object->get_owner_id() != $this->get_user_id())
  59. {
  60. $failures ++;
  61. }
  62. elseif ($object->get_parent_id() != $destination)
  63. {
  64. if (! $object->move_allowed($destination))
  65. {
  66. $failures ++;
  67. }
  68. else
  69. {
  70. $object->move($destination);
  71. }
  72. }
  73. }
  74. }
  75. // TODO: SCARA - Correctto reflect possible version errors
  76. if ($failures)
  77. {
  78. if (count($ids) == 1)
  79. {
  80. $message = Translation :: get('ObjectNotMoved', array(
  81. 'OBJECT' => Translation :: get('ContentObject')), Utilities :: COMMON_LIBRARIES);
  82. }
  83. else
  84. {
  85. $message = Translation :: get('ObjectsNotMoved', array(
  86. 'OBJECTS' => Translation :: get('ContentObjects')), Utilities :: COMMON_LIBRARIES);
  87. }
  88. }
  89. else
  90. {
  91. if (count($ids) == 1)
  92. {
  93. $message = Translation :: get('ObjectMoved', array(
  94. 'OBJECT' => Translation :: get('ContentObject')), Utilities :: COMMON_LIBRARIES);
  95. }
  96. else
  97. {
  98. $message = Translation :: get('ObjectsMoved', array(
  99. 'OBJECTS' => Translation :: get('ContentObjects')), Utilities :: COMMON_LIBRARIES);
  100. }
  101. }
  102. $parameters = array();
  103. $parameters[Application :: PARAM_ACTION] = RepositoryManager :: ACTION_BROWSE_CONTENT_OBJECTS;
  104. $parameters[RepositoryManager :: PARAM_CATEGORY_ID] = $object->get_parent_id();
  105. $this->redirect($message, ($failures ? true : false), $parameters);
  106. }
  107. else
  108. {
  109. $this->display_header(null, false, true);
  110. echo $form->toHTML();
  111. $this->display_footer();
  112. }
  113. }
  114. else
  115. {
  116. $this->display_error_page(htmlentities(Translation :: get('NoObjectSelected', array(
  117. 'OBJECT' => Translation :: get('ContentObject')), Utilities :: COMMON_LIBRARIES)));
  118. }
  119. }
  120. /**
  121. * Get all categories from which a user can select a target category when
  122. * moving learning objects.
  123. * @param array $exclude An array of category-id's which should be excluded
  124. * from the resulting list.
  125. * @return array A list of possible categories from which a user can choose.
  126. * Can be used as input for a QuickForm select field.
  127. */
  128. private $level = 1;
  129. private $tree = array();
  130. private function get_categories_for_select($parent_id, $current_parent)
  131. {
  132. $conditions[] = new EqualityCondition(PlatformCategory :: PROPERTY_PARENT, $parent_id);
  133. $conditions[] = new EqualityCondition(RepositoryCategory :: PROPERTY_USER_ID, $this->get_user_id());
  134. //$conditions[] = new NotCondition(new EqualityCondition(PlatformCategory :: PROPERTY_ID, $current_parent));
  135. $condition = new AndCondition($conditions);
  136. $categories = $this->retrieve_categories($condition);
  137. $tree = array();
  138. while ($cat = $categories->next_result())
  139. {
  140. $this->tree[$cat->get_id()] = str_repeat('--', $this->level) . ' ' . $cat->get_name();
  141. if ($current_parent == $cat->get_id())
  142. {
  143. $this->tree[$cat->get_id()] .= ' (' . Translation :: get('Current', null, Utilities :: COMMON_LIBRARIES) . ')';
  144. }
  145. $this->level ++;
  146. $this->get_categories_for_select($cat->get_id(), $current_parent);
  147. $this->level --;
  148. }
  149. }
  150. function add_additional_breadcrumbs(BreadcrumbTrail $breadcrumbtrail)
  151. {
  152. $breadcrumbtrail->add(new Breadcrumb($this->get_url(array(
  153. RepositoryManager :: PARAM_ACTION => RepositoryManager :: ACTION_BROWSE_CONTENT_OBJECTS)), Translation :: get('RepositoryManagerBrowserComponent')));
  154. $breadcrumbtrail->add_help('repository_mover');
  155. }
  156. function get_additional_parameters()
  157. {
  158. return array(RepositoryManager :: PARAM_CONTENT_OBJECT_ID);
  159. }
  160. }
  161. ?>