<?php

// require library
require_once '../autoload.php';
// initialization
$se = new SphereEngine\Api("access_token", "v3", "endpoint");
$client = $se->getCompilersClient();
<?php

// require library
require_once '../autoload.php';
// initialization
$se = new SphereEngine\Api("access_token", "v3", "endpoint");
$client = $se->getProblemsClient();
// API usage
$problemCode = 'PROBLEMNAME';
$source = '//source code';
$compiler = 11;
// C language
// proper error handeling with accordance to the documentation
try {
    $response = $client->submissions->create($problemCode, $source, $compiler);
    $submissionId = $response['id'];
} catch (SphereEngine\SphereEngineResponseException $e) {
    if ($e->getCode() == 403) {
        echo "Error: unauthorized access";
    } elseif ($e->getCode() == 404) {
        if ($e->getMessage() == 'Problem not found') {
            // handle wrong problem code
        } elseif ($e->getMessage() == 'Compiler not found') {
            // handle wrong compiler ID
        } elseif ($e->getMessage() == 'User not found') {
            // handle wrong user ID
        } else {
            // handle unexpected error
        }
    } elseif ($e->getCode() == 400) {
        if ($e->getMessage() == 'Empty source code') {
 /**
  * Called to define this moodle form
  *
  * @return void
  */
 public function definition()
 {
     global $CFG, $COURSE, $DB, $PAGE;
     $mform = $this->_form;
     $mform->addElement('header', 'general', get_string('general', 'form'));
     $mform->addElement('select', 'state', 'State/Province: ');
     $mform->addElement('text', 'name', get_string('seplmentname', 'sepl'), array('size' => '64'));
     if (!empty($CFG->formatstringstriptags)) {
         $mform->setType('name', PARAM_TEXT);
     } else {
         $mform->setType('name', PARAM_CLEANHTML);
     }
     $se_problems = new SphereEngine\Api("dadeea82cdd4b840cbde18858d9f17d6fb11a957", "v3", "problems");
     $problemsClient = $se_problems->getProblemsClient();
     #$client = (new SphereEngine\Api("dadeea82cdd4b840cbde18858d9f17d6fb11a957", "v3", "problems"))->getProblemsClient();
     #$result = $problemsClient->problems->all();
     #$mform->addElement('select', 'problem', 'Problem: ',$result);//, $state);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, "http://problems.sphere-engine.com/api/v3/problems?access_token=dadeea82cdd4b840cbde18858d9f17d6fb11a957");
     curl_setopt($ch, CURLOPT_POST, 1);
     #curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
     // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $server_output2 = curl_exec($ch);
     curl_close($ch);
     $client = (new SphereEngine\Api("dadeea82cdd4b840cbde18858d9f17d6fb11a957", "v3", "endpoint"))->getProblemsClient();
     var_dump($user);
     #var_dump(
     #$client->problems->all(['items']);
     var_dump($server_output2);
     $result2 = json_decode($server_output2, true);
     var_dump($result2);
     $mform->addElement('select', 'problem', 'Problem: ', $result2);
     //, $state);
     $mform->addRule('name', null, 'required', null, 'client');
     $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
     $this->standard_intro_elements(get_string('description', 'sepl'));
     $mform->addElement('filemanager', 'introattachments', get_string('introattachments', 'sepl'), null, array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes));
     $mform->addHelpButton('introattachments', 'introattachments', 'sepl');
     $ctx = null;
     if ($this->current && $this->current->coursemodule) {
         $cm = get_coursemodule_from_instance('sepl', $this->current->id, 0, false, MUST_EXIST);
         $ctx = context_module::instance($cm->id);
     }
     $seplment = new sepl($ctx, null, null);
     if ($this->current && $this->current->course) {
         if (!$ctx) {
             $ctx = context_course::instance($this->current->course);
         }
         $course = $DB->get_record('course', array('id' => $this->current->course), '*', MUST_EXIST);
         $seplment->set_course($course);
     }
     $config = get_config('sepl');
     $mform->addElement('header', 'availability', get_string('availability', 'sepl'));
     $mform->setExpanded('availability', true);
     $name = get_string('allowsubmissionsfromdate', 'sepl');
     $options = array('optional' => true);
     $mform->addElement('date_time_selector', 'allowsubmissionsfromdate', $name, $options);
     $mform->addHelpButton('allowsubmissionsfromdate', 'allowsubmissionsfromdate', 'sepl');
     $name = get_string('duedate', 'sepl');
     $mform->addElement('date_time_selector', 'duedate', $name, array('optional' => true));
     $mform->addHelpButton('duedate', 'duedate', 'sepl');
     $name = get_string('cutoffdate', 'sepl');
     $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional' => true));
     $mform->addHelpButton('cutoffdate', 'cutoffdate', 'sepl');
     $name = get_string('alwaysshowdescription', 'sepl');
     $mform->addElement('checkbox', 'alwaysshowdescription', $name);
     $mform->addHelpButton('alwaysshowdescription', 'alwaysshowdescription', 'sepl');
     $mform->disabledIf('alwaysshowdescription', 'allowsubmissionsfromdate[enabled]', 'notchecked');
     $seplment->add_all_plugin_settings($mform);
     $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'sepl'));
     $name = get_string('submissiondrafts', 'sepl');
     $mform->addElement('selectyesno', 'submissiondrafts', $name);
     $mform->addHelpButton('submissiondrafts', 'submissiondrafts', 'sepl');
     $name = get_string('requiresubmissionstatement', 'sepl');
     $mform->addElement('selectyesno', 'requiresubmissionstatement', $name);
     $mform->addHelpButton('requiresubmissionstatement', 'requiresubmissionstatement', 'sepl');
     $mform->setType('requiresubmissionstatement', PARAM_BOOL);
     $options = array(ASSIGN_ATTEMPT_REOPEN_METHOD_NONE => get_string('attemptreopenmethod_none', 'mod_sepl'), ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL => get_string('attemptreopenmethod_manual', 'mod_sepl'), ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS => get_string('attemptreopenmethod_untilpass', 'mod_sepl'));
     $mform->addElement('select', 'attemptreopenmethod', get_string('attemptreopenmethod', 'mod_sepl'), $options);
     $mform->addHelpButton('attemptreopenmethod', 'attemptreopenmethod', 'mod_sepl');
     $options = array(ASSIGN_UNLIMITED_ATTEMPTS => get_string('unlimitedattempts', 'mod_sepl'));
     $options += array_combine(range(1, 30), range(1, 30));
     $mform->addElement('select', 'maxattempts', get_string('maxattempts', 'mod_sepl'), $options);
     $mform->addHelpButton('maxattempts', 'maxattempts', 'sepl');
     $mform->disabledIf('maxattempts', 'attemptreopenmethod', 'eq', ASSIGN_ATTEMPT_REOPEN_METHOD_NONE);
     $mform->addElement('header', 'groupsubmissionsettings', get_string('groupsubmissionsettings', 'sepl'));
     $name = get_string('teamsubmission', 'sepl');
     $mform->addElement('selectyesno', 'teamsubmission', $name);
     $mform->addHelpButton('teamsubmission', 'teamsubmission', 'sepl');
     if ($seplment->has_submissions_or_grades()) {
         $mform->freeze('teamsubmission');
     }
     $name = get_string('preventsubmissionnotingroup', 'sepl');
     $mform->addElement('selectyesno', 'preventsubmissionnotingroup', $name);
     $mform->addHelpButton('preventsubmissionnotingroup', 'preventsubmissionnotingroup', 'sepl');
     $mform->setType('preventsubmissionnotingroup', PARAM_BOOL);
     $mform->disabledIf('preventsubmissionnotingroup', 'teamsubmission', 'eq', 0);
     $name = get_string('requireallteammemberssubmit', 'sepl');
     $mform->addElement('selectyesno', 'requireallteammemberssubmit', $name);
     $mform->addHelpButton('requireallteammemberssubmit', 'requireallteammemberssubmit', 'sepl');
     $mform->disabledIf('requireallteammemberssubmit', 'teamsubmission', 'eq', 0);
     $mform->disabledIf('requireallteammemberssubmit', 'submissiondrafts', 'eq', 0);
     $groupings = groups_get_all_groupings($seplment->get_course()->id);
     $options = array();
     $options[0] = get_string('none');
     foreach ($groupings as $grouping) {
         $options[$grouping->id] = $grouping->name;
     }
     $name = get_string('teamsubmissiongroupingid', 'sepl');
     $mform->addElement('select', 'teamsubmissiongroupingid', $name, $options);
     $mform->addHelpButton('teamsubmissiongroupingid', 'teamsubmissiongroupingid', 'sepl');
     $mform->disabledIf('teamsubmissiongroupingid', 'teamsubmission', 'eq', 0);
     if ($seplment->has_submissions_or_grades()) {
         $mform->freeze('teamsubmissiongroupingid');
     }
     $mform->addElement('header', 'notifications', get_string('notifications', 'sepl'));
     $name = get_string('sendnotifications', 'sepl');
     $mform->addElement('selectyesno', 'sendnotifications', $name);
     $mform->addHelpButton('sendnotifications', 'sendnotifications', 'sepl');
     $name = get_string('sendlatenotifications', 'sepl');
     $mform->addElement('selectyesno', 'sendlatenotifications', $name);
     $mform->addHelpButton('sendlatenotifications', 'sendlatenotifications', 'sepl');
     $mform->disabledIf('sendlatenotifications', 'sendnotifications', 'eq', 1);
     $name = get_string('sendstudentnotificationsdefault', 'sepl');
     $mform->addElement('selectyesno', 'sendstudentnotifications', $name);
     $mform->addHelpButton('sendstudentnotifications', 'sendstudentnotificationsdefault', 'sepl');
     // Plagiarism enabling form.
     if (!empty($CFG->enableplagiarism)) {
         require_once $CFG->libdir . '/plagiarismlib.php';
         plagiarism_get_form_elements_module($mform, $ctx->get_course_context(), 'mod_sepl');
     }
     $this->standard_grading_coursemodule_elements();
     $name = get_string('blindmarking', 'sepl');
     $mform->addElement('selectyesno', 'blindmarking', $name);
     $mform->addHelpButton('blindmarking', 'blindmarking', 'sepl');
     if ($seplment->has_submissions_or_grades()) {
         $mform->freeze('blindmarking');
     }
     $name = get_string('markingworkflow', 'sepl');
     $mform->addElement('selectyesno', 'markingworkflow', $name);
     $mform->addHelpButton('markingworkflow', 'markingworkflow', 'sepl');
     $name = get_string('markingallocation', 'sepl');
     $mform->addElement('selectyesno', 'markingallocation', $name);
     $mform->addHelpButton('markingallocation', 'markingallocation', 'sepl');
     $mform->disabledIf('markingallocation', 'markingworkflow', 'eq', 0);
     $this->standard_coursemodule_elements();
     $this->apply_admin_defaults();
     $this->add_action_buttons();
     // Add warning popup/noscript tag, if grades are changed by user.
     $hasgrade = false;
     if (!empty($this->_instance)) {
         $hasgrade = $DB->record_exists_select('sepl_grades', 'seplment = ? AND grade <> -1', array($this->_instance));
     }
     if ($mform->elementExists('grade') && $hasgrade) {
         $module = array('name' => 'mod_sepl', 'fullpath' => '/mod/sepl/module.js', 'requires' => array('node', 'event'), 'strings' => array(array('changegradewarning', 'mod_sepl')));
         $PAGE->requires->js_init_call('M.mod_sepl.init_grade_change', null, false, $module);
         // Add noscript tag in case.
         $noscriptwarning = $mform->createElement('static', 'warning', null, html_writer::tag('noscript', get_string('changegradewarning', 'mod_sepl')));
         $mform->insertElementBefore($noscriptwarning, 'grade');
     }
 }
