cm_webservice_course.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. require_once __DIR__.'/cm_webservice.php';
  8. /**
  9. * Web services available for the Course module. This class extends the WS class.
  10. */
  11. class WSCMCourse extends WSCM
  12. {
  13. /**
  14. * Deletes a course.
  15. *
  16. * @param string API secret key
  17. * @param string Course id field name
  18. * @param string Course id value
  19. */
  20. public function DeleteCourse(
  21. $secret_key,
  22. $course_id_field_name,
  23. $course_id_value
  24. ) {
  25. $verifKey = $this->verifyKey($secret_key);
  26. if ($verifKey instanceof WSError) {
  27. $this->handleError($verifKey);
  28. } else {
  29. $result = $this->deleteCourseHelper(
  30. $course_id_field_name,
  31. $course_id_value
  32. );
  33. if ($result instanceof WSError) {
  34. $this->handleError($result);
  35. }
  36. }
  37. }
  38. /**
  39. * Deletes multiple courses.
  40. *
  41. * @param string API secret key
  42. * @param array Array of courses with elements of the form
  43. * array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value')
  44. *
  45. * @return array Array with elements like
  46. * array('course_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
  47. * Note that if the result array contains a code different
  48. * than 0, an error occured
  49. */
  50. public function DeleteCourses($secret_key, $courses)
  51. {
  52. $verifKey = $this->verifyKey($secret_key);
  53. if ($verifKey instanceof WSError) {
  54. $this->handleError($verifKey);
  55. } else {
  56. $results = [];
  57. foreach ($courses as $course) {
  58. $result_tmp = [];
  59. $result_op = $this->deleteCourseHelper($course['course_id_field_name'], $course['course_id_value']);
  60. $result_tmp['course_id_value'] = $course['course_id_value'];
  61. if ($result_op instanceof WSCMError) {
  62. // Return the error in the results
  63. $result_tmp['result'] = $result_op->toArray();
  64. } else {
  65. $result_tmp['result'] = $this->getSuccessfulResult();
  66. }
  67. $results[] = $result_tmp;
  68. }
  69. return $results;
  70. }
  71. }
  72. /**
  73. * Creates a course.
  74. *
  75. * @param string API secret key
  76. * @param string Title
  77. * @param string Category code
  78. * @param string Wanted code. If it's not defined, it will be generated automatically
  79. * @param string Tutor name
  80. * @param string Course admin user id field name
  81. * @param string Course admin user id value
  82. * @param string Course language
  83. * @param string Course id field name
  84. * @param string Course id value
  85. * @param array Course extra fields
  86. *
  87. * @return int Course id generated
  88. */
  89. public function CreateCourse(
  90. $secret_key,
  91. $title,
  92. $category_code,
  93. $wanted_code,
  94. $tutor_name,
  95. $course_admin_user_id_field_name,
  96. $course_admin_user_id_value,
  97. $language,
  98. $course_id_field_name,
  99. $course_id_value,
  100. $extras
  101. ) {
  102. // First, verify the secret key
  103. $verifKey = $this->verifyKey($secret_key);
  104. if ($verifKey instanceof WSError) {
  105. $this->handleError($verifKey);
  106. } else {
  107. $result = $this->createCourseHelper(
  108. $title,
  109. $category_code,
  110. $wanted_code,
  111. $tutor_name,
  112. $course_admin_user_id_field_name,
  113. $course_admin_user_id_value,
  114. $language,
  115. $course_id_field_name,
  116. $course_id_value,
  117. $extras
  118. );
  119. if ($result instanceof WSError) {
  120. $this->handleError($result);
  121. } else {
  122. return $result;
  123. }
  124. }
  125. }
  126. /**
  127. * Create multiple courses.
  128. *
  129. * @param string API secret key
  130. * @param array Courses to be created, with elements following the structure presented in CreateCourse
  131. *
  132. * @return array Array with elements of the form
  133. * array('course_id_value' => 'original value sent', 'course_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful'))
  134. */
  135. public function CreateCourses($secret_key, $courses)
  136. {
  137. // First, verify the secret key
  138. $verifKey = $this->verifyKey($secret_key);
  139. if ($verifKey instanceof WSCMError) {
  140. $this->handleError($verifKey);
  141. } else {
  142. $results = [];
  143. foreach ($courses as $course) {
  144. $result_tmp = [];
  145. //reinitialize variables just in case
  146. $title = $category_code = $wanted_code = $tutor_name = $course_admin_user_id_field_name = $course_admin_user_id_value = $language = $course_id_field_name = $course_id_value = $extras = null;
  147. extract($course);
  148. $result = $this->createCourseHelper(
  149. $title,
  150. $category_code,
  151. $wanted_code,
  152. $tutor_name,
  153. $course_admin_user_id_field_name,
  154. $course_admin_user_id_value,
  155. $language,
  156. $course_id_field_name,
  157. $course_id_value,
  158. $extras
  159. );
  160. if ($result instanceof WSCMError) {
  161. $result_tmp['result'] = $result->toArray();
  162. $result_tmp['course_id_value'] = $course_id_value;
  163. $result_tmp['course_id_generated'] = 0;
  164. } else {
  165. $result_tmp['result'] = $this->getSuccessfulResult();
  166. $result_tmp['course_id_value'] = $course_id_value;
  167. $result_tmp['course_id_generated'] = $result;
  168. }
  169. $results[] = $result_tmp;
  170. }
  171. return $results;
  172. }
  173. }
  174. /**
  175. * Edits a course.
  176. *
  177. * @param string API secret key
  178. * @param string Course id field name
  179. * @param string Course id value
  180. * @param string Title
  181. * @param string Category code
  182. * @param string Department name
  183. * @param string Department url
  184. * @param string Course language
  185. * @param int Visibility
  186. * @param int Subscribe (0 = denied, 1 = allowed)
  187. * @param int Unsubscribe (0 = denied, 1 = allowed)
  188. * @param string Visual code
  189. * @param array Course extra fields
  190. */
  191. public function EditCourse(
  192. $secret_key,
  193. $course_id_field_name,
  194. $course_id_value,
  195. $title,
  196. $category_code,
  197. $department_name,
  198. $department_url,
  199. $language,
  200. $visibility,
  201. $subscribe,
  202. $unsubscribe,
  203. $visual_code,
  204. $extras
  205. ) {
  206. $verifKey = $this->verifyKey($secret_key);
  207. if ($verifKey instanceof WSCMError) {
  208. $this->handleError($verifKey);
  209. } else {
  210. $result = $this->editCourseHelper(
  211. $course_id_field_name,
  212. $course_id_value,
  213. $title,
  214. $category_code,
  215. $department_name,
  216. $department_url,
  217. $language,
  218. $visibility,
  219. $subscribe,
  220. $unsubscribe,
  221. $visual_code,
  222. $extras
  223. );
  224. if ($result instanceof WSCMError) {
  225. $this->handleError($result);
  226. }
  227. }
  228. }
  229. /**
  230. * List courses.
  231. *
  232. * @param string API secret key
  233. * @param string Course id field name. Use "chamilo_course_id" to use internal id
  234. *
  235. * @return array An array with elements of the form
  236. * ('id' => 'Course internal id', 'code' => 'Course code', 'title' => 'Course title', 'language' => 'Course language', 'visibility' => 'Course visibility',
  237. * 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id')
  238. */
  239. public function ListCourses($secret_key, $course_id_field_name)
  240. {
  241. $verifKey = $this->verifyKey($secret_key);
  242. if ($verifKey instanceof WSError) {
  243. $this->handleError($verifKey);
  244. } else {
  245. $courses_result = [];
  246. $category_names = [];
  247. $courses = CourseManager::get_courses_list();
  248. foreach ($courses as $course) {
  249. $course_tmp = [];
  250. $course_tmp['id'] = $course['id'];
  251. $course_tmp['code'] = $course['code'];
  252. $course_tmp['title'] = $course['title'];
  253. $course_tmp['language'] = $course['course_language'];
  254. $course_tmp['visibility'] = $course['visibility'];
  255. // Determining category name
  256. if ($category_names[$course['category_code']]) {
  257. $course_tmp['category_name'] = $category_names[$course['category_code']];
  258. } else {
  259. $category = CourseManager::get_course_category($course['category_code']);
  260. $category_names[$course['category_code']] = $category['name'];
  261. $course_tmp['category_name'] = $category['name'];
  262. }
  263. // Determining number of students registered in course
  264. $user_list = CourseManager::get_user_list_from_course_code($course['code']);
  265. $course_tmp['number_students'] = count($user_list);
  266. // Determining external course id
  267. $course_tmp['external_course_id'] = CourseManager::get_course_extra_field_value($course_id_field_name, $course['code']);
  268. $courses_result[] = $course_tmp;
  269. }
  270. return $courses_result;
  271. }
  272. }
  273. /**
  274. * Subscribe user to a course.
  275. *
  276. * @param string API secret key
  277. * @param string Course id field name. Use "chamilo_course_id" to use internal id
  278. * @param string course id value
  279. * @param string User id field name. Use "chamilo_user_id" to use internal id
  280. * @param string User id value
  281. * @param int Status (1 = Teacher, 5 = Student)
  282. */
  283. public function SubscribeUserToCourse(
  284. $secret_key,
  285. $course_id_field_name,
  286. $course_id_value,
  287. $user_id_field_name,
  288. $user_id_value,
  289. $status
  290. ) {
  291. $verifKey = $this->verifyKey($secret_key);
  292. if ($verifKey instanceof WSError) {
  293. $this->handleError($verifKey);
  294. } else {
  295. $result = $this->changeUserSubscription(
  296. $course_id_field_name,
  297. $course_id_value,
  298. $user_id_field_name,
  299. $user_id_value,
  300. 1,
  301. $status
  302. );
  303. if ($result instanceof WSError) {
  304. $this->handleError($result);
  305. }
  306. }
  307. }
  308. /**
  309. * Unsusbscribe user from course.
  310. *
  311. * @param string API secret key
  312. * @param string Course id field name. Use "chamilo_course_id" to use internal id
  313. * @param string course id value
  314. * @param string User id field name. Use "chamilo_user_id" to use internal id
  315. * @param string User id value
  316. */
  317. public function UnsubscribeUserFromCourse(
  318. $secret_key,
  319. $course_id_field_name,
  320. $course_id_value,
  321. $user_id_field_name,
  322. $user_id_value
  323. ) {
  324. $verifKey = $this->verifyKey($secret_key);
  325. if ($verifKey instanceof WSError) {
  326. $this->handleError($verifKey);
  327. } else {
  328. $result = $this->changeUserSubscription(
  329. $course_id_field_name,
  330. $course_id_value,
  331. $user_id_field_name,
  332. $user_id_value,
  333. 0
  334. );
  335. if ($result instanceof WSError) {
  336. $this->handleError($result);
  337. }
  338. }
  339. }
  340. /**
  341. * Returns the descriptions of a course, along with their id.
  342. *
  343. * @param string API secret key
  344. * @param string Course id field name
  345. * @param string Course id value
  346. *
  347. * @return array Returns an array with elements of the form
  348. * array('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content')
  349. */
  350. public function GetCourseDescriptions(
  351. $secret_key,
  352. $course_id_field_name,
  353. $course_id_value
  354. ) {
  355. $verifKey = $this->verifyKey($secret_key);
  356. if ($verifKey instanceof WSError) {
  357. $this->handleError($verifKey);
  358. } else {
  359. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  360. if ($course_id instanceof WSError) {
  361. return $course_id;
  362. } else {
  363. // Course exists, get its descriptions
  364. $descriptions = CourseDescription::get_descriptions($course_id);
  365. $results = [];
  366. foreach ($descriptions as $description) {
  367. $results[] = ['course_desc_id' => $description->get_description_type(),
  368. 'course_desc_title' => $description->get_title(),
  369. 'course_desc_content' => $description->get_content(), ];
  370. }
  371. return $results;
  372. }
  373. }
  374. }
  375. /**
  376. * Edit course description.
  377. *
  378. * @param string API secret key
  379. * @param string Course id field name
  380. * @param string Course id value
  381. * @param int Category id from course description
  382. * @param string Description title
  383. * @param string Course description content
  384. */
  385. public function EditCourseDescription(
  386. $secret_key,
  387. $course_id_field_name,
  388. $course_id_value,
  389. $course_desc_id,
  390. $course_desc_title,
  391. $course_desc_content
  392. ) {
  393. $verifKey = $this->verifyKey($secret_key);
  394. if ($verifKey instanceof WSError) {
  395. $this->handleError($verifKey);
  396. } else {
  397. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  398. if ($course_id instanceof WSError) {
  399. return $course_id;
  400. } else {
  401. // Create the new course description
  402. $cd = new CourseDescription();
  403. $cd->set_description_type($course_desc_id);
  404. $cd->set_title($course_desc_title);
  405. $cd->set_content($course_desc_content);
  406. $cd->set_session_id(0);
  407. // Get course info
  408. $course_info = CourseManager::get_course_information(
  409. CourseManager::get_course_code_from_course_id($course_id)
  410. );
  411. // Check if this course description exists
  412. $descriptions = CourseDescription::get_descriptions($course_id);
  413. $exists = false;
  414. foreach ($descriptions as $description) {
  415. if ($description->get_description_type() == $course_desc_id) {
  416. $exists = true;
  417. }
  418. }
  419. $cd->set_course_id($course_info['real_id']);
  420. if (!$exists) {
  421. $cd->set_progress(0);
  422. $cd->insert();
  423. } else {
  424. $cd->update();
  425. }
  426. }
  427. }
  428. }
  429. public function unreadMessage($username, $password)
  430. {
  431. if ($this->verifyUserPass($username, $password) == "valid") {
  432. $table_message = Database::get_main_table(TABLE_MESSAGE);
  433. $user_id = UserManager::get_user_id_from_username($username);
  434. $condition_msg_status = ' msg_status = 1 '; // define('MESSAGE_STATUS_UNREAD', '1');
  435. $sql_query = "SELECT COUNT(*) as number_messages
  436. FROM $table_message
  437. WHERE $condition_msg_status AND user_receiver_id=".$user_id;
  438. $sql_result = Database::query($sql_query);
  439. $result = Database::fetch_array($sql_result);
  440. return $result['number_messages'];
  441. }
  442. return "0";
  443. }
  444. public function get_message_data($username, $password)
  445. {
  446. if ($this->verifyUserPass($username, $password) == "valid") {
  447. $user_id = get_user_id_from_username($username);
  448. }
  449. }
  450. public function nada($username, $password)
  451. {
  452. if ($this->verifyUserPass($username, $password) == "valid") {
  453. return $username.$password;
  454. }
  455. return $username;
  456. }
  457. /**
  458. * Deletes a course (helper method).
  459. *
  460. * @param string Course id field name
  461. * @param string Course id value
  462. *
  463. * @return mixed True if the course was successfully deleted, WSError otherwise
  464. */
  465. protected function deleteCourseHelper(
  466. $course_id_field_name,
  467. $course_id_value
  468. ) {
  469. $course_id = $this->getCourseId(
  470. $course_id_field_name,
  471. $course_id_value
  472. );
  473. if ($course_id instanceof WSCMError) {
  474. return $course_id;
  475. } else {
  476. $course_code = CourseManager::get_course_code_from_course_id(
  477. $course_id
  478. );
  479. CourseManager::delete_course($course_code);
  480. return true;
  481. }
  482. }
  483. /**
  484. * Creates a course (helper method).
  485. *
  486. * @param string Title
  487. * @param string Category code
  488. * @param string Wanted code. If it's not defined, it will be generated automatically
  489. * @param string Tutor name
  490. * @param string Course admin user id field name
  491. * @param string Course admin user id value
  492. * @param string Course language
  493. * @param string Course id field name
  494. * @param string Course id value
  495. * @param array Course extra fields
  496. *
  497. * @return mixed Generated id if creation was successful, WSError otherwise
  498. */
  499. protected function createCourseHelper(
  500. $title,
  501. $category_code,
  502. $wanted_code,
  503. $tutor_name,
  504. $course_admin_user_id_field_name,
  505. $course_admin_user_id_value,
  506. $language,
  507. $course_id_field_name,
  508. $course_id_value,
  509. $extras
  510. ) {
  511. // Add the original course id field name and value to the extra fields if needed
  512. $extras_associative = [];
  513. if ($course_id_field_name != "chamilo_course_id") {
  514. $extras_associative[$course_id_field_name] = $course_id_value;
  515. }
  516. foreach ($extras as $extra) {
  517. $extras_associative[$extra['field_name']] = $extra['field_value'];
  518. }
  519. $course_admin_id = $this->getUserId($course_admin_user_id_field_name, $course_admin_user_id_value);
  520. if ($course_admin_id instanceof WSError) {
  521. return $course_admin_id;
  522. }
  523. if ($wanted_code == '') {
  524. $wanted_code = CourseManager::generate_course_code($title);
  525. }
  526. $result = create_course(
  527. $wanted_code,
  528. $title,
  529. $tutor_name,
  530. $category_code,
  531. $language,
  532. $course_admin_id,
  533. $this->_configuration['db_prefix'],
  534. 0
  535. );
  536. if (!$result) {
  537. return new WSError(202, 'There was an error creating the course');
  538. } else {
  539. // Update extra fields
  540. foreach ($extras_associative as $fname => $fvalue) {
  541. CourseManager::update_course_extra_field_value($result, $fname, $fvalue);
  542. }
  543. // Get course id
  544. $course_info = CourseManager::get_course_information($result);
  545. return $course_info['real_id'];
  546. }
  547. }
  548. /**
  549. * Edits a course (helper method).
  550. *
  551. * @param string Course id field name
  552. * @param string Course id value
  553. * @param string Title
  554. * @param string Category code
  555. * @param string Department name
  556. * @param string Department url
  557. * @param string Course language
  558. * @param int Visibility
  559. * @param int Subscribe (0 = denied, 1 = allowed)
  560. * @param int Unsubscribe (0 = denied, 1 = allowed)
  561. * @param string Visual code
  562. * @param array Course extra fields
  563. *
  564. * @return mixed True in case of success, WSError otherwise
  565. */
  566. protected function editCourseHelper(
  567. $course_id_field_name,
  568. $course_id_value,
  569. $title,
  570. $category_code,
  571. $department_name,
  572. $department_url,
  573. $language,
  574. $visibility,
  575. $subscribe,
  576. $unsubscribe,
  577. $visual_code,
  578. $extras
  579. ) {
  580. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  581. if ($course_id instanceof WSCMError) {
  582. return $course_id;
  583. } else {
  584. $attributes = [];
  585. if (!empty($title)) {
  586. $attributes['title'] = $title;
  587. }
  588. if (!empty($category_code)) {
  589. $attributes['category_code'] = $category_code;
  590. }
  591. if (!empty($department_name)) {
  592. $attributes['department_name'] = $department_name;
  593. }
  594. if (!empty($department_url)) {
  595. $attributes['department_url'] = $department_url;
  596. }
  597. if (!empty($language)) {
  598. $attributes['course_language'] = $language;
  599. }
  600. if ($visibility != '') {
  601. $attributes['visibility'] = (int) $visibility;
  602. }
  603. if ($subscribe != '') {
  604. $attributes['subscribe'] = (int) $subscribe;
  605. }
  606. if ($unsubscribe != '') {
  607. $attributes['unsubscribe'] = (int) $unsubscribe;
  608. }
  609. if (!empty($visual_code)) {
  610. $attributes['visual_code'] = $visual_code;
  611. }
  612. if (!empty($attributes)) {
  613. CourseManager::update_attributes($course_id, $attributes);
  614. }
  615. if (!empty($extras)) {
  616. $course_code = CourseManager::get_course_code_from_course_id($course_id);
  617. $extras_associative = [];
  618. foreach ($extras as $extra) {
  619. $extras_associative[$extra['field_name']] = $extra['field_value'];
  620. }
  621. foreach ($extras_associative as $fname => $fvalue) {
  622. CourseManager::update_extra_field_value($course_code, $fname, $fvalue);
  623. }
  624. }
  625. return true;
  626. }
  627. }
  628. /**
  629. * Subscribe or unsubscribe user to a course (helper method).
  630. *
  631. * @param string Course id field name. Use "chamilo_course_id" to use internal id
  632. * @param string course id value
  633. * @param string User id field name. Use "chamilo_user_id" to use internal id
  634. * @param string User id value
  635. * @param int Set to 1 to subscribe, 0 to unsubscribe
  636. * @param int Status (STUDENT or TEACHER) Used for subscription only
  637. *
  638. * @return mixed True if subscription or unsubscription was successful, false otherwise
  639. */
  640. protected function changeUserSubscription(
  641. $course_id_field_name,
  642. $course_id_value,
  643. $user_id_field_name,
  644. $user_id_value,
  645. $state,
  646. $status = STUDENT
  647. ) {
  648. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  649. if ($course_id instanceof WSError) {
  650. return $course_id;
  651. } else {
  652. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  653. if ($user_id instanceof WSError) {
  654. return $user_id;
  655. } else {
  656. $course_code = CourseManager::get_course_code_from_course_id($course_id);
  657. if ($state == 0) {
  658. // Unsubscribe user
  659. CourseManager::unsubscribe_user($user_id, $course_code);
  660. return true;
  661. } else {
  662. // Subscribe user
  663. if (CourseManager::subscribeUser($user_id, $course_code, $status)) {
  664. return true;
  665. } else {
  666. return new WSError(203, 'An error occured subscribing to this course');
  667. }
  668. }
  669. }
  670. }
  671. }
  672. }