CourseBuilder.class.php 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'Resource.class.php';
  4. require_once 'Course.class.php';
  5. require_once 'Event.class.php';
  6. require_once 'Link.class.php';
  7. require_once 'ToolIntro.class.php';
  8. require_once 'Document.class.php';
  9. require_once 'ScormDocument.class.php';
  10. require_once 'LinkCategory.class.php';
  11. require_once 'CourseDescription.class.php';
  12. require_once 'ForumPost.class.php';
  13. require_once 'ForumTopic.class.php';
  14. require_once 'Forum.class.php';
  15. require_once 'ForumCategory.class.php';
  16. require_once 'Quiz.class.php';
  17. require_once 'QuizQuestion.class.php';
  18. require_once 'CourseCopyTestCategory.php';
  19. require_once 'CourseCopyLearnpath.class.php';
  20. require_once 'Survey.class.php';
  21. require_once 'SurveyQuestion.class.php';
  22. require_once 'Glossary.class.php';
  23. require_once 'CourseSession.class.php';
  24. require_once 'wiki.class.php';
  25. require_once 'Thematic.class.php';
  26. require_once 'Attendance.class.php';
  27. require_once 'Work.class.php';
  28. require_once 'QuizQuestionOption.class.php';
  29. /**
  30. * Class CourseBuilder
  31. * Builds a course-object from a Chamilo-course.
  32. * @author Bart Mollet <bart.mollet@hogent.be>
  33. * @package chamilo.backup
  34. */
  35. class CourseBuilder
  36. {
  37. /** @var Course */
  38. public $course;
  39. /* With this array you can filter the tools you want to be parsed by
  40. default all tools are included */
  41. public $tools_to_build = array(
  42. 'announcements',
  43. 'attendance',
  44. 'course_descriptions',
  45. 'documents',
  46. 'events',
  47. 'forum_category',
  48. 'forums',
  49. 'forum_topics',
  50. 'glossary',
  51. 'quizzes',
  52. 'test_category',
  53. 'learnpaths',
  54. 'links',
  55. 'surveys',
  56. 'tool_intro',
  57. 'thematic',
  58. 'wiki',
  59. 'works'
  60. );
  61. /* With this array you can filter wich elements of the tools are going
  62. to be added in the course obj (only works with LPs) */
  63. public $specific_id_list = array();
  64. /**
  65. * Create a new CourseBuilder
  66. * @param string $type
  67. * @param null $course
  68. */
  69. public function __construct($type = '', $course = null)
  70. {
  71. $_course = api_get_course_info();
  72. if (!empty($course['official_code'])) {
  73. $_course = $course;
  74. }
  75. $this->course = new Course();
  76. $this->course->code = $_course['official_code'];
  77. $this->course->type = $type;
  78. $this->course->path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/';
  79. $this->course->backup_path = api_get_path(SYS_COURSE_PATH).$_course['path'];
  80. $this->course->encoding = api_get_system_encoding();
  81. $this->course->info = $_course;
  82. }
  83. /**
  84. * @param array $array
  85. */
  86. public function set_tools_to_build($array)
  87. {
  88. $this->tools_to_build = $array;
  89. }
  90. /**
  91. *
  92. * @param array $array
  93. */
  94. public function set_tools_specific_id_list($array)
  95. {
  96. $this->specific_id_list = $array;
  97. }
  98. /**
  99. * Get the created course
  100. * @return course The course
  101. */
  102. public function get_course()
  103. {
  104. return $this->course;
  105. }
  106. /**
  107. * Build the course-object
  108. *
  109. * @param int $session_id
  110. * @param string $courseCode
  111. * @param bool true if you want to get the elements that exists in the course and
  112. * in the session, (session_id = 0 or session_id = X)
  113. * @return object The course object structure
  114. */
  115. public function build(
  116. $session_id = 0,
  117. $courseCode = '',
  118. $with_base_content = false
  119. ) {
  120. $table_link = Database:: get_course_table(TABLE_LINKED_RESOURCES);
  121. $table_properties = Database:: get_course_table(TABLE_ITEM_PROPERTY);
  122. $course = api_get_course_info($courseCode);
  123. $courseId = $course['real_id'];
  124. foreach ($this->tools_to_build as $tool) {
  125. $function_build = 'build_'.$tool;
  126. $specificIdList = isset($this->specific_id_list[$tool]) ? $this->specific_id_list[$tool] : null;
  127. $this->$function_build(
  128. $session_id,
  129. $courseId,
  130. $with_base_content,
  131. $specificIdList
  132. );
  133. }
  134. //TABLE_LINKED_RESOURCES is the "resource" course table, which is deprecated, apparently
  135. foreach ($this->course->resources as $type => $resources) {
  136. foreach ($resources as $id => $resource) {
  137. $sql = "SELECT * FROM $table_link
  138. WHERE
  139. c_id = $courseId AND
  140. source_type = '".$resource->get_type()."' AND
  141. source_id = '".$resource->get_id()."'";
  142. $res = Database::query($sql);
  143. while ($link = Database::fetch_object($res)) {
  144. $this->course->resources[$type][$id]->add_linked_resource(
  145. $link->resource_type,
  146. $link->resource_id
  147. );
  148. }
  149. }
  150. }
  151. // Once we've built the resources array a bit more, try to get items
  152. // from the item_property table and order them in the "resources" array
  153. foreach ($this->course->resources as $type => $resources) {
  154. foreach ($resources as $id => $resource) {
  155. $tool = $resource->get_tool();
  156. if ($tool != null) {
  157. $sql = "SELECT * FROM $table_properties
  158. WHERE
  159. c_id = $courseId AND
  160. tool = '".$tool."' AND
  161. ref = '".$resource->get_id()."'";
  162. $res = Database::query($sql);
  163. $all_properties = array();
  164. while ($item_property = Database::fetch_array($res)) {
  165. $all_properties[] = $item_property;
  166. }
  167. $this->course->resources[$type][$id]->item_properties = $all_properties;
  168. }
  169. }
  170. }
  171. return $this->course;
  172. }
  173. /**
  174. * Build the documents
  175. * @param int $session_id
  176. * @param int $courseId
  177. * @param bool $with_base_content
  178. * @param array $id_list
  179. */
  180. public function build_documents(
  181. $session_id = 0,
  182. $courseId = 0,
  183. $with_base_content = false,
  184. $id_list = array()
  185. ) {
  186. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  187. $table_prop = Database::get_course_table(TABLE_ITEM_PROPERTY);
  188. //Remove chat_files and shared_folder files
  189. $avoid_paths = " path NOT LIKE '/shared_folder%' AND
  190. path NOT LIKE '/chat_files%' ";
  191. //$avoid_paths = " 1 = 1 ";
  192. if (!empty($courseId) && !empty($session_id)) {
  193. $session_id = intval($session_id);
  194. if ($with_base_content) {
  195. $session_condition = api_get_session_condition(
  196. $session_id,
  197. true,
  198. true,
  199. 'd.session_id'
  200. );
  201. } else {
  202. $session_condition = api_get_session_condition(
  203. $session_id,
  204. true,
  205. false,
  206. 'd.session_id'
  207. );
  208. }
  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
  211. FROM $table_doc d INNER JOIN $table_prop p
  212. ON (p.ref = d.id AND d.c_id = p.c_id)
  213. WHERE
  214. d.c_id = $courseId AND
  215. p.c_id = $courseId AND
  216. tool = '".TOOL_DOCUMENT."' AND
  217. p.visibility != 2 AND
  218. path NOT LIKE '/images/gallery%' AND
  219. $avoid_paths
  220. $session_condition
  221. ORDER BY path";
  222. } else {
  223. $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
  224. FROM $table_doc d INNER JOIN $table_prop p
  225. ON (p.ref = d.id AND d.c_id = p.c_id)
  226. WHERE
  227. d.c_id = $courseId AND
  228. p.c_id = $courseId AND
  229. tool = '".TOOL_DOCUMENT."' AND
  230. $avoid_paths AND
  231. p.visibility != 2 $session_condition
  232. ORDER BY path";
  233. }
  234. $db_result = Database::query($sql);
  235. while ($obj = Database::fetch_object($db_result)) {
  236. $doc = new Document(
  237. $obj->id,
  238. $obj->path,
  239. $obj->comment,
  240. $obj->title,
  241. $obj->filetype,
  242. $obj->size
  243. );
  244. $this->course->add_resource($doc);
  245. }
  246. } else {
  247. if (!empty($this->course->type) && $this->course->type == 'partial') {
  248. $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
  249. FROM $table_doc d INNER JOIN $table_prop p
  250. ON (p.ref = d.id AND d.c_id = p.c_id)
  251. WHERE
  252. d.c_id = $courseId AND
  253. p.c_id = $courseId AND
  254. tool = '".TOOL_DOCUMENT."' AND
  255. p.visibility != 2 AND
  256. path NOT LIKE '/images/gallery%' AND
  257. $avoid_paths AND
  258. (d.session_id = 0 OR d.session_id IS NULL)
  259. ORDER BY path";
  260. } else {
  261. $sql = "SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
  262. FROM $table_doc d INNER JOIN $table_prop p
  263. ON (p.ref = d.id AND d.c_id = p.c_id)
  264. WHERE
  265. d.c_id = $courseId AND
  266. p.c_id = $courseId AND
  267. tool = '".TOOL_DOCUMENT."' AND
  268. p.visibility != 2 AND
  269. $avoid_paths AND
  270. (d.session_id = 0 OR d.session_id IS NULL)
  271. ORDER BY path";
  272. }
  273. $db_result = Database::query($sql);
  274. while ($obj = Database::fetch_object($db_result)) {
  275. $doc = new Document(
  276. $obj->id,
  277. $obj->path,
  278. $obj->comment,
  279. $obj->title,
  280. $obj->filetype,
  281. $obj->size
  282. );
  283. $this->course->add_resource($doc);
  284. }
  285. }
  286. }
  287. /**
  288. * Build the forums
  289. * @param int $session_id Internal session ID
  290. * @param int $courseId Internal course ID
  291. * @param bool $with_base_content Whether to include content from the course without session or not
  292. * @param array $id_list If you want to restrict the structure to only the given IDs
  293. * @return void
  294. */
  295. public function build_forums(
  296. $session_id = 0,
  297. $courseId = 0,
  298. $with_base_content = false,
  299. $id_list = array()
  300. ) {
  301. $table = Database:: get_course_table(TABLE_FORUM);
  302. $sessionCondition = api_get_session_condition(
  303. $session_id,
  304. true,
  305. $with_base_content
  306. );
  307. $sql = "SELECT * FROM $table WHERE c_id = $courseId $sessionCondition";
  308. $sql .= " ORDER BY forum_title, forum_category";
  309. $db_result = Database::query($sql);
  310. while ($obj = Database::fetch_object($db_result)) {
  311. $forum = new Forum($obj);
  312. $this->course->add_resource($forum);
  313. }
  314. }
  315. /**
  316. * Build a forum-category
  317. * @param int $session_id Internal session ID
  318. * @param int $courseId Internal course ID
  319. * @param bool $with_base_content Whether to include content from the course without session or not
  320. * @param array $id_list If you want to restrict the structure to only the given IDs
  321. * @return void
  322. */
  323. public function build_forum_category(
  324. $session_id = 0,
  325. $courseId = 0,
  326. $with_base_content = false,
  327. $id_list = array()
  328. ) {
  329. $table = Database:: get_course_table(TABLE_FORUM_CATEGORY);
  330. $sessionCondition = api_get_session_condition(
  331. $session_id,
  332. true,
  333. $with_base_content
  334. );
  335. $sql = "SELECT * FROM $table
  336. WHERE c_id = $courseId $sessionCondition
  337. ORDER BY cat_title";
  338. $result = Database::query($sql);
  339. while ($obj = Database::fetch_object($result)) {
  340. $forumCategory = new ForumCategory($obj);
  341. $this->course->add_resource($forumCategory);
  342. }
  343. }
  344. /**
  345. * Build the forum-topics
  346. * @param int $session_id Internal session ID
  347. * @param int $courseId Internal course ID
  348. * @param bool $with_base_content Whether to include content from the course without session or not
  349. * @param array $id_list If you want to restrict the structure to only the given IDs
  350. * @return void
  351. */
  352. public function build_forum_topics(
  353. $session_id = 0,
  354. $courseId = 0,
  355. $with_base_content = false,
  356. $id_list = array()
  357. ) {
  358. $table = Database:: get_course_table(TABLE_FORUM_THREAD);
  359. $sessionCondition = api_get_session_condition(
  360. $session_id,
  361. true,
  362. $with_base_content
  363. );
  364. $sql = "SELECT * FROM $table WHERE c_id = $courseId
  365. $sessionCondition
  366. ORDER BY thread_title ";
  367. $db_result = Database::query($sql);
  368. while ($obj = Database::fetch_object($db_result)) {
  369. $forum_topic = new ForumTopic($obj);
  370. $this->course->add_resource($forum_topic);
  371. $this->build_forum_posts($courseId, $obj->thread_id, $obj->forum_id, true);
  372. }
  373. }
  374. /**
  375. * Build the forum-posts
  376. * TODO: All tree structure of posts should be built, attachments for example.
  377. * @param int $courseId Internal course ID
  378. * @param int $thread_id Internal thread ID
  379. * @param int $forum_id Internal forum ID
  380. * @param bool $only_first_post Whether to only copy the first post or not
  381. */
  382. public function build_forum_posts(
  383. $courseId = 0,
  384. $thread_id = null,
  385. $forum_id = null,
  386. $only_first_post = false
  387. ) {
  388. $table = Database :: get_course_table(TABLE_FORUM_POST);
  389. $sql = "SELECT * FROM $table WHERE c_id = $courseId ";
  390. if (!empty($thread_id) && !empty($forum_id)) {
  391. $forum_id = intval($forum_id);
  392. $thread_id = intval($thread_id);
  393. $sql .= " AND thread_id = $thread_id AND forum_id = $forum_id ";
  394. }
  395. $sql .= " ORDER BY post_id ASC LIMIT 1";
  396. $db_result = Database::query($sql);
  397. while ($obj = Database::fetch_object($db_result)) {
  398. $forum_post = new ForumPost($obj);
  399. $this->course->add_resource($forum_post);
  400. }
  401. }
  402. /**
  403. * Build the links
  404. * @param int $session_id Internal session ID
  405. * @param int $courseId Internal course ID
  406. * @param bool $with_base_content Whether to include content from the course without session or not
  407. * @param array $id_list If you want to restrict the structure to only the given IDs
  408. */
  409. public function build_links(
  410. $session_id = 0,
  411. $courseId = 0,
  412. $with_base_content = false,
  413. $id_list = array()
  414. ) {
  415. $table = Database :: get_course_table(TABLE_LINK);
  416. $table_prop = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  417. if (!empty($session_id) && !empty($courseId)) {
  418. $session_id = intval($session_id);
  419. if ($with_base_content) {
  420. $session_condition = api_get_session_condition(
  421. $session_id,
  422. true,
  423. true,
  424. 'l.session_id'
  425. );
  426. } else {
  427. $session_condition = api_get_session_condition(
  428. $session_id,
  429. true,
  430. false,
  431. 'l.session_id'
  432. );
  433. }
  434. $sql = "SELECT l.id, l.title, l.url, l.description, l.category_id, l.on_homepage
  435. FROM $table l, $table_prop p
  436. WHERE
  437. l.c_id = $courseId AND
  438. p.c_id = $courseId AND
  439. p.ref=l.id AND
  440. p.tool = '".TOOL_LINK."' AND
  441. p.visibility != 2 $session_condition
  442. ORDER BY l.display_order";
  443. } else {
  444. $sql = "SELECT l.id, l.title, l.url, l.description, l.category_id, l.on_homepage
  445. FROM $table l, $table_prop p
  446. WHERE
  447. l.c_id = $courseId AND
  448. p.c_id = $courseId AND
  449. p.ref=l.id AND
  450. p.tool = '".TOOL_LINK."' AND
  451. p.visibility != 2 AND
  452. (l.session_id = 0 OR l.session_id IS NULL)
  453. ORDER BY l.display_order";
  454. }
  455. $db_result = Database::query($sql);
  456. while ($obj = Database::fetch_object($db_result)) {
  457. $link = new Link(
  458. $obj->id,
  459. $obj->title,
  460. $obj->url,
  461. $obj->description,
  462. $obj->category_id,
  463. $obj->on_homepage
  464. );
  465. $this->course->add_resource($link);
  466. if (!empty($courseId)) {
  467. $res = $this->build_link_category($obj->category_id, $courseId);
  468. } else {
  469. $res = $this->build_link_category($obj->category_id);
  470. }
  471. if ($res > 0) {
  472. $this->course->resources[RESOURCE_LINK][$obj->id]->add_linked_resource(
  473. RESOURCE_LINKCATEGORY,
  474. $obj->category_id
  475. );
  476. }
  477. }
  478. }
  479. /**
  480. * Build tool intro
  481. * @param int $session_id Internal session ID
  482. * @param int $courseId Internal course ID
  483. * @param bool $with_base_content Whether to include content from the course without session or not
  484. * @param array $id_list If you want to restrict the structure to only the given IDs
  485. */
  486. public function build_tool_intro(
  487. $session_id = 0,
  488. $courseId = 0,
  489. $with_base_content = false,
  490. $id_list = array()
  491. ) {
  492. $table = Database:: get_course_table(TABLE_TOOL_INTRO);
  493. $sessionCondition = api_get_session_condition(
  494. $session_id,
  495. true,
  496. $with_base_content
  497. );
  498. $sql = "SELECT * FROM $table
  499. WHERE c_id = $courseId $sessionCondition";
  500. $db_result = Database::query($sql);
  501. while ($obj = Database::fetch_object($db_result)) {
  502. $tool_intro = new ToolIntro($obj->id, $obj->intro_text);
  503. $this->course->add_resource($tool_intro);
  504. }
  505. }
  506. /**
  507. * Build a link category
  508. * @param int $id Internal link ID
  509. * @param int $courseId Internal course ID
  510. * @return int
  511. */
  512. public function build_link_category($id, $courseId = 0)
  513. {
  514. $link_cat_table = Database :: get_course_table(TABLE_LINK_CATEGORY);
  515. $sql = "SELECT * FROM $link_cat_table
  516. WHERE c_id = $courseId AND id = $id";
  517. $db_result = Database::query($sql);
  518. while ($obj = Database::fetch_object($db_result)) {
  519. $link_category = new LinkCategory(
  520. $obj->id,
  521. $obj->category_title,
  522. $obj->description,
  523. $obj->display_order
  524. );
  525. $this->course->add_resource($link_category);
  526. return $id;
  527. }
  528. return 0;
  529. }
  530. /**
  531. * Build the Quizzes
  532. * @param int $session_id Internal session ID
  533. * @param int $courseId Internal course ID
  534. * @param bool $with_base_content Whether to include content from the course without session or not
  535. * @param array $id_list If you want to restrict the structure to only the given IDs
  536. */
  537. public function build_quizzes(
  538. $session_id = 0,
  539. $courseId = 0,
  540. $with_base_content = false,
  541. $id_list = array()
  542. ) {
  543. $table_qui = Database:: get_course_table(TABLE_QUIZ_TEST);
  544. $table_rel = Database:: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  545. $table_doc = Database:: get_course_table(TABLE_DOCUMENT);
  546. if (!empty($courseId) && !empty($session_id)) {
  547. $session_id = intval($session_id);
  548. if ($with_base_content) {
  549. $session_condition = api_get_session_condition(
  550. $session_id,
  551. true,
  552. true
  553. );
  554. } else {
  555. $session_condition = api_get_session_condition(
  556. $session_id,
  557. true
  558. );
  559. }
  560. $sql = "SELECT * FROM $table_qui
  561. WHERE c_id = $courseId AND active >=0 $session_condition";
  562. //select only quizzes with active = 0 or 1 (not -1 which is for deleted quizzes)
  563. } else {
  564. $sql = "SELECT * FROM $table_qui
  565. WHERE c_id = $courseId AND active >=0 AND (session_id = 0 OR session_id IS NULL)";
  566. //select only quizzes with active = 0 or 1 (not -1 which is for deleted quizzes)
  567. }
  568. $db_result = Database::query($sql);
  569. while ($obj = Database::fetch_object($db_result)) {
  570. if (strlen($obj->sound) > 0) {
  571. $sql = "SELECT id FROM $table_doc
  572. WHERE c_id = $courseId AND path = '/audio/".$obj->sound."'";
  573. $res = Database::query($sql);
  574. $doc = Database::fetch_object($res);
  575. $obj->sound = $doc->id;
  576. }
  577. $quiz = new Quiz($obj);
  578. $sql = 'SELECT * FROM '.$table_rel.'
  579. WHERE c_id = '.$courseId.' AND exercice_id = '.$obj->id;
  580. $db_result2 = Database::query($sql);
  581. while ($obj2 = Database::fetch_object($db_result2)) {
  582. $quiz->add_question($obj2->question_id, $obj2->question_order);
  583. }
  584. $this->course->add_resource($quiz);
  585. }
  586. if (!empty($courseId)) {
  587. $this->build_quiz_questions($courseId);
  588. } else {
  589. $this->build_quiz_questions();
  590. }
  591. }
  592. /**
  593. * Build the Quiz-Questions
  594. * @param int $courseId Internal course ID
  595. */
  596. public function build_quiz_questions($courseId = 0)
  597. {
  598. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
  599. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  600. $table_que = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  601. $table_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  602. // Building normal tests.
  603. $sql = "SELECT * FROM $table_que
  604. WHERE c_id = $courseId ";
  605. $result = Database::query($sql);
  606. while ($obj = Database::fetch_object($result)) {
  607. // find the question category
  608. // @todo : need to be adapted for multi category questions in 1.10
  609. $question_category_id = TestCategory::getCategoryForQuestion(
  610. $obj->id,
  611. $courseId
  612. );
  613. // build the backup resource question object
  614. $question = new QuizQuestion(
  615. $obj->id,
  616. $obj->question,
  617. $obj->description,
  618. $obj->ponderation,
  619. $obj->type,
  620. $obj->position,
  621. $obj->picture,
  622. $obj->level,
  623. $obj->extra,
  624. $question_category_id
  625. );
  626. $sql = 'SELECT * FROM '.$table_ans.'
  627. WHERE c_id = '.$courseId.' AND question_id = '.$obj->id;
  628. $db_result2 = Database::query($sql);
  629. while ($obj2 = Database::fetch_object($db_result2)) {
  630. $question->add_answer(
  631. $obj2->id,
  632. $obj2->answer,
  633. $obj2->correct,
  634. $obj2->comment,
  635. $obj2->ponderation,
  636. $obj2->position,
  637. $obj2->hotspot_coordinates,
  638. $obj2->hotspot_type
  639. );
  640. if ($obj->type == MULTIPLE_ANSWER_TRUE_FALSE) {
  641. $table_options = Database::get_course_table(
  642. TABLE_QUIZ_QUESTION_OPTION
  643. );
  644. $sql = 'SELECT * FROM '.$table_options.'
  645. WHERE c_id = '.$courseId.' AND question_id = '.$obj->id;
  646. $db_result3 = Database::query($sql);
  647. while ($obj3 = Database::fetch_object($db_result3)) {
  648. $question_option = new QuizQuestionOption($obj3);
  649. $question->add_option($question_option);
  650. }
  651. }
  652. }
  653. $this->course->add_resource($question);
  654. }
  655. // Building a fictional test for collecting orphan questions.
  656. // When a course is emptied this option should be activated (true).
  657. $build_orphan_questions = !empty($_POST['recycle_option']);
  658. // 1st union gets the orphan questions from deleted exercises
  659. // 2nd union gets the orphan questions from question that were deleted in a exercise.
  660. $sql = " (
  661. SELECT question_id, q.* FROM $table_que q INNER JOIN $table_rel r
  662. ON (q.c_id = r.c_id AND q.id = r.question_id)
  663. INNER JOIN $table_qui ex
  664. ON (ex.id = r.exercice_id AND ex.c_id = r.c_id )
  665. WHERE ex.c_id = $courseId AND ex.active = '-1'
  666. )
  667. UNION
  668. (
  669. SELECT question_id, q.* FROM $table_que q left
  670. OUTER JOIN $table_rel r
  671. ON (q.c_id = r.c_id AND q.id = r.question_id)
  672. WHERE q.c_id = $courseId AND r.question_id is null
  673. )
  674. UNION
  675. (
  676. SELECT question_id, q.* FROM $table_que q
  677. INNER JOIN $table_rel r
  678. ON (q.c_id = r.c_id AND q.id = r.question_id)
  679. WHERE r.c_id = $courseId AND (r.exercice_id = '-1' OR r.exercice_id = '0')
  680. )
  681. ";
  682. $result = Database::query($sql);
  683. if (Database::num_rows($result) > 0) {
  684. $build_orphan_questions = true;
  685. $orphanQuestionIds = array();
  686. while ($obj = Database::fetch_object($result)) {
  687. // Orphan questions
  688. if (!empty($obj->question_id)) {
  689. $obj->id = $obj->question_id;
  690. }
  691. // Avoid adding the same question twice
  692. if (!isset($this->course->resources[$obj->id])) {
  693. // find the question category
  694. // @todo : need to be adapted for multi category questions in 1.10
  695. $question_category_id = TestCategory::getCategoryForQuestion(
  696. $obj->id,
  697. $courseId
  698. );
  699. $question = new QuizQuestion(
  700. $obj->id,
  701. $obj->question,
  702. $obj->description,
  703. $obj->ponderation,
  704. $obj->type,
  705. $obj->position,
  706. $obj->picture,
  707. $obj->level,
  708. $obj->extra,
  709. $question_category_id
  710. );
  711. $sql = "SELECT * FROM $table_ans
  712. WHERE c_id = $courseId AND question_id = ".$obj->id;
  713. $db_result2 = Database::query($sql);
  714. if (Database::num_rows($db_result2)) {
  715. while ($obj2 = Database::fetch_object($db_result2)) {
  716. $question->add_answer(
  717. $obj2->id,
  718. $obj2->answer,
  719. $obj2->correct,
  720. $obj2->comment,
  721. $obj2->ponderation,
  722. $obj2->position,
  723. $obj2->hotspot_coordinates,
  724. $obj2->hotspot_type
  725. );
  726. }
  727. $orphanQuestionIds[] = $obj->id;
  728. }
  729. $this->course->add_resource($question);
  730. }
  731. }
  732. }
  733. if ($build_orphan_questions) {
  734. $obj = array(
  735. 'id' => -1,
  736. 'title' => get_lang('OrphanQuestions', ''),
  737. 'type' => 2
  738. );
  739. $newQuiz = new Quiz((object)$obj);
  740. if (!empty($orphanQuestionIds)) {
  741. foreach ($orphanQuestionIds as $index => $orphanId) {
  742. $order = $index + 1;
  743. $newQuiz->add_question($orphanId, $order);
  744. }
  745. }
  746. $this->course->add_resource($newQuiz);
  747. }
  748. }
  749. /**
  750. * Build the orphan questions
  751. */
  752. public function build_quiz_orphan_questions()
  753. {
  754. $table_qui = Database:: get_course_table(TABLE_QUIZ_TEST);
  755. $table_rel = Database:: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  756. $table_que = Database:: get_course_table(TABLE_QUIZ_QUESTION);
  757. $table_ans = Database:: get_course_table(TABLE_QUIZ_ANSWER);
  758. $courseId = api_get_course_int_id();
  759. $sql = 'SELECT *
  760. FROM '.$table_que.' as questions
  761. LEFT JOIN '.$table_rel.' as quizz_questions
  762. ON questions.id=quizz_questions.question_id
  763. LEFT JOIN '.$table_qui.' as exercises
  764. ON quizz_questions.exercice_id = exercises.id
  765. WHERE
  766. questions.c_id = quizz_questions.c_id AND
  767. questions.c_id = exercises.c_id AND
  768. exercises.c_id = '.$courseId.' AND
  769. (quizz_questions.exercice_id IS NULL OR
  770. exercises.active = -1)';
  771. $db_result = Database::query($sql);
  772. if (Database::num_rows($db_result) > 0) {
  773. // This is the fictional test for collecting orphan questions.
  774. $orphan_questions = new Quiz(
  775. -1,
  776. get_lang('OrphanQuestions', ''),
  777. '',
  778. 0,
  779. 0,
  780. 1,
  781. '',
  782. 0
  783. );
  784. $this->course->add_resource($orphan_questions);
  785. while ($obj = Database::fetch_object($db_result)) {
  786. $question = new QuizQuestion(
  787. $obj->id,
  788. $obj->question,
  789. $obj->description,
  790. $obj->ponderation,
  791. $obj->type,
  792. $obj->position,
  793. $obj->picture,
  794. $obj->level,
  795. $obj->extra
  796. );
  797. $sql = 'SELECT * FROM '.$table_ans.' WHERE question_id = '.$obj->id;
  798. $db_result2 = Database::query($sql);
  799. while ($obj2 = Database::fetch_object($db_result2)) {
  800. $question->add_answer(
  801. $obj2->id,
  802. $obj2->answer,
  803. $obj2->correct,
  804. $obj2->comment,
  805. $obj2->ponderation,
  806. $obj2->position,
  807. $obj2->hotspot_coordinates,
  808. $obj2->hotspot_type
  809. );
  810. }
  811. $this->course->add_resource($question);
  812. }
  813. }
  814. }
  815. /**
  816. * Build the test category
  817. * @param int $session_id Internal session ID
  818. * @param int $courseId Internal course ID
  819. * @param bool $with_base_content Whether to include content from the course without session or not
  820. * @param array $id_list If you want to restrict the structure to only the given IDs
  821. * @todo add course session
  822. */
  823. public function build_test_category(
  824. $session_id = 0,
  825. $courseId = 0,
  826. $with_base_content = false,
  827. $id_list = array()
  828. ) {
  829. // get all test category in course
  830. $tab_test_categories_id = TestCategory::getCategoryListInfo(
  831. "id",
  832. $courseId
  833. );
  834. foreach ($tab_test_categories_id as $test_category_id) {
  835. $test_category = new TestCategory($test_category_id);
  836. $copy_course_test_category = new CourseCopyTestcategory(
  837. $test_category_id,
  838. $test_category->name,
  839. $test_category->description
  840. );
  841. $this->course->add_resource($copy_course_test_category);
  842. }
  843. }
  844. /**
  845. * Build the Surveys
  846. * @param int $session_id Internal session ID
  847. * @param int $courseId Internal course ID
  848. * @param bool $with_base_content Whether to include content from the course without session or not
  849. * @param array $id_list If you want to restrict the structure to only the given IDs
  850. */
  851. public function build_surveys(
  852. $session_id = 0,
  853. $courseId = 0,
  854. $with_base_content = false,
  855. $id_list = array()
  856. ) {
  857. $table_survey = Database:: get_course_table(TABLE_SURVEY);
  858. $table_question = Database:: get_course_table(TABLE_SURVEY_QUESTION);
  859. $sessionCondition = api_get_session_condition(
  860. $session_id,
  861. true,
  862. $with_base_content
  863. );
  864. $sql = 'SELECT * FROM '.$table_survey.'
  865. WHERE c_id = '.$courseId.' '.$sessionCondition;
  866. $db_result = Database::query($sql);
  867. while ($obj = Database::fetch_object($db_result)) {
  868. $survey = new Survey(
  869. $obj->survey_id, $obj->code, $obj->title,
  870. $obj->subtitle, $obj->author, $obj->lang,
  871. $obj->avail_from, $obj->avail_till, $obj->is_shared,
  872. $obj->template, $obj->intro, $obj->surveythanks,
  873. $obj->creation_date, $obj->invited, $obj->answered,
  874. $obj->invite_mail, $obj->reminder_mail
  875. );
  876. $sql = 'SELECT * FROM '.$table_question.'
  877. WHERE c_id = '.$courseId.' AND survey_id = '.$obj->survey_id;
  878. $db_result2 = Database::query($sql);
  879. while ($obj2 = Database::fetch_object($db_result2)) {
  880. $survey->add_question($obj2->question_id);
  881. }
  882. $this->course->add_resource($survey);
  883. }
  884. $this->build_survey_questions($courseId);
  885. }
  886. /**
  887. * Build the Survey Questions
  888. * @param int $courseId Internal course ID
  889. */
  890. public function build_survey_questions($courseId) {
  891. $table_que = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  892. $table_opt = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  893. $sql = 'SELECT * FROM '.$table_que.' WHERE c_id = '.$courseId.' ';
  894. $db_result = Database::query($sql);
  895. while ($obj = Database::fetch_object($db_result)) {
  896. $question = new SurveyQuestion(
  897. $obj->question_id,
  898. $obj->survey_id,
  899. $obj->survey_question,
  900. $obj->survey_question_comment,
  901. $obj->type,
  902. $obj->display,
  903. $obj->sort,
  904. $obj->shared_question_id,
  905. $obj->max_value
  906. );
  907. $sql = 'SELECT * FROM '.$table_opt.'
  908. WHERE c_id = '.$courseId.' AND question_id = '.$obj->question_id;
  909. $db_result2 = Database::query($sql);
  910. while ($obj2 = Database::fetch_object($db_result2)) {
  911. $question->add_answer($obj2->option_text, $obj2->sort);
  912. }
  913. $this->course->add_resource($question);
  914. }
  915. }
  916. /**
  917. * Build the announcements
  918. * @param int $session_id Internal session ID
  919. * @param int $courseId Internal course ID
  920. * @param bool $with_base_content Whether to include content from the course without session or not
  921. * @param array $id_list If you want to restrict the structure to only the given IDs
  922. */
  923. public function build_announcements(
  924. $session_id = 0,
  925. $courseId = 0,
  926. $with_base_content = false,
  927. $id_list = array()
  928. ) {
  929. $table = Database:: get_course_table(TABLE_ANNOUNCEMENT);
  930. $sessionCondition = api_get_session_condition(
  931. $session_id,
  932. true,
  933. $with_base_content
  934. );
  935. $sql = 'SELECT * FROM '.$table.'
  936. WHERE c_id = '.$courseId.' '.$sessionCondition;
  937. $db_result = Database::query($sql);
  938. $table_attachment = Database:: get_course_table(
  939. TABLE_ANNOUNCEMENT_ATTACHMENT
  940. );
  941. while ($obj = Database::fetch_object($db_result)) {
  942. if (empty($obj->id)) {
  943. continue;
  944. }
  945. $sql = 'SELECT path, comment, filename, size
  946. FROM '.$table_attachment.'
  947. WHERE c_id = '.$courseId.' AND announcement_id = '.$obj->id.'';
  948. $result = Database::query($sql);
  949. $attachment_obj = Database::fetch_object($result);
  950. $att_path = $att_filename = $att_size = $atth_comment = '';
  951. if (!empty($attachment_obj)) {
  952. $att_path = $attachment_obj->path;
  953. $att_filename = $attachment_obj->filename;
  954. $att_size = $attachment_obj->size;
  955. $atth_comment = $attachment_obj->comment;
  956. }
  957. $announcement = new Announcement(
  958. $obj->id,
  959. $obj->title,
  960. $obj->content,
  961. $obj->end_date,
  962. $obj->display_order,
  963. $obj->email_sent,
  964. $att_path,
  965. $att_filename,
  966. $att_size,
  967. $atth_comment
  968. );
  969. $this->course->add_resource($announcement);
  970. }
  971. }
  972. /**
  973. * Build the events
  974. * @param int $session_id Internal session ID
  975. * @param int $courseId Internal course ID
  976. * @param bool $with_base_content Whether to include content from the course without session or not
  977. * @param array $id_list If you want to restrict the structure to only the given IDs
  978. */
  979. public function build_events(
  980. $session_id = 0,
  981. $courseId = 0,
  982. $with_base_content = false,
  983. $id_list = array()
  984. ) {
  985. $table = Database:: get_course_table(TABLE_AGENDA);
  986. $sessionCondition = api_get_session_condition(
  987. $session_id,
  988. true,
  989. $with_base_content
  990. );
  991. $sql = 'SELECT * FROM '.$table.'
  992. WHERE c_id = '.$courseId.' '.$sessionCondition;
  993. $db_result = Database::query($sql);
  994. while ($obj = Database::fetch_object($db_result)) {
  995. $table_attachment = Database:: get_course_table(
  996. TABLE_AGENDA_ATTACHMENT
  997. );
  998. $sql = 'SELECT path, comment, filename, size
  999. FROM '.$table_attachment.'
  1000. WHERE c_id = '.$courseId.' AND agenda_id = '.$obj->id.'';
  1001. $result = Database::query($sql);
  1002. $attachment_obj = Database::fetch_object($result);
  1003. $att_path = $att_filename = $att_size = $atth_comment = '';
  1004. if (!empty($attachment_obj)) {
  1005. $att_path = $attachment_obj->path;
  1006. $att_filename = $attachment_obj->filename;
  1007. $att_size = $attachment_obj->size;
  1008. $atth_comment = $attachment_obj->comment;
  1009. }
  1010. $event = new CalendarEvent(
  1011. $obj->id,
  1012. $obj->title,
  1013. $obj->content,
  1014. $obj->start_date,
  1015. $obj->end_date,
  1016. $att_path,
  1017. $att_filename,
  1018. $att_size,
  1019. $atth_comment,
  1020. $obj->all_day
  1021. );
  1022. $this->course->add_resource($event);
  1023. }
  1024. }
  1025. /**
  1026. * Build the course-descriptions
  1027. * @param int $session_id Internal session ID
  1028. * @param int $courseId Internal course ID
  1029. * @param bool $with_base_content Whether to include content from the course without session or not
  1030. * @param array $id_list If you want to restrict the structure to only the given IDs
  1031. */
  1032. public function build_course_descriptions(
  1033. $session_id = 0,
  1034. $courseId = 0,
  1035. $with_base_content = false,
  1036. $id_list = array()
  1037. ) {
  1038. $table = Database:: get_course_table(TABLE_COURSE_DESCRIPTION);
  1039. if (!empty($session_id) && !empty($courseId)) {
  1040. $session_id = intval($session_id);
  1041. if ($with_base_content) {
  1042. $session_condition = api_get_session_condition(
  1043. $session_id,
  1044. true,
  1045. true
  1046. );
  1047. } else {
  1048. $session_condition = api_get_session_condition(
  1049. $session_id,
  1050. true
  1051. );
  1052. }
  1053. $sql = 'SELECT * FROM '.$table.'
  1054. WHERE c_id = '.$courseId.' '.$session_condition;
  1055. } else {
  1056. $table = Database:: get_course_table(TABLE_COURSE_DESCRIPTION);
  1057. $sql = 'SELECT * FROM '.$table.'
  1058. WHERE c_id = '.$courseId.' AND session_id = 0';
  1059. }
  1060. $db_result = Database::query($sql);
  1061. while ($obj = Database::fetch_object($db_result)) {
  1062. $cd = new CourseDescription(
  1063. $obj->id,
  1064. $obj->title,
  1065. $obj->content,
  1066. $obj->description_type
  1067. );
  1068. $this->course->add_resource($cd);
  1069. }
  1070. }
  1071. /**
  1072. * Build the learnpaths
  1073. * @param int $session_id Internal session ID
  1074. * @param int $courseId Internal course ID
  1075. * @param bool $with_base_content Whether to include content from the course without session or not
  1076. * @param array $id_list If you want to restrict the structure to only the given IDs
  1077. */
  1078. public function build_learnpaths(
  1079. $session_id = 0,
  1080. $courseId = 0,
  1081. $with_base_content = false,
  1082. $id_list = array()
  1083. ) {
  1084. $table_main = Database::get_course_table(TABLE_LP_MAIN);
  1085. $table_item = Database::get_course_table(TABLE_LP_ITEM);
  1086. $table_tool = Database::get_course_table(TABLE_TOOL_LIST);
  1087. if (!empty($session_id) && !empty($courseId)) {
  1088. $session_id = intval($session_id);
  1089. if ($with_base_content) {
  1090. $session_condition = api_get_session_condition(
  1091. $session_id,
  1092. true,
  1093. true
  1094. );
  1095. } else {
  1096. $session_condition = api_get_session_condition(
  1097. $session_id,
  1098. true
  1099. );
  1100. }
  1101. $sql = 'SELECT * FROM '.$table_main.'
  1102. WHERE c_id = '.$courseId.' '.$session_condition;
  1103. } else {
  1104. $sql = 'SELECT * FROM '.$table_main.'
  1105. WHERE c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
  1106. }
  1107. if (!empty($id_list)) {
  1108. $id_list = array_map('intval', $id_list);
  1109. $sql .= " AND id IN (".implode(', ', $id_list).") ";
  1110. }
  1111. $db_result = Database::query($sql);
  1112. if ($db_result) {
  1113. while ($obj = Database::fetch_object($db_result)) {
  1114. $items = array();
  1115. $sql_items = "SELECT * FROM ".$table_item."
  1116. WHERE c_id = '$courseId' AND lp_id = ".$obj->id;
  1117. $db_items = Database::query($sql_items);
  1118. while ($obj_item = Database::fetch_object($db_items)) {
  1119. $item['id'] = $obj_item->id;
  1120. $item['item_type'] = $obj_item->item_type;
  1121. $item['ref'] = $obj_item->ref;
  1122. $item['title'] = $obj_item->title;
  1123. $item['description'] = $obj_item->description;
  1124. $item['path'] = $obj_item->path;
  1125. $item['min_score'] = $obj_item->min_score;
  1126. $item['max_score'] = $obj_item->max_score;
  1127. $item['mastery_score'] = $obj_item->mastery_score;
  1128. $item['parent_item_id'] = $obj_item->parent_item_id;
  1129. $item['previous_item_id'] = $obj_item->previous_item_id;
  1130. $item['next_item_id'] = $obj_item->next_item_id;
  1131. $item['display_order'] = $obj_item->display_order;
  1132. $item['prerequisite'] = $obj_item->prerequisite;
  1133. $item['parameters'] = $obj_item->parameters;
  1134. $item['launch_data'] = $obj_item->launch_data;
  1135. $item['audio'] = $obj_item->audio;
  1136. $items[] = $item;
  1137. }
  1138. $sql_tool = "SELECT id FROM $table_tool
  1139. WHERE
  1140. c_id = $courseId AND
  1141. (link LIKE '%lp_controller.php%lp_id=".$obj->id."%' AND image='scormbuilder.gif') AND
  1142. visibility = '1' ";
  1143. $db_tool = Database::query($sql_tool);
  1144. if (Database::num_rows($db_tool)) {
  1145. $visibility = '1';
  1146. } else {
  1147. $visibility = '0';
  1148. }
  1149. $lp = new CourseCopyLearnpath(
  1150. $obj->id,
  1151. $obj->lp_type,
  1152. $obj->name,
  1153. $obj->path,
  1154. $obj->ref,
  1155. $obj->description,
  1156. $obj->content_local,
  1157. $obj->default_encoding,
  1158. $obj->default_view_mod,
  1159. $obj->prevent_reinit,
  1160. $obj->force_commit,
  1161. $obj->content_maker,
  1162. $obj->display_order,
  1163. $obj->js_lib,
  1164. $obj->content_license,
  1165. $obj->debug,
  1166. $visibility,
  1167. $obj->author,
  1168. $obj->preview_image,
  1169. $obj->use_max_score,
  1170. $obj->autolaunch,
  1171. $obj->created_on,
  1172. $obj->modified_on,
  1173. $obj->publicated_on,
  1174. $obj->expired_on,
  1175. $obj->session_id,
  1176. $items
  1177. );
  1178. $this->course->add_resource($lp);
  1179. }
  1180. }
  1181. // Save scorm directory (previously build_scorm_documents())
  1182. $i = 1;
  1183. if ($dir = @opendir($this->course->backup_path.'/scorm')) {
  1184. while ($file = readdir($dir)) {
  1185. if (is_dir($this->course->backup_path.'/scorm/'.$file) &&
  1186. !in_array($file, array('.', '..'))
  1187. ) {
  1188. $doc = new ScormDocument($i++, '/'.$file, $file);
  1189. $this->course->add_resource($doc);
  1190. }
  1191. }
  1192. closedir($dir);
  1193. }
  1194. }
  1195. /**
  1196. * Build the glossaries
  1197. * @param int $session_id Internal session ID
  1198. * @param int $courseId Internal course ID
  1199. * @param bool $with_base_content Whether to include content from the course without session or not
  1200. * @param array $id_list If you want to restrict the structure to only the given IDs
  1201. */
  1202. public function build_glossary(
  1203. $session_id = 0,
  1204. $courseId = 0,
  1205. $with_base_content = false,
  1206. $id_list = array()
  1207. ) {
  1208. $table_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  1209. if (!empty($session_id) && !empty($courseId)) {
  1210. $session_id = intval($session_id);
  1211. if ($with_base_content) {
  1212. $session_condition = api_get_session_condition(
  1213. $session_id,
  1214. true,
  1215. true
  1216. );
  1217. } else {
  1218. $session_condition = api_get_session_condition(
  1219. $session_id,
  1220. true
  1221. );
  1222. }
  1223. //@todo check this queries are the same ...
  1224. if (!empty($this->course->type) && $this->course->type == 'partial') {
  1225. $sql = 'SELECT * FROM '.$table_glossary.' g
  1226. WHERE g.c_id = '.$courseId.' '.$session_condition;
  1227. } else {
  1228. $sql = 'SELECT * FROM '.$table_glossary.' g
  1229. WHERE g.c_id = '.$courseId.' '.$session_condition;
  1230. }
  1231. } else {
  1232. $table_glossary = Database:: get_course_table(TABLE_GLOSSARY);
  1233. //@todo check this queries are the same ... ayayay
  1234. if (!empty($this->course->type) && $this->course->type == 'partial') {
  1235. $sql = 'SELECT * FROM '.$table_glossary.' g
  1236. WHERE g.c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
  1237. } else {
  1238. $sql = 'SELECT * FROM '.$table_glossary.' g
  1239. WHERE g.c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
  1240. }
  1241. }
  1242. $db_result = Database::query($sql);
  1243. while ($obj = Database::fetch_object($db_result)) {
  1244. $doc = new Glossary(
  1245. $obj->glossary_id,
  1246. $obj->name,
  1247. $obj->description,
  1248. $obj->display_order
  1249. );
  1250. $this->course->add_resource($doc);
  1251. }
  1252. }
  1253. /*
  1254. * Build session course by jhon
  1255. */
  1256. public function build_session_course()
  1257. {
  1258. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1259. $tbl_session_course = Database::get_main_table(
  1260. TABLE_MAIN_SESSION_COURSE
  1261. );
  1262. $list_course = CourseManager::get_course_list();
  1263. $list = array();
  1264. foreach ($list_course as $_course) {
  1265. $this->course = new Course();
  1266. $this->course->code = $_course['code'];
  1267. $this->course->type = 'partial';
  1268. $this->course->path = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/';
  1269. $this->course->backup_path = api_get_path(SYS_COURSE_PATH).$_course['directory'];
  1270. $this->course->encoding = api_get_system_encoding(); //current platform encoding
  1271. $code_course = $_course['code'];
  1272. $courseId = $_course['real_id'];
  1273. $sql_session = "SELECT s.id, name, c_id
  1274. FROM $tbl_session_course sc
  1275. INNER JOIN $tbl_session s
  1276. ON sc.session_id = s.id
  1277. WHERE sc.c_id = '$courseId' ";
  1278. $query_session = Database::query($sql_session);
  1279. while ($rows_session = Database::fetch_assoc($query_session)) {
  1280. $session = new CourseSession(
  1281. $rows_session['id'],
  1282. $rows_session['name']
  1283. );
  1284. $this->course->add_resource($session);
  1285. }
  1286. $list[] = $this->course;
  1287. }
  1288. return $list;
  1289. }
  1290. /**
  1291. * @param int $session_id Internal session ID
  1292. * @param int $courseId Internal course ID
  1293. * @param bool $with_base_content Whether to include content from the course without session or not
  1294. * @param array $id_list If you want to restrict the structure to only the given IDs
  1295. */
  1296. public function build_wiki(
  1297. $session_id = 0,
  1298. $courseId = 0,
  1299. $with_base_content = false,
  1300. $id_list = array()
  1301. ) {
  1302. $tbl_wiki = Database::get_course_table(TABLE_WIKI);
  1303. if (!empty($session_id) && !empty($courseId)) {
  1304. $session_id = intval($session_id);
  1305. if ($with_base_content) {
  1306. $session_condition = api_get_session_condition(
  1307. $session_id,
  1308. true,
  1309. true
  1310. );
  1311. } else {
  1312. $session_condition = api_get_session_condition(
  1313. $session_id,
  1314. true
  1315. );
  1316. }
  1317. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1318. WHERE c_id = '.$courseId.' '.$session_condition;
  1319. } else {
  1320. $tbl_wiki = Database::get_course_table(TABLE_WIKI);
  1321. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1322. WHERE c_id = '.$courseId.' AND (session_id = 0 OR session_id IS NULL)';
  1323. }
  1324. $db_result = Database::query($sql);
  1325. while ($obj = Database::fetch_object($db_result)) {
  1326. $wiki = new Wiki(
  1327. $obj->id,
  1328. $obj->page_id,
  1329. $obj->reflink,
  1330. $obj->title,
  1331. $obj->content,
  1332. $obj->user_id,
  1333. $obj->group_id,
  1334. $obj->dtime,
  1335. $obj->progress,
  1336. $obj->version
  1337. );
  1338. $this->course->add_resource($wiki);
  1339. }
  1340. }
  1341. /**
  1342. * Build the Surveys
  1343. * @param int $session_id Internal session ID
  1344. * @param int $courseId Internal course ID
  1345. * @param bool $with_base_content Whether to include content from the course without session or not
  1346. * @param array $id_list If you want to restrict the structure to only the given IDs
  1347. */
  1348. public function build_thematic(
  1349. $session_id = 0,
  1350. $courseId = 0,
  1351. $with_base_content = false,
  1352. $id_list = array()
  1353. ) {
  1354. $table_thematic = Database :: get_course_table(TABLE_THEMATIC);
  1355. $table_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
  1356. $table_thematic_plan = Database :: get_course_table(TABLE_THEMATIC_PLAN);
  1357. $session_id = intval($session_id);
  1358. if ($with_base_content) {
  1359. $session_condition = api_get_session_condition(
  1360. $session_id,
  1361. true,
  1362. true
  1363. );
  1364. } else {
  1365. $session_condition = api_get_session_condition($session_id, true);
  1366. }
  1367. $sql = "SELECT * FROM $table_thematic
  1368. WHERE c_id = $courseId $session_condition ";
  1369. $db_result = Database::query($sql);
  1370. while ($row = Database::fetch_array($db_result, 'ASSOC')) {
  1371. $thematic = new Thematic($row);
  1372. $sql = 'SELECT * FROM '.$table_thematic_advance.'
  1373. WHERE c_id = '.$courseId.' AND thematic_id = '.$row['id'];
  1374. $result = Database::query($sql);
  1375. while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
  1376. $thematic->add_thematic_advance($sub_row);
  1377. }
  1378. $items = api_get_item_property_by_tool(
  1379. 'thematic_plan',
  1380. api_get_course_id(),
  1381. $session_id
  1382. );
  1383. $thematic_plan_id_list = array();
  1384. if (!empty($items)) {
  1385. foreach ($items as $item) {
  1386. $thematic_plan_id_list[] = $item['ref'];
  1387. //$thematic_plan_complete_list[$item['ref']] = $item;
  1388. }
  1389. }
  1390. if (count($thematic_plan_id_list) > 0) {
  1391. $sql = "SELECT tp.*
  1392. FROM $table_thematic_plan tp
  1393. INNER JOIN $table_thematic t ON (t.id=tp.thematic_id)
  1394. WHERE
  1395. t.c_id = $courseId AND
  1396. tp.c_id = $courseId AND
  1397. thematic_id = {$row['id']} AND
  1398. tp.id IN (".implode(', ', $thematic_plan_id_list).") ";
  1399. $result = Database::query($sql);
  1400. while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
  1401. $thematic->add_thematic_plan($sub_row);
  1402. }
  1403. }
  1404. $this->course->add_resource($thematic);
  1405. }
  1406. }
  1407. /**
  1408. * Build the attendances
  1409. * @param int $session_id Internal session ID
  1410. * @param int $courseId Internal course ID
  1411. * @param bool $with_base_content Whether to include content from the course without session or not
  1412. * @param array $id_list If you want to restrict the structure to only the given IDs
  1413. */
  1414. public function build_attendance(
  1415. $session_id = 0,
  1416. $courseId = 0,
  1417. $with_base_content = false,
  1418. $id_list = array()
  1419. ) {
  1420. $table_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  1421. $table_attendance_calendar = Database :: get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1422. $sessionCondition = api_get_session_condition($session_id, true, $with_base_content);
  1423. $sql = 'SELECT * FROM '.$table_attendance.'
  1424. WHERE c_id = '.$courseId.' '.$sessionCondition;
  1425. $db_result = Database::query($sql);
  1426. while ($row = Database::fetch_array($db_result, 'ASSOC')) {
  1427. $obj = new Attendance($row);
  1428. $sql = 'SELECT * FROM '.$table_attendance_calendar.'
  1429. WHERE c_id = '.$courseId.' AND attendance_id = '.$row['id'];
  1430. $result = Database::query($sql);
  1431. while ($sub_row = Database::fetch_array($result, 'ASSOC')) {
  1432. $obj->add_attendance_calendar($sub_row);
  1433. }
  1434. $this->course->add_resource($obj);
  1435. }
  1436. }
  1437. /**
  1438. * Build the works (or "student publications", or "assignments")
  1439. * @param int $session_id Internal session ID
  1440. * @param int $courseId Internal course ID
  1441. * @param bool $with_base_content Whether to include content from the course without session or not
  1442. * @param array $id_list If you want to restrict the structure to only the given IDs
  1443. */
  1444. public function build_works(
  1445. $session_id = 0,
  1446. $courseId = 0,
  1447. $with_base_content = false,
  1448. $id_list = array()
  1449. ) {
  1450. $table_work = Database:: get_course_table(TABLE_STUDENT_PUBLICATION);
  1451. $sessionCondition = api_get_session_condition(
  1452. $session_id,
  1453. true,
  1454. $with_base_content
  1455. );
  1456. $sql = "SELECT * FROM $table_work
  1457. WHERE
  1458. c_id = $courseId
  1459. $sessionCondition AND
  1460. filetype = 'folder' AND
  1461. parent_id = 0 AND
  1462. active = 1";
  1463. $db_result = Database::query($sql);
  1464. while ($row = Database::fetch_array($db_result, 'ASSOC')) {
  1465. $obj = new Work($row);
  1466. $this->course->add_resource($obj);
  1467. }
  1468. }
  1469. }