course_description.lib.php 17 KB

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