CourseBuilder.class.php 38 KB

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