course_description.lib.php 17 KB

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