-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_question.php
executable file
·156 lines (133 loc) · 5.49 KB
/
edit_question.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* This file is part of Cria.
* Cria is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Cria is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Cria. If not, see <https://www.gnu.org/licenses/>.
*
* @package local_cria
* @author Patrick Thibaudeau
* @copyright 2024 onwards York University (https://yorku.ca)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("../../config.php");
require_once($CFG->dirroot . "/local/cria/classes/forms/edit_question_form.php");
use local_cria\base;
use local_cria\intent;
use local_cria\question;
global $CFG, $OUTPUT, $USER, $PAGE, $DB, $SITE;
$context = CONTEXT_SYSTEM::instance();
require_login(1, false);
$intent_id = required_param('intent_id', PARAM_INT);
$id = optional_param('id', 0, PARAM_INT);
$parent_id = optional_param('parent_id', 0, PARAM_INT);
$INTENT = new intent($intent_id);
if ($id != 0) {
$formdata = $DB->get_record('local_cria_question', array('id' => $id));
$formdata->bot_id = $INTENT->get_bot_id();
// Get all question examples
$examples = $DB->get_records('local_cria_question_example', array('questionid' => $id));
$formdata->examples = array_values($examples);
$formdata->create_example_questions = false;
$formdata->keywords = json_decode($formdata->keywords);
$formdata->related_prompts = $formdata->related_questions;
$draftid = file_get_submitted_draft_itemid('answereditor');
$currentText = file_prepare_draft_area($draftid, $context->id, 'local_cria', 'answer', $formdata->id, base::getEditorOptions($context), $formdata->answer);
$formdata->answereditor = array('text' => $currentText, 'format' => FORMAT_HTML, 'itemid' => $draftid);
} else {
$formdata = new stdClass();
$formdata->id = 0;
$formdata->intent_id = $INTENT->get_id();
$formdata->bot_id = $INTENT->get_bot_id();
$formdata->lang = $INTENT->get_lang();
$formdata->faculty = $INTENT->get_faculty();
$formdata->program = $INTENT->get_program();
$formdata->examples = false;
$formdata->create_example_questions = true;
$formdata->parent_id = $parent_id;
$formdata->generate_answer = 0;
}
// Create form
$mform = new \local_cria\edit_question_form(null, array('formdata' => $formdata));
if ($mform->is_cancelled()) {
//Handle form cancel operation, if cancel button is present on form
redirect($CFG->wwwroot . '/local/cria/content.php?bot_id=' . $formdata->bot_id);
} else if ($data = $mform->get_data()) {
// Convert keywords to JSON
if (!isset($data->keywords)) {
$data->keywords = [];
$keywords = json_encode($data->keywords);
} else {
$keywords = '';
}
unset($data->keywords);
$data->related_questions = $data->related_prompts;
unset($data->related_prompts);
$data->keywords = $keywords;
$data->usermodified = $USER->id;
$data->timemodified = time();
if ($data->id == 0) {
// Insert record
$data->timecreated = time();
$data->id = $DB->insert_record('local_cria_question', $data);
// Update record adding the question id to the parent_id
$data->parent_id = $data->id;
$DB->update_record('local_cria_question', $data);
} else {
// Update record
$data->published = 0;
$DB->update_record('local_cria_question', $data);
}
if ($data->create_example_questions == true) {
$INTENT = new intent($data->intent_id);
$INTENT->generate_example_questions($data->id);
}
//save editor text
$draftid = file_get_submitted_draft_itemid('answereditor');
$answerText = file_save_draft_area_files(
$draftid,
$context->id,
'local_cria',
'answer',
$data->id,
base::getEditorOptions($context),
$data->answereditor['text']
);
$data->answer = $answerText;
// Update record
$DB->set_field('local_cria_question', 'answer', $data->answer, array('id' => $data->id));
// If save and publish button is set, plublish the question
if (isset($data->submitpublishbutton)) {
$QUESTION = new question($data->id);
$status = $QUESTION->publish();
if ($status->status == 404) {
core\notification::error(get_string('error_publishing_question', 'local_cria'));
}
unset($QUESTION);
}
// Redirect to content page
redirect($CFG->wwwroot . '/local/cria/edit_question.php?id=' . $data->id . '&intent_id=' . $data->intent_id);
} else {
// Show form
$mform->set_data($mform);
}
base::page(
new moodle_url('/local/cria/edit_intent.php', ['bot_id' => $INTENT->get_bot_id(), 'id' => $id]),
get_string('edit_intent', 'local_cria'),
'',
$context,
'standard'
);
$PAGE->requires->css(new moodle_url('/local/cria/css/select2.min.css'));
$PAGE->requires->css(new moodle_url('/local/cria/css/select2-bootstrap4.min.css'));
$PAGE->requires->js_call_amd('local_cria/question_form', 'init', array());
echo $OUTPUT->header();
//**********************
//*** DISPLAY HEADER ***
//
$mform->display();
//**********************
//*** DISPLAY FOOTER ***
//**********************
echo $OUTPUT->footer();
?>