course_description.lib.php 17 KB

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