/**
  * 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();
 }
 /**
  * Shows table containing information about the users' ratings
  * and their distribution over the choices (allocations).
  *
  * @return HTML code
  */
 public function ratings_csv_for_ratingallocate(ratingallocate $ratingallocate, csv_export_writer $csvexport)
 {
     $exporttitle[0] = 'userid';
     $exporttitle[1] = 'username';
     $exporttitle[2] = 'firstname';
     $exporttitle[3] = 'lastname';
     $offsetchoices = count($exporttitle);
     $columnids = array();
     $rateable_choices = $ratingallocate->get_rateable_choices();
     foreach ($rateable_choices as $choice) {
         $columnids[$choice->id] = $choice->id + $offsetchoices;
         $exporttitle[$choice->id + $offsetchoices] = $choice->id . '|' . $choice->title;
     }
     // Sort headings by (choice-)id to align them with exported data (created below).
     ksort($exporttitle);
     $maxcolumnid = key(array_slice($exporttitle, -1, 1, true));
     $exporttitle[$maxcolumnid + 1] = "allocation";
     $columnids["allocation"] = $maxcolumnid + 1;
     // add the header to the data
     $csvexport->add_data($exporttitle);
     $ratingscells = array();
     foreach ($ratingallocate->get_raters_in_course() as $user) {
         if (!array_key_exists($user->id, $ratingscells)) {
             $ratingscells[$user->id] = array();
         }
         $ratingscells[$user->id][0] = $user->id;
         $ratingscells[$user->id][1] = $user->username;
         $ratingscells[$user->id][2] = fullname($user);
         $ratingscells[$user->id][3] = $user->lastname;
         foreach ($columnids as $choice => $choicecolumn) {
             $ratingscells[$user->id][$choicecolumn] = '';
         }
         // Sort ratings by choiceid to align them with the group names in the table
         ksort($ratingscells[$user->id]);
     }
     $ratings = $ratingallocate->get_ratings_for_rateable_choices();
     // get rating titles
     $titles = $this->get_options_titles(array_map(function ($rating) {
         return $rating->rating;
     }, $ratings), $ratingallocate);
     foreach ($ratings as $rating) {
         $choicecolumnindex = $columnids[$rating->choiceid];
         $ratingscells[$rating->userid][$choicecolumnindex] = $titles[$rating->rating];
     }
     $memberships = $ratingallocate->get_allocations();
     $allocationcolumnindex = $columnids["allocation"];
     foreach ($memberships as $membership) {
         $choice = $rateable_choices[$membership->choiceid];
         $ratingscells[$membership->userid][$allocationcolumnindex] = $choice->id . '|' . $choice->title;
     }
     foreach ($ratingscells as $userline) {
         $csvexport->add_data($userline);
     }
 }
        $this->print_csv_data();
        exit;
    }
    /**
     * Make sure that everything is closed when we are finished.
     */
    public function __destruct()
    {
        fclose($this->fp);
        // unlink ( $this->path );
    }
}
global $DB;
$writer = new lp_export_write();
$writer->set_filename("lp_solve_export_course_" . $ratingallocate->id);
$choices = $ratingallocateobj->get_rateable_choices();
$ratings = $ratingallocateobj->get_ratings_for_rateable_choices();
$ratingscells = array();
foreach ($ratings as $rating) {
    if (!array_key_exists($rating->userid, $ratingscells)) {
        $ratingscells[$rating->userid] = array();
    }
    $ratingscells[$rating->userid][$rating->choiceid] = $rating->rating;
}
$zielfkt = 'max ';
// die zu maximierende Zielfunktion
$usernb = '';
// Stelle die NB pro User auf, dass er nur in eine Wahl kommt
$variablenerkl = '';
$nbkurs = array();
// nebenbedingungen pro Kurs