function submit_work($uid, $group_id, $id, $file) { global $groupPath, $langUploadError, $langUploadSuccess, $langBack, $m, $tool_content, $workPath, $group_sql, $webDir, $course_code, $is_editor; $ext = get_file_extension($file); $local_name = greek_to_latin('Group ' . $group_id . (empty($ext) ? '' : '.' . $ext)); $original_filename = Database::get()->querySingle("SELECT filename FROM document WHERE $group_sql AND path = ?s", $file)->filename; $source = $groupPath . $file; $destination = work_secret($id) . "/$local_name"; delete_submissions_by_uid($uid, $group_id, $id, $destination); if (is_dir($source)) { $original_filename = $original_filename . '.zip'; $zip_filename = $webDir . 'courses/temp/' . safe_filename('zip'); zip_documents_directory($zip_filename, $file, $is_editor); $source = $zip_filename; } if (copy($source, "$workPath/$destination")) { Database::get()->query("INSERT INTO assignment_submit (uid, assignment_id, submission_date, submission_ip, file_path, file_name, comments, group_id, grade_comments) VALUES (?d, ?d, NOW(), '$_SERVER[REMOTE_ADDR]', ?s, ?s, ?s, ?d, ''", $uid, $id, $destination, $original_filename, $_POST['comments'], $group_id); $tool_content .="<div class='alert alert-success'>$langUploadSuccess <br>$m[the_file] \"$original_filename\" $m[was_submitted]<br> <a href='index.php?course=$course_code'>$langBack</a></div><br>"; } else { $tool_content .="<div class='alert alert-danger'>$langUploadError<br> <a href='index.php?course=$course_code'>$langBack</a></div><br>"; } }
function edit_assignment($id) { global $tool_content, $langBackAssignment, $langEditSuccess, $m, $langTheField, $langEditError, $course_code, $works_url, $course_id, $uid, $workPath, $langFormErrors; $v = new Valitron\Validator($_POST); $v->rule('required', array('title', 'max_grade')); $v->rule('numeric', array('max_grade')); $v->labels(array('title' => "{$langTheField} {$m['title']}", 'max_grade' => "{$langTheField} {$m['max_grade']}")); if ($v->validate()) { $row = Database::get()->querySingle("SELECT * FROM assignment WHERE id = ?d", $id); $title = $_POST['title']; $desc = purify($_POST['desc']); $deadline = trim($_POST['WorkEnd']) == FALSE ? '0000-00-00 00:00' : date('Y-m-d H:i', strtotime($_POST['WorkEnd'])); $late_submission = isset($_POST['late_submission']) && trim($_POST['WorkEnd']) != FALSE ? 1 : 0; $group_submissions = $_POST['group_submissions']; $max_grade = filter_input(INPUT_POST, 'max_grade', FILTER_VALIDATE_FLOAT); $assign_to_specific = filter_input(INPUT_POST, 'assign_to_specific', FILTER_VALIDATE_INT); $assigned_to = filter_input(INPUT_POST, 'ingroup', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY); $auto_judge = filter_input(INPUT_POST, 'auto_judge', FILTER_VALIDATE_INT); $auto_judge_scenarios = serialize($_POST['auto_judge_scenarios']); $lang = filter_input(INPUT_POST, 'lang'); if ($assign_to_specific == 1 && empty($assigned_to)) { $assign_to_specific = 0; } if (!isset($_POST['comments'])) { $comments = ''; } else { $comments = purify($_POST['comments']); } if (!isset($_FILES) || !$_FILES['userfile']['size']) { $_FILES['userfile']['name'] = ''; $_FILES['userfile']['tmp_name'] = ''; $filename = $row->file_path; $file_name = $row->file_name; } else { validateUploadedFile($_FILES['userfile']['name'], 2); if (preg_match('/\\.(ade|adp|bas|bat|chm|cmd|com|cpl|crt|exe|hlp|hta|' . 'inf|ins|isp|jse|lnk|mdb|mde|msc|msi|msp|mst|pcd|pif|reg|scr|sct|shs|' . 'shb|url|vbe|vbs|wsc|wsf|wsh)$/', $_FILES['userfile']['name'])) { $tool_content .= "<p class=\"caution\">{$langUnwantedFiletype}: {$_FILES['userfile']['name']}<br />"; $tool_content .= "<a href=\"{$_SERVER['SCRIPT_NAME']}?course={$course_code}&id={$id}\">{$langBack}</a></p><br />"; return; } $local_name = uid_to_name($uid); $am = Database::get()->querySingle("SELECT am FROM user WHERE id = ?d", $uid)->am; if (!empty($am)) { $local_name .= $am; } $local_name = greek_to_latin($local_name); $local_name = replace_dangerous_char($local_name); $secret = $row->secret_directory; $ext = get_file_extension($_FILES['userfile']['name']); $filename = "{$secret}/{$local_name}" . (empty($ext) ? '' : '.' . $ext); if (move_uploaded_file($_FILES['userfile']['tmp_name'], "{$workPath}/admin_files/{$filename}")) { @chmod("{$workPath}/admin_files/{$filename}", 0644); $file_name = $_FILES['userfile']['name']; } } Database::get()->query("UPDATE assignment SET title = ?s, description = ?s, deadline = ?t, late_submission = ?d, comments = ?s,\n group_submissions = ?d, max_grade = ?d, assign_to_specific = ?d, file_path = ?s, file_name = ?s,\n auto_judge = ?d, auto_judge_scenarios = ?s, lang = ?s WHERE course_id = ?d AND id = ?d", $title, $desc, $deadline, $late_submission, $comments, $group_submissions, $max_grade, $assign_to_specific, $filename, $file_name, $auto_judge, $auto_judge_scenarios, $lang, $course_id, $id); Database::get()->query("DELETE FROM assignment_to_specific WHERE assignment_id = ?d", $id); if ($assign_to_specific && !empty($assigned_to)) { if ($group_submissions == 1) { $column = 'group_id'; $other_column = 'user_id'; } else { $column = 'user_id'; $other_column = 'group_id'; } foreach ($assigned_to as $assignee_id) { Database::get()->query("INSERT INTO assignment_to_specific ({$column}, {$other_column}, assignment_id) VALUES (?d, ?d, ?d)", $assignee_id, 0, $id); } } Log::record($course_id, MODULE_ID_ASSIGN, LOG_MODIFY, array('id' => $id, 'title' => $title, 'description' => $desc, 'deadline' => $deadline, 'group' => $group_submissions)); \Session::Messages($langEditSuccess, 'alert-success'); redirect_to_home_page("modules/work/index.php?course={$course_code}"); } else { // $new_or_modify = isset($_GET['NewExercise']) ? "&NewExercise=Yes" : "&exerciseId=$_GET[exerciseId]&modifyExercise=yes"; Session::flashPost()->Messages($langFormErrors)->Errors($v->errors()); redirect_to_home_page("modules/work/index.php?course={$course_code}&id={$id}&choice=edit"); } }
Database::get()->query("UPDATE theme_options SET styles = ?s WHERE id = ?d", $serialized_data, $theme_id); redirect_to_home_page('modules/admin/theme_options.php'); } if (isset($_GET['export'])) { if (!$theme_id) redirect_to_home_page('modules/admin/theme_options.php'); // if default theme require_once 'include/pclzip/pclzip.lib.php'; require_once 'include/lib/fileUploadLib.inc.php'; if (!is_dir("courses/theme_data")) mkdir("courses/theme_data", 0755); $theme_options = Database::get()->querySingle("SELECT * FROM theme_options WHERE id = ?d", $theme_id); $theme_name = $theme_options->name; $styles = unserialize($theme_options->styles); $export_data = base64_encode(serialize($theme_options)); $export_data_file = 'courses/theme_data/theme_options.txt'; file_put_contents('courses/theme_data/theme_options.txt', $export_data); $filename = "courses/theme_data/".replace_dangerous_char(greek_to_latin($theme_name)).".zip"; $file_list = array("courses/theme_data/theme_options.txt"); if (isset($styles['bgImage'])) array_push($file_list, "courses/theme_data/$theme_id/$styles[bgImage]"); if (isset($styles['imageUpload'])) array_push($file_list, "courses/theme_data/$theme_id/$styles[imageUpload]"); if (isset($styles['imageUploadSmall'])) array_push($file_list, "courses/theme_data/$theme_id/$styles[imageUploadSmall]"); if (isset($styles['loginImg'])) array_push($file_list, "courses/theme_data/$theme_id/$styles[loginImg]"); $zip = new PclZip($filename); $zip->create($file_list, PCLZIP_OPT_REMOVE_PATH, 'courses/theme_data'); header("Content-Type: application/x-zip"); header("Content-Disposition: attachment; filename=$filename"); stop_output_buffering(); @readfile($filename); @unlink($filename); @unlink($export_data_file);
function convert_to_real_filename_common($p_event, &$p_header) { global $common_filenames; $p_header['stored_filename'] = substr(greek_to_latin($common_filenames[$p_header['filename']]), 1); return 1; }
function submit_work($id, $on_behalf_of = null) { global $course_id, $uid, $langOnBehalfOfGroupComment, $works_url, $langOnBehalfOfUserComment, $workPath, $langUploadSuccess, $langUploadError, $course_code, $langAutoJudgeEmptyFile, $langAutoJudgeInvalidFileType, $langAutoJudgeScenariosPassed; $connector = AutojudgeApp::getAutojudge(); $langExt = $connector->getSupportedLanguages(); $row = Database::get()->querySingle("SELECT id, title, group_submissions, submission_type, deadline, late_submission, CAST(UNIX_TIMESTAMP(deadline)-UNIX_TIMESTAMP(NOW()) AS SIGNED) AS time, auto_judge, auto_judge_scenarios, lang, max_grade FROM assignment WHERE course_id = ?d AND id = ?d", $course_id, $id); $auto_judge = $row->auto_judge; $auto_judge_scenarios = ($auto_judge == true) ? unserialize($row->auto_judge_scenarios) : null; $lang = $row->lang; $max_grade = $row->max_grade; $nav[] = $works_url; $nav[] = array('url' => "$_SERVER[SCRIPT_NAME]?id=$id", 'name' => q($row->title)); $submit_ok = FALSE; // Default do not allow submission if (isset($uid) && $uid) { // check if logged-in if ($GLOBALS['status'] == USER_GUEST) { // user is guest $submit_ok = FALSE; } else { // user NOT guest if (isset($_SESSION['courses']) && isset($_SESSION['courses'][$_SESSION['dbname']])) { // user is registered to this lesson if (($row->time < 0 && (int) $row->deadline && !$row->late_submission) and !$on_behalf_of) { $submit_ok = FALSE; // after assignment deadline } else { $submit_ok = TRUE; // before deadline } } else { //user NOT registered to this lesson $submit_ok = FALSE; } } } //checks for submission validity end here if ($submit_ok) { $success_msgs = array(); $error_msgs = array(); //Preparing variables $user_id = isset($on_behalf_of) ? $on_behalf_of : $uid; if ($row->group_submissions) { $group_id = isset($_POST['group_id']) ? intval($_POST['group_id']) : -1; $gids = user_group_info($on_behalf_of ? null : $user_id, $course_id); } else { $group_id = 0; } // If submission type is Online Text if($row->submission_type){ $filename = ''; $file_name = ''; $success_msgs[] = $langUploadSuccess; } else { // If submission type is File if ($row->group_submissions) { $local_name = isset($gids[$group_id]) ? greek_to_latin($gids[$group_id]) : ''; } else { $student_name = trim(uid_to_name($user_id)); $local_name = !empty($student_name)? $student_name : uid_to_name($user_id, 'username'); $am = Database::get()->querySingle("SELECT am FROM user WHERE id = ?d", $user_id)->am; if (!empty($am)) { $local_name .= $am; } $local_name = greek_to_latin($local_name); } $local_name = replace_dangerous_char($local_name); if (isset($on_behalf_of) and !isset($_FILES)) { $_FILES['userfile']['name'] = ''; $_FILES['userfile']['tmp_name'] = ''; $no_files = true; } else { $no_files = false; } $file_name = $_FILES['userfile']['name']; validateUploadedFile($file_name, 2); $secret = work_secret($row->id); $ext = get_file_extension($file_name); $filename = "$secret/$local_name" . (empty($ext) ? '' : '.' . $ext); if ($no_files or move_uploaded_file($_FILES['userfile']['tmp_name'], "$workPath/$filename")) { if ($no_files) { $filename = ''; } else { @chmod("$workPath/$filename", 0644); } $success_msgs[] = $langUploadSuccess; } else { $error_msgs[] = $langUploadError; Session::Messages($error_msgs, 'alert-danger'); redirect_to_home_page("modules/work/index.php?course=$course_code&id=$id"); } } $submit_ip = $_SERVER['REMOTE_ADDR']; $submission_text = isset($_POST['submission_text']) ? purify($_POST['submission_text']) : NULL; if (isset($on_behalf_of)) { if ($row->group_submissions) { $stud_comments = sprintf($langOnBehalfOfGroupComment, uid_to_name($uid), $gids[$group_id]); } else { $stud_comments = sprintf($langOnBehalfOfUserComment, uid_to_name($uid), uid_to_name($user_id)); } $grade_comments = $_POST['stud_comments']; $grade_valid = filter_input(INPUT_POST, 'grade', FILTER_VALIDATE_FLOAT); (isset($_POST['grade']) && $grade_valid!== false) ? $grade = $grade_valid : $grade = NULL; $grade_ip = $submit_ip; } else { if ($row->group_submissions) { if (array_key_exists($group_id, $gids)) { $del_submission_msg = delete_submissions_by_uid(-1, $group_id, $row->id); if (!empty($del_submission_msg)) { $success_msgs[] = $del_submission_msg; } } } else { $del_submission_msg = delete_submissions_by_uid($user_id, -1, $row->id); if (!empty($del_submission_msg)) { $success_msgs[] = $del_submission_msg; } } $stud_comments = $_POST['stud_comments']; $grade = NULL; $grade_comments = $grade_ip = ""; } if (!$row->group_submissions || array_key_exists($group_id, $gids)) { $data = array( $user_id, $row->id, $submit_ip, $filename, $file_name, $submission_text, $stud_comments, $grade, $grade_comments, $grade_ip, $group_id ); $sid = Database::get()->query("INSERT INTO assignment_submit (uid, assignment_id, submission_date, submission_ip, file_path, file_name, submission_text, comments, grade, grade_comments, grade_submission_ip, grade_submission_date, group_id) VALUES (?d, ?d, NOW(), ?s, ?s, ?s, ?s, ?s, ?f, ?s, ?s, NOW(), ?d)", $data)->lastInsertID; Log::record($course_id, MODULE_ID_ASSIGN, LOG_INSERT, array('id' => $sid, 'title' => $row->title, 'assignment_id' => $row->id, 'filepath' => $filename, 'filename' => $file_name, 'comments' => $stud_comments, 'group_id' => $group_id)); if ($row->group_submissions) { $group_id = Database::get()->querySingle("SELECT group_id FROM assignment_submit WHERE id = ?d", $sid)->group_id; $user_ids = Database::get()->queryArray("SELECT user_id FROM group_members WHERE group_id = ?d", $group_id); foreach ($user_ids as $user_id) { update_attendance_book($user_id, $row->id, GRADEBOOK_ACTIVITY_ASSIGNMENT); update_gradebook_book($user_id, $row->id, $grade/$row->max_grade, GRADEBOOK_ACTIVITY_ASSIGNMENT); } } else { $quserid = Database::get()->querySingle("SELECT uid FROM assignment_submit WHERE id = ?d", $sid)->uid; // update attendance book as well update_attendance_book($quserid, $row->id, GRADEBOOK_ACTIVITY_ASSIGNMENT); //update gradebook if needed update_gradebook_book($quserid, $id, $grade/$row->max_grade, GRADEBOOK_ACTIVITY_ASSIGNMENT); } if ($on_behalf_of and isset($_POST['email'])) { $email_grade = $_POST['grade']; $email_comments = "\n$auto_comments\n\n" . $_POST['stud_comments']; grade_email_notify($row->id, $sid, $email_grade, $email_comments); } } // Auto-judge: Send file to hackearth if(AutojudgeApp::getAutojudge()->isEnabled()) { if ($auto_judge && $ext === $langExt[$lang]) { $content = file_get_contents("$workPath/$filename"); // Run each scenario and count how many passed $auto_judge_scenarios_output = array( array( 'student_output'=> '', 'passed'=> 0, ) ); $passed = 0; $i = 0; $partial = 0; $errorsComment = ''; $weight_sum = 0; foreach($auto_judge_scenarios as $curScenario) { $input = new AutoJudgeConnectorInput(); $input->input = $curScenario['input']; $input->code = $content; $input->lang = $lang; $result = $connector->compile($input); // Check if we have compilation errors. if ($result->compileStatus !== $result::COMPILE_STATUS_OK) { // Write down the error message. $num = $i+1; $errorsComment = $result->compileStatus." ".$result->output."<br />"; $auto_judge_scenarios_output[$i]['passed'] = 0; } else { // Get all needed values to run the assertion. $auto_judge_scenarios_output[$i]['student_output'] = $result->output; $scenarioOutputExpectation = trim($curScenario['output']); $scenarionAssertion = $curScenario['assertion']; // Do it now. $assertionResult = doScenarioAssertion( $scenarionAssertion, $auto_judge_scenarios_output[$i]['student_output'], $scenarioOutputExpectation ); // Check if assertion passed. if ($assertionResult) { $passed++; $auto_judge_scenarios_output[$i]['passed'] = 1; $partial += $curScenario['weight']; } else { $num = $i+1; $auto_judge_scenarios_output[$i]['passed'] = 0; } } $weight_sum += $curScenario['weight']; $i++; } // 3 decimal digits precision $grade = round($partial / $weight_sum * $max_grade, 3); // allow an error of 0.001 if($max_grade - $grade <= 0.001) $grade = $max_grade; // Add the output as a comment $comment = $langAutoJudgeScenariosPassed.': '.$passed.'/'.count($auto_judge_scenarios); rtrim($errorsComment, '<br />'); if ($errorsComment !== '') { $comment .= '<br /><br />'.$errorsComment; } submit_grade_comments(array( 'assignment' => $id, 'submission' => $sid, 'grade' => $grade, 'comments' => $comment, 'email' => false, 'auto_judge_scenarios_output' => $auto_judge_scenarios_output, 'preventUiAlterations' => true, )); } else if ($auto_judge && $ext !== $langExt[$lang]) { if($lang == null) { die('Auto Judge is enabled but no language is selected'); } if($langExt[$lang] == null) { die('An unsupported language was selected. Perhaps platform-wide auto judge settings have been changed?'); } submit_grade_comments($id, $sid, 0, sprintf($langAutoJudgeInvalidFileType, $langExt[$lang], $ext), false, null, true); } } // End Auto-judge Session::Messages($success_msgs, 'alert-success'); redirect_to_home_page("modules/work/index.php?course=$course_code&id=$id"); } else { // not submit_ok Session::Messages($langExerciseNotPermit); redirect_to_home_page("modules/work/index.php?course=$course_code"); } }