course_description.lib.php 18 KB

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