Skip to content

Commit

Permalink
Merge pull request #13 from examus/hyper
Browse files Browse the repository at this point in the history
Various work on the plugin
  • Loading branch information
DikNuken authored Nov 11, 2019
2 parents dc52373 + 0705ab4 commit c38fe6c
Show file tree
Hide file tree
Showing 21 changed files with 820 additions and 277 deletions.
51 changes: 51 additions & 0 deletions classes/common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace availability_examus;
use \stdClass;
defined('MOODLE_INTERNAL') || die();

class common {
public static function reset_entry($conditions){
global $DB;
$oldentry = $DB->get_record('availability_examus', $conditions);
if ($oldentry and $oldentry->status != 'Not inited') {
$entries = $DB->get_records('availability_examus', [
'userid' => $oldentry->userid,
'courseid' => $oldentry->courseid,
'cmid' => $oldentry->cmid,
'status' => 'Not inited']);
if (count($entries) == 0) {
$timenow = time();
$entry = new stdClass();
$entry->userid = $oldentry->userid;
$entry->courseid = $oldentry->courseid;
$entry->cmid = $oldentry->cmid;
$entry->accesscode = md5(uniqid(rand(), 1));
$entry->status = 'Not inited';
$entry->timecreated = $timenow;
$entry->timemodified = $timenow;
$DB->insert_record('availability_examus', $entry);
return true;
} else {
return false;
}
}
}

public static function delete_empty_entries($userid, $courseid, $cmid = null){
global $DB;

$condition = [
'userid' => $userid,
'courseid' => $courseid,
'status' => 'Not inited'
];

if(!empty($cmid)) {
$condition['cmid'] = $cmid;
}

$DB->delete_records('availability_examus', $condition);
}


}
72 changes: 24 additions & 48 deletions classes/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ class condition extends \core_availability\condition {
* @param stdClass $structure Structure
*/
public function __construct($structure) {
$manual_modes = ['normal', 'identification'];

if (!empty($structure->duration)) {
$this->duration = $structure->duration;
}
if (!empty($structure->mode)) {
$this->mode = $structure->mode;
}
if (array_key_exists("scheduling_required", $structure)) {
$this->scheduling_required = $structure->scheduling_required;
}

$this->scheduling_required = in_array($this->mode, $manual_modes);

if (!empty($structure->rules)) {
$this->rules = $structure->rules;
}
Expand All @@ -78,11 +80,23 @@ public function __construct($structure) {
* @param int $cmid Cm id
*/
private static function delete_empty_entry($userid, $courseid, $cmid) {
global $DB;
$DB->delete_records('availability_examus', array(
'userid' => $userid, 'courseid' => $courseid, 'cmid' => $cmid, 'status' => 'Not inited'));
common::delete_empty_entries($userid, $courseid, $cmid);
}

/**
* delete empty entry for cm
*
* @param int $userid User id
* @param stdClass $cm Cm
* @return bool
*/
public static function delete_empty_entry_for_cm($userid, $cm) {
$course = $cm->get_course();
$courseid = $course->id;
self::delete_empty_entry($userid, $courseid, $cm->id);
}


/**
* has examus condition
*
Expand Down Expand Up @@ -165,6 +179,7 @@ private static function get_examus_conditions($cm) {
*/
public function save() {
return (object) [
'type' => 'examus',
'duration' => (int) $this->duration,
'mode' => (string) $this->mode,
'scheduling_required' => (bool) $this->scheduling_required,
Expand Down Expand Up @@ -233,17 +248,6 @@ protected function get_debug_string() {
return in_array('examus', $_SESSION) ? 'YES' : 'NO';
}

/**
* course mudule deleted handler
*
* @param \core\event\course_module_deleted $event Event
*/
public static function course_module_deleted(\core\event\course_module_deleted $event) {
global $DB;
$cmid = $event->contextinstanceid;
$DB->delete_records('availability_examus', array('cmid' => $cmid));
}

/**
* create entry for cm
*
Expand All @@ -257,18 +261,6 @@ public static function create_entry_for_cm($userid, $cm) {
return self::create_entry_if_not_exist($userid, $courseid, $cm->id);
}

/**
* delete empty entry for cm
*
* @param int $userid User id
* @param stdClass $cm Cm
* @return bool
*/
public static function delete_empty_entry_for_cm($userid, $cm) {
$course = $cm->get_course();
$courseid = $course->id;
self::delete_empty_entry($userid, $courseid, $cm->id);
}

public static function make_entry($courseid, $cmid, $userid=null) {
$timenow = time();
Expand All @@ -295,9 +287,10 @@ public static function make_entry($courseid, $cmid, $userid=null) {
private static function create_entry_if_not_exist($userid, $courseid, $cmid) {
// TODO: refactor this to get courseid and duration from cm.
global $DB;

$entries = $DB->get_records(
'availability_examus',
array('userid' => $userid, 'courseid' => $courseid, 'cmid' => $cmid),
['userid' => $userid, 'courseid' => $courseid, 'cmid' => $cmid],
$sort = 'id');

if (count($entries) == 0) {
Expand All @@ -314,21 +307,4 @@ private static function create_entry_if_not_exist($userid, $courseid, $cmid) {
return null;
}

/**
* user enrolment deleted handles
*
* @param \core\event\user_enrolment_deleted $event Event
*/
public static function user_enrolment_deleted(\core\event\user_enrolment_deleted $event) {
$cmid = $event->contextinstanceid;
$course = get_course($event->courseid);
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->get_cm($cmid);
$userid = $event->relateduserid;

if (self::has_examus_condition($cm)) {
self::delete_empty_entry($userid, $event->courseid, $cmid);
}
}

}
}
13 changes: 7 additions & 6 deletions classes/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ class frontend extends \core_availability\frontend {
* @return array
*/
protected function get_javascript_strings() {
return ['title', 'error_setduration', 'duration', 'link', 'mode', 'normal_mode',
'rules', 'olympics_mode', 'identification_mode', 'allow_to_use_websites',
'allow_to_use_books', 'allow_to_use_paper', 'allow_to_use_messengers',
'allow_to_use_calculator', 'allow_to_use_excel', 'allow_to_use_human_assistant',
'allow_absence_in_frame', 'allow_voices', 'allow_wrong_gaze_direction',
'scheduling_required'];
return [
'title', 'error_setduration', 'duration', 'link', 'mode', 'normal_mode',
'rules', 'olympics_mode', 'identification_mode', 'allow_to_use_websites',
'allow_to_use_books', 'allow_to_use_paper', 'allow_to_use_messengers',
'allow_to_use_calculator', 'allow_to_use_excel', 'allow_to_use_human_assistant',
'allow_absence_in_frame', 'allow_voices', 'allow_wrong_gaze_direction'
];
}

/**
Expand Down
Loading

0 comments on commit c38fe6c

Please sign in to comment.