cm_webservice_course.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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($wanted_code, $title, $tutor_name, $category_code, $language, $course_admin_id, $this->_configuration['db_prefix'], 0);
  137. if (!$result) {
  138. return new WSError(202, 'There was an error creating the course');
  139. } else {
  140. // Update extra fields
  141. foreach ($extras_associative as $fname => $fvalue) {
  142. CourseManager::update_course_extra_field_value($result, $fname, $fvalue);
  143. }
  144. // Get course id
  145. $course_info = CourseManager::get_course_information($result);
  146. return $course_info['real_id'];
  147. }
  148. }
  149. /**
  150. * Creates a course
  151. *
  152. * @param string API secret key
  153. * @param string Title
  154. * @param string Category code
  155. * @param string Wanted code. If it's not defined, it will be generated automatically
  156. * @param string Tutor name
  157. * @param string Course admin user id field name
  158. * @param string Course admin user id value
  159. * @param string Course language
  160. * @param string Course id field name
  161. * @param string Course id value
  162. * @param array Course extra fields
  163. * @return int Course id generated
  164. */
  165. public function CreateCourse(
  166. $secret_key,
  167. $title,
  168. $category_code,
  169. $wanted_code,
  170. $tutor_name,
  171. $course_admin_user_id_field_name,
  172. $course_admin_user_id_value,
  173. $language,
  174. $course_id_field_name,
  175. $course_id_value,
  176. $extras
  177. ) {
  178. // First, verify the secret key
  179. $verifKey = $this->verifyKey($secret_key);
  180. if ($verifKey instanceof WSError) {
  181. $this->handleError($verifKey);
  182. } else {
  183. $result = $this->createCourseHelper($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);
  184. if ($result instanceof WSError) {
  185. $this->handleError($result);
  186. } else {
  187. return $result;
  188. }
  189. }
  190. }
  191. /**
  192. * Create multiple courses
  193. *
  194. * @param string API secret key
  195. * @param array Courses to be created, with elements following the structure presented in CreateCourse
  196. * @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'))
  197. */
  198. public function CreateCourses($secret_key, $courses)
  199. {
  200. // First, verify the secret key
  201. $verifKey = $this->verifyKey($secret_key);
  202. if ($verifKey instanceof WSCMError) {
  203. $this->handleError($verifKey);
  204. } else {
  205. $results = array();
  206. foreach ($courses as $course) {
  207. $result_tmp = array();
  208. //reinitialize variables just in case
  209. $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;
  210. extract($course);
  211. $result = $this->createCourseHelper($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);
  212. if ($result instanceof WSCMError) {
  213. $result_tmp['result'] = $result->toArray();
  214. $result_tmp['course_id_value'] = $course_id_value;
  215. $result_tmp['course_id_generated'] = 0;
  216. } else {
  217. $result_tmp['result'] = $this->getSuccessfulResult();
  218. $result_tmp['course_id_value'] = $course_id_value;
  219. $result_tmp['course_id_generated'] = $result;
  220. }
  221. $results[] = $result_tmp;
  222. }
  223. return $results;
  224. }
  225. }
  226. /**
  227. * Edits a course (helper method)
  228. *
  229. * @param string Course id field name
  230. * @param string Course id value
  231. * @param string Title
  232. * @param string Category code
  233. * @param string Department name
  234. * @param string Department url
  235. * @param string Course language
  236. * @param int Visibility
  237. * @param int Subscribe (0 = denied, 1 = allowed)
  238. * @param int Unsubscribe (0 = denied, 1 = allowed)
  239. * @param string Visual code
  240. * @param array Course extra fields
  241. * @return mixed True in case of success, WSError otherwise
  242. */
  243. protected function editCourseHelper(
  244. $course_id_field_name,
  245. $course_id_value,
  246. $title,
  247. $category_code,
  248. $department_name,
  249. $department_url,
  250. $language,
  251. $visibility,
  252. $subscribe,
  253. $unsubscribe,
  254. $visual_code,
  255. $extras
  256. ) {
  257. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  258. if ($course_id instanceof WSCMError) {
  259. return $course_id;
  260. } else {
  261. $attributes = array();
  262. if (!empty($title)) {
  263. $attributes['title'] = $title;
  264. }
  265. if (!empty($category_code)) {
  266. $attributes['category_code'] = $category_code;
  267. }
  268. if (!empty($department_name)) {
  269. $attributes['department_name'] = $department_name;
  270. }
  271. if (!empty($department_url)) {
  272. $attributes['department_url'] = $department_url;
  273. }
  274. if (!empty($language)) {
  275. $attributes['course_language'] = $language;
  276. }
  277. if ($visibility != '') {
  278. $attributes['visibility'] = (int) $visibility;
  279. }
  280. if ($subscribe != '') {
  281. $attributes['subscribe'] = (int) $subscribe;
  282. }
  283. if ($unsubscribe != '') {
  284. $attributes['unsubscribe'] = (int) $unsubscribe;
  285. }
  286. if (!empty($visual_code)) {
  287. $attributes['visual_code'] = $visual_code;
  288. }
  289. if (!empty($attributes)) {
  290. CourseManager::update_attributes($course_id, $attributes);
  291. }
  292. if (!empty($extras)) {
  293. $course_code = CourseManager::get_course_code_from_course_id($course_id);
  294. $extras_associative = array();
  295. foreach ($extras as $extra) {
  296. $extras_associative[$extra['field_name']] = $extra['field_value'];
  297. }
  298. foreach ($extras_associative as $fname => $fvalue) {
  299. CourseManager::update_extra_field_value($course_code, $fname, $fvalue);
  300. }
  301. }
  302. return true;
  303. }
  304. }
  305. /**
  306. * Edits a course
  307. *
  308. * @param string API secret key
  309. * @param string Course id field name
  310. * @param string Course id value
  311. * @param string Title
  312. * @param string Category code
  313. * @param string Department name
  314. * @param string Department url
  315. * @param string Course language
  316. * @param int Visibility
  317. * @param int Subscribe (0 = denied, 1 = allowed)
  318. * @param int Unsubscribe (0 = denied, 1 = allowed)
  319. * @param string Visual code
  320. * @param array Course extra fields
  321. */
  322. public function EditCourse(
  323. $secret_key,
  324. $course_id_field_name,
  325. $course_id_value,
  326. $title,
  327. $category_code,
  328. $department_name,
  329. $department_url,
  330. $language,
  331. $visibility,
  332. $subscribe,
  333. $unsubscribe,
  334. $visual_code,
  335. $extras
  336. ) {
  337. $verifKey = $this->verifyKey($secret_key);
  338. if ($verifKey instanceof WSCMError) {
  339. $this->handleError($verifKey);
  340. } else {
  341. $result = $this->editCourseHelper($course_id_field_name, $course_id_value, $title, $category_code, $department_name, $department_url, $language, $visibility, $subscribe, $unsubscribe, $visual_code, $extras);
  342. if ($result instanceof WSCMError) {
  343. $this->handleError($result);
  344. }
  345. }
  346. }
  347. /**
  348. * List courses
  349. *
  350. * @param string API secret key
  351. * @param string Course id field name. Use "chamilo_course_id" to use internal id
  352. * @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',
  353. * 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id')
  354. */
  355. public function ListCourses($secret_key, $course_id_field_name)
  356. {
  357. $verifKey = $this->verifyKey($secret_key);
  358. if ($verifKey instanceof WSError) {
  359. $this->handleError($verifKey);
  360. } else {
  361. $courses_result = array();
  362. $category_names = array();
  363. $courses = CourseManager::get_courses_list();
  364. foreach ($courses as $course) {
  365. $course_tmp = array();
  366. $course_tmp['id'] = $course['id'];
  367. $course_tmp['code'] = $course['code'];
  368. $course_tmp['title'] = $course['title'];
  369. $course_tmp['language'] = $course['course_language'];
  370. $course_tmp['visibility'] = $course['visibility'];
  371. // Determining category name
  372. if ($category_names[$course['category_code']]) {
  373. $course_tmp['category_name'] = $category_names[$course['category_code']];
  374. } else {
  375. $category = CourseManager::get_course_category($course['category_code']);
  376. $category_names[$course['category_code']] = $category['name'];
  377. $course_tmp['category_name'] = $category['name'];
  378. }
  379. // Determining number of students registered in course
  380. $user_list = CourseManager::get_user_list_from_course_code($course['code']);
  381. $course_tmp['number_students'] = count($user_list);
  382. // Determining external course id
  383. $course_tmp['external_course_id'] = CourseManager::get_course_extra_field_value($course_id_field_name, $course['code']);
  384. $courses_result[] = $course_tmp;
  385. }
  386. return $courses_result;
  387. }
  388. }
  389. /**
  390. * Subscribe or unsubscribe user to a course (helper method)
  391. *
  392. * @param string Course id field name. Use "chamilo_course_id" to use internal id
  393. * @param string Course id value.
  394. * @param string User id field name. Use "chamilo_user_id" to use internal id
  395. * @param string User id value
  396. * @param int Set to 1 to subscribe, 0 to unsubscribe
  397. * @param int Status (STUDENT or TEACHER) Used for subscription only
  398. * @return mixed True if subscription or unsubscription was successful, false otherwise
  399. */
  400. protected function changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, $state, $status = STUDENT) {
  401. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  402. if ($course_id instanceof WSError) {
  403. return $course_id;
  404. } else {
  405. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  406. if ($user_id instanceof WSError) {
  407. return $user_id;
  408. } else {
  409. $course_code = CourseManager::get_course_code_from_course_id($course_id);
  410. if ($state == 0) {
  411. // Unsubscribe user
  412. CourseManager::unsubscribe_user($user_id, $course_code);
  413. return true;
  414. } else {
  415. // Subscribe user
  416. if (CourseManager::subscribe_user($user_id, $course_code, $status)) {
  417. return true;
  418. } else {
  419. return new WSError(203, 'An error occured subscribing to this course');
  420. }
  421. }
  422. }
  423. }
  424. }
  425. /**
  426. * Subscribe user to a course
  427. *
  428. * @param string API secret key
  429. * @param string Course id field name. Use "chamilo_course_id" to use internal id
  430. * @param string Course id value.
  431. * @param string User id field name. Use "chamilo_user_id" to use internal id
  432. * @param string User id value
  433. * @param int Status (1 = Teacher, 5 = Student)
  434. */
  435. public function SubscribeUserToCourse($secret_key, $course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, $status) {
  436. $verifKey = $this->verifyKey($secret_key);
  437. if ($verifKey instanceof WSError) {
  438. $this->handleError($verifKey);
  439. } else {
  440. $result = $this->changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, 1, $status);
  441. if ($result instanceof WSError) {
  442. $this->handleError($result);
  443. }
  444. }
  445. }
  446. /**
  447. * Unsusbscribe user from course
  448. *
  449. * @param string API secret key
  450. * @param string Course id field name. Use "chamilo_course_id" to use internal id
  451. * @param string Course id value.
  452. * @param string User id field name. Use "chamilo_user_id" to use internal id
  453. * @param string User id value
  454. */
  455. public function UnsubscribeUserFromCourse($secret_key, $course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value) {
  456. $verifKey = $this->verifyKey($secret_key);
  457. if ($verifKey instanceof WSError) {
  458. $this->handleError($verifKey);
  459. } else {
  460. $result = $this->changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, 0);
  461. if ($result instanceof WSError) {
  462. $this->handleError($result);
  463. }
  464. }
  465. }
  466. /**
  467. * Returns the descriptions of a course, along with their id
  468. *
  469. * @param string API secret key
  470. * @param string Course id field name
  471. * @param string Course id value
  472. * @return array Returns an array with elements of the form ('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content')
  473. */
  474. public function GetCourseDescriptions($secret_key, $course_id_field_name, $course_id_value) {
  475. $verifKey = $this->verifyKey($secret_key);
  476. if ($verifKey instanceof WSError) {
  477. $this->handleError($verifKey);
  478. } else {
  479. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  480. if ($course_id instanceof WSError) {
  481. return $course_id;
  482. } else {
  483. // Course exists, get its descriptions
  484. $descriptions = CourseDescription::get_descriptions($course_id);
  485. $results = array();
  486. foreach ($descriptions as $description) {
  487. $results[] = array('course_desc_id' => $description->get_description_type(),
  488. 'course_desc_title' => $description->get_title(),
  489. 'course_desc_content' => $description->get_content());
  490. }
  491. return $results;
  492. }
  493. }
  494. }
  495. /**
  496. * Edit course description
  497. *
  498. * @param string API secret key
  499. * @param string Course id field name
  500. * @param string Course id value
  501. * @param int Category id from course description
  502. * @param string Description title
  503. * @param string Course description content
  504. */
  505. public function EditCourseDescription($secret_key, $course_id_field_name, $course_id_value, $course_desc_id, $course_desc_title, $course_desc_content) {
  506. $verifKey = $this->verifyKey($secret_key);
  507. if ($verifKey instanceof WSError) {
  508. $this->handleError($verifKey);
  509. } else {
  510. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  511. if ($course_id instanceof WSError) {
  512. return $course_id;
  513. } else {
  514. // Create the new course description
  515. $cd = new CourseDescription();
  516. $cd->set_description_type($course_desc_id);
  517. $cd->set_title($course_desc_title);
  518. $cd->set_content($course_desc_content);
  519. $cd->set_session_id(0);
  520. // Get course info
  521. $course_info = CourseManager::get_course_information(
  522. CourseManager::get_course_code_from_course_id($course_id)
  523. );
  524. // Check if this course description exists
  525. $descriptions = CourseDescription::get_descriptions($course_id);
  526. $exists = false;
  527. foreach ($descriptions as $description) {
  528. if ($description->get_description_type() == $course_desc_id) {
  529. $exists = true;
  530. }
  531. }
  532. $cd->set_course_id($course_info['real_id']);
  533. if (!$exists) {
  534. $cd->set_progress(0);
  535. $cd->insert();
  536. } else {
  537. $cd->update();
  538. }
  539. }
  540. }
  541. }
  542. public function unreadMessage($username, $password)
  543. {
  544. if ($this->verifyUserPass($username, $password) == "valid")
  545. {
  546. $table_message = Database::get_main_table(TABLE_MESSAGE);
  547. $user_id = UserManager::get_user_id_from_username($username);
  548. $condition_msg_status = ' msg_status = 1 '; // define('MESSAGE_STATUS_UNREAD', '1');
  549. $sql_query = "SELECT COUNT(*) as number_messages FROM $table_message WHERE $condition_msg_status AND user_receiver_id=".$user_id;
  550. $sql_result = Database::query($sql_query);
  551. $result = Database::fetch_array($sql_result);
  552. return $result['number_messages'];
  553. }
  554. return "0";
  555. }
  556. public function get_message_data($username, $password)
  557. {
  558. if ($this->verifyUserPass($username, $password) == "valid")
  559. {
  560. $user_id = get_user_id_from_username($username);
  561. }
  562. }
  563. public function nada($username, $password)
  564. {
  565. if ($this->verifyUserPass($username, $password) == "valid")
  566. return $username.$password;
  567. return $username;
  568. }
  569. }