Course.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'LinkCategory.class.php';
  4. require_once 'Announcement.class.php';
  5. require_once 'Event.class.php';
  6. /**
  7. * A course-object to use in Export/Import/Backup/Copy
  8. * @author Bart Mollet <bart.mollet@hogent.be>
  9. * @package chamilo.backup
  10. */
  11. class Course
  12. {
  13. public $resources;
  14. public $code;
  15. public $path;
  16. public $destination_path;
  17. public $destination_db;
  18. public $encoding;
  19. /**
  20. * Create a new Course-object
  21. */
  22. public function __construct()
  23. {
  24. $this->resources = array();
  25. $this->code = '';
  26. $this->path = '';
  27. $this->backup_path = '';
  28. $this->encoding = api_get_system_encoding();
  29. }
  30. /**
  31. * Check if a resource links to the given resource
  32. */
  33. public function is_linked_resource(& $resource_to_check)
  34. {
  35. foreach ($this->resources as $type => $resources) {
  36. if (is_array($resources)) {
  37. foreach ($resources as $resource) {
  38. if ($resource->links_to($resource_to_check) ) {
  39. return true;
  40. }
  41. if ($type == RESOURCE_LEARNPATH && get_class($resource)=='CourseCopyLearnpath') {
  42. if ($resource->has_item($resource_to_check)) {
  43. return true;
  44. }
  45. }
  46. }
  47. }
  48. }
  49. return false;
  50. }
  51. /**
  52. * Add a resource from a given type to this course
  53. */
  54. public function add_resource(& $resource)
  55. {
  56. $this->resources[$resource->get_type()][$resource->get_id()] = $resource;
  57. }
  58. /**
  59. * Does this course has resources?
  60. * @param int $resource_type Check if this course has resources of the
  61. * given type. If no type is given, check if course has resources of any
  62. * type.
  63. */
  64. public function has_resources($resource_type = null)
  65. {
  66. if( $resource_type != null) {
  67. return isset($this->resources[$resource_type]) && is_array($this->resources[$resource_type]) && (count(
  68. $this->resources[$resource_type]
  69. ) > 0);
  70. }
  71. return (count($this->resources) > 0);
  72. }
  73. /**
  74. * @inheritdoc
  75. */
  76. public function show()
  77. { /*
  78. echo '<pre>';
  79. print_r($this);
  80. echo '</pre>';*/
  81. }
  82. /**
  83. * Returns sample text based on the imported course content.
  84. * This sample text is to be used for course language or encoding detection if there is missing (meta)data in the archive.
  85. * @return string The resulting sample text extracted from some common resources' data fields.
  86. */
  87. public function get_sample_text()
  88. {
  89. $sample_text = '';
  90. foreach ($this->resources as $type => & $resources) {
  91. if (count($resources) > 0) {
  92. foreach ($resources as $id => & $resource) {
  93. $title = '';
  94. $description = '';
  95. switch ($type) {
  96. case RESOURCE_ANNOUNCEMENT:
  97. $title = $resource->title;
  98. $description = $resource->content;
  99. break;
  100. case RESOURCE_DOCUMENT:
  101. $title = $resource->title;
  102. $description = $resource->comment;
  103. break;
  104. case RESOURCE_EVENT:
  105. $title = $resource->title;
  106. $description = $resource->content;
  107. break;
  108. case RESOURCE_FORUM:
  109. $title = $resource->title;
  110. $description = $resource->description;
  111. break;
  112. case RESOURCE_FORUMCATEGORY:
  113. $title = $resource->title;
  114. $description = $resource->description;
  115. break;
  116. case RESOURCE_FORUMPOST:
  117. $title = $resource->title;
  118. $description = $resource->text;
  119. break;
  120. case RESOURCE_FORUMTOPIC:
  121. $title = $resource->title;
  122. break;
  123. case RESOURCE_GLOSSARY:
  124. $title = $resource->name;
  125. $description = $resource->description;
  126. break;
  127. case RESOURCE_LEARNPATH:
  128. $title = $resource->name;
  129. $description = $resource->description;
  130. break;
  131. case RESOURCE_LINK:
  132. $title = $resource->title;
  133. $description = $resource->description;
  134. break;
  135. case RESOURCE_LINKCATEGORY:
  136. $title = $resource->title;
  137. $description = $resource->description;
  138. break;
  139. case RESOURCE_QUIZ:
  140. $title = $resource->title;
  141. $description = $resource->description;
  142. break;
  143. case RESOURCE_TEST_CATEGORY:
  144. $title = $resource->title;
  145. $description = $resource->description;
  146. break;
  147. case RESOURCE_QUIZQUESTION:
  148. $title = $resource->question;
  149. $description = $resource->description;
  150. break;
  151. case RESOURCE_SCORM:
  152. $title = $resource->title;
  153. break;
  154. case RESOURCE_SURVEY:
  155. $title = $resource->title;
  156. $description = $resource->subtitle;
  157. break;
  158. case RESOURCE_SURVEYQUESTION:
  159. $title = $resource->survey_question;
  160. $description = $resource->survey_question_comment;
  161. break;
  162. case RESOURCE_TOOL_INTRO:
  163. $description = $resource->intro_text;
  164. break;
  165. case RESOURCE_WIKI:
  166. $title = $resource->title;
  167. $description = $resource->content;
  168. break;
  169. case RESOURCE_THEMATIC:
  170. $title = $resource->title;
  171. $description = $resource->content;
  172. break;
  173. case RESOURCE_ATTENDANCE:
  174. $title = $resource->params['name'];
  175. $description = $resource->params['description'];
  176. break;
  177. case RESOURCE_WORK:
  178. $title = $resource->title;
  179. $description = $resource->description;
  180. break;
  181. default:
  182. break;
  183. }
  184. $title = api_html_to_text($title);
  185. $description = api_html_to_text($description);
  186. if (!empty($title)) {
  187. $sample_text .= $title."\n";
  188. }
  189. if (!empty($description)) {
  190. $sample_text .= $description."\n";
  191. }
  192. if (!empty($title) || !empty($description)) {
  193. $sample_text .= "\n";
  194. }
  195. }
  196. }
  197. }
  198. return $sample_text;
  199. }
  200. /**
  201. * Converts to the system encoding all the language-sensitive fields in the imported course.
  202. */
  203. public function to_system_encoding()
  204. {
  205. if (api_equal_encodings($this->encoding, api_get_system_encoding())) {
  206. return;
  207. }
  208. foreach ($this->resources as $type => & $resources) {
  209. if (count($resources) > 0) {
  210. foreach ($resources as & $resource) {
  211. switch ($type) {
  212. case RESOURCE_ANNOUNCEMENT:
  213. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  214. $resource->content = api_to_system_encoding($resource->content, $this->encoding);
  215. break;
  216. case RESOURCE_DOCUMENT:
  217. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  218. $resource->comment = api_to_system_encoding($resource->comment, $this->encoding);
  219. break;
  220. case RESOURCE_EVENT:
  221. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  222. $resource->content = api_to_system_encoding($resource->content, $this->encoding);
  223. break;
  224. case RESOURCE_FORUM:
  225. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  226. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  227. break;
  228. case RESOURCE_FORUMCATEGORY:
  229. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  230. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  231. break;
  232. case RESOURCE_FORUMPOST:
  233. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  234. $resource->text = api_to_system_encoding($resource->text, $this->encoding);
  235. $resource->poster_name = api_to_system_encoding($resource->poster_name, $this->encoding);
  236. break;
  237. case RESOURCE_FORUMTOPIC:
  238. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  239. $resource->topic_poster_name = api_to_system_encoding($resource->topic_poster_name, $this->encoding);
  240. $resource->title_qualify = api_to_system_encoding($resource->title_qualify, $this->encoding);
  241. break;
  242. case RESOURCE_GLOSSARY:
  243. $resource->name = api_to_system_encoding($resource->name, $this->encoding);
  244. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  245. break;
  246. case RESOURCE_LEARNPATH:
  247. $resource->name = api_to_system_encoding($resource->name, $this->encoding);
  248. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  249. $resource->content_maker = api_to_system_encoding($resource->content_maker, $this->encoding);
  250. $resource->content_license = api_to_system_encoding($resource->content_license, $this->encoding);
  251. break;
  252. case RESOURCE_LINK:
  253. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  254. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  255. break;
  256. case RESOURCE_LINKCATEGORY:
  257. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  258. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  259. break;
  260. case RESOURCE_QUIZ:
  261. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  262. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  263. break;
  264. case RESOURCE_QUIZQUESTION:
  265. $resource->question = api_to_system_encoding($resource->question, $this->encoding);
  266. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  267. if (is_array($resource->answers) && count($resource->answers) > 0) {
  268. foreach ($resource->answers as $index => & $answer) {
  269. $answer['answer'] = api_to_system_encoding($answer['answer'], $this->encoding);
  270. $answer['comment'] = api_to_system_encoding($answer['comment'], $this->encoding);
  271. }
  272. }
  273. break;
  274. case RESOURCE_TEST_CATEGORY:
  275. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  276. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  277. break;
  278. case RESOURCE_SCORM:
  279. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  280. break;
  281. case RESOURCE_SURVEY:
  282. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  283. $resource->subtitle = api_to_system_encoding($resource->subtitle, $this->encoding);
  284. $resource->author = api_to_system_encoding($resource->author, $this->encoding);
  285. $resource->intro = api_to_system_encoding($resource->intro, $this->encoding);
  286. $resource->surveythanks = api_to_system_encoding($resource->surveythanks, $this->encoding);
  287. break;
  288. case RESOURCE_SURVEYQUESTION:
  289. $resource->survey_question = api_to_system_encoding($resource->survey_question, $this->encoding);
  290. $resource->survey_question_comment = api_to_system_encoding($resource->survey_question_comment, $this->encoding);
  291. break;
  292. case RESOURCE_TOOL_INTRO:
  293. $resource->intro_text = api_to_system_encoding($resource->intro_text, $this->encoding);
  294. break;
  295. case RESOURCE_WIKI:
  296. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  297. $resource->content = api_to_system_encoding($resource->content, $this->encoding);
  298. $resource->reflink = api_to_system_encoding($resource->reflink, $this->encoding);
  299. break;
  300. case RESOURCE_WORK:
  301. $resource->url = api_to_system_encoding($resource->url, $this->encoding);
  302. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  303. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. }
  310. }
  311. $this->encoding = api_get_system_encoding();
  312. }
  313. /**
  314. * Serialize the course with the best serializer available
  315. */
  316. public static function serialize($course)
  317. {
  318. if (extension_loaded('igbinary')) {
  319. return igbinary_serialize($course);
  320. } else {
  321. return serialize($course);
  322. }
  323. }
  324. /**
  325. * Unserialize the course with the best serializer available
  326. */
  327. public static function unserialize($course)
  328. {
  329. if (extension_loaded('igbinary')) {
  330. return igbinary_unserialize($course);
  331. } else {
  332. return unserialize($course);
  333. }
  334. }
  335. }