Skip to content

Commit

Permalink
Code checker fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnOLane committed Jul 15, 2024
1 parent 2b3aeb9 commit ed8bf57
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 87 deletions.
2 changes: 1 addition & 1 deletion datelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
$scopemenu = array('activity' => get_string('thisscheduler', 'scheduler'),
'course' => get_string('thiscourse', 'scheduler'),
'site' => get_string('thissite', 'scheduler'));
$select = $output->single_select($taburl, 'scope', $scopemenu, $scope, $nothing = array('' => 'choosedots'), null, array('scopeform'));
$select = $output->single_select($taburl, 'scope', $scopemenu, $scope, $nothing = ['' => 'choosedots'], null, ['scopeform']);
echo html_writer::div(get_string($scopemenukey, 'scheduler', $select), 'dropdownmenu');
}

Expand Down
2 changes: 1 addition & 1 deletion export.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
$data->includeemptyslots,
$pageperteacher,
$data->meetingstart,
$data->meetingend
$data->meetingend
);

$limit = 20;
Expand Down
4 changes: 2 additions & 2 deletions import/sessions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
require_once($CFG->dirroot . '/mod/scheduler/lib.php');
require_once($CFG->dirroot . '/mod/scheduler/locallib.php');

//ADDED
// ADDED.
require_once($CFG->dirroot . '/mod/scheduler/import/sessions_confirm.php');
require_once($CFG->dirroot . '/mod/scheduler/import/sessions_form.php');
require_once($CFG->dirroot . '/mod/scheduler/import/sessions_process.php');
//END OF ADDED
// END OF ADDED.

