Course.class.php 11 KB

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