forked from studentquiz/moodle-mod_studentquiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externallib.php
133 lines (119 loc) · 5.26 KB
/
externallib.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
<?php
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
/**
* Defines external functions for the studentquiz module.
*
* @package mod_studentquiz
* @author Huong Nguyen <[email protected]>
* @copyright 2019 HSR (http://www.hsr.ch)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_studentquiz\local\studentquiz_helper;
use mod_studentquiz\local\studentquiz_question;
use mod_studentquiz\utils;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/studentquiz/locallib.php');
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->libdir . '/questionlib.php');
/**
* Defines external functions for the studentquiz module.
*
* @package mod_studentquiz
* @author Huong Nguyen <[email protected]>
* @copyright 2019 HSR (http://www.hsr.ch)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_studentquiz_external extends external_api {
/**
* Get the required question state parameters.
* @return external_function_parameters
*/
public static function change_question_state_parameters() {
return new external_function_parameters([
'courseid' => new external_value(PARAM_INT, 'Course id', VALUE_REQUIRED),
'cmid' => new external_value(PARAM_INT, 'coursemodule id', VALUE_REQUIRED),
'studentquizquestionid' => new external_value(PARAM_INT, 'id of studentquiz_question table', VALUE_REQUIRED),
'state' => new external_value(PARAM_INT, 'Question state', VALUE_REQUIRED)
]);
}
/**
* Set the question state as provided.
*
* @param int $courseid Course id
* @param int $cmid Course module id
* @param int $studentquizquestionid StudentQuiz-Question id,
* @param int $state State value
* @return array Response
*/
public static function change_question_state($courseid, $cmid, $studentquizquestionid, $state) {
global $PAGE, $USER;
if ($state == studentquiz_helper::STATE_HIDE) {
$type = 'hidden';
$value = 1;
} else if ($state == studentquiz_helper::STATE_DELETE) {
$type = 'deleted';
$value = 1;
} else {
$type = 'state';
$value = $state;
}
// Student can not delete the question when the question is in approved state.
$context = \context_course::instance($courseid);
$canmanage = has_capability('mod/studentquiz:manage', $context);
$contextmodule = context_module::instance($cmid);
$cm = get_coursemodule_from_id('studentquiz', $cmid);
$studentquiz = mod_studentquiz_load_studentquiz($cmid, $contextmodule->id);
$studentquizquestion = new studentquiz_question($studentquizquestionid, null, $studentquiz);
if (!$canmanage && $state == studentquiz_helper::STATE_DELETE) {
if ($studentquizquestion->get_state() == studentquiz_helper::STATE_APPROVED) {
$result = [];
$result['status'] = get_string('api_state_change_error_title', 'studentquiz');
$result['message'] = get_string('api_state_change_error_content', 'studentquiz');
return $result;
}
}
$studentquizquestion->change_state_visibility($type, $value);
$studentquizquestion->save_action($state, $USER->id);
// Additionally always unhide the question when it got approved.
if ($state == studentquiz_helper::STATE_APPROVED && $studentquizquestion->is_hidden()) {
$studentquizquestion->change_state_visibility( 'hidden', 0);
$studentquizquestion->save_action(studentquiz_helper::STATE_SHOW, null);
}
$course = get_course($courseid);
$PAGE->set_context($contextmodule);
if (!$canmanage) {
if ($state == studentquiz_helper::STATE_REVIEWABLE) {
mod_studentquiz_notify_reviewable_question($studentquizquestion, $course, $cm);
}
} else {
mod_studentquiz_state_notify($studentquizquestion, $course, $cm, $type);
}
$result = [];
$result['status'] = get_string('api_state_change_success_title', 'studentquiz');
$result['message'] = get_string('api_state_change_success_content', 'studentquiz');
return $result;
}
/**
* Get available state return fields.
* @return external_single_structure
*/
public static function change_question_state_returns() {
return new external_single_structure([
'status' => new external_value(PARAM_TEXT, 'status'),
'message' => new external_value(PARAM_TEXT, 'message')
]);
}
}