Cleaned things up somewhat and made it more generic
[moodle.git] / mod / resource / lib.php
CommitLineData
2a439ba7 1<?PHP // $Id$
2
3define("REFERENCE", "1");
4define("WEBPAGE", "2");
5define("UPLOADEDFILE","3");
6define("PLAINTEXT", "4");
7define("WEBLINK", "5");
8define("HTML", "6");
f7a5c8fa 9define("PROGRAM", "7");
2a439ba7 10
11$RESOURCE_TYPE = array (REFERENCE => get_string("resourcetype1", "resource"),
12 WEBPAGE => get_string("resourcetype2", "resource"),
13 UPLOADEDFILE => get_string("resourcetype3", "resource"),
14 PLAINTEXT => get_string("resourcetype4", "resource"),
15 WEBLINK => get_string("resourcetype5", "resource"),
f7a5c8fa 16 HTML => get_string("resourcetype6", "resource"),
17 PROGRAM => get_string("resourcetype7", "resource") );
2a439ba7 18
19function resource_list_all_resources($courseid=0, $sort="name ASC", $recent=0) {
20 // Returns list of all resource links in an array of strings
21
22 global $CFG, $USER;
23
24 if ($courseid) {
25 if (! $course = get_record("course", "id", $courseid)) {
26 error("Could not find the specified course");
27 }
28
29 require_login($course->id);
30
31 } else {
32 if (! $course = get_record("course", "category", 0)) {
33 error("Could not find a top-level course!");
34 }
35 }
36
37 if ($resources = get_all_instances_in_course("resource", $course->id, $sort)) {
38 foreach ($resources as $resource) {
39 $link = "<A HREF=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->coursemodule\">$resource->name</A>";
40 if ($USER->editing) {
41 $link .= "&nbsp; &nbsp;
42 <A HREF=\"$CFG->wwwroot/course/mod.php?delete=$resource->coursemodule\"><IMG
43 SRC=\"$CFG->wwwroot/pix/t/delete.gif\" BORDER=0 ALT=Delete></A>
44 <A HREF=\"$CFG->wwwroot/course/mod.php?update=$resource->coursemodule\"><IMG
45 SRC=\"$CFG->wwwroot/pix/t/edit.gif\" BORDER=0 ALT=Update></A>";
46 }
47 $links[] = $link;
48 }
49 }
50
51 return $links;
52}
53
54
55function resource_user_outline($course, $user, $mod, $resource) {
ebc3bd2b 56 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
57 AND action='view' AND info='$resource->id'", "time ASC")) {
2a439ba7 58
59 $numviews = count($logs);
60 $lastlog = array_pop($logs);
61
62 $result->info = get_string("numviews", "", $numviews);
63 $result->time = $lastlog->time;
64
65 return $result;
66 }
67 return NULL;
68}
69
70
71function resource_user_complete($course, $user, $mod, $resource) {
72 global $CFG, $THEME;
73
ebc3bd2b 74 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
75 AND action='view' AND info='$resource->id'", "time ASC")) {
2a439ba7 76 $numviews = count($logs);
77 $lastlog = array_pop($logs);
78
79 $strmostrecently = get_string("mostrecently");
80 $strnumviews = get_string("numviews", "", $numviews);
81
82 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
83
84 } else {
4282d7dd 85 print_string("neverseen", "resource");
2a439ba7 86 }
87}
88
89function resource_add_instance($resource) {
90// Given an object containing all the necessary data,
91// (defined by the form in mod.html) this function
92// will create a new instance and return the id number
93// of the new instance.
94
95 $resource->timemodified = time();
96
97 return insert_record("resource", $resource);
98}
99
100
101function resource_update_instance($resource) {
102// Given an object containing all the necessary data,
103// (defined by the form in mod.html) this function
104// will update an existing instance with new data.
105
106 $resource->id = $resource->instance;
107 $resource->timemodified = time();
108
109 return update_record("resource", $resource);
110}
111
112
113function resource_delete_instance($id) {
114// Given an ID of an instance of this module,
115// this function will permanently delete the instance
116// and any data that depends on it.
117
118 if (! $resource = get_record("resource", "id", "$id")) {
119 return false;
120 }
121
122 $result = true;
123
124 if (! delete_records("resource", "id", "$resource->id")) {
125 $result = false;
126 }
127
128 return $result;
129}
130
131
132?>