Exemple #1
0
 function validation($data, $files)
 {
     global $COURSE;
     $errors = parent::validation($data, $files);
     $name = trim(stripslashes($data['name']));
     if ($data['id'] and $grouping = get_record('groupings', 'id', $data['id'])) {
         if ($grouping->name != $name) {
             if (groups_get_grouping_by_name($COURSE->id, $name)) {
                 $errors['name'] = get_string('groupingnameexists', 'group', $name);
             }
         }
     } else {
         if (groups_get_grouping_by_name($COURSE->id, $name)) {
             $errors['name'] = get_string('groupingnameexists', 'group', $name);
         }
     }
     return $errors;
 }
Exemple #2
0
 /**
  * Form validation
  *
  * @param array $data
  * @param array $files
  * @return array $errors An array of validataion errors for the form.
  */
 function validation($data, $files)
 {
     global $COURSE, $DB;
     $errors = parent::validation($data, $files);
     $name = trim($data['name']);
     if ($data['id'] and $grouping = $DB->get_record('groupings', array('id' => $data['id']))) {
         if (textlib::strtolower($grouping->name) != textlib::strtolower($name)) {
             if (groups_get_grouping_by_name($COURSE->id, $name)) {
                 $errors['name'] = get_string('groupingnameexists', 'group', $name);
             }
         }
     } else {
         if (groups_get_grouping_by_name($COURSE->id, $name)) {
             $errors['name'] = get_string('groupingnameexists', 'group', $name);
         }
     }
     return $errors;
 }
 function validation($data)
 {
     global $COURSE;
     $errors = array();
     $name = stripslashes($data['name']);
     if ($data['id'] and $grouping = get_record('groupings', 'id', $data['id'])) {
         if ($grouping->name != $name) {
             if (groups_get_grouping_by_name($COURSE->id, $name)) {
                 $errors['name'] = get_string('groupingnameexists', 'group', $name);
             }
         }
     } else {
         if (groups_get_grouping_by_name($COURSE->id, $name)) {
             $errors['name'] = get_string('groupingnameexists', 'group', $name);
         }
     }
     if (count($errors) > 0) {
         return $errors;
     } else {
         return true;
     }
 }
Exemple #4
0
 /**
  * Form validation
  *
  * @param array $data
  * @param array $files
  * @return array $errors An array of validataion errors for the form.
  */
 function validation($data, $files)
 {
     global $COURSE, $DB;
     $errors = parent::validation($data, $files);
     $name = trim($data['name']);
     if (isset($data['idnumber'])) {
         $idnumber = trim($data['idnumber']);
     } else {
         $idnumber = '';
     }
     if ($data['id'] and $grouping = $DB->get_record('groupings', array('id' => $data['id']))) {
         if (core_text::strtolower($grouping->name) != core_text::strtolower($name)) {
             if (groups_get_grouping_by_name($COURSE->id, $name)) {
                 $errors['name'] = get_string('groupingnameexists', 'group', $name);
             }
         }
         if (!empty($idnumber) && $grouping->idnumber != $idnumber) {
             if (groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
                 $errors['idnumber'] = get_string('idnumbertaken');
             }
         }
     } else {
         if (groups_get_grouping_by_name($COURSE->id, $name)) {
             $errors['name'] = get_string('groupingnameexists', 'group', $name);
         } else {
             if (!empty($idnumber) && groups_get_grouping_by_idnumber($COURSE->id, $idnumber)) {
                 $errors['idnumber'] = get_string('idnumbertaken');
             }
         }
     }
     return $errors;
 }
