/** * validation * * @param xxx $data * @return xxx */ function validation($data, $files) { global $USER; // http://docs.moodle.org/en/Development:lib/formslib.php_Validation // Note: see "lang/en/error.php" for a list of common messages $errors = array(); // get the $files specified in the form $usercontext = hotpot_get_context(CONTEXT_USER, $USER->id); $fs = get_file_storage(); $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['sourceitemid'], 'sortorder, id', 0); // files only, no dirs // check we have at least one file // (and set mainfile marker, if necessary) if (empty($files)) { $errors['sourceitemid'] = get_string('required'); // $errors['sourceitemid'] = get_string('nofile', 'error'); } else { $mainfile = false; foreach ($files as $file) { if ($file->get_sortorder() == 1) { $mainfile = true; break; } } if (!$mainfile) { $file = reset($files); // first file in the list file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename(), 1); } } // studentfeedbackurl if ($data['studentfeedback'] == hotpot::FEEDBACK_WEBPAGE || $data['studentfeedback'] == hotpot::FEEDBACK_FORMMAIL) { if (empty($data['studentfeedbackurl']) || !preg_match('/^https?:\\/\\/.+/', $data['studentfeedbackurl'])) { // empty or invalid url $errors['studentfeedback_elements'] = get_string('invalidurl', 'error'); } } return $errors; }
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. /** * mod/hotpot/tools/index.php * * @package mod-hotpot * @copyright 2010 Gordon Bateson <*****@*****.**> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/config.php'; require_once $CFG->dirroot . '/mod/hotpot/lib.php'; require_login(SITEID); require_capability('moodle/site:config', hotpot_get_context(CONTEXT_SYSTEM)); // $SCRIPT is set by initialise_fullme() in "lib/setuplib.php" // it is the path below $CFG->wwwroot of this script $PAGE->set_url($CFG->wwwroot . $SCRIPT); // set title $title = 'HotPot Utilities index'; $PAGE->set_title($title); $PAGE->set_heading($title); $PAGE->set_pagelayout('admin'); echo $OUTPUT->header(); echo $OUTPUT->box_start(); // get path to this directory $dirname = dirname($SCRIPT); $dirpath = $CFG->dirroot . $dirname; echo html_writer::start_tag('ul') . "\n"; $items = new DirectoryIterator($dirpath);
/** * get_feedback_teachers * * @return xxx */ function get_feedback_teachers() { $context = hotpot_get_context(CONTEXT_COURSE, $this->hotpot->source->courseid); $teachers = get_users_by_capability($context, 'mod/hotpot:reviewallattempts'); $details = array(); if (isset($teachers) && count($teachers)) { if ($this->hotpot->studentfeedback == hotpot::FEEDBACK_MOODLEMESSAGING) { $detail = 'id'; } else { $detail = 'email'; } foreach ($teachers as $teacher) { $details[] = "new Array('" . addslashes_js(fullname($teacher)) . "', '" . addslashes_js($teacher->{$detail}) . "')"; } } if ($details = implode(', ', $details)) { return 'new Array(' . $details . ')'; } else { return ''; // no teachers } }
/** * Returns the stored_file object for this HotPot's source file * * @return stored_file */ public function get_sourcefile() { global $CFG, $DB; $fs = get_file_storage(); $filename = basename($this->sourcefile); $filepath = dirname($this->sourcefile); // require leading and trailing slash on $filepath if (substr($filepath, 0, 1) == '/' && substr($filepath, -1) == '/') { // do nothing - $filepath is valid } else { // fix filepath - shouldn't happen !! // maybe leftover from a messy upgrade if ($filepath == '.' || $filepath == '') { $filepath = '/'; } else { $filepath = '/' . ltrim($filepath, '/'); $filepath = rtrim($filepath, '/') . '/'; } $this->sourcefile = $filepath . $filename; $DB->set_field('hotpot', 'sourcefile', $this->sourcefile, array('id' => $this->id)); } if ($file = $fs->get_file($this->context->id, 'mod_hotpot', 'sourcefile', 0, $filepath, $filename)) { return $file; } // the source file is missing, probably this HotPot // has recently been upgraded/imported from Moodle 1.9 // so we are going to try to create the missing stored file $file_record = array('contextid' => $this->context->id, 'component' => 'mod_hotpot', 'filearea' => 'sourcefile', 'sortorder' => 1, 'itemid' => 0, 'filepath' => $filepath, 'filename' => $filename); $coursecontext = hotpot_get_context(CONTEXT_COURSE, $this->course->id); $filehash = sha1('/' . $coursecontext->id . '/course/legacy/0' . $filepath . $filename); if ($file = $fs->get_file_by_hash($filehash)) { // file exists in legacy course files if ($file = $fs->create_file_from_storedfile($file_record, $file)) { return $file; } } $oldfilepath = $CFG->dataroot . '/' . $this->course->id . $filepath . $filename; if (file_exists($oldfilepath)) { // file exists on server's filesystem if ($file = $fs->create_file_from_pathname($file_record, $oldfilepath)) { return $file; } } // source file not found - shouldn't happen !! throw new moodle_exception('sourcefilenotfound', 'hotpot', '', $this->sourcefile); }
/** * hotpot_update_events * * @param xxx $hotpot (passed by reference) * @param xxx $eventids (passed by reference) * @param xxx $delete */ function hotpot_update_events(&$hotpot, &$eventids, $delete) { global $CFG, $DB; require_once $CFG->dirroot . '/calendar/lib.php'; static $stropens = ''; static $strcloses = ''; static $maxduration = null; // check to see if this user is allowed // to manage calendar events in this course $capability = 'moodle/calendar:manageentries'; if (has_capability($capability, hotpot_get_context(CONTEXT_SYSTEM))) { $can_manage_events = true; // site admin } else { if (has_capability($capability, hotpot_get_context(CONTEXT_COURSE, $hotpot->course))) { $can_manage_events = true; // course admin/teacher } else { $can_manage_events = false; // not allowed to add/edit calendar events !! } } // don't check calendar capabiltiies // whwne adding or updating events $checkcapabilties = false; // cache text strings and max duration (first time only) if (is_null($maxduration)) { if (isset($CFG->hotpot_maxeventlength)) { $maxeventlength = $CFG->hotpot_maxeventlength; } else { $maxeventlength = 5; // 5 days is default } // set $maxduration (secs) from $maxeventlength (days) $maxduration = $maxeventlength * 24 * 60 * 60; $stropens = get_string('activityopens', 'hotpot'); $strcloses = get_string('activitycloses', 'hotpot'); } // array to hold events for this hotpot $events = array(); // only setup calendar events, // if this user is allowed to if ($can_manage_events) { // set duration if ($hotpot->timeclose && $hotpot->timeopen) { $duration = max(0, $hotpot->timeclose - $hotpot->timeopen); } else { $duration = 0; } if ($duration > $maxduration) { // long duration, two events $events[] = (object) array('name' => $hotpot->name . ' (' . $stropens . ')', 'eventtype' => 'open', 'timestart' => $hotpot->timeopen, 'timeduration' => 0); $events[] = (object) array('name' => $hotpot->name . ' (' . $strcloses . ')', 'eventtype' => 'close', 'timestart' => $hotpot->timeclose, 'timeduration' => 0); } else { if ($duration) { // short duration, just a single event if ($duration < DAYSECS) { // less than a day (1:07 p.m.) $fmt = get_string('strftimetime'); } else { if ($duration < WEEKSECS) { // less than a week (Thu, 13:07) $fmt = get_string('strftimedaytime'); } else { if ($duration < YEARSECS) { // more than a week (2 Feb, 13:07) $fmt = get_string('strftimerecent'); } else { // more than a year (Thu, 2 Feb 2012, 01:07 pm) $fmt = get_string('strftimerecentfull'); } } } $events[] = (object) array('name' => $hotpot->name . ' (' . userdate($hotpot->timeopen, $fmt) . ' - ' . userdate($hotpot->timeclose, $fmt) . ')', 'eventtype' => 'open', 'timestart' => $hotpot->timeopen, 'timeduration' => $duration); } else { if ($hotpot->timeopen) { // only an open date $events[] = (object) array('name' => $hotpot->name . ' (' . $stropens . ')', 'eventtype' => 'open', 'timestart' => $hotpot->timeopen, 'timeduration' => 0); } else { if ($hotpot->timeclose) { // only a closing date $events[] = (object) array('name' => $hotpot->name . ' (' . $strcloses . ')', 'eventtype' => 'close', 'timestart' => $hotpot->timeclose, 'timeduration' => 0); } } } } } // cache description and visiblity (saves doing it twice for long events) if (empty($hotpot->entrytext)) { $description = ''; } else { $description = $hotpot->entrytext; } $visible = instance_is_visible('hotpot', $hotpot); foreach ($events as $event) { $event->groupid = 0; $event->userid = 0; $event->courseid = $hotpot->course; $event->modulename = 'hotpot'; $event->instance = $hotpot->id; $event->description = $description; $event->visible = $visible; if (count($eventids)) { $event->id = array_shift($eventids); $calendarevent = calendar_event::load($event->id); $calendarevent->update($event, $checkcapabilties); } else { calendar_event::create($event, $checkcapabilties); } } // delete surplus events, if required // (no need to check capabilities here) if ($delete) { while (count($eventids)) { $id = array_shift($eventids); $event = calendar_event::load($id); $event->delete(); } } }
/** * hotpot_add_to_log * * @param integer $courseid * @param string $module name e.g. "hotpot" * @param string $action * @param string $url (optional, default='') * @param string $info (optional, default='') often a hotpot id * @param string $cmid (optional, default=0) * @param integer $userid (optional, default=0) */ function hotpot_add_to_log($courseid, $module, $action, $url = '', $info = '', $cmid = 0, $userid = 0) { global $DB, $PAGE; // detect new event API (Moodle >= 2.6) if (function_exists('get_log_manager')) { // map old $action to new $eventname switch ($action) { case 'attempt': $eventname = 'attempt_started'; break; case 'report': $eventname = 'report_viewed'; break; case 'review': $eventname = 'attempt_reviewed'; break; case 'submit': $eventname = 'attempt_submitted'; break; case 'view': $eventname = 'course_module_viewed'; break; case 'index': // legacy $action // legacy $action case 'view all': $eventname = 'course_module_instance_list_viewed'; break; default: $eventname = $action; } $classname = '\\mod_hotpot\\event\\' . $eventname; if (class_exists($classname)) { $context = null; $course = null; $hotpot = null; $params = null; $objectid = 0; if ($action == 'index' || $action == 'view all') { // course context if (isset($PAGE->course) && $PAGE->course->id == $courseid) { // normal Moodle use $context = $PAGE->context; $course = $PAGE->course; } else { if ($courseid) { // Moodle upgrade $context = hotpot_get_context(CONTEXT_COURSE, $courseid); $course = $DB->get_record('course', array('id' => $courseid)); } } if ($context) { $params = array('context' => $context); } } else { // course module context if (isset($PAGE->cm) && $PAGE->cm->id == $cmid) { // normal Moodle use $objectid = $PAGE->cm->instance; $context = $PAGE->context; $course = $PAGE->course; $hotpot = $PAGE->activityrecord; } else { if ($cmid) { // Moodle upgrade $objectid = $DB->get_field('course_modules', 'instance', array('id' => $cmid)); $context = hotpot_get_context(CONTEXT_MODULE, $cmid); $course = $DB->get_record('course', array('id' => $courseid)); $hotpot = $DB->get_record('hotpot', array('id' => $objectid)); } } if ($context && $objectid) { $params = array('context' => $context, 'objectid' => $objectid); } } if ($params) { if ($userid) { $params['relateduserid'] = $userid; } // use call_user_func() to prevent syntax error in PHP 5.2.x $event = call_user_func(array($classname, 'create'), $params); if ($course) { $event->add_record_snapshot('course', $course); } if ($hotpot) { $event->add_record_snapshot('hotpot', $hotpot); } $event->trigger(); } } } else { if (function_exists('add_to_log')) { // Moodle <= 2.5 add_to_log($courseid, $module, $action, $url, $info, $cmid, $userid); } } }