course_description.lib.php 17 KB

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