Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 22... Línea 22...
22
 * @author     2021, Guillermo Gomez Arias <guillermogomez@catalyst-au.net>
22
 * @author     2021, Guillermo Gomez Arias <guillermogomez@catalyst-au.net>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
24
 */
Línea 25... Línea 25...
25
 
25
 
26
require_once(__DIR__ . '/../../../config.php');
26
require_once(__DIR__ . '/../../../config.php');
Línea -... Línea 27...
-
 
27
require_once($CFG->dirroot . '/question/editlib.php');
-
 
28
 
27
require_once($CFG->dirroot."/question/editlib.php");
29
use core_question\output\qbank_actionbar;
28
 
30
use core_question\category_manager;
-
 
31
use qbank_managecategories\form\question_move_form;
-
 
32
use qbank_managecategories\helper;
29
use qbank_managecategories\form\question_move_form;
33
use qbank_managecategories\output\categories;
Línea 30... Línea 34...
30
use qbank_managecategories\helper;
34
use qbank_managecategories\output\categories_header;
31
use qbank_managecategories\question_category_object;
35
use qbank_managecategories\question_categories;
Línea -... Línea 36...
-
 
36
 
-
 
37
require_login();
-
 
38
core_question\local\bank\helper::require_plugin_enabled(helper::PLUGINNAME);
-
 
39
 
-
 
40
// Since Moodle 5.0 any request with the courseid parameter is deprecated and will redirect to the banks management page.
32
 
41
if ($courseid = optional_param('courseid', 0, PARAM_INT)) {
33
require_login();
42
    redirect(new moodle_url('/question/banks.php', ['courseid' => $courseid]));
Línea 34... Línea -...
34
core_question\local\bank\helper::require_plugin_enabled(helper::PLUGINNAME);
-
 
35
 
-
 
36
list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
-
 
37
        question_edit_setup('categories', '/question/bank/managecategories/category.php');
-
 
38
 
-
 
39
// Get values from form for actions on this page.
-
 
40
$param = new stdClass();
43
}
41
$param->moveup = optional_param('moveup', 0, PARAM_INT);
-
 
42
$param->movedown = optional_param('movedown', 0, PARAM_INT);
-
 
43
$param->moveupcontext = optional_param('moveupcontext', 0, PARAM_INT);
-
 
44
$param->movedowncontext = optional_param('movedowncontext', 0, PARAM_INT);
-
 
45
$param->tocontext = optional_param('tocontext', 0, PARAM_INT);
-
 
46
$param->left = optional_param('left', 0, PARAM_INT);
-
 
47
$param->right = optional_param('right', 0, PARAM_INT);
-
 
48
$param->delete = optional_param('delete', 0, PARAM_INT);
-
 
49
$param->confirm = optional_param('confirm', 0, PARAM_INT);
-
 
50
$param->cancel = optional_param('cancel', '', PARAM_ALPHA);
-
 
51
$param->move = optional_param('move', 0, PARAM_INT);
-
 
52
$param->moveto = optional_param('moveto', 0, PARAM_INT);
-
 
53
$param->edit = optional_param('edit', null, PARAM_INT);
-
 
54
 
-
 
55
$url = new moodle_url($thispageurl);
-
 
56
foreach ((array)$param as $key => $value) {
-
 
57
    if (($key !== 'cancel' && $key !== 'edit' && $value !== 0) ||
-
 
58
            ($key === 'cancel' && $value !== '') ||
-
 
Línea 59... Línea -...
59
            ($key === 'edit' && $value !== null)) {
-
 
60
        $url->param($key, $value);
-
 
61
    }
44
 
62
}
-
 
63
$PAGE->set_url($url);
-
 
64
 
-
 
65
$qcobject = new question_category_object($pagevars['cpage'], $thispageurl,
-
 
66
        $contexts->having_one_edit_tab_cap('categories'), $param->edit,
-
 
67
        $pagevars['cat'], $param->delete, $contexts->having_cap('moodle/question:add'));
-
 
68
 
-
 
69
if ($param->left || $param->right || $param->moveup || $param->movedown) {
-
 
70
    require_sesskey();
-
 
Línea 71... Línea 45...
71
 
45
list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
72
    foreach ($qcobject->editlists as $list) {
46
    question_edit_setup('categories', '/question/bank/managecategories/category.php');
Línea 73... Línea -...
73
        // Processing of these actions is handled in the method where appropriate and page redirects.
-
 
74
        $list->process_actions($param->left, $param->right, $param->moveup, $param->movedown);
-
 
75
    }
-
 
76
}
-
 