admin_externalpage_setup('managemodules');
$pagetitle = get_string('importsessions', 'scheduler');
Expand Down
92 changes: 45 additions & 47 deletions import/sessions_process.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function __construct($text = null, $encoding = null, $delimiter = null, $

$session = new stdClass();
$session->course = $this->get_column_data($row, $mapping['course']);
if (empty($session->course)) {
if (empty($session->course)) {
\mod_scheduler_notifyqueue::notify_problem(get_string('error:sessioncourseinvalid', 'scheduler'));
continue;
}
Expand Down Expand Up @@ -250,40 +250,40 @@ public function __construct($text = null, $encoding = null, $delimiter = null, $
//check if am or pm placed
$amsplit = explode(" ",$fromsplit[1]);

if(strlen($amsplit[1]) > 1){
if (strlen($amsplit[1]) > 1) {
//convert to 24 hour
$time24 = date("H:i", strtotime($from));
$time24split = explode(':', $time24);

$session->sestime['starthour'] = $time24split[0];
$session->sestime['startminute'] = $time24split[1];
}else{
} else {
$session->sestime['starthour'] = $fromsplit[0];
$session->sestime['startminute'] = $fromsplit[1];
}

$to = $this->get_column_data($row, $mapping['time']);
if (empty($to)) {
\mod_scheduler_notifyqueue::notify_problem(get_string('error:sessionendinvalid', 'scheduler'));
continue;
}

//ADD DURATION
//ADD DURATION
$duration = $this->get_column_data($row, $mapping['duration']);
if (empty($from)) {
\mod_scheduler_notifyqueue::notify_problem(get_string('error:sessionstartinvalid', 'scheduler'));
continue;
}

$session->duration = clean_param($duration, PARAM_INT);

$studentname = format_text($this->get_column_data($row, $mapping['studentname']),FORMAT_PLAIN);
$studentname = explode(",",$studentname);
$firstname = ltrim($studentname[1]," ");

$session->studentfirstname = $firstname;
$session->studentlastname = $studentname[0];

$session->statusset = 0;

$sessions[] = $session;
Expand Down Expand Up @@ -340,7 +340,7 @@ public function import() {
$programfield= $DB->get_record_sql($sql,array(),MUST_EXIST);

$course = $DB->get_record_sql("SELECT id,shortname FROM mdl_course WHERE id = '{$programfield->instanceid}'");
if ($course) {
if ($course) {
// Check course has activities.
if ($DB->record_exists('scheduler', array(
'course' => $course->id
Expand All @@ -349,10 +349,8 @@ public function import() {
$schedulerdb = $DB->get_records('scheduler', array('course' => $course->id, 'name'=>$session->scheduler), 'id', 'id');
$value = reset($schedulerdb);
$schedulerdb = current( $schedulerdb);

if(! empty($schedulerdb)) {


if (!empty($schedulerdb)) {
$scheduler = \scheduler_instance::load_by_id($schedulerdb->id);

// Build the session data.
Expand All @@ -364,16 +362,15 @@ public function import() {

// get teacherid from course
$teacher = $DB->get_record_sql(" SELECT c.id, c.shortname, u.id, u.username, u.firstname, u.lastname
FROM mdl_course c
LEFT OUTER JOIN mdl_context cx ON c.id = cx.instanceid
LEFT OUTER JOIN mdl_role_assignments ra ON cx.id = ra.contextid AND ra.roleid = '3'
LEFT OUTER JOIN mdl_user u ON ra.userid = u.id
FROM mdl_course c
LEFT OUTER JOIN mdl_context cx ON c.id = cx.instanceid
LEFT OUTER JOIN mdl_role_assignments ra ON cx.id = ra.contextid AND ra.roleid = '3'
LEFT OUTER JOIN mdl_user u ON ra.userid = u.id
WHERE cx.contextlevel = '50' AND c.id =".$course->id.";");


//check if action is to add or to delete
if(strtolower($session->action) == "add"){

if (strtolower($session->action) == "add") {
//format slot for DB add
$slot = $this->construct_slot_data_for_add($session,$schedulerdb->id, $teacher->id);

Expand All @@ -384,17 +381,17 @@ public function import() {
'activity' => $session->studentfirstname." ".$session->studentlastname." for ".userdate($session->sessiondate )
))));
unset($slot);
}
}

if (! empty($slot)) {

//Not the best method... with user id would be better
$student = $DB->get_record_sql(" SELECT u.id, u.firstname, u.lastname
FROM mdl_user u
WHERE u.firstname = '".$session->studentfirstname."' AND u.lastname='".$session->studentlastname."';");

//Need a check to see if it is even a student in the course
if($this->student_course($course->id, $student->id)){
if ($this->student_course($course->id, $student->id)) {
//get new slot id
$slotid = $this->add_slot($slot,$scheduler);

Expand All @@ -410,23 +407,23 @@ public function import() {
}else{
mod_scheduler_notifyqueue::notify_problem(get_string('error:invalidstudent','scheduler', ['name' => $session->studentfirstname." ".$session->studentlastname, 'course' => $session->course]));
}
}
}else if(strtolower($session->action) == "delete"){
}
}else if (strtolower($session->action) == "delete") {
//format slot for DB add
$slot = $this->construct_slot_data_for_add($session,$schedulerdb->id, $teacher->id);

$slotid = $this->session_exists($slot);
// Check if exists if not error.
if ($slotid!= false) {
$slot->id=$slotid;
$slot->id = $slotid;
$result = $this->delete_slot($slot, $scheduler);
unset($slot);
if($result ==true){
if ($result == true) {
$deletecount++;
}else{
//throw error as not matching
mod_scheduler_notifyqueue::notify_problem($result." ".$session->studentfirstname." ".$session->studentlastname.": ".$session->course.": ".userdate($session->sessiondate ));
}
}
} else{
//throw error as not matching
mod_scheduler_notifyqueue::notify_problem(get_string('error:invaliddelete', 'scheduler', ['name' => $session->studentfirstname." ".$session->studentlastname, 'course' => $session->course, 'date'=>userdate($session->sessiondate )]));
Expand All @@ -439,7 +436,7 @@ public function import() {
mod_scheduler_notifyqueue::notify_problem(get_string('error:invalidschedulername','scheduler', $session->scheduler));
}
} else {
mod_scheduler_notifyqueue::notify_problem(get_string('error:coursehasnoattendance','scheduler', $session->course));
mod_scheduler_notifyqueue::notify_problem(get_string('error:coursehasnoattendance','scheduler', $session->course));
}
} else {
mod_scheduler_notifyqueue::notify_problem(get_string('error:coursenotfound', 'scheduler', $session->course));
Expand All @@ -463,7 +460,7 @@ public function import() {
/**
* Check if student is in the course
*
* @param int $courseid
* @param int $courseid
* @param int $studentid
* @return boolean
*/
Expand All @@ -473,10 +470,11 @@ private function student_course($courseid, $studentid) {
$students = get_role_users(5 , $context);

foreach($students as $student){
if($student->id == $studentid)
return TRUE;
if ($student->id == $studentid) {
return true;
}
}
return FALSE;
return false;
}

/**
Expand All @@ -500,15 +498,15 @@ private function session_exists(stdClass $session) {
unset($check->hideuntil);
$check = (array) $check;

if ($record = $DB->get_record('scheduler_slots', $check, $fields='id', $strictness=IGNORE_MISSING)) {
if ($record = $DB->get_record('scheduler_slots', $check, $fields = 'id', $strictness = IGNORE_MISSING)) {
return $record->id;
}
return false;
}


/**
* Create the slot to be added to the DB
* Create the slot to be added to the DB
* @param stdClass $formdata moodleform - attendance form.
* @param int $schedulerid id of scheduler in DB
* @param int $teacherid of teacher in scheduler
Expand All @@ -523,7 +521,7 @@ function construct_slot_data_for_add($formdata, $schedulerid, $teacherid) {
$duration = $formdata->duration;

$sess = array();

$sess = new stdClass();
//Start with schedulerid
$sess->schedulerid = $schedulerid;
Expand Down Expand Up @@ -563,7 +561,7 @@ public function add_slot($sess, $scheduler) {
global $DB;

$context = get_context_instance(CONTEXT_COURSE, $scheduler->course);

$sess->id = $DB->insert_record('scheduler_slots', $sess);

//Need to add potential file slot into draft area? for notes, appointment notes, teachernote, studentnote
Expand All @@ -575,7 +573,7 @@ public function add_slot($sess, $scheduler) {
// Trigger a session added event.
$slotobj = $scheduler->get_slot($sess->id);
\mod_scheduler\event\slot_added::create_from_slot($slotobj)->trigger();

return $sess->id;
}

Expand All @@ -589,22 +587,22 @@ public function delete_slot($slot, $scheduler) {

global $DB;

$context = get_context_instance(CONTEXT_COURSE, $scheduler->course);
$context = get_context_instance(CONTEXT_COURSE, $scheduler->course);
$result = true;

//clear calendar
//clear calendar
//for teachers calendar
$tcal = "SSsup:{$slot->id}:{$scheduler->course}";
$result = $DB->delete_records('event', array('eventtype' => $tcal, "timestart" => $slot->starttime));

if(!$result){
if (!$result) {
return get_string('error:deletecalendart', 'scheduler');
}
//for student calendar
$scal = "SSstu:{$slot->id}:{$scheduler->course}";
$result = $DB->delete_records('event', array('eventtype' => $scal,"timestart" => $slot->starttime));

if(!$result){
if (!$result) {
return get_string('error:deletecalendars', 'scheduler');
}

Expand All @@ -615,8 +613,8 @@ public function delete_slot($slot, $scheduler) {


//need to get all scheduler appointments
$appointments = $DB->get_records("scheduler_appointment", array("slotid"=>$slot->id), $sort='', $fields='*');
$appointments = $DB->get_records("scheduler_appointment", array("slotid" => $slot->id), $sort = '', $fields = '*');


foreach($appointments as $appointment){
//clear appointment storage
Expand All @@ -633,7 +631,7 @@ public function delete_slot($slot, $scheduler) {

//delete slot
$result = $DB->delete_records("scheduler_slots", array('id' => $slot->id));
if(!$result){
if (!$result) {
return get_string('error:deleteslot', 'scheduler');
}
return $result;
Expand Down Expand Up @@ -700,16 +698,16 @@ function construct_appointment_data_for_add($formdata, $slotid, $studentid) {
*/
public function add_appointment($sess,$context) {
global $DB;

$sess->id = $DB->insert_record('scheduler_appointment', $sess);


//Need to add potential file slot into draft area? appointment notes, teachernote, studentnote
$description = file_save_draft_area_files(0,
$context->id, 'mod_scheduler', 'appointmentnote', $sess->id,
array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0),
$sess->appointmentnote);

$description = file_save_draft_area_files(0,
$context->id, 'mod_scheduler', 'teachernote', $sess->id,
array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0),
Expand All @@ -719,7 +717,7 @@ public function add_appointment($sess,$context) {
$context->id, 'mod_scheduler', 'studentnote', $sess->id,
array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0),
$sess->studentnote);

return $sess->id;
}

Expand Down
Loading

0 comments on commit ed8bf57

Please sign in to comment.