course_description.lib.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains a class used like library provides functions for course description tool. It's also used like model to course_description_controller (MVC pattern)
  5. * @author Christian Fasanando <christian1827@gmail.com>
  6. * @package chamilo.course_description
  7. */
  8. /**
  9. * CourseDescription can be used to instanciate objects or as a library to manage course descriptions
  10. * @package chamilo.course_description
  11. */
  12. class CourseDescription
  13. {
  14. private $id;
  15. private $title;
  16. private $content;
  17. private $session_id;
  18. private $description_type;
  19. private $progress;
  20. /**
  21. * Constructor
  22. */
  23. public function __construct() {}
  24. /**
  25. * Get all data of course description by session id,
  26. * first you must set session_id property with the object CourseDescription
  27. * @return array
  28. */
  29. public function get_description_data() {
  30. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  31. $condition_session = api_get_session_condition($this->session_id, false, true);
  32. $sql = "SELECT * FROM $tbl_course_description $condition_session ORDER BY id ";
  33. $rs = Database::query($sql);
  34. $data = array();
  35. while ($description = Database::fetch_array($rs)) {
  36. if ($description['description_type'] == THEMATIC_ADVANCE) {
  37. $description['progress_icon'] = $this->get_progress_porcent();
  38. }
  39. $data['descriptions'][$description['description_type']] = $description;
  40. //reload titles to ensure we have the last version (after edition)
  41. $data['default_description_titles'][$description['description_type']] = $description['title'];
  42. }
  43. return $data;
  44. }
  45. /**
  46. * Get all data of course description by session id,
  47. * first you must set session_id property with the object CourseDescription
  48. * @return array
  49. */
  50. public function get_description_history($description_type) {
  51. $tbl_stats_item_property = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY);
  52. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  53. $course_id = api_get_real_course_id();
  54. $description_id = $this->get_id_by_description_type($description_type);
  55. $item_property_id = api_get_item_property_id($course_id, TOOL_COURSE_DESCRIPTION, $description_id);
  56. $sql = "SELECT tip.id, tip.course_id, tip.item_property_id, tip.title, tip.content, tip.progress, tip.lastedit_date, tip.session_id FROM $tbl_stats_item_property tip
  57. INNER JOIN $tbl_item_property ip ON ip.tool = '".TOOL_COURSE_DESCRIPTION."' AND ip.id = tip.item_property_id
  58. WHERE tip.course_id = '$course_id' AND tip.session_id = '".intval($this->session_id)."' ORDER BY tip.lastedit_date DESC";
  59. $rs = Database::query($sql);
  60. $data = array();
  61. while ($description = Database::fetch_array($rs)) {
  62. $data['descriptions'][] = $description;
  63. }
  64. return $data;
  65. }
  66. /**
  67. * Get all data by description and session id,
  68. * first you must set session_id property with the object CourseDescription
  69. * @param int description type
  70. * @param string course code (optional)
  71. * @param int session id (optional)
  72. * @return array
  73. */
  74. public function get_data_by_description_type($description_type, $course_code = '', $session_id = null) {
  75. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  76. if (!isset($session_id)) {
  77. $session_id = $this->session_id;
  78. }
  79. $condition_session = api_get_session_condition($session_id);
  80. if (!empty($course_code)) {
  81. $course_info = api_get_course_info($course_code);
  82. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION, $course_info['dbName']);
  83. }
  84. $sql = "SELECT * FROM $tbl_course_description WHERE description_type='$description_type' $condition_session ";
  85. $rs = Database::query($sql);
  86. $data = array();
  87. if ($description = Database::fetch_array($rs)) {
  88. $data['description_title'] = $description['title'];
  89. $data['description_content'] = $description['content'];
  90. $data['progress'] = $description['progress'];
  91. }
  92. return $data;
  93. }
  94. /**
  95. * Get maximum description type by session id, first you must set session_id properties with the object CourseDescription
  96. * @return int maximum description time adding one
  97. */
  98. public function get_max_description_type() {
  99. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  100. $sql = "SELECT MAX(description_type) as MAX FROM $tbl_course_description WHERE session_id='".$this->session_id."'";
  101. $rs = Database::query($sql);
  102. $max = Database::fetch_array($rs);
  103. $description_type = $max['MAX']+1;
  104. if ($description_type < ADD_BLOCK) {
  105. $description_type = ADD_BLOCK;
  106. }
  107. return $description_type;
  108. }
  109. /**
  110. * Insert a description to the course_description table,
  111. * first you must set description_type, title, content, progress and session_id properties with the object CourseDescription
  112. * @return int affected rows
  113. */
  114. public function insert() {
  115. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  116. $sql = "INSERT IGNORE INTO $tbl_course_description SET description_type='".intval($this->description_type)."', title = '".Database::escape_string($this->title)."', content = '".Database::escape_string($this->content)."', progress = '".intval($this->progress)."', session_id = '".intval($this->session_id)."' ";
  117. Database::query($sql);
  118. $last_id = Database::insert_id();
  119. $affected_rows = Database::affected_rows();
  120. if ($last_id > 0) {
  121. //insert into item_property
  122. api_item_property_update(api_get_course_info(), TOOL_COURSE_DESCRIPTION, $last_id, 'CourseDescriptionAdded', api_get_user_id());
  123. }
  124. return $affected_rows;
  125. }
  126. /**
  127. * Insert a row like history inside track_e_item_property table
  128. * first you must set description_type, title, content, progress and session_id properties with the object CourseDescription
  129. * @param int description type
  130. * @return int affected rows
  131. */
  132. public function insert_stats($description_type) {
  133. $tbl_stats_item_property = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY);
  134. $description_id = $this->get_id_by_description_type($description_type);
  135. $course_id = api_get_real_course_id();
  136. $course_code = api_get_course_id();
  137. $item_property_id = api_get_item_property_id($course_code, TOOL_COURSE_DESCRIPTION, $description_id);
  138. $sql = "INSERT IGNORE INTO $tbl_stats_item_property SET
  139. course_id = '$course_id',
  140. item_property_id = '$item_property_id',
  141. title = '".Database::escape_string($this->title)."',
  142. content = '".Database::escape_string($this->content)."',
  143. progress = '".intval($this->progress)."',
  144. lastedit_date = '".date('Y-m-d H:i:s')."',
  145. lastedit_user_id = '".api_get_user_id()."',
  146. session_id = '".intval($this->session_id)."'";
  147. Database::query($sql);
  148. $affected_rows = Database::affected_rows();
  149. return $affected_rows;
  150. }
  151. /**
  152. * Update a description, first you must set description_type, title, content, progress
  153. * and session_id properties with the object CourseDescription
  154. * @return int affected rows
  155. */
  156. public function update() {
  157. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  158. $sql = "UPDATE $tbl_course_description SET title = '".Database::escape_string($this->title)."', content = '".Database::escape_string($this->content)."', progress = '".$this->progress."' WHERE description_type='".intval($this->description_type)."' AND session_id = '".$this->session_id."'";
  159. Database::query($sql);
  160. $affected_rows = Database::affected_rows();
  161. $description_id = $this->get_id_by_description_type($this->description_type);
  162. if ($description_id > 0) {
  163. //insert into item_property
  164. api_item_property_update(api_get_course_info(), TOOL_COURSE_DESCRIPTION, $description_id, 'CourseDescriptionUpdated', api_get_user_id());
  165. }
  166. return $affected_rows;
  167. }
  168. /**
  169. * Delete a description, first you must set description_type and session_id properties with the object CourseDescription
  170. * @return int affected rows
  171. */
  172. public function delete() {
  173. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  174. $description_id = $this->get_id_by_description_type($this->description_type);
  175. $sql = "DELETE FROM $tbl_course_description WHERE description_type = '".intval($this->description_type)."' AND session_id = '".intval($this->session_id)."'";
  176. Database::query($sql);
  177. $affected_rows = Database::affected_rows();
  178. if ($description_id > 0) {
  179. //insert into item_property
  180. api_item_property_update(api_get_course_info(), TOOL_COURSE_DESCRIPTION, $description_id, 'CourseDescriptionDeleted', api_get_user_id());
  181. }
  182. return $affected_rows;
  183. }
  184. /**
  185. * Get description id by description type
  186. * @param int description type
  187. * @return int description id
  188. */
  189. public function get_id_by_description_type($description_type) {
  190. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  191. $sql = "SELECT id FROM $tbl_course_description WHERE description_type = '".intval($description_type)."'";
  192. $rs = Database::query($sql);
  193. $row = Database::fetch_array($rs);
  194. $description_id = $row['id'];
  195. return $description_id;
  196. }
  197. /**
  198. * get thematic progress in porcent for a course,
  199. * first you must set session_id property with the object CourseDescription
  200. * @param bool true for showing a icon about the progress, false otherwise (optional)
  201. * @param int Description type (optional)
  202. * @return string img html
  203. */
  204. public function get_progress_porcent($with_icon = false, $description_type = THEMATIC_ADVANCE) {
  205. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  206. $session_id = intval($session_id);
  207. $sql = "SELECT progress FROM $tbl_course_description WHERE description_type = '".intval($description_type)."' AND session_id = '".intval($this->session_id)."' ";
  208. $rs = Database::query($sql);
  209. $progress = '';
  210. $img = '';
  211. $title = '0%';
  212. $image = 'level_0.png';
  213. if (Database::num_rows($rs) > 0) {
  214. $row = Database::fetch_array($rs);
  215. $progress = $row['progress'].'%';
  216. $image = 'level_'.$row['progress'].'.png';
  217. }
  218. if ($with_icon) {
  219. $img = Display::return_icon($image,get_lang('ThematicAdvance'),array('style'=>'vertical-align:middle'));
  220. }
  221. $progress = $img.$progress;
  222. return $progress;
  223. }
  224. /**
  225. * Get description titles by default
  226. * @return array
  227. */
  228. public function get_default_description_title() {
  229. $default_description_titles = array();
  230. $default_description_titles[1]= get_lang('GeneralDescription');
  231. $default_description_titles[2]= get_lang('Objectives');
  232. $default_description_titles[3]= get_lang('Topics');
  233. $default_description_titles[4]= get_lang('Methodology');
  234. $default_description_titles[5]= get_lang('CourseMaterial');
  235. $default_description_titles[6]= get_lang('HumanAndTechnicalResources');
  236. $default_description_titles[7]= get_lang('Assessment');
  237. //$default_description_titles[8]= get_lang('ThematicAdvance');
  238. $default_description_titles[8]= get_lang('Other');
  239. return $default_description_titles;
  240. }
  241. /**
  242. * Get description titles editable by default
  243. * @return array
  244. */
  245. public function get_default_description_title_editable() {
  246. $default_description_title_editable = array();
  247. $default_description_title_editable[1] = true;
  248. $default_description_title_editable[2] = true;
  249. $default_description_title_editable[3] = true;
  250. $default_description_title_editable[4] = true;
  251. $default_description_title_editable[5] = true;
  252. $default_description_title_editable[6] = true;
  253. $default_description_title_editable[7] = true;
  254. //$default_description_title_editable[8] = true;
  255. return $default_description_title_editable;
  256. }
  257. /**
  258. * Get description icons by default
  259. * @return array
  260. */
  261. public function get_default_description_icon() {
  262. $default_description_icon = array();
  263. $default_description_icon[1]= 'edu_miscellaneous.gif';
  264. $default_description_icon[2]= 'spire.gif';
  265. $default_description_icon[3]= 'kcmdf_big.gif';
  266. $default_description_icon[4]= 'misc.gif';
  267. $default_description_icon[5]= 'laptop.gif';
  268. $default_description_icon[6]= 'personal.gif';
  269. $default_description_icon[7]= 'korganizer.gif';
  270. //$default_description_icon[8]= 'porcent.png';
  271. $default_description_icon[8]= 'ktip.gif';
  272. return $default_description_icon;
  273. }
  274. /**
  275. * Get questions by default for help
  276. * @return array
  277. */
  278. public function get_default_question() {
  279. $question = array();
  280. $question[1]= get_lang('GeneralDescriptionQuestions');
  281. $question[2]= get_lang('ObjectivesQuestions');
  282. $question[3]= get_lang('TopicsQuestions');
  283. $question[4]= get_lang('MethodologyQuestions');
  284. $question[5]= get_lang('CourseMaterialQuestions');
  285. $question[6]= get_lang('HumanAndTechnicalResourcesQuestions');
  286. $question[7]= get_lang('AssessmentQuestions');
  287. //$question[8]= get_lang('ThematicAdvanceQuestions');
  288. return $question;
  289. }
  290. /**
  291. * Get informations by default for help
  292. * @return array
  293. */
  294. public function get_default_information() {
  295. $information = array();
  296. $information[1]= get_lang('GeneralDescriptionInformation');
  297. $information[2]= get_lang('ObjectivesInformation');
  298. $information[3]= get_lang('TopicsInformation');
  299. $information[4]= get_lang('MethodologyInformation');
  300. $information[5]= get_lang('CourseMaterialInformation');
  301. $information[6]= get_lang('HumanAndTechnicalResourcesInformation');
  302. $information[7]= get_lang('AssessmentInformation');
  303. //$information[8]= get_lang('ThematicAdvanceInformation');
  304. return $information;
  305. }
  306. /**
  307. * Set description id
  308. * @return void
  309. */
  310. public function set_id($id) {
  311. $this->id = $id;
  312. }
  313. /**
  314. * Set description title
  315. * @return void
  316. */
  317. public function set_title($title) {
  318. $this->title = $title;
  319. }
  320. /**
  321. * Set description content
  322. * @return void
  323. */
  324. public function set_content($content) {
  325. $this->content = $content;
  326. }
  327. /**
  328. * Set description session id
  329. * @return void
  330. */
  331. public function set_session_id($session_id) {
  332. $this->session_id = $session_id;
  333. }
  334. /**
  335. * Set description type
  336. * @return void
  337. */
  338. public function set_description_type($description_type) {
  339. $this->description_type = $description_type;
  340. }
  341. /**
  342. * Set progress of a description
  343. * @return void
  344. */
  345. public function set_progress($progress) {
  346. $this->progress = $progress;
  347. }
  348. /**
  349. * get description id
  350. * @return int
  351. */
  352. public function get_id() {
  353. return $this->id;
  354. }
  355. /**
  356. * get description title
  357. * @return string
  358. */
  359. public function get_title() {
  360. return $this->title;
  361. }
  362. /**
  363. * get description content
  364. * @return string
  365. */
  366. public function get_content() {
  367. return $this->content;
  368. }
  369. /**
  370. * get session id
  371. * @return int
  372. */
  373. public function get_session_id() {
  374. return $this->session_id;
  375. }
  376. /**
  377. * get description type
  378. * @return int
  379. */
  380. public function get_description_type() {
  381. return $this->description_type;
  382. }
  383. /**
  384. * get progress of a description
  385. * @return int
  386. */
  387. public function get_progress() {
  388. return $this->progress;
  389. }
  390. }
  391. ?>