C#-XMLRPC

新增專案後,增加參考"ClickAP.XMLRPC"(為維護共同參加學校權益,請加入社團後下載)並建立一個 IMoodle.cs 內存放介面與結構。

建立使用者


IMoodle.cs
    using ClickAP.XMLRPC;
    using System.Reflection;

    public struct User
    {
        public string username;
        public string password;
        public string firstname;
        public string lastname;
        public string email;
        public string auth;
        public string idnumber;
        public string lang;
        public string description;
    }

    public interface IMoodle : IXmlRpcProxy
    {
        [XmlRpcMethod("moodleset_create_users")]
        object[] CreateUsers(User[] users);
    }

Program.cs
    using ClickAP.XMLRPC;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                IMoodle moodleProxy;
                moodleProxy = XmlRpcProxyGen.Create<IMoodle>();
                moodleProxy.Url = "http://localhost/mod314/webservice/xmlrpc/server.php?wsdl=1&wstoken=1a2222bbcccd33333333e5f6g7777h88";
                User[] users = new User[2];
                users[0].username = "jk001";
                users[0].firstname = "JK001";
                users[0].lastname = "ClickAP";
                users[0].password = "Xyz!1234Abc";
                users[0].auth = "manual";
                users[0].idnumber = "";
                users[0].lang = "zh_tw";
                users[0].email = "jk001@click-ap.com";
                users[0].description = "";

                users[1].username = "jk002";
                users[1].firstname = "JK002";
                users[1].lastname = "ClickAP";
                users[1].password = "Xyz!1234Abc";
                users[1].auth = "manual";
                users[1].idnumber = "";
                users[1].lang = "zh_tw";
                users[1].email = "jk002@click-ap.com";
                users[1].description = "";

                object[] b = moodleProxy.CreateUsers(users);
                Console.WriteLine("State name is {0}", b.Length);

            }
        }
    }


建立類別


IMoodle.cs
    public struct Category
    {
        public string name;
        public int parent;
        public string description;
    }
    
    public interface IMoodle : IXmlRpcProxy
    {
        // ...

        [XmlRpcMethod("moodleset_create_categories")]
        object[] CreateCategories(Category[] categories);

        // ...
    }


Program.cs
    static void Main(string[] args)
            {
                IMoodle moodleProxy;
                moodleProxy = XmlRpcProxyGen.Create<IMoodle>();
                //moodleProxy.Url = "localhost/mod314/webservice/xmlrpc/server.php?wsdl=1&wstoken=1a2222bbcccd33333333e5f6g7777h88";
                moodleProxy.Url = "localhost/webservice/xmlrpc/server.php?wsdl=1&wstoken=1a2222bbcccd33333333e5f6g7777h88";

                Category[] categories = new Category[2];
                categories[0].name = "Cate1";
                categories[0].parent = 0;
                categories[0].description = "";

                categories[1].name = "Cate2";
                categories[1].parent = 0;
                categories[1].description = "";


                object[] b = moodleProxy.CreateCategories(categories);
                Console.WriteLine("State name is {0}", b.Length);

            }


建立課程


IMoodle.cs
    public struct Course
    {
        public string shortname;
        public int categoryid;
        public string fullname;
        public string idnumber;
        public string summary;
        public int summaryformat;
        public string format;
        public int showgrades;
        public int newsitems;
        public int startdate;
        public int numsections;
        public int showreports;
        public int maxbytes;
        public int visible;
        public int hiddensections;
        public int groupmode;
        public int groupmodeforce;
        public int defaultgroupingid;
        public int enablecompletion;
        public int completionnotify;
        public string lang;
        public string forcetheme;

    }

    public interface IMoodle : IXmlRpcProxy
    {
        // ...

        [XmlRpcMethod("moodleset_create_courses")]
        object[] CreateCourses(Course[] courses);

        // ...
    }

Program.cs
    static void Main(string[] args)
            {
                IMoodle moodleProxy;
                moodleProxy = XmlRpcProxyGen.Create<IMoodle>();
                moodleProxy.Url = "http://loaclhost/webservice/xmlrpc/server.php?wsdl=1&wstoken=1a2222bbcccd33333333e5f6g7777h88";
                //moodleProxy.Url = "http://loaclhost/webservice/xmlrpc/server.php?wstoken=1a2222bbcccd33333333e5f6g7777h88";

               Course[] courses = new Course[1];
                courses[0].fullname = "Course3";
                courses[0].shortname = "Course3";
                courses[0].categoryid = 1;
                courses[0].idnumber = "";
                courses[0].format = "topics";
                courses[0].lang = "";
                courses[0].forcetheme = "";
                courses[0].summary = "";


                object[] b = moodleProxy.CreateCourses(courses);
                Console.WriteLine("State name is {0}", b.Length);

            }


選課


IMoodle.cs
public struct Enrolment
{
    public int roleid;
    public int courseid;
    public int userid;
    public int timestart;
    public int timeend;
    public int suspend;
}

public interface IMoodle : IXmlRpcProxy
{
    // ...

    [XmlRpcMethod("moodleset_enrol_users")]
    object[] Enrol(Enrolment[] enrolments);

    // ...
}

Program.cs
 static void Main(string[] args)
        {
            IMoodle moodleProxy;
            moodleProxy = XmlRpcProxyGen.Create<IMoodle>();
            moodleProxy.Url = "loaclhost/mod314/webservice/xmlrpc/server.php?wsdl=1&wstoken=1a2222bbcccd33333333e5f6g7777h88";

            Enrolment[] enrolment = new Enrolment[1];
            enrolment[0].roleid = 5;
            enrolment[0].courseid = 3;
            enrolment[0].userid = 5;
            enrolment[0].suspend = 0;

            try
            {
                moodleProxy.Enrol(enrolment);
            }
            catch (XmlRpcInvalidXmlRpcException)
            {
                
                throw;
                          }
   }


退選


IMoodle.cs
public struct UnEnrolment
{
    public int roleid;
    public int courseid;
    public int userid;
}
...
[XmlRpcMethod("moodleset_unenrol_users")]
    object[] UnEnrol(UnEnrolment[] enrolments);

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClickAP.XMLRPC;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IMoodle moodleProxy;
            moodleProxy = XmlRpcProxyGen.Create<IMoodle>();
            moodleProxy.Url = "localhost/mod314/webservice/xmlrpc/server.php?wstoken=1a2222bbcccd33333333e5f6g7777h88";
            //moodleProxy.Url = "localhost/webservice/xmlrpc/server.php?wstoken=1a2222bbcccd33333333e5f6g7777h88";

            UnEnrolment[] unenrolment = new UnEnrolment[1];
            unenrolment[0].roleid = 5;
            unenrolment[0].courseid = 3;
            unenrolment[0].userid = 5;

            try
            {
                moodleProxy.UnEnrol(unenrolment);
            }
            catch (XmlRpcInvalidXmlRpcException)
            {

                throw;
            } 
        }
    }
}

Comments