Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Cohort related management functions, this file needs to be included manually.
19
 *
20
 * @package    core_cohort
21
 * @copyright  2010 Petr Skoda  {@link http://skodak.org}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
require('../config.php');
26
require_once($CFG->dirroot.'/course/lib.php');
27
require_once($CFG->dirroot.'/cohort/lib.php');
28
require_once($CFG->dirroot.'/cohort/edit_form.php');
29
 
30
$id        = optional_param('id', 0, PARAM_INT);
31
$contextid = optional_param('contextid', 0, PARAM_INT);
32
$show      = optional_param('show', 0, PARAM_BOOL);
33
$hide      = optional_param('hide', 0, PARAM_BOOL);
34
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
35
 
36
require_login();
37
 
38
$category = null;
39
if ($id) {
40
    $cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
41
    $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
42
} else {
43
    $context = context::instance_by_id($contextid, MUST_EXIST);
44
    if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
45
        throw new \moodle_exception('invalidcontext');
46
    }
47
    $cohort = new stdClass();
48
    $cohort->id          = 0;
49
    $cohort->contextid   = $context->id;
50
    $cohort->name        = '';
51
    $cohort->description = '';
52
}
53
 
54
require_capability('moodle/cohort:manage', $context);
55
 
56
if ($returnurl) {
57
    $returnurl = new moodle_url($returnurl);
58
} else {
59
    $returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
60
}
61
 
62
if (!empty($cohort->component)) {
63
    // We can not manually edit cohorts that were created by external systems, sorry.
64
    redirect($returnurl);
65
}
66
 
67
$PAGE->set_context($context);
68
$baseurl = new moodle_url('/cohort/edit.php', array('contextid' => $context->id, 'id' => $cohort->id));
69
$PAGE->set_url($baseurl);
70
$PAGE->set_context($context);
71
$PAGE->set_pagelayout('admin');
72
 
73
if ($context->contextlevel == CONTEXT_COURSECAT) {
74
    core_course_category::page_setup();
75
    // Set the cohorts node active in the settings navigation block.
76
    if ($cohortsnode = $PAGE->settingsnav->find('cohort', navigation_node::TYPE_SETTING)) {
77
        $cohortsnode->make_active();
78
    }
79
 
80
    $PAGE->set_secondary_active_tab('cohort');
81
 
82
} else {
83
    navigation_node::override_active_url(new moodle_url('/cohort/index.php', array()));
84
    $PAGE->set_heading($COURSE->fullname);
85
}
86
 
87
if ($show && $cohort->id && confirm_sesskey()) {
88
    if (!$cohort->visible) {
89
        $record = (object)array('id' => $cohort->id, 'visible' => 1, 'contextid' => $cohort->contextid);
90
        cohort_update_cohort($record);
91
    }
92
    redirect($returnurl);
93
}
94
 
95
if ($hide && $cohort->id && confirm_sesskey()) {
96
    if ($cohort->visible) {
97
        $record = (object)array('id' => $cohort->id, 'visible' => 0, 'contextid' => $cohort->contextid);
98
        cohort_update_cohort($record);
99
    }
100
    redirect($returnurl);
101
}
102
 
103
$editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES,
104
    'maxbytes' => $SITE->maxbytes, 'context' => $context);
105
if ($cohort->id) {
106
    // Edit existing.
107
    $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions,
108
            $context, 'cohort', 'description', $cohort->id);
109
    $strheading = get_string('editcohort', 'cohort');
110
 
111
} else {
112
    // Add new.
113
    $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions,
114
            $context, 'cohort', 'description', null);
115
    $strheading = get_string('addcohort', 'cohort');
116
}
117
 
118
$PAGE->set_title($strheading);
119
$PAGE->navbar->add($strheading);
120
 
121
$editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort, 'returnurl'=>$returnurl));
122
 
123
if ($editform->is_cancelled()) {
124
    redirect($returnurl);
125
 
126
} else if ($data = $editform->get_data()) {
127
    $oldcontextid = $context->id;
128
    $editoroptions['context'] = $context = context::instance_by_id($data->contextid);
129
 
130
    if ($data->id) {
131
        if ($data->contextid != $oldcontextid) {
132
            // Cohort was moved to another context.
133
            get_file_storage()->move_area_files_to_new_context($oldcontextid, $context->id,
134
                    'cohort', 'description', $data->id);
135
        }
136
        $data = file_postupdate_standard_editor($data, 'description', $editoroptions,
137
                $context, 'cohort', 'description', $data->id);
138
        cohort_update_cohort($data);
139
    } else {
140
        $data->descriptionformat = $data->description_editor['format'];
141
        $data->description = $description = $data->description_editor['text'];
142
        $data->id = cohort_add_cohort($data);
143
        $editoroptions['context'] = $context = context::instance_by_id($data->contextid);
144
        $data = file_postupdate_standard_editor($data, 'description', $editoroptions,
145
                $context, 'cohort', 'description', $data->id);
146
        if ($description != $data->description) {
147
            $updatedata = (object)array('id' => $data->id,
148
                'description' => $data->description, 'contextid' => $context->id);
149
            cohort_update_cohort($updatedata);
150
        }
151
    }
152
 
153
    if ($returnurl->get_param('showall') || $returnurl->get_param('contextid') == $data->contextid) {
154
        // Redirect to where we were before.
155
        redirect($returnurl);
156
    } else {
157
        // Use new context id, it has been changed.
158
        redirect(new moodle_url('/cohort/index.php', array('contextid' => $data->contextid)));
159
    }
160
}
161
 
162
echo $OUTPUT->header();
163
echo $OUTPUT->heading($strheading);
164
 
165
if (!$id && ($editcontrols = cohort_edit_controls($context, $baseurl))) {
166
    echo $OUTPUT->render($editcontrols);
167
}
168
 
169
echo $editform->display();
170
echo $OUTPUT->footer();
171