Hello, my plugin auto enrols students from a csv file.As it is now, it auto enrolls, no questions asked. But I want to add an extra step before the enrollment. I would like to only enroll checkboxed students. And those, would appear in a new table(on a new page), enrolled on the course, after clicking a button. Can a html table validate data as well? Here's my code:
$courseid= required_param('id', PARAM_INT);
$plugin=enrol_get_plugin('manual');
$instance = $DB->get_record('enrol', array('courseid'=>$courseid, 'enrol'=>'manual'), '*', MUST_EXIST);
$fp=fopen('sample.csv','r');
$row=1;
$enrolinfo='';
$totalnumber=0;
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('uploaduserspreview','local_ecoclipaluno'));
$table= new html_table();
$table->head =array(get_string('selection','local_ecoclipaluno'),get_string('username','local_ecoclipaluno'),get_string('firstname','local_ecoclipaluno'),get_string('lastname','local_ecoclipaluno'),get_string('number','local_ecoclipaluno'),get_string('email','local_ecoclipaluno'),get_string('auth','local_ecoclipaluno'),get_string('enrollment','local_ecoclipaluno'),'Curso');
$table->tablealign = 'center';
$table->attributes['class']='generaltable';
fgetcsv($fp,1000,";");
while(($data=fgetcsv($fp,1000,";"))!==FALSE){
$user=$DB->get_record('user',array('username'=>trim($data[0])));
if($user==FALSE){
$enrolinfo=get_string('usernotregistered','local_ecoclipaluno');
}
$totalnumber++;
if($user && !$user->deleted){
if(is_enrolled($context,$user)){
$enrolinfo=get_string('alreadyenrolled','local_ecoclipaluno');
//continue;
//echo $user->username."\r\n".get_string('alreadyenrolled','local_ecoclipaluno')."\n";
//$table->data[get_string('enrollment','local_ecoclipaluno')]=get_string('alreadyenrolled','local_ecoclipaluno')."\n";
}
else{
$timestart = time();
$timestart = make_timestamp(date('Y', $timestart), date('m', $timestart), date('d', $timestart), date('h',$timestart), 0,0);
if ($instance->enrolperiod) {
$timeend = $timestart + $instance->enrolperiod;
} else {
$timeend = 0;
}
$plugin->enrol_user($instance, $user->id, 5, $timestart, $timeend);
$enrolinfo=get_string('newenrollment','local_ecoclipaluno');
}
}
$table->data[]= array(html_writer::empty_tag('input',array('type'=>'checkbox','name' =>'xpto','checked'=>'checked')),$data[0],$user->firstname,$user->lastname,array_shift($dataClipNumber),$data[1],$user->auth,$enrolinfo,whatCourse(array_shift($dataClipCourse)));
}
fclose($fp);
echo html_writer::start_tag('form',array('action'=>'/somefile.php','method'=>'post'));
echo html_writer::table($table);
echo html_writer::end_tag('form');