CourseBuilder.class.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. require_once 'Work.class.php';
  26. /**
  27. * Class which can build a course-object from a Chamilo-course.
  28. * @author Bart Mollet <bart.mollet@hogent.be>
  29. * @package chamilo.backup
  30. */
  31. class CourseBuilder
  32. {
  33. /** Course */
  34. public $course;
  35. /* With this array you can filter the tools you want to be parsed by default all tools are included*/
  36. public $tools_to_build = array(
  37. 'announcements',
  38. 'attendance',
  39. 'course_descriptions',
  40. 'documents',
  41. 'events',
  42. 'forum_category',
  43. 'forums',
  44. 'forum_topics',
  45. 'glossary',
  46. 'quizzes',
  47. 'learnpaths',
  48. 'links',
  49. 'surveys',
  50. 'tool_intro',
  51. 'thematic',
  52. 'wiki',
  53. 'works'
  54. );
  55. /* With this array you can filter wich elements of the tools are going to be added in the course obj (only works with LPs) */
  56. public $specific_id_list = array();
  57. /**
  58. * Create a new CourseBuilder
  59. */
  60. public function __construct($type='', $course = null)
  61. {
  62. $_course = api_get_course_info();
  63. if (!empty($course['official_code'])){
  64. $_course = $course;
  65. }
  66. $this->course = new Course();
  67. $this->course->code = $_course['official_code'];
  68. $this->course->type = $type;
  69. $this->course->path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/';
  70. $this->course->backup_path = api_get_path(SYS_COURSE_PATH).$_course['path'];
  71. $this->course->encoding = api_get_system_encoding(); //current platform encoding
  72. $this->course->db_name = $_course['dbName'];
  73. $this->course->info = $_course;
  74. }
  75. /**
  76. *
  77. * @param type $array
  78. */
  79. function set_tools_to_build($array)
  80. {
  81. $this->tools_to_build = $array;
  82. }
  83. /**
  84. *
  85. * @param type $array
  86. */
  87. function set_tools_specific_id_list($array)
  88. {
  89. $this->specific_id_list = $array;
  90. }
  91. /**
  92. * Get the created course
  93. * @return course The course
  94. */
  95. function get_course()
  96. {
  97. return $this->course;
  98. }
  99. /**
  100. * Build the course-object
  101. *
  102. * @param int session_id
  103. * @param string course_code
  104. * @param bool true if you want to get the elements that exists in the course and
  105. * in the session, (session_id = 0 or session_id = X)
  106. */
  107. function build($session_id = 0, $course_code = '', $with_base_content = false)
  108. {
  109. $table_link = Database :: get_course_table(TABLE_LINKED_RESOURCES);
  110. $table_properties = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  111. $course_info = api_get_course_info($course_code);
  112. $course_id = $course_info['real_id'];
  113. foreach ($this->tools_to_build as $tool) {
  114. $function_build = 'build_'.$tool;
  115. $specificIdList = isset($this->specific_id_list[$tool]) ? $this->specific_id_list[$tool] : null;
  116. $this->$function_build($session_id, $course_code, $with_base_content, $specificIdList);
  117. }
  118. //TABLE_LINKED_RESOURCES is the "resource" course table, which is deprecated, apparently
  119. foreach ($this->course->resources as $type => $resources) {
  120. foreach ($resources as $id => $resource) {
  121. $sql = "SELECT * FROM ".$table_link." WHERE c_id = $course_id AND source_type = '".$resource->get_type()."' AND source_id = '".$resource->get_id()."'";
  122. $res = Database::query($sql);
  123. while ($link = Database::fetch_object($res)) {
  124. $this->course->resources[$type][$id]->add_linked_resource($link->resource_type, $link->resource_id);
  125. }
  126. }
  127. }
  128. // Once we've built the resouces array a bit more, try to get items
  129. // from the item_propry table and order them in the "resources" array
  130. foreach ($this->course->resources as $type => $resources) {
  131. foreach ($resources as $id => $resource) {
  132. $tool = $resource->get_tool();
  133. if ($tool != null) {
  134. $sql = "SELECT * FROM $table_properties WHERE c_id = $course_id AND TOOL = '".$tool."' AND ref='".$resource->get_id()."'";
  135. $res = Database::query($sql);
  136. $all_properties = array ();
  137. while ($item_property = Database::fetch_array($res)) {
  138. $all_properties[]= $item_property;
  139. }
  140. $this->course->resources[$type][$id]->item_properties = $all_properties;
  141. }
  142. }
  143. }
  144. return $this->course;
  145. }
  146. /**
  147. * Build the documents
  148. */
  149. function build_documents($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array())
  150. {
  151. $course_info = api_get_course_info($course_code);
  152. $course_id = $course_info['real_id'];
  153. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  154. $table_prop = Database::get_course_table(TABLE_ITEM_PROPERTY);
  155. //Remove chat_files and shared_folder files
  156. $avoid_paths = " path NOT LIKE '/shared_folder%' AND
  157. path NOT LIKE '/chat_files%' ";
  158. //$avoid_paths = " 1 = 1 ";
  159. if (!empty($course_code) && !empty($session_id)) {
  160. $session_id = intval($session_id);
  161. if ($with_base_content) {
  162. $session_condition = api_get_session_condition($session_id, true, true);
  163. } else {
  164. $session_condition = api_get_session_condition($session_id, true);
  165. }
  166. if (!empty($this->course->type) && $this->course->type=='partial') {
  167. $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size FROM $table_doc d, $table_prop p
  168. WHERE d.c_id = $course_id AND
  169. p.c_id = $course_id AND
  170. tool = '".TOOL_DOCUMENT."' AND
  171. p.ref = d.id AND
  172. p.visibility != 2 AND
  173. path NOT LIKE '/images/gallery%' AND
  174. $avoid_paths
  175. $session_condition
  176. ORDER BY path";
  177. } else {
  178. $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size FROM $table_doc d, $table_prop p
  179. WHERE d.c_id = $course_id AND
  180. p.c_id = $course_id AND
  181. tool = '".TOOL_DOCUMENT."' AND
  182. $avoid_paths AND
  183. p.ref = d.id AND
  184. p.visibility != 2 $session_condition
  185. ORDER BY path";
  186. }
  187. $db_result = Database::query($sql);
  188. while ($obj = Database::fetch_object($db_result)) {
  189. $doc = new Document($obj->id, $obj->path, $obj->comment, $obj->title, $obj->filetype, $obj->size);
  190. $this->course->add_resource($doc);
  191. }
  192. } else {
  193. if (!empty($this->course->type) && $this->course->type=='partial')
  194. $sql = 'SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size FROM '.$table_doc.' d, '.$table_prop.' p
  195. WHERE d.c_id = '.$course_id.' AND
  196. p.c_id = '.$course_id.' AND
  197. tool = \''.TOOL_DOCUMENT.'\' AND
  198. p.ref = d.id AND
  199. p.visibility != 2 AND
  200. path NOT LIKE \'/images/gallery%\' AND
  201. '.$avoid_paths.'AND
  202. d.session_id = 0
  203. ORDER BY path';
  204. else
  205. $sql = 'SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
  206. FROM '.$table_doc.' d, '.$table_prop.' p
  207. WHERE d.c_id = '.$course_id.' AND
  208. p.c_id = '.$course_id.' AND
  209. tool = \''.TOOL_DOCUMENT.'\' AND
  210. p.ref = d.id AND
  211. p.visibility != 2 AND
  212. '.$avoid_paths.' AND
  213. d.session_id = 0
  214. ORDER BY path';
  215. $db_result = Database::query($sql);
  216. while ($obj = Database::fetch_object($db_result)) {
  217. $doc = new Document($obj->id, $obj->path, $obj->comment, $obj->title, $obj->filetype, $obj->size);
  218. $this->course->add_resource($doc);
  219. }
  220. }
  221. }
  222. /**
  223. * Build the forums
  224. */
  225. function build_forums($session_id = 0, $course_code = null, $with_base_content = false, $id_list = array())
  226. {
  227. $course_info = api_get_course_info($course_code);
  228. $course_id = $course_info['real_id'];
  229. $table = Database :: get_course_table(TABLE_FORUM);
  230. $sql = "SELECT * FROM $table WHERE c_id = $course_id ";
  231. $sql .= " ORDER BY forum_title, forum_category";
  232. $db_result = Database::query($sql);
  233. while ($obj = Database::fetch_object($db_result)) {
  234. $forum = new Forum($obj);
  235. $this->course->add_resource($forum);
  236. }
  237. }
  238. /**
  239. * Build a forum-category
  240. */
  241. function build_forum_category($session_id = 0, $course_code = null, $with_base_content = false, $id_list = array())
  242. {
  243. $table = Database :: get_course_table(TABLE_FORUM_CATEGORY);
  244. $course_info = api_get_course_info($course_code);
  245. $course_id = $course_info['real_id'];
  246. $sql = "SELECT * FROM $table WHERE c_id = $course_id ORDER BY cat_title";
  247. $db_result = Database::query($sql);
  248. while ($obj = Database::fetch_object($db_result)) {
  249. $forum_category = new ForumCategory($obj);
  250. $this->course->add_resource($forum_category);
  251. }
  252. }
  253. /**
  254. * Build the forum-topics
  255. */
  256. function build_forum_topics($session_id = 0, $course_code = null, $with_base_content = false, $id_list = array())
  257. {
  258. $table = Database :: get_course_table(TABLE_FORUM_THREAD);
  259. $course_info = api_get_course_info($course_code);
  260. $course_id = $course_info['real_id'];
  261. $sql = "SELECT * FROM $table WHERE c_id = $course_id
  262. ORDER BY thread_title ";
  263. $db_result = Database::query($sql);
  264. while ($obj = Database::fetch_object($db_result)) {
  265. $forum_topic = new ForumTopic($obj);
  266. $this->course->add_resource($forum_topic);
  267. $this->build_forum_posts($obj->thread_id, $obj->forum_id, true);
  268. }
  269. }
  270. /**
  271. * Build the forum-posts
  272. * TODO: All tree structure of posts should be built, attachments for example.
  273. */
  274. function build_forum_posts($thread_id = null, $forum_id = null, $only_first_post = false)
  275. {
  276. $table = Database :: get_course_table(TABLE_FORUM_POST);
  277. $course_id = api_get_course_int_id();
  278. $sql = "SELECT * FROM $table WHERE c_id = $course_id ";
  279. if (!empty($thread_id) && !empty($forum_id)) {
  280. $forum_id = intval($forum_id);
  281. $thread_id = intval($thread_id);
  282. $sql .= " AND thread_id = $thread_id AND forum_id = $forum_id ";
  283. }
  284. $sql .= " ORDER BY post_id ASC LIMIT 1";
  285. $db_result = Database::query($sql);
  286. while ($obj = Database::fetch_object($db_result)) {
  287. $forum_post = new ForumPost($obj);
  288. $this->course->add_resource($forum_post);
  289. }
  290. }
  291. /**
  292. * Build the links
  293. */
  294. function build_links($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array())
  295. {
  296. $course_info = api_get_course_info($course_code);
  297. $course_id = $course_info['real_id'];
  298. $table = Database :: get_course_table(TABLE_LINK);
  299. $table_prop = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  300. if (!empty($session_id) && !empty($course_code)) {
  301. $session_id = intval($session_id);
  302. if ($with_base_content) {
  303. $session_condition = api_get_session_condition($session_id, true, true);
  304. } else {
  305. $session_condition = api_get_session_condition($session_id, true);
  306. }
  307. $sql = "SELECT l.id, l.title, l.url, l.description, l.category_id, l.on_homepage
  308. FROM $table l, $table_prop p
  309. 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
  310. ORDER BY l.display_order";
  311. } else {
  312. $sql = "SELECT l.id, l.title, l.url, l.description, l.category_id, l.on_homepage
  313. FROM $table l, $table_prop p
  314. 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
  315. ORDER BY l.display_order";
  316. }
  317. $db_result = Database::query($sql);
  318. while ($obj = Database::fetch_object($db_result)) {
  319. $link = new Link($obj->id, $obj->title, $obj->url, $obj->description, $obj->category_id, $obj->on_homepage);
  320. $this->course->add_resource($link);
  321. if (!empty($course_code)) {
  322. $res = $this->build_link_category($obj->category_id,$course_code);
  323. } else {
  324. $res = $this->build_link_category($obj->category_id);
  325. }
  326. if($res > 0) {
  327. $this->course->resources[RESOURCE_LINK][$obj->id]->add_linked_resource(RESOURCE_LINKCATEGORY, $obj->category_id);
  328. }
  329. }
  330. }
  331. /**
  332. * Build tool intro
  333. */
  334. function build_tool_intro($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array())
  335. {
  336. $table = Database :: get_course_table(TABLE_TOOL_INTRO);
  337. $course_id = api_get_course_int_id();
  338. $sql = "SELECT * FROM $table WHERE c_id = $course_id ";
  339. $db_result = Database::query($sql);
  340. while ($obj = Database::fetch_object($db_result)) {
  341. $tool_intro = new ToolIntro($obj->id, $obj->intro_text);
  342. $this->course->add_resource($tool_intro);
  343. }
  344. }
  345. /**
  346. * Build a link category
  347. */
  348. function build_link_category($id, $course_code = '')
  349. {
  350. $course_info = api_get_course_info($course_code);
  351. $course_id = $course_info['real_id'];
  352. $link_cat_table = Database :: get_course_table(TABLE_LINK_CATEGORY);
  353. $sql = "SELECT * FROM $link_cat_table WHERE c_id = $course_id AND id = $id";
  354. $db_result = Database::query($sql);
  355. while ($obj = Database::fetch_object($db_result)) {
  356. $link_category = new LinkCategory($obj->id, $obj->category_title, $obj->description, $obj->display_order);
  357. $this->course->add_resource($link_category);
  358. return $id;
  359. }
  360. return 0;
  361. }
  362. /**
  363. * Build the Quizzes
  364. */
  365. function build_quizzes($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array())
  366. {
  367. $course_info = api_get_course_info($course_code);
  368. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
  369. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  370. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  371. $course_id = $course_info['real_id'];
  372. if (!empty($course_code) && !empty($session_id)) {
  373. $session_id = intval($session_id);
  374. if ($with_base_content) {
  375. $session_condition = api_get_session_condition($session_id, true, true);
  376. } else {
  377. $session_condition = api_get_session_condition($session_id, true);
  378. }
  379. $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)
  380. } else {
  381. $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)
  382. }
  383. $db_result = Database::query($sql);
  384. while ($obj = Database::fetch_object($db_result)) {
  385. if (strlen($obj->sound) > 0) {
  386. $sql = "SELECT id FROM $table_doc WHERE c_id = $course_id AND path = '/audio/".$obj->sound."'";
  387. $res = Database::query($sql);
  388. $doc = Database::fetch_object($res);
  389. $obj->sound = $doc->id;
  390. }
  391. $quiz = new Quiz($obj);
  392. $sql = 'SELECT * FROM '.$table_rel.' WHERE c_id = '.$course_id.' AND exercice_id = '.$obj->id;
  393. $db_result2 = Database::query($sql);
  394. while ($obj2 = Database::fetch_object($db_result2)) {
  395. $quiz->add_question($obj2->question_id, $obj2->question_order);
  396. }
  397. $this->course->add_resource($quiz);
  398. }
  399. if (!empty($course_code)) {
  400. $this->build_quiz_questions($course_code);
  401. } else {
  402. $this->build_quiz_questions();
  403. }
  404. }
  405. /**
  406. * Build the Quiz-Questions
  407. */
  408. function build_quiz_questions($course_code = null)
  409. {
  410. $course_info = api_get_course_info($course_code);
  411. $course_id = $course_info['real_id'];
  412. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
  413. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  414. $table_que = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  415. $table_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  416. // Building normal tests.
  417. $sql = "SELECT * FROM $table_que WHERE c_id = $course_id ";
  418. $db_result = Database::query($sql);
  419. while ($obj = Database::fetch_object($db_result)) {
  420. $question = new QuizQuestion($obj->id, $obj->question, $obj->description, $obj->ponderation, $obj->type, $obj->position, $obj->picture, $obj->level, $obj->extra);
  421. $sql = 'SELECT * FROM '.$table_ans.' WHERE c_id = '.$course_id.' AND question_id = '.$obj->id;
  422. $db_result2 = Database::query($sql);
  423. while ($obj2 = Database::fetch_object($db_result2)) {
  424. $question->add_answer($obj2->id, $obj2->answer, $obj2->correct, $obj2->comment, $obj2->ponderation, $obj2->position, $obj2->hotspot_coordinates, $obj2->hotspot_type);
  425. if ($obj->type == MULTIPLE_ANSWER_TRUE_FALSE) {
  426. $table_options = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  427. $sql = 'SELECT * FROM '.$table_options.' WHERE c_id = '.$course_id.' AND question_id = '.$obj->id;
  428. $db_result3 = Database::query($sql);
  429. while ($obj3 = Database::fetch_object($db_result3)) {
  430. $question_option = new QuizQuestionOption($obj3);
  431. $question->add_option($question_option);
  432. }
  433. }
  434. }
  435. $this->course->add_resource($question);
  436. }
  437. // Building a fictional test for collecting orphan questions.
  438. $build_orphan_questions = !empty($_POST['recycle_option']); // When a course is emptied this option should be activated (true).
  439. //1st union gets the orphan questions from deleted exercises
  440. //2nd union gets the orphan questions from question that were deleted in a exercise.
  441. $sql = " (
  442. SELECT question_id, q.* FROM $table_que q INNER JOIN $table_rel r
  443. ON (q.c_id = r.c_id AND q.id = r.question_id)
  444. INNER JOIN $table_qui ex
  445. ON (ex.id = r.exercice_id AND ex.c_id = r.c_id )
  446. WHERE ex.c_id = $course_id AND ex.active = '-1'
  447. )
  448. UNION
  449. (
  450. SELECT question_id, q.* FROM $table_que q left
  451. OUTER JOIN $table_rel r
  452. ON (q.c_id = r.c_id AND q.id = r.question_id)
  453. WHERE q.c_id = $course_id AND r.question_id is null
  454. )
  455. UNION
  456. (
  457. SELECT question_id, q.* FROM $table_que q
  458. INNER JOIN $table_rel r
  459. ON (q.c_id = r.c_id AND q.id = r.question_id)
  460. WHERE r.c_id = $course_id AND (r.exercice_id = '-1' OR r.exercice_id = '0')
  461. )
  462. ";
  463. $db_result = Database::query($sql);
  464. if (Database::num_rows($db_result) > 0) {
  465. $build_orphan_questions = true;
  466. $orphanQuestionIds = array();
  467. while ($obj = Database::fetch_object($db_result)) {
  468. //Avoid adding the same question twice
  469. $obj->id = $obj->question_id;
  470. if (!isset($this->course->resources[$obj->id])) {
  471. $question = new QuizQuestion($obj->id, $obj->question, $obj->description, $obj->ponderation, $obj->type, $obj->position, $obj->picture,$obj->level, $obj->extra);
  472. $sql = "SELECT * FROM $table_ans WHERE c_id = $course_id AND question_id = ".$obj->id;
  473. $db_result2 = Database::query($sql);
  474. if (Database::num_rows($db_result2)) {
  475. while ($obj2 = Database::fetch_object($db_result2)) {
  476. $question->add_answer($obj2->id, $obj2->answer, $obj2->correct, $obj2->comment, $obj2->ponderation, $obj2->position, $obj2->hotspot_coordinates, $obj2->hotspot_type);
  477. }
  478. $orphanQuestionIds[] = $obj->id;
  479. }
  480. $this->course->add_resource($question);
  481. }
  482. }
  483. }
  484. if ($build_orphan_questions) {
  485. $obj = array(
  486. 'id' => -1,
  487. 'title' => get_lang('OrphanQuestions', ''),
  488. 'type' => 2
  489. );
  490. $newQuiz = new Quiz((object)$obj);
  491. if (!empty($orphanQuestionIds)) {
  492. foreach ($orphanQuestionIds as $index => $orphanId) {
  493. $order = $index + 1;
  494. $newQuiz->add_question($orphanId, $order);
  495. }
  496. }
  497. $this->course->add_resource($newQuiz);
  498. }
  499. }
  500. /**
  501. * Build the orphan questions
  502. */
  503. function build_quiz_orphan_questions()
  504. {
  505. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
  506. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  507. $table_que = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  508. $table_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  509. $course_id = api_get_course_int_id();
  510. $sql = 'SELECT *
  511. FROM '.$table_que.' as questions
  512. LEFT JOIN '.$table_rel.' as quizz_questions
  513. ON questions.id=quizz_questions.question_id
  514. LEFT JOIN '.$table_qui.' as exercices
  515. ON exercice_id=exercices.id
  516. WHERE questions.c_id = '.$course_id.' AND
  517. quizz_questions.c_id = '.$course_id.' AND
  518. exercices.c_id = '.$course_id.' AND
  519. (quizz_questions.exercice_id IS NULL OR
  520. exercices.active = -1)';
  521. $db_result = Database::query($sql);
  522. if (Database::num_rows($db_result) > 0) {
  523. $orphan_questions = new Quiz(-1, get_lang('OrphanQuestions', ''), '', 0, 0, 1, '', 0); // Tjis is the fictional test for collecting orphan questions.
  524. $this->course->add_resource($orphan_questions);
  525. while ($obj = Database::fetch_object($db_result)) {
  526. $question = new QuizQuestion($obj->id, $obj->question, $obj->description, $obj->ponderation, $obj->type, $obj->position, $obj->picture,$obj->level,$obj->extra);
  527. $sql = 'SELECT * FROM '.$table_ans.' WHERE question_id = '.$obj->id;
  528. $db_result2 = Database::query($sql);
  529. while ($obj2 = Database::fetch_object($db_result2)) {
  530. $question->add_answer($obj2->id, $obj2->answer, $obj2->correct, $obj2->comment, $obj2->ponderation, $obj2->position, $obj2->hotspot_coordinates, $obj2->hotspot_type);
  531. }
  532. $this->course->add_resource($question);
  533. }
  534. }
  535. }
  536. /**
  537. * Build the Surveys
  538. */
  539. function build_surveys($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array())
  540. {
  541. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  542. $table_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  543. $course_id = api_get_course_int_id();
  544. $sql = 'SELECT * FROM '.$table_survey.' WHERE c_id = '.$course_id.' AND session_id = 0 ';
  545. $db_result = Database::query($sql);
  546. while ($obj = Database::fetch_object($db_result)) {
  547. $survey = new Survey($obj->survey_id, $obj->code,$obj->title,
  548. $obj->subtitle, $obj->author, $obj->lang,
  549. $obj->avail_from, $obj->avail_till, $obj->is_shared,
  550. $obj->template, $obj->intro, $obj->surveythanks,
  551. $obj->creation_date, $obj->invited, $obj->answered,
  552. $obj->invite_mail, $obj->reminder_mail);
  553. $sql = 'SELECT * FROM '.$table_question.' WHERE c_id = '.$course_id.' AND survey_id = '.$obj->survey_id;
  554. $db_result2 = Database::query($sql);
  555. while ($obj2 = Database::fetch_object($db_result2)){
  556. $survey->add_question($obj2->question_id);
  557. }
  558. $this->course->add_resource($survey);
  559. }
  560. $this->build_survey_questions();
  561. }
  562. /**
  563. * Build the Survey Questions
  564. */
  565. function build_survey_questions() {
  566. $table_que = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  567. $table_opt = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  568. $course_id = api_get_course_int_id();
  569. $sql = 'SELECT * FROM '.$table_que.' WHERE c_id = '.$course_id.' ';
  570. $db_result = Database::query($sql);
  571. while ($obj = Database::fetch_object($db_result)){
  572. $question = new SurveyQuestion($obj->question_id, $obj->survey_id,
  573. $obj->survey_question, $obj->survey_question_comment,
  574. $obj->type, $obj->display, $obj->sort,
  575. $obj->shared_question_id, $obj->max_value);
  576. $sql = 'SELECT * FROM '.$table_opt.' WHERE c_id = '.$course_id.' AND question_id = '.$obj->question_id;
  577. $db_result2 = Database::query($sql);
  578. while ($obj2 = Database::fetch_object($db_result2)) {
  579. $question->add_answer($obj2->option_text, $obj2->sort);
  580. }
  581. $this->course->add_resource($question);
  582. }
  583. }
  584. /**
  585. * Build the announcements
  586. */
  587. function build_announcements($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array()) {
  588. $table = Database :: get_course_table(TABLE_ANNOUNCEMENT);
  589. $course_id = api_get_course_int_id();
  590. $sql = 'SELECT * FROM '.$table.' WHERE c_id = '.$course_id.' AND session_id = 0';
  591. $db_result = Database::query($sql);
  592. $table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  593. while ($obj = Database::fetch_object($db_result)) {
  594. $sql = 'SELECT path, comment, filename, size FROM '.$table_attachment.' WHERE c_id = '.$course_id.' AND announcement_id = '.$obj->id.'';
  595. $result = Database::query($sql);
  596. $attachment_obj = Database::fetch_object($result);
  597. $att_path = $att_filename = $att_size = $atth_comment = '';
  598. if (!empty($attachment_obj)) {
  599. $att_path = $attachment_obj->path;
  600. $att_filename = $attachment_obj->filename;
  601. $att_size = $attachment_obj->size;
  602. $atth_comment = $attachment_obj->comment;
  603. }
  604. $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);
  605. $this->course->add_resource($announcement);
  606. }
  607. }
  608. /**
  609. * Build the events
  610. */
  611. function build_events($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array()) {
  612. $table = Database :: get_course_table(TABLE_AGENDA);
  613. $course_id = api_get_course_int_id();
  614. $sql = 'SELECT * FROM '.$table.' WHERE c_id = '.$course_id.' AND session_id = 0';
  615. $db_result = Database::query($sql);
  616. while ($obj = Database::fetch_object($db_result)) {
  617. $table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT);
  618. $sql = 'SELECT path, comment, filename, size FROM '.$table_attachment.' WHERE c_id = '.$course_id.' AND agenda_id = '.$obj->id.'';
  619. $result = Database::query($sql);
  620. $attachment_obj = Database::fetch_object($result);
  621. $att_path = $att_filename = $att_size = $atth_comment = '';
  622. if (!empty($attachment_obj)) {
  623. $att_path = $attachment_obj->path;
  624. $att_filename = $attachment_obj->filename;
  625. $att_size = $attachment_obj->size;
  626. $atth_comment = $attachment_obj->comment;
  627. }
  628. $event = new Event($obj->id, $obj->title, $obj->content, $obj->start_date, $obj->end_date, $att_path, $att_filename, $att_size, $atth_comment, $obj->all_day);
  629. $this->course->add_resource($event);
  630. }
  631. }
  632. /**
  633. * Build the course-descriptions
  634. */
  635. function build_course_descriptions($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array()) {
  636. $course_info = api_get_course_info($course_code);
  637. $course_id = $course_info['real_id'];
  638. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  639. if (!empty($session_id) && !empty($course_code)) {
  640. $session_id = intval($session_id);
  641. if ($with_base_content) {
  642. $session_condition = api_get_session_condition($session_id, true, true);
  643. } else {
  644. $session_condition = api_get_session_condition($session_id, true);
  645. }
  646. $sql = 'SELECT * FROM '.$table. ' WHERE c_id = '.$course_id.' '.$session_condition;
  647. } else {
  648. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  649. $sql = 'SELECT * FROM '.$table. ' WHERE c_id = '.$course_id.' AND session_id = 0';
  650. }
  651. $db_result = Database::query($sql);
  652. while ($obj = Database::fetch_object($db_result)) {
  653. $cd = new CourseDescription($obj->id, $obj->title, $obj->content, $obj->description_type);
  654. $this->course->add_resource($cd);
  655. }
  656. }
  657. /**
  658. * Build the learnpaths
  659. */
  660. function build_learnpaths($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array()) {
  661. $course_info = api_get_course_info($course_code);
  662. $course_id = $course_info['real_id'];
  663. $table_main = Database::get_course_table(TABLE_LP_MAIN);
  664. $table_item = Database::get_course_table(TABLE_LP_ITEM);
  665. $table_tool = Database::get_course_table(TABLE_TOOL_LIST);
  666. if (!empty($session_id) && !empty($course_code)) {
  667. $session_id = intval($session_id);
  668. if ($with_base_content) {
  669. $session_condition = api_get_session_condition($session_id, true, true);
  670. } else {
  671. $session_condition = api_get_session_condition($session_id, true);
  672. }
  673. $sql = 'SELECT * FROM '.$table_main.' WHERE c_id = '.$course_id.' '.$session_condition;
  674. } else {
  675. $sql = 'SELECT * FROM '.$table_main.' WHERE c_id = '.$course_id.' AND session_id = 0';
  676. }
  677. if (!empty($id_list)) {
  678. $id_list = array_map('intval', $id_list);
  679. $sql .=" AND id IN (".implode(', ', $id_list).") ";
  680. }
  681. $db_result = Database::query($sql);
  682. if ($db_result)
  683. while ($obj = Database::fetch_object($db_result)) {
  684. $items = array();
  685. $sql_items = "SELECT * FROM ".$table_item." WHERE c_id = '$course_id' AND lp_id = ".$obj->id;
  686. $db_items = Database::query($sql_items);
  687. while ($obj_item = Database::fetch_object($db_items)) {
  688. $item['id'] = $obj_item->id;
  689. $item['item_type'] = $obj_item->item_type;
  690. $item['ref'] = $obj_item->ref;
  691. $item['title'] = $obj_item->title;
  692. $item['description'] = $obj_item->description;
  693. $item['path'] = $obj_item->path;
  694. $item['min_score'] = $obj_item->min_score;
  695. $item['max_score'] = $obj_item->max_score;
  696. $item['mastery_score'] = $obj_item->mastery_score;
  697. $item['parent_item_id'] = $obj_item->parent_item_id;
  698. $item['previous_item_id'] = $obj_item->previous_item_id;
  699. $item['next_item_id'] = $obj_item->next_item_id;
  700. $item['display_order'] = $obj_item->display_order;
  701. $item['prerequisite'] = $obj_item->prerequisite;
  702. $item['parameters'] = $obj_item->parameters;
  703. $item['launch_data'] = $obj_item->launch_data;
  704. $item['audio'] = $obj_item->audio;
  705. $items[] = $item;
  706. }
  707. $sql_tool = "SELECT id FROM $table_tool
  708. WHERE c_id = $course_id AND
  709. (link LIKE '%lp_controller.php%lp_id=".$obj->id."%' AND image='scormbuilder.gif') AND
  710. visibility = '1' ";
  711. $db_tool = Database::query($sql_tool);
  712. if (Database::num_rows($db_tool)) {
  713. $visibility = '1';
  714. } else {
  715. $visibility = '0';
  716. }
  717. $lp = new CourseCopyLearnpath(
  718. $obj->id,
  719. $obj->lp_type,
  720. $obj->name,
  721. $obj->path,
  722. $obj->ref,
  723. $obj->description,
  724. $obj->content_local,
  725. $obj->default_encoding,
  726. $obj->default_view_mod,
  727. $obj->prevent_reinit,
  728. $obj->force_commit,
  729. $obj->content_maker,
  730. $obj->display_order,
  731. $obj->js_lib,
  732. $obj->content_license,
  733. $obj->debug,
  734. $visibility,
  735. $obj->author,
  736. $obj->preview_image,
  737. $obj->use_max_score,
  738. $obj->autolunch,
  739. $obj->created_on,
  740. $obj->modified_on,
  741. $obj->publicated_on,
  742. $obj->expired_on,
  743. $obj->session_id,
  744. $items);
  745. $this->course->add_resource($lp);
  746. }
  747. //save scorm directory (previously build_scorm_documents())
  748. $i = 1;
  749. if ($dir=@opendir($this->course->backup_path.'/scorm')) {
  750. while($file=readdir($dir)) {
  751. if(is_dir($this->course->backup_path.'/scorm/'.$file) && !in_array($file,array('.','..'))) {
  752. $doc = new ScormDocument($i++, '/'.$file, $file);
  753. $this->course->add_resource($doc);
  754. }
  755. }
  756. closedir($dir);
  757. }
  758. }
  759. /**
  760. * Build the glossarys
  761. */
  762. function build_glossary($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array()) {
  763. $course_info = api_get_course_info($course_code);
  764. $table_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  765. $course_id = $course_info['real_id'];
  766. if (!empty($session_id) && !empty($course_code)) {
  767. $session_id = intval($session_id);
  768. if ($with_base_content) {
  769. $session_condition = api_get_session_condition($session_id, true, true);
  770. } else {
  771. $session_condition = api_get_session_condition($session_id, true);
  772. }
  773. //@todo check this queries are the same ...
  774. if (!empty($this->course->type) && $this->course->type=='partial') {
  775. $sql = 'SELECT * FROM '.$table_glossary.' g WHERE g.c_id = '.$course_id.' '.$session_condition;
  776. } else {
  777. $sql = 'SELECT * FROM '.$table_glossary.' g WHERE g.c_id = '.$course_id.' '.$session_condition;
  778. }
  779. } else {
  780. $table_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  781. //@todo check this queries are the same ... ayayay
  782. if (!empty($this->course->type) && $this->course->type=='partial') {
  783. $sql = 'SELECT * FROM '.$table_glossary.' g WHERE g.c_id = '.$course_id.' AND session_id = 0';
  784. } else {
  785. $sql = 'SELECT * FROM '.$table_glossary.' g WHERE g.c_id = '.$course_id.' AND session_id = 0';
  786. }
  787. }
  788. $db_result = Database::query($sql);
  789. while ($obj = Database::fetch_object($db_result)) {
  790. $doc = new Glossary($obj->glossary_id, $obj->name, $obj->description, $obj->display_order);
  791. $this->course->add_resource($doc);
  792. }
  793. }
  794. /*
  795. * build session course by jhon
  796. * */
  797. function build_session_course(){
  798. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  799. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  800. $list_course = Database::get_course_list();
  801. $list = array();
  802. foreach($list_course as $_course) {
  803. $this->course = new Course();
  804. $this->course->code = $_course['code'];
  805. $this->course->type = 'partial';
  806. $this->course->path = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/';
  807. $this->course->backup_path = api_get_path(SYS_COURSE_PATH).$_course['directory'];
  808. $this->course->encoding = api_get_system_encoding(); //current platform encoding
  809. $code_course = $_course['code'];
  810. $sql_session = "SELECT id, name, course_code FROM $tbl_session_course
  811. INNER JOIN $tbl_session ON id_session = id
  812. WHERE course_code = '$code_course' ";
  813. $query_session = Database::query($sql_session);
  814. while ($rows_session = Database::fetch_assoc($query_session)) {
  815. $session = new CourseSession($rows_session['id'], $rows_session['name']);
  816. $this->course->add_resource($session);
  817. }
  818. $list[] = $this->course;
  819. }
  820. return $list;
  821. }
  822. function build_wiki($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array()) {
  823. $course_info = api_get_course_info($course_code);
  824. $tbl_wiki = Database::get_course_table(TABLE_WIKI);
  825. $course_id = $course_info['real_id'];
  826. if (!empty($session_id) && !empty($course_code)) {
  827. $session_id = intval($session_id);
  828. if ($with_base_content) {
  829. $session_condition = api_get_session_condition($session_id, true, true);
  830. } else {
  831. $session_condition = api_get_session_condition($session_id, true);
  832. }
  833. $sql = 'SELECT * FROM ' . $tbl_wiki . ' WHERE c_id = '.$course_id.' '.$session_condition;
  834. } else {
  835. $tbl_wiki = Database::get_course_table(TABLE_WIKI);
  836. $sql = 'SELECT * FROM ' . $tbl_wiki . ' WHERE c_id = '.$course_id.' AND session_id = 0';
  837. }
  838. $db_result = Database::query($sql);
  839. while ($obj = Database::fetch_object($db_result)) {
  840. $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);
  841. $this->course->add_resource($wiki);
  842. }
  843. }
  844. /**
  845. * Build the Surveys
  846. */
  847. function build_thematic($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array()) {
  848. $table_thematic = Database :: get_course_table(TABLE_THEMATIC);
  849. $table_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
  850. $table_thematic_plan = Database :: get_course_table(TABLE_THEMATIC_PLAN);
  851. $session_id = intval($session_id);
  852. if ($with_base_content) {
  853. $session_condition = api_get_session_condition($session_id, true, true);
  854. } else {
  855. $session_condition = api_get_session_condition($session_id, true);
  856. }
  857. $course_id = api_get_course_int_id();
  858. $sql = "SELECT * FROM $table_thematic WHERE c_id = $course_id $session_condition ";
  859. $db_result = Database::query($sql);
  860. while ($row = Database::fetch_array($db_result,'ASSOC')) {
  861. $thematic = new Thematic($row);
  862. $sql = 'SELECT * FROM '.$table_thematic_advance.' WHERE c_id = '.$course_id.' AND thematic_id = '.$row['id'];
  863. $result = Database::query($sql);
  864. while ($sub_row = Database::fetch_array($result,'ASSOC')) {
  865. $thematic->add_thematic_advance($sub_row);
  866. }
  867. $items = api_get_item_property_by_tool('thematic_plan', api_get_course_id(), $session_id);
  868. //$items_from_session = api_get_item_property_by_tool('thematic_plan', api_get_course_id(), api_get_session_id());
  869. $thematic_plan_id_list = array();
  870. if (!empty($items)) {
  871. foreach($items as $item) {
  872. $thematic_plan_id_list[] = $item['ref'];
  873. //$thematic_plan_complete_list[$item['ref']] = $item;
  874. }
  875. }
  876. //$sql = 'SELECT * FROM '.$table_thematic_plan.' WHERE c_id = '.$course_id.' AND thematic_id = '.$row['id'];
  877. $sql = "SELECT tp.*
  878. FROM $table_thematic_plan tp
  879. INNER JOIN $table_thematic t ON (t.id=tp.thematic_id)
  880. WHERE t.c_id = $course_id AND
  881. tp.c_id = $course_id AND
  882. thematic_id = {$row['id']} AND
  883. tp.id IN (".implode(', ', $thematic_plan_id_list).") ";
  884. $result = Database::query($sql);
  885. while ($sub_row = Database::fetch_array($result,'ASSOC')) {
  886. $thematic->add_thematic_plan($sub_row);
  887. }
  888. $this->course->add_resource($thematic);
  889. }
  890. }
  891. /**
  892. * Build the attendances
  893. */
  894. function build_attendance($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array()) {
  895. $table_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  896. $table_attendance_calendar = Database :: get_course_table(TABLE_ATTENDANCE_CALENDAR);
  897. $course_id = api_get_course_int_id();
  898. $sql = 'SELECT * FROM '.$table_attendance.' WHERE c_id = '.$course_id.' AND session_id = 0 ';
  899. $db_result = Database::query($sql);
  900. while ($row = Database::fetch_array($db_result,'ASSOC')) {
  901. $obj = new Attendance($row);
  902. $sql = 'SELECT * FROM '.$table_attendance_calendar.' WHERE c_id = '.$course_id.' AND attendance_id = '.$row['id'];
  903. $result = Database::query($sql);
  904. while ($sub_row = Database::fetch_array($result,'ASSOC')) {
  905. $obj->add_attendance_calendar($sub_row);
  906. }
  907. $this->course->add_resource($obj);
  908. }
  909. }
  910. /**
  911. * Build the works (or "student publications", or "assignments")
  912. */
  913. function build_works($session_id = 0, $course_code = '', $with_base_content = false, $id_list = array()) {
  914. $table_work = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  915. //$table_work_assignment = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  916. $course_id = api_get_course_int_id();
  917. $sql = 'SELECT * FROM '.$table_work.' WHERE c_id = '.$course_id.' AND session_id = 0 AND filetype = \'folder\' AND parent_id = 0 AND active = 1';
  918. $db_result = Database::query($sql);
  919. while ($row = Database::fetch_array($db_result,'ASSOC')) {
  920. $obj = new Work($row);
  921. $this->course->add_resource($obj);
  922. }
  923. }
  924. }