course_description.lib.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains a class used like library provides functions for
  5. * course description tool. It's also used like model to
  6. * course_description_controller (MVC pattern)
  7. * @author Christian Fasanando <christian1827@gmail.com>
  8. * @package chamilo.course_description
  9. */
  10. /**
  11. * Class CourseDescription Lanage course descriptions
  12. * @package chamilo.course_description
  13. */
  14. class CourseDescription
  15. {
  16. private $id;
  17. private $course_id;
  18. private $title;
  19. private $content;
  20. private $session_id;
  21. private $description_type;
  22. private $progress;
  23. /**
  24. * Constructor
  25. */
  26. public function __construct()
  27. {
  28. }
  29. /**
  30. * Returns an array of objects of type CourseDescription corresponding to
  31. * a specific course, without session ids (session id = 0)
  32. *
  33. * @param int Course id
  34. * @return array Array of CourseDescriptions
  35. */
  36. public static function get_descriptions($course_id)
  37. {
  38. // Get course code
  39. $course_info = api_get_course_info_by_id($course_id);
  40. if (!empty($course_info)) {
  41. $course_id = $course_info['real_id'];
  42. } else {
  43. return array();
  44. }
  45. $t_course_desc = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  46. $sql = "SELECT * FROM $t_course_desc
  47. WHERE c_id = $course_id AND session_id = '0'";
  48. $sql_result = Database::query($sql);
  49. $results = array();
  50. while ($row = Database::fetch_array($sql_result)) {
  51. $desc_tmp = new CourseDescription();
  52. $desc_tmp->set_id($row['id']);
  53. $desc_tmp->set_title($row['title']);
  54. $desc_tmp->set_content($row['content']);
  55. $desc_tmp->set_session_id($row['session_id']);
  56. $desc_tmp->set_description_type($row['description_type']);
  57. $desc_tmp->set_progress($row['progress']);
  58. $results[] = $desc_tmp;
  59. }
  60. return $results;
  61. }
  62. /**
  63. * Get all data of course description by session id,
  64. * first you must set session_id property with the object CourseDescription
  65. * @return array
  66. */
  67. public function get_description_data()
  68. {
  69. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  70. $condition_session = api_get_session_condition($this->session_id, true, true);
  71. $course_id = api_get_course_int_id();
  72. $sql = "SELECT * FROM $tbl_course_description
  73. WHERE c_id = $course_id $condition_session
  74. ORDER BY id ";
  75. $rs = Database::query($sql);
  76. $data = array();
  77. while ($description = Database::fetch_array($rs)) {
  78. $data['descriptions'][$description['id']] = Security::remove_XSS($description, STUDENT);
  79. //reload titles to ensure we have the last version (after edition)
  80. //$data['default_description_titles'][$description['id']] = Security::remove_XSS($description['title'], STUDENT);
  81. }
  82. return $data;
  83. }
  84. /**
  85. * Get all data of course description by session id,
  86. * first you must set session_id property with the object CourseDescription
  87. * @deprecated
  88. * @return array
  89. */
  90. public function get_description_history($description_type)
  91. {
  92. $tbl_stats_item_property = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY);
  93. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  94. $description_id = $this->get_id_by_description_type($description_type);
  95. $item_property_id = api_get_item_property_id(api_get_course_id(), TOOL_COURSE_DESCRIPTION, $description_id);
  96. $course_id = api_get_course_int_id();
  97. $sql = "SELECT tip.id, tip.course_id, tip.item_property_id, tip.title, tip.content, tip.progress, tip.lastedit_date, tip.session_id
  98. FROM $tbl_stats_item_property tip INNER JOIN $tbl_item_property ip
  99. ON ip.tool = '" . TOOL_COURSE_DESCRIPTION . "' AND ip.id = tip.item_property_id
  100. WHERE ip.c_id = $course_id AND tip.course_id = '$course_id' AND tip.session_id = '" . intval($this->session_id) . "'
  101. ORDER BY tip.lastedit_date DESC";
  102. $rs = Database::query($sql);
  103. $data = array();
  104. while ($description = Database::fetch_array($rs)) {
  105. $data['descriptions'][] = $description;
  106. }
  107. return $data;
  108. }
  109. /**
  110. * Get all data by description and session id,
  111. * first you must set session_id property with the object CourseDescription
  112. * @param int description type
  113. * @param string course code (optional)
  114. * @param int session id (optional)
  115. * @return array
  116. */
  117. public function get_data_by_description_type(
  118. $description_type,
  119. $course_code = '',
  120. $session_id = null
  121. ) {
  122. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  123. $course_id = api_get_course_int_id();
  124. if (!isset($session_id)) {
  125. $session_id = $this->session_id;
  126. }
  127. $condition_session = api_get_session_condition($session_id);
  128. if (!empty($course_code)) {
  129. $course_info = api_get_course_info($course_code);
  130. $course_id = $course_info['real_id'];
  131. }
  132. $description_type = intval($description_type);
  133. $sql = "SELECT * FROM $tbl_course_description
  134. WHERE c_id = $course_id AND description_type='$description_type' $condition_session ";
  135. $rs = Database::query($sql);
  136. $data = array();
  137. if ($description = Database::fetch_array($rs)) {
  138. $data['description_title'] = $description['title'];
  139. $data['description_content'] = $description['content'];
  140. $data['progress'] = $description['progress'];
  141. }
  142. return $data;
  143. }
  144. /**
  145. * @param int $id
  146. * @param string $course_code
  147. * @param int $session_id
  148. * @return array
  149. */
  150. public function get_data_by_id($id, $course_code = '', $session_id = null)
  151. {
  152. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  153. $course_id = api_get_course_int_id();
  154. if (!isset($session_id)) {
  155. $session_id = $this->session_id;
  156. }
  157. $condition_session = api_get_session_condition($session_id);
  158. if (!empty($course_code)) {
  159. $course_info = api_get_course_info($course_code);
  160. $course_id = $course_info['real_id'];
  161. }
  162. $id = intval($id);
  163. $sql = "SELECT * FROM $tbl_course_description
  164. WHERE c_id = $course_id AND id='$id' $condition_session ";
  165. $rs = Database::query($sql);
  166. $data = array();
  167. if ($description = Database::fetch_array($rs)) {
  168. $data['description_type'] = $description['description_type'];
  169. $data['description_title'] = $description['title'];
  170. $data['description_content'] = $description['content'];
  171. $data['progress'] = $description['progress'];
  172. }
  173. return $data;
  174. }
  175. /**
  176. * Get maximum description type by session id,
  177. * first you must set session_id properties with the object CourseDescription
  178. * @return int maximum description time adding one
  179. */
  180. public function get_max_description_type()
  181. {
  182. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  183. $course_id = api_get_course_int_id();
  184. $sql = "SELECT MAX(description_type) as MAX FROM $tbl_course_description
  185. WHERE c_id = $course_id AND session_id='" . $this->session_id . "'";
  186. $rs = Database::query($sql);
  187. $max = Database::fetch_array($rs);
  188. $description_type = $max['MAX'] + 1;
  189. if ($description_type < ADD_BLOCK) {
  190. $description_type = ADD_BLOCK;
  191. }
  192. return $description_type;
  193. }
  194. /**
  195. * Insert a description to the course_description table,
  196. * first you must set description_type, title, content, progress and
  197. * session_id properties with the object CourseDescription
  198. * @return int affected rows
  199. */
  200. public function insert()
  201. {
  202. if (empty($this->course_id)) {
  203. $course_id = api_get_course_int_id();
  204. } else {
  205. $course_id = $this->course_id;
  206. }
  207. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  208. $sql = "INSERT IGNORE INTO $tbl_course_description SET
  209. c_id = $course_id,
  210. description_type = '" . intval($this->description_type) . "',
  211. title = '" . Database::escape_string($this->title) . "',
  212. content = '" . Database::escape_string($this->content) . "',
  213. progress = '" . intval($this->progress) . "',
  214. session_id = '" . intval($this->session_id) . "' ";
  215. $result = Database::query($sql);
  216. $last_id = Database::insert_id();
  217. $affected_rows = Database::affected_rows($result);
  218. if ($last_id > 0) {
  219. $sql = "UPDATE $tbl_course_description SET id = iid WHERE iid = $last_id";
  220. Database::query($sql);
  221. //insert into item_property
  222. api_item_property_update(
  223. api_get_course_info(),
  224. TOOL_COURSE_DESCRIPTION,
  225. $last_id,
  226. 'CourseDescriptionAdded',
  227. api_get_user_id()
  228. );
  229. }
  230. return $affected_rows;
  231. }
  232. /**
  233. * Insert a row like history inside track_e_item_property table
  234. * first you must set description_type, title, content, progress and
  235. * session_id properties with the object CourseDescription
  236. * @param int description type
  237. * @return int affected rows
  238. */
  239. public function insert_stats($description_type)
  240. {
  241. $tbl_stats_item_property = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY);
  242. $description_id = $this->get_id_by_description_type($description_type);
  243. $course_id = api_get_real_course_id();
  244. $course_code = api_get_course_id();
  245. $item_property_id = api_get_item_property_id($course_code,
  246. TOOL_COURSE_DESCRIPTION, $description_id);
  247. $sql = "INSERT IGNORE INTO $tbl_stats_item_property SET
  248. c_id = " . api_get_course_int_id() . ",
  249. course_id = '$course_id',
  250. item_property_id = '$item_property_id',
  251. title = '" . Database::escape_string($this->title) . "',
  252. content = '" . Database::escape_string($this->content) . "',
  253. progress = '" . intval($this->progress) . "',
  254. lastedit_date = '" . date('Y-m-d H:i:s') . "',
  255. lastedit_user_id = '" . api_get_user_id() . "',
  256. session_id = '" . intval($this->session_id) . "'";
  257. $result = Database::query($sql);
  258. $affected_rows = Database::affected_rows($result);
  259. return $affected_rows;
  260. }
  261. /**
  262. * Update a description, first you must set description_type, title, content, progress
  263. * and session_id properties with the object CourseDescription
  264. * @return int affected rows
  265. */
  266. public function update()
  267. {
  268. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  269. $sql = "UPDATE $tbl_course_description SET
  270. title = '" . Database::escape_string($this->title) . "',
  271. content = '" . Database::escape_string($this->content) . "',
  272. progress = '" . $this->progress . "'
  273. WHERE
  274. id = '" . intval($this->id) . "' AND
  275. session_id = '" . $this->session_id . "' AND
  276. c_id = " . api_get_course_int_id();
  277. $result = Database::query($sql);
  278. $affected_rows = Database::affected_rows($result);
  279. if ($this->id > 0) {
  280. //insert into item_property
  281. api_item_property_update(
  282. api_get_course_info(),
  283. TOOL_COURSE_DESCRIPTION,
  284. $this->id,
  285. 'CourseDescriptionUpdated',
  286. api_get_user_id()
  287. );
  288. }
  289. return $affected_rows;
  290. }
  291. /**
  292. * Delete a description, first you must set description_type and session_id
  293. * properties with the object CourseDescription
  294. * @return int affected rows
  295. */
  296. public function delete()
  297. {
  298. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  299. $course_id = api_get_course_int_id();
  300. $sql = "DELETE FROM $tbl_course_description
  301. WHERE
  302. c_id = $course_id AND
  303. id = '" . intval($this->id) . "' AND
  304. session_id = '" . intval($this->session_id) . "'";
  305. $result = Database::query($sql);
  306. $affected_rows = Database::affected_rows($result);
  307. if ($this->id > 0) {
  308. //insert into item_property
  309. api_item_property_update(
  310. api_get_course_info(),
  311. TOOL_COURSE_DESCRIPTION,
  312. $this->id,
  313. 'CourseDescriptionDeleted',
  314. api_get_user_id()
  315. );
  316. }
  317. return $affected_rows;
  318. }
  319. /**
  320. * Get description id by description type
  321. * @param int description type
  322. * @return int description id
  323. */
  324. public function get_id_by_description_type($description_type)
  325. {
  326. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  327. $course_id = api_get_course_int_id();
  328. $sql = "SELECT id FROM $tbl_course_description
  329. WHERE c_id = $course_id AND description_type = '" . intval($description_type) . "'";
  330. $rs = Database::query($sql);
  331. $row = Database::fetch_array($rs);
  332. $description_id = $row['id'];
  333. return $description_id;
  334. }
  335. /**
  336. * get thematic progress in porcent for a course,
  337. * first you must set session_id property with the object CourseDescription
  338. * @param bool true for showing a icon about the progress, false otherwise (optional)
  339. * @param int Description type (optional)
  340. * @return string img html
  341. */
  342. public function get_progress_porcent(
  343. $with_icon = false,
  344. $description_type = THEMATIC_ADVANCE
  345. ) {
  346. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  347. $session_id = api_get_session_id();
  348. $course_id = api_get_course_int_id();
  349. $sql = "SELECT progress FROM $tbl_course_description
  350. WHERE
  351. c_id = $course_id AND
  352. description_type = '" . intval($description_type) . "' AND
  353. session_id = '" . intval($this->session_id) . "' ";
  354. $rs = Database::query($sql);
  355. $progress = '';
  356. $img = '';
  357. $title = '0%';
  358. $image = 'level_0.png';
  359. if (Database::num_rows($rs) > 0) {
  360. $row = Database::fetch_array($rs);
  361. $progress = $row['progress'] . '%';
  362. $image = 'level_' . $row['progress'] . '.png';
  363. }
  364. if ($with_icon) {
  365. $img = Display::return_icon($image, get_lang('ThematicAdvance'),
  366. array('style' => 'vertical-align:middle'));
  367. }
  368. $progress = $img . $progress;
  369. return $progress;
  370. }
  371. /**
  372. * Get description titles by default
  373. * @return array
  374. */
  375. public function get_default_description_title()
  376. {
  377. $default_description_titles = array();
  378. $default_description_titles[1] = get_lang('GeneralDescription');
  379. $default_description_titles[2] = get_lang('Objectives');
  380. $default_description_titles[3] = get_lang('Topics');
  381. $default_description_titles[4] = get_lang('Methodology');
  382. $default_description_titles[5] = get_lang('CourseMaterial');
  383. $default_description_titles[6] = get_lang('HumanAndTechnicalResources');
  384. $default_description_titles[7] = get_lang('Assessment');
  385. $default_description_titles[8] = get_lang('Other');
  386. return $default_description_titles;
  387. }
  388. /**
  389. * Get description titles editable by default
  390. * @return array
  391. */
  392. public function get_default_description_title_editable()
  393. {
  394. $default_description_title_editable = array();
  395. $default_description_title_editable[1] = true;
  396. $default_description_title_editable[2] = true;
  397. $default_description_title_editable[3] = true;
  398. $default_description_title_editable[4] = true;
  399. $default_description_title_editable[5] = true;
  400. $default_description_title_editable[6] = true;
  401. $default_description_title_editable[7] = true;
  402. //$default_description_title_editable[8] = true;
  403. return $default_description_title_editable;
  404. }
  405. /**
  406. * Get description icons by default
  407. * @return array
  408. */
  409. public function get_default_description_icon()
  410. {
  411. $default_description_icon = array();
  412. $default_description_icon[1] = 'info.png';
  413. $default_description_icon[2] = 'objective.png';
  414. $default_description_icon[3] = 'topics.png';
  415. $default_description_icon[4] = 'strategy.png';
  416. $default_description_icon[5] = 'laptop.png';
  417. $default_description_icon[6] = 'teacher.png';
  418. $default_description_icon[7] = 'assessment.png';
  419. //$default_description_icon[8]= 'porcent.png';
  420. $default_description_icon[8] = 'wizard.png';
  421. return $default_description_icon;
  422. }
  423. /**
  424. * Get questions by default for help
  425. * @return array
  426. */
  427. public function get_default_question()
  428. {
  429. $question = array();
  430. $question[1] = get_lang('GeneralDescriptionQuestions');
  431. $question[2] = get_lang('ObjectivesQuestions');
  432. $question[3] = get_lang('TopicsQuestions');
  433. $question[4] = get_lang('MethodologyQuestions');
  434. $question[5] = get_lang('CourseMaterialQuestions');
  435. $question[6] = get_lang('HumanAndTechnicalResourcesQuestions');
  436. $question[7] = get_lang('AssessmentQuestions');
  437. //$question[8]= get_lang('ThematicAdvanceQuestions');
  438. return $question;
  439. }
  440. /**
  441. * Get informations by default for help
  442. * @return array
  443. */
  444. public function get_default_information()
  445. {
  446. $information = array();
  447. $information[1] = get_lang('GeneralDescriptionInformation');
  448. $information[2] = get_lang('ObjectivesInformation');
  449. $information[3] = get_lang('TopicsInformation');
  450. $information[4] = get_lang('MethodologyInformation');
  451. $information[5] = get_lang('CourseMaterialInformation');
  452. $information[6] = get_lang('HumanAndTechnicalResourcesInformation');
  453. $information[7] = get_lang('AssessmentInformation');
  454. //$information[8]= get_lang('ThematicAdvanceInformation');
  455. return $information;
  456. }
  457. /**
  458. * Set description id
  459. * @return void
  460. */
  461. public function set_id($id)
  462. {
  463. $this->id = $id;
  464. }
  465. /**
  466. * Set description's course id
  467. * @param int Course ID
  468. * @return void
  469. */
  470. public function set_course_id($id)
  471. {
  472. $this->course_id = intval($id);
  473. }
  474. /**
  475. * Set description title
  476. * @return void
  477. */
  478. public function set_title($title)
  479. {
  480. $this->title = $title;
  481. }
  482. /**
  483. * Set description content
  484. * @return void
  485. */
  486. public function set_content($content)
  487. {
  488. $this->content = $content;
  489. }
  490. /**
  491. * Set description session id
  492. * @return void
  493. */
  494. public function set_session_id($session_id)
  495. {
  496. $this->session_id = $session_id;
  497. }
  498. /**
  499. * Set description type
  500. * @return void
  501. */
  502. public function set_description_type($description_type)
  503. {
  504. $this->description_type = $description_type;
  505. }
  506. /**
  507. * Set progress of a description
  508. * @return void
  509. */
  510. public function set_progress($progress)
  511. {
  512. $this->progress = $progress;
  513. }
  514. /**
  515. * get description id
  516. * @return int
  517. */
  518. public function get_id()
  519. {
  520. return $this->id;
  521. }
  522. /**
  523. * get description title
  524. * @return string
  525. */
  526. public function get_title()
  527. {
  528. return $this->title;
  529. }
  530. /**
  531. * get description content
  532. * @return string
  533. */
  534. public function get_content()
  535. {
  536. return $this->content;
  537. }
  538. /**
  539. * get session id
  540. * @return int
  541. */
  542. public function get_session_id()
  543. {
  544. return $this->session_id;
  545. }
  546. /**
  547. * get description type
  548. * @return int
  549. */
  550. public function get_description_type()
  551. {
  552. return $this->description_type;
  553. }
  554. /**
  555. * get progress of a description
  556. * @return int
  557. */
  558. public function get_progress()
  559. {
  560. return $this->progress;
  561. }
  562. }