77
 
-
 
78
if ($param->moveupcontext || $param->movedowncontext) {
-
 
79
    require_sesskey();
-
 
80
 
-
 
81
    if ($param->moveupcontext) {
-
 
82
        $catid = $param->moveupcontext;
-
 
83
    } else {
-
 
84
        $catid = $param->movedowncontext;
47
 
85
    }
-
 
86
    $newtopcat = question_get_top_category($param->tocontext);
-
 
87
    if (!$newtopcat) {
-
 
88
        throw new moodle_exception('invalidcontext');
-
 
89
    }
-
 
90
    $oldcat = $DB->get_record('question_categories', ['id' => $catid], '*', MUST_EXIST);
-
 
91
    // Log the move to another context.
-
 
92
    $category = new stdClass();
-
 
93
    $category->id = explode(',', $pagevars['cat'], -1)[0];
-
 
Línea 94... Línea 48...
94
    $category->contextid = $param->tocontext;
48
$thiscontext = context_module::instance($cmid)->id;
95
    $event = \core\event\question_category_moved::create_from_question_category_instance($category);
49
 
96
    $event->trigger();
50
$todelete = optional_param('delete', 0, PARAM_INT); // The ID of a category to delete.
97
    // Update the set_reference records when moving a category to a different context.
51
 
Línea 98... Línea 52...
98
    move_question_set_references($catid, $catid, $oldcat->contextid, $category->contextid);
52
$PAGE->set_url($thispageurl);
Línea 99... Línea 53...
99
    $qcobject->update_category($catid, "{$newtopcat->id},{$param->tocontext}", $oldcat->name, $oldcat->info);
53
$PAGE->add_body_class('limitedwidth');
Línea 100... Línea 54...
100
    // The previous line does a redirect().
54
 
101
}
55
$manager = new category_manager($thispageurl);
102
 
56
 