<?php

require_once './autoload.php';
$se_compilers = new SphereEngine\Api("access_token", "v3", "endpoint");
$compilersClient = $se_compilers->getCompilersClient();
$se_problems = new SphereEngine\Api("access_token", "v3", "endpoint");
$problemsClient = $se_problems->getProblemsClient();
$compilers_unit_tests = ['wrong access token' => function ($not_used) {
    $client = (new SphereEngine\Api("fake_access_token", "v3", "endpoint"))->getCompilersClient();
    try {
        $client->test();
        return 0;
    } catch (SphereEngine\SphereEngineResponseException $e) {
        if ($e->getCode() == 401) {
            return 1;
        }
        return 0;
    }
}, 'test' => function ($client) {
    return $client->test()['pi'] == 3.14;
}, 'compilers' => function ($client) {
    return $client->compilers()[11][0] == 'C';
}, 'submissions get existing' => function ($client) {
    return $client->submissions->get(38436074)['result'] == 15;
}, 'submissions get non existing' => function ($client) {
    try {
        $r = $client->submissions->get(9999999999);
        return 0;
    } catch (SphereEngine\SphereEngineResponseException $e) {
        if ($e->getCode() == 404) {
            return 1;
Example #5
0
 public function sphereGetCompilers()
 {
     $seCompilersApi = new SphereEngine\Api("02994435062fb95b5f3e6938e5aa433e", "v3", "languages");
     //new SphereEngine\Api($TOKEN, "v3", "languages");
     $seCompilersClient = $seCompilersApi->getCompilersClient();
     $serverOutput = $seCompilersClient->compilers();
     $this->sphereSaveCompilers($serverOutput);
     return $serverOutput;
 }
 /**
  * Define this form - called by the parent constructor
  */
 public function definition()
 {
     $mform = $this->_form;
     list($sepl, $data) = $this->_customdata;
     $sepl->add_submission_form_elements($mform, $data);
     /*
     
     $ch = curl_init();
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, "http://api.compilers.sphere-engine.com/api/v3/languages?access_token=02994435062fb95b5f3e6938e5aa433e");
     curl_setopt($ch, CURLOPT_POST, 1);
     #curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
     // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $server_output = curl_exec ($ch);*/
     #var_dump($result);
     $client = (new SphereEngine\Api("02994435062fb95b5f3e6938e5aa433e", "v3", "languages"))->getProblemsClient();
     $se_compilers = new SphereEngine\Api("02994435062fb95b5f3e6938e5aa433e", "v3", "languages");
     $compilersClient = $se_compilers->getCompilersClient();
     $server_output = $compilersClient->compilers();
     # $result = json_decode($server_output, true);
     #  $jsonString = json_decode($server_output, true);
     #curl_close ($ch);
     #var_dump($jsonString);
     $mform->addElement('select', 'compiler', 'Compiler: ', $server_output);
     //, $state);
     $this->add_action_buttons(false, 'Refresh');
     #$mform->addElement('select', 'compiler', 'Compiler: ',$server_output);//, $state);
     $this->add_action_buttons(true, get_string('savechanges', 'sepl'));
     if ($data) {
         # 	$compiler = $mform->g
         #var_dump($data);
         $ii = 0;
         foreach ($data as $tes) {
             $ii = 0;
             foreach ($tes as $tes2) {
                 var_dump($tes2);
                 if ($ii == 0) {
                     $textToSend = $tes2;
                 }
                 //text from online editor - ugly way but it works...
                 $ii++;
             }
         }
         $myToken = "02994435062fb95b5f3e6938e5aa433e";
         $client = (new SphereEngine\Api($myToken, "v3", "endpoint"))->getCompilersClient();
         $res = $client->submissions->create("UT1952", $textToSend, 11);
         $selectedItem =& $mform->getElement('compiler')->getSelected();
         var_dump($selectedItem);
         #var_dump($mform->get_data('compiler'));
         /*
         *  example of saving record in moodle db
         * $newrecord = new stdClass();
         $newrecord->name = $data->dname;
         $newrecord->id = $DB->insert_record('tbl_name', $newrecord);
         * 
         */
         $mform->addElement('text', 'sphereReturn', $res, $attributes);
         var_dump($res);
         $this->set_data($data);
     }
 }