function process_enrollment($user, $section, $fields) { global $CFG; $role = (int) $fields[0]; // Student role=1 // Else it's a Teacher primary role=2 if ($role == 1) { $status = lookup_statuscode($fields[1], STATUSCODE_TYPE_STUDENT); $student = CoursePrefsStudent::findByUnique($section->getId(), $user->getId()); $credit_hours = $fields[4]; if (!$student && $status == 'unenroll') { throw new Exception(get_string('cant_drop', 'block_courseprefs')); } else { if (!$student) { $student = new CoursePrefsStudent($section->getId(), $user->getId(), $status, $credit_hours); } else { // Recalculate hours $student->findHours($status, $credit_hours); } } try { $student->save(); } catch (Exception $e) { throw new Exception($e->getMessage()); } // Time to add a log entry for this course CoursePrefsLog::add_to_log($user->getId(), $section->getId(), strtotime($fields[2]), $status, strtotime($fields[3])); } else { $status = lookup_statuscode($fields[1], STATUSCODE_TYPE_TEACHER); $primary_flag = $role == 2 ? 1 : 0; $teacher = CoursePrefsTeacher::findByUnique($user->getId(), $section->getId(), $primary_flag); // In the event that the teacher does not exist, and the mainframe // gives us a non-primary teacher drop, then change the info to be a // primary teacher drop. if (!$teacher && !$primary_flag && $status == 'unenroll') { $role = 2; $teacher = CoursePrefsTeacher::findByUnique($user->getId(), $section->getId(), 1); } if (!$teacher) { $teacher = new CoursePrefsTeacher($section->getId(), $user->getId(), $primary_flag, $status); } /* $teacher->setPrimaryFlag($primary_flag); */ $teacher->setStatus($status); $teacher->setTimeStamp(strtotime($fields[2])); try { $teacher->save(); CoursePrefsLog::add_to_log($user->getId(), $section->getId(), strtotime($fields[2]), $status, 'Teacher event'); } catch (Exception $e) { throw new Exception($e->getMessage()); } // Great job; now if the primary teacher is being dropped, mark the section as pending // cron will take care of the rest if ($role == 2 && $status == 'unenroll') { $section->setStatus('pending'); $section->save(); // Clean any preference that was applied foreach (array('crosslist', 'split') as $pref) { $sql = "UPDATE {$CFG->prefix}block_courseprefs_{$pref}\n SET status='todo'\n WHERE sectionsid={$section->getId()}"; execute_sql($sql, false); // delete_records('block_courseprefs_'.$pref, 'sectionsid', $section->getId()); } } } }