/**
  * Run forum cron.
  */
 public function execute()
 {
     global $DB, $CFG;
     $currenttime = time();
     $statement = 'SELECT R.* FROM {ratingallocate} AS R
     LEFT JOIN {ratingallocate_allocations} AS A
     ON R.' . this_db\ratingallocate::ID . '=A.' . this_db\ratingallocate_allocations::RATINGALLOCATEID . '
     WHERE A.' . this_db\ratingallocate_allocations::ID . ' IS NULL AND R.' . this_db\ratingallocate::ACCESSTIMESTOP . '<' . $currenttime;
     $records = $DB->get_records_sql($statement);
     $course = null;
     foreach ($records as $record) {
         $cm = get_coursemodule_from_instance(this_db\ratingallocate::TABLE, $record->{this_db\ratingallocate::ID});
         // Fetch the data for the course, if is has changed
         if (!$course || $course->id != $record->{this_db\ratingallocate::COURSE}) {
             $course = $DB->get_record('course', array('id' => $record->{this_db\ratingallocate::COURSE}), '*', MUST_EXIST);
         }
         // Create ratingallocate instance from record
         $ratingallocate = new \ratingallocate($record, $course, $cm, \context_module::instance($cm->id));
         $currenttime = time();
         $timetoterminate = $CFG->ratingallocate_algorithm_timeout + $ratingallocate->ratingallocate->algorithmstarttime;
         // If last execution exeeds timeout limit assume failure of algorithm run.
         if ($ratingallocate->ratingallocate->algorithmstarttime && $currenttime >= $timetoterminate && $ratingallocate->get_algorithm_status() === \mod_ratingallocate\algorithm_status::running) {
             $ratingallocate->set_algorithm_failed();
             return true;
         }
         // Only start the algorithm, if it should be run by the cron and hasn't been started somehow, yet.
         if ($ratingallocate->ratingallocate->runalgorithmbycron === "1" && $ratingallocate->get_algorithm_status() === \mod_ratingallocate\algorithm_status::notstarted) {
             // Run allocation.
             $ratingallocate->distrubute_choices();
         }
     }
     return true;
 }
 public function execute()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/mod/ratingallocate/locallib.php';
     $site = get_site();
     // parse customdata passed
     $customdata = $this->get_custom_data();
     $userid = $customdata->userid;
     $ratingallocateid = $customdata->ratingallocateid;
     //get instance of ratingallocate
     $ratingallocate = $DB->get_record(this_db\ratingallocate::TABLE, array(this_db\ratingallocate::ID => $ratingallocateid), '*', MUST_EXIST);
     $courseid = $ratingallocate->course;
     $course = get_course($courseid);
     $cm = get_coursemodule_from_instance('ratingallocate', $ratingallocate->id, $courseid);
     $context = \context_module::instance($cm->id);
     $ratingallocateobj = new \ratingallocate($ratingallocate, $course, $cm, $context);
     $ratingallocateobj->notify_users_distribution($userid);
 }
 /**
  * Entry-point for the \ratingallocate object to call a solver
  * @param \ratingallocate $ratingallocate
  */
 public function distribute_users(\ratingallocate $ratingallocate)
 {
     // Extend PHP time limit
     //        core_php_time_limit::raise();
     // Load data from database
     $choicerecords = $ratingallocate->get_rateable_choices();
     $ratings = $ratingallocate->get_ratings_for_rateable_choices();
     // Randomize the order of the enrties to prevent advantages for early entry
     shuffle($ratings);
     $usercount = count($ratingallocate->get_raters_in_course());
     $distributions = $this->compute_distribution($choicerecords, $ratings, $usercount);
     $transaction = $ratingallocate->db->start_delegated_transaction();
     // perform all allocation manipulation / inserts in one transaction
     $ratingallocate->clear_all_allocations();
     foreach ($distributions as $choiceid => $users) {
         foreach ($users as $userid) {
             $ratingallocate->add_allocation($choiceid, $userid, $ratingallocate->ratingallocate->id);
         }
     }
     $transaction->allow_commit();
 }
 * @copyright  based on code by Stefan Koegel copyright (C) 2013 Stefan Koegel
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once dirname(dirname(dirname(__FILE__))) . '/config.php';
require_once dirname(__FILE__) . '/locallib.php';
require_once dirname(__FILE__) . '/solver/ford-fulkerson-koegel.php';
$id = optional_param('id', 0, PARAM_INT);
// course_module ID, or
$n = optional_param('m', 0, PARAM_INT);
// ratingallocate instance ID - it should be named as the first character of the module
if ($id) {
    $cm = get_coursemodule_from_id('ratingallocate', $id, 0, false, MUST_EXIST);
    $course = get_course($cm->course);
    $ratingallocate = $DB->get_record('ratingallocate', array('id' => $cm->instance), '*', MUST_EXIST);
} else {
    if ($n) {
        $ratingallocate = $DB->get_record('ratingallocate', array('id' => $n), '*', MUST_EXIST);
        $course = get_course($ratingallocate->course);
        $cm = get_coursemodule_from_instance('ratingallocate', $ratingallocate->id, $course->id, false, MUST_EXIST);
    } else {
        error('You must specify a course_module ID or an instance ID');
    }
}
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
$PAGE->set_title($cm->name);
$PAGE->set_context($context);
$PAGE->set_url('/mod/ratingallocate/view.php', array('id' => $cm->id));
require_capability('mod/ratingallocate:view', $context);
$ratingallocateobj = new ratingallocate($ratingallocate, $course, $cm, $context);
echo $ratingallocateobj->handle_view();
 /**
  * Formats the ratings
  * @param unknown $ratings
  * @return multitype:Ambigous <string, lang_string>
  */
 private function get_options_titles($ratings, ratingallocate $ratingallocate)
 {
     $titles = array();
     $unique_ratings = array_unique($ratings);
     $options = $ratingallocate->get_options_titles($unique_ratings);
     foreach ($options as $id => $option) {
         $titles[$id] = empty($option) ? get_string('no_rating_given', ratingallocate_MOD_NAME) : get_string('rating_raw', ratingallocate_MOD_NAME, $option);
     }
     return $titles;
 }
