Course.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. 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. function is_linked_resource(& $resource_to_check)
  34. {
  35. foreach ($this->resources as $type => $resources) {
  36. if (is_array($resources)) {
  37. foreach ($resources as $id => $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. 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 const $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. 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. * Show this course resources
  75. */
  76. 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_QUIZQUESTION:
  144. $title = $resource->question;
  145. $description = $resource->description;
  146. break;
  147. case RESOURCE_SCORM:
  148. $title = $resource->title;
  149. break;
  150. case RESOURCE_SURVEY:
  151. $title = $resource->title;
  152. $description = $resource->subtitle;
  153. break;
  154. case RESOURCE_SURVEYQUESTION:
  155. $title = $resource->survey_question;
  156. $description = $resource->survey_question_comment;
  157. break;
  158. case RESOURCE_TOOL_INTRO:
  159. $description = $resource->intro_text;
  160. break;
  161. case RESOURCE_WIKI:
  162. $title = $resource->title;
  163. $description = $resource->content;
  164. break;
  165. case RESOURCE_THEMATIC:
  166. $title = $resource->title;
  167. $description = $resource->content;
  168. break;
  169. case RESOURCE_ATTENDANCE:
  170. $title = $resource->params['name'];
  171. $description = $resource->params['description'];
  172. break;
  173. default:
  174. break;
  175. }
  176. $title = Text::api_html_to_text($title);
  177. $description = Text::api_html_to_text($description);
  178. if (!empty($title)) {
  179. $sample_text .= $title."\n";
  180. }
  181. if (!empty($description)) {
  182. $sample_text .= $description."\n";
  183. }
  184. if (!empty($title) || !empty($description)) {
  185. $sample_text .= "\n";
  186. }
  187. }
  188. }
  189. }
  190. return $sample_text;
  191. }
  192. /**
  193. * Converts to the system encoding all the language-sensitive fields in the imported course.
  194. */
  195. public function to_system_encoding()
  196. {
  197. if (api_equal_encodings($this->encoding, api_get_system_encoding())) {
  198. return;
  199. }
  200. foreach ($this->resources as $type => & $resources) {
  201. if (count($resources) > 0) {
  202. foreach ($resources as $id => & $resource) {
  203. switch ($type) {
  204. case RESOURCE_ANNOUNCEMENT:
  205. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  206. $resource->content = api_to_system_encoding($resource->content, $this->encoding);
  207. break;
  208. case RESOURCE_DOCUMENT:
  209. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  210. $resource->comment = api_to_system_encoding($resource->comment, $this->encoding);
  211. break;
  212. case RESOURCE_EVENT:
  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_FORUM:
  217. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  218. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  219. break;
  220. case RESOURCE_FORUMCATEGORY:
  221. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  222. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  223. break;
  224. case RESOURCE_FORUMPOST:
  225. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  226. $resource->text = api_to_system_encoding($resource->text, $this->encoding);
  227. $resource->poster_name = api_to_system_encoding($resource->poster_name, $this->encoding);
  228. break;
  229. case RESOURCE_FORUMTOPIC:
  230. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  231. $resource->topic_poster_name = api_to_system_encoding(
  232. $resource->topic_poster_name,
  233. $this->encoding
  234. );
  235. $resource->title_qualify = api_to_system_encoding(
  236. $resource->title_qualify,
  237. $this->encoding
  238. );
  239. break;
  240. case RESOURCE_GLOSSARY:
  241. $resource->name = api_to_system_encoding($resource->name, $this->encoding);
  242. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  243. break;
  244. case RESOURCE_LEARNPATH:
  245. $resource->name = api_to_system_encoding($resource->name, $this->encoding);
  246. $resource->description = api_to_system_encoding(
  247. $resource->description,
  248. $this->encoding
  249. );
  250. $resource->content_maker = api_to_system_encoding(
  251. $resource->content_maker,
  252. $this->encoding
  253. );
  254. $resource->content_license = api_to_system_encoding(
  255. $resource->content_license,
  256. $this->encoding
  257. );
  258. //$resource->author = api_to_system_encoding($resource->author, $this->encoding); // Needs implamantation.
  259. break;
  260. case RESOURCE_LINK:
  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_LINKCATEGORY:
  265. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  266. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  267. break;
  268. case RESOURCE_QUIZ:
  269. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  270. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  271. break;
  272. case RESOURCE_QUIZQUESTION:
  273. $resource->question = api_to_system_encoding($resource->question, $this->encoding);
  274. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  275. if (is_array($resource->answers) && count($resource->answers) > 0) {
  276. foreach ($resource->answers as $index => & $answer) {
  277. $answer['answer'] = api_to_system_encoding($answer['answer'], $this->encoding);
  278. $answer['comment'] = api_to_system_encoding($answer['comment'], $this->encoding);
  279. }
  280. }
  281. break;
  282. case RESOURCE_SCORM:
  283. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  284. break;
  285. case RESOURCE_SURVEY:
  286. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  287. $resource->subtitle = api_to_system_encoding($resource->subtitle, $this->encoding);
  288. $resource->author = api_to_system_encoding($resource->author, $this->encoding);
  289. $resource->intro = api_to_system_encoding($resource->intro, $this->encoding);
  290. $resource->surveythanks = api_to_system_encoding($resource->surveythanks, $this->encoding);
  291. break;
  292. case RESOURCE_SURVEYQUESTION:
  293. $resource->survey_question = api_to_system_encoding(
  294. $resource->survey_question,
  295. $this->encoding
  296. );
  297. $resource->survey_question_comment = api_to_system_encoding(
  298. $resource->survey_question_comment,
  299. $this->encoding
  300. );
  301. break;
  302. case RESOURCE_TOOL_INTRO:
  303. $resource->intro_text = api_to_system_encoding($resource->intro_text, $this->encoding);
  304. break;
  305. case RESOURCE_WIKI:
  306. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  307. $resource->content = api_to_system_encoding($resource->content, $this->encoding);
  308. $resource->reflink = api_to_system_encoding($resource->reflink, $this->encoding);
  309. break;
  310. default:
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. $this->encoding = api_get_system_encoding();
  317. }
  318. /**
  319. * Serialize the course with the best serializer available
  320. */
  321. public static function serialize($course)
  322. {
  323. if (extension_loaded('igbinary')) {
  324. return igbinary_serialize($course);
  325. } else {
  326. return serialize($course);
  327. }
  328. }
  329. /**
  330. * Unserialize the course with the best serializer available
  331. */
  332. public static function unserialize($course)
  333. {
  334. if (extension_loaded('igbinary')) {
  335. return igbinary_unserialize($course);
  336. } else {
  337. return unserialize($course);
  338. }
  339. }
  340. }