Course.class.php 16 KB

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