require_once dirname(__FILE__) . '/../locallib.php';
$id = required_param('id', PARAM_INT);
// course_module ID, or
$action = optional_param('action', '', PARAM_ACTION);
if ($id) {
    $cm = get_coursemodule_from_id('ratingallocate', $id, 0, false, MUST_EXIST);
    $course = get_course($cm->course);
    $ratingallocate = $DB->get_record('ratingallocate', array('id' => $cm->instance), '*', MUST_EXIST);
} else {
    error('You must specify a course_module ID');
}
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/ratingallocate:export_ratings', $context);
/* @var $ratingallocateobj ratingallocate */
$ratingallocateobj = new ratingallocate($ratingallocate, $course, $cm, $context);
/**
 * Eine beim csv_export_writer abgeschaute Klasse, die in Dateien schreiben kann und zum Download anbieten.
 * @copyright (c) 2014, M Schulze
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class lp_export_write
{
    /** @var $filename path to write file */
    protected $filename;
    /** @var string $path The directory path for storing the temporary csv file. */
    protected $path;
    /** @var resource $fp File pointer for the csv file. */
    protected $fp;
    /**
     * Constructor for the csv export reader
// to include $CFG, for example
require_once $CFG->libdir . '/csvlib.class.php';
require_once './locallib.php';
$id = required_param('id', PARAM_INT);
// course_module ID, or
if ($id) {
    $cm = get_coursemodule_from_id('ratingallocate', $id, 0, false, MUST_EXIST);
    $course = get_course($cm->course);
    $ratingallocate = $DB->get_record('ratingallocate', array('id' => $cm->instance), '*', MUST_EXIST);
} else {
    error('You must specify a course_module ID');
}
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/ratingallocate:export_ratings', $context);
$ratingallocateobj = new ratingallocate($ratingallocate, $course, $cm, $context);
global $DB;
// print all the exported data here
$downloadfilename = clean_filename('export_ratings_' . $ratingallocateobj->ratingallocate->name);
$csvexport = new csv_export_writer('semicolon');
$csvexport->set_filename($downloadfilename);
// id firstname lastname
$exporttitle[0] = 'userid';
$exporttitle[1] = 'username';
$exporttitle[2] = 'firstname';
$exporttitle[3] = 'lastname';
$offsetchoices = count($exporttitle);
$choices = $ratingallocateobj->get_rateable_choices();
foreach ($choices as $choice) {
    $columnid[$choice->id] = $choice->id + $offsetchoices;
    $exporttitle[$choice->id + $offsetchoices] = $choice->id . '|' . $choice->title;