cm_webservice_course.php 24 KB

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