This repository was archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoffline_questions.php
147 lines (117 loc) · 4.71 KB
/
offline_questions.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
<?php
/**
* File: offline_questions.php
* Encoding: UTF8
*
* @package: moodle_mod_openwebinar
*
* @Version: 1.0.0
* @Since 3-1-2017
* @Author : MoodleFreak.com | Ldesign.nl - Luuk Verhoeven
**/
require_once("../../config.php");
require_once(dirname(__FILE__) . '/lib.php');
$id = optional_param('id', 0, PARAM_INT); // Course_module ID, or
$n = optional_param('n', 0, PARAM_INT); // ... openwebinar instance ID - it should be named as the first character of the module.
$action = optional_param('action', false, PARAM_TEXT);
$questionid = optional_param('questionid', 0, PARAM_INT);
if ($id) {
$cm = get_coursemodule_from_id('openwebinar', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$openwebinar = $DB->get_record('openwebinar', array('id' => $cm->instance), '*', MUST_EXIST);
} else {
if ($n) {
$openwebinar = $DB->get_record('openwebinar', array('id' => $n), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $openwebinar->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('openwebinar', $openwebinar->id, $course->id, false, MUST_EXIST);
} else {
error('You must specify a course_module ID or an instance ID');
}
}
// Get context.
$context = context_module::instance($cm->id);
// Print the page header.
$PAGE->set_context($context);
$baseurl = new moodle_url('/mod/openwebinar/offline_questions.php', [
'id' => $cm->id,
'questionid' => $questionid,
'action' => $action,
]);
$PAGE->set_url($baseurl);
require_login($course, true, $cm);
$PAGE->set_title(format_string($openwebinar->name));
$PAGE->set_heading(format_string($course->fullname));
/** @var mod_openwebinar_renderer $renderer */
$renderer = $PAGE->get_renderer('mod_openwebinar');
switch ($action) {
case 'edit':
$form = new \mod_openwebinar\form\addquestion($PAGE->url);
// Set data.
if ($questionid > 0) {
$question = $DB->get_record('openwebinar_question', ['id' => $questionid], '*', MUST_EXIST);
$question->question_data = unserialize($question->question_data);
$data = new stdClass();
$data->question = $question->question_data->question;
$data->summary = $question->question_data->summary;
$data->question_type = $question->question_type;
$data->answers_options = $question->question_data->answers_options;
$data->comment = $question->question_data->comment;
$form->set_data($data);
}
// When cancel btn is pressed.
if ($form->is_cancelled()) {
$baseurl->param('action' , '');
redirect($baseurl);
}
// Set data.
if (($data = $form->get_data()) != false) {
// Serializing question data.
$questionrow = new stdClass();
$questionrow->question = $data->question;
$questionrow->summary = $data->summary;
$questionrow->comment = $data->comment;
$questionrow->answers_options = $data->answers_options;
$data->question_data = serialize($questionrow);
// Serializing user data.
$users = new stdClass();
$users->all_enrolled_users = true;
$users->user_ids = array();
$data->question_users = serialize($users);
if ($questionid > 0) {
$data->id = $questionid;
$DB->update_record('openwebinar_question', $data);
} else {
// We will add a new question.
$data->created_by = $USER->id;
$data->openwebinar_id = $openwebinar->id;
$data->grouptype = 'template';
$data->added_on = time();
$DB->insert_record('openwebinar_question', $data);
}
$baseurl->param('action' , '');
redirect($baseurl);
}
// Output starts here.
echo $OUTPUT->header();
echo $form->display();
echo $OUTPUT->footer();
break;
case 'delete':
$DB->delete_records('openwebinar_question', ['id' => $questionid]);
$baseurl->param('action' , '');
redirect($baseurl);
break;
default:
// Output starts here.
echo $OUTPUT->header();
echo \html_writer::link(new moodle_url('/mod/openwebinar/offline_questions.php', [
'questionid' => 0,
'id' => $cm->id,
'action' => 'edit'
]), get_string('btn:new', 'openwebinar'), [
'class' => 'btn btn-primary'
]);
echo '<hr/>';
echo $renderer->question_table($openwebinar);
echo $OUTPUT->footer();
}