Пример #1
0
/**
 * process the mass enrolment
 * @param csv_import_reader $cir  an import reader created by caller
 * @param Object $course  a course record from table mdl_course
 * @param Object $context  course context instance
 * @param Object $data    data from a moodleform 
 * @return string  log of operations 
 */
function mass_enroll($cir, $course, $context, $data) {
    global $CFG,$DB;
    require_once ($CFG->dirroot . '/group/lib.php');

    $result = '';
    
    $courseid=$course->id;
    $roleid = $data->roleassign;
    $useridfield = $data->firstcolumn;

    $enrollablecount = 0;
    $createdgroupscount = 0;
    $createdgroupingscount = 0;
    $createdgroups = '';
    $createdgroupings = '';


    $plugin = enrol_get_plugin('manual');
    //Moodle 2.x enrolment and role assignment are different
    // make sure couse DO have a manual enrolment plugin instance in that course
    //that we are going to use (only one instance is allowed @see enrol/manual/lib.php get_new_instance)
    // thus call to get_record is safe 
    $instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
    if (empty($instance)) {
        // Only add an enrol instance to the course if non-existent
        $enrolid = $plugin->add_instance($course);
        $instance = $DB->get_record('enrol', array('id' => $enrolid));
    }
    
   
    // init csv import helper
    $cir->init();
    while ($fields = $cir->next()) {
        $a = new StdClass();

        if (empty ($fields))
            continue;

        // print_r($fields);
        // $enrollablecount++;
        // continue;
        
        // 1rst column = id Moodle (idnumber,username or email)    
        // get rid on eventual double quotes unfortunately not done by Moodle CSV importer 
            $fields[0]= str_replace('"', '', trim($fields[0]));
        
        if (!$user = $DB->get_record('user', array($useridfield => $fields[0]))) {
            $result .= '<div class="alert alert-error">'.get_string('im:user_unknown', 'local_mass_enroll', $fields[0] ). '</div>';
            continue;
        }
        if(!$DB->record_exists_sql("select id from {local_userdata} where costcenterid=$course->costcenter AND userid=$user->id")){
            $costcentername = $DB->get_field('local_costcenter','fullname',array('id'=>$course->costcenter));
            $cs_object = new stdClass();
            $cs_object->csname = $costcentername;
            $cs_object->user   = fullname($user);
            $result .= '<div class="alert alert-error">'.get_string('im:user_notcostcenter', 'local_mass_enroll',$cs_object ). '</div>';
            continue; 
        }
        //already enroled ?
        if (user_has_role_assignment($user->id, $roleid, $context->id)) {
            $result .= '<div class="alert alert-error">'.get_string('im:already_in', 'local_mass_enroll', fullname($user)). '</div>';

        } else {
            //TODO take care of timestart/timeend in course settings
            // done in rev 1.1
            $timestart = time();
            // remove time part from the timestamp and keep only the date part
            $timestart = make_timestamp(date('Y', $timestart), date('m', $timestart), date('d', $timestart), 0, 0, 0);
            if ($instance->enrolperiod) {
                $timeend = $timestart + $instance->enrolperiod;
            } else {
                $timeend = 0;
            }
            // not anymore so easy in Moodle 2.x
            // if (!role_assign($roleid, $user->id, null, $context->id, $timestart, $timeend, 0, 'flatfile')) {
            //    $result .= get_string('im:error_in', 'local_mass_enroll', fullname($user)) . "";
            //    continue;
            //}
            //
            // Enrol the user with this plugin instance (unfortunately return void, no more status )
            $plugin->enrol_user($instance, $user->id,$roleid,$timestart,$timeend);
            $result .= '<div class="alert alert-success">'.get_string('im:enrolled_ok', 'local_mass_enroll', fullname($user)).'</div>';
            $enrollablecount++;
        }

        $group = str_replace('"','',trim($fields[1]));
        // 2nd column ?
        if (empty ($group)) {
            $result .= "";
            continue; // no group for this one
        }

        // create group if needed
        if (!($gid = mass_enroll_group_exists($group, $courseid))) {
            if ($data->creategroups) {
                if (!($gid = mass_enroll_add_group($group, $courseid))) {
                    $a->group = $group;
                    $a->courseid = $courseid;
                    $result .= '<div class="alert alert-error">'.get_string('im:error_addg', 'local_mass_enroll', $a) . '</div>';
                    continue;
                }
                $createdgroupscount++;
                $createdgroups .= " $group";
            } else {
                $result .= '<div class="alert alert-error">'.get_string('im:error_g_unknown', 'local_mass_enroll', $group) . '</div>';
                continue;
            }
        }

        // if groupings are enabled on the site (should be ?)
        // if ($CFG->enablegroupings) { // not anymore in Moodle 2.x
        if (!($gpid = mass_enroll_grouping_exists($group, $courseid))) {
            if ($data->creategroupings) {
                if (!($gpid = mass_enroll_add_grouping($group, $courseid))) {
                    $a->group = $group;
                    $a->courseid = $courseid;
                    $result .= '<div class="alert alert-error">'.get_string('im:error_add_grp', 'local_mass_enroll', $a) . '</div>';
                    continue;
                }
                $createdgroupingscount++;
                $createdgroupings .= " $group";
            } else {
                // don't complains,
                // just do the enrolment to group
            }
        }
        // if grouping existed or has just been created
        if ($gpid && !(mass_enroll_group_in_grouping($gid, $gpid))) {
            if (!(mass_enroll_add_group_grouping($gid, $gpid))) {
                $a->group = $group;
                $result .= '<div class="alert alert-error">'.get_string('im:error_add_g_grp', 'local_mass_enroll', $a) . '</div>';
                continue;
            }
        }
        //}

        // finally add to group if needed
        if (!groups_is_member($gid, $user->id)) {
            $ok = groups_add_member($gid, $user->id);
            if ($ok) {
                $result .= '<div class="alert alert-success">'.get_string('im:and_added_g', 'local_mass_enroll', $group) . '</div>';
            } else {
                $result .= '<div class="alert alert-error">'.get_string('im:error_adding_u_g', 'local_mass_enroll', $group) . '</div>';
            }
        } else {
            $result .= '<div class="alert alert-notice">'.get_string('im:already_in_g', 'local_mass_enroll', $group) . '</div>';
        }

    }
    $result .= '<br />';
    //recap final
    $result .= get_string('im:stats_i', 'local_mass_enroll', $enrollablecount) . "";
    $a->nb = $createdgroupscount;
    if(!isset($createdgroups) || empty($createdgroups)||$createdgroups='')
    $a->what = '-';
    else
    $a->what = $createdgroups;
    $result .= get_string('im:stats_g', 'local_mass_enroll', $a) . "";
    $a->nb = $createdgroupingscount;
    if(!isset($createdgroupings) || empty($createdgroupings)||$createdgroupings='')
    $a->what = '-';
    else
    $a->what = $createdgroupings;
    $result .= get_string('im:stats_grp', 'local_mass_enroll', $a) . "";

    return $result;
}
/**
 * process the mass enrolment
 *
 * @param csv_import_reader $cir  an import reader created by caller
 * @param stdClass $course  a course record from table mdl_course
 * @param stdClass $context  course context instance
 * @param stdClass $data    data from a moodleform
 * @return string  log of operations
 */
