course_description.lib.php 17 KB

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