Exemple #5
0
     unset($newgroup->groupidnumber);
 }
 if ($groupid = groups_get_group_by_name($newgroup->courseid, $groupname)) {
     echo $OUTPUT->notification("{$groupname} :" . get_string('groupexistforcourse', 'error', $groupname));
 } else {
     if ($groupid = groups_create_group($newgroup)) {
         echo $OUTPUT->notification(get_string('groupaddedsuccesfully', 'group', $groupname), 'notifysuccess');
     } else {
         echo $OUTPUT->notification(get_string('groupnotaddederror', 'error', $groupname));
         continue;
     }
 }
 // Add group to grouping
 if (!empty($newgroup->groupingname) || is_numeric($newgroup->groupingname)) {
     $groupingname = $newgroup->groupingname;
     if (!($groupingid = groups_get_grouping_by_name($newgroup->courseid, $groupingname))) {
         $data = new stdClass();
         $data->courseid = $newgroup->courseid;
         $data->name = $groupingname;
         if ($groupingid = groups_create_grouping($data)) {
             echo $OUTPUT->notification(get_string('groupingaddedsuccesfully', 'group', $groupingname), 'notifysuccess');
         } else {
             echo $OUTPUT->notification(get_string('groupingnotaddederror', 'error', $groupingname));
             continue;
         }
     }
     // if we have reached here we definitely have a groupingid
     $a = array('groupname' => $groupname, 'groupingname' => $groupingname);
     try {
         groups_assign_grouping($groupingid, $groupid);
         echo $OUTPUT->notification(get_string('groupaddedtogroupingsuccesfully', 'group', $a), 'notifysuccess');
 function validation($data, $files)
 {
     global $CFG, $COURSE;
     $errors = parent::validation($data, $files);
     if ($data['allocateby'] != 'no') {
         if (!($users = groups_get_potential_members($data['courseid'], $data['roleid']))) {
             $errors['roleid'] = get_string('nousersinrole', 'group');
         }
         /// Check the number entered is sane
         if ($data['groupby'] == 'groups') {
             $usercnt = count($users);
             if ($data['number'] > $usercnt || $data['number'] < 1) {
                 $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
             }
         }
     }
     //try to detect group name duplicates
     $name = groups_parse_name(stripslashes(trim($data['namingscheme'])), 0);
     if (groups_get_group_by_name($COURSE->id, $name)) {
         $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
     }
     // check grouping name duplicates
     if (isset($data['grouping']) && $data['grouping'] == '-1') {
         $name = trim(stripslashes($data['groupingname']));
         if (empty($name)) {
             $errors['groupingname'] = get_string('required');
         } else {
             if (groups_get_grouping_by_name($COURSE->id, $name)) {
                 $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
             }
         }
     }
     /// Check the naming scheme
     $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
     if ($matchcnt != 1) {
         $errors['namingscheme'] = get_string('badnamingscheme', 'group');
     }
     return $errors;
 }
 /**
  * Validate that our constant expect end result is reached
  */
 private function validate_end_result()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/lib/grouplib.php';
     // Validate group creation.
     $groupid = groups_get_group_by_name(SITEID, 'testusersetname');
     $this->assertNotEquals(false, $groupid);
     // Validate user-group assignment.
     $userid = $DB->get_field('user', 'id', array('username' => 'testuserusername'));
     $this->assertTrue(groups_is_member($groupid, $userid));
     // Validate grouping creation.
     $groupingid = groups_get_grouping_by_name(SITEID, 'testusersetname');
     $this->assertNotEquals(false, $groupingid);
     // Validate group-grouping assignment.
     $this->assertTrue($DB->record_exists('groupings_groups', array('groupingid' => $groupingid, 'groupid' => $groupid)));
 }
Exemple #8
0
/**
 * @param string $name group name
 * @param int $courseid course
 * @return string or false
 */
function mass_enroll_grouping_exists($name, $courseid) {
    return groups_get_grouping_by_name($courseid, $name);

}
 /**
  * Validation for administration-form
  * If there are errors return array of errors ("fieldname"=>"error message"),
  * otherwise true if ok.
  *
  * @param array $data array of ("fieldname"=>value) of submitted data
  * @param array $files array of uploaded files "element_name"=>tmp_file_path
  * @return array of "element_name"=>"error_description" if there are errors,
  *               or an empty array if everything is OK.
  */
 public function validation($data, $files)
 {
     global $DB;
     $parenterrors = parent::validation($data, $files);
     $errors = array();
     if ($data['target'] == -1 && empty($data['name'])) {
         $errors['name'] = get_string('required');
     }
     if ($data['target'] == -1 && groups_get_grouping_by_name($data['courseid'], $data['name'])) {
         $errors['name'] = get_string('groupingnameexists', 'group', $data['name']);
     }
     return array_merge($parenterrors, $errors);
 }
 public function test_groups_get_grouping()
 {
     $this->resetAfterTest(true);
     $generator = $this->getDataGenerator();
     // Create a course category and course.
     $cat = $generator->create_category(array('parent' => 0));
     $course = $generator->create_course(array('category' => $cat->id));
     $name1 = 'Grouping 1';
     $name2 = 'Grouping 2';
     // Test with an empty and a null idnumber.
     $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
     $this->assertFalse(groups_get_grouping_by_name($course->id, null));
     // Even when a group exists.
     $generator->create_group(array('courseid' => $course->id));
     $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
     $this->assertFalse(groups_get_grouping_by_name($course->id, null));
     // Test with a valid name, but one that doesn't exist yet.
     $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
     $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
     // We should now have a valid group returned by the name search.
     $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1));
     $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1));
     $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
     // We should now have a two valid groups returned by the name search.
     $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2));
     $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1));
     $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2));
     // Delete a group.
     $this->assertTrue(groups_delete_grouping($group1));
     $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
     $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2));
     /*
      * Group idnumbers are unique within a course so test that we don't
      * retrieve groups for the first course.
      */
     // Create a second course.
     $course = $generator->create_course(array('category' => $cat->id));
     // An empty name should always return a false value.
     $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
     $this->assertFalse(groups_get_grouping_by_name($course->id, null));
     // Our existing names shouldn't be returned here as we're in a different course.
     $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
     $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
     // We should be able to reuse the idnumbers again since this is a different course.
     $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1));
     $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1));
     $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2));
     $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2));
 }
