CourseBuilder.class.php 59 KB

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