Esempio n. 1
0
 public function process_grouping($data)
 {
     global $DB;
     $data = (object) $data;
     // handy
     $data->courseid = $this->get_courseid();
     // Only allow the idnumber to be set if the user has permission and the idnumber is not already in use by
     // another a grouping in the same course
     $context = context_course::instance($data->courseid);
     if (isset($data->idnumber) and has_capability('moodle/course:changeidnumber', $context, $this->task->get_userid())) {
         if (groups_get_grouping_by_idnumber($data->courseid, $data->idnumber)) {
             unset($data->idnumber);
         }
     } else {
         unset($data->idnumber);
     }
     $oldid = $data->id;
     // need this saved for later
     $restorefiles = false;
     // Only if we end creating the grouping
     // Search if the grouping already exists (by name & description) in the target course
     $description_clause = '';
     $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name);
     if (!empty($data->description)) {
         $description_clause = ' AND ' . $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':description');
         $params['description'] = $data->description;
     }
     if (!($groupingdb = $DB->get_record_sql("SELECT *\n                                                  FROM {groupings}\n                                                 WHERE courseid = :courseid\n                                                   AND name = :grname {$description_clause}", $params))) {
         // grouping doesn't exist, create
         $newitemid = $DB->insert_record('groupings', $data);
         $restorefiles = true;
         // We'll restore the files
     } else {
         // grouping exists, use it
         $newitemid = $groupingdb->id;
     }
     // Save the id mapping
     $this->set_mapping('grouping', $oldid, $newitemid, $restorefiles);
     // Invalidate the course group data cache just in case.
     cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
 }
Esempio n. 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 (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;
 }
Esempio n. 3
0
/**
 * Update grouping
 *
 * @param stdClass $data grouping properties (with magic quotes)
 * @param array $editoroptions
 * @return bool true or exception
 */
