CourseBuilder.class.php 40 KB

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