group MDL-19798 Updated print_header and build_navigation to OUTPUT and PAGE equivalents
[moodle.git] / group / grouping.php
1 <?php
2 /**
3  * Create grouping OR edit grouping settings.
4  *
5  * @copyright &copy; 2006 The Open University
6  * @author N.D.Freear AT open.ac.uk
7  * @author J.White AT open.ac.uk
8  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9  * @package groups
10  */
11 require_once('../config.php');
12 require_once('lib.php');
13 require_once('grouping_form.php');
15 /// get url variables
16 $courseid = optional_param('courseid', 0, PARAM_INT);
17 $id       = optional_param('id', 0, PARAM_INT);
18 $delete   = optional_param('delete', 0, PARAM_BOOL);
19 $confirm  = optional_param('confirm', 0, PARAM_BOOL);
21 if ($id) {
22     if (!$grouping = $DB->get_record('groupings', array('id'=>$id))) {
23         print_error('invalidgroupid');
24     }
25     $grouping->description = clean_text($grouping->description);
26     if (empty($courseid)) {
27         $courseid = $grouping->courseid;
29     } else if ($courseid != $grouping->courseid) {
30         print_error('invalidcourseid');
31     }
33     if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
34         print_error('invalidcourseid');
35     }
37 } else {
38     if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
39         print_error('invalidcourseid');
40     }
41     $grouping = new object();
42     $grouping->courseid = $course->id;
43 }
45 require_login($course);
46 $context = get_context_instance(CONTEXT_COURSE, $course->id);
47 require_capability('moodle/course:managegroups', $context);
49 $returnurl = $CFG->wwwroot.'/group/groupings.php?id='.$course->id;
52 if ($id and $delete) {
53     if (!$confirm) {
54         $PAGE->set_title(get_string('deletegrouping', 'group'));
55         $PAGE->set_heading(get_string('deletegrouping', 'group'));
56         echo $OUTPUT->header();
57         $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
58         $optionsno  = array('id'=>$courseid);
59         $formcontinue = html_form::make_button('grouping.php', $optionsyes, get_string('yes'), 'get');
60         $formcancel = html_form::make_button('groupings.php', $optionsno, get_string('no'), 'get');
61         echo $OUTPUT->confirm(get_string('deletegroupingconfirm', 'group', $grouping->name), $formcontinue, $formcancel);
62         echo $OUTPUT->footer();
63         die;
65     } else if (confirm_sesskey()){
66         if (groups_delete_grouping($id)) {
67             redirect($returnurl);
68         } else {
69             print_error('erroreditgrouping', 'group', $returnurl);
70         }
71     }
72 }
74 /// First create the form
75 $editform = new grouping_form();
76 $editform->set_data($grouping);
78 if ($editform->is_cancelled()) {
79     redirect($returnurl);
81 } elseif ($data = $editform->get_data()) {
82     $success = true;
84     if ($data->id) {
85         groups_update_grouping($data);
87     } else {
88         groups_create_grouping($data);
89     }
91     redirect($returnurl);
93 }
95 $strgroupings    = get_string('groupings', 'group');
96 $strparticipants = get_string('participants');
98 if ($id) {
99     $strheading = get_string('editgroupingsettings', 'group');
100 } else {
101     $strheading = get_string('creategrouping', 'group');
104 $PAGE->navbar->add($strparticipants, new moodle_url($CFG->wwwroot.'/user/index.php', array('id'=>$courseid)));
105 $PAGE->navbar->add($strgroups, new moodle_url($CFG->wwwroot.'/group/groupings.php', array('id'=>$courseid)));
106 $PAGE->navbar->add($strheading);
108 /// Print header
109 $PAGE->set_title($strgroupings);
110 $PAGE->set_heading(': '.$strgroupings);
111 echo $OUTPUT->header();
112 echo $OUTPUT->heading($strheading);
113 $editform->display();
114 echo $OUTPUT->footer();
116 ?>