webservice_course.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once(dirname(__FILE__).'/../inc/global.inc.php');
  7. $libpath = api_get_path(LIBRARY_PATH);
  8. require_once(dirname(__FILE__).'/webservice.php');
  9. /**
  10. * Web services available for the Course module. This class extends the WS class
  11. */
  12. class WSCourse extends WS
  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($course_id_field_name, $course_id_value) {
  22. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  23. if($course_id instanceof WSError) {
  24. return $course_id;
  25. } else {
  26. $course_code = CourseManager::get_course_code_from_course_id($course_id);
  27. CourseManager::delete_course($course_code);
  28. return true;
  29. }
  30. }
  31. /**
  32. * Deletes a course
  33. *
  34. * @param string API secret key
  35. * @param string Course id field name
  36. * @param string Course id value
  37. */
  38. public function DeleteCourse($secret_key, $course_id_field_name, $course_id_value) {
  39. $verifKey = $this->verifyKey($secret_key);
  40. if($verifKey instanceof WSError) {
  41. $this->handleError($verifKey);
  42. } else {
  43. $result = $this->deleteCourseHelper($course_id_field_name, $course_id_value);
  44. if($result instanceof WSError) {
  45. $this->handleError($result);
  46. }
  47. }
  48. }
  49. /**
  50. * Deletes multiple courses
  51. *
  52. * @param string API secret key
  53. * @param array Array of courses with elements of the form array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value')
  54. * @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
  55. * than 0, an error occured
  56. */
  57. public function DeleteCourses($secret_key, $courses) {
  58. $verifKey = $this->verifyKey($secret_key);
  59. if($verifKey instanceof WSError) {
  60. $this->handleError($verifKey);
  61. } else {
  62. $results = array();
  63. foreach($courses as $course) {
  64. $result_tmp = array();
  65. $result_op = $this->deleteCourseHelper($course['course_id_field_name'], $course['course_id_value']);
  66. $result_tmp['course_id_value'] = $course['course_id_value'];
  67. if($result_op instanceof WSError) {
  68. // Return the error in the results
  69. $result_tmp['result'] = $result_op->toArray();
  70. } else {
  71. $result_tmp['result'] = $this->getSuccessfulResult();
  72. }
  73. $results[] = $result_tmp;
  74. }
  75. return $results;
  76. }
  77. }
  78. /**
  79. * Creates a course (helper method)
  80. *
  81. * @param string Title
  82. * @param string Category code
  83. * @param string Wanted code. If it's not defined, it will be generated automatically
  84. * @param string Tutor name
  85. * @param string Course admin user id field name
  86. * @param string Course admin user id value
  87. * @param string Course language
  88. * @param string Course id field name
  89. * @param string Course id value
  90. * @param array Course extra fields
  91. * @return mixed Generated id if creation was successful, WSError otherwise
  92. */
  93. protected function 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) {
  94. // Add the original course id field name and value to the extra fields if needed
  95. $extras_associative = array();
  96. if($course_id_field_name != "chamilo_course_id") {
  97. $extras_associative[$course_id_field_name] = $course_id_value;
  98. }
  99. foreach($extras as $extra) {
  100. $extras_associative[$extra['field_name']] = $extra['field_value'];
  101. }
  102. $course_admin_id = $this->getUserId($course_admin_user_id_field_name, $course_admin_user_id_value);
  103. if($course_admin_id instanceof WSError) {
  104. return $course_admin_id;
  105. }
  106. if($wanted_code == '') {
  107. $wanted_code = CourseManager::generate_course_code($title);
  108. }
  109. $result = create_course($wanted_code, $title, $tutor_name, $category_code, $language, $course_admin_id, $this->_configuration['db_prefix'], 0);
  110. if (!$result) {
  111. return new WSError(202, 'There was an error creating the course');
  112. } else {
  113. // Update extra fields
  114. foreach($extras_associative as $fname => $fvalue) {
  115. CourseManager::update_course_extra_field_value($result, $fname, $fvalue);
  116. }
  117. // Get course id
  118. $course_info = CourseManager::get_course_information($result);
  119. return $course_info['real_id'];
  120. }
  121. }
  122. /**
  123. * Creates a course
  124. *
  125. * @param string API secret key
  126. * @param string Title
  127. * @param string Category code
  128. * @param string Wanted code. If it's not defined, it will be generated automatically
  129. * @param string Tutor name
  130. * @param string Course admin user id field name
  131. * @param string Course admin user id value
  132. * @param string Course language
  133. * @param string Course id field name
  134. * @param string Course id value
  135. * @param array Course extra fields
  136. * @return int Course id generated
  137. */
  138. public function CreateCourse($secret_key, $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) {
  139. // First, verify the secret key
  140. $verifKey = $this->verifyKey($secret_key);
  141. if($verifKey instanceof WSError) {
  142. $this->handleError($verifKey);
  143. } else {
  144. $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);
  145. if($result instanceof WSError) {
  146. $this->handleError($result);
  147. } else {
  148. return $result;
  149. }
  150. }
  151. }
  152. /**
  153. * Create multiple courses
  154. *
  155. * @param string API secret key
  156. * @param array Courses to be created, with elements following the structure presented in CreateCourse
  157. * @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'))
  158. */
  159. public function CreateCourses($secret_key, $courses) {
  160. // First, verify the secret key
  161. $verifKey = $this->verifyKey($secret_key);
  162. if($verifKey instanceof WSError) {
  163. $this->handleError($verifKey);
  164. } else {
  165. $results = array();
  166. foreach($courses as $course) {
  167. $result_tmp = array();
  168. // re-initialize variables just in case
  169. $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 = 0;
  170. extract($course);
  171. $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);
  172. if($result instanceof WSError) {
  173. $result_tmp['result'] = $result->toArray();
  174. $result_tmp['course_id_value'] = $course_id_value;
  175. $result_tmp['course_id_generated'] = 0;
  176. } else {
  177. $result_tmp['result'] = $this->getSuccessfulResult();
  178. $result_tmp['course_id_value'] = $course_id_value;
  179. $result_tmp['course_id_generated'] = $result;
  180. }
  181. $results[] = $result_tmp;
  182. }
  183. return $results;
  184. }
  185. }
  186. /**
  187. * Edits a course (helper method)
  188. *
  189. * @param string Course id field name
  190. * @param string Course id value
  191. * @param string Title
  192. * @param string Category code
  193. * @param string Department name
  194. * @param string Department url
  195. * @param string Course language
  196. * @param int Visibility
  197. * @param int Subscribe (0 = denied, 1 = allowed)
  198. * @param int Unsubscribe (0 = denied, 1 = allowed)
  199. * @param string Visual code
  200. * @param array Course extra fields
  201. * @return mixed True in case of success, WSError otherwise
  202. */
  203. protected function editCourseHelper($course_id_field_name, $course_id_value, $title, $category_code, $department_name, $department_url, $language, $visibility, $subscribe, $unsubscribe, $visual_code, $extras) {
  204. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  205. if($course_id instanceof WSError) {
  206. return $course_id;
  207. } else {
  208. $attributes = array();
  209. if(!is_empty($title)) {
  210. $attributes['title'] = $title;
  211. }
  212. if(!is_empty($category_code)) {
  213. $attributes['category_code'] = $category_code;
  214. }
  215. if(!is_empty($department_name)) {
  216. $attributes['department_name'] = $department_name;
  217. }
  218. if(!is_empty($department_url)) {
  219. $attributes['department_url'] = $department_url;
  220. }
  221. if(!is_empty($language)) {
  222. $attributes['course_language'] = $language;
  223. }
  224. if($visibility != '') {
  225. $attributes['visibility'] = (int)$visibility;
  226. }
  227. if($subscribe != '') {
  228. $attributes['subscribe'] = (int)$subscribe;
  229. }
  230. if($unsubscribe != '') {
  231. $attributes['unsubscribe'] = (int)$unsubscribe;
  232. }
  233. if(!is_empty($visual_code)) {
  234. $attributes['visual_code'] = $visual_code;
  235. }
  236. if(!is_empty($attributes)) {
  237. CourseManager::update_attributes($course_id, $attributes);
  238. }
  239. if(!empty($extras)) {
  240. $course_code = CourseManager::get_course_code_from_course_id($course_id);
  241. $extras_associative = array();
  242. foreach($extras as $extra) {
  243. $extras_associative[$extra['field_name']] = $extra['field_value'];
  244. }
  245. foreach($extras_associative as $fname => $fvalue) {
  246. CourseManager::update_extra_field_value($course_code, $fname, $fvalue);
  247. }
  248. }
  249. return true;
  250. }
  251. }
  252. /**
  253. * Edits a course
  254. *
  255. * @param string API secret key
  256. * @param string Course id field name
  257. * @param string Course id value
  258. * @param string Title
  259. * @param string Category code
  260. * @param string Department name
  261. * @param string Department url
  262. * @param string Course language
  263. * @param int Visibility
  264. * @param int Subscribe (0 = denied, 1 = allowed)
  265. * @param int Unsubscribe (0 = denied, 1 = allowed)
  266. * @param string Visual code
  267. * @param array Course extra fields
  268. */
  269. public function EditCourse($secret_key, $course_id_field_name, $course_id_value, $title, $category_code, $department_name, $department_url, $language, $visibility, $subscribe, $unsubscribe, $visual_code, $extras) {
  270. $verifKey = $this->verifyKey($secret_key);
  271. if($verifKey instanceof WSError) {
  272. $this->handleError($verifKey);
  273. } else {
  274. $result = $this->editCourseHelper($course_id_field_name, $course_id_value, $title, $category_code, $department_name, $department_url, $language, $visibility, $subscribe, $unsubscribe, $visual_code, $extras);
  275. if($result instanceof WSError) {
  276. $this->handleError($result);
  277. }
  278. }
  279. }
  280. /**
  281. * List courses
  282. *
  283. * @param string API secret key
  284. * @param string A list of visibility filter we want to apply
  285. * @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',
  286. * 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id')
  287. */
  288. public function ListCourses($secret_key, $visibility = 'public,public-registered,private,closed') {
  289. $verifKey = $this->verifyKey($secret_key);
  290. if($verifKey instanceof WSError) {
  291. $this->handleError($verifKey);
  292. } else {
  293. $visibilities = split(',',$visibility);
  294. $vis = array('public' => '3', 'public-registered' => '2', 'private' => '1', 'closed' => '0');
  295. foreach ($visibilities as $p => $visibility) {
  296. $visibilities[$p] = $vis[$visibility];
  297. }
  298. $courses_result = array();
  299. $category_names = array();
  300. $courses = CourseManager::get_courses_list();
  301. foreach($courses as $course) {
  302. //skip elements that do not match required visibility
  303. if (!in_array($course['visibility'],$visibilities)) { continue; }
  304. $course_tmp = array();
  305. $course_tmp['id'] = $course['id'];
  306. $course_tmp['code'] = $course['code'];
  307. $course_tmp['title'] = $course['title'];
  308. $course_tmp['language'] = $course['course_language'];
  309. $course_tmp['visibility'] = $course['visibility'];
  310. // Determining category name
  311. if($category_names[$course['category_code']]) {
  312. $course_tmp['category_name'] = $category_names[$course['category_code']];
  313. } else {
  314. $category = CourseManager::get_course_category($course['category_code']);
  315. $category_names[$course['category_code']] = $category['name'];
  316. $course_tmp['category_name'] = $category['name'];
  317. }
  318. // Determining number of students registered in course
  319. $user_list = CourseManager::get_user_list_from_course_code($course['code'], 0);
  320. $course_tmp['number_students'] = count($user_list);
  321. // Determining external course id - this code misses the external course id field name
  322. // $course_tmp['external_course_id'] = CourseManager::get_course_extra_field_value($course_field_name, $course['code']);
  323. $courses_result[] = $course_tmp;
  324. }
  325. return $courses_result;
  326. }
  327. }
  328. /**
  329. * Subscribe or unsubscribe user to a course (helper method)
  330. *
  331. * @param string Course id field name. Use "chamilo_course_id" to use internal id
  332. * @param string Course id value.
  333. * @param string User id field name. Use "chamilo_user_id" to use internal id
  334. * @param string User id value
  335. * @param int Set to 1 to subscribe, 0 to unsubscribe
  336. * @param int Status (STUDENT or TEACHER) Used for subscription only
  337. * @return mixed True if subscription or unsubscription was successful, false otherwise
  338. */
  339. protected function changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, $state, $status = STUDENT) {
  340. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  341. if($course_id instanceof WSError) {
  342. return $course_id;
  343. } else {
  344. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  345. if($user_id instanceof WSError) {
  346. return $user_id;
  347. } else {
  348. $course_code = CourseManager::get_course_code_from_course_id($course_id);
  349. if($state == 0) {
  350. // Unsubscribe user
  351. CourseManager::unsubscribe_user($user_id, $course_code);
  352. return true;
  353. } else {
  354. // Subscribe user
  355. if(CourseManager::subscribe_user($user_id, $course_code, $status)) {
  356. return true;
  357. } else {
  358. return new WSError(203, 'An error occured subscribing to this course');
  359. }
  360. }
  361. }
  362. }
  363. }
  364. /**
  365. * Subscribe user to a course
  366. *
  367. * @param string API secret key
  368. * @param string Course id field name. Use "chamilo_course_id" to use internal id
  369. * @param string Course id value.
  370. * @param string User id field name. Use "chamilo_user_id" to use internal id
  371. * @param string User id value
  372. * @param int Status (1 = Teacher, 5 = Student)
  373. */
  374. public function SubscribeUserToCourse($secret_key, $course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, $status) {
  375. $verifKey = $this->verifyKey($secret_key);
  376. if($verifKey instanceof WSError) {
  377. $this->handleError($verifKey);
  378. } else {
  379. $result = $this->changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, 1, $status);
  380. if($result instanceof WSError) {
  381. $this->handleError($result);
  382. }
  383. }
  384. }
  385. /**
  386. * Unsusbscribe user from course
  387. *
  388. * @param string API secret key
  389. * @param string Course id field name. Use "chamilo_course_id" to use internal id
  390. * @param string Course id value.
  391. * @param string User id field name. Use "chamilo_user_id" to use internal id
  392. * @param string User id value
  393. */
  394. public function UnsubscribeUserFromCourse($secret_key, $course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value) {
  395. $verifKey = $this->verifyKey($secret_key);
  396. if($verifKey instanceof WSError) {
  397. $this->handleError($verifKey);
  398. } else {
  399. $result = $this->changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, 0);
  400. if($result instanceof WSError) {
  401. $this->handleError($result);
  402. }
  403. }
  404. }
  405. /**
  406. * Returns the descriptions of a course, along with their id
  407. *
  408. * @param string API secret key
  409. * @param string Course id field name
  410. * @param string Course id value
  411. * @return array Returns an array with elements of the form ('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content')
  412. */
  413. public function GetCourseDescriptions($secret_key, $course_id_field_name, $course_id_value) {
  414. $verifKey = $this->verifyKey($secret_key);
  415. if($verifKey instanceof WSError) {
  416. $this->handleError($verifKey);
  417. } else {
  418. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  419. if($course_id instanceof WSError) {
  420. return $course_id;
  421. } else {
  422. // Course exists, get its descriptions
  423. $descriptions = CourseDescription::get_descriptions($course_id);
  424. $results = array();
  425. foreach($descriptions as $description) {
  426. $results[] = array('course_desc_id' => $description->get_description_type(),
  427. 'course_desc_title' => $description->get_title(),
  428. 'course_desc_content' => $description->get_content());
  429. }
  430. return $results;
  431. }
  432. }
  433. }
  434. /**
  435. * Edit course description
  436. *
  437. * @param string API secret key
  438. * @param string Course id field name
  439. * @param string Course id value
  440. * @param int Category id from course description
  441. * @param string Description title
  442. * @param string Course description content
  443. */
  444. public function EditCourseDescription($secret_key, $course_id_field_name, $course_id_value, $course_desc_id, $course_desc_title, $course_desc_content) {
  445. $verifKey = $this->verifyKey($secret_key);
  446. if($verifKey instanceof WSError) {
  447. $this->handleError($verifKey);
  448. } else {
  449. $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
  450. if($course_id instanceof WSError) {
  451. return $course_id;
  452. } else {
  453. // Create the new course description
  454. $cd = new CourseDescription();
  455. $cd->set_description_type($course_desc_id);
  456. $cd->set_title($course_desc_title);
  457. $cd->set_content($course_desc_content);
  458. $cd->set_session_id(0);
  459. // Get course info
  460. $course_info = CourseManager::get_course_information(CourseManager::get_course_code_from_course_id($course_id));
  461. $cd->set_course_id($course_info['real_id']);
  462. // Check if this course description exists
  463. $descriptions = CourseDescription::get_descriptions($course_id);
  464. $exists = false;
  465. foreach($descriptions as $description) {
  466. if($description->get_description_type() == $course_desc_id) {
  467. $exists = true;
  468. }
  469. }
  470. if (!$exists) {
  471. $cd->set_progress(0);
  472. $cd->insert();
  473. } else {
  474. $cd->update();
  475. }
  476. }
  477. }
  478. }
  479. }