Пример #1
0
 /**
  * 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 . '&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 . '&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 . '&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');
     }
 }