Resource.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. namespace Coursecopy;
  4. define('RESOURCE_DOCUMENT', 'document');
  5. define('RESOURCE_GLOSSARY', 'glossary');
  6. define('RESOURCE_EVENT', 'calendar_event');
  7. define('RESOURCE_LINK', 'link');
  8. define('RESOURCE_COURSEDESCRIPTION', 'course_description');
  9. define('RESOURCE_LEARNPATH', 'learnpath');
  10. define('RESOURCE_ANNOUNCEMENT', 'announcement');
  11. define('RESOURCE_FORUM', 'forum');
  12. define('RESOURCE_FORUMTOPIC', 'thread');
  13. define('RESOURCE_FORUMPOST', 'post');
  14. define('RESOURCE_QUIZ', 'quiz');
  15. define('RESOURCE_TEST_CATEGORY', 'test_category');
  16. define('RESOURCE_QUIZQUESTION', 'Exercise_Question');
  17. define('RESOURCE_TOOL_INTRO', 'Tool introduction');
  18. define('RESOURCE_LINKCATEGORY', 'Link_Category');
  19. define('RESOURCE_FORUMCATEGORY', 'Forum_Category');
  20. define('RESOURCE_SCORM', 'Scorm');
  21. define('RESOURCE_SURVEY', 'survey');
  22. define('RESOURCE_SURVEYQUESTION', 'survey_question');
  23. define('RESOURCE_SURVEYINVITATION', 'survey_invitation');
  24. define('RESOURCE_WIKI', 'wiki');
  25. define('RESOURCE_THEMATIC', 'thematic');
  26. define('RESOURCE_ATTENDANCE', 'attendance');
  27. define('RESOURCE_WORK', 'work');
  28. define('RESOURCE_SESSION_COURSE', 'session_course');
  29. define('RESOURCE_GRADEBOOK', 'gradebook');
  30. /**
  31. * Class Resource
  32. * Representation of a resource in a Chamilo-course.
  33. * This is a base class of which real resource-classes (for Links,
  34. * Documents,...) should be derived.
  35. * @author Bart Mollet <bart.mollet@hogent.be>s
  36. * @package chamilo.backup
  37. * @todo Use the globally defined constants voor tools and remove the RESOURCE_*
  38. * constants
  39. */
  40. class Resource
  41. {
  42. /**
  43. * The id from this resource in the source course
  44. */
  45. public $source_id;
  46. /**
  47. * The id from this resource in the destination course
  48. */
  49. public $destination_id;
  50. /**
  51. * The type of this resource
  52. */
  53. public $type;
  54. /**
  55. * Linked resources
  56. */
  57. public $linked_resources;
  58. /**
  59. * The properties of this resource
  60. */
  61. public $item_properties;
  62. public $obj = null;
  63. /**
  64. * Create a new Resource
  65. * @param int $id The id of this resource in the source course.
  66. * @param integer $type The type of this resource.
  67. */
  68. public function __construct($id, $type)
  69. {
  70. $this->source_id = $id;
  71. $this->type = $type;
  72. $this->destination_id = -1;
  73. $this->linked_resources = array();
  74. $this->item_properties = array();
  75. }
  76. /**
  77. * Add linked resource
  78. */
  79. public function add_linked_resource($type, $id)
  80. {
  81. $this->linked_resources[$type][] = $id;
  82. }
  83. /**
  84. * Get linked resources
  85. */
  86. public function get_linked_resources()
  87. {
  88. return $this->linked_resources;
  89. }
  90. /**
  91. * Checks if this resource links to a given resource
  92. */
  93. public function links_to(& $resource)
  94. {
  95. self::setClassType($resource);
  96. $type = $resource->get_type();
  97. if (isset($this->linked_resources[$type]) &&
  98. is_array($this->linked_resources[$type])
  99. ) {
  100. return in_array(
  101. $resource->get_id(),
  102. $this->linked_resources[$type]
  103. );
  104. }
  105. return false;
  106. }
  107. /**
  108. * Returns the id of this resource.
  109. * @return int The id of this resource in the source course.
  110. */
  111. public function get_id()
  112. {
  113. return $this->source_id;
  114. }
  115. /**
  116. * Resturns the type of this resource
  117. * @return integer The type.
  118. */
  119. public function get_type()
  120. {
  121. return $this->type;
  122. }
  123. /**
  124. * Get the constant which defines the tool of this resource. This is
  125. * used in the item_properties table.
  126. * @param bool $for_item_property_table (optional) Added by Ivan,
  127. * 29-AUG-2009: A parameter for resolving differencies between defined TOOL_*
  128. * constants and hardcoded strings that are stored in the database.
  129. * Example: The constant TOOL_THREAD is defined in the main_api.lib.php
  130. * with the value 'thread', but the "Forums" tool records in the field 'tool'
  131. * in the item property table the hardcoded value 'forum_thread'.
  132. * @todo once the RESOURCE_* constants are replaced by the globally
  133. * defined TOOL_* constants, this function will be replaced by get_type()
  134. */
  135. public function get_tool($for_item_property_table = true)
  136. {
  137. switch ($this->get_type()) {
  138. case RESOURCE_DOCUMENT:
  139. return TOOL_DOCUMENT;
  140. case RESOURCE_LINK:
  141. return TOOL_LINK;
  142. case RESOURCE_EVENT:
  143. return TOOL_CALENDAR_EVENT;
  144. case RESOURCE_COURSEDESCRIPTION:
  145. return TOOL_COURSE_DESCRIPTION;
  146. case RESOURCE_LEARNPATH:
  147. return TOOL_LEARNPATH;
  148. case RESOURCE_ANNOUNCEMENT:
  149. return TOOL_ANNOUNCEMENT;
  150. case RESOURCE_FORUMCATEGORY:
  151. return 'forum_category'; // Ivan, 29-AUG-2009: A constant like TOOL_FORUM_CATEGORY is missing in main_api.lib.php. Such a constant has been defined in the forum tool for local needs.
  152. case RESOURCE_FORUM:
  153. return TOOL_FORUM;
  154. case RESOURCE_FORUMTOPIC:
  155. if ($for_item_property_table) {
  156. return 'forum_thread'; // Ivan, 29-AUG-2009: A hardcoded value that the "Forums" tool stores in the item property table.
  157. }
  158. return TOOL_THREAD;
  159. case RESOURCE_FORUMPOST:
  160. return TOOL_POST;
  161. case RESOURCE_QUIZ:
  162. return TOOL_QUIZ;
  163. case RESOURCE_TEST_CATEGORY:
  164. return TOOL_TEST_CATEGORY;
  165. //case RESOURCE_QUIZQUESTION: //no corresponding global constant
  166. // return TOOL_QUIZ_QUESTION;
  167. //case RESOURCE_TOOL_INTRO:
  168. // return TOOL_INTRO;
  169. //case RESOURCE_LINKCATEGORY:
  170. // return TOOL_LINK_CATEGORY;
  171. //case RESOURCE_SCORM:
  172. // return TOOL_SCORM_DOCUMENT;
  173. case RESOURCE_SURVEY:
  174. return TOOL_SURVEY;
  175. //case RESOURCE_SURVEYQUESTION:
  176. // return TOOL_SURVEY_QUESTION;
  177. //case RESOURCE_SURVEYINVITATION:
  178. // return TOOL_SURVEY_INVITATION;
  179. case RESOURCE_GLOSSARY:
  180. return TOOL_GLOSSARY;
  181. case RESOURCE_WIKI:
  182. return TOOL_WIKI;
  183. case RESOURCE_THEMATIC:
  184. return TOOL_COURSE_PROGRESS;
  185. case RESOURCE_ATTENDANCE:
  186. return TOOL_ATTENDANCE;
  187. case RESOURCE_WORK:
  188. return TOOL_STUDENTPUBLICATION;
  189. default:
  190. return null;
  191. }
  192. }
  193. /**
  194. * Set the destination id
  195. * @param int $id The id of this resource in the destination course.
  196. */
  197. public function set_new_id($id)
  198. {
  199. $this->destination_id = $id;
  200. }
  201. /**
  202. * Check if this resource is allready restored in the destination course.
  203. * @return bool true if allready restored (i.e. destination_id is set).
  204. */
  205. public function is_restored()
  206. {
  207. return $this->destination_id > -1;
  208. }
  209. /**
  210. * Show this resource
  211. */
  212. public function show()
  213. {
  214. //echo 'RESOURCE: '.$this->get_id().' '.$type[$this->get_type()].' ';
  215. }
  216. /**
  217. * Fix objects coming from 1.9.x to 1.10.x
  218. * Example class Event to CalendarEvent
  219. *
  220. * @param Resource $resource
  221. */
  222. public static function setClassType(&$resource)
  223. {
  224. $class = get_class($resource);
  225. switch ($class) {
  226. case 'Event':
  227. /** @var $resource \CalendarEvent */
  228. $newResource = new \CalendarEvent(
  229. $resource->source_id,
  230. $resource->title,
  231. $resource->content,
  232. $resource->start_date,
  233. $resource->end_date,
  234. $resource->attachment_path,
  235. $resource->attachment_filename,
  236. $resource->attachment_size,
  237. $resource->attachment_comment,
  238. $resource->all_day
  239. );
  240. $resource = $newResource;
  241. break;
  242. case 'CourseDescription':
  243. if (!method_exists($resource, 'show')) {
  244. $resource = (array) $resource;
  245. $newResource = new CourseDescription(
  246. isset($resource['id']) ? $resource['id'] : '',
  247. $resource['title'],
  248. $resource['content'],
  249. $resource['description_type']
  250. );
  251. $newResource->source_id = $resource['source_id'];
  252. $newResource->destination_id = $resource['source_id'];
  253. $newResource->linked_resources = $resource['source_id'];
  254. $newResource->item_properties = $resource['source_id'];
  255. $newResource->obj = $resource['obj'];
  256. $resource = $newResource;
  257. }
  258. break;
  259. }
  260. }
  261. }