Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d699cd1e54
Fetching contributors…

Cannot retrieve contributors at this time

file 302 lines (239 sloc) 11.831 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
<?PHP // $Id$

include_once("$CFG->dirroot/files/mimetypes.php");



function assignment_add_instance($assignment) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.

    $assignment->timemodified = time();
    
    $assignment->timedue = make_timestamp($assignment->dueyear, $assignment->duemonth, $assignment->dueday,
                                          $assignment->duehour, $assignment->dueminute);

    return insert_record("assignment", $assignment);
}


function assignment_update_instance($assignment) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.

    $assignment->timemodified = time();
    $assignment->timedue = make_timestamp($assignment->dueyear, $assignment->duemonth, $assignment->dueday,
                                          $assignment->duehour, $assignment->dueminute);
    $assignment->id = $assignment->instance;

    return update_record("assignment", $assignment);
}


function assignment_delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.

    if (! $assignment = get_record("assignment", "id", "$id")) {
        return false;
    }

    $result = true;

    if (! delete_records("assignment_submissions", "assignment", "$assignment->id")) {
        $result = false;
    }

    if (! delete_records("assignment", "id", "$assignment->id")) {
        $result = false;
    }

    return $result;
}

function assignment_cron () {
// Function to be run periodically according to the moodle cron
// Finds all assignment notifications that have yet to be mailed out, and mails them

    global $CFG;

    $cutofftime = time() - $CFG->maxeditingtime;

    if ($submissions = get_records_sql("SELECT s.*, a.course, a.name
FROM assignment_submissions s, assignment a
WHERE s.mailed = '0'
AND s.timemarked < '$cutofftime' AND s.timemarked > 0
AND s.assignment = a.id")) {
        $timenow = time();

        foreach ($submissions as $submission) {

            echo "Processing assignment submission $submission->id\n";

            if (! $user = get_record("user", "id", "$submission->user")) {
                echo "Could not find user $post->user\n";
                continue;
            }

            if (! $course = get_record("course", "id", "$submission->course")) {
                echo "Could not find course $submission->course\n";
                continue;
            }

            if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
                continue; // Not an active participant
            }

            if (! $teacher = get_record("user", "id", "$submission->teacher")) {
                echo "Could not find teacher $submission->teacher\n";
                continue;
            }

            if (! $mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id)) {
                echo "Could not find course module for assignment id $submission->assignment\n";
                continue;
            }

            $strassignments = get_string("modulenameplural", "assignment");
            $strassignment = get_string("modulename", "assignment");

            $postsubject = "$course->shortname: $strassignments: $submission->name";
            $posttext = "$course->shortname -> $strassignments -> $submission->name\n";
            $posttext .= "---------------------------------------------------------------------\n";
            $posttext .= "$teacher->firstname $teacher->lastname has posted some feedback on your\n";
            $posttext .= "assignment submission for '$submission->name'\n\n";
            $posttext .= "You can see it appended to your assignment submission:\n";
            $posttext .= " $CFG->wwwroot/mod/assignment/view.php?id=$mod->id\n";
            $posttext .= "---------------------------------------------------------------------\n";
            if ($user->mailformat == 1) { // HTML
                $posthtml = "<P><FONT FACE=sans-serif>".
              "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->".
"<A HREF=\"$CFG->wwwroot/mod/assignment/index.php?id=$course->id\">$strassignments</A> ->".
"<A HREF=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">$submission->name</A></FONT></P>";
$posthtml .= "<HR><FONT FACE=sans-serif>";
$posthtml .= "<P>$teacher->firstname $teacher->lastname has posted some feedback on your";
$posthtml .= " assignment submission for '<B>$submission->name</B>'</P>";
$posthtml .= "<P>You can see it <A HREF=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">";
$posthtml .= "appended to your assignment submission</A>.</P></FONT><HR>";
} else {
$posthtml = "";
}

if (! email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) {
echo "Error: assignment cron: Could not send out mail for id $submission->id to user $user->id ($user->email)\n";
}
if (! set_field("assignment_submissions", "mailed", "1", "id", "$submission->id")) {
echo "Could not update the mailed field for id $submission->id\n";
}
}
}

return true;
}


//////////////////////////////////////////////////////////////////////////////////////

function assignment_file_area_name($assignment, $user) {
// Creates a directory file name, suitable for make_upload_directory()
return "$assignment->course/assignment/$assignment->id/$user->id";
}

function assignment_file_area($assignment, $user) {
return make_upload_directory( assignment_file_area_name($assignment, $user) );
}

