/**
  * Triggered when the '\mod_workshopplus\event\course_module_viewed' event is triggered.
  *
  * This does the same job as {@link workshopplusallocation_scheduled_cron()} but for the
  * single workshopplus. The idea is that we do not need to wait for cron to execute.
  * Displaying the workshopplus main view.php can trigger the scheduled allocation, too.
  *
  * @param \mod_workshopplus\event\course_module_viewed $event
  * @return bool
  */
 public static function workshopplus_viewed($event)
 {
     global $DB, $CFG;
     require_once $CFG->dirroot . '/mod/workshopplus/locallib.php';
     $workshopplus = $event->get_record_snapshot('workshopplus', $event->objectid);
     $course = $event->get_record_snapshot('course', $event->courseid);
     $cm = $event->get_record_snapshot('course_modules', $event->contextinstanceid);
     $workshopplus = new \workshopplus($workshopplus, $cm, $course);
     $now = time();
     // Non-expensive check to see if the scheduled allocation can even happen.
     if ($workshopplus->phase == \workshopplus::PHASE_SUBMISSION and $workshopplus->submissionend > 0 and $workshopplus->submissionend < $now) {
         // Make sure the scheduled allocation has been configured for this workshopplus, that it has not
         // been executed yet and that the passed workshopplus record is still valid.
         $sql = "SELECT a.id\n                      FROM {workshopplusallocation_sch} a\n                      JOIN {workshopplus} w ON a.workshopplusid = w.id\n                     WHERE w.id = :workshopplusid\n                           AND a.enabled = 1\n                           AND w.phase = :phase\n                           AND w.submissionend > 0\n                           AND w.submissionend < :now\n                           AND (a.timeallocated IS NULL OR a.timeallocated < w.submissionend)";
         $params = array('workshopplusid' => $workshopplus->id, 'phase' => \workshopplus::PHASE_SUBMISSION, 'now' => $now);
         if ($DB->record_exists_sql($sql, $params)) {
             // Allocate submissions for assessments.
             $allocator = $workshopplus->allocator_instance('scheduled');
             $result = $allocator->execute();
             // Todo inform the teachers about the results.
         }
     }
     return true;
 }
// course module
$method = optional_param('method', 'manual', PARAM_ALPHA);
// method to use
$cm = get_coursemodule_from_id('workshopplus', $cmid, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$workshopplus = $DB->get_record('workshopplus', array('id' => $cm->instance), '*', MUST_EXIST);
$workshopplus = new workshopplus($workshopplus, $cm, $course);
global $PAGE, $DB;
$PAGE->set_url($workshopplus->allocation_url($method));
require_login($course, false, $cm);
$context = $PAGE->context;
require_capability('mod/workshopplus:allocate', $context);
$PAGE->set_title($workshopplus->name);
$PAGE->set_heading($course->fullname);
$PAGE->navbar->add('Map students to TAs');
$allocator = $workshopplus->allocator_instance($method);
$initresult = $allocator->init();
//
// Output starts here
//
$output = $PAGE->get_renderer('mod_workshopplus');
echo $output->header();
echo $OUTPUT->heading(format_string($workshopplus->name));
// SQL to fetch list of TAs
$sql_TA = "select user.id, role.shortname, user.firstname, user.lastname  " . "from {role} role " . "inner join {role_assignments} assign on(role.id=assign.roleid and role.id=4) " . "inner join {user} user on (assign.userid=user.id) " . "order by 1";
$list_of_tas = $DB->get_records_sql($sql_TA, $params);
$option_array_list_of_tas = array();
foreach ($list_of_tas as $ta) {
    $option_array_list_of_tas[$ta->id] = "{$ta->firstname} {$ta->lastname}";
}
// Fetch selected TA
/**
 * Regular jobs to execute via cron
 */
function workshopplusallocation_scheduled_cron()
{
    global $CFG, $DB;
    $sql = "SELECT w.*\n              FROM {workshopplusallocation_sch} a\n              JOIN {workshopplus} w ON a.workshopplusid = w.id\n             WHERE a.enabled = 1\n                   AND w.phase = 20\n                   AND w.submissionend > 0\n                   AND w.submissionend < ?\n                   AND (a.timeallocated IS NULL OR a.timeallocated < w.submissionend)";
    $workshoppluss = $DB->get_records_sql($sql, array(time()));
    if (empty($workshoppluss)) {
        mtrace('... no workshoppluss awaiting scheduled allocation. ', '');
        return;
    }
    mtrace('... executing scheduled allocation in ' . count($workshoppluss) . ' workshopplus(s) ... ', '');
    // let's have some fun!
    require_once $CFG->dirroot . '/mod/workshopplus/locallib.php';
    foreach ($workshoppluss as $workshopplus) {
        $cm = get_coursemodule_from_instance('workshopplus', $workshopplus->id, $workshopplus->course, false, MUST_EXIST);
        $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
        $workshopplus = new workshopplus($workshopplus, $cm, $course);
        $allocator = $workshopplus->allocator_instance('scheduled');
        $result = $allocator->execute();
        // todo inform the teachers about the results
    }
}