cm_webservice_course.php 19 KB

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