function groups_update_grouping($data, $editoroptions = null)
{
    global $DB;
    $data->timemodified = time();
    $data->name = trim($data->name);
    if (isset($data->idnumber)) {
        $data->idnumber = trim($data->idnumber);
        if (($existing = groups_get_grouping_by_idnumber($data->courseid, $data->idnumber)) && $existing->id != $data->id) {
            throw new moodle_exception('idnumbertaken');
        }
    }
    if ($editoroptions !== null) {
        $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $editoroptions['context'], 'grouping', 'description', $data->id);
    }
    $DB->update_record('groupings', $data);
    // Invalidate the group data.
    cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
    //trigger groups events
    events_trigger('groups_grouping_updated', $data);
    return true;
}
 public function test_groups_get_grouping_by_idnumber()
 {
     $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));
     $idnumber1 = 'idnumber1';
     $idnumber2 = 'idnumber2';
     /*
      * Test with an empty and a null idnumber.
      */
     // An empty idnumber should always return a false value.
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
     // Even when a grouping exists which also has an empty idnumber.
     $generator->create_grouping(array('courseid' => $course->id));
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
     /*
      * Test with a valid idnumber
      */
     // There is no matching idnumber at present.
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber1));
     // We should now have a valid group returned by the idnumber search.
     $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber1));
     $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber1));
     // An empty idnumber should still return false.
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
     /*
      * Test with another idnumber.
      */
     // There is no matching idnumber at present.
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber2));
     // We should now have a valid grouping returned by the idnumber search.
     $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2));
     $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber2));
     /*
      * Grouping idnumbers are unique within a course so test that we don't
      * retrieve groupings for the first course.
      */
     // Create a second course.
     $course = $generator->create_course(array('category' => $cat->id));
     // An empty idnumber should always return a false value.
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
     // Our existing idnumbers shouldn't be returned here as we're in a different course.
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber1));
     $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber2));
     // We should be able to reuse the idnumbers again since this is a different course.
     $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber1));
     $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber1));
     $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2));
     $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber2));
 }
 private function process_action_allocation_to_grouping()
 {
     $now = time();
     if ($this->ratingallocate->accesstimestop < $now) {
         global $OUTPUT;
         $allgroupings = groups_get_all_groupings($this->course->id);
         $groupingidname = ratingallocate_MOD_NAME . '_instid_' . $this->ratingallocateid;
         // search if there is already a grouping from us
         $grouping = groups_get_grouping_by_idnumber($this->course->id, $groupingidname);
         $groupingid = null;
         if (!$grouping) {
             // create grouping
             $data = new stdClass();
             $data->name = get_string('groupingname', ratingallocate_MOD_NAME, $this->ratingallocate->name);
             $data->idnumber = $groupingidname;
             $data->courseid = $this->course->id;
             $groupingid = groups_create_grouping($data);
         } else {
             $groupingid = $grouping->id;
         }
         $group_identifier_from_choice_id = function ($choiceid) {
             return ratingallocate_MOD_NAME . '_c_' . $choiceid;
         };
         $choices = $this->get_choices_with_allocationcount();
         // make a new array containing only the identifiers of the choices
         $choice_identifiers = array();
         foreach ($choices as $id => $choice) {
             $choice_identifiers[$group_identifier_from_choice_id($choice->id)] = array('key' => $id);
         }
         // find all associated groups in this grouping
         $groups = groups_get_all_groups($this->course->id, 0, $groupingid);
         // loop through the groups in the grouping: if the choice does not exist anymore -> delete
         // otherwise mark it
         foreach ($groups as $group) {
             if (array_key_exists($group->idnumber, $choice_identifiers)) {
                 // group exists, mark
                 $choice_identifiers[$group->idnumber]['exists'] = true;
                 $choice_identifiers[$group->idnumber]['groupid'] = $group->id;
             } else {
                 // delete group $group->id
                 groups_delete_group($group->id);
             }
         }
         // create groups groups for new identifiers or empty group if it exists
         foreach ($choice_identifiers as $group_idnumber => $choice) {
             if (key_exists('exists', $choice)) {
                 // remove all members
                 groups_delete_group_members_by_group($choice['groupid']);
             } else {
                 $data = new stdClass();
                 $data->courseid = $this->course->id;
                 $data->name = $choices[$choice['key']]->title;
                 $data->idnumber = $group_idnumber;
                 $createdid = groups_create_group($data);
                 groups_assign_grouping($groupingid, $createdid);
                 $choice_identifiers[$group_idnumber]['groupid'] = $createdid;
             }
         }
         // add all participants in the correct group
         $allocations = $this->get_allocations();
         foreach ($allocations as $id => $allocation) {
             $choice_id = $allocation->choiceid;
             $user_id = $allocation->userid;
             $choiceidnumber = $group_identifier_from_choice_id($choice_id);
             groups_add_member($choice_identifiers[$choiceidnumber]['groupid'], $user_id);
         }
         // Invalidate the grouping cache for the course
         cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($this->course->id));
         $renderer = $this->get_renderer();
         $renderer->add_notification(get_string('moodlegroups_created', ratingallocate_MOD_NAME), self::NOTIFY_SUCCESS);
     }
     return $this->process_default();
 }
Esempio n. 6
0
/**
 * Update grouping
 *
 * @param stdClass $data grouping properties (with magic quotes)
 * @param array $editoroptions
 * @return bool true or exception
 */
function groups_update_grouping($data, $editoroptions = null)
{
    global $DB;
    $data->timemodified = time();
    $data->name = trim($data->name);
    if (isset($data->idnumber)) {
        $data->idnumber = trim($data->idnumber);
        if (($existing = groups_get_grouping_by_idnumber($data->courseid, $data->idnumber)) && $existing->id != $data->id) {
            throw new moodle_exception('idnumbertaken');
        }
    }
    if ($editoroptions !== null) {
        $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $editoroptions['context'], 'grouping', 'description', $data->id);
    }
    $DB->update_record('groupings', $data);
    // Invalidate the group data.
    cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
    // Trigger group event.
    $params = array('context' => context_course::instance($data->courseid), 'objectid' => $data->id);
    $event = \core\event\grouping_updated::create($params);
    $event->set_legacy_eventdata($data);
    $event->trigger();
    return true;
}