Course.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. case RESOURCE_WORK:
  174. $title = $resource->title;
  175. $description = $resource->description;
  176. break;
  177. default:
  178. break;
  179. }
  180. $title = Text::api_html_to_text($title);
  181. $description = Text::api_html_to_text($description);
  182. if (!empty($title)) {
  183. $sample_text .= $title."\n";
  184. }
  185. if (!empty($description)) {
  186. $sample_text .= $description."\n";
  187. }
  188. if (!empty($title) || !empty($description)) {
  189. $sample_text .= "\n";
  190. }
  191. }
  192. }
  193. }
  194. return $sample_text;
  195. }
  196. /**
  197. * Converts to the system encoding all the language-sensitive fields in the imported course.
  198. */
  199. public function to_system_encoding()
  200. {
  201. if (api_equal_encodings($this->encoding, api_get_system_encoding())) {
  202. return;
  203. }
  204. foreach ($this->resources as $type => & $resources) {
  205. if (count($resources) > 0) {
  206. foreach ($resources as $id => & $resource) {
  207. switch ($type) {
  208. case RESOURCE_ANNOUNCEMENT:
  209. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  210. $resource->content = api_to_system_encoding($resource->content, $this->encoding);
  211. break;
  212. case RESOURCE_DOCUMENT:
  213. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  214. $resource->comment = api_to_system_encoding($resource->comment, $this->encoding);
  215. break;
  216. case RESOURCE_EVENT:
  217. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  218. $resource->content = api_to_system_encoding($resource->content, $this->encoding);
  219. break;
  220. case RESOURCE_FORUM:
  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_FORUMCATEGORY:
  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_FORUMPOST:
  229. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  230. $resource->text = api_to_system_encoding($resource->text, $this->encoding);
  231. $resource->poster_name = api_to_system_encoding($resource->poster_name, $this->encoding);
  232. break;
  233. case RESOURCE_FORUMTOPIC:
  234. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  235. $resource->topic_poster_name = api_to_system_encoding($resource->topic_poster_name, $this->encoding);
  236. $resource->title_qualify = api_to_system_encoding($resource->title_qualify, $this->encoding);
  237. break;
  238. case RESOURCE_GLOSSARY:
  239. $resource->name = api_to_system_encoding($resource->name, $this->encoding);
  240. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  241. break;
  242. case RESOURCE_LEARNPATH:
  243. $resource->name = api_to_system_encoding($resource->name, $this->encoding);
  244. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  245. $resource->content_maker = api_to_system_encoding($resource->content_maker, $this->encoding);
  246. $resource->content_license = api_to_system_encoding($resource->content_license, $this->encoding);
  247. //$resource->author = api_to_system_encoding($resource->author, $this->encoding); // Needs implamantation.
  248. break;
  249. case RESOURCE_LINK:
  250. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  251. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  252. break;
  253. case RESOURCE_LINKCATEGORY:
  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_QUIZ:
  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_QUIZQUESTION:
  262. $resource->question = api_to_system_encoding($resource->question, $this->encoding);
  263. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  264. if (is_array($resource->answers) && count($resource->answers) > 0) {
  265. foreach ($resource->answers as $index => & $answer) {
  266. $answer['answer'] = api_to_system_encoding($answer['answer'], $this->encoding);
  267. $answer['comment'] = api_to_system_encoding($answer['comment'], $this->encoding);
  268. }
  269. }
  270. break;
  271. case RESOURCE_SCORM:
  272. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  273. break;
  274. case RESOURCE_SURVEY:
  275. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  276. $resource->subtitle = api_to_system_encoding($resource->subtitle, $this->encoding);
  277. $resource->author = api_to_system_encoding($resource->author, $this->encoding);
  278. $resource->intro = api_to_system_encoding($resource->intro, $this->encoding);
  279. $resource->surveythanks = api_to_system_encoding($resource->surveythanks, $this->encoding);
  280. break;
  281. case RESOURCE_SURVEYQUESTION:
  282. $resource->survey_question = api_to_system_encoding($resource->survey_question, $this->encoding);
  283. $resource->survey_question_comment = api_to_system_encoding($resource->survey_question_comment, $this->encoding);
  284. break;
  285. case RESOURCE_TOOL_INTRO:
  286. $resource->intro_text = api_to_system_encoding($resource->intro_text, $this->encoding);
  287. break;
  288. case RESOURCE_WIKI:
  289. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  290. $resource->content = api_to_system_encoding($resource->content, $this->encoding);
  291. $resource->reflink = api_to_system_encoding($resource->reflink, $this->encoding);
  292. break;
  293. case RESOURCE_WORK:
  294. $resource->url = api_to_system_encoding($resource->url, $this->encoding);
  295. $resource->title = api_to_system_encoding($resource->title, $this->encoding);
  296. $resource->description = api_to_system_encoding($resource->description, $this->encoding);
  297. break;
  298. default:
  299. break;
  300. }
  301. }
  302. }
  303. }
  304. $this->encoding = api_get_system_encoding();
  305. }
  306. /**
  307. * Serialize the course with the best serializer available
  308. */
  309. public static function serialize($course)
  310. {
  311. if (extension_loaded('igbinary')) {
  312. return igbinary_serialize($course);
  313. } else {
  314. return serialize($course);
  315. }
  316. }
  317. /**
  318. * Unserialize the course with the best serializer available
  319. */
  320. public static function unserialize($course)
  321. {
  322. if (extension_loaded('igbinary')) {
  323. return igbinary_unserialize($course);
  324. } else {
  325. return unserialize($course);
  326. }
  327. }
  328. }