forked from yedidiaklein/moodle-local_aiquestions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstory.php
104 lines (89 loc) · 3.88 KB
/
story.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
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle 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.
//
// Moodle 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 Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
* Plugin administration pages are defined here.
*
* @package local_aiquestions
* @category admin
* @copyright 2023 Ruthy Salomon <[email protected]> , Yedidia Klein <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__ . '/../../config.php');
defined('MOODLE_INTERNAL') || die();
// Get course id for creating the questions in it's bank.
$courseid = optional_param('courseid', 0, PARAM_INT);
if ($courseid == 0) {
redirect(new moodle_url('/local/aiquestions/index.php'));
}
require_login($courseid);
// Check if the user has the capability to create questions.
$context = context_course::instance($courseid);
require_capability('moodle/question:add', $context);
require_once("$CFG->libdir/formslib.php");
require_once(__DIR__ . '/locallib.php');
$PAGE->set_context(context_system::instance());
$PAGE->set_heading(get_string('pluginname', 'local_aiquestions'));
$PAGE->set_title(get_string('pluginname', 'local_aiquestions'));
$PAGE->set_url('/local/aiquestions/story.php?courseid=' . $courseid);
$PAGE->set_pagelayout('standard');
$PAGE->navbar->add(get_string('pluginname', 'local_aiquestions'), new moodle_url('/local/aiquestions/'));
$PAGE->navbar->add(get_string('story', 'local_aiquestions'),
new moodle_url('/local/aiquestions/story.php?courseid=' . $courseid));
$PAGE->requires->js_call_amd('local_aiquestions/state');
echo $OUTPUT->header();
$mform = new local_aiquestions_story_form();
if ($mform->is_cancelled()) {
redirect($CFG->wwwroot . '/course/view.php?id=' . $courseid);
} else if ($data = $mform->get_data()) {
// Call the adhoc task.
$task = new \local_aiquestions\task\questions();
if ($task) {
$uniqid = uniqid($USER->id, true);
$preset = $data->preset;
$primer = 'primer' . $preset;
$instructions = 'instructions' . $preset;
$example = 'example' . $preset;
$task->set_custom_data(['category' => $data->category,
'primer' => $data->$primer,
'instructions' => $data->$instructions,
'example' => $data->$example,
'story' => $data->story,
'numofquestions' => $data->numofquestions,
'courseid' => $data->courseid,
'userid' => $USER->id,
'uniqid' => $uniqid ]);
\core\task\manager::queue_adhoc_task($task);
$success = get_string('tasksuccess', 'local_aiquestions');
} else {
$error = get_string('taskerror', 'local_aiquestions');
}
// Check if the cron is overdue.
$lastcron = get_config('tool_task', 'lastcronstart');
$cronoverdue = ($lastcron < time() - 3600 * 24);
// Prepare the data for the template.
$datafortemplate = [
'courseid' => $courseid,
'wwwroot' => $CFG->wwwroot,
'uniqid' => $uniqid,
'userid' => $USER->id,
'cron' => $cronoverdue,
];
// Load the ready template.
echo $OUTPUT->render_from_template('local_aiquestions/loading', $datafortemplate);
} else {
$mform->display();
}
echo $OUTPUT->footer();