PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/php/lib/tool/component/rights_editor.class.php

https://bitbucket.org/chamilo/chamilo-app-weblcms-dev/
PHP | 316 lines | 241 code | 52 blank | 23 comment | 22 complexity | 7b7299f2ca82292b830067baa15f4c61 MD5 | raw file
  1. <?php
  2. namespace application\weblcms;
  3. use common\libraries\Application;
  4. use common\libraries\BreadcrumbTrail;
  5. use common\libraries\Request;
  6. use common\libraries\Translation;
  7. use common\libraries\Utilities;
  8. use common\libraries\Display;
  9. use common\libraries\EqualityCondition;
  10. use common\libraries\AndCondition;
  11. use common\libraries\InequalityCondition;
  12. use group\Group;
  13. use group\GroupDataManager;
  14. use group\GroupRelUser;
  15. use rights\TemplateEntity;
  16. use rights\RightsManager;
  17. use common\extensions\new_rights_editor_manager\RightsEditorManager;
  18. /**
  19. * $Id: reporting_viewer.class.php 216 2009-11-13 14:08:06Z kariboe $
  20. * @package application.lib.weblcms.tool.component
  21. */
  22. /**
  23. * Description of reporting_template_viewerclass
  24. *
  25. * @author Sven Vanpoucke
  26. */
  27. class ToolComponentRightsEditorComponent extends ToolComponent
  28. {
  29. const ADDITIONAL_INFORMATION_OBJECT_SEPARATOR = ' | '; // use <br /> to put each object on a different line
  30. const LOCATION_TYPE_OBJECT = 'Objects';
  31. const LOCATION_TYPE_LOCATIONS = 'Locations';
  32. /**
  33. * Runs this component and displays its output.
  34. */
  35. function run()
  36. {
  37. $course = $this->get_course();
  38. if (!$course->is_course_admin($this->get_user()) && !$this->get_user()->is_platform_admin())
  39. {
  40. $this->display_header();
  41. Display :: error_message(Translation :: get('NotAllowed', null, Utilities:: COMMON_LIBRARIES));
  42. $this->display_footer();
  43. exit();
  44. }
  45. $trail = BreadcrumbTrail :: get_instance();
  46. $location = $this->get_locations();
  47. RightsEditorManager :: launch($this, WeblcmsManager :: APPLICATION_NAME, $location, $this->get_entities());
  48. }
  49. function get_available_rights($location)
  50. {
  51. return $this->get_parent()->get_available_rights($location);
  52. }
  53. function get_additional_information()
  54. {
  55. $publication_ids = Request :: get(WeblcmsManager :: PARAM_PUBLICATION);
  56. if(! is_array($publication_ids))
  57. {
  58. $publication_ids = array($publication_ids);
  59. }
  60. if(count($publication_ids) > 0 && isset($publication_ids[0]))
  61. {
  62. $type = self :: LOCATION_TYPE_OBJECT;
  63. }
  64. else
  65. {
  66. $type = self :: LOCATION_TYPE_LOCATIONS;
  67. }
  68. $info = array();
  69. $translation_context = Application :: determine_namespace(RightsManager :: APPLICATION_NAME);
  70. $info[] = Translation :: get("YouAreEditingRightsFor", array("TYPE" => Translation :: get($type, null, $translation_context)), $translation_context);
  71. $info[] = '<br/>';
  72. switch($type)
  73. {
  74. case self :: LOCATION_TYPE_OBJECT :
  75. $publication_ids = Request :: get(WeblcmsManager :: PARAM_PUBLICATION);
  76. if(! is_array($publication_ids))
  77. {
  78. $publication_ids = array($publication_ids);
  79. }
  80. foreach($publication_ids as $publication_id)
  81. {
  82. $publication = WeblcmsDataManager :: get_instance()->retrieve_content_object_publication($publication_id);
  83. if($publication)
  84. {
  85. $content_object = $publication->get_content_object();
  86. if($content_object)
  87. {
  88. $info[] = '<a href="' . $this->get_publication_rights_editor_url($publication_id) . '">';
  89. $info[] = $content_object->get_title();
  90. $info[] = '</a>';
  91. $info[] = self :: ADDITIONAL_INFORMATION_OBJECT_SEPARATOR;
  92. }
  93. }
  94. }
  95. break;
  96. case self :: LOCATION_TYPE_LOCATIONS :
  97. // course link
  98. $course = WeblcmsDataManager :: get_instance()->retrieve_course(Request :: get(WeblcmsManager :: PARAM_COURSE));
  99. if($course)
  100. {
  101. $info[] = '<a href="' . $this->get_course_rights_editor_url() . '">';
  102. $info[] = $course->get_name();
  103. $info[] = '</a>';
  104. $info[] .= ' > ';
  105. }
  106. // tool link (only displayed if tool is not the Rights tool)
  107. $tool = Translation :: get(Utilities :: underscores_to_camelcase(Request :: get(WeblcmsManager :: PARAM_TOOL)));
  108. if($tool && strtolower($tool) != strtolower(Tool :: class_to_type(RightsTool)))
  109. {
  110. $info[] = '<a href="' . $this->get_tool_rights_editor_url() . '">';
  111. $info[] .= $tool;
  112. $info[] = '</a>';
  113. $info[] .= ' > ';
  114. // categories
  115. $category_id = Request :: get(WeblcmsManager :: PARAM_CATEGORY);
  116. if($category_id)
  117. {
  118. // get the given category
  119. $category = WeblcmsDataManager :: get_instance()->retrieve_content_object_publication_category($category_id);
  120. if($category)
  121. {
  122. // shift in the parents -> build categories in reverse order, from bottom to root
  123. $index = count($info);
  124. while($category)
  125. {
  126. // construct the single link
  127. $category_link = array();
  128. $category_link[] = '<a href="' . $this->get_category_rights_editor_url($category->get_id()) . '">';
  129. $category_link[] = $category->get_name();
  130. $category_link[] = '</a>';
  131. $category_link[] = ' > ';
  132. // shift the link into the info array after document
  133. array_splice($info, $index, 0, $category_link);
  134. // parent
  135. $category = WeblcmsDataManager :: get_instance()->retrieve_content_object_publication_category($category->get_parent());
  136. }
  137. }
  138. }
  139. }
  140. break;
  141. }
  142. //remove last separator, or '<br/>' if no objects were found
  143. array_pop($info);
  144. return implode("\n", $info);
  145. }
  146. function add_category_string($category, $array)
  147. {
  148. if($category)
  149. {
  150. $info[] = '<a href="' . $this->get_category_rights_editor_url($category->get_id()) . '">';
  151. $array[] .= $category->get_name();
  152. $info[] = '</a>';
  153. $array[] .= ' > ';
  154. }
  155. }
  156. function get_additional_parameters()
  157. {
  158. return array(WeblcmsManager :: PARAM_PUBLICATION, WeblcmsManager :: PARAM_CATEGORY);
  159. }
  160. function get_entities()
  161. {
  162. $wdm = WeblcmsDataManager::get_instance();
  163. $relation_condition = new EqualityCondition(CourseGroupRelation :: PROPERTY_COURSE_ID, $this->get_course_id());
  164. $platform_group_relations = $wdm->retrieve_course_group_relations($relation_condition);
  165. $limited_users = array();
  166. while ($platform_group_relation = $platform_group_relations->next_result())
  167. {
  168. $platform_group_id = $platform_group_relation->get_group_id();
  169. $subscribed_platform_group_ids[] = $platform_group_id;
  170. $limited_platform_groups[] = $platform_group_id;
  171. //get the users in that platform group
  172. $eq_condition = new EqualityCondition(GroupRelUser :: PROPERTY_GROUP_ID, $platform_group_id);
  173. $group_rel_users_dataset = GroupDataManager :: get_instance()->retrieve_group_rel_users($eq_condition);
  174. while ($group_rel_user = $group_rel_users_dataset->next_result())
  175. {
  176. $limited_users[] = $group_rel_user->get_user_id();
  177. }
  178. $group = GroupDataManager :: get_instance()->retrieve_group($platform_group_id);
  179. if(!$group)
  180. {
  181. continue;
  182. }
  183. $children_conditions = array();
  184. $children_conditions[] = new InequalityCondition(Group :: PROPERTY_LEFT_VALUE,
  185. InequalityCondition :: GREATER_THAN, $group->get_left_value());
  186. $children_conditions[] = new InequalityCondition(Group :: PROPERTY_RIGHT_VALUE,
  187. InequalityCondition :: LESS_THAN, $group->get_right_value());
  188. $children_condition = new AndCondition($children_conditions);
  189. //retrieve the subgroups subscribed implicitly
  190. $child_groups = GroupDataManager :: get_instance()->retrieve_groups($children_condition);
  191. while ($cgroup = $child_groups->next_result())
  192. {
  193. $limited_platform_groups[] = $cgroup->get_id();
  194. $eq_condition = new EqualityCondition(GroupRelUser :: PROPERTY_GROUP_ID, $cgroup->get_id());
  195. $group_rel_users_dataset = GroupDataManager :: get_instance()->retrieve_group_rel_users($eq_condition);
  196. while ($group_rel_user = $group_rel_users_dataset->next_result())
  197. {
  198. $limited_users[] = $group_rel_user->get_user_id();
  199. }
  200. }
  201. }
  202. $conditions = array();
  203. $conditions[] = new EqualityCondition(CourseUserRelation :: PROPERTY_COURSE,
  204. $this->get_course_id());
  205. $conditions[] = new EqualityCondition(CourseUserRelation :: PROPERTY_STATUS,
  206. CourseUserRelation :: STATUS_STUDENT);
  207. $condition = new AndCondition($conditions);
  208. $relations = WeblcmsDataManager :: get_instance()->retrieve_course_user_relations($condition);
  209. while ($relation = $relations->next_result())
  210. {
  211. $limited_users[] = $relation->get_user();
  212. }
  213. $limited_users[] = $this->get_user_id();
  214. $excluded_users[] = $this->get_user_id();
  215. $limited_platform_groups[] = 1;
  216. $excluded_platform_groups[] = 1;
  217. $entities = array();
  218. $user_entity = new CourseUserEntity($this->get_course_id());
  219. $user_entity->exclude_users($excluded_users);
  220. $user_entity->limit_users($limited_users);
  221. $entities[CourseUserEntity :: ENTITY_TYPE] = $user_entity;
  222. $entities[CourseGroupEntity :: ENTITY_TYPE] = new CourseGroupEntity($this->get_course_id());
  223. if(!empty($subscribed_platform_group_ids))
  224. {
  225. $group_entity = new CoursePlatformGroupEntity($this->get_course_id());
  226. $group_entity->set_subscribed_platform_group_ids($subscribed_platform_group_ids);
  227. $group_entity->exclude_groups($excluded_platform_groups);
  228. $group_entity->limit_groups($limited_platform_groups);
  229. $entities[CoursePlatformGroupEntity :: ENTITY_TYPE] = $group_entity;
  230. }
  231. $entities[TemplateEntity :: ENTITY_TYPE] = new TemplateEntity();
  232. return $entities;
  233. }
  234. function get_course_rights_editor_url()
  235. {
  236. return $this->get_url(array(
  237. WeblcmsManager :: PARAM_TOOL => Tool :: class_to_type(RightsTool),
  238. Tool :: PARAM_ACTION => Tool :: ACTION_EDIT_RIGHTS,
  239. WeblcmsManager :: PARAM_CATEGORY => null
  240. ));
  241. }
  242. function get_tool_rights_editor_url()
  243. {
  244. return $this->get_url(array(
  245. Tool :: PARAM_ACTION => Tool :: ACTION_EDIT_RIGHTS,
  246. WeblcmsManager :: PARAM_CATEGORY => null
  247. ));
  248. }
  249. function get_category_rights_editor_url($category_id)
  250. {
  251. return $this->get_url(array(
  252. Tool :: PARAM_ACTION => Tool :: ACTION_EDIT_RIGHTS,
  253. WeblcmsManager :: PARAM_CATEGORY => $category_id
  254. ));
  255. }
  256. function get_publication_rights_editor_url($publication_id)
  257. {
  258. return $this->get_url(array(
  259. Tool :: PARAM_ACTION => Tool :: ACTION_EDIT_RIGHTS,
  260. WeblcmsManager :: PARAM_PUBLICATION => $publication_id
  261. ));
  262. }
  263. }
  264. ?>