course_description.lib.php 15 KB

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