diff --git a/lib/datalib.php b/lib/datalib.php index 2663acf56a2..cb938e0846b 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -1080,31 +1080,46 @@ function get_coursemodule_from_instance($modulename, $instance, $courseid) { } -function get_all_instances_in_course($modulename, $courseid, $sort="cw.section") { +function get_all_instances_in_course($modulename, $course) { /// Returns an array of all the active instances of a particular -/// module in a given course. Returns false on any errors. +/// module in a given course, sorted in the order they are defined +/// in the course. Returns false on any errors. +/// $course is a course object, this depends on an accurate $course->modinfo global $CFG; - // Hide non-visible instances from students - if (isteacher($courseid)) { - $showvisible = ""; - } else { - $showvisible = "AND cm.visible = '1'"; + if (!$modinfo = unserialize($course->modinfo)) { + return array(); } - return get_records_sql("SELECT m.*,cw.section,cm.id as coursemodule,cm.visible as visible + if (!$rawmods = get_records_sql("SELECT cm.id as coursemodule, m.*,cw.section,cm.visible as visible FROM {$CFG->prefix}course_modules cm, {$CFG->prefix}course_sections cw, {$CFG->prefix}modules md, {$CFG->prefix}$modulename m - WHERE cm.course = '$courseid' AND + WHERE cm.course = '$course->id' AND cm.instance = m.id AND cm.deleted = '0' AND cm.section = cw.id AND md.name = '$modulename' AND - md.id = cm.module $showvisible - ORDER BY $sort"); + md.id = cm.module")) { + return array(); + } + + // Hide non-visible instances from students + if (isteacher($course->id)) { + $invisible = -1; + } else { + $invisible = 0; + } + + foreach ($modinfo as $mod) { + if ($mod->mod == $modulename and $mod->visible > $invisible) { + $outputarray[] = $rawmods[$mod->cm]; + } + } + + return $outputarray; } diff --git a/mod/assignment/index.php b/mod/assignment/index.php index d15d8dfebd8..b071b423ca8 100644 --- a/mod/assignment/index.php +++ b/mod/assignment/index.php @@ -27,7 +27,7 @@ print_header("$course->shortname: $strassignments", "$course->fullname", "$navigation $strassignments", "", "", true, "", navmenu($course)); - if (! $assignments = get_all_instances_in_course("assignment", $course->id, "cw.section ASC")) { + if (! $assignments = get_all_instances_in_course("assignment", $course)) { notice("There are no assignments", "../../course/view.php?id=$course->id"); die; } diff --git a/mod/chat/index.php b/mod/chat/index.php index 2d9e0bd1687..f411248b967 100644 --- a/mod/chat/index.php +++ b/mod/chat/index.php @@ -30,7 +30,7 @@ /// Get all the appropriate data - if (! $chats = get_all_instances_in_course("chat", $course->id, "cw.section ASC")) { + if (! $chats = get_all_instances_in_course("chat", $course)) { notice("There are no chats", "../../course/view.php?id=$course->id"); die; } diff --git a/mod/choice/index.php b/mod/choice/index.php index afcff8d1c08..a4784c24be4 100644 --- a/mod/choice/index.php +++ b/mod/choice/index.php @@ -26,7 +26,7 @@ "$navigation $strchoices", "", "", true, "", navmenu($course)); - if (! $choices = get_all_instances_in_course("choice", $course->id, "cw.section ASC")) { + if (! $choices = get_all_instances_in_course("choice", $course)) { notice("There are no choices", "../../course/view.php?id=$course->id"); } diff --git a/mod/forum/discuss.php b/mod/forum/discuss.php index 88ebc0e72f3..a5f24a3fce3 100644 --- a/mod/forum/discuss.php +++ b/mod/forum/discuss.php @@ -90,7 +90,7 @@ } if (isteacher($course->id)) { // Popup menu to allow discussions to be moved to other forums - if ($forums = get_all_instances_in_course("forum", $course->id, "cw.section ASC")) { + if ($forums = get_all_instances_in_course("forum", $course)) { foreach ($forums as $courseforum) { if ($courseforum->id != $forum->id) { $url = "discuss.php?d=$discussion->id&move=$courseforum->id"; diff --git a/mod/forum/index.php b/mod/forum/index.php index 4414a6f528f..ffc6d7dea8f 100644 --- a/mod/forum/index.php +++ b/mod/forum/index.php @@ -115,7 +115,7 @@ array_unshift($table->head, ""); array_unshift($table->align, "center"); - if ($learningforums = get_all_instances_in_course("forum", $course->id)) { + if ($learningforums = get_all_instances_in_course("forum", $course)) { foreach ($learningforums as $key => $forum) { if ($forum->type == "news" or $forum->type == "social") { unset($learningforums[$key]); // Remove these diff --git a/mod/journal/index.php b/mod/journal/index.php index 9504d388795..838c8d63ea3 100644 --- a/mod/journal/index.php +++ b/mod/journal/index.php @@ -29,7 +29,7 @@ "", "", true, "", navmenu($course)); - if (! $journals = get_all_instances_in_course("journal", $course->id, "cw.section ASC")) { + if (! $journals = get_all_instances_in_course("journal", $course)) { notice("There are no journals", "../../course/view.php?id=$course->id"); die; } diff --git a/mod/quiz/index.php b/mod/quiz/index.php index 20b1e467424..b2153d63eaf 100644 --- a/mod/quiz/index.php +++ b/mod/quiz/index.php @@ -30,7 +30,7 @@ // Get all the appropriate data - if (! $quizzes = get_all_instances_in_course("quiz", $course->id, "cw.section ASC")) { + if (! $quizzes = get_all_instances_in_course("quiz", $course)) { notice("There are no quizzes", "../../course/view.php?id=$course->id"); die; } diff --git a/mod/resource/index.php b/mod/resource/index.php index f235040fd6f..e855f46536c 100644 --- a/mod/resource/index.php +++ b/mod/resource/index.php @@ -26,13 +26,7 @@ print_header("$course->shortname: $strresources", "$course->fullname", "$navigation $strresources", "", "", true, "", navmenu($course)); - if ($course->format == "weeks" or $course->format == "topics") { - $sortorder = "cw.section ASC"; - } else { - $sortorder = "m.timemodified DESC"; - } - - if (! $resources = get_all_instances_in_course("resource", $course->id, $sortorder)) { + if (! $resources = get_all_instances_in_course("resource", $course)) { notice("There are no resources", "../../course/view.php?id=$course->id"); exit; } diff --git a/mod/resource/lib.php b/mod/resource/lib.php index 8e54de4d0af..c72f4e49ca8 100644 --- a/mod/resource/lib.php +++ b/mod/resource/lib.php @@ -21,76 +21,6 @@ $RESOURCE_TYPE = array (REFERENCE => get_string("resourcetype1", "resource"), $RESOURCE_FRAME_SIZE = 130; -function resource_list_all_resources($courseid=0, $sort="name ASC", $recent=0) { - // Returns list of all resource links in an array of strings - - global $CFG, $USER; - - if ($courseid) { - if (! $course = get_record("course", "id", $courseid)) { - error("Could not find the specified course"); - } - - require_login($course->id); - - } else { - if (! $course = get_record("course", "category", 0)) { - error("Could not find a top-level course!"); - } - } - - if ($resources = get_all_instances_in_course("resource", $course->id, $sort)) { - foreach ($resources as $resource) { - $link = "wwwroot/mod/resource/view.php?id=$resource->coursemodule\">$resource->name"; - if ($USER->editing) { - $link .= "    - wwwroot/course/mod.php?delete=$resource->coursemodule\">wwwroot/pix/t/delete.gif\" BORDER=0 ALT=Delete> - wwwroot/course/mod.php?update=$resource->coursemodule\">wwwroot/pix/t/edit.gif\" BORDER=0 ALT=Update>"; - } - $links[] = $link; - } - } - - return $links; -} - - -function resource_user_outline($course, $user, $mod, $resource) { - if ($logs = get_records_select("log", "userid='$user->id' AND module='resource' - AND action='view' AND info='$resource->id'", "time ASC")) { - - $numviews = count($logs); - $lastlog = array_pop($logs); - - $result->info = get_string("numviews", "", $numviews); - $result->time = $lastlog->time; - - return $result; - } - return NULL; -} - - -function resource_user_complete($course, $user, $mod, $resource) { - global $CFG, $THEME; - - if ($logs = get_records_select("log", "userid='$user->id' AND module='resource' - AND action='view' AND info='$resource->id'", "time ASC")) { - $numviews = count($logs); - $lastlog = array_pop($logs); - - $strmostrecently = get_string("mostrecently"); - $strnumviews = get_string("numviews", "", $numviews); - - echo "$strnumviews - $strmostrecently ".userdate($lastlog->time); - - } else { - print_string("neverseen", "resource"); - } -} - function resource_add_instance($resource) { // Given an object containing all the necessary data, // (defined by the form in mod.html) this function @@ -134,4 +64,39 @@ function resource_delete_instance($id) { } +function resource_user_outline($course, $user, $mod, $resource) { + if ($logs = get_records_select("log", "userid='$user->id' AND module='resource' + AND action='view' AND info='$resource->id'", "time ASC")) { + + $numviews = count($logs); + $lastlog = array_pop($logs); + + $result->info = get_string("numviews", "", $numviews); + $result->time = $lastlog->time; + + return $result; + } + return NULL; +} + + +function resource_user_complete($course, $user, $mod, $resource) { + global $CFG, $THEME; + + if ($logs = get_records_select("log", "userid='$user->id' AND module='resource' + AND action='view' AND info='$resource->id'", "time ASC")) { + $numviews = count($logs); + $lastlog = array_pop($logs); + + $strmostrecently = get_string("mostrecently"); + $strnumviews = get_string("numviews", "", $numviews); + + echo "$strnumviews - $strmostrecently ".userdate($lastlog->time); + + } else { + print_string("neverseen", "resource"); + } +} + + ?> diff --git a/mod/survey/index.php b/mod/survey/index.php index 237c85afdd1..41249f76628 100644 --- a/mod/survey/index.php +++ b/mod/survey/index.php @@ -28,7 +28,7 @@ print_header("$course->shortname: $strsurveys", "$course->fullname", "$navigation $strsurveys", "", "", true, "", navmenu($course)); - if (! $surveys = get_all_instances_in_course("survey", $course->id, "cw.section ASC")) { + if (! $surveys = get_all_instances_in_course("survey", $course)) { notice("There are no surveys.", "../../course/view.php?id=$course->id"); } diff --git a/mod/workshop/index.php b/mod/workshop/index.php index 552f0f56bb5..304e5655f9a 100644 --- a/mod/workshop/index.php +++ b/mod/workshop/index.php @@ -26,7 +26,7 @@ print_header("$course->shortname: $strworkshops", "$course->fullname", "$navigation $strworkshops", "", "", true, "", navmenu($course)); - if (! $workshops = get_all_instances_in_course("workshop", $course->id, "cw.section ASC")) { + if (! $workshops = get_all_instances_in_course("workshop", $course)) { notice("There are no workshops", "../../course/view.php?id=$course->id"); die; }