function assignment_get_submission($assignment, $user) {
return get_record_sql("SELECT * from assignment_submissions
                           WHERE assignment = '$assignment->id' AND user = '$user->id'");
}

function assignment_get_all_submissions($assignment) {
return get_records("assignment_submissions", "assignment", $assignment->id, "timemodified DESC");
}

function assignment_get_users_done($assignment) {
return get_records_sql("SELECT u.* FROM user u, user_students s, assignment_submissions a
                            WHERE s.course = '$assignment->course' AND s.user = u.id
                              AND u.id = a.user AND a.assignment = '$assignment->id'
                            ORDER BY a.timemodified DESC");
}

function assignment_print_difference($time) {
if ($time < 0) {
$timetext = get_string("late", "assignment", format_time($time));
return " (<FONT COLOR=RED>$timetext</FONT>)";
} else {
$timetext = get_string("early", "assignment", format_time($time));
return " ($timetext)";
}
}

function assignment_print_submission($assignment, $user, $submission, $teachers, $grades) {
global $THEME;

echo "\n<TABLE BORDER=1 CELLSPACING=0 valign=top cellpadding=10>";

echo "\n<TR>";
echo "\n<TD ROWSPAN=2 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
print_user_picture($user->id, $assignment->course, $user->picture);
echo "</TD>";
echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">$user->firstname $user->lastname";
if ($submission) {
echo "&nbsp;&nbsp;<FONT SIZE=1>".get_string("lastmodified").": ";
echo userdate($submission->timemodified);
echo assignment_print_difference($assignment->timedue - $submission->timemodified);
echo "</FONT>";
}
echo "</TR>";

echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
if ($submission) {
assignment_print_user_files($assignment, $user);
} else {
print_string("notsubmittedyet", "assignment");
}
echo "</TD></TR>";

if ($submission) {
echo "\n<TR>";
echo "<TD WIDTH=35 VALIGN=TOP>";
if (!$submission->teacher) {
$submission->teacher = $USER->id;
}
print_user_picture($submission->teacher, $assignment->course, $teachers[$submission->teacher]->picture);
echo "<TD BGCOLOR=\"$THEME->cellheading\">Teacher Feedback:";
choose_from_menu($grades, "g$submission->id", $submission->grade, get_string("grade")."...");
if ($submission->timemarked) {
echo "&nbsp;&nbsp;<FONT SIZE=1>".userdate($submission->timemarked)."</FONT>";
}
echo "<BR><TEXTAREA NAME=\"c$submission->id\" ROWS=6 COLS=60 WRAP=virtual>";
p($submission->comment);
echo "</TEXTAREA><BR>";
echo "</TD></TR>";
}
echo "</TABLE><BR CLEAR=ALL>\n";
}

function assignment_print_feedback($course, $submission) {
global $CFG, $THEME, $RATING;

if (! $teacher = get_record("user", "id", $submission->teacher)) {
error("Weird assignment error");
}

echo "\n<TABLE BORDER=1 CELLSPACING=0 valign=top cellpadding=10>";

echo "\n<TR>";
echo "\n<TD ROWSPAN=3 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
print_user_picture($teacher->id, $course->id, $teacher->picture);
echo "</TD>";
echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">$teacher->firstname $teacher->lastname";
echo "&nbsp;&nbsp;<FONT SIZE=2><I>".userdate($submission->timemarked)."</I>";
echo "</TR>";

echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";

echo "<P ALIGN=RIGHT><FONT SIZE=-1><I>";
if ($submission->grade) {
echo get_string("grade").": $submission->grade";
} else {
echo get_string("nograde");
}
echo "</I></FONT></P>";

echo text_to_html($submission->comment);
echo "</TD></TR></TABLE>";
}


function assignment_print_user_files($assignment, $user) {
// Arguments are objects

global $CFG;

$filearea = assignment_file_area_name($assignment, $user);

if ($basedir = assignment_file_area($assignment, $user)) {
if ($files = get_directory_list($basedir)) {
foreach ($files as $file) {
$icon = mimeinfo("icon", $file);
echo "<IMG SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 BORDER=0 ALT=\"File\">";
                echo "&nbsp;<A TARGET=\"uploadedfile\" HREF=\"$CFG->wwwroot/file.php/$filearea/$file\">$file</A>";
echo "<BR>";
}
}
}
}

function assignment_delete_user_files($assignment, $user, $exception) {
// Deletes all the user files in the assignment area for a user
// EXCEPT for any file named $exception

if ($basedir = assignment_file_area($assignment, $user)) {
if ($files = get_directory_list($basedir)) {
foreach ($files as $file) {
if ($file != $exception) {
unlink("$basedir/$file");
notify("Existing file '$file' has been deleted!");
}
}
}
}
}

function assignment_print_upload_form($assignment) {
// Arguments are objects

echo "<DIV ALIGN=CENTER>";
echo "<FORM ENCTYPE=\"multipart/form-data\" METHOD=\"POST\" ACTION=upload.php>";
    echo " <INPUT TYPE=hidden NAME=MAX_FILE_SIZE value=\"$assignment->maxfilesize\">";
echo " <INPUT TYPE=hidden NAME=id VALUE=\"$assignment->id\">";
echo " <INPUT NAME=\"newfile\" TYPE=\"file\" size=\"50\">";
    echo " <INPUT TYPE=submit NAME=save VALUE=\"".get_string("uploadthisfile")."\">";
    echo "</FORM>";
    echo "</DIV>";
}

?>
Something went wrong with that request. Please try again.