Exemple #11
0
    /**
     * Performs validation of the form information
     *
     * @param array $data
     * @param array $files
     * @return array $errors An array of $errors
     */
    function validation($data, $files) {
        global $CFG, $COURSE;
        $errors = parent::validation($data, $files);

        if ($data['allocateby'] != 'no') {
            $source = array();
            if ($data['cohortid']) {
                $source['cohortid'] = $data['cohortid'];
            }
            if ($data['groupingid']) {
                $source['groupingid'] = $data['groupingid'];
            }
            if ($data['groupid']) {
                $source['groupid'] = $data['groupid'];
            }
            if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $source)) {
                $errors['roleid'] = get_string('nousersinrole', 'group');
            }

           /// Check the number entered is sane
            if ($data['groupby'] == 'groups') {
                $usercnt = count($users);

                if ($data['number'] > $usercnt || $data['number'] < 1) {
                    $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
                }
            }
        }

        //try to detect group name duplicates
        $name = groups_parse_name(trim($data['namingscheme']), 0);
        if (groups_get_group_by_name($COURSE->id, $name)) {
            $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
        }

        // check grouping name duplicates
        if ( isset($data['grouping']) && $data['grouping'] == '-1') {
            $name = trim($data['groupingname']);
            if (empty($name)) {
                $errors['groupingname'] = get_string('required');
            } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
                $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
            }
        }

       /// Check the naming scheme
        if ($data['groupby'] == 'groups' and $data['number'] == 1) {
            // we can use the name as is because there will be only one group max
        } else {
            $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
            if ($matchcnt != 1) {
                $errors['namingscheme'] = get_string('badnamingscheme', 'group');
            }
        }

        return $errors;
    }
Exemple #12
0
/**
 * Updates all parent cluster's groupings with the existence of a group for this cluster
 *
 * @param    int      $clusterid         The cluster to check the parents for
 * @param    boolean  $include_children  If true, make child cluster-groups trickle up the tree
 * @return   boolean                     Returns true to satisfy event handlers
 */
