Initial import

This commit is contained in:
2008-03-02 16:04:34 -03:00
commit 5e4951fa47
798 changed files with 59730 additions and 0 deletions

66
app/models/attachment.rb Normal file
View File

@@ -0,0 +1,66 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
require 'fileutils.rb'
class Attachment < ActiveRecord::Base
belongs_to :course
generate_validations
acts_as_paranoid
# Atributo virtual file
def file=(new_file)
@tmp_file = new_file
self.size = new_file.size
end
# Limpa o nome do arquivo
protected
def sanitize(filename)
filename = File.basename(filename)
filename.gsub(/[^\w\.\-]/, '_')
end
# Verifica se o arquivo é válido
def validate
if @tmp_file
errors.add("file") if @tmp_file.size == 0
errors.add("file", "is too large"[]) if @tmp_file.size > App.max_upload_file_size
else
# Caso o objeto possua id, significa que ele já está no banco de dados.
# Um arquivo em branco, entao, não é inválido: significa que a pessoa só quer
# modificar a descrição, ou algo assim..
errors.add("file", "is needed"[]) if not self.id
end
end
# Salva o arquivo fisicamente no HD
def after_save
@file_path = "#{RAILS_ROOT}/public/upload/#{course.id}/#{self.id}"
FileUtils.mkdir_p(File.dirname(@file_path))
if @tmp_file
logger.debug("Saving #{self.id}")
File.open(@file_path, "wb") do |f|
f.write(@tmp_file.read)
end
end
end
# Deleta o arquivo
#def after_destroy
# @file_path = "#{RAILS_ROOT}/public/upload/#{course.id}/#{self.id}"
# File.delete(@file_path) if File.exists?(@file_path)
#end
end

56
app/models/course.rb Normal file
View File

@@ -0,0 +1,56 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
class Course < ActiveRecord::Base
has_many :attachments, :order => "file_name"
has_many :wiki_pages, :order => "position"
has_many :shoutbox_messages,
:class_name => 'CourseShoutboxMessage',
:foreign_key => "receiver_id",
:order => 'id desc'
has_many :news,
:class_name => 'News',
:foreign_key => "receiver_id",
:order => 'id desc'
has_many :events, :order => "date asc, time asc"
has_many :log_entries, :order => "created_at desc"
generate_validations
validates_uniqueness_of :short_name
validates_format_of :short_name, :with => /^[^0-9]/
def after_create
App.inital_wiki_pages.each do |page_title|
wiki_page = WikiPage.new(:title => page_title, :version => 1, :content => App.initial_wiki_page_content)
self.wiki_pages << wiki_page
end
end
def after_destroy
associations = [:attachments, :wiki_pages, :shoutbox_messages, :news, :events]
associations.each do |assoc|
send("#{assoc}").each do |record|
record.destroy
end
end
end
def to_param
self.short_name
end
end

33
app/models/event.rb Normal file
View File

@@ -0,0 +1,33 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
class Event < ActiveRecord::Base
acts_as_paranoid
generate_validations
def Event.to_ical(courses)
cal = Icalendar::Calendar.new
courses.each do |course|
course.events.each do |user_event|
event = Icalendar::Event.new
event.start = user_event.date
event.end = user_event.date
event.summary = "#{course.short_name}: #{user_event.title}"
event.description = user_event.description
cal.add(event)
end
end
return cal.to_ical
end
end

24
app/models/log_entry.rb Normal file
View File

@@ -0,0 +1,24 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
class LogEntry < ActiveRecord::Base
belongs_to :user
belongs_to :course
def reversible?() false end
end
require 'log_entry/attachment_log_entry.rb'
require 'log_entry/event_log_entry.rb'
require 'log_entry/news_log_entry.rb'
require 'log_entry/wiki_log_entry.rb'

View File

@@ -0,0 +1,35 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
class AttachmentLogEntry < LogEntry
def attachment
Attachment.find_with_deleted(target_id)
end
end
class AttachmentDeleteLogEntry < AttachmentLogEntry
def reversible?()
a = Attachment.find_with_deleted(target_id)
a.deleted_at != nil
end
def undo!(current_user)
a = Attachment.find_with_deleted(target_id)
a.update_attribute(:deleted_at, nil)
AttachmentRestoreLogEntry.create!(:target_id => a.id, :user_id => current_user.id,
:course => a.course)
end
end
class AttachmentEditLogEntry < AttachmentLogEntry; end
class AttachmentCreateLogEntry < AttachmentLogEntry; end
class AttachmentRestoreLogEntry < AttachmentLogEntry; end

View File

@@ -0,0 +1,35 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
class EventLogEntry < LogEntry
def event
Event.find_with_deleted(target_id)
end
end
class EventDeleteLogEntry < EventLogEntry
def reversible?()
e = Event.find_with_deleted(target_id)
e.deleted_at != nil
end
def undo!(current_user)
e = Event.find_with_deleted(target_id)
e.update_attribute(:deleted_at, nil)
EventRestoreLogEntry.create!(:target_id => e.id, :user_id => current_user.id,
:course => e.course)
end
end
class EventEditLogEntry < EventLogEntry; end
class EventCreateLogEntry < EventLogEntry; end
class EventRestoreLogEntry < EventLogEntry; end

View File

@@ -0,0 +1,35 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
class NewsLogEntry < LogEntry
def news
News.find_with_deleted(target_id)
end
end
class NewsDeleteLogEntry < NewsLogEntry
def reversible?()
n = News.find_with_deleted(target_id)
n.deleted_at != nil
end
def undo!(current_user)
n = News.find_with_deleted(target_id)
n.update_attribute(:deleted_at, nil)
NewsRestoreLogEntry.create!(:target_id => n.id, :user_id => current_user.id,
:course => n.course)
end
end
class NewsEditLogEntry < NewsLogEntry; end
class NewsCreateLogEntry < NewsLogEntry; end
class NewsRestoreLogEntry < NewsLogEntry; end