103
if ($param->delete) {
57
if ($todelete) {
104
    if (!$category = $DB->get_record("question_categories", ["id" => $param->delete])) {
58
    if (!$category = $DB->get_record("question_categories", ["id" => $todelete])) {
105
        throw new moodle_exception('nocate', 'question', $thispageurl->out(), $param->delete);
59
        throw new moodle_exception('nocate', 'question', $thispageurl->out(), $todelete);
-
 
60
    }
-
 
61
 
-
 
62
    helper::question_remove_stale_questions_from_category($todelete);
-
 
63
 
-
 
64
    $questionstomove = count($manager->get_real_question_ids_in_category($todelete));
-
 
65
 
106
    }
66
    // Second pass, if we still have questions to move, setup the form.
107
 
67
    if ($questionstomove) {
108
    helper::question_remove_stale_questions_from_category($param->delete);
68
        $categorycontext = context::instance_by_id($category->contextid);
109
 
69
        $moveform = new question_move_form($thispageurl,
110
    $questionstomove = count($qcobject->get_real_question_ids_in_category($param->delete));
70
            ['contexts' => [$categorycontext], 'currentcat' => "$todelete"]);
111
 
71
        if ($moveform->is_cancelled()) {
112
    // Second pass, if we still have questions to move, setup the form.
72
            $thispageurl->remove_all_params();
113
    if ($questionstomove) {
73
            if (!is_null($cmid)) {
114
        $categorycontext = context::instance_by_id($category->contextid);
74
                $thispageurl->param('cmid', $cmid);
115
        $moveform = new question_move_form($thispageurl,
75
            } else {
116
            ['contexts' => [$categorycontext], 'currentcat' => $param->delete]);
76
                $thispageurl->param('courseid', $courseid);
Línea 117... Línea -...
117
        if ($moveform->is_cancelled()) {
-
 
118
            redirect($thispageurl);
-
 
119
        } else if ($formdata = $moveform->get_data()) {
-
 
120
            list($tocategoryid, $tocontextid) = explode(',', $formdata->category);
-
 
121
            $qcobject->move_questions_and_delete_category($formdata->delete, $tocategoryid);
-
 
122
            $thispageurl->remove_params('cat', 'category');
-
 
123
            redirect($thispageurl);
-
 
124
        }
-
 
125
    }
-
 
126
} else {
-
 
127
    $questionstomove = 0;
-
 
128
}
-
 
129
 
-
 
130
if ($qcobject->catform->is_cancelled()) {
77
            }
131
    redirect($thispageurl);
78
            redirect($thispageurl);
132
} else if ($catformdata = $qcobject->catform->get_data()) {
79
        } else if ($formdata = $moveform->get_data()) {
133
    $catformdata->infoformat = $catformdata->info['format'];
80
            list($tocategoryid, $tocontextid) = explode(',', $formdata->category);
134
    $catformdata->info       = $catformdata->info['text'];
81
            $manager->move_questions_and_delete_category($formdata->delete, $tocategoryid);
Línea 135... Línea -...
135
    if (!$catformdata->id) {// New category.
-
 
136
        $qcobject->add_category($catformdata->parent, $catformdata->name,
-
 
137
                $catformdata->info, false, $catformdata->infoformat, $catformdata->idnumber);
-
 
138
    } else {
-
 
139
        $qcobject->update_category($catformdata->id, $catformdata->parent,
-
 
140
                $catformdata->name, $catformdata->info, $catformdata->infoformat, $catformdata->idnumber);
-
 
141
    }
82
            $thispageurl->remove_params('cat', 'category');
142
    redirect($thispageurl);
83
            redirect($thispageurl);
143
} else if ((!empty($param->delete) and (!$questionstomove) and confirm_sesskey())) {
84
        }
Línea 144... Línea -...
144
    $qcobject->delete_category($param->delete);// Delete the category now no questions to move.
-
 
145
    $thispageurl->remove_params('cat', 'category');
-
 
146
    redirect($thispageurl);
85
    }
147
}
86
} else {
Línea 148... Línea -...
148
 
-
 
149
if ($param->edit !== null || $qcobject->catform->is_submitted()) {
87
    $questionstomove = 0;
150
    // In the is_submitted case, we only get here if it was submitted,
-
 
151
    // but not valid, so we need to show the validation error.
88
}
152
    $PAGE->navbar->add(get_string('editingcategory', 'question'));
-
 
153
}
89
 
154
 
-
 
155
$PAGE->set_title(get_string('editcategories', 'question'));
-
 
156
$PAGE->set_heading($COURSE->fullname);
-
 
157
$PAGE->activityheader->disable();
-
 
158
 
-
 
159
echo $OUTPUT->header();
-
 
160
 
-
 
161
// Print horizontal nav if needed.
90
if ((!empty($todelete) && (!$questionstomove) && confirm_sesskey())) {
162
$renderer = $PAGE->get_renderer('core_question', 'bank');
91
    $manager->delete_category($todelete);// Delete the category now no questions to move.
163
 
92
    $thispageurl->remove_params('cat', 'category');
164
$qbankaction = new \core_question\output\qbank_action_menu($url);
93
    redirect($thispageurl);
165
if (is_null($param->edit)) {
94
}
166
    $actionurl = new moodle_url($url, ['edit' => 0]);
95
 
167
    $qbankaction->set_action_button($actionurl, get_string('addcategory', 'question'));
96
$PAGE->set_title(get_string('editcategories', 'question'));
-
 
97
$PAGE->set_heading($COURSE->fullname);
-
 
98
$PAGE->activityheader->disable();
-
 
99
 
168
}
100
// Print horizontal nav if needed.
169
echo $renderer->render($qbankaction);
101
$renderer = $PAGE->get_renderer('core_question', 'bank');
-
 
102
 
-
 
103
echo $OUTPUT->header();
-
 
104
$qbankaction = new \core_question\output\qbank_action_menu($thispageurl);
-
 
105
echo $renderer->render($qbankaction);
-
 
106
if ($questionstomove) {
-
 
107
    $vars = new stdClass();
-
 
108
    $vars->name = $category->name;
-
 
109
    $vars->count = $questionstomove;
-
 
110
    echo $OUTPUT->box(get_string('categorymove', 'question', $vars), 'generalbox boxaligncenter');
170
 
111
    $moveform->display();
-
 
112
} else {
171
// Display the UI.
113
    // Get module contexts we have capabilities to manage.