function userset_groups_update_grouping_closure($clusterid, $include_children = false)
{
    global $CFG;
    $enabled = get_config('elisprogram_usetgroups', 'userset_groupings');
    if (empty($enabled) || !userset_groups_grouping_allowed($clusterid)) {
        return true;
    }
    $cluster = new userset($clusterid);
    //get the group id for this cluster
    if ($groupid = groups_get_group_by_name(SITEID, $cluster->name)) {
        //go through the chain of parent clusters
        while (!empty($cluster->parent)) {
            $cluster = new userset($cluster->parent);
            //add this to grouping if applicable
            $grouping = groups_get_grouping_by_name(SITEID, $cluster->name);
            if ($grouping = groups_get_grouping($grouping)) {
                groups_assign_grouping($grouping->id, $groupid);
                //recurse into children if possible
                if ($include_children) {
                    //get all child clusters
                    $child_cluster_ids = userset_groups_get_child_usersets($cluster->id);
                    foreach ($child_cluster_ids as $child_cluster_id) {
                        //children only
                        if ($child_cluster_id != $cluster->id) {
                            $child_cluster = new userset($child_cluster_id);
                            //make sure the group exists
                            if ($child_groupid = groups_get_group_by_name(SITEID, $child_cluster->name) and userset_groups_userset_allows_groups($child_cluster->id)) {
                                groups_assign_grouping($grouping->id, $child_groupid);
                            }
                        }
                    }
                }
            }
        }
    }
    return true;
}
 /**
  * Outputs the content of the administration tab and manages actions taken in this tab
  */
 public function view_administration()
 {
     global $SESSION, $OUTPUT, $PAGE, $DB, $USER, $CFG;
     $output = $PAGE->get_renderer('mod_grouptool');
     $id = $this->cm->id;
     $context = context_course::instance($this->course->id);
     // Get applicable roles!
     $rolenames = array();
     if ($roles = get_profile_roles($context)) {
         foreach ($roles as $role) {
             $rolenames[$role->id] = strip_tags(role_get_name($role, $context));
         }
     }
     $filter = optional_param('filter', null, PARAM_INT);
     if ($filter !== null) {
         set_user_preference('mod_grouptool_group_filter', $filter, $USER->id);
     } else {
         $filter = get_user_preferences('mod_grouptool_group_filter', self::FILTER_ACTIVE, $USER->id);
     }
     $inactivetabs = array();
     $filtertabs['active'] = new tabobject(self::FILTER_ACTIVE, $CFG->wwwroot . '/mod/grouptool/view.php?id=' . $id . '&amp;tab=group_admin&filter=' . self::FILTER_ACTIVE, get_string('active', 'grouptool'), '', false);
     $filtertabs['inactive'] = new tabobject(self::FILTER_INACTIVE, $CFG->wwwroot . '/mod/grouptool/view.php?id=' . $id . '&amp;tab=group_admin&filter=' . self::FILTER_INACTIVE, get_string('inactive'), '', false);
     $filtertabs['all'] = new tabobject(self::FILTER_ALL, $CFG->wwwroot . '/mod/grouptool/view.php?id=' . $id . '&amp;tab=group_admin&filter=' . self::FILTER_ALL, get_string('all'), '', false);
     echo html_writer::tag('div', $OUTPUT->tabtree($filtertabs, $filter, $inactivetabs), array('id' => 'filtertabs'));
     // Check if everything has been confirmed, so we can finally start working!
     if (optional_param('confirm', 0, PARAM_BOOL)) {
         if (isset($SESSION->grouptool->view_administration->createGroupings)) {
             require_capability('mod/grouptool:create_groupings', $this->context);
             $target = required_param('target', PARAM_INT);
             switch ($target) {
                 // ...grpg_target | grpg_groupingname | use_all (0 sel | 1 all).
                 case 0:
                     // Invalid - no action! TODO Add message!
                     $error = true;
                     $preview = '';
                     break;
                 case -2:
                     // One grouping per group!
                     list($error, $preview) = $this->create_group_groupings();
                     break;
                 case -1:
                     // One new grouping for all!
                     list($error, $preview) = $this->update_grouping($target, required_param('name', PARAM_ALPHANUMEXT));
                     break;
                 default:
                     list($error, $preview) = $this->update_grouping($target);
                     break;
             }
             $preview = html_writer::tag('div', $preview, array('class' => 'centered'));
             echo $OUTPUT->box($preview, 'generalbox');
         }
         unset($SESSION->grouptool->view_administration);
     }
     if ($rename = optional_param('rename', 0, PARAM_INT)) {
         // Show Rename Form!
         $gform = new \mod_grouptool\group_rename_form(null, array('id' => $this->cm->id, 'instance' => $this->cm->instance, 'rename' => $rename));
         if (!$gform->is_cancelled() && ($fromform = $gform->get_data())) {
             $group = new stdClass();
             $group->id = $fromform->rename;
             $group->name = $fromform->name;
             $group->courseid = $fromform->courseid;
             groups_update_group($group);
         } else {
             if (!$gform->is_cancelled()) {
                 $data = new stdClass();
                 $data->name = $DB->get_field('groups', 'name', array('id' => $rename));
                 $gform->set_data($data);
                 $gform->display();
                 echo $OUTPUT->footer();
                 die;
             }
         }
     }
     if ($resize = optional_param('resize', 0, PARAM_INT)) {
         // Show Rename Form!
         $gform = new \mod_grouptool\group_resize_form(null, array('id' => $this->cm->id, 'instance' => $this->cm->instance, 'resize' => $resize));
         if (!$gform->is_cancelled() && ($fromform = $gform->get_data())) {
             if (empty($fromform->size)) {
                 $DB->set_field('grouptool_agrps', 'grpsize', null, array('groupid' => $fromform->resize, 'grouptoolid' => $this->cm->id));
             } else {
                 $group = new stdClass();
                 $group->id = $DB->get_field('grouptool_agrps', 'id', array('groupid' => $fromform->resize, 'grouptoolid' => $this->cm->instance));
                 $group->grpsize = $fromform->size;
                 $DB->update_record('grouptool_agrps', $group);
             }
         } else {
             if (!$gform->is_cancelled()) {
                 $data = new stdClass();
                 $data->size = $DB->get_field('grouptool_agrps', 'grpsize', array('groupid' => $resize, 'grouptoolid' => $this->cm->instance));
                 $gform->set_data($data);
                 $gform->display();
                 echo $OUTPUT->footer();
                 die;
             }
         }
     }
     if ($delete = optional_param('delete', 0, PARAM_INT)) {
         if (!optional_param('confirm', 0, PARAM_BOOL)) {
             // Show Confirm!
             $cancel = new moodle_url($PAGE->url);
             $continue = new moodle_url($cancel, array('confirm' => 1, 'delete' => $delete));
             $cancel = new single_button($cancel, get_string('no'), 'post');
             $continue = new single_button($continue, get_string('yes'), 'post');
             $confirmtext = get_string('confirm_delete', 'grouptool');
             echo $this->confirm($confirmtext, $continue, $cancel);
             echo $OUTPUT->footer();
             die;
         } else {
             // Delete it!
             groups_delete_group($delete);
         }
     }
     if ($toggle = optional_param('toggle', 0, PARAM_INT)) {
         if (!empty($toggle)) {
             $conditions = array('grouptoolid' => $this->cm->instance, 'groupid' => $toggle);
             if (!$DB->record_exists('grouptool_agrps', $conditions)) {
                 echo $OUTPUT->box($OUTPUT->notification(get_string('group_not_found', 'grouptool'), 'notifyproblem', $conditions), 'generalbox');
             } else {
                 $record = $DB->get_record('grouptool_agrps', $conditions);
                 if (!empty($record->active)) {
                     $DB->set_field('grouptool_agrps', 'active', 0, $conditions);
                 } else {
                     $DB->set_field('grouptool_agrps', 'active', 1, $conditions);
                 }
             }
         }
     }
     if (($bulkaction = optional_param('bulkaction', null, PARAM_ALPHA)) && ($selected = optional_param_array('selected', array(), PARAM_INT)) && optional_param('start_bulkaction', 0, PARAM_BOOL)) {
         switch ($bulkaction) {
             case 'activate':
                 // ...also via ajax bulk action?
                 // Activate now!
                 $groups = optional_param_array('selected', null, PARAM_INT);
                 if (!empty($groups)) {
                     list($grpsql, $grpparams) = $DB->get_in_or_equal($groups);
                     $DB->set_field_select("grouptool_agrps", "active", 1, " grouptoolid = ? AND groupid " . $grpsql, array_merge(array($this->cm->instance), $grpparams));
                 }
                 echo $OUTPUT->notification(get_string('activated_groups', 'grouptool'), 'notifysuccess');
                 $continue = new moodle_url($PAGE->url, array('tab' => 'group_admin'));
                 echo $this->confirm('', $continue);
                 break;
             case 'deactivate':
                 // ...also via ajax bulk action?
                 // Deactivate now!
                 $groups = optional_param_array('selected', null, PARAM_INT);
                 if (!empty($groups)) {
                     list($grpsql, $grpparams) = $DB->get_in_or_equal($groups);
                     $DB->set_field_select("grouptool_agrps", "active", 0, " grouptoolid = ? AND groupid " . $grpsql, array_merge(array($this->cm->instance), $grpparams));
                 }
                 echo $OUTPUT->notification(get_string('deactivated_groups', 'grouptool'), 'notifysuccess');
                 $continue = new moodle_url($PAGE->url, array('tab' => 'group_admin'));
                 echo $this->confirm('', $continue);
                 break;
             case 'delete':
                 // ...also via ajax bulk action?
                 // Show confirmation dialogue!
                 if (optional_param('confirm', 0, PARAM_BOOL)) {
                     $groups = optional_param_array('selected', null, PARAM_INT);
                     $groups = $DB->get_records_list('groups', 'id', $groups);
                     foreach ($groups as $group) {
                         groups_delete_group($group);
                     }
                     echo $OUTPUT->notification(get_string('successfully_deleted_groups', 'grouptool'), 'notifysuccess');
                     $continue = new moodle_url($PAGE->url, array('tab' => 'group_admin'));
                     echo $this->confirm('', $continue);
                 } else {
                     $cancel = new moodle_url($PAGE->url, array('tab' => 'group_admin'));
                     $params = array('confirm' => 1, 'bulkaction' => 'delete', 'start_bulkaction' => 1);
                     $text = get_string('confirm_delete', 'grouptool') . html_writer::start_tag('ul');
                     $groups = $DB->get_records_list('groups', 'id', $selected);
                     foreach ($selected as $select) {
                         $params['selected[' . $select . ']'] = $select;
                         $text .= html_writer::tag('li', $groups[$select]->name);
                     }
                     $text .= html_writer::end_tag('ul');
                     $continue = new moodle_url($cancel, $params);
                     echo $this->confirm($text, $continue, $cancel);
                     echo $OUTPUT->footer();
                     die;
                 }
                 break;
             case 'grouping':
                 // Show grouping creation form!
                 $selected = optional_param_array('selected', array(), PARAM_INT);
                 $mform = new \mod_grouptool\groupings_creation_form(null, array('id' => $id, 'selected' => $selected));
                 $groups = $DB->get_records_list('groups', 'id', $selected);
                 if ($mform->is_cancelled()) {
                     $bulkaction = null;
                     $selected = array();
                 } else {
                     if ($fromform = $mform->get_data()) {
                         // Some groupings should be created...
                         if ($fromform->target == -2) {
                             // One new grouping per group!
                             foreach ($groups as $group) {
                                 $grouping = new stdClass();
                                 if (!($grouping->id = groups_get_grouping_by_name($this->course->id, $group->name))) {
                                     $grouping = new stdClass();
                                     $grouping->courseid = $this->course->id;
                                     $grouping->name = $group->name;
                                     $grouping->id = groups_create_grouping($grouping);
                                 }
                                 // Insert group!
                                 groups_assign_grouping($grouping->id, $group->id);
                             }
                         } else {
                             if ($fromform->target == -1) {
                                 // One new grouping!
                                 // Create grouping if it doesn't exist...
                                 $grouping = new stdClass();
                                 if (!($grouping->id = groups_get_grouping_by_name($this->course->id, $fromform->name))) {
                                     $grouping = new stdClass();
                                     $grouping->courseid = $this->course->id;
                                     $grouping->name = trim($fromform->name);
                                     $grouping->id = groups_create_grouping($grouping);
                                 }
                                 // Insert groups!
                                 foreach ($groups as $group) {
                                     groups_assign_grouping($grouping->id, $group->id);
                                 }
                             } else {
                                 if ($fromform->target > 0) {
                                     // Existing Grouping!
                                     $grouping = groups_get_grouping($fromform->target);
                                     if ($grouping) {
                                         foreach ($groups as $group) {
                                             groups_assign_grouping($grouping->id, $group->id);
                                         }
                                     }
                                 }
                             }
                         }
                         // ...redirect to show sortlist again!
                         $url = new moodle_url('/mod/grouptool/view.php', array('id' => $this->cm->id, 'tab' => 'group_admin', 'filter' => $filter));
                         $text = $OUTPUT->notification(get_string('groupings_created_and_groups_added', 'grouptool'), 'notifymessage');
                         echo $this->confirm($text, $url);
                     } else {
                         $mform->display();
                     }
                 }
                 break;
         }
     }
     if (!$bulkaction || !$selected || !optional_param('start_bulkaction', 0, PARAM_BOOL)) {
         // Show form!
         $formaction = new moodle_url('/mod/grouptool/view.php', array('id' => $this->cm->id, 'tab' => 'group_admin', 'filter' => $filter));
         $mform = new MoodleQuickForm('bulk', 'post', $formaction, '');
         $mform->addElement('hidden', 'sesskey');
         $mform->setDefault('sesskey', sesskey());
         $sortlist = new \mod_grouptool\output\sortlist($this->course->id, $this->cm, $filter);
         $sortlistcontroller = new \mod_grouptool\output\sortlist_controller($sortlist);
         $mform->addElement('html', $output->render($sortlistcontroller));
         $mform->addElement('html', $output->render($sortlist));
         $actions = array('' => get_string('choose', 'grouptool'), 'activate' => get_string('setactive', 'grouptool'), 'deactivate' => get_string('setinactive', 'grouptool'), 'delete' => get_string('delete'), 'grouping' => get_string('createinsertgrouping', 'grouptool'));
         $grp = array();
         $grp[] =& $mform->createElement('static', 'with_selection', '', get_string('with_selection', 'grouptool'));
         $grp[] =& $mform->createElement('select', 'bulkaction', '', $actions);
         $grp[] =& $mform->createElement('submit', 'start_bulkaction', get_string('start', 'grouptool'));
         $mform->addGroup($grp, 'actiongrp', '', ' ', false);
         $mform->disable_form_change_checker();
         $mform->display();
         switch ($filter) {
             case self::FILTER_ACTIVE:
                 $curfilter = 'active';
                 break;
             case self::FILTER_INACTIVE:
                 $curfilter = 'inactive';
                 break;
             case self::FILTER_ALL:
                 $curfilter = 'all';
                 break;
         }
         $PAGE->requires->yui_module('moodle-mod_grouptool-administration', 'M.mod_grouptool.init_administration', array(array('lang' => current_language(), 'contextid' => $this->context->id, 'filter' => $curfilter, 'filterid' => $filter, 'globalsize' => $this->grouptool->grpsize)));
         $PAGE->requires->strings_for_js(array('active', 'inactive', 'confirm_delete'), 'mod_grouptool');
         $PAGE->requires->string_for_js('ajax_edit_size_help', 'mod_grouptool');
         $PAGE->requires->strings_for_js(array('yes', 'no'), 'moodle');
     }
 }