View File

@@ -0,0 +1,43 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
class WikiLogEntry < LogEntry
def wiki_page
w = WikiPage.find_with_deleted(target_id)
w.revert_to(version)
return w
end
end
class WikiEditLogEntry < WikiLogEntry
validates_presence_of :version
end
class WikiDeleteLogEntry < WikiLogEntry
def reversible?()
w = WikiPage.find_with_deleted(target_id)
w.deleted_at != nil
end
def undo!(current_user)
w = WikiPage.find_with_deleted(target_id)
w.update_attribute(:deleted_at, nil)
w.position = w.course.wiki_pages.maximum(:position) + 1
w.save!
WikiRestoreLogEntry.create!(:target_id => w.id, :user_id => current_user.id,
:course => w.course)
end
end
class WikiCreateLogEntry < WikiLogEntry; end
class WikiRestoreLogEntry < WikiLogEntry; end

39
app/models/message.rb Normal file
View File

@@ -0,0 +1,39 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
class Message < ActiveRecord::Base
acts_as_paranoid
belongs_to :user, :foreign_key => "sender_id"
end
class PrivateMessage < Message
end
class News < Message
validates_presence_of :title
belongs_to :course, :foreign_key => "receiver_id"
end
class ShoutboxMessage < Message
end
class CourseShoutboxMessage < ShoutboxMessage
end
class UserShoutboxMessage < ShoutboxMessage
end

View File

@@ -0,0 +1,25 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
class Notifications < ActionMailer::Base
def forgot_password(to, login, pass, sent_at = Time.now)
@subject = "Your password is ..."
@body['login']=login
@body['pass']=pass
@recipients = to
@from = 'support@yourdomain.com'
@sent_on = sent_at
@headers = {}
end
end

108
app/models/user.rb Normal file
View File

@@ -0,0 +1,108 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
require 'digest/sha1'
class User < ActiveRecord::Base
has_and_belongs_to_many :courses, :order => 'full_name'
validates_length_of :login, :within => 3..40
validates_length_of :name, :within => 3..40
validates_length_of :display_name, :within => 3..40
validates_presence_of :login, :email, :display_name
validates_uniqueness_of :login, :email, :display_name
validates_format_of :display_name, :with => /^[^0-9]/
validates_format_of :email,
:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
attr_protected :id, :salt
attr_accessor :password, :password_confirmation
has_many :shoutbox_messages,
:class_name => 'UserShoutboxMessage',
:foreign_key => "receiver_id",
:order => 'id desc'
def User.find_by_login_and_pass(login, pass)
user = find(:first, :conditions => [ "login = ?", login ])
return (!user.nil? and User.encrypt(pass, user.salt) == user.hashed_password) ? user : nil
end
def to_xml(options = {})
options[:indent] ||= 2
xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
xml.instruct! unless options[:skip_instruct]
xml.user do
xml.id self.id
xml.name self.name
xml.created_at self.created_at
xml.last_seen self.last_seen
xml.description self.description
end
end
# Gera uma nova senha, e a envia por email.
def send_new_password
new_pass = User.random_string(10)
@password = @password_confirmation = new_pass
save
Notifications.deliver_forgot_password(self.email, self.login, new_pass)
end
def reset_login_key
self.login_key = Digest::SHA1.hexdigest(Time.now.to_s + password.to_s + rand(123456789).to_s).to_s
end
def reset_login_key!
reset_login_key
save!
self.login_key
end
def to_param
self.login
end
protected
def validate
if new_record?
errors.add_on_blank :password
errors.add_on_blank :password_confirmation
end
if !@password.blank?
errors.add(:password_confirmation) if @password_confirmation.blank? or @password != @password_confirmation
errors.add(:password, 'é muito curta') if !(5..40).include?(@password.length)
end
end
def before_save
self.salt = User.random_string(10) if !self.salt?
self.hashed_password = User.encrypt(@password, self.salt) if !@password.blank?
end
def self.encrypt(pass, salt)
Digest::SHA1.hexdigest(pass + salt)
end
def self.random_string(len)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
newpass = ""
1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
return newpass
end
end

59
app/models/wiki_page.rb Normal file
View File

@@ -0,0 +1,59 @@
# Engenharia de Software 2007.1
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
require 'acts_as_versioned'
require 'tempfile'
class WikiPage < ActiveRecord::Base
belongs_to :course
belongs_to :user
generate_validations
validates_uniqueness_of :title, :scope => :course_id
validates_format_of :title, :with => /^[^0-9]/
acts_as_paranoid
acts_as_list :scope => 'course_id = #{course_id}'
acts_as_versioned :if_changed => [ :content, :description, :title ]
self.non_versioned_fields << 'position'
def to_html(text = self.content)
return BlueCloth.new(text).to_html
end
def to_param
self.title
end
def WikiPage.diff(from, to)
# Cria um arquivo com o conteudo da versao antiga
original_content_file = Tempfile.new("old")
original_content_file << from.content << "\n"
original_content_file.flush
# Cria um arquivo com o conteudo da versao nova
new_content_file = Tempfile.new("new")
new_content_file << to.content << "\n"
new_content_file.flush
# Calcula as diferencas
diff = `diff -u #{original_content_file.path()} #{new_content_file.path()}`
# Fecha os arquivos temporarios
new_content_file.close!
original_content_file.close!
return diff
end
end