course_description.lib.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. * Class CourseDescription course descriptions
  12. *
  13. * @package chamilo.course_description
  14. */
  15. class CourseDescription
  16. {
  17. private $id;
  18. private $course_id;
  19. private $title;
  20. private $content;
  21. private $session_id;
  22. private $description_type;
  23. private $progress;
  24. /**
  25. * Constructor
  26. */
  27. public function __construct()
  28. {
  29. }
  30. /**
  31. * Returns an array of objects of type CourseDescription corresponding to
  32. * a specific course, without session ids (session id = 0)
  33. *
  34. * @param int $course_id
  35. *
  36. * @return array Array of CourseDescriptions
  37. */
  38. public static function get_descriptions($course_id)
  39. {
  40. // Get course code
  41. $course_info = api_get_course_info_by_id($course_id);
  42. if (!empty($course_info)) {
  43. $course_id = $course_info['real_id'];
  44. } else {
  45. return array();
  46. }
  47. $t_course_desc = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  48. $sql = "SELECT * FROM $t_course_desc
  49. WHERE c_id = $course_id AND session_id = '0'";
  50. $sql_result = Database::query($sql);
  51. $results = array();
  52. while ($row = Database::fetch_array($sql_result)) {
  53. $desc_tmp = new CourseDescription();
  54. $desc_tmp->set_id($row['id']);
  55. $desc_tmp->set_title($row['title']);
  56. $desc_tmp->set_content($row['content']);
  57. $desc_tmp->set_session_id($row['session_id']);
  58. $desc_tmp->set_description_type($row['description_type']);
  59. $desc_tmp->set_progress($row['progress']);
  60. $results[] = $desc_tmp;
  61. }
  62. return $results;
  63. }
  64. /**
  65. * Get all data of course description by session id,
  66. * first you must set session_id property with the object CourseDescription
  67. * @return array
  68. */
  69. public function get_description_data()
  70. {
  71. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  72. $condition_session = api_get_session_condition($this->session_id, true, true);
  73. $course_id = api_get_course_int_id();
  74. $sql = "SELECT * FROM $tbl_course_description
  75. WHERE c_id = $course_id $condition_session
  76. ORDER BY id ";
  77. $rs = Database::query($sql);
  78. $data = array();
  79. while ($description = Database::fetch_array($rs)) {
  80. $data['descriptions'][$description['id']] = Security::remove_XSS($description, STUDENT);
  81. }
  82. return $data;
  83. }
  84. /**
  85. * Get all data by description and session id,
  86. * first you must set session_id property with the object CourseDescription
  87. * @param int $description_type Description type
  88. * @param string $courseId Course code (optional)
  89. * @param int $session_id Session id (optional)
  90. * @return array List of fields from the descriptions found of the given type
  91. */
  92. public function get_data_by_description_type(
  93. $description_type,
  94. $courseId = null,
  95. $session_id = null
  96. ) {
  97. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  98. if (empty($courseId)) {
  99. $courseId = api_get_course_int_id();
  100. }
  101. if (!isset($session_id)) {
  102. $session_id = $this->session_id;
  103. }
  104. $condition_session = api_get_session_condition($session_id);
  105. $description_type = intval($description_type);
  106. $sql = "SELECT * FROM $tbl_course_description
  107. WHERE c_id = $courseId AND description_type='$description_type' $condition_session ";
  108. $rs = Database::query($sql);
  109. $data = array();
  110. if ($description = Database::fetch_array($rs)) {
  111. $data['description_title'] = $description['title'];
  112. $data['description_content'] = $description['content'];
  113. $data['progress'] = $description['progress'];
  114. $data['id'] = $description['id'];
  115. }
  116. return $data;
  117. }
  118. /**
  119. * @param int $id
  120. * @param string $course_code
  121. * @param int $session_id
  122. *
  123. * @return array
  124. */
  125. public function get_data_by_id($id, $course_code = '', $session_id = null)
  126. {
  127. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  128. $course_id = api_get_course_int_id();
  129. if (!isset($session_id)) {
  130. $session_id = $this->session_id;
  131. }
  132. $condition_session = api_get_session_condition($session_id);
  133. if (!empty($course_code)) {
  134. $course_info = api_get_course_info($course_code);
  135. $course_id = $course_info['real_id'];
  136. }
  137. $id = intval($id);
  138. $sql = "SELECT * FROM $tbl_course_description
  139. WHERE c_id = $course_id AND id='$id' $condition_session ";
  140. $rs = Database::query($sql);
  141. $data = array();
  142. if ($description = Database::fetch_array($rs)) {
  143. $data['description_type'] = $description['description_type'];
  144. $data['description_title'] = $description['title'];
  145. $data['description_content'] = $description['content'];
  146. $data['progress'] = $description['progress'];
  147. }
  148. return $data;
  149. }
  150. /**
  151. * Get maximum description type by session id,
  152. * first you must set session_id properties with the object CourseDescription
  153. * @return int maximum description time adding one
  154. */
  155. public function get_max_description_type()
  156. {
  157. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  158. $course_id = api_get_course_int_id();
  159. $sql = "SELECT MAX(description_type) as MAX
  160. FROM $tbl_course_description
  161. WHERE c_id = $course_id AND session_id='".$this->session_id."'";
  162. $rs = Database::query($sql);
  163. $max = Database::fetch_array($rs);
  164. if ($max['MAX'] >= 8) {
  165. $description_type = 8;
  166. } else {
  167. $description_type = $max['MAX'] + 1;
  168. }
  169. if ($description_type < ADD_BLOCK) {
  170. $description_type = ADD_BLOCK;
  171. }
  172. return $description_type;
  173. }
  174. /**
  175. * Insert a description to the course_description table,
  176. * first you must set description_type, title, content, progress and
  177. * session_id properties with the object CourseDescription
  178. * @return int affected rows
  179. */
  180. public function insert()
  181. {
  182. if (empty($this->course_id)) {
  183. $course_id = api_get_course_int_id();
  184. } else {
  185. $course_id = $this->course_id;
  186. }
  187. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  188. $params = [
  189. 'c_id' => $course_id,
  190. 'description_type' => $this->description_type,
  191. 'title' => $this->title,
  192. 'content' => $this->content,
  193. 'progress' => intval($this->progress),
  194. 'session_id' => $this->session_id
  195. ];
  196. $last_id = Database::insert($table, $params);
  197. if ($last_id > 0) {
  198. $sql = "UPDATE $table SET id = iid WHERE iid = $last_id";
  199. Database::query($sql);
  200. // insert into item_property
  201. api_item_property_update(
  202. api_get_course_info(),
  203. TOOL_COURSE_DESCRIPTION,
  204. $last_id,
  205. 'CourseDescriptionAdded',
  206. api_get_user_id()
  207. );
  208. }
  209. return ($last_id > 0) ? 1 : 0;
  210. }
  211. /**
  212. * Insert a row like history inside track_e_item_property table
  213. * first you must set description_type, title, content, progress and
  214. * session_id properties with the object CourseDescription
  215. * @param int $description_type
  216. * @return int affected rows
  217. */
  218. public function insert_stats($description_type)
  219. {
  220. $tbl_stats_item_property = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY);
  221. $description_id = $this->get_id_by_description_type($description_type);
  222. $course_id = api_get_course_int_id();
  223. $course_code = api_get_course_id();
  224. $item_property_id = api_get_item_property_id(
  225. $course_code,
  226. TOOL_COURSE_DESCRIPTION,
  227. $description_id
  228. );
  229. $params = [
  230. 'c_id' => api_get_course_int_id(),
  231. 'course_id' => $course_id,
  232. 'item_property_id' => $item_property_id,
  233. 'title' => $this->title,
  234. 'content' => $this->content,
  235. 'progress' => $this->progress,
  236. 'lastedit_date' => api_get_utc_datetime(),
  237. 'lastedit_user_id' => api_get_user_id(),
  238. 'session_id' => $this->session_id,
  239. ];
  240. $result = Database::insert($tbl_stats_item_property, $params);
  241. return $result ? 1 : 0;
  242. }
  243. /**
  244. * Update a description, first you must set description_type, title, content, progress
  245. * and session_id properties with the object CourseDescription
  246. * @return int affected rows
  247. */
  248. public function update()
  249. {
  250. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  251. $params = [
  252. 'title' => $this->title,
  253. 'content' => $this->content,
  254. 'progress' => intval($this->progress),
  255. ];
  256. Database::update(
  257. $table,
  258. $params,
  259. [
  260. 'id = ? AND session_id = ? AND c_id = ?' => [
  261. $this->id,
  262. $this->session_id,
  263. $this->course_id ? $this->course_id : api_get_course_int_id(),
  264. ]
  265. ]
  266. );
  267. if ($this->id > 0) {
  268. //insert into item_property
  269. api_item_property_update(
  270. api_get_course_info(),
  271. TOOL_COURSE_DESCRIPTION,
  272. $this->id,
  273. 'CourseDescriptionUpdated',
  274. api_get_user_id()
  275. );
  276. }
  277. return 1;
  278. }
  279. /**
  280. * Delete a description, first you must set description_type and session_id
  281. * properties with the object CourseDescription
  282. * @return int affected rows
  283. */
  284. public function delete()
  285. {
  286. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  287. $course_id = api_get_course_int_id();
  288. $sql = "DELETE FROM $tbl_course_description
  289. WHERE
  290. c_id = $course_id AND
  291. id = '".intval($this->id)."' AND
  292. session_id = '" . intval($this->session_id)."'";
  293. $result = Database::query($sql);
  294. $affected_rows = Database::affected_rows($result);
  295. if ($this->id > 0) {
  296. //insert into item_property
  297. api_item_property_update(
  298. api_get_course_info(),
  299. TOOL_COURSE_DESCRIPTION,
  300. $this->id,
  301. 'CourseDescriptionDeleted',
  302. api_get_user_id()
  303. );
  304. }
  305. return $affected_rows;
  306. }
  307. /**
  308. * Get description id by description type
  309. * @param int $description_type
  310. *
  311. * @return int description id
  312. */
  313. public function get_id_by_description_type($description_type)
  314. {
  315. $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  316. $course_id = api_get_course_int_id();
  317. $sql = "SELECT id FROM $tbl_course_description
  318. WHERE c_id = $course_id AND description_type = '".intval($description_type)."'";
  319. $rs = Database::query($sql);
  320. $row = Database::fetch_array($rs);
  321. $description_id = $row['id'];
  322. return $description_id;
  323. }
  324. /**
  325. * Get description titles by default
  326. * @return array
  327. */
  328. public function get_default_description_title()
  329. {
  330. $default_description_titles = array();
  331. $default_description_titles[1] = get_lang('GeneralDescription');
  332. $default_description_titles[2] = get_lang('Objectives');
  333. $default_description_titles[3] = get_lang('Topics');
  334. $default_description_titles[4] = get_lang('Methodology');
  335. $default_description_titles[5] = get_lang('CourseMaterial');
  336. $default_description_titles[6] = get_lang('HumanAndTechnicalResources');
  337. $default_description_titles[7] = get_lang('Assessment');
  338. $default_description_titles[8] = get_lang('Other');
  339. return $default_description_titles;
  340. }
  341. /**
  342. * Get description titles editable by default
  343. * @return array
  344. */
  345. public function get_default_description_title_editable()
  346. {
  347. $default_description_title_editable = array();
  348. $default_description_title_editable[1] = true;
  349. $default_description_title_editable[2] = true;
  350. $default_description_title_editable[3] = true;
  351. $default_description_title_editable[4] = true;
  352. $default_description_title_editable[5] = true;
  353. $default_description_title_editable[6] = true;
  354. $default_description_title_editable[7] = true;
  355. //$default_description_title_editable[8] = true;
  356. return $default_description_title_editable;
  357. }
  358. /**
  359. * Get description icons by default
  360. * @return array
  361. */
  362. public function get_default_description_icon()
  363. {
  364. $default_description_icon = array();
  365. $default_description_icon[1] = 'info.png';
  366. $default_description_icon[2] = 'objective.png';
  367. $default_description_icon[3] = 'topics.png';
  368. $default_description_icon[4] = 'strategy.png';
  369. $default_description_icon[5] = 'laptop.png';
  370. $default_description_icon[6] = 'teacher.png';
  371. $default_description_icon[7] = 'assessment.png';
  372. //$default_description_icon[8]= 'porcent.png';
  373. $default_description_icon[8] = 'wizard.png';
  374. return $default_description_icon;
  375. }
  376. /**
  377. * Get questions by default for help
  378. * @return array
  379. */
  380. public function get_default_question()
  381. {
  382. $question = array();
  383. $question[1] = get_lang('GeneralDescriptionQuestions');
  384. $question[2] = get_lang('ObjectivesQuestions');
  385. $question[3] = get_lang('TopicsQuestions');
  386. $question[4] = get_lang('MethodologyQuestions');
  387. $question[5] = get_lang('CourseMaterialQuestions');
  388. $question[6] = get_lang('HumanAndTechnicalResourcesQuestions');
  389. $question[7] = get_lang('AssessmentQuestions');
  390. //$question[8]= get_lang('ThematicAdvanceQuestions');
  391. return $question;
  392. }
  393. /**
  394. * Get informations by default for help
  395. * @return array
  396. */
  397. public function get_default_information()
  398. {
  399. $information = array();
  400. $information[1] = get_lang('GeneralDescriptionInformation');
  401. $information[2] = get_lang('ObjectivesInformation');
  402. $information[3] = get_lang('TopicsInformation');
  403. $information[4] = get_lang('MethodologyInformation');
  404. $information[5] = get_lang('CourseMaterialInformation');
  405. $information[6] = get_lang('HumanAndTechnicalResourcesInformation');
  406. $information[7] = get_lang('AssessmentInformation');
  407. //$information[8]= get_lang('ThematicAdvanceInformation');
  408. return $information;
  409. }
  410. /**
  411. * Set description id
  412. * @return void
  413. */
  414. public function set_id($id)
  415. {
  416. $this->id = $id;
  417. }
  418. /**
  419. * Set description's course id
  420. * @param int $id Course ID
  421. * @return void
  422. */
  423. public function set_course_id($id)
  424. {
  425. $this->course_id = intval($id);
  426. }
  427. /**
  428. * Set description title
  429. * @param string $title
  430. */
  431. public function set_title($title)
  432. {
  433. $this->title = $title;
  434. }
  435. /**
  436. * Set description content
  437. *
  438. * @param string $content
  439. */
  440. public function set_content($content)
  441. {
  442. $this->content = $content;
  443. }
  444. /**
  445. *
  446. * Set description session id
  447. *
  448. * @param int $session_id
  449. */
  450. public function set_session_id($session_id)
  451. {
  452. $this->session_id = $session_id;
  453. }
  454. /**
  455. * Set description type
  456. * @return void
  457. */
  458. public function set_description_type($description_type)
  459. {
  460. $this->description_type = $description_type;
  461. }
  462. /**
  463. *
  464. * Set progress of a description
  465. *
  466. * @param string $progress
  467. */
  468. public function set_progress($progress)
  469. {
  470. $this->progress = $progress;
  471. }
  472. /**
  473. * get description id
  474. *
  475. * @return int
  476. */
  477. public function get_id()
  478. {
  479. return $this->id;
  480. }
  481. /**
  482. * get description title
  483. *
  484. * @return string
  485. */
  486. public function get_title()
  487. {
  488. return $this->title;
  489. }
  490. /**
  491. * get description content
  492. * @return string
  493. */
  494. public function get_content()
  495. {
  496. return $this->content;
  497. }
  498. /**
  499. * get session id
  500. *
  501. * @return int
  502. */
  503. public function get_session_id()
  504. {
  505. return $this->session_id;
  506. }
  507. /**
  508. * get description type
  509. *
  510. * @return int
  511. */
  512. public function get_description_type()
  513. {
  514. return $this->description_type;
  515. }
  516. /**
  517. * get progress of a description
  518. *
  519. * @return int
  520. */
  521. public function get_progress()
  522. {
  523. return $this->progress;
  524. }
  525. }