Exemple #14
0
/**
 * Updates all parent cluster's groupings with the existence of a group for this cluster
 *
 * @param    int      $clusterid         The cluster to check the parents for
 * @param    boolean  $include_children  If true, make child cluster-groups trickle up the tree
 * @return   boolean                     Returns true to satisfy event handlers
 */
function cluster_groups_update_grouping_closure($clusterid, $include_children = false)
{
    global $CFG, $CURMAN;
    if (empty($CFG->enablegroupings) || empty($CURMAN->config->cluster_groupings) || !cluster_groups_grouping_allowed($clusterid)) {
        return true;
    }
    $cluster = new cluster($clusterid);
    //get the group id for this cluster
    if ($groupid = groups_get_group_by_name(SITEID, $cluster->name)) {
        //go through the chain of parent clusters
        while (!empty($cluster->parent)) {
            $cluster = new cluster($cluster->parent);
            //add this to grouping if applicable
            $grouping = groups_get_grouping_by_name(SITEID, $cluster->name);
            if ($grouping = groups_get_grouping($grouping)) {
                groups_assign_grouping($grouping->id, $groupid);
                //recurse into children if possible
                if ($include_children) {
                    //get all child clusters
                    $child_cluster_ids = cluster_groups_get_child_clusters($cluster->id);
                    foreach ($child_cluster_ids as $child_cluster_id) {
                        //children only
                        if ($child_cluster_id != $cluster->id) {
                            $child_cluster = new cluster($child_cluster_id);
                            //make sure the group exists
                            if ($child_groupid = groups_get_group_by_name(SITEID, $child_cluster->name) and cluster_groups_cluster_allows_groups($child_cluster->id)) {
                                groups_assign_grouping($grouping->id, $child_groupid);
                            }
                        }
                    }
                }
            }
        }
    }
    return true;
}
function blended_create_unique_grouping($grouping_name, $course)
{
    $data = new stdClass();
    if ($grouping_name == '') {
        $grouping_name = get_string('newgrouping', 'group');
    }
    $data->name = $grouping_name;
    $data->courseid = $course->id;
    $data->description_editor['text'] = ' ';
    $data->description_editor['format'] = 1;
    // Check existence of grouping name
    $count = 1;
    do {
        $prev_grouping = groups_get_grouping_by_name($course->id, $data->name);
        if ($prev_grouping !== 0) {
            $data->name = $grouping_name . " ({$count})";
        } else {
            $data->name = $grouping_name;
        }
        $count++;
    } while ($prev_grouping);
    $groupingid = groups_create_grouping($data);
    return $groupingid;
}
Exemple #16
0
 /**
  * Create an enrolment
  *
  * @param object $record One record of import data
  * @param string $filename The import file name, used for logging
  * @return boolean true on success, otherwise false
  */
 function enrolment_create($record, $filename)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/lib/enrollib.php';
     //set initial logging state with respect to enrolments (give non-specific message for now)
     $this->fslogger->set_enrolment_state(false, false);
     //field length checking
     $lengthcheck = $this->check_enrolment_field_lengths($record, $filename);
     if (!$lengthcheck) {
         return false;
     }
     //data checking
     if (!($roleid = $DB->get_field('role', 'id', array('shortname' => $record->role)))) {
         $identifier = $this->mappings['role'];
         $this->fslogger->log_failure("{$identifier} value of \"{$record->role}\" does not refer " . "to a valid role.", 0, $filename, $this->linenumber, $record, 'enrolment');
         return false;
     }
     // Check for valid enrolment time
     if (isset($record->enrolmenttime)) {
         $value = $this->parse_date($record->enrolmenttime);
         if ($value === false) {
             $identifier = $this->mappings['enrolmenttime'];
             $this->fslogger->log_failure("{$identifier} value of \"{$record->enrolmenttime}\" is not a valid date in " . "MM/DD/YYYY, DD-MM-YYYY, YYYY.MM.DD, or MMM/DD/YYYY format.", 0, $filename, $this->linenumber, $record, "enrolment");
             return false;
         }
     }
     // Check for valid complete time
     if (isset($record->completetime)) {
         $value = $this->parse_date($record->completetime);
         if ($value === false) {
             $identifier = $this->mappings['completetime'];
             $this->fslogger->log_failure("{$identifier} value of \"{$record->completetime}\" is not a valid date in " . "MM/DD/YYYY, DD-MM-YYYY, YYYY.MM.DD, or MMM/DD/YYYY format.", 0, $filename, $this->linenumber, $record, "enrolment");
             return false;
         }
     }
     //find existing user record
     if (!($userid = $this->get_userid_from_record($record, $filename))) {
         return false;
     }
     //track context info
     $contextinfo = $this->get_contextinfo_from_record($record, $filename);
     if ($contextinfo == false) {
         return false;
     }
     list($contextlevel, $context) = $contextinfo;
     //make sure the role is assignable at the course context level
     if (!$DB->record_exists('role_context_levels', array('roleid' => $roleid, 'contextlevel' => $contextlevel))) {
         $this->fslogger->log_failure("The role with shortname \"{$record->role}\" is not assignable " . "on the {$record->context} context level.", 0, $filename, $this->linenumber, $record, "enrolment");
         return false;
     }
     //note: this seems redundant but will be useful for error messages later
     $params = array('roleid' => $roleid, 'contextid' => $context->id, 'userid' => $userid, 'component' => '', 'itemid' => 0);
     $role_assignment_exists = $DB->record_exists('role_assignments', $params);
     //track whether an enrolment exists
     $enrolment_exists = false;
     if ($contextlevel == CONTEXT_COURSE) {
         $enrolment_exists = is_enrolled($context, $userid);
     }
     //after this point, general error messages should contain role assignment info
     //they should also contain enrolment info if the context is a course
     $track_enrolments = $record->context == 'course';
     $this->fslogger->set_enrolment_state(true, $track_enrolments);
     //track the group and grouping specified
     $groupid = 0;
     $groupingid = 0;
     //duplicate group / grouping name checks and name validity checking
     if ($record->context == 'course' && isset($record->group)) {
         $count = $DB->count_records('groups', array('name' => $record->group, 'courseid' => $context->instanceid));
         $creategroups = get_config('dhimport_version1', 'creategroupsandgroupings');
         if ($count > 1) {
             //ambiguous
             $identifier = $this->mappings['group'];
             $this->fslogger->log_failure("{$identifier} value of \"{$record->group}\" refers to multiple " . "groups in course with shortname \"{$record->instance}\".", 0, $filename, $this->linenumber, $record, 'enrolment');
             return false;
         } else {
             if ($count == 0 && empty($creategroups)) {
                 //does not exist and not creating
                 $identifier = $this->mappings['group'];
                 $this->fslogger->log_failure("{$identifier} value of \"{$record->group}\" does not refer to " . "a valid group in course with shortname \"{$record->instance}\".", 0, $filename, $this->linenumber, $record, "enrolment");
                 return false;
             } else {
                 //exact group exists
                 $groupid = groups_get_group_by_name($context->instanceid, $record->group);
             }
         }
         if (isset($record->grouping)) {
             $count = $DB->count_records('groupings', array('name' => $record->grouping, 'courseid' => $context->instanceid));
             if ($count > 1) {
                 //ambiguous
                 $identifier = $this->mappings['grouping'];
                 $this->fslogger->log_failure("{$identifier} value of \"{$record->grouping}\" refers to multiple " . "groupings in course with shortname \"{$record->instance}\".", 0, $filename, $this->linenumber, $record, "enrolment");
                 return false;
             } else {
                 if ($count == 0 && empty($creategroups)) {
                     //does not exist and not creating
                     $identifier = $this->mappings['grouping'];
                     $this->fslogger->log_failure("{$identifier} value of \"{$record->grouping}\" does not refer to " . "a valid grouping in course with shortname \"{$record->instance}\".", 0, $filename, $this->linenumber, $record, "enrolment");
                     return false;
                 } else {
                     //exact grouping exists
                     $groupingid = groups_get_grouping_by_name($context->instanceid, $record->grouping);
                 }
             }
         }
     }
     //string to describe the user
     $user_descriptor = $this->get_user_descriptor($record);
     //string to describe the context instance
     $context_descriptor = $this->get_context_descriptor($record);
     //going to collect all messages for this action
     $logmessages = array();
     if ($record->context == 'course') {
         // Set enrolment start and end time if specified, otherwise set enrolment time to 'now' to allow immediate access.
         $timestart = empty($record->enrolmenttime) ? time() : $this->parse_date($record->enrolmenttime);
         $timeend = empty($record->completetime) ? 0 : $this->parse_date($record->completetime);
         if ($role_assignment_exists && !$enrolment_exists) {
             // Role assignment already exists, so just enrol the user.
             enrol_try_internal_enrol($context->instanceid, $userid, null, $timestart, $timeend);
         } else {
             if (!$enrolment_exists) {
                 // Role assignment does not exist, so enrol and assign role.
                 enrol_try_internal_enrol($context->instanceid, $userid, $roleid, $timestart, $timeend);
                 //collect success message for logging at end of action
                 $logmessages[] = "User with {$user_descriptor} successfully assigned role with shortname " . "\"{$record->role}\" on {$context_descriptor}.";
             } else {
                 if (!$role_assignment_exists) {
                     //just assign the role
                     role_assign($roleid, $userid, $context->id);
                     //collect success message for logging at end of action
                     $logmessages[] = "User with {$user_descriptor} successfully assigned role with " . "shortname \"{$record->role}\" on {$context_descriptor}.";
                 } else {
                     //duplicate enrolment attempt
                     $this->fslogger->log_failure("User with {$user_descriptor} is already assigned role " . "with shortname \"{$record->role}\" on {$context_descriptor}. " . "User with {$user_descriptor} is already enrolled in course with " . "shortname \"{$record->instance}\".", 0, $filename, $this->linenumber, $record, 'enrolment');
                     return false;
                 }
             }
         }
         //collect success message for logging at end of action
         if (!$enrolment_exists) {
             $logmessages[] = "User with {$user_descriptor} enrolled in course with shortname \"{$record->instance}\".";
             if (!empty($context->instanceid) && !empty($userid)) {
                 $this->newenrolmentemail($userid, $context->instanceid);
             }
         }
     } else {
         if ($role_assignment_exists) {
             //role assignment already exists, so this action serves no purpose
             $this->fslogger->log_failure("User with {$user_descriptor} is already assigned role " . "with shortname \"{$record->role}\" on {$context_descriptor}.", 0, $filename, $this->linenumber, $record, 'enrolment');
             return false;
         }
         role_assign($roleid, $userid, $context->id);
         //collect success message for logging at end of action
         $logmessages[] = "User with {$user_descriptor} successfully assigned role with shortname \"{$record->role}\" on {$context_descriptor}.";
     }
     if ($record->context == 'course' && isset($record->group)) {
         //process specified group
         require_once $CFG->dirroot . '/lib/grouplib.php';
         require_once $CFG->dirroot . '/group/lib.php';
         if ($groupid == 0) {
             //need to create the group
             $data = new stdClass();
             $data->courseid = $context->instanceid;
             $data->name = $record->group;
             $groupid = groups_create_group($data);
             //collect success message for logging at end of action
             $logmessages[] = "Group created with name \"{$record->group}\".";
         }
         if (groups_is_member($groupid, $userid)) {
             //error handling
             $logmessages[] = "User with {$user_descriptor} is already assigned to group with name \"{$record->group}\".";
         } else {
             //try to assign the user to the group
             if (!groups_add_member($groupid, $userid)) {
                 //should never happen
             }
             //collect success message for logging at end of action
             $logmessages[] = "Assigned user with {$user_descriptor} to group with name \"{$record->group}\".";
         }
         if (isset($record->grouping)) {
             //process the specified grouping
             if ($groupingid == 0) {
                 //need to create the grouping
                 $data = new stdClass();
                 $data->courseid = $context->instanceid;
                 $data->name = $record->grouping;
                 $groupingid = groups_create_grouping($data);
                 //collect success message for logging at end of action
                 $logmessages[] = "Created grouping with name \"{$record->grouping}\".";
             }
             //assign the group to the grouping
             if ($DB->record_exists('groupings_groups', array('groupingid' => $groupingid, 'groupid' => $groupid))) {
                 //error handling
                 $logmessages[] = "Group with name \"{$record->group}\" is already assigned to grouping with name \"{$record->grouping}\".";
             } else {
                 if (!groups_assign_grouping($groupingid, $groupid)) {
                     //should never happen
                 }
                 //collect success message for logging at end of action
                 $logmessages[] = "Assigned group with name \"{$record->group}\" to grouping with name \"{$record->grouping}\".";
             }
         }
     }
     //log success
     $this->fslogger->log_success(implode(' ', $logmessages), 0, $filename, $this->linenumber);
     if (!$this->fslogger->get_logfile_status()) {
         return false;
     }
     return true;
 }