function mass_enroll($cir, $course, $context, $data)
{
    global $CFG, $DB;
    require_once $CFG->dirroot . '/group/lib.php';
    $result = '';
    $roleid = $data->roleassign;
    $useridfield = $data->firstcolumn;
    $enrollablecount = 0;
    $createdgroupscount = 0;
    $createdgroupingscount = 0;
    $createdgroups = '';
    $createdgroupings = '';
    $role = $DB->get_record('role', array('id' => $roleid));
    $result .= get_string('im:using_role', 'local_mass_enroll', $role->name) . "\n";
    $plugin = enrol_get_plugin('manual');
    // Moodle 2.x enrolment and role assignment are different.
    // Assure course has manual enrolment plugin instance we are going to use.
    // Only one instance is allowed; see enrol/manual/lib.php get_new_instance().
    $instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
    if (empty($instance)) {
        // Only add an enrol instance to the course if non-existent.
        $enrolid = $plugin->add_instance($course);
        $instance = $DB->get_record('enrol', array('id' => $enrolid));
    }
    // Init csv import helper.
    $cir->init();
    while ($fields = $cir->next()) {
        $a = new stdClass();
        if (empty($fields)) {
            continue;
        }
        // First column = id Moodle (idnumber,username or email).
        // Get rid on eventual double quotes unfortunately not done by Moodle CSV importer.
        $fields[0] = str_replace('"', '', trim($fields[0]));
        if (!($user = $DB->get_record('user', array($useridfield => $fields[0])))) {
            $result .= get_string('im:user_unknown', 'local_mass_enroll', $fields[0]) . "\n";
            continue;
        }
        // Already enroled?
        // We DO NOT support multiple roles in a course.
        if ($ue = $DB->get_record('user_enrolments', array('enrolid' => $instance->id, 'userid' => $user->id))) {
            $result .= get_string('im:already_in', 'local_mass_enroll', fullname($user));
        } else {
            // Take care of timestart/timeend in course settings.
            $timestart = time();
            // Remove time part from the timestamp and keep only the date part.
            $timestart = make_timestamp(date('Y', $timestart), date('m', $timestart), date('d', $timestart), 0, 0, 0);
            if ($instance->enrolperiod) {
                $timeend = $timestart + $instance->enrolperiod;
            } else {
                $timeend = 0;
            }
            // Enrol the user with this plugin instance (unfortunately return void, no more status).
            $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
            $result .= get_string('im:enrolled_ok', 'local_mass_enroll', fullname($user));
            $enrollablecount++;
        }
        $group = str_replace('"', '', trim($fields[1]));
        // 2nd column?
        if (empty($group)) {
            $result .= "\n";
            continue;
            // No group for this one.
        }
        // Create group if needed.
        if (!($gid = mass_enroll_group_exists($group, $course->id))) {
            if ($data->creategroups) {
                if (!($gid = mass_enroll_add_group($group, $course->id))) {
                    $a->group = $group;
                    $a->courseid = $course->id;
                    $result .= get_string('im:error_addg', 'local_mass_enroll', $a) . "\n";
                    continue;
                }
                $createdgroupscount++;
                $createdgroups .= " {$group}";
            } else {
                $result .= get_string('im:error_g_unknown', 'local_mass_enroll', $group) . "\n";
                continue;
            }
        }
        // If groupings are enabled on the site (should be?).
        if (!($gpid = mass_enroll_grouping_exists($group, $course->id))) {
            if ($data->creategroupings) {
                if (!($gpid = mass_enroll_add_grouping($group, $course->id))) {
                    $a->group = $group;
                    $a->courseid = $course->id;
                    $result .= get_string('im:error_add_grp', 'local_mass_enroll', $a) . "\n";
                    continue;
                }
                $createdgroupingscount++;
                $createdgroupings .= " {$group}";
            }
        }
        // If grouping existed or has just been created.
        if ($gpid && !mass_enroll_group_in_grouping($gid, $gpid)) {
            if (!mass_enroll_add_group_grouping($gid, $gpid)) {
                $a->group = $group;
                $result .= get_string('im:error_add_g_grp', 'local_mass_enroll', $a) . "\n";
                continue;
            }
        }
        // Finally add to group if needed.
        if (!groups_is_member($gid, $user->id)) {
            $ok = groups_add_member($gid, $user->id);
            if ($ok) {
                $result .= get_string('im:and_added_g', 'local_mass_enroll', $group) . "\n";
            } else {
                $result .= get_string('im:error_adding_u_g', 'local_mass_enroll', $group) . "\n";
            }
        } else {
            $result .= get_string('im:already_in_g', 'local_mass_enroll', $group) . "\n";
        }
    }
    // Recap final.
    $result .= get_string('im:stats_i', 'local_mass_enroll', $enrollablecount) . "\n";
    $a->nb = $createdgroupscount;
    $a->what = $createdgroups;
    $result .= get_string('im:stats_g', 'local_mass_enroll', $a) . "\n";
    $a->nb = $createdgroupingscount;
    $a->what = $createdgroupings;
    $result .= get_string('im:stats_grp', 'local_mass_enroll', $a) . "\n";
    // Trigger event.
    $event = \local_mass_enroll\event\mass_enrolment_created::create(array('objectid' => $course->id, 'courseid' => $course->id, 'context' => \context_course::instance($course->id), 'other' => array('info' => get_string('mass_enroll', 'local_mass_enroll'))));
    $event->trigger();
    return $result;
}