course_description.lib.php 17 KB

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