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