create groups on courses by code

create groups on courses by code

by Andres Felipe Tamayo -
Number of replies: 6

Hi,

Here its my situation:

i am making a code who create groups, for that i am using the groups table:

"insert into di082_groups
                (id,courseid,name,description,enrolmentkey,descriptionformat) values (default,$arr[0],'$this->grupo','','',1)");

It works well it inserts the register. But when i go to the course and try to add an user on that group. it doesnt appears on the selection scream.

 

For example on the curse i created manually the group 2, and group 1 by code. and it only appears 2.

 

 

what other table do i have to write?? i hope some one can help me.

 

T

Average of ratings: -
In reply to Andres Felipe Tamayo

Re: create groups on courses by code

by Darko Miletić -

You should always try to use Moodle API for doing things. This is a working example:

 
require_once($CFG->dirroot.'/group/lib.php');

$data = new stdClass();
$data->courseid = idcourse;
$data->idnumber = someidnumber;
$data->name = 'group name';
$data->description = 'group description';
$data->descriptionformat = FORMAT_HTML;

$newgroupid = groups_create_group($data);
Average of ratings: Useful (2)
In reply to Darko Miletić

Re: create groups on courses by code

by Andres Felipe Tamayo -

HI, thanks for the help, i tried to do what you said:

                require_once('/ruteserverxxxx/moodle2/group/lib.php');
                
                $data = new stdClass();
                $data->courseid = $arr[0];
                $data->id = 'default';
                $data->name = $this->grupo;
                $data->description = 'group description';
                $data->descriptionformat = 'FORMAT_HTML';
                //$DB->insert_record('group', $data, false);
                echo "antes de: $this->grupo - $arr[0]<br>";
                $id=groups_create_group($data);

 

But it throws me this mistake:

Fatal error: Call to a member function get_record() on a non-object in /rutemyservermoodle/group/lib.php on line 223

what am i making wrong

 

In reply to Andres Felipe Tamayo

Re: create groups on courses by code

by Andres Felipe Tamayo -

SOme Help please!!!

In reply to Andres Felipe Tamayo

Re: create groups on courses by code

by Darko Miletić -

When writing a Moodle code you must start a page with this:

require('/some/path/to/moodle/config.php');

and than add the code I posted. Including config.php initializes Moodle enviroment and prepares all basic core API for use.

In reply to Darko Miletić

Re: create groups on courses by code

by Roger Mepham -

Hi Darko

Thanks for your code posting it has been very useful!

if possible could you please advise some sample code to add a user to a group with the function groups_add_member() from the group lib code.

Many thanks

Roger

In reply to Roger Mepham

Re: create groups on courses by code

by Jaswant Tak -

Hi Roger

Thats quite simple, you just have to pass group id and userid as parameters,


require_once($CFG->dirroot.'/group/lib.php');

groups_add_member($groupid, $userid);


Cheers

Jaswant