cm_webservice_course.php 19 KB

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