diff --git a/main/webservices/cm_webservice_announcements.php b/main/webservices/cm_webservice_announcements.php index f5f9059462..e8a542b6e7 100755 --- a/main/webservices/cm_webservice_announcements.php +++ b/main/webservices/cm_webservice_announcements.php @@ -18,67 +18,82 @@ class WSCMAnnouncements extends WSCM public function get_announcements_id($username, $password, $course_code) { - if ($this->verifyUserPass($username, $password) == "valid") - { - + if ($this->verifyUserPass($username, $password) == "valid") { $result = self::get_announcements($username, $course_code); $announcements = "#"; while ($announcement = Database::fetch_array($result)) { $announcements .= $announcement['id']."#"; } + return $announcements; - } else + } else { return get_lang('InvalidId'); - + } } - public function get_announcement_data($username, $password, $course_code, $announcement_id, $field) - { - if ($this->verifyUserPass($username, $password) == "valid") - { + public function get_announcement_data( + $username, + $password, + $course_code, + $announcement_id, + $field + ) { + if ($this->verifyUserPass($username, $password) == "valid") { $htmlcode = false; $user_id = UserManager::get_user_id_from_username($username); - $result = self::get_announcements($username, $course_code, $announcement_id); - while ($announcement = Database::fetch_array($result)) - { + $result = self::get_announcements( + $username, + $course_code, + $announcement_id + ); + while ($announcement = Database::fetch_array($result)) { $announcements[] = $announcement; } - switch ($field) - { + switch ($field) { case 'sender': $field_table = "insert_user_id"; - $sender = api_get_user_info($announcements[0][$field_table]); + $sender = api_get_user_info( + $announcements[0][$field_table] + ); $announcements[0][$field_table] = $sender['firstname']." ".$sender['lastname']; break; - case 'title' : + case 'title': $htmlcode = true; $field_table = "title"; break; - case 'date' : + case 'date': $field_table = "end_date"; break; - case 'content' : + case 'content': $htmlcode = true; $field_table = "content"; - $announcements[0][$field_table] = nl2br_revert($announcements[0][$field_table]); + $announcements[0][$field_table] = nl2br_revert( + $announcements[0][$field_table] + ); break; default: $field_table = "title"; } - return (htmlcode) ? html_entity_decode($announcements[0][$field_table]) : $announcements[0][$field_table]; + return (htmlcode) ? html_entity_decode( + $announcements[0][$field_table] + ) : $announcements[0][$field_table]; - } else + } else { return get_lang('InvalidId'); + } } - private function get_announcements($username, $course_code, $announcement_id = 0) - { + private function get_announcements( + $username, + $course_code, + $announcement_id = 0 + ) { $session_id = api_get_session_id(); $condition_session = api_get_session_condition($session_id); @@ -89,7 +104,10 @@ class WSCMAnnouncements extends WSCM $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); $maximum = '12'; - $group_memberships = GroupManager::get_group_ids($course_info['real_id'], $user_id); + $group_memberships = GroupManager::get_group_ids( + $course_info['real_id'], + $user_id + ); if (api_get_group_id() == 0) { $cond_user_id = " AND ( @@ -125,7 +143,6 @@ class WSCMAnnouncements extends WSCM // the user is not member of any group // this is an identified user => show the general announcements AND his personal announcements if ($user_id) { - if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) { $cond_user_id = " AND ( ip.lastedit_user_id = '".api_get_user_id()."' OR @@ -149,7 +166,6 @@ class WSCMAnnouncements extends WSCM ORDER BY display_order DESC LIMIT 0,$maximum"; } else { - if (api_get_course_setting('allow_user_edit_announcement')) { $cond_user_id = " AND ( ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL @@ -178,9 +194,6 @@ class WSCMAnnouncements extends WSCM return $result; } - - - } /* diff --git a/main/webservices/soap_report.php b/main/webservices/soap_report.php index 6a06605ba5..85e5a911ec 100755 --- a/main/webservices/soap_report.php +++ b/main/webservices/soap_report.php @@ -9,15 +9,21 @@ require_once __DIR__.'/soap.php'; $s = WSSoapServer::singleton(); $s->wsdl->addComplexType( - 'user_id', - 'complexType', - 'struct', - 'all', - '', - array( - 'user_id_field_name' => array('name' => 'user_id_field_name', 'type' => 'xsd:string'), - 'user_id_value' => array('name' => 'user_id_value', 'type' => 'xsd:string') - ) + 'user_id', + 'complexType', + 'struct', + 'all', + '', + array( + 'user_id_field_name' => array( + 'name' => 'user_id_field_name', + 'type' => 'xsd:string', + ), + 'user_id_value' => array( + 'name' => 'user_id_value', + 'type' => 'xsd:string', + ), + ) ); $s->wsdl->addComplexType( @@ -27,8 +33,14 @@ $s->wsdl->addComplexType( 'all', '', array( - 'course_id_field_name' => array('name' => 'course_id_field_name', 'type' => 'xsd:string'), - 'course_id_value' => array('name' => 'course_id_value', 'type' => 'xsd:string') + 'course_id_field_name' => array( + 'name' => 'course_id_field_name', + 'type' => 'xsd:string', + ), + 'course_id_value' => array( + 'name' => 'course_id_value', + 'type' => 'xsd:string', + ), ) ); @@ -39,8 +51,14 @@ $s->wsdl->addComplexType( 'all', '', array( - 'session_id_field_name' => array('name' => 'session_id_field_name', 'type' => 'xsd:string'), - 'session_id_value' => array('name' => 'session_id_value', 'type' => 'xsd:string') + 'session_id_field_name' => array( + 'name' => 'session_id_field_name', + 'type' => 'xsd:string', + ), + 'session_id_value' => array( + 'name' => 'session_id_value', + 'type' => 'xsd:string', + ), ) ); @@ -59,112 +77,186 @@ $s->wsdl->addComplexType( $s->wsdl->addComplexType( - 'user_result', - 'complexType', - 'struct', - 'all', - '', - array( - 'id' => array('name' => 'id', 'type' => 'xsd:string'), - 'title' => array('name' => 'title', 'type' => 'xsd:string') - ) + 'user_result', + 'complexType', + 'struct', + 'all', + '', + array( + 'id' => array('name' => 'id', 'type' => 'xsd:string'), + 'title' => array('name' => 'title', 'type' => 'xsd:string'), + ) ); $s->wsdl->addComplexType( - 'progress_result', - 'complexType', - 'struct', - 'all', - '', - array( - 'progress_bar_mode' => array('name' => 'progress_bar_mode', 'type' => 'xsd:string'), - 'progress_db' => array('name' => 'progress_db', 'type' => 'xsd:string') - ) + 'progress_result', + 'complexType', + 'struct', + 'all', + '', + array( + 'progress_bar_mode' => array( + 'name' => 'progress_bar_mode', + 'type' => 'xsd:string', + ), + 'progress_db' => array('name' => 'progress_db', 'type' => 'xsd:string'), + ) ); $s->wsdl->addComplexType( - 'score_result', - 'complexType', - 'struct', - 'all', - '', - array( - 'min_score' => array('name' => 'min_score', 'type' => 'xsd:string'), - 'max_score' => array('name' => 'max_score', 'type' => 'xsd:string'), - 'mastery_score' => array('name' => 'mastery_score', 'type' => 'xsd:string'), - 'current_score' => array('name' => 'current_score', 'type' => 'xsd:string'), - ) + 'score_result', + 'complexType', + 'struct', + 'all', + '', + array( + 'min_score' => array('name' => 'min_score', 'type' => 'xsd:string'), + 'max_score' => array('name' => 'max_score', 'type' => 'xsd:string'), + 'mastery_score' => array( + 'name' => 'mastery_score', + 'type' => 'xsd:string', + ), + 'current_score' => array( + 'name' => 'current_score', + 'type' => 'xsd:string', + ), + ) ); $s->wsdl->addComplexType( - 'user_result_array', - 'complexType', - 'array', - '', - 'SOAP-ENC:Array', - array(), - array(array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:user_result[]')), - 'tns:user_result' + 'user_result_array', + 'complexType', + 'array', + '', + 'SOAP-ENC:Array', + array(), + array( + array( + 'ref' => 'SOAP-ENC:arrayType', + 'wsdl:arrayType' => 'tns:user_result[]', + ), + ), + 'tns:user_result' ); - $s->register( 'WSReport.GetTimeSpentOnPlatform', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string'), + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + ), array('return' => 'xsd:string') ); $s->register( 'WSReport.GetTimeSpentOnCourse', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string'), - array('return' => 'xsd:string') + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'course_id_field_name' => 'xsd:string', + 'course_id_value' => 'xsd:string', + ), + array('return' => 'xsd:string') ); $s->register( 'WSReport.GetTimeSpentOnCourseInSession', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string', 'session_id_field_name' => 'xsd:string', 'session_id_value' => 'xsd:string'), + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'course_id_field_name' => 'xsd:string', + 'course_id_value' => 'xsd:string', + 'session_id_field_name' => 'xsd:string', + 'session_id_value' => 'xsd:string', + ), array('return' => 'xsd:string') ); $s->register( 'WSReport.GetTimeSpentOnLearnpathInCourse', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string', 'learnpath_id' => 'xsd:string'), + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'course_id_field_name' => 'xsd:string', + 'course_id_value' => 'xsd:string', + 'learnpath_id' => 'xsd:string', + ), array('return' => 'xsd:string') ); $s->register( 'WSReport.GetLearnpathsByCourse', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string'), + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'course_id_field_name' => 'xsd:string', + 'course_id_value' => 'xsd:string', + ), array('return' => 'tns:user_result_array') ); $s->register( 'WSReport.GetLearnpathProgress', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string', 'learnpath_id' => 'xsd:string'), + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'course_id_field_name' => 'xsd:string', + 'course_id_value' => 'xsd:string', + 'learnpath_id' => 'xsd:string', + ), array('return' => 'tns:progress_result') ); $s->register( 'WSReport.GetLearnpathHighestLessonLocation', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string', 'learnpath_id' => 'xsd:string'), + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'course_id_field_name' => 'xsd:string', + 'course_id_value' => 'xsd:string', + 'learnpath_id' => 'xsd:string', + ), array('return' => 'xsd:string') ); $s->register( 'WSReport.GetLearnpathScoreSingleItem', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string', 'learnpath_id' => 'xsd:string', 'learnpath_item_id' => 'xsd:string'), + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'course_id_field_name' => 'xsd:string', + 'course_id_value' => 'xsd:string', + 'learnpath_id' => 'xsd:string', + 'learnpath_item_id' => 'xsd:string', + ), array('return' => 'tns:score_result') ); $s->register( 'WSReport.GetLearnpathStatusSingleItem', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string', 'learnpath_id' => 'xsd:string', 'learnpath_item_id' => 'xsd:string'), + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'course_id_field_name' => 'xsd:string', + 'course_id_value' => 'xsd:string', + 'learnpath_id' => 'xsd:string', + 'learnpath_item_id' => 'xsd:string', + ), array('return' => 'xsd:string') ); $s->register( 'WSReport.test', array(), - array('return' => 'xsd:string') + array('return' => 'xsd:string') ); diff --git a/main/webservices/soap_session.php b/main/webservices/soap_session.php index f9dbdfc7c5..3b3be8599d 100755 --- a/main/webservices/soap_session.php +++ b/main/webservices/soap_session.php @@ -12,19 +12,19 @@ $s = WSSoapServer::singleton(); $s->register( 'WSSession.CreateSession', array( - 'secret_key' => 'xsd:string', - 'name' => 'xsd:string', - 'start_date' => 'xsd:string', - 'end_date' => 'xsd:string', - 'nb_days_access_before' => 'xsd:int', - 'nb_days_access_after' => 'xsd:int', - 'nolimit' => 'xsd:int', - 'visibility' => 'xsd:int', - 'user_id_field_name' => 'xsd:string', - 'user_id_value' => 'xsd:string', - 'session_id_field_name' => 'xsd:string', - 'session_id_value' => 'xsd:string', - 'extras' => 'tns:extra_field' + 'secret_key' => 'xsd:string', + 'name' => 'xsd:string', + 'start_date' => 'xsd:string', + 'end_date' => 'xsd:string', + 'nb_days_access_before' => 'xsd:int', + 'nb_days_access_after' => 'xsd:int', + 'nolimit' => 'xsd:int', + 'visibility' => 'xsd:int', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'session_id_field_name' => 'xsd:string', + 'session_id_value' => 'xsd:string', + 'extras' => 'tns:extra_field', ), array('return' => 'xsd:int') ); @@ -32,50 +32,50 @@ $s->register( $s->register( 'WSSession.DeleteSession', array( - 'secret_key' => 'xsd:string', - 'session_id_field_name' => 'xsd:string', - 'session_id_value' => 'xsd:string' + 'secret_key' => 'xsd:string', + 'session_id_field_name' => 'xsd:string', + 'session_id_value' => 'xsd:string', ) ); $s->register( 'WSSession.EditSession', array( - 'secret_key' => 'xsd:string', - 'name' => 'xsd:string', - 'start_date' => 'xsd:string', - 'end_date' => 'xsd:string', - 'nb_days_access_before' => 'xsd:int', - 'nb_days_access_after' => 'xsd:int', - 'nolimit' => 'xsd:int', - 'visibility' => 'xsd:int', - 'user_id_field_name' => 'xsd:string', - 'user_id_value' => 'xsd:string', - 'session_id_field_name' => 'xsd:string', - 'session_id_value' => 'xsd:string', - 'extras' => 'tns:extra_field' + 'secret_key' => 'xsd:string', + 'name' => 'xsd:string', + 'start_date' => 'xsd:string', + 'end_date' => 'xsd:string', + 'nb_days_access_before' => 'xsd:int', + 'nb_days_access_after' => 'xsd:int', + 'nolimit' => 'xsd:int', + 'visibility' => 'xsd:int', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'session_id_field_name' => 'xsd:string', + 'session_id_value' => 'xsd:string', + 'extras' => 'tns:extra_field', ) ); $s->register( 'WSSession.SubscribeUserToSession', array( - 'secret_key' => 'xsd:string', - 'user_id_field_name' => 'xsd:string', - 'user_id_value' => 'xsd:string', - 'session_id_field_name' => 'xsd:string', - 'session_id_value' => 'xsd:string' + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'session_id_field_name' => 'xsd:string', + 'session_id_value' => 'xsd:string', ) ); $s->register( 'WSSession.UnsubscribeUserFromSession', array( - 'secret_key' => 'xsd:string', - 'user_id_field_name' => 'xsd:string', - 'user_id_value' => 'xsd:string', - 'session_id_field_name' => 'xsd:string', - 'session_id_value' => 'xsd:string' + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'session_id_field_name' => 'xsd:string', + 'session_id_value' => 'xsd:string', ) ); @@ -83,46 +83,46 @@ $s->register( 'WSSession.SubscribeTeacherToSessionCourse', array( 'secret_key' => 'xsd:string', - 'user_id_field_name' => 'xsd:string', - 'user_id_value' => 'xsd:string', - 'session_id_field_name' => 'xsd:string', - 'session_id_value' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'session_id_field_name' => 'xsd:string', + 'session_id_value' => 'xsd:string', 'course_id_field_name' => 'xsd:string', - 'course_id_value' => 'xsd:string' + 'course_id_value' => 'xsd:string', ) ); $s->register( 'WSSession.UnsubscribeTeacherFromSessionCourse', array( - 'secret_key' => 'xsd:string', - 'user_id_field_name' => 'xsd:string', - 'user_id_value' => 'xsd:string', - 'session_id_field_name' => 'xsd:string', - 'session_id_value' => 'xsd:string', + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'session_id_field_name' => 'xsd:string', + 'session_id_value' => 'xsd:string', 'course_id_field_name' => 'xsd:string', - 'course_id_value' => 'xsd:string' + 'course_id_value' => 'xsd:string', ) ); $s->register( 'WSSession.SubscribeCourseToSession', array( - 'secret_key' => 'xsd:string', - 'course_id_field_name' => 'xsd:string', - 'course_id_value' => 'xsd:string', - 'session_id_field_name' => 'xsd:string', - 'session_id_value' => 'xsd:string' + 'secret_key' => 'xsd:string', + 'course_id_field_name' => 'xsd:string', + 'course_id_value' => 'xsd:string', + 'session_id_field_name' => 'xsd:string', + 'session_id_value' => 'xsd:string', ) ); $s->register( 'WSSession.UnsubscribeCourseFromSession', array( - 'secret_key' => 'xsd:string', - 'course_id_field_name' => 'xsd:string', - 'course_id_value' => 'xsd:string', - 'session_id_field_name' => 'xsd:string', - 'session_id_value' => 'xsd:string' + 'secret_key' => 'xsd:string', + 'course_id_field_name' => 'xsd:string', + 'course_id_value' => 'xsd:string', + 'session_id_field_name' => 'xsd:string', + 'session_id_value' => 'xsd:string', ) ); diff --git a/main/webservices/soap_user.php b/main/webservices/soap_user.php index 50188c1244..62517ea7ed 100755 --- a/main/webservices/soap_user.php +++ b/main/webservices/soap_user.php @@ -14,223 +14,292 @@ require_once __DIR__.'/soap.php'; $s = WSSoapServer::singleton(); $s->wsdl->addComplexType( - 'user_id', - 'complexType', - 'struct', - 'all', - '', - array( - 'user_id_field_name' => array('name' => 'user_id_field_name', 'type' => 'xsd:string'), - 'user_id_value' => array('name' => 'user_id_value', 'type' => 'xsd:string') - ) + 'user_id', + 'complexType', + 'struct', + 'all', + '', + array( + 'user_id_field_name' => array( + 'name' => 'user_id_field_name', + 'type' => 'xsd:string', + ), + 'user_id_value' => array( + 'name' => 'user_id_value', + 'type' => 'xsd:string', + ), + ) ); $s->wsdl->addComplexType( - 'user_result', - 'complexType', - 'struct', - 'all', - '', - array( - 'user_id_value' => array('name' => 'user_id_value', 'type' => 'xsd:string'), - 'result' => array('name' => 'result', 'type' => 'tns:result') - ) + 'user_result', + 'complexType', + 'struct', + 'all', + '', + array( + 'user_id_value' => array( + 'name' => 'user_id_value', + 'type' => 'xsd:string', + ), + 'result' => array('name' => 'result', 'type' => 'tns:result'), + ) ); $s->wsdl->addComplexType( - 'user_result_array', - 'complexType', - 'array', - '', - 'SOAP-ENC:Array', - array(), - array(array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:user_result[]')), - 'tns:user_result' + 'user_result_array', + 'complexType', + 'array', + '', + 'SOAP-ENC:Array', + array(), + array( + array( + 'ref' => 'SOAP-ENC:arrayType', + 'wsdl:arrayType' => 'tns:user_result[]', + ), + ), + 'tns:user_result' ); $s->register( - 'WSUser.DisableUser', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string') + 'WSUser.DisableUser', + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + ) ); $s->register( - 'WSUser.DisableUsers', - array('secret_key' => 'xsd:string', 'users' => 'tns:user_id[]'), - array('return' => 'tns:user_result_array') + 'WSUser.DisableUsers', + array('secret_key' => 'xsd:string', 'users' => 'tns:user_id[]'), + array('return' => 'tns:user_result_array') ); $s->register( - 'WSUser.EnableUser', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string') + 'WSUser.EnableUser', + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + ) ); $s->register( - 'WSUser.EnableUsers', - array('secret_key' => 'xsd:string', 'users' => 'tns:user_id[]'), - array('return' => 'tns:user_result_array') + 'WSUser.EnableUsers', + array('secret_key' => 'xsd:string', 'users' => 'tns:user_id[]'), + array('return' => 'tns:user_result_array') ); $s->register( - 'WSUser.DeleteUser', - array('secret_key' => 'xsd:string', 'user_id_field_name' => 'xsd:string', 'user_id_value' => 'xsd:string') + 'WSUser.DeleteUser', + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + ) ); $s->register( - 'WSUser.DeleteUsers', - array('secret_key' => 'xsd:string', 'users' => 'tns:user_id[]'), - array('return' => 'tns:user_result_array') + 'WSUser.DeleteUsers', + array('secret_key' => 'xsd:string', 'users' => 'tns:user_id[]'), + array('return' => 'tns:user_result_array') ); $s->register( - 'WSUser.CreateUser', - array( - 'secret_key' => 'xsd:string', - 'firstname' => 'xsd:string', - 'lastname' => 'xsd:string', - 'status' => 'xsd:int', - 'loginname' => 'xsd:string', - 'password' => 'xsd:string', - 'encrypt_method' => 'xsd:string', - 'user_id_field_name' => 'xsd:string', - 'user_id_value' => 'xsd:string', - 'visibility' => 'xsd:int', - 'email' => 'xsd:string', - 'language' => 'xsd:string', - 'phone' => 'xsd:string', - 'expiration_date' => 'xsd:string', - 'extras' => 'tns:extra_field' - ), - array('return' => 'xsd:int') + 'WSUser.CreateUser', + array( + 'secret_key' => 'xsd:string', + 'firstname' => 'xsd:string', + 'lastname' => 'xsd:string', + 'status' => 'xsd:int', + 'loginname' => 'xsd:string', + 'password' => 'xsd:string', + 'encrypt_method' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'visibility' => 'xsd:int', + 'email' => 'xsd:string', + 'language' => 'xsd:string', + 'phone' => 'xsd:string', + 'expiration_date' => 'xsd:string', + 'extras' => 'tns:extra_field', + ), + array('return' => 'xsd:int') ); $s->wsdl->addComplexType( - 'user_create', - 'complexType', - 'struct', - 'all', - '', - array( - 'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'), - 'lastname' => array('name' => 'lastname', 'type' => 'xsd:string'), - 'status' => array('name' => 'status', 'type' => 'xsd:int'), - 'loginname' => array('name' => 'loginname', 'type' => 'xsd:string'), - 'password' => array('name' => 'password', 'type' => 'xsd:string'), - 'encrypt_method' => array('name' => 'encrypt_method', 'type' => 'xsd:string'), - 'user_id_field_name' => array('name' => 'user_id_field_name', 'type' => 'xsd:string'), - 'user_id_value' => array('name' => 'user_id_value', 'type' => 'xsd:string'), - 'visibility' => array('name' => 'visibility', 'type' => 'xsd:int'), - 'email' => array('name' => 'email', 'type' => 'xsd:string'), - 'language' => array('name' => 'language', 'type' => 'xsd:string'), - 'phone' => array('name' => 'phone', 'type' => 'xsd:string'), - 'expiration_date' => array('name' => 'expiration_date', 'type' => 'xsd:string'), - 'extras' => array('name' => 'extras', 'type' => 'tns:extra_field') - ) + 'user_create', + 'complexType', + 'struct', + 'all', + '', + array( + 'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'), + 'lastname' => array('name' => 'lastname', 'type' => 'xsd:string'), + 'status' => array('name' => 'status', 'type' => 'xsd:int'), + 'loginname' => array('name' => 'loginname', 'type' => 'xsd:string'), + 'password' => array('name' => 'password', 'type' => 'xsd:string'), + 'encrypt_method' => array( + 'name' => 'encrypt_method', + 'type' => 'xsd:string', + ), + 'user_id_field_name' => array( + 'name' => 'user_id_field_name', + 'type' => 'xsd:string', + ), + 'user_id_value' => array( + 'name' => 'user_id_value', + 'type' => 'xsd:string', + ), + 'visibility' => array('name' => 'visibility', 'type' => 'xsd:int'), + 'email' => array('name' => 'email', 'type' => 'xsd:string'), + 'language' => array('name' => 'language', 'type' => 'xsd:string'), + 'phone' => array('name' => 'phone', 'type' => 'xsd:string'), + 'expiration_date' => array( + 'name' => 'expiration_date', + 'type' => 'xsd:string', + ), + 'extras' => array('name' => 'extras', 'type' => 'tns:extra_field'), + ) ); $s->wsdl->addComplexType( - 'user_create_result', - 'complexType', - 'struct', - 'all', - '', - array( - 'user_id_value' => array('name' => 'user_id_value', 'type' => 'xsd:string'), - 'user_id_generated' => array('name' => 'user_id_generated', 'type' => 'xsd:int'), - 'result' => array('name' => 'result', 'type' => 'tns:result') - ) + 'user_create_result', + 'complexType', + 'struct', + 'all', + '', + array( + 'user_id_value' => array( + 'name' => 'user_id_value', + 'type' => 'xsd:string', + ), + 'user_id_generated' => array( + 'name' => 'user_id_generated', + 'type' => 'xsd:int', + ), + 'result' => array('name' => 'result', 'type' => 'tns:result'), + ) ); $s->wsdl->addComplexType( - 'user_create_result_array', - 'complexType', - 'array', - '', - 'SOAP-ENC:Array', - array(), - array(array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:user_create_result[]')), - 'tns:user_create_result' + 'user_create_result_array', + 'complexType', + 'array', + '', + 'SOAP-ENC:Array', + array(), + array( + array( + 'ref' => 'SOAP-ENC:arrayType', + 'wsdl:arrayType' => 'tns:user_create_result[]', + ), + ), + 'tns:user_create_result' ); $s->register( - 'WSUser.CreateUsers', - array( - 'secret_key' => 'xsd:string', - 'users' => 'tns:user_create[]' - ), - array('return' => 'tns:user_create_result_array') + 'WSUser.CreateUsers', + array( + 'secret_key' => 'xsd:string', + 'users' => 'tns:user_create[]', + ), + array('return' => 'tns:user_create_result_array') ); $s->register( - 'WSUser.EditUser', - array( - 'secret_key' => 'xsd:string', - 'user_id_field_name' => 'xsd:string', - 'user_id_value' => 'xsd:string', - 'firstname' => 'xsd:string', - 'lastname' => 'xsd:string', - 'status' => 'xsd:int', - 'loginname' => 'xsd:string', - 'password' => 'xsd:string', - 'encrypt_method' => 'xsd:string', - 'email' => 'xsd:string', - 'language' => 'xsd:string', - 'phone' => 'xsd:string', - 'expiration_date' => 'xsd:string', - 'extras' => 'tns:extra_field' - ) + 'WSUser.EditUser', + array( + 'secret_key' => 'xsd:string', + 'user_id_field_name' => 'xsd:string', + 'user_id_value' => 'xsd:string', + 'firstname' => 'xsd:string', + 'lastname' => 'xsd:string', + 'status' => 'xsd:int', + 'loginname' => 'xsd:string', + 'password' => 'xsd:string', + 'encrypt_method' => 'xsd:string', + 'email' => 'xsd:string', + 'language' => 'xsd:string', + 'phone' => 'xsd:string', + 'expiration_date' => 'xsd:string', + 'extras' => 'tns:extra_field', + ) ); $s->wsdl->addComplexType( - 'user_edit', - 'complexType', - 'struct', - 'all', - '', - array( - 'user_id_field_name' => array('name' => 'user_id_field_name', 'type' => 'xsd:string'), - 'user_id_value' => array('name' => 'user_id_value', 'type' => 'xsd:string'), - 'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'), - 'lastname' => array('name' => 'lastname', 'type' => 'xsd:string'), - 'status' => array('name' => 'status', 'type' => 'xsd:int'), - 'loginname' => array('name' => 'loginname', 'type' => 'xsd:string'), - 'password' => array('name' => 'password', 'type' => 'xsd:string'), - 'encrypt_method' => array('name' => 'encrypt_method', 'type' => 'xsd:string'), - 'email' => array('name' => 'email', 'type' => 'xsd:string'), - 'language' => array('name' => 'language', 'type' => 'xsd:string'), - 'phone' => array('name' => 'phone', 'type' => 'xsd:string'), - 'expiration_date' => array('name' => 'expiration_date', 'type' => 'xsd:string'), - 'extras' => array('name' => 'extras', 'type' => 'tns:extra_field') - ) + 'user_edit', + 'complexType', + 'struct', + 'all', + '', + array( + 'user_id_field_name' => array( + 'name' => 'user_id_field_name', + 'type' => 'xsd:string', + ), + 'user_id_value' => array( + 'name' => 'user_id_value', + 'type' => 'xsd:string', + ), + 'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'), + 'lastname' => array('name' => 'lastname', 'type' => 'xsd:string'), + 'status' => array('name' => 'status', 'type' => 'xsd:int'), + 'loginname' => array('name' => 'loginname', 'type' => 'xsd:string'), + 'password' => array('name' => 'password', 'type' => 'xsd:string'), + 'encrypt_method' => array( + 'name' => 'encrypt_method', + 'type' => 'xsd:string', + ), + 'email' => array('name' => 'email', 'type' => 'xsd:string'), + 'language' => array('name' => 'language', 'type' => 'xsd:string'), + 'phone' => array('name' => 'phone', 'type' => 'xsd:string'), + 'expiration_date' => array( + 'name' => 'expiration_date', + 'type' => 'xsd:string', + ), + 'extras' => array('name' => 'extras', 'type' => 'tns:extra_field'), + ) ); $s->wsdl->addComplexType( - 'user_edit_result', - 'complexType', - 'struct', - 'all', - '', - array( - 'user_id_value' => array('name' => 'user_id_value', 'type' => 'xsd:string'), - 'result' => array('name' => 'result', 'type' => 'tns:result') - ) + 'user_edit_result', + 'complexType', + 'struct', + 'all', + '', + array( + 'user_id_value' => array( + 'name' => 'user_id_value', + 'type' => 'xsd:string', + ), + 'result' => array('name' => 'result', 'type' => 'tns:result'), + ) ); $s->wsdl->addComplexType( - 'user_edit_result_array', - 'complexType', - 'array', - '', - 'SOAP-ENC:Array', - array(), - array(array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:user_edit_result[]')), - 'tns:user_edit_result' + 'user_edit_result_array', + 'complexType', + 'array', + '', + 'SOAP-ENC:Array', + array(), + array( + array( + 'ref' => 'SOAP-ENC:arrayType', + 'wsdl:arrayType' => 'tns:user_edit_result[]', + ), + ), + 'tns:user_edit_result' ); $s->register( - 'WSUser.EditUsers', - array( - 'secret_key' => 'xsd:string', - 'users' => 'tns:user_edit[]' - ), - array('return' => 'tns:user_edit_result_array') + 'WSUser.EditUsers', + array( + 'secret_key' => 'xsd:string', + 'users' => 'tns:user_edit[]', + ), + array('return' => 'tns:user_edit_result_array') ); diff --git a/main/webservices/user_info.soap.php b/main/webservices/user_info.soap.php index fd45b11ec2..cb81dc9b16 100755 --- a/main/webservices/user_info.soap.php +++ b/main/webservices/user_info.soap.php @@ -20,18 +20,23 @@ $server->configureWSDL('WSUserInfo', 'urn:WSUserInfo'); // Register the data structures used by the service $server->wsdl->addComplexType( - 'courseDetails', - 'complexType', - 'struct', - 'all', - '', - array( - 'name'=>'code', 'type'=>'xsd:string', - 'name'=>'title', 'type'=>'xsd:string', - 'name'=>'url', 'type'=>'xsd:string', - 'name'=>'teacher', 'type'=>'xsd:string', - 'name'=>'language', 'type'=>'xsd:string', - ) + 'courseDetails', + 'complexType', + 'struct', + 'all', + '', + array( + 'name' => 'code', + 'type' => 'xsd:string', + 'name' => 'title', + 'type' => 'xsd:string', + 'name' => 'url', + 'type' => 'xsd:string', + 'name' => 'teacher', + 'type' => 'xsd:string', + 'name' => 'language', + 'type' => 'xsd:string', + ) ); $server->wsdl->addComplexType( @@ -42,16 +47,21 @@ $server->wsdl->addComplexType( 'SOAP-ENC:Array', array(), array( - array('ref'=>'SOAP-ENC:arrayType', - 'wsdl:arrayType'=>'tns:courseDetails[]') + array( + 'ref' => 'SOAP-ENC:arrayType', + 'wsdl:arrayType' => 'tns:courseDetails[]', + ), ), 'tns:courseDetails' ); // Register the method to expose -$server->register('WSCourseListOfUser', // method name - array('username' => 'xsd:string', - 'signature' => 'xsd:string'), // input parameters +$server->register( + 'WSCourseListOfUser', // method name + array( + 'username' => 'xsd:string', + 'signature' => 'xsd:string', + ), // input parameters array('return' => 'xsd:Array'), // output parameters 'urn:WSUserInfo', // namespace 'urn:WSUserInfo#WSUserInfo', // soapaction @@ -68,8 +78,11 @@ $server->register('WSCourseListOfUser', // method name * @param string Signature (composed of the sha1(username+apikey) * @return array Courses list (code=>[title=>'title',url='http://...',teacher=>'...',language=>''],code=>[...],...) */ -function WSCourseListOfUser($username, $signature) { - if (empty($username) or empty($signature)) { return -1; } +function WSCourseListOfUser($username, $signature) +{ + if (empty($username) or empty($signature)) { + return -1; + } global $_configuration; $info = api_get_user_info_from_username($username); @@ -77,7 +90,7 @@ function WSCourseListOfUser($username, $signature) { $list = UserManager::get_api_keys($user_id, 'dokeos'); $key = ''; foreach ($list as $key) { - break; + break; } $local_key = $username.$key; @@ -90,8 +103,17 @@ function WSCourseListOfUser($username, $signature) { $courses_list_tmp = CourseManager::get_courses_list_by_user_id($user_id); foreach ($courses_list_tmp as $index => $course) { $course_info = CourseManager::get_course_information($course['code']); - $courses_list[] = array('code' => $course['code'], 'title' => api_utf8_encode($course_info['title']), 'url' => api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/', 'teacher' => api_utf8_encode($course_info['tutor_name']), 'language' => $course_info['course_language']); + $courses_list[] = array( + 'code' => $course['code'], + 'title' => api_utf8_encode($course_info['title']), + 'url' => api_get_path( + WEB_COURSE_PATH + ).$course_info['directory'].'/', + 'teacher' => api_utf8_encode($course_info['tutor_name']), + 'language' => $course_info['course_language'], + ); } + return $courses_list; } @@ -104,11 +126,16 @@ $server->wsdl->addComplexType( 'all', '', array( - 'name'=>'datestart', 'type'=>'xsd:string', - 'name'=>'dateend', 'type'=>'xsd:string', - 'name'=>'title', 'type'=>'xsd:string', - 'name'=>'link', 'type'=>'xsd:string', - 'name'=>'coursetitle', 'type'=>'xsd:string', + 'name' => 'datestart', + 'type' => 'xsd:string', + 'name' => 'dateend', + 'type' => 'xsd:string', + 'name' => 'title', + 'type' => 'xsd:string', + 'name' => 'link', + 'type' => 'xsd:string', + 'name' => 'coursetitle', + 'type' => 'xsd:string', ) ); @@ -120,23 +147,35 @@ $server->wsdl->addComplexType( 'SOAP-ENC:Array', array(), array( - array('ref'=>'SOAP-ENC:arrayType', - 'wsdl:arrayType'=>'tns:eventDetails[]') + array( + 'ref' => 'SOAP-ENC:arrayType', + 'wsdl:arrayType' => 'tns:eventDetails[]', + ), ), 'tns:eventDetails' ); // Register the method to expose -$server->register('WSEventsList', // method name - array('username' => 'xsd:string', - 'signature' => 'xsd:string', - 'datestart' => 'xsd:int', - 'dateend' => 'xsd:int'), // input parameters - array('return' => 'xsd:Array'), // output parameters - 'urn:WSUserInfo', // namespace - 'urn:WSUserInfo#WSEventsList', // soapaction - 'rpc', // style - 'encoded', // use +$server->register( + 'WSEventsList', + // method name + array( + 'username' => 'xsd:string', + 'signature' => 'xsd:string', + 'datestart' => 'xsd:int', + 'dateend' => 'xsd:int', + ), + // input parameters + array('return' => 'xsd:Array'), + // output parameters + 'urn:WSUserInfo', + // namespace + 'urn:WSUserInfo#WSEventsList', + // soapaction + 'rpc', + // style + 'encoded', + // use 'This service returns a list of events of the courses the given user is subscribed to' // documentation ); @@ -170,10 +209,16 @@ function WSEventsList($username, $signature, $datestart = 0, $dateend = 0) { $events_list = array(); $user_id = UserManager::get_user_id_from_username($username); - if ($user_id === false) { return $events_list; } // Error in user id recovery. + if ($user_id === false) { + return $events_list; + } // Error in user id recovery. $ds = substr($datestart, 0, 4).'-'.substr($datestart, 4, 2).'-'.substr($datestart, 6, 2).' 00:00:00'; $de = substr($dateend, 0, 4).'-'.substr($dateend, 4, 2).'-'.substr($dateend, 6, 2).' 00:00:00'; - $events_list = Agenda::get_personal_agenda_items_between_dates($user_id, $ds, $de); + $events_list = Agenda::get_personal_agenda_items_between_dates( + $user_id, + $ds, + $de + ); return $events_list; } diff --git a/main/webservices/webservice.php b/main/webservices/webservice.php index e39a09fbfa..0280ccfb3b 100755 --- a/main/webservices/webservice.php +++ b/main/webservices/webservice.php @@ -11,237 +11,255 @@ require_once __DIR__.'/../inc/global.inc.php'; */ class WSError { - /** - * Error handler. This needs to be a class that implements the interface WSErrorHandler - * - * @var WSErrorHandler - */ - protected static $_handler; - - /** - * Error code - * - * @var int - */ - public $code; - - /** - * Error message - * - * @var string - */ - public $message; - - /** - * Constructor - * - * @param int Error code - * @param string Error message - */ - public function __construct($code, $message) { - $this->code = $code; - $this->message = $message; - } - - /** - * Sets the error handler - * - * @param WSErrorHandler Error handler - */ - public static function setErrorHandler($handler) { - if ($handler instanceof WSErrorHandler) { - self::$_handler = $handler; - } - } - - /** - * Returns the error handler - * - * @return WSErrorHandler Error handler - */ - public static function getErrorHandler() { - return self::$_handler; - } - - /** - * Transforms the error into an array - * - * @return array Associative array with code and message - */ - public function toArray() { - return array('code' => $this->code, 'message' => $this->message); - } + /** + * Error handler. This needs to be a class that implements the interface WSErrorHandler + * + * @var WSErrorHandler + */ + protected static $_handler; + + /** + * Error code + * + * @var int + */ + public $code; + + /** + * Error message + * + * @var string + */ + public $message; + + /** + * Constructor + * + * @param int Error code + * @param string Error message + */ + public function __construct($code, $message) + { + $this->code = $code; + $this->message = $message; + } + + /** + * Sets the error handler + * + * @param WSErrorHandler Error handler + */ + public static function setErrorHandler($handler) + { + if ($handler instanceof WSErrorHandler) { + self::$_handler = $handler; + } + } + + /** + * Returns the error handler + * + * @return WSErrorHandler Error handler + */ + public static function getErrorHandler() + { + return self::$_handler; + } + + /** + * Transforms the error into an array + * + * @return array Associative array with code and message + */ + public function toArray() + { + return array('code' => $this->code, 'message' => $this->message); + } } /** * Interface that must be implemented by any error handler */ -interface WSErrorHandler { - /** - * Handle method - * - * @param WSError Error - */ - public function handle($error); +interface WSErrorHandler +{ + /** + * Handle method + * + * @param WSError Error + */ + public function handle($error); } /** * Main class of the webservice. Webservice classes extend this class */ -class WS { - /** - * Chamilo configuration - * - * @var array - */ - protected $_configuration; - - /** - * Constructor - */ - public function __construct() { - $this->_configuration = $GLOBALS['_configuration']; - } - - /** - * Verifies the API key - * - * @param string Secret key - * @return mixed WSError in case of failure, null in case of success - */ - protected function verifyKey($secret_key) { - $ip = trim($_SERVER['REMOTE_ADDR']); - // if we are behind a reverse proxy, assume it will send the - // HTTP_X_FORWARDED_FOR header and use this IP instead - if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { - list($ip1, $ip2) = preg_split('/,/', $_SERVER['HTTP_X_FORWARDED_FOR']); - $ip = trim($ip1); - } - $security_key = $ip.$this->_configuration['security_key']; - - if (!api_is_valid_secret_key($secret_key, $security_key)) { - return new WSError(1, "API key is invalid"); - } else { - return null; - } - } - - /** - * Gets the real user id based on the user id field name and value. - * Note that if the user id field name is "chamilo_user_id", it will use the user id - * in the system database - * - * @param string User id field name - * @param string User id value - * @return mixed System user id if the user was found, WSError otherwise - */ - protected function getUserId($user_id_field_name, $user_id_value) { - if ($user_id_field_name == "chamilo_user_id") { - if (UserManager::is_user_id_valid(intval($user_id_value))) { - return intval($user_id_value); - } else { - return new WSError(100, "User not found"); - } - } else { - $user_id = UserManager::get_user_id_from_original_id($user_id_value, $user_id_field_name); - if ($user_id == 0) { - return new WSError(100, "User not found"); - } else { - return $user_id; - } - } - } - - /** - * Gets the real course id based on the course id field name and value. - * Note that if the course id field name is "chamilo_course_id", it will use the course id - * in the system database - * - * @param string Course id field name - * @param string Course id value - * @return mixed System course id if the course was found, WSError otherwise - */ - protected function getCourseId($course_id_field_name, $course_id_value) - { - if ($course_id_field_name == "chamilo_course_id") { - if (CourseManager::get_course_code_from_course_id( - intval($course_id_value) - ) != null - ) { - return intval($course_id_value); - } else { - return new WSError(200, "Course not found"); - } - } else { - $courseId = CourseManager::get_course_code_from_original_id( - $course_id_value, - $course_id_field_name - ); - if (!empty($courseId)) { - return $courseId; - } else { - return new WSError(200, "Course not found"); - } - } - } - - /** - * Gets the real session id based on the session id field name and value. - * Note that if the session id field name is "chamilo_session_id", it will use the session id - * in the system database - * - * @param string Session id field name - * @param string Session id value - * @return mixed System session id if the session was found, WSError otherwise - */ - protected function getSessionId($session_id_field_name, $session_id_value) - { - if ($session_id_field_name == "chamilo_session_id") { - $session = SessionManager::fetch((int) $session_id_value); - if (!empty($session)) { - return intval($session_id_value); - } else { - return new WSError(300, "Session not found"); - } - } else { - $session_id = SessionManager::getSessionIdFromOriginalId( - $session_id_value, - $session_id_field_name - ); - if ($session_id == 0) { - return new WSError(300, "Session not found"); - } else { - return $session_id; - } - } - } - - /** - * Handles an error by calling the WSError error handler - * - * @param WSError Error - */ - protected function handleError($error) { - $handler = WSError::getErrorHandler(); - $handler->handle($error); - } - - /** - * Gets a successful result - * - * @return array Array with a code of 0 and a message 'Operation was successful' - */ - protected function getSuccessfulResult() { - return array('code' => 0, 'message' => 'Operation was successful'); - } - - /** - * Test function. Returns the string success - * - * @return string Success - */ - public function test() { - return "success"; - } +class WS +{ + /** + * Chamilo configuration + * + * @var array + */ + protected $_configuration; + + /** + * Constructor + */ + public function __construct() + { + $this->_configuration = $GLOBALS['_configuration']; + } + + /** + * Verifies the API key + * + * @param string Secret key + * @return mixed WSError in case of failure, null in case of success + */ + protected function verifyKey($secret_key) + { + $ip = trim($_SERVER['REMOTE_ADDR']); + // if we are behind a reverse proxy, assume it will send the + // HTTP_X_FORWARDED_FOR header and use this IP instead + if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { + list($ip1, $ip2) = preg_split( + '/,/', + $_SERVER['HTTP_X_FORWARDED_FOR'] + ); + $ip = trim($ip1); + } + $security_key = $ip.$this->_configuration['security_key']; + + if (!api_is_valid_secret_key($secret_key, $security_key)) { + return new WSError(1, "API key is invalid"); + } else { + return null; + } + } + + /** + * Gets the real user id based on the user id field name and value. + * Note that if the user id field name is "chamilo_user_id", it will use the user id + * in the system database + * + * @param string User id field name + * @param string User id value + * @return mixed System user id if the user was found, WSError otherwise + */ + protected function getUserId($user_id_field_name, $user_id_value) + { + if ($user_id_field_name == "chamilo_user_id") { + if (UserManager::is_user_id_valid(intval($user_id_value))) { + return intval($user_id_value); + } else { + return new WSError(100, "User not found"); + } + } else { + $user_id = UserManager::get_user_id_from_original_id( + $user_id_value, + $user_id_field_name + ); + if ($user_id == 0) { + return new WSError(100, "User not found"); + } else { + return $user_id; + } + } + } + + /** + * Gets the real course id based on the course id field name and value. + * Note that if the course id field name is "chamilo_course_id", it will use the course id + * in the system database + * + * @param string Course id field name + * @param string Course id value + * @return mixed System course id if the course was found, WSError otherwise + */ + protected function getCourseId($course_id_field_name, $course_id_value) + { + if ($course_id_field_name == "chamilo_course_id") { + if (CourseManager::get_course_code_from_course_id( + intval($course_id_value) + ) != null + ) { + return intval($course_id_value); + } else { + return new WSError(200, "Course not found"); + } + } else { + $courseId = CourseManager::get_course_code_from_original_id( + $course_id_value, + $course_id_field_name + ); + if (!empty($courseId)) { + return $courseId; + } else { + return new WSError(200, "Course not found"); + } + } + } + + /** + * Gets the real session id based on the session id field name and value. + * Note that if the session id field name is "chamilo_session_id", it will use the session id + * in the system database + * + * @param string Session id field name + * @param string Session id value + * @return mixed System session id if the session was found, WSError otherwise + */ + protected function getSessionId($session_id_field_name, $session_id_value) + { + if ($session_id_field_name == "chamilo_session_id") { + $session = SessionManager::fetch((int)$session_id_value); + if (!empty($session)) { + return intval($session_id_value); + } else { + return new WSError(300, "Session not found"); + } + } else { + $session_id = SessionManager::getSessionIdFromOriginalId( + $session_id_value, + $session_id_field_name + ); + if ($session_id == 0) { + return new WSError(300, "Session not found"); + } else { + return $session_id; + } + } + } + + /** + * Handles an error by calling the WSError error handler + * + * @param WSError Error + */ + protected function handleError($error) + { + $handler = WSError::getErrorHandler(); + $handler->handle($error); + } + + /** + * Gets a successful result + * + * @return array Array with a code of 0 and a message 'Operation was successful' + */ + protected function getSuccessfulResult() + { + return array('code' => 0, 'message' => 'Operation was successful'); + } + + /** + * Test function. Returns the string success + * + * @return string Success + */ + public function test() + { + return "success"; + } } diff --git a/main/webservices/webservice_course.php b/main/webservices/webservice_course.php index b4e1ca88aa..4800d5d0ad 100755 --- a/main/webservices/webservice_course.php +++ b/main/webservices/webservice_course.php @@ -12,490 +12,712 @@ require_once __DIR__.'/webservice.php'; */ class WSCourse extends WS { - /** - * Deletes a course (helper method) - * - * @param string Course id field name - * @param string Course id value - * @return mixed True if the course was successfully deleted, WSError otherwise - */ - protected function deleteCourseHelper($course_id_field_name, $course_id_value) { - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); - if($course_id instanceof WSError) { - return $course_id; - } else { - $course_code = CourseManager::get_course_code_from_course_id($course_id); - CourseManager::delete_course($course_code); - return true; - } - } + /** + * Deletes a course (helper method) + * + * @param string Course id field name + * @param string Course id value + * @return mixed True if the course was successfully deleted, WSError otherwise + */ + protected function deleteCourseHelper( + $course_id_field_name, + $course_id_value + ) { + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); + if ($course_id instanceof WSError) { + return $course_id; + } else { + $course_code = CourseManager::get_course_code_from_course_id( + $course_id + ); + CourseManager::delete_course($course_code); - /** - * Deletes a course - * - * @param string API secret key - * @param string Course id field name - * @param string Course id value - */ - public function DeleteCourse($secret_key, $course_id_field_name, $course_id_value) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->deleteCourseHelper($course_id_field_name, $course_id_value); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + return true; + } + } - /** - * Deletes multiple courses - * - * @param string API secret key - * @param array Array of courses with elements of the form array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value') - * @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 - * than 0, an error occured - */ - public function DeleteCourses($secret_key, $courses) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $results = array(); - foreach($courses as $course) { - $result_tmp = array(); - $result_op = $this->deleteCourseHelper($course['course_id_field_name'], $course['course_id_value']); - $result_tmp['course_id_value'] = $course['course_id_value']; - if($result_op instanceof WSError) { - // Return the error in the results - $result_tmp['result'] = $result_op->toArray(); - } else { - $result_tmp['result'] = $this->getSuccessfulResult(); - } - $results[] = $result_tmp; - } - return $results; - } - } + /** + * Deletes a course + * + * @param string API secret key + * @param string Course id field name + * @param string Course id value + */ + public function DeleteCourse( + $secret_key, + $course_id_field_name, + $course_id_value + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->deleteCourseHelper( + $course_id_field_name, + $course_id_value + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } + + /** + * Deletes multiple courses + * + * @param string API secret key + * @param array Array of courses with elements of the form array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value') + * @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 + * than 0, an error occured + */ + public function DeleteCourses($secret_key, $courses) + { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $results = array(); + foreach ($courses as $course) { + $result_tmp = array(); + $result_op = $this->deleteCourseHelper( + $course['course_id_field_name'], + $course['course_id_value'] + ); + $result_tmp['course_id_value'] = $course['course_id_value']; + if ($result_op instanceof WSError) { + // Return the error in the results + $result_tmp['result'] = $result_op->toArray(); + } else { + $result_tmp['result'] = $this->getSuccessfulResult(); + } + $results[] = $result_tmp; + } + + return $results; + } + } - /** - * Creates a course (helper method) - * - * @param string Title - * @param string Category code - * @param string Wanted code. If it's not defined, it will be generated automatically - * @param string Tutor name - * @param string Course admin user id field name - * @param string Course admin user id value - * @param string Course language - * @param string Course id field name - * @param string Course id value - * @param array Course extra fields - * @return mixed Generated id if creation was successful, WSError otherwise - */ - 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) { - // Add the original course id field name and value to the extra fields if needed - $extras_associative = array(); - if($course_id_field_name != "chamilo_course_id") { - $extras_associative[$course_id_field_name] = $course_id_value; - } - foreach($extras as $extra) { - $extras_associative[$extra['field_name']] = $extra['field_value']; - } - $course_admin_id = $this->getUserId($course_admin_user_id_field_name, $course_admin_user_id_value); - if($course_admin_id instanceof WSError) { - return $course_admin_id; - } - if($wanted_code == '') { - $wanted_code = CourseManager::generate_course_code($title); - } - $result = create_course($wanted_code, $title, $tutor_name, $category_code, $language, $course_admin_id, $this->_configuration['db_prefix'], 0); - if (!$result) { - return new WSError(202, 'There was an error creating the course'); - } else { - // Update extra fields - foreach($extras_associative as $fname => $fvalue) { - CourseManager::update_course_extra_field_value($result, $fname, $fvalue); - } - // Get course id - $course_info = CourseManager::get_course_information($result); - return $course_info['real_id']; - } - } + /** + * Creates a course (helper method) + * + * @param string Title + * @param string Category code + * @param string Wanted code. If it's not defined, it will be generated automatically + * @param string Tutor name + * @param string Course admin user id field name + * @param string Course admin user id value + * @param string Course language + * @param string Course id field name + * @param string Course id value + * @param array Course extra fields + * @return mixed Generated id if creation was successful, WSError otherwise + */ + 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 + ) { + // Add the original course id field name and value to the extra fields if needed + $extras_associative = array(); + if ($course_id_field_name != "chamilo_course_id") { + $extras_associative[$course_id_field_name] = $course_id_value; + } + foreach ($extras as $extra) { + $extras_associative[$extra['field_name']] = $extra['field_value']; + } + $course_admin_id = $this->getUserId( + $course_admin_user_id_field_name, + $course_admin_user_id_value + ); + if ($course_admin_id instanceof WSError) { + return $course_admin_id; + } + if ($wanted_code == '') { + $wanted_code = CourseManager::generate_course_code($title); + } + $result = create_course( + $wanted_code, + $title, + $tutor_name, + $category_code, + $language, + $course_admin_id, + $this->_configuration['db_prefix'], + 0 + ); + if (!$result) { + return new WSError(202, 'There was an error creating the course'); + } else { + // Update extra fields + foreach ($extras_associative as $fname => $fvalue) { + CourseManager::update_course_extra_field_value( + $result, + $fname, + $fvalue + ); + } + // Get course id + $course_info = CourseManager::get_course_information($result); + + return $course_info['real_id']; + } + } - /** - * Creates a course - * - * @param string API secret key - * @param string Title - * @param string Category code - * @param string Wanted code. If it's not defined, it will be generated automatically - * @param string Tutor name - * @param string Course admin user id field name - * @param string Course admin user id value - * @param string Course language - * @param string Course id field name - * @param string Course id value - * @param array Course extra fields - * @return int Course id generated - */ - 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) { - // First, verify the secret key - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $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); - if($result instanceof WSError) { - $this->handleError($result); - } else { - return $result; - } - } - } + /** + * Creates a course + * + * @param string API secret key + * @param string Title + * @param string Category code + * @param string Wanted code. If it's not defined, it will be generated automatically + * @param string Tutor name + * @param string Course admin user id field name + * @param string Course admin user id value + * @param string Course language + * @param string Course id field name + * @param string Course id value + * @param array Course extra fields + * @return int Course id generated + */ + 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 + ) { + // First, verify the secret key + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $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 + ); + if ($result instanceof WSError) { + $this->handleError($result); + } else { + return $result; + } + } + } - /** - * Create multiple courses - * - * @param string API secret key - * @param array Courses to be created, with elements following the structure presented in CreateCourse - * @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')) - */ - public function CreateCourses($secret_key, $courses) { - // First, verify the secret key - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $results = array(); - foreach($courses as $course) { - $result_tmp = array(); + /** + * Create multiple courses + * + * @param string API secret key + * @param array Courses to be created, with elements following the structure presented in CreateCourse + * @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')) + */ + public function CreateCourses($secret_key, $courses) + { + // First, verify the secret key + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $results = array(); + foreach ($courses as $course) { + $result_tmp = array(); // re-initialize variables just in case $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; - extract($course); - $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); - if($result instanceof WSError) { - $result_tmp['result'] = $result->toArray(); - $result_tmp['course_id_value'] = $course_id_value; - $result_tmp['course_id_generated'] = 0; - } else { - $result_tmp['result'] = $this->getSuccessfulResult(); - $result_tmp['course_id_value'] = $course_id_value; - $result_tmp['course_id_generated'] = $result; - } - $results[] = $result_tmp; - } - return $results; - } - } + extract($course); + $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 + ); + if ($result instanceof WSError) { + $result_tmp['result'] = $result->toArray(); + $result_tmp['course_id_value'] = $course_id_value; + $result_tmp['course_id_generated'] = 0; + } else { + $result_tmp['result'] = $this->getSuccessfulResult(); + $result_tmp['course_id_value'] = $course_id_value; + $result_tmp['course_id_generated'] = $result; + } + $results[] = $result_tmp; + } + + return $results; + } + } + + /** + * Edits a course (helper method) + * + * @param string Course id field name + * @param string Course id value + * @param string Title + * @param string Category code + * @param string Department name + * @param string Department url + * @param string Course language + * @param int Visibility + * @param int Subscribe (0 = denied, 1 = allowed) + * @param int Unsubscribe (0 = denied, 1 = allowed) + * @param string Visual code + * @param array Course extra fields + * @return mixed True in case of success, WSError otherwise + */ + protected function editCourseHelper( + $course_id_field_name, + $course_id_value, + $title, + $category_code, + $department_name, + $department_url, + $language, + $visibility, + $subscribe, + $unsubscribe, + $visual_code, + $extras + ) { + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); + if ($course_id instanceof WSError) { + return $course_id; + } else { + $attributes = array(); + if (!empty($title)) { + $attributes['title'] = $title; + } + if (!empty($category_code)) { + $attributes['category_code'] = $category_code; + } + if (!empty($department_name)) { + $attributes['department_name'] = $department_name; + } + if (!empty($department_url)) { + $attributes['department_url'] = $department_url; + } + if (!empty($language)) { + $attributes['course_language'] = $language; + } + if ($visibility != '') { + $attributes['visibility'] = (int)$visibility; + } + if ($subscribe != '') { + $attributes['subscribe'] = (int)$subscribe; + } + if ($unsubscribe != '') { + $attributes['unsubscribe'] = (int)$unsubscribe; + } + if (!empty($visual_code)) { + $attributes['visual_code'] = $visual_code; + } + if (!empty($attributes)) { + CourseManager::update_attributes($course_id, $attributes); + } + if (!empty($extras)) { + $course_code = CourseManager::get_course_code_from_course_id( + $course_id + ); + $extras_associative = array(); + foreach ($extras as $extra) { + $extras_associative[$extra['field_name']] = $extra['field_value']; + } + foreach ($extras_associative as $fname => $fvalue) { + CourseManager::update_extra_field_value( + $course_code, + $fname, + $fvalue + ); + } + } - /** - * Edits a course (helper method) - * - * @param string Course id field name - * @param string Course id value - * @param string Title - * @param string Category code - * @param string Department name - * @param string Department url - * @param string Course language - * @param int Visibility - * @param int Subscribe (0 = denied, 1 = allowed) - * @param int Unsubscribe (0 = denied, 1 = allowed) - * @param string Visual code - * @param array Course extra fields - * @return mixed True in case of success, WSError otherwise - */ - protected function editCourseHelper($course_id_field_name, $course_id_value, $title, $category_code, $department_name, $department_url, $language, $visibility, $subscribe, $unsubscribe, $visual_code, $extras) { - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); - if($course_id instanceof WSError) { - return $course_id; - } else { - $attributes = array(); - if(!empty($title)) { - $attributes['title'] = $title; - } - if(!empty($category_code)) { - $attributes['category_code'] = $category_code; - } - if(!empty($department_name)) { - $attributes['department_name'] = $department_name; - } - if(!empty($department_url)) { - $attributes['department_url'] = $department_url; - } - if(!empty($language)) { - $attributes['course_language'] = $language; - } - if($visibility != '') { - $attributes['visibility'] = (int)$visibility; - } - if($subscribe != '') { - $attributes['subscribe'] = (int)$subscribe; - } - if($unsubscribe != '') { - $attributes['unsubscribe'] = (int)$unsubscribe; - } - if(!empty($visual_code)) { - $attributes['visual_code'] = $visual_code; - } - if(!empty($attributes)) { - CourseManager::update_attributes($course_id, $attributes); - } - if(!empty($extras)) { - $course_code = CourseManager::get_course_code_from_course_id($course_id); - $extras_associative = array(); - foreach($extras as $extra) { - $extras_associative[$extra['field_name']] = $extra['field_value']; - } - foreach($extras_associative as $fname => $fvalue) { - CourseManager::update_extra_field_value($course_code, $fname, $fvalue); - } - } - return true; - } - } + return true; + } + } - /** - * Edits a course - * - * @param string API secret key - * @param string Course id field name - * @param string Course id value - * @param string Title - * @param string Category code - * @param string Department name - * @param string Department url - * @param string Course language - * @param int Visibility - * @param int Subscribe (0 = denied, 1 = allowed) - * @param int Unsubscribe (0 = denied, 1 = allowed) - * @param string Visual code - * @param array Course extra fields - */ - 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) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->editCourseHelper($course_id_field_name, $course_id_value, $title, $category_code, $department_name, $department_url, $language, $visibility, $subscribe, $unsubscribe, $visual_code, $extras); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + /** + * Edits a course + * + * @param string API secret key + * @param string Course id field name + * @param string Course id value + * @param string Title + * @param string Category code + * @param string Department name + * @param string Department url + * @param string Course language + * @param int Visibility + * @param int Subscribe (0 = denied, 1 = allowed) + * @param int Unsubscribe (0 = denied, 1 = allowed) + * @param string Visual code + * @param array Course extra fields + */ + 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 + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->editCourseHelper( + $course_id_field_name, + $course_id_value, + $title, + $category_code, + $department_name, + $department_url, + $language, + $visibility, + $subscribe, + $unsubscribe, + $visual_code, + $extras + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } - /** - * List courses - * - * @param string API secret key - * @param string A list of visibility filter we want to apply - * @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', - * 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id') - */ - public function ListCourses($secret_key, $visibility = 'public,public-registered,private,closed') { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $visibilities = split(',',$visibility); - $vis = array('public' => '3', 'public-registered' => '2', 'private' => '1', 'closed' => '0'); + /** + * List courses + * + * @param string API secret key + * @param string A list of visibility filter we want to apply + * @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', + * 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id') + */ + public function ListCourses( + $secret_key, + $visibility = 'public,public-registered,private,closed' + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $visibilities = split(',', $visibility); + $vis = array( + 'public' => '3', + 'public-registered' => '2', + 'private' => '1', + 'closed' => '0', + ); foreach ($visibilities as $p => $visibility) { $visibilities[$p] = $vis[$visibility]; } - $courses_result = array(); - $category_names = array(); + $courses_result = array(); + $category_names = array(); - $courses = CourseManager::get_courses_list(); - foreach($courses as $course) { + $courses = CourseManager::get_courses_list(); + foreach ($courses as $course) { //skip elements that do not match required visibility - if (!in_array($course['visibility'],$visibilities)) { continue; } - $course_tmp = array(); - $course_tmp['id'] = $course['id']; - $course_tmp['code'] = $course['code']; - $course_tmp['title'] = $course['title']; - $course_tmp['language'] = $course['course_language']; - $course_tmp['visibility'] = $course['visibility']; + if (!in_array($course['visibility'], $visibilities)) { + continue; + } + $course_tmp = array(); + $course_tmp['id'] = $course['id']; + $course_tmp['code'] = $course['code']; + $course_tmp['title'] = $course['title']; + $course_tmp['language'] = $course['course_language']; + $course_tmp['visibility'] = $course['visibility']; - // Determining category name - if($category_names[$course['category_code']]) { - $course_tmp['category_name'] = $category_names[$course['category_code']]; - } else { - $category = CourseManager::get_course_category($course['category_code']); - $category_names[$course['category_code']] = $category['name']; - $course_tmp['category_name'] = $category['name']; - } + // Determining category name + if ($category_names[$course['category_code']]) { + $course_tmp['category_name'] = $category_names[$course['category_code']]; + } else { + $category = CourseManager::get_course_category( + $course['category_code'] + ); + $category_names[$course['category_code']] = $category['name']; + $course_tmp['category_name'] = $category['name']; + } - // Determining number of students registered in course - $user_list = CourseManager::get_user_list_from_course_code($course['code'], 0); - $course_tmp['number_students'] = count($user_list); + // Determining number of students registered in course + $user_list = CourseManager::get_user_list_from_course_code( + $course['code'], + 0 + ); + $course_tmp['number_students'] = count($user_list); - // Determining external course id - this code misses the external course id field name - // $course_tmp['external_course_id'] = CourseManager::get_course_extra_field_value($course_field_name, $course['code']); + // Determining external course id - this code misses the external course id field name + // $course_tmp['external_course_id'] = CourseManager::get_course_extra_field_value($course_field_name, $course['code']); - $courses_result[] = $course_tmp; - } + $courses_result[] = $course_tmp; + } - return $courses_result; - } - } + return $courses_result; + } + } - /** - * Subscribe or unsubscribe user to a course (helper method) - * - * @param string Course id field name. Use "chamilo_course_id" to use internal id - * @param string Course id value. - * @param string User id field name. Use "chamilo_user_id" to use internal id - * @param string User id value - * @param int Set to 1 to subscribe, 0 to unsubscribe - * @param int Status (STUDENT or TEACHER) Used for subscription only - * @return mixed True if subscription or unsubscription was successful, false otherwise - */ - protected function changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, $state, $status = STUDENT) { - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); - if($course_id instanceof WSError) { - return $course_id; - } else { - $user_id = $this->getUserId($user_id_field_name, $user_id_value); - if($user_id instanceof WSError) { - return $user_id; - } else { - $course_code = CourseManager::get_course_code_from_course_id($course_id); - if($state == 0) { - // Unsubscribe user - CourseManager::unsubscribe_user($user_id, $course_code); - return true; - } else { - // Subscribe user - if(CourseManager::subscribe_user($user_id, $course_code, $status)) { - return true; - } else { - return new WSError(203, 'An error occured subscribing to this course'); - } - } - } - } - } + /** + * Subscribe or unsubscribe user to a course (helper method) + * + * @param string Course id field name. Use "chamilo_course_id" to use internal id + * @param string Course id value. + * @param string User id field name. Use "chamilo_user_id" to use internal id + * @param string User id value + * @param int Set to 1 to subscribe, 0 to unsubscribe + * @param int Status (STUDENT or TEACHER) Used for subscription only + * @return mixed True if subscription or unsubscription was successful, false otherwise + */ + protected function changeUserSubscription( + $course_id_field_name, + $course_id_value, + $user_id_field_name, + $user_id_value, + $state, + $status = STUDENT + ) { + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); + if ($course_id instanceof WSError) { + return $course_id; + } else { + $user_id = $this->getUserId($user_id_field_name, $user_id_value); + if ($user_id instanceof WSError) { + return $user_id; + } else { + $course_code = CourseManager::get_course_code_from_course_id( + $course_id + ); + if ($state == 0) { + // Unsubscribe user + CourseManager::unsubscribe_user($user_id, $course_code); - /** - * Subscribe user to a course - * - * @param string API secret key - * @param string Course id field name. Use "chamilo_course_id" to use internal id - * @param string Course id value. - * @param string User id field name. Use "chamilo_user_id" to use internal id - * @param string User id value - * @param int Status (1 = Teacher, 5 = Student) - */ - public function SubscribeUserToCourse($secret_key, $course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, $status) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, 1, $status); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + return true; + } else { + // Subscribe user + if (CourseManager::subscribe_user( + $user_id, + $course_code, + $status + ) + ) { + return true; + } else { + return new WSError( + 203, + 'An error occured subscribing to this course' + ); + } + } + } + } + } - /** - * Unsusbscribe user from course - * - * @param string API secret key - * @param string Course id field name. Use "chamilo_course_id" to use internal id - * @param string Course id value. - * @param string User id field name. Use "chamilo_user_id" to use internal id - * @param string User id value - */ - public function UnsubscribeUserFromCourse($secret_key, $course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->changeUserSubscription($course_id_field_name, $course_id_value, $user_id_field_name, $user_id_value, 0); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + /** + * Subscribe user to a course + * + * @param string API secret key + * @param string Course id field name. Use "chamilo_course_id" to use internal id + * @param string Course id value. + * @param string User id field name. Use "chamilo_user_id" to use internal id + * @param string User id value + * @param int Status (1 = Teacher, 5 = Student) + */ + public function SubscribeUserToCourse( + $secret_key, + $course_id_field_name, + $course_id_value, + $user_id_field_name, + $user_id_value, + $status + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->changeUserSubscription( + $course_id_field_name, + $course_id_value, + $user_id_field_name, + $user_id_value, + 1, + $status + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } - /** - * Returns the descriptions of a course, along with their id - * - * @param string API secret key - * @param string Course id field name - * @param string Course id value - * @return array Returns an array with elements of the form ('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content') - */ - public function GetCourseDescriptions($secret_key, $course_id_field_name, $course_id_value) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); - if($course_id instanceof WSError) { - return $course_id; - } else { - // Course exists, get its descriptions - $descriptions = CourseDescription::get_descriptions($course_id); - $results = array(); - foreach($descriptions as $description) { - $results[] = array('course_desc_id' => $description->get_description_type(), - 'course_desc_title' => $description->get_title(), - 'course_desc_content' => $description->get_content()); - } - return $results; - } - } - } + /** + * Unsusbscribe user from course + * + * @param string API secret key + * @param string Course id field name. Use "chamilo_course_id" to use internal id + * @param string Course id value. + * @param string User id field name. Use "chamilo_user_id" to use internal id + * @param string User id value + */ + public function UnsubscribeUserFromCourse( + $secret_key, + $course_id_field_name, + $course_id_value, + $user_id_field_name, + $user_id_value + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->changeUserSubscription( + $course_id_field_name, + $course_id_value, + $user_id_field_name, + $user_id_value, + 0 + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } + /** + * Returns the descriptions of a course, along with their id + * + * @param string API secret key + * @param string Course id field name + * @param string Course id value + * @return array Returns an array with elements of the form ('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content') + */ + public function GetCourseDescriptions( + $secret_key, + $course_id_field_name, + $course_id_value + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); + if ($course_id instanceof WSError) { + return $course_id; + } else { + // Course exists, get its descriptions + $descriptions = CourseDescription::get_descriptions($course_id); + $results = array(); + foreach ($descriptions as $description) { + $results[] = array( + 'course_desc_id' => $description->get_description_type( + ), + 'course_desc_title' => $description->get_title(), + 'course_desc_content' => $description->get_content(), + ); + } - /** - * Edit course description - * - * @param string API secret key - * @param string Course id field name - * @param string Course id value - * @param int Category id from course description - * @param string Description title - * @param string Course description content - */ - public function EditCourseDescription($secret_key, $course_id_field_name, $course_id_value, $course_desc_id, $course_desc_title, $course_desc_content) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); - if($course_id instanceof WSError) { - return $course_id; - } else { - // Create the new course description - $cd = new CourseDescription(); - $cd->set_description_type($course_desc_id); - $cd->set_title($course_desc_title); - $cd->set_content($course_desc_content); - $cd->set_session_id(0); - // Get course info - $course_info = CourseManager::get_course_information(CourseManager::get_course_code_from_course_id($course_id)); + return $results; + } + } + } + + + /** + * Edit course description + * + * @param string API secret key + * @param string Course id field name + * @param string Course id value + * @param int Category id from course description + * @param string Description title + * @param string Course description content + */ + public function EditCourseDescription( + $secret_key, + $course_id_field_name, + $course_id_value, + $course_desc_id, + $course_desc_title, + $course_desc_content + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); + if ($course_id instanceof WSError) { + return $course_id; + } else { + // Create the new course description + $cd = new CourseDescription(); + $cd->set_description_type($course_desc_id); + $cd->set_title($course_desc_title); + $cd->set_content($course_desc_content); + $cd->set_session_id(0); + // Get course info + $course_info = CourseManager::get_course_information( + CourseManager::get_course_code_from_course_id($course_id) + ); $cd->set_course_id($course_info['real_id']); - // Check if this course description exists - $descriptions = CourseDescription::get_descriptions($course_id); - $exists = false; - foreach($descriptions as $description) { - if($description->get_description_type() == $course_desc_id) { - $exists = true; - } - } - if (!$exists) { - $cd->set_progress(0); - $cd->insert(); - } else { - $cd->update(); - } - } - } - } + // Check if this course description exists + $descriptions = CourseDescription::get_descriptions($course_id); + $exists = false; + foreach ($descriptions as $description) { + if ($description->get_description_type( + ) == $course_desc_id + ) { + $exists = true; + } + } + if (!$exists) { + $cd->set_progress(0); + $cd->insert(); + } else { + $cd->update(); + } + } + } + } } diff --git a/main/webservices/webservice_report.php b/main/webservices/webservice_report.php index d03dff9789..b13f225701 100755 --- a/main/webservices/webservice_report.php +++ b/main/webservices/webservice_report.php @@ -9,46 +9,59 @@ require_once __DIR__.'/webservice.php'; /** * Web services available for the User module. This class extends the WS class */ -class WSReport extends WS { +class WSReport extends WS +{ - /** - * Gets the time spent on the platform by a given user - * - * @param string User id field name - * @param string User id value + /** + * Gets the time spent on the platform by a given user + * + * @param string User id field name + * @param string User id value * @return array Array of results - */ - public function GetTimeSpentOnPlatform($user_id_field_name, $user_id_value) { - $user_id = $this->getUserId($user_id_field_name, $user_id_value); - if ($user_id instanceof WSError) { - return $user_id; - } else { + */ + public function GetTimeSpentOnPlatform($user_id_field_name, $user_id_value) + { + $user_id = $this->getUserId($user_id_field_name, $user_id_value); + if ($user_id instanceof WSError) { + return $user_id; + } else { return Tracking::get_time_spent_on_the_platform($user_id); - } - } + } + } - /** + /** * Gets the time spent in a course by a given user - * + * * @param string User id field name * @param string User id value * @param string Course id field name * @param string Course id value - * @return array Array of results - */ - public function GetTimeSpentOnCourse($user_id_field_name, $user_id_value, $course_id_field_name, $course_id_value) { + * @return array Array of results + */ + public function GetTimeSpentOnCourse( + $user_id_field_name, + $user_id_value, + $course_id_field_name, + $course_id_value + ) { $user_id = $this->getUserId($user_id_field_name, $user_id_value); if ($user_id instanceof WSError) { return $user_id; } - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); if ($course_id instanceof WSError) { return $course_id; } else { - $course_code = CourseManager::get_course_code_from_course_id($course_id); + $course_code = CourseManager::get_course_code_from_course_id( + $course_id + ); } + return Tracking::get_time_spent_on_the_course($user_id, $course_id); - } + } /** * Gets the time spent in a course by a given user @@ -59,23 +72,44 @@ class WSReport extends WS { * @param string Course id value * @return array Array of results */ - public function GetTimeSpentOnCourseInSession($user_id_field_name, $user_id_value, $course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value) { + public function GetTimeSpentOnCourseInSession( + $user_id_field_name, + $user_id_value, + $course_id_field_name, + $course_id_value, + $session_id_field_name, + $session_id_value + ) { $user_id = $this->getUserId($user_id_field_name, $user_id_value); if ($user_id instanceof WSError) { return $user_id; } - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); if ($course_id instanceof WSError) { return $course_id; } else { - $course_code = CourseManager::get_course_code_from_course_id($course_id); + $course_code = CourseManager::get_course_code_from_course_id( + $course_id + ); } - $session_id = $this->getSessionId($session_id_field_name, $session_id_value); + $session_id = $this->getSessionId( + $session_id_field_name, + $session_id_value + ); if ($session_id instanceof WSError) { return $session_id; } - return Tracking::get_time_spent_on_the_course($user_id, $course_id, $session_id); + + return Tracking::get_time_spent_on_the_course( + $user_id, + $course_id, + $session_id + ); } + /** * Gets a list of learning paths by course * @@ -85,26 +119,39 @@ class WSReport extends WS { * @param string Course id value * @return array Array of id=>title of learning paths */ - public function GetLearnpathsByCourse($secret_key, $user_id_field_name, $user_id_value, $course_id_field_name, $course_id_value) { + public function GetLearnpathsByCourse( + $secret_key, + $user_id_field_name, + $user_id_value, + $course_id_field_name, + $course_id_value + ) { $user_id = $this->getUserId($user_id_field_name, $user_id_value); if ($user_id instanceof WSError) { return $user_id; } - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); if ($course_id instanceof WSError) { return $course_id; } else { - $course_code = CourseManager::get_course_code_from_course_id($course_id); + $course_code = CourseManager::get_course_code_from_course_id( + $course_id + ); } $lp = new LearnpathList($user_id, $course_code); $list = $lp->list; $return = array(); foreach ($list as $id => $item) { - $return[] = array('id'=>$id, 'title' => $item['lp_name']); + $return[] = array('id' => $id, 'title' => $item['lp_name']); } + return $return; } + /** * Gets progress attained in the given learning path by the given user * @@ -115,22 +162,35 @@ class WSReport extends WS { * @param string Learnpath ID * @return double Between 0 and 100 (% of progress) */ - public function GetLearnpathProgress($secret_key, $user_id_field_name, $user_id_value, $course_id_field_name, $course_id_value, $learnpath_id) { + public function GetLearnpathProgress( + $secret_key, + $user_id_field_name, + $user_id_value, + $course_id_field_name, + $course_id_value, + $learnpath_id + ) { $user_id = $this->getUserId($user_id_field_name, $user_id_value); if ($user_id instanceof WSError) { return $user_id; } - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); if ($course_id instanceof WSError) { return $course_id; } else { - $course_code = CourseManager::get_course_code_from_course_id($course_id); + $course_code = CourseManager::get_course_code_from_course_id( + $course_id + ); } $lp = new learnpath($course_code, $learnpath_id, $user_id); $return = array( - 'progress_bar_mode' => $lp->progress_bar_mode, - 'progress_db' => $lp->progress_db, + 'progress_bar_mode' => $lp->progress_bar_mode, + 'progress_db' => $lp->progress_db, ); + return $return; } @@ -148,20 +208,33 @@ class WSReport extends WS { * @param string Learnpath ID * @return string The last item's lesson_location value */ - public function GetLearnpathHighestLessonLocation($secret_key, $user_id_field_name, $user_id_value, $course_id_field_name, $course_id_value, $learnpath_id) { + public function GetLearnpathHighestLessonLocation( + $secret_key, + $user_id_field_name, + $user_id_value, + $course_id_field_name, + $course_id_value, + $learnpath_id + ) { $user_id = $this->getUserId($user_id_field_name, $user_id_value); if ($user_id instanceof WSError) { return $user_id; } - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); if ($course_id instanceof WSError) { return $course_id; } else { - $course_code = CourseManager::get_course_code_from_course_id($course_id); + $course_code = CourseManager::get_course_code_from_course_id( + $course_id + ); } $lp = new learnpath($course_code, $learnpath_id, $user_id); $item = $lp->last_item_seen; $return = $lp->items[$item]->get_lesson_location(); + return $return; } @@ -177,27 +250,42 @@ class WSReport extends WS { * @param int Learnpath *ITEM* ID * @return double Generally between 0 and 100 */ - public function GetLearnpathScoreSingleItem($secret_key, $user_id_field_name, $user_id_value, $course_id_field_name, $course_id_value, $learnpath_id, $learnpath_item_id) { + public function GetLearnpathScoreSingleItem( + $secret_key, + $user_id_field_name, + $user_id_value, + $course_id_field_name, + $course_id_value, + $learnpath_id, + $learnpath_item_id + ) { $user_id = $this->getUserId($user_id_field_name, $user_id_value); if ($user_id instanceof WSError) { return $user_id; } - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); if ($course_id instanceof WSError) { return $course_id; } else { - $course_code = CourseManager::get_course_code_from_course_id($course_id); + $course_code = CourseManager::get_course_code_from_course_id( + $course_id + ); } $lp = new learnpath($course_code, $learnpath_id, $user_id); $return = array( - 'min_score' => $lp->items[$learnpath_item_id]->min_score, - 'max_score' => $lp->items[$learnpath_item_id]->max_score, - 'mastery_score' => $lp->items[$learnpath_item_id]->mastery_score, - 'current_score' => $lp->items[$learnpath_item_id]->current_score, + 'min_score' => $lp->items[$learnpath_item_id]->min_score, + 'max_score' => $lp->items[$learnpath_item_id]->max_score, + 'mastery_score' => $lp->items[$learnpath_item_id]->mastery_score, + 'current_score' => $lp->items[$learnpath_item_id]->current_score, ); + return $return; } + /** * Gets status obtained in the given learning path by the given user, * assuming there is only one item (SCO) in the learning path @@ -211,7 +299,15 @@ class WSReport extends WS { * @param int Learnpath *ITEM* ID * @return string "not attempted", "passed", "completed", "failed", "incomplete" */ - public function GetLearnpathStatusSingleItem($secret_key, $user_id_field_name, $user_id_value, $course_id_field_name, $course_id_value, $learnpath_id, $learnpath_item_id) { + public function GetLearnpathStatusSingleItem( + $secret_key, + $user_id_field_name, + $user_id_value, + $course_id_field_name, + $course_id_value, + $learnpath_id, + $learnpath_item_id + ) { $verifKey = $this->verifyKey($secret_key); if ($verifKey instanceof WSError) { $this->handleError($verifKey); @@ -220,19 +316,26 @@ class WSReport extends WS { if ($user_id instanceof WSError) { return $user_id; } - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); if ($course_id instanceof WSError) { return $course_id; } else { - $course_code = CourseManager::get_course_code_from_course_id($course_id); + $course_code = CourseManager::get_course_code_from_course_id( + $course_id + ); } $lp = new learnpath($course_code, $learnpath_id, $user_id); + return $lp->items[$learnpath_item_id]->status; } } - public function test() { + public function test() + { return 'Hello world!'; } } diff --git a/main/webservices/webservice_session.php b/main/webservices/webservice_session.php index b873c6df7e..127e1ed894 100755 --- a/main/webservices/webservice_session.php +++ b/main/webservices/webservice_session.php @@ -12,178 +12,225 @@ require_once __DIR__.'/webservice.php'; */ class WSSession extends WS { + /** + * Creates a session (helper method) + * + * @param string Name of the session + * @param string Start date, use the 'YYYY-MM-DD' format + * @param string End date, use the 'YYYY-MM-DD' format + * @param int Access delays of the coach (days before) + * @param int Access delays of the coach (days after) + * @param int Nolimit (0 = no limit of time, 1 = limit of time) + * @param int Visibility + * @param string User id field name for the coach + * @param string User id value for the coach + * @param string Original session id field name (use "chamilo_session_id" to use internal id) + * @param string Original session id value + * @param array Array of extra fields + * @return mixed Generated id in case of success, WSError otherwise + */ + protected function createSessionHelper( + $name, + $start_date, + $end_date, + $nb_days_access_before, + $nb_days_access_after, + $nolimit, + $visibility, + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + $extras + ) { + // Verify that coach exists and get its id + $user_id = $this->getUserId($user_id_field_name, $user_id_value); + if ($user_id instanceof WSError) { + return $user_id; + } - /** - * Creates a session (helper method) - * - * @param string Name of the session - * @param string Start date, use the 'YYYY-MM-DD' format - * @param string End date, use the 'YYYY-MM-DD' format - * @param int Access delays of the coach (days before) - * @param int Access delays of the coach (days after) - * @param int Nolimit (0 = no limit of time, 1 = limit of time) - * @param int Visibility - * @param string User id field name for the coach - * @param string User id value for the coach - * @param string Original session id field name (use "chamilo_session_id" to use internal id) - * @param string Original session id value - * @param array Array of extra fields - * @return mixed Generated id in case of success, WSError otherwise - */ - protected function createSessionHelper( - $name, - $start_date, - $end_date, - $nb_days_access_before, - $nb_days_access_after, - $nolimit, - $visibility, - $user_id_field_name, - $user_id_value, - $session_id_field_name, - $session_id_value, - $extras - ) { - // Verify that coach exists and get its id - $user_id = $this->getUserId($user_id_field_name, $user_id_value); - if ($user_id instanceof WSError) { - return $user_id; - } + $coachStartDate = null; + if (!empty($nb_days_access_before)) { + $day = intval($nb_days_access_before); + $coachStartDate = date( + 'Y-m-d ', + strtotime($start_date.' + '.$day.' days') + ); + } - $coachStartDate = null; - if (!empty($nb_days_access_before)) { - $day = intval($nb_days_access_before); - $coachStartDate = date('Y-m-d ', strtotime($start_date. ' + '.$day.' days')); - } + $coachEndDate = null; + if (!empty($nb_days_access_after)) { + $day = intval($nb_days_access_after); + $coachEndDate = date( + 'Y-m-d ', + strtotime($end_date.' + '.$day.' days') + ); + } - $coachEndDate = null; - if (!empty($nb_days_access_after)) { - $day = intval($nb_days_access_after); - $coachEndDate = date('Y-m-d ', strtotime($end_date. ' + '.$day.' days')); - } + // Try to create the session + $session_id = SessionManager::create_session( + $name, + $start_date, + $end_date, + $start_date, + $end_date, + $coachStartDate, + $coachEndDate, + $user_id, + 0, + $visibility + ); + if (!is_int($session_id)) { + return new WSError(301, 'Could not create the session'); + } else { + // Add the Original session id to the extra fields + $extras_associative = array(); + if ($session_id_field_name != "chamilo_session_id") { + $extras_associative[$session_id_field_name] = $session_id_value; + } + foreach ($extras as $extra) { + $extras_associative[$extra['field_name']] = $extra['field_value']; + } + // Create the extra fields + foreach ($extras_associative as $fname => $fvalue) { + SessionManager::create_session_extra_field($fname, 1, $fname); + SessionManager::update_session_extra_field_value( + $session_id, + $fname, + $fvalue + ); + } - // Try to create the session - $session_id = SessionManager::create_session( - $name, - $start_date, - $end_date, - $start_date, - $end_date, - $coachStartDate, - $coachEndDate, - $user_id, - 0, - $visibility - ); - if(!is_int($session_id)) { - return new WSError(301, 'Could not create the session'); - } else { - // Add the Original session id to the extra fields - $extras_associative = array(); - if($session_id_field_name != "chamilo_session_id") { - $extras_associative[$session_id_field_name] = $session_id_value; - } - foreach($extras as $extra) { - $extras_associative[$extra['field_name']] = $extra['field_value']; - } - // Create the extra fields - foreach($extras_associative as $fname => $fvalue) { - SessionManager::create_session_extra_field($fname, 1, $fname); - SessionManager::update_session_extra_field_value( - $session_id, - $fname, - $fvalue - ); - } - return $session_id; - } - } + return $session_id; + } + } - /** - * Creates a session - * - * @param string API secret key - * @param string Name of the session - * @param string Start date, use the 'YYYY-MM-DD' format - * @param string End date, use the 'YYYY-MM-DD' format - * @param int Access delays of the coach (days before) - * @param int Access delays of the coach (days after) - * @param int Nolimit (0 = no limit of time, 1 = limit of time) - * @param int Visibility - * @param string User id field name for the coach - * @param string User id value for the coach - * @param string Original session id field name (use "chamilo_session_id" to use internal id) - * @param string Original session id value - * @param array Array of extra fields - * @return int Session id generated - */ - public function CreateSession($secret_key, $name, $start_date, $end_date, $nb_days_access_before, $nb_days_access_after, $nolimit, $visibility, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $extras) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $session_id = $this->createSessionHelper($name, $start_date, $end_date, $nb_days_access_before, $nb_days_access_after, $nolimit, $visibility, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $extras); - if($session_id instanceof WSError) { - $this->handleError($session_id); - } else { - return $session_id; - } - } - } + /** + * Creates a session + * + * @param string API secret key + * @param string Name of the session + * @param string Start date, use the 'YYYY-MM-DD' format + * @param string End date, use the 'YYYY-MM-DD' format + * @param int Access delays of the coach (days before) + * @param int Access delays of the coach (days after) + * @param int Nolimit (0 = no limit of time, 1 = limit of time) + * @param int Visibility + * @param string User id field name for the coach + * @param string User id value for the coach + * @param string Original session id field name (use "chamilo_session_id" to use internal id) + * @param string Original session id value + * @param array Array of extra fields + * @return int Session id generated + */ + public function CreateSession( + $secret_key, + $name, + $start_date, + $end_date, + $nb_days_access_before, + $nb_days_access_after, + $nolimit, + $visibility, + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + $extras + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $session_id = $this->createSessionHelper( + $name, + $start_date, + $end_date, + $nb_days_access_before, + $nb_days_access_after, + $nolimit, + $visibility, + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + $extras + ); + if ($session_id instanceof WSError) { + $this->handleError($session_id); + } else { + return $session_id; + } + } + } + + /** + * Deletes a session (helper method) + * + * @param string Session id field name + * @param string Session id value + * @return mixed True in case of success, WSError otherwise + */ + protected function deleteSessionHelper( + $session_id_field_name, + $session_id_value + ) { + $session_id = $this->getSessionId( + $session_id_field_name, + $session_id_value + ); + if ($session_id instanceof WSError) { + return $session_id; + } else { + SessionManager::delete($session_id, true); - /** - * Deletes a session (helper method) - * - * @param string Session id field name - * @param string Session id value - * @return mixed True in case of success, WSError otherwise - */ - protected function deleteSessionHelper($session_id_field_name, $session_id_value) { - $session_id = $this->getSessionId($session_id_field_name, $session_id_value); - if($session_id instanceof WSError) { - return $session_id; - } else { - SessionManager::delete($session_id, true); - return true; - } - } + return true; + } + } - /** - * Deletes a session - * - * @param string API secret key - * @param string Session id field name - * @param string Session id value - */ - public function DeleteSession($secret_key, $session_id_field_name, $session_id_value) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->deleteSessionHelper($session_id_field_name, $session_id_value); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + /** + * Deletes a session + * + * @param string API secret key + * @param string Session id field name + * @param string Session id value + */ + public function DeleteSession( + $secret_key, + $session_id_field_name, + $session_id_value + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->deleteSessionHelper( + $session_id_field_name, + $session_id_value + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } - /** - * Edits a session (helper method) - * - * @param string Name of the session - * @param string Start date, use the 'YYYY-MM-DD' format - * @param string End date, use the 'YYYY-MM-DD' format - * @param int Access delays of the coach (days before) - * @param int Access delays of the coach (days after) - * @param int Nolimit (0 = no limit of time, 1 = limit of time) - * @param int Visibility - * @param string User id field name for the coach - * @param string User id value for the coach - * @param string Original session id field name (use "chamilo_session_id" to use internal id) - * @param string Original session id value - * @param array Array of extra fields - * @return mixed True on success, WSError otherwise - */ + /** + * Edits a session (helper method) + * + * @param string Name of the session + * @param string Start date, use the 'YYYY-MM-DD' format + * @param string End date, use the 'YYYY-MM-DD' format + * @param int Access delays of the coach (days before) + * @param int Access delays of the coach (days after) + * @param int Nolimit (0 = no limit of time, 1 = limit of time) + * @param int Visibility + * @param string User id field name for the coach + * @param string User id value for the coach + * @param string Original session id field name (use "chamilo_session_id" to use internal id) + * @param string Original session id value + * @param array Array of extra fields + * @return mixed True on success, WSError otherwise + */ protected function editSessionHelper( $name, $start_date, @@ -198,26 +245,35 @@ class WSSession extends WS $session_id_value, $extras ) { - $session_id = $this->getSessionId($session_id_field_name, $session_id_value); - if($session_id instanceof WSError) { - return $session_id; - } else { - // Verify that coach exists and get its id - $user_id = $this->getUserId($user_id_field_name, $user_id_value); - if ($user_id instanceof WSError) { - return $user_id; - } + $session_id = $this->getSessionId( + $session_id_field_name, + $session_id_value + ); + if ($session_id instanceof WSError) { + return $session_id; + } else { + // Verify that coach exists and get its id + $user_id = $this->getUserId($user_id_field_name, $user_id_value); + if ($user_id instanceof WSError) { + return $user_id; + } $coachStartDate = null; if (!empty($nb_days_access_before)) { $day = intval($nb_days_access_before); - $coachStartDate = date('Y-m-d ', strtotime($start_date. ' + '.$day.' days')); + $coachStartDate = date( + 'Y-m-d ', + strtotime($start_date.' + '.$day.' days') + ); } $coachEndDate = null; if (!empty($nb_days_access_after)) { $day = intval($nb_days_access_after); - $coachEndDate = date('Y-m-d ', strtotime($end_date. ' + '.$day.' days')); + $coachEndDate = date( + 'Y-m-d ', + strtotime($end_date.' + '.$day.' days') + ); } $result_id = SessionManager::edit_session( @@ -233,128 +289,207 @@ class WSSession extends WS 0, (int)$visibility ); - if(!is_int($result_id)) { - return new WSError(302, 'Could not edit the session'); - } else { - if(!empty($extras)) { - $extras_associative = array(); - foreach($extras as $extra) { - $extras_associative[$extra['field_name']] = $extra['field_value']; - } - // Create the extra fields - foreach($extras_associative as $fname => $fvalue) { - SessionManager::create_session_extra_field($fname, 1, $fname); - SessionManager::update_session_extra_field_value($session_id, $fname, $fvalue); - } - } - return true; - } - } - } + if (!is_int($result_id)) { + return new WSError(302, 'Could not edit the session'); + } else { + if (!empty($extras)) { + $extras_associative = array(); + foreach ($extras as $extra) { + $extras_associative[$extra['field_name']] = $extra['field_value']; + } + // Create the extra fields + foreach ($extras_associative as $fname => $fvalue) { + SessionManager::create_session_extra_field( + $fname, + 1, + $fname + ); + SessionManager::update_session_extra_field_value( + $session_id, + $fname, + $fvalue + ); + } + } - /** - * Edits a session - * - * @param string API secret key - * @param string Name of the session - * @param string Start date, use the 'YYYY-MM-DD' format - * @param string End date, use the 'YYYY-MM-DD' format - * @param int Access delays of the coach (days before) - * @param int Access delays of the coach (days after) - * @param int Nolimit (0 = no limit of time, 1 = limit of time) - * @param int Visibility - * @param string User id field name for the coach - * @param string User id value for the coach - * @param string Original session id field name (use "chamilo_session_id" to use internal id) - * @param string Original session id value - * @param array Array of extra fields - */ - public function EditSession($secret_key, $name, $start_date, $end_date, $nb_days_access_before, $nb_days_access_after, $nolimit, $visibility, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $extras) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->editSessionHelper($name, $start_date, $end_date, $nb_days_access_before, $nb_days_access_after, $nolimit, $visibility, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $extras); - if($session_id_value instanceof WSError) { - $this->handleError($result); - } - } - } + return true; + } + } + } - /** - * Change user subscription (helper method) - * - * @param string User id field name - * @param string User id value - * @param string Session id field name - * @param string Session id value - * @param int State (1 to subscribe, 0 to unsubscribe) - * @return mixed True on success, WSError otherwise - */ - protected function changeUserSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $state) { - $session_id = $this->getSessionId($session_id_field_name, $session_id_value); - if($session_id instanceof WSError) { - return $session_id; - } else { - $user_id = $this->getUserId($user_id_field_name, $user_id_value); - if($user_id instanceof WSError) { - return $user_id; - } else { - if($state == 1) { - SessionManager::subscribe_users_to_session($session_id, array($user_id)); - } else { - $result = SessionManager::unsubscribe_user_from_session($session_id, $user_id); - if (!$result) { - return new WSError(303, 'There was an error unsubscribing this user from the session'); - } - } - return true; - } - } - } + /** + * Edits a session + * + * @param string API secret key + * @param string Name of the session + * @param string Start date, use the 'YYYY-MM-DD' format + * @param string End date, use the 'YYYY-MM-DD' format + * @param int Access delays of the coach (days before) + * @param int Access delays of the coach (days after) + * @param int Nolimit (0 = no limit of time, 1 = limit of time) + * @param int Visibility + * @param string User id field name for the coach + * @param string User id value for the coach + * @param string Original session id field name (use "chamilo_session_id" to use internal id) + * @param string Original session id value + * @param array Array of extra fields + */ + public function EditSession( + $secret_key, + $name, + $start_date, + $end_date, + $nb_days_access_before, + $nb_days_access_after, + $nolimit, + $visibility, + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + $extras + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->editSessionHelper( + $name, + $start_date, + $end_date, + $nb_days_access_before, + $nb_days_access_after, + $nolimit, + $visibility, + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + $extras + ); + if ($session_id_value instanceof WSError) { + $this->handleError($result); + } + } + } - /** - * Subscribe user to a session - * - * @param string API secret key - * @param string User id field name - * @param string User id value - * @param string Session id field name - * @param string Session id value - */ - public function SubscribeUserToSession($secret_key, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->changeUserSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, 1); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + /** + * Change user subscription (helper method) + * + * @param string User id field name + * @param string User id value + * @param string Session id field name + * @param string Session id value + * @param int State (1 to subscribe, 0 to unsubscribe) + * @return mixed True on success, WSError otherwise + */ + protected function changeUserSubscription( + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + $state + ) { + $session_id = $this->getSessionId( + $session_id_field_name, + $session_id_value + ); + if ($session_id instanceof WSError) { + return $session_id; + } else { + $user_id = $this->getUserId($user_id_field_name, $user_id_value); + if ($user_id instanceof WSError) { + return $user_id; + } else { + if ($state == 1) { + SessionManager::subscribe_users_to_session( + $session_id, + array($user_id) + ); + } else { + $result = SessionManager::unsubscribe_user_from_session( + $session_id, + $user_id + ); + if (!$result) { + return new WSError( + 303, + 'There was an error unsubscribing this user from the session' + ); + } + } + + return true; + } + } + } + + /** + * Subscribe user to a session + * + * @param string API secret key + * @param string User id field name + * @param string User id value + * @param string Session id field name + * @param string Session id value + */ + public function SubscribeUserToSession( + $secret_key, + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->changeUserSubscription( + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + 1 + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } + + /** + * Subscribe user to a session + * + * @param string API secret key + * @param string User id field name + * @param string User id value + * @param string Session id field name + * @param string Session id value + */ + public function UnsubscribeUserFromSession( + $secret_key, + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->changeUserSubscription( + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + 0 + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } - /** - * Subscribe user to a session - * - * @param string API secret key - * @param string User id field name - * @param string User id value - * @param string Session id field name - * @param string Session id value - */ - public function UnsubscribeUserFromSession($secret_key, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->changeUserSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, 0); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } - /** * Change Teacher subscription (helper method) * @@ -367,9 +502,19 @@ class WSSession extends WS * @param int State (1 to subscribe, 0 to unsubscribe) * @return mixed True on success, WSError otherwise */ - protected function changeTeacherSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $course_id_field_name, $course_id_value, $state) - { - $session_id = $this->getSessionId($session_id_field_name, $session_id_value); + protected function changeTeacherSubscription( + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + $course_id_field_name, + $course_id_value, + $state + ) { + $session_id = $this->getSessionId( + $session_id_field_name, + $session_id_value + ); if ($session_id instanceof WSError) { return $session_id; } else { @@ -377,19 +522,34 @@ class WSSession extends WS if ($user_id instanceof WSError) { return $user_id; } else { - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); if ($course_id instanceof WSError) { return $course_id; } else { if ($state == 1) { - SessionManager::set_coach_to_course_session($user_id, $session_id, $course_id); + SessionManager::set_coach_to_course_session( + $user_id, + $session_id, + $course_id + ); } else { - $user_id = array (0 => $user_id); - $result = SessionManager::removeUsersFromCourseSession($user_id, $session_id, $course_id); + $user_id = array(0 => $user_id); + $result = SessionManager::removeUsersFromCourseSession( + $user_id, + $session_id, + $course_id + ); if (!$result) { - return new WSError(303, 'There was an error unsubscribing this Teacher from the session'); + return new WSError( + 303, + 'There was an error unsubscribing this Teacher from the session' + ); } } + return true; } } @@ -407,13 +567,28 @@ class WSSession extends WS * @param string Course id field name * @param string Course id value */ - public function SubscribeTeacherToSessionCourse($secret_key, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $course_id_field_name, $course_id_value) - { + public function SubscribeTeacherToSessionCourse( + $secret_key, + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + $course_id_field_name, + $course_id_value + ) { $verifKey = $this->verifyKey($secret_key); if ($verifKey instanceof WSError) { $this->handleError($verifKey); } else { - $result = $this->changeUserSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $course_id_field_name, $course_id_value, 1); + $result = $this->changeUserSubscription( + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + $course_id_field_name, + $course_id_value, + 1 + ); if ($result instanceof WSError) { $this->handleError($result); } @@ -431,13 +606,28 @@ class WSSession extends WS * @param string Course id field name * @param string Course id value */ - public function UnsubscribeTeacherFromSessionCourse($secret_key, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $course_id_field_name, $course_id_value) - { + public function UnsubscribeTeacherFromSessionCourse( + $secret_key, + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + $course_id_field_name, + $course_id_value + ) { $verifKey = $this->verifyKey($secret_key); if ($verifKey instanceof WSError) { $this->handleError($verifKey); } else { - $result = $this->changeUserSubscription($user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $course_id_field_name, $course_id_value, 0); + $result = $this->changeUserSubscription( + $user_id_field_name, + $user_id_value, + $session_id_field_name, + $session_id_value, + $course_id_field_name, + $course_id_value, + 0 + ); if ($result instanceof WSError) { $this->handleError($result); } @@ -445,79 +635,125 @@ class WSSession extends WS } /** - * Change course subscription - * - * @param string Course id field name - * @param string Course id value - * @param string Session id field name - * @param string Session id value - * @param int State (1 to subscribe, 0 to unsubscribe) - * @return mixed True on success, WSError otherwise - */ - protected function changeCourseSubscription($course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value, $state) { - $session_id = $this->getSessionId($session_id_field_name, $session_id_value); - if($session_id instanceof WSError) { - return $session_id; - } else { - $course_id = $this->getCourseId($course_id_field_name, $course_id_value); - if($course_id instanceof WSError) { - return $course_id; - } else { - if($state == 1) { - SessionManager::add_courses_to_session($session_id, array($course_id)); - return true; - } else { - $result = SessionManager::unsubscribe_course_from_session($session_id, $course_id); - if ($result) { - return true; - } else { - return new WSError(304, 'Error unsubscribing course from session'); - } - } - } - } + * Change course subscription + * + * @param string Course id field name + * @param string Course id value + * @param string Session id field name + * @param string Session id value + * @param int State (1 to subscribe, 0 to unsubscribe) + * @return mixed True on success, WSError otherwise + */ + protected function changeCourseSubscription( + $course_id_field_name, + $course_id_value, + $session_id_field_name, + $session_id_value, + $state + ) { + $session_id = $this->getSessionId( + $session_id_field_name, + $session_id_value + ); + if ($session_id instanceof WSError) { + return $session_id; + } else { + $course_id = $this->getCourseId( + $course_id_field_name, + $course_id_value + ); + if ($course_id instanceof WSError) { + return $course_id; + } else { + if ($state == 1) { + SessionManager::add_courses_to_session( + $session_id, + array($course_id) + ); + + return true; + } else { + $result = SessionManager::unsubscribe_course_from_session( + $session_id, + $course_id + ); + if ($result) { + return true; + } else { + return new WSError( + 304, + 'Error unsubscribing course from session' + ); + } + } + } + } } - /** - * Subscribe course to session - * - * @param string API secret key - * @param string Course id field name - * @param string Course id value - * @param string Session id field name - * @param string Session id value - */ - public function SubscribeCourseToSession($secret_key, $course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->changeCourseSubscription($course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value, 1); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + /** + * Subscribe course to session + * + * @param string API secret key + * @param string Course id field name + * @param string Course id value + * @param string Session id field name + * @param string Session id value + */ + public function SubscribeCourseToSession( + $secret_key, + $course_id_field_name, + $course_id_value, + $session_id_field_name, + $session_id_value + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->changeCourseSubscription( + $course_id_field_name, + $course_id_value, + $session_id_field_name, + $session_id_value, + 1 + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } - /** - * Unsubscribe course from session - * - * @param string API secret key - * @param string Course id field name - * @param string Course id value - * @param string Session id field name - * @param string Session id value - */ - public function UnsubscribeCourseFromSession($secret_key, $course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->changeCourseSubscription($course_id_field_name, $course_id_value, $session_id_field_name, $session_id_value, 0); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + /** + * Unsubscribe course from session + * + * @param string API secret key + * @param string Course id field name + * @param string Course id value + * @param string Session id field name + * @param string Session id value + */ + public function UnsubscribeCourseFromSession( + $secret_key, + $course_id_field_name, + $course_id_value, + $session_id_field_name, + $session_id_value + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->changeCourseSubscription( + $course_id_field_name, + $course_id_value, + $session_id_field_name, + $session_id_value, + 0 + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } } diff --git a/main/webservices/webservice_user.php b/main/webservices/webservice_user.php index 5d3a9c7783..17519a0991 100755 --- a/main/webservices/webservice_user.php +++ b/main/webservices/webservice_user.php @@ -9,462 +9,625 @@ require_once __DIR__.'/webservice.php'; /** * Web services available for the User module. This class extends the WS class */ -class WSUser extends WS { +class WSUser extends WS +{ + /** + * Enables or disables a user + * + * @param string User id field name + * @param string User id value + * @param int Set to 1 to enable and to 0 to disable + */ + protected function changeUserActiveState( + $user_id_field_name, + $user_id_value, + $state + ) { + $user_id = $this->getUserId($user_id_field_name, $user_id_value); + if ($user_id instanceof WSError) { + return $user_id; + } else { + if ($state == 0) { + UserManager::disable($user_id); + } else { + if ($state == 1) { + UserManager::enable($user_id); + } + } + } + } - /** - * Enables or disables a user - * - * @param string User id field name - * @param string User id value - * @param int Set to 1 to enable and to 0 to disable - */ - protected function changeUserActiveState($user_id_field_name, $user_id_value, $state) { - $user_id = $this->getUserId($user_id_field_name, $user_id_value); - if($user_id instanceof WSError) { - return $user_id; - } else { - if($state == 0) { - UserManager::disable($user_id); - } else if($state == 1) { - UserManager::enable($user_id); - } - } - } + /** + * Enables or disables multiple users + * + * @param array Users + * @param int Set to 1 to enable and to 0 to disable + * @return array Array of results + */ + protected function changeUsersActiveState($users, $state) + { + $results = array(); + foreach ($users as $user) { + $result_tmp = array(); + $result_op = $this->changeUserActiveState( + $user['user_id_field_name'], + $user['user_id_value'], + $state + ); + $result_tmp['user_id_value'] = $user['user_id_value']; + if ($result_op instanceof WSError) { + // Return the error in the results + $result_tmp['result'] = $result_op->toArray(); + } else { + $result_tmp['result'] = $this->getSuccessfulResult(); + } + $results[] = $result_tmp; + } - /** - * Enables or disables multiple users - * - * @param array Users - * @param int Set to 1 to enable and to 0 to disable - * @return array Array of results - */ - protected function changeUsersActiveState($users, $state) { - $results = array(); - foreach($users as $user) { - $result_tmp = array(); - $result_op = $this->changeUserActiveState($user['user_id_field_name'], $user['user_id_value'], $state); - $result_tmp['user_id_value'] = $user['user_id_value']; - if($result_op instanceof WSError) { - // Return the error in the results - $result_tmp['result'] = $result_op->toArray(); - } else { - $result_tmp['result'] = $this->getSuccessfulResult(); - } - $results[] = $result_tmp; - } - return $results; - } + return $results; + } - /** - * Disables a user - * - * @param string API secret key - * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id - * @param string User id value - */ - public function DisableUser($secret_key, $user_id_field_name, $user_id_value) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - // Let the implementation handle it - $this->handleError($verifKey); - } else { - $result = $this->changeUserActiveState($user_id_field_name, $user_id_value, 0); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + /** + * Disables a user + * + * @param string API secret key + * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id + * @param string User id value + */ + public function DisableUser( + $secret_key, + $user_id_field_name, + $user_id_value + ) { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + // Let the implementation handle it + $this->handleError($verifKey); + } else { + $result = $this->changeUserActiveState( + $user_id_field_name, + $user_id_value, + 0 + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } - /** - * Disables multiple users - * - * @param string API secret key - * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') - * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different - * than 0, an error occured - */ - public function DisableUsers($secret_key, $users) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - // Let the implementation handle it - $this->handleError($verifKey); - } else { - return $this->changeUsersActiveState($users, 0); - } - } + /** + * Disables multiple users + * + * @param string API secret key + * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') + * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different + * than 0, an error occured + */ + public function DisableUsers($secret_key, $users) + { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + // Let the implementation handle it + $this->handleError($verifKey); + } else { + return $this->changeUsersActiveState($users, 0); + } + } - /** - * Enables a user - * - * @param string API secret key - * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id - * @param string User id value - */ - public function EnableUser($secret_key, $user_id_field_name, $user_id_value) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->changeUserActiveState($user_id_field_name, $user_id_value, 1); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + /** + * Enables a user + * + * @param string API secret key + * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id + * @param string User id value + */ + public function EnableUser($secret_key, $user_id_field_name, $user_id_value) + { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->changeUserActiveState( + $user_id_field_name, + $user_id_value, + 1 + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } - /** - * Enables multiple users - * - * @param string API secret key - * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') - * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different - * than 0, an error occured - */ - public function EnableUsers($secret_key, $users) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - // Let the implementation handle it - $this->handleError($verifKey); - } else { - return $this->changeUsersActiveState($users, 1); - } - } + /** + * Enables multiple users + * + * @param string API secret key + * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') + * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different + * than 0, an error occured + */ + public function EnableUsers($secret_key, $users) + { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + // Let the implementation handle it + $this->handleError($verifKey); + } else { + return $this->changeUsersActiveState($users, 1); + } + } - /** - * Deletes a user (helper method) - * - * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id - * @param string User id value - * @return mixed True if user was successfully deleted, WSError otherwise - */ - protected function deleteUserHelper($user_id_field_name, $user_id_value) { - $user_id = $this->getUserId($user_id_field_name, $user_id_value); - if($user_id instanceof WSError) { - return $user_id; - } else { - if(!UserManager::delete_user($user_id)) { - return new WSError(101, "There was a problem while deleting this user"); - } else { - return true; - } - } - } + /** + * Deletes a user (helper method) + * + * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id + * @param string User id value + * @return mixed True if user was successfully deleted, WSError otherwise + */ + protected function deleteUserHelper($user_id_field_name, $user_id_value) + { + $user_id = $this->getUserId($user_id_field_name, $user_id_value); + if ($user_id instanceof WSError) { + return $user_id; + } else { + if (!UserManager::delete_user($user_id)) { + return new WSError( + 101, + "There was a problem while deleting this user" + ); + } else { + return true; + } + } + } - /** - * Deletes a user - * - * @param string API secret key - * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id - * @param string User id value - */ - public function DeleteUser($secret_key, $user_id_field_name, $user_id_value) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->deleteUserHelper($user_id_field_name, $user_id_value); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + /** + * Deletes a user + * + * @param string API secret key + * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id + * @param string User id value + */ + public function DeleteUser($secret_key, $user_id_field_name, $user_id_value) + { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->deleteUserHelper( + $user_id_field_name, + $user_id_value + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } - /** - * Deletes multiple users - * - * @param string API secret key - * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') - * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different - * than 0, an error occured - */ - public function DeleteUsers($secret_key, $users) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $results = array(); - foreach($users as $user) { - $result_tmp = array(); - $result_op = $this->deleteUserHelper($user['user_id_field_name'], $user['user_id_value']); - $result_tmp['user_id_value'] = $user['user_id_value']; - if($result_op instanceof WSError) { - // Return the error in the results - $result_tmp['result'] = $result_op->toArray(); - } else { - $result_tmp['result'] = $this->getSuccessfulResult(); - } - $results[] = $result_tmp; - } - return $results; - } - } + /** + * Deletes multiple users + * + * @param string API secret key + * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') + * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different + * than 0, an error occured + */ + public function DeleteUsers($secret_key, $users) + { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $results = array(); + foreach ($users as $user) { + $result_tmp = array(); + $result_op = $this->deleteUserHelper( + $user['user_id_field_name'], + $user['user_id_value'] + ); + $result_tmp['user_id_value'] = $user['user_id_value']; + if ($result_op instanceof WSError) { + // Return the error in the results + $result_tmp['result'] = $result_op->toArray(); + } else { + $result_tmp['result'] = $this->getSuccessfulResult(); + } + $results[] = $result_tmp; + } - /** - * Creates a user (helper method) - * - * @param string User first name - * @param string User last name - * @param int User status - * @param string Login name - * @param string Password (encrypted or not) - * @param string Encrypt method. Leave blank if you are passing the password in clear text, set to the encrypt method used to encrypt the password otherwise. Remember - * to include the salt in the extra fields if you are encrypting the password - * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id - * @param string User id value. Leave blank if you are using the internal user_id - * @param int Visibility. - * @param string User email. - * @param string Language. - * @param string Phone. - * @param string Expiration date - * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). - * @return mixed New user id generated by the system, WSError otherwise - */ - protected function createUserHelper($firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility, $email, $language, $phone, $expiration_date, $extras = array()) { + return $results; + } + } - // Add the original user id field name and value to the extra fields if needed - $extras_associative = array(); - if($user_id_field_name != "chamilo_user_id") { - $extras_associative[$user_id_field_name] = $user_id_value; - } - if (!empty($extras)) { - foreach($extras as $extra) { - $extras_associative[$extra['field_name']] = $extra['field_value']; - } - } - $result = UserManager::create_user($firstname, $lastname, $status, $email, $login, $password, '', $language, $phone, '', PLATFORM_AUTH_SOURCE, $expiration_date, $visibility, 0, $extras_associative, $encrypt_method); - if (!$result) { + /** + * Creates a user (helper method) + * + * @param string User first name + * @param string User last name + * @param int User status + * @param string Login name + * @param string Password (encrypted or not) + * @param string Encrypt method. Leave blank if you are passing the password in clear text, set to the encrypt method used to encrypt the password otherwise. Remember + * to include the salt in the extra fields if you are encrypting the password + * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id + * @param string User id value. Leave blank if you are using the internal user_id + * @param int Visibility. + * @param string User email. + * @param string Language. + * @param string Phone. + * @param string Expiration date + * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). + * @return mixed New user id generated by the system, WSError otherwise + */ + protected function createUserHelper( + $firstname, + $lastname, + $status, + $login, + $password, + $encrypt_method, + $user_id_field_name, + $user_id_value, + $visibility, + $email, + $language, + $phone, + $expiration_date, + $extras = array() + ) { - return new WSError(104, 'There was an error creating the user'); + // Add the original user id field name and value to the extra fields if needed + $extras_associative = array(); + if ($user_id_field_name != "chamilo_user_id") { + $extras_associative[$user_id_field_name] = $user_id_value; + } + if (!empty($extras)) { + foreach ($extras as $extra) { + $extras_associative[$extra['field_name']] = $extra['field_value']; + } + } + $result = UserManager::create_user( + $firstname, + $lastname, + $status, + $email, + $login, + $password, + '', + $language, + $phone, + '', + PLATFORM_AUTH_SOURCE, + $expiration_date, + $visibility, + 0, + $extras_associative, + $encrypt_method + ); + if (!$result) { + return new WSError(104, 'There was an error creating the user'); - /*$failure = $api_failureList[0]; - if($failure == 'login-pass already taken') { - return new WSError(102, 'This username is already taken'); - } else if($failure == 'encrypt_method invalid') { - return new WSError(103, 'The encryption of the password is invalid'); - } else { - return new WSError(104, 'There was an error creating the user'); - }*/ - } else { - return $result; - } - } + /*$failure = $api_failureList[0]; + if($failure == 'login-pass already taken') { + return new WSError(102, 'This username is already taken'); + } else if($failure == 'encrypt_method invalid') { + return new WSError(103, 'The encryption of the password is invalid'); + } else { + return new WSError(104, 'There was an error creating the user'); + }*/ + } else { + return $result; + } + } - /** - * Creates a user - * - * @param string API secret key - * @param string User first name - * @param string User last name - * @param int User status - * @param string Login name - * @param string Password (encrypted or not) - * @param string Encrypt method. Leave blank if you are passing the password in clear text, set to the encrypt method used to encrypt the password otherwise. Remember - * to include the salt in the extra fields if you are encrypting the password - * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id - * @param string User id value. Leave blank if you are using the internal user_id - * @param int Visibility. Set by default to 1 - * @param string User email. Set by default to an empty string - * @param string Language. Set by default to english - * @param string Phone. Set by default to an empty string - * @param string Expiration date. Set to null by default - * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Set to an empty array by default - * @return int New user id generated by the system - */ - public function CreateUser($secret_key, $firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility = 1, $email = '', $language = 'english', $phone = '', $expiration_date = '0000-00-00 00:00:00', $extras = array()) { - // First, verify the secret key - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->createUserHelper($firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility, $email, $language, $phone, $expiration_date, $extras); - if($result instanceof WSError) { - $this->handleError($result); - } else { - return $result; - } - } - } + /** + * Creates a user + * + * @param string API secret key + * @param string User first name + * @param string User last name + * @param int User status + * @param string Login name + * @param string Password (encrypted or not) + * @param string Encrypt method. Leave blank if you are passing the password in clear text, set to the encrypt method used to encrypt the password otherwise. Remember + * to include the salt in the extra fields if you are encrypting the password + * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id + * @param string User id value. Leave blank if you are using the internal user_id + * @param int Visibility. Set by default to 1 + * @param string User email. Set by default to an empty string + * @param string Language. Set by default to english + * @param string Phone. Set by default to an empty string + * @param string Expiration date. Set to null by default + * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Set to an empty array by default + * @return int New user id generated by the system + */ + public function CreateUser( + $secret_key, + $firstname, + $lastname, + $status, + $login, + $password, + $encrypt_method, + $user_id_field_name, + $user_id_value, + $visibility = 1, + $email = '', + $language = 'english', + $phone = '', + $expiration_date = '0000-00-00 00:00:00', + $extras = array() + ) { + // First, verify the secret key + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $result = $this->createUserHelper( + $firstname, + $lastname, + $status, + $login, + $password, + $encrypt_method, + $user_id_field_name, + $user_id_value, + $visibility, + $email, + $language, + $phone, + $expiration_date, + $extras + ); + if ($result instanceof WSError) { + $this->handleError($result); + } else { + return $result; + } + } + } - /** - * Creates multiple users - * - * @param string API secret key - * @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method - * @return array Array with elements of the form array('user_id_value' => 'original value sent', 'user_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful')) - */ - public function CreateUsers($secret_key, $users) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $results = array(); - foreach($users as $user) { - $result_tmp = array(); + /** + * Creates multiple users + * + * @param string API secret key + * @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method + * @return array Array with elements of the form array('user_id_value' => 'original value sent', 'user_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful')) + */ + public function CreateUsers($secret_key, $users) + { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $results = array(); + foreach ($users as $user) { + $result_tmp = array(); // re-initialize variables just in case $firstname = $lastname = $status = $login = $password = $encrypt_method = $user_id_field_name = $user_id_value = $visibility = $email = $language = $phone = $expiration_date = $extras = null; - extract($user); - $result = $this->createUserHelper($firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility, $email, $language, $phone, $expiration_date, $extras); - if($result instanceof WSError) { - $result_tmp['result'] = $result->toArray(); - $result_tmp['user_id_value'] = $user_id_value; - $result_tmp['user_id_generated'] = 0; - } else { - $result_tmp['result'] = $this->getSuccessfulResult(); - $result_tmp['user_id_value'] = $user_id_value; - $result_tmp['user_id_generated'] = $result; - } - $results[] = $result_tmp; - } - return $results; - } - } + extract($user); + $result = $this->createUserHelper( + $firstname, + $lastname, + $status, + $login, + $password, + $encrypt_method, + $user_id_field_name, + $user_id_value, + $visibility, + $email, + $language, + $phone, + $expiration_date, + $extras + ); + if ($result instanceof WSError) { + $result_tmp['result'] = $result->toArray(); + $result_tmp['user_id_value'] = $user_id_value; + $result_tmp['user_id_generated'] = 0; + } else { + $result_tmp['result'] = $this->getSuccessfulResult(); + $result_tmp['user_id_value'] = $user_id_value; + $result_tmp['user_id_generated'] = $result; + } + $results[] = $result_tmp; + } - /** - * Edits user info (helper method) - * - * @param string User id field name. Use "chamilo_user_id" in order to use internal system id - * @param string User id value - * @param string First name - * @param string Last name - * @param int User status - * @param string Login name - * @param string Password. Leave blank if you don't want to update it - * @param string Encrypt method - * @param string User email - * @param string Language. Set by default to english - * @param string Phone. Set by default to an empty string - * @param string Expiration date. Set to null by default - * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update - * @return mixed True if user was successfully updated, WSError otherwise - */ - protected function editUserHelper( - $user_id_field_name, - $user_id_value, - $firstname, - $lastname, - $status, - $loginname, - $password, - $encrypt_method, - $email, - $language, - $phone, - $expiration_date, - $extras - ) { - $user_id = $this->getUserId($user_id_field_name, $user_id_value); - if($user_id instanceof WSError) { - return $user_id; - } else { - if($password == '') { - $password = null; - } - $user_info = api_get_user_info($user_id); - if (count($extras) == 0) { - $extras = null; - } + return $results; + } + } - $result = UserManager::update_user( - $user_id, - $firstname, - $lastname, - $loginname, - $password, - PLATFORM_AUTH_SOURCE, - $email, - $status, - '', - $phone, - $user_info['picture_uri'], - $expiration_date, - $user_info['active'], - null, - $user_info['hr_dept_id'], - $extras, - $encrypt_method - ); - if (!$result) { - /*if($failure == 'encrypt_method invalid') { - return new WSError(103, 'The encryption of the password is invalid'); - } else { - return new WSError(105, 'There was an error updating the user'); - }*/ - return new WSError(105, 'There was an error updating the user'); - } else { - return $result; - } - } - } + /** + * Edits user info (helper method) + * + * @param string User id field name. Use "chamilo_user_id" in order to use internal system id + * @param string User id value + * @param string First name + * @param string Last name + * @param int User status + * @param string Login name + * @param string Password. Leave blank if you don't want to update it + * @param string Encrypt method + * @param string User email + * @param string Language. Set by default to english + * @param string Phone. Set by default to an empty string + * @param string Expiration date. Set to null by default + * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update + * @return mixed True if user was successfully updated, WSError otherwise + */ + protected function editUserHelper( + $user_id_field_name, + $user_id_value, + $firstname, + $lastname, + $status, + $loginname, + $password, + $encrypt_method, + $email, + $language, + $phone, + $expiration_date, + $extras + ) { + $user_id = $this->getUserId($user_id_field_name, $user_id_value); + if ($user_id instanceof WSError) { + return $user_id; + } else { + if ($password == '') { + $password = null; + } + $user_info = api_get_user_info($user_id); + if (count($extras) == 0) { + $extras = null; + } - /** - * Edits user info - * - * @param string API secret key - * @param string User id field name. Use "chamilo_user_id" in order to use internal system id - * @param string User id value - * @param string First name - * @param string Last name - * @param int User status - * @param string Login name - * @param string Password. Leave blank if you don't want to update it - * @param string Encrypt method - * @param string User email - * @param string Language. Set by default to english - * @param string Phone. Set by default to an empty string - * @param string Expiration date. Set to null by default - * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update - */ - public function EditUser($secret_key, $user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras) { - // First, verify the secret key - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { + $result = UserManager::update_user( + $user_id, + $firstname, + $lastname, + $loginname, + $password, + PLATFORM_AUTH_SOURCE, + $email, + $status, + '', + $phone, + $user_info['picture_uri'], + $expiration_date, + $user_info['active'], + null, + $user_info['hr_dept_id'], + $extras, + $encrypt_method + ); + if (!$result) { + /*if($failure == 'encrypt_method invalid') { + return new WSError(103, 'The encryption of the password is invalid'); + } else { + return new WSError(105, 'There was an error updating the user'); + }*/ + return new WSError(105, 'There was an error updating the user'); + } else { + return $result; + } + } + } - $extras_associative = array(); - if (!empty($extras)) { - foreach($extras as $extra) { - $extras_associative[$extra['field_name']] = $extra['field_value']; - } - } + /** + * Edits user info + * + * @param string API secret key + * @param string User id field name. Use "chamilo_user_id" in order to use internal system id + * @param string User id value + * @param string First name + * @param string Last name + * @param int User status + * @param string Login name + * @param string Password. Leave blank if you don't want to update it + * @param string Encrypt method + * @param string User email + * @param string Language. Set by default to english + * @param string Phone. Set by default to an empty string + * @param string Expiration date. Set to null by default + * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update + */ + public function EditUser( + $secret_key, + $user_id_field_name, + $user_id_value, + $firstname, + $lastname, + $status, + $loginname, + $password, + $encrypt_method, + $email, + $language, + $phone, + $expiration_date, + $extras + ) { + // First, verify the secret key + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $extras_associative = array(); + if (!empty($extras)) { + foreach ($extras as $extra) { + $extras_associative[$extra['field_name']] = $extra['field_value']; + } + } - $result = $this->editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras_associative); - if($result instanceof WSError) { - $this->handleError($result); - } - } - } + $result = $this->editUserHelper( + $user_id_field_name, + $user_id_value, + $firstname, + $lastname, + $status, + $loginname, + $password, + $encrypt_method, + $email, + $language, + $phone, + $expiration_date, + $extras_associative + ); + if ($result instanceof WSError) { + $this->handleError($result); + } + } + } - /** - * Edits multiple users - * - * @param string API secret key - * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method - * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different - * than 0, an error occured - */ - public function EditUsers($secret_key, $users) { - $verifKey = $this->verifyKey($secret_key); - if($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $results = array(); - foreach($users as $user) { - $result_tmp = array(); + /** + * Edits multiple users + * + * @param string API secret key + * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method + * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different + * than 0, an error occured + */ + public function EditUsers($secret_key, $users) + { + $verifKey = $this->verifyKey($secret_key); + if ($verifKey instanceof WSError) { + $this->handleError($verifKey); + } else { + $results = array(); + foreach ($users as $user) { + $result_tmp = array(); // re-initialize variables just in case $user_id_field_name = $user_id_value = $firstname = $lastname = $status = $loginname = $password = $encrypt_method = $email = $language = $phone = $expiration_date = $extras = null; - extract($user); - $result_op = $this->editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras); - $result_tmp['user_id_value'] = $user['user_id_value']; - if($result_op instanceof WSError) { - // Return the error in the results - $result_tmp['result'] = $result_op->toArray(); - } else { - $result_tmp['result'] = $this->getSuccessfulResult(); - } - $results[] = $result_tmp; - } - return $results; - } - } + extract($user); + $result_op = $this->editUserHelper( + $user_id_field_name, + $user_id_value, + $firstname, + $lastname, + $status, + $loginname, + $password, + $encrypt_method, + $email, + $language, + $phone, + $expiration_date, + $extras + ); + $result_tmp['user_id_value'] = $user['user_id_value']; + if ($result_op instanceof WSError) { + // Return the error in the results + $result_tmp['result'] = $result_op->toArray(); + } else { + $result_tmp['result'] = $this->getSuccessfulResult(); + } + $results[] = $result_tmp; + } + + return $results; + } + } }