course_description.lib.php 16 KB

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