Skip to content

Commit

Permalink
Merge pull request #33 from talview/release/v1.2.0
Browse files Browse the repository at this point in the history
Release/v1.2.0
  • Loading branch information
rakeshprabhu authored Sep 6, 2023
2 parents 4c09282 + d5ad945 commit 7e8570a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 81 deletions.
51 changes: 28 additions & 23 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
* @copyright 2014 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

interface CustomThrowable extends \Throwable
{

Expand All @@ -45,7 +44,9 @@ class CustomException extends \Exception implements CustomThrowable
{

}
class quizaccess_proctor_observer {

class quizaccess_proctor_observer
{

/** @var int indicates that course module was created */
const CM_CREATED = 0;
Expand Down Expand Up @@ -82,19 +83,6 @@ public static function store(\core\event\base $event)
return;
}
$eventdata = new \stdClass();
switch ($event->eventname) {
case '\core\event\course_module_created':
$eventdata->action = self::CM_CREATED;
break;
case '\core\event\course_module_updated':
$eventdata->action = self::CM_UPDATED;
break;
case '\core\event\course_module_deleted':
$eventdata->action = self::CM_DELETED;
break;
default:
return;
}
$quiz = $DB->get_record('quiz', array('id' => $event->other['instanceid']));
$quiz_proctor_settings = $DB->get_record('quizaccess_proctor', array('quizid' => $event->other['instanceid']));
$eventdata->quiz_title = $event->other['name'];
Expand All @@ -113,6 +101,21 @@ public static function store(\core\event\base $event)
$eventdata->timemodified = $quiz->timemodified;
$eventdata->timecreated = $quiz->timecreated;
$eventdata->userid = $event->userid;
switch ($event->eventname) {
case '\core\event\course_module_created':
if ($quiz_proctor_settings->proctortype === 'noproctor')
return;
$eventdata->action = self::CM_CREATED;
break;
case '\core\event\course_module_updated':
$eventdata->action = self::CM_UPDATED;
break;
case '\core\event\course_module_deleted':
$eventdata->action = self::CM_DELETED;
break;
default:
return;
}
try {
$auth_response = self::generate_auth_token($api_base_url, $auth_payload);
if (!$auth_response) {
Expand All @@ -128,11 +131,11 @@ public static function store(\core\event\base $event)
}



private static function generate_auth_token($api_base_url, $payload) {
private static function generate_auth_token($api_base_url, $payload)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $api_base_url.'/auth',
CURLOPT_URL => $api_base_url . '/auth',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
Expand Down Expand Up @@ -161,10 +164,11 @@ private static function generate_auth_token($api_base_url, $payload) {
}
}

private static function send_quiz_details($api_base_url, $token, $eventdata) {
private static function send_quiz_details($api_base_url, $token, $eventdata)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $api_base_url.'/quiz',
CURLOPT_URL => $api_base_url . '/quiz',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
Expand All @@ -173,7 +177,7 @@ private static function send_quiz_details($api_base_url, $token, $eventdata) {
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($eventdata),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer ".$token,
"Authorization: Bearer " . $token,
"Content-Type: application/json"
],
]);
Expand All @@ -194,8 +198,9 @@ private static function send_quiz_details($api_base_url, $token, $eventdata) {
}
}

public static function capture_error (\Throwable $err) {
\Sentry\init(['dsn' => 'https://[email protected]/5304587' ]);
public static function capture_error(\Throwable $err)
{
\Sentry\init(['dsn' => 'https://[email protected]/5304587']);
\Sentry\captureException($err);
}
}
44 changes: 26 additions & 18 deletions classes/quiz_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quiz_settings extends persistent {
class quiz_settings extends persistent
{

/** Table name for the persistent. */
const TABLE = 'quizaccess_proctor';
Expand All @@ -56,7 +57,8 @@ class quiz_settings extends persistent {
*
* @return array
*/
protected static function define_properties() : array {
protected static function define_properties(): array
{
return [
'quizid' => [
'type' => PARAM_INT,
Expand All @@ -77,10 +79,10 @@ protected static function define_properties() : array {
'default' => '',
],
'instructions' => [
'type' => PARAM_TEXT,
'type' => PARAM_RAW,
'default' => '',
],

];
}

Expand All @@ -92,15 +94,17 @@ protected static function define_properties() : array {
* @param int $quizid Quiz id.
* @return false|\quizaccess_proctor\quiz_settings
*/
public static function get_by_quiz_id(int $quizid) {
public static function get_by_quiz_id(int $quizid)
{
return self::get_record(['quizid' => $quizid]);
}


/**
* Use the boolean map to add Moodle boolean setting to config PList.
*/
private function process_bool_settings() {
private function process_bool_settings()
{
$settings = $this->to_record();
$map = $this->get_bool_proctor_setting_map();
foreach ($settings as $setting => $value) {
Expand All @@ -115,7 +119,8 @@ private function process_bool_settings() {
*
* @param string $name Setting name matching one from self::get_bool_proctor_setting_map.
*/
private function process_bool_setting(string $name) {
private function process_bool_setting(string $name)
{
$map = $this->get_bool_proctor_setting_map();

if (!isset($map[$name])) {
Expand All @@ -126,12 +131,12 @@ private function process_bool_setting(string $name) {
$this->plist->set_or_update_value($map[$name], new CFBoolean($enabled));
}



/**
* Turn return separated strings for URL filters into a PList array and add to config PList.
*/
private function process_url_filters() {
private function process_url_filters()
{
$settings = $this->to_record();
// Create rules to each expression provided and add to config.
$urlfilterrules = [];
Expand Down Expand Up @@ -163,22 +168,24 @@ private function process_url_filters() {
* @param bool $isregex Regex or simple.
* @return CFDictionary A PList dictionary.
*/
private function create_filter_rule(string $rulestring, bool $allowed, bool $isregex) : CFDictionary {
private function create_filter_rule(string $rulestring, bool $allowed, bool $isregex): CFDictionary
{
$action = $allowed ? 1 : 0;
return new CFDictionary([
'action' => new CFNumber($action),
'active' => new CFBoolean(true),
'expression' => new CFString(trim($rulestring)),
'regex' => new CFBoolean($isregex),
]);
'action' => new CFNumber($action),
'active' => new CFBoolean(true),
'expression' => new CFString(trim($rulestring)),
'regex' => new CFBoolean($isregex),
]);
}

/**
* Map the settings that are booleans to the Safe Exam Browser config keys.
*
* @return array Moodle setting as key, proctor setting as value.
*/
private function get_bool_proctor_setting_map() : array {
private function get_bool_proctor_setting_map(): array
{
return [
'activateurlfiltering' => 'URLFilterEnable',
'allowspellchecking' => 'allowSpellCheck',
Expand All @@ -202,7 +209,8 @@ private function get_bool_proctor_setting_map() : array {
* @param string|null $keys the allowed keys.
* @return array of string, the separate keys.
*/
private function split_keys($keys) : array {
private function split_keys($keys): array
{
$keys = preg_split('~[ \t\n\r,;]+~', $keys, -1, PREG_SPLIT_NO_EMPTY);
foreach ($keys as $i => $key) {
$keys[$i] = strtolower($key);
Expand Down
Loading

0 comments on commit 7e8570a

Please sign in to comment.