Exemplo n.º 1
0
 /**
  * Group created
  *
  * @param \core\event\group_created $event
  * @return void
  */
 public static function group_created(\core\event\group_created $event)
 {
     global $DB;
     $group = $event->get_record_snapshot('groups', $event->objectid);
     $courseids = local_metagroups_parent_courses($group->courseid);
     foreach ($courseids as $courseid) {
         $course = get_course($courseid);
         // If parent course doesn't use groups, we can skip synchronization.
         if (groups_get_course_groupmode($course) == NOGROUPS) {
             continue;
         }
         if (!$DB->record_exists('groups', array('courseid' => $course->id, 'idnumber' => $group->id))) {
             $metagroup = new \stdClass();
             $metagroup->courseid = $course->id;
             $metagroup->idnumber = $group->id;
             $metagroup->name = $group->name;
             groups_create_group($metagroup, false, false);
         }
     }
 }
Exemplo n.º 2
0
/**
 * Add a new group
 *
 * @param stdClass $data group properties
 * @param stdClass $editform
 * @param array $editoroptions
 * @return id of group or false if error
 */
function groups_create_group($data, $editform = false, $editoroptions = false)
{
    global $CFG, $DB;
    //check that courseid exists
    $course = $DB->get_record('course', array('id' => $data->courseid), '*', MUST_EXIST);
    $context = context_course::instance($course->id);
    $data->timecreated = time();
    $data->timemodified = $data->timecreated;
    $data->name = trim($data->name);
    if (isset($data->idnumber)) {
        $data->idnumber = trim($data->idnumber);
        if (groups_get_group_by_idnumber($course->id, $data->idnumber)) {
            throw new moodle_exception('idnumbertaken');
        }
    }
    if ($editform and $editoroptions) {
        $data->description = $data->description_editor['text'];
        $data->descriptionformat = $data->description_editor['format'];
    }
    $data->id = $DB->insert_record('groups', $data);
    if ($editform and $editoroptions) {
        // Update description from editor with fixed files
        $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context, 'group', 'description', $data->id);
        $upd = new stdClass();
        $upd->id = $data->id;
        $upd->description = $data->description;
        $upd->descriptionformat = $data->descriptionformat;
        $DB->update_record('groups', $upd);
    }
    $group = $DB->get_record('groups', array('id' => $data->id));
    if ($editform) {
        groups_update_group_icon($group, $data, $editform);
    }
    // Invalidate the grouping cache for the course
    cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($course->id));
    // Trigger group event.
    $params = array('context' => $context, 'objectid' => $group->id);
    $event = \core\event\group_created::create($params);
    $event->add_record_snapshot('groups', $group);
    $event->trigger();
    return $group->id;
}
Exemplo n.º 3
0
 /**
  * group_created
  *
  * @param  \core\event\group_created $event Event object containing useful data
  * @return bool true if success
  */
 public static function group_created(\core\event\group_created $event)
 {
     global $DB;
     $data = $event->get_record_snapshot('groups', $event->objectid);
     $course = $DB->get_record('course', array('id' => $data->courseid));
     if (!($grouptools = get_all_instances_in_course('grouptool', $course))) {
         return true;
     }
     $sortorder = $DB->get_records_sql("SELECT agrp.grouptoolid, MAX(agrp.sort_order) AS max\n                                             FROM {grouptool_agrps} agrp\n                                         GROUP BY agrp.grouptoolid");
     foreach ($grouptools as $grouptool) {
         $newagrp = new StdClass();
         $newagrp->grouptoolid = $grouptool->id;
         $newagrp->groupid = $data->id;
         if (!array_key_exists($grouptool->id, $sortorder)) {
             $newagrp->sort_order = 1;
         } else {
             $newagrp->sort_order = $sortorder[$grouptool->id]->max + 1;
         }
         $newagrp->active = 0;
         if (!$DB->record_exists('grouptool_agrps', array('grouptoolid' => $grouptool->id, 'groupid' => $data->id))) {
             $newagrp->id = $DB->insert_record('grouptool_agrps', $newagrp);
             // Trigger event!
             $cm = get_coursemodule_from_instance('grouptool', $grouptool->id);
             \mod_grouptool\event\agrp_created::create_from_object($cm, $newagrp)->trigger();
         }
     }
     return true;
 }