CourseBuilder.class.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'Course.class.php';
  4. require_once 'Event.class.php';
  5. require_once 'Link.class.php';
  6. require_once 'ToolIntro.class.php';
  7. require_once 'Document.class.php';
  8. require_once 'ScormDocument.class.php';
  9. require_once 'LinkCategory.class.php';
  10. require_once 'CourseDescription.class.php';
  11. require_once 'ForumPost.class.php';
  12. require_once 'ForumTopic.class.php';
  13. require_once 'Forum.class.php';
  14. require_once 'ForumCategory.class.php';
  15. require_once 'Quiz.class.php';
  16. require_once 'QuizQuestion.class.php';
  17. require_once 'CourseCopyLearnpath.class.php';
  18. require_once 'Survey.class.php';
  19. require_once 'SurveyQuestion.class.php';
  20. require_once 'Glossary.class.php';
  21. require_once 'CourseSession.class.php';
  22. require_once 'wiki.class.php';
  23. require_once 'Thematic.class.php';
  24. require_once 'Attendance.class.php';
  25. /**
  26. * Class which can build a course-object from a Chamilo-course.
  27. * @author Bart Mollet <bart.mollet@hogent.be>
  28. * @package chamilo.backup
  29. */
  30. class CourseBuilder {
  31. /**
  32. * The course
  33. */
  34. var $course;
  35. /**
  36. * Create a new CourseBuilder
  37. */
  38. function __construct($type='', $course = null) {
  39. global $_course;
  40. if (!empty($course['official_code'])){
  41. $_course = $course;
  42. }
  43. $this->course = new Course();
  44. $this->course->code = $_course['official_code'];
  45. $this->course->type = $type;
  46. $this->course->path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/';
  47. $this->course->backup_path = api_get_path(SYS_COURSE_PATH).$_course['path'];
  48. $this->course->encoding = api_get_system_encoding(); //current platform encoding
  49. $this->course->db_name = $_course['dbName'];
  50. $this->course->info = $_course;
  51. }
  52. /**
  53. * Get the created course
  54. * @return course The course
  55. */
  56. function get_course() {
  57. return $this->course;
  58. }
  59. /**
  60. * Build the course-object
  61. *
  62. * @param int session_id
  63. * @param string course_code
  64. * @param bool true if you want to get the elements that exists in the course and
  65. * in the session, (session_id = 0 or session_id = X)
  66. */
  67. function build($session_id = 0, $course_code = '', $with_base_content = false) {
  68. $table_link = Database :: get_course_table(TABLE_LINKED_RESOURCES);
  69. $table_properties = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  70. $course_info = api_get_course_info($course_code);
  71. $course_id = $course_info['real_id'];
  72. if (!empty($session_id) && !empty($course_code)) {
  73. $this->build_documents($session_id, $course_code, $with_base_content);
  74. $this->build_quizzes($session_id, $course_code, $with_base_content);
  75. $this->build_glossary($session_id, $course_code, $with_base_content);
  76. $this->build_learnpaths($session_id, $course_code,$with_base_content);
  77. $this->build_links($session_id, $course_code, $with_base_content);
  78. $this->build_course_descriptions($session_id, $course_code, $with_base_content);
  79. $this->build_wiki($session_id, $course_code, $with_base_content);
  80. //$this->build_thematic($session_id, $course_code, $with_base_content);
  81. $this->build_thematic();
  82. $this->build_attendance();
  83. } else {
  84. $this->build_events();
  85. $this->build_announcements();
  86. $this->build_links();
  87. $this->build_tool_intro();
  88. $this->build_documents();
  89. $this->build_course_descriptions();
  90. $this->build_wiki();
  91. $this->build_quizzes();
  92. $this->build_learnpaths();
  93. $this->build_surveys();
  94. $this->build_glossary();
  95. $this->build_thematic();
  96. $this->build_attendance();
  97. }
  98. //TABLE_LINKED_RESOURCES is the "resource" course table, which is deprecated, apparently
  99. foreach ($this->course->resources as $type => $resources) {
  100. foreach ($resources as $id => $resource) {
  101. $sql = "SELECT * FROM ".$table_link." WHERE c_id = $course_id AND source_type = '".$resource->get_type()."' AND source_id = '".$resource->get_id()."'";
  102. $res = Database::query($sql);
  103. while ($link = Database::fetch_object($res)) {
  104. $this->course->resources[$type][$id]->add_linked_resource($link->resource_type, $link->resource_id);
  105. }
  106. }
  107. }
  108. foreach ($this->course->resources as $type => $resources) {
  109. foreach ($resources as $id => $resource)
  110. {
  111. $tool = $resource->get_tool();
  112. if ($tool != null) {
  113. $sql = "SELECT * FROM $table_properties WHERE c_id = $course_id AND TOOL = '".$tool."' AND ref='".$resource->get_id()."'";
  114. $res = Database::query($sql);
  115. $all_properties = array ();
  116. while ($item_property = Database::fetch_array($res))
  117. {
  118. $all_properties[] = $item_property;
  119. }
  120. $this->course->resources[$type][$id]->item_properties = $all_properties;
  121. }
  122. }
  123. }
  124. return $this->course;
  125. }
  126. /**
  127. * Build the documents
  128. */
  129. function build_documents($session_id = 0, $course_code = '', $with_base_content = false) {
  130. $course_info = api_get_course_info($course_code);
  131. $course_id = $course_info['real_id'];
  132. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  133. $table_prop = Database::get_course_table(TABLE_ITEM_PROPERTY);
  134. if (!empty($course_code) && !empty($session_id)) {
  135. $session_id = intval($session_id);
  136. if ($with_base_content) {
  137. $session_condition = api_get_session_condition($session_id, true, true);
  138. } else {
  139. $session_condition = api_get_session_condition($session_id, true);
  140. }
  141. if (!empty($this->course->type) && $this->course->type=='partial') {
  142. $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size FROM $table_doc d, $table_prop p
  143. WHERE d.c_id = $course_id AND
  144. p.c_id = $course_id AND
  145. tool = '".TOOL_DOCUMENT."' AND
  146. p.ref = d.id AND
  147. p.visibility != 2 AND
  148. path NOT LIKE '/images/gallery%'
  149. $session_condition
  150. ORDER BY path";
  151. } else {
  152. $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size FROM $table_doc d, $table_prop p
  153. WHERE d.c_id = $course_id AND
  154. p.c_id = $course_id AND
  155. tool = '".TOOL_DOCUMENT."' AND
  156. p.ref = d.id AND p.visibility != 2 $session_condition
  157. ORDER BY path";
  158. }
  159. $db_result = Database::query($sql);
  160. while ($obj = Database::fetch_object($db_result)) {
  161. $doc = new Document($obj->id, $obj->path, $obj->comment, $obj->title, $obj->filetype, $obj->size);
  162. $this->course->add_resource($doc);
  163. }
  164. } else {
  165. if (!empty($this->course->type) && $this->course->type=='partial')
  166. $sql = 'SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size FROM '.$table_doc.' d, '.$table_prop.' p
  167. WHERE d.c_id = '.$course_id.' AND
  168. p.c_id = '.$course_id.' AND
  169. tool = \''.TOOL_DOCUMENT.'\' AND p.ref = d.id AND p.visibility != 2 AND path NOT LIKE \'/images/gallery%\' AND d.session_id = 0 ORDER BY path';
  170. else
  171. $sql = 'SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
  172. FROM '.$table_doc.' d, '.$table_prop.' p
  173. WHERE d.c_id = '.$course_id.' AND
  174. p.c_id = '.$course_id.' AND
  175. tool = \''.TOOL_DOCUMENT.'\' AND p.ref = d.id AND p.visibility != 2 AND d.session_id = 0 ORDER BY path';
  176. $db_result = Database::query($sql);
  177. while ($obj = Database::fetch_object($db_result)) {
  178. $doc = new Document($obj->id, $obj->path, $obj->comment, $obj->title, $obj->filetype, $obj->size);
  179. $this->course->add_resource($doc);
  180. }
  181. }
  182. }
  183. /**
  184. * Build the forums
  185. */
  186. function build_forums() {
  187. $table = Database :: get_course_table(TABLE_FORUM);
  188. $course_id = api_get_course_int_id();
  189. $sql = "SELECT * FROM $table WHERE c_id = $course_id ";
  190. $db_result = Database::query($sql);
  191. while ($obj = Database::fetch_object($db_result)) {
  192. $forum = new Forum($obj->forum_id, $obj->forum_title, $obj->forum_comment, $obj->forum_category, $obj->forum_last_post, $obj->forum_threads, $obj->forum_posts, $obj->allow_anonymous, $obj->allow_edit, $obj->approval_direct_post, $obj->allow_attachements, $obj->allow_new_threads, $obj->default_view, $obj->forum_of_group, $obj->forum_group_public_private, $obj->forum_order, $obj->locked, $obj->session_id, $obj->forum_image);
  193. $this->course->add_resource($forum);
  194. $this->build_forum_category($obj->forum_category);
  195. }
  196. $this->build_forum_topics();
  197. $this->build_forum_posts();
  198. }
  199. /**
  200. * Build a forum-category
  201. */
  202. function build_forum_category($id) {
  203. $table = Database :: get_course_table(TABLE_FORUM_CATEGORY);
  204. $course_id = api_get_course_int_id();
  205. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND cat_id = $id";
  206. $db_result = Database::query($sql);
  207. while ($obj = Database::fetch_object($db_result)) {
  208. $forum_category = new ForumCategory($obj->cat_id, $obj->cat_title, $obj->cat_comment, $obj->cat_order, $obj->locked, $obj->session_id);
  209. $this->course->add_resource($forum_category);
  210. }
  211. }
  212. /**
  213. * Build the forum-topics
  214. */
  215. function build_forum_topics() {
  216. $table = Database :: get_course_table(TABLE_FORUM_THREAD);
  217. $course_id = api_get_course_int_id();
  218. $sql = "SELECT * FROM $table WHERE c_id = $course_id ";
  219. $db_result = Database::query($sql);
  220. while ($obj = Database::fetch_object($db_result))
  221. {
  222. $forum_topic = new ForumTopic($obj->thread_id, $obj->thread_title, $obj->thread_date, $obj->thread_poster_id, $obj->thread_poster_name, $obj->forum_id, $obj->thread_last_post, $obj->thread_replies, $obj->thread_views, $obj->thread_sticky, $obj->locked, $obj->thread_close_date, $obj->thread_weight, $obj->thread_title_qualify, $obj->thread_qualify_max);
  223. $this->course->add_resource($forum_topic);
  224. }
  225. }
  226. /**
  227. * Build the forum-posts
  228. * TODO: All tree structure of posts should be built, attachments for example.
  229. */
  230. function build_forum_posts()
  231. {
  232. $table = Database :: get_course_table(TABLE_FORUM_POST);
  233. $course_id = api_get_course_int_id();
  234. $sql = "SELECT * FROM $table WHERE c_id = $course_id ";
  235. $db_result = Database::query($sql);
  236. while ($obj = Database::fetch_object($db_result)) {
  237. $forum_post = new ForumPost($obj->post_id, $obj->post_title, $obj->post_text, $obj->post_date, $obj->poster_id, $obj->poster_name, $obj->post_notification, $obj->post_parent_id, $obj->thread_id, $obj->forum_id, $obj->visible);
  238. $this->course->add_resource($forum_post);
  239. }
  240. }
  241. /**
  242. * Build the links
  243. */
  244. function build_links($session_id = 0, $course_code = '', $with_base_content = false) {
  245. $course_info = api_get_course_info($course_code);
  246. $course_id = $course_info['real_id'];
  247. $table = Database :: get_course_table(TABLE_LINK);
  248. $table_prop = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  249. if (!empty($session_id) && !empty($course_code)) {
  250. $session_id = intval($session_id);
  251. if ($with_base_content) {
  252. $session_condition = api_get_session_condition($session_id, true, true);
  253. } else {
  254. $session_condition = api_get_session_condition($session_id, true);
  255. }
  256. $sql = "SELECT l.id, l.title, l.url, l.description, l.category_id, l.on_homepage
  257. FROM $table l, $table_prop p
  258. WHERE l.c_id = $course_id AND p.c_id = $course_id AND p.ref=l.id AND p.tool = '".TOOL_LINK."' AND p.visibility != 2 $session_condition
  259. ORDER BY l.display_order";
  260. } else {
  261. $sql = "SELECT l.id, l.title, l.url, l.description, l.category_id, l.on_homepage
  262. FROM $table l, $table_prop p
  263. WHERE l.c_id = $course_id AND p.c_id = $course_id AND p.ref=l.id AND p.tool = '".TOOL_LINK."' AND p.visibility != 2 AND l.session_id = 0
  264. ORDER BY l.display_order";
  265. }
  266. $db_result = Database::query($sql);
  267. while ($obj = Database::fetch_object($db_result)) {
  268. $link = new Link($obj->id, $obj->title, $obj->url, $obj->description, $obj->category_id, $obj->on_homepage);
  269. $this->course->add_resource($link);
  270. if (!empty($course_code)) {
  271. $res = $this->build_link_category($obj->category_id,$course_code);
  272. } else {
  273. $res = $this->build_link_category($obj->category_id);
  274. }
  275. if($res > 0) {
  276. $this->course->resources[RESOURCE_LINK][$obj->id]->add_linked_resource(RESOURCE_LINKCATEGORY, $obj->category_id);
  277. }
  278. }
  279. }
  280. /**
  281. * Build tool intro
  282. */
  283. function build_tool_intro()
  284. {
  285. $table = Database :: get_course_table(TABLE_TOOL_INTRO);
  286. $course_id = api_get_course_int_id();
  287. $sql = "SELECT * FROM $table WHERE c_id = $course_id ";
  288. $db_result = Database::query($sql);
  289. while ($obj = Database::fetch_object($db_result)) {
  290. $tool_intro = new ToolIntro($obj->id, $obj->intro_text);
  291. $this->course->add_resource($tool_intro);
  292. }
  293. }
  294. /**
  295. * Build a link category
  296. */
  297. function build_link_category($id, $course_code = '') {
  298. $course_info = api_get_course_info($course_code);
  299. $course_id = $course_info['real_id'];
  300. $link_cat_table = Database :: get_course_table(TABLE_LINK_CATEGORY);
  301. $sql = "SELECT * FROM $link_cat_table WHERE c_id = $course_id AND id = $id";
  302. $db_result = Database::query($sql);
  303. while ($obj = Database::fetch_object($db_result)) {
  304. $link_category = new LinkCategory($obj->id, $obj->category_title, $obj->description, $obj->display_order);
  305. $this->course->add_resource($link_category);
  306. return $id;
  307. }
  308. return 0;
  309. }
  310. /**
  311. * Build the Quizzes
  312. */
  313. function build_quizzes($session_id = 0, $course_code = '', $with_base_content = false) {
  314. $course_info = api_get_course_info($course_code);
  315. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
  316. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  317. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  318. $course_id = $course_info['real_id'];
  319. if (!empty($course_code) && !empty($session_id)) {
  320. $session_id = intval($session_id);
  321. if ($with_base_content) {
  322. $session_condition = api_get_session_condition($session_id, true, true);
  323. } else {
  324. $session_condition = api_get_session_condition($session_id, true);
  325. }
  326. $session_id = intval($session_id);
  327. $sql = "SELECT * FROM $table_qui WHERE c_id = $course_id AND active >=0 $session_condition"; //select only quizzes with active = 0 or 1 (not -1 which is for deleted quizzes)
  328. } else {
  329. $sql = "SELECT * FROM $table_qui WHERE c_id = $course_id AND active >=0 AND session_id = 0"; //select only quizzes with active = 0 or 1 (not -1 which is for deleted quizzes)
  330. }
  331. $db_result = Database::query($sql);
  332. while ($obj = Database::fetch_object($db_result)) {
  333. if (strlen($obj->sound) > 0) {
  334. $doc = Database::fetch_object(Database::query("SELECT id FROM ".$table_doc." WHERE c_id = $course_id AND path = '/audio/".$obj->sound."'"));
  335. $obj->sound = $doc->id;
  336. }
  337. $quiz = new Quiz($obj->id, $obj->title, $obj->description, $obj->random, $obj->type, $obj->active, $obj->sound, $obj->max_attempt,
  338. $obj->results_disabled, $obj->access_condition, $obj->start_time, $obj->end_time, $obj->feedback_type, $obj->random_answers, $obj->expired_time, $obj->session_id);
  339. $sql = 'SELECT * FROM '.$table_rel.' WHERE c_id = '.$course_id.' AND exercice_id = '.$obj->id;
  340. $db_result2 = Database::query($sql);
  341. while ($obj2 = Database::fetch_object($db_result2)) {
  342. $quiz->add_question($obj2->question_id, $obj2->question_order);
  343. }
  344. $this->course->add_resource($quiz);
  345. }
  346. if (!empty($course_code)) {
  347. $this->build_quiz_questions($course_code);
  348. } else {
  349. $this->build_quiz_questions();
  350. }
  351. }
  352. /**
  353. * Build the Quiz-Questions
  354. */
  355. function build_quiz_questions($course_code = '') {
  356. $course_info = api_get_course_info($course_code);
  357. $course_id = $course_info['real_id'];
  358. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
  359. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  360. $table_que = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  361. $table_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  362. // Building normal tests.
  363. $sql = "SELECT * FROM $table_que WHERE c_id = $course_id ";
  364. $db_result = Database::query($sql);
  365. while ($obj = Database::fetch_object($db_result)) {
  366. $question = new QuizQuestion($obj->id, $obj->question, $obj->description, $obj->ponderation, $obj->type, $obj->position, $obj->picture, $obj->level, $obj->extra);
  367. $sql = 'SELECT * FROM '.$table_ans.' WHERE c_id = '.$course_id.' AND question_id = '.$obj->id;
  368. $db_result2 = Database::query($sql);
  369. while ($obj2 = Database::fetch_object($db_result2)) {
  370. $question->add_answer($obj2->id, $obj2->answer, $obj2->correct, $obj2->comment, $obj2->ponderation, $obj2->position, $obj2->hotspot_coordinates, $obj2->hotspot_type);
  371. }
  372. $this->course->add_resource($question);
  373. }
  374. // Building a fictional test for collecting orphan questions.
  375. $build_orphan_questions = !empty($_POST['recycle_option']); // When a course is emptied this option should be activated (true).
  376. $sql = "SELECT * FROM $table_que as questions LEFT JOIN $table_rel as quizz_questions
  377. ON questions.id=quizz_questions.question_id LEFT JOIN $table_qui as exercices
  378. ON exercice_id=exercices.id
  379. WHERE questions.c_id = $course_id AND
  380. quizz_questions.c_id = $course_id AND
  381. exercices.c_id = $course_id AND
  382. quizz_questions.exercice_id IS NULL OR
  383. exercices.active = -1"; // active = -1 means "deleted" test.
  384. $db_result = Database::query($sql);
  385. if (Database::num_rows($db_result) > 0) {
  386. $build_orphan_questions = true;
  387. while ($obj = Database::fetch_object($db_result)) {
  388. $question = new QuizQuestion($obj->id, $obj->question, $obj->description, $obj->ponderation, $obj->type, $obj->position, $obj->picture,$obj->level, $obj->extra);
  389. $sql = "SELECT * FROM $table_ans WHERE c_id = $course_id AND question_id = ".$obj->id;
  390. $db_result2 = Database::query($sql);
  391. while ($obj2 = Database::fetch_object($db_result2)) {
  392. $question->add_answer($obj2->id, $obj2->answer, $obj2->correct, $obj2->comment, $obj2->ponderation, $obj2->position, $obj2->hotspot_coordinates, $obj2->hotspot_type);
  393. }
  394. $this->course->add_resource($question);
  395. }
  396. }
  397. if ($build_orphan_questions) {
  398. //$this->course->add_resource(new Quiz(-1, get_lang('OrphanQuestions', ''), '', 0, 0, 1, '', 0));
  399. }
  400. }
  401. /**
  402. * Build the orphan questions
  403. */
  404. function build_quiz_orphan_questions()
  405. {
  406. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
  407. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  408. $table_que = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  409. $table_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  410. $course_id = api_get_course_int_id();
  411. $sql = 'SELECT * FROM '.$table_que.' as questions LEFT JOIN '.$table_rel.' as quizz_questions
  412. ON questions.id=quizz_questions.question_id LEFT JOIN '.$table_qui.' as exercices
  413. ON exercice_id=exercices.id
  414. WHERE questions.c_id = '.$course_id.' AND
  415. quizz_questions.c_id = '.$course_id.' AND
  416. exercices.c_id = '.$course_id.' AND
  417. quizz_questions.exercice_id IS NULL OR
  418. exercices.active = -1';
  419. $db_result = Database::query($sql);
  420. if (Database::num_rows($db_result) > 0) {
  421. $orphan_questions = new Quiz(-1, get_lang('OrphanQuestions', ''), '', 0, 0, 1, '', 0); // Tjis is the fictional test for collecting orphan questions.
  422. $this->course->add_resource($orphan_questions);
  423. while ($obj = Database::fetch_object($db_result))
  424. {
  425. $question = new QuizQuestion($obj->id, $obj->question, $obj->description, $obj->ponderation, $obj->type, $obj->position, $obj->picture,$obj->level,$obj->extra);
  426. $sql = 'SELECT * FROM '.$table_ans.' WHERE question_id = '.$obj->id;
  427. $db_result2 = Database::query($sql);
  428. while ($obj2 = Database::fetch_object($db_result2))
  429. {
  430. $question->add_answer($obj2->id, $obj2->answer, $obj2->correct, $obj2->comment, $obj2->ponderation, $obj2->position, $obj2->hotspot_coordinates, $obj2->hotspot_type);
  431. }
  432. $this->course->add_resource($question);
  433. }
  434. }
  435. }
  436. /**
  437. * Build the Surveys
  438. */
  439. function build_surveys() {
  440. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  441. $table_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  442. $course_id = api_get_course_int_id();
  443. $sql = 'SELECT * FROM '.$table_survey.' WHERE c_id = '.$course_id.' AND session_id = 0 ';
  444. $db_result = Database::query($sql);
  445. while ($obj = Database::fetch_object($db_result))
  446. {
  447. $survey = new Survey($obj->survey_id, $obj->code,$obj->title,
  448. $obj->subtitle, $obj->author, $obj->lang,
  449. $obj->avail_from, $obj->avail_till, $obj->is_shared,
  450. $obj->template, $obj->intro, $obj->surveythanks,
  451. $obj->creation_date, $obj->invited, $obj->answered,
  452. $obj->invite_mail, $obj->reminder_mail);
  453. $sql = 'SELECT * FROM '.$table_question.' WHERE survey_id = '.$obj->survey_id;
  454. $db_result2 = Database::query($sql);
  455. while ($obj2 = Database::fetch_object($db_result2))
  456. {
  457. $survey->add_question($obj2->question_id);
  458. }
  459. $this->course->add_resource($survey);
  460. }
  461. $this->build_survey_questions();
  462. }
  463. /**
  464. * Build the Survey Questions
  465. */
  466. function build_survey_questions()
  467. {
  468. $table_que = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  469. $table_opt = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  470. $course_id = api_get_course_int_id();
  471. $sql = 'SELECT * FROM '.$table_que.' WHERE c_id = '.$course_id.' ';
  472. $db_result = Database::query($sql);
  473. while ($obj = Database::fetch_object($db_result))
  474. {
  475. $question = new SurveyQuestion($obj->question_id, $obj->survey_id,
  476. $obj->survey_question, $obj->survey_question_comment,
  477. $obj->type, $obj->display, $obj->sort,
  478. $obj->shared_question_id, $obj->max_value);
  479. $sql = 'SELECT * FROM '.$table_opt.' WHERE c_id = '.$course_id.' AND question_id = '."'".$obj->question_id."'";
  480. $db_result2 = Database::query($sql);
  481. while ($obj2 = Database::fetch_object($db_result2))
  482. {
  483. $question->add_answer($obj2->option_text, $obj2->sort);
  484. }
  485. $this->course->add_resource($question);
  486. }
  487. }
  488. /**
  489. * Build the announcements
  490. */
  491. function build_announcements() {
  492. $table = Database :: get_course_table(TABLE_ANNOUNCEMENT);
  493. $course_id = api_get_course_int_id();
  494. $sql = 'SELECT * FROM '.$table.' WHERE c_id = '.$course_id.' AND session_id = 0';
  495. $db_result = Database::query($sql);
  496. $table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  497. while ($obj = Database::fetch_object($db_result)) {
  498. $sql = 'SELECT path, comment, filename, size FROM '.$table_attachment.' WHERE c_id = '.$course_id.' AND announcement_id = '.$obj->id.'';
  499. $result = Database::query($sql);
  500. $attachment_obj = Database::fetch_object($result);
  501. $att_path = $att_filename = $att_size = $atth_comment = '';
  502. if (!empty($attachment_obj)) {
  503. $att_path = $attachment_obj->path;
  504. $att_filename = $attachment_obj->filename;
  505. $att_size = $attachment_obj->size;
  506. $atth_comment = $attachment_obj->comment;
  507. }
  508. $announcement = new Announcement($obj->id, $obj->title, $obj->content, $obj->end_date,$obj->display_order,$obj->email_sent, $att_path, $att_filename, $att_size, $atth_comment);
  509. $this->course->add_resource($announcement);
  510. }
  511. }
  512. /**
  513. * Build the events
  514. */
  515. function build_events()
  516. {
  517. $table = Database :: get_course_table(TABLE_AGENDA);
  518. $course_id = api_get_course_int_id();
  519. $sql = 'SELECT * FROM '.$table.' WHERE c_id = '.$course_id.' AND session_id = 0';
  520. $db_result = Database::query($sql);
  521. while ($obj = Database::fetch_object($db_result)) {
  522. $table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT);
  523. $sql = 'SELECT path, comment, filename, size FROM '.$table_attachment.' WHERE c_id = '.$course_id.' AND agenda_id = '.$obj->id.'';
  524. $result = Database::query($sql);
  525. $attachment_obj = Database::fetch_object($result);
  526. $att_path = $att_filename = $att_size = $atth_comment = '';
  527. if (!empty($attachment_obj)) {
  528. $att_path = $attachment_obj->path;
  529. $att_filename = $attachment_obj->filename;
  530. $att_size = $attachment_obj->size;
  531. $atth_comment = $attachment_obj->comment;
  532. }
  533. $event = new Event($obj->id, $obj->title, $obj->content, $obj->start_date, $obj->end_date, $att_path, $att_filename, $att_size, $atth_comment);
  534. $this->course->add_resource($event);
  535. }
  536. }
  537. /**
  538. * Build the course-descriptions
  539. */
  540. function build_course_descriptions($session_id = 0,$course_code = '', $with_base_content = false) {
  541. $course_info = api_get_course_info($course_code);
  542. $course_id = $course_info['real_id'];
  543. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  544. if (!empty($session_id) && !empty($course_code)) {
  545. $session_id = intval($session_id);
  546. if ($with_base_content) {
  547. $session_condition = api_get_session_condition($session_id, true, true);
  548. } else {
  549. $session_condition = api_get_session_condition($session_id, true);
  550. }
  551. $sql = 'SELECT * FROM '.$table. ' WHERE c_id = '.$course_id.' '.$session_condition;
  552. } else {
  553. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  554. $sql = 'SELECT * FROM '.$table. ' WHERE c_id = '.$course_id.' AND session_id = 0';
  555. }
  556. $db_result = Database::query($sql);
  557. while ($obj = Database::fetch_object($db_result)) {
  558. $cd = new CourseDescription($obj->id, $obj->title, $obj->content, $obj->description_type);
  559. $this->course->add_resource($cd);
  560. }
  561. }
  562. /**
  563. * Build the learnpaths
  564. */
  565. function build_learnpaths($session_id = 0,$course_code = '', $with_base_content = false) {
  566. $course_info = api_get_course_info($course_code);
  567. $course_id = $course_info['real_id'];
  568. $table_main = Database::get_course_table(TABLE_LP_MAIN);
  569. $table_item = Database::get_course_table(TABLE_LP_ITEM);
  570. $table_tool = Database::get_course_table(TABLE_TOOL_LIST);
  571. if (!empty($session_id) && !empty($course_code)) {
  572. $session_id = intval($session_id);
  573. if ($with_base_content) {
  574. $session_condition = api_get_session_condition($session_id, true, true);
  575. } else {
  576. $session_condition = api_get_session_condition($session_id, true);
  577. }
  578. $sql = 'SELECT * FROM '.$table_main.' WHERE c_id = '.$course_id.' '.$session_condition;
  579. } else {
  580. $sql = 'SELECT * FROM '.$table_main.' WHERE c_id = '.$course_id.' AND session_id = 0';
  581. }
  582. $db_result = Database::query($sql);
  583. if ($db_result)
  584. while ($obj = Database::fetch_object($db_result)) {
  585. $items = array();
  586. $sql_items = "SELECT * FROM ".$table_item." WHERE c_id = '$course_id' AND lp_id = ".$obj->id;
  587. $db_items = Database::query($sql_items);
  588. while ($obj_item = Database::fetch_object($db_items)) {
  589. $item['id'] = $obj_item->id;
  590. $item['item_type'] = $obj_item->item_type;
  591. $item['ref'] = $obj_item->ref;
  592. $item['title'] = $obj_item->title;
  593. $item['description'] = $obj_item->description;
  594. $item['path'] = $obj_item->path;
  595. $item['min_score'] = $obj_item->min_score;
  596. $item['max_score'] = $obj_item->max_score;
  597. $item['mastery_score'] = $obj_item->mastery_score;
  598. $item['parent_item_id'] = $obj_item->parent_item_id;
  599. $item['previous_item_id'] = $obj_item->previous_item_id;
  600. $item['next_item_id'] = $obj_item->next_item_id;
  601. $item['display_order'] = $obj_item->display_order;
  602. $item['prerequisite'] = $obj_item->prerequisite;
  603. $item['parameters'] = $obj_item->parameters;
  604. $item['launch_data'] = $obj_item->launch_data;
  605. $item['audio'] = $obj_item->audio;
  606. $items[] = $item;
  607. }
  608. $sql_tool = "SELECT id FROM ".$table_tool." WHERE c_id = $course_id AND (link LIKE '%lp_controller.php%lp_id=".$obj->id."%' and image='scormbuilder.gif') AND visibility='1'";
  609. $db_tool = Database::query($sql_tool);
  610. if(Database::num_rows($db_tool)) {
  611. $visibility='1';
  612. } else {
  613. $visibility='0';
  614. }
  615. $lp = new CourseCopyLearnpath($obj->id,
  616. $obj->lp_type,
  617. $obj->name,
  618. $obj->path,
  619. $obj->ref,
  620. $obj->description,
  621. $obj->content_local,
  622. $obj->default_encoding,
  623. $obj->default_view_mod,
  624. $obj->prevent_reinit,
  625. $obj->force_commit,
  626. $obj->content_maker,
  627. $obj->display_order,
  628. $obj->js_lib,
  629. $obj->content_license,
  630. $obj->debug,
  631. $visibility,
  632. $obj->author,
  633. $obj->preview_image,
  634. $obj->use_max_score,
  635. $obj->autolunch,
  636. $obj->created_on,
  637. $obj->modified_on,
  638. $obj->publicated_on,
  639. $obj->expired_on,
  640. $obj->session_id,
  641. $items);
  642. $this->course->add_resource($lp);
  643. }
  644. //save scorm directory (previously build_scorm_documents())
  645. $i = 1;
  646. if ($dir=@opendir($this->course->backup_path.'/scorm')) {
  647. while($file=readdir($dir)) {
  648. if(is_dir($this->course->backup_path.'/scorm/'.$file) && !in_array($file,array('.','..'))) {
  649. $doc = new ScormDocument($i++, '/'.$file, $file);
  650. $this->course->add_resource($doc);
  651. }
  652. }
  653. closedir($dir);
  654. }
  655. }
  656. /**
  657. * Build the glossarys
  658. */
  659. function build_glossary($session_id = 0, $course_code = '', $with_base_content = false) {
  660. $course_info = api_get_course_info($course_code);
  661. $table_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  662. $course_id = $course_info['real_id'];
  663. if (!empty($session_id) && !empty($course_code)) {
  664. $session_id = intval($session_id);
  665. if ($with_base_content) {
  666. $session_condition = api_get_session_condition($session_id, true, true);
  667. } else {
  668. $session_condition = api_get_session_condition($session_id, true);
  669. }
  670. //@todo check this queries are the same ...
  671. if (!empty($this->course->type) && $this->course->type=='partial') {
  672. $sql = 'SELECT * FROM '.$table_glossary.' g WHERE g.c_id = '.$course_id.' '.$session_condition;
  673. } else {
  674. $sql = 'SELECT * FROM '.$table_glossary.' g WHERE g.c_id = '.$course_id.' '.$session_condition;
  675. }
  676. } else {
  677. $table_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  678. //@todo check this queries are the same ... ayayay
  679. if (!empty($this->course->type) && $this->course->type=='partial') {
  680. $sql = 'SELECT * FROM '.$table_glossary.' g WHERE g.c_id = '.$course_id.' AND session_id = 0';
  681. } else {
  682. $sql = 'SELECT * FROM '.$table_glossary.' g WHERE g.c_id = '.$course_id.' AND session_id = 0';
  683. }
  684. }
  685. $db_result = Database::query($sql);
  686. while ($obj = Database::fetch_object($db_result)) {
  687. $doc = new Glossary($obj->glossary_id, $obj->name, $obj->description, $obj->display_order);
  688. $this->course->add_resource($doc);
  689. }
  690. }
  691. /*
  692. * build session course by jhon
  693. * */
  694. function build_session_course(){
  695. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  696. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  697. $list_course = Database::get_course_list();
  698. $list = array();
  699. foreach($list_course as $_course) {
  700. $this->course = new Course();
  701. $this->course->code = $_course['code'];
  702. $this->course->type = 'partial';
  703. $this->course->path = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/';
  704. $this->course->backup_path = api_get_path(SYS_COURSE_PATH).$_course['directory'];
  705. $this->course->encoding = api_get_system_encoding(); //current platform encoding
  706. $code_course = $_course['code'];
  707. $sql_session = "SELECT id, name, course_code FROM $tbl_session_course
  708. INNER JOIN $tbl_session ON id_session = id
  709. WHERE course_code = '$code_course' ";
  710. $query_session = Database::query($sql_session);
  711. while ($rows_session = Database::fetch_assoc($query_session)) {
  712. $session = new CourseSession($rows_session['id'], $rows_session['name']);
  713. $this->course->add_resource($session);
  714. }
  715. $list[] = $this->course;
  716. }
  717. return $list;
  718. }
  719. function build_wiki($session_id = 0, $course_code = 0, $with_base_content = false) {
  720. $course_info = api_get_course_info($course_code);
  721. $tbl_wiki = Database::get_course_table(TABLE_WIKI);
  722. $course_id = $course_info['real_id'];
  723. if (!empty($session_id) && !empty($course_code)) {
  724. $session_id = intval($session_id);
  725. if ($with_base_content) {
  726. $session_condition = api_get_session_condition($session_id, true, true);
  727. } else {
  728. $session_condition = api_get_session_condition($session_id, true);
  729. }
  730. $sql = 'SELECT * FROM ' . $tbl_wiki . ' WHERE c_id = '.$course_id.' '.$session_condition;
  731. } else {
  732. $tbl_wiki = Database::get_course_table(TABLE_WIKI);
  733. $sql = 'SELECT * FROM ' . $tbl_wiki . ' WHERE c_id = '.$course_id.' AND session_id = 0';
  734. }
  735. $db_result = Database::query($sql);
  736. while ($obj = Database::fetch_object($db_result)) {
  737. $wiki = new Wiki($obj->id, $obj->page_id, $obj->reflink, $obj->title, $obj->content, $obj->user_id, $obj->group_id, $obj->dtime, $obj->progress, $obj->version);
  738. $this->course->add_resource($wiki);
  739. }
  740. }
  741. /**
  742. * Build the Surveys
  743. */
  744. function build_thematic() {
  745. $table_thematic = Database :: get_course_table(TABLE_THEMATIC);
  746. $table_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
  747. $table_thematic_plan = Database :: get_course_table(TABLE_THEMATIC_PLAN);
  748. $course_id = api_get_course_int_id();
  749. $sql = 'SELECT * FROM '.$table_thematic.' WHERE c_id = '.$course_id.' AND session_id = 0 ';
  750. $db_result = Database::query($sql);
  751. while ($row = Database::fetch_array($db_result,'ASSOC')) {
  752. $thematic = new Thematic($row);
  753. $sql = 'SELECT * FROM '.$table_thematic_advance.' WHERE c_id = '.$course_id.' AND thematic_id = '.$row['id'];
  754. $result = Database::query($sql);
  755. while ($sub_row = Database::fetch_array($result,'ASSOC')) {
  756. $thematic->add_thematic_advance($sub_row);
  757. }
  758. $sql = 'SELECT * FROM '.$table_thematic_plan.' WHERE c_id = '.$course_id.' AND thematic_id = '.$row['id'];
  759. $result = Database::query($sql);
  760. while ($sub_row = Database::fetch_array($result,'ASSOC')) {
  761. $thematic->add_thematic_plan($sub_row);
  762. }
  763. $this->course->add_resource($thematic);
  764. }
  765. }
  766. /**
  767. * Build the Surveys
  768. */
  769. function build_attendance() {
  770. $table_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  771. $table_attendance_calendar = Database :: get_course_table(TABLE_ATTENDANCE_CALENDAR);
  772. $course_id = api_get_course_int_id();
  773. $sql = 'SELECT * FROM '.$table_attendance.' WHERE c_id = '.$course_id.' AND session_id = 0 ';
  774. $db_result = Database::query($sql);
  775. while ($row = Database::fetch_array($db_result,'ASSOC')) {
  776. $obj = new Attendance($row);
  777. $sql = 'SELECT * FROM '.$table_attendance_calendar.' WHERE c_id = '.$course_id.' AND attendance_id = '.$row['id'];
  778. $result = Database::query($sql);
  779. while ($sub_row = Database::fetch_array($result,'ASSOC')) {
  780. $obj->add_attendance_calendar($sub_row);
  781. }
  782. $this->course->add_resource($obj);
  783. }
